blob: 35ba6f8e1bba7542cf4c558c79bafab4b5a34dcb [file] [log] [blame]
Reid Spencer3d7a6142004-08-29 19:22:48 +00001//===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
Mikhail Glushenkovf64d93d2010-10-21 20:40:39 +00002//
Reid Spencer3d7a6142004-08-29 19:22:48 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Glushenkovf64d93d2010-10-21 20:40:39 +00007//
Reid Spencer3d7a6142004-08-29 19:22:48 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Signals class.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola9aa3d5d2013-06-14 13:59:21 +000014#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include <algorithm>
Reid Spencer70b68352004-09-28 23:58:03 +000016#include <stdio.h>
Reid Spencer4aff78a2004-09-16 15:53:16 +000017#include <vector>
18
Chandler Carruth10b09152014-01-07 12:37:13 +000019// The Windows.h header must be after LLVM and standard headers.
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000020#include "WindowsSupport.h"
Chandler Carruth10b09152014-01-07 12:37:13 +000021
Jeff Cohen07e22ba2005-02-19 03:01:13 +000022#ifdef __MINGW32__
Reid Spencer187b4ad2006-06-01 19:03:21 +000023 #include <imagehlp.h>
Reid Spencer99049282004-09-23 14:47:10 +000024#else
Reid Spencer187b4ad2006-06-01 19:03:21 +000025 #include <dbghelp.h>
Reid Spencer99049282004-09-23 14:47:10 +000026#endif
27#include <psapi.h>
Reid Spencer4aff78a2004-09-16 15:53:16 +000028
Michael J. Spencer44a36c82011-10-01 00:05:20 +000029#ifdef _MSC_VER
30 #pragma comment(lib, "psapi.lib")
31 #pragma comment(lib, "dbghelp.lib")
32#elif __MINGW32__
Reid Spencer187b4ad2006-06-01 19:03:21 +000033 #if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
34 #error "libimagehlp.a & libpsapi.a should be present"
35 #endif
Michael J. Spencer44a36c82011-10-01 00:05:20 +000036 // The version of g++ that comes with MinGW does *not* properly understand
37 // the ll format specifier for printf. However, MinGW passes the format
38 // specifiers on to the MSVCRT entirely, and the CRT understands the ll
39 // specifier. So these warnings are spurious in this case. Since we compile
40 // with -Wall, this will generate these warnings which should be ignored. So
41 // we will turn off the warnings for this just file. However, MinGW also does
42 // not support push and pop for diagnostics, so we have to manually turn it
43 // back on at the end of the file.
44 #pragma GCC diagnostic ignored "-Wformat"
45 #pragma GCC diagnostic ignored "-Wformat-extra-args"
46
Anton Korobeynikovb27f11e2011-10-21 09:38:50 +000047 #if !defined(__MINGW64_VERSION_MAJOR)
48 // MinGW.org does not have updated support for the 64-bit versions of the
49 // DebugHlp APIs. So we will have to load them manually. The structures and
50 // method signatures were pulled from DbgHelp.h in the Windows Platform SDK,
51 // and adjusted for brevity.
Michael J. Spencer44a36c82011-10-01 00:05:20 +000052 typedef struct _IMAGEHLP_LINE64 {
53 DWORD SizeOfStruct;
54 PVOID Key;
55 DWORD LineNumber;
56 PCHAR FileName;
57 DWORD64 Address;
58 } IMAGEHLP_LINE64, *PIMAGEHLP_LINE64;
59
60 typedef struct _IMAGEHLP_SYMBOL64 {
61 DWORD SizeOfStruct;
62 DWORD64 Address;
63 DWORD Size;
64 DWORD Flags;
65 DWORD MaxNameLength;
66 CHAR Name[1];
67 } IMAGEHLP_SYMBOL64, *PIMAGEHLP_SYMBOL64;
68
69 typedef struct _tagADDRESS64 {
70 DWORD64 Offset;
71 WORD Segment;
72 ADDRESS_MODE Mode;
73 } ADDRESS64, *LPADDRESS64;
74
75 typedef struct _KDHELP64 {
76 DWORD64 Thread;
77 DWORD ThCallbackStack;
78 DWORD ThCallbackBStore;
79 DWORD NextCallback;
80 DWORD FramePointer;
81 DWORD64 KiCallUserMode;
82 DWORD64 KeUserCallbackDispatcher;
83 DWORD64 SystemRangeStart;
84 DWORD64 KiUserExceptionDispatcher;
85 DWORD64 StackBase;
86 DWORD64 StackLimit;
87 DWORD64 Reserved[5];
88 } KDHELP64, *PKDHELP64;
89
90 typedef struct _tagSTACKFRAME64 {
91 ADDRESS64 AddrPC;
92 ADDRESS64 AddrReturn;
93 ADDRESS64 AddrFrame;
94 ADDRESS64 AddrStack;
95 ADDRESS64 AddrBStore;
96 PVOID FuncTableEntry;
97 DWORD64 Params[4];
98 BOOL Far;
99 BOOL Virtual;
100 DWORD64 Reserved[3];
101 KDHELP64 KdHelp;
102 } STACKFRAME64, *LPSTACKFRAME64;
103
104typedef BOOL (__stdcall *PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE hProcess,
105 DWORD64 qwBaseAddress, PVOID lpBuffer, DWORD nSize,
106 LPDWORD lpNumberOfBytesRead);
107
108typedef PVOID (__stdcall *PFUNCTION_TABLE_ACCESS_ROUTINE64)( HANDLE ahProcess,
109 DWORD64 AddrBase);
110
111typedef DWORD64 (__stdcall *PGET_MODULE_BASE_ROUTINE64)(HANDLE hProcess,
112 DWORD64 Address);
113
114typedef DWORD64 (__stdcall *PTRANSLATE_ADDRESS_ROUTINE64)(HANDLE hProcess,
115 HANDLE hThread, LPADDRESS64 lpaddr);
116
117typedef BOOL (WINAPI *fpStackWalk64)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64,
118 PVOID, PREAD_PROCESS_MEMORY_ROUTINE64,
119 PFUNCTION_TABLE_ACCESS_ROUTINE64,
120 PGET_MODULE_BASE_ROUTINE64,
121 PTRANSLATE_ADDRESS_ROUTINE64);
122static fpStackWalk64 StackWalk64;
123
124typedef DWORD64 (WINAPI *fpSymGetModuleBase64)(HANDLE, DWORD64);
125static fpSymGetModuleBase64 SymGetModuleBase64;
126
127typedef BOOL (WINAPI *fpSymGetSymFromAddr64)(HANDLE, DWORD64,
128 PDWORD64, PIMAGEHLP_SYMBOL64);
129static fpSymGetSymFromAddr64 SymGetSymFromAddr64;
130
131typedef BOOL (WINAPI *fpSymGetLineFromAddr64)(HANDLE, DWORD64,
132 PDWORD, PIMAGEHLP_LINE64);
133static fpSymGetLineFromAddr64 SymGetLineFromAddr64;
134
135typedef PVOID (WINAPI *fpSymFunctionTableAccess64)(HANDLE, DWORD64);
136static fpSymFunctionTableAccess64 SymFunctionTableAccess64;
137
138static bool load64BitDebugHelp(void) {
David Majnemer17a44962013-10-07 09:52:36 +0000139 HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000140 if (hLib) {
141 StackWalk64 = (fpStackWalk64)
142 ::GetProcAddress(hLib, "StackWalk64");
143 SymGetModuleBase64 = (fpSymGetModuleBase64)
144 ::GetProcAddress(hLib, "SymGetModuleBase64");
145 SymGetSymFromAddr64 = (fpSymGetSymFromAddr64)
146 ::GetProcAddress(hLib, "SymGetSymFromAddr64");
147 SymGetLineFromAddr64 = (fpSymGetLineFromAddr64)
148 ::GetProcAddress(hLib, "SymGetLineFromAddr64");
149 SymFunctionTableAccess64 = (fpSymFunctionTableAccess64)
150 ::GetProcAddress(hLib, "SymFunctionTableAccess64");
151 }
152 return StackWalk64 != NULL;
153}
Anton Korobeynikovb27f11e2011-10-21 09:38:50 +0000154 #endif // !defined(__MINGW64_VERSION_MAJOR)
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000155#endif // __MINGW32__
Reid Spencer4aff78a2004-09-16 15:53:16 +0000156
157// Forward declare.
158static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
159static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
160
Jeff Cohenba7cc682005-08-02 03:04:47 +0000161// InterruptFunction - The function to call if ctrl-c is pressed.
162static void (*InterruptFunction)() = 0;
163
Rafael Espindola4f35da72013-06-13 21:16:58 +0000164static std::vector<std::string> *FilesToRemove = NULL;
Chris Lattner4fdd0422009-03-04 21:21:36 +0000165static std::vector<std::pair<void(*)(void*), void*> > *CallBacksToRun = 0;
Reid Spencer4aff78a2004-09-16 15:53:16 +0000166static bool RegisteredUnhandledExceptionFilter = false;
Reid Spencer1bdd0f02004-09-19 05:37:39 +0000167static bool CleanupExecuted = false;
Daniel Dunbar4c7b0ca2009-09-22 09:50:28 +0000168static bool ExitOnUnhandledExceptions = false;
Reid Spencer1bdd0f02004-09-19 05:37:39 +0000169static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
Reid Spencer90debc52004-09-17 03:02:27 +0000170
171// Windows creates a new thread to execute the console handler when an event
172// (such as CTRL/C) occurs. This causes concurrency issues with the above
173// globals which this critical section addresses.
Reid Spencer4aff78a2004-09-16 15:53:16 +0000174static CRITICAL_SECTION CriticalSection;
175
Reid Spencer3d7a6142004-08-29 19:22:48 +0000176namespace llvm {
Reid Spencer3d7a6142004-08-29 19:22:48 +0000177
178//===----------------------------------------------------------------------===//
Mikhail Glushenkovf64d93d2010-10-21 20:40:39 +0000179//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer4aff78a2004-09-16 15:53:16 +0000180//=== and must not be UNIX code
Reid Spencer3d7a6142004-08-29 19:22:48 +0000181//===----------------------------------------------------------------------===//
182
Daniel Dunbar1bdedd32009-09-22 15:58:35 +0000183#ifdef _MSC_VER
Reid Klecknerbd39f212013-04-05 16:18:03 +0000184/// AvoidMessageBoxHook - Emulates hitting "retry" from an "abort, retry,
185/// ignore" CRT debug report dialog. "retry" raises an exception which
186/// ultimately triggers our stack dumper.
187static int AvoidMessageBoxHook(int ReportType, char *Message, int *Return) {
188 // Set *Return to the retry code for the return value of _CrtDbgReport:
189 // http://msdn.microsoft.com/en-us/library/8hyw4sy7(v=vs.71).aspx
190 // This may also trigger just-in-time debugging via DebugBreak().
191 if (Return)
192 *Return = 1;
193 // Don't call _CrtDbgReport.
194 return TRUE;
195}
196
Daniel Dunbar1bdedd32009-09-22 15:58:35 +0000197#endif
Daniel Dunbar4c7b0ca2009-09-22 09:50:28 +0000198
199static void RegisterHandler() {
Anton Korobeynikovb27f11e2011-10-21 09:38:50 +0000200#if __MINGW32__ && !defined(__MINGW64_VERSION_MAJOR)
201 // On MinGW.org, we need to load up the symbols explicitly, because the
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000202 // Win32 framework they include does not have support for the 64-bit
203 // versions of the APIs we need. If we cannot load up the APIs (which
204 // would be unexpected as they should exist on every version of Windows
205 // we support), we will bail out since there would be nothing to report.
206 if (!load64BitDebugHelp()) {
207 assert(false && "These APIs should always be available");
208 return;
209 }
210#endif
211
Reid Spencer1bdd0f02004-09-19 05:37:39 +0000212 if (RegisteredUnhandledExceptionFilter) {
Reid Spencer90debc52004-09-17 03:02:27 +0000213 EnterCriticalSection(&CriticalSection);
Reid Spencer4aff78a2004-09-16 15:53:16 +0000214 return;
Reid Spencer90debc52004-09-17 03:02:27 +0000215 }
Reid Spencer4aff78a2004-09-16 15:53:16 +0000216
217 // Now's the time to create the critical section. This is the first time
218 // through here, and there's only one thread.
219 InitializeCriticalSection(&CriticalSection);
220
221 // Enter it immediately. Now if someone hits CTRL/C, the console handler
222 // can't proceed until the globals are updated.
223 EnterCriticalSection(&CriticalSection);
224
225 RegisteredUnhandledExceptionFilter = true;
Reid Spencer1bdd0f02004-09-19 05:37:39 +0000226 OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
Reid Spencer4aff78a2004-09-16 15:53:16 +0000227 SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
228
Daniel Dunbar4c7b0ca2009-09-22 09:50:28 +0000229 // Environment variable to disable any kind of crash dialog.
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000230 if (getenv("LLVM_DISABLE_CRASH_REPORT")) {
NAKAMURA Takumiad205572010-10-06 02:15:22 +0000231#ifdef _MSC_VER
Rafael Espindola05e53fc2013-07-26 14:55:36 +0000232 _CrtSetReportHook(AvoidMessageBoxHook);
NAKAMURA Takumiad205572010-10-06 02:15:22 +0000233#endif
NAKAMURA Takumibb4ea1f2011-01-17 22:41:15 +0000234 SetErrorMode(SEM_FAILCRITICALERRORS |
235 SEM_NOGPFAULTERRORBOX |
236 SEM_NOOPENFILEERRORBOX);
Daniel Dunbar1bdedd32009-09-22 15:58:35 +0000237 ExitOnUnhandledExceptions = true;
Daniel Dunbar4c7b0ca2009-09-22 09:50:28 +0000238 }
239
Reid Spencer4aff78a2004-09-16 15:53:16 +0000240 // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
241 // else multi-threading problems will ensue.
242}
243
Reid Spencer3d7a6142004-08-29 19:22:48 +0000244// RemoveFileOnSignal - The public API
Rafael Espindola4f35da72013-06-13 21:16:58 +0000245bool sys::RemoveFileOnSignal(StringRef Filename, std::string* ErrMsg) {
Reid Spencer4aff78a2004-09-16 15:53:16 +0000246 RegisterHandler();
247
Reid Spencer50eac3b2006-08-25 21:37:17 +0000248 if (CleanupExecuted) {
249 if (ErrMsg)
250 *ErrMsg = "Process terminating -- cannot register for removal";
251 return true;
252 }
Reid Spencer1bdd0f02004-09-19 05:37:39 +0000253
Reid Spencer4aff78a2004-09-16 15:53:16 +0000254 if (FilesToRemove == NULL)
Rafael Espindola4f35da72013-06-13 21:16:58 +0000255 FilesToRemove = new std::vector<std::string>;
Reid Spencer4aff78a2004-09-16 15:53:16 +0000256
Reid Spencerf070b6c2004-11-16 06:59:53 +0000257 FilesToRemove->push_back(Filename);
Reid Spencer4aff78a2004-09-16 15:53:16 +0000258
259 LeaveCriticalSection(&CriticalSection);
Reid Spencer50eac3b2006-08-25 21:37:17 +0000260 return false;
Reid Spencer3d7a6142004-08-29 19:22:48 +0000261}
262
Dan Gohmane201c072010-09-01 14:17:34 +0000263// DontRemoveFileOnSignal - The public API
Rafael Espindola4f35da72013-06-13 21:16:58 +0000264void sys::DontRemoveFileOnSignal(StringRef Filename) {
Dan Gohmane201c072010-09-01 14:17:34 +0000265 if (FilesToRemove == NULL)
266 return;
267
NAKAMURA Takumi3f688b92010-10-22 01:23:50 +0000268 RegisterHandler();
269
Dan Gohmane201c072010-09-01 14:17:34 +0000270 FilesToRemove->push_back(Filename);
Rafael Espindola4f35da72013-06-13 21:16:58 +0000271 std::vector<std::string>::reverse_iterator I =
Dan Gohmane201c072010-09-01 14:17:34 +0000272 std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename);
273 if (I != FilesToRemove->rend())
274 FilesToRemove->erase(I.base()-1);
275
276 LeaveCriticalSection(&CriticalSection);
277}
278
Reid Spencer3d7a6142004-08-29 19:22:48 +0000279/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
280/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Reid Spencer4aff78a2004-09-16 15:53:16 +0000281void sys::PrintStackTraceOnErrorSignal() {
282 RegisterHandler();
283 LeaveCriticalSection(&CriticalSection);
Reid Spencer3d7a6142004-08-29 19:22:48 +0000284}
285
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000286void llvm::sys::PrintStackTrace(FILE *) {
287 // FIXME: Implement.
288}
289
Chris Lattner6a5d6ec2005-08-02 02:14:22 +0000290
291void sys::SetInterruptFunction(void (*IF)()) {
Jeff Cohenba7cc682005-08-02 03:04:47 +0000292 RegisterHandler();
Jeff Cohen9aafa062005-08-02 03:26:32 +0000293 InterruptFunction = IF;
Jeff Cohenba7cc682005-08-02 03:04:47 +0000294 LeaveCriticalSection(&CriticalSection);
Chris Lattner6a5d6ec2005-08-02 02:14:22 +0000295}
Sebastian Redl8d5baa02009-03-19 23:26:52 +0000296
297
298/// AddSignalHandler - Add a function to be called when a signal is delivered
299/// to the process. The handler can have a cookie passed to it to identify
300/// what instance of the handler it is.
301void sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
302 if (CallBacksToRun == 0)
303 CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >();
304 CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
305 RegisterHandler();
Torok Edwin6fb09562010-03-31 12:07:16 +0000306 LeaveCriticalSection(&CriticalSection);
Sebastian Redl8d5baa02009-03-19 23:26:52 +0000307}
Reid Spencer3d7a6142004-08-29 19:22:48 +0000308}
309
Reid Spencer4aff78a2004-09-16 15:53:16 +0000310static void Cleanup() {
311 EnterCriticalSection(&CriticalSection);
312
Reid Spencer1bdd0f02004-09-19 05:37:39 +0000313 // Prevent other thread from registering new files and directories for
314 // removal, should we be executing because of the console handler callback.
315 CleanupExecuted = true;
316
317 // FIXME: open files cannot be deleted.
318
Reid Spencer4aff78a2004-09-16 15:53:16 +0000319 if (FilesToRemove != NULL)
320 while (!FilesToRemove->empty()) {
Rafael Espindolad724c282014-02-23 13:37:37 +0000321 llvm::sys::fs::remove(FilesToRemove->back());
Reid Spencer4aff78a2004-09-16 15:53:16 +0000322 FilesToRemove->pop_back();
323 }
324
Chris Lattner4fdd0422009-03-04 21:21:36 +0000325 if (CallBacksToRun)
326 for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
327 (*CallBacksToRun)[i].first((*CallBacksToRun)[i].second);
Reid Spencer4aff78a2004-09-16 15:53:16 +0000328
329 LeaveCriticalSection(&CriticalSection);
330}
331
Daniel Dunbar68272562010-05-08 02:10:34 +0000332void llvm::sys::RunInterruptHandlers() {
333 Cleanup();
334}
335
Reid Spencer4aff78a2004-09-16 15:53:16 +0000336static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
Mikhail Glushenkov32acd742010-10-27 09:09:04 +0000337 Cleanup();
Mikhail Glushenkovf64d93d2010-10-21 20:40:39 +0000338
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000339 // Initialize the STACKFRAME structure.
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000340 STACKFRAME64 StackFrame;
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000341 memset(&StackFrame, 0, sizeof(StackFrame));
Reid Spencer4aff78a2004-09-16 15:53:16 +0000342
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000343 DWORD machineType;
344#if defined(_M_X64)
345 machineType = IMAGE_FILE_MACHINE_AMD64;
346 StackFrame.AddrPC.Offset = ep->ContextRecord->Rip;
347 StackFrame.AddrPC.Mode = AddrModeFlat;
348 StackFrame.AddrStack.Offset = ep->ContextRecord->Rsp;
349 StackFrame.AddrStack.Mode = AddrModeFlat;
350 StackFrame.AddrFrame.Offset = ep->ContextRecord->Rbp;
351 StackFrame.AddrFrame.Mode = AddrModeFlat;
352#elif defined(_M_IX86)
353 machineType = IMAGE_FILE_MACHINE_I386;
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000354 StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
355 StackFrame.AddrPC.Mode = AddrModeFlat;
356 StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
357 StackFrame.AddrStack.Mode = AddrModeFlat;
358 StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
359 StackFrame.AddrFrame.Mode = AddrModeFlat;
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000360#endif
Reid Spencer4aff78a2004-09-16 15:53:16 +0000361
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000362 HANDLE hProcess = GetCurrentProcess();
363 HANDLE hThread = GetCurrentThread();
Reid Spencer4aff78a2004-09-16 15:53:16 +0000364
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000365 // Initialize the symbol handler.
366 SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
367 SymInitialize(hProcess, NULL, TRUE);
Reid Spencer4aff78a2004-09-16 15:53:16 +0000368
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000369 while (true) {
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000370 if (!StackWalk64(machineType, hProcess, hThread, &StackFrame,
371 ep->ContextRecord, NULL, SymFunctionTableAccess64,
372 SymGetModuleBase64, NULL)) {
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000373 break;
Reid Spencer4aff78a2004-09-16 15:53:16 +0000374 }
Chuck Rose III07b57d22007-11-21 00:37:56 +0000375
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000376 if (StackFrame.AddrFrame.Offset == 0)
377 break;
378
379 // Print the PC in hexadecimal.
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000380 DWORD64 PC = StackFrame.AddrPC.Offset;
381#if defined(_M_X64)
382 fprintf(stderr, "0x%016llX", PC);
383#elif defined(_M_IX86)
384 fprintf(stderr, "0x%08lX", static_cast<DWORD>(PC));
385#endif
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000386
387 // Print the parameters. Assume there are four.
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000388#if defined(_M_X64)
389 fprintf(stderr, " (0x%016llX 0x%016llX 0x%016llX 0x%016llX)",
390 StackFrame.Params[0],
391 StackFrame.Params[1],
392 StackFrame.Params[2],
393 StackFrame.Params[3]);
394#elif defined(_M_IX86)
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000395 fprintf(stderr, " (0x%08lX 0x%08lX 0x%08lX 0x%08lX)",
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000396 static_cast<DWORD>(StackFrame.Params[0]),
397 static_cast<DWORD>(StackFrame.Params[1]),
398 static_cast<DWORD>(StackFrame.Params[2]),
399 static_cast<DWORD>(StackFrame.Params[3]));
400#endif
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000401 // Verify the PC belongs to a module in this process.
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000402 if (!SymGetModuleBase64(hProcess, PC)) {
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000403 fputs(" <unknown module>\n", stderr);
404 continue;
405 }
406
407 // Print the symbol name.
408 char buffer[512];
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000409 IMAGEHLP_SYMBOL64 *symbol = reinterpret_cast<IMAGEHLP_SYMBOL64 *>(buffer);
410 memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64));
411 symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
412 symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL64);
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000413
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000414 DWORD64 dwDisp;
415 if (!SymGetSymFromAddr64(hProcess, PC, &dwDisp, symbol)) {
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000416 fputc('\n', stderr);
417 continue;
418 }
419
420 buffer[511] = 0;
421 if (dwDisp > 0)
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000422 fprintf(stderr, ", %s() + 0x%llX bytes(s)", symbol->Name, dwDisp);
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000423 else
424 fprintf(stderr, ", %s", symbol->Name);
425
426 // Print the source file and line number information.
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000427 IMAGEHLP_LINE64 line;
428 DWORD dwLineDisp;
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000429 memset(&line, 0, sizeof(line));
430 line.SizeOfStruct = sizeof(line);
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000431 if (SymGetLineFromAddr64(hProcess, PC, &dwLineDisp, &line)) {
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000432 fprintf(stderr, ", %s, line %lu", line.FileName, line.LineNumber);
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000433 if (dwLineDisp > 0)
434 fprintf(stderr, " + 0x%lX byte(s)", dwLineDisp);
Mikhail Glushenkov080d86f2010-10-28 08:25:44 +0000435 }
436
437 fputc('\n', stderr);
438 }
439
Daniel Dunbar4c7b0ca2009-09-22 09:50:28 +0000440 if (ExitOnUnhandledExceptions)
NAKAMURA Takumi64404a32011-11-29 07:47:04 +0000441 _exit(ep->ExceptionRecord->ExceptionCode);
Daniel Dunbar4c7b0ca2009-09-22 09:50:28 +0000442
Reid Spencer4aff78a2004-09-16 15:53:16 +0000443 // Allow dialog box to pop up allowing choice to start debugger.
Reid Spencer1bdd0f02004-09-19 05:37:39 +0000444 if (OldFilter)
445 return (*OldFilter)(ep);
446 else
447 return EXCEPTION_CONTINUE_SEARCH;
Reid Spencer4aff78a2004-09-16 15:53:16 +0000448}
449
450static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
Jeff Cohen9aafa062005-08-02 03:26:32 +0000451 // We are running in our very own thread, courtesy of Windows.
Jeff Cohenba7cc682005-08-02 03:04:47 +0000452 EnterCriticalSection(&CriticalSection);
Reid Spencer4aff78a2004-09-16 15:53:16 +0000453 Cleanup();
454
Jeff Cohenba7cc682005-08-02 03:04:47 +0000455 // If an interrupt function has been set, go and run one it; otherwise,
456 // the process dies.
457 void (*IF)() = InterruptFunction;
458 InterruptFunction = 0; // Don't run it on another CTRL-C.
459
460 if (IF) {
Jeff Cohen9aafa062005-08-02 03:26:32 +0000461 // Note: if the interrupt function throws an exception, there is nothing
462 // to catch it in this thread so it will kill the process.
463 IF(); // Run it now.
Jeff Cohenba7cc682005-08-02 03:04:47 +0000464 LeaveCriticalSection(&CriticalSection);
465 return TRUE; // Don't kill the process.
466 }
467
Reid Spencer90debc52004-09-17 03:02:27 +0000468 // Allow normal processing to take place; i.e., the process dies.
Jeff Cohenba7cc682005-08-02 03:04:47 +0000469 LeaveCriticalSection(&CriticalSection);
Reid Spencer4aff78a2004-09-16 15:53:16 +0000470 return FALSE;
471}
Michael J. Spencer44a36c82011-10-01 00:05:20 +0000472
473#if __MINGW32__
474 // We turned these warnings off for this file so that MinGW-g++ doesn't
475 // complain about the ll format specifiers used. Now we are turning the
476 // warnings back on. If MinGW starts to support diagnostic stacks, we can
477 // replace this with a pop.
478 #pragma GCC diagnostic warning "-Wformat"
479 #pragma GCC diagnostic warning "-Wformat-extra-args"
480#endif