Printing full backtrace in c++
Asked Answered
J

1

15

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?

Jotham answered 2/11, 2011 at 10:44 Comment(1)
duplicate: https://mcmap.net/q/65974/-how-to-get-a-stack-trace-for-c-using-gcc-with-line-number-information-duplicate which includes a solution in the questionPerceptible
M
7

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..

Milligan answered 2/11, 2011 at 10:55 Comment(1)
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 github.com/Warzone2100/warzone2100/blob/master/lib/… )Orpha

© 2022 - 2024 — McMap. All rights reserved.