Connecting Tech Pros Worldwide Help | Site Map

Calling function name

Saroj
Guest
 
Posts: n/a
#1: Nov 14 '05
Is there an way for a function to know who called him ?

baumann@pan
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Calling function name


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:[color=blue]
> Is there an way for a function to know who called him ?[/color]

Prawit Chaivong
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Calling function name




baumann@pan wrote:[color=blue]
> 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:[color=green]
> > Is there an way for a function to know who called him ?[/color][/color]

Is there another way?
This way look like:

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

baumann@pan
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Calling function name



#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:[color=blue]
> 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:[color=green]
> > Is there an way for a function to know who called him ?[/color][/color]

baumann@pan
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Calling function name


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:[color=blue]
> Is there an way for a function to know who called him ?[/color]

Saroj
Guest
 
Posts: n/a
#6: Nov 14 '05

re: Calling function name


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

pete
Guest
 
Posts: n/a
#7: Nov 14 '05

re: Calling function name


baumann@pan wrote:[color=blue]
>
> the lastest C standard has __function__ you use as argument[/color]

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

--
pete
junky_fellow@yahoo.co.in
Guest
 
Posts: n/a
#8: Nov 14 '05

re: Calling function name




Saroj wrote:[color=blue]
> Is there an way for a function to know who called him ?[/color]

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).

Richard Bos
Guest
 
Posts: n/a
#9: Nov 14 '05

re: Calling function name


pete <pfiland@mindspring.com> wrote:
[color=blue]
> baumann@pan wrote:[color=green]
> >
> > the lastest C standard has __function__ you use as argument[/color]
>
> It's not in N869.[/color]

That's because it's actually spelled __func__.
[color=blue]
> What section is it in, in the last or latest C standard?[/color]

6.4.2.2.

Richard
Paul Mesken
Guest
 
Posts: n/a
#10: Nov 14 '05

re: Calling function name


On 6 Jun 2005 03:41:33 -0700, "Saroj" <sarojkmohapatra@gmail.com>
wrote:
[color=blue]
>all this means i should also have a access to modify the function who
>is calling.
>dont think it will work.[/color]

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.

SM Ryan
Guest
 
Posts: n/a
#11: Nov 14 '05

re: Calling function name


"Saroj" <sarojkmohapatra@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.
pete
Guest
 
Posts: n/a
#12: Nov 14 '05

re: Calling function name


Richard Bos wrote:[color=blue]
>
> pete <pfiland@mindspring.com> wrote:
>[color=green]
> > baumann@pan wrote:[color=darkred]
> > >
> > > the lastest C standard has __function__ you use as argument[/color]
> >
> > It's not in N869.[/color]
>
> That's because it's actually spelled __func__.[/color]

Thank you.

--
pete
Keith Thompson
Guest
 
Posts: n/a
#13: Nov 14 '05

re: Calling function name


junky_fellow@yahoo.co.in writes:[color=blue]
> Saroj wrote:[color=green]
>> Is there an way for a function to know who called him ?[/color]
>
> 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).[/color]

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) kst-u@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.
Mark McIntyre
Guest
 
Posts: n/a
#14: Nov 14 '05

re: Calling function name


On 6 Jun 2005 00:20:53 -0700, in comp.lang.c , "Saroj"
<sarojkmohapatra@gmail.com> wrote:
[color=blue]
>Is there an way for a function to know who called him ?[/color]

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 =----
Ben Pfaff
Guest
 
Posts: n/a
#15: Nov 14 '05

re: Calling function name


"Saroj" <sarojkmohapatra@gmail.com> writes:
[color=blue]
> Is there an way for a function to know who called him ?[/color]

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;}
CdiVer
Guest
 
Posts: n/a
#16: Nov 14 '05

re: Calling function name


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'

Lawrence Kirby
Guest
 
Posts: n/a
#17: Nov 14 '05

re: Calling function name


On Tue, 07 Jun 2005 04:08:37 -0700, CdiVer wrote:
[color=blue]
> hi there to know who called the function there is THIS pointer (*this)
> which points at the object which invoked the function[/color]

But not the caller, i.e. the code that called the function.
[color=blue]
> BUT This is done
> only IN C++[/color]

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

Lawrence

Closed Thread