use os_bsd and OS_BSD and add some OS_FREEBSD conditions
this change is the first part of the FreeBSD support
BUG=
TEST=
Review URL: http://codereview.chromium.org/8773051
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@112936 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/base.gypi b/base/base.gypi
index c67575a..6d6f0d7 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -221,6 +221,7 @@
'process_posix.cc',
'process_util.cc',
'process_util.h',
+ 'process_util_freebsd.cc',
'process_util_linux.cc',
'process_util_mac.mm',
'process_util_openbsd.cc',
@@ -444,7 +445,7 @@
[ 'exclude', '^message_pump_x\\.cc$',],
],
}],
- [ 'OS != "linux" and OS != "openbsd"', {
+ [ 'OS != "linux" and os_bsd != 1', {
'sources!': [
# Not automatically excluded by the *linux.cc rules.
'linux_util.cc',
@@ -522,7 +523,7 @@
['exclude', '^sys_string_conversions_posix\\.cc$'],
],
}],
- [ 'OS == "openbsd"', {
+ [ 'os_bsd==1', {
'sources/': [
['exclude', '^files/file_path_watcher_linux\\.cc$'],
['exclude', '^files/file_path_watcher_stub\\.cc$'],
diff --git a/base/debug/debugger_posix.cc b/base/debug/debugger_posix.cc
index 1c7304c..4dee92db 100644
--- a/base/debug/debugger_posix.cc
+++ b/base/debug/debugger_posix.cc
@@ -33,6 +33,10 @@
#include <sys/sysctl.h>
#endif
+#if defined(OS_FREEBSD)
+#include <sys/user.h>
+#endif
+
#include <ostream>
#include "base/basictypes.h"
@@ -108,7 +112,9 @@
// This process is being debugged if the P_TRACED flag is set.
is_set = true;
-#if defined(OS_BSD)
+#if defined(OS_FREEBSD)
+ being_debugged = (info.ki_flag & P_TRACED) != 0;
+#elif defined(OS_BSD)
being_debugged = (info.p_flag & P_TRACED) != 0;
#else
being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0;
@@ -152,22 +158,14 @@
return pid_index < status.size() && status[pid_index] != '0';
}
-#elif defined(OS_NACL)
-
-bool BeingDebugged() {
- NOTIMPLEMENTED();
- return false;
-}
-
#else
bool BeingDebugged() {
- // TODO(benl): can we determine this under FreeBSD?
NOTIMPLEMENTED();
return false;
}
-#endif // defined(OS_FREEBSD)
+#endif
// We want to break into the debugger in Debug mode, and cause a crash dump in
// Release mode. Breakpad behaves as follows:
diff --git a/base/process_util.h b/base/process_util.h
index 3c9dfbe..9bcd6d4 100644
--- a/base/process_util.h
+++ b/base/process_util.h
@@ -14,14 +14,14 @@
#if defined(OS_WIN)
#include <windows.h>
#include <tlhelp32.h>
-#elif defined(OS_MACOSX) || defined(OS_OPENBSD)
+#elif defined(OS_MACOSX) || defined(OS_BSD)
// kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration
// is sufficient for the vector<kinfo_proc> below.
struct kinfo_proc;
// malloc_zone_t is defined in <malloc/malloc.h>, but this forward declaration
// is sufficient for GetPurgeableZone() below.
typedef struct _malloc_zone_t malloc_zone_t;
-#if !defined(OS_OPENBSD)
+#if !defined(OS_BSD)
#include <mach/mach.h>
#endif
#elif defined(OS_POSIX)
@@ -167,7 +167,7 @@
// Win XP SP1 as well.
BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
-#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_OPENBSD)
+#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_BSD)
// Returns the path to the executable of the given process.
BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
#endif
@@ -568,7 +568,7 @@
#if defined(OS_WIN)
HANDLE snapshot_;
bool started_iteration_;
-#elif defined(OS_MACOSX) || defined(OS_OPENBSD)
+#elif defined(OS_MACOSX) || defined(OS_BSD)
std::vector<kinfo_proc> kinfo_procs_;
size_t index_of_kinfo_proc_;
#elif defined(OS_POSIX)
diff --git a/base/process_util_freebsd.cc b/base/process_util_freebsd.cc
new file mode 100644
index 0000000..1157fb2
--- /dev/null
+++ b/base/process_util_freebsd.cc
@@ -0,0 +1,293 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/process_util.h"
+
+#include <ctype.h>
+#include <dirent.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/string_number_conversions.h"
+#include "base/string_split.h"
+#include "base/string_tokenizer.h"
+#include "base/string_util.h"
+#include "base/sys_info.h"
+
+namespace base {
+
+ProcessId GetParentProcessId(ProcessHandle process) {
+ struct kinfo_proc info;
+ size_t length;
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process };
+
+ if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
+ return -1;
+
+ return info.ki_ppid;
+}
+
+FilePath GetProcessExecutablePath(ProcessHandle process) {
+ char pathname[PATH_MAX];
+ size_t length;
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, process };
+
+ length = sizeof(pathname);
+
+ if (sysctl(mib, arraysize(mib), pathname, &length, NULL, 0) < 0 ||
+ length == 0) {
+ return FilePath();
+ }
+
+ return FilePath(std::string(pathname));
+}
+
+ProcessIterator::ProcessIterator(const ProcessFilter* filter)
+ : index_of_kinfo_proc_(),
+ filter_(filter) {
+
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid() };
+
+ bool done = false;
+ int try_num = 1;
+ const int max_tries = 10;
+
+ do {
+ size_t len = 0;
+ if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) <0 ){
+ LOG(ERROR) << "failed to get the size needed for the process list";
+ kinfo_procs_.resize(0);
+ done = true;
+ } else {
+ size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
+ // Leave some spare room for process table growth (more could show up
+ // between when we check and now)
+ num_of_kinfo_proc += 16;
+ kinfo_procs_.resize(num_of_kinfo_proc);
+ len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
+ if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) <0) {
+ // If we get a mem error, it just means we need a bigger buffer, so
+ // loop around again. Anything else is a real error and give up.
+ if (errno != ENOMEM) {
+ LOG(ERROR) << "failed to get the process list";
+ kinfo_procs_.resize(0);
+ done = true;
+ }
+ } else {
+ // Got the list, just make sure we're sized exactly right
+ size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
+ kinfo_procs_.resize(num_of_kinfo_proc);
+ done = true;
+ }
+ }
+ } while (!done && (try_num++ < max_tries));
+
+ if (!done) {
+ LOG(ERROR) << "failed to collect the process list in a few tries";
+ kinfo_procs_.resize(0);
+ }
+}
+
+ProcessIterator::~ProcessIterator() {
+}
+
+bool ProcessIterator::CheckForNextProcess() {
+ std::string data;
+
+ for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++ index_of_kinfo_proc_) {
+ size_t length;
+ struct kinfo_proc kinfo = kinfo_procs_[index_of_kinfo_proc_];
+ int mib[] = { CTL_KERN, KERN_PROC_ARGS, kinfo.ki_pid };
+
+ if ((kinfo.ki_pid > 0) && (kinfo.ki_stat == SZOMB))
+ continue;
+
+ length = 0;
+ if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) {
+ LOG(ERROR) << "failed to figure out the buffer size for a command line";
+ continue;
+ }
+
+ data.resize(length);
+
+ if (sysctl(mib, arraysize(mib), &data[0], &length, NULL, 0) < 0) {
+ LOG(ERROR) << "failed to fetch a commandline";
+ continue;
+ }
+
+ std::string delimiters;
+ delimiters.push_back('\0');
+ Tokenize(data, delimiters, &entry_.cmd_line_args_);
+
+ size_t exec_name_end = data.find('\0');
+ if (exec_name_end == std::string::npos) {
+ LOG(ERROR) << "command line data didn't match expected format";
+ continue;
+ }
+
+ entry_.pid_ = kinfo.ki_pid;
+ entry_.ppid_ = kinfo.ki_ppid;
+ entry_.gid_ = kinfo.ki_pgid;
+
+ size_t last_slash = data.rfind('/', exec_name_end);
+ if (last_slash == std::string::npos) {
+ entry_.exe_file_.assign(data, 0, exec_name_end);
+ } else {
+ entry_.exe_file_.assign(data, last_slash + 1,
+ exec_name_end - last_slash - 1);
+ }
+
+ // Start w/ the next entry next time through
+ ++index_of_kinfo_proc_;
+
+ return true;
+ }
+ return false;
+}
+
+bool NamedProcessIterator::IncludeEntry() {
+ if(executable_name_ != entry().exe_file())
+ return false;
+
+ return ProcessIterator::IncludeEntry();
+}
+
+
+ProcessMetrics::ProcessMetrics(ProcessHandle process)
+ : process_(process),
+ last_time_(0),
+ last_system_time_(0),
+ last_cpu_(0) {
+ processor_count_ = base::SysInfo::NumberOfProcessors();
+}
+
+// static
+ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
+ return new ProcessMetrics(process);
+}
+
+size_t ProcessMetrics::GetPagefileUsage() const {
+ struct kinfo_proc info;
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ };
+ size_t length = sizeof(info);
+
+ if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
+ return 0;
+
+ return info.ki_size;
+}
+
+size_t ProcessMetrics::GetPeakPagefileUsage() const {
+ return 0;
+}
+
+size_t ProcessMetrics::GetWorkingSetSize() const {
+ struct kinfo_proc info;
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ };
+ size_t length = sizeof(info);
+
+ if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
+ return 0;
+
+ return info.ki_rssize * getpagesize();
+}
+
+size_t ProcessMetrics::GetPeakWorkingSetSize() const {
+ return 0;
+}
+
+bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
+ size_t* shared_bytes) {
+ WorkingSetKBytes ws_usage;
+ if (!GetWorkingSetKBytes(&ws_usage))
+ return false;
+
+ if (private_bytes)
+ *private_bytes = ws_usage.priv << 10;
+
+ if (shared_bytes)
+ *shared_bytes = ws_usage.shared * 1024;
+
+ return true;
+}
+
+bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
+// TODO(bapt) be sure we can't be precise
+ size_t priv = GetWorkingSetSize();
+ if (!priv)
+ return false;
+ ws_usage->priv = priv / 1024;
+ ws_usage->shareable = 0;
+ ws_usage->shared = 0;
+
+ return true;
+}
+
+bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
+ return false;
+}
+
+double ProcessMetrics::GetCPUUsage() {
+ struct kinfo_proc info;
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ };
+ size_t length = sizeof(info);
+
+ struct timeval now;
+ int retval = gettimeofday(&now, NULL);
+ if (retval)
+ return 0;
+
+ if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
+ return 0;
+
+ return (info.ki_pctcpu / FSCALE) * 100.0;
+}
+
+size_t GetSystemCommitCharge() {
+ int mib[2], pagesize;
+ unsigned long mem_total, mem_free, mem_inactive;
+ size_t length = sizeof(mem_total);
+
+ if (sysctl(mib, arraysize(mib), &mem_total, &length, NULL, 0) < 0)
+ return 0;
+
+ length = sizeof(mem_free);
+ if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0)
+ return 0;
+
+ length = sizeof(mem_inactive);
+ if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length,
+ NULL, 0) < 0) {
+ return 0;
+ }
+
+ pagesize = getpagesize();
+
+ return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
+}
+
+void EnableTerminationOnOutOfMemory() {
+ DLOG(WARNING) << "Not feasible.";
+}
+
+void EnableTerminationOnHeapCorruption() {
+ // Nothing to do.
+}
+
+bool AdjustOOMScore(ProcessId process, int score) {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+} // namespace base
diff --git a/base/process_util_posix.cc b/base/process_util_posix.cc
index f0e9fb0..addd1c3 100644
--- a/base/process_util_posix.cc
+++ b/base/process_util_posix.cc
@@ -33,6 +33,11 @@
#include "base/threading/thread_restrictions.h"
#include "base/time.h"
+#if defined(OS_FREEBSD)
+#include <sys/event.h>
+#include <sys/ucontext.h>
+#endif
+
#if defined(OS_MACOSX)
#include <crt_externs.h>
#include <sys/event.h>
diff --git a/build/common.gypi b/build/common.gypi
index 991021e5..fcd3d3fb 100644
--- a/build/common.gypi
+++ b/build/common.gypi
@@ -1207,15 +1207,15 @@
'-Wsign-compare',
]
}],
- [ 'os_posix==1 and OS!="mac" and OS!="openbsd" and OS!="android" and chromeos==0', {
+ [ 'os_posix==1 and os_bsd!=1 and OS!="mac" and OS!="android" and chromeos==0', {
'cflags': [
# Don't warn about ignoring the return value from e.g. close().
# This is off by default in some gccs but on by default in others.
# Currently this option is not set for Chrome OS build because
# the current version of gcc (4.3.4) used for building Chrome in
# Chrome OS chroot doesn't support this option.
- # OpenBSD does not support this option either, since it's using
- # gcc 4.2.1, which does not have this flag yet.
+ # BSD systems do not support this option either, since they are
+ # usually using gcc 4.2.1, which does not have this flag yet.
# TODO(mazda): remove the conditional for Chrome OS when gcc
# version is upgraded.
'-Wno-unused-result',
@@ -1280,7 +1280,7 @@
['exclude', '(^|/)gtk_[^/]*\\.(h|cc)$'],
],
}],
- ['OS!="linux" and OS!="openbsd"', {
+ ['OS!="linux" and OS!="openbsd" and OS!="freebsd"', {
'sources/': [
['exclude', '_xdg(_unittest)?\\.(h|cc)$'],
],
@@ -1292,10 +1292,10 @@
['exclude', '(^|/)(wayland)_[^/]*\\.(h|cc)$'],
],
}],
- # Do not exclude the linux files on OpenBSD since most of them can be
+ # Do not exclude the linux files on *BSD since most of them can be
# shared at this point.
# In case a file is not needed, it is going to be excluded later on.
- ['OS!="linux" and OS!="openbsd"', {
+ ['OS!="linux" and OS!="openbsd" and OS!="freebsd"', {
'sources/': [
['exclude', '_linux(_unittest)?\\.(h|cc)$'],
['exclude', '(^|/)linux/'],