2

Inside my C program I'm getting function call stack by using backtrace and then function names by backtrace_symbols, using output of backtrace. The resulting strings contain function names and addresses of call initiating code, withing the function. Is there a way to convert those addresses into file names and line numbers? I mean by using C library (Linux only will work) of gcc extensions. Thanks in advance!

5
  • 3
    C or C++? Choose a tag. Commented Jun 4, 2015 at 2:18
  • What do you actually want? C has no backtrace statement. You're talking about debugging? These are two different things. Commented Jun 4, 2015 at 2:19
  • 1
    Use libbacktrace. Here's the example from their repo.
    – Pradhan
    Commented Jun 4, 2015 at 2:26
  • OP's presumably refering to this API.... Commented Jun 4, 2015 at 2:28
  • I've written a small util which translates addresses to files, lines and functions. It's based on addr2line. github.com/EmilOhlsson/call-trace
    – Kotte
    Commented Nov 11, 2017 at 22:42

1 Answer 1

5

Outside of an application, you can convert an address to a source file name and line number using the addr2line utility. (This utility is Linux-specific; Mac OS X has the similar atos utility.)

Within an application, you can use the dladdr function to resolve an address to a shared object and symbol name. I'm not aware of any way to access debug information to get a line number within an application, though.

1
  • I have a gist where I explain the process of getting the source code line number from an address (say, in a crash backtrace). Hope it helps. Commented Jul 10, 2019 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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