fixunix
Tags Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

How to know the function name by function pointer's address - Unix

This is a discussion on How to know the function name by function pointer's address - Unix ; Hi forks: I use the gcc's compiler flag -finstrument-functions which will generate instrumentation calls for entry and exit to every functions.Just after function entry and just before function exit, the fol- lowing profiling functions will be called with the address ...


Fix Unix > Unix > How to know the function name by function pointer's address

Reply
 
LinkBack Tools
  #1  
Old 10-04-2007, 12:09 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default How to know the function name by function pointer's address

Hi forks:

I use the gcc's compiler flag -finstrument-functions which will
generate instrumentation calls for entry and exit to every
functions.Just after function entry and just before function exit, the
fol-
lowing profiling functions will be called with the address of the
current function and its call site.

The prototype of function which shall be called afetr function entry
is
void __cyg_profile_func_enter (void *this_fn, void *call_site);

the pointer this_fn is the address of callee function, and the
call_site is the address of caller function.

How do I know the function name by these addresses?

BR,

Reply With Quote
  #2  
Old 10-04-2007, 12:09 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: How to know the function name by function pointer's address

Chisin wrote:
> Hi forks:
>
> I use the gcc's compiler flag -finstrument-functions which will
> generate instrumentation calls for entry and exit to every
> functions.Just after function entry and just before function exit, the
> fol-
> lowing profiling functions will be called with the address of the
> current function and its call site.
>
> The prototype of function which shall be called afetr function entry
> is
> void __cyg_profile_func_enter (void *this_fn, void *call_site);
>
> the pointer this_fn is the address of callee function, and the
> call_site is the address of caller function.
>
> How do I know the function name by these addresses?



In Linux/GNU libc maybe you can use:

#include

backtrace()
backtrace_symbols()
Reply With Quote
  #3  
Old 10-04-2007, 12:09 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: How to know the function name by function pointer's address

On 10 Aug, 10:26, Chisin wrote:

> I use the gcc's compiler flag -finstrument-functions which will
> generate instrumentation calls for entry and exit to every
> functions.Just after function entry and just before function exit, the
> fol-
> lowing profiling functions will be called with the address of the
> current function and its call site.
>
> The prototype of function which shall be called afetr function entry
> is
> void __cyg_profile_func_enter (void *this_fn, void *call_site);
>
> the pointer this_fn is the address of callee function, and the
> call_site is the address of caller function.
>
> How do I know the function name by these addresses?


You can get it from the symbol table using libbfd, as nm utility does.
http://en.wikipedia.org/wiki/Binary_File_Descriptor

Reply With Quote
  #4  
Old 10-04-2007, 12:09 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: How to know the function name by function pointer's address

Chisin writes:

> Hi forks:
>
> I use the gcc's compiler flag -finstrument-functions which will
> generate instrumentation calls for entry and exit to every
> functions.Just after function entry and just before function exit, the
> fol-
> lowing profiling functions will be called with the address of the
> current function and its call site.
>
> The prototype of function which shall be called afetr function entry
> is
> void __cyg_profile_func_enter (void *this_fn, void *call_site);
>
> the pointer this_fn is the address of callee function, and the
> call_site is the address of caller function.
>
> How do I know the function name by these addresses?


But if you want to do it portably, you have to do it explicitely.

% cat a.c

#include

int f(){return(0);}
int g(){return(1);}
int h(){return(2);}

struct { void* fun; char* name; } table[] = {{f,"f"},{g,"g"},{h,"h"},{0,0}};

const char* getname(void* fun){
int i;
for(i=0;table[i].name!=0;i++){
if(fun==table[i].fun){
return(table[i].name);
}
}
return(0);
}

int main(){
printf("%s %s %s\n",getname(f),getname(g),getname(h));
return(0);
}

% cc a.c -o a
% ./a
f g h

You can also play with macros to avoid filling the table explicitely.

--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
Reply With Quote
  #5  
Old 10-04-2007, 12:09 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: How to know the function name by function pointer's address

On Aug 10, 7:28 pm, Pascal Bourguignon wrote:
> Chisin writes:
> > Hi forks:

>
> > I use the gcc's compiler flag -finstrument-functions which will
> > generate instrumentation calls for entry and exit to every
> > functions.Just after function entry and just before function exit, the
> > fol-
> > lowing profiling functions will be called with the address of the
> > current function and its call site.

