By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,730 Members | 1,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,730 IT Pros & Developers. It's quick & easy.

is it possible to find out the caller of a method programmatically

P: n/a
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by
some combination of assembly and pointer manipulation - I can print
the name of the caller of the method
Thanks for any suggestions
Nov 13 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
"2pac" <ca******@yahoo.com> wrote in message
news:d0**************************@posting.google.c om...
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by
some combination of assembly and pointer manipulation - I can print
the name of the caller of the method
Thanks for any suggestions


Unfortunately, the only advice that can be given to you is that
there is no standard support for such a functionality in C.
(except if foo1 explicitly receives information about the caller,
as a function parameter or in some global variable).

But yes, in platform-specific ways and in 'debug mode' compiles,
it is typically possible to obtain the information you need from
the call stack. Sometimes a dedicated API is even provided.
But you will have to ask in a forum specific to your platform
(OS, ISA & compiler)...

Regards,
--
http://www.post1.com/~ivec <> Ivan Vecerina

Nov 13 '05 #2

P: n/a
On 6 Sep 2003 12:46:09 -0700, in comp.lang.c , ca******@yahoo.com
(2pac) wrote:
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2


Your debugger can do this, so obviously its possible. However its not
possible in standard C, you'd need to use platform specific debugging
calls. Read your compiler manual, or ask in a group specialising in
it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #3

P: n/a
2pac wrote:
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by
some combination of assembly and pointer manipulation - I can print
the name of the caller of the method
Thanks for any suggestions
cat foo.c #include <stdio.h>

int foo(const char* function, const char* file, int line) {
fprintf(stderr,
"Function foo(const char*, const char*, int)\n"
"called from function %s in file %s at line %d.\n",
function, file, line);
return 0;
}

int main(int argc, char* argv[]) {
foo(__func__, __FILE__, __LINE__);
return 0;
}
gcc -Wall -std=c99 -pedantic -o foo foo.c
./foo

Function foo(const char*, const char*, int)
called from function main in file foo.c at line 12.

Nov 13 '05 #4

P: n/a
ca******@yahoo.com (2pac) wrote in
news:d0**************************@posting.google.c om on Sat 06 Sep 2003
01:46:09p:
- I can print
the name of the caller of the method
Thanks for any suggestions


C has no methods. C has subroutines, sometimes called functions by those
who know no better. Methods are parts of object-oriented languages. Maybe
you meant to post this in an Objective-C group, maybe you just need to
learn the vocabulary better.

Nov 13 '05 #5

P: n/a
>in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by
No, it is *NOT* obvious that any function names (addresses, probably,
names, no) appear anywhere in an executable (unless placed there by
the programmer with explicit strings or using __FUNCTION__). Even
if they do, it is not obvious that they are ever loaded into memory
when run normally (that is, not under a debugger). And if the
program is run under a debugger, the debugger knows where the symbols
are, the program doesn't. The debugger also knows the file name of
the executable file, and the program doesn't (unless the programmer
hard-coded a guess in there somewhere).

The symbols aren't necessarily the name of the function, either,
especially in compilers that also support C++, where you can have
multiple functions with the same name.

Oh, yes, there is no portable way for a running program to find a
file name for its own executable file. argv[0] can be of spectacularly
little help, especially under UNIX, where argv[0] and the pathname
of the program to run are independent arguments to exec*() that
need not have any relationship to each other.
some combination of assembly and pointer manipulation - I can print
the name of the caller of the method


C doesn't have methods.

Gordon L. Burdit
Nov 13 '05 #6

P: n/a
August Derleth wrote:
C has subroutines, sometimes called functions by those
who know no better.


LOL!

--
pete
Nov 13 '05 #7

P: n/a
j

"August Derleth" <li*****************@onewest.net> wrote in message
news:Xn**********************************@63.223.5 .101...
ca******@yahoo.com (2pac) wrote in
news:d0**************************@posting.google.c om on Sat 06 Sep 2003
01:46:09p:
- I can print
the name of the caller of the method
Thanks for any suggestions


C has no methods. C has subroutines, sometimes called functions by those
who know no better. Methods are parts of object-oriented languages. Maybe
you meant to post this in an Objective-C group, maybe you just need to
learn the vocabulary better.


The standard refers to them as ``functions'' or ``function definition(s)''.
So I don't see how you can say they are referred to as ``functions'' by
those who know no better.
Nov 13 '05 #8

P: n/a

"j" <ja****@bellsouth.net> wrote in message
news:ny***************@bignews3.bellsouth.net...

