Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 API stack walk with MinGW/MSYS?

Tags:

c++

mingw

winapi

i've to develop a stack trace walk to our win32 API plattform support to catch any exception with a behaviour of a JAVA stacktrace. Actually we got a full functional implementation for Linux plattforms.

First i've tried to implement a stackwalk using the win32 CaptureStackBackTrace API mechanism. But this method is not integrated in the actually winbase header of mingw (using MSYS/MinGW 5.1.x) ...

So i decided tried use the dbgheader mechanism follwoing the instructions of this link : http://sites.google.com/site/kenscode/prime-programs/boinc-with-mingw-on-win32

But i fail again and run into linker failures i could not solve. I think MinGW could not resolve the dbgheader library ....

DrMinGW is not an option for me, while it is a JIT Debugger, i've to implement a stack trace mechansim, for any exception occuring on runtime with a customizable log file tracebility like we know from JAVA ...

Has anyone get MSYS/MinGW runable with the win32 API in handshake? I will not change the compiler to the ugly MVC compiler instead of using MSYS/MinGW...

Thanks for any hint. Best regards,

Christian

like image 273
Hellhound Avatar asked Jul 23 '10 13:07

Hellhound


3 Answers

Check Mr. Edd's stack trace library at the following link. It will produce a nice stack frame listing and has specific code to support MinGW.

http://www.mr-edd.co.uk/code/stack_trace

His library uses dbghelp.dll, however, so you may get into some problems trying to compile it. As far as I know, MinGW doesn't include an import library for this DLL (see a old feature request here). I had success, however, creating one import library myself. You can do the same just using a .def file from the Wine project (check the previous link's attached files for one) and running the MingW utility dlltool:

dlltool -k -d dbghelp.def -l dbghelp.a

You can then include the resulting dbghelp.a file in your project. You won't probably like to have dependencies towards dbghelp.dll in your release builds, as the DLL itself is surely not redistributable.

like image 195
José Luis Cebrián Avatar answered Nov 12 '22 17:11

José Luis Cebrián


Here is a method to walk the call stack using the Win32 API which you can call from MinGW.

http://www.codeproject.com/KB/threads/StackWalker.aspx

like image 33
jcoffland Avatar answered Nov 12 '22 17:11

jcoffland


I got stack traces working in MingGW with Edd's dbg library, which is a successor to his minimal stack_trace library:

With msys2, this should get you a stack trace:

$ pacman -S mingw-w64-x86_64-edd-dbg
// main.cpp
#include <dbg/frames.hpp>
#include <dbg/symbols.hpp>
#include <iostream>

int main()
{
  dbg::symdb db;
  dbg::call_stack<64> traceback;
  traceback.collect(0);
  traceback.log(db, std::cout);
  return 0;
}
$ g++ -ggdb main.cpp -ldbg
$ ./a.exe
0x0000000000402ee9: dbg::call_stack<64u>::collect(unsigned int) in C:\msys64\home\phil\stacktrace-example\a.exe
0x00000000004015f2: main in C:\msys64\home\phil\stacktrace-example\a.exe
0x00000000004013f8: __tmainCRTStartup in C:\msys64\home\phil\stacktrace-example\a.exe
0x000000000040151b: mainCRTStartup in C:\msys64\home\phil\stacktrace-example\a.exe
0x00007ffbb0838102: BaseThreadInitThunk in C:\WINDOWS\system32\KERNEL32.DLL
0x00007ffbb27cc5b4: RtlUserThreadStart in C:\WINDOWS\SYSTEM32\ntdll.dll

More about dbg can be found in the Wiki. The code is available here: https://bitbucket.org/edd/dbg/src

like image 1
Philipp Claßen Avatar answered Nov 12 '22 17:11

Philipp Claßen