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!
1 Answer
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.
-
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
backtrace
statement. You're talking about debugging? These are two different things.libbacktrace
. Here's the example from their repo.