【问题标题】:Attaching GDB debugger and logging backtraces附加 GDB 调试器并记录回溯
【发布时间】:2017-10-15 16:09:01
【问题描述】:

几天来,我一直在努力调试我的应用程序。我想做的是将 GDB 附加到正在运行的进程(使用批处理静默),但如果发生崩溃,则将回溯记录到文本文件中。我能够毫无问题地附加调试器(gdb attach pid)。但是一直无法实现与它一起静默运行的日志记录。

提前致谢。

【问题讨论】:

    标签: linux gdb


    【解决方案1】:

    但是没办法

    你试过什么?示例:

    cat t.c
    int main() { sleep(5); printf("Aborting\n"); abort(); }
    
    gcc -w -g t.c && ./a.out &
    sleep 0.1 && rm -f gdb.txt &&
    gdb -q --batch-silent -p "$(pgrep a.out)" -ex 'set logging on' \
       -ex continue -ex where -ex quit &&
    cat gdb.txt
    

    这会产生:

    [1] 38218       # bash reports background process
    Aborting        # process done sleeping
    
                    # contents of gdb.txt:
    Program received signal SIGABRT, Aborted.
    #0  0x00007f99aeb50c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
    #1  0x00007f99aeb54028 in __GI_abort () at abort.c:89
    #2  0x00000000004005df in main () at t.c:1
    
                    # bash reports process termination:
    $ -bash: line 98: 38225 Aborted                 (core dumped) ./a.out    
    [1]+  Exit 134                gcc -w -g t.c && ./a.out
    

    【讨论】:

      【解决方案2】:

      您确定需要附加到正在运行的进程吗?

      如果您的目标是修复崩溃,您可以启用核心转储并在崩溃发生后调试核心转储。与调试实时进程相比,调试核心转储有几个限制,但在您的情况下(仅查看回溯),它看起来更合适。调试核心转储运行:

      gdb /path/to/binary /path/to/core
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        • 1970-01-01
        • 2014-01-03
        • 2016-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多