PDA

View Full Version : How to get the name of source code function() at run-time ?


bzchevalier
12-22-2003, 07:59 AM
Hello,

Do you know, please, if it exists a function or a macro that gives the name of the function (name in the source code) where we are at this point of the program execution.

It my question is not very clear, here is a code example :


void my_function (char *prefix)
{
fprintf(stderr,"[%s] Here we are in : %s",prefix,help_me(which_param));
}


help_me() is the name of the function that I search. And the execution of my_function whould display :

[my_prefix] Here we are in : my_function

If this function doesn't exist in system calls or C library, can we write it in a simply way ?

Thanks in advance for all your indications.

bzchevalier

i3839
12-22-2003, 01:47 PM
Use gcc's __FUNCTION__ or the standard C99 __func__

Example:
printf("Running function '%s' in file '%s', line %i", __func__, __FILE__, __LINE__);

felix
12-24-2003, 11:59 AM
Hi!

Use gcc's __FUNCTION__ or the standard C99 __func__

Example:
printf("Running function '%s' in file '%s', line %i", __func__, __FILE__, __LINE__);

Very cool!! Very instersting....

I have a doubt based on it... suposse that we write a wrapper for glibc (like http://www.developerweb.net/forum/viewtopic.php?t=762 ) if i use the example above, it will print the data about the wrapper... exist a way to do it print the data of who call the wrapper, or who call the caller (recursivilly), etc.., exist a way to do it ?

Thkz a lot and Marry Christmas!

[ ]'s

i3839
12-24-2003, 05:34 PM
Not that I know.. But if you want such info, then run the program in a debugger like GDB and put a breakpoint at your wrapper function (then you can run some command to get the callgraph or whatever, I've not much experience with debuggers yet).

felix
12-26-2003, 12:24 PM
Hi i3839,

Not that I know.. But if you want such info, then run the program in a debugger like GDB and put a breakpoint at your wrapper function (then you can run some command to get the callgraph or whatever, I've not much experience with debuggers yet).

Thkz for help. I can't use a compiler... my idea is get this information to generate logs. For example NOW i'm only doing getppid() and getting the pid and lloking at /proc/pid/cdmline and getting the name of process to generate log from the wrapper. But if exist a way to do something in the wrapper similar to "gcc's __FUNCTION__ or the standard C99 __func__" would be much better... :)

Thkz a lot.

Regards.