Discussion:
Call stack
(too old to reply)
Lothar Frings
2016-12-14 17:09:33 UTC
Permalink
Dear people,
is there a way of determininig programmatically
which function called the current function?

If I put a variable at the very beginning of the
current function its address will be the address
of the function it is in, right? But how can I
read the call stack? The return address must be somewhere.
Geoff
2016-12-14 19:56:30 UTC
Permalink
On Wed, 14 Dec 2016 09:09:33 -0800 (PST), Lothar Frings
Post by Lothar Frings
Dear people,
is there a way of determininig programmatically
which function called the current function?
If I put a variable at the very beginning of the
current function its address will be the address
of the function it is in, right? But how can I
read the call stack? The return address must be somewhere.
I'm not an expert on assembly language programming so I don't know how
they work the magic of stack back-tracing to the function names in the
debuggers but the return address is on the stack.

For Win32 (x86 ABI) the return address is at EBP+4.
In x64 it's at RBP+8.

Note however, that this is not the address of the function that called
yours but the place where control will resume when your function
returns. For a callback function this return address may not be the
function that actually passed control to yours.
Lothar Frings
2016-12-14 20:13:45 UTC
Permalink
Post by Geoff
On Wed, 14 Dec 2016 09:09:33 -0800 (PST), Lothar Frings
Post by Lothar Frings
Dear people,
is there a way of determininig programmatically
which function called the current function?
If I put a variable at the very beginning of the
current function its address will be the address
of the function it is in, right? But how can I
read the call stack? The return address must be somewhere.
I'm not an expert on assembly language programming so I don't know how
they work the magic of stack back-tracing to the function names in the
debuggers but the return address is on the stack.
For Win32 (x86 ABI) the return address is at EBP+4.
In x64 it's at RBP+8.
Note however, that this is not the address of the function that called
yours but the place where control will resume when your function
returns. For a callback function this return address may not be the
function that actually passed control to yours.
Of course - I plan to evaluate the .map file
to find the name of the calling function.
Geoff
2016-12-14 21:54:57 UTC
Permalink
On Wed, 14 Dec 2016 12:13:45 -0800 (PST), Lothar Frings
Post by Lothar Frings
Post by Geoff
On Wed, 14 Dec 2016 09:09:33 -0800 (PST), Lothar Frings
Post by Lothar Frings
Dear people,
is there a way of determininig programmatically
which function called the current function?
If I put a variable at the very beginning of the
current function its address will be the address
of the function it is in, right? But how can I
read the call stack? The return address must be somewhere.
I'm not an expert on assembly language programming so I don't know how
they work the magic of stack back-tracing to the function names in the
debuggers but the return address is on the stack.
For Win32 (x86 ABI) the return address is at EBP+4.
In x64 it's at RBP+8.
Note however, that this is not the address of the function that called
yours but the place where control will resume when your function
returns. For a callback function this return address may not be the
function that actually passed control to yours.
Of course - I plan to evaluate the .map file
to find the name of the calling function.
I see. I was taking in the more general case where the caller might be
out of your hands.

In the case where you control the source of caller and callee you
might want use the __FUNCTION__ macro to store the function name of
the caller and/or pass it to the callee for debugging purposes. Might
be far easier than peeking at map files.
Cholo Lennon
2016-12-21 16:49:39 UTC
Permalink
Post by Lothar Frings
Dear people,
is there a way of determininig programmatically
which function called the current function?
If I put a variable at the very beginning of the
current function its address will be the address
of the function it is in, right? But how can I
read the call stack? The return address must be somewhere.
Check StackWalk64 API.

Examples at CodeProject:

https://www.codeproject.com/search.aspx?q=StackWalk64&x=0&y=0&sbo=kw

Regards
--
Cholo Lennon
Bs.As.
ARG
Continue reading on narkive:
Loading...