1 //===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Jeff Cohen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Signals class.
12 //===----------------------------------------------------------------------===//
25 #pragma comment(lib, "psapi.lib")
26 #pragma comment(lib, "dbghelp.lib")
29 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
30 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
32 // InterruptFunction - The function to call if ctrl-c is pressed.
33 static void (*InterruptFunction)() = 0;
35 static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
36 static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
37 static bool RegisteredUnhandledExceptionFilter = false;
38 static bool CleanupExecuted = false;
39 static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
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.
44 static CRITICAL_SECTION CriticalSection;
48 //===----------------------------------------------------------------------===//
49 //=== WARNING: Implementation here must contain only Win32 specific code
50 //=== and must not be UNIX code
51 //===----------------------------------------------------------------------===//
54 static void RegisterHandler() {
55 if (RegisteredUnhandledExceptionFilter) {
56 EnterCriticalSection(&CriticalSection);
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);
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);
68 RegisteredUnhandledExceptionFilter = true;
69 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
70 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
72 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
73 // else multi-threading problems will ensue.
76 // RemoveFileOnSignal - The public API
77 void sys::RemoveFileOnSignal(const sys::Path &Filename) {
81 throw std::string("Process terminating -- cannot register for removal");
83 if (FilesToRemove == NULL)
84 FilesToRemove = new std::vector<sys::Path>;
86 FilesToRemove->push_back(Filename);
88 LeaveCriticalSection(&CriticalSection);
91 // RemoveDirectoryOnSignal - The public API
92 void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
96 throw std::string("Process terminating -- cannot register for removal");
98 if (path.isDirectory()) {
99 if (DirectoriesToRemove == NULL)
100 DirectoriesToRemove = new std::vector<sys::Path>;
102 DirectoriesToRemove->push_back(path);
105 LeaveCriticalSection(&CriticalSection);
108 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
109 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
110 void sys::PrintStackTraceOnErrorSignal() {
112 LeaveCriticalSection(&CriticalSection);
116 void sys::SetInterruptFunction(void (*IF)()) {
118 InterruptFunction = IF;
119 LeaveCriticalSection(&CriticalSection);
123 static void Cleanup() {
124 EnterCriticalSection(&CriticalSection);
126 // 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;
130 // FIXME: open files cannot be deleted.
132 if (FilesToRemove != NULL)
133 while (!FilesToRemove->empty()) {
135 FilesToRemove->back().eraseFromDisk();
138 FilesToRemove->pop_back();
141 if (DirectoriesToRemove != NULL)
142 while (!DirectoriesToRemove->empty()) {
144 DirectoriesToRemove->back().eraseFromDisk(true);
147 DirectoriesToRemove->pop_back();
150 LeaveCriticalSection(&CriticalSection);
153 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
157 // Initialize the STACKFRAME structure.
158 STACKFRAME StackFrame;
159 memset(&StackFrame, 0, sizeof(StackFrame));
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;
168 HANDLE hProcess = GetCurrentProcess();
169 HANDLE hThread = GetCurrentThread();
171 // Initialize the symbol handler.
172 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
173 SymInitialize(hProcess, NULL, TRUE);
176 if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
177 ep->ContextRecord, NULL, SymFunctionTableAccess,
178 SymGetModuleBase, NULL)) {
182 if (StackFrame.AddrFrame.Offset == 0)
185 // Print the PC in hexadecimal.
186 DWORD PC = StackFrame.AddrPC.Offset;
187 fprintf(stderr, "%08X", PC);
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]);
193 // Verify the PC belongs to a module in this process.
194 if (!SymGetModuleBase(hProcess, PC)) {
195 fputs(" <unknown module>\n", stderr);
199 // Print the symbol name.
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);
207 if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
214 fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp);
216 fprintf(stderr, ", %s", symbol->Name);
218 // Print the source file and line number information.
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);
225 fprintf(stderr, "+%04d byte(s)", dwDisp);
231 assert(!"Crashed in LLVMUnhandledExceptionFilter");
234 // Allow dialog box to pop up allowing choice to start debugger.
236 return (*OldFilter)(ep);
238 return EXCEPTION_CONTINUE_SEARCH;
241 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
242 // We are running in our very own thread, courtesy of Windows.
243 EnterCriticalSection(&CriticalSection);
246 // If an interrupt function has been set, go and run one it; otherwise,
248 void (*IF)() = InterruptFunction;
249 InterruptFunction = 0; // Don't run it on another CTRL-C.
252 // Note: if the interrupt function throws an exception, there is nothing
253 // to catch it in this thread so it will kill the process.
255 LeaveCriticalSection(&CriticalSection);
256 return TRUE; // Don't kill the process.
259 // Allow normal processing to take place; i.e., the process dies.
260 LeaveCriticalSection(&CriticalSection);