2004-08-30 03:22:48 +08:00
|
|
|
//===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-08-30 03:22:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Win32 specific implementation of the Signals class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
#include "Win32.h"
|
2004-09-29 07:58:03 +08:00
|
|
|
#include <stdio.h>
|
2004-09-16 23:53:16 +08:00
|
|
|
#include <vector>
|
2009-03-20 07:26:52 +08:00
|
|
|
#include <algorithm>
|
2004-09-16 23:53:16 +08:00
|
|
|
|
2005-02-19 11:01:13 +08:00
|
|
|
#ifdef __MINGW32__
|
2006-06-02 03:03:21 +08:00
|
|
|
#include <imagehlp.h>
|
2004-09-23 22:47:10 +08:00
|
|
|
#else
|
2006-06-02 03:03:21 +08:00
|
|
|
#include <dbghelp.h>
|
2004-09-23 22:47:10 +08:00
|
|
|
#endif
|
|
|
|
#include <psapi.h>
|
2004-09-16 23:53:16 +08:00
|
|
|
|
2006-06-02 03:03:21 +08:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
|
|
|
|
#error "libimagehlp.a & libpsapi.a should be present"
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#pragma comment(lib, "psapi.lib")
|
|
|
|
#pragma comment(lib, "dbghelp.lib")
|
|
|
|
#endif
|
2004-09-16 23:53:16 +08:00
|
|
|
|
|
|
|
// Forward declare.
|
|
|
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
|
|
|
|
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
|
|
|
|
|
2005-08-02 11:04:47 +08:00
|
|
|
// InterruptFunction - The function to call if ctrl-c is pressed.
|
|
|
|
static void (*InterruptFunction)() = 0;
|
|
|
|
|
2004-09-29 07:58:03 +08:00
|
|
|
static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
|
2009-03-05 05:21:36 +08:00
|
|
|
static std::vector<std::pair<void(*)(void*), void*> > *CallBacksToRun = 0;
|
2004-09-16 23:53:16 +08:00
|
|
|
static bool RegisteredUnhandledExceptionFilter = false;
|
2004-09-19 13:37:39 +08:00
|
|
|
static bool CleanupExecuted = false;
|
2009-09-23 00:10:35 +08:00
|
|
|
#ifdef _MSC_VER
|
2009-09-22 17:50:28 +08:00
|
|
|
static bool ExitOnUnhandledExceptions = false;
|
2009-09-23 00:10:35 +08:00
|
|
|
#endif
|
2004-09-19 13:37:39 +08:00
|
|
|
static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
|
2004-09-17 11:02:27 +08:00
|
|
|
|
|
|
|
// Windows creates a new thread to execute the console handler when an event
|
|
|
|
// (such as CTRL/C) occurs. This causes concurrency issues with the above
|
|
|
|
// globals which this critical section addresses.
|
2004-09-16 23:53:16 +08:00
|
|
|
static CRITICAL_SECTION CriticalSection;
|
|
|
|
|
2004-08-30 03:22:48 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only Win32 specific code
|
2004-09-16 23:53:16 +08:00
|
|
|
//=== and must not be UNIX code
|
2004-08-30 03:22:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-22 23:58:35 +08:00
|
|
|
#ifdef _MSC_VER
|
2009-09-22 17:50:28 +08:00
|
|
|
/// CRTReportHook - Function called on a CRT debugging event.
|
|
|
|
static int CRTReportHook(int ReportType, char *Message, int *Return) {
|
|
|
|
// Don't cause a DebugBreak() on return.
|
|
|
|
if (Return)
|
|
|
|
*Return = 0;
|
|
|
|
|
|
|
|
switch (ReportType) {
|
|
|
|
default:
|
|
|
|
case _CRT_ASSERT:
|
|
|
|
fprintf(stderr, "CRT assert: %s\n", Message);
|
|
|
|
// FIXME: Is there a way to just crash? Perhaps throw to the unhandled
|
|
|
|
// exception code? Perhaps SetErrorMode() handles this.
|
|
|
|
_exit(3);
|
|
|
|
break;
|
|
|
|
case _CRT_ERROR:
|
|
|
|
fprintf(stderr, "CRT error: %s\n", Message);
|
|
|
|
// FIXME: Is there a way to just crash? Perhaps throw to the unhandled
|
|
|
|
// exception code? Perhaps SetErrorMode() handles this.
|
|
|
|
_exit(3);
|
|
|
|
break;
|
|
|
|
case _CRT_WARN:
|
|
|
|
fprintf(stderr, "CRT warn: %s\n", Message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't call _CrtDbgReport.
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-09-22 23:58:35 +08:00
|
|
|
#endif
|
2004-09-16 23:53:16 +08:00
|
|
|
|
2009-09-22 17:50:28 +08:00
|
|
|
static void RegisterHandler() {
|
2004-09-19 13:37:39 +08:00
|
|
|
if (RegisteredUnhandledExceptionFilter) {
|
2004-09-17 11:02:27 +08:00
|
|
|
EnterCriticalSection(&CriticalSection);
|
2004-09-16 23:53:16 +08:00
|
|
|
return;
|
2004-09-17 11:02:27 +08:00
|
|
|
}
|
2004-09-16 23:53:16 +08:00
|
|
|
|
|
|
|
// Now's the time to create the critical section. This is the first time
|
|
|
|
// through here, and there's only one thread.
|
|
|
|
InitializeCriticalSection(&CriticalSection);
|
|
|
|
|
|
|
|
// Enter it immediately. Now if someone hits CTRL/C, the console handler
|
|
|
|
// can't proceed until the globals are updated.
|
|
|
|
EnterCriticalSection(&CriticalSection);
|
|
|
|
|
|
|
|
RegisteredUnhandledExceptionFilter = true;
|
2004-09-19 13:37:39 +08:00
|
|
|
OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
|
2004-09-16 23:53:16 +08:00
|
|
|
SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
|
|
|
|
|
2009-09-22 17:50:28 +08:00
|
|
|
// Environment variable to disable any kind of crash dialog.
|
2009-09-22 23:58:35 +08:00
|
|
|
#ifdef _MSC_VER
|
2009-09-22 17:50:28 +08:00
|
|
|
if (getenv("LLVM_DISABLE_CRT_DEBUG")) {
|
|
|
|
_CrtSetReportHook(CRTReportHook);
|
2009-09-22 23:58:35 +08:00
|
|
|
ExitOnUnhandledExceptions = true;
|
2009-09-22 17:50:28 +08:00
|
|
|
}
|
2009-09-22 23:58:35 +08:00
|
|
|
#endif
|
2009-09-22 17:50:28 +08:00
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
// IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
|
|
|
|
// else multi-threading problems will ensue.
|
|
|
|
}
|
|
|
|
|
2004-08-30 03:22:48 +08:00
|
|
|
// RemoveFileOnSignal - The public API
|
2006-08-26 05:37:17 +08:00
|
|
|
bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
|
2004-09-16 23:53:16 +08:00
|
|
|
RegisterHandler();
|
|
|
|
|
2006-08-26 05:37:17 +08:00
|
|
|
if (CleanupExecuted) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Process terminating -- cannot register for removal";
|
|
|
|
return true;
|
|
|
|
}
|
2004-09-19 13:37:39 +08:00
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
if (FilesToRemove == NULL)
|
2004-09-29 07:58:03 +08:00
|
|
|
FilesToRemove = new std::vector<sys::Path>;
|
2004-09-16 23:53:16 +08:00
|
|
|
|
2004-11-16 14:59:53 +08:00
|
|
|
FilesToRemove->push_back(Filename);
|
2004-09-16 23:53:16 +08:00
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2006-08-26 05:37:17 +08:00
|
|
|
return false;
|
2004-08-30 03:22:48 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 22:17:34 +08:00
|
|
|
// DontRemoveFileOnSignal - The public API
|
|
|
|
void sys::DontRemoveFileOnSignal(const sys::Path &Filename) {
|
|
|
|
if (FilesToRemove == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FilesToRemove->push_back(Filename);
|
|
|
|
std::vector<sys::Path>::reverse_iterator I =
|
|
|
|
std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename);
|
|
|
|
if (I != FilesToRemove->rend())
|
|
|
|
FilesToRemove->erase(I.base()-1);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
|
|
|
|
2004-08-30 03:22:48 +08:00
|
|
|
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
|
|
|
|
/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
|
2004-09-16 23:53:16 +08:00
|
|
|
void sys::PrintStackTraceOnErrorSignal() {
|
|
|
|
RegisterHandler();
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
|
|
|
|
2005-08-02 10:14:22 +08:00
|
|
|
|
|
|
|
void sys::SetInterruptFunction(void (*IF)()) {
|
2005-08-02 11:04:47 +08:00
|
|
|
RegisterHandler();
|
2005-08-02 11:26:32 +08:00
|
|
|
InterruptFunction = IF;
|
2005-08-02 11:04:47 +08:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2005-08-02 10:14:22 +08:00
|
|
|
}
|
2009-03-20 07:26:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// AddSignalHandler - Add a function to be called when a signal is delivered
|
|
|
|
/// to the process. The handler can have a cookie passed to it to identify
|
|
|
|
/// what instance of the handler it is.
|
|
|
|
void sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
|
|
|
|
if (CallBacksToRun == 0)
|
|
|
|
CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >();
|
|
|
|
CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
|
|
|
|
RegisterHandler();
|
2010-03-31 20:07:16 +08:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2009-03-20 07:26:52 +08:00
|
|
|
}
|
2004-08-30 03:22:48 +08:00
|
|
|
}
|
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
static void Cleanup() {
|
|
|
|
EnterCriticalSection(&CriticalSection);
|
|
|
|
|
2004-09-19 13:37:39 +08:00
|
|
|
// Prevent other thread from registering new files and directories for
|
|
|
|
// removal, should we be executing because of the console handler callback.
|
|
|
|
CleanupExecuted = true;
|
|
|
|
|
|
|
|
// FIXME: open files cannot be deleted.
|
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
if (FilesToRemove != NULL)
|
|
|
|
while (!FilesToRemove->empty()) {
|
2009-07-10 00:17:28 +08:00
|
|
|
FilesToRemove->back().eraseFromDisk();
|
2004-09-16 23:53:16 +08:00
|
|
|
FilesToRemove->pop_back();
|
|
|
|
}
|
|
|
|
|
2009-03-05 05:21:36 +08:00
|
|
|
if (CallBacksToRun)
|
|
|
|
for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
|
|
|
|
(*CallBacksToRun)[i].first((*CallBacksToRun)[i].second);
|
2004-09-16 23:53:16 +08:00
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
|
|
|
|
2010-05-08 10:10:34 +08:00
|
|
|
void llvm::sys::RunInterruptHandlers() {
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
|
|
|
|
try {
|
|
|
|
Cleanup();
|
2007-11-21 08:37:56 +08:00
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
// TODO: provide a x64 friendly version of the following
|
|
|
|
#else
|
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
// Initialize the STACKFRAME structure.
|
|
|
|
STACKFRAME StackFrame;
|
|
|
|
memset(&StackFrame, 0, sizeof(StackFrame));
|
|
|
|
|
|
|
|
StackFrame.AddrPC.Offset = ep->ContextRecord->Eip;
|
|
|
|
StackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrStack.Offset = ep->ContextRecord->Esp;
|
|
|
|
StackFrame.AddrStack.Mode = AddrModeFlat;
|
|
|
|
StackFrame.AddrFrame.Offset = ep->ContextRecord->Ebp;
|
|
|
|
StackFrame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
|
|
|
|
HANDLE hProcess = GetCurrentProcess();
|
|
|
|
HANDLE hThread = GetCurrentThread();
|
|
|
|
|
|
|
|
// Initialize the symbol handler.
|
|
|
|
SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
|
2004-09-19 13:37:39 +08:00
|
|
|
SymInitialize(hProcess, NULL, TRUE);
|
2004-09-16 23:53:16 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
|
|
|
|
ep->ContextRecord, NULL, SymFunctionTableAccess,
|
|
|
|
SymGetModuleBase, NULL)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StackFrame.AddrFrame.Offset == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Print the PC in hexadecimal.
|
|
|
|
DWORD PC = StackFrame.AddrPC.Offset;
|
2009-04-22 00:04:56 +08:00
|
|
|
fprintf(stderr, "%08lX", PC);
|
2004-09-16 23:53:16 +08:00
|
|
|
|
|
|
|
// Print the parameters. Assume there are four.
|
2009-04-22 00:04:56 +08:00
|
|
|
fprintf(stderr, " (0x%08lX 0x%08lX 0x%08lX 0x%08lX)", StackFrame.Params[0],
|
2004-09-16 23:53:16 +08:00
|
|
|
StackFrame.Params[1], StackFrame.Params[2], StackFrame.Params[3]);
|
|
|
|
|
|
|
|
// Verify the PC belongs to a module in this process.
|
|
|
|
if (!SymGetModuleBase(hProcess, PC)) {
|
2004-09-19 13:37:39 +08:00
|
|
|
fputs(" <unknown module>\n", stderr);
|
2004-09-16 23:53:16 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the symbol name.
|
|
|
|
char buffer[512];
|
|
|
|
IMAGEHLP_SYMBOL *symbol = reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer);
|
|
|
|
memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL));
|
|
|
|
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
|
|
|
|
symbol->MaxNameLength = 512 - sizeof(IMAGEHLP_SYMBOL);
|
|
|
|
|
|
|
|
DWORD dwDisp;
|
|
|
|
if (!SymGetSymFromAddr(hProcess, PC, &dwDisp, symbol)) {
|
|
|
|
fputc('\n', stderr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[511] = 0;
|
|
|
|
if (dwDisp > 0)
|
2009-04-22 00:04:56 +08:00
|
|
|
fprintf(stderr, ", %s()+%04lu bytes(s)", symbol->Name, dwDisp);
|
2004-09-16 23:53:16 +08:00
|
|
|
else
|
|
|
|
fprintf(stderr, ", %s", symbol->Name);
|
|
|
|
|
|
|
|
// Print the source file and line number information.
|
|
|
|
IMAGEHLP_LINE line;
|
|
|
|
memset(&line, 0, sizeof(line));
|
|
|
|
line.SizeOfStruct = sizeof(line);
|
|
|
|
if (SymGetLineFromAddr(hProcess, PC, &dwDisp, &line)) {
|
2009-04-22 00:04:56 +08:00
|
|
|
fprintf(stderr, ", %s, line %lu", line.FileName, line.LineNumber);
|
2004-09-16 23:53:16 +08:00
|
|
|
if (dwDisp > 0)
|
2009-04-22 00:04:56 +08:00
|
|
|
fprintf(stderr, "+%04lu byte(s)", dwDisp);
|
2004-09-16 23:53:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
2007-11-21 08:37:56 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-09-19 13:37:39 +08:00
|
|
|
} catch (...) {
|
2009-03-05 05:21:36 +08:00
|
|
|
assert(0 && "Crashed in LLVMUnhandledExceptionFilter");
|
2004-09-16 23:53:16 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 23:58:35 +08:00
|
|
|
#ifdef _MSC_VER
|
2009-09-22 17:50:28 +08:00
|
|
|
if (ExitOnUnhandledExceptions)
|
2010-07-12 16:16:59 +08:00
|
|
|
_exit(-3);
|
2009-09-22 23:58:35 +08:00
|
|
|
#endif
|
2009-09-22 17:50:28 +08:00
|
|
|
|
2004-09-16 23:53:16 +08:00
|
|
|
// Allow dialog box to pop up allowing choice to start debugger.
|
2004-09-19 13:37:39 +08:00
|
|
|
if (OldFilter)
|
|
|
|
return (*OldFilter)(ep);
|
|
|
|
else
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
2004-09-16 23:53:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
|
2005-08-02 11:26:32 +08:00
|
|
|
// We are running in our very own thread, courtesy of Windows.
|
2005-08-02 11:04:47 +08:00
|
|
|
EnterCriticalSection(&CriticalSection);
|
2004-09-16 23:53:16 +08:00
|
|
|
Cleanup();
|
|
|
|
|
2005-08-02 11:04:47 +08:00
|
|
|
// If an interrupt function has been set, go and run one it; otherwise,
|
|
|
|
// the process dies.
|
|
|
|
void (*IF)() = InterruptFunction;
|
|
|
|
InterruptFunction = 0; // Don't run it on another CTRL-C.
|
|
|
|
|
|
|
|
if (IF) {
|
2005-08-02 11:26:32 +08:00
|
|
|
// Note: if the interrupt function throws an exception, there is nothing
|
|
|
|
// to catch it in this thread so it will kill the process.
|
|
|
|
IF(); // Run it now.
|
2005-08-02 11:04:47 +08:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
return TRUE; // Don't kill the process.
|
|
|
|
}
|
|
|
|
|
2004-09-17 11:02:27 +08:00
|
|
|
// Allow normal processing to take place; i.e., the process dies.
|
2005-08-02 11:04:47 +08:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2004-09-16 23:53:16 +08:00
|
|
|
return FALSE;
|
2004-08-30 03:22:48 +08:00
|
|
|
}
|
|
|
|
|