22

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

edit: I am working in a Linux environment.

I know I can call addr2line directly and I know that I can use the source code of addr2line in my program (which is also GPL licensed). But I guess calling a library function, if it exists, is cleaner.

edit: I will use bfd of the binutils, just as addr2line does. What does bfd mean anyway?

7

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

| improve this answer | |
6

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.

| improve this answer | |
  • 1
    Thank you for the tipps. With dladdr() I get information only for dynamically linked code and only the filename, not the line, if I understand the manpage right. I am already using backtrace to get the address. What I need now is something to get the file and line out of the address. stacktrace is a mere wrapper for backtrace. – steffen Jul 19 '12 at 9:10
  • 1
    In addition to these, there is also the libunwind library: nongnu.org/libunwind – Arto Bendiken Jul 3 '15 at 22:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.