如何在Linux内核中打印当前线程堆栈跟踪? [英] How to print the current thread stack trace inside the Linux kernel?
问题描述
我希望能够在Linux内核中打印线程的堆栈跟踪.
I would like to be able to print the stack trace of a thread in the Linux kernel.
详细信息:我想向特定函数(例如swap_writepage()
)添加代码,这些代码将打印调用此函数的线程的完整堆栈跟踪.像这样:
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;
}
我的故事: 最近,Linux内核开发人员在改进用C编写的内核时开始采用面向对象的原理.由于C不是OO语言,所以事情开始变得非常丑陋和难以理解,更不用说没有像样的IDE了.分析C代码.而且我不想开始在调试器下运行Linux.注意:如果您是内核开发新手,并且想要在调试器下运行Linux,请不要付出任何努力……这将被证明是徒劳的(单步执行无济于事).
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. And I do not want to get started on running Linux under a debugger. 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内核具有众所周知的功能,称为dump_stack()
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.
这篇关于如何在Linux内核中打印当前线程堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!