blob: 1b6202a9db14b9e4c1561989a395232b4d861557 [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"
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.
25static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
26static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
27
28static std::vector<std::string> *FilesToRemove = NULL;
29static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
30static bool RegisteredUnhandledExceptionFilter = false;
Reid Spencer7b60a152004-09-19 05:37:39 +000031static bool CleanupExecuted = false;
32static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
Reid Spencer298d6c12004-09-17 03:02:27 +000033
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 Spencer90b54132004-09-16 15:53:16 +000037static CRITICAL_SECTION CriticalSection;
38
Reid Spencer496c2772004-08-29 19:22:48 +000039namespace llvm {
Reid Spencer496c2772004-08-29 19:22:48 +000040
41//===----------------------------------------------------------------------===//
42//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer90b54132004-09-16 15:53:16 +000043//=== and must not be UNIX code
Reid Spencer496c2772004-08-29 19:22:48 +000044//===----------------------------------------------------------------------===//
45
Reid Spencer90b54132004-09-16 15:53:16 +000046
47static void RegisterHandler() {
Reid Spencer7b60a152004-09-19 05:37:39 +000048 if (RegisteredUnhandledExceptionFilter) {
Reid Spencer298d6c12004-09-17 03:02:27 +000049 EnterCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +000050 return;
Reid Spencer298d6c12004-09-17 03:02:27 +000051 }
Reid Spencer90b54132004-09-16 15:53:16 +000052
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 Spencer7b60a152004-09-19 05:37:39 +000062 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
Reid Spencer90b54132004-09-16 15:53:16 +000063 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
64
65 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
66 // else multi-threading problems will ensue.
67}
68
Reid Spencer496c2772004-08-29 19:22:48 +000069// RemoveFileOnSignal - The public API
Reid Spencer90b54132004-09-16 15:53:16 +000070void sys::RemoveFileOnSignal(const std::string &Filename) {
71 RegisterHandler();
72
Reid Spencer7b60a152004-09-19 05:37:39 +000073 if (CleanupExecuted)
74 throw std::string("Process terminating -- cannot register for removal");
75
Reid Spencer90b54132004-09-16 15:53:16 +000076 if (FilesToRemove == NULL)
77 FilesToRemove = new std::vector<std::string>;
78
79 FilesToRemove->push_back(Filename);
80
81 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +000082}
83
84// RemoveDirectoryOnSignal - The public API
Reid Spencer90b54132004-09-16 15:53:16 +000085void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
86 RegisterHandler();
87
Reid Spencer7b60a152004-09-19 05:37:39 +000088 if (CleanupExecuted)
89 throw std::string("Process terminating -- cannot register for removal");
90
Reid Spencer90b54132004-09-16 15:53:16 +000091 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 Spencer496c2772004-08-29 19:22:48 +000099}
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 Spencer90b54132004-09-16 15:53:16 +0000103void sys::PrintStackTraceOnErrorSignal() {
104 RegisterHandler();
105 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +0000106}
107
108}
109
Reid Spencer90b54132004-09-16 15:53:16 +0000110static void Cleanup() {
111 EnterCriticalSection(&CriticalSection);
112
Reid Spencer7b60a152004-09-19 05:37:39 +0000113 // 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 Spencer90b54132004-09-16 15:53:16 +0000119 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
140static 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 Spencer7b60a152004-09-19 05:37:39 +0000160 SymInitialize(hProcess, NULL, TRUE);
Reid Spencer90b54132004-09-16 15:53:16 +0000161
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 Spencer7b60a152004-09-19 05:37:39 +0000174 fprintf(stderr, "%08X", PC);
Reid Spencer90b54132004-09-16 15:53:16 +0000175
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 Spencer7b60a152004-09-19 05:37:39 +0000182 fputs(" <unknown module>\n", stderr);
Reid Spencer90b54132004-09-16 15:53:16 +0000183 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 Spencer7b60a152004-09-19 05:37:39 +0000217 } catch (...) {
Reid Spencer90b54132004-09-16 15:53:16 +0000218 assert(!"Crashed in LLVMUnhandledExceptionFilter");
219 }
220
221 // Allow dialog box to pop up allowing choice to start debugger.
Reid Spencer7b60a152004-09-19 05:37:39 +0000222 if (OldFilter)
223 return (*OldFilter)(ep);
224 else
225 return EXCEPTION_CONTINUE_SEARCH;
Reid Spencer90b54132004-09-16 15:53:16 +0000226}
227
228static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
229 Cleanup();
230
Reid Spencer298d6c12004-09-17 03:02:27 +0000231 // Allow normal processing to take place; i.e., the process dies.
Reid Spencer90b54132004-09-16 15:53:16 +0000232 return FALSE;
233}
234
Reid Spencer496c2772004-08-29 19:22:48 +0000235// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab