//===----------------------------------------------------------------------===//
#include "Win32.h"
-#include <llvm/System/Signals.h>
#include <stdio.h>
#include <vector>
-#ifdef __MINGW
-#include <imagehlp.h>
+#ifdef __MINGW32__
+ #include <imagehlp.h>
#else
-#include <dbghelp.h>
+ #include <dbghelp.h>
#endif
#include <psapi.h>
-#pragma comment(lib, "psapi.lib")
-#pragma comment(lib, "dbghelp.lib")
+#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);
+// 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;
}
// 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<sys::Path>;
- FilesToRemove->push_back(sys::Path(Filename));
+ 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?
+ const sys::FileStatus *Status = path.getFileStatus(false, ErrMsg);
+ if (!Status)
+ return true;
+ if (!Status->isDir) {
+ if (ErrMsg)
+ *ErrMsg = path.toString() + " is not a directory";
+ return true;
+ }
- if (path.isDirectory()) {
- 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 {
- FilesToRemove->back().destroyFile();
+ FilesToRemove->back().eraseFromDisk();
} catch (...) {
}
FilesToRemove->pop_back();
if (DirectoriesToRemove != NULL)
while (!DirectoriesToRemove->empty()) {
try {
- DirectoriesToRemove->back().destroyDirectory(true);
+ DirectoriesToRemove->back().eraseFromDisk(true);
} catch (...) {
}
DirectoriesToRemove->pop_back();
}
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