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