Reputation: 1307
I know gcc has built in __function__
and related macros, but from an outside function, if I'm being passed a function pointer, is there a way to get the name of that function out of the symbol table at compile time?
Basically:
void* foo(){
profile_func(foo);
do_something();
}
profile_func(void* func){
printf("function is %s\n", NAME_OF(foo));
}
Of course, I know the symbol table is not an exact representation of the function, but it's enough to get the information I need.
I'm using -finstrument_functions
, and this is going to be going in the related __cyg_profile_func_enter()
function. I'm surprised that I can't find this already done by someone (unless my google-fu is lacking)
Upvotes: 0
Views: 420
Reputation: 2944
You can do this at compile time but it is not exactly well supported. You have to create the table on your own by running multiple passes of the compiler + nm
or similar + some reformatting (e.g., via awk
) as described in this answer.
Upvotes: 0
Reputation: 215173
Definitely not in a way that's constant at compile-time, because in general, what the function pointer points to is not constant at compile-time.
If you want to get it at runtime, the dladdr
function does what you want, subject to limitation that it may only work when dynamic-linked and is only able to find functions in the dynamic symbol table (so it won't find hidden, static, etc. functions). You could of course roll your own that looks at a more detailed debugging symbol table.
Upvotes: 2