Hi all
I have a function and it is being called by many other functions. Is there any method or macro which can return the function name which calls the current function.

EX:

int a()
{
  pintf (<required a macro for printing the caller function name>);
  /* do operation */
}

int b()
{
  /* do operation */
  a();
  /* do operation */
}

int c()
{
  /* do operation */
  a();
  /* do operation */
}
 ....
 ....
 ....

int main()
{
  /* do operation */
  a();
  b();
  c();
  /* do operation */
}

Please help me, its very urgent for my assignment debugging.

Thanks in advance
Kandarpa

Some compiler IDEs such as Microsoft can provide that information, but C and C++ lcnaugages know nothing about function names -- only addresses.

A small example:
gcc/g++ provides __LINE__ to get the linenumber and __FILE__ to get the file name in sucha way that do we have any macro to get the caller function name?

Not that I am aware of. If you want to add it as a parameter to a function then just do it yourself, such as foo("MyFunction");

C99 added __func__, which evaluates to the name of the currently executing function. __FUNCTION__ is a common non-standard extension that does the basically same thing.

Thakns Ancient Dragon, Narue for ur replies.

Narue :
__func__ returns current function but I need caller function.

I have tried following code, please let me know ur inputs:
In a() function I have added following code:

void a()
{
pid_t=getpid();
char cmd[50];
sprintf(cmd, "pstack %d >> stackTrace;", pid_t);
system(cmd);

/* do the operation */
}

I think this is not a correct procedure but i felt that this is simple to get the stack trace. please let me know ur responce.

>__func__ returns current function but I need caller function.
Did it occur to you to pass the result of __func__ to the called function? The result you want can be had fairly easily, but you'll need to compromise a little. There's not a simple and fast way to get the calling function name from within the called function without some kind of scaffolding in place.

Redefine your function with a macro that logs FILE, FUNCTION, LINE message and then calls the function. Hope this helps.

e.g. #define LogAndCallA() printf("Calling from %s:%s:%d",FILE,_FUNCTION__,LINE); a()

Replace calls of a() with LogAndCallA() macro.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.