RE: Call stack
"Jon Watte" <[email protected]> Wed, 9 Feb 2005 09:24:25 -0800
| Newsgroups | gmane.games.devel.windows |
|---|---|
| Message-ID | <[email protected]> |
In debug mode, you can walk the EBP frames pretty easily. Use
some inline assembly or setjmp() to read the value of the register
and then treat it like a pointer-to-void*, and you're good to go.
In release mode, it might still be possible to do, assuming you
don't turn on the "omit frame pointer" optimization. Sadly, that
optimization gives you another whole register to play with, so it's
usually something you'll want for your shipping builds. At that
point, I've had reasonable success with scanning the stack for
addresses that point right after a CALL or JMP REG instruction
(JMP REG is used for DLL/vectored calls).
In debug mode, it looks like this:
#include <stdio.h>
__declspec( naked ) char ** get_ebp() {
__asm {
mov eax, ebp
ret
}
}
// the stack root needs to be thread-local for multi-threaded
// applications
char ** root = 0;
void init_callstack( void * ptr ) {
root = (char **)ptr;
}
void callstack( char const * name ) {
char ** ptr = get_ebp();
while( ptr < root ) {
printf( "%s: frame: %08x caller: %08x\n", name, *ptr, ptr[1] );
ptr = *(char ***)ptr;
}
printf( "\n" );
}
void func3() {
callstack( "func3" );
}
void func2() {
callstack( "func2" );
func3();
}
void func1() {
func3();
func2();
}
int main( int argc, char * argv[] ) {
int temp;
init_callstack( &temp );
func1();
return 0;
}
C:\temp>stack.exe
func3: frame: 0012ff6c caller: 0040106d
func3: frame: 0012ff74 caller: 00401091
func3: frame: 0012ff80 caller: 004010ad
func2: frame: 0012ff6c caller: 0040107f
func2: frame: 0012ff74 caller: 00401096
func2: frame: 0012ff80 caller: 004010ad
func3: frame: 0012ff64 caller: 0040106d
func3: frame: 0012ff6c caller: 00401087
func3: frame: 0012ff74 caller: 00401096
func3: frame: 0012ff80 caller: 004010ad
Turning the addresses to actual function named needs the use of the
DbgHelp library, which can open and use pdb files for you.
Cheers,
/ h+
-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of
Diogo de Andrade
Sent: Wednesday, February 09, 2005 5:43 AM
To: [email protected]
Subject: [GD-Windows] Call stack
Hey all!
This is a long shot, but is there any way to output the call stack in
MSVC 6.0?
More precisely, I'm having some problems with the recording/playback in
my engine, and I tracked it to the random number generated (when on
playback, it is being called more often than on recording, which is a
strange behavior). Anyway, I'm logging the output of the random number
generator (this is how I found out the asynch), but it would be really
helpful if I could output to the log file the call stack just to see
exactly WHO is calling the random number generator out of turn...
Is there any "simple" way of doing this (just for debug purposes)?
Thanks in advance (and sorry if this is OT, but I'm not sure)
Diogo de Andrade
Creative and Technical Director
Spellcaster Studios
[email protected]
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555