blob: 473168edfcf6f5ea4d7fa06988af09c4fb2c65f7 [file] [log] [blame]
Reid Spencer496c2772004-08-29 19:22:48 +00001//===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
Mikhail Glushenkov84ba14f2010-10-21 20:40:39 +00002//
Reid Spencer496c2772004-08-29 19:22:48 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Mikhail Glushenkov84ba14f2010-10-21 20:40:39 +00007//
Reid Spencer496c2772004-08-29 19:22:48 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Signals class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000014#include "Windows.h"
Reid Spencerced41102004-09-28 23:58:03 +000015#include <stdio.h>
Reid Spencer90b54132004-09-16 15:53:16 +000016#include <vector>
Sebastian Redl48fe6352009-03-19 23:26:52 +000017#include <algorithm>
Reid Spencer90b54132004-09-16 15:53:16 +000018
Jeff Cohen23a1cf32005-02-19 03:01:13 +000019#ifdef __MINGW32__
Reid Spencer48fdf912006-06-01 19:03:21 +000020 #include <imagehlp.h>
Reid Spencerf6cbc0f2004-09-23 14:47:10 +000021#else
Reid Spencer48fdf912006-06-01 19:03:21 +000022 #include <dbghelp.h>
Reid Spencerf6cbc0f2004-09-23 14:47:10 +000023#endif
24#include <psapi.h>
Reid Spencer90b54132004-09-16 15:53:16 +000025
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +000026#ifdef _MSC_VER
27 #pragma comment(lib, "psapi.lib")
28 #pragma comment(lib, "dbghelp.lib")
29#elif __MINGW32__
Reid Spencer48fdf912006-06-01 19:03:21 +000030 #if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
31 #error "libimagehlp.a & libpsapi.a should be present"
32 #endif
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +000033 // The version of g++ that comes with MinGW does *not* properly understand
34 // the ll format specifier for printf. However, MinGW passes the format
35 // specifiers on to the MSVCRT entirely, and the CRT understands the ll
36 // specifier. So these warnings are spurious in this case. Since we compile
37 // with -Wall, this will generate these warnings which should be ignored. So
38 // we will turn off the warnings for this just file. However, MinGW also does
39 // not support push and pop for diagnostics, so we have to manually turn it
40 // back on at the end of the file.
41 #pragma GCC diagnostic ignored "-Wformat"
42 #pragma GCC diagnostic ignored "-Wformat-extra-args"
43
44 // MinGW does not have updated support for the 64-bit versions of the DebugHlp
45 // APIs. So we will have to load them manually. The structures and method
46 // signatures were pulled from DbgHelp.h in the Windows Platform SDK, and
47 // adjusted for brevity.
48 typedef struct _IMAGEHLP_LINE64 {
49 DWORD SizeOfStruct;
50 PVOID Key;
51 DWORD LineNumber;
52 PCHAR FileName;
53 DWORD64 Address;
54 } IMAGEHLP_LINE64, *PIMAGEHLP_LINE64;
55
56 typedef struct _IMAGEHLP_SYMBOL64 {
57 DWORD SizeOfStruct;
58 DWORD64 Address;
59 DWORD Size;
60 DWORD Flags;
61 DWORD MaxNameLength;
62 CHAR Name[1];
63 } IMAGEHLP_SYMBOL64, *PIMAGEHLP_SYMBOL64;
64
65 typedef struct _tagADDRESS64 {
66 DWORD64 Offset;
67 WORD Segment;
68 ADDRESS_MODE Mode;
69 } ADDRESS64, *LPADDRESS64;
70
71 typedef struct _KDHELP64 {
72 DWORD64 Thread;
73 DWORD ThCallbackStack;
74 DWORD ThCallbackBStore;
75 DWORD NextCallback;
76 DWORD FramePointer;
77 DWORD64 KiCallUserMode;
78 DWORD64 KeUserCallbackDispatcher;
79 DWORD64 SystemRangeStart;
80 DWORD64 KiUserExceptionDispatcher;
81 DWORD64 StackBase;
82 DWORD64 StackLimit;
83 DWORD64 Reserved[5];
84 } KDHELP64, *PKDHELP64;
85
86 typedef struct _tagSTACKFRAME64 {
87 ADDRESS64 AddrPC;
88 ADDRESS64 AddrReturn;
89 ADDRESS64 AddrFrame;
90 ADDRESS64 AddrStack;
91 ADDRESS64 AddrBStore;
92 PVOID FuncTableEntry;
93 DWORD64 Params[4];
94 BOOL Far;
95 BOOL Virtual;
96 DWORD64 Reserved[3];
97 KDHELP64 KdHelp;
98 } STACKFRAME64, *LPSTACKFRAME64;
99
100typedef BOOL (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE hProcess,
101 DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize,
102 LPDWORD lpNumberOfBytesRead);
103
104typedef PVOID (__stdcall *PFUNCTION_TABLE_ACCESS_ROUTINE64)( HANDLE ahProcess,
105 DWORD64 AddrBase);
106
107typedef DWORD64 (__stdcall *PGET_MODULE_BASE_ROUTINE64)(HANDLE hProcess,
108 DWORD64 Address);
109
110typedef DWORD64 (__stdcall *PTRANSLATE_ADDRESS_ROUTINE64)(HANDLE hProcess,
111 HANDLE hThread, LPADDRESS64 lpaddr);
112
113typedef BOOL (WINAPI *fpStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64,
114 PVOID, PREAD_PROCESS_MEMORY_ROUTINE64,
115 PFUNCTION_TABLE_ACCESS_ROUTINE64,
116 PGET_MODULE_BASE_ROUTINE64,
117 PTRANSLATE_ADDRESS_ROUTINE64);
118static fpStackWalk64 StackWalk64;
119
120typedef DWORD64 (WINAPI *fpSymGetModuleBase64)(HANDLE, DWORD64);
121static fpSymGetModuleBase64 SymGetModuleBase64;
122
123typedef BOOL (WINAPI *fpSymGetSymFromAddr64)(HANDLE, DWORD64,
124 PDWORD64, PIMAGEHLP_SYMBOL64);
125static fpSymGetSymFromAddr64 SymGetSymFromAddr64;
126
127typedef BOOL (WINAPI *fpSymGetLineFromAddr64)(HANDLE, DWORD64,
128 PDWORD, PIMAGEHLP_LINE64);
129static fpSymGetLineFromAddr64 SymGetLineFromAddr64;
130
131typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64);
132static fpSymFunctionTableAccess64 SymFunctionTableAccess64;
133
134static bool load64BitDebugHelp(void) {
135 HMODULE hLib = ::LoadLibrary("Dbghelp.dll");
136 if (hLib) {
137 StackWalk64 = (fpStackWalk64)
138 ::GetProcAddress(hLib, "StackWalk64");
139 SymGetModuleBase64 = (fpSymGetModuleBase64)
140 ::GetProcAddress(hLib, "SymGetModuleBase64");
141 SymGetSymFromAddr64 = (fpSymGetSymFromAddr64)
142 ::GetProcAddress(hLib, "SymGetSymFromAddr64");
143 SymGetLineFromAddr64 = (fpSymGetLineFromAddr64)
144 ::GetProcAddress(hLib, "SymGetLineFromAddr64");
145 SymFunctionTableAccess64 = (fpSymFunctionTableAccess64)
146 ::GetProcAddress(hLib, "SymFunctionTableAccess64");
147 }
148 return StackWalk64 != NULL;
149}
150#endif // __MINGW32__
Reid Spencer90b54132004-09-16 15:53:16 +0000151
152// Forward declare.
153static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
154static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
155
Jeff Cohenee841a12005-08-02 03:04:47 +0000156// InterruptFunction - The function to call if ctrl-c is pressed.
157static void (*InterruptFunction)() = 0;
158
Reid Spencerced41102004-09-28 23:58:03 +0000159static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
Chris Lattner35033a52009-03-04 21:21:36 +0000160static std::vector<std::pair<void(*)(void*), void*> > *CallBacksToRun = 0;
Reid Spencer90b54132004-09-16 15:53:16 +0000161static bool RegisteredUnhandledExceptionFilter = false;
Reid Spencer7b60a152004-09-19 05:37:39 +0000162static bool CleanupExecuted = false;
Daniel Dunbardf338842009-09-22 09:50:28 +0000163static bool ExitOnUnhandledExceptions = false;
Reid Spencer7b60a152004-09-19 05:37:39 +0000164static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
Reid Spencer298d6c12004-09-17 03:02:27 +0000165
166// Windows creates a new thread to execute the console handler when an event
167// (such as CTRL/C) occurs. This causes concurrency issues with the above
168// globals which this critical section addresses.
Reid Spencer90b54132004-09-16 15:53:16 +0000169static CRITICAL_SECTION CriticalSection;
170
Reid Spencer496c2772004-08-29 19:22:48 +0000171namespace llvm {
Reid Spencer496c2772004-08-29 19:22:48 +0000172
173//===----------------------------------------------------------------------===//
Mikhail Glushenkov84ba14f2010-10-21 20:40:39 +0000174//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer90b54132004-09-16 15:53:16 +0000175//=== and must not be UNIX code
Reid Spencer496c2772004-08-29 19:22:48 +0000176//===----------------------------------------------------------------------===//
177
Daniel Dunbar3be2d122009-09-22 15:58:35 +0000178#ifdef _MSC_VER
Daniel Dunbardf338842009-09-22 09:50:28 +0000179/// CRTReportHook - Function called on a CRT debugging event.
180static int CRTReportHook(int ReportType, char *Message, int *Return) {
181 // Don't cause a DebugBreak() on return.
182 if (Return)
183 *Return = 0;
Reid Spencer90b54132004-09-16 15:53:16 +0000184
Daniel Dunbardf338842009-09-22 09:50:28 +0000185 switch (ReportType) {
186 default:
187 case _CRT_ASSERT:
188 fprintf(stderr, "CRT assert: %s\n", Message);
189 // FIXME: Is there a way to just crash? Perhaps throw to the unhandled
190 // exception code? Perhaps SetErrorMode() handles this.
191 _exit(3);
192 break;
193 case _CRT_ERROR:
194 fprintf(stderr, "CRT error: %s\n", Message);
195 // FIXME: Is there a way to just crash? Perhaps throw to the unhandled
196 // exception code? Perhaps SetErrorMode() handles this.
197 _exit(3);
198 break;
199 case _CRT_WARN:
200 fprintf(stderr, "CRT warn: %s\n", Message);
201 break;
202 }
203
204 // Don't call _CrtDbgReport.
205 return TRUE;
206}
Daniel Dunbar3be2d122009-09-22 15:58:35 +0000207#endif
Daniel Dunbardf338842009-09-22 09:50:28 +0000208
209static void RegisterHandler() {
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000210#if __MINGW32__
211 // On MinGW, we need to load up the symbols explicitly, because the
212 // Win32 framework they include does not have support for the 64-bit
213 // versions of the APIs we need. If we cannot load up the APIs (which
214 // would be unexpected as they should exist on every version of Windows
215 // we support), we will bail out since there would be nothing to report.
216 if (!load64BitDebugHelp()) {
217 assert(false && "These APIs should always be available");
218 return;
219 }
220#endif
221
Reid Spencer7b60a152004-09-19 05:37:39 +0000222 if (RegisteredUnhandledExceptionFilter) {
Reid Spencer298d6c12004-09-17 03:02:27 +0000223 EnterCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +0000224 return;
Reid Spencer298d6c12004-09-17 03:02:27 +0000225 }
Reid Spencer90b54132004-09-16 15:53:16 +0000226
227 // Now's the time to create the critical section. This is the first time
228 // through here, and there's only one thread.
229 InitializeCriticalSection(&CriticalSection);
230
231 // Enter it immediately. Now if someone hits CTRL/C, the console handler
232 // can't proceed until the globals are updated.
233 EnterCriticalSection(&CriticalSection);
234
235 RegisteredUnhandledExceptionFilter = true;
Reid Spencer7b60a152004-09-19 05:37:39 +0000236 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
Reid Spencer90b54132004-09-16 15:53:16 +0000237 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
238
Daniel Dunbardf338842009-09-22 09:50:28 +0000239 // Environment variable to disable any kind of crash dialog.
240 if (getenv("LLVM_DISABLE_CRT_DEBUG")) {
NAKAMURA Takumia8bbe702010-10-06 02:15:22 +0000241#ifdef _MSC_VER
Daniel Dunbardf338842009-09-22 09:50:28 +0000242 _CrtSetReportHook(CRTReportHook);
NAKAMURA Takumia8bbe702010-10-06 02:15:22 +0000243#endif
NAKAMURA Takumiac25e442011-01-17 22:41:15 +0000244 SetErrorMode(SEM_FAILCRITICALERRORS |
245 SEM_NOGPFAULTERRORBOX |
246 SEM_NOOPENFILEERRORBOX);
Daniel Dunbar3be2d122009-09-22 15:58:35 +0000247 ExitOnUnhandledExceptions = true;
Daniel Dunbardf338842009-09-22 09:50:28 +0000248 }
249
Reid Spencer90b54132004-09-16 15:53:16 +0000250 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
251 // else multi-threading problems will ensue.
252}
253
Reid Spencer496c2772004-08-29 19:22:48 +0000254// RemoveFileOnSignal - The public API
Reid Spencer05545752006-08-25 21:37:17 +0000255bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
Reid Spencer90b54132004-09-16 15:53:16 +0000256 RegisterHandler();
257
Reid Spencer05545752006-08-25 21:37:17 +0000258 if (CleanupExecuted) {
259 if (ErrMsg)
260 *ErrMsg = "Process terminating -- cannot register for removal";
261 return true;
262 }
Reid Spencer7b60a152004-09-19 05:37:39 +0000263
Reid Spencer90b54132004-09-16 15:53:16 +0000264 if (FilesToRemove == NULL)
Reid Spencerced41102004-09-28 23:58:03 +0000265 FilesToRemove = new std::vector<sys::Path>;
Reid Spencer90b54132004-09-16 15:53:16 +0000266
Reid Spencer98601212004-11-16 06:59:53 +0000267 FilesToRemove->push_back(Filename);
Reid Spencer90b54132004-09-16 15:53:16 +0000268
269 LeaveCriticalSection(&CriticalSection);
Reid Spencer05545752006-08-25 21:37:17 +0000270 return false;
Reid Spencer496c2772004-08-29 19:22:48 +0000271}
272
Dan Gohman41154112010-09-01 14:17:34 +0000273// DontRemoveFileOnSignal - The public API
274void sys::DontRemoveFileOnSignal(const sys::Path &Filename) {
275 if (FilesToRemove == NULL)
276 return;
277
NAKAMURA Takumi2172f3c2010-10-22 01:23:50 +0000278 RegisterHandler();
279
Dan Gohman41154112010-09-01 14:17:34 +0000280 FilesToRemove->push_back(Filename);
281 std::vector<sys::Path>::reverse_iterator I =
282 std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename);
283 if (I != FilesToRemove->rend())
284 FilesToRemove->erase(I.base()-1);
285
286 LeaveCriticalSection(&CriticalSection);
287}
288
Reid Spencer496c2772004-08-29 19:22:48 +0000289/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
290/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Reid Spencer90b54132004-09-16 15:53:16 +0000291void sys::PrintStackTraceOnErrorSignal() {
292 RegisterHandler();
293 LeaveCriticalSection(&CriticalSection);
Reid Spencer496c2772004-08-29 19:22:48 +0000294}
295
Chris Lattnerfa8c2922005-08-02 02:14:22 +0000296
297void sys::SetInterruptFunction(void (*IF)()) {
Jeff Cohenee841a12005-08-02 03:04:47 +0000298 RegisterHandler();
Jeff Cohen64fe5842005-08-02 03:26:32 +0000299 InterruptFunction = IF;
Jeff Cohenee841a12005-08-02 03:04:47 +0000300 LeaveCriticalSection(&CriticalSection);
Chris Lattnerfa8c2922005-08-02 02:14:22 +0000301}
Sebastian Redl48fe6352009-03-19 23:26:52 +0000302
303
304/// AddSignalHandler - Add a function to be called when a signal is delivered
305/// to the process. The handler can have a cookie passed to it to identify
306/// what instance of the handler it is.
307void sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
308 if (CallBacksToRun == 0)
309 CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >();
310 CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
311 RegisterHandler();
Torok Edwin61633362010-03-31 12:07:16 +0000312 LeaveCriticalSection(&CriticalSection);
Sebastian Redl48fe6352009-03-19 23:26:52 +0000313}
Reid Spencer496c2772004-08-29 19:22:48 +0000314}
315
Reid Spencer90b54132004-09-16 15:53:16 +0000316static void Cleanup() {
317 EnterCriticalSection(&CriticalSection);
318
Reid Spencer7b60a152004-09-19 05:37:39 +0000319 // Prevent other thread from registering new files and directories for
320 // removal, should we be executing because of the console handler callback.
321 CleanupExecuted = true;
322
323 // FIXME: open files cannot be deleted.
324
Reid Spencer90b54132004-09-16 15:53:16 +0000325 if (FilesToRemove != NULL)
326 while (!FilesToRemove->empty()) {
Chris Lattnerac356602009-07-09 16:17:28 +0000327 FilesToRemove->back().eraseFromDisk();
Reid Spencer90b54132004-09-16 15:53:16 +0000328 FilesToRemove->pop_back();
329 }
330
Chris Lattner35033a52009-03-04 21:21:36 +0000331 if (CallBacksToRun)
332 for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
333 (*CallBacksToRun)[i].first((*CallBacksToRun)[i].second);
Reid Spencer90b54132004-09-16 15:53:16 +0000334
335 LeaveCriticalSection(&CriticalSection);
336}
337
Daniel Dunbarfb89e082010-05-08 02:10:34 +0000338void llvm::sys::RunInterruptHandlers() {
339 Cleanup();
340}
341
Reid Spencer90b54132004-09-16 15:53:16 +0000342static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
Mikhail Glushenkov11d03f62010-10-27 09:09:04 +0000343 Cleanup();
Mikhail Glushenkov84ba14f2010-10-21 20:40:39 +0000344
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000345 // Initialize the STACKFRAME structure.
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000346 STACKFRAME64 StackFrame;
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000347 memset(&StackFrame, 0, sizeof(StackFrame));
Reid Spencer90b54132004-09-16 15:53:16 +0000348
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000349 DWORD machineType;
350#if defined(_M_X64)
351 machineType = IMAGE_FILE_MACHINE_AMD64;
352 StackFrame.AddrPC.Offset = ep->ContextRecord->Rip;
353 StackFrame.AddrPC.Mode = AddrModeFlat;
354 StackFrame.AddrStack.Offset = ep->ContextRecord->Rsp;
355 StackFrame.AddrStack.Mode = AddrModeFlat;
356 StackFrame.AddrFrame.Offset = ep->ContextRecord->Rbp;
357 StackFrame.AddrFrame.Mode = AddrModeFlat;
358#elif defined(_M_IX86)
359 machineType = IMAGE_FILE_MACHINE_I386;
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000360 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
361 StackFrame.AddrPC.Mode = AddrModeFlat;
362 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
363 StackFrame.AddrStack.Mode = AddrModeFlat;
364 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
365 StackFrame.AddrFrame.Mode = AddrModeFlat;
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000366#endif
Reid Spencer90b54132004-09-16 15:53:16 +0000367
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000368 HANDLE hProcess = GetCurrentProcess();
369 HANDLE hThread = GetCurrentThread();
Reid Spencer90b54132004-09-16 15:53:16 +0000370
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000371 // Initialize the symbol handler.
372 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
373 SymInitialize(hProcess, NULL, TRUE);
Reid Spencer90b54132004-09-16 15:53:16 +0000374
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000375 while (true) {
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000376 if (!StackWalk64(machineType, hProcess, hThread, &StackFrame,
377 ep->ContextRecord, NULL, SymFunctionTableAccess64,
378 SymGetModuleBase64, NULL)) {
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000379 break;
Reid Spencer90b54132004-09-16 15:53:16 +0000380 }
Chuck Rose III0ccb9302007-11-21 00:37:56 +0000381
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000382 if (StackFrame.AddrFrame.Offset == 0)
383 break;
384
385 // Print the PC in hexadecimal.
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000386 DWORD64 PC = StackFrame.AddrPC.Offset;
387#if defined(_M_X64)
388 fprintf(stderr, "0x%016llX", PC);
389#elif defined(_M_IX86)
390 fprintf(stderr, "0x%08lX", static_cast<DWORD>(PC));
391#endif
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000392
393 // Print the parameters. Assume there are four.
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000394#if defined(_M_X64)
395 fprintf(stderr, " (0x%016llX 0x%016llX 0x%016llX 0x%016llX)",
396 StackFrame.Params[0],
397 StackFrame.Params[1],
398 StackFrame.Params[2],
399 StackFrame.Params[3]);
400#elif defined(_M_IX86)
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000401 fprintf(stderr, " (0x%08lX 0x%08lX 0x%08lX 0x%08lX)",
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000402 static_cast<DWORD>(StackFrame.Params[0]),
403 static_cast<DWORD>(StackFrame.Params[1]),
404 static_cast<DWORD>(StackFrame.Params[2]),
405 static_cast<DWORD>(StackFrame.Params[3]));
406#endif
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000407 // Verify the PC belongs to a module in this process.
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000408 if (!SymGetModuleBase64(hProcess, PC)) {
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000409 fputs(" <unknown module>\n", stderr);
410 continue;
411 }
412
413 // Print the symbol name.
414 char buffer[512];
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000415 IMAGEHLP_SYMBOL64 *symbol = reinterpret_cast<IMAGEHLP_SYMBOL64 *>(buffer);
416 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64));
417 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
418 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64);
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000419
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000420 DWORD64 dwDisp;
421 if (!SymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000422 fputc('\n', stderr);
423 continue;
424 }
425
426 buffer[511] = 0;
427 if (dwDisp > 0)
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000428 fprintf(stderr, ", %s() + 0x%llX bytes(s)", symbol->Name, dwDisp);
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000429 else
430 fprintf(stderr, ", %s", symbol->Name);
431
432 // Print the source file and line number information.
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000433 IMAGEHLP_LINE64 line;
434 DWORD dwLineDisp;
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000435 memset(&line, 0, sizeof(line));
436 line.SizeOfStruct = sizeof(line);
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000437 if (SymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) {
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000438 fprintf(stderr, ", %s, line %lu", line.FileName, line.LineNumber);
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000439 if (dwLineDisp > 0)
440 fprintf(stderr, " + 0x%lX byte(s)", dwLineDisp);
Mikhail Glushenkov6d8ac5a2010-10-28 08:25:44 +0000441 }
442
443 fputc('\n', stderr);
444 }
445
Daniel Dunbardf338842009-09-22 09:50:28 +0000446 if (ExitOnUnhandledExceptions)
Duncan Sands34727662010-07-12 08:16:59 +0000447 _exit(-3);
Daniel Dunbardf338842009-09-22 09:50:28 +0000448
Reid Spencer90b54132004-09-16 15:53:16 +0000449 // Allow dialog box to pop up allowing choice to start debugger.
Reid Spencer7b60a152004-09-19 05:37:39 +0000450 if (OldFilter)
451 return (*OldFilter)(ep);
452 else
453 return EXCEPTION_CONTINUE_SEARCH;
Reid Spencer90b54132004-09-16 15:53:16 +0000454}
455
456static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
Jeff Cohen64fe5842005-08-02 03:26:32 +0000457 // We are running in our very own thread, courtesy of Windows.
Jeff Cohenee841a12005-08-02 03:04:47 +0000458 EnterCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +0000459 Cleanup();
460
Jeff Cohenee841a12005-08-02 03:04:47 +0000461 // If an interrupt function has been set, go and run one it; otherwise,
462 // the process dies.
463 void (*IF)() = InterruptFunction;
464 InterruptFunction = 0; // Don't run it on another CTRL-C.
465
466 if (IF) {
Jeff Cohen64fe5842005-08-02 03:26:32 +0000467 // Note: if the interrupt function throws an exception, there is nothing
468 // to catch it in this thread so it will kill the process.
469 IF(); // Run it now.
Jeff Cohenee841a12005-08-02 03:04:47 +0000470 LeaveCriticalSection(&CriticalSection);
471 return TRUE; // Don't kill the process.
472 }
473
Reid Spencer298d6c12004-09-17 03:02:27 +0000474 // Allow normal processing to take place; i.e., the process dies.
Jeff Cohenee841a12005-08-02 03:04:47 +0000475 LeaveCriticalSection(&CriticalSection);
Reid Spencer90b54132004-09-16 15:53:16 +0000476 return FALSE;
477}
Michael J. Spencer0bcd9c72011-10-01 00:05:20 +0000478
479#if __MINGW32__
480 // We turned these warnings off for this file so that MinGW-g++ doesn't
481 // complain about the ll format specifiers used. Now we are turning the
482 // warnings back on. If MinGW starts to support diagnostic stacks, we can
483 // replace this with a pop.
484 #pragma GCC diagnostic warning "-Wformat"
485 #pragma GCC diagnostic warning "-Wformat-extra-args"
486#endif