|function call stack walk/print function names firstname.lastname@example.org (2000-08-27)|
|Re: function call stack walk/print function names email@example.com (Graeme Roy) (2000-09-02)|
|Re: function call stack walk/print function names firstname.lastname@example.org (Martin Waller) (2000-09-08)|
|From:||Graeme Roy <email@example.com>|
|Date:||2 Sep 2000 16:19:37 -0400|
On 27 Aug 2000 firstname.lastname@example.org wrote:
> Inspirated by an article in the MSDN, i got the following idea:
> In case of a crash I walk down the stack with BP/SP from function to
> function. That way I could print out the address of the function. But
> that's not enough - i also want to have the function name. So i got
> the idea of declaring a static char *FUNC_NAME="foobar"; as the FIRST
> local variable in every function i write. When walking down the stack
> i can access this string with BP plus offset.
The Microsoft Win32 API has a set of functions for mapping code
addresses to symbols in the IMAGEHLP library. That way, if you know
the return address for a function, you can map it to the function name
using one of the Sym* functions. This will only work for executables
that have been compiled with debugging information enabled (and also
for exported functions in DLLs) but if you are writing a debugger then
you would normally expect that. That way you won't need to declare
special variables for every function.
Not only does the IMAGEHLP library have support for reading symbols
from PE object files, but it also contains a StackWalk() routine for
traversing a function call stack. Perhaps you might want to use that
instead of simply reading the BP, since some functions may not
preserve the BP if they are optimised.
If you want to see an example of how to use these functions to perform
stack tracing, either look at the examples in MSDN or look at:
and download the latest version, which contains all of the source code
to a freely distributable malloc()-tracing library that I wrote with
full stack tracing support on Windows and UNIX. The stack tracing on
Windows uses the functions from the IMAGEHLP library.
DSP Development Tools Product Line
Analog Devices, Inc.
Return to the
Search the comp.compilers archives again.