Calling function name

B

baumann@pan

lastest gcc ,vs.net support __FUNCTION__, use it

#include <stdio.h>

int func1(char * str)
{
printf("called by %s\n",str);
return 0;
}

int main(void)
{
func1(__FUNCTION__);
return 0;
}
 
P

Prawit Chaivong

baumann@pan said:
lastest gcc ,vs.net support __FUNCTION__, use it

#include <stdio.h>

int func1(char * str)
{
printf("called by %s\n",str);
return 0;
}

int main(void)
{
func1(__FUNCTION__);
return 0;
}

Is there another way?
This way look like:

int main()
{
func1("main"); // but need to chage func1(char*) to
// func1(const char*)
return 0;
}
 
B

baumann@pan

#if DISPLAY_CALLER
#define func1_hooked() func1(__FUNCTION__)
#else
#define func1_hooked() func1(0)
#endif

now

in main,

int main(void)
{
func1_hooked();
return 0;
}
 
B

baumann@pan

the lastest C standard has __function__ you use as argument

int func1(char * str)
{
printf("called by %s\n",str);
return 0;
}
int main(void)
{
func1(__FUNCTION__);
return 0;
}
 
S

Saroj

all this means i should also have a access to modify the function who
is calling.
dont think it will work.
 
J

junky_fellow

Saroj said:
Is there an way for a function to know who called him ?

I think it cannot be done portably. You will need to write
an implementation specific code for that.
You will need to know the C calling convention for that
architecture. For eg. in PowerPC, when a function is called
the return address is put in Link Register (LR).
 
P

Paul Mesken

all this means i should also have a access to modify the function who
is calling.
dont think it will work.

You mean as in "self modifying code"? C cannot modify its own code,
only interpreted languages or Assembly (depending on whether the
system allows it) might offer this capability.

The return address (which can typically be extracted by using inline
Assembly, if your compiler allows such a thing) is of little use. It's
not the function pointer, only the place (in the calling function)
which will be returned to. Also, such a thing is highly unportable.

If you want to know which function has called another function then
you have to pass some suitable parameter indicating the calling
function.
 
S

SM Ryan

# Is there an way for a function to know who called him ?

There's no standard way to inspect your own call stack. On some systems
you can use the debugger or loader tables used by the debugger.
 
K

Keith Thompson

I think it cannot be done portably. You will need to write
an implementation specific code for that.
You will need to know the C calling convention for that
architecture. For eg. in PowerPC, when a function is called
the return address is put in Link Register (LR).

That will, at best, give you the address of the caller, not its name.

Anything like that is horrendously non-portable, and probably not
worth the effort. If that's the solution, try to redefine the
problem.
 
M

Mark McIntyre

Is there an way for a function to know who called him ?

Only by rewriting the function signature to include a string which is
the name of the caller.

There may be system-specific methods such as those used by debuggers,
eg inspecting the function call stack, but you'd need to ask in a
system specific group about those.
 
B

Ben Pfaff

Saroj said:
Is there an way for a function to know who called him ?

Not portably. Some specific environments have specific
solutions; for example, if you are using the GNU C library, you
might be able to find out with backtrace(), but even that won't
always tell you the correct answer.

In some cases there might not even be a way to find out. For
example, if the function that called the current function did so
as its last action, then the compiler might have emitted a "jump"
instruction instead of a "call" instruction, which in turn would
have erased all the obvious evidence that it was the function
that did the call. But even the possibility for this is system
specific.
 
C

CdiVer

hi there to know who called the function there is THIS pointer (*this)
which points at the object which invoked the function BUT This is done
only IN C++
Example
class A
{
public:
void fun()
{
.................

cout<<this;
}
};

void main()
{
A x;// x is a variiable of class A
x.fun();
}

// this pointer points at x and gives the ADD. of 'x'
 
L

Lawrence Kirby

hi there to know who called the function there is THIS pointer (*this)
which points at the object which invoked the function

But not the caller, i.e. the code that called the function.
BUT This is done
only IN C++

Which of course makes it irrelevant to comp.lang.c.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top