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 //===----------------------------------------------------------------------===//
15 #include <llvm/System/Signals.h>
26 #pragma comment(lib, "psapi.lib")
27 #pragma comment(lib, "dbghelp.lib")
30 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
31 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
33 static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
34 static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
35 static bool RegisteredUnhandledExceptionFilter = false;
36 static bool CleanupExecuted = false;
37 static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
39 // Windows creates a new thread to execute the console handler when an event
40 // (such as CTRL/C) occurs. This causes concurrency issues with the above
41 // globals which this critical section addresses.
42 static CRITICAL_SECTION CriticalSection;
46 //===----------------------------------------------------------------------===//
47 //=== WARNING: Implementation here must contain only Win32 specific code
48 //=== and must not be UNIX code
49 //===----------------------------------------------------------------------===//
52 static void RegisterHandler() {
53 if (RegisteredUnhandledExceptionFilter) {
54 EnterCriticalSection(&CriticalSection);
58 // Now's the time to create the critical section. This is the first time
59 // through here, and there's only one thread.
60 InitializeCriticalSection(&CriticalSection);
62 // Enter it immediately. Now if someone hits CTRL/C, the console handler
63 // can't proceed until the globals are updated.
64 EnterCriticalSection(&CriticalSection);
66 RegisteredUnhandledExceptionFilter = true;
67 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
68 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
70 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
71 // else multi-threading problems will ensue.
74 // RemoveFileOnSignal - The public API
75 void sys::RemoveFileOnSignal(const std::string &Filename) {
79 throw std::string("Process terminating -- cannot register for removal");
81 if (FilesToRemove == NULL)
82 FilesToRemove = new std::vector<sys::Path>;
84 FilesToRemove->push_back(sys::Path(Filename));
86 LeaveCriticalSection(&CriticalSection);
89 // RemoveDirectoryOnSignal - The public API
90 void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
94 throw std::string("Process terminating -- cannot register for removal");
96 if (path.isDirectory()) {
97 if (DirectoriesToRemove == NULL)
98 DirectoriesToRemove = new std::vector<sys::Path>;
100 DirectoriesToRemove->push_back(path);
103 LeaveCriticalSection(&CriticalSection);
106 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
107 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
108 void sys::PrintStackTraceOnErrorSignal() {
110 LeaveCriticalSection(&CriticalSection);
115 static void Cleanup() {
116 EnterCriticalSection(&CriticalSection);
118 // Prevent other thread from registering new files and directories for
119 // removal, should we be executing because of the console handler callback.
120 CleanupExecuted = true;
122 // FIXME: open files cannot be deleted.
124 if (FilesToRemove != NULL)
125 while (!FilesToRemove->empty()) {
127 FilesToRemove->back().destroyFile();
130 FilesToRemove->pop_back();
133 if (DirectoriesToRemove != NULL)
134 while (!DirectoriesToRemove->empty()) {
136 DirectoriesToRemove->back().destroyDirectory(true);
139 DirectoriesToRemove->pop_back();
142 LeaveCriticalSection(&CriticalSection);
145 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
149 // Initialize the STACKFRAME structure.
150 STACKFRAME StackFrame;
151 memset(&StackFrame, 0, sizeof(StackFrame));
153 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
154 StackFrame.AddrPC.Mode = AddrModeFlat;
155 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
156 StackFrame.AddrStack.Mode = AddrModeFlat;
157 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
158 StackFrame.AddrFrame.Mode = AddrModeFlat;
160 HANDLE hProcess = GetCurrentProcess();
161 HANDLE hThread = GetCurrentThread();
163 // Initialize the symbol handler.
164 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
165 SymInitialize(hProcess, NULL, TRUE);
168 if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
169 ep->ContextRecord, NULL, SymFunctionTableAccess,
170 SymGetModuleBase, NULL)) {
174 if (StackFrame.AddrFrame.Offset == 0)
177 // Print the PC in hexadecimal.
178 DWORD PC = StackFrame.AddrPC.Offset;
179 fprintf(stderr, "%08X", PC);
181 // Print the parameters. Assume there are four.
182 fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0],
183 StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);
185 // Verify the PC belongs to a module in this process.
186 if (!SymGetModuleBase(hProcess, PC)) {
187 fputs(" <unknown module>\n", stderr);
191 // Print the symbol name.
193 IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
194 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
195 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
196 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
199 if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
206 fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp);
208 fprintf(stderr, ", %s", symbol->Name);
210 // Print the source file and line number information.
212 memset(&line, 0, sizeof(line));
213 line.SizeOfStruct = sizeof(line);
214 if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) {
215 fprintf(stderr, ", %s, line %d", line.FileName, line.LineNumber);
217 fprintf(stderr, "+%04d byte(s)", dwDisp);
223 assert(!"Crashed in LLVMUnhandledExceptionFilter");
226 // Allow dialog box to pop up allowing choice to start debugger.
228 return (*OldFilter)(ep);
230 return EXCEPTION_CONTINUE_SEARCH;
233 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
236 // Allow normal processing to take place; i.e., the process dies.
240 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab