diff options
author | Reid Spencer <rspencer@reidspencer.com> | 2004-09-19 05:37:39 +0000 |
---|---|---|
committer | Reid Spencer <rspencer@reidspencer.com> | 2004-09-19 05:37:39 +0000 |
commit | 7b60a15abc155196d9515a1e242ec0137afff753 (patch) | |
tree | f52a501d1cbdffb60a0bc122e6e020c3872f1e21 /lib/System | |
parent | f5afcabff887c2e9023f0c69c44f1de15b5c4347 (diff) | |
download | external_llvm-7b60a15abc155196d9515a1e242ec0137afff753.zip external_llvm-7b60a15abc155196d9515a1e242ec0137afff753.tar.gz external_llvm-7b60a15abc155196d9515a1e242ec0137afff753.tar.bz2 |
Minor correction to Signals implementation.
Patch submitted by Jeff Cohen. Thanks Jeff!
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16401 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/System')
-rw-r--r-- | lib/System/Win32/Signals.cpp | 38 | ||||
-rw-r--r-- | lib/System/Win32/Signals.inc | 38 |
2 files changed, 48 insertions, 28 deletions
diff --git a/lib/System/Win32/Signals.cpp b/lib/System/Win32/Signals.cpp index 130d3d6..1b6202a 100644 --- a/lib/System/Win32/Signals.cpp +++ b/lib/System/Win32/Signals.cpp @@ -28,6 +28,8 @@ static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType); static std::vector<std::string> *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 @@ -43,8 +45,7 @@ namespace llvm { static void RegisterHandler() { - if (RegisteredUnhandledExceptionFilter) - { + if (RegisteredUnhandledExceptionFilter) { EnterCriticalSection(&CriticalSection); return; } @@ -58,7 +59,7 @@ static void RegisterHandler() { EnterCriticalSection(&CriticalSection); RegisteredUnhandledExceptionFilter = true; - SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter); + OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter); SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE); // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or @@ -69,6 +70,9 @@ static void RegisterHandler() { void sys::RemoveFileOnSignal(const std::string &Filename) { RegisterHandler(); + if (CleanupExecuted) + throw std::string("Process terminating -- cannot register for removal"); + if (FilesToRemove == NULL) FilesToRemove = new std::vector<std::string>; @@ -81,6 +85,9 @@ void sys::RemoveFileOnSignal(const std::string &Filename) { void sys::RemoveDirectoryOnSignal(const sys::Path& path) { RegisterHandler(); + if (CleanupExecuted) + throw std::string("Process terminating -- cannot register for removal"); + if (path.is_directory()) { if (DirectoriesToRemove == NULL) DirectoriesToRemove = new std::vector<sys::Path>; @@ -103,6 +110,12 @@ void sys::PrintStackTraceOnErrorSignal() { 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 { @@ -144,7 +157,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, @@ -158,7 +171,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], @@ -166,7 +179,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; } @@ -201,21 +214,18 @@ 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) { - // FIXME: This handler executes on a different thread. The main thread - // is still running, potentially creating new files to be cleaned up - // in the tiny window between the call to Cleanup() and process termination. - // Also, any files currently open cannot be deleted. Cleanup(); // Allow normal processing to take place; i.e., the process dies. diff --git a/lib/System/Win32/Signals.inc b/lib/System/Win32/Signals.inc index 130d3d6..1b6202a 100644 --- a/lib/System/Win32/Signals.inc +++ b/lib/System/Win32/Signals.inc @@ -28,6 +28,8 @@ static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType); static std::vector<std::string> *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 @@ -43,8 +45,7 @@ namespace llvm { static void RegisterHandler() { - if (RegisteredUnhandledExceptionFilter) - { + if (RegisteredUnhandledExceptionFilter) { EnterCriticalSection(&CriticalSection); return; } @@ -58,7 +59,7 @@ static void RegisterHandler() { EnterCriticalSection(&CriticalSection); RegisteredUnhandledExceptionFilter = true; - SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter); + OldFilter = SetUnhandledExceptionFilter(LLVMUnhandledExceptionFilter); SetConsoleCtrlHandler(LLVMConsoleCtrlHandler, TRUE); // IMPORTANT NOTE: Caller must call LeaveCriticalSection(&CriticalSection) or @@ -69,6 +70,9 @@ static void RegisterHandler() { void sys::RemoveFileOnSignal(const std::string &Filename) { RegisterHandler(); + if (CleanupExecuted) + throw std::string("Process terminating -- cannot register for removal"); + if (FilesToRemove == NULL) FilesToRemove = new std::vector<std::string>; @@ -81,6 +85,9 @@ void sys::RemoveFileOnSignal(const std::string &Filename) { void sys::RemoveDirectoryOnSignal(const sys::Path& path) { RegisterHandler(); + if (CleanupExecuted) + throw std::string("Process terminating -- cannot register for removal"); + if (path.is_directory()) { if (DirectoriesToRemove == NULL) DirectoriesToRemove = new std::vector<sys::Path>; @@ -103,6 +110,12 @@ void sys::PrintStackTraceOnErrorSignal() { 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 { @@ -144,7 +157,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, @@ -158,7 +171,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], @@ -166,7 +179,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; } @@ -201,21 +214,18 @@ 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) { - // FIXME: This handler executes on a different thread. The main thread - // is still running, potentially creating new files to be cleaned up - // in the tiny window between the call to Cleanup() and process termination. - // Also, any files currently open cannot be deleted. Cleanup(); // Allow normal processing to take place; i.e., the process dies. |