473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
20 3366
"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
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
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
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
>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
August Derleth wrote:
C has subroutines, sometimes called functions by those
who know no better.


LOL!

--
pete
Nov 13 '05 #7
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

"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
"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
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
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
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
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
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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: AMD | last post by:
Hi, I need to write a Win32 DLL and I would like to use Python instead of VB, C++ or Delphi. Is this possible? Thank you, Andre M. Descombes
8
by: John Smith | last post by:
Hi, I'm writing a library in C++ which is supposed to be used by people using C. One function I have must return a string to users which is arbitrary length. The user must be able to use this...
10
by: Santi | last post by:
Hi, Is there anyway to find if a dll or exe has been compiled from C++ or Delphi or whatever language?
9
by: jaden10001 | last post by:
I have read that the function.caller method is now depricated. I see alot of browsers still support it but Opera 7 does not. Is there a way to get the name of the caller of a function in Opera 7?
6
by: Terentius Neo | last post by:
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a select statement? I mean something like this: Select c.number, call procedure( c.number ) as list from table c With best...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance, it is GREATLY appreciated. I'm wondering if there is a way to duplicate a function in regedit of find and find next for keywords programmatically,...
5
by: pamelafluente | last post by:
Hi guys, How do I get the full name of the current (overload) function or sub (whatever it be) ? Sub SomeFunction() Dim FullNameOfThisFunction = ??? msgbox(FullNameOfThisFunction )
9
by: kj7ny | last post by:
Is there a way that I can programmatically find the name of a method I have created from within that method? I would like to be able to log a message from within that method (def) and I would like...
5
by: Simon | last post by:
I heard that we could do that by using AJAX. Could anybody share how to do it? Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...

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.