I would like to be able to print the stack trace of a thread in the Linux kernel.
In details: I want to add code to specific functions (e.g. swap_writepage()
) that will print the complete stack trace of the thread where this function is being called. Something like this:
int swap_writepage(struct page *page, struct writeback_control *wbc)
{
/* code goes here to print stack trace */
int ret = 0;
if (try_to_free_swap(page)) {
unlock_page(page);
goto out;
}
if (frontswap_store(page) == 0) {
set_page_writeback(page);
unlock_page(page);
end_page_writeback(page);
goto out;
}
ret = __swap_writepage(page, wbc, end_swap_bio_write);
out:
return ret;
}
Just use new Throwable(). printStackTrace() method and it will print complete stack trace from where a method is called, into the console.
Linux kernel provides a function to print the stack trace: dump_stack(). The dump_stack function produces a stack trace much like panic and oops, but causes no problems and we return to the normal control flow. Calling dump_stack() function will print the stack trace at that point.
Kernel has the ability to examine the size of the kernel stack and how much stack space each function is using. Enabling the stack tracer ( CONFIG_STACK_TRACER ) will show where the biggest use of the stack takes place. The stack tracer is built form the function tracer infrastructure.
Linux kernel has very well known function called dump_stack()
here, which prints the content of the stack. Place it in your function in according to see stack info.
@rakib is exactly right of course.
In addition, I'd like to point out that one can define simple and elegant macros that help print debug info as and when required. Over the years, I've put these macros and conveneince routines into a header file; you can check it out and download it here: "A Header of Convenience".
There are macros / functions to:
Whew :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With