Subj : Re: call stack To : borland.public.cpp.borlandcpp From : Bob Gonder Date : Tue Dec 21 2004 08:34 am Jeff Kish wrote: >On Mon, 20 Dec 2004 14:00:30 -0800, Bob Gonder > >>Jeff Kish wrote: >> >>>Yes, I am running Borland 5.02 with OWL. >>> >>>Is there some way (easy I hope) to get the call stack if I build with >>>debug, and display it somewhere like a messagebox, or a log file? >> >>I don't see a function to help with that, but it shouldn't be hard. >>To help you see every function, turn on Standard Stack Frame option. >> >>The basic functionality (in 32bit Windows code) would be like this: >>(written off-the-cuff, so may be some tweaks needed) >> First, for reference: How a StackFrame is contructed: >> >>FunctionEntryPoint: >> push ebp >> mov ebp,esp >> sub esp,16 ;16 bytes local variables >> other code >> mov esp,ebp >> pop ebp >> ret >> >>How to walk the stack: >> >> DWORD * pointer; >> DWORD return_address; >> asm mov [pointer],ebp; >>Again: >> asm{ >> mov eax,[pointer] >> mov edx,[eax+4] >> mov [return_address],edx >> mov eax,[eax] >> mov [pointer],eax >> }; >> printf the return address >> if( you_want_more_data ) >> goto Again; >> >> >Thanks. >So Bob, I need to embed a small amount of assembly code into the c++ >code? Does that mean I need the assembler product? I would think 5.02 came with it. But, we could rewrite it closer to C, I think wouldn't need the tasm. typedef struct tsf{ DWORD next; DWORD ret; }STACKFRAME,*LPSTACKFRAME; void walk_the_stack(int depth) { LPSTACKFRAME sf; sf = (LPSTACKFRAME)_EBP; >> asm mov [pointer],ebp; >>Again: while(depth--) { >> asm{ >> mov eax,[pointer] >> mov edx,[eax+4] >> mov [return_address],edx >> mov eax,[eax] >> mov [pointer],eax sf = sf->next; >> }; >> printf the return address printf( "The return address is %p\r\n", sf->ret); /* put the printf here if you don't want to include the call to this function., otherwise put it right after the while().*/ >> if( you_want_more_data ) >> goto Again; }; } .