>
> > The prototype of function which shall be called afetr function entry
> > is
> > void __cyg_profile_func_enter (void *this_fn, void *call_site);

>
> > the pointer this_fn is the address of callee function, and the
> > call_site is the address of caller function.

>
> > How do I know the function name by these addresses?

>
> But if you want to do it portably, you have to do it explicitely.
>
> % cat a.c
>
> #include
>
> int f(){return(0);}
> int g(){return(1);}
> int h(){return(2);}
>
> struct { void* fun; char* name; } table[] = {{f,"f"},{g,"g"},{h,"h"},{0,0}};
>
> const char* getname(void* fun){
> int i;
> for(i=0;table[i].name!=0;i++){
> if(fun==table[i].fun){
> return(table[i].name);
> }
> }
> return(0);
>
> }
>
> int main(){
> printf("%s %s %s\n",getname(f),getname(g),getname(h));
> return(0);
>
> }
>
> % cc a.c -o a
> % ./a
> f g h
>
> You can also play with macros to avoid filling the table explicitely.
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> NOTE: The most fundamental particles in this product are held
> together by a "gluing" force about which little is currently known
> and whose adhesive power can therefore not be permanently
> guaranteed.- Hide quoted text -
>
> - Show quoted text -


I know it's a good way to make my own table, but if I own hundreds of
functions?
It seems the better way is to use the libbfd as my solution.
Any suggestions?


BR,
Chisin



Reply With Quote
  #6  
Old 10-04-2007, 12:09 AM
Junior Member
 
Join Date: Sep 2009
Posts: 0
Default Re: How to know the function name by function pointer's address

Chisin writes:
> I know it's a good way to make my own table, but if I own hundreds of
> functions?
> It seems the better way is to use the libbfd as my solution.
> Any suggestions?


Then:

>> You can also play with macros to avoid filling the table explicitely.


Use a macro like:

--------------------------(implemacs.h)---------------------------------
#define DEFUN(name,args,resultype) resultype name args

------------------------------------------------------------------------
#include

DEFUN(f,(int a,int b),float)
{
return(0.0);
}

DEFUN(g,(int a,int b),int)
{
return(a+b);
}
------------------------------------------------------------------------

everywhere.



So now you can easily do some nice stuff, like making the .h from the .c:

--------------------------(intermacs.h)---------------------------------
echo '#define DEFUN(name,args,resultype) extern resultype name args;

--------------------------(Makefile)------------------------------------
SOURCES=src1.c src2.c
OBJECTS=$(SOURCES:.c=.o)
HEADERS=$(SOURCES:.c=.h)
$HEADERS:$(SOURCES)
for f in $(SOURCES) ; do \
echo '#include ' > $${f/.c/.h} ;\
grep -e '/^DEFUN(/' < $$f >> $${f/.c/.h} ;\
done

# But you can also easily build the table:

table.c: $(SOURCES)
for f in $(SOURCES) ; do \
echo '#define DEFUN(name,args,resultype) { name , #name },' ;\
echo 'struct {void* fun,char* nam} table[]={' ;\
grep -e '/^DEFUN(/' < $$f ;\
echo '{0,0}};' ;\
done > table.c

------------------------------------------------------------------------

for example...

--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
Reply With Quote
  #7  
Old 05-23-2008, 11:04 AM
Junior Member
 
Join Date: May 2008
Posts: 1
Default Re: How to know the function name by function pointer's address

hi,
i am trying to do something similar, i have used backtrace and backtrace_symbols
but even symbols is giving library .so name and address.
How do i get the function name from it?
Can someone please help me.

Thanks.
~taker
Reply With Quote
Reply

Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
v2.6.25-4569-gb69d398: Section mismatch in reference from the function uniq_ioapic_id() to the function .init.text:io_apic_get_unique_id() unix Kernel 0 04-25-2008 08:40 AM
[PATCH 1/8] x86_64: Change GET_APIC_ID() from an inline function to an out-of-line function unix Kernel 0 03-28-2008 07:20 PM
[RFC 1/8] x86_64: Change GET_APIC_ID() from an inline function to an out-of-line function unix Kernel 3 03-25-2008 02:20 AM
Incorrect function address placed in function pointer in dynamicallylinked shared library unix Unix 2 01-12-2008 03:56 AM
issues while calling a javascript function from a function defined in XPCOM Implementation file . unix Mozilla 6 09-11-2007 04:52 AM


All times are GMT. The time now is 08:35 AM.