[go: nahoru, domu]

blob: fb2eb442efcfa0456bfc61b61a68dd47171d93bc [file] [log] [blame]
erg@google.coma502bbe72011-01-07 18:06:451// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5// This file/namespace contains utility functions for enumerating, ending and
6// computing statistics of processes.
7
deanm@google.comdb717282008-08-27 13:48:038#ifndef BASE_PROCESS_UTIL_H_
9#define BASE_PROCESS_UTIL_H_
thakis@chromium.org32b76ef2010-07-26 23:08:2410#pragma once
initial.commitd7cae122008-07-26 21:49:3811
paulg@google.com61659062008-08-06 01:04:1812#include "base/basictypes.h"
13
dkegel@google.comab0e2222008-10-31 20:19:4314#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3815#include <windows.h>
16#include <tlhelp32.h>
mark@chromium.org817f0f142011-10-13 04:23:2217#elif defined(OS_MACOSX) || defined(OS_OPENBSD)
wtc@chromium.org4a34ce02009-08-31 22:25:0018// kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration
19// is sufficient for the vector<kinfo_proc> below.
20struct kinfo_proc;
avi@chromium.org6cfa3392010-07-01 20:11:4321// malloc_zone_t is defined in <malloc/malloc.h>, but this forward declaration
22// is sufficient for GetPurgeableZone() below.
23typedef struct _malloc_zone_t malloc_zone_t;
mark@chromium.org817f0f142011-10-13 04:23:2224#if !defined(OS_OPENBSD)
thakis@chromium.orgc0028792010-01-12 00:39:1525#include <mach/mach.h>
mark@chromium.org817f0f142011-10-13 04:23:2226#endif
wtc@chromium.org4a34ce02009-08-31 22:25:0027#elif defined(OS_POSIX)
dkegel@google.comab0e2222008-10-31 20:19:4328#include <dirent.h>
29#include <limits.h>
30#include <sys/types.h>
paulg@google.com61659062008-08-06 01:04:1831#endif
initial.commitd7cae122008-07-26 21:49:3832
maruel@chromium.orgb6128aa2010-04-29 17:44:4233#include <list>
mcgrathr@chromium.org77540fdf2011-11-15 18:49:1034#include <set>
paulg@google.com61659062008-08-06 01:04:1835#include <string>
thestig@chromium.org9ec8db02009-07-21 07:00:1336#include <utility>
mark@chromium.org962dd312009-02-05 21:44:1337#include <vector>
paulg@google.com61659062008-08-06 01:04:1838
darin@chromium.org0bea7252011-08-05 15:34:0039#include "base/base_export.h"
maruel@chromium.orga5a00b1d2010-04-08 15:52:4540#include "base/file_descriptor_shuffle.h"
avi@chromium.org4f260d02010-12-23 18:35:4241#include "base/file_path.h"
initial.commitd7cae122008-07-26 21:49:3842#include "base/process.h"
43
erg@google.com5d91c9e2010-07-28 17:25:2844class CommandLine;
erg@google.com5d91c9e2010-07-28 17:25:2845
maruel@chromium.orga5a00b1d2010-04-08 15:52:4546namespace base {
47
paulg@google.com61659062008-08-06 01:04:1848#if defined(OS_WIN)
maruel@chromium.orga5a00b1d2010-04-08 15:52:4549struct ProcessEntry : public PROCESSENTRY32 {
maruel@chromium.orgb6128aa2010-04-29 17:44:4250 ProcessId pid() const { return th32ProcessID; }
51 ProcessId parent_pid() const { return th32ParentProcessID; }
52 const wchar_t* exe_file() const { return szExeFile; }
maruel@chromium.orga5a00b1d2010-04-08 15:52:4553};
maruel@chromium.orgb6128aa2010-04-29 17:44:4254
maruel@chromium.orga5a00b1d2010-04-08 15:52:4555struct IoCounters : public IO_COUNTERS {
56};
57
sanjeevr@chromium.org7d11f6d52010-10-12 21:44:2358// Process access masks. These constants provide platform-independent
59// definitions for the standard Windows access masks.
60// See http://msdn.microsoft.com/en-us/library/ms684880(VS.85).aspx for
61// the specific semantics of each mask value.
62const uint32 kProcessAccessTerminate = PROCESS_TERMINATE;
63const uint32 kProcessAccessCreateThread = PROCESS_CREATE_THREAD;
64const uint32 kProcessAccessSetSessionId = PROCESS_SET_SESSIONID;
65const uint32 kProcessAccessVMOperation = PROCESS_VM_OPERATION;
66const uint32 kProcessAccessVMRead = PROCESS_VM_READ;
67const uint32 kProcessAccessVMWrite = PROCESS_VM_WRITE;
68const uint32 kProcessAccessDuplicateHandle = PROCESS_DUP_HANDLE;
69const uint32 kProcessAccessCreateProcess = PROCESS_CREATE_PROCESS;
70const uint32 kProcessAccessSetQuota = PROCESS_SET_QUOTA;
71const uint32 kProcessAccessSetInformation = PROCESS_SET_INFORMATION;
72const uint32 kProcessAccessQueryInformation = PROCESS_QUERY_INFORMATION;
73const uint32 kProcessAccessSuspendResume = PROCESS_SUSPEND_RESUME;
74const uint32 kProcessAccessQueryLimitedInfomation =
75 PROCESS_QUERY_LIMITED_INFORMATION;
76const uint32 kProcessAccessWaitForTermination = SYNCHRONIZE;
paulg@google.com61659062008-08-06 01:04:1877#elif defined(OS_POSIX)
maruel@chromium.orga5a00b1d2010-04-08 15:52:4578
dkegel@google.comab0e2222008-10-31 20:19:4379struct ProcessEntry {
erg@google.combbf94a32010-10-13 17:44:1580 ProcessEntry();
81 ~ProcessEntry();
82
maruel@chromium.orgb6128aa2010-04-29 17:44:4283 ProcessId pid() const { return pid_; }
84 ProcessId parent_pid() const { return ppid_; }
rsimha@chromium.orgc47d81d2010-10-05 23:41:0485 ProcessId gid() const { return gid_; }
maruel@chromium.orgb6128aa2010-04-29 17:44:4286 const char* exe_file() const { return exe_file_.c_str(); }
rsimha@chromium.orgc47d81d2010-10-05 23:41:0487 const std::vector<std::string>& cmd_line_args() const {
88 return cmd_line_args_;
89 }
erg@google.coma502bbe72011-01-07 18:06:4590
91 ProcessId pid_;
92 ProcessId ppid_;
93 ProcessId gid_;
94 std::string exe_file_;
95 std::vector<std::string> cmd_line_args_;
dkegel@google.comab0e2222008-10-31 20:19:4396};
97
evanm@google.com0b100bc82008-10-14 20:49:1698struct IoCounters {
evan@chromium.org34b2b002009-11-20 06:53:2899 uint64_t ReadOperationCount;
100 uint64_t WriteOperationCount;
101 uint64_t OtherOperationCount;
102 uint64_t ReadTransferCount;
103 uint64_t WriteTransferCount;
104 uint64_t OtherTransferCount;
evanm@google.com0b100bc82008-10-14 20:49:16105};
agl@chromium.org3f04f2b2009-04-30 19:40:03106
sanjeevr@chromium.org7d11f6d52010-10-12 21:44:23107// Process access masks. They are not used on Posix because access checking
108// does not happen during handle creation.
109const uint32 kProcessAccessTerminate = 0;
110const uint32 kProcessAccessCreateThread = 0;
111const uint32 kProcessAccessSetSessionId = 0;
112const uint32 kProcessAccessVMOperation = 0;
113const uint32 kProcessAccessVMRead = 0;
114const uint32 kProcessAccessVMWrite = 0;
115const uint32 kProcessAccessDuplicateHandle = 0;
116const uint32 kProcessAccessCreateProcess = 0;
117const uint32 kProcessAccessSetQuota = 0;
118const uint32 kProcessAccessSetInformation = 0;
119const uint32 kProcessAccessQueryInformation = 0;
120const uint32 kProcessAccessSuspendResume = 0;
121const uint32 kProcessAccessQueryLimitedInfomation = 0;
122const uint32 kProcessAccessWaitForTermination = 0;
maruel@chromium.orga5a00b1d2010-04-08 15:52:45123#endif // defined(OS_POSIX)
initial.commitd7cae122008-07-26 21:49:38124
gspencer@chromium.org443b80e2010-12-14 00:42:23125// Return status values from GetTerminationStatus. Don't use these as
126// exit code arguments to KillProcess*(), use platform/application
127// specific values instead.
128enum TerminationStatus {
129 TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status
130 TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status
131 TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill
132 TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault
scheib@chromium.orgbcb2c772011-04-01 20:20:53133 TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet
134 TERMINATION_STATUS_MAX_ENUM
cpu@google.comeef576f2008-11-03 23:28:06135};
136
initial.commitd7cae122008-07-26 21:49:38137// Returns the id of the current process.
darin@chromium.org0bea7252011-08-05 15:34:00138BASE_EXPORT ProcessId GetCurrentProcId();
initial.commitd7cae122008-07-26 21:49:38139
erikkay@google.com113ab132008-09-18 20:42:55140// Returns the ProcessHandle of the current process.
darin@chromium.org0bea7252011-08-05 15:34:00141BASE_EXPORT ProcessHandle GetCurrentProcessHandle();
maruel@chromium.org52a261f2009-03-03 15:01:12142
brettw@google.com5986ed22009-02-06 00:19:17143// Converts a PID to a process handle. This handle must be closed by
phajdan.jr@chromium.org6c6cc802009-04-03 17:01:36144// CloseProcessHandle when you are done with it. Returns true on success.
darin@chromium.org0bea7252011-08-05 15:34:00145BASE_EXPORT bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle);
maruel@chromium.org52a261f2009-03-03 15:01:12146
phajdan.jr@chromium.org5d438dbad2009-04-30 08:59:39147// Converts a PID to a process handle. On Windows the handle is opened
148// with more access rights and must only be used by trusted code.
149// You have to close returned handle using CloseProcessHandle. Returns true
150// on success.
sanjeevr@chromium.org7d11f6d52010-10-12 21:44:23151// TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the
152// more specific OpenProcessHandleWithAccess method and delete this.
darin@chromium.org0bea7252011-08-05 15:34:00153BASE_EXPORT bool OpenPrivilegedProcessHandle(ProcessId pid,
154 ProcessHandle* handle);
phajdan.jr@chromium.org5d438dbad2009-04-30 08:59:39155
sanjeevr@chromium.org7d11f6d52010-10-12 21:44:23156// Converts a PID to a process handle using the desired access flags. Use a
157// combination of the kProcessAccess* flags defined above for |access_flags|.
darin@chromium.org0bea7252011-08-05 15:34:00158BASE_EXPORT bool OpenProcessHandleWithAccess(ProcessId pid,
159 uint32 access_flags,
160 ProcessHandle* handle);
sanjeevr@chromium.org7d11f6d52010-10-12 21:44:23161
brettw@google.com5986ed22009-02-06 00:19:17162// Closes the process handle opened by OpenProcessHandle.
darin@chromium.org0bea7252011-08-05 15:34:00163BASE_EXPORT void CloseProcessHandle(ProcessHandle process);
erikkay@google.com113ab132008-09-18 20:42:55164
cpu@google.comeef576f2008-11-03 23:28:06165// Returns the unique ID for the specified process. This is functionally the
initial.commitd7cae122008-07-26 21:49:38166// same as Windows' GetProcessId(), but works on versions of Windows before
167// Win XP SP1 as well.
darin@chromium.org0bea7252011-08-05 15:34:00168BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
initial.commitd7cae122008-07-26 21:49:38169
robert.nagy@gmail.comea725b32011-10-25 17:43:05170#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_OPENBSD)
dkegel@google.com78c6dd62009-06-08 23:29:11171// Returns the path to the executable of the given process.
darin@chromium.org0bea7252011-08-05 15:34:00172BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
robert.nagy@gmail.comea725b32011-10-25 17:43:05173#endif
evan@chromium.orgd2ed23832009-09-19 01:57:39174
robert.nagy@gmail.comea725b32011-10-25 17:43:05175#if defined(OS_LINUX) || defined(OS_ANDROID)
evan@chromium.orgd2ed23832009-09-19 01:57:39176// Parse the data found in /proc/<pid>/stat and return the sum of the
177// CPU-related ticks. Returns -1 on parse error.
178// Exposed for testing.
darin@chromium.org0bea7252011-08-05 15:34:00179BASE_EXPORT int ParseProcStatCPU(const std::string& input);
thestig@chromium.orge5856a7a2009-12-10 02:08:10180
gspencer@google.com03eb9d22011-08-23 17:58:21181// The maximum allowed value for the OOM score.
182const int kMaxOomScore = 1000;
thestig@chromium.orge5856a7a2009-12-10 02:08:10183
gspencer@google.com03eb9d22011-08-23 17:58:21184// This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will
185// prefer to kill certain process types over others. The range for the
186// adjustment is [-1000, 1000], with [0, 1000] being user accessible.
187// If the Linux system doesn't support the newer oom_score_adj range
188// of [0, 1000], then we revert to using the older oom_adj, and
189// translate the given value into [0, 15]. Some aliasing of values
190// may occur in that case, of course.
darin@chromium.org0bea7252011-08-05 15:34:00191BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
jingzhao@chromium.org4d0f93c2011-09-29 04:43:54192#endif // defined(OS_LINUX) || defined(OS_ANDROID)
dkegel@google.com78c6dd62009-06-08 23:29:11193
jeremy@chromium.orgfa3097a6a52008-12-17 22:41:50194#if defined(OS_POSIX)
dmaclach@chromium.org56f0f262011-02-24 17:14:36195// Returns the ID for the parent of the given process.
darin@chromium.org0bea7252011-08-05 15:34:00196BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process);
dmaclach@chromium.org56f0f262011-02-24 17:14:36197
gspencer@chromium.org443b80e2010-12-14 00:42:23198// Close all file descriptors, except those which are a destination in the
agl@chromium.org3f04f2b2009-04-30 19:40:03199// given multimap. Only call this function in a child process where you know
200// that there aren't any other threads.
darin@chromium.org0bea7252011-08-05 15:34:00201BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map);
gspencer@google.com03eb9d22011-08-23 17:58:21202#endif // defined(OS_POSIX)
jeremy@chromium.orgfa3097a6a52008-12-17 22:41:50203
evan@chromium.org898a81a2011-06-30 22:56:15204// TODO(evan): rename these to use StudlyCaps.
205typedef std::vector<std::pair<std::string, std::string> > environment_vector;
206typedef std::vector<std::pair<int, int> > file_handle_mapping_vector;
207
mark@chromium.org84b2d1d2011-09-01 21:44:24208#if defined(OS_MACOSX)
209// Used with LaunchOptions::synchronize and LaunchSynchronize, a
210// LaunchSynchronizationHandle is an opaque value that LaunchProcess will
211// create and set, and that LaunchSynchronize will consume and destroy.
212typedef int* LaunchSynchronizationHandle;
213#endif // defined(OS_MACOSX)
214
evan@chromium.org075fb932011-07-19 16:31:28215// Options for launching a subprocess that are passed to LaunchProcess().
evan@chromium.orge5992182011-07-15 16:47:02216// The default constructor constructs the object with default options.
evan@chromium.org898a81a2011-06-30 22:56:15217struct LaunchOptions {
evan@chromium.orge5992182011-07-15 16:47:02218 LaunchOptions() : wait(false),
evan@chromium.org898a81a2011-06-30 22:56:15219#if defined(OS_WIN)
220 start_hidden(false), inherit_handles(false), as_user(NULL),
phajdan.jr@chromium.org86f723ec22011-09-07 21:54:42221 empty_desktop_name(false), job_handle(NULL)
evan@chromium.org898a81a2011-06-30 22:56:15222#else
mcgrathr@chromium.org77540fdf2011-11-15 18:49:10223 environ(NULL), fds_to_remap(NULL), maximize_rlimits(NULL),
224 new_process_group(false)
mark@chromium.org84b2d1d2011-09-01 21:44:24225#if defined(OS_LINUX)
226 , clone_flags(0)
227#endif // OS_LINUX
228#if defined(OS_MACOSX)
229 , synchronize(NULL)
230#endif // defined(OS_MACOSX)
gspencer@google.com03eb9d22011-08-23 17:58:21231#endif // !defined(OS_WIN)
evan@chromium.org898a81a2011-06-30 22:56:15232 {}
233
234 // If true, wait for the process to complete.
235 bool wait;
evan@chromium.org898a81a2011-06-30 22:56:15236
237#if defined(OS_WIN)
238 bool start_hidden;
239
240 // If true, the new process inherits handles from the parent.
241 bool inherit_handles;
242
243 // If non-NULL, runs as if the user represented by the token had launched it.
244 // Whether the application is visible on the interactive desktop depends on
245 // the token belonging to an interactive logon session.
246 //
247 // To avoid hard to diagnose problems, when specified this loads the
248 // environment variables associated with the user and if this operation fails
249 // the entire call fails as well.
250 UserTokenHandle as_user;
251
252 // If true, use an empty string for the desktop name.
253 bool empty_desktop_name;
phajdan.jr@chromium.org86f723ec22011-09-07 21:54:42254
255 // If non-NULL, launches the application in that job object.
256 HANDLE job_handle;
evan@chromium.org898a81a2011-06-30 22:56:15257#else
258 // If non-NULL, set/unset environment variables.
259 // See documentation of AlterEnvironment().
260 // This pointer is owned by the caller and must live through the
261 // call to LaunchProcess().
262 const environment_vector* environ;
263
264 // If non-NULL, remap file descriptors according to the mapping of
265 // src fd->dest fd to propagate FDs into the child process.
266 // This pointer is owned by the caller and must live through the
267 // call to LaunchProcess().
268 const file_handle_mapping_vector* fds_to_remap;
269
mcgrathr@chromium.org77540fdf2011-11-15 18:49:10270 // Each element is an RLIMIT_* constant that should be raised to its
271 // rlim_max. This pointer is owned by the caller and must live through
272 // the call to LaunchProcess().
273 const std::set<int>* maximize_rlimits;
274
evan@chromium.org898a81a2011-06-30 22:56:15275 // If true, start the process in a new process group, instead of
276 // inheriting the parent's process group. The pgid of the child process
277 // will be the same as its pid.
278 bool new_process_group;
bradchen@google.comca7c4562011-07-15 23:55:21279
mark@chromium.org84b2d1d2011-09-01 21:44:24280#if defined(OS_LINUX)
bradchen@google.comca7c4562011-07-15 23:55:21281 // If non-zero, start the process using clone(), using flags as provided.
282 int clone_flags;
mark@chromium.org84b2d1d2011-09-01 21:44:24283#endif // defined(OS_LINUX)
284
285#if defined(OS_MACOSX)
286 // When non-NULL, a new LaunchSynchronizationHandle will be created and
287 // stored in *synchronize whenever LaunchProcess returns true in the parent
288 // process. The child process will have been created (with fork) but will
289 // be waiting (before exec) for the parent to call LaunchSynchronize with
290 // this handle. Only when LaunchSynchronize is called will the child be
291 // permitted to continue execution and call exec. LaunchSynchronize
292 // destroys the handle created by LaunchProcess.
293 //
294 // When synchronize is non-NULL, the parent must call LaunchSynchronize
295 // whenever LaunchProcess returns true. No exceptions.
296 //
297 // Synchronization is useful when the parent process needs to guarantee that
298 // it can take some action (such as recording the newly-forked child's
299 // process ID) before the child does something (such as using its process ID
300 // to communicate with its parent).
301 //
302 // |synchronize| and |wait| must not both be set simultaneously.
303 LaunchSynchronizationHandle* synchronize;
304#endif // defined(OS_MACOSX)
305
306#endif // !defined(OS_WIN)
evan@chromium.org898a81a2011-06-30 22:56:15307};
308
309// Launch a process via the command line |cmdline|.
evan@chromium.orge5992182011-07-15 16:47:02310// See the documentation of LaunchOptions for details on |options|.
311//
312// If |process_handle| is non-NULL, it will be filled in with the
313// handle of the launched process. NOTE: In this case, the caller is
314// responsible for closing the handle so that it doesn't leak!
315// Otherwise, the process handle will be implicitly closed.
evan@chromium.org898a81a2011-06-30 22:56:15316//
317// Unix-specific notes:
mark@chromium.org84b2d1d2011-09-01 21:44:24318// - All file descriptors open in the parent process will be closed in the
319// child process except for any preserved by options::fds_to_remap, and
320// stdin, stdout, and stderr. If not remapped by options::fds_to_remap,
321// stdin is reopened as /dev/null, and the child is allowed to inherit its
322// parent's stdout and stderr.
evan@chromium.org898a81a2011-06-30 22:56:15323// - If the first argument on the command line does not contain a slash,
324// PATH will be searched. (See man execvp.)
darin@chromium.org0bea7252011-08-05 15:34:00325BASE_EXPORT bool LaunchProcess(const CommandLine& cmdline,
326 const LaunchOptions& options,
327 ProcessHandle* process_handle);
evan@chromium.org898a81a2011-06-30 22:56:15328
estade@chromium.orgfb7f9be2008-10-22 01:15:47329#if defined(OS_WIN)
tommi@chromium.org48dc9e12010-08-26 19:49:57330
331enum IntegrityLevel {
332 INTEGRITY_UNKNOWN,
333 LOW_INTEGRITY,
334 MEDIUM_INTEGRITY,
335 HIGH_INTEGRITY,
336};
337// Determine the integrity level of the specified process. Returns false
338// if the system does not support integrity levels (pre-Vista) or in the case
339// of an underlying system failure.
darin@chromium.org0bea7252011-08-05 15:34:00340BASE_EXPORT bool GetProcessIntegrityLevel(ProcessHandle process,
341 IntegrityLevel *level);
tommi@chromium.org48dc9e12010-08-26 19:49:57342
evan@chromium.org898a81a2011-06-30 22:56:15343// Windows-specific LaunchProcess that takes the command line as a
344// string. Useful for situations where you need to control the
345// command line arguments directly, but prefer the CommandLine version
346// if launching Chrome itself.
initial.commitd7cae122008-07-26 21:49:38347//
evan@chromium.org898a81a2011-06-30 22:56:15348// The first command line argument should be the path to the process,
349// and don't forget to quote it.
initial.commitd7cae122008-07-26 21:49:38350//
351// Example (including literal quotes)
352// cmdline = "c:\windows\explorer.exe" -foo "c:\bar\"
darin@chromium.org0bea7252011-08-05 15:34:00353BASE_EXPORT bool LaunchProcess(const string16& cmdline,
354 const LaunchOptions& options,
355 ProcessHandle* process_handle);
cpu@chromium.orge50130b2010-02-01 03:28:47356
estade@chromium.orgfb7f9be2008-10-22 01:15:47357#elif defined(OS_POSIX)
evan@chromium.org898a81a2011-06-30 22:56:15358// A POSIX-specific version of LaunchProcess that takes an argv array
359// instead of a CommandLine. Useful for situations where you need to
360// control the command line arguments directly, but prefer the
361// CommandLine version if launching Chrome itself.
darin@chromium.org0bea7252011-08-05 15:34:00362BASE_EXPORT bool LaunchProcess(const std::vector<std::string>& argv,
363 const LaunchOptions& options,
364 ProcessHandle* process_handle);
stuartmorgan@google.com2aea9e092009-08-06 20:03:01365
agl@chromium.orgef73044e2010-03-11 15:25:54366// AlterEnvironment returns a modified environment vector, constructed from the
367// given environment and the list of changes given in |changes|. Each key in
368// the environment is matched against the first element of the pairs. In the
369// event of a match, the value is replaced by the second of the pair, unless
370// the second is empty, in which case the key-value is removed.
371//
372// The returned array is allocated using new[] and must be freed by the caller.
darin@chromium.org0bea7252011-08-05 15:34:00373BASE_EXPORT char** AlterEnvironment(const environment_vector& changes,
374 const char* const* const env);
mark@chromium.org84b2d1d2011-09-01 21:44:24375
376#if defined(OS_MACOSX)
377
378// After a successful call to LaunchProcess with LaunchOptions::synchronize
379// set, the parent process must call LaunchSynchronize to allow the child
380// process to proceed, and to destroy the LaunchSynchronizationHandle.
381BASE_EXPORT void LaunchSynchronize(LaunchSynchronizationHandle handle);
382
383#endif // defined(OS_MACOSX)
thestig@chromium.org9ec8db02009-07-21 07:00:13384#endif // defined(OS_POSIX)
initial.commitd7cae122008-07-26 21:49:38385
jcampan@chromium.org1e312112009-04-21 21:44:12386// Executes the application specified by |cl| and wait for it to exit. Stores
phajdan.jr@chromium.org1912cfe2009-04-21 08:09:30387// the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true
388// on success (application launched and exited cleanly, with exit code
maruel@chromium.org96878a212010-06-10 18:26:33389// indicating success).
darin@chromium.org0bea7252011-08-05 15:34:00390BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output);
phajdan.jr@chromium.orgc0b210ee2009-04-17 09:57:52391
viettrungluu@chromium.orgf164cea2009-11-05 23:37:40392#if defined(OS_POSIX)
393// A restricted version of |GetAppOutput()| which (a) clears the environment,
394// and (b) stores at most |max_output| bytes; also, it doesn't search the path
395// for the command.
darin@chromium.org0bea7252011-08-05 15:34:00396BASE_EXPORT bool GetAppOutputRestricted(const CommandLine& cl,
397 std::string* output, size_t max_output);
benwells@chromium.orgd03b05c2011-07-06 06:14:48398
399// A version of |GetAppOutput()| which also returns the exit code of the
400// executed command. Returns true if the application runs and exits cleanly. If
401// this is the case the exit code of the application is available in
402// |*exit_code|.
darin@chromium.org0bea7252011-08-05 15:34:00403BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl,
404 std::string* output, int* exit_code);
gspencer@google.com03eb9d22011-08-23 17:58:21405#endif // defined(OS_POSIX)
viettrungluu@chromium.orgf164cea2009-11-05 23:37:40406
initial.commitd7cae122008-07-26 21:49:38407// Used to filter processes by process ID.
408class ProcessFilter {
409 public:
410 // Returns true to indicate set-inclusion and false otherwise. This method
411 // should not have side-effects and should be idempotent.
maruel@chromium.orgb6128aa2010-04-29 17:44:42412 virtual bool Includes(const ProcessEntry& entry) const = 0;
ziadh@chromium.org695092f2010-08-02 16:34:16413
414 protected:
415 virtual ~ProcessFilter() {}
initial.commitd7cae122008-07-26 21:49:38416};
417
418// Returns the number of processes on the machine that are running from the
419// given executable name. If filter is non-null, then only processes selected
420// by the filter will be counted.
darin@chromium.org0bea7252011-08-05 15:34:00421BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name,
422 const ProcessFilter* filter);
initial.commitd7cae122008-07-26 21:49:38423
424// Attempts to kill all the processes on the current machine that were launched
425// from the given executable name, ending them with the given exit code. If
426// filter is non-null, then only processes selected by the filter are killed.
rsimha@chromium.org61b93f88f2010-09-22 17:28:30427// Returns true if all processes were able to be killed off, false if at least
initial.commitd7cae122008-07-26 21:49:38428// one couldn't be killed.
darin@chromium.org0bea7252011-08-05 15:34:00429BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name,
430 int exit_code, const ProcessFilter* filter);
initial.commitd7cae122008-07-26 21:49:38431
432// Attempts to kill the process identified by the given process
433// entry structure, giving it the specified exit code. If |wait| is true, wait
434// for the process to be actually terminated before returning.
435// Returns true if this is successful, false otherwise.
darin@chromium.org0bea7252011-08-05 15:34:00436BASE_EXPORT bool KillProcess(ProcessHandle process, int exit_code, bool wait);
rsimha@chromium.org61b93f88f2010-09-22 17:28:30437
438#if defined(OS_POSIX)
439// Attempts to kill the process group identified by |process_group_id|. Returns
440// true on success.
darin@chromium.org0bea7252011-08-05 15:34:00441BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id);
gspencer@google.com03eb9d22011-08-23 17:58:21442#endif // defined(OS_POSIX)
rsimha@chromium.org61b93f88f2010-09-22 17:28:30443
agl@chromium.orgdfe14862009-01-22 01:23:11444#if defined(OS_WIN)
darin@chromium.org0bea7252011-08-05 15:34:00445BASE_EXPORT bool KillProcessById(ProcessId process_id, int exit_code,
446 bool wait);
gspencer@google.com03eb9d22011-08-23 17:58:21447#endif // defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38448
gspencer@chromium.org443b80e2010-12-14 00:42:23449// Get the termination status of the process by interpreting the
450// circumstances of the child process' death. |exit_code| is set to
451// the status returned by waitpid() on POSIX, and from
452// GetExitCodeProcess() on Windows. |exit_code| may be NULL if the
453// caller is not interested in it. Note that on Linux, this function
454// will only return a useful result the first time it is called after
455// the child exits (because it will reap the child and the information
456// will no longer be available).
darin@chromium.org0bea7252011-08-05 15:34:00457BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle,
458 int* exit_code);
initial.commitd7cae122008-07-26 21:49:38459
dmaclach@chromium.org56f0f262011-02-24 17:14:36460// Waits for process to exit. On POSIX systems, if the process hasn't been
phajdan.jr@chromium.orgc7856632009-01-13 17:38:49461// signaled then puts the exit code in |exit_code|; otherwise it's considered
462// a failure. On Windows |exit_code| is always filled. Returns true on success,
463// and closes |handle| in any case.
darin@chromium.org0bea7252011-08-05 15:34:00464BASE_EXPORT bool WaitForExitCode(ProcessHandle handle, int* exit_code);
phajdan.jr@chromium.orgc7856632009-01-13 17:38:49465
phajdan.jr@chromium.org8004e682010-03-16 07:41:22466// Waits for process to exit. If it did exit within |timeout_milliseconds|,
phajdan.jr@chromium.org40bbe592011-04-06 12:18:20467// then puts the exit code in |exit_code|, and returns true.
phajdan.jr@chromium.org8004e682010-03-16 07:41:22468// In POSIX systems, if the process has been signaled then |exit_code| is set
469// to -1. Returns false on failure (the caller is then responsible for closing
470// |handle|).
phajdan.jr@chromium.org40bbe592011-04-06 12:18:20471// The caller is always responsible for closing the |handle|.
darin@chromium.org0bea7252011-08-05 15:34:00472BASE_EXPORT bool WaitForExitCodeWithTimeout(ProcessHandle handle,
473 int* exit_code,
474 int64 timeout_milliseconds);
phajdan.jr@chromium.org8004e682010-03-16 07:41:22475
initial.commitd7cae122008-07-26 21:49:38476// Wait for all the processes based on the named executable to exit. If filter
477// is non-null, then only processes selected by the filter are waited on.
478// Returns after all processes have exited or wait_milliseconds have expired.
479// Returns true if all the processes exited, false otherwise.
darin@chromium.org0bea7252011-08-05 15:34:00480BASE_EXPORT bool WaitForProcessesToExit(
rvargas@google.com26fbf802011-03-25 18:48:03481 const FilePath::StringType& executable_name,
482 int64 wait_milliseconds,
483 const ProcessFilter* filter);
initial.commitd7cae122008-07-26 21:49:38484
estade@chromium.orgfb7f9be2008-10-22 01:15:47485// Wait for a single process to exit. Return true if it exited cleanly within
dmaclach@chromium.org56f0f262011-02-24 17:14:36486// the given time limit. On Linux |handle| must be a child process, however
487// on Mac and Windows it can be any process.
darin@chromium.org0bea7252011-08-05 15:34:00488BASE_EXPORT bool WaitForSingleProcess(ProcessHandle handle,
489 int64 wait_milliseconds);
phajdan.jr@chromium.orgc7c980972011-04-05 18:05:34490
initial.commitd7cae122008-07-26 21:49:38491// Waits a certain amount of time (can be 0) for all the processes with a given
492// executable name to exit, then kills off any of them that are still around.
493// If filter is non-null, then only processes selected by the filter are waited
494// on. Killed processes are ended with the given exit code. Returns false if
495// any processes needed to be killed, true if they all exited cleanly within
496// the wait_milliseconds delay.
darin@chromium.org0bea7252011-08-05 15:34:00497BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name,
498 int64 wait_milliseconds,
499 int exit_code,
500 const ProcessFilter* filter);
initial.commitd7cae122008-07-26 21:49:38501
maruel@chromium.orgb6128aa2010-04-29 17:44:42502// This class provides a way to iterate through a list of processes on the
503// current machine with a specified filter.
504// To use, create an instance and then call NextProcessEntry() until it returns
505// false.
darin@chromium.org0bea7252011-08-05 15:34:00506class BASE_EXPORT ProcessIterator {
initial.commitd7cae122008-07-26 21:49:38507 public:
maruel@chromium.orgbaead4f2010-06-11 19:10:30508 typedef std::list<ProcessEntry> ProcessEntries;
509
maruel@chromium.orgb6128aa2010-04-29 17:44:42510 explicit ProcessIterator(const ProcessFilter* filter);
511 virtual ~ProcessIterator();
initial.commitd7cae122008-07-26 21:49:38512
513 // If there's another process that matches the given executable name,
514 // returns a const pointer to the corresponding PROCESSENTRY32.
515 // If there are no more matching processes, returns NULL.
516 // The returned pointer will remain valid until NextProcessEntry()
517 // is called again or this NamedProcessIterator goes out of scope.
518 const ProcessEntry* NextProcessEntry();
519
maruel@chromium.orgb6128aa2010-04-29 17:44:42520 // Takes a snapshot of all the ProcessEntry found.
maruel@chromium.orgbaead4f2010-06-11 19:10:30521 ProcessEntries Snapshot();
maruel@chromium.orgb6128aa2010-04-29 17:44:42522
523 protected:
524 virtual bool IncludeEntry();
525 const ProcessEntry& entry() { return entry_; }
526
initial.commitd7cae122008-07-26 21:49:38527 private:
528 // Determines whether there's another process (regardless of executable)
529 // left in the list of all processes. Returns true and sets entry_ to
530 // that process's info if there is one, false otherwise.
531 bool CheckForNextProcess();
532
initial.commitd7cae122008-07-26 21:49:38533 // Initializes a PROCESSENTRY32 data structure so that it's ready for
534 // use with Process32First/Process32Next.
535 void InitProcessEntry(ProcessEntry* entry);
dkegel@google.comab0e2222008-10-31 20:19:43536
537#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38538 HANDLE snapshot_;
initial.commitd7cae122008-07-26 21:49:38539 bool started_iteration_;
mark@chromium.org817f0f142011-10-13 04:23:22540#elif defined(OS_MACOSX) || defined(OS_OPENBSD)
mark@chromium.org962dd312009-02-05 21:44:13541 std::vector<kinfo_proc> kinfo_procs_;
542 size_t index_of_kinfo_proc_;
wtc@chromium.org4a34ce02009-08-31 22:25:00543#elif defined(OS_POSIX)
544 DIR *procfs_dir_;
dkegel@google.comab0e2222008-10-31 20:19:43545#endif
initial.commitd7cae122008-07-26 21:49:38546 ProcessEntry entry_;
547 const ProcessFilter* filter_;
548
maruel@chromium.orgb6128aa2010-04-29 17:44:42549 DISALLOW_COPY_AND_ASSIGN(ProcessIterator);
550};
551
552// This class provides a way to iterate through the list of processes
553// on the current machine that were started from the given executable
554// name. To use, create an instance and then call NextProcessEntry()
555// until it returns false.
darin@chromium.org0bea7252011-08-05 15:34:00556class BASE_EXPORT NamedProcessIterator : public ProcessIterator {
maruel@chromium.orgb6128aa2010-04-29 17:44:42557 public:
avi@chromium.org4f260d02010-12-23 18:35:42558 NamedProcessIterator(const FilePath::StringType& executable_name,
maruel@chromium.orgb6128aa2010-04-29 17:44:42559 const ProcessFilter* filter);
560 virtual ~NamedProcessIterator();
561
562 protected:
563 virtual bool IncludeEntry();
564
565 private:
avi@chromium.org4f260d02010-12-23 18:35:42566 FilePath::StringType executable_name_;
maruel@chromium.orgb6128aa2010-04-29 17:44:42567
568 DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator);
initial.commitd7cae122008-07-26 21:49:38569};
570
571// Working Set (resident) memory usage broken down by
agl@chromium.org54fd1d32009-09-01 00:12:58572//
573// On Windows:
initial.commitd7cae122008-07-26 21:49:38574// priv (private): These pages (kbytes) cannot be shared with any other process.
575// shareable: These pages (kbytes) can be shared with other processes under
576// the right circumstances.
577// shared : These pages (kbytes) are currently shared with at least one
578// other process.
agl@chromium.org54fd1d32009-09-01 00:12:58579//
580// On Linux:
581// priv: Pages mapped only by this process
582// shared: PSS or 0 if the kernel doesn't support this
583// shareable: 0
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04584//
585// On OS X: TODO(thakis): Revise.
586// priv: Memory.
587// shared: 0
588// shareable: 0
initial.commitd7cae122008-07-26 21:49:38589struct WorkingSetKBytes {
agl@chromium.org54fd1d32009-09-01 00:12:58590 WorkingSetKBytes() : priv(0), shareable(0), shared(0) {}
initial.commitd7cae122008-07-26 21:49:38591 size_t priv;
592 size_t shareable;
593 size_t shared;
594};
595
596// Committed (resident + paged) memory usage broken down by
597// private: These pages cannot be shared with any other process.
598// mapped: These pages are mapped into the view of a section (backed by
599// pagefile.sys)
600// image: These pages are mapped into the view of an image section (backed by
601// file system)
602struct CommittedKBytes {
agl@chromium.org54fd1d32009-09-01 00:12:58603 CommittedKBytes() : priv(0), mapped(0), image(0) {}
initial.commitd7cae122008-07-26 21:49:38604 size_t priv;
605 size_t mapped;
606 size_t image;
607};
608
609// Free memory (Megabytes marked as free) in the 2G process address space.
610// total : total amount in megabytes marked as free. Maximum value is 2048.
611// largest : size of the largest contiguous amount of memory found. It is
612// always smaller or equal to FreeMBytes::total.
613// largest_ptr: starting address of the largest memory block.
614struct FreeMBytes {
615 size_t total;
616 size_t largest;
617 void* largest_ptr;
618};
619
evan@chromium.orgd2ed23832009-09-19 01:57:39620// Convert a POSIX timeval to microseconds.
darin@chromium.org0bea7252011-08-05 15:34:00621BASE_EXPORT int64 TimeValToMicroseconds(const struct timeval& tv);
evan@chromium.orgd2ed23832009-09-19 01:57:39622
initial.commitd7cae122008-07-26 21:49:38623// Provides performance metrics for a specified process (CPU usage, memory and
624// IO counters). To use it, invoke CreateProcessMetrics() to get an instance
625// for a specific process, then access the information with the different get
626// methods.
darin@chromium.org0bea7252011-08-05 15:34:00627class BASE_EXPORT ProcessMetrics {
initial.commitd7cae122008-07-26 21:49:38628 public:
erg@google.coma502bbe72011-01-07 18:06:45629 ~ProcessMetrics();
630
initial.commitd7cae122008-07-26 21:49:38631 // Creates a ProcessMetrics for the specified process.
632 // The caller owns the returned object.
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04633#if !defined(OS_MACOSX)
initial.commitd7cae122008-07-26 21:49:38634 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process);
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04635#else
636 class PortProvider {
637 public:
thakis@chromium.orgb2e8e082009-12-21 17:44:20638 // Should return the mach task for |process| if possible, or else
639 // |MACH_PORT_NULL|. Only processes that this returns tasks for will have
640 // metrics on OS X (except for the current process, which always gets
641 // metrics).
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04642 virtual mach_port_t TaskForPid(ProcessHandle process) const = 0;
643 };
644
645 // The port provider needs to outlive the ProcessMetrics object returned by
646 // this function. If NULL is passed as provider, the returned object
647 // only returns valid metrics if |process| is the current process.
648 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process,
649 PortProvider* port_provider);
maruel@chromium.orgb6128aa2010-04-29 17:44:42650#endif // !defined(OS_MACOSX)
initial.commitd7cae122008-07-26 21:49:38651
652 // Returns the current space allocated for the pagefile, in bytes (these pages
thomasvl@chromium.org796da7c2009-06-11 12:45:45653 // may or may not be in memory). On Linux, this returns the total virtual
654 // memory size.
phajdan.jr@chromium.orgd043c2cc2009-03-25 18:30:45655 size_t GetPagefileUsage() const;
initial.commitd7cae122008-07-26 21:49:38656 // Returns the peak space allocated for the pagefile, in bytes.
phajdan.jr@chromium.orgd043c2cc2009-03-25 18:30:45657 size_t GetPeakPagefileUsage() const;
thomasvl@chromium.org796da7c2009-06-11 12:45:45658 // Returns the current working set size, in bytes. On Linux, this returns
659 // the resident set size.
phajdan.jr@chromium.orgd043c2cc2009-03-25 18:30:45660 size_t GetWorkingSetSize() const;
tc@google.com0c557f12009-05-11 23:35:52661 // Returns the peak working set size, in bytes.
662 size_t GetPeakWorkingSetSize() const;
erg@chromium.org98947a02010-05-11 17:46:08663 // Returns private and sharedusage, in bytes. Private bytes is the amount of
664 // memory currently allocated to a process that cannot be shared. Returns
665 // false on platform specific error conditions. Note: |private_bytes|
666 // returns 0 on unsupported OSes: prior to XP SP2.
667 bool GetMemoryBytes(size_t* private_bytes,
668 size_t* shared_bytes);
initial.commitd7cae122008-07-26 21:49:38669 // Fills a CommittedKBytes with both resident and paged
670 // memory usage as per definition of CommittedBytes.
phajdan.jr@chromium.orgd043c2cc2009-03-25 18:30:45671 void GetCommittedKBytes(CommittedKBytes* usage) const;
initial.commitd7cae122008-07-26 21:49:38672 // Fills a WorkingSetKBytes containing resident private and shared memory
673 // usage in bytes, as per definition of WorkingSetBytes.
phajdan.jr@chromium.orgd043c2cc2009-03-25 18:30:45674 bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const;
initial.commitd7cae122008-07-26 21:49:38675
676 // Computes the current process available memory for allocation.
677 // It does a linear scan of the address space querying each memory region
678 // for its free (unallocated) status. It is useful for estimating the memory
679 // load and fragmentation.
phajdan.jr@chromium.orgd043c2cc2009-03-25 18:30:45680 bool CalculateFreeMemory(FreeMBytes* free) const;
initial.commitd7cae122008-07-26 21:49:38681
682 // Returns the CPU usage in percent since the last time this method was
683 // called. The first time this method is called it returns 0 and will return
684 // the actual CPU info on subsequent calls.
thakis@chromium.org022eab62010-01-13 04:55:06685 // On Windows, the CPU usage value is for all CPUs. So if you have 2 CPUs and
686 // your process is using all the cycles of 1 CPU and not the other CPU, this
687 // method returns 50.
688 double GetCPUUsage();
initial.commitd7cae122008-07-26 21:49:38689
690 // Retrieves accounting information for all I/O operations performed by the
691 // process.
692 // If IO information is retrieved successfully, the function returns true
693 // and fills in the IO_COUNTERS passed in. The function returns false
694 // otherwise.
phajdan.jr@chromium.orgd043c2cc2009-03-25 18:30:45695 bool GetIOCounters(IoCounters* io_counters) const;
initial.commitd7cae122008-07-26 21:49:38696
697 private:
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04698#if !defined(OS_MACOSX)
initial.commitd7cae122008-07-26 21:49:38699 explicit ProcessMetrics(ProcessHandle process);
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04700#else
701 ProcessMetrics(ProcessHandle process, PortProvider* port_provider);
gspencer@google.com03eb9d22011-08-23 17:58:21702#endif // defined(OS_MACOSX)
initial.commitd7cae122008-07-26 21:49:38703
704 ProcessHandle process_;
705
706 int processor_count_;
707
evan@chromium.orgd2ed23832009-09-19 01:57:39708 // Used to store the previous times and CPU usage counts so we can
709 // compute the CPU usage between calls.
initial.commitd7cae122008-07-26 21:49:38710 int64 last_time_;
711 int64 last_system_time_;
712
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04713#if defined(OS_MACOSX)
714 // Queries the port provider if it's set.
715 mach_port_t TaskForPid(ProcessHandle process) const;
716
717 PortProvider* port_provider_;
pvalchev@google.com66700d42010-03-10 07:46:43718#elif defined(OS_POSIX)
719 // Jiffie count at the last_time_ we updated.
720 int last_cpu_;
gspencer@google.com03eb9d22011-08-23 17:58:21721#endif // defined(OS_POSIX)
viettrungluu@chromium.org3740cb9b52009-12-19 04:50:04722
maruel@chromium.orgb6128aa2010-04-29 17:44:42723 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics);
initial.commitd7cae122008-07-26 21:49:38724};
725
jingzhao@chromium.org4d0f93c2011-09-29 04:43:54726#if defined(OS_LINUX) || defined(OS_ANDROID)
jamescook@chromium.orgf37661dd2011-08-11 18:10:04727// Data from /proc/meminfo about system-wide memory consumption.
728// Values are in KB.
729struct SystemMemoryInfoKB {
730 SystemMemoryInfoKB() : total(0), free(0), buffers(0), cached(0),
731 active_anon(0), inactive_anon(0), shmem(0) {}
732 int total;
733 int free;
734 int buffers;
735 int cached;
736 int active_anon;
737 int inactive_anon;
738 int shmem;
739};
jamescook@chromium.org14cb2b2d2011-08-02 16:33:24740// Retrieves data from /proc/meminfo about system-wide memory consumption.
jamescook@chromium.orgf37661dd2011-08-11 18:10:04741// Fills in the provided |meminfo| structure. Returns true on success.
742// Exposed for memory debugging widget.
743BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo);
jingzhao@chromium.org4d0f93c2011-09-29 04:43:54744#endif // defined(OS_LINUX) || defined(OS_ANDROID)
jamescook@chromium.org14cb2b2d2011-08-02 16:33:24745
746// Returns the memory committed by the system in KBytes.
sgk@chromium.orged26d942009-11-09 06:57:28747// Returns 0 if it can't compute the commit charge.
darin@chromium.org0bea7252011-08-05 15:34:00748BASE_EXPORT size_t GetSystemCommitCharge();
sgk@chromium.orged26d942009-11-09 06:57:28749
initial.commitd7cae122008-07-26 21:49:38750// Enables low fragmentation heap (LFH) for every heaps of this process. This
751// won't have any effect on heaps created after this function call. It will not
752// modify data allocated in the heaps before calling this function. So it is
753// better to call this function early in initialization and again before
754// entering the main loop.
755// Note: Returns true on Windows 2000 without doing anything.
darin@chromium.org0bea7252011-08-05 15:34:00756BASE_EXPORT bool EnableLowFragmentationHeap();
initial.commitd7cae122008-07-26 21:49:38757
maruel@chromium.orgd6fc9fd2009-10-27 18:03:47758// Enables 'terminate on heap corruption' flag. Helps protect against heap
maruel@google.comc9d40872008-09-24 12:58:37759// overflow. Has no effect if the OS doesn't provide the necessary facility.
darin@chromium.org0bea7252011-08-05 15:34:00760BASE_EXPORT void EnableTerminationOnHeapCorruption();
maruel@google.comc9d40872008-09-24 12:58:37761
jam@chromium.orgfecbc6c2011-08-31 20:37:59762// Turns on process termination if memory runs out.
darin@chromium.org0bea7252011-08-05 15:34:00763BASE_EXPORT void EnableTerminationOnOutOfMemory();
jam@chromium.orgfecbc6c2011-08-31 20:37:59764
avi@chromium.org6cfa3392010-07-01 20:11:43765#if defined(OS_MACOSX)
766// Exposed for testing.
darin@chromium.org0bea7252011-08-05 15:34:00767BASE_EXPORT malloc_zone_t* GetPurgeableZone();
gspencer@google.com03eb9d22011-08-23 17:58:21768#endif // defined(OS_MACOSX)
avi@chromium.orgcccb21212009-11-12 20:39:56769
maruel@chromium.orgd6fc9fd2009-10-27 18:03:47770// Enables stack dump to console output on exception and signals.
771// When enabled, the process will quit immediately. This is meant to be used in
772// unit_tests only!
darin@chromium.org0bea7252011-08-05 15:34:00773BASE_EXPORT bool EnableInProcessStackDumping();
maruel@chromium.orgd6fc9fd2009-10-27 18:03:47774
deanm@google.comdb717282008-08-27 13:48:03775// If supported on the platform, and the user has sufficent rights, increase
776// the current process's scheduling priority to a high priority.
darin@chromium.org0bea7252011-08-05 15:34:00777BASE_EXPORT void RaiseProcessToHighPriority();
deanm@google.comdb717282008-08-27 13:48:03778
mark@chromium.orge9a8c19f2009-09-03 21:27:36779#if defined(OS_MACOSX)
780// Restore the default exception handler, setting it to Apple Crash Reporter
781// (ReportCrash). When forking and execing a new process, the child will
782// inherit the parent's exception ports, which may be set to the Breakpad
783// instance running inside the parent. The parent's Breakpad instance should
784// not handle the child's exceptions. Calling RestoreDefaultExceptionHandler
785// in the child after forking will restore the standard exception handler.
786// See http://crbug.com/20371/ for more details.
787void RestoreDefaultExceptionHandler();
maruel@chromium.orgb6128aa2010-04-29 17:44:42788#endif // defined(OS_MACOSX)
mark@chromium.orge9a8c19f2009-09-03 21:27:36789
brettw@google.com176aa482008-11-14 03:25:15790} // namespace base
initial.commitd7cae122008-07-26 21:49:38791
deanm@google.comdb717282008-08-27 13:48:03792#endif // BASE_PROCESS_UTIL_H_