473,840 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to know the know calling function name by C programme

7 New Member
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
12 31949
Banfa
9,065 Recognized Expert Moderator Expert
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_FUNCTI ON__ which outputs the function declaration.
Jun 15 '09 #2
donbock
2,426 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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,426 Recognized Expert Top Contributor
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,426 Recognized Expert Top Contributor
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__,__LIN E__," 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
mcarthybn
7 New Member
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 Recognized Expert MVP
@mcarthybn
If you have one function per file ...

kind regards,

Jos
Jun 16 '09 #8
donbock
2,426 Recognized Expert Top Contributor
@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 Recognized Expert Moderator Expert
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

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

Similar topics

8
2970
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 p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
5
2571
by: claus.hirth | last post by:
If I create the function HELLO in schema S01 as follows, @ CREATE FUNCTION S01.HELLO() RETURNS VARCHAR(32) EXTERNAL NAME 'UDFSRVXYZ!sayHelloWorld' LANGUAGE JAVA PARAMETER STYLE DB2GENERAL NO SQL DETERMINISTIC
2
9788
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 towards this approach such as how would it works in multi-thread. Thanks Tony
161
7927
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.. (After reading that
0
3498
by: bog39 | last post by:
We have z/os and DB/2 V. 8 running. I try to create a new UDF using the command CREATE FUNCTION: CREATE FUNCTION CNGETADR (INTEGER) RETURNS CHAR(50) EXTERNAL NAME CNADR001 PARAMETER STYLE DB2SQL WLM ENVIRONMENT WLMENV LANGUAGE COBOL DETERMINISTIC NO SQL NO DBINFO
15
22886
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 could either be f1() or f2(). BRs!
47
3319
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 editor which is called on the Get_Content.php. in Main Page. I have included the js files in the head section. all the files are on the same directory.
1
2033
by: nth | last post by:
I wan to filter the database show out information by using the user login name. Means that when that user login by entering their names n password, if correct then it will go to the other page by showing that user result only, not other user result. Can somebody help me pls?. Following is the login page code: <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!--#include file="Connections/conResult.asp" --> <% Dim Recordset1__MMColParam...
1
1893
by: mrsmith221189 | last post by:
Hello everyone. I'm a novice to XML/XSL so it would be great to learn from the experts. The ouput of my xml file is BBC EPG(Electronic Programme Guide) table. I'm having problems defining a variable name for element in xml file. The output I would like to have is a table is produced with programme name, programme start time,programme duration and programme details. The programme start time and programme duration are taken from another...
0
9860
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9699
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10603
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9440
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7838
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7023
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4076
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3138
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.