自动化GDB在特定断点处打印堆栈帧

Nob*_*lis 8 c gdb

是否可以将gdb附加到正在运行的进程的PID上,并且每次程序命中特定断点时gdb都会将堆栈输出到外部文件?

我看了一眼这个这个,但没有就是否有可能GDB附加到一个已经运行的进程没有提及(而不是让GDB启动它).

我可以将gdb附加到PID就好了,但是我想自动运行它bt,将输出存储在外部文件中然后运行continue.目前我正在手动执行此操作,每次遇到断点时我都必须这样做.

Voi*_*ter 6

如果您需要的是在知道 PID 和函数的情况下使用 gdb 自动打印堆栈帧,那么您可以尝试这个..(给定最少的代码即可运行)

/root/.gdb_init:

set pagination off
set logging file gdb.txt
set logging on

br fun_convert
# ^^ when breaking at function fun_convert, execute `commands` till next `end`
commands
    bt
    print "Sample print command 1 \n"
    continue
end

br file.c:451
# ^^ when breaking at line 451 of file.c, execute from `commands` till next `end`
commands
    bt
    print "Sample print command 2 \n"
    continue
end

continue
Run Code Online (Sandbox Code Playgroud)

调用GDB获取PID6474和命令文件/root/.gdb_init

gdb -p 6474 -x /root/.gdb_init
Run Code Online (Sandbox Code Playgroud)

这里,fun_convert是要中断的函数。这br是实际的breakgdb 命令,您还可以使用br file.c:451. 有关更多break选项,请查看 gdb 帮助。commands您可以在和之间添加您需要的任何 gdb 命令end以对应相应的br. 有关 的更多信息commands,请help commands查看gdb

注意:SO 的 JS 在我的浏览器上被破坏,请原谅任何错误并随时纠正。也无法添加评论:(


Jey*_*ram 5

Is it possible to have gdb attached to the PID of a running process??
Run Code Online (Sandbox Code Playgroud)

是。可能。

更新:

步骤1:

.gdbinit文件中,添加以下命令,

define callstack
     set $Cnt = $arg0

     while($Cnt)
        commands $Cnt
        silent
        bt
        c
        end
        set $Cnt = $Cnt - 1
     end
end
Run Code Online (Sandbox Code Playgroud)

步骤2:使用调用gdb -x <path to .gdbinit file >。记住PID也用于运行过程。

步骤3:在任何需要的地方放置断点。

步骤4:调用用户定义的命令callstack并传递断点号。

    gdb> callstack <No.of. Break Points> 
Run Code Online (Sandbox Code Playgroud)

步骤5:现在输入“ c”继续。Bcos进程已经在运行。

对于日志记录,我建议遵循@VoidPointer的回答。

set pagination off
set logging file gdb.txt
set logging on 
Run Code Online (Sandbox Code Playgroud)

为我工作。参考