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