1 //===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides the Win32 specific implementation of the Signals class.
11 //===----------------------------------------------------------------------===//
12 #include "llvm/Support/ConvertUTF.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Process.h"
16 #include "llvm/Support/WindowsError.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/raw_ostream.h"
25 // The Windows.h header must be after LLVM and standard headers.
26 #include "WindowsSupport.h"
37 #pragma comment(lib, "psapi.lib")
39 // The version of g++ that comes with MinGW does *not* properly understand
40 // the ll format specifier for printf. However, MinGW passes the format
41 // specifiers on to the MSVCRT entirely, and the CRT understands the ll
42 // specifier. So these warnings are spurious in this case. Since we compile
43 // with -Wall, this will generate these warnings which should be ignored. So
44 // we will turn off the warnings for this just file. However, MinGW also does
45 // not support push and pop for diagnostics, so we have to manually turn it
46 // back on at the end of the file.
47 #pragma GCC diagnostic ignored "-Wformat"
48 #pragma GCC diagnostic ignored "-Wformat-extra-args"
50 #if !defined(__MINGW64_VERSION_MAJOR)
51 // MinGW.org does not have updated support for the 64-bit versions of the
52 // DebugHlp APIs. So we will have to load them manually. The structures and
53 // method signatures were pulled from DbgHelp.h in the Windows Platform SDK,
54 // and adjusted for brevity.
55 typedef struct _IMAGEHLP_LINE64 {
61 } IMAGEHLP_LINE64, *PIMAGEHLP_LINE64;
63 typedef struct _IMAGEHLP_SYMBOL64 {
70 } IMAGEHLP_SYMBOL64, *PIMAGEHLP_SYMBOL64;
72 typedef struct _tagADDRESS64 {
76 } ADDRESS64, *LPADDRESS64;
78 typedef struct _KDHELP64 {
80 DWORD ThCallbackStack;
81 DWORD ThCallbackBStore;
84 DWORD64 KiCallUserMode;
85 DWORD64 KeUserCallbackDispatcher;
86 DWORD64 SystemRangeStart;
87 DWORD64 KiUserExceptionDispatcher;
91 } KDHELP64, *PKDHELP64;
93 typedef struct _tagSTACKFRAME64 {
105 } STACKFRAME64, *LPSTACKFRAME64;
106 #endif // !defined(__MINGW64_VERSION_MAJOR)
107 #endif // __MINGW32__
109 typedef BOOL (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE hProcess,
110 DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize,
111 LPDWORD lpNumberOfBytesRead);
113 typedef PVOID (__stdcall *PFUNCTION_TABLE_ACCESS_ROUTINE64)( HANDLE ahProcess,
116 typedef DWORD64 (__stdcall *PGET_MODULE_BASE_ROUTINE64)(HANDLE hProcess,
119 typedef DWORD64 (__stdcall *PTRANSLATE_ADDRESS_ROUTINE64)(HANDLE hProcess,
120 HANDLE hThread, LPADDRESS64 lpaddr);
122 typedef BOOL(WINAPI *fpMiniDumpWriteDump)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE,
123 PMINIDUMP_EXCEPTION_INFORMATION,
124 PMINIDUMP_USER_STREAM_INFORMATION,
125 PMINIDUMP_CALLBACK_INFORMATION);
126 static fpMiniDumpWriteDump fMiniDumpWriteDump;
128 typedef BOOL (WINAPI *fpStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64,
129 PVOID, PREAD_PROCESS_MEMORY_ROUTINE64,
130 PFUNCTION_TABLE_ACCESS_ROUTINE64,
131 PGET_MODULE_BASE_ROUTINE64,
132 PTRANSLATE_ADDRESS_ROUTINE64);
133 static fpStackWalk64 fStackWalk64;
135 typedef DWORD64 (WINAPI *fpSymGetModuleBase64)(HANDLE, DWORD64);
136 static fpSymGetModuleBase64 fSymGetModuleBase64;
138 typedef BOOL (WINAPI *fpSymGetSymFromAddr64)(HANDLE, DWORD64,
139 PDWORD64, PIMAGEHLP_SYMBOL64);
140 static fpSymGetSymFromAddr64 fSymGetSymFromAddr64;
142 typedef BOOL (WINAPI *fpSymGetLineFromAddr64)(HANDLE, DWORD64,
143 PDWORD, PIMAGEHLP_LINE64);
144 static fpSymGetLineFromAddr64 fSymGetLineFromAddr64;
146 typedef BOOL(WINAPI *fpSymGetModuleInfo64)(HANDLE hProcess, DWORD64 dwAddr,
147 PIMAGEHLP_MODULE64 ModuleInfo);
148 static fpSymGetModuleInfo64 fSymGetModuleInfo64;
150 typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64);
151 static fpSymFunctionTableAccess64 fSymFunctionTableAccess64;
153 typedef DWORD (WINAPI *fpSymSetOptions)(DWORD);
154 static fpSymSetOptions fSymSetOptions;
156 typedef BOOL (WINAPI *fpSymInitialize)(HANDLE, PCSTR, BOOL);
157 static fpSymInitialize fSymInitialize;
159 typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
160 static fpEnumerateLoadedModules fEnumerateLoadedModules;
162 static bool load64BitDebugHelp(void) {
163 HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
165 fMiniDumpWriteDump = (fpMiniDumpWriteDump)
166 ::GetProcAddress(hLib, "MiniDumpWriteDump");
167 fStackWalk64 = (fpStackWalk64)
168 ::GetProcAddress(hLib, "StackWalk64");
169 fSymGetModuleBase64 = (fpSymGetModuleBase64)
170 ::GetProcAddress(hLib, "SymGetModuleBase64");
171 fSymGetSymFromAddr64 = (fpSymGetSymFromAddr64)
172 ::GetProcAddress(hLib, "SymGetSymFromAddr64");
173 fSymGetLineFromAddr64 = (fpSymGetLineFromAddr64)
174 ::GetProcAddress(hLib, "SymGetLineFromAddr64");
175 fSymGetModuleInfo64 = (fpSymGetModuleInfo64)
176 ::GetProcAddress(hLib, "SymGetModuleInfo64");
177 fSymFunctionTableAccess64 = (fpSymFunctionTableAccess64)
178 ::GetProcAddress(hLib, "SymFunctionTableAccess64");
179 fSymSetOptions = (fpSymSetOptions)::GetProcAddress(hLib, "SymSetOptions");
180 fSymInitialize = (fpSymInitialize)::GetProcAddress(hLib, "SymInitialize");
181 fEnumerateLoadedModules = (fpEnumerateLoadedModules)
182 ::GetProcAddress(hLib, "EnumerateLoadedModules64");
184 return fStackWalk64 && fSymInitialize && fSymSetOptions && fMiniDumpWriteDump;
187 using namespace llvm;
190 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
191 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
193 // The function to call if ctrl-c is pressed.
194 static void (*InterruptFunction)() = 0;
196 static std::vector<std::string> *FilesToRemove = NULL;
197 static bool RegisteredUnhandledExceptionFilter = false;
198 static bool CleanupExecuted = false;
199 static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
201 // Windows creates a new thread to execute the console handler when an event
202 // (such as CTRL/C) occurs. This causes concurrency issues with the above
203 // globals which this critical section addresses.
204 static CRITICAL_SECTION CriticalSection;
205 static bool CriticalSectionInitialized = false;
207 static StringRef Argv0;
211 NativeMachineType = IMAGE_FILE_MACHINE_AMD64
212 #elif defined(_M_ARM64)
213 NativeMachineType = IMAGE_FILE_MACHINE_ARM64
214 #elif defined(_M_IX86)
215 NativeMachineType = IMAGE_FILE_MACHINE_I386
216 #elif defined(_M_ARM)
217 NativeMachineType = IMAGE_FILE_MACHINE_ARMNT
219 NativeMachineType = IMAGE_FILE_MACHINE_UNKNOWN
223 static bool printStackTraceWithLLVMSymbolizer(llvm::raw_ostream &OS,
224 HANDLE hProcess, HANDLE hThread,
225 STACKFRAME64 &StackFrameOrig,
226 CONTEXT *ContextOrig) {
227 // StackWalk64 modifies the incoming stack frame and context, so copy them.
228 STACKFRAME64 StackFrame = StackFrameOrig;
230 // Copy the register context so that we don't modify it while we unwind. We
231 // could use InitializeContext + CopyContext, but that's only required to get
232 // at AVX registers, which typically aren't needed by StackWalk64. Reduce the
233 // flag set to indicate that there's less data.
234 CONTEXT Context = *ContextOrig;
235 Context.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
237 static void *StackTrace[256];
239 while (fStackWalk64(NativeMachineType, hProcess, hThread, &StackFrame,
240 &Context, 0, fSymFunctionTableAccess64,
241 fSymGetModuleBase64, 0)) {
242 if (StackFrame.AddrFrame.Offset == 0)
244 StackTrace[Depth++] = (void *)(uintptr_t)StackFrame.AddrPC.Offset;
245 if (Depth >= array_lengthof(StackTrace))
249 return printSymbolizedStackTrace(Argv0, &StackTrace[0], Depth, OS);
253 struct FindModuleData {
256 const char **Modules;
258 StringSaver *StrPool;
262 static BOOL CALLBACK findModuleCallback(PCSTR ModuleName,
263 DWORD64 ModuleBase, ULONG ModuleSize,
265 FindModuleData *Data = (FindModuleData*)VoidData;
266 intptr_t Beg = ModuleBase;
267 intptr_t End = Beg + ModuleSize;
268 for (int I = 0; I < Data->Depth; I++) {
269 if (Data->Modules[I])
271 intptr_t Addr = (intptr_t)Data->StackTrace[I];
272 if (Beg <= Addr && Addr < End) {
273 Data->Modules[I] = Data->StrPool->save(ModuleName).data();
274 Data->Offsets[I] = Addr - Beg;
280 static bool findModulesAndOffsets(void **StackTrace, int Depth,
281 const char **Modules, intptr_t *Offsets,
282 const char *MainExecutableName,
283 StringSaver &StrPool) {
284 if (!fEnumerateLoadedModules)
287 Data.StackTrace = StackTrace;
289 Data.Modules = Modules;
290 Data.Offsets = Offsets;
291 Data.StrPool = &StrPool;
292 fEnumerateLoadedModules(GetCurrentProcess(), findModuleCallback, &Data);
296 static void PrintStackTraceForThread(llvm::raw_ostream &OS, HANDLE hProcess,
297 HANDLE hThread, STACKFRAME64 &StackFrame,
299 // Initialize the symbol handler.
300 fSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES);
301 fSymInitialize(hProcess, NULL, TRUE);
303 // Try llvm-symbolizer first. llvm-symbolizer knows how to deal with both PDBs
304 // and DWARF, so it should do a good job regardless of what debug info or
306 if (printStackTraceWithLLVMSymbolizer(OS, hProcess, hThread, StackFrame,
312 if (!fStackWalk64(NativeMachineType, hProcess, hThread, &StackFrame,
313 Context, 0, fSymFunctionTableAccess64,
314 fSymGetModuleBase64, 0)) {
318 if (StackFrame.AddrFrame.Offset == 0)
321 using namespace llvm;
322 // Print the PC in hexadecimal.
323 DWORD64 PC = StackFrame.AddrPC.Offset;
324 #if defined(_M_X64) || defined(_M_ARM64)
325 OS << format("0x%016llX", PC);
326 #elif defined(_M_IX86) || defined(_M_ARM)
327 OS << format("0x%08lX", static_cast<DWORD>(PC));
330 // Print the parameters. Assume there are four.
331 #if defined(_M_X64) || defined(_M_ARM64)
332 OS << format(" (0x%016llX 0x%016llX 0x%016llX 0x%016llX)",
333 StackFrame.Params[0], StackFrame.Params[1], StackFrame.Params[2],
334 StackFrame.Params[3]);
335 #elif defined(_M_IX86) || defined(_M_ARM)
336 OS << format(" (0x%08lX 0x%08lX 0x%08lX 0x%08lX)",
337 static_cast<DWORD>(StackFrame.Params[0]),
338 static_cast<DWORD>(StackFrame.Params[1]),
339 static_cast<DWORD>(StackFrame.Params[2]),
340 static_cast<DWORD>(StackFrame.Params[3]));
342 // Verify the PC belongs to a module in this process.
343 if (!fSymGetModuleBase64(hProcess, PC)) {
344 OS << " <unknown module>\n";
348 // Print the symbol name.
350 IMAGEHLP_SYMBOL64 *symbol = reinterpret_cast<IMAGEHLP_SYMBOL64 *>(buffer);
351 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64));
352 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
353 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64);
356 if (!fSymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
363 OS << format(", %s() + 0x%llX bytes(s)", (const char*)symbol->Name,
366 OS << format(", %s", (const char*)symbol->Name);
368 // Print the source file and line number information.
369 IMAGEHLP_LINE64 line = {};
371 line.SizeOfStruct = sizeof(line);
372 if (fSymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) {
373 OS << format(", %s, line %lu", line.FileName, line.LineNumber);
375 OS << format(" + 0x%lX byte(s)", dwLineDisp);
384 //===----------------------------------------------------------------------===//
385 //=== WARNING: Implementation here must contain only Win32 specific code
386 //=== and must not be UNIX code
387 //===----------------------------------------------------------------------===//
390 /// Emulates hitting "retry" from an "abort, retry, ignore" CRT debug report
391 /// dialog. "retry" raises an exception which ultimately triggers our stack
393 static LLVM_ATTRIBUTE_UNUSED int
394 AvoidMessageBoxHook(int ReportType, char *Message, int *Return) {
395 // Set *Return to the retry code for the return value of _CrtDbgReport:
396 // http://msdn.microsoft.com/en-us/library/8hyw4sy7(v=vs.71).aspx
397 // This may also trigger just-in-time debugging via DebugBreak().
400 // Don't call _CrtDbgReport.
406 extern "C" void HandleAbort(int Sig) {
407 if (Sig == SIGABRT) {
412 static void InitializeThreading() {
413 if (CriticalSectionInitialized)
416 // Now's the time to create the critical section. This is the first time
417 // through here, and there's only one thread.
418 InitializeCriticalSection(&CriticalSection);
419 CriticalSectionInitialized = true;
422 static void RegisterHandler() {
423 // If we cannot load up the APIs (which would be unexpected as they should
424 // exist on every version of Windows we support), we will bail out since
425 // there would be nothing to report.
426 if (!load64BitDebugHelp()) {
427 assert(false && "These APIs should always be available");
431 if (RegisteredUnhandledExceptionFilter) {
432 EnterCriticalSection(&CriticalSection);
436 InitializeThreading();
438 // Enter it immediately. Now if someone hits CTRL/C, the console handler
439 // can't proceed until the globals are updated.
440 EnterCriticalSection(&CriticalSection);
442 RegisteredUnhandledExceptionFilter = true;
443 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
444 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
446 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
447 // else multi-threading problems will ensue.
451 bool sys::RemoveFileOnSignal(StringRef Filename, std::string* ErrMsg) {
454 if (CleanupExecuted) {
456 *ErrMsg = "Process terminating -- cannot register for removal";
460 if (FilesToRemove == NULL)
461 FilesToRemove = new std::vector<std::string>;
463 FilesToRemove->push_back(Filename);
465 LeaveCriticalSection(&CriticalSection);
470 void sys::DontRemoveFileOnSignal(StringRef Filename) {
471 if (FilesToRemove == NULL)
476 std::vector<std::string>::reverse_iterator I =
477 find(reverse(*FilesToRemove), Filename);
478 if (I != FilesToRemove->rend())
479 FilesToRemove->erase(I.base()-1);
481 LeaveCriticalSection(&CriticalSection);
484 void sys::DisableSystemDialogsOnCrash() {
485 // Crash to stack trace handler on abort.
486 signal(SIGABRT, HandleAbort);
488 // The following functions are not reliably accessible on MinGW.
490 // We're already handling writing a "something went wrong" message.
491 _set_abort_behavior(0, _WRITE_ABORT_MSG);
492 // Disable Dr. Watson.
493 _set_abort_behavior(0, _CALL_REPORTFAULT);
494 _CrtSetReportHook(AvoidMessageBoxHook);
497 // Disable standard error dialog box.
498 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
499 SEM_NOOPENFILEERRORBOX);
500 _set_error_mode(_OUT_TO_STDERR);
503 /// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
504 /// process, print a stack trace and then exit.
505 void sys::PrintStackTraceOnErrorSignal(StringRef Argv0,
506 bool DisableCrashReporting) {
509 if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT"))
510 Process::PreventCoreFiles();
512 DisableSystemDialogsOnCrash();
514 LeaveCriticalSection(&CriticalSection);
518 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
519 // Provide a prototype for RtlCaptureContext, mingw32 from mingw.org is
520 // missing it but mingw-w64 has it.
521 extern "C" VOID WINAPI RtlCaptureContext(PCONTEXT ContextRecord);
524 void llvm::sys::PrintStackTrace(raw_ostream &OS) {
525 STACKFRAME64 StackFrame = {};
526 CONTEXT Context = {};
527 ::RtlCaptureContext(&Context);
529 StackFrame.AddrPC.Offset = Context.Rip;
530 StackFrame.AddrStack.Offset = Context.Rsp;
531 StackFrame.AddrFrame.Offset = Context.Rbp;
532 #elif defined(_M_IX86)
533 StackFrame.AddrPC.Offset = Context.Eip;
534 StackFrame.AddrStack.Offset = Context.Esp;
535 StackFrame.AddrFrame.Offset = Context.Ebp;
536 #elif defined(_M_ARM64)
537 StackFrame.AddrPC.Offset = Context.Pc;
538 StackFrame.AddrStack.Offset = Context.Sp;
539 StackFrame.AddrFrame.Offset = Context.Fp;
540 #elif defined(_M_ARM)
541 StackFrame.AddrPC.Offset = Context.Pc;
542 StackFrame.AddrStack.Offset = Context.Sp;
543 StackFrame.AddrFrame.Offset = Context.R11;
545 StackFrame.AddrPC.Mode = AddrModeFlat;
546 StackFrame.AddrStack.Mode = AddrModeFlat;
547 StackFrame.AddrFrame.Mode = AddrModeFlat;
548 PrintStackTraceForThread(OS, GetCurrentProcess(), GetCurrentThread(),
549 StackFrame, &Context);
553 void llvm::sys::SetInterruptFunction(void (*IF)()) {
555 InterruptFunction = IF;
556 LeaveCriticalSection(&CriticalSection);
559 void llvm::sys::SetInfoSignalFunction(void (*Handler)()) {
563 void llvm::sys::SetPipeSignalFunction(void (*Handler)()) {
567 /// Add a function to be called when a signal is delivered to the process. The
568 /// handler can have a cookie passed to it to identify what instance of the
570 void llvm::sys::AddSignalHandler(sys::SignalHandlerCallback FnPtr,
572 insertSignalHandler(FnPtr, Cookie);
574 LeaveCriticalSection(&CriticalSection);
577 static void Cleanup() {
581 EnterCriticalSection(&CriticalSection);
583 // Prevent other thread from registering new files and directories for
584 // removal, should we be executing because of the console handler callback.
585 CleanupExecuted = true;
587 // FIXME: open files cannot be deleted.
588 if (FilesToRemove != NULL)
589 while (!FilesToRemove->empty()) {
590 llvm::sys::fs::remove(FilesToRemove->back());
591 FilesToRemove->pop_back();
593 llvm::sys::RunSignalHandlers();
594 LeaveCriticalSection(&CriticalSection);
597 void llvm::sys::RunInterruptHandlers() {
598 // The interrupt handler may be called from an interrupt, but it may also be
599 // called manually (such as the case of report_fatal_error with no registered
600 // error handler). We must ensure that the critical section is properly
602 InitializeThreading();
606 /// Find the Windows Registry Key for a given location.
608 /// \returns a valid HKEY if the location exists, else NULL.
609 static HKEY FindWERKey(const llvm::Twine &RegistryLocation) {
611 if (ERROR_SUCCESS != ::RegOpenKeyExA(HKEY_LOCAL_MACHINE,
612 RegistryLocation.str().c_str(), 0,
613 KEY_QUERY_VALUE | KEY_READ, &Key))
619 /// Populate ResultDirectory with the value for "DumpFolder" for a given
620 /// Windows Registry key.
622 /// \returns true if a valid value for DumpFolder exists, false otherwise.
623 static bool GetDumpFolder(HKEY Key,
624 llvm::SmallVectorImpl<char> &ResultDirectory) {
625 using llvm::sys::windows::UTF16ToUTF8;
630 DWORD BufferLengthBytes = 0;
632 if (ERROR_SUCCESS != ::RegGetValueW(Key, 0, L"DumpFolder", REG_EXPAND_SZ,
633 NULL, NULL, &BufferLengthBytes))
636 SmallVector<wchar_t, MAX_PATH> Buffer(BufferLengthBytes);
638 if (ERROR_SUCCESS != ::RegGetValueW(Key, 0, L"DumpFolder", REG_EXPAND_SZ,
639 NULL, Buffer.data(), &BufferLengthBytes))
642 DWORD ExpandBufferSize = ::ExpandEnvironmentStringsW(Buffer.data(), NULL, 0);
644 if (!ExpandBufferSize)
647 SmallVector<wchar_t, MAX_PATH> ExpandBuffer(ExpandBufferSize);
649 if (ExpandBufferSize != ::ExpandEnvironmentStringsW(Buffer.data(),
654 if (UTF16ToUTF8(ExpandBuffer.data(), ExpandBufferSize - 1, ResultDirectory))
660 /// Populate ResultType with a valid MINIDUMP_TYPE based on the value of
661 /// "DumpType" for a given Windows Registry key.
664 /// https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181(v=vs.85).aspx
665 /// valid values for DumpType are:
669 /// If "Custom dump" is specified then the "CustomDumpFlags" field is read
670 /// containing a bitwise combination of MINIDUMP_TYPE values.
672 /// \returns true if a valid value for ResultType can be set, false otherwise.
673 static bool GetDumpType(HKEY Key, MINIDUMP_TYPE &ResultType) {
678 DWORD TypeSize = sizeof(DumpType);
679 if (ERROR_SUCCESS != ::RegGetValueW(Key, NULL, L"DumpType", RRF_RT_REG_DWORD,
687 if (ERROR_SUCCESS != ::RegGetValueW(Key, NULL, L"CustomDumpFlags",
688 RRF_RT_REG_DWORD, NULL, &Flags,
692 ResultType = static_cast<MINIDUMP_TYPE>(Flags);
696 ResultType = MiniDumpNormal;
699 ResultType = MiniDumpWithFullMemory;
707 /// Write a Windows dump file containing process information that can be
708 /// used for post-mortem debugging.
710 /// \returns zero error code if a mini dump created, actual error code
712 static std::error_code WINAPI
713 WriteWindowsDumpFile(PMINIDUMP_EXCEPTION_INFORMATION ExceptionInfo) {
714 using namespace llvm;
715 using namespace llvm::sys;
717 std::string MainExecutableName = fs::getMainExecutable(nullptr, nullptr);
718 StringRef ProgramName;
720 if (MainExecutableName.empty()) {
721 // If we can't get the executable filename,
722 // things are in worse shape than we realize
723 // and we should just bail out.
724 return mapWindowsError(::GetLastError());
727 ProgramName = path::filename(MainExecutableName.c_str());
729 // The Windows Registry location as specified at
730 // https://msdn.microsoft.com/en-us/library/windows/desktop/bb787181%28v=vs.85%29.aspx
731 // "Collecting User-Mode Dumps" that may optionally be set to collect crash
732 // dumps in a specified location.
733 StringRef LocalDumpsRegistryLocation =
734 "SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps";
736 // The key pointing to the Registry location that may contain global crash
737 // dump settings. This will be NULL if the location can not be found.
738 ScopedRegHandle DefaultLocalDumpsKey(FindWERKey(LocalDumpsRegistryLocation));
740 // The key pointing to the Registry location that may contain
741 // application-specific crash dump settings. This will be NULL if the
742 // location can not be found.
743 ScopedRegHandle AppSpecificKey(
744 FindWERKey(Twine(LocalDumpsRegistryLocation) + "\\" + ProgramName));
746 // Look to see if a dump type is specified in the registry; first with the
747 // app-specific key and failing that with the global key. If none are found
748 // default to a normal dump (GetDumpType will return false either if the key
749 // is NULL or if there is no valid DumpType value at its location).
750 MINIDUMP_TYPE DumpType;
751 if (!GetDumpType(AppSpecificKey, DumpType))
752 if (!GetDumpType(DefaultLocalDumpsKey, DumpType))
753 DumpType = MiniDumpNormal;
755 // Look to see if a dump location is specified in the registry; first with the
756 // app-specific key and failing that with the global key. If none are found
757 // we'll just create the dump file in the default temporary file location
758 // (GetDumpFolder will return false either if the key is NULL or if there is
759 // no valid DumpFolder value at its location).
760 bool ExplicitDumpDirectorySet = true;
761 SmallString<MAX_PATH> DumpDirectory;
762 if (!GetDumpFolder(AppSpecificKey, DumpDirectory))
763 if (!GetDumpFolder(DefaultLocalDumpsKey, DumpDirectory))
764 ExplicitDumpDirectorySet = false;
767 SmallString<MAX_PATH> DumpPath;
769 if (ExplicitDumpDirectorySet) {
770 if (std::error_code EC = fs::create_directories(DumpDirectory))
772 if (std::error_code EC = fs::createUniqueFile(
773 Twine(DumpDirectory) + "\\" + ProgramName + ".%%%%%%.dmp", FD,
776 } else if (std::error_code EC =
777 fs::createTemporaryFile(ProgramName, "dmp", FD, DumpPath))
780 // Our support functions return a file descriptor but Windows wants a handle.
781 ScopedCommonHandle FileHandle(reinterpret_cast<HANDLE>(_get_osfhandle(FD)));
783 if (!fMiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(),
784 FileHandle, DumpType, ExceptionInfo, NULL, NULL))
785 return mapWindowsError(::GetLastError());
787 llvm::errs() << "Wrote crash dump file \"" << DumpPath << "\"\n";
788 return std::error_code();
791 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
794 // We'll automatically write a Minidump file here to help diagnose
795 // the nasty sorts of crashes that aren't 100% reproducible from a set of
796 // inputs (or in the event that the user is unable or unwilling to provide a
797 // reproducible case).
798 if (!llvm::sys::Process::AreCoreFilesPrevented()) {
799 MINIDUMP_EXCEPTION_INFORMATION ExceptionInfo;
800 ExceptionInfo.ThreadId = ::GetCurrentThreadId();
801 ExceptionInfo.ExceptionPointers = ep;
802 ExceptionInfo.ClientPointers = FALSE;
804 if (std::error_code EC = WriteWindowsDumpFile(&ExceptionInfo))
805 llvm::errs() << "Could not write crash dump file: " << EC.message()
809 // Initialize the STACKFRAME structure.
810 STACKFRAME64 StackFrame = {};
813 StackFrame.AddrPC.Offset = ep->ContextRecord->Rip;
814 StackFrame.AddrPC.Mode = AddrModeFlat;
815 StackFrame.AddrStack.Offset = ep->ContextRecord->Rsp;
816 StackFrame.AddrStack.Mode = AddrModeFlat;
817 StackFrame.AddrFrame.Offset = ep->ContextRecord->Rbp;
818 StackFrame.AddrFrame.Mode = AddrModeFlat;
819 #elif defined(_M_IX86)
820 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
821 StackFrame.AddrPC.Mode = AddrModeFlat;
822 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
823 StackFrame.AddrStack.Mode = AddrModeFlat;
824 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
825 StackFrame.AddrFrame.Mode = AddrModeFlat;
826 #elif defined(_M_ARM64) || defined(_M_ARM)
827 StackFrame.AddrPC.Offset = ep->ContextRecord->Pc;
828 StackFrame.AddrPC.Mode = AddrModeFlat;
829 StackFrame.AddrStack.Offset = ep->ContextRecord->Sp;
830 StackFrame.AddrStack.Mode = AddrModeFlat;
831 #if defined(_M_ARM64)
832 StackFrame.AddrFrame.Offset = ep->ContextRecord->Fp;
834 StackFrame.AddrFrame.Offset = ep->ContextRecord->R11;
836 StackFrame.AddrFrame.Mode = AddrModeFlat;
839 HANDLE hProcess = GetCurrentProcess();
840 HANDLE hThread = GetCurrentThread();
841 PrintStackTraceForThread(llvm::errs(), hProcess, hThread, StackFrame,
844 _exit(ep->ExceptionRecord->ExceptionCode);
847 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
848 // We are running in our very own thread, courtesy of Windows.
849 EnterCriticalSection(&CriticalSection);
852 // If an interrupt function has been set, go and run one it; otherwise,
854 void (*IF)() = InterruptFunction;
855 InterruptFunction = 0; // Don't run it on another CTRL-C.
858 // Note: if the interrupt function throws an exception, there is nothing
859 // to catch it in this thread so it will kill the process.
861 LeaveCriticalSection(&CriticalSection);
862 return TRUE; // Don't kill the process.
865 // Allow normal processing to take place; i.e., the process dies.
866 LeaveCriticalSection(&CriticalSection);
871 // We turned these warnings off for this file so that MinGW-g++ doesn't
872 // complain about the ll format specifiers used. Now we are turning the
873 // warnings back on. If MinGW starts to support diagnostic stacks, we can
874 // replace this with a pop.
875 #pragma GCC diagnostic warning "-Wformat"
876 #pragma GCC diagnostic warning "-Wformat-extra-args"