2004-08-29 19:22:48 +00:00
|
|
|
//===- Win32/Signals.cpp - Win32 Signals Implementation ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-08-29 19:22:48 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Win32 specific implementation of the Signals class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-16 15:53:16 +00:00
|
|
|
#include "Win32.h"
|
2004-09-28 23:58:03 +00:00
|
|
|
#include <stdio.h>
|
2004-09-16 15:53:16 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2005-02-19 03:01:13 +00:00
|
|
|
#ifdef __MINGW32__
|
2006-06-01 19:03:21 +00:00
|
|
|
#include <imagehlp.h>
|
2004-09-23 14:47:10 +00:00
|
|
|
#else
|
2006-06-01 19:03:21 +00:00
|
|
|
#include <dbghelp.h>
|
2004-09-23 14:47:10 +00:00
|
|
|
#endif
|
|
|
|
#include <psapi.h>
|
2004-09-16 15:53:16 +00:00
|
|
|
|
2006-06-01 19:03:21 +00: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 15:53:16 +00:00
|
|
|
|
|
|
|
// Forward declare.
|
|
|
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
|
|
|
|
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
|
|
|
|
|
2005-08-02 03:04:47 +00:00
|
|
|
// InterruptFunction - The function to call if ctrl-c is pressed.
|
|
|
|
static void (*InterruptFunction)() = 0;
|
|
|
|
|
2004-09-28 23:58:03 +00:00
|
|
|
static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
|
2004-09-16 15:53:16 +00:00
|
|
|
static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
|
|
|
|
static bool RegisteredUnhandledExceptionFilter = false;
|
2004-09-19 05:37:39 +00:00
|
|
|
static bool CleanupExecuted = false;
|
|
|
|
static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
|
2004-09-17 03:02:27 +00: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 15:53:16 +00:00
|
|
|
static CRITICAL_SECTION CriticalSection;
|
|
|
|
|
2004-08-29 19:22:48 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only Win32 specific code
|
2004-09-16 15:53:16 +00:00
|
|
|
//=== and must not be UNIX code
|
2004-08-29 19:22:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-16 15:53:16 +00:00
|
|
|
|
|
|
|
static void RegisterHandler() {
|
2004-09-19 05:37:39 +00:00
|
|
|
if (RegisteredUnhandledExceptionFilter) {
|
2004-09-17 03:02:27 +00:00
|
|
|
EnterCriticalSection(&CriticalSection);
|
2004-09-16 15:53:16 +00:00
|
|
|
return;
|
2004-09-17 03:02:27 +00:00
|
|
|
}
|
2004-09-16 15:53:16 +00: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 05:37:39 +00:00
|
|
|
OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
|
2004-09-16 15:53:16 +00:00
|
|
|
SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
|
|
|
|
|
|
|
|
// IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
|
|
|
|
// else multi-threading problems will ensue.
|
|
|
|
}
|
|
|
|
|
2004-08-29 19:22:48 +00:00
|
|
|
// RemoveFileOnSignal - The public API
|
2006-08-25 21:37:17 +00:00
|
|
|
bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
|
2004-09-16 15:53:16 +00:00
|
|
|
RegisterHandler();
|
|
|
|
|
2006-08-25 21:37:17 +00:00
|
|
|
if (CleanupExecuted) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Process terminating -- cannot register for removal";
|
|
|
|
return true;
|
|
|
|
}
|
2004-09-19 05:37:39 +00:00
|
|
|
|
2004-09-16 15:53:16 +00:00
|
|
|
if (FilesToRemove == NULL)
|
2004-09-28 23:58:03 +00:00
|
|
|
FilesToRemove = new std::vector<sys::Path>;
|
2004-09-16 15:53:16 +00:00
|
|
|
|
2004-11-16 06:59:53 +00:00
|
|
|
FilesToRemove->push_back(Filename);
|
2004-09-16 15:53:16 +00:00
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2006-08-25 21:37:17 +00:00
|
|
|
return false;
|
2004-08-29 19:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDirectoryOnSignal - The public API
|
2006-08-25 21:37:17 +00:00
|
|
|
bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) {
|
2006-08-01 17:59:14 +00:00
|
|
|
// Not a directory?
|
2007-04-07 20:47:27 +00:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
|
|
|
|
MakeErrMsg(ErrMsg, path.toString() + ": can't get status of file");
|
2007-03-29 19:05:44 +00:00
|
|
|
return true;
|
2007-04-07 20:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
2006-08-25 21:37:17 +00:00
|
|
|
if (ErrMsg)
|
2007-04-07 20:47:27 +00:00
|
|
|
*ErrMsg = path.toString() + ": not a directory";
|
2006-08-25 21:37:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-08-01 17:59:14 +00:00
|
|
|
|
2004-09-16 15:53:16 +00:00
|
|
|
RegisterHandler();
|
|
|
|
|
2006-08-25 21:37:17 +00:00
|
|
|
if (CleanupExecuted) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Process terminating -- cannot register for removal";
|
|
|
|
return true;
|
|
|
|
}
|
2004-09-19 05:37:39 +00:00
|
|
|
|
2006-08-01 17:59:14 +00:00
|
|
|
if (DirectoriesToRemove == NULL)
|
|
|
|
DirectoriesToRemove = new std::vector<sys::Path>;
|
|
|
|
DirectoriesToRemove->push_back(path);
|
2004-09-16 15:53:16 +00:00
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2006-08-25 21:37:17 +00:00
|
|
|
return false;
|
2004-08-29 19:22:48 +00: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 15:53:16 +00:00
|
|
|
void sys::PrintStackTraceOnErrorSignal() {
|
|
|
|
RegisterHandler();
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
|
|
|
|
2005-08-02 02:14:22 +00:00
|
|
|
|
|
|
|
void sys::SetInterruptFunction(void (*IF)()) {
|
2005-08-02 03:04:47 +00:00
|
|
|
RegisterHandler();
|
2005-08-02 03:26:32 +00:00
|
|
|
InterruptFunction = IF;
|
2005-08-02 03:04:47 +00:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2005-08-02 02:14:22 +00:00
|
|
|
}
|
2004-08-29 19:22:48 +00:00
|
|
|
}
|
|
|
|
|
2004-09-16 15:53:16 +00:00
|
|
|
static void Cleanup() {
|
|
|
|
EnterCriticalSection(&CriticalSection);
|
|
|
|
|
2004-09-19 05:37:39 +00: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 15:53:16 +00:00
|
|
|
if (FilesToRemove != NULL)
|
|
|
|
while (!FilesToRemove->empty()) {
|
|
|
|
try {
|
2005-07-08 04:50:08 +00:00
|
|
|
FilesToRemove->back().eraseFromDisk();
|
2004-09-16 15:53:16 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
FilesToRemove->pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DirectoriesToRemove != NULL)
|
|
|
|
while (!DirectoriesToRemove->empty()) {
|
|
|
|
try {
|
2005-07-08 04:50:08 +00:00
|
|
|
DirectoriesToRemove->back().eraseFromDisk(true);
|
2004-09-16 15:53:16 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
DirectoriesToRemove->pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
|
|
|
|
try {
|
|
|
|
Cleanup();
|
2007-11-21 00:37:56 +00:00
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
// TODO: provide a x64 friendly version of the following
|
|
|
|
#else
|
|
|
|
|
2004-09-16 15:53:16 +00: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 05:37:39 +00:00
|
|
|
SymInitialize(hProcess, NULL, TRUE);
|
2004-09-16 15:53:16 +00: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;
|
2004-09-19 05:37:39 +00:00
|
|
|
fprintf(stderr, "%08X", PC);
|
2004-09-16 15:53:16 +00:00
|
|
|
|
|
|
|
// Print the parameters. Assume there are four.
|
|
|
|
fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0],
|
|
|
|
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 05:37:39 +00:00
|
|
|
fputs(" <unknown module>\n", stderr);
|
2004-09-16 15:53:16 +00: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)
|
|
|
|
fprintf(stderr, ", %s()+%04d bytes(s)", symbol->Name, dwDisp);
|
|
|
|
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)) {
|
|
|
|
fprintf(stderr, ", %s, line %d", line.FileName, line.LineNumber);
|
|
|
|
if (dwDisp > 0)
|
|
|
|
fprintf(stderr, "+%04d byte(s)", dwDisp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
2007-11-21 00:37:56 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-09-19 05:37:39 +00:00
|
|
|
} catch (...) {
|
2004-09-16 15:53:16 +00:00
|
|
|
assert(!"Crashed in LLVMUnhandledExceptionFilter");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow dialog box to pop up allowing choice to start debugger.
|
2004-09-19 05:37:39 +00:00
|
|
|
if (OldFilter)
|
|
|
|
return (*OldFilter)(ep);
|
|
|
|
else
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
2004-09-16 15:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
|
2005-08-02 03:26:32 +00:00
|
|
|
// We are running in our very own thread, courtesy of Windows.
|
2005-08-02 03:04:47 +00:00
|
|
|
EnterCriticalSection(&CriticalSection);
|
2004-09-16 15:53:16 +00:00
|
|
|
Cleanup();
|
|
|
|
|
2005-08-02 03:04:47 +00: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 03:26:32 +00: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 03:04:47 +00:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
|
|
|
return TRUE; // Don't kill the process.
|
|
|
|
}
|
|
|
|
|
2004-09-17 03:02:27 +00:00
|
|
|
// Allow normal processing to take place; i.e., the process dies.
|
2005-08-02 03:04:47 +00:00
|
|
|
LeaveCriticalSection(&CriticalSection);
|
2004-09-16 15:53:16 +00:00
|
|
|
return FALSE;
|
2004-08-29 19:22:48 +00:00
|
|
|
}
|
|
|
|
|