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