13

I want to dump a backtrace from a C++ program in Linux in a similar format as it is done in gdb. I tried to use the backtrace() and backtrace_symbols() functions for this purpose. These returned function names and offsets. I can use the __cxa_demangle() function to get a readable function name.

Is there any way to get the file/line positions too, as it is done by gdb?

petersohn
  • 11,292
  • 13
  • 61
  • 98
  • 1
    duplicate: http://stackoverflow.com/questions/4636456/stack-trace-for-c-using-gcc which includes a solution in the question – rve Nov 02 '11 at 13:42

1 Answers1

6

How it's better to invoke gdb from program to print its stacktrace?`

Methode #4, shows a way to get filename and line. But uses a extern program..

Community
  • 1
  • 1
KoKuToru
  • 4,055
  • 2
  • 20
  • 21
  • If you don't mind really executing GDB to get a backtrace you can take a look at how the Warzone 2100 project does it. We're basically using a combination of these techniques to get GDB-quality output if possible and progressively fall back to simpler strategies (e.g. if GDB isn't available or libc doesn't provide backtrace()). (the gist of it resides in https://github.com/Warzone2100/warzone2100/blob/master/lib/exceptionhandler/exceptionhandler.cpp ) – Giel Jan 28 '15 at 12:49