如何使用gcc和行号信息获取C ++的堆栈跟踪? [英] How to get a stack trace for C++ using gcc with line number information?
问题描述
我们在专有的 assert
类似的宏中使用堆栈跟踪来捕捉开发人员的错误 - 当发现错误时,会打印堆栈跟踪。
我发现gcc的对 backtrace()
/ backtrace_symbols()
方法不足:
- 名称已损坏
- 无行信息
第一个问题可以通过 abi :: __ cxa_demangle < a>。
然而第二个问题更加困难。我发现替换backtrace_symbols()。
这比gcc的backtrace_symbols()好,因为它可以检索行号(如果使用-g编译),并且不需要使用-rdynamic编译。
任何提议?
PS
gdb可以打印传递给函数的参数。
可能已经太多要求:)
PS 2
类似问题(谢谢nobar)
不久前我回答了类似的问题。您应该查看方法#4上的源代码,该代码还会打印行号和文件名。
We use stack traces in proprietary assert
like macro to catch developer mistakes - when error is caught, stack trace is printed.
I find gcc's pair backtrace()
/backtrace_symbols()
methods insufficient:
- Names are mangled
- No line information
1st problem can be resolved by abi::__cxa_demangle.
However 2nd problem s more tough. I found replacement for backtrace_symbols(). This is better than gcc's backtrace_symbols(), since it can retrieve line numbers (if compiled with -g) and you don't need to compile with -rdynamic.
Hoverer the code is GNU licenced, so IMHO I can't use it in commercial code.
Any proposal?
P.S.
gdb is capable to print out arguments passed to functions. Probably it's already too much to ask for :)
PS 2
Similar question (thanks nobar)
Not too long ago I answered a similar question. You should take a look at the source code available on method #4, which also prints line numbers and filenames.
这篇关于如何使用gcc和行号信息获取C ++的堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!