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