打印Backtrace / Stacktrace

时间:2018-01-14 14:36:11

标签: c++ mingw mingw32 mingw-w64

我已尝试过很多方法在MinGW 64中打印stacktrace / backtrace - 但似乎无论如何都不可能。

看起来像backtrace库,它的必需包含/编译器标志在MinGW中不可用 - 包含execinfo.h状态错误,因此是编译器标志-rdynamic。

请让我知道一个干净的方法来生成一个函数的回溯堆栈 - 如果没有外部依赖,那会更好但是如果需要我们可以使用,因为没有这样的其他机制

---------------------------------修订------------- --------------------------

根据建议我尝试了下面的程序

#include <stdio.h>

int main( void )
{
     unsigned int   i;
     void         * stack[ 100 ];
     unsigned short frames;
     SYMBOL_INFO  * symbol;
     HANDLE         process;

     process = GetCurrentProcess();

     SymInitialize( process, NULL, TRUE );

     frames               = CaptureStackBackTrace( 0, 100, stack, NULL );
     symbol               = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
     symbol->MaxNameLen   = 255;
     symbol->SizeOfStruct = sizeof( SYMBOL_INFO );

     for( i = 0; i < frames; i++ )
     {
         SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );

         printf( "%i: %s - 0x%0X\n", frames - i - 1, symbol->Name, symbol->Address );
     }

     free( symbol );
}

我收到以下编译错误 - 我知道我遗漏了一些标题,但我不确定哪一个:

$ /c/tools/mingw64/bin/c++ test27.cpp
test27.cpp: In function 'int main()':
test27.cpp:8:6: error: 'SYMBOL_INFO' was not declared in this scope
      SYMBOL_INFO  * symbol;
      ^
test27.cpp:8:21: error: 'symbol' was not declared in this scope
      SYMBOL_INFO  * symbol;
                     ^
test27.cpp:9:6: error: 'HANDLE' was not declared in this scope
      HANDLE         process;
      ^
test27.cpp:11:6: error: 'process' was not declared in this scope
      process = GetCurrentProcess();
      ^
test27.cpp:11:34: error: 'GetCurrentProcess' was not declared in this scope
      process = GetCurrentProcess();
                                  ^
test27.cpp:13:36: error: 'TRUE' was not declared in this scope
      SymInitialize( process, NULL, TRUE );
                                    ^
test27.cpp:13:41: error: 'SymInitialize' was not declared in this scope
      SymInitialize( process, NULL, TRUE );
                                         ^
test27.cpp:15:72: error: 'CaptureStackBackTrace' was not declared in this scope
      frames               = CaptureStackBackTrace( 0, 100, stack, NULL );
                                                                        ^
test27.cpp:16:45: error: expected primary-expression before ')' token
      symbol               = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
                                             ^
test27.cpp:22:34: error: 'DWORD64' was not declared in this scope
          SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
                                  ^
test27.cpp:22:69: error: 'SymFromAddr' was not declared in this scope
          SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
                                                                     ^
test27.cpp:27:19: error: 'free' was not declared in this scope
      free( symbol );
                   ^

0 个答案:

没有答案