Commit 05046956 authored by mark@chromium.org's avatar mark@chromium.org

Use the new ScopedMachVM class and the MACH_LOG family of logging macros

where it makes sense to do so in existing code.

This migration shook out a couple of bugs in mach_logging.h carried over
from mac_logging.h (fixed there too).

R=rsesek@chromium.org, sky@chromium.org

Review URL: https://codereview.chromium.org/278923002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269483 0039d316-1c4b-4281-b951-d872f2087c98
parent 62bf48ae
......@@ -5,6 +5,8 @@
#ifndef BASE_MAC_MAC_LOGGING_H_
#define BASE_MAC_MAC_LOGGING_H_
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/logging.h"
#include "build/build_config.h"
......@@ -43,6 +45,12 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage {
} // namespace logging
#if defined(NDEBUG)
#define MAC_DVLOG_IS_ON(verbose_level) 0
#else
#define MAC_DVLOG_IS_ON(verbose_level) VLOG_IS_ON(verbose_level)
#endif
#define OSSTATUS_LOG_STREAM(severity, status) \
COMPACT_GOOGLE_LOG_EX_ ## severity(OSStatusLogMessage, status).stream()
#define OSSTATUS_VLOG_STREAM(verbose_level, status) \
......@@ -74,10 +82,10 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage {
#define OSSTATUS_DVLOG(verbose_level, status) \
LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \
DVLOG_IS_ON(verbose_level))
MAC_DVLOG_IS_ON(verbose_level))
#define OSSTATUS_DVLOG_IF(verbose_level, condition, status) \
LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status) \
DVLOG_IS_ON(verbose_level) && (condition))
LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \
MAC_DVLOG_IS_ON(verbose_level) && (condition))
#define OSSTATUS_DCHECK(condition, status) \
LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), \
......
......@@ -7,6 +7,7 @@
#include <mach/mach.h>
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/logging.h"
......@@ -31,7 +32,7 @@
namespace logging {
class MachLogMessage : public logging::LogMessage {
class BASE_EXPORT MachLogMessage : public logging::LogMessage {
public:
MachLogMessage(const char* file_path,
int line,
......@@ -45,7 +46,7 @@ class MachLogMessage : public logging::LogMessage {
DISALLOW_COPY_AND_ASSIGN(MachLogMessage);
};
class BootstrapLogMessage : public logging::LogMessage {
class BASE_EXPORT BootstrapLogMessage : public logging::LogMessage {
public:
BootstrapLogMessage(const char* file_path,
int line,
......@@ -61,6 +62,12 @@ class BootstrapLogMessage : public logging::LogMessage {
} // namespace logging
#if defined(NDEBUG)
#define MACH_DVLOG_IS_ON(verbose_level) 0
#else
#define MACH_DVLOG_IS_ON(verbose_level) VLOG_IS_ON(verbose_level)
#endif
#define MACH_LOG_STREAM(severity, mach_err) \
COMPACT_GOOGLE_LOG_EX_ ## severity(MachLogMessage, mach_err).stream()
#define MACH_VLOG_STREAM(verbose_level, mach_err) \
......@@ -92,16 +99,18 @@ class BootstrapLogMessage : public logging::LogMessage {
#define MACH_DVLOG(verbose_level, mach_err) \
LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \
DVLOG_IS_ON(verbose_level))
MACH_DVLOG_IS_ON(verbose_level))
#define MACH_DVLOG_IF(verbose_level, condition, mach_err) \
LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err) \
DVLOG_IS_ON(verbose_level) && (condition))
LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \
MACH_DVLOG_IS_ON(verbose_level) && (condition))
#define MACH_DCHECK(condition, mach_err) \
LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), \
DCHECK_IS_ON && !(condition)) \
<< "Check failed: " # condition << ". "
#define BOOTSTRAP_DVLOG_IS_ON MACH_DVLOG_IS_ON
#define BOOTSTRAP_LOG_STREAM(severity, bootstrap_err) \
COMPACT_GOOGLE_LOG_EX_ ## severity(BootstrapLogMessage, \
bootstrap_err).stream()
......@@ -136,10 +145,10 @@ class BootstrapLogMessage : public logging::LogMessage {
#define BOOTSTRAP_DVLOG(verbose_level, bootstrap_err) \
LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \
DVLOG_IS_ON(verbose_level))
BOOTSTRAP_DVLOG_IS_ON(verbose_level))
#define BOOTSTRAP_DVLOG_IF(verbose_level, condition, bootstrap_err) \
LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err) \
DVLOG_IS_ON(verbose_level) && (condition))
LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \
BOOTSTRAP_DVLOG_IS_ON(verbose_level) && (condition))
#define BOOTSTRAP_DCHECK(condition, bootstrap_err) \
LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), \
......
......@@ -7,8 +7,8 @@
#include <mach/mach.h>
#include "base/basictypes.h"
#include "base/base_export.h"
#include "base/basictypes.h"
namespace base {
namespace mac {
......
......@@ -10,6 +10,7 @@
#include <algorithm>
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/logging.h"
......@@ -45,7 +46,7 @@
namespace base {
namespace mac {
class ScopedMachVM {
class BASE_EXPORT ScopedMachVM {
public:
explicit ScopedMachVM(mach_vm_address_t address = 0, mach_vm_size_t size = 0)
: address_(address),
......
......@@ -5,11 +5,13 @@
#include "base/memory/discardable_memory.h"
#include <mach/mach.h>
#include <sys/mman.h>
#include <mach/mach_vm.h>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_vm.h"
#include "base/memory/discardable_memory_emulated.h"
#include "base/memory/discardable_memory_malloc.h"
#include "base/memory/scoped_ptr.h"
......@@ -25,39 +27,44 @@ const int kDiscardableMemoryTag = VM_MAKE_TAG(252);
class DiscardableMemoryMac : public DiscardableMemory {
public:
explicit DiscardableMemoryMac(size_t size)
: buffer_(0),
size_(size) {
: memory_(0, 0),
size_(mach_vm_round_page(size)) {
}
bool Initialize() {
kern_return_t ret = vm_allocate(mach_task_self(),
&buffer_,
size_,
VM_FLAGS_PURGABLE |
VM_FLAGS_ANYWHERE |
kDiscardableMemoryTag);
DCHECK_EQ(memory_.size(), 0u);
mach_vm_address_t address = 0;
kern_return_t ret = mach_vm_allocate(mach_task_self(),
&address,
size_,
VM_FLAGS_PURGABLE |
VM_FLAGS_ANYWHERE |
kDiscardableMemoryTag);
if (ret != KERN_SUCCESS) {
DLOG(ERROR) << "vm_allocate() failed";
MACH_DLOG(ERROR, ret) << "mach_vm_allocate";
return false;
}
memory_.reset(address, size_);
return true;
}
virtual ~DiscardableMemoryMac() {
if (buffer_)
vm_deallocate(mach_task_self(), buffer_, size_);
}
virtual DiscardableMemoryLockStatus Lock() OVERRIDE {
DCHECK_EQ(0, mprotect(reinterpret_cast<void*>(buffer_),
size_,
PROT_READ | PROT_WRITE));
kern_return_t ret;
MACH_DCHECK((ret = mach_vm_protect(mach_task_self(),
memory_.address(),
memory_.size(),
FALSE,
VM_PROT_DEFAULT)) == KERN_SUCCESS, ret);
int state = VM_PURGABLE_NONVOLATILE;
kern_return_t ret = vm_purgable_control(mach_task_self(),
buffer_,
VM_PURGABLE_SET_STATE,
&state);
ret = mach_vm_purgable_control(mach_task_self(),
memory_.address(),
VM_PURGABLE_SET_STATE,
&state);
if (ret != KERN_SUCCESS)
return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
......@@ -67,22 +74,25 @@ class DiscardableMemoryMac : public DiscardableMemory {
virtual void Unlock() OVERRIDE {
int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT;
kern_return_t ret = vm_purgable_control(mach_task_self(),
buffer_,
VM_PURGABLE_SET_STATE,
&state);
DCHECK_EQ(0, mprotect(reinterpret_cast<void*>(buffer_), size_, PROT_NONE));
if (ret != KERN_SUCCESS)
DLOG(ERROR) << "Failed to unlock memory.";
kern_return_t ret = mach_vm_purgable_control(mach_task_self(),
memory_.address(),
VM_PURGABLE_SET_STATE,
&state);
MACH_DLOG_IF(ERROR, ret != KERN_SUCCESS, ret) << "mach_vm_purgable_control";
MACH_DCHECK((ret = mach_vm_protect(mach_task_self(),
memory_.address(),
memory_.size(),
FALSE,
VM_PROT_NONE)) == KERN_SUCCESS, ret);
}
virtual void* Memory() const OVERRIDE {
return reinterpret_cast<void*>(buffer_);
return reinterpret_cast<void*>(memory_.address());
}
private:
vm_address_t buffer_;
const size_t size_;
mac::ScopedMachVM memory_;
size_t size_;
DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac);
};
......@@ -149,7 +159,7 @@ scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType(
// static
void DiscardableMemory::PurgeForTesting() {
int state = 0;
vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state);
mach_vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state);
internal::DiscardableMemoryEmulated::PurgeForTesting();
}
......
......@@ -16,6 +16,7 @@
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/mac/mach_logging.h"
#include "base/scoped_clear_errno.h"
#include "third_party/apple_apsl/CFBase.h"
#include "third_party/apple_apsl/malloc.h"
......@@ -222,10 +223,12 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone,
reinterpret_cast<vm_region_info_t>(&info),
&count,
&unused);
CHECK(result == KERN_SUCCESS);
MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_region";
result = mach_port_deallocate(mach_task_self(), unused);
CHECK(result == KERN_SUCCESS);
// The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but
// balance it with a deallocate in case this ever changes. See 10.9.2
// xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region.
mach_port_deallocate(mach_task_self(), unused);
// Does the region fully enclose the zone pointers? Possibly unwarranted
// simplification used: using the size of a full version 8 malloc zone rather
......@@ -248,7 +251,7 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone,
*reprotection_length,
false,
info.protection | VM_PROT_WRITE);
CHECK(result == KERN_SUCCESS);
MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
}
}
......@@ -646,7 +649,7 @@ void EnableTerminationOnOutOfMemory() {
default_reprotection_length,
false,
default_reprotection_value);
CHECK(result == KERN_SUCCESS);
MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
}
if (purgeable_reprotection_start) {
......@@ -655,7 +658,7 @@ void EnableTerminationOnOutOfMemory() {
purgeable_reprotection_length,
false,
purgeable_reprotection_value);
CHECK(result == KERN_SUCCESS);
MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
}
#endif
......
......@@ -11,6 +11,7 @@
#include "base/containers/hash_tables.h"
#include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_port.h"
#include "base/sys_info.h"
......@@ -116,7 +117,6 @@ size_t ProcessMetrics::GetPeakWorkingSetSize() const {
// shared_bytes is the size of shared resident memory.
bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
size_t* shared_bytes) {
kern_return_t kr;
size_t private_pages_count = 0;
size_t shared_pages_count = 0;
......@@ -153,22 +153,26 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
vm_region_top_info_data_t info;
mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
mach_port_t object_name;
kr = mach_vm_region(task,
&address,
&size,
VM_REGION_TOP_INFO,
(vm_region_info_t)&info,
&info_count,
&object_name);
kern_return_t kr = mach_vm_region(task,
&address,
&size,
VM_REGION_TOP_INFO,
reinterpret_cast<vm_region_info_t>(&info),
&info_count,
&object_name);
if (kr == KERN_INVALID_ADDRESS) {
// We're at the end of the address space.
break;
} else if (kr != KERN_SUCCESS) {
DLOG(ERROR) << "Calling mach_vm_region failed with error: "
<< mach_error_string(kr);
MACH_DLOG(ERROR, kr) << "mach_vm_region";
return false;
}
// The kernel always returns a null object for VM_REGION_TOP_INFO, but
// balance it with a deallocate in case this ever changes. See 10.9.2
// xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region.
mach_port_deallocate(mach_task_self(), object_name);
if (IsAddressInSharedRegion(address, cpu_type) &&
info.share_mode != SM_PRIVATE)
continue;
......@@ -196,18 +200,10 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
}
}
vm_size_t page_size;
kr = host_page_size(task, &page_size);
if (kr != KERN_SUCCESS) {
DLOG(ERROR) << "Failed to fetch host page size, error: "
<< mach_error_string(kr);
return false;
}
if (private_bytes)
*private_bytes = private_pages_count * page_size;
*private_bytes = private_pages_count * PAGE_SIZE;
if (shared_bytes)
*shared_bytes = shared_pages_count * page_size;
*shared_bytes = shared_pages_count * PAGE_SIZE;
return true;
}
......@@ -235,16 +231,14 @@ double ProcessMetrics::GetCPUUsage() {
if (task == MACH_PORT_NULL)
return 0;
kern_return_t kr;
// Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage()
// in libtop.c), but this is more concise and gives the same results:
task_thread_times_info thread_info_data;
mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
kr = task_info(task,
TASK_THREAD_TIMES_INFO,
reinterpret_cast<task_info_t>(&thread_info_data),
&thread_info_count);
kern_return_t kr = task_info(task,
TASK_THREAD_TIMES_INFO,
reinterpret_cast<task_info_t>(&thread_info_data),
&thread_info_count);
if (kr != KERN_SUCCESS) {
// Most likely cause: |task| is a zombie.
return 0;
......@@ -294,14 +288,12 @@ int ProcessMetrics::GetIdleWakeupsPerSecond() {
if (task == MACH_PORT_NULL)
return 0;
kern_return_t kr;
task_power_info power_info_data;
mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT;
kr = task_info(task,
TASK_POWER_INFO,
reinterpret_cast<task_info_t>(&power_info_data),
&power_info_count);
kern_return_t kr = task_info(task,
TASK_POWER_INFO,
reinterpret_cast<task_info_t>(&power_info_data),
&power_info_count);
if (kr != KERN_SUCCESS) {
// Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system
// where TASK_POWER_INFO isn't supported yet.
......@@ -362,19 +354,12 @@ size_t GetSystemCommitCharge() {
kern_return_t kr = host_statistics(host, HOST_VM_INFO,
reinterpret_cast<host_info_t>(&data),
&count);
if (kr) {
DLOG(WARNING) << "Failed to fetch host statistics.";
return 0;
}
vm_size_t page_size;
kr = host_page_size(host, &page_size);
if (kr) {
DLOG(ERROR) << "Failed to fetch host page size.";
if (kr != KERN_SUCCESS) {
MACH_DLOG(WARNING, kr) << "host_statistics";
return 0;
}
return (data.active_count * page_size) / 1024;
return (data.active_count * PAGE_SIZE) / 1024;
}
} // namespace base
......@@ -14,6 +14,7 @@
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/threading/thread_id_name_manager.h"
#include "base/tracked_objects.h"
......@@ -61,20 +62,19 @@ void SetPriorityNormal(mach_port_t mach_thread_id) {
// Please note that this call could fail in rare cases depending
// on runtime conditions.
thread_standard_policy policy;
kern_return_t result = thread_policy_set(mach_thread_id,
THREAD_STANDARD_POLICY,
(thread_policy_t)&policy,
THREAD_STANDARD_POLICY_COUNT);
kern_return_t result =
thread_policy_set(mach_thread_id,
THREAD_STANDARD_POLICY,
reinterpret_cast<thread_policy_t>(&policy),
THREAD_STANDARD_POLICY_COUNT);
if (result != KERN_SUCCESS)
DVLOG(1) << "thread_policy_set() failure: " << result;
MACH_DVLOG(1, result) << "thread_policy_set";
}
// Enables time-contraint policy and priority suitable for low-latency,
// glitch-resistant audio.
void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
kern_return_t result;
// Increase thread priority to real-time.
// Please note that the thread_policy_set() calls may fail in
......@@ -85,12 +85,13 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
// Make thread fixed priority.
thread_extended_policy_data_t policy;
policy.timeshare = 0; // Set to 1 for a non-fixed thread.
result = thread_policy_set(mach_thread_id,
THREAD_EXTENDED_POLICY,
(thread_policy_t)&policy,
THREAD_EXTENDED_POLICY_COUNT);
kern_return_t result =
thread_policy_set(mach_thread_id,
THREAD_EXTENDED_POLICY,
reinterpret_cast<thread_policy_t>(&policy),
THREAD_EXTENDED_POLICY_COUNT);
if (result != KERN_SUCCESS) {
DVLOG(1) << "thread_policy_set() failure: " << result;
MACH_DVLOG(1, result) << "thread_policy_set";
return;
}
......@@ -99,10 +100,10 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
precedence.importance = 63;
result = thread_policy_set(mach_thread_id,
THREAD_PRECEDENCE_POLICY,
(thread_policy_t)&precedence,
reinterpret_cast<thread_policy_t>(&precedence),
THREAD_PRECEDENCE_POLICY_COUNT);
if (result != KERN_SUCCESS) {
DVLOG(1) << "thread_policy_set() failure: " << result;
MACH_DVLOG(1, result) << "thread_policy_set";
return;
}
......@@ -133,7 +134,7 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
mach_timebase_info_data_t tb_info;
mach_timebase_info(&tb_info);
double ms_to_abs_time =
((double)tb_info.denom / (double)tb_info.numer) * 1000000;
(static_cast<double>(tb_info.denom) / tb_info.numer) * 1000000;
thread_time_constraint_policy_data_t time_constraints;
time_constraints.period = kTimeQuantum * ms_to_abs_time;
......@@ -141,12 +142,12 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time;
time_constraints.preemptible = 0;
result = thread_policy_set(mach_thread_id,
THREAD_TIME_CONSTRAINT_POLICY,
(thread_policy_t)&time_constraints,
THREAD_TIME_CONSTRAINT_POLICY_COUNT);
if (result != KERN_SUCCESS)
DVLOG(1) << "thread_policy_set() failure: " << result;
result =
thread_policy_set(mach_thread_id,
THREAD_TIME_CONSTRAINT_POLICY,
reinterpret_cast<thread_policy_t>(&time_constraints),
THREAD_TIME_CONSTRAINT_POLICY_COUNT);
MACH_DVLOG_IF(1, result != KERN_SUCCESS, result) << "thread_policy_set";
return;
}
......
......@@ -15,6 +15,7 @@
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_mach_port.h"
......@@ -46,7 +47,7 @@ uint64_t ComputeCurrentTicks() {
// whether mach_timebase_info has already been called. This is
// recommended by Apple's QA1398.
kern_return_t kr = mach_timebase_info(&timebase_info);
DCHECK_EQ(KERN_SUCCESS, kr);
MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info";
}
// mach_absolute_time is it when it comes to ticks on the Mac. Other calls
......@@ -85,7 +86,7 @@ uint64_t ComputeThreadTicks() {
THREAD_BASIC_INFO,
reinterpret_cast<thread_info_t>(&thread_info_data),
&thread_info_count);
DCHECK_EQ(KERN_SUCCESS, kr);
MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info";
return (thread_info_data.user_time.seconds *
base::Time::kMicrosecondsPerSecond) +
......
......@@ -24,6 +24,7 @@
#include "base/mac/authorization_util.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/mac_logging.h"
#include "base/mac/mach_logging.h"
#import "base/mac/mac_util.h"
#include "base/mac/scoped_authorizationref.h"
#include "base/mac/scoped_cftyperef.h"
......@@ -81,7 +82,7 @@ io_service_t CopyHDIXDriveServiceForMedia(io_service_t media) {
kIORegistryIterateParents,
&iterator_ref);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "IORegistryEntryCreateIterator: " << kr;
MACH_LOG(ERROR, kr) << "IORegistryEntryCreateIterator";
return IO_OBJECT_NULL;
}
base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref);
......@@ -207,7 +208,7 @@ bool IsPathOnReadOnlyDiskImage(const char path[],
match_dict,
&iterator_ref);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "IOServiceGetMatchingServices: " << kr;
MACH_LOG(ERROR, kr) << "IOServiceGetMatchingServices";
return false;
}
base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref);
......
......@@ -12,6 +12,7 @@
#include "base/command_line.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_port.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
......@@ -28,11 +29,6 @@ namespace content {
namespace {
// Prints a string representation of a Mach error code.
std::string MachErrorCode(kern_return_t err) {
return base::StringPrintf("0x%x %s", err, mach_error_string(err));
}
// Mach message structure used in the child as a sending message.
struct MachBroker_ChildSendMsg {
mach_msg_header_t header;
......@@ -64,8 +60,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
MACH_PORT_RIGHT_RECEIVE,
&port);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to allocate MachBroker server port: "
<< MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "mach_port_allocate";
return false;
}
......@@ -73,8 +68,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
kr = mach_port_insert_right(
mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to insert send right for MachBroker server port: "
<< MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "mach_port_insert_right";
return false;
}
......@@ -96,33 +90,35 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
msg.header.msgh_size = sizeof(msg);
msg.header.msgh_local_port = server_port_.get();
const mach_msg_option_t options = MACH_RCV_MSG |
MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) |
MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT);
kern_return_t kr;
do {
while ((kr = mach_msg(&msg.header,
options,
0,
sizeof(msg),
server_port_,
MACH_MSG_TIMEOUT_NONE,
MACH_PORT_NULL)) == KERN_SUCCESS) {
// Use the kernel audit information to make sure this message is from
// a task that this process spawned. The kernel audit token contains the
// unspoofable pid of the task that sent the message.
mach_msg_option_t options = MACH_RCV_MSG |
MACH_RCV_TRAILER_TYPE(MACH_RCV_TRAILER_AUDIT) |
MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT);
kr = mach_msg(&msg.header, options, 0, sizeof(msg), server_port_,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (kr == KERN_SUCCESS) {
// TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
pid_t child_pid;
audit_token_to_au32(msg.trailer.msgh_audit,
NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
mach_port_t child_task_port = msg.child_task_port.name;
// Take the lock and update the broker information.
base::AutoLock lock(broker_->GetLock());
broker_->FinalizePid(child_pid, child_task_port);
}
} while (kr == KERN_SUCCESS);
LOG(ERROR) << "MachBroker thread exiting; mach_msg() likely failed: "
<< MachErrorCode(kr);
//
// TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
pid_t child_pid;
audit_token_to_au32(msg.trailer.msgh_audit,
NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
mach_port_t child_task_port = msg.child_task_port.name;
// Take the lock and update the broker information.
base::AutoLock lock(broker_->GetLock());
broker_->FinalizePid(child_pid, child_task_port);
}
MACH_LOG(ERROR, kr) << "mach_msg";
}
private:
......@@ -141,7 +137,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
mach_port_t bootstrap_port;
kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to look up bootstrap port: " << MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "task_get_bootstrap_port";
return false;
}
......@@ -149,7 +145,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
kr = bootstrap_look_up(bootstrap_port,
const_cast<char*>(GetMachPortName().c_str()), &parent_port);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to look up named parent port: " << MachErrorCode(kr);
BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
return false;
}
......@@ -169,7 +165,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg),
0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL);
if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to send task port to parent: " << MachErrorCode(kr);
MACH_LOG(ERROR, kr) << "mach_msg";
return false;
}
......@@ -279,9 +275,7 @@ void MachBroker::InvalidatePid(base::ProcessHandle pid) {
kern_return_t kr = mach_port_deallocate(mach_task_self(),
it->second);
LOG_IF(WARNING, kr != KERN_SUCCESS)
<< "Failed to mach_port_deallocate mach task " << it->second
<< ", error " << MachErrorCode(kr);
MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "mach_port_deallocate";
mach_map_.erase(it);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment