2017-10-15 38 views
1

我一直在努力尝试调试我的应用程序。我想要做的是将GDB附加到正在运行的进程(使用批处理无提示),但是如果发生崩溃,则会将文件追溯到文本文件。我能够附加调试器没有问题(gdb attach pid)。但一直没有能够实现与默默运行的日志记录。附加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 ([email protected]=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 
0

你确定你需要连接到正在运行的进程?

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

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