473,788 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Print function name of calling function?

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!

Feb 20 '07
15 22863
matevzb wrote:
On Feb 20, 8:26 am, "dspfun" <dsp...@hotmail .comwrote:
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().
If I remember correctly, a C99 implementation should implement the
__func__ identifier. It's implicitly declared and equals to
static const char __func__[] = "function-name";
You are correct. However, __func__ contains the name of the current
function, not of the calling function.

Feb 20 '07 #11
On Feb 20, 10:09 am, "Harald van Dijk" <true...@gmail. comwrote:
matevzb wrote:
On Feb 20, 8:26 am, "dspfun" <dsp...@hotmail .comwrote:
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().
If I remember correctly, a C99 implementation should implement the
__func__ identifier. It's implicitly declared and equals to
static const char __func__[] = "function-name";

You are correct. However, __func__ contains the name of the current
function, not of the calling function.
Sorry, I misread the OP's question. In that case it's probably best if
the __func__ is passed to the called function.
--
WYCIWYG - what you C is what you get

Feb 20 '07 #12
Is it possible to print the function name of the calling function?
>
Not portably.

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

So the caller can't rely on f3() behaving consistently?

If you need more info than you pass in the function arguments, the
obvious suggestion is to simply add some more function arguments.

In gdb it is possible to get a call trace, how can gdb get the call
trace? Does gdb use some OS-specific functions for this?

gdb uses system-specific ways to get the function's addresses, but
loads the compiler-generated debug info (including the calling
function's name) from the executable. In your own code, you cannot
even assume that your own program can read itself, let alone rely on a
specific debugging format.
It gets worse than that. Also, gdb has been known to get this
WRONG, especially if something managed to clobber the stack (if
there is one) frame.

- You cannot assume that your own program can even FIND itself. argv[0]
need not be a *file* name. Also, it's easy for malicious invokers to
fake this information. Same goes for getenv("PATH").
- Not all systems have things like /proc/curproc/file (FreeBSD) or
/proc/self/exe (Linux) which provide a symlink to the executable.
You still might not have permission to access it in any case.
- You cannot assume that you have read permission on the executable.
Execute-only permission is possible in a number of operating systems.
Plus, the higher-level directory permissions may not permit access.
- You cannot assume that you can open the executable for read while it's
running. Some operating systems give an ETXTBSY error. (More give
ETXTBSY if you try opening for *write*).
- You cannot assume that the executable has any debugging symbols in *ANY*
format, much less one that you can interpret. They may not be generated,
or they may be stripped off later for production use. Among other things,
in a large software distribution, those symbols can take up a lot of
disk space. And closed-source software distributions will probably remove
symbols to avoid giving away too much information.
- Even if the executable has debugging symbols, you may not have all the
debugging symbols you need (e.g. for libraries, static-linked or dynamic-
linked). Sometimes a library function can call YOUR function (e.g.
qsort calling a compare function).
- Finding the symbols for a dynamic-linked library can be very
system-dependent. No, you can't depend on having dynamic-linked libraries,
but neither can you depend on NOT having them.
- Even if you have all the function addresses, stack (if there is one) frame
formats and calling sequences are very system-dependent.
- The symbol associated with the function name may bear minimal resemblance
to the actual function name (C++ name-mangling, for example).

Feb 21 '07 #13
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().
If I remember correctly, a C99 implementation should implement the
__func__ identifier. It's implicitly declared and equals to
static const char __func__[] = "function-name";

You are correct. However, __func__ contains the name of the current
function, not of the calling function.
True, but you could still use it to advantage. Define an N-argument
macro f3(), which calls f3_real() with the N arguments plus an added
one, passed as __func__. Now, the name of the calling function is
passed automatically.
Feb 21 '07 #14
Harald van D?k wrote:
matevzb wrote:
>On Feb 20, 8:26 am, "dspfun" <dsp...@hotmail .comwrote:
>>>
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().

If I remember correctly, a C99 implementation should implement the
__func__ identifier. It's implicitly declared and equals to
static const char __func__[] = "function-name";

You are correct. However, __func__ contains the name of the current
function, not of the calling function.
Exactly. So where do you think the '__func__' should be used? The
result is a char* pointer, which you can pass wherever you think it
might be of interest.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 21 '07 #15
On Feb 20, 5:51 pm, gordonb.vr...@b urditt.org (Gordon Burditt) wrote:
gdb uses system-specific ways to get the function's addresses, but
loads the compiler-generated debug info (including the calling
function's name) from the executable. In your own code, you cannot
even assume that your own program can read itself, let alone rely on a
specific debugging format.

It gets worse than that. Also, gdb has been known to get this
WRONG, especially if something managed to clobber the stack (if
there is one) frame.

While I can't speak to GDB, I've seen plenty of situations where
somewhat odd (but still valid) stack frames exist due to various sorts
of optimization, and which confuse things trying to trace "standard"
stack frames.

Inlined functions will often have that attribute.

Feb 21 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3342
by: DOA | last post by:
Help! I've been doing C language development for a long time, but this problem has me stumped. I would like the user to be able to enter the name of a function that has been linked into the application they are currently running, and have the application call the function they have requested. Example code: char fn_name;
6
1705
by: SDK Builder | last post by:
Found this in CSS group - instant function/style/tag... search for PHP, CSS, HTML, MySQL etc. Here: http://www.gotapi.com For PHP you click Add/Remove on top and load PHP modue. Some other interesting stuff there too. Enjoy.
10
3620
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a cast if necessary but preferably to one of the /right/ function pointer
12
17263
by: sinbad | last post by:
hi, is it possible to get function name from function pointer. I am using gcc compiler on linux. thanks
4
2659
by: conics | last post by:
what can i do when on compiling a "C" program i have arrive at the error of out of memory in function <name of function> thanks in advance.. conics
0
9656
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
9498
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
10364
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
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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...
0
6750
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();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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 we have to send another system
2
3670
muto222
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.