2017-10-15 71 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