473,773 Members | 2,315 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 #1
15 22862
In article <11************ **********@l53g 2000cwa.googleg roups.com>,
dspfun <ds****@hotmail .comwrote:
>Is it possible to print the function name of the calling function?
There is no facility for that in standard C.

>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().
Some systems provide extensions to allow the calling stack to
be examined, but C doesn't require it, and it isn't necessarily
easy even on systems where it can be done.
--
Programming is what happens while you're busy making other plans.
Feb 20 '07 #2
dspfun wrote:
Hi,

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.

Feb 20 '07 #3
On 20 Feb, 08:32, "Harald van Dijk" <true...@gmail. comwrote:
dspfun wrote:
Hi,
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?

Feb 20 '07 #4
Yes it is possible in gdb. Use the command "bt" to get a stack trace.

On Feb 20, 4:42 pm, "dspfun" <dsp...@hotmail .comwrote:
On 20 Feb, 08:32, "Harald van Dijk" <true...@gmail. comwrote:
dspfun wrote:
Hi,
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?

Feb 20 '07 #5
dspfun wrote:
On 20 Feb, 08:32, "Harald van Dijk" <true...@gmail. comwrote:
dspfun wrote:
Hi,
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.

Feb 20 '07 #6
On 20 Feb, 08:51, "Harald van Dijk" <true...@gmail. comwrote:
dspfun wrote:
On 20 Feb, 08:32, "Harald van Dijk" <true...@gmail. comwrote:
dspfun wrote:
Hi,
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.- Dölj citerad text -

- Visa citerad text -
What is the reason that a program cannot read itself? Why can't the
program rely on the debugging format (except for portability reasons)?

Feb 20 '07 #7
dspfun wrote:
On 20 Feb, 08:51, "Harald van Dijk" <true...@gmail. comwrote:
dspfun wrote:
On 20 Feb, 08:32, "Harald van Dijk" <true...@gmail. comwrote:
dspfun wrote:
Hi,
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 bef1()
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.

What is the reason that a program cannot read itself?
Many reasons, but for a simple one, on a Unix-like system, try chmod a-
r executable. You (and thus the program) won't be able to read it, but
you can still run it.
Why can't the
program rely on the debugging format (except for portability reasons)?
Well, if you don't count the reasons that a program cannot rely on a
specific debug format, then there are no reasons. :) Seriously, the
debugging format is not even sure to remain the same with different
versions of the same compiler for the same system.

Feb 20 '07 #8
dspfun wrote:
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 you want functions to know the names of their callers, pass it as an
argument.
Feb 20 '07 #9
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";
--
WYCIWYG - what you C is what you get

Feb 20 '07 #10

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
1700
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
3619
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
17258
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
9621
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
9454
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
10264
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
10106
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
8937
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
7463
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
5355
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
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.