blob: d1bd2e84e043aa1b3813509c21151ad80c1eb092 [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;
31static CRITICAL_SECTION CriticalSection;
32
Reid Spencer496c2772004-08-29 19:22:48 +000033namespace llvm {
Reid Spencer496c2772004-08-29 19:22:48 +000034
35//===----------------------------------------------------------------------===//
36//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer90b54132004-09-16 15:53:16 +000037//=== and must not be UNIX code
Reid Spencer496c2772004-08-29 19:22:48 +000038//===----------------------------------------------------------------------===//
39
Reid Spencer90b54132004-09-16 15:53:16 +000040
41static void RegisterHandler() {
42 if (RegisteredUnhandledExceptionFilter)
43 return;
44
45 // Now's the time to create the critical section. This is the first time
46 // through here, and there's only one thread.
47 InitializeCriticalSection(&CriticalSection);
48
49 // Enter it immediately. Now if someone hits CTRL/C, the console handler
50 // can't proceed until the globals are updated.
51 EnterCriticalSection(&CriticalSection);
52
53 RegisteredUnhandledExceptionFilter = true;
54 SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
55 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
56
57 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
58 // else multi-threading problems will ensue.
59}
60
Reid Spencer496c2772004-08-29 19:22:48 +000061// RemoveFileOnSignal - The public API
Reid Spencer90b54132004-09-16 15:53:16 +000062void sys::RemoveFileOnSignal(const std::string &Filename) {
63 RegisterHandler();
64
65 if (FilesToRemove == NULL)
66 FilesToRemove = new std::vector<std::string>;
67
68 FilesToRemove->push_back(Filename);
69
70 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +000071}
72
73// RemoveDirectoryOnSignal - The public API
Reid Spencer90b54132004-09-16 15:53:16 +000074void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
75 RegisterHandler();
76
77 if (path.is_directory()) {
78 if (DirectoriesToRemove == NULL)
79 DirectoriesToRemove = new std::vector<sys::Path>;
80
81 DirectoriesToRemove->push_back(path);
82 }
83
84 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +000085}
86
87/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
88/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Reid Spencer90b54132004-09-16 15:53:16 +000089void sys::PrintStackTraceOnErrorSignal() {
90 RegisterHandler();
91 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +000092}
93
94}
95
Reid Spencer90b54132004-09-16 15:53:16 +000096static void Cleanup() {
97 EnterCriticalSection(&CriticalSection);
98
99 if (FilesToRemove != NULL)
100 while (!FilesToRemove->empty()) {
101 try {
102 std::remove(FilesToRemove->back().c_str());
103 } catch (...) {
104 }
105 FilesToRemove->pop_back();
106 }
107
108 if (DirectoriesToRemove != NULL)
109 while (!DirectoriesToRemove->empty()) {
110 try {
111 DirectoriesToRemove->back().destroy_directory(true);
112 } catch (...) {
113 }
114 DirectoriesToRemove->pop_back();
115 }
116
117 LeaveCriticalSection(&CriticalSection);
118}
119
120static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
121 try {
122 Cleanup();
123
124 // Initialize the STACKFRAME structure.
125 STACKFRAME StackFrame;
126 memset(&StackFrame, 0, sizeof(StackFrame));
127
128 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
129 StackFrame.AddrPC.Mode = AddrModeFlat;
130 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
131 StackFrame.AddrStack.Mode = AddrModeFlat;
132 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
133 StackFrame.AddrFrame.Mode = AddrModeFlat;
134
135 HANDLE hProcess = GetCurrentProcess();
136 HANDLE hThread = GetCurrentThread();
137
138 // Initialize the symbol handler.
139 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
140 SymInitialize(GetCurrentProcess(), NULL, TRUE);
141
142 while (true) {
143 if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
144 ep->ContextRecord, NULL, SymFunctionTableAccess,
145 SymGetModuleBase, NULL)) {
146 break;
147 }
148
149 if (StackFrame.AddrFrame.Offset == 0)
150 break;
151
152 // Print the PC in hexadecimal.
153 DWORD PC = StackFrame.AddrPC.Offset;
154 fprintf(stderr, "%04X:%08X", ep->ContextRecord->SegCs, PC);
155
156 // Print the parameters. Assume there are four.
157 fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0],
158 StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);
159
160 // Verify the PC belongs to a module in this process.
161 if (!SymGetModuleBase(hProcess, PC)) {
162 fputc('\n', stderr);
163 continue;
164 }
165
166 // Print the symbol name.
167 char buffer[512];
168 IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
169 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
170 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
171 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
172
173 DWORD dwDisp;
174 if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
175 fputc('\n', stderr);
176 continue;
177 }
178
179 buffer[511] = 0;
180 if (dwDisp > 0)
181 fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp);
182 else
183 fprintf(stderr, ", %s", symbol->Name);
184
185 // Print the source file and line number information.
186 IMAGEHLP_LINE line;
187 memset(&line, 0, sizeof(line));
188 line.SizeOfStruct = sizeof(line);
189 if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) {
190 fprintf(stderr, ", %s, line %d", line.FileName, line.LineNumber);
191 if (dwDisp > 0)
192 fprintf(stderr, "+%04d byte(s)", dwDisp);
193 }
194
195 fputc('\n', stderr);
196 }
197 }
198 catch (...)
199 {
200 assert(!"Crashed in LLVMUnhandledExceptionFilter");
201 }
202
203 // Allow dialog box to pop up allowing choice to start debugger.
204 return EXCEPTION_CONTINUE_SEARCH;
205}
206
207static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
208 Cleanup();
209
210 // Allow normal processing to take place.
211 return FALSE;
212}
213
Reid Spencer496c2772004-08-29 19:22:48 +0000214// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab