Is there a way to find out the name of a function that calls

Hello.

I was wondering if there is any way within a C program to determine the name
of a function that has called another function. So, let’s say we have 3
functions: foo(), bar() and baz(). These functions call a function called
quux(). Is there a way that I could, from quux(), determine which of the
three functions called me? I was curious if this was possible, since there
are macros like FILE and LINE that indicate what file or line you
are dealing with exist. I know that what I am asking can be done by using a
global variable that everyone can set, but I was wondering if there is a
more elegant way. Thus, if quux() was a function that was used for printing
out errors, I could find out the function name where quux() was called from
and that would aid the composition of an error message.

TIA.

Rodney Lott

Rodney Lott <rlott@fct.ca> wrote:

Hello.

I was wondering if there is any way within a C program to determine the name
of a function that has called another function. So, let’s say we have 3
functions: foo(), bar() and baz(). These functions call a function called
quux(). Is there a way that I could, from quux(), determine which of the
three functions called me? I was curious if this was possible, since there
are macros like FILE and LINE that indicate what file or line you
are dealing with exist. I know that what I am asking can be done by using a
global variable that everyone can set, but I was wondering if there is a
more elegant way. Thus, if quux() was a function that was used for printing
out errors, I could find out the function name where quux() was called from
and that would aid the composition of an error message.

There’s prolly a more elegant “stack based” solution, but you could simply
#define quux(a,b,c) real_quux(FILE,LINE,a,b,c)

I’ve done this for an “efopen()” and “emalloc()” function that exits if the
fopen() or malloc() doesn’t work, and prints the caller’s location…
I believe that there’s some macro expansion for the current function,
but the FILE and LINE macros should get you close enough, depending on
what you want to do…

:slight_smile:

Cheers,
-RK

TIA.

Rodney Lott


Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Books, Video-based and Instructor-led
Training and Consulting at www.parse.com.
Email my initials at parse dot com.

Rodney Lott <rlott@fct.ca> wrote:

Hello.

Without cooperation of the calling function there isn’t really an
easy way to do this.

You could try examining the call stack for the return location,
and then comparing this to a map file generated at compile time… and
figure things out from there – could be a bit messy though, as Watcom
by default uses register-based calling conventions, so I’m not sure
how complete the stack frames are, or how easy to decode… should still
be doable.

Easiest would be to have all the calling functions pass in a string of
their name when they call you.

I was wondering if there is any way within a C program to determine the name
of a function that has called another function. So, let’s say we have 3
functions: foo(), bar() and baz(). These functions call a function called
quux(). Is there a way that I could, from quux(), determine which of the
three functions called me? I was curious if this was possible, since there
are macros like FILE and LINE that indicate what file or line you
are dealing with exist. I know that what I am asking can be done by using a
global variable that everyone can set, but I was wondering if there is a
more elegant way. Thus, if quux() was a function that was used for printing
out errors, I could find out the function name where quux() was called from
and that would aid the composition of an error message.

The FILE and LINE are pre-processor directives, filled in at that
point. There isn’t, and can’t be such a function for calling routine.
(i.e. Compiler can’t now when, for instance, a function pointer might
be used to call your function.)

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Okay. I just wanted to make sure.

Thanks, David.

Rodney Lott.

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:afalce$s69$1@nntp.qnx.com

Rodney Lott <> rlott@fct.ca> > wrote:
Hello.

Without cooperation of the calling function there isn’t really an
easy way to do this.

You could try examining the call stack for the return location,
and then comparing this to a map file generated at compile time… and
figure things out from there – could be a bit messy though, as Watcom
by default uses register-based calling conventions, so I’m not sure
how complete the stack frames are, or how easy to decode… should still
be doable.

Easiest would be to have all the calling functions pass in a string of
their name when they call you.

I was wondering if there is any way within a C program to determine the
name
of a function that has called another function. So, let’s say we have 3
functions: foo(), bar() and baz(). These functions call a function
called
quux(). Is there a way that I could, from quux(), determine which of
the
three functions called me? I was curious if this was possible, since
there
are macros like FILE and LINE that indicate what file or line
you
are dealing with exist. I know that what I am asking can be done by
using a
global variable that everyone can set, but I was wondering if there is a
more elegant way. Thus, if quux() was a function that was used for
printing
out errors, I could find out the function name where quux() was called
from
and that would aid the composition of an error message.

The FILE and LINE are pre-processor directives, filled in at that
point. There isn’t, and can’t be such a function for calling routine.
(i.e. Compiler can’t now when, for instance, a function pointer might
be used to call your function.)

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

You could always setup a table of function addresses/strings, and every
function you call you pass in the address of the caller as a parameter.
Using macros to do this would make life easier as well. Then when you need
to translate the function address you can always do a table lookup.

Or even better, as part of a preamble/postamble for each function, you push
the function pointer on a global callstack (your own) and pop it off when
you come out of each function. Then use the table lookup when you want to
“stringify” it.

Cheers,
Adam

QNX Software Systems Ltd.
[ amallory@qnx.com ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>

“Rodney Lott” <rlott@fct.ca> wrote in message
news:afcbdb$7oi$1@nntp.qnx.com

Okay. I just wanted to make sure.

Thanks, David.

Rodney Lott.

“David Gibbs” <> dagibbs@qnx.com> > wrote in message
news:afalce$s69$> 1@nntp.qnx.com> …
Rodney Lott <> rlott@fct.ca> > wrote:
Hello.

Without cooperation of the calling function there isn’t really an
easy way to do this.

You could try examining the call stack for the return location,
and then comparing this to a map file generated at compile time… and
figure things out from there – could be a bit messy though, as Watcom
by default uses register-based calling conventions, so I’m not sure
how complete the stack frames are, or how easy to decode… should still
be doable.

Easiest would be to have all the calling functions pass in a string of
their name when they call you.

I was wondering if there is any way within a C program to determine
the
name
of a function that has called another function. So, let’s say we have
3
functions: foo(), bar() and baz(). These functions call a function
called
quux(). Is there a way that I could, from quux(), determine which of
the
three functions called me? I was curious if this was possible, since
there
are macros like FILE and LINE that indicate what file or line
you
are dealing with exist. I know that what I am asking can be done by
using a
global variable that everyone can set, but I was wondering if there is
a
more elegant way. Thus, if quux() was a function that was used for
printing
out errors, I could find out the function name where quux() was called
from
and that would aid the composition of an error message.

The FILE and LINE are pre-processor directives, filled in at
that
point. There isn’t, and can’t be such a function for calling routine.
(i.e. Compiler can’t now when, for instance, a function pointer might
be used to call your function.)

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

You could always setup a table of function addresses/strings, and every
function you call you pass in the address of the caller as a parameter.
Using macros to do this would make life easier as well. Then when you
need
to translate the function address you can always do a table lookup.

Then he could pass a function name as a string as well - no lookup needed.

But if you asked me, I would say that all these tricks are a bit… eh…
tricky. Why make life harder for someone who will read your source?