blob: cf5cb40d4d0592eaa7fd82eee533dcf3192ac461 [file] [log] [blame]
Reid Spencer496c2772004-08-29 19:22:48 +00001//===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer90b54132004-09-16 15:53:16 +00005// This file was developed by Jeff Cohen and is distributed under the
Reid Spencer496c2772004-08-29 19:22:48 +00006// 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 Spencer90b54132004-09-16 15:53:16 +000014#include "Win32.h"
Reid Spencerced41102004-09-28 23:58:03 +000015#include <stdio.h>
Reid Spencer90b54132004-09-16 15:53:16 +000016#include <vector>
17
Jeff Cohen23a1cf32005-02-19 03:01:13 +000018#ifdef __MINGW32__
Reid Spencerf6cbc0f2004-09-23 14:47:10 +000019#include <imagehlp.h>
20#else
21#include <dbghelp.h>
22#endif
23#include <psapi.h>
Reid Spencer90b54132004-09-16 15:53:16 +000024
25#pragma comment(lib, "psapi.lib")
26#pragma comment(lib, "dbghelp.lib")
27
28// Forward declare.
29static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
30static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
31
Jeff Cohenee841a12005-08-02 03:04:47 +000032// InterruptFunction - The function to call if ctrl-c is pressed.
33static void (*InterruptFunction)() = 0;
34
Reid Spencerced41102004-09-28 23:58:03 +000035static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
Reid Spencer90b54132004-09-16 15:53:16 +000036static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
37static bool RegisteredUnhandledExceptionFilter = false;
Reid Spencer7b60a152004-09-19 05:37:39 +000038static bool CleanupExecuted = false;
39static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
Reid Spencer298d6c12004-09-17 03:02:27 +000040
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 Spencer90b54132004-09-16 15:53:16 +000044static CRITICAL_SECTION CriticalSection;
45
Reid Spencer496c2772004-08-29 19:22:48 +000046namespace llvm {
Reid Spencer496c2772004-08-29 19:22:48 +000047
48//===----------------------------------------------------------------------===//
49//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer90b54132004-09-16 15:53:16 +000050//=== and must not be UNIX code
Reid Spencer496c2772004-08-29 19:22:48 +000051//===----------------------------------------------------------------------===//
52
Reid Spencer90b54132004-09-16 15:53:16 +000053
54static void RegisterHandler() {
Reid Spencer7b60a152004-09-19 05:37:39 +000055 if (RegisteredUnhandledExceptionFilter) {
Reid Spencer298d6c12004-09-17 03:02:27 +000056 EnterCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +000057 return;
Reid Spencer298d6c12004-09-17 03:02:27 +000058 }
Reid Spencer90b54132004-09-16 15:53:16 +000059
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 Spencer7b60a152004-09-19 05:37:39 +000069 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
Reid Spencer90b54132004-09-16 15:53:16 +000070 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
71
72 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
73 // else multi-threading problems will ensue.
74}
75
Reid Spencer496c2772004-08-29 19:22:48 +000076// RemoveFileOnSignal - The public API
Reid Spencerabc86502004-11-15 17:20:28 +000077void sys::RemoveFileOnSignal(const sys::Path &Filename) {
Reid Spencer90b54132004-09-16 15:53:16 +000078 RegisterHandler();
79
Reid Spencer7b60a152004-09-19 05:37:39 +000080 if (CleanupExecuted)
81 throw std::string("Process terminating -- cannot register for removal");
82
Reid Spencer90b54132004-09-16 15:53:16 +000083 if (FilesToRemove == NULL)
Reid Spencerced41102004-09-28 23:58:03 +000084 FilesToRemove = new std::vector<sys::Path>;
Reid Spencer90b54132004-09-16 15:53:16 +000085
Reid Spencer98601212004-11-16 06:59:53 +000086 FilesToRemove->push_back(Filename);
Reid Spencer90b54132004-09-16 15:53:16 +000087
88 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +000089}
90
91// RemoveDirectoryOnSignal - The public API
Reid Spencer90b54132004-09-16 15:53:16 +000092void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
93 RegisterHandler();
94
Reid Spencer7b60a152004-09-19 05:37:39 +000095 if (CleanupExecuted)
96 throw std::string("Process terminating -- cannot register for removal");
97
Reid Spencer07adb282004-11-05 22:15:36 +000098 if (path.isDirectory()) {
Reid Spencer90b54132004-09-16 15:53:16 +000099 if (DirectoriesToRemove == NULL)
100 DirectoriesToRemove = new std::vector<sys::Path>;
101
102 DirectoriesToRemove->push_back(path);
103 }
104
105 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +0000106}
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 Spencer90b54132004-09-16 15:53:16 +0000110void sys::PrintStackTraceOnErrorSignal() {
111 RegisterHandler();
112 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +0000113}
114
Chris Lattnerfa8c2922005-08-02 02:14:22 +0000115
116void sys::SetInterruptFunction(void (*IF)()) {
Jeff Cohenee841a12005-08-02 03:04:47 +0000117 InterruptFunction = IF;
118 RegisterHandler();
119 LeaveCriticalSection(&CriticalSection);
Chris Lattnerfa8c2922005-08-02 02:14:22 +0000120}
Reid Spencer496c2772004-08-29 19:22:48 +0000121}
122
Reid Spencer90b54132004-09-16 15:53:16 +0000123static void Cleanup() {
124 EnterCriticalSection(&CriticalSection);
125
Reid Spencer7b60a152004-09-19 05:37:39 +0000126 // 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 Spencer90b54132004-09-16 15:53:16 +0000132 if (FilesToRemove != NULL)
133 while (!FilesToRemove->empty()) {
134 try {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000135 FilesToRemove->back().eraseFromDisk();
Reid Spencer90b54132004-09-16 15:53:16 +0000136 } catch (...) {
137 }
138 FilesToRemove->pop_back();
139 }
140
141 if (DirectoriesToRemove != NULL)
142 while (!DirectoriesToRemove->empty()) {
143 try {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000144 DirectoriesToRemove->back().eraseFromDisk(true);
Reid Spencer90b54132004-09-16 15:53:16 +0000145 } catch (...) {
146 }
147 DirectoriesToRemove->pop_back();
148 }
149
150 LeaveCriticalSection(&CriticalSection);
151}
152
153static 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 Spencer7b60a152004-09-19 05:37:39 +0000173 SymInitialize(hProcess, NULL, TRUE);
Reid Spencer90b54132004-09-16 15:53:16 +0000174
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 Spencer7b60a152004-09-19 05:37:39 +0000187 fprintf(stderr, "%08X", PC);
Reid Spencer90b54132004-09-16 15:53:16 +0000188
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 Spencer7b60a152004-09-19 05:37:39 +0000195 fputs(" <unknown module>\n", stderr);
Reid Spencer90b54132004-09-16 15:53:16 +0000196 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 Spencer7b60a152004-09-19 05:37:39 +0000230 } catch (...) {
Reid Spencer90b54132004-09-16 15:53:16 +0000231 assert(!"Crashed in LLVMUnhandledExceptionFilter");
232 }
233
234 // Allow dialog box to pop up allowing choice to start debugger.
Reid Spencer7b60a152004-09-19 05:37:39 +0000235 if (OldFilter)
236 return (*OldFilter)(ep);
237 else
238 return EXCEPTION_CONTINUE_SEARCH;
Reid Spencer90b54132004-09-16 15:53:16 +0000239}
240
241static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
Jeff Cohenee841a12005-08-02 03:04:47 +0000242 EnterCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +0000243 Cleanup();
244
Jeff Cohenee841a12005-08-02 03:04:47 +0000245 // 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 Spencer298d6c12004-09-17 03:02:27 +0000262 // Allow normal processing to take place; i.e., the process dies.
Jeff Cohenee841a12005-08-02 03:04:47 +0000263 LeaveCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +0000264 return FALSE;
265}
266