2021-10-18 03:49:18

by Dongliang Mu

[permalink] [raw]
Subject: Any tracing mechanism can track the executed instructions of a user process in the kernel?

Hi all,

I am writing to kindly ask one question: is there any tracing
mechanism in Linux kernel that can trace all the executed instructions
of a user process? If this user process is run on different
processors, traces of this process on different processors should be
also recorded.

Any comment is welcome.

--
My best regards to you.

No System Is Safe!
Dongliang Mu


2021-10-18 07:10:25

by Pavel Skripkin

[permalink] [raw]
Subject: Re: Any tracing mechanism can track the executed instructions of a user process in the kernel?

On 10/18/21 05:45, Dongliang Mu wrote:
> Hi all,
>
> I am writing to kindly ask one question: is there any tracing
> mechanism in Linux kernel that can trace all the executed instructions
> of a user process? If this user process is run on different
> processors, traces of this process on different processors should be
> also recorded.
>
>

I think, kernel is not supposed to do that kind of things. I mean, there
is no such wrapper in the kernel to do this task, AFAIK.

You have an access to all system calls, so you can implement your own
gdb in the kernel via ptrace() :)


If you need only report about instructions, you can call `perf record` +
`perf report` via call_usermodehelper() and somehow parse the output of
these helpers.


> Any comment is welcome.
>


With regards,
Pavel Skripkin

2021-10-18 08:43:52

by Dongliang Mu

[permalink] [raw]
Subject: Re: Any tracing mechanism can track the executed instructions of a user process in the kernel?

On Mon, Oct 18, 2021 at 4:07 PM FMDF <[email protected]> wrote:
>
> On Mon, 18 Oct 2021, 04:46 Dongliang Mu, <[email protected]> wrote:
>>
>> Hi all,
>>
>> I am writing to kindly ask one question: is there any tracing
>> mechanism in Linux kernel that can trace all the executed instructions
>> of a user process? If this user process is run on different
>> processors, traces of this process on different processors should be
>> also recorded.
>
>
> You've not explained in detail what is the purpose of the tracing that you want to do. Missing this information I can only provide you a list of links to various tools and methods. Take a look by yourself and try to figure out what is better suited for your needs...

I want to log all the executed instructions of a user process (e.g.,
poc.c in syzkaller) in the kernel mode and then would like to leverage
backward analysis to capture the root cause of kernel panic/crash.

Therefore, I need the instruction-level tracing mechanisms or tools.

>
> https://lwn.net/Kernel/Index/#Development_tools-Kernel_tracing
> trace-cmd: https://trace-cmd.org

Ftrace is working at the function level. Therefore, ftrace and tracers
based on ftrace are not suitable for my scenario.

> perf ftrace: <Linux source>/tools/perf/Documentation/perf-trace.txt
> perf-tools: https://github.com/brendangregg/perf-tools

I am not familiar with the perf tool and cannot verify if it works for
my scenario.

>
> In particular, the "function_graph" tracer of ftrace seems to be suited for your purposes:
>
> https://man7.org/linux/man-pages/man1/trace-cmd.1.html
> https://man7.org/linux/man-pages/man1/trace-cmd-record.1.html
> https://man7.org/linux/man-pages/man1/trace-cmd-report.1.html
>
> Obviously, gdb (and friends) is still invaluable (but it may be difficult to use, depending on your knowledge and experience, so use it if you really know that you need it):
>
> https://www.kernel.org/doc/html/latest/dev-tools/gdb-kernel-debugging.html
> https://www.kernel.org/doc/html/latest/dev-tools/kgdb.html
>
> Regards,
>
> Fabio M. De Francesco
>
>>
>> Any comment is welcome.
>>
>> --
>> My best regards to you.
>>
>> No System Is Safe!
>> Dongliang Mu
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> [email protected]
>> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

2021-10-18 08:50:08

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: Any tracing mechanism can track the executed instructions of a user process in the kernel?

On Mon, Oct 18, 2021 at 04:41:14PM +0800, Dongliang Mu wrote:
> On Mon, Oct 18, 2021 at 4:07 PM FMDF <[email protected]> wrote:
> >
> > On Mon, 18 Oct 2021, 04:46 Dongliang Mu, <[email protected]> wrote:
> >>
> >> Hi all,
> >>
> >> I am writing to kindly ask one question: is there any tracing
> >> mechanism in Linux kernel that can trace all the executed instructions
> >> of a user process? If this user process is run on different
> >> processors, traces of this process on different processors should be
> >> also recorded.
> >
> >
> > You've not explained in detail what is the purpose of the tracing that you want to do. Missing this information I can only provide you a list of links to various tools and methods. Take a look by yourself and try to figure out what is better suited for your needs...
>
> I want to log all the executed instructions of a user process (e.g.,
> poc.c in syzkaller) in the kernel mode and then would like to leverage
> backward analysis to capture the root cause of kernel panic/crash.
>
> Therefore, I need the instruction-level tracing mechanisms or tools.

Then use a userspace debugger like gdb, that is what they are designed
for.

good luck!

greg k-h

2021-10-18 09:20:32

by Dongliang Mu

[permalink] [raw]
Subject: Re: Any tracing mechanism can track the executed instructions of a user process in the kernel?

+Brendan Gregg

On Mon, Oct 18, 2021 at 4:41 PM Dongliang Mu <[email protected]> wrote:
>
> On Mon, Oct 18, 2021 at 4:07 PM FMDF <[email protected]> wrote:
> >
> > On Mon, 18 Oct 2021, 04:46 Dongliang Mu, <[email protected]> wrote:
> >>
> >> Hi all,
> >>
> >> I am writing to kindly ask one question: is there any tracing
> >> mechanism in Linux kernel that can trace all the executed instructions
> >> of a user process? If this user process is run on different
> >> processors, traces of this process on different processors should be
> >> also recorded.
> >
> >
> > You've not explained in detail what is the purpose of the tracing that you want to do. Missing this information I can only provide you a list of links to various tools and methods. Take a look by yourself and try to figure out what is better suited for your needs...
>
> I want to log all the executed instructions of a user process (e.g.,
> poc.c in syzkaller) in the kernel mode and then would like to leverage
> backward analysis to capture the root cause of kernel panic/crash.
>
> Therefore, I need the instruction-level tracing mechanisms or tools.
>
> >
> > https://lwn.net/Kernel/Index/#Development_tools-Kernel_tracing
> > trace-cmd: https://trace-cmd.org
>
> Ftrace is working at the function level. Therefore, ftrace and tracers
> based on ftrace are not suitable for my scenario.
>
> > perf ftrace: <Linux source>/tools/perf/Documentation/perf-trace.txt
> > perf-tools: https://github.com/brendangregg/perf-tools
>
> I am not familiar with the perf tool and cannot verify if it works for
> my scenario.
>
> >
> > In particular, the "function_graph" tracer of ftrace seems to be suited for your purposes:
> >
> > https://man7.org/linux/man-pages/man1/trace-cmd.1.html
> > https://man7.org/linux/man-pages/man1/trace-cmd-record.1.html
> > https://man7.org/linux/man-pages/man1/trace-cmd-report.1.html
> >
> > Obviously, gdb (and friends) is still invaluable (but it may be difficult to use, depending on your knowledge and experience, so use it if you really know that you need it):
> >
> > https://www.kernel.org/doc/html/latest/dev-tools/gdb-kernel-debugging.html
> > https://www.kernel.org/doc/html/latest/dev-tools/kgdb.html
> >
> > Regards,
> >
> > Fabio M. De Francesco
> >
> >>
> >> Any comment is welcome.
> >>
> >> --
> >> My best regards to you.
> >>
> >> No System Is Safe!
> >> Dongliang Mu
> >>
> >> _______________________________________________
> >> Kernelnewbies mailing list
> >> [email protected]
> >> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

2021-10-18 15:12:14

by Valdis Klētnieks

[permalink] [raw]
Subject: Re: Any tracing mechanism can track the executed instructions of a user process in the kernel?

On Mon, 18 Oct 2021 16:41:14 +0800, Dongliang Mu said:

> I want to log all the executed instructions of a user process (e.g.,
> poc.c in syzkaller) in the kernel mode and then would like to leverage
> backward analysis to capture the root cause of kernel panic/crash.

> Therefore, I need the instruction-level tracing mechanisms or tools.

Tracing just the instructions won't get you where you want to be if
you're going through this approach.

You *also* need to track all the data - the instruction path inside two
different runs of syzkaller may be essentially identical, but pass 2 different
values as the 3rd parameter of a syscall.

You may also have to deal with insane amounts of data - the actual error could
have been minutes or even hours before, or the interaction between two
different processes.

You probably want to take a *really* close look at how prof and friends avoid
infinite regress when code execution drops inside the prof code, because you're
going to hit the same issues.

Or....

You can work smarter rather than harder, and ask yourself what's the minimum
amount and type of additional information to make a significant improvement in
the debugging of system crashes.

For example, 95% of the time, you can figure out what the bug is by merely
looking at the stack traceback. For most of the rest of the cases, simply
capturing the parameter values from the syscall and the basic info for page
faults and other interrupts is probably sufficient, and you can probably
leverage the audit subsystem for most of that. It can already record syscall
parameters, while logging page faults and other interrupts can probably be done
with prof.

At that point, you don't actually *need* every instruction - only tracing
branch and call instructions is sufficient, because you already know that each
instruction between the target of a branch/call and the next branch/call will
be executed.

Similarly, the lockdep code will catch most locking issues. But it won't flag
issues with data that should be protected with a lock, but are bereft of any
locking. So ask yourself: What ways are there to analyze the code and detect
critical sections prone to race conditions? Is there a sparse-on-steroids
approach that wil do the heavy lifting for those? (Note that this isn't an easy
task for the general case, but identifying two or three specific common
patterns and finding a way to detect them may be worthwhile)

And many of the rest of crashes are timing related, and "let's trace every single
instruction" is almost guaranteed to make things slow enough to change/bypass
the timing issue.

So... What's left that would be the most helpful with the least amount of data?

Go look at some threads on linux-kernel. Look at the kernel bugs that were the
result of a Homer Simpson "D'oh!" moment. What can we do to make those
bugs less likely to make it into the code in the first place? For the more subtle
bugs, what data finally made the debugging come together?



Attachments:
(No filename) (505.00 B)

2021-10-18 21:04:17

by Jim Cromie

[permalink] [raw]
Subject: Re: Any tracing mechanism can track the executed instructions of a user process in the kernel?

On Sun, Oct 17, 2021 at 8:46 PM Dongliang Mu <[email protected]> wrote:
>
> Hi all,
>
> I am writing to kindly ask one question: is there any tracing
> mechanism in Linux kernel that can trace all the executed instructions
> of a user process? If this user process is run on different
> processors, traces of this process on different processors should be
> also recorded.
>
> Any comment is welcome.
>

take a look at rr-project.org

what rr does

rr aspires to be your primary C/C++ debugging tool for Linux,
replacing — well, enhancing — gdb. You record a failure once, then
debug the recording, deterministically, as many times as you want. The
same execution is replayed every time.

rr also provides efficient reverse execution under gdb. Set
breakpoints and data watchpoints and quickly reverse-execute to where
they were hit.