For PR789:
[oota-llvm.git] / lib / System / Win32 / Signals.inc
index d1bd2e84e043aa1b3813509c21151ad80c1eb092..8adf7674faf387077162845395d567d9c7add8e2 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #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 {
@@ -39,8 +58,10 @@ 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.
@@ -51,7 +72,7 @@ static void RegisterHandler() {
   EnterCriticalSection(&CriticalSection);
 
   RegisteredUnhandledExceptionFilter = true;
-  SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
+  OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter);
   SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE);
 
   // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or
@@ -59,29 +80,50 @@ static void RegisterHandler() {
 }
 
 // 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
@@ -91,15 +133,27 @@ void sys::PrintStackTraceOnErrorSignal() {
   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();
@@ -108,7 +162,7 @@ static void Cleanup() {
   if (DirectoriesToRemove != NULL)
     while (!DirectoriesToRemove->empty()) {
       try {
-        DirectoriesToRemove->back().destroy_directory(true);
+        DirectoriesToRemove->back().eraseFromDisk(true);
       } catch (...) {
       }
       DirectoriesToRemove->pop_back();
@@ -137,7 +191,7 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 
     // 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,
@@ -151,7 +205,7 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 
       // 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],
@@ -159,7 +213,7 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 
       // Verify the PC belongs to a module in this process.
       if (!SymGetModuleBase(hProcess, PC)) {
-        fputc('\n', stderr);
+        fputs(" <unknown module>\n", stderr);
         continue;
       }
 
@@ -194,21 +248,37 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 
       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