//===----------------------------------------------------------------------===//
#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;
+static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
+
+// 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.
static CRITICAL_SECTION CriticalSection;
namespace llvm {
static void RegisterHandler() {
- if (RegisteredUnhandledExceptionFilter)
+ if (RegisteredUnhandledExceptionFilter) {
+ EnterCriticalSection(&CriticalSection);
return;
+ }
// Now's the time to create the critical section. This is the first time
// through here, and there's only one thread.
EnterCriticalSection(&CriticalSection);
RegisteredUnhandledExceptionFilter = true;
- SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
+ OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
// IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
}
// RemoveFileOnSignal - The public API
-void sys::RemoveFileOnSignal(const std::string &Filename) {
+bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
RegisterHandler();
+ 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();
+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.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() {
EnterCriticalSection(&CriticalSection);
+ // 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.
+
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();
// Initialize the symbol handler.
SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
- SymInitialize(GetCurrentProcess(), NULL, TRUE);
+ SymInitialize(hProcess, NULL, TRUE);
while (true) {
if (!StackWalk(IMAGE_FILE_MACHINE_I386, hProcess, hThread, &StackFrame,
// Print the PC in hexadecimal.
DWORD PC = StackFrame.AddrPC.Offset;
- fprintf(stderr, "%04X:%08X", ep->ContextRecord->SegCs, PC);
+ fprintf(stderr, "%08X", PC);
// Print the parameters. Assume there are four.
fprintf(stderr, " (0x%08X 0x%08X 0x%08X 0x%08X)", StackFrame.Params[0],
// Verify the PC belongs to a module in this process.
if (!SymGetModuleBase(hProcess, PC)) {
- fputc('\n', stderr);
+ fputs(" <unknown module>\n", stderr);
continue;
}
fputc('\n', stderr);
}
- }
- catch (...)
- {
+ } catch (...) {
assert(!"Crashed in LLVMUnhandledExceptionFilter");
}
// Allow dialog box to pop up allowing choice to start debugger.
- return EXCEPTION_CONTINUE_SEARCH;
+ if (OldFilter)
+ return (*OldFilter)(ep);
+ else
+ return EXCEPTION_CONTINUE_SEARCH;
}
static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
+ // We are running in our very own thread, courtesy of Windows.
+ EnterCriticalSection(&CriticalSection);
Cleanup();
- // Allow normal processing to take place.
+ // 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