Index: base/process_util_win.cc |
diff --git a/base/process_util_win.cc b/base/process_util_win.cc |
index c5ba392920ec5bed6489445da3355b74c4f8b3f1..bbe04d26a0d103330b058c9df16dfc2c9676e653 100644 |
--- a/base/process_util_win.cc |
+++ b/base/process_util_win.cc |
@@ -69,39 +69,6 @@ long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { |
return EXCEPTION_CONTINUE_SEARCH; |
} |
-// Connects back to a console if available. |
-void AttachToConsole() { |
- if (!AttachConsole(ATTACH_PARENT_PROCESS)) { |
- unsigned int result = GetLastError(); |
- // Was probably already attached. |
- if (result == ERROR_ACCESS_DENIED) |
- return; |
- |
- if (result == ERROR_INVALID_HANDLE || result == ERROR_INVALID_HANDLE) { |
- // TODO(maruel): Walk up the process chain if deemed necessary. |
- } |
- // Continue even if the function call fails. |
- AllocConsole(); |
- } |
- // http://support.microsoft.com/kb/105305 |
- int raw_out = _open_osfhandle( |
- reinterpret_cast<intptr_t>(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT); |
- *stdout = *_fdopen(raw_out, "w"); |
- setvbuf(stdout, NULL, _IONBF, 0); |
- |
- int raw_err = _open_osfhandle( |
- reinterpret_cast<intptr_t>(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT); |
- *stderr = *_fdopen(raw_err, "w"); |
- setvbuf(stderr, NULL, _IONBF, 0); |
- |
- int raw_in = _open_osfhandle( |
- reinterpret_cast<intptr_t>(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT); |
- *stdin = *_fdopen(raw_in, "r"); |
- setvbuf(stdin, NULL, _IONBF, 0); |
- // Fix all cout, wcout, cin, wcin, cerr, wcerr, clog and wclog. |
- std::ios::sync_with_stdio(); |
-} |
- |
void OnNoMemory() { |
// Kill the process. This is important for security, since WebKit doesn't |
// NULL-check many memory allocations. If a malloc fails, returns NULL, and |
@@ -167,6 +134,31 @@ void TimerExpiredTask::KillProcess() { |
} // namespace |
+void RouteStdioToConsole() { |
+ if (!AttachConsole(ATTACH_PARENT_PROCESS)) { |
+ unsigned int result = GetLastError(); |
+ // Was probably already attached. |
+ if (result == ERROR_ACCESS_DENIED) |
+ return; |
+ // Don't bother creating a new console for each child process if the |
+ // parent process is invalid (eg: crashed). |
+ if (result == ERROR_GEN_FAILURE) |
+ return; |
+ // Make a new console if attaching to parent fails with any other error. |
+ // It should be ERROR_INVALID_HANDLE at this point, which means the browser |
+ // was likely not started from a console. |
+ AllocConsole(); |
+ } |
+ |
+ if (freopen("CONOUT$", "w", stdout)) |
+ setvbuf(stdout, NULL, _IONBF, 0); |
+ if (freopen("CONERR$", "w", stderr)) |
+ setvbuf(stderr, NULL, _IONBF, 0); |
+ |
+ // Fix all cout, wcout, cin, wcin, cerr, wcerr, clog and wclog. |
+ std::ios::sync_with_stdio(); |
+} |
+ |
ProcessId GetCurrentProcId() { |
return ::GetCurrentProcessId(); |
} |
@@ -954,7 +946,7 @@ bool EnableInProcessStackDumping() { |
// Add stack dumping support on exception on windows. Similar to OS_POSIX |
// signal() handling in process_util_posix.cc. |
g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter); |
- AttachToConsole(); |
+ RouteStdioToConsole(); |
return true; |
} |