Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 1 | //===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===// |
Mikhail Glushenkov | f64d93d | 2010-10-21 20:40:39 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Mikhail Glushenkov | f64d93d | 2010-10-21 20:40:39 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Signals class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Rafael Espindola | 9aa3d5d | 2013-06-14 13:59:21 +0000 | [diff] [blame] | 13 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Michael J. Spencer | 89b0ad2 | 2015-01-29 17:20:29 +0000 | [diff] [blame] | 15 | #include <signal.h> |
Reid Spencer | 70b6835 | 2004-09-28 23:58:03 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
Chandler Carruth | 10b0915 | 2014-01-07 12:37:13 +0000 | [diff] [blame] | 22 | // The Windows.h header must be after LLVM and standard headers. |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 23 | #include "WindowsSupport.h" |
Chandler Carruth | 10b0915 | 2014-01-07 12:37:13 +0000 | [diff] [blame] | 24 | |
Jeff Cohen | 07e22ba | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 25 | #ifdef __MINGW32__ |
Reid Spencer | 187b4ad | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 26 | #include <imagehlp.h> |
Reid Spencer | 9904928 | 2004-09-23 14:47:10 +0000 | [diff] [blame] | 27 | #else |
Reid Spencer | 187b4ad | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 28 | #include <dbghelp.h> |
Reid Spencer | 9904928 | 2004-09-23 14:47:10 +0000 | [diff] [blame] | 29 | #endif |
| 30 | #include <psapi.h> |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 31 | |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 32 | #ifdef _MSC_VER |
| 33 | #pragma comment(lib, "psapi.lib") |
| 34 | #pragma comment(lib, "dbghelp.lib") |
| 35 | #elif __MINGW32__ |
Reid Spencer | 187b4ad | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 36 | #if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1)) |
| 37 | #error "libimagehlp.a & libpsapi.a should be present" |
| 38 | #endif |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 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" |
| 49 | |
Anton Korobeynikov | b27f11e | 2011-10-21 09:38:50 +0000 | [diff] [blame] | 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. |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 55 | typedef struct _IMAGEHLP_LINE64 { |
| 56 | DWORD SizeOfStruct; |
| 57 | PVOID Key; |
| 58 | DWORD LineNumber; |
| 59 | PCHAR FileName; |
| 60 | DWORD64 Address; |
| 61 | } IMAGEHLP_LINE64, *PIMAGEHLP_LINE64; |
| 62 | |
| 63 | typedef struct _IMAGEHLP_SYMBOL64 { |
| 64 | DWORD SizeOfStruct; |
| 65 | DWORD64 Address; |
| 66 | DWORD Size; |
| 67 | DWORD Flags; |
| 68 | DWORD MaxNameLength; |
| 69 | CHAR Name[1]; |
| 70 | } IMAGEHLP_SYMBOL64, *PIMAGEHLP_SYMBOL64; |
| 71 | |
| 72 | typedef struct _tagADDRESS64 { |
| 73 | DWORD64 Offset; |
| 74 | WORD Segment; |
| 75 | ADDRESS_MODE Mode; |
| 76 | } ADDRESS64, *LPADDRESS64; |
| 77 | |
| 78 | typedef struct _KDHELP64 { |
| 79 | DWORD64 Thread; |
| 80 | DWORD ThCallbackStack; |
| 81 | DWORD ThCallbackBStore; |
| 82 | DWORD NextCallback; |
| 83 | DWORD FramePointer; |
| 84 | DWORD64 KiCallUserMode; |
| 85 | DWORD64 KeUserCallbackDispatcher; |
| 86 | DWORD64 SystemRangeStart; |
| 87 | DWORD64 KiUserExceptionDispatcher; |
| 88 | DWORD64 StackBase; |
| 89 | DWORD64 StackLimit; |
| 90 | DWORD64 Reserved[5]; |
| 91 | } KDHELP64, *PKDHELP64; |
| 92 | |
| 93 | typedef struct _tagSTACKFRAME64 { |
| 94 | ADDRESS64 AddrPC; |
| 95 | ADDRESS64 AddrReturn; |
| 96 | ADDRESS64 AddrFrame; |
| 97 | ADDRESS64 AddrStack; |
| 98 | ADDRESS64 AddrBStore; |
| 99 | PVOID FuncTableEntry; |
| 100 | DWORD64 Params[4]; |
| 101 | BOOL Far; |
| 102 | BOOL Virtual; |
| 103 | DWORD64 Reserved[3]; |
| 104 | KDHELP64 KdHelp; |
| 105 | } STACKFRAME64, *LPSTACKFRAME64; |
| 106 | |
| 107 | typedef BOOL (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE hProcess, |
| 108 | DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize, |
| 109 | LPDWORD lpNumberOfBytesRead); |
| 110 | |
| 111 | typedef PVOID (__stdcall *PFUNCTION_TABLE_ACCESS_ROUTINE64)( HANDLE ahProcess, |
| 112 | DWORD64 AddrBase); |
| 113 | |
| 114 | typedef DWORD64 (__stdcall *PGET_MODULE_BASE_ROUTINE64)(HANDLE hProcess, |
| 115 | DWORD64 Address); |
| 116 | |
| 117 | typedef DWORD64 (__stdcall *PTRANSLATE_ADDRESS_ROUTINE64)(HANDLE hProcess, |
| 118 | HANDLE hThread, LPADDRESS64 lpaddr); |
| 119 | |
| 120 | typedef BOOL (WINAPI *fpStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64, |
| 121 | PVOID, PREAD_PROCESS_MEMORY_ROUTINE64, |
| 122 | PFUNCTION_TABLE_ACCESS_ROUTINE64, |
| 123 | PGET_MODULE_BASE_ROUTINE64, |
| 124 | PTRANSLATE_ADDRESS_ROUTINE64); |
| 125 | static fpStackWalk64 StackWalk64; |
| 126 | |
| 127 | typedef DWORD64 (WINAPI *fpSymGetModuleBase64)(HANDLE, DWORD64); |
| 128 | static fpSymGetModuleBase64 SymGetModuleBase64; |
| 129 | |
| 130 | typedef BOOL (WINAPI *fpSymGetSymFromAddr64)(HANDLE, DWORD64, |
| 131 | PDWORD64, PIMAGEHLP_SYMBOL64); |
| 132 | static fpSymGetSymFromAddr64 SymGetSymFromAddr64; |
| 133 | |
| 134 | typedef BOOL (WINAPI *fpSymGetLineFromAddr64)(HANDLE, DWORD64, |
| 135 | PDWORD, PIMAGEHLP_LINE64); |
| 136 | static fpSymGetLineFromAddr64 SymGetLineFromAddr64; |
| 137 | |
| 138 | typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64); |
| 139 | static fpSymFunctionTableAccess64 SymFunctionTableAccess64; |
| 140 | |
| 141 | static bool load64BitDebugHelp(void) { |
David Majnemer | 17a4496 | 2013-10-07 09:52:36 +0000 | [diff] [blame] | 142 | HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll"); |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 143 | if (hLib) { |
| 144 | StackWalk64 = (fpStackWalk64) |
| 145 | ::GetProcAddress(hLib, "StackWalk64"); |
| 146 | SymGetModuleBase64 = (fpSymGetModuleBase64) |
| 147 | ::GetProcAddress(hLib, "SymGetModuleBase64"); |
| 148 | SymGetSymFromAddr64 = (fpSymGetSymFromAddr64) |
| 149 | ::GetProcAddress(hLib, "SymGetSymFromAddr64"); |
| 150 | SymGetLineFromAddr64 = (fpSymGetLineFromAddr64) |
| 151 | ::GetProcAddress(hLib, "SymGetLineFromAddr64"); |
| 152 | SymFunctionTableAccess64 = (fpSymFunctionTableAccess64) |
| 153 | ::GetProcAddress(hLib, "SymFunctionTableAccess64"); |
| 154 | } |
| 155 | return StackWalk64 != NULL; |
| 156 | } |
Anton Korobeynikov | b27f11e | 2011-10-21 09:38:50 +0000 | [diff] [blame] | 157 | #endif // !defined(__MINGW64_VERSION_MAJOR) |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 158 | #endif // __MINGW32__ |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 159 | |
| 160 | // Forward declare. |
| 161 | static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep); |
| 162 | static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType); |
| 163 | |
Jeff Cohen | ba7cc68 | 2005-08-02 03:04:47 +0000 | [diff] [blame] | 164 | // InterruptFunction - The function to call if ctrl-c is pressed. |
| 165 | static void (*InterruptFunction)() = 0; |
| 166 | |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 167 | static std::vector<std::string> *FilesToRemove = NULL; |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 168 | static std::vector<std::pair<void(*)(void*), void*> > *CallBacksToRun = 0; |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 169 | static bool RegisteredUnhandledExceptionFilter = false; |
Reid Spencer | 1bdd0f0 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 170 | static bool CleanupExecuted = false; |
| 171 | static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL; |
Reid Spencer | 90debc5 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 172 | |
| 173 | // Windows creates a new thread to execute the console handler when an event |
| 174 | // (such as CTRL/C) occurs. This causes concurrency issues with the above |
| 175 | // globals which this critical section addresses. |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 176 | static CRITICAL_SECTION CriticalSection; |
Aaron Ballman | 50af8d4 | 2015-03-26 16:24:38 +0000 | [diff] [blame] | 177 | static bool CriticalSectionInitialized = false; |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 178 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 179 | static void PrintStackTraceForThread(llvm::raw_ostream &OS, HANDLE hProcess, |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 180 | HANDLE hThread, STACKFRAME64 &StackFrame, |
| 181 | CONTEXT *Context) { |
| 182 | DWORD machineType; |
| 183 | #if defined(_M_X64) |
| 184 | machineType = IMAGE_FILE_MACHINE_AMD64; |
| 185 | #else |
| 186 | machineType = IMAGE_FILE_MACHINE_I386; |
| 187 | #endif |
| 188 | |
| 189 | // Initialize the symbol handler. |
| 190 | SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES); |
| 191 | SymInitialize(hProcess, NULL, TRUE); |
| 192 | |
| 193 | while (true) { |
| 194 | if (!StackWalk64(machineType, hProcess, hThread, &StackFrame, Context, NULL, |
| 195 | SymFunctionTableAccess64, SymGetModuleBase64, NULL)) { |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | if (StackFrame.AddrFrame.Offset == 0) |
| 200 | break; |
| 201 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 202 | using namespace llvm; |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 203 | // Print the PC in hexadecimal. |
| 204 | DWORD64 PC = StackFrame.AddrPC.Offset; |
| 205 | #if defined(_M_X64) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 206 | OS << format("0x%016llX", PC); |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 207 | #elif defined(_M_IX86) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 208 | OS << format("0x%08lX", static_cast<DWORD>(PC)); |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 209 | #endif |
| 210 | |
| 211 | // Print the parameters. Assume there are four. |
| 212 | #if defined(_M_X64) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 213 | OS << format(" (0x%016llX 0x%016llX 0x%016llX 0x%016llX)", |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 214 | StackFrame.Params[0], StackFrame.Params[1], StackFrame.Params[2], |
| 215 | StackFrame.Params[3]); |
| 216 | #elif defined(_M_IX86) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 217 | OS << format(" (0x%08lX 0x%08lX 0x%08lX 0x%08lX)", |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 218 | static_cast<DWORD>(StackFrame.Params[0]), |
| 219 | static_cast<DWORD>(StackFrame.Params[1]), |
| 220 | static_cast<DWORD>(StackFrame.Params[2]), |
| 221 | static_cast<DWORD>(StackFrame.Params[3])); |
| 222 | #endif |
| 223 | // Verify the PC belongs to a module in this process. |
| 224 | if (!SymGetModuleBase64(hProcess, PC)) { |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 225 | OS << " <unknown module>\n"; |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 226 | continue; |
| 227 | } |
| 228 | |
| 229 | // Print the symbol name. |
| 230 | char buffer[512]; |
| 231 | IMAGEHLP_SYMBOL64 *symbol = reinterpret_cast<IMAGEHLP_SYMBOL64 *>(buffer); |
| 232 | memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64)); |
| 233 | symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64); |
| 234 | symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64); |
| 235 | |
| 236 | DWORD64 dwDisp; |
| 237 | if (!SymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) { |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 238 | OS << '\n'; |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 239 | continue; |
| 240 | } |
| 241 | |
| 242 | buffer[511] = 0; |
| 243 | if (dwDisp > 0) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 244 | OS << format(", %s() + 0x%llX bytes(s)", (const char*)symbol->Name, |
| 245 | dwDisp); |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 246 | else |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 247 | OS << format(", %s", (const char*)symbol->Name); |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 248 | |
| 249 | // Print the source file and line number information. |
Yaron Keren | 24a86df | 2015-04-24 15:39:47 +0000 | [diff] [blame] | 250 | IMAGEHLP_LINE64 line = {}; |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 251 | DWORD dwLineDisp; |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 252 | line.SizeOfStruct = sizeof(line); |
| 253 | if (SymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) { |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 254 | OS << format(", %s, line %lu", line.FileName, line.LineNumber); |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 255 | if (dwLineDisp > 0) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 256 | OS << format(" + 0x%lX byte(s)", dwLineDisp); |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 259 | OS << '\n'; |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 263 | namespace llvm { |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 264 | |
| 265 | //===----------------------------------------------------------------------===// |
Mikhail Glushenkov | f64d93d | 2010-10-21 20:40:39 +0000 | [diff] [blame] | 266 | //=== WARNING: Implementation here must contain only Win32 specific code |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 267 | //=== and must not be UNIX code |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | |
Daniel Dunbar | 1bdedd3 | 2009-09-22 15:58:35 +0000 | [diff] [blame] | 270 | #ifdef _MSC_VER |
Reid Kleckner | bd39f21 | 2013-04-05 16:18:03 +0000 | [diff] [blame] | 271 | /// AvoidMessageBoxHook - Emulates hitting "retry" from an "abort, retry, |
| 272 | /// ignore" CRT debug report dialog. "retry" raises an exception which |
| 273 | /// ultimately triggers our stack dumper. |
Reid Kleckner | 542a454 | 2015-02-26 21:08:21 +0000 | [diff] [blame] | 274 | static LLVM_ATTRIBUTE_UNUSED int |
| 275 | AvoidMessageBoxHook(int ReportType, char *Message, int *Return) { |
Reid Kleckner | bd39f21 | 2013-04-05 16:18:03 +0000 | [diff] [blame] | 276 | // Set *Return to the retry code for the return value of _CrtDbgReport: |
| 277 | // http://msdn.microsoft.com/en-us/library/8hyw4sy7(v=vs.71).aspx |
| 278 | // This may also trigger just-in-time debugging via DebugBreak(). |
| 279 | if (Return) |
| 280 | *Return = 1; |
| 281 | // Don't call _CrtDbgReport. |
| 282 | return TRUE; |
| 283 | } |
| 284 | |
Daniel Dunbar | 1bdedd3 | 2009-09-22 15:58:35 +0000 | [diff] [blame] | 285 | #endif |
Daniel Dunbar | 4c7b0ca | 2009-09-22 09:50:28 +0000 | [diff] [blame] | 286 | |
Aaron Ballman | 03b968e | 2015-01-29 20:48:34 +0000 | [diff] [blame] | 287 | extern "C" void HandleAbort(int Sig) { |
Michael J. Spencer | 89b0ad2 | 2015-01-29 17:20:29 +0000 | [diff] [blame] | 288 | if (Sig == SIGABRT) { |
| 289 | LLVM_BUILTIN_TRAP; |
| 290 | } |
| 291 | } |
| 292 | |
Aaron Ballman | 50af8d4 | 2015-03-26 16:24:38 +0000 | [diff] [blame] | 293 | static void InitializeThreading() { |
| 294 | if (CriticalSectionInitialized) |
| 295 | return; |
| 296 | |
| 297 | // Now's the time to create the critical section. This is the first time |
| 298 | // through here, and there's only one thread. |
| 299 | InitializeCriticalSection(&CriticalSection); |
| 300 | CriticalSectionInitialized = true; |
| 301 | } |
| 302 | |
Daniel Dunbar | 4c7b0ca | 2009-09-22 09:50:28 +0000 | [diff] [blame] | 303 | static void RegisterHandler() { |
Anton Korobeynikov | b27f11e | 2011-10-21 09:38:50 +0000 | [diff] [blame] | 304 | #if __MINGW32__ && !defined(__MINGW64_VERSION_MAJOR) |
| 305 | // On MinGW.org, we need to load up the symbols explicitly, because the |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 306 | // Win32 framework they include does not have support for the 64-bit |
| 307 | // versions of the APIs we need. If we cannot load up the APIs (which |
| 308 | // would be unexpected as they should exist on every version of Windows |
| 309 | // we support), we will bail out since there would be nothing to report. |
| 310 | if (!load64BitDebugHelp()) { |
| 311 | assert(false && "These APIs should always be available"); |
| 312 | return; |
| 313 | } |
| 314 | #endif |
| 315 | |
Reid Spencer | 1bdd0f0 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 316 | if (RegisteredUnhandledExceptionFilter) { |
Reid Spencer | 90debc5 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 317 | EnterCriticalSection(&CriticalSection); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 318 | return; |
Reid Spencer | 90debc5 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 319 | } |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 320 | |
Aaron Ballman | 50af8d4 | 2015-03-26 16:24:38 +0000 | [diff] [blame] | 321 | InitializeThreading(); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 322 | |
| 323 | // Enter it immediately. Now if someone hits CTRL/C, the console handler |
| 324 | // can't proceed until the globals are updated. |
| 325 | EnterCriticalSection(&CriticalSection); |
| 326 | |
| 327 | RegisteredUnhandledExceptionFilter = true; |
Reid Spencer | 1bdd0f0 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 328 | OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 329 | SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE); |
| 330 | |
| 331 | // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or |
| 332 | // else multi-threading problems will ensue. |
| 333 | } |
| 334 | |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 335 | // RemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 336 | bool sys::RemoveFileOnSignal(StringRef Filename, std::string* ErrMsg) { |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 337 | RegisterHandler(); |
| 338 | |
Reid Spencer | 50eac3b | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 339 | if (CleanupExecuted) { |
| 340 | if (ErrMsg) |
| 341 | *ErrMsg = "Process terminating -- cannot register for removal"; |
| 342 | return true; |
| 343 | } |
Reid Spencer | 1bdd0f0 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 344 | |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 345 | if (FilesToRemove == NULL) |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 346 | FilesToRemove = new std::vector<std::string>; |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 347 | |
Reid Spencer | f070b6c | 2004-11-16 06:59:53 +0000 | [diff] [blame] | 348 | FilesToRemove->push_back(Filename); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 349 | |
| 350 | LeaveCriticalSection(&CriticalSection); |
Reid Spencer | 50eac3b | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 351 | return false; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 354 | // DontRemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 355 | void sys::DontRemoveFileOnSignal(StringRef Filename) { |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 356 | if (FilesToRemove == NULL) |
| 357 | return; |
| 358 | |
NAKAMURA Takumi | 3f688b9 | 2010-10-22 01:23:50 +0000 | [diff] [blame] | 359 | RegisterHandler(); |
| 360 | |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 361 | std::vector<std::string>::reverse_iterator I = |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 362 | std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename); |
| 363 | if (I != FilesToRemove->rend()) |
| 364 | FilesToRemove->erase(I.base()-1); |
| 365 | |
| 366 | LeaveCriticalSection(&CriticalSection); |
| 367 | } |
| 368 | |
Michael J. Spencer | 89b0ad2 | 2015-01-29 17:20:29 +0000 | [diff] [blame] | 369 | void sys::DisableSystemDialogsOnCrash() { |
| 370 | // Crash to stack trace handler on abort. |
| 371 | signal(SIGABRT, HandleAbort); |
| 372 | |
| 373 | // The following functions are not reliably accessible on MinGW. |
| 374 | #ifdef _MSC_VER |
| 375 | // We're already handling writing a "something went wrong" message. |
| 376 | _set_abort_behavior(0, _WRITE_ABORT_MSG); |
| 377 | // Disable Dr. Watson. |
| 378 | _set_abort_behavior(0, _CALL_REPORTFAULT); |
| 379 | _CrtSetReportHook(AvoidMessageBoxHook); |
| 380 | #endif |
| 381 | |
| 382 | // Disable standard error dialog box. |
| 383 | SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | |
| 384 | SEM_NOOPENFILEERRORBOX); |
| 385 | _set_error_mode(_OUT_TO_STDERR); |
| 386 | } |
| 387 | |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 388 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or |
| 389 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
Pete Cooper | 6bea2f4 | 2015-04-07 20:43:23 +0000 | [diff] [blame] | 390 | void sys::PrintStackTraceOnErrorSignal(bool DisableCrashReporting) { |
Michael J. Spencer | 89b0ad2 | 2015-01-29 17:20:29 +0000 | [diff] [blame] | 391 | DisableSystemDialogsOnCrash(); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 392 | RegisterHandler(); |
| 393 | LeaveCriticalSection(&CriticalSection); |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 394 | } |
Benjamin Kramer | f97eff6 | 2015-03-11 15:41:15 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Yaron Keren | bdae8d6 | 2015-03-14 19:20:56 +0000 | [diff] [blame] | 397 | #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) |
| 398 | // Provide a prototype for RtlCaptureContext, mingw32 from mingw.org is |
| 399 | // missing it but mingw-w64 has it. |
Benjamin Kramer | f97eff6 | 2015-03-11 15:41:15 +0000 | [diff] [blame] | 400 | extern "C" VOID WINAPI RtlCaptureContext(PCONTEXT ContextRecord); |
Benjamin Kramer | b47d549 | 2015-03-11 16:09:02 +0000 | [diff] [blame] | 401 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 402 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 403 | void llvm::sys::PrintStackTrace(raw_ostream &OS) { |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 404 | |
Reid Kleckner | e658058 | 2015-03-05 18:26:58 +0000 | [diff] [blame] | 405 | STACKFRAME64 StackFrame = {}; |
Yaron Keren | 24a86df | 2015-04-24 15:39:47 +0000 | [diff] [blame] | 406 | CONTEXT Context = {}; |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 407 | ::RtlCaptureContext(&Context); |
| 408 | #if defined(_M_X64) |
| 409 | StackFrame.AddrPC.Offset = Context.Rip; |
| 410 | StackFrame.AddrStack.Offset = Context.Rsp; |
| 411 | StackFrame.AddrFrame.Offset = Context.Rbp; |
| 412 | #else |
| 413 | StackFrame.AddrPC.Offset = Context.Eip; |
| 414 | StackFrame.AddrStack.Offset = Context.Esp; |
| 415 | StackFrame.AddrFrame.Offset = Context.Ebp; |
| 416 | #endif |
| 417 | StackFrame.AddrPC.Mode = AddrModeFlat; |
| 418 | StackFrame.AddrStack.Mode = AddrModeFlat; |
| 419 | StackFrame.AddrFrame.Mode = AddrModeFlat; |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 420 | PrintStackTraceForThread(OS, GetCurrentProcess(), GetCurrentThread(), |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 421 | StackFrame, &Context); |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 424 | |
Benjamin Kramer | 90c2db2 | 2015-03-11 15:53:24 +0000 | [diff] [blame] | 425 | void llvm::sys::SetInterruptFunction(void (*IF)()) { |
Jeff Cohen | ba7cc68 | 2005-08-02 03:04:47 +0000 | [diff] [blame] | 426 | RegisterHandler(); |
Jeff Cohen | 9aafa06 | 2005-08-02 03:26:32 +0000 | [diff] [blame] | 427 | InterruptFunction = IF; |
Jeff Cohen | ba7cc68 | 2005-08-02 03:04:47 +0000 | [diff] [blame] | 428 | LeaveCriticalSection(&CriticalSection); |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 429 | } |
Sebastian Redl | 8d5baa0 | 2009-03-19 23:26:52 +0000 | [diff] [blame] | 430 | |
| 431 | |
| 432 | /// AddSignalHandler - Add a function to be called when a signal is delivered |
| 433 | /// to the process. The handler can have a cookie passed to it to identify |
| 434 | /// what instance of the handler it is. |
Benjamin Kramer | b47d549 | 2015-03-11 16:09:02 +0000 | [diff] [blame] | 435 | void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { |
Sebastian Redl | 8d5baa0 | 2009-03-19 23:26:52 +0000 | [diff] [blame] | 436 | if (CallBacksToRun == 0) |
| 437 | CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >(); |
| 438 | CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie)); |
| 439 | RegisterHandler(); |
Torok Edwin | 6fb0956 | 2010-03-31 12:07:16 +0000 | [diff] [blame] | 440 | LeaveCriticalSection(&CriticalSection); |
Sebastian Redl | 8d5baa0 | 2009-03-19 23:26:52 +0000 | [diff] [blame] | 441 | } |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 442 | |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 443 | static void Cleanup() { |
Yaron Keren | 356aa46 | 2015-05-19 13:31:25 +0000 | [diff] [blame^] | 444 | if (CleanupExecuted) |
| 445 | return; |
| 446 | |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 447 | EnterCriticalSection(&CriticalSection); |
| 448 | |
Reid Spencer | 1bdd0f0 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 449 | // Prevent other thread from registering new files and directories for |
| 450 | // removal, should we be executing because of the console handler callback. |
| 451 | CleanupExecuted = true; |
| 452 | |
| 453 | // FIXME: open files cannot be deleted. |
| 454 | |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 455 | if (FilesToRemove != NULL) |
| 456 | while (!FilesToRemove->empty()) { |
Rafael Espindola | d724c28 | 2014-02-23 13:37:37 +0000 | [diff] [blame] | 457 | llvm::sys::fs::remove(FilesToRemove->back()); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 458 | FilesToRemove->pop_back(); |
| 459 | } |
| 460 | |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 461 | if (CallBacksToRun) |
| 462 | for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i) |
| 463 | (*CallBacksToRun)[i].first((*CallBacksToRun)[i].second); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 464 | |
| 465 | LeaveCriticalSection(&CriticalSection); |
| 466 | } |
| 467 | |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 468 | void llvm::sys::RunInterruptHandlers() { |
Aaron Ballman | 50af8d4 | 2015-03-26 16:24:38 +0000 | [diff] [blame] | 469 | // The interrupt handler may be called from an interrupt, but it may also be |
| 470 | // called manually (such as the case of report_fatal_error with no registered |
| 471 | // error handler). We must ensure that the critical section is properly |
| 472 | // initialized. |
| 473 | InitializeThreading(); |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 474 | Cleanup(); |
| 475 | } |
| 476 | |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 477 | static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) { |
Mikhail Glushenkov | 32acd74 | 2010-10-27 09:09:04 +0000 | [diff] [blame] | 478 | Cleanup(); |
Mikhail Glushenkov | f64d93d | 2010-10-21 20:40:39 +0000 | [diff] [blame] | 479 | |
Mikhail Glushenkov | 080d86f | 2010-10-28 08:25:44 +0000 | [diff] [blame] | 480 | // Initialize the STACKFRAME structure. |
Yaron Keren | 24a86df | 2015-04-24 15:39:47 +0000 | [diff] [blame] | 481 | STACKFRAME64 StackFrame = {}; |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 482 | |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 483 | #if defined(_M_X64) |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 484 | StackFrame.AddrPC.Offset = ep->ContextRecord->Rip; |
| 485 | StackFrame.AddrPC.Mode = AddrModeFlat; |
| 486 | StackFrame.AddrStack.Offset = ep->ContextRecord->Rsp; |
| 487 | StackFrame.AddrStack.Mode = AddrModeFlat; |
| 488 | StackFrame.AddrFrame.Offset = ep->ContextRecord->Rbp; |
| 489 | StackFrame.AddrFrame.Mode = AddrModeFlat; |
| 490 | #elif defined(_M_IX86) |
Mikhail Glushenkov | 080d86f | 2010-10-28 08:25:44 +0000 | [diff] [blame] | 491 | StackFrame.AddrPC.Offset = ep->ContextRecord->Eip; |
| 492 | StackFrame.AddrPC.Mode = AddrModeFlat; |
| 493 | StackFrame.AddrStack.Offset = ep->ContextRecord->Esp; |
| 494 | StackFrame.AddrStack.Mode = AddrModeFlat; |
| 495 | StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp; |
| 496 | StackFrame.AddrFrame.Mode = AddrModeFlat; |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 497 | #endif |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 498 | |
Mikhail Glushenkov | 080d86f | 2010-10-28 08:25:44 +0000 | [diff] [blame] | 499 | HANDLE hProcess = GetCurrentProcess(); |
| 500 | HANDLE hThread = GetCurrentThread(); |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 501 | PrintStackTraceForThread(llvm::errs(), hProcess, hThread, StackFrame, |
Zachary Turner | 62b7b61 | 2015-03-05 17:47:52 +0000 | [diff] [blame] | 502 | ep->ContextRecord); |
Mikhail Glushenkov | 080d86f | 2010-10-28 08:25:44 +0000 | [diff] [blame] | 503 | |
Michael J. Spencer | 89b0ad2 | 2015-01-29 17:20:29 +0000 | [diff] [blame] | 504 | _exit(ep->ExceptionRecord->ExceptionCode); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) { |
Jeff Cohen | 9aafa06 | 2005-08-02 03:26:32 +0000 | [diff] [blame] | 508 | // We are running in our very own thread, courtesy of Windows. |
Jeff Cohen | ba7cc68 | 2005-08-02 03:04:47 +0000 | [diff] [blame] | 509 | EnterCriticalSection(&CriticalSection); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 510 | Cleanup(); |
| 511 | |
Jeff Cohen | ba7cc68 | 2005-08-02 03:04:47 +0000 | [diff] [blame] | 512 | // If an interrupt function has been set, go and run one it; otherwise, |
| 513 | // the process dies. |
| 514 | void (*IF)() = InterruptFunction; |
| 515 | InterruptFunction = 0; // Don't run it on another CTRL-C. |
| 516 | |
| 517 | if (IF) { |
Jeff Cohen | 9aafa06 | 2005-08-02 03:26:32 +0000 | [diff] [blame] | 518 | // Note: if the interrupt function throws an exception, there is nothing |
| 519 | // to catch it in this thread so it will kill the process. |
| 520 | IF(); // Run it now. |
Jeff Cohen | ba7cc68 | 2005-08-02 03:04:47 +0000 | [diff] [blame] | 521 | LeaveCriticalSection(&CriticalSection); |
| 522 | return TRUE; // Don't kill the process. |
| 523 | } |
| 524 | |
Reid Spencer | 90debc5 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 525 | // Allow normal processing to take place; i.e., the process dies. |
Jeff Cohen | ba7cc68 | 2005-08-02 03:04:47 +0000 | [diff] [blame] | 526 | LeaveCriticalSection(&CriticalSection); |
Reid Spencer | 4aff78a | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 527 | return FALSE; |
| 528 | } |
Michael J. Spencer | 44a36c8 | 2011-10-01 00:05:20 +0000 | [diff] [blame] | 529 | |
| 530 | #if __MINGW32__ |
| 531 | // We turned these warnings off for this file so that MinGW-g++ doesn't |
| 532 | // complain about the ll format specifiers used. Now we are turning the |
| 533 | // warnings back on. If MinGW starts to support diagnostic stacks, we can |
| 534 | // replace this with a pop. |
| 535 | #pragma GCC diagnostic warning "-Wformat" |
| 536 | #pragma GCC diagnostic warning "-Wformat-extra-args" |
| 537 | #endif |