crashreport.gdb
在看TrinityCore
代码时,看到一段有趣的脚本(crashreport.gdb):
set logging overwrite on
set logging file backtrace.log.temp
set style enabled off
set height unlimited
set width unlimited
set disassembly-flavor intel
handle SIG33 pass nostop noprint
set pagination 0
set logging on
echo \n--- DEBUG: --- START\n\n
run
# check if exited normally
if $_isvoid($_exitcode)echo \n--- DEBUG: BACKTRACE FULL\n# print assertion message if this is ASSERT crashif TrinityAssertionFailedMessage != 0printf "%s\n", TrinityAssertionFailedMessageendecho \nbacktrace fullecho \n--- DEBUG: INFO REGISTERS\n\ninfo registersecho \n--- DEBUG: CALLS (x/16i $pc)\n\nx/16i $pcecho \n--- DEBUG: THREAD APPLY ALL BACKTRACE\nthread apply all backtraceecho \n--- DEBUG: --- STOP\n\necho Generated by crashreport.gdb script version 1.4\nset logging off# rename log file to avoid it getting overwritten by restart scriptsshell mv backtrace.log.temp backtrace_$(date +%Y-%m-%d-%T).log
else# normal exit, cleanup temp fileset logging offshell rm backtrace.log.temp
end
quit
脚本内容都是 gdb 的命令,很简单,可问 ChatGPT
这段脚本,可以在程序 crash 时,把堆栈信息,存入文件
如何使用
gdb -x crashreport.gdb <executable file>
其他
原来我的做法是,用 signal 在程序内注册异常处理句柄,执行 gdb 命令
相比而言,这种方法更轻便、灵活,且无侵入性