Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SymFromAddr returns ERROR_INVALID_ADDRESS flag, how to get stack trace in mingw?

All I try to do is to print stack of methods that called a given line. I got code from https://stackoverflow.com/a/5699483/393087 answer. Slightly refactored it to show where lies the problem.

#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();
}

it returns:

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

And it compiles and links without any warning.

compiler: 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

like image 536
rsk82 Avatar asked Nov 12 '22 12:11

rsk82


1 Answers

dbghelp only reads PDB files. Therefore, your options are:

  1. Reimplement dbghelp APIs to use DWARF (probably more work than you want)
  2. Convert your DWARF symbols to PDB.

A bit of googling for #2 found cv2pdb, which appears to be oriented towards the D programming language, but it seems generic enough that it might be a simpler approach.

like image 83
Eric Brown Avatar answered Nov 15 '22 06:11

Eric Brown