mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-25 16:29:20 +00:00
Add Windows x64 stack walking support. Patch by Aaron Ballman!
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140906 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
52e7dfadc6
commit
0bcd9c70b1
@ -23,14 +23,131 @@
|
|||||||
#endif
|
#endif
|
||||||
#include <psapi.h>
|
#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))
|
#if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
|
||||||
#error "libimagehlp.a & libpsapi.a should be present"
|
#error "libimagehlp.a & libpsapi.a should be present"
|
||||||
#endif
|
#endif
|
||||||
#else
|
// The version of g++ that comes with MinGW does *not* properly understand
|
||||||
#pragma comment(lib, "psapi.lib")
|
// the ll format specifier for printf. However, MinGW passes the format
|
||||||
#pragma comment(lib, "dbghelp.lib")
|
// specifiers on to the MSVCRT entirely, and the CRT understands the ll
|
||||||
#endif
|
// 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.
|
// Forward declare.
|
||||||
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
|
||||||
@ -90,6 +207,18 @@ static int CRTReportHook(int ReportType, char *Message, int *Return) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void RegisterHandler() {
|
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) {
|
if (RegisteredUnhandledExceptionFilter) {
|
||||||
EnterCriticalSection(&CriticalSection);
|
EnterCriticalSection(&CriticalSection);
|
||||||
return;
|
return;
|
||||||
@ -213,20 +342,28 @@ void llvm::sys::RunInterruptHandlers() {
|
|||||||
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
|
||||||
Cleanup();
|
Cleanup();
|
||||||
|
|
||||||
#ifdef _WIN64
|
|
||||||
// TODO: provide a x64 friendly version of the following
|
|
||||||
#else
|
|
||||||
|
|
||||||
// Initialize the STACKFRAME structure.
|
// Initialize the STACKFRAME structure.
|
||||||
STACKFRAME StackFrame;
|
STACKFRAME64 StackFrame;
|
||||||
memset(&StackFrame, 0, sizeof(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.Offset = ep->ContextRecord->Eip;
|
||||||
StackFrame.AddrPC.Mode = AddrModeFlat;
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
||||||
StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
|
StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
|
||||||
StackFrame.AddrStack.Mode = AddrModeFlat;
|
StackFrame.AddrStack.Mode = AddrModeFlat;
|
||||||
StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
|
StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
|
||||||
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
||||||
|
#endif
|
||||||
|
|
||||||
HANDLE hProcess = GetCurrentProcess();
|
HANDLE hProcess = GetCurrentProcess();
|
||||||
HANDLE hThread = GetCurrentThread();
|
HANDLE hThread = GetCurrentThread();
|
||||||
@ -236,9 +373,9 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
|
|||||||
SymInitialize(hProcess, NULL, TRUE);
|
SymInitialize(hProcess, NULL, TRUE);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
|
if (!StackWalk64(machineType, hProcess, hThread, &StackFrame,
|
||||||
ep->ContextRecord, NULL, SymFunctionTableAccess,
|
ep->ContextRecord, NULL, SymFunctionTableAccess64,
|
||||||
SymGetModuleBase, NULL)) {
|
SymGetModuleBase64, NULL)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,54 +383,66 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
// Print the PC in hexadecimal.
|
// Print the PC in hexadecimal.
|
||||||
DWORD PC = StackFrame.AddrPC.Offset;
|
DWORD64 PC = StackFrame.AddrPC.Offset;
|
||||||
fprintf(stderr, "%08lX", PC);
|
#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.
|
// Print the parameters. Assume there are four.
|
||||||
fprintf(stderr, " (0x%08lX 0x%08lX 0x%08lX 0x%08lX)",
|
#if defined(_M_X64)
|
||||||
|
fprintf(stderr, " (0x%016llX 0x%016llX 0x%016llX 0x%016llX)",
|
||||||
StackFrame.Params[0],
|
StackFrame.Params[0],
|
||||||
StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);
|
StackFrame.Params[1],
|
||||||
|
StackFrame.Params[2],
|
||||||
|
StackFrame.Params[3]);
|
||||||
|
#elif defined(_M_IX86)
|
||||||
|
fprintf(stderr, " (0x%08lX 0x%08lX 0x%08lX 0x%08lX)",
|
||||||
|
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.
|
// Verify the PC belongs to a module in this process.
|
||||||
if (!SymGetModuleBase(hProcess, PC)) {
|
if (!SymGetModuleBase64(hProcess, PC)) {
|
||||||
fputs(" <unknown module>\n", stderr);
|
fputs(" <unknown module>\n", stderr);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the symbol name.
|
// Print the symbol name.
|
||||||
char buffer[512];
|
char buffer[512];
|
||||||
IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
|
IMAGEHLP_SYMBOL64 *symbol = reinterpret_cast<IMAGEHLP_SYMBOL64 *>(buffer);
|
||||||
memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
|
memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64));
|
||||||
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
|
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
|
||||||
symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
|
symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64);
|
||||||
|
|
||||||
DWORD dwDisp;
|
DWORD64 dwDisp;
|
||||||
if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
|
if (!SymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
|
||||||
fputc('\n', stderr);
|
fputc('\n', stderr);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer[511] = 0;
|
buffer[511] = 0;
|
||||||
if (dwDisp > 0)
|
if (dwDisp > 0)
|
||||||
fprintf(stderr, ", %s()+%04lu bytes(s)", symbol->Name, dwDisp);
|
fprintf(stderr, ", %s() + 0x%llX bytes(s)", symbol->Name, dwDisp);
|
||||||
else
|
else
|
||||||
fprintf(stderr, ", %s", symbol->Name);
|
fprintf(stderr, ", %s", symbol->Name);
|
||||||
|
|
||||||
// Print the source file and line number information.
|
// Print the source file and line number information.
|
||||||
IMAGEHLP_LINE line;
|
IMAGEHLP_LINE64 line;
|
||||||
|
DWORD dwLineDisp;
|
||||||
memset(&line, 0, sizeof(line));
|
memset(&line, 0, sizeof(line));
|
||||||
line.SizeOfStruct = 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);
|
fprintf(stderr, ", %s, line %lu", line.FileName, line.LineNumber);
|
||||||
if (dwDisp > 0)
|
if (dwLineDisp > 0)
|
||||||
fprintf(stderr, "+%04lu byte(s)", dwDisp);
|
fprintf(stderr, " + 0x%lX byte(s)", dwLineDisp);
|
||||||
}
|
}
|
||||||
|
|
||||||
fputc('\n', stderr);
|
fputc('\n', stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (ExitOnUnhandledExceptions)
|
if (ExitOnUnhandledExceptions)
|
||||||
_exit(-3);
|
_exit(-3);
|
||||||
|
|
||||||
@ -326,3 +475,12 @@ static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
|
|||||||
LeaveCriticalSection(&CriticalSection);
|
LeaveCriticalSection(&CriticalSection);
|
||||||
return FALSE;
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user