| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 priority(0) { | 83 priority(0) { |
| 84 } | 84 } |
| 85 | 85 |
| 86 ProcessReader::Module::Module() : name(), reader(nullptr), timestamp(0) { | 86 ProcessReader::Module::Module() : name(), reader(nullptr), timestamp(0) { |
| 87 } | 87 } |
| 88 | 88 |
| 89 ProcessReader::Module::~Module() { | 89 ProcessReader::Module::~Module() { |
| 90 } | 90 } |
| 91 | 91 |
| 92 ProcessReader::ProcessReader() | 92 ProcessReader::ProcessReader() |
| 93 : kern_proc_info_(), | 93 : process_info_(), |
| 94 threads_(), | 94 threads_(), |
| 95 modules_(), | 95 modules_(), |
| 96 module_readers_(), | 96 module_readers_(), |
| 97 task_memory_(), | 97 task_memory_(), |
| 98 task_(TASK_NULL), | 98 task_(TASK_NULL), |
| 99 initialized_(), | 99 initialized_(), |
| 100 is_64_bit_(false), | 100 is_64_bit_(false), |
| 101 initialized_threads_(false), | 101 initialized_threads_(false), |
| 102 initialized_modules_(false) { | 102 initialized_modules_(false) { |
| 103 } | 103 } |
| 104 | 104 |
| 105 ProcessReader::~ProcessReader() { | 105 ProcessReader::~ProcessReader() { |
| 106 for (const Thread& thread : threads_) { | 106 for (const Thread& thread : threads_) { |
| 107 kern_return_t kr = mach_port_deallocate(mach_task_self(), thread.port); | 107 kern_return_t kr = mach_port_deallocate(mach_task_self(), thread.port); |
| 108 MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr) << "mach_port_deallocate"; | 108 MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr) << "mach_port_deallocate"; |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool ProcessReader::Initialize(task_t task) { | 112 bool ProcessReader::Initialize(task_t task) { |
| 113 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); | 113 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
| 114 | 114 |
| 115 pid_t pid; | 115 if (!process_info_.InitializeFromTask(task)) { |
| 116 kern_return_t kr = pid_for_task(task, &pid); | |
| 117 if (kr != KERN_SUCCESS) { | |
| 118 MACH_LOG(ERROR, kr) << "pid_for_task"; | |
| 119 return false; | 116 return false; |
| 120 } | 117 } |
| 121 | 118 |
| 122 int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; | 119 is_64_bit_ = process_info_.Is64Bit(); |
| 123 size_t len = sizeof(kern_proc_info_); | |
| 124 if (sysctl(mib, arraysize(mib), &kern_proc_info_, &len, nullptr, 0) != 0) { | |
| 125 PLOG(ERROR) << "sysctl for pid " << pid; | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 DCHECK_EQ(kern_proc_info_.kp_proc.p_pid, pid); | |
| 130 | |
| 131 is_64_bit_ = kern_proc_info_.kp_proc.p_flag & P_LP64; | |
| 132 | 120 |
| 133 task_memory_.reset(new TaskMemory(task)); | 121 task_memory_.reset(new TaskMemory(task)); |
| 134 task_ = task; | 122 task_ = task; |
| 135 | 123 |
| 136 INITIALIZATION_STATE_SET_VALID(initialized_); | 124 INITIALIZATION_STATE_SET_VALID(initialized_); |
| 137 return true; | 125 return true; |
| 138 } | 126 } |
| 139 | 127 |
| 140 void ProcessReader::StartTime(timeval* start_time) const { | |
| 141 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | |
| 142 *start_time = kern_proc_info_.kp_proc.p_starttime; | |
| 143 } | |
| 144 | |
| 145 bool ProcessReader::CPUTimes(timeval* user_time, timeval* system_time) const { | 128 bool ProcessReader::CPUTimes(timeval* user_time, timeval* system_time) const { |
| 146 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 129 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 147 | 130 |
| 148 // Calculate user and system time the same way the kernel does for | 131 // Calculate user and system time the same way the kernel does for |
| 149 // getrusage(). See 10.9.2 xnu-2422.90.20/bsd/kern/kern_resource.c calcru(). | 132 // getrusage(). See 10.9.2 xnu-2422.90.20/bsd/kern/kern_resource.c calcru(). |
| 150 timerclear(user_time); | 133 timerclear(user_time); |
| 151 timerclear(system_time); | 134 timerclear(system_time); |
| 152 | 135 |
| 153 // As of the 10.8 SDK, the preferred routine is MACH_TASK_BASIC_INFO. | 136 // As of the 10.8 SDK, the preferred routine is MACH_TASK_BASIC_INFO. |
| 154 // TASK_BASIC_INFO_64 is equivalent and works on earlier systems. | 137 // TASK_BASIC_INFO_64 is equivalent and works on earlier systems. |
| (...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 // The red zone would go lower into another region in memory, but no | 691 // The red zone would go lower into another region in memory, but no |
| 709 // region was found. Memory can only be captured to an address as low as | 692 // region was found. Memory can only be captured to an address as low as |
| 710 // the base address of the region already found. | 693 // the base address of the region already found. |
| 711 *start_address = *region_base; | 694 *start_address = *region_base; |
| 712 } | 695 } |
| 713 } | 696 } |
| 714 #endif | 697 #endif |
| 715 } | 698 } |
| 716 | 699 |
| 717 } // namespace crashpad | 700 } // namespace crashpad |
| OLD | NEW |