blob: a2c7ae2c307a4bc8b4ff7dd5341343fad6109346 [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
Reid Spencerced41102004-09-28 23:58:03 +000032static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
Reid Spencer90b54132004-09-16 15:53:16 +000033static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
34static bool RegisteredUnhandledExceptionFilter = false;
Reid Spencer7b60a152004-09-19 05:37:39 +000035static bool CleanupExecuted = false;
36static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
Reid Spencer298d6c12004-09-17 03:02:27 +000037
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 Spencer90b54132004-09-16 15:53:16 +000041static CRITICAL_SECTION CriticalSection;
42
Reid Spencer496c2772004-08-29 19:22:48 +000043namespace llvm {
Reid Spencer496c2772004-08-29 19:22:48 +000044
45//===----------------------------------------------------------------------===//
46//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer90b54132004-09-16 15:53:16 +000047//=== and must not be UNIX code
Reid Spencer496c2772004-08-29 19:22:48 +000048//===----------------------------------------------------------------------===//
49
Reid Spencer90b54132004-09-16 15:53:16 +000050
51static void RegisterHandler() {
Reid Spencer7b60a152004-09-19 05:37:39 +000052 if (RegisteredUnhandledExceptionFilter) {
Reid Spencer298d6c12004-09-17 03:02:27 +000053 EnterCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +000054 return;
Reid Spencer298d6c12004-09-17 03:02:27 +000055 }
Reid Spencer90b54132004-09-16 15:53:16 +000056
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 Spencer7b60a152004-09-19 05:37:39 +000066 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
Reid Spencer90b54132004-09-16 15:53:16 +000067 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
68
69 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
70 // else multi-threading problems will ensue.
71}
72
Reid Spencer496c2772004-08-29 19:22:48 +000073// RemoveFileOnSignal - The public API
Reid Spencerabc86502004-11-15 17:20:28 +000074void sys::RemoveFileOnSignal(const sys::Path &Filename) {
Reid Spencer90b54132004-09-16 15:53:16 +000075 RegisterHandler();
76
Reid Spencer7b60a152004-09-19 05:37:39 +000077 if (CleanupExecuted)
78 throw std::string("Process terminating -- cannot register for removal");
79
Reid Spencer90b54132004-09-16 15:53:16 +000080 if (FilesToRemove == NULL)
Reid Spencerced41102004-09-28 23:58:03 +000081 FilesToRemove = new std::vector<sys::Path>;
Reid Spencer90b54132004-09-16 15:53:16 +000082
Reid Spencer98601212004-11-16 06:59:53 +000083 FilesToRemove->push_back(Filename);
Reid Spencer90b54132004-09-16 15:53:16 +000084
85 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +000086}
87
88// RemoveDirectoryOnSignal - The public API
Reid Spencer90b54132004-09-16 15:53:16 +000089void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
90 RegisterHandler();
91
Reid Spencer7b60a152004-09-19 05:37:39 +000092 if (CleanupExecuted)
93 throw std::string("Process terminating -- cannot register for removal");
94
Reid Spencer07adb282004-11-05 22:15:36 +000095 if (path.isDirectory()) {
Reid Spencer90b54132004-09-16 15:53:16 +000096 if (DirectoriesToRemove == NULL)
97 DirectoriesToRemove = new std::vector<sys::Path>;
98
99 DirectoriesToRemove->push_back(path);
100 }
101
102 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +0000103}
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 Spencer90b54132004-09-16 15:53:16 +0000107void sys::PrintStackTraceOnErrorSignal() {
108 RegisterHandler();
109 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +0000110}
111
Chris Lattnerfa8c2922005-08-02 02:14:22 +0000112
113void sys::SetInterruptFunction(void (*IF)()) {
114 // Currently unimplemented.
115}
Reid Spencer496c2772004-08-29 19:22:48 +0000116}
117
Reid Spencer90b54132004-09-16 15:53:16 +0000118static void Cleanup() {
119 EnterCriticalSection(&CriticalSection);
120
Reid Spencer7b60a152004-09-19 05:37:39 +0000121 // Prevent other thread from registering new files and directories for
122 // removal, should we be executing because of the console handler callback.
123 CleanupExecuted = true;
124
125 // FIXME: open files cannot be deleted.
126
Reid Spencer90b54132004-09-16 15:53:16 +0000127 if (FilesToRemove != NULL)
128 while (!FilesToRemove->empty()) {
129 try {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000130 FilesToRemove->back().eraseFromDisk();
Reid Spencer90b54132004-09-16 15:53:16 +0000131 } catch (...) {
132 }
133 FilesToRemove->pop_back();
134 }
135
136 if (DirectoriesToRemove != NULL)
137 while (!DirectoriesToRemove->empty()) {
138 try {
Jeff Cohen8f0e8f22005-07-08 04:50:08 +0000139 DirectoriesToRemove->back().eraseFromDisk(true);
Reid Spencer90b54132004-09-16 15:53:16 +0000140 } catch (...) {
141 }
142 DirectoriesToRemove->pop_back();
143 }
144
145 LeaveCriticalSection(&CriticalSection);
146}
147
148static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
149 try {
150 Cleanup();
151
152 // Initialize the STACKFRAME structure.
153 STACKFRAME StackFrame;
154 memset(&StackFrame, 0, sizeof(StackFrame));
155
156 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
157 StackFrame.AddrPC.Mode = AddrModeFlat;
158 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
159 StackFrame.AddrStack.Mode = AddrModeFlat;
160 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
161 StackFrame.AddrFrame.Mode = AddrModeFlat;
162
163 HANDLE hProcess = GetCurrentProcess();
164 HANDLE hThread = GetCurrentThread();
165
166 // Initialize the symbol handler.
167 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
Reid Spencer7b60a152004-09-19 05:37:39 +0000168 SymInitialize(hProcess, NULL, TRUE);
Reid Spencer90b54132004-09-16 15:53:16 +0000169
170 while (true) {
171 if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
172 ep->ContextRecord, NULL, SymFunctionTableAccess,
173 SymGetModuleBase, NULL)) {
174 break;
175 }
176
177 if (StackFrame.AddrFrame.Offset == 0)
178 break;
179
180 // Print the PC in hexadecimal.
181 DWORD PC = StackFrame.AddrPC.Offset;
Reid Spencer7b60a152004-09-19 05:37:39 +0000182 fprintf(stderr, "%08X", PC);
Reid Spencer90b54132004-09-16 15:53:16 +0000183
184 // Print the parameters. Assume there are four.
185 fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0],
186 StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);
187
188 // Verify the PC belongs to a module in this process.
189 if (!SymGetModuleBase(hProcess, PC)) {
Reid Spencer7b60a152004-09-19 05:37:39 +0000190 fputs(" <unknown module>\n", stderr);
Reid Spencer90b54132004-09-16 15:53:16 +0000191 continue;
192 }
193
194 // Print the symbol name.
195 char buffer[512];
196 IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
197 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
198 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
199 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
200
201 DWORD dwDisp;
202 if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
203 fputc('\n', stderr);
204 continue;
205 }
206
207 buffer[511] = 0;
208 if (dwDisp > 0)
209 fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp);
210 else
211 fprintf(stderr, ", %s", symbol->Name);
212
213 // Print the source file and line number information.
214 IMAGEHLP_LINE line;
215 memset(&line, 0, sizeof(line));
216 line.SizeOfStruct = sizeof(line);
217 if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) {
218 fprintf(stderr, ", %s, line %d", line.FileName, line.LineNumber);
219 if (dwDisp > 0)
220 fprintf(stderr, "+%04d byte(s)", dwDisp);
221 }
222
223 fputc('\n', stderr);
224 }
Reid Spencer7b60a152004-09-19 05:37:39 +0000225 } catch (...) {
Reid Spencer90b54132004-09-16 15:53:16 +0000226 assert(!"Crashed in LLVMUnhandledExceptionFilter");
227 }
228
229 // Allow dialog box to pop up allowing choice to start debugger.
Reid Spencer7b60a152004-09-19 05:37:39 +0000230 if (OldFilter)
231 return (*OldFilter)(ep);
232 else
233 return EXCEPTION_CONTINUE_SEARCH;
Reid Spencer90b54132004-09-16 15:53:16 +0000234}
235
236static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
237 Cleanup();
238
Reid Spencer298d6c12004-09-17 03:02:27 +0000239 // Allow normal processing to take place; i.e., the process dies.
Reid Spencer90b54132004-09-16 15:53:16 +0000240 return FALSE;
241}
242