Following a code path in the Linux Kernel without a debugger

Sometimes you don’t get to use a debugger. When do bare metal development, often it is faster to get to the root of a problem by throwing in trace statements, and seeing what path is taken through the code.

There are two main techniques I have been using to do this. The first is to print out the spot in the code using built in macros that tell the file, the name of the function, and the line number. That looks like this:

pr_info("%s %s %d", __FILE__, __func__,  __LINE);

I know it looks a little weird having some upper and some lower case in there, but that is what works.

However, Linux makes heavy use of function pointers, and you cannot use tags to jump to a function whose name you do not know. To print out the source of a function from a pointer, you can use the print formatting macros specific to the Linux Kernel. For example: I can use

printk("%ps", pmu->event_init);

In my case, that prints out:

arm_cspmu_event_init [arm_cspmu_module]

Which I could then jump to using the :tag command in vim.

1 thought on “Following a code path in the Linux Kernel without a debugger

  1. This is all very interesting, but I would like to hear more about music and mountain climbing. Just kidding. Your blog is really great, and I learn a lot from it. Many thanks for these posts!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.