I'm trying to profile the function calls using -finstrument-functions option. Basically, what I have done is to write the following into any compiled source:
static int __stepper=0;
void __cyg_profile_func_enter(void *this_fn, void *call_site)
__attribute__((no_instrument_function));
void __cyg_profile_func_enter(void *this_fn, void *call_site) {
int i=0;
for( ; i<__stepper; i++ ) printf(" ");
printf("E: %p %p\n", this_fn, call_site);
__stepper ++;
} /* __cyg_profile_func_enter */
void __cyg_profile_func_exit(void *this_fn, void *call_site)
__attribute__((no_instrument_function));
void __cyg_profile_func_exit(void *this_fn, void *call_site) {
int i=0;
__stepper --;
for( ; i<__stepper; i++ ) printf(" ");
printf("L: %p %p\n", this_fn, call_site);
} /* __cyg_profile_func_enter */
And got the following results:
E: 0xb7597ea0 0xb75987a8
E: 0xb7597de0 0xb7597ef5
L: 0xb7597de0 0xb7597ef5
L: 0xb7597ea0 0xb75987a8
All the function calls address is around that region (0xb7.......) But, if I try to read the symbols for function using 'readelf -s' it gives the following:
2157: 00101150 361 FUNC LOCAL DEFAULT 13 usb_audio_initfn
2158: 00100940 234 FUNC LOCAL DEFAULT 13 usb_audio_handle_reset
2159: 00100de0 867 FUNC LOCAL DEFAULT 13 usb_audio_handle_control
The address region of all the functions in binary is around 0x00...... So, I can not be able to get the function name from the function pointers. Looks like some how the function pointer gets an offset or something.
Anybody has any idea?
What you need is this dladdr function. If you've built in debug mode the module (your main program or the shared library) in which the function in question is defined, then by calling the
dladdr
function you''ll get the function name based on its address and also the base address where the module (e.g. your shared library) is loaded:You have to add -ldl when linking.
Bear in mind that:
find_func
needs to be called from your profiled process (read: somewhere from your__cyg_profile_func_enter
or__cyg_profile_func_exit
functions) because the addresspfnFuncAddr
is the actual function address (read: should be equal tothis_fn
orcall_site
arguments of the__cyg_*
functions)Function name that you'll get may be mangled (if it is a c++ function/method of a class). You can demangle the name using command line tool called c++filt. If you want to demangle from your profiler code then you need to look at the bfd library and functions like
bfd_read_minisymbols
bfd_demangle
and friends. If you really want o profile your code demangling all the function names later (after profiling) may be a good idea.The difference in address values that you observed is exactly the difference between the actual address of the function(s) in question and the base address at which the module that contains the function was loaded (read: the
info.dli_fbase
).I hope that helps.
From the question it looks like you're profiling a library function.
To know what are the functions being measured you have 2 options:
1 Run the program which uses the library under
gdb
and stop atmain
. At this point, get thepid
of the programPID=...
and do `cat /proc/$PID/maps'. There you should see something like this:Here
7fa174cc9000
is base address of the/lib/x86_64-linux-gnu/libcap.so.2.22
library. So all the addresses you get byreadelf -s
will be offset by that value. Knowing base address you can calculate back what the original offset in file was.I.e. if you got the value
7fa174206370
and base address of the library is7fa1741cf000
then offset is7fa174206370 - 7fa1741cf000 = 37370
. In my example it'ssigsuspend
from GLIBC:2 Run
gdb
on the program which uses these libraries. It'll either immediately find the loaded library in memory, or will need to be pointed to the.text
section of the library.This way you know that
0x7fa174206386
is insidesigsuspend
.In case
gdb
doesn't load any symbols by itself (no output likeReading symbols from ... Loading symbols for ...
after attach), you can look up the base address of library as in option 1, then add to it the offset of.text
section7fa174cc9000 + 0000000000001620
in hexadecimal gives7FA174CCA620
, and then you attach bygdb
as above and doThen you should be able to find symbols (via
x/i ADDRESS
as in option 1) even ifgdb
doesn't load them by itself.Please ask if anything is unclear, I'll try to explain.
Clarification on why is this so:
The observed behavior is due to the libraries being compiled as Position-Independent Code. It allows us to easily support dynamic libraries. PIC essentially means that library's ELF has
.plt
and.got
sections and can be loaded at any base address. PLT is procedure linkage table and it contains traps for calls of functions located in other modules, which first go to program interpreter to allow it to relocate the called function, and then just jump to the function after the first call. It works because program interpreter updates GOT (Global Offset Table), which contains addresses of functions to call. Initially the GOT is initialized so that on first function call the jump is performed to the function of program interpreter which performs resolution of currently called function.On x86-64, PLT entries typically looks like this:
The first
jmpq
is jump to address, stored in GOT at location%rip + 0x202be2
:%rip + 0x202be2
will be0x204012
, and that gets added to the base address of the library to produce absolute address relevant to location where the library is actually loaded. I.e. if it's loaded at0x7f66dfc03000
, then the resulting address of corresponding GOT entry will be0x7F66DFE07012
. The address stored at that location is address of (in this example)free
function. It's maintained by program interpreter to point to actualfree
inlibc
.More information on this can be found here.