如果您需要的是在知道 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
是实际的break
gdb 命令,您还可以使用br file.c:451
. 有关更多break
选项,请查看 gdb 帮助。commands
您可以在和之间添加您需要的任何 gdb 命令end
以对应相应的br
. 有关 的更多信息commands
,请help commands
查看gdb。
注意:SO 的 JS 在我的浏览器上被破坏,请原谅任何错误并随时纠正。也无法添加评论:(
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)
为我工作。参考。