469,306 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

How to know the know calling function name by C programme

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 in a problem that , i have seen that one fucntion is get called by from some 160 postions. and i need to solve some problem and it is very probmatic to get print from 160 postions.So ,is there any way to get to know who is calling that function in runtime ? Is there any small programme or trick will be there , get to know who is calling that function in runtime.so i can print that function name inside that called function or any other way and my work will be easy .

Please help me on this.
I am waiting for your reply
Thanks
mcarthy
Jun 15 '09 #1
11 19064
Banfa
9,064 Expert Mod 8TB
Well here is a hack that might help you, it wont identify the calling function but will identify the calling line of code.

Lets assume that you function is defined like so

Expand|Select|Wrap|Line Numbers
  1. int aFunction(int param)
  2. {
  3.     ...
  4. }
  5.  
Declared in a header like so

Expand|Select|Wrap|Line Numbers
  1. extern int aFunction(int param);
  2.  
and called like so

Expand|Select|Wrap|Line Numbers
  1. int result;
  2. int param;
  3.  
  4. // initialise param
  5.  
  6. ...
  7.  
  8. result = aFunction(param);
  9.  
  10. ...
  11.  
  12. // use result
  13.  
You make use of the __LINE__ and __FILE__ predefine macros in C that resolve to the current line number and current file name. You can use these along with the C/C++ preprocessor to crowbar some debuging into your code by making the following modifications

Cahnge the function definition to
Expand|Select|Wrap|Line Numbers
  1. int aFunctionWithDebug(int param, const char *file, int line)
  2. {
  3.     printf("%s(%d): Called aFunction\n", file, line);
  4.     ...
  5. }
  6.  
Change the declaration in the header file to

Expand|Select|Wrap|Line Numbers
  1. extern int aFunctionWithDebug(int param);
  2. #define aFunction(param)       aFunctionWithDebug(param)
  3.  
and don't change the call to the function at all.

Expand|Select|Wrap|Line Numbers
  1. int result;
  2. int param;
  3.  
  4. // initialise param
  5.  
  6. ...
  7.  
  8. result = aFunction(param);
  9.  
  10. ...
  11.  
  12. // use result
  13.  
You will need to recompile your entire source (which sounds like it could take a while) and if you get the macro or any calls to the function wrong you will get not entirely intuitive compiler diagnostics but the printf would output something like

/home/user/source/file.c(45): called a function

which would allow you to locate the function call.

If you are using a C99 compiler (gcc for instance) then you could use the __FUNCTION__ in a similar manor. This actually resolves to the current function name. gcc has an extension of this __PRETTY_FUNCTION__ which outputs the function declaration.
Jun 15 '09 #2
donbock
2,422 Expert 2GB
If you can find out the calling conventions of your particular compiler then you can hack a very nonportable mechanism for printing the call tree [who called who to get to where you are].

For instance, perhaps your compiler invariably creates a stack frame by loading one particular register with the value of the stack pointer upon entry to each function. Unwrapping the stack frames is the same as unwrapping the function calls.

This technique only gives you the address from which each function was called; you will have to consult a link map to translate that to function name.

Be sure your trace unwrapper will terminate so you don't loop forever.
Jun 15 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
I would recommend installing a logfile.

By this I mean a circular text file of, say 256 fixed-length records. That is a flat file. The first record contains a counter indicating the record to write to next. After writing, the counter in the first record is incremented. When it gets to 255, it is reset to 1 (record 0 has the counter, record 1 is the first log entry).

This way the file never has more than 256 records and can run indefinitely without requiring maintenance of any sort.

In addition, I would add a system setting that the logfile fuinction can test to determine whether logging is turned on or not. When logging is off, the logfile function is a null function. Usually, I have a second setting for the logfile pathname.

Implement this as you would fprintf:

Expand|Select|Wrap|Line Numbers
  1. int LogFile(char* arg, ...);
Then when you call a function you could:

Expand|Select|Wrap|Line Numbers
  1. void MyFunc(char* arg)
  2. {
  3.    Logfile("%s%d", "MyFunc calling YourFunc with a", 10);
  4.    YourFunc(10);
  5.  
  6. }
The Logfile function now becomes part of your production program. In crash cases the logfile is cool since after the program aborts, the last 256 log entries are still in the logfile and are the input to the crash autopsy.
Jun 16 '09 #4
donbock
2,422 Expert 2GB
You can add a severity argument to the Logfile function; for example, severity could be an enumerated type with values Failure, Warning, and Memo. Then provide another function to set the minimum severity that gets written into the log.
Jun 16 '09 #5
donbock
2,422 Expert 2GB
C99 supports preprocessor macros with a variable number of parameters; however C89 does not. If your compiler supports this feature then you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. int LogFileFunc(const char *file, int line, const char *fmt, ...);
  2.  
  3. #define LogFile(fmt, ...) \
  4.     LogFileFunc(__FILE__, __LINE__, fmt, __VA_ARGS__)
The macro automates inclusion of the file name and line number in the LogFileFunc call. You could do this without a macro if you took the time to always type "__FILE__,__LINE__," as the first arguments to LogFileFunc.

I know C++ users don't like to use preprocessor macros. Is there some other clever way to do this in C++?
Jun 16 '09 #6
hi ,
My question is to know the caller function name because if i put __FILE__ in my function , obviously it will not help to know who is caller of my function.
According to donbock how to print the stack tree or capture the stack pointer or link register , so it will help in crash time also heavily..

Regarding log file also , i have to put everywhere where a function are get called., so it will not help me to solve bug/crash quick way.

How other editor capturing the stack tree, so cant we make such kind of way.

Any help .please
Mcarthy
Jun 16 '09 #7
JosAH
11,448 Expert 8TB
@mcarthybn
If you have one function per file ...

kind regards,

Jos
Jun 16 '09 #8
donbock
2,422 Expert 2GB
@JosAH
That won't tell you who the caller of your function was. The only way to do that is to unwind the return addresses from the stack. The OP needs to research the details of how his compiler implements function calls.
Jun 16 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
Unwinding the stack will not produce the function names. Everything is in binary.

If you need the actual function name, then you have to compile it into the program.

I'm sorry if it takes code to implement a log file to capture calling function names but that's the way it is. It is no more difficult than implementing reference counting in C.

I would write the logfile function and get it working. Then I would install the logfile function calls in what I deem to be the most likely problem areas. Most likely at the end of the day, you won't need to log every last function call.

My logfile entries are more general in nature and reflect the type of processing that's going on.
Jun 17 '09 #10
how about using StackWalk64 API
have a look at http://msdn.microsoft.com/en-us/library/ms680650%28VS.85%29.aspx
I have attached a sample project
Attached Files
File Type: zip StackWalker.zip (64.2 KB, 307 views)
Jan 11 '10 #11
You can use this if you use gcc.

http://www.gnu.org/s/libc/manual/htm...acktraces.html
I like donbock's solution a macro instead of the actual function which gives line numbers too. If you dont want that , walk through the stack using the backtrace function. And resolve symbols.
Nov 3 '10 #12

Post your reply

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

Similar topics

8 posts views Thread by Muthu | last post: by
161 posts views Thread by KraftDiner | last post: by
15 posts views Thread by dspfun | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.