SymFromAddr返回ERROR_INVALID_ADDRESS标志,如何在mingw中获取堆栈跟踪?

时间:2013-02-03 14:10:30

标签: c++ c debugging winapi backtrace

我尝试做的就是打印一堆调用给定行的方法。我从https://stackoverflow.com/a/5699483/393087回答了代码。稍微重构它以显示问题所在。

#include <windows.h>
#include <iostream>
#include <imagehlp.h>
#include <dbghelp.h>

void printStack( void ) {
  HMODULE dbghelp_lib = LoadLibrary("dbghelp.dll");
  if (NULL == dbghelp_lib) {
    printf("dbghelp.dll failed");
  }
  HANDLE process = GetCurrentProcess();
  if (!SymInitialize( process, NULL, TRUE )) {
    printf("SymInitialize failed: %d\n", GetLastError());
    abort();
  } else SetLastError(0);
  void * stack[100];
  ULONG FramesToSkip = 0;
  ULONG FramesToCapture = 32;
  unsigned short frames = CaptureStackBackTrace( FramesToSkip, FramesToCapture, stack, NULL );
  SYMBOL_INFO * symbol;
  symbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
  symbol->MaxNameLen = 255;
  symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
  for(unsigned int i = 0; i < frames; i++ ) {
    if(!SymFromAddr( process, ( DWORD )( stack[ i ] ), 0, symbol )) {
      printf("SymFromAddr failed: %d\n", GetLastError());
    }
    printf( "%i: %s - 0x%0X\n", frames - i - 1, symbol->Name, symbol->Address );
  }
  free( symbol );
}

void testfunc() {
  printStack();
}

int main() {
  testfunc();
}

它返回:

SymFromAddr failed: 487
3:  - 0x0
SymFromAddr failed: 487
2:  - 0x0
SymFromAddr failed: 487
1:  - 0x0
0: RegisterWaitForInputIdle - 0x7C81702E

它编译和链接没有任何警告。

编译器:http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev7.7z

1 个答案:

答案 0 :(得分:1)

dbghelp只读取PDB文件。因此,您的选择是:

  1. 重新实现dbghelp API以使用DWARF(可能比您想要的更多工作)
  2. 将DWARF符号转换为PDB。
  3. #2发现了cv2pdb的一些谷歌搜索,它似乎面向D编程语言,但看起来很通用,可能是一种更简单的方法。