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.
A small improvement I’ve done on method #3 to print line numbers. This could be copied to work on method #2 also.
Basically, it uses addr2line to convert addresses into file names and line numbers.
The source code below prints line numbers for all local functions. If a function from another library is called, you might see a couple of
??:0instead of file names.This code should be compiled as:
gcc sighandler.c -o sighandler -rdynamicThe program outputs: