Accessing symbol table to find function given address

Brian Dessent brian@dessent.net
Sat May 26 21:17:00 GMT 2007


"Paulo J. Matos" wrote:

> Is there a way to access the symbol table during runtime to know which
> function belongs to a given address. The problem is to print the
> function name if I am instrumenting functions. I get the address, can
> I during runtime print the function name? (or the solution is get it
> through addr2line after program as run)?

Gcc provides the standard symbols __func__ and __FUNCTION__ as well as
the extension __PRETTY_FUNCTION__.  See
<http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html#index-g_t_0040code_007b_005f_005fPRETTY_005fFUNCTION_005f_005f_007d-identifier-2118>. 
To use them you'd have to arrange to pass them to your instrumentation
routine from a call within the function though, they can't be used to
just map a memory address to a function name.  To do that will be
extremely target-specific and seeing as how gcc supports something like
3 dozen various targets, there's no way to answer that without more
details.  If you're using a glibc platform, you can use dladdr(); see
dlopen(3).  But realize this is nonportable, it is a glibc extension.

Brian



More information about the Gcc-help mailing list