Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: runtime/bin/platform_win.cc

Issue 2976063002: Add top-level exception handler on Windows to dump stack traces (Closed)
Patch Set: Added more comments and restoring of console Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | runtime/vm/profiler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(HOST_OS_WINDOWS) 6 #if defined(HOST_OS_WINDOWS)
7 7
8 #include "bin/platform.h" 8 #include "bin/platform.h"
9 9
10 #include <crtdbg.h> 10 #include <crtdbg.h>
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 // See: https://msdn.microsoft.com/en-us/library/1y71x448.aspx 55 // See: https://msdn.microsoft.com/en-us/library/1y71x448.aspx
56 _CrtSetReportMode(_CRT_ASSERT, 0); 56 _CrtSetReportMode(_CRT_ASSERT, 0);
57 // Disable dialog boxes for "critical" errors or when OpenFile cannot find 57 // Disable dialog boxes for "critical" errors or when OpenFile cannot find
58 // the requested file. See: 58 // the requested file. See:
59 // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v= vs.85).aspx 59 // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v= vs.85).aspx
60 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); 60 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
61 // Set up a signal handler that restores the console state on a 61 // Set up a signal handler that restores the console state on a
62 // CTRL_C_EVENT signal. This will only run when there is no signal handler 62 // CTRL_C_EVENT signal. This will only run when there is no signal handler
63 // registered for the CTRL_C_EVENT from Dart code. 63 // registered for the CTRL_C_EVENT from Dart code.
64 SetConsoleCtrlHandler(SignalHandler, TRUE); 64 SetConsoleCtrlHandler(SignalHandler, TRUE);
65 #ifndef PRODUCT
66 // Set up global exception handler to be able to dump stack trace on crash.
67 SetExceptionHandler();
68 #endif
65 } 69 }
66 70
67 static BOOL WINAPI SignalHandler(DWORD signal) { 71 static BOOL WINAPI SignalHandler(DWORD signal) {
68 if (signal == CTRL_C_EVENT) { 72 if (signal == CTRL_C_EVENT) {
69 // We call this without taking the lock because this is a signal 73 // We call this without taking the lock because this is a signal
70 // handler, and because the process is about to go down. 74 // handler, and because the process is about to go down.
71 RestoreConsoleLocked(); 75 RestoreConsoleLocked();
72 } 76 }
73 return FALSE; 77 return FALSE;
74 } 78 }
(...skipping 24 matching lines...) Expand all
99 // TODO(28984): Due to issue #29104, we cannot set 103 // TODO(28984): Due to issue #29104, we cannot set
100 // ENABLE_VIRTUAL_TERMINAL_INPUT here, as it causes ENABLE_PROCESSED_INPUT 104 // ENABLE_VIRTUAL_TERMINAL_INPUT here, as it causes ENABLE_PROCESSED_INPUT
101 // to be ignored. 105 // to be ignored.
102 } 106 }
103 107
104 static void RestoreConsole() { 108 static void RestoreConsole() {
105 MutexLocker ml(platform_win_mutex_); 109 MutexLocker ml(platform_win_mutex_);
106 RestoreConsoleLocked(); 110 RestoreConsoleLocked();
107 } 111 }
108 112
113
114 // Windows top-level unhandled exception handler function.
115 // See MSDN documentation for UnhandledExceptionFilter.
116 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms681401(v=vs.85). aspx
117 static LONG WINAPI
118 DartExceptionHandler(struct _EXCEPTION_POINTERS* ExceptionInfo) {
119 if (ExceptionInfo->ExceptionRecord->ExceptionCode ==
120 EXCEPTION_ACCESS_VIOLATION) {
121 Dart_DumpNativeStackTrace(ExceptionInfo->ContextRecord);
122 RestoreConsole();
123 abort();
124 }
125 return EXCEPTION_CONTINUE_SEARCH;
126 }
127
128
129 static void SetExceptionHandler() {
130 SetUnhandledExceptionFilter(DartExceptionHandler);
131 }
132
109 private: 133 private:
110 static Mutex* platform_win_mutex_; 134 static Mutex* platform_win_mutex_;
111 static int saved_output_cp_; 135 static int saved_output_cp_;
112 static int saved_input_cp_; 136 static int saved_input_cp_;
113 137
114 static void RestoreConsoleLocked() { 138 static void RestoreConsoleLocked() {
115 // STD_OUTPUT_HANDLE and STD_INPUT_HANDLE may have been closed or 139 // STD_OUTPUT_HANDLE and STD_INPUT_HANDLE may have been closed or
116 // redirected. Therefore, we explicitly open the CONOUT$ and CONIN$ 140 // redirected. Therefore, we explicitly open the CONOUT$ and CONIN$
117 // devices, so that we can be sure that we are really unsetting 141 // devices, so that we can be sure that we are really unsetting
118 // ENABLE_VIRTUAL_TERMINAL_PROCESSING and ENABLE_VIRTUAL_TERMINAL_INPUT 142 // ENABLE_VIRTUAL_TERMINAL_PROCESSING and ENABLE_VIRTUAL_TERMINAL_INPUT
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 PlatformWin::RestoreConsole(); 316 PlatformWin::RestoreConsole();
293 // On Windows we use ExitProcess so that threads can't clobber the exit_code. 317 // On Windows we use ExitProcess so that threads can't clobber the exit_code.
294 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 318 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870
295 ::ExitProcess(exit_code); 319 ::ExitProcess(exit_code);
296 } 320 }
297 321
298 } // namespace bin 322 } // namespace bin
299 } // namespace dart 323 } // namespace dart
300 324
301 #endif // defined(HOST_OS_WINDOWS) 325 #endif // defined(HOST_OS_WINDOWS)
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/profiler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698