Win32 API stack walk with MinGW/MSYS?

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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

回答1:

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.



回答2:

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



回答3:

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  #include  #include   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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!