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 //===----------------------------------------------------------------------===//
26 #if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
27 #error "libimagehlp.a & libpsapi.a should be present"
30 #pragma comment(lib, "psapi.lib")
31 #pragma comment(lib, "dbghelp.lib")
35 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
36 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
38 // InterruptFunction - The function to call if ctrl-c is pressed.
39 static void (*InterruptFunction)() = 0;
41 static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
42 static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
43 static bool RegisteredUnhandledExceptionFilter = false;
44 static bool CleanupExecuted = false;
45 static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
47 // Windows creates a new thread to execute the console handler when an event
48 // (such as CTRL/C) occurs. This causes concurrency issues with the above
49 // globals which this critical section addresses.
50 static CRITICAL_SECTION CriticalSection;
54 //===----------------------------------------------------------------------===//
55 //=== WARNING: Implementation here must contain only Win32 specific code
56 //=== and must not be UNIX code
57 //===----------------------------------------------------------------------===//
60 static void RegisterHandler() {
61 if (RegisteredUnhandledExceptionFilter) {
62 EnterCriticalSection(&CriticalSection);
66 // Now's the time to create the critical section. This is the first time
67 // through here, and there's only one thread.
68 InitializeCriticalSection(&CriticalSection);
70 // Enter it immediately. Now if someone hits CTRL/C, the console handler
71 // can't proceed until the globals are updated.
72 EnterCriticalSection(&CriticalSection);
74 RegisteredUnhandledExceptionFilter = true;
75 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
76 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
78 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
79 // else multi-threading problems will ensue.
82 // RemoveFileOnSignal - The public API
83 void sys::RemoveFileOnSignal(const sys::Path &Filename) {
87 throw std::string("Process terminating -- cannot register for removal");
89 if (FilesToRemove == NULL)
90 FilesToRemove = new std::vector<sys::Path>;
92 FilesToRemove->push_back(Filename);
94 LeaveCriticalSection(&CriticalSection);
97 // RemoveDirectoryOnSignal - The public API
98 void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
102 throw std::string("Process terminating -- cannot register for removal");
104 if (path.isDirectory()) {
105 if (DirectoriesToRemove == NULL)
106 DirectoriesToRemove = new std::vector<sys::Path>;
108 DirectoriesToRemove->push_back(path);
111 LeaveCriticalSection(&CriticalSection);
114 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
115 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
116 void sys::PrintStackTraceOnErrorSignal() {
118 LeaveCriticalSection(&CriticalSection);
122 void sys::SetInterruptFunction(void (*IF)()) {
124 InterruptFunction = IF;
125 LeaveCriticalSection(&CriticalSection);
129 static void Cleanup() {
130 EnterCriticalSection(&CriticalSection);
132 // Prevent other thread from registering new files and directories for
133 // removal, should we be executing because of the console handler callback.
134 CleanupExecuted = true;
136 // FIXME: open files cannot be deleted.
138 if (FilesToRemove != NULL)
139 while (!FilesToRemove->empty()) {
141 FilesToRemove->back().eraseFromDisk();
144 FilesToRemove->pop_back();
147 if (DirectoriesToRemove != NULL)
148 while (!DirectoriesToRemove->empty()) {
150 DirectoriesToRemove->back().eraseFromDisk(true);
153 DirectoriesToRemove->pop_back();
156 LeaveCriticalSection(&CriticalSection);
159 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
163 // Initialize the STACKFRAME structure.
164 STACKFRAME StackFrame;
165 memset(&StackFrame, 0, sizeof(StackFrame));
167 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
168 StackFrame.AddrPC.Mode = AddrModeFlat;
169 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
170 StackFrame.AddrStack.Mode = AddrModeFlat;
171 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
172 StackFrame.AddrFrame.Mode = AddrModeFlat;
174 HANDLE hProcess = GetCurrentProcess();
175 HANDLE hThread = GetCurrentThread();
177 // Initialize the symbol handler.
178 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
179 SymInitialize(hProcess, NULL, TRUE);
182 if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
183 ep->ContextRecord, NULL, SymFunctionTableAccess,
184 SymGetModuleBase, NULL)) {
188 if (StackFrame.AddrFrame.Offset == 0)
191 // Print the PC in hexadecimal.
192 DWORD PC = StackFrame.AddrPC.Offset;
193 fprintf(stderr, "%08X", PC);
195 // Print the parameters. Assume there are four.
196 fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0],
197 StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);
199 // Verify the PC belongs to a module in this process.
200 if (!SymGetModuleBase(hProcess, PC)) {
201 fputs(" <unknown module>\n", stderr);
205 // Print the symbol name.
207 IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
208 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
209 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
210 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
213 if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
220 fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp);
222 fprintf(stderr, ", %s", symbol->Name);
224 // Print the source file and line number information.
226 memset(&line, 0, sizeof(line));
227 line.SizeOfStruct = sizeof(line);
228 if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) {
229 fprintf(stderr, ", %s, line %d", line.FileName, line.LineNumber);
231 fprintf(stderr, "+%04d byte(s)", dwDisp);
237 assert(!"Crashed in LLVMUnhandledExceptionFilter");
240 // Allow dialog box to pop up allowing choice to start debugger.
242 return (*OldFilter)(ep);
244 return EXCEPTION_CONTINUE_SEARCH;
247 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
248 // We are running in our very own thread, courtesy of Windows.
249 EnterCriticalSection(&CriticalSection);
252 // If an interrupt function has been set, go and run one it; otherwise,
254 void (*IF)() = InterruptFunction;
255 InterruptFunction = 0; // Don't run it on another CTRL-C.
258 // Note: if the interrupt function throws an exception, there is nothing
259 // to catch it in this thread so it will kill the process.
261 LeaveCriticalSection(&CriticalSection);
262 return TRUE; // Don't kill the process.
265 // Allow normal processing to take place; i.e., the process dies.
266 LeaveCriticalSection(&CriticalSection);