views:

44

answers:

1

I want to implement an analog of backtrace utility under windows in order to add this information to exception for example.

I need to capture return addresses and then translate it into symbols names.

I'm aware of StackWalk64 and of StackWalker project but unfortunately it has several important drawbacks:

  • It is known to be very slow (the StackWalk64) and I don't want to waste much time for collecting the trace the basically can be done as fast as walking on linked list.
  • The function StackWalk64 is known to be not thread safe.

I want to support only x86 and possible x86_64 architectures

Basic idea I have is following:

  1. Run on stack using esp/ebp registers similarly to what GCC's __builtin_return_address(x)/__builtin_frame_address(x) doe till I reach the bottom of the stack (this is what glibc does).
  2. Translate addresses to symbols
  3. Demangle them.

Problems/Questions:

  1. How do I know that I reach the to of the stack? For example glibc implementation has __libc_stack_end so it is easy to find where to stop. Is there any analog of such thing under Windows? How can I get stack bottom address?
  2. What are the analogs of dladdr functionality. Now I know that unlike ELF platform that keeps most of symbol names, PE format does not. So it should read somehow the debug information. Any ideas?
+1  A: 

You use StackWalk but resolve symbols later.

Will
I've mentioned my problems with StackWalk function
Artyom
and I've reiterated its the right way to go
Will
@Will, in such case I just would not implement, it as overhead of StackWalk64 is too high, in comparison for example with walking on ebp frame pointers.
Artyom
It is my experience that stackwalk is not so slow; its even used in stack snapshot sample profilers. What you have to do however is avoid symbol lookups when collecting data; its the symbol lookups that are very expensive; so cache them, and compute them lazily, and so on.
Will
Even if performance is not an issue, there are other problems with StackWalk - thread safety, total lack of any usable documentation (that you can actually read and not refer to 1001 3rd part samples) etc. If I have the pointer to bottom of the stack, I can do it easily on my own. So I really want to avoid this function.
Artyom