"August Derleth" <li*****************@onewest.net> wrote in message
news:Xn**********************************@63.223.5 .101...
ca******@yahoo.com (2pac) wrote in
news:d0**************************@posting.google.c om on Sat 06 Sep 2003
01:46:09p:
- I can print
the name of the caller of the method
Thanks for any suggestions
C has no methods. C has subroutines, sometimes called functions by those
who know no better. Methods are parts of object-oriented languages. Maybe you meant to post this in an Objective-C group, maybe you just need to
learn the vocabulary better.


The standard refers to them as ``functions'' or ``function

definition(s)''. So I don't see how you can say they are referred to as ``functions'' by
those who know no better.


I agree with you. I don't think a C programmer will call "subroutines"
everytime when he is talking about a function.

When we are talking about "function pointer", how should we call it ?

I won't call it "subroutines pointer" :-)

--
Jeff

Nov 13 '05 #9

P: n/a
"j" <ja****@bellsouth.net> wrote in
news:ny***************@bignews3.bellsouth.net on Sun 07 Sep 2003
12:31:00a:

"August Derleth" <li*****************@onewest.net> wrote in message
news:Xn**********************************@63.223.5 .101...
C has no methods. C has subroutines, sometimes called functions by
those who know no better. Methods are parts of object-oriented
languages. Maybe you meant to post this in an Objective-C group, maybe
you just need to learn the vocabulary better.


The standard refers to them as ``functions'' or ``function
definition(s)''. So I don't see how you can say they are referred to as
``functions'' by those who know no better.


At base, functions are defined by the lambda calculus. This endows them
with properties C subroutines do not possess, such as a first-class status
in the programming language and the ability to be created anonymously
within another function. They are at a higher level of abstraction than
subroutines are.

The Standard apparently doesn't know better, else it would refer to
subroutines as such, or it would extend C subroutines to become functions.
It has done neither, so it apparently doesn't know any better.

Nov 13 '05 #10

P: n/a
On 6 Sep 2003 12:46:09 -0700, ca******@yahoo.com (2pac) wrote in
comp.lang.c:
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by


You may think that "the information is obviously there in the stack",
and you might even be right on many platforms. But C does not require
a stack and does not even use one the way you think it does even on
all architectures that provide one in hardware.

When your function begins execution on an ARM processor, for example,
the return address is in what is called the "link register", not on
the stack at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #11

P: n/a
On Sun, 07 Sep 2003 00:58:04 GMT, August Derleth
<li*****************@onewest.net> wrote in comp.lang.c:
ca******@yahoo.com (2pac) wrote in
news:d0**************************@posting.google.c om on Sat 06 Sep 2003
01:46:09p:
- I can print
the name of the caller of the method
Thanks for any suggestions


C has no methods. C has subroutines, sometimes called functions by those
who know no better. Methods are parts of object-oriented languages. Maybe
you meant to post this in an Objective-C group, maybe you just need to
learn the vocabulary better.


C has functions, it does not have subroutines.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #12

P: n/a
On Sun, 07 Sep 2003 17:48:39 GMT, August Derleth
<li*****************@onewest.net> wrote in comp.lang.c:
"j" <ja****@bellsouth.net> wrote in
news:ny***************@bignews3.bellsouth.net on Sun 07 Sep 2003
12:31:00a:

"August Derleth" <li*****************@onewest.net> wrote in message
news:Xn**********************************@63.223.5 .101...
C has no methods. C has subroutines, sometimes called functions by
those who know no better. Methods are parts of object-oriented
languages. Maybe you meant to post this in an Objective-C group, maybe
you just need to learn the vocabulary better.

The standard refers to them as ``functions'' or ``function
definition(s)''. So I don't see how you can say they are referred to as
``functions'' by those who know no better.


At base, functions are defined by the lambda calculus. This endows them


Perhaps in your mind. In C, functions are defined by ANSI/ISO/IEC
9899, and before that by K&R.
with properties C subroutines do not possess, such as a first-class status
C functions posses the properties that the C standard defines.
in the programming language and the ability to be created anonymously
within another function. They are at a higher level of abstraction than
subroutines are.
You are free to use whatever terminology you like. Unfortunately that
makes you irrelevant here.
The Standard apparently doesn't know better, else it would refer to
subroutines as such, or it would extend C subroutines to become functions.
It has done neither, so it apparently doesn't know any better.


Apparently you don't know better. A C function is a function because
the C standard defines it as such. Whether or not it meets your idea,
derived from other languages or any other sources, of what a function
should be, is immaterial. Unless a document with such definitions is
referenced as normative in the C standard, it is meaningless in this
context.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #13

