Commit 07c19ac6 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@269829 0039d316-1c4b-4281-b951-d872f2087c98
parent 353132b9
...@@ -829,8 +829,10 @@ ...@@ -829,8 +829,10 @@
['include', '^mac/bundle_locations\\.'], ['include', '^mac/bundle_locations\\.'],
['include', '^mac/foundation_util\\.'], ['include', '^mac/foundation_util\\.'],
['include', '^mac/mac_logging\\.'], ['include', '^mac/mac_logging\\.'],
['include', '^mac/mach_logging\\.'],
['include', '^mac/objc_property_releaser\\.'], ['include', '^mac/objc_property_releaser\\.'],
['include', '^mac/scoped_mach_port\\.'], ['include', '^mac/scoped_mach_port\\.'],
['include', '^mac/scoped_mach_vm\\.'],
['include', '^mac/scoped_nsautorelease_pool\\.'], ['include', '^mac/scoped_nsautorelease_pool\\.'],
['include', '^mac/scoped_nsobject\\.'], ['include', '^mac/scoped_nsobject\\.'],
['include', '^memory/discardable_memory_mac\\.'], ['include', '^memory/discardable_memory_mac\\.'],
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef BASE_MAC_MAC_LOGGING_H_ #ifndef BASE_MAC_MAC_LOGGING_H_
#define 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 "base/logging.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -43,6 +45,12 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage { ...@@ -43,6 +45,12 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage {
} // namespace logging } // 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) \ #define OSSTATUS_LOG_STREAM(severity, status) \
COMPACT_GOOGLE_LOG_EX_ ## severity(OSStatusLogMessage, status).stream() COMPACT_GOOGLE_LOG_EX_ ## severity(OSStatusLogMessage, status).stream()
#define OSSTATUS_VLOG_STREAM(verbose_level, status) \ #define OSSTATUS_VLOG_STREAM(verbose_level, status) \
...@@ -74,10 +82,10 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage { ...@@ -74,10 +82,10 @@ class BASE_EXPORT OSStatusLogMessage : public logging::LogMessage {
#define OSSTATUS_DVLOG(verbose_level, status) \ #define OSSTATUS_DVLOG(verbose_level, status) \
LAZY_STREAM(OSSTATUS_VLOG_STREAM(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) \ #define OSSTATUS_DVLOG_IF(verbose_level, condition, status) \
LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status) \ LAZY_STREAM(OSSTATUS_VLOG_STREAM(verbose_level, status), \
DVLOG_IS_ON(verbose_level) && (condition)) MAC_DVLOG_IS_ON(verbose_level) && (condition))
#define OSSTATUS_DCHECK(condition, status) \ #define OSSTATUS_DCHECK(condition, status) \
LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), \ LAZY_STREAM(OSSTATUS_LOG_STREAM(FATAL, status), \
......
...@@ -4,13 +4,15 @@ ...@@ -4,13 +4,15 @@
#include "base/mac/mach_logging.h" #include "base/mac/mach_logging.h"
#include <servers/bootstrap.h>
#include <iomanip> #include <iomanip>
#include <string> #include <string>
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#if !defined(OS_IOS)
#include <servers/bootstrap.h>
#endif // !OS_IOS
namespace { namespace {
std::string FormatMachErrorNumber(mach_error_t mach_err) { std::string FormatMachErrorNumber(mach_error_t mach_err) {
...@@ -41,6 +43,8 @@ MachLogMessage::~MachLogMessage() { ...@@ -41,6 +43,8 @@ MachLogMessage::~MachLogMessage() {
<< FormatMachErrorNumber(mach_err_); << FormatMachErrorNumber(mach_err_);
} }
#if !defined(OS_IOS)
BootstrapLogMessage::BootstrapLogMessage(const char* file_path, BootstrapLogMessage::BootstrapLogMessage(const char* file_path,
int line, int line,
LogSeverity severity, LogSeverity severity,
...@@ -78,4 +82,6 @@ BootstrapLogMessage::~BootstrapLogMessage() { ...@@ -78,4 +82,6 @@ BootstrapLogMessage::~BootstrapLogMessage() {
} }
} }
#endif // !OS_IOS
} // namespace logging } // namespace logging
...@@ -7,8 +7,10 @@ ...@@ -7,8 +7,10 @@
#include <mach/mach.h> #include <mach/mach.h>
#include "base/base_export.h"
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/logging.h" #include "base/logging.h"
#include "build/build_config.h"
// Use the MACH_LOG family of macros along with a mach_error_t (kern_return_t) // Use the MACH_LOG family of macros along with a mach_error_t (kern_return_t)
// containing a Mach error. The error value will be decoded so that logged // containing a Mach error. The error value will be decoded so that logged
...@@ -26,12 +28,12 @@ ...@@ -26,12 +28,12 @@
// MACH_LOG(ERROR, kr) << "mach_timebase_info"; // MACH_LOG(ERROR, kr) << "mach_timebase_info";
// } // }
// //
// kr = mach_vm_deallocate(task, address, size); // kr = vm_deallocate(task, address, size);
// MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_vm_deallocate"; // MACH_DCHECK(kr == KERN_SUCCESS, kr) << "vm_deallocate";
namespace logging { namespace logging {
class MachLogMessage : public logging::LogMessage { class BASE_EXPORT MachLogMessage : public logging::LogMessage {
public: public:
MachLogMessage(const char* file_path, MachLogMessage(const char* file_path,
int line, int line,
...@@ -45,22 +47,14 @@ class MachLogMessage : public logging::LogMessage { ...@@ -45,22 +47,14 @@ class MachLogMessage : public logging::LogMessage {
DISALLOW_COPY_AND_ASSIGN(MachLogMessage); DISALLOW_COPY_AND_ASSIGN(MachLogMessage);
}; };
class BootstrapLogMessage : public logging::LogMessage {
public:
BootstrapLogMessage(const char* file_path,
int line,
LogSeverity severity,
kern_return_t bootstrap_err);
~BootstrapLogMessage();
private:
kern_return_t bootstrap_err_;
DISALLOW_COPY_AND_ASSIGN(BootstrapLogMessage);
};
} // namespace logging } // 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) \ #define MACH_LOG_STREAM(severity, mach_err) \
COMPACT_GOOGLE_LOG_EX_ ## severity(MachLogMessage, mach_err).stream() COMPACT_GOOGLE_LOG_EX_ ## severity(MachLogMessage, mach_err).stream()
#define MACH_VLOG_STREAM(verbose_level, mach_err) \ #define MACH_VLOG_STREAM(verbose_level, mach_err) \
...@@ -92,16 +86,38 @@ class BootstrapLogMessage : public logging::LogMessage { ...@@ -92,16 +86,38 @@ class BootstrapLogMessage : public logging::LogMessage {
#define MACH_DVLOG(verbose_level, mach_err) \ #define MACH_DVLOG(verbose_level, mach_err) \
LAZY_STREAM(MACH_VLOG_STREAM(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) \ #define MACH_DVLOG_IF(verbose_level, condition, mach_err) \
LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err) \ LAZY_STREAM(MACH_VLOG_STREAM(verbose_level, mach_err), \
DVLOG_IS_ON(verbose_level) && (condition)) MACH_DVLOG_IS_ON(verbose_level) && (condition))
#define MACH_DCHECK(condition, mach_err) \ #define MACH_DCHECK(condition, mach_err) \
LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), \ LAZY_STREAM(MACH_LOG_STREAM(FATAL, mach_err), \
DCHECK_IS_ON && !(condition)) \ DCHECK_IS_ON && !(condition)) \
<< "Check failed: " # condition << ". " << "Check failed: " # condition << ". "
#if !defined(OS_IOS)
namespace logging {
class BASE_EXPORT BootstrapLogMessage : public logging::LogMessage {
public:
BootstrapLogMessage(const char* file_path,
int line,
LogSeverity severity,
kern_return_t bootstrap_err);
~BootstrapLogMessage();
private:
kern_return_t bootstrap_err_;
DISALLOW_COPY_AND_ASSIGN(BootstrapLogMessage);
};
} // namespace logging
#define BOOTSTRAP_DVLOG_IS_ON MACH_DVLOG_IS_ON
#define BOOTSTRAP_LOG_STREAM(severity, bootstrap_err) \ #define BOOTSTRAP_LOG_STREAM(severity, bootstrap_err) \
COMPACT_GOOGLE_LOG_EX_ ## severity(BootstrapLogMessage, \ COMPACT_GOOGLE_LOG_EX_ ## severity(BootstrapLogMessage, \
bootstrap_err).stream() bootstrap_err).stream()
...@@ -136,14 +152,16 @@ class BootstrapLogMessage : public logging::LogMessage { ...@@ -136,14 +152,16 @@ class BootstrapLogMessage : public logging::LogMessage {
#define BOOTSTRAP_DVLOG(verbose_level, bootstrap_err) \ #define BOOTSTRAP_DVLOG(verbose_level, bootstrap_err) \
LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(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) \ #define BOOTSTRAP_DVLOG_IF(verbose_level, condition, bootstrap_err) \
LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err) \ LAZY_STREAM(BOOTSTRAP_VLOG_STREAM(verbose_level, bootstrap_err), \
DVLOG_IS_ON(verbose_level) && (condition)) BOOTSTRAP_DVLOG_IS_ON(verbose_level) && (condition))
#define BOOTSTRAP_DCHECK(condition, bootstrap_err) \ #define BOOTSTRAP_DCHECK(condition, bootstrap_err) \
LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), \ LAZY_STREAM(BOOTSTRAP_LOG_STREAM(FATAL, bootstrap_err), \
DCHECK_IS_ON && !(condition)) \ DCHECK_IS_ON && !(condition)) \
<< "Check failed: " # condition << ". " << "Check failed: " # condition << ". "
#endif // !OS_IOS
#endif // BASE_MAC_MACH_LOGGING_H_ #endif // BASE_MAC_MACH_LOGGING_H_
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#include <mach/mach.h> #include <mach/mach.h>
#include "base/basictypes.h"
#include "base/base_export.h" #include "base/base_export.h"
#include "base/basictypes.h"
namespace base { namespace base {
namespace mac { namespace mac {
......
...@@ -7,21 +7,21 @@ ...@@ -7,21 +7,21 @@
namespace base { namespace base {
namespace mac { namespace mac {
void ScopedMachVM::reset(mach_vm_address_t address, mach_vm_size_t size) { void ScopedMachVM::reset(vm_address_t address, vm_size_t size) {
DCHECK(address % PAGE_SIZE == 0); DCHECK(address % PAGE_SIZE == 0);
DCHECK(size % PAGE_SIZE == 0); DCHECK(size % PAGE_SIZE == 0);
if (size_) { if (size_) {
if (address_ < address) { if (address_ < address) {
mach_vm_deallocate(mach_task_self(), vm_deallocate(mach_task_self(),
address_, address_,
std::min(size_, address - address_)); std::min(size_, address - address_));
} }
if (address_ + size_ > address + size) { if (address_ + size_ > address + size) {
mach_vm_address_t deallocate_start = std::max(address_, address + size); vm_address_t deallocate_start = std::max(address_, address + size);
mach_vm_deallocate(mach_task_self(), vm_deallocate(mach_task_self(),
deallocate_start, deallocate_start,
address_ + size_ - deallocate_start); address_ + size_ - deallocate_start);
} }
} }
......
...@@ -6,15 +6,15 @@ ...@@ -6,15 +6,15 @@
#define BASE_MAC_SCOPED_MACH_VM_H_ #define BASE_MAC_SCOPED_MACH_VM_H_
#include <mach/mach.h> #include <mach/mach.h>
#include <mach/mach_vm.h>
#include <algorithm> #include <algorithm>
#include "base/base_export.h"
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/logging.h" #include "base/logging.h"
// Use ScopedMachVM to supervise ownership of pages in the current process // Use ScopedMachVM to supervise ownership of pages in the current process
// through the Mach VM subsystem. Pages allocated with mach_vm_allocate can be // through the Mach VM subsystem. Pages allocated with vm_allocate can be
// released when exiting a scope with ScopedMachVM. // released when exiting a scope with ScopedMachVM.
// //
// The Mach VM subsystem operates on a page-by-page basis, and a single VM // The Mach VM subsystem operates on a page-by-page basis, and a single VM
...@@ -33,10 +33,10 @@ ...@@ -33,10 +33,10 @@
// //
// Example: // Example:
// //
// mach_vm_address_t address = 0; // vm_address_t address = 0;
// mach_vm_size_t size = 12345; // This requested size is not page-aligned. // vm_size_t size = 12345; // This requested size is not page-aligned.
// kern_return_t kr = // kern_return_t kr =
// mach_vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE); // vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
// if (kr != KERN_SUCCESS) { // if (kr != KERN_SUCCESS) {
// return false; // return false;
// } // }
...@@ -45,9 +45,9 @@ ...@@ -45,9 +45,9 @@
namespace base { namespace base {
namespace mac { namespace mac {
class ScopedMachVM { class BASE_EXPORT ScopedMachVM {
public: public:
explicit ScopedMachVM(mach_vm_address_t address = 0, mach_vm_size_t size = 0) explicit ScopedMachVM(vm_address_t address = 0, vm_size_t size = 0)
: address_(address), : address_(address),
size_(size) { size_(size) {
DCHECK(address % PAGE_SIZE == 0); DCHECK(address % PAGE_SIZE == 0);
...@@ -56,17 +56,17 @@ class ScopedMachVM { ...@@ -56,17 +56,17 @@ class ScopedMachVM {
~ScopedMachVM() { ~ScopedMachVM() {
if (size_) { if (size_) {
mach_vm_deallocate(mach_task_self(), address_, size_); vm_deallocate(mach_task_self(), address_, size_);
} }
} }
void reset(mach_vm_address_t address = 0, mach_vm_size_t size = 0); void reset(vm_address_t address = 0, vm_size_t size = 0);
mach_vm_address_t address() const { vm_address_t address() const {
return address_; return address_;
} }
mach_vm_size_t size() const { vm_size_t size() const {
return size_; return size_;
} }
...@@ -81,8 +81,8 @@ class ScopedMachVM { ...@@ -81,8 +81,8 @@ class ScopedMachVM {
} }
private: private:
mach_vm_address_t address_; vm_address_t address_;
mach_vm_size_t size_; vm_size_t size_;
DISALLOW_COPY_AND_ASSIGN(ScopedMachVM); DISALLOW_COPY_AND_ASSIGN(ScopedMachVM);
}; };
......
...@@ -5,12 +5,13 @@ ...@@ -5,12 +5,13 @@
#include "base/memory/discardable_memory.h" #include "base/memory/discardable_memory.h"
#include <mach/mach.h> #include <mach/mach.h>
#include <sys/mman.h>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/logging.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_emulated.h"
#include "base/memory/discardable_memory_malloc.h" #include "base/memory/discardable_memory_malloc.h"
#include "base/memory/discardable_memory_manager.h" #include "base/memory/discardable_memory_manager.h"
...@@ -40,7 +41,9 @@ class DiscardableMemoryMac ...@@ -40,7 +41,9 @@ class DiscardableMemoryMac
public internal::DiscardableMemoryManagerAllocation { public internal::DiscardableMemoryManagerAllocation {
public: public:
explicit DiscardableMemoryMac(size_t bytes) explicit DiscardableMemoryMac(size_t bytes)
: buffer_(0), bytes_(bytes), is_locked_(false) { : memory_(0, 0),
bytes_(mach_vm_round_page(bytes)),
is_locked_(false) {
g_shared_state.Pointer()->manager.Register(this, bytes); g_shared_state.Pointer()->manager.Register(this, bytes);
} }
...@@ -50,8 +53,6 @@ class DiscardableMemoryMac ...@@ -50,8 +53,6 @@ class DiscardableMemoryMac
if (is_locked_) if (is_locked_)
Unlock(); Unlock();
g_shared_state.Pointer()->manager.Unregister(this); g_shared_state.Pointer()->manager.Unregister(this);
if (buffer_)
vm_deallocate(mach_task_self(), buffer_, bytes_);
} }
// Overridden from DiscardableMemory: // Overridden from DiscardableMemory:
...@@ -66,64 +67,78 @@ class DiscardableMemoryMac ...@@ -66,64 +67,78 @@ class DiscardableMemoryMac
return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
: DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
} }
virtual void Unlock() OVERRIDE { virtual void Unlock() OVERRIDE {
DCHECK(is_locked_); DCHECK(is_locked_);
g_shared_state.Pointer()->manager.ReleaseLock(this); g_shared_state.Pointer()->manager.ReleaseLock(this);
is_locked_ = false; is_locked_ = false;
} }
virtual void* Memory() const OVERRIDE { virtual void* Memory() const OVERRIDE {
DCHECK(is_locked_); DCHECK(is_locked_);
return reinterpret_cast<void*>(buffer_); return reinterpret_cast<void*>(memory_.address());
} }
// Overridden from internal::DiscardableMemoryManagerAllocation: // Overridden from internal::DiscardableMemoryManagerAllocation:
virtual bool AllocateAndAcquireLock() OVERRIDE { virtual bool AllocateAndAcquireLock() OVERRIDE {
bool persistent = true; bool persistent = true;
if (!buffer_) { kern_return_t ret;
kern_return_t ret = vm_allocate( if (!memory_.size()) {
vm_address_t address = 0;
ret = vm_allocate(
mach_task_self(), mach_task_self(),
&buffer_, &address,
bytes_, bytes_,
VM_FLAGS_PURGABLE | VM_FLAGS_ANYWHERE | kDiscardableMemoryTag); VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE | kDiscardableMemoryTag);
CHECK_EQ(KERN_SUCCESS, ret) << "wm_allocate() failed."; MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_allocate";
memory_.reset(address, bytes_);
persistent = false; persistent = false;
} }
#if !defined(NDEBUG) #if !defined(NDEBUG)
int status = mprotect( ret = vm_protect(mach_task_self(),
reinterpret_cast<void*>(buffer_), bytes_, PROT_READ | PROT_WRITE); memory_.address(),
DCHECK_EQ(0, status); memory_.size(),
FALSE,
VM_PROT_DEFAULT);
MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect";
#endif #endif
int state = VM_PURGABLE_NONVOLATILE; int state = VM_PURGABLE_NONVOLATILE;
kern_return_t ret = vm_purgable_control(mach_task_self(), ret = vm_purgable_control(mach_task_self(),
buffer_, memory_.address(),
VM_PURGABLE_SET_STATE, VM_PURGABLE_SET_STATE,
&state); &state);
CHECK_EQ(KERN_SUCCESS, ret) << "Failed to lock memory."; MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control";
if (state & VM_PURGABLE_EMPTY) if (state & VM_PURGABLE_EMPTY)
persistent = false; persistent = false;
return persistent; return persistent;
} }
virtual void ReleaseLock() OVERRIDE { virtual void ReleaseLock() OVERRIDE {
int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT;
kern_return_t ret = vm_purgable_control(mach_task_self(), kern_return_t ret = vm_purgable_control(mach_task_self(),
buffer_, memory_.address(),
VM_PURGABLE_SET_STATE, VM_PURGABLE_SET_STATE,
&state); &state);
CHECK_EQ(KERN_SUCCESS, ret) << "Failed to unlock memory."; MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control";
#if !defined(NDEBUG) #if !defined(NDEBUG)
int status = mprotect(reinterpret_cast<void*>(buffer_), bytes_, PROT_NONE); ret = vm_protect(mach_task_self(),
DCHECK_EQ(0, status); memory_.address(),
memory_.size(),
FALSE,
VM_PROT_NONE);
MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect";
#endif #endif
} }
virtual void Purge() OVERRIDE { virtual void Purge() OVERRIDE {
if (buffer_) { memory_.reset();
vm_deallocate(mach_task_self(), buffer_, bytes_);
buffer_ = 0;
}
} }
private: private:
vm_address_t buffer_; mac::ScopedMachVM memory_;
const size_t bytes_; const size_t bytes_;
bool is_locked_; bool is_locked_;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/mac_util.h" #include "base/mac/mac_util.h"
#include "base/mac/mach_logging.h"
#include "base/scoped_clear_errno.h" #include "base/scoped_clear_errno.h"
#include "third_party/apple_apsl/CFBase.h" #include "third_party/apple_apsl/CFBase.h"
#include "third_party/apple_apsl/malloc.h" #include "third_party/apple_apsl/malloc.h"
...@@ -222,10 +223,12 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone, ...@@ -222,10 +223,12 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone,
reinterpret_cast<vm_region_info_t>(&info), reinterpret_cast<vm_region_info_t>(&info),
&count, &count,
&unused); &unused);
CHECK(result == KERN_SUCCESS); MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_region";
result = mach_port_deallocate(mach_task_self(), unused); // The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but
CHECK(result == KERN_SUCCESS); // 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 // Does the region fully enclose the zone pointers? Possibly unwarranted
// simplification used: using the size of a full version 8 malloc zone rather // simplification used: using the size of a full version 8 malloc zone rather
...@@ -248,7 +251,7 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone, ...@@ -248,7 +251,7 @@ void DeprotectMallocZone(ChromeMallocZone* default_zone,
*reprotection_length, *reprotection_length,
false, false,
info.protection | VM_PROT_WRITE); info.protection | VM_PROT_WRITE);
CHECK(result == KERN_SUCCESS); MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
} }
} }
...@@ -646,7 +649,7 @@ void EnableTerminationOnOutOfMemory() { ...@@ -646,7 +649,7 @@ void EnableTerminationOnOutOfMemory() {
default_reprotection_length, default_reprotection_length,
false, false,
default_reprotection_value); default_reprotection_value);
CHECK(result == KERN_SUCCESS); MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
} }
if (purgeable_reprotection_start) { if (purgeable_reprotection_start) {
...@@ -655,7 +658,7 @@ void EnableTerminationOnOutOfMemory() { ...@@ -655,7 +658,7 @@ void EnableTerminationOnOutOfMemory() {
purgeable_reprotection_length, purgeable_reprotection_length,
false, false,
purgeable_reprotection_value); purgeable_reprotection_value);
CHECK(result == KERN_SUCCESS); MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect";
} }
#endif #endif
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/containers/hash_tables.h" #include "base/containers/hash_tables.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_port.h" #include "base/mac/scoped_mach_port.h"
#include "base/sys_info.h" #include "base/sys_info.h"
...@@ -116,7 +117,6 @@ size_t ProcessMetrics::GetPeakWorkingSetSize() const { ...@@ -116,7 +117,6 @@ size_t ProcessMetrics::GetPeakWorkingSetSize() const {
// shared_bytes is the size of shared resident memory. // shared_bytes is the size of shared resident memory.
bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
size_t* shared_bytes) { size_t* shared_bytes) {
kern_return_t kr;
size_t private_pages_count = 0; size_t private_pages_count = 0;
size_t shared_pages_count = 0; size_t shared_pages_count = 0;
...@@ -153,22 +153,26 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, ...@@ -153,22 +153,26 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
vm_region_top_info_data_t info; vm_region_top_info_data_t info;
mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
mach_port_t object_name; mach_port_t object_name;
kr = mach_vm_region(task, kern_return_t kr = mach_vm_region(task,
&address, &address,
&size, &size,
VM_REGION_TOP_INFO, VM_REGION_TOP_INFO,
(vm_region_info_t)&info, reinterpret_cast<vm_region_info_t>(&info),
&info_count, &info_count,
&object_name); &object_name);
if (kr == KERN_INVALID_ADDRESS) { if (kr == KERN_INVALID_ADDRESS) {
// We're at the end of the address space. // We're at the end of the address space.
break; break;
} else if (kr != KERN_SUCCESS) { } else if (kr != KERN_SUCCESS) {
DLOG(ERROR) << "Calling mach_vm_region failed with error: " MACH_DLOG(ERROR, kr) << "mach_vm_region";
<< mach_error_string(kr);
return false; 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) && if (IsAddressInSharedRegion(address, cpu_type) &&
info.share_mode != SM_PRIVATE) info.share_mode != SM_PRIVATE)
continue; continue;
...@@ -196,18 +200,10 @@ bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, ...@@ -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) if (private_bytes)
*private_bytes = private_pages_count * page_size; *private_bytes = private_pages_count * PAGE_SIZE;
if (shared_bytes) if (shared_bytes)
*shared_bytes = shared_pages_count * page_size; *shared_bytes = shared_pages_count * PAGE_SIZE;
return true; return true;
} }
...@@ -235,16 +231,14 @@ double ProcessMetrics::GetCPUUsage() { ...@@ -235,16 +231,14 @@ double ProcessMetrics::GetCPUUsage() {
if (task == MACH_PORT_NULL) if (task == MACH_PORT_NULL)
return 0; return 0;
kern_return_t kr;
// Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage() // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage()
// in libtop.c), but this is more concise and gives the same results: // in libtop.c), but this is more concise and gives the same results:
task_thread_times_info thread_info_data; task_thread_times_info thread_info_data;
mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
kr = task_info(task, kern_return_t kr = task_info(task,
TASK_THREAD_TIMES_INFO, TASK_THREAD_TIMES_INFO,
reinterpret_cast<task_info_t>(&thread_info_data), reinterpret_cast<task_info_t>(&thread_info_data),
&thread_info_count); &thread_info_count);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
// Most likely cause: |task| is a zombie. // Most likely cause: |task| is a zombie.
return 0; return 0;
...@@ -294,14 +288,12 @@ int ProcessMetrics::GetIdleWakeupsPerSecond() { ...@@ -294,14 +288,12 @@ int ProcessMetrics::GetIdleWakeupsPerSecond() {
if (task == MACH_PORT_NULL) if (task == MACH_PORT_NULL)
return 0; return 0;
kern_return_t kr;
task_power_info power_info_data; task_power_info power_info_data;
mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT; mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT;
kr = task_info(task, kern_return_t kr = task_info(task,
TASK_POWER_INFO, TASK_POWER_INFO,
reinterpret_cast<task_info_t>(&power_info_data), reinterpret_cast<task_info_t>(&power_info_data),
&power_info_count); &power_info_count);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
// Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system // 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. // where TASK_POWER_INFO isn't supported yet.
...@@ -362,19 +354,12 @@ size_t GetSystemCommitCharge() { ...@@ -362,19 +354,12 @@ size_t GetSystemCommitCharge() {
kern_return_t kr = host_statistics(host, HOST_VM_INFO, kern_return_t kr = host_statistics(host, HOST_VM_INFO,
reinterpret_cast<host_info_t>(&data), reinterpret_cast<host_info_t>(&data),
&count); &count);
if (kr) { if (kr != KERN_SUCCESS) {
DLOG(WARNING) << "Failed to fetch host statistics."; MACH_DLOG(WARNING, kr) << "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.";
return 0; return 0;
} }
return (data.active_count * page_size) / 1024; return (data.active_count * PAGE_SIZE) / 1024;
} }
} // namespace base } // namespace base
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/threading/thread_id_name_manager.h" #include "base/threading/thread_id_name_manager.h"
#include "base/tracked_objects.h" #include "base/tracked_objects.h"
...@@ -61,20 +62,19 @@ void SetPriorityNormal(mach_port_t mach_thread_id) { ...@@ -61,20 +62,19 @@ void SetPriorityNormal(mach_port_t mach_thread_id) {
// Please note that this call could fail in rare cases depending // Please note that this call could fail in rare cases depending
// on runtime conditions. // on runtime conditions.
thread_standard_policy policy; thread_standard_policy policy;
kern_return_t result = thread_policy_set(mach_thread_id, kern_return_t result =
THREAD_STANDARD_POLICY, thread_policy_set(mach_thread_id,
(thread_policy_t)&policy, THREAD_STANDARD_POLICY,
THREAD_STANDARD_POLICY_COUNT); reinterpret_cast<thread_policy_t>(&policy),
THREAD_STANDARD_POLICY_COUNT);
if (result != KERN_SUCCESS) 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, // Enables time-contraint policy and priority suitable for low-latency,
// glitch-resistant audio. // glitch-resistant audio.
void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
kern_return_t result;
// Increase thread priority to real-time. // Increase thread priority to real-time.
// Please note that the thread_policy_set() calls may fail in // Please note that the thread_policy_set() calls may fail in
...@@ -85,12 +85,13 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { ...@@ -85,12 +85,13 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
// Make thread fixed priority. // Make thread fixed priority.
thread_extended_policy_data_t policy; thread_extended_policy_data_t policy;
policy.timeshare = 0; // Set to 1 for a non-fixed thread. policy.timeshare = 0; // Set to 1 for a non-fixed thread.
result = thread_policy_set(mach_thread_id, kern_return_t result =
THREAD_EXTENDED_POLICY, thread_policy_set(mach_thread_id,
(thread_policy_t)&policy, THREAD_EXTENDED_POLICY,
THREAD_EXTENDED_POLICY_COUNT); reinterpret_cast<thread_policy_t>(&policy),
THREAD_EXTENDED_POLICY_COUNT);
if (result != KERN_SUCCESS) { if (result != KERN_SUCCESS) {
DVLOG(1) << "thread_policy_set() failure: " << result; MACH_DVLOG(1, result) << "thread_policy_set";
return; return;
} }
...@@ -99,10 +100,10 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { ...@@ -99,10 +100,10 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
precedence.importance = 63; precedence.importance = 63;
result = thread_policy_set(mach_thread_id, result = thread_policy_set(mach_thread_id,
THREAD_PRECEDENCE_POLICY, THREAD_PRECEDENCE_POLICY,
(thread_policy_t)&precedence, reinterpret_cast<thread_policy_t>(&precedence),
THREAD_PRECEDENCE_POLICY_COUNT); THREAD_PRECEDENCE_POLICY_COUNT);
if (result != KERN_SUCCESS) { if (result != KERN_SUCCESS) {
DVLOG(1) << "thread_policy_set() failure: " << result; MACH_DVLOG(1, result) << "thread_policy_set";
return; return;
} }
...@@ -133,7 +134,7 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { ...@@ -133,7 +134,7 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
mach_timebase_info_data_t tb_info; mach_timebase_info_data_t tb_info;
mach_timebase_info(&tb_info); mach_timebase_info(&tb_info);
double ms_to_abs_time = 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; thread_time_constraint_policy_data_t time_constraints;
time_constraints.period = kTimeQuantum * ms_to_abs_time; time_constraints.period = kTimeQuantum * ms_to_abs_time;
...@@ -141,12 +142,12 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { ...@@ -141,12 +142,12 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time; time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time;
time_constraints.preemptible = 0; time_constraints.preemptible = 0;
result = thread_policy_set(mach_thread_id, result =
THREAD_TIME_CONSTRAINT_POLICY, thread_policy_set(mach_thread_id,
(thread_policy_t)&time_constraints, THREAD_TIME_CONSTRAINT_POLICY,
THREAD_TIME_CONSTRAINT_POLICY_COUNT); reinterpret_cast<thread_policy_t>(&time_constraints),
if (result != KERN_SUCCESS) THREAD_TIME_CONSTRAINT_POLICY_COUNT);
DVLOG(1) << "thread_policy_set() failure: " << result; MACH_DVLOG_IF(1, result != KERN_SUCCESS, result) << "thread_policy_set";
return; return;
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_mach_port.h" #include "base/mac/scoped_mach_port.h"
...@@ -46,7 +47,7 @@ uint64_t ComputeCurrentTicks() { ...@@ -46,7 +47,7 @@ uint64_t ComputeCurrentTicks() {
// whether mach_timebase_info has already been called. This is // whether mach_timebase_info has already been called. This is
// recommended by Apple's QA1398. // recommended by Apple's QA1398.
kern_return_t kr = mach_timebase_info(&timebase_info); 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 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
...@@ -85,7 +86,7 @@ uint64_t ComputeThreadTicks() { ...@@ -85,7 +86,7 @@ uint64_t ComputeThreadTicks() {
THREAD_BASIC_INFO, THREAD_BASIC_INFO,
reinterpret_cast<thread_info_t>(&thread_info_data), reinterpret_cast<thread_info_t>(&thread_info_data),
&thread_info_count); &thread_info_count);
DCHECK_EQ(KERN_SUCCESS, kr); MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info";
return (thread_info_data.user_time.seconds * return (thread_info_data.user_time.seconds *
base::Time::kMicrosecondsPerSecond) + base::Time::kMicrosecondsPerSecond) +
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "base/mac/authorization_util.h" #include "base/mac/authorization_util.h"
#include "base/mac/bundle_locations.h" #include "base/mac/bundle_locations.h"
#include "base/mac/mac_logging.h" #include "base/mac/mac_logging.h"
#include "base/mac/mach_logging.h"
#import "base/mac/mac_util.h" #import "base/mac/mac_util.h"
#include "base/mac/scoped_authorizationref.h" #include "base/mac/scoped_authorizationref.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
...@@ -81,7 +82,7 @@ io_service_t CopyHDIXDriveServiceForMedia(io_service_t media) { ...@@ -81,7 +82,7 @@ io_service_t CopyHDIXDriveServiceForMedia(io_service_t media) {
kIORegistryIterateParents, kIORegistryIterateParents,
&iterator_ref); &iterator_ref);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
LOG(ERROR) << "IORegistryEntryCreateIterator: " << kr; MACH_LOG(ERROR, kr) << "IORegistryEntryCreateIterator";
return IO_OBJECT_NULL; return IO_OBJECT_NULL;
} }
base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref); base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref);
...@@ -207,7 +208,7 @@ bool IsPathOnReadOnlyDiskImage(const char path[], ...@@ -207,7 +208,7 @@ bool IsPathOnReadOnlyDiskImage(const char path[],
match_dict, match_dict,
&iterator_ref); &iterator_ref);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
LOG(ERROR) << "IOServiceGetMatchingServices: " << kr; MACH_LOG(ERROR, kr) << "IOServiceGetMatchingServices";
return false; return false;
} }
base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref); base::mac::ScopedIOObject<io_iterator_t> iterator(iterator_ref);
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/mach_logging.h"
#include "base/mac/scoped_mach_port.h" #include "base/mac/scoped_mach_port.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
...@@ -28,11 +29,6 @@ namespace content { ...@@ -28,11 +29,6 @@ namespace content {
namespace { 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. // Mach message structure used in the child as a sending message.
struct MachBroker_ChildSendMsg { struct MachBroker_ChildSendMsg {
mach_msg_header_t header; mach_msg_header_t header;
...@@ -64,8 +60,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate { ...@@ -64,8 +60,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
MACH_PORT_RIGHT_RECEIVE, MACH_PORT_RIGHT_RECEIVE,
&port); &port);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to allocate MachBroker server port: " MACH_LOG(ERROR, kr) << "mach_port_allocate";
<< MachErrorCode(kr);
return false; return false;
} }
...@@ -73,8 +68,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate { ...@@ -73,8 +68,7 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
kr = mach_port_insert_right( kr = mach_port_insert_right(
mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to insert send right for MachBroker server port: " MACH_LOG(ERROR, kr) << "mach_port_insert_right";
<< MachErrorCode(kr);
return false; return false;
} }
...@@ -96,33 +90,35 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate { ...@@ -96,33 +90,35 @@ class MachListenerThreadDelegate : public base::PlatformThread::Delegate {
msg.header.msgh_size = sizeof(msg); msg.header.msgh_size = sizeof(msg);
msg.header.msgh_local_port = server_port_.get(); 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; 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 // Use the kernel audit information to make sure this message is from
// a task that this process spawned. The kernel audit token contains the // a task that this process spawned. The kernel audit token contains the
// unspoofable pid of the task that sent the message. // 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) | // TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT); pid_t child_pid;
audit_token_to_au32(msg.trailer.msgh_audit,
kr = mach_msg(&msg.header, options, 0, sizeof(msg), server_port_, NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL);
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
if (kr == KERN_SUCCESS) { mach_port_t child_task_port = msg.child_task_port.name;
// TODO(rsesek): In the 10.7 SDK, there's audit_token_to_pid().
pid_t child_pid; // Take the lock and update the broker information.
audit_token_to_au32(msg.trailer.msgh_audit, base::AutoLock lock(broker_->GetLock());
NULL, NULL, NULL, NULL, NULL, &child_pid, NULL, NULL); broker_->FinalizePid(child_pid, child_task_port);
}
mach_port_t child_task_port = msg.child_task_port.name;
MACH_LOG(ERROR, kr) << "mach_msg";
// 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);
} }
private: private:
...@@ -141,7 +137,7 @@ bool MachBroker::ChildSendTaskPortToParent() { ...@@ -141,7 +137,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
mach_port_t bootstrap_port; mach_port_t bootstrap_port;
kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port); kern_return_t kr = task_get_bootstrap_port(mach_task_self(), &bootstrap_port);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to look up bootstrap port: " << MachErrorCode(kr); MACH_LOG(ERROR, kr) << "task_get_bootstrap_port";
return false; return false;
} }
...@@ -149,7 +145,7 @@ bool MachBroker::ChildSendTaskPortToParent() { ...@@ -149,7 +145,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
kr = bootstrap_look_up(bootstrap_port, kr = bootstrap_look_up(bootstrap_port,
const_cast<char*>(GetMachPortName().c_str()), &parent_port); const_cast<char*>(GetMachPortName().c_str()), &parent_port);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to look up named parent port: " << MachErrorCode(kr); BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
return false; return false;
} }
...@@ -169,7 +165,7 @@ bool MachBroker::ChildSendTaskPortToParent() { ...@@ -169,7 +165,7 @@ bool MachBroker::ChildSendTaskPortToParent() {
kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg), kr = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(msg),
0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); 0, MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL);
if (kr != KERN_SUCCESS) { if (kr != KERN_SUCCESS) {
LOG(ERROR) << "Failed to send task port to parent: " << MachErrorCode(kr); MACH_LOG(ERROR, kr) << "mach_msg";
return false; return false;
} }
...@@ -279,9 +275,7 @@ void MachBroker::InvalidatePid(base::ProcessHandle pid) { ...@@ -279,9 +275,7 @@ void MachBroker::InvalidatePid(base::ProcessHandle pid) {
kern_return_t kr = mach_port_deallocate(mach_task_self(), kern_return_t kr = mach_port_deallocate(mach_task_self(),
it->second); it->second);
LOG_IF(WARNING, kr != KERN_SUCCESS) MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "mach_port_deallocate";
<< "Failed to mach_port_deallocate mach task " << it->second
<< ", error " << MachErrorCode(kr);
mach_map_.erase(it); 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