简体   繁体   English

如何在 Linux 内核中打印当前线程堆栈跟踪?

[英]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,请不要为此付出努力……这将被证明是徒劳的(单步执行是没有意义的)。

Linux kernel has very well known function called dump_stack() here , which prints the content of the stack. Linux 内核有一个非常有名的函数,叫做dump_stack() here ,它打印堆栈的内容。 Place it in your function in according to see stack info.根据查看堆栈信息将其放入您的函数中。

@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:有宏/函数可以:

  • make debug prints along with funcname / line# info (via the usual printk() or trace_printk()) and only if DEBUG mode is On使调试打印连同 funcname / line# info(通过通常的 printk() 或 trace_printk())并且仅当调试模式为 On 时
  • dump the kernel-mode stack转储内核模式堆栈
  • print the current context (process or interrupt along with flags in the form that ftrace uses)打印当前上下文(进程或中断以及ftrace使用的形式的标志)
  • a simple assert() macro (!)一个简单的 assert() 宏 (!)
  • a cpu-intensive DELAY_LOOP (useful for test rigs that must spin on the processor) cpu 密集型 DELAY_LOOP(对于必须在处理器上旋转的测试台很有用)
  • an equivalent to usermode sleep functionality相当于用户模式睡眠功能
  • a function to calculate the time delta given two timestamps (timeval structs)一个函数来计算给定两个时间戳的时间增量(timeval 结构)
  • convert decimal to binary, and将十进制转换为二进制,以及
  • a few more.还有几个。

Whew :-)哇:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM