[英]How to print the current thread stack trace inside the Linux kernel?
I would like to be able to print the stack trace of a thread in the Linux kernel.我希望能够打印 Linux 内核中线程的堆栈跟踪。
In details: I want to add code to specific functions (eg swap_writepage()
) that will print the complete stack trace of the thread where this function is being called.详细说明:我想向特定函数(例如swap_writepage()
)添加代码,这些函数将打印调用此函数的线程的完整堆栈跟踪。 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;
}
My story: Recently, Linux kernel developers started to adopt object-oriented principles when improving the kernel, which is written in C. Since C is not an OO languages things started to look very ugly and harder ti understand, let alone not having a decent IDE that can analyze C code.我的故事:最近,Linux 内核开发人员在改进用 C 编写的内核时开始采用面向对象的原则。由于 C 不是面向对象的语言,所以事情开始变得非常丑陋且难以理解,更不用说没有像样的可以分析 C 代码的 IDE。 And I do not want to get started on running Linux under a debugger.我不想开始在调试器下运行 Linux。 Note: if you are a kernel development newb and want to run Linux under a debugger do not put efforts into that...it will prove to be fruitless (stepping makes no sense).注意:如果您是内核开发新手并且想在调试器下运行 Linux,请不要为此付出努力……这将被证明是徒劳的(单步执行是没有意义的)。
@rakib is exactly right of course. @rakib 当然是完全正确的。
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" .您可以在此处查看并下载:“A Header of Convenience” 。
There are macros / functions to:有宏/函数可以:
Whew :-)哇:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.