P: n/a
On Sun, 07 Sep 2003 17:48:39 GMT
August Derleth <li*****************@onewest.net> wrote:
"j" <ja****@bellsouth.net> wrote in
news:ny***************@bignews3.bellsouth.net on Sun 07 Sep 2003
12:31:00a:
"August Derleth" <li*****************@onewest.net> wrote in message
news:Xn**********************************@63.223.5 .101...
C has no methods. C has subroutines, sometimes called functions by
those who know no better. Methods are parts of object-oriented
languages. Maybe you meant to post this in an Objective-C group,maybe> you just need to learn the vocabulary better.


The standard refers to them as ``functions'' or ``function
definition(s)''. So I don't see how you can say they are referred to
as``functions'' by those who know no better.


At base, functions are defined by the lambda calculus. This endows
them with properties C subroutines do not possess, such as a
first-class status in the programming language and the ability to be
created anonymously within another function. They are at a higher
level of abstraction than subroutines are.


No, functions are defined by the relevant documentation just like any
other term. For instance one of the functions of my mobile phone is
taking voice memos, something which has nothing to do with programming
languages at all. Just one instance of the word function not meaning
what you claim is its only meaning.
The Standard apparently doesn't know better, else it would refer to
subroutines as such, or it would extend C subroutines to become
functions. It has done neither, so it apparently doesn't know any
better.


The standard doesn't know anything, it is a document. However the people
who wrote the standard do know what they are talking about and have
based their definition of a C function on one of the common definitions
(in the programming world) of the word function.
--
Mark Gordon
Nov 13 '05 #14

P: n/a
On Sun, 07 Sep 2003 17:48:39 GMT, in comp.lang.c , August Derleth
<li*****************@onewest.net> wrote:
"j" <ja****@bellsouth.net> wrote in
news:ny***************@bignews3.bellsouth.net on Sun 07 Sep 2003
12:31:00a:
The standard refers to them as ``functions'' or ``function
definition(s)''. So I don't see how you can say they are referred to as
``functions'' by those who know no better.

At base, functions are defined by the lambda calculus.


