Provide hooks for libgcc symbols' address resolution inside lli on mingw32.
[oota-llvm.git] / lib / System / Win32 / Signals.inc
index 1b6202a9db14b9e4c1561989a395232b4d861557..02dab86f22d8494426fe7adee1208a86b2b7f03d 100644 (file)
@@ -2,8 +2,8 @@
 // 
 //                     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;
@@ -67,35 +80,53 @@ 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)
-    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
@@ -105,6 +136,12 @@ void sys::PrintStackTraceOnErrorSignal() {
   LeaveCriticalSection(&CriticalSection);
 }
 
+
+void sys::SetInterruptFunction(void (*IF)()) {
+  RegisterHandler();
+  InterruptFunction = IF;
+  LeaveCriticalSection(&CriticalSection);
+}
 }
 
 static void Cleanup() {
@@ -119,7 +156,7 @@ static void Cleanup() {
   if (FilesToRemove != NULL)
     while (!FilesToRemove->empty()) {
       try {
-        std::remove(FilesToRemove->back().c_str());
+        FilesToRemove->back().eraseFromDisk();
       } catch (...) {
       }
       FilesToRemove->pop_back();
@@ -128,7 +165,7 @@ static void Cleanup() {
   if (DirectoriesToRemove != NULL)
     while (!DirectoriesToRemove->empty()) {
       try {
-        DirectoriesToRemove->back().destroy_directory(true);
+        DirectoriesToRemove->back().eraseFromDisk(true);
       } catch (...) {
       }
       DirectoriesToRemove->pop_back();
@@ -140,7 +177,11 @@ static void Cleanup() {
 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));
@@ -214,6 +255,9 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 
       fputc('\n', stderr);
     }
+
+#endif
+
   } catch (...) {
       assert(!"Crashed in LLVMUnhandledExceptionFilter");
   }
@@ -226,10 +270,25 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 }
 
 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