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