代码之家  ›  专栏  ›  技术社区  ›  YotKay

如何在没有pstack和gdb的情况下获取线程堆栈信息

  •  3
  • YotKay  · 技术社区  · 7 年前

    1 回复  |  直到 7 年前
        1
  •  1
  •   einpoklum    7 年前

    我的建议是:

    1. 能够检索、操作和打印堆栈跟踪 在您自己的计划内
    2. 为您的进程安装一个信号处理程序(例如,用于SIGUSR1),该处理程序可以做到这一点

    现在(2)和(3)非常简单。对于(3),它是 kill -USR1 1234

    #include <signal.h>
    typedef void (*sighandler_t)(int);
    sighandler_t sigset(int sig, sighandler_t disp);
    

    man sigset 详细信息)。

    至于(1),这过去是相当困难和棘手的,但现在基本上已经解决了:

    : Code | Documentation

    #include <iostream>
    #include <boost/stacktrace.hpp>
    
    // ... somewhere inside the `bar(int)` function that is called recursively:
    std::cout << boost::stacktrace::stacktrace();
    

    可能导致以下输出:

    0# bar(int) at /path/to/source/file.cpp:70
    1# bar(int) at /path/to/source/file.cpp:70
    2# bar(int) at /path/to/source/file.cpp:70
    3# bar(int) at /path/to/source/file.cpp:70
    4# main at /path/to/main.cpp:93
    5# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
    6# _start
    

    注: