2004-08-29 21:22:48 +02:00
|
|
|
//===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
|
2010-10-21 22:40:39 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-10-21 22:40:39 +02:00
|
|
|
//
|
2004-08-29 21:22:48 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Win32 specific implementation of the Signals class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-02 00:23:46 +02:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
2013-06-14 15:59:21 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-05-04 18:56:51 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/WindowsError.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include <algorithm>
|
2016-05-04 18:56:51 +02:00
|
|
|
#include <io.h>
|
2015-01-29 18:20:29 +01:00
|
|
|
#include <signal.h>
|
2004-09-29 01:58:03 +02:00
|
|
|
#include <stdio.h>
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2015-03-05 20:10:52 +01:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2014-01-07 13:37:13 +01:00
|
|
|
// The Windows.h header must be after LLVM and standard headers.
|
2020-02-28 09:59:24 +01:00
|
|
|
#include "llvm/Support/Windows/WindowsSupport.h"
|
2014-01-07 13:37:13 +01:00
|
|
|
|
2005-02-19 04:01:13 +01:00
|
|
|
#ifdef __MINGW32__
|
2006-06-01 21:03:21 +02:00
|
|
|
#include <imagehlp.h>
|
2004-09-23 16:47:10 +02:00
|
|
|
#else
|
2016-12-21 19:50:52 +01:00
|
|
|
#include <crtdbg.h>
|
2006-06-01 21:03:21 +02:00
|
|
|
#include <dbghelp.h>
|
2004-09-23 16:47:10 +02:00
|
|
|
#endif
|
|
|
|
#include <psapi.h>
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2011-10-01 02:05:20 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma comment(lib, "psapi.lib")
|
|
|
|
#elif __MINGW32__
|
|
|
|
// 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"
|
|
|
|
|
2011-10-21 11:38:50 +02:00
|
|
|
#if !defined(__MINGW64_VERSION_MAJOR)
|
|
|
|
// MinGW.org 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.
|
2011-10-01 02:05:20 +02:00
|
|
|
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;
|
2015-07-02 16:34:57 +02:00
|
|
|
#endif // !defined(__MINGW64_VERSION_MAJOR)
|
|
|
|
#endif // __MINGW32__
|
2011-10-01 02:05:20 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-05-04 18:56:51 +02:00
|
|
|
typedef BOOL(WINAPI *fpMiniDumpWriteDump)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE,
|
|
|
|
PMINIDUMP_EXCEPTION_INFORMATION,
|
|
|
|
PMINIDUMP_USER_STREAM_INFORMATION,
|
|
|
|
PMINIDUMP_CALLBACK_INFORMATION);
|
|
|
|
static fpMiniDumpWriteDump fMiniDumpWriteDump;
|
|
|
|
|
2011-10-01 02:05:20 +02:00
|
|
|
typedef BOOL (WINAPI *fpStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64,
|
|
|
|
PVOID, PREAD_PROCESS_MEMORY_ROUTINE64,
|
|
|
|
PFUNCTION_TABLE_ACCESS_ROUTINE64,
|
|
|
|
PGET_MODULE_BASE_ROUTINE64,
|
|
|
|
PTRANSLATE_ADDRESS_ROUTINE64);
|
2015-07-02 16:34:57 +02:00
|
|
|
static fpStackWalk64 fStackWalk64;
|
2011-10-01 02:05:20 +02:00
|
|
|
|
|
|
|
typedef DWORD64 (WINAPI *fpSymGetModuleBase64)(HANDLE, DWORD64);
|
2015-07-02 16:34:57 +02:00
|
|
|
static fpSymGetModuleBase64 fSymGetModuleBase64;
|
2011-10-01 02:05:20 +02:00
|
|
|
|
|
|
|
typedef BOOL (WINAPI *fpSymGetSymFromAddr64)(HANDLE, DWORD64,
|
|
|
|
PDWORD64, PIMAGEHLP_SYMBOL64);
|
2015-07-02 16:34:57 +02:00
|
|
|
static fpSymGetSymFromAddr64 fSymGetSymFromAddr64;
|
2011-10-01 02:05:20 +02:00
|
|
|
|
|
|
|
typedef BOOL (WINAPI *fpSymGetLineFromAddr64)(HANDLE, DWORD64,
|
|
|
|
PDWORD, PIMAGEHLP_LINE64);
|
2015-07-02 16:34:57 +02:00
|
|
|
static fpSymGetLineFromAddr64 fSymGetLineFromAddr64;
|
2011-10-01 02:05:20 +02:00
|
|
|
|
2015-11-05 02:07:54 +01:00
|
|
|
typedef BOOL(WINAPI *fpSymGetModuleInfo64)(HANDLE hProcess, DWORD64 dwAddr,
|
|
|
|
PIMAGEHLP_MODULE64 ModuleInfo);
|
|
|
|
static fpSymGetModuleInfo64 fSymGetModuleInfo64;
|
|
|
|
|
2011-10-01 02:05:20 +02:00
|
|
|
typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64);
|
2015-07-02 16:34:57 +02:00
|
|
|
static fpSymFunctionTableAccess64 fSymFunctionTableAccess64;
|
|
|
|
|
|
|
|
typedef DWORD (WINAPI *fpSymSetOptions)(DWORD);
|
|
|
|
static fpSymSetOptions fSymSetOptions;
|
|
|
|
|
|
|
|
typedef BOOL (WINAPI *fpSymInitialize)(HANDLE, PCSTR, BOOL);
|
|
|
|
static fpSymInitialize fSymInitialize;
|
2011-10-01 02:05:20 +02:00
|
|
|
|
2015-11-05 02:07:54 +01:00
|
|
|
typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
|
|
|
|
static fpEnumerateLoadedModules fEnumerateLoadedModules;
|
|
|
|
|
2011-10-01 02:05:20 +02:00
|
|
|
static bool load64BitDebugHelp(void) {
|
2013-10-07 11:52:36 +02:00
|
|
|
HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
|
2011-10-01 02:05:20 +02:00
|
|
|
if (hLib) {
|
2016-05-04 18:56:51 +02:00
|
|
|
fMiniDumpWriteDump = (fpMiniDumpWriteDump)
|
|
|
|
::GetProcAddress(hLib, "MiniDumpWriteDump");
|
2015-07-02 16:34:57 +02:00
|
|
|
fStackWalk64 = (fpStackWalk64)
|
2011-10-01 02:05:20 +02:00
|
|
|
::GetProcAddress(hLib, "StackWalk64");
|
2015-07-02 16:34:57 +02:00
|
|
|
fSymGetModuleBase64 = (fpSymGetModuleBase64)
|
2011-10-01 02:05:20 +02:00
|
|
|
::GetProcAddress(hLib, "SymGetModuleBase64");
|
2015-07-02 16:34:57 +02:00
|
|
|
fSymGetSymFromAddr64 = (fpSymGetSymFromAddr64)
|
2011-10-01 02:05:20 +02:00
|
|
|
::GetProcAddress(hLib, "SymGetSymFromAddr64");
|
2015-07-02 16:34:57 +02:00
|
|
|
fSymGetLineFromAddr64 = (fpSymGetLineFromAddr64)
|
2011-10-01 02:05:20 +02:00
|
|
|
::GetProcAddress(hLib, "SymGetLineFromAddr64");
|
2015-11-05 02:07:54 +01:00
|
|
|
fSymGetModuleInfo64 = (fpSymGetModuleInfo64)
|
|
|
|
::GetProcAddress(hLib, "SymGetModuleInfo64");
|
2015-07-02 16:34:57 +02:00
|
|
|
fSymFunctionTableAccess64 = (fpSymFunctionTableAccess64)
|
2011-10-01 02:05:20 +02:00
|
|
|
::GetProcAddress(hLib, "SymFunctionTableAccess64");
|
2015-07-02 16:34:57 +02:00
|
|
|
fSymSetOptions = (fpSymSetOptions)::GetProcAddress(hLib, "SymSetOptions");
|
|
|
|
fSymInitialize = (fpSymInitialize)::GetProcAddress(hLib, "SymInitialize");
|
2015-11-05 02:07:54 +01:00
|
|
|
fEnumerateLoadedModules = (fpEnumerateLoadedModules)
|
|
|
|
::GetProcAddress(hLib, "EnumerateLoadedModules64");
|
2011-10-01 02:05:20 +02:00
|
|
|
}
|
2016-05-04 18:56:51 +02:00
|
|
|
return fStackWalk64 && fSymInitialize && fSymSetOptions && fMiniDumpWriteDump;
|
2011-10-01 02:05:20 +02:00
|
|
|
}
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2015-07-22 21:01:14 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-09-16 17:53:16 +02:00
|
|
|
// Forward declare.
|
|
|
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
|
|
|
|
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
|
|
|
|
|
2018-05-15 06:06:28 +02:00
|
|
|
// The function to call if ctrl-c is pressed.
|
2005-08-02 05:04:47 +02:00
|
|
|
static void (*InterruptFunction)() = 0;
|
|
|
|
|
2013-06-13 23:16:58 +02:00
|
|
|
static std::vector<std::string> *FilesToRemove = NULL;
|
2004-09-16 17:53:16 +02:00
|
|
|
static bool RegisteredUnhandledExceptionFilter = false;
|
2004-09-19 07:37:39 +02:00
|
|
|
static bool CleanupExecuted = false;
|
|
|
|
static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
|
2004-09-17 05:02:27 +02:00
|
|
|
|
|
|
|
// Windows creates a new thread to execute the console handler when an event
|
|
|
|
// (such as CTRL/C) occurs. This causes concurrency issues with the above
|
|
|
|
// globals which this critical section addresses.
|
2004-09-16 17:53:16 +02:00
|
|
|
static CRITICAL_SECTION CriticalSection;
|
2015-03-26 17:24:38 +01:00
|
|
|
static bool CriticalSectionInitialized = false;
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2016-06-09 02:53:21 +02:00
|
|
|
static StringRef Argv0;
|
|
|
|
|
2015-11-05 02:07:54 +01:00
|
|
|
enum {
|
2015-03-05 18:47:52 +01:00
|
|
|
#if defined(_M_X64)
|
2015-11-05 02:07:54 +01:00
|
|
|
NativeMachineType = IMAGE_FILE_MACHINE_AMD64
|
2017-08-04 01:12:33 +02:00
|
|
|
#elif defined(_M_ARM64)
|
|
|
|
NativeMachineType = IMAGE_FILE_MACHINE_ARM64
|
|
|
|
#elif defined(_M_IX86)
|
2015-11-05 02:07:54 +01:00
|
|
|
NativeMachineType = IMAGE_FILE_MACHINE_I386
|
2017-08-04 01:12:33 +02:00
|
|
|
#elif defined(_M_ARM)
|
|
|
|
NativeMachineType = IMAGE_FILE_MACHINE_ARMNT
|
|
|
|
#else
|
|
|
|
NativeMachineType = IMAGE_FILE_MACHINE_UNKNOWN
|
2015-03-05 18:47:52 +01:00
|
|
|
#endif
|
2015-11-05 02:07:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool printStackTraceWithLLVMSymbolizer(llvm::raw_ostream &OS,
|
|
|
|
HANDLE hProcess, HANDLE hThread,
|
|
|
|
STACKFRAME64 &StackFrameOrig,
|
|
|
|
CONTEXT *ContextOrig) {
|
|
|
|
// StackWalk64 modifies the incoming stack frame and context, so copy them.
|
|
|
|
STACKFRAME64 StackFrame = StackFrameOrig;
|
|
|
|
|
|
|
|
// Copy the register context so that we don't modify it while we unwind. We
|
|
|
|
// could use InitializeContext + CopyContext, but that's only required to get
|
|
|
|
// at AVX registers, which typically aren't needed by StackWalk64. Reduce the
|
|
|
|
// flag set to indicate that there's less data.
|
|
|
|
CONTEXT Context = *ContextOrig;
|
|
|
|
Context.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
|
|
|
|
|
|
|
|
static void *StackTrace[256];
|
2015-11-05 15:22:56 +01:00
|
|
|
size_t Depth = 0;
|
2015-11-05 02:07:54 +01:00
|
|
|
while (fStackWalk64(NativeMachineType, hProcess, hThread, &StackFrame,
|
|
|
|
&Context, 0, fSymFunctionTableAccess64,
|
|
|
|
fSymGetModuleBase64, 0)) {
|
|
|
|
if (StackFrame.AddrFrame.Offset == 0)
|
|
|
|
break;
|
|
|
|
StackTrace[Depth++] = (void *)(uintptr_t)StackFrame.AddrPC.Offset;
|
|
|
|
if (Depth >= array_lengthof(StackTrace))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-09 02:53:21 +02:00
|
|
|
return printSymbolizedStackTrace(Argv0, &StackTrace[0], Depth, OS);
|
2015-11-05 02:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct FindModuleData {
|
|
|
|
void **StackTrace;
|
|
|
|
int Depth;
|
|
|
|
const char **Modules;
|
|
|
|
intptr_t *Offsets;
|
|
|
|
StringSaver *StrPool;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-07 01:13:09 +01:00
|
|
|
static BOOL CALLBACK findModuleCallback(PCSTR ModuleName,
|
2015-11-05 02:07:54 +01:00
|
|
|
DWORD64 ModuleBase, ULONG ModuleSize,
|
|
|
|
void *VoidData) {
|
|
|
|
FindModuleData *Data = (FindModuleData*)VoidData;
|
|
|
|
intptr_t Beg = ModuleBase;
|
|
|
|
intptr_t End = Beg + ModuleSize;
|
|
|
|
for (int I = 0; I < Data->Depth; I++) {
|
|
|
|
if (Data->Modules[I])
|
|
|
|
continue;
|
|
|
|
intptr_t Addr = (intptr_t)Data->StackTrace[I];
|
|
|
|
if (Beg <= Addr && Addr < End) {
|
2016-10-05 03:41:11 +02:00
|
|
|
Data->Modules[I] = Data->StrPool->save(ModuleName).data();
|
2015-11-05 02:07:54 +01:00
|
|
|
Data->Offsets[I] = Addr - Beg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool findModulesAndOffsets(void **StackTrace, int Depth,
|
|
|
|
const char **Modules, intptr_t *Offsets,
|
|
|
|
const char *MainExecutableName,
|
|
|
|
StringSaver &StrPool) {
|
|
|
|
if (!fEnumerateLoadedModules)
|
|
|
|
return false;
|
|
|
|
FindModuleData Data;
|
|
|
|
Data.StackTrace = StackTrace;
|
|
|
|
Data.Depth = Depth;
|
|
|
|
Data.Modules = Modules;
|
|
|
|
Data.Offsets = Offsets;
|
|
|
|
Data.StrPool = &StrPool;
|
|
|
|
fEnumerateLoadedModules(GetCurrentProcess(), findModuleCallback, &Data);
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-05 18:47:52 +01:00
|
|
|
|
2015-11-05 02:07:54 +01:00
|
|
|
static void PrintStackTraceForThread(llvm::raw_ostream &OS, HANDLE hProcess,
|
|
|
|
HANDLE hThread, STACKFRAME64 &StackFrame,
|
|
|
|
CONTEXT *Context) {
|
2015-03-05 18:47:52 +01:00
|
|
|
// Initialize the symbol handler.
|
2015-07-02 16:34:57 +02:00
|
|
|
fSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES);
|
|
|
|
fSymInitialize(hProcess, NULL, TRUE);
|
2015-03-05 18:47:52 +01:00
|
|
|
|
2015-11-05 02:07:54 +01:00
|
|
|
// Try llvm-symbolizer first. llvm-symbolizer knows how to deal with both PDBs
|
|
|
|
// and DWARF, so it should do a good job regardless of what debug info or
|
|
|
|
// linker is in use.
|
|
|
|
if (printStackTraceWithLLVMSymbolizer(OS, hProcess, hThread, StackFrame,
|
|
|
|
Context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-05 18:47:52 +01:00
|
|
|
while (true) {
|
2015-11-05 02:07:54 +01:00
|
|
|
if (!fStackWalk64(NativeMachineType, hProcess, hThread, &StackFrame,
|
|
|
|
Context, 0, fSymFunctionTableAccess64,
|
|
|
|
fSymGetModuleBase64, 0)) {
|
2015-03-05 18:47:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StackFrame.AddrFrame.Offset == 0)
|
|
|
|
break;
|
|
|
|
|
2015-03-05 20:10:52 +01:00
|
|
|
using namespace llvm;
|
2015-03-05 18:47:52 +01:00
|
|
|
// Print the PC in hexadecimal.
|
|
|
|
DWORD64 PC = StackFrame.AddrPC.Offset;
|
2017-08-04 01:12:33 +02:00
|
|
|
#if defined(_M_X64) || defined(_M_ARM64)
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format("0x%016llX", PC);
|
2017-08-04 01:12:33 +02:00
|
|
|
#elif defined(_M_IX86) || defined(_M_ARM)
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format("0x%08lX", static_cast<DWORD>(PC));
|
2015-03-05 18:47:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Print the parameters. Assume there are four.
|
2017-08-04 01:12:33 +02:00
|
|
|
#if defined(_M_X64) || defined(_M_ARM64)
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format(" (0x%016llX 0x%016llX 0x%016llX 0x%016llX)",
|
2015-03-05 18:47:52 +01:00
|
|
|
StackFrame.Params[0], StackFrame.Params[1], StackFrame.Params[2],
|
|
|
|
StackFrame.Params[3]);
|
2017-08-04 01:12:33 +02:00
|
|
|
#elif defined(_M_IX86) || defined(_M_ARM)
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format(" (0x%08lX 0x%08lX 0x%08lX 0x%08lX)",
|
2015-03-05 18:47:52 +01:00
|
|
|
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.
|
2015-07-02 16:34:57 +02:00
|
|
|
if (!fSymGetModuleBase64(hProcess, PC)) {
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << " <unknown module>\n";
|
2015-03-05 18:47:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the symbol name.
|
|
|
|
char buffer[512];
|
|
|
|
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);
|
|
|
|
|
|
|
|
DWORD64 dwDisp;
|
2015-07-02 16:34:57 +02:00
|
|
|
if (!fSymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << '\n';
|
2015-03-05 18:47:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[511] = 0;
|
|
|
|
if (dwDisp > 0)
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format(", %s() + 0x%llX bytes(s)", (const char*)symbol->Name,
|
|
|
|
dwDisp);
|
2015-03-05 18:47:52 +01:00
|
|
|
else
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format(", %s", (const char*)symbol->Name);
|
2015-03-05 18:47:52 +01:00
|
|
|
|
|
|
|
// Print the source file and line number information.
|
2015-04-24 17:39:47 +02:00
|
|
|
IMAGEHLP_LINE64 line = {};
|
2015-03-05 18:47:52 +01:00
|
|
|
DWORD dwLineDisp;
|
|
|
|
line.SizeOfStruct = sizeof(line);
|
2015-07-02 16:34:57 +02:00
|
|
|
if (fSymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) {
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format(", %s, line %lu", line.FileName, line.LineNumber);
|
2015-03-05 18:47:52 +01:00
|
|
|
if (dwLineDisp > 0)
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << format(" + 0x%lX byte(s)", dwLineDisp);
|
2015-03-05 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
2015-03-05 20:10:52 +01:00
|
|
|
OS << '\n';
|
2015-03-05 18:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-29 21:22:48 +02:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-10-21 22:40:39 +02:00
|
|
|
//=== WARNING: Implementation here must contain only Win32 specific code
|
2004-09-16 17:53:16 +02:00
|
|
|
//=== and must not be UNIX code
|
2004-08-29 21:22:48 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-22 17:58:35 +02:00
|
|
|
#ifdef _MSC_VER
|
2018-05-15 06:06:28 +02:00
|
|
|
/// Emulates hitting "retry" from an "abort, retry, ignore" CRT debug report
|
|
|
|
/// dialog. "retry" raises an exception which ultimately triggers our stack
|
|
|
|
/// dumper.
|
2015-02-26 22:08:21 +01:00
|
|
|
static LLVM_ATTRIBUTE_UNUSED int
|
|
|
|
AvoidMessageBoxHook(int ReportType, char *Message, int *Return) {
|
2013-04-05 18:18:03 +02:00
|
|
|
// Set *Return to the retry code for the return value of _CrtDbgReport:
|
|
|
|
// http://msdn.microsoft.com/en-us/library/8hyw4sy7(v=vs.71).aspx
|
|
|
|
// This may also trigger just-in-time debugging via DebugBreak().
|
|
|
|
if (Return)
|
|
|
|
*Return = 1;
|
|
|
|
// Don't call _CrtDbgReport.
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-09-22 17:58:35 +02:00
|
|
|
#endif
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2015-01-29 21:48:34 +01:00
|
|
|
extern "C" void HandleAbort(int Sig) {
|
2015-01-29 18:20:29 +01:00
|
|
|
if (Sig == SIGABRT) {
|
|
|
|
LLVM_BUILTIN_TRAP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 17:24:38 +01:00
|
|
|
static void InitializeThreading() {
|
|
|
|
if (CriticalSectionInitialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Now's the time to create the critical section. This is the first time
|
|
|
|
// through here, and there's only one thread.
|
|
|
|
InitializeCriticalSection(&CriticalSection);
|
|
|
|
CriticalSectionInitialized = true;
|
|
|
|
}
|
|
|
|
|
2009-09-22 11:50:28 +02:00
|
|
|
static void RegisterHandler() {
|
2015-07-02 16:34:57 +02:00
|
|
|
// 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.
|
2016-01-11 22:07:48 +01:00
|
|
|
if (!load64BitDebugHelp()) {
|
|
|
|
assert(false && "These APIs should always be available");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-01 02:05:20 +02:00
|
|
|
|
2004-09-19 07:37:39 +02:00
|
|
|
if (RegisteredUnhandledExceptionFilter) {
|
2004-09-17 05:02:27 +02:00
|
|
|
EnterCriticalSection(&CriticalSection);
|
2004-09-16 17:53:16 +02:00
|
|
|
return;
|
2004-09-17 05:02:27 +02:00
|
|
|
}
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2015-03-26 17:24:38 +01:00
|
|
|
InitializeThreading();
|
2004-09-16 17:53:16 +02:00
|
|
|
|
|
|
|
// Enter it immediately. Now if someone hits CTRL/C, the console handler
|
|
|
|
// can't proceed until the globals are updated.
|
|
|
|
EnterCriticalSection(&CriticalSection);
|
|
|
|
|
|
|
|
RegisteredUnhandledExceptionFilter = true;
|
2004-09-19 07:37:39 +02:00
|
|
|
OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
|
2004-09-16 17:53:16 +02:00
|
|
|
SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
|
|
|
|
|
|
|
|
// IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
|
|
|
|
// else multi-threading problems will ensue.
|
|
|
|
}
|
|
|
|
|
2018-05-15 06:06:28 +02:00
|
|
|
// The public API
|
2013-06-13 23:16:58 +02:00
|
|
|
bool sys::RemoveFileOnSignal(StringRef Filename, std::string* ErrMsg) {
|
2004-09-16 17:53:16 +02:00
|
|
|
RegisterHandler();
|
|
|
|
|
2006-08-25 23:37:17 +02:00
|
|
|
if (CleanupExecuted) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Process terminating -- cannot register for removal";
|
|
|
|
return true;
|
|
|
|
}
|
2004-09-19 07:37:39 +02:00
|
|
|
|
2004-09-16 17:53:16 +02:00
|
|
|
if (FilesToRemove == NULL)
|
2013-06-13 23:16:58 +02:00
|
|
|
FilesToRemove = new std::vector<std::string>;
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2020-01-29 00:02:26 +01:00
|
|
|
FilesToRemove->push_back(std::string(Filename));
|
2004-09-16 17:53:16 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2006-08-25 23:37:17 +02:00
|
|
|
return false;
|
2004-08-29 21:22:48 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 06:06:28 +02:00
|
|
|
// The public API
|
2013-06-13 23:16:58 +02:00
|
|
|
void sys::DontRemoveFileOnSignal(StringRef Filename) {
|
2010-09-01 16:17:34 +02:00
|
|
|
if (FilesToRemove == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-10-22 03:23:50 +02:00
|
|
|
RegisterHandler();
|
|
|
|
|
2013-06-13 23:16:58 +02:00
|
|
|
std::vector<std::string>::reverse_iterator I =
|
2016-08-12 05:55:06 +02:00
|
|
|
find(reverse(*FilesToRemove), Filename);
|
2010-09-01 16:17:34 +02:00
|
|
|
if (I != FilesToRemove->rend())
|
|
|
|
FilesToRemove->erase(I.base()-1);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
|
|
|
|
2015-01-29 18:20:29 +01:00
|
|
|
void sys::DisableSystemDialogsOnCrash() {
|
|
|
|
// Crash to stack trace handler on abort.
|
|
|
|
signal(SIGABRT, HandleAbort);
|
|
|
|
|
|
|
|
// The following functions are not reliably accessible on MinGW.
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// We're already handling writing a "something went wrong" message.
|
|
|
|
_set_abort_behavior(0, _WRITE_ABORT_MSG);
|
|
|
|
// Disable Dr. Watson.
|
|
|
|
_set_abort_behavior(0, _CALL_REPORTFAULT);
|
|
|
|
_CrtSetReportHook(AvoidMessageBoxHook);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Disable standard error dialog box.
|
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
|
|
|
|
SEM_NOOPENFILEERRORBOX);
|
|
|
|
_set_error_mode(_OUT_TO_STDERR);
|
|
|
|
}
|
|
|
|
|
2018-05-15 06:06:28 +02:00
|
|
|
/// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
|
|
|
|
/// process, print a stack trace and then exit.
|
2016-06-09 02:53:21 +02:00
|
|
|
void sys::PrintStackTraceOnErrorSignal(StringRef Argv0,
|
|
|
|
bool DisableCrashReporting) {
|
|
|
|
::Argv0 = Argv0;
|
|
|
|
|
2016-05-04 18:56:51 +02:00
|
|
|
if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT"))
|
|
|
|
Process::PreventCoreFiles();
|
|
|
|
|
2015-01-29 18:20:29 +01:00
|
|
|
DisableSystemDialogsOnCrash();
|
2004-09-16 17:53:16 +02:00
|
|
|
RegisterHandler();
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
2015-03-11 16:41:15 +01:00
|
|
|
}
|
|
|
|
|
2015-03-14 20:20:56 +01:00
|
|
|
#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
|
|
|
|
// Provide a prototype for RtlCaptureContext, mingw32 from mingw.org is
|
|
|
|
// missing it but mingw-w64 has it.
|
2015-03-11 16:41:15 +01:00
|
|
|
extern "C" VOID WINAPI RtlCaptureContext(PCONTEXT ContextRecord);
|
2015-03-11 17:09:02 +01:00
|
|
|
#endif
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2020-01-11 21:27:07 +01:00
|
|
|
static void LocalPrintStackTrace(raw_ostream &OS, PCONTEXT C) {
|
|
|
|
STACKFRAME64 StackFrame{};
|
|
|
|
CONTEXT Context{};
|
|
|
|
if (!C) {
|
|
|
|
::RtlCaptureContext(&Context);
|
|
|
|
C = &Context;
|
|
|
|
}
|
2015-03-05 18:47:52 +01:00
|
|
|
#if defined(_M_X64)
|
|
|
|
StackFrame.AddrPC.Offset = Context.Rip;
|
|
|
|
StackFrame.AddrStack.Offset = Context.Rsp;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.Rbp;
|
2017-08-04 01:12:33 +02:00
|
|
|
#elif defined(_M_IX86)
|
2015-03-05 18:47:52 +01:00
|
|
|
StackFrame.AddrPC.Offset = Context.Eip;
|
|
|
|
StackFrame.AddrStack.Offset = Context.Esp;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.Ebp;
|
2018-04-13 08:38:02 +02:00
|
|
|
#elif defined(_M_ARM64)
|
2017-08-04 01:12:33 +02:00
|
|
|
StackFrame.AddrPC.Offset = Context.Pc;
|
|
|
|
StackFrame.AddrStack.Offset = Context.Sp;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.Fp;
|
2018-04-13 08:38:02 +02:00
|
|
|
#elif defined(_M_ARM)
|
|
|
|
StackFrame.AddrPC.Offset = Context.Pc;
|
|
|
|
StackFrame.AddrStack.Offset = Context.Sp;
|
|
|
|
StackFrame.AddrFrame.Offset = Context.R11;
|
2015-03-05 18:47:52 +01:00
|
|
|
#endif
|
|
|
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrStack.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
2015-03-05 20:10:52 +01:00
|
|
|
PrintStackTraceForThread(OS, GetCurrentProcess(), GetCurrentThread(),
|
2020-01-11 21:27:07 +01:00
|
|
|
StackFrame, C);
|
2013-01-09 20:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-08-26 15:21:34 +02:00
|
|
|
void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
|
|
|
|
// FIXME: Handle "Depth" parameter to print stack trace upto specified Depth
|
2020-01-11 21:27:07 +01:00
|
|
|
LocalPrintStackTrace(OS, nullptr);
|
|
|
|
}
|
2005-08-02 04:14:22 +02:00
|
|
|
|
2015-03-11 16:53:24 +01:00
|
|
|
void llvm::sys::SetInterruptFunction(void (*IF)()) {
|
2005-08-02 05:04:47 +02:00
|
|
|
RegisterHandler();
|
2005-08-02 05:26:32 +02:00
|
|
|
InterruptFunction = IF;
|
2005-08-02 05:04:47 +02:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2005-08-02 04:14:22 +02:00
|
|
|
}
|
2009-03-20 00:26:52 +01:00
|
|
|
|
2019-07-12 18:05:09 +02:00
|
|
|
void llvm::sys::SetInfoSignalFunction(void (*Handler)()) {
|
|
|
|
// Unimplemented.
|
|
|
|
}
|
|
|
|
|
2019-11-14 23:30:56 +01:00
|
|
|
void llvm::sys::SetOneShotPipeSignalFunction(void (*Handler)()) {
|
|
|
|
// Unimplemented.
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::sys::DefaultOneShotPipeSignalHandler() {
|
|
|
|
// Unimplemented.
|
|
|
|
}
|
2009-03-20 00:26:52 +01:00
|
|
|
|
2018-05-15 06:06:28 +02:00
|
|
|
/// Add a function to be called when a signal is delivered to the process. The
|
|
|
|
/// handler can have a cookie passed to it to identify what instance of the
|
|
|
|
/// handler it is.
|
Signal handling should be signal-safe
Summary:
Before this patch, signal handling wasn't signal safe. This leads to real-world
crashes. It used ManagedStatic inside of signals, this can allocate and can lead
to unexpected state when a signal occurs during llvm_shutdown (because
llvm_shutdown destroys the ManagedStatic). It also used cl::opt without custom
backing storage. Some de-allocation was performed as well. Acquiring a lock in a
signal handler is also a great way to deadlock.
We can't just disable signals on llvm_shutdown because the signals might do
useful work during that shutdown. We also can't just disable llvm_shutdown for
programs (instead of library uses of clang) because we'd have to then mark the
pointers as not leaked and make sure all the ManagedStatic uses are OK to leak
and remain so.
Move all of the code to lock-free datastructures instead, and avoid having any
of them in an inconsistent state. I'm not trying to be fancy, I'm not using any
explicit memory order because this code isn't hot. The only purpose of the
atomics is to guarantee that a signal firing on the same or a different thread
doesn't see an inconsistent state and crash. In some cases we might miss some
state (for example, we might fail to delete a temporary file), but that's fine.
Note that I haven't touched any of the backtrace support despite it not
technically being totally signal-safe. When that code is called we know
something bad is up and we don't expect to continue execution, so calling
something that e.g. sets errno is the least of our problems.
A similar patch should be applied to lib/Support/Windows/Signals.inc, but that
can be done separately.
Fix r332428 which I reverted in r332429. I originally used double-wide CAS
because I was lazy, but some platforms use a runtime function for that which
thankfully failed to link (it would have been bad for signal handlers
otherwise). I use a separate flag to guard the data instead.
<rdar://problem/28010281>
Reviewers: dexonsmith
Subscribers: steven_wu, llvm-commits
llvm-svn: 332496
2018-05-16 19:25:35 +02:00
|
|
|
void llvm::sys::AddSignalHandler(sys::SignalHandlerCallback FnPtr,
|
|
|
|
void *Cookie) {
|
|
|
|
insertSignalHandler(FnPtr, Cookie);
|
2009-03-20 00:26:52 +01:00
|
|
|
RegisterHandler();
|
2010-03-31 14:07:16 +02:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2009-03-20 00:26:52 +01:00
|
|
|
}
|
2004-08-29 21:22:48 +02:00
|
|
|
|
2020-05-13 11:29:02 +02:00
|
|
|
static void Cleanup(bool ExecuteSignalHandlers) {
|
2015-05-19 15:31:25 +02:00
|
|
|
if (CleanupExecuted)
|
|
|
|
return;
|
|
|
|
|
2004-09-16 17:53:16 +02:00
|
|
|
EnterCriticalSection(&CriticalSection);
|
|
|
|
|
2004-09-19 07:37:39 +02:00
|
|
|
// Prevent other thread from registering new files and directories for
|
|
|
|
// removal, should we be executing because of the console handler callback.
|
|
|
|
CleanupExecuted = true;
|
|
|
|
|
|
|
|
// FIXME: open files cannot be deleted.
|
2004-09-16 17:53:16 +02:00
|
|
|
if (FilesToRemove != NULL)
|
|
|
|
while (!FilesToRemove->empty()) {
|
2014-02-23 14:37:37 +01:00
|
|
|
llvm::sys::fs::remove(FilesToRemove->back());
|
2004-09-16 17:53:16 +02:00
|
|
|
FilesToRemove->pop_back();
|
|
|
|
}
|
2020-05-13 11:29:02 +02:00
|
|
|
|
|
|
|
if (ExecuteSignalHandlers)
|
|
|
|
llvm::sys::RunSignalHandlers();
|
|
|
|
|
2004-09-16 17:53:16 +02:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
|
|
|
|
2010-05-08 04:10:34 +02:00
|
|
|
void llvm::sys::RunInterruptHandlers() {
|
2015-03-26 17:24:38 +01:00
|
|
|
// The interrupt handler may be called from an interrupt, but it may also be
|
|
|
|
// called manually (such as the case of report_fatal_error with no registered
|
|
|
|
// error handler). We must ensure that the critical section is properly
|
|
|
|
// initialized.
|
|
|
|
InitializeThreading();
|
2020-05-13 11:29:02 +02:00
|
|
|
Cleanup(true);
|
2010-05-08 04:10:34 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Find the Windows Registry Key for a given location.
|
2016-05-04 18:56:51 +02:00
|
|
|
///
|
|
|
|
/// \returns a valid HKEY if the location exists, else NULL.
|
|
|
|
static HKEY FindWERKey(const llvm::Twine &RegistryLocation) {
|
|
|
|
HKEY Key;
|
2016-06-23 16:45:54 +02:00
|
|
|
if (ERROR_SUCCESS != ::RegOpenKeyExA(HKEY_LOCAL_MACHINE,
|
|
|
|
RegistryLocation.str().c_str(), 0,
|
|
|
|
KEY_QUERY_VALUE | KEY_READ, &Key))
|
2016-05-04 18:56:51 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return Key;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Populate ResultDirectory with the value for "DumpFolder" for a given
|
2016-05-04 18:56:51 +02:00
|
|
|
/// Windows Registry key.
|
|
|
|
///
|
|
|
|
/// \returns true if a valid value for DumpFolder exists, false otherwise.
|
|
|
|
static bool GetDumpFolder(HKEY Key,
|
|
|
|
llvm::SmallVectorImpl<char> &ResultDirectory) {
|
|
|
|
using llvm::sys::windows::UTF16ToUTF8;
|
|
|
|
|
|
|
|
if (!Key)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DWORD BufferLengthBytes = 0;
|
|
|
|
|
|
|
|
if (ERROR_SUCCESS != ::RegGetValueW(Key, 0, L"DumpFolder", REG_EXPAND_SZ,
|
|
|
|
NULL, NULL, &BufferLengthBytes))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<wchar_t, MAX_PATH> Buffer(BufferLengthBytes);
|
|
|
|
|
|
|
|
if (ERROR_SUCCESS != ::RegGetValueW(Key, 0, L"DumpFolder", REG_EXPAND_SZ,
|
|
|
|
NULL, Buffer.data(), &BufferLengthBytes))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DWORD ExpandBufferSize = ::ExpandEnvironmentStringsW(Buffer.data(), NULL, 0);
|
|
|
|
|
|
|
|
if (!ExpandBufferSize)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<wchar_t, MAX_PATH> ExpandBuffer(ExpandBufferSize);
|
|
|
|
|
|
|
|
if (ExpandBufferSize != ::ExpandEnvironmentStringsW(Buffer.data(),
|
|
|
|
ExpandBuffer.data(),
|
|
|
|
ExpandBufferSize))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (UTF16ToUTF8(ExpandBuffer.data(), ExpandBufferSize - 1, ResultDirectory))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Populate ResultType with a valid MINIDUMP_TYPE based on the value of
|
2016-05-04 18:56:51 +02:00
|
|
|
/// "DumpType" for a given Windows Registry key.
|
|
|
|
///
|
|
|
|
/// According to
|
|
|
|
/// https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181(v=vs.85).aspx
|
|
|
|
/// valid values for DumpType are:
|
|
|
|
/// * 0: Custom dump
|
|
|
|
/// * 1: Mini dump
|
|
|
|
/// * 2: Full dump
|
|
|
|
/// If "Custom dump" is specified then the "CustomDumpFlags" field is read
|
|
|
|
/// containing a bitwise combination of MINIDUMP_TYPE values.
|
|
|
|
///
|
|
|
|
/// \returns true if a valid value for ResultType can be set, false otherwise.
|
|
|
|
static bool GetDumpType(HKEY Key, MINIDUMP_TYPE &ResultType) {
|
|
|
|
if (!Key)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DWORD DumpType;
|
|
|
|
DWORD TypeSize = sizeof(DumpType);
|
|
|
|
if (ERROR_SUCCESS != ::RegGetValueW(Key, NULL, L"DumpType", RRF_RT_REG_DWORD,
|
|
|
|
NULL, &DumpType,
|
|
|
|
&TypeSize))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (DumpType) {
|
|
|
|
case 0: {
|
|
|
|
DWORD Flags = 0;
|
|
|
|
if (ERROR_SUCCESS != ::RegGetValueW(Key, NULL, L"CustomDumpFlags",
|
|
|
|
RRF_RT_REG_DWORD, NULL, &Flags,
|
|
|
|
&TypeSize))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ResultType = static_cast<MINIDUMP_TYPE>(Flags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
ResultType = MiniDumpNormal;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ResultType = MiniDumpWithFullMemory;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Write a Windows dump file containing process information that can be
|
2016-05-04 18:56:51 +02:00
|
|
|
/// used for post-mortem debugging.
|
|
|
|
///
|
|
|
|
/// \returns zero error code if a mini dump created, actual error code
|
|
|
|
/// otherwise.
|
|
|
|
static std::error_code WINAPI
|
|
|
|
WriteWindowsDumpFile(PMINIDUMP_EXCEPTION_INFORMATION ExceptionInfo) {
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
std::string MainExecutableName = fs::getMainExecutable(nullptr, nullptr);
|
|
|
|
StringRef ProgramName;
|
|
|
|
|
|
|
|
if (MainExecutableName.empty()) {
|
|
|
|
// If we can't get the executable filename,
|
|
|
|
// things are in worse shape than we realize
|
|
|
|
// and we should just bail out.
|
|
|
|
return mapWindowsError(::GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramName = path::filename(MainExecutableName.c_str());
|
|
|
|
|
|
|
|
// The Windows Registry location as specified at
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181%28v=vs.85%29.aspx
|
|
|
|
// "Collecting User-Mode Dumps" that may optionally be set to collect crash
|
|
|
|
// dumps in a specified location.
|
|
|
|
StringRef LocalDumpsRegistryLocation =
|
|
|
|
"SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps";
|
|
|
|
|
|
|
|
// The key pointing to the Registry location that may contain global crash
|
|
|
|
// dump settings. This will be NULL if the location can not be found.
|
|
|
|
ScopedRegHandle DefaultLocalDumpsKey(FindWERKey(LocalDumpsRegistryLocation));
|
|
|
|
|
|
|
|
// The key pointing to the Registry location that may contain
|
|
|
|
// application-specific crash dump settings. This will be NULL if the
|
|
|
|
// location can not be found.
|
|
|
|
ScopedRegHandle AppSpecificKey(
|
|
|
|
FindWERKey(Twine(LocalDumpsRegistryLocation) + "\\" + ProgramName));
|
|
|
|
|
|
|
|
// Look to see if a dump type is specified in the registry; first with the
|
|
|
|
// app-specific key and failing that with the global key. If none are found
|
|
|
|
// default to a normal dump (GetDumpType will return false either if the key
|
|
|
|
// is NULL or if there is no valid DumpType value at its location).
|
|
|
|
MINIDUMP_TYPE DumpType;
|
|
|
|
if (!GetDumpType(AppSpecificKey, DumpType))
|
|
|
|
if (!GetDumpType(DefaultLocalDumpsKey, DumpType))
|
|
|
|
DumpType = MiniDumpNormal;
|
|
|
|
|
2021-03-23 18:45:37 +01:00
|
|
|
// Look to see if a dump location is specified on the command line. If not,
|
|
|
|
// look to see if a dump location is specified in the registry; first with the
|
2016-05-04 18:56:51 +02:00
|
|
|
// app-specific key and failing that with the global key. If none are found
|
|
|
|
// we'll just create the dump file in the default temporary file location
|
|
|
|
// (GetDumpFolder will return false either if the key is NULL or if there is
|
|
|
|
// no valid DumpFolder value at its location).
|
|
|
|
bool ExplicitDumpDirectorySet = true;
|
2021-07-16 01:52:44 +02:00
|
|
|
SmallString<MAX_PATH> DumpDirectory(*CrashDiagnosticsDirectory);
|
2021-03-23 18:45:37 +01:00
|
|
|
if (DumpDirectory.empty())
|
|
|
|
if (!GetDumpFolder(AppSpecificKey, DumpDirectory))
|
|
|
|
if (!GetDumpFolder(DefaultLocalDumpsKey, DumpDirectory))
|
|
|
|
ExplicitDumpDirectorySet = false;
|
2016-05-04 18:56:51 +02:00
|
|
|
|
|
|
|
int FD;
|
|
|
|
SmallString<MAX_PATH> DumpPath;
|
|
|
|
|
|
|
|
if (ExplicitDumpDirectorySet) {
|
|
|
|
if (std::error_code EC = fs::create_directories(DumpDirectory))
|
|
|
|
return EC;
|
|
|
|
if (std::error_code EC = fs::createUniqueFile(
|
|
|
|
Twine(DumpDirectory) + "\\" + ProgramName + ".%%%%%%.dmp", FD,
|
|
|
|
DumpPath))
|
|
|
|
return EC;
|
|
|
|
} else if (std::error_code EC =
|
|
|
|
fs::createTemporaryFile(ProgramName, "dmp", FD, DumpPath))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
// Our support functions return a file descriptor but Windows wants a handle.
|
|
|
|
ScopedCommonHandle FileHandle(reinterpret_cast<HANDLE>(_get_osfhandle(FD)));
|
|
|
|
|
|
|
|
if (!fMiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(),
|
|
|
|
FileHandle, DumpType, ExceptionInfo, NULL, NULL))
|
|
|
|
return mapWindowsError(::GetLastError());
|
|
|
|
|
|
|
|
llvm::errs() << "Wrote crash dump file \"" << DumpPath << "\"\n";
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:27:07 +01:00
|
|
|
void sys::CleanupOnSignal(uintptr_t Context) {
|
|
|
|
LLVMUnhandledExceptionFilter((LPEXCEPTION_POINTERS)Context);
|
|
|
|
}
|
|
|
|
|
2004-09-16 17:53:16 +02:00
|
|
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
|
2020-05-13 11:29:02 +02:00
|
|
|
Cleanup(true);
|
2010-10-21 22:40:39 +02:00
|
|
|
|
2016-05-04 18:56:51 +02:00
|
|
|
// We'll automatically write a Minidump file here to help diagnose
|
|
|
|
// the nasty sorts of crashes that aren't 100% reproducible from a set of
|
|
|
|
// inputs (or in the event that the user is unable or unwilling to provide a
|
|
|
|
// reproducible case).
|
2017-03-31 16:58:52 +02:00
|
|
|
if (!llvm::sys::Process::AreCoreFilesPrevented()) {
|
2016-05-04 18:56:51 +02:00
|
|
|
MINIDUMP_EXCEPTION_INFORMATION ExceptionInfo;
|
|
|
|
ExceptionInfo.ThreadId = ::GetCurrentThreadId();
|
|
|
|
ExceptionInfo.ExceptionPointers = ep;
|
|
|
|
ExceptionInfo.ClientPointers = FALSE;
|
|
|
|
|
|
|
|
if (std::error_code EC = WriteWindowsDumpFile(&ExceptionInfo))
|
|
|
|
llvm::errs() << "Could not write crash dump file: " << EC.message()
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:39:14 +01:00
|
|
|
// Stack unwinding appears to modify the context. Copy it to preserve the
|
|
|
|
// caller's context.
|
|
|
|
CONTEXT ContextCopy;
|
|
|
|
if (ep)
|
|
|
|
memcpy(&ContextCopy, ep->ContextRecord, sizeof(ContextCopy));
|
|
|
|
|
|
|
|
LocalPrintStackTrace(llvm::errs(), ep ? &ContextCopy : nullptr);
|
2010-10-28 10:25:44 +02:00
|
|
|
|
2020-01-11 21:27:07 +01:00
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
2004-09-16 17:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
|
2005-08-02 05:26:32 +02:00
|
|
|
// We are running in our very own thread, courtesy of Windows.
|
2005-08-02 05:04:47 +02:00
|
|
|
EnterCriticalSection(&CriticalSection);
|
2020-05-13 11:29:02 +02:00
|
|
|
// This function is only ever called when a CTRL-C or similar control signal
|
|
|
|
// is fired. Killing a process in this way is normal, so don't trigger the
|
|
|
|
// signal handlers.
|
|
|
|
Cleanup(false);
|
2004-09-16 17:53:16 +02:00
|
|
|
|
2005-08-02 05:04:47 +02:00
|
|
|
// If an interrupt function has been set, go and run one it; otherwise,
|
|
|
|
// the process dies.
|
|
|
|
void (*IF)() = InterruptFunction;
|
|
|
|
InterruptFunction = 0; // Don't run it on another CTRL-C.
|
|
|
|
|
|
|
|
if (IF) {
|
2005-08-02 05:26:32 +02:00
|
|
|
// Note: if the interrupt function throws an exception, there is nothing
|
|
|
|
// to catch it in this thread so it will kill the process.
|
|
|
|
IF(); // Run it now.
|
2005-08-02 05:04:47 +02:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
return TRUE; // Don't kill the process.
|
|
|
|
}
|
|
|
|
|
2004-09-17 05:02:27 +02:00
|
|
|
// Allow normal processing to take place; i.e., the process dies.
|
2005-08-02 05:04:47 +02:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2004-09-16 17:53:16 +02:00
|
|
|
return FALSE;
|
2004-08-29 21:22:48 +02:00
|
|
|
}
|
2011-10-01 02:05:20 +02:00
|
|
|
|
|
|
|
#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
|
2020-09-24 14:14:45 +02:00
|
|
|
|
|
|
|
void sys::unregisterHandlers() {}
|