'How do I access the `addr2line` functionality within my C++ program?

I need to get the information provided by addr2line (source file and line from backtracing a function call) from within a C++ program.

I know I can call addr2line directly as a subprocess and I know that I can copy the source code of addr2line into my program (which is also GPL licensed). But the code looks complicated and I don't feel comfortable using it directly. What does bfd mean, anyway? I would prefer to use some function in the C++ STL library, because that would be cleaner.

I am working in a Linux environment.



Solution 1:[1]

You can try the function dladdr(). It uses the dynamic symbols of the program, not the debugging information (compile the program with gcc -rdynamic).

Also, you can check the backtrace library, or the higher level stacktrace library.
Not exactly what you are asking, but they may prove useful.

Solution 2:[2]

Check the source code of bsd implementation of addr2line, it has only about 400 lines code. Change the source to a library function should be very easy. http://sourceforge.net/p/elftoolchain/code/HEAD/tree/trunk/addr2line/addr2line.c

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rodrigo
Solution 2 m7913d