473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling function name

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

Nov 14 '05 #1
16 2010
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


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

#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
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
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
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


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
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
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
"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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2935
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
10
600
by: headware | last post by:
I know that you can call the method of one from from inside another form by doing something like this Forms("MyForm").MyFunction(12, 34) However, you have to know that MyForm has a function...
2
9743
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
17
2175
by: Bill Grigg | last post by:
I have been successfully calling DLL's using VC++ 6.0 and also using VC++7.1 (.NET). I only mention this because I have never felt comfortable with the process, but nonetheless it did work....
4
3324
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias...
18
4311
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
15
22791
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
11
3169
by: briankirkpatrick | last post by:
Forgive me if my post seems a little amateurish... I'm requesting assistance from some of you smart folks out there to get the managed calls write that meet the specification in the esa.h for...
47
3228
by: mukeshrasm | last post by:
Hi I am calling two pages using Ajax Get_Pages.php and Get_Content.php from combo box. Both pages are displayed based on selection from combo box. Main problem is that it is not showing the...
4
3565
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.