| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file/namespace contains utility functions for enumerating, ending and | 5 // This file/namespace contains utility functions for enumerating, ending and |
| 6 // computing statistics of processes. | 6 // computing statistics of processes. |
| 7 | 7 |
| 8 #ifndef BASE_PROCESS_UTIL_H_ | 8 #ifndef BASE_PROCESS_UTIL_H_ |
| 9 #define BASE_PROCESS_UTIL_H_ | 9 #define BASE_PROCESS_UTIL_H_ |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 #include <list> | 30 #include <list> |
| 31 #include <set> | 31 #include <set> |
| 32 #include <string> | 32 #include <string> |
| 33 #include <utility> | 33 #include <utility> |
| 34 #include <vector> | 34 #include <vector> |
| 35 | 35 |
| 36 #include "base/base_export.h" | 36 #include "base/base_export.h" |
| 37 #include "base/files/file_path.h" | 37 #include "base/files/file_path.h" |
| 38 #include "base/process.h" | 38 #include "base/process.h" |
| 39 #include "base/process/memory.h" |
| 39 #include "base/process/process_iterator.h" | 40 #include "base/process/process_iterator.h" |
| 40 #include "base/process/process_metrics.h" | 41 #include "base/process/process_metrics.h" |
| 41 | 42 |
| 42 #if defined(OS_POSIX) | 43 #if defined(OS_POSIX) |
| 43 #include "base/posix/file_descriptor_shuffle.h" | 44 #include "base/posix/file_descriptor_shuffle.h" |
| 44 #endif | 45 #endif |
| 45 | 46 |
| 46 class CommandLine; | 47 class CommandLine; |
| 47 | 48 |
| 48 namespace base { | 49 namespace base { |
| 49 | 50 |
| 50 // Return status values from GetTerminationStatus. Don't use these as | 51 // Return status values from GetTerminationStatus. Don't use these as |
| 51 // exit code arguments to KillProcess*(), use platform/application | 52 // exit code arguments to KillProcess*(), use platform/application |
| 52 // specific values instead. | 53 // specific values instead. |
| 53 enum TerminationStatus { | 54 enum TerminationStatus { |
| 54 TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status | 55 TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status |
| 55 TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status | 56 TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status |
| 56 TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill | 57 TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill |
| 57 TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault | 58 TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault |
| 58 TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet | 59 TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet |
| 59 TERMINATION_STATUS_MAX_ENUM | 60 TERMINATION_STATUS_MAX_ENUM |
| 60 }; | 61 }; |
| 61 | 62 |
| 62 #if defined(USE_LINUX_BREAKPAD) | |
| 63 BASE_EXPORT extern size_t g_oom_size; | |
| 64 #endif | |
| 65 | |
| 66 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
| 67 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran | 64 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran |
| 68 // chrome. This is not thread-safe: only call from main thread. | 65 // chrome. This is not thread-safe: only call from main thread. |
| 69 BASE_EXPORT void RouteStdioToConsole(); | 66 BASE_EXPORT void RouteStdioToConsole(); |
| 70 #endif | 67 #endif |
| 71 | 68 |
| 72 // Returns the id of the current process. | 69 // Returns the id of the current process. |
| 73 BASE_EXPORT ProcessId GetCurrentProcId(); | 70 BASE_EXPORT ProcessId GetCurrentProcId(); |
| 74 | 71 |
| 75 // Returns the ProcessHandle of the current process. | 72 // Returns the ProcessHandle of the current process. |
| 76 BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); | 73 BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); |
| 77 | 74 |
| 78 #if defined(OS_WIN) | |
| 79 // Returns the module handle to which an address belongs. The reference count | |
| 80 // of the module is not incremented. | |
| 81 BASE_EXPORT HMODULE GetModuleFromAddress(void* address); | |
| 82 #endif | |
| 83 | |
| 84 // Converts a PID to a process handle. This handle must be closed by | 75 // Converts a PID to a process handle. This handle must be closed by |
| 85 // CloseProcessHandle when you are done with it. Returns true on success. | 76 // CloseProcessHandle when you are done with it. Returns true on success. |
| 86 BASE_EXPORT bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); | 77 BASE_EXPORT bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); |
| 87 | 78 |
| 88 // Converts a PID to a process handle. On Windows the handle is opened | 79 // Converts a PID to a process handle. On Windows the handle is opened |
| 89 // with more access rights and must only be used by trusted code. | 80 // with more access rights and must only be used by trusted code. |
| 90 // You have to close returned handle using CloseProcessHandle. Returns true | 81 // You have to close returned handle using CloseProcessHandle. Returns true |
| 91 // on success. | 82 // on success. |
| 92 // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the | 83 // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the |
| 93 // more specific OpenProcessHandleWithAccess method and delete this. | 84 // more specific OpenProcessHandleWithAccess method and delete this. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 113 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); | 104 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); |
| 114 #endif | 105 #endif |
| 115 | 106 |
| 116 #if defined(OS_LINUX) || defined(OS_ANDROID) | 107 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 117 // Get the number of threads of |process| as available in /proc/<pid>/stat. | 108 // Get the number of threads of |process| as available in /proc/<pid>/stat. |
| 118 // This should be used with care as no synchronization with running threads is | 109 // This should be used with care as no synchronization with running threads is |
| 119 // done. This is mostly useful to guarantee being single-threaded. | 110 // done. This is mostly useful to guarantee being single-threaded. |
| 120 // Returns 0 on failure. | 111 // Returns 0 on failure. |
| 121 BASE_EXPORT int GetNumberOfThreads(ProcessHandle process); | 112 BASE_EXPORT int GetNumberOfThreads(ProcessHandle process); |
| 122 | 113 |
| 123 // The maximum allowed value for the OOM score. | |
| 124 const int kMaxOomScore = 1000; | |
| 125 | |
| 126 // This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will | |
| 127 // prefer to kill certain process types over others. The range for the | |
| 128 // adjustment is [-1000, 1000], with [0, 1000] being user accessible. | |
| 129 // If the Linux system doesn't support the newer oom_score_adj range | |
| 130 // of [0, 1000], then we revert to using the older oom_adj, and | |
| 131 // translate the given value into [0, 15]. Some aliasing of values | |
| 132 // may occur in that case, of course. | |
| 133 BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score); | |
| 134 | |
| 135 // /proc/self/exe refers to the current executable. | 114 // /proc/self/exe refers to the current executable. |
| 136 BASE_EXPORT extern const char kProcSelfExe[]; | 115 BASE_EXPORT extern const char kProcSelfExe[]; |
| 137 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 116 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 138 | 117 |
| 139 #if defined(OS_POSIX) | 118 #if defined(OS_POSIX) |
| 140 // Returns the ID for the parent of the given process. | 119 // Returns the ID for the parent of the given process. |
| 141 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); | 120 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); |
| 142 | 121 |
| 143 // Returns the maximum number of file descriptors that can be open by a process | 122 // Returns the maximum number of file descriptors that can be open by a process |
| 144 // at once. If the number is unavailable, a conservative best guess is returned. | 123 // at once. If the number is unavailable, a conservative best guess is returned. |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 // and SYNCHRONIZE permissions. | 447 // and SYNCHRONIZE permissions. |
| 469 // | 448 // |
| 470 BASE_EXPORT void EnsureProcessTerminated(ProcessHandle process_handle); | 449 BASE_EXPORT void EnsureProcessTerminated(ProcessHandle process_handle); |
| 471 | 450 |
| 472 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 451 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 473 // The nicer version of EnsureProcessTerminated() that is patient and will | 452 // The nicer version of EnsureProcessTerminated() that is patient and will |
| 474 // wait for |process_handle| to finish and then reap it. | 453 // wait for |process_handle| to finish and then reap it. |
| 475 BASE_EXPORT void EnsureProcessGetsReaped(ProcessHandle process_handle); | 454 BASE_EXPORT void EnsureProcessGetsReaped(ProcessHandle process_handle); |
| 476 #endif | 455 #endif |
| 477 | 456 |
| 478 // Enables low fragmentation heap (LFH) for every heaps of this process. This | |
| 479 // won't have any effect on heaps created after this function call. It will not | |
| 480 // modify data allocated in the heaps before calling this function. So it is | |
| 481 // better to call this function early in initialization and again before | |
| 482 // entering the main loop. | |
| 483 // Note: Returns true on Windows 2000 without doing anything. | |
| 484 BASE_EXPORT bool EnableLowFragmentationHeap(); | |
| 485 | |
| 486 // Enables 'terminate on heap corruption' flag. Helps protect against heap | |
| 487 // overflow. Has no effect if the OS doesn't provide the necessary facility. | |
| 488 BASE_EXPORT void EnableTerminationOnHeapCorruption(); | |
| 489 | |
| 490 // Turns on process termination if memory runs out. | |
| 491 BASE_EXPORT void EnableTerminationOnOutOfMemory(); | |
| 492 | |
| 493 // If supported on the platform, and the user has sufficent rights, increase | 457 // If supported on the platform, and the user has sufficent rights, increase |
| 494 // the current process's scheduling priority to a high priority. | 458 // the current process's scheduling priority to a high priority. |
| 495 BASE_EXPORT void RaiseProcessToHighPriority(); | 459 BASE_EXPORT void RaiseProcessToHighPriority(); |
| 496 | 460 |
| 497 #if defined(OS_MACOSX) | 461 #if defined(OS_MACOSX) |
| 498 // Restore the default exception handler, setting it to Apple Crash Reporter | 462 // Restore the default exception handler, setting it to Apple Crash Reporter |
| 499 // (ReportCrash). When forking and execing a new process, the child will | 463 // (ReportCrash). When forking and execing a new process, the child will |
| 500 // inherit the parent's exception ports, which may be set to the Breakpad | 464 // inherit the parent's exception ports, which may be set to the Breakpad |
| 501 // instance running inside the parent. The parent's Breakpad instance should | 465 // instance running inside the parent. The parent's Breakpad instance should |
| 502 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 466 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 503 // in the child after forking will restore the standard exception handler. | 467 // in the child after forking will restore the standard exception handler. |
| 504 // See http://crbug.com/20371/ for more details. | 468 // See http://crbug.com/20371/ for more details. |
| 505 void RestoreDefaultExceptionHandler(); | 469 void RestoreDefaultExceptionHandler(); |
| 506 #endif // defined(OS_MACOSX) | 470 #endif // defined(OS_MACOSX) |
| 507 | 471 |
| 508 #if defined(OS_MACOSX) | |
| 509 // Very large images or svg canvases can cause huge mallocs. Skia | |
| 510 // does tricks on tcmalloc-based systems to allow malloc to fail with | |
| 511 // a NULL rather than hit the oom crasher. This replicates that for | |
| 512 // OSX. | |
| 513 // | |
| 514 // IF YOU USE THIS WITHOUT CONSULTING YOUR FRIENDLY OSX DEVELOPER, | |
| 515 // YOUR CODE IS LIKELY TO BE REVERTED. THANK YOU. | |
| 516 // | |
| 517 // TODO(shess): Weird place to put it, but this is where the OOM | |
| 518 // killer currently lives. | |
| 519 BASE_EXPORT void* UncheckedMalloc(size_t size); | |
| 520 #endif // defined(OS_MACOSX) | |
| 521 | |
| 522 } // namespace base | 472 } // namespace base |
| 523 | 473 |
| 524 #endif // BASE_PROCESS_UTIL_H_ | 474 #endif // BASE_PROCESS_UTIL_H_ |
| OLD | NEW |