By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,038 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,038 IT Pros & Developers. It's quick & easy.

Calling function name

P: n/a
Is there an way for a function to know who called him ?

Nov 14 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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;
}

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


Nov 14 '05 #2

P: n/a


baumann@pan wrote:
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;
}

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


Is there another way?
This way look like:

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

Nov 14 '05 #3

P: n/a

#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;
}
baumann@pan wrote:
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;
}

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


Nov 14 '05 #4

P: n/a
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;
}

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


Nov 14 '05 #5

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

Nov 14 '05 #6

P: n/a
baumann@pan wrote:

the lastest C standard has __function__ you use as argument


It's not in N869.
What section is it in, in the last or latest C standard?

--
pete
Nov 14 '05 #7

P: n/a


Saroj wrote:
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).

Nov 14 '05 #8

P: n/a
pete <pf*****@mindspring.com> wrote:
baumann@pan wrote:

the lastest C standard has __function__ you use as argument
It's not in N869.


That's because it's actually spelled __func__.
What section is it in, in the last or latest C standard?


6.4.2.2.

Richard
Nov 14 '05 #9

P: n/a
On 6 Jun 2005 03:41:33 -0700, "Saroj" <sa*************@gmail.com>
wrote:
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.

Nov 14 '05 #10

P: n/a
"Saroj" <sa*************@gmail.com> wrote:
# 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Nov 14 '05 #11

P: n/a
Richard Bos wrote:

pete <pf*****@mindspring.com> wrote:
baumann@pan wrote:

the lastest C standard has __function__ you use as argument


It's not in N869.


That's because it's actually spelled __func__.


Thank you.

--
pete
Nov 14 '05 #12

P: n/a
ju**********@yahoo.co.in writes:
Saroj wrote:
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).


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13

P: n/a
On 6 Jun 2005 00:20:53 -0700, in comp.lang.c , "Saroj"
<sa*************@gmail.com> wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #14

P: n/a
"Saroj" <sa*************@gmail.com> writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #15

P: n/a
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'

Nov 14 '05 #16

P: n/a
On Tue, 07 Jun 2005 04:08:37 -0700, CdiVer wrote:
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

Nov 14 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.