如何在 Linux 内核中打印当前线程堆栈跟踪? [英] How to print the current thread stack trace inside the Linux kernel?

查看:19
本文介绍了如何在 Linux 内核中打印当前线程堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够打印 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 不是面向对象的语言,所以事情开始变得非常丑陋和难以理解,更不用说没有像样的 IDE 可以分析 C 代码.我不想开始在调试器下运行 Linux.注意:如果你是内核开发新手并且想在调试器下运行 Linux,请不要为此付出努力......它会被证明是徒劳的(步进没有意义).

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