//
// The LLVM Compiler Infrastructure
//
-// This file was developed by Jeff Cohen and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
#include "Win32.h"
-#include <llvm/System/Signals.h>
+#include <stdio.h>
#include <vector>
-#include "dbghelp.h"
-#include "psapi.h"
-
-#pragma comment(lib, "psapi.lib")
-#pragma comment(lib, "dbghelp.lib")
+#ifdef __MINGW32__
+ #include <imagehlp.h>
+#else
+ #include <dbghelp.h>
+#endif
+#include <psapi.h>
+
+#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
// Forward declare.
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
-static std::vector<std::string> *FilesToRemove = NULL;
+// InterruptFunction - The function to call if ctrl-c is pressed.
+static void (*InterruptFunction)() = 0;
+
+static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
static bool RegisteredUnhandledExceptionFilter = false;
static bool CleanupExecuted = false;
}
// RemoveFileOnSignal - The public API
-void sys::RemoveFileOnSignal(const std::string &Filename) {
+bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
RegisterHandler();
- if (CleanupExecuted)
- throw std::string("Process terminating -- cannot register for removal");
+ if (CleanupExecuted) {
+ if (ErrMsg)
+ *ErrMsg = "Process terminating -- cannot register for removal";
+ return true;
+ }
if (FilesToRemove == NULL)
- FilesToRemove = new std::vector<std::string>;
+ FilesToRemove = new std::vector<sys::Path>;
FilesToRemove->push_back(Filename);
LeaveCriticalSection(&CriticalSection);
+ return false;
}
// RemoveDirectoryOnSignal - The public API
-void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
- RegisterHandler();
-
- if (CleanupExecuted)
- throw std::string("Process terminating -- cannot register for removal");
+bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) {
+ // Not a directory?
+ WIN32_FILE_ATTRIBUTE_DATA fi;
+ if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
+ MakeErrMsg(ErrMsg, path.toString() + ": can't get status of file");
+ return true;
+ }
+
+ if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
+ if (ErrMsg)
+ *ErrMsg = path.toString() + ": not a directory";
+ return true;
+ }
- if (path.is_directory()) {
- if (DirectoriesToRemove == NULL)
- DirectoriesToRemove = new std::vector<sys::Path>;
+ RegisterHandler();
- DirectoriesToRemove->push_back(path);
+ if (CleanupExecuted) {
+ if (ErrMsg)
+ *ErrMsg = "Process terminating -- cannot register for removal";
+ return true;
}
+ if (DirectoriesToRemove == NULL)
+ DirectoriesToRemove = new std::vector<sys::Path>;
+ DirectoriesToRemove->push_back(path);
+
LeaveCriticalSection(&CriticalSection);
+ return false;
}
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
LeaveCriticalSection(&CriticalSection);
}
+
+void sys::SetInterruptFunction(void (*IF)()) {
+ RegisterHandler();
+ InterruptFunction = IF;
+ LeaveCriticalSection(&CriticalSection);
+}
}
static void Cleanup() {
if (FilesToRemove != NULL)
while (!FilesToRemove->empty()) {
try {
- std::remove(FilesToRemove->back().c_str());
+ FilesToRemove->back().eraseFromDisk();
} catch (...) {
}
FilesToRemove->pop_back();
if (DirectoriesToRemove != NULL)
while (!DirectoriesToRemove->empty()) {
try {
- DirectoriesToRemove->back().destroy_directory(true);
+ DirectoriesToRemove->back().eraseFromDisk(true);
} catch (...) {
}
DirectoriesToRemove->pop_back();
static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
try {
Cleanup();
-
+
+#ifdef _WIN64
+ // TODO: provide a x64 friendly version of the following
+#else
+
// Initialize the STACKFRAME structure.
STACKFRAME StackFrame;
memset(&StackFrame, 0, sizeof(StackFrame));
fputc('\n', stderr);
}
+
+#endif
+
} catch (...) {
assert(!"Crashed in LLVMUnhandledExceptionFilter");
}
}
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
+ // We are running in our very own thread, courtesy of Windows.
+ EnterCriticalSection(&CriticalSection);
Cleanup();
+ // 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) {
+ // 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.
+ LeaveCriticalSection(&CriticalSection);
+ return TRUE; // Don't kill the process.
+ }
+
// Allow normal processing to take place; i.e., the process dies.
+ LeaveCriticalSection(&CriticalSection);
return FALSE;
}
-// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab