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 static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
33 static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
34 static bool RegisteredUnhandledExceptionFilter = false;
35 static bool CleanupExecuted = false;
36 static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
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.
41 static CRITICAL_SECTION CriticalSection;
45 //===----------------------------------------------------------------------===//
46 //=== WARNING: Implementation here must contain only Win32 specific code
47 //=== and must not be UNIX code
48 //===----------------------------------------------------------------------===//
51 static void RegisterHandler() {
52 if (RegisteredUnhandledExceptionFilter) {
53 EnterCriticalSection(&CriticalSection);
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);
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);
65 RegisteredUnhandledExceptionFilter = true;
66 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
67 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
69 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
70 // else multi-threading problems will ensue.
73 // RemoveFileOnSignal - The public API
74 void sys::RemoveFileOnSignal(const sys::Path &Filename) {
78 throw std::string("Process terminating -- cannot register for removal");
80 if (FilesToRemove == NULL)
81 FilesToRemove = new std::vector<sys::Path>;
83 FilesToRemove->push_back(Filename);
85 LeaveCriticalSection(&CriticalSection);
88 // RemoveDirectoryOnSignal - The public API
89 void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
93 throw std::string("Process terminating -- cannot register for removal");
95 if (path.isDirectory()) {
96 if (DirectoriesToRemove == NULL)
97 DirectoriesToRemove = new std::vector<sys::Path>;
99 DirectoriesToRemove->push_back(path);
102 LeaveCriticalSection(&CriticalSection);
105 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
106 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
107 void sys::PrintStackTraceOnErrorSignal() {
109 LeaveCriticalSection(&CriticalSection);
114 static void Cleanup() {
115 EnterCriticalSection(&CriticalSection);
117 // Prevent other thread from registering new files and directories for
118 // removal, should we be executing because of the console handler callback.
119 CleanupExecuted = true;
121 // FIXME: open files cannot be deleted.
123 if (FilesToRemove != NULL)
124 while (!FilesToRemove->empty()) {
126 FilesToRemove->back().destroyFile();
129 FilesToRemove->pop_back();
132 if (DirectoriesToRemove != NULL)
133 while (!DirectoriesToRemove->empty()) {
135 DirectoriesToRemove->back().destroyDirectory(true);
138 DirectoriesToRemove->pop_back();
141 LeaveCriticalSection(&CriticalSection);
144 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
148 // Initialize the STACKFRAME structure.
149 STACKFRAME StackFrame;
150 memset(&StackFrame, 0, sizeof(StackFrame));
152 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
153 StackFrame.AddrPC.Mode = AddrModeFlat;
154 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
155 StackFrame.AddrStack.Mode = AddrModeFlat;
156 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
157 StackFrame.AddrFrame.Mode = AddrModeFlat;
159 HANDLE hProcess = GetCurrentProcess();
160 HANDLE hThread = GetCurrentThread();
162 // Initialize the symbol handler.
163 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
164 SymInitialize(hProcess, NULL, TRUE);
167 if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
168 ep->ContextRecord, NULL, SymFunctionTableAccess,
169 SymGetModuleBase, NULL)) {
173 if (StackFrame.AddrFrame.Offset == 0)
176 // Print the PC in hexadecimal.
177 DWORD PC = StackFrame.AddrPC.Offset;
178 fprintf(stderr, "%08X", PC);
180 // Print the parameters. Assume there are four.
181 fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0],
182 StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);
184 // Verify the PC belongs to a module in this process.
185 if (!SymGetModuleBase(hProcess, PC)) {
186 fputs(" <unknown module>\n", stderr);
190 // Print the symbol name.
192 IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
193 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
194 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
195 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
198 if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
205 fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp);
207 fprintf(stderr, ", %s", symbol->Name);
209 // Print the source file and line number information.
211 memset(&line, 0, sizeof(line));
212 line.SizeOfStruct = sizeof(line);
213 if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) {
214 fprintf(stderr, ", %s, line %d", line.FileName, line.LineNumber);
216 fprintf(stderr, "+%04d byte(s)", dwDisp);
222 assert(!"Crashed in LLVMUnhandledExceptionFilter");
225 // Allow dialog box to pop up allowing choice to start debugger.
227 return (*OldFilter)(ep);
229 return EXCEPTION_CONTINUE_SEARCH;
232 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
235 // Allow normal processing to take place; i.e., the process dies.
239 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab