OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process_util.h" | 5 #include "base/process_util.h" |
6 | 6 |
| 7 #include <fcntl.h> |
| 8 #include <io.h> |
7 #include <windows.h> | 9 #include <windows.h> |
8 #include <winternl.h> | 10 #include <winternl.h> |
9 #include <psapi.h> | 11 #include <psapi.h> |
10 | 12 |
| 13 #include <ios> |
| 14 |
| 15 #include "base/debug_util.h" |
11 #include "base/histogram.h" | 16 #include "base/histogram.h" |
12 #include "base/logging.h" | 17 #include "base/logging.h" |
13 #include "base/scoped_handle_win.h" | 18 #include "base/scoped_handle_win.h" |
14 #include "base/scoped_ptr.h" | 19 #include "base/scoped_ptr.h" |
15 | 20 |
| 21 namespace base { |
| 22 |
16 namespace { | 23 namespace { |
17 | 24 |
18 // System pagesize. This value remains constant on x86/64 architectures. | 25 // System pagesize. This value remains constant on x86/64 architectures. |
19 const int PAGESIZE_KB = 4; | 26 const int PAGESIZE_KB = 4; |
20 | 27 |
21 // HeapSetInformation function pointer. | 28 // HeapSetInformation function pointer. |
22 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 29 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
23 | 30 |
| 31 // Previous unhandled filter. Will be called if not NULL when we intercept an |
| 32 // exception. Only used in unit tests. |
| 33 LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL; |
| 34 |
| 35 // Prints the exception call stack. |
| 36 // This is the unit tests exception filter. |
| 37 long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { |
| 38 StackTrace(info).PrintBacktrace(); |
| 39 if (g_previous_filter) |
| 40 return g_previous_filter(info); |
| 41 return EXCEPTION_CONTINUE_SEARCH; |
| 42 } |
| 43 |
| 44 // Connects back to a console if available. |
| 45 // Only necessary on Windows, no-op on other platforms. |
| 46 void AttachToConsole() { |
| 47 if (!AttachConsole(ATTACH_PARENT_PROCESS)) { |
| 48 unsigned int result = GetLastError(); |
| 49 // Was probably already attached. |
| 50 if (result == ERROR_ACCESS_DENIED) |
| 51 return; |
| 52 |
| 53 if (result == ERROR_INVALID_HANDLE || result == ERROR_INVALID_HANDLE) { |
| 54 // TODO(maruel): Walk up the process chain if deemed necessary. |
| 55 } |
| 56 // Continue even if the function call fails. |
| 57 AllocConsole(); |
| 58 } |
| 59 // http://support.microsoft.com/kb/105305 |
| 60 int raw_out = _open_osfhandle( |
| 61 reinterpret_cast<intptr_t>(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT); |
| 62 *stdout = *_fdopen(raw_out, "w"); |
| 63 setvbuf(stdout, NULL, _IONBF, 0); |
| 64 |
| 65 int raw_err = _open_osfhandle( |
| 66 reinterpret_cast<intptr_t>(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT); |
| 67 *stderr = *_fdopen(raw_err, "w"); |
| 68 setvbuf(stderr, NULL, _IONBF, 0); |
| 69 |
| 70 int raw_in = _open_osfhandle( |
| 71 reinterpret_cast<intptr_t>(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT); |
| 72 *stdin = *_fdopen(raw_in, "r"); |
| 73 setvbuf(stdin, NULL, _IONBF, 0); |
| 74 // Fix all cout, wcout, cin, wcin, cerr, wcerr, clog and wclog. |
| 75 std::ios::sync_with_stdio(); |
| 76 } |
| 77 |
24 } // namespace | 78 } // namespace |
25 | 79 |
26 namespace base { | |
27 | |
28 ProcessId GetCurrentProcId() { | 80 ProcessId GetCurrentProcId() { |
29 return ::GetCurrentProcessId(); | 81 return ::GetCurrentProcessId(); |
30 } | 82 } |
31 | 83 |
32 ProcessHandle GetCurrentProcessHandle() { | 84 ProcessHandle GetCurrentProcessHandle() { |
33 return ::GetCurrentProcess(); | 85 return ::GetCurrentProcess(); |
34 } | 86 } |
35 | 87 |
36 bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle) { | 88 bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle) { |
37 // We try to limit privileges granted to the handle. If you need this | 89 // We try to limit privileges granted to the handle. If you need this |
(...skipping 718 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
756 sizeof(lfh_flag)); | 808 sizeof(lfh_flag)); |
757 } | 809 } |
758 return true; | 810 return true; |
759 } | 811 } |
760 | 812 |
761 void EnableTerminationOnHeapCorruption() { | 813 void EnableTerminationOnHeapCorruption() { |
762 // Ignore the result code. Supported on XP SP3 and Vista. | 814 // Ignore the result code. Supported on XP SP3 and Vista. |
763 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 815 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
764 } | 816 } |
765 | 817 |
| 818 bool EnableInProcessStackDumping() { |
| 819 // Add stack dumping support on exception on windows. Similar to OS_POSIX |
| 820 // signal() handling in process_util_posix.cc. |
| 821 g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter); |
| 822 AttachToConsole(); |
| 823 return true; |
| 824 } |
| 825 |
766 void RaiseProcessToHighPriority() { | 826 void RaiseProcessToHighPriority() { |
767 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); | 827 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); |
768 } | 828 } |
769 | 829 |
770 } // namespace base | 830 } // namespace base |
OLD | NEW |