Re: function call stack walk/print function names

Graeme Roy <graeme@epc.co.uk>
2 Sep 2000 16:19:37 -0400

          From comp.compilers

Related articles
function call stack walk/print function names chr1701@my-deja.com (2000-08-27)
Re: function call stack walk/print function names graeme@epc.co.uk (Graeme Roy) (2000-09-02)
Re: function call stack walk/print function names martinwaller@beeb.net (Martin Waller) (2000-09-08)
| List of all articles for this month |

From: Graeme Roy <graeme@epc.co.uk>
Newsgroups: comp.compilers
Date: 2 Sep 2000 16:19:37 -0400
Organization: analog.com
References: 00-08-134
Keywords: debug

On 27 Aug 2000 chr1701@my-deja.com 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:


        http://www.cbmamiga.demon.co.uk/mpatrol/


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.


Graeme Roy
DSP Development Tools Product Line
Analog Devices, Inc.
E-Mail: graeme@epc.co.uk
http://www.epc.co.uk


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.