473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is there any to know which has called my function

14 New Member
Hi all,
can anyone tell me the way to know which function has called my function, i.e., I want to know the calling function name in my function. Is there any predefined macro like __FUNCTION__ to know the called function

Thanks in Advance
Feb 26 '09 #1
4 1954
donbock
2,426 Recognized Expert Top Contributor
The C Standard doesn't provide any portable way to do it.

If you have an intimate knowledge of how your compiler and processor manage the stack it can be possible to write a nonportable function that unwinds the call stack to find the address from whence its caller was called. This is not quite the same thing as what you're asking for.
Feb 26 '09 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
The easiest thing to do is learn how to use your debugger.

Every professional quality debugger has the ability to display the call stack. Not only can you see the line of code that called your function, you can also see the line of code that called the function that called your function right back to the operating system calling your main().
Feb 26 '09 #3
Savage
1,764 Recognized Expert Top Contributor
Do you mean something like this:

Expand|Select|Wrap|Line Numbers
  1. void PrintInfo(char *Caller,char *Func)
  2. {
  3.     printf("Caller:%s \t Func:%s",Caller,Func);
  4. }
  5.  
  6. #define CALL(FUNCTION,RET,...){PrintInfo(__FUNCTION__,#FUNCTION);RET=FUNCTION(__VA_ARGS__);}
  7.  
  8. It's used like this:
  9.  
  10. int Sum(int a,int b,int c)
  11. {
  12.     return a+b+c;
  13. }
  14.  
  15. void DoWork()
  16. {
  17.     int ret=0;
  18.  
  19.     CALL(Sum,ret,10,15,20);
  20.  
  21.     printf("\nSum: %d \n",ret);
  22. }
?

So how does this macro works:

This macro uses previously defined function to print(or store info for later use which is what you want to do ).Parameters for that functions are __FUNCTION__ which is the calling function,and second parameter is made by using the preprocessor tokenizer which turns the called function name into a string using the # operator.
Then it calls the function storing the return value if any in RET and passing the variable argument list to it.(Separate macro needed for void functions)
Feb 26 '09 #4
maheshgupta024
14 New Member
#define CALL(FUNCTION,RET,...){PrintInfo(__FUNCTION__,#FUN CTION);RET=FUNCTION(__VA_ARGS__);}

Thanks Savage.. Im looking to get a macro like this.. This will be good input for me.
Mar 5 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Shi Jin | last post by:
Hi there, While I was trying to disassemble a simple C program for my OS course, I found that the call to function longjmp is actually called to siglongjmp under linux. And similarly, under...
14
by: mluvw47 | last post by:
Know the answers to the following? Line Contains 50 char *b, q, *r; 200 b = getbuf (); 201 q = *b; 212 R = anotherfunction (b); 213-2003 /* we want to use 'q' and 'r'...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
9
by: Coleen | last post by:
Hi All :-) I am desperately looking for some help/information on how to direct page flow. Forget what I have done - here's what I need to do: I have a large ASPX.Net - VB.Net web application...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
1
by: rasmidas | last post by:
I have a function sEntityFree() which is a function from a third party S/w we are using. For this we have our enhanced code. sEntityFree() has been called inside our enhanced code 2000 or 3000...
1
by: laila2ethan | last post by:
Need someones help , I am having a very difficult time understanding parameters and functions, can someone help me answer these questions 1. Write a program that helps an elementary school student...
0
by: Avi | last post by:
Hi experts, I have a function that needs to be public. However, I have a few objects in my system that I would want to prevent them from calling this function. Is it possible to know...
12
by: mcarthybn | last post by:
hi , I am working on huge C programming project.I am doin debugging only by printf measn i dont have any dubugger .So it is very difficult know who is actually calling a function and i am stuck...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.