c++来自未处理异常的堆栈跟踪?
这个问题之前已经被问过,并且有特定于 Windows 的答案,但没有令人满意的 gcc 答案。我可以使用 set_terminate()
设置一个在抛出未处理的异常时将被调用的函数(代替 terminate()
)。我知道如何使用 backtrace 库从程序中的给定点生成堆栈跟踪。但是,当调用我的终止替换时,这将无济于事,因为此时堆栈已展开。
然而,如果我只是允许程序abort()
,它将生成一个核心转储,其中包含抛出异常时的完整堆栈信息。所以信息就在那里——但是有没有一种编程方式来获取它,例如可以记录它,而不必检查核心文件?
This question has been asked before and there have been windows-specific answers but no satisfactory gcc answer. I can use set_terminate()
to set a function that will be called (in place of terminate()
) when an unhandled exception is thrown. I know how to use the backtrace library to generate a stack trace from a given point in the program. However, this won't help when my terminate-replacement is called since at that point the stack has been unwound.
Yet if I simply allow the program to abort()
, it will produce a core-dump which contains the full stack information from the point at which the exception was thrown. So the information is there -- but is there a programmatic way to get it, for example so it can be logged, rather than having to examine a core file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用libunwind(只需将
-lunwind
添加到链接器参数)(使用clang++ 3.6
测试):demagle.hpp:
demangle.cpp:
backtrace .hpp:
backtrace.cpp:
backtrace_on_terminate.hpp:
有 关于这个问题的好文章。
You can use libunwind (just add
-lunwind
to linker parameters) (tested withclang++ 3.6
):demagle.hpp:
demangle.cpp:
backtrace.hpp:
backtrace.cpp:
backtrace_on_terminate.hpp:
There is good article concerning the issue.
我怀疑我的经验是否适合您的需求,但无论如何,我的经验就在这里。
我重载了
abort()
:通过在 libc 之前添加我自己的目标文件或使用 LD_PRELOAD。在我自己的 abort() 版本中,我启动调试器,告诉它附加到进程(好吧,我当然知道我的 PID)并将堆栈跟踪转储到文件中(命令被传递到通过命令行调试器)。调试器完成后,使用_exit(100)
终止进程。那是在 Linux 上使用 GDB。在 Solaris 上,我经常使用类似的技巧,但由于没有健全的调试器,我使用 pstack 工具:
system("pstack")
。I doubt my experience would fit your needs but here it goes anyway.
I was overloading
abort()
: either by adding my own object file before the libc or using LD_PRELOAD. In my own version ofabort()
I was starting the debugger telling it to attach to the process (well, I surely know my PID) and dump the stack trace into a file (commands were passed to the debugger via command line). After debugger had finished, terminate the process with e.g._exit(100)
.That was on Linux using GDB. On Solaris I routinely employ similar trick but due to unavailability of a sane debugger I use the pstack tool:
system("pstack <PID>")
.编辑答案:
您可以使用 std::set_terminate
给出此输出:
假设您有调试二进制文件中的符号,然后可以使用 addr2line 构建更漂亮的堆栈跟踪事后
原始答案如下
我过去使用 boost::error_info 使用
execinfo.h
backtrace 注入堆栈跟踪> 进入抛出的异常。然后当捕获异常时,你可以这样做
Edited Answer:
You can use std::set_terminate
giving this output:
assuming you have debug symbols in your binary, you can then use addr2line to construct a prettier stack trace postmortem
original answer is below
I've done this in the past using boost::error_info to inject the stack trace using
backtrace
fromexecinfo.h
into an exception that is thrown.Then when catching the exceptions, you can do