yeah, whatever. Pop over to comp.programming if you want to debate the
meaning of the word "function".
Here, we use the one implied from the ISO standard. 5.1.2.2.1 and
7.anything provide a pretty simple examples, and if they don't have
"lambda calculus" whatever that is (please don't tell me), then your
definition, which may be correct in some abstract sense, is
irrelevant.
The Standard apparently doesn't know better,


ROFL !

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #15

P: n/a

"August Derleth" <li*****************@onewest.net> wrote in message
news:Xn**********************************@63.223.5 .101...
"j" <ja****@bellsouth.net> wrote in
news:ny***************@bignews3.bellsouth.net on Sun 07 Sep 2003
12:31:00a:

"August Derleth" <li*****************@onewest.net> wrote in message
news:Xn**********************************@63.223.5 .101...
C has no methods. C has subroutines, sometimes called functions by
those who know no better. Methods are parts of object-oriented
languages. Maybe you meant to post this in an Objective-C group, maybe
you just need to learn the vocabulary better.


The standard refers to them as ``functions'' or ``function
definition(s)''. So I don't see how you can say they are referred to as
``functions'' by those who know no better.


At base, functions are defined by the lambda calculus. This endows them
with properties C subroutines do not possess, such as a first-class status
in the programming language and the ability to be created anonymously
within another function. They are at a higher level of abstraction than
subroutines are.

The Standard apparently doesn't know better, else it would refer to
subroutines as such, or it would extend C subroutines to become functions.
It has done neither, so it apparently doesn't know any better.


What ? functions are defined by the lambda calculus ??

We are talking about "C language" here, and it is defined by the ISO
standard, and the standard is always "Correct" and "Authoritative" for the C
language. The meaning of "function" is defined by the standard, and it is
NOT open for debate.

The other meaning of "function" is off-topic here.
--
Jeff
Nov 13 '05 #16

P: n/a
Mark McIntyre <ma**********@spamcop.net> wrote in
news:5q********************************@4ax.com on Sun 07 Sep 2003
02:07:28p:
"lambda calculus" whatever that is (please don't tell me),


I pity any programmer who is innocent of the lambda calculus, and I pity
any person who is innocent of the desire to learn. Both are mentally
crippled.
Nov 13 '05 #17

P: n/a
Jack Klein <ja*******@spamcop.net> wrote in
news:vh********************************@4ax.com on Sun 07 Sep 2003
01:11:58p:
On Sun, 07 Sep 2003 17:48:39 GMT, August Derleth
<li*****************@onewest.net> wrote in comp.lang.c:

At base, functions are defined by the lambda calculus. This endows
them
Perhaps in your mind. In C, functions are defined by ANSI/ISO/IEC
9899, and before that by K&R.


And in the rest of the programming and mathematical world, they are
defined by Alonzo Church. I think that takes precedent over any flavor-of-
the-month standard.
with properties C subroutines do not possess, such as a first-class
status
C functions posses the properties that the C standard defines.


But C functions are not functions. You cannot call bicycles `Bavarian
Copying Machines' because they cannot be considered any subset of the
class of copying machines, proper or improper.
in the programming language and the ability to be created anonymously
within another function. They are at a higher level of abstraction
than subroutines are.
You are free to use whatever terminology you like. Unfortunately that
makes you irrelevant here.


Again, the Humpty Dumpty logic strikes back. Words have meaning beyond
what you say, and they have context beyond what the Standard defines. If
the Standard's definition is at variance with the rest of the world, the
Standard is wrong.

After all, (1==0) must always evaluate to false. Even if a future Standard
allows it to evaluate to true, even if a future Standard requires it
evaluate to true.
The Standard apparently doesn't know better, else it would refer to
subroutines as such, or it would extend C subroutines to become
functions. It has done neither, so it apparently doesn't know any
better.
Apparently you don't know better. A C function is a function because
the C standard defines it as such.


Humpty Dumpty logic, Humpty Dumpty language.
Whether or not it meets your idea,
Not my idea. The mathematical idea, and the idea followed by other
programming languages.
derived from other languages or any other sources, of what a function
should be, is immaterial.
Hardly immaterial. If we cannot agree on proper terminology, we might as
well abandon all pretense of rational communication.

Functions were defined long before C was, and they will survive long after
C is relegated to the dustbin of history.
Unless a document with such definitions is
referenced as normative in the C standard, it is meaningless in this
context.


This context is a subset of a larger mathematical context, one in which
functions have a precise definition the Standard is innocent of.
Therefore, there exists a variance, or a logical inconsistency. In all
cases of such a variance, something is correct and something is incorrect.
As the notion of a Standard upending nearly a century of mathematical
thought is absurd, the Standard must be incorrect.

Nov 13 '05 #18

P: n/a
August Derleth <li*****************@onewest.net> wrote:
[...]
This context is a subset of a larger mathematical context, one in which
functions have a precise definition the Standard is innocent of.
Therefore, there exists a variance, or a logical inconsistency. In all
cases of such a variance, something is correct and something is incorrect.
As the notion of a Standard upending nearly a century of mathematical
thought is absurd, the Standard must be incorrect.


It is quite common for words in English to have more than one meaning,
which must be disambiguated by context. This is particularly the case
in technical fields, and doubly so in computing.

The user manual for my DVD player describes "an auto power off function"
- should I complain that it is using the word in a sloppy manner? The
word function has a different, widely understood meaning in the context
of C, with a history of over 20 years.

Luckily, the human brain has incredible pattern-recognition.. err..
functionality... so determining the intended meaning of a word from
the context is a task we're all well-equipped for.

- Kevin.

Nov 13 '05 #19

P: n/a
August Derleth wrote:

Jack Klein <ja*******@spamcop.net> wrote in
news:vh********************************@4ax.com on Sun 07 Sep 2003
01:11:58p:
On Sun, 07 Sep 2003 17:48:39 GMT, August Derleth
<li*****************@onewest.net> wrote in comp.lang.c:

At base, functions are defined by the lambda calculus. This endows
them


Perhaps in your mind. In C, functions are defined by ANSI/ISO/IEC
9899, and before that by K&R.


And in the rest of the programming and mathematical world, they are
defined by Alonzo Church.
I think that takes precedent over any flavor-of- the-month standard.


Not on the flavor of the month newsgroup.

--
pete
Nov 13 '05 #20

P: n/a
August Derleth <li*****************@onewest.net> writes:
Jack Klein <ja*******@spamcop.net> wrote in
news:vh********************************@4ax.com on Sun 07 Sep 2003
01:11:58p:

[...]
Perhaps in your mind. In C, functions are defined by ANSI/ISO/IEC
9899, and before that by K&R.


And in the rest of the programming and mathematical world, they are
defined by Alonzo Church. I think that takes precedent over any flavor-of-
the-month standard.


According to www.m-w.com, the English word "function" dates back to
1533, some time before Alonzo Church showed up. (I'm not claiming
that www.m-w.com is the ultimate authority, but it's probably close to
correct on this point.)

There are a number of programming languages that use the term
"function" in ways that are inconsistent with the strict mathematical
sense in which Alonzo Church used the term.

Look at it this way. The word "function" is a common English word.
Alonzo Church, quite reasonably, chose to invent a narrow technical
definition for it. The designers of the C programming language,
building on precedents from earlier programming languages, chose to
invent a different narrow technical definition for the same word, also
quite reasonably.

Since this newsgroup discusses the C programming language, I suggest
you learn to cope with this if you want to hang out here.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.