Skip to content

Commit

Permalink
Add Windows x64 stack walking support. Patch by Aaron Ballman!
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140906 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Bigcheese committed Oct 1, 2011
1 parent 52e7dfa commit 0bcd9c7
Showing 1 changed file with 190 additions and 32 deletions.
222 changes: 190 additions & 32 deletions lib/Support/Windows/Signals.inc
Expand Up @@ -23,14 +23,131 @@
#endif
#include <psapi.h>

#ifdef __MINGW32__
#ifdef _MSC_VER
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "dbghelp.lib")
#elif __MINGW32__
#if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
#error "libimagehlp.a & libpsapi.a should be present"
#endif
#else
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "dbghelp.lib")
#endif
// The version of g++ that comes with MinGW does *not* properly understand
// the ll format specifier for printf. However, MinGW passes the format
// specifiers on to the MSVCRT entirely, and the CRT understands the ll
// specifier. So these warnings are spurious in this case. Since we compile
// with -Wall, this will generate these warnings which should be ignored. So
// we will turn off the warnings for this just file. However, MinGW also does
// not support push and pop for diagnostics, so we have to manually turn it
// back on at the end of the file.
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"

// MinGW does not have updated support for the 64-bit versions of the DebugHlp
// APIs. So we will have to load them manually. The structures and method
// signatures were pulled from DbgHelp.h in the Windows Platform SDK, and
// adjusted for brevity.
typedef struct _IMAGEHLP_LINE64 {
DWORD SizeOfStruct;
PVOID Key;
DWORD LineNumber;
PCHAR FileName;
DWORD64 Address;
} IMAGEHLP_LINE64, *PIMAGEHLP_LINE64;

typedef struct _IMAGEHLP_SYMBOL64 {
DWORD SizeOfStruct;
DWORD64 Address;
DWORD Size;
DWORD Flags;
DWORD MaxNameLength;
CHAR Name[1];
} IMAGEHLP_SYMBOL64, *PIMAGEHLP_SYMBOL64;

typedef struct _tagADDRESS64 {
DWORD64 Offset;
WORD Segment;
ADDRESS_MODE Mode;
} ADDRESS64, *LPADDRESS64;

typedef struct _KDHELP64 {
DWORD64 Thread;
DWORD ThCallbackStack;
DWORD ThCallbackBStore;
DWORD NextCallback;
DWORD FramePointer;
DWORD64 KiCallUserMode;
DWORD64 KeUserCallbackDispatcher;
DWORD64 SystemRangeStart;
DWORD64 KiUserExceptionDispatcher;
DWORD64 StackBase;
DWORD64 StackLimit;
DWORD64 Reserved[5];
} KDHELP64, *PKDHELP64;

typedef struct _tagSTACKFRAME64 {
ADDRESS64 AddrPC;
ADDRESS64 AddrReturn;
ADDRESS64 AddrFrame;
ADDRESS64 AddrStack;
ADDRESS64 AddrBStore;
PVOID FuncTableEntry;
DWORD64 Params[4];
BOOL Far;
BOOL Virtual;
DWORD64 Reserved[3];
KDHELP64 KdHelp;
} STACKFRAME64, *LPSTACKFRAME64;

typedef BOOL (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE hProcess,
DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize,
LPDWORD lpNumberOfBytesRead);

typedef PVOID (__stdcall *PFUNCTION_TABLE_ACCESS_ROUTINE64)( HANDLE ahProcess,
DWORD64 AddrBase);

typedef DWORD64 (__stdcall *PGET_MODULE_BASE_ROUTINE64)(HANDLE hProcess,
DWORD64 Address);

typedef DWORD64 (__stdcall *PTRANSLATE_ADDRESS_ROUTINE64)(HANDLE hProcess,
HANDLE hThread, LPADDRESS64 lpaddr);

typedef BOOL (WINAPI *fpStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64,
PVOID, PREAD_PROCESS_MEMORY_ROUTINE64,
PFUNCTION_TABLE_ACCESS_ROUTINE64,
PGET_MODULE_BASE_ROUTINE64,
PTRANSLATE_ADDRESS_ROUTINE64);
static fpStackWalk64 StackWalk64;

typedef DWORD64 (WINAPI *fpSymGetModuleBase64)(HANDLE, DWORD64);
static fpSymGetModuleBase64 SymGetModuleBase64;

typedef BOOL (WINAPI *fpSymGetSymFromAddr64)(HANDLE, DWORD64,
PDWORD64, PIMAGEHLP_SYMBOL64);
static fpSymGetSymFromAddr64 SymGetSymFromAddr64;

typedef BOOL (WINAPI *fpSymGetLineFromAddr64)(HANDLE, DWORD64,
PDWORD, PIMAGEHLP_LINE64);
static fpSymGetLineFromAddr64 SymGetLineFromAddr64;

typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64);
static fpSymFunctionTableAccess64 SymFunctionTableAccess64;

static bool load64BitDebugHelp(void) {
HMODULE hLib = ::LoadLibrary("Dbghelp.dll");
if (hLib) {
StackWalk64 = (fpStackWalk64)
::GetProcAddress(hLib, "StackWalk64");
SymGetModuleBase64 = (fpSymGetModuleBase64)
::GetProcAddress(hLib, "SymGetModuleBase64");
SymGetSymFromAddr64 = (fpSymGetSymFromAddr64)
::GetProcAddress(hLib, "SymGetSymFromAddr64");
SymGetLineFromAddr64 = (fpSymGetLineFromAddr64)
::GetProcAddress(hLib, "SymGetLineFromAddr64");
SymFunctionTableAccess64 = (fpSymFunctionTableAccess64)
::GetProcAddress(hLib, "SymFunctionTableAccess64");
}
return StackWalk64 != NULL;
}
#endif // __MINGW32__

// Forward declare.
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
Expand Down Expand Up @@ -90,6 +207,18 @@ static int CRTReportHook(int ReportType, char *Message, int *Return) {
#endif

static void RegisterHandler() {
#if __MINGW32__
// On MinGW, we need to load up the symbols explicitly, because the
// Win32 framework they include does not have support for the 64-bit
// versions of the APIs we need. If we cannot load up the APIs (which
// would be unexpected as they should exist on every version of Windows
// we support), we will bail out since there would be nothing to report.
if (!load64BitDebugHelp()) {
assert(false && "These APIs should always be available");
return;
}
#endif

if (RegisteredUnhandledExceptionFilter) {
EnterCriticalSection(&CriticalSection);
return;
Expand Down Expand Up @@ -213,20 +342,28 @@ void llvm::sys::RunInterruptHandlers() {
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
Cleanup();

#ifdef _WIN64
// TODO: provide a x64 friendly version of the following
#else

// Initialize the STACKFRAME structure.
STACKFRAME StackFrame;
STACKFRAME64 StackFrame;
memset(&StackFrame, 0, sizeof(StackFrame));

DWORD machineType;
#if defined(_M_X64)
machineType = IMAGE_FILE_MACHINE_AMD64;
StackFrame.AddrPC.Offset = ep->ContextRecord->Rip;
StackFrame.AddrPC.Mode = AddrModeFlat;
StackFrame.AddrStack.Offset = ep->ContextRecord->Rsp;
StackFrame.AddrStack.Mode = AddrModeFlat;
StackFrame.AddrFrame.Offset = ep->ContextRecord->Rbp;
StackFrame.AddrFrame.Mode = AddrModeFlat;
#elif defined(_M_IX86)
machineType = IMAGE_FILE_MACHINE_I386;
StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
StackFrame.AddrPC.Mode = AddrModeFlat;
StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
StackFrame.AddrStack.Mode = AddrModeFlat;
StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
StackFrame.AddrFrame.Mode = AddrModeFlat;
#endif

HANDLE hProcess = GetCurrentProcess();
HANDLE hThread = GetCurrentThread();
Expand All @@ -236,64 +373,76 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
SymInitialize(hProcess, NULL, TRUE);

while (true) {
if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
ep->ContextRecord, NULL, SymFunctionTableAccess,
SymGetModuleBase, NULL)) {
if (!StackWalk64(machineType, hProcess, hThread, &StackFrame,
ep->ContextRecord, NULL, SymFunctionTableAccess64,
SymGetModuleBase64, NULL)) {
break;
}

if (StackFrame.AddrFrame.Offset == 0)
break;

// Print the PC in hexadecimal.
DWORD PC = StackFrame.AddrPC.Offset;
fprintf(stderr, "%08lX", PC);
DWORD64 PC = StackFrame.AddrPC.Offset;
#if defined(_M_X64)
fprintf(stderr, "0x%016llX", PC);
#elif defined(_M_IX86)
fprintf(stderr, "0x%08lX", static_cast<DWORD>(PC));
#endif

// Print the parameters. Assume there are four.
#if defined(_M_X64)
fprintf(stderr, " (0x%016llX 0x%016llX 0x%016llX 0x%016llX)",
StackFrame.Params[0],
StackFrame.Params[1],
StackFrame.Params[2],
StackFrame.Params[3]);
#elif defined(_M_IX86)
fprintf(stderr, " (0x%08lX 0x%08lX 0x%08lX 0x%08lX)",
StackFrame.Params[0],
StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);

static_cast<DWORD>(StackFrame.Params[0]),
static_cast<DWORD>(StackFrame.Params[1]),
static_cast<DWORD>(StackFrame.Params[2]),
static_cast<DWORD>(StackFrame.Params[3]));
#endif
// Verify the PC belongs to a module in this process.
if (!SymGetModuleBase(hProcess, PC)) {
if (!SymGetModuleBase64(hProcess, PC)) {
fputs(" <unknown module>\n", stderr);
continue;
}

// Print the symbol name.
char buffer[512];
IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
IMAGEHLP_SYMBOL64 *symbol = reinterpret_cast<IMAGEHLP_SYMBOL64 *>(buffer);
memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64));
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64);

DWORD dwDisp;
if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
DWORD64 dwDisp;
if (!SymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
fputc('\n', stderr);
continue;
}

buffer[511] = 0;
if (dwDisp > 0)
fprintf(stderr, ", %s()+%04lu bytes(s)", symbol->Name, dwDisp);
fprintf(stderr, ", %s() + 0x%llX bytes(s)", symbol->Name, dwDisp);
else
fprintf(stderr, ", %s", symbol->Name);

// Print the source file and line number information.
IMAGEHLP_LINE line;
IMAGEHLP_LINE64 line;
DWORD dwLineDisp;
memset(&line, 0, sizeof(line));
line.SizeOfStruct = sizeof(line);
if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) {
if (SymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) {
fprintf(stderr, ", %s, line %lu", line.FileName, line.LineNumber);
if (dwDisp > 0)
fprintf(stderr, "+%04lu byte(s)", dwDisp);
if (dwLineDisp > 0)
fprintf(stderr, " + 0x%lX byte(s)", dwLineDisp);
}

fputc('\n', stderr);
}

#endif

if (ExitOnUnhandledExceptions)
_exit(-3);

Expand Down Expand Up @@ -326,3 +475,12 @@ static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
LeaveCriticalSection(&CriticalSection);
return FALSE;
}

#if __MINGW32__
// We turned these warnings off for this file so that MinGW-g++ doesn't
// complain about the ll format specifiers used. Now we are turning the
// warnings back on. If MinGW starts to support diagnostic stacks, we can
// replace this with a pop.
#pragma GCC diagnostic warning "-Wformat"
#pragma GCC diagnostic warning "-Wformat-extra-args"
#endif

0 comments on commit 0bcd9c7

Please sign in to comment.