Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 1 | //===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 5 | // This file was developed by Jeff Cohen and is distributed under the |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Signals class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 14 | #include "Win32.h" |
Reid Spencer | ced4110 | 2004-09-28 23:58:03 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Jeff Cohen | 23a1cf3 | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 18 | #ifdef __MINGW32__ |
Reid Spencer | f6cbc0f | 2004-09-23 14:47:10 +0000 | [diff] [blame] | 19 | #include <imagehlp.h> |
| 20 | #else |
| 21 | #include <dbghelp.h> |
| 22 | #endif |
| 23 | #include <psapi.h> |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 24 | |
| 25 | #pragma comment(lib, "psapi.lib") |
| 26 | #pragma comment(lib, "dbghelp.lib") |
| 27 | |
| 28 | // Forward declare. |
| 29 | static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep); |
| 30 | static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType); |
| 31 | |
Jeff Cohen | ee841a1 | 2005-08-02 03:04:47 +0000 | [diff] [blame^] | 32 | // InterruptFunction - The function to call if ctrl-c is pressed. |
| 33 | static void (*InterruptFunction)() = 0; |
| 34 | |
Reid Spencer | ced4110 | 2004-09-28 23:58:03 +0000 | [diff] [blame] | 35 | static std::vector<llvm::sys::Path> *FilesToRemove = NULL; |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 36 | static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL; |
| 37 | static bool RegisteredUnhandledExceptionFilter = false; |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 38 | static bool CleanupExecuted = false; |
| 39 | static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL; |
Reid Spencer | 298d6c1 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 40 | |
| 41 | // Windows creates a new thread to execute the console handler when an event |
| 42 | // (such as CTRL/C) occurs. This causes concurrency issues with the above |
| 43 | // globals which this critical section addresses. |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 44 | static CRITICAL_SECTION CriticalSection; |
| 45 | |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 46 | namespace llvm { |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 47 | |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | //=== WARNING: Implementation here must contain only Win32 specific code |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 50 | //=== and must not be UNIX code |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 53 | |
| 54 | static void RegisterHandler() { |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 55 | if (RegisteredUnhandledExceptionFilter) { |
Reid Spencer | 298d6c1 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 56 | EnterCriticalSection(&CriticalSection); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 57 | return; |
Reid Spencer | 298d6c1 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 58 | } |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 59 | |
| 60 | // Now's the time to create the critical section. This is the first time |
| 61 | // through here, and there's only one thread. |
| 62 | InitializeCriticalSection(&CriticalSection); |
| 63 | |
| 64 | // Enter it immediately. Now if someone hits CTRL/C, the console handler |
| 65 | // can't proceed until the globals are updated. |
| 66 | EnterCriticalSection(&CriticalSection); |
| 67 | |
| 68 | RegisteredUnhandledExceptionFilter = true; |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 69 | OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 70 | SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE); |
| 71 | |
| 72 | // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or |
| 73 | // else multi-threading problems will ensue. |
| 74 | } |
| 75 | |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 76 | // RemoveFileOnSignal - The public API |
Reid Spencer | abc8650 | 2004-11-15 17:20:28 +0000 | [diff] [blame] | 77 | void sys::RemoveFileOnSignal(const sys::Path &Filename) { |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 78 | RegisterHandler(); |
| 79 | |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 80 | if (CleanupExecuted) |
| 81 | throw std::string("Process terminating -- cannot register for removal"); |
| 82 | |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 83 | if (FilesToRemove == NULL) |
Reid Spencer | ced4110 | 2004-09-28 23:58:03 +0000 | [diff] [blame] | 84 | FilesToRemove = new std::vector<sys::Path>; |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 85 | |
Reid Spencer | 9860121 | 2004-11-16 06:59:53 +0000 | [diff] [blame] | 86 | FilesToRemove->push_back(Filename); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 87 | |
| 88 | LeaveCriticalSection(&CriticalSection); |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // RemoveDirectoryOnSignal - The public API |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 92 | void sys::RemoveDirectoryOnSignal(const sys::Path& path) { |
| 93 | RegisterHandler(); |
| 94 | |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 95 | if (CleanupExecuted) |
| 96 | throw std::string("Process terminating -- cannot register for removal"); |
| 97 | |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 98 | if (path.isDirectory()) { |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 99 | if (DirectoriesToRemove == NULL) |
| 100 | DirectoriesToRemove = new std::vector<sys::Path>; |
| 101 | |
| 102 | DirectoriesToRemove->push_back(path); |
| 103 | } |
| 104 | |
| 105 | LeaveCriticalSection(&CriticalSection); |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or |
| 109 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 110 | void sys::PrintStackTraceOnErrorSignal() { |
| 111 | RegisterHandler(); |
| 112 | LeaveCriticalSection(&CriticalSection); |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | fa8c292 | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 115 | |
| 116 | void sys::SetInterruptFunction(void (*IF)()) { |
Jeff Cohen | ee841a1 | 2005-08-02 03:04:47 +0000 | [diff] [blame^] | 117 | InterruptFunction = IF; |
| 118 | RegisterHandler(); |
| 119 | LeaveCriticalSection(&CriticalSection); |
Chris Lattner | fa8c292 | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 120 | } |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 123 | static void Cleanup() { |
| 124 | EnterCriticalSection(&CriticalSection); |
| 125 | |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 126 | // Prevent other thread from registering new files and directories for |
| 127 | // removal, should we be executing because of the console handler callback. |
| 128 | CleanupExecuted = true; |
| 129 | |
| 130 | // FIXME: open files cannot be deleted. |
| 131 | |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 132 | if (FilesToRemove != NULL) |
| 133 | while (!FilesToRemove->empty()) { |
| 134 | try { |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 135 | FilesToRemove->back().eraseFromDisk(); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 136 | } catch (...) { |
| 137 | } |
| 138 | FilesToRemove->pop_back(); |
| 139 | } |
| 140 | |
| 141 | if (DirectoriesToRemove != NULL) |
| 142 | while (!DirectoriesToRemove->empty()) { |
| 143 | try { |
Jeff Cohen | 8f0e8f2 | 2005-07-08 04:50:08 +0000 | [diff] [blame] | 144 | DirectoriesToRemove->back().eraseFromDisk(true); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 145 | } catch (...) { |
| 146 | } |
| 147 | DirectoriesToRemove->pop_back(); |
| 148 | } |
| 149 | |
| 150 | LeaveCriticalSection(&CriticalSection); |
| 151 | } |
| 152 | |
| 153 | static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) { |
| 154 | try { |
| 155 | Cleanup(); |
| 156 | |
| 157 | // Initialize the STACKFRAME structure. |
| 158 | STACKFRAME StackFrame; |
| 159 | memset(&StackFrame, 0, sizeof(StackFrame)); |
| 160 | |
| 161 | StackFrame.AddrPC.Offset = ep->ContextRecord->Eip; |
| 162 | StackFrame.AddrPC.Mode = AddrModeFlat; |
| 163 | StackFrame.AddrStack.Offset = ep->ContextRecord->Esp; |
| 164 | StackFrame.AddrStack.Mode = AddrModeFlat; |
| 165 | StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp; |
| 166 | StackFrame.AddrFrame.Mode = AddrModeFlat; |
| 167 | |
| 168 | HANDLE hProcess = GetCurrentProcess(); |
| 169 | HANDLE hThread = GetCurrentThread(); |
| 170 | |
| 171 | // Initialize the symbol handler. |
| 172 | SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES); |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 173 | SymInitialize(hProcess, NULL, TRUE); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 174 | |
| 175 | while (true) { |
| 176 | if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame, |
| 177 | ep->ContextRecord, NULL, SymFunctionTableAccess, |
| 178 | SymGetModuleBase, NULL)) { |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | if (StackFrame.AddrFrame.Offset == 0) |
| 183 | break; |
| 184 | |
| 185 | // Print the PC in hexadecimal. |
| 186 | DWORD PC = StackFrame.AddrPC.Offset; |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 187 | fprintf(stderr, "%08X", PC); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 188 | |
| 189 | // Print the parameters. Assume there are four. |
| 190 | fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0], |
| 191 | StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]); |
| 192 | |
| 193 | // Verify the PC belongs to a module in this process. |
| 194 | if (!SymGetModuleBase(hProcess, PC)) { |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 195 | fputs(" <unknown module>\n", stderr); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 196 | continue; |
| 197 | } |
| 198 | |
| 199 | // Print the symbol name. |
| 200 | char buffer[512]; |
| 201 | IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer); |
| 202 | memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL)); |
| 203 | symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL); |
| 204 | symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL); |
| 205 | |
| 206 | DWORD dwDisp; |
| 207 | if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) { |
| 208 | fputc('\n', stderr); |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | buffer[511] = 0; |
| 213 | if (dwDisp > 0) |
| 214 | fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp); |
| 215 | else |
| 216 | fprintf(stderr, ", %s", symbol->Name); |
| 217 | |
| 218 | // Print the source file and line number information. |
| 219 | IMAGEHLP_LINE line; |
| 220 | memset(&line, 0, sizeof(line)); |
| 221 | line.SizeOfStruct = sizeof(line); |
| 222 | if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) { |
| 223 | fprintf(stderr, ", %s, line %d", line.FileName, line.LineNumber); |
| 224 | if (dwDisp > 0) |
| 225 | fprintf(stderr, "+%04d byte(s)", dwDisp); |
| 226 | } |
| 227 | |
| 228 | fputc('\n', stderr); |
| 229 | } |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 230 | } catch (...) { |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 231 | assert(!"Crashed in LLVMUnhandledExceptionFilter"); |
| 232 | } |
| 233 | |
| 234 | // Allow dialog box to pop up allowing choice to start debugger. |
Reid Spencer | 7b60a15 | 2004-09-19 05:37:39 +0000 | [diff] [blame] | 235 | if (OldFilter) |
| 236 | return (*OldFilter)(ep); |
| 237 | else |
| 238 | return EXCEPTION_CONTINUE_SEARCH; |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) { |
Jeff Cohen | ee841a1 | 2005-08-02 03:04:47 +0000 | [diff] [blame^] | 242 | EnterCriticalSection(&CriticalSection); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 243 | Cleanup(); |
| 244 | |
Jeff Cohen | ee841a1 | 2005-08-02 03:04:47 +0000 | [diff] [blame^] | 245 | // If an interrupt function has been set, go and run one it; otherwise, |
| 246 | // the process dies. |
| 247 | void (*IF)() = InterruptFunction; |
| 248 | InterruptFunction = 0; // Don't run it on another CTRL-C. |
| 249 | |
| 250 | if (IF) { |
| 251 | try { |
| 252 | IF(); // Run it now. |
| 253 | } catch (...) { |
| 254 | // Kill the process on an exception. |
| 255 | LeaveCriticalSection(&CriticalSection); |
| 256 | return FALSE; |
| 257 | } |
| 258 | LeaveCriticalSection(&CriticalSection); |
| 259 | return TRUE; // Don't kill the process. |
| 260 | } |
| 261 | |
Reid Spencer | 298d6c1 | 2004-09-17 03:02:27 +0000 | [diff] [blame] | 262 | // Allow normal processing to take place; i.e., the process dies. |
Jeff Cohen | ee841a1 | 2005-08-02 03:04:47 +0000 | [diff] [blame^] | 263 | LeaveCriticalSection(&CriticalSection); |
Reid Spencer | 90b5413 | 2004-09-16 15:53:16 +0000 | [diff] [blame] | 264 | return FALSE; |
| 265 | } |
| 266 | |