1a332978bSJason Molenda//===-- MachTask.cpp --------------------------------------------*- C++ -*-===// 2a332978bSJason Molenda// 32946cd70SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth// See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a332978bSJason Molenda// 7a332978bSJason Molenda//===----------------------------------------------------------------------===// 8a332978bSJason Molenda//---------------------------------------------------------------------- 9a332978bSJason Molenda// 10a332978bSJason Molenda// MachTask.cpp 11a332978bSJason Molenda// debugserver 12a332978bSJason Molenda// 13a332978bSJason Molenda// Created by Greg Clayton on 12/5/08. 14a332978bSJason Molenda// 15a332978bSJason Molenda//===----------------------------------------------------------------------===// 16a332978bSJason Molenda 17a332978bSJason Molenda#include "MachTask.h" 18a332978bSJason Molenda 19a332978bSJason Molenda// C Includes 20a332978bSJason Molenda 21a332978bSJason Molenda#include <mach-o/dyld_images.h> 22a332978bSJason Molenda#include <mach/mach_vm.h> 23a332978bSJason Molenda#import <sys/sysctl.h> 24a332978bSJason Molenda 2536a216eeSJason Molenda#if defined(__APPLE__) 2636a216eeSJason Molenda#include <pthread.h> 2736a216eeSJason Molenda#include <sched.h> 2836a216eeSJason Molenda#endif 2936a216eeSJason Molenda 30a332978bSJason Molenda// C++ Includes 31a332978bSJason Molenda#include <iomanip> 32a332978bSJason Molenda#include <sstream> 33a332978bSJason Molenda 34a332978bSJason Molenda// Other libraries and framework includes 35a332978bSJason Molenda// Project includes 36a332978bSJason Molenda#include "CFUtils.h" 37a332978bSJason Molenda#include "DNB.h" 38b9c1b51eSKate Stone#include "DNBDataRef.h" 39a332978bSJason Molenda#include "DNBError.h" 40a332978bSJason Molenda#include "DNBLog.h" 41a332978bSJason Molenda#include "MachProcess.h" 42a332978bSJason Molenda 43a332978bSJason Molenda#ifdef WITH_SPRINGBOARD 44a332978bSJason Molenda 45a332978bSJason Molenda#include <CoreFoundation/CoreFoundation.h> 46a332978bSJason Molenda#include <SpringBoardServices/SBSWatchdogAssertion.h> 47b9c1b51eSKate Stone#include <SpringBoardServices/SpringBoardServer.h> 48a332978bSJason Molenda 49a332978bSJason Molenda#endif 50a332978bSJason Molenda 51a332978bSJason Molenda#ifdef WITH_BKS 52b9c1b51eSKate Stoneextern "C" { 53a332978bSJason Molenda#import <BackBoardServices/BKSWatchdogAssertion.h> 54b9c1b51eSKate Stone#import <BackBoardServices/BackBoardServices.h> 55b9c1b51eSKate Stone#import <Foundation/Foundation.h> 56a332978bSJason Molenda} 57a332978bSJason Molenda#endif 58a332978bSJason Molenda 59705b1809SJason Molenda#include <AvailabilityMacros.h> 60705b1809SJason Molenda 61705b1809SJason Molenda#ifdef LLDB_ENERGY 62705b1809SJason Molenda#include <mach/mach_time.h> 63705b1809SJason Molenda#include <pmenergy.h> 64705b1809SJason Molenda#include <pmsample.h> 65705b1809SJason Molenda#endif 66705b1809SJason Molenda 67480a3835SJason Molendaextern "C" int 68480a3835SJason Molendaproc_get_cpumon_params(pid_t pid, int *percentage, 69480a3835SJason Molenda int *interval); // <libproc_internal.h> SPI 70480a3835SJason Molenda 71a332978bSJason Molenda//---------------------------------------------------------------------- 72a332978bSJason Molenda// MachTask constructor 73a332978bSJason Molenda//---------------------------------------------------------------------- 74b9c1b51eSKate StoneMachTask::MachTask(MachProcess *process) 75b9c1b51eSKate Stone : m_process(process), m_task(TASK_NULL), m_vm_memory(), 7683a131b2SJason Molenda m_exception_thread(0), m_exception_port(MACH_PORT_NULL), 7783a131b2SJason Molenda m_exec_will_be_suspended(false), m_do_double_resume(false) { 78a332978bSJason Molenda memset(&m_exc_port_info, 0, sizeof(m_exc_port_info)); 79a332978bSJason Molenda} 80a332978bSJason Molenda 81a332978bSJason Molenda//---------------------------------------------------------------------- 82a332978bSJason Molenda// Destructor 83a332978bSJason Molenda//---------------------------------------------------------------------- 84b9c1b51eSKate StoneMachTask::~MachTask() { Clear(); } 85a332978bSJason Molenda 86a332978bSJason Molenda//---------------------------------------------------------------------- 87a332978bSJason Molenda// MachTask::Suspend 88a332978bSJason Molenda//---------------------------------------------------------------------- 89b9c1b51eSKate Stonekern_return_t MachTask::Suspend() { 90a332978bSJason Molenda DNBError err; 91a332978bSJason Molenda task_t task = TaskPort(); 92a332978bSJason Molenda err = ::task_suspend(task); 93a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 94a332978bSJason Molenda err.LogThreaded("::task_suspend ( target_task = 0x%4.4x )", task); 9597206d57SZachary Turner return err.Status(); 96a332978bSJason Molenda} 97a332978bSJason Molenda 98a332978bSJason Molenda//---------------------------------------------------------------------- 99a332978bSJason Molenda// MachTask::Resume 100a332978bSJason Molenda//---------------------------------------------------------------------- 101b9c1b51eSKate Stonekern_return_t MachTask::Resume() { 102a332978bSJason Molenda struct task_basic_info task_info; 103a332978bSJason Molenda task_t task = TaskPort(); 104a332978bSJason Molenda if (task == TASK_NULL) 105a332978bSJason Molenda return KERN_INVALID_ARGUMENT; 106a332978bSJason Molenda 107a332978bSJason Molenda DNBError err; 108a332978bSJason Molenda err = BasicInfo(task, &task_info); 109a332978bSJason Molenda 110b9c1b51eSKate Stone if (err.Success()) { 11183a131b2SJason Molenda if (m_do_double_resume && task_info.suspend_count == 2) { 11283a131b2SJason Molenda err = ::task_resume(task); 11383a131b2SJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 11483a131b2SJason Molenda err.LogThreaded("::task_resume double-resume after exec-start-stopped " 11583a131b2SJason Molenda "( target_task = 0x%4.4x )", task); 11683a131b2SJason Molenda } 11783a131b2SJason Molenda m_do_double_resume = false; 11883a131b2SJason Molenda 119a332978bSJason Molenda // task_resume isn't counted like task_suspend calls are, are, so if the 120a332978bSJason Molenda // task is not suspended, don't try and resume it since it is already 121a332978bSJason Molenda // running 122b9c1b51eSKate Stone if (task_info.suspend_count > 0) { 123a332978bSJason Molenda err = ::task_resume(task); 124a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 125a332978bSJason Molenda err.LogThreaded("::task_resume ( target_task = 0x%4.4x )", task); 126a332978bSJason Molenda } 127a332978bSJason Molenda } 12897206d57SZachary Turner return err.Status(); 129a332978bSJason Molenda} 130a332978bSJason Molenda 131a332978bSJason Molenda//---------------------------------------------------------------------- 132a332978bSJason Molenda// MachTask::ExceptionPort 133a332978bSJason Molenda//---------------------------------------------------------------------- 134b9c1b51eSKate Stonemach_port_t MachTask::ExceptionPort() const { return m_exception_port; } 135a332978bSJason Molenda 136a332978bSJason Molenda//---------------------------------------------------------------------- 137a332978bSJason Molenda// MachTask::ExceptionPortIsValid 138a332978bSJason Molenda//---------------------------------------------------------------------- 139b9c1b51eSKate Stonebool MachTask::ExceptionPortIsValid() const { 140a332978bSJason Molenda return MACH_PORT_VALID(m_exception_port); 141a332978bSJason Molenda} 142a332978bSJason Molenda 143a332978bSJason Molenda//---------------------------------------------------------------------- 144a332978bSJason Molenda// MachTask::Clear 145a332978bSJason Molenda//---------------------------------------------------------------------- 146b9c1b51eSKate Stonevoid MachTask::Clear() { 147a332978bSJason Molenda // Do any cleanup needed for this task 148a332978bSJason Molenda m_task = TASK_NULL; 149a332978bSJason Molenda m_exception_thread = 0; 150a332978bSJason Molenda m_exception_port = MACH_PORT_NULL; 15183a131b2SJason Molenda m_exec_will_be_suspended = false; 15283a131b2SJason Molenda m_do_double_resume = false; 153a332978bSJason Molenda} 154a332978bSJason Molenda 155a332978bSJason Molenda//---------------------------------------------------------------------- 156a332978bSJason Molenda// MachTask::SaveExceptionPortInfo 157a332978bSJason Molenda//---------------------------------------------------------------------- 158b9c1b51eSKate Stonekern_return_t MachTask::SaveExceptionPortInfo() { 159a332978bSJason Molenda return m_exc_port_info.Save(TaskPort()); 160a332978bSJason Molenda} 161a332978bSJason Molenda 162a332978bSJason Molenda//---------------------------------------------------------------------- 163a332978bSJason Molenda// MachTask::RestoreExceptionPortInfo 164a332978bSJason Molenda//---------------------------------------------------------------------- 165b9c1b51eSKate Stonekern_return_t MachTask::RestoreExceptionPortInfo() { 166a332978bSJason Molenda return m_exc_port_info.Restore(TaskPort()); 167a332978bSJason Molenda} 168a332978bSJason Molenda 169a332978bSJason Molenda//---------------------------------------------------------------------- 170a332978bSJason Molenda// MachTask::ReadMemory 171a332978bSJason Molenda//---------------------------------------------------------------------- 172b9c1b51eSKate Stonenub_size_t MachTask::ReadMemory(nub_addr_t addr, nub_size_t size, void *buf) { 173a332978bSJason Molenda nub_size_t n = 0; 174a332978bSJason Molenda task_t task = TaskPort(); 175b9c1b51eSKate Stone if (task != TASK_NULL) { 176a332978bSJason Molenda n = m_vm_memory.Read(task, addr, buf, size); 177a332978bSJason Molenda 178b9c1b51eSKate Stone DNBLogThreadedIf(LOG_MEMORY, "MachTask::ReadMemory ( addr = 0x%8.8llx, " 179b9c1b51eSKate Stone "size = %llu, buf = %p) => %llu bytes read", 180b9c1b51eSKate Stone (uint64_t)addr, (uint64_t)size, buf, (uint64_t)n); 181b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_MEMORY_DATA_LONG) || 182b9c1b51eSKate Stone (DNBLogCheckLogBit(LOG_MEMORY_DATA_SHORT) && size <= 8)) { 183a332978bSJason Molenda DNBDataRef data((uint8_t *)buf, n, false); 184b9c1b51eSKate Stone data.Dump(0, static_cast<DNBDataRef::offset_t>(n), addr, 185b9c1b51eSKate Stone DNBDataRef::TypeUInt8, 16); 186a332978bSJason Molenda } 187a332978bSJason Molenda } 188a332978bSJason Molenda return n; 189a332978bSJason Molenda} 190a332978bSJason Molenda 191a332978bSJason Molenda//---------------------------------------------------------------------- 192a332978bSJason Molenda// MachTask::WriteMemory 193a332978bSJason Molenda//---------------------------------------------------------------------- 194b9c1b51eSKate Stonenub_size_t MachTask::WriteMemory(nub_addr_t addr, nub_size_t size, 195b9c1b51eSKate Stone const void *buf) { 196a332978bSJason Molenda nub_size_t n = 0; 197a332978bSJason Molenda task_t task = TaskPort(); 198b9c1b51eSKate Stone if (task != TASK_NULL) { 199a332978bSJason Molenda n = m_vm_memory.Write(task, addr, buf, size); 200b9c1b51eSKate Stone DNBLogThreadedIf(LOG_MEMORY, "MachTask::WriteMemory ( addr = 0x%8.8llx, " 201b9c1b51eSKate Stone "size = %llu, buf = %p) => %llu bytes written", 202b9c1b51eSKate Stone (uint64_t)addr, (uint64_t)size, buf, (uint64_t)n); 203b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_MEMORY_DATA_LONG) || 204b9c1b51eSKate Stone (DNBLogCheckLogBit(LOG_MEMORY_DATA_SHORT) && size <= 8)) { 20507d95614SVedant Kumar DNBDataRef data((const uint8_t *)buf, n, false); 206b9c1b51eSKate Stone data.Dump(0, static_cast<DNBDataRef::offset_t>(n), addr, 207b9c1b51eSKate Stone DNBDataRef::TypeUInt8, 16); 208a332978bSJason Molenda } 209a332978bSJason Molenda } 210a332978bSJason Molenda return n; 211a332978bSJason Molenda} 212a332978bSJason Molenda 213a332978bSJason Molenda//---------------------------------------------------------------------- 214a332978bSJason Molenda// MachTask::MemoryRegionInfo 215a332978bSJason Molenda//---------------------------------------------------------------------- 216b9c1b51eSKate Stoneint MachTask::GetMemoryRegionInfo(nub_addr_t addr, DNBRegionInfo *region_info) { 217a332978bSJason Molenda task_t task = TaskPort(); 218a332978bSJason Molenda if (task == TASK_NULL) 219a332978bSJason Molenda return -1; 220a332978bSJason Molenda 221a332978bSJason Molenda int ret = m_vm_memory.GetMemoryRegionInfo(task, addr, region_info); 222b9c1b51eSKate Stone DNBLogThreadedIf(LOG_MEMORY, "MachTask::MemoryRegionInfo ( addr = 0x%8.8llx " 223b9c1b51eSKate Stone ") => %i (start = 0x%8.8llx, size = 0x%8.8llx, " 224b9c1b51eSKate Stone "permissions = %u)", 225b9c1b51eSKate Stone (uint64_t)addr, ret, (uint64_t)region_info->addr, 226b9c1b51eSKate Stone (uint64_t)region_info->size, region_info->permissions); 227a332978bSJason Molenda return ret; 228a332978bSJason Molenda} 229a332978bSJason Molenda 230b9c1b51eSKate Stone#define TIME_VALUE_TO_TIMEVAL(a, r) \ 231b9c1b51eSKate Stone do { \ 232a332978bSJason Molenda (r)->tv_sec = (a)->seconds; \ 233a332978bSJason Molenda (r)->tv_usec = (a)->microseconds; \ 234a332978bSJason Molenda } while (0) 235a332978bSJason Molenda 236a332978bSJason Molenda// We should consider moving this into each MacThread. 237b9c1b51eSKate Stonestatic void get_threads_profile_data(DNBProfileDataScanType scanType, 238b9c1b51eSKate Stone task_t task, nub_process_t pid, 239b9c1b51eSKate Stone std::vector<uint64_t> &threads_id, 240b9c1b51eSKate Stone std::vector<std::string> &threads_name, 241b9c1b51eSKate Stone std::vector<uint64_t> &threads_used_usec) { 242a332978bSJason Molenda kern_return_t kr; 243a332978bSJason Molenda thread_act_array_t threads; 244a332978bSJason Molenda mach_msg_type_number_t tcnt; 245a332978bSJason Molenda 246a332978bSJason Molenda kr = task_threads(task, &threads, &tcnt); 247a332978bSJason Molenda if (kr != KERN_SUCCESS) 248a332978bSJason Molenda return; 249a332978bSJason Molenda 250b9c1b51eSKate Stone for (mach_msg_type_number_t i = 0; i < tcnt; i++) { 251a332978bSJason Molenda thread_identifier_info_data_t identifier_info; 252a332978bSJason Molenda mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT; 253b9c1b51eSKate Stone kr = ::thread_info(threads[i], THREAD_IDENTIFIER_INFO, 254b9c1b51eSKate Stone (thread_info_t)&identifier_info, &count); 255b9c1b51eSKate Stone if (kr != KERN_SUCCESS) 256b9c1b51eSKate Stone continue; 257a332978bSJason Molenda 258a332978bSJason Molenda thread_basic_info_data_t basic_info; 259a332978bSJason Molenda count = THREAD_BASIC_INFO_COUNT; 260b9c1b51eSKate Stone kr = ::thread_info(threads[i], THREAD_BASIC_INFO, 261b9c1b51eSKate Stone (thread_info_t)&basic_info, &count); 262b9c1b51eSKate Stone if (kr != KERN_SUCCESS) 263b9c1b51eSKate Stone continue; 264a332978bSJason Molenda 265b9c1b51eSKate Stone if ((basic_info.flags & TH_FLAGS_IDLE) == 0) { 266b9c1b51eSKate Stone nub_thread_t tid = 267b9c1b51eSKate Stone MachThread::GetGloballyUniqueThreadIDForMachPortID(threads[i]); 268a332978bSJason Molenda threads_id.push_back(tid); 269a332978bSJason Molenda 270b9c1b51eSKate Stone if ((scanType & eProfileThreadName) && 271b9c1b51eSKate Stone (identifier_info.thread_handle != 0)) { 272a332978bSJason Molenda struct proc_threadinfo proc_threadinfo; 273b9c1b51eSKate Stone int len = ::proc_pidinfo(pid, PROC_PIDTHREADINFO, 274b9c1b51eSKate Stone identifier_info.thread_handle, 275b9c1b51eSKate Stone &proc_threadinfo, PROC_PIDTHREADINFO_SIZE); 276b9c1b51eSKate Stone if (len && proc_threadinfo.pth_name[0]) { 277a332978bSJason Molenda threads_name.push_back(proc_threadinfo.pth_name); 278b9c1b51eSKate Stone } else { 279a332978bSJason Molenda threads_name.push_back(""); 280a332978bSJason Molenda } 281b9c1b51eSKate Stone } else { 282a332978bSJason Molenda threads_name.push_back(""); 283a332978bSJason Molenda } 284a332978bSJason Molenda struct timeval tv; 285a332978bSJason Molenda struct timeval thread_tv; 286a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&basic_info.user_time, &thread_tv); 287a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&basic_info.system_time, &tv); 288a332978bSJason Molenda timeradd(&thread_tv, &tv, &thread_tv); 289a332978bSJason Molenda uint64_t used_usec = thread_tv.tv_sec * 1000000ULL + thread_tv.tv_usec; 290a332978bSJason Molenda threads_used_usec.push_back(used_usec); 291a332978bSJason Molenda } 292a332978bSJason Molenda 293aeb9a06fSJason Molenda mach_port_deallocate(mach_task_self(), threads[i]); 294a332978bSJason Molenda } 295b9c1b51eSKate Stone mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)(uintptr_t)threads, 296b9c1b51eSKate Stone tcnt * sizeof(*threads)); 297a332978bSJason Molenda} 298a332978bSJason Molenda 299a332978bSJason Molenda#define RAW_HEXBASE std::setfill('0') << std::hex << std::right 300a332978bSJason Molenda#define DECIMAL std::dec << std::setfill(' ') 301b9c1b51eSKate Stonestd::string MachTask::GetProfileData(DNBProfileDataScanType scanType) { 302a332978bSJason Molenda std::string result; 303a332978bSJason Molenda 304a332978bSJason Molenda static int32_t numCPU = -1; 305a332978bSJason Molenda struct host_cpu_load_info host_info; 306b9c1b51eSKate Stone if (scanType & eProfileHostCPU) { 307a332978bSJason Molenda int32_t mib[] = {CTL_HW, HW_AVAILCPU}; 308a332978bSJason Molenda size_t len = sizeof(numCPU); 309b9c1b51eSKate Stone if (numCPU == -1) { 310b9c1b51eSKate Stone if (sysctl(mib, sizeof(mib) / sizeof(int32_t), &numCPU, &len, NULL, 0) != 311b9c1b51eSKate Stone 0) 312a332978bSJason Molenda return result; 313a332978bSJason Molenda } 314a332978bSJason Molenda 315a332978bSJason Molenda mach_port_t localHost = mach_host_self(); 316a332978bSJason Molenda mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT; 317b9c1b51eSKate Stone kern_return_t kr = host_statistics(localHost, HOST_CPU_LOAD_INFO, 318b9c1b51eSKate Stone (host_info_t)&host_info, &count); 319a332978bSJason Molenda if (kr != KERN_SUCCESS) 320a332978bSJason Molenda return result; 321a332978bSJason Molenda } 322a332978bSJason Molenda 323a332978bSJason Molenda task_t task = TaskPort(); 324a332978bSJason Molenda if (task == TASK_NULL) 325a332978bSJason Molenda return result; 326a332978bSJason Molenda 327705b1809SJason Molenda pid_t pid = m_process->ProcessID(); 328705b1809SJason Molenda 329a332978bSJason Molenda struct task_basic_info task_info; 330a332978bSJason Molenda DNBError err; 331a332978bSJason Molenda err = BasicInfo(task, &task_info); 332a332978bSJason Molenda 333a332978bSJason Molenda if (!err.Success()) 334a332978bSJason Molenda return result; 335a332978bSJason Molenda 336a332978bSJason Molenda uint64_t elapsed_usec = 0; 337a332978bSJason Molenda uint64_t task_used_usec = 0; 338b9c1b51eSKate Stone if (scanType & eProfileCPU) { 339a332978bSJason Molenda // Get current used time. 340a332978bSJason Molenda struct timeval current_used_time; 341a332978bSJason Molenda struct timeval tv; 342a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&task_info.user_time, ¤t_used_time); 343a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&task_info.system_time, &tv); 344a332978bSJason Molenda timeradd(¤t_used_time, &tv, ¤t_used_time); 345b9c1b51eSKate Stone task_used_usec = 346b9c1b51eSKate Stone current_used_time.tv_sec * 1000000ULL + current_used_time.tv_usec; 347a332978bSJason Molenda 348a332978bSJason Molenda struct timeval current_elapsed_time; 349a332978bSJason Molenda int res = gettimeofday(¤t_elapsed_time, NULL); 350b9c1b51eSKate Stone if (res == 0) { 351b9c1b51eSKate Stone elapsed_usec = current_elapsed_time.tv_sec * 1000000ULL + 352b9c1b51eSKate Stone current_elapsed_time.tv_usec; 353a332978bSJason Molenda } 354a332978bSJason Molenda } 355a332978bSJason Molenda 356a332978bSJason Molenda std::vector<uint64_t> threads_id; 357a332978bSJason Molenda std::vector<std::string> threads_name; 358a332978bSJason Molenda std::vector<uint64_t> threads_used_usec; 359a332978bSJason Molenda 360b9c1b51eSKate Stone if (scanType & eProfileThreadsCPU) { 361b9c1b51eSKate Stone get_threads_profile_data(scanType, task, pid, threads_id, threads_name, 362b9c1b51eSKate Stone threads_used_usec); 363a332978bSJason Molenda } 364a332978bSJason Molenda 36574c14819SHan Ming Ong vm_statistics64_data_t vminfo; 36641423692SHan Ming Ong uint64_t physical_memory = 0; 36741423692SHan Ming Ong uint64_t anonymous = 0; 36841423692SHan Ming Ong uint64_t phys_footprint = 0; 36941423692SHan Ming Ong uint64_t memory_cap = 0; 370b9c1b51eSKate Stone if (m_vm_memory.GetMemoryProfile(scanType, task, task_info, 371b9c1b51eSKate Stone m_process->GetCPUType(), pid, vminfo, 37241423692SHan Ming Ong physical_memory, anonymous, 37341423692SHan Ming Ong phys_footprint, memory_cap)) { 374a332978bSJason Molenda std::ostringstream profile_data_stream; 375a332978bSJason Molenda 376b9c1b51eSKate Stone if (scanType & eProfileHostCPU) { 377a332978bSJason Molenda profile_data_stream << "num_cpu:" << numCPU << ';'; 378b9c1b51eSKate Stone profile_data_stream << "host_user_ticks:" 379b9c1b51eSKate Stone << host_info.cpu_ticks[CPU_STATE_USER] << ';'; 380b9c1b51eSKate Stone profile_data_stream << "host_sys_ticks:" 381b9c1b51eSKate Stone << host_info.cpu_ticks[CPU_STATE_SYSTEM] << ';'; 382b9c1b51eSKate Stone profile_data_stream << "host_idle_ticks:" 383b9c1b51eSKate Stone << host_info.cpu_ticks[CPU_STATE_IDLE] << ';'; 384a332978bSJason Molenda } 385a332978bSJason Molenda 386b9c1b51eSKate Stone if (scanType & eProfileCPU) { 387a332978bSJason Molenda profile_data_stream << "elapsed_usec:" << elapsed_usec << ';'; 388a332978bSJason Molenda profile_data_stream << "task_used_usec:" << task_used_usec << ';'; 389a332978bSJason Molenda } 390a332978bSJason Molenda 391b9c1b51eSKate Stone if (scanType & eProfileThreadsCPU) { 392ee2ed525SGreg Clayton const size_t num_threads = threads_id.size(); 393b9c1b51eSKate Stone for (size_t i = 0; i < num_threads; i++) { 394b9c1b51eSKate Stone profile_data_stream << "thread_used_id:" << std::hex << threads_id[i] 395b9c1b51eSKate Stone << std::dec << ';'; 396b9c1b51eSKate Stone profile_data_stream << "thread_used_usec:" << threads_used_usec[i] 397b9c1b51eSKate Stone << ';'; 398a332978bSJason Molenda 399b9c1b51eSKate Stone if (scanType & eProfileThreadName) { 400a332978bSJason Molenda profile_data_stream << "thread_used_name:"; 401ee2ed525SGreg Clayton const size_t len = threads_name[i].size(); 402b9c1b51eSKate Stone if (len) { 403a332978bSJason Molenda const char *thread_name = threads_name[i].c_str(); 404a332978bSJason Molenda // Make sure that thread name doesn't interfere with our delimiter. 405a332978bSJason Molenda profile_data_stream << RAW_HEXBASE << std::setw(2); 406a332978bSJason Molenda const uint8_t *ubuf8 = (const uint8_t *)(thread_name); 407b9c1b51eSKate Stone for (size_t j = 0; j < len; j++) { 408a332978bSJason Molenda profile_data_stream << (uint32_t)(ubuf8[j]); 409a332978bSJason Molenda } 410a332978bSJason Molenda // Reset back to DECIMAL. 411a332978bSJason Molenda profile_data_stream << DECIMAL; 412a332978bSJason Molenda } 413a332978bSJason Molenda profile_data_stream << ';'; 414a332978bSJason Molenda } 415a332978bSJason Molenda } 416a332978bSJason Molenda } 417a332978bSJason Molenda 418a332978bSJason Molenda if (scanType & eProfileHostMemory) 419a332978bSJason Molenda profile_data_stream << "total:" << physical_memory << ';'; 420a332978bSJason Molenda 421b9c1b51eSKate Stone if (scanType & eProfileMemory) { 42274c14819SHan Ming Ong static vm_size_t pagesize = vm_kernel_page_size; 423a332978bSJason Molenda 42474c14819SHan Ming Ong // This mimicks Activity Monitor. 425b9c1b51eSKate Stone uint64_t total_used_count = 426b9c1b51eSKate Stone (physical_memory / pagesize) - 427b9c1b51eSKate Stone (vminfo.free_count - vminfo.speculative_count) - 428b9c1b51eSKate Stone vminfo.external_page_count - vminfo.purgeable_count; 429a332978bSJason Molenda profile_data_stream << "used:" << total_used_count * pagesize << ';'; 430a332978bSJason Molenda 431b9c1b51eSKate Stone if (scanType & eProfileMemoryAnonymous) { 432a332978bSJason Molenda profile_data_stream << "anonymous:" << anonymous << ';'; 433a332978bSJason Molenda } 434017edbddSHan Ming Ong 435017edbddSHan Ming Ong profile_data_stream << "phys_footprint:" << phys_footprint << ';'; 436a332978bSJason Molenda } 437a332978bSJason Molenda 43841423692SHan Ming Ong if (scanType & eProfileMemoryCap) { 43941423692SHan Ming Ong profile_data_stream << "mem_cap:" << memory_cap << ';'; 44041423692SHan Ming Ong } 44141423692SHan Ming Ong 442705b1809SJason Molenda#ifdef LLDB_ENERGY 44383d178deSHan Ming Ong if (scanType & eProfileEnergy) { 444705b1809SJason Molenda struct rusage_info_v2 info; 445705b1809SJason Molenda int rc = proc_pid_rusage(pid, RUSAGE_INFO_V2, (rusage_info_t *)&info); 446b9c1b51eSKate Stone if (rc == 0) { 447705b1809SJason Molenda uint64_t now = mach_absolute_time(); 448705b1809SJason Molenda pm_task_energy_data_t pm_energy; 449705b1809SJason Molenda memset(&pm_energy, 0, sizeof(pm_energy)); 450705b1809SJason Molenda /* 451705b1809SJason Molenda * Disable most features of pm_sample_pid. It will gather 452705b1809SJason Molenda * network/GPU/WindowServer information; fill in the rest. 453705b1809SJason Molenda */ 454b9c1b51eSKate Stone pm_sample_task_and_pid(task, pid, &pm_energy, now, 455b9c1b51eSKate Stone PM_SAMPLE_ALL & ~PM_SAMPLE_NAME & 456b9c1b51eSKate Stone ~PM_SAMPLE_INTERVAL & ~PM_SAMPLE_CPU & 457b9c1b51eSKate Stone ~PM_SAMPLE_DISK); 458705b1809SJason Molenda pm_energy.sti.total_user = info.ri_user_time; 459705b1809SJason Molenda pm_energy.sti.total_system = info.ri_system_time; 460705b1809SJason Molenda pm_energy.sti.task_interrupt_wakeups = info.ri_interrupt_wkups; 461705b1809SJason Molenda pm_energy.sti.task_platform_idle_wakeups = info.ri_pkg_idle_wkups; 462705b1809SJason Molenda pm_energy.diskio_bytesread = info.ri_diskio_bytesread; 463705b1809SJason Molenda pm_energy.diskio_byteswritten = info.ri_diskio_byteswritten; 464705b1809SJason Molenda pm_energy.pageins = info.ri_pageins; 465705b1809SJason Molenda 466b9c1b51eSKate Stone uint64_t total_energy = 467b9c1b51eSKate Stone (uint64_t)(pm_energy_impact(&pm_energy) * NSEC_PER_SEC); 468705b1809SJason Molenda // uint64_t process_age = now - info.ri_proc_start_abstime; 469b9c1b51eSKate Stone // uint64_t avg_energy = 100.0 * (double)total_energy / 470b9c1b51eSKate Stone // (double)process_age; 471705b1809SJason Molenda 472705b1809SJason Molenda profile_data_stream << "energy:" << total_energy << ';'; 473705b1809SJason Molenda } 474705b1809SJason Molenda } 475705b1809SJason Molenda#endif 476705b1809SJason Molenda 477480a3835SJason Molenda if (scanType & eProfileEnergyCPUCap) { 478480a3835SJason Molenda int percentage = -1; 479480a3835SJason Molenda int interval = -1; 480480a3835SJason Molenda int result = proc_get_cpumon_params(pid, &percentage, &interval); 481480a3835SJason Molenda if ((result == 0) && (percentage >= 0) && (interval >= 0)) { 482480a3835SJason Molenda profile_data_stream << "cpu_cap_p:" << percentage << ';'; 483480a3835SJason Molenda profile_data_stream << "cpu_cap_t:" << interval << ';'; 484480a3835SJason Molenda } 485480a3835SJason Molenda } 486480a3835SJason Molenda 487a332978bSJason Molenda profile_data_stream << "--end--;"; 488a332978bSJason Molenda 489a332978bSJason Molenda result = profile_data_stream.str(); 490a332978bSJason Molenda } 491a332978bSJason Molenda 492a332978bSJason Molenda return result; 493a332978bSJason Molenda} 494a332978bSJason Molenda 495a332978bSJason Molenda//---------------------------------------------------------------------- 496a332978bSJason Molenda// MachTask::TaskPortForProcessID 497a332978bSJason Molenda//---------------------------------------------------------------------- 498b9c1b51eSKate Stonetask_t MachTask::TaskPortForProcessID(DNBError &err, bool force) { 499bb492890SGreg Clayton if (((m_task == TASK_NULL) || force) && m_process != NULL) 500a332978bSJason Molenda m_task = MachTask::TaskPortForProcessID(m_process->ProcessID(), err); 501a332978bSJason Molenda return m_task; 502a332978bSJason Molenda} 503a332978bSJason Molenda 504a332978bSJason Molenda//---------------------------------------------------------------------- 505a332978bSJason Molenda// MachTask::TaskPortForProcessID 506a332978bSJason Molenda//---------------------------------------------------------------------- 507b9c1b51eSKate Stonetask_t MachTask::TaskPortForProcessID(pid_t pid, DNBError &err, 508b9c1b51eSKate Stone uint32_t num_retries, 509b9c1b51eSKate Stone uint32_t usec_interval) { 510b9c1b51eSKate Stone if (pid != INVALID_NUB_PROCESS) { 511a332978bSJason Molenda DNBError err; 512a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 513a332978bSJason Molenda task_t task = TASK_NULL; 514b9c1b51eSKate Stone for (uint32_t i = 0; i < num_retries; i++) { 515a332978bSJason Molenda err = ::task_for_pid(task_self, pid, &task); 516a332978bSJason Molenda 517b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) { 518a332978bSJason Molenda char str[1024]; 519b9c1b51eSKate Stone ::snprintf(str, sizeof(str), "::task_for_pid ( target_tport = 0x%4.4x, " 520b9c1b51eSKate Stone "pid = %d, &task ) => err = 0x%8.8x (%s)", 52197206d57SZachary Turner task_self, pid, err.Status(), 522a332978bSJason Molenda err.AsString() ? err.AsString() : "success"); 5233bf883eaSJason Molenda if (err.Fail()) { 524a332978bSJason Molenda err.SetErrorString(str); 5253bf883eaSJason Molenda DNBLogError ("MachTask::TaskPortForProcessID task_for_pid failed: %s", str); 5263bf883eaSJason Molenda } 527a332978bSJason Molenda err.LogThreaded(str); 528a332978bSJason Molenda } 529a332978bSJason Molenda 530a332978bSJason Molenda if (err.Success()) 531a332978bSJason Molenda return task; 532a332978bSJason Molenda 533a332978bSJason Molenda // Sleep a bit and try again 534a332978bSJason Molenda ::usleep(usec_interval); 535a332978bSJason Molenda } 536a332978bSJason Molenda } 537a332978bSJason Molenda return TASK_NULL; 538a332978bSJason Molenda} 539a332978bSJason Molenda 540a332978bSJason Molenda//---------------------------------------------------------------------- 541a332978bSJason Molenda// MachTask::BasicInfo 542a332978bSJason Molenda//---------------------------------------------------------------------- 543b9c1b51eSKate Stonekern_return_t MachTask::BasicInfo(struct task_basic_info *info) { 544a332978bSJason Molenda return BasicInfo(TaskPort(), info); 545a332978bSJason Molenda} 546a332978bSJason Molenda 547a332978bSJason Molenda//---------------------------------------------------------------------- 548a332978bSJason Molenda// MachTask::BasicInfo 549a332978bSJason Molenda//---------------------------------------------------------------------- 550b9c1b51eSKate Stonekern_return_t MachTask::BasicInfo(task_t task, struct task_basic_info *info) { 551a332978bSJason Molenda if (info == NULL) 552a332978bSJason Molenda return KERN_INVALID_ARGUMENT; 553a332978bSJason Molenda 554a332978bSJason Molenda DNBError err; 555a332978bSJason Molenda mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT; 556a332978bSJason Molenda err = ::task_info(task, TASK_BASIC_INFO, (task_info_t)info, &count); 557a332978bSJason Molenda const bool log_process = DNBLogCheckLogBit(LOG_TASK); 558a332978bSJason Molenda if (log_process || err.Fail()) 559b9c1b51eSKate Stone err.LogThreaded("::task_info ( target_task = 0x%4.4x, flavor = " 560b9c1b51eSKate Stone "TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => " 561b9c1b51eSKate Stone "%u )", 562b9c1b51eSKate Stone task, info, count); 563b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_TASK) && DNBLogCheckLogBit(LOG_VERBOSE) && 564b9c1b51eSKate Stone err.Success()) { 565b9c1b51eSKate Stone float user = (float)info->user_time.seconds + 566b9c1b51eSKate Stone (float)info->user_time.microseconds / 1000000.0f; 567b9c1b51eSKate Stone float system = (float)info->user_time.seconds + 568b9c1b51eSKate Stone (float)info->user_time.microseconds / 1000000.0f; 569b9c1b51eSKate Stone DNBLogThreaded("task_basic_info = { suspend_count = %i, virtual_size = " 570b9c1b51eSKate Stone "0x%8.8llx, resident_size = 0x%8.8llx, user_time = %f, " 571b9c1b51eSKate Stone "system_time = %f }", 572b9c1b51eSKate Stone info->suspend_count, (uint64_t)info->virtual_size, 573b9c1b51eSKate Stone (uint64_t)info->resident_size, user, system); 574a332978bSJason Molenda } 57597206d57SZachary Turner return err.Status(); 576a332978bSJason Molenda} 577a332978bSJason Molenda 578b9c1b51eSKate Stone//---------------------------------------------------------------------- 579b9c1b51eSKate Stone// MachTask::IsValid 580b9c1b51eSKate Stone// 581b9c1b51eSKate Stone// Returns true if a task is a valid task port for a current process. 582b9c1b51eSKate Stone//---------------------------------------------------------------------- 583b9c1b51eSKate Stonebool MachTask::IsValid() const { return MachTask::IsValid(TaskPort()); } 584a332978bSJason Molenda 585a332978bSJason Molenda//---------------------------------------------------------------------- 586a332978bSJason Molenda// MachTask::IsValid 587a332978bSJason Molenda// 588a332978bSJason Molenda// Returns true if a task is a valid task port for a current process. 589a332978bSJason Molenda//---------------------------------------------------------------------- 590b9c1b51eSKate Stonebool MachTask::IsValid(task_t task) { 591b9c1b51eSKate Stone if (task != TASK_NULL) { 592a332978bSJason Molenda struct task_basic_info task_info; 593a332978bSJason Molenda return BasicInfo(task, &task_info) == KERN_SUCCESS; 594a332978bSJason Molenda } 595a332978bSJason Molenda return false; 596a332978bSJason Molenda} 597a332978bSJason Molenda 598*27012c0fSAlessandro Arzillibool MachTask::StartExceptionThread(bool unmask_signals, DNBError &err) { 599a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__); 60036a216eeSJason Molenda 601a332978bSJason Molenda task_t task = TaskPortForProcessID(err); 602b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 603a332978bSJason Molenda // Got the mach port for the current process 604a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 605a332978bSJason Molenda 606a332978bSJason Molenda // Allocate an exception port that we will use to track our child process 607b9c1b51eSKate Stone err = ::mach_port_allocate(task_self, MACH_PORT_RIGHT_RECEIVE, 608b9c1b51eSKate Stone &m_exception_port); 609a332978bSJason Molenda if (err.Fail()) 610a332978bSJason Molenda return false; 611a332978bSJason Molenda 612a332978bSJason Molenda // Add the ability to send messages on the new exception port 613b9c1b51eSKate Stone err = ::mach_port_insert_right(task_self, m_exception_port, 614b9c1b51eSKate Stone m_exception_port, MACH_MSG_TYPE_MAKE_SEND); 615a332978bSJason Molenda if (err.Fail()) 616a332978bSJason Molenda return false; 617a332978bSJason Molenda 618a332978bSJason Molenda // Save the original state of the exception ports for our child process 619a332978bSJason Molenda SaveExceptionPortInfo(); 620a332978bSJason Molenda 621a332978bSJason Molenda // We weren't able to save the info for our exception ports, we must stop... 622b9c1b51eSKate Stone if (m_exc_port_info.mask == 0) { 623a332978bSJason Molenda err.SetErrorString("failed to get exception port info"); 624a332978bSJason Molenda return false; 625a332978bSJason Molenda } 626a332978bSJason Molenda 627*27012c0fSAlessandro Arzilli if (unmask_signals) { 628*27012c0fSAlessandro Arzilli m_exc_port_info.mask = m_exc_port_info.mask & 629*27012c0fSAlessandro Arzilli ~(EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | 630*27012c0fSAlessandro Arzilli EXC_MASK_ARITHMETIC); 631*27012c0fSAlessandro Arzilli } 632*27012c0fSAlessandro Arzilli 633a332978bSJason Molenda // Set the ability to get all exceptions on this port 634b9c1b51eSKate Stone err = ::task_set_exception_ports( 635b9c1b51eSKate Stone task, m_exc_port_info.mask, m_exception_port, 636b9c1b51eSKate Stone EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE); 637b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_EXCEPTIONS) || err.Fail()) { 638b9c1b51eSKate Stone err.LogThreaded("::task_set_exception_ports ( task = 0x%4.4x, " 639b9c1b51eSKate Stone "exception_mask = 0x%8.8x, new_port = 0x%4.4x, behavior " 640b9c1b51eSKate Stone "= 0x%8.8x, new_flavor = 0x%8.8x )", 641b9c1b51eSKate Stone task, m_exc_port_info.mask, m_exception_port, 642a332978bSJason Molenda (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 643a332978bSJason Molenda THREAD_STATE_NONE); 644a332978bSJason Molenda } 645a332978bSJason Molenda 646a332978bSJason Molenda if (err.Fail()) 647a332978bSJason Molenda return false; 648a332978bSJason Molenda 649a332978bSJason Molenda // Create the exception thread 650b9c1b51eSKate Stone err = ::pthread_create(&m_exception_thread, NULL, MachTask::ExceptionThread, 651b9c1b51eSKate Stone this); 652a332978bSJason Molenda return err.Success(); 653b9c1b51eSKate Stone } else { 654b9c1b51eSKate Stone DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", 655b9c1b51eSKate Stone __FUNCTION__); 656a332978bSJason Molenda } 657a332978bSJason Molenda return false; 658a332978bSJason Molenda} 659a332978bSJason Molenda 660b9c1b51eSKate Stonekern_return_t MachTask::ShutDownExcecptionThread() { 661a332978bSJason Molenda DNBError err; 662a332978bSJason Molenda 663a332978bSJason Molenda err = RestoreExceptionPortInfo(); 664a332978bSJason Molenda 665a332978bSJason Molenda // NULL our our exception port and let our exception thread exit 666a332978bSJason Molenda mach_port_t exception_port = m_exception_port; 667a026de05SBruce Mitchener m_exception_port = 0; 668a332978bSJason Molenda 669a332978bSJason Molenda err.SetError(::pthread_cancel(m_exception_thread), DNBError::POSIX); 670a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 671a332978bSJason Molenda err.LogThreaded("::pthread_cancel ( thread = %p )", m_exception_thread); 672a332978bSJason Molenda 673a332978bSJason Molenda err.SetError(::pthread_join(m_exception_thread, NULL), DNBError::POSIX); 674a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 675b9c1b51eSKate Stone err.LogThreaded("::pthread_join ( thread = %p, value_ptr = NULL)", 676b9c1b51eSKate Stone m_exception_thread); 677a332978bSJason Molenda 678a332978bSJason Molenda // Deallocate our exception port that we used to track our child process 679a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 680a332978bSJason Molenda err = ::mach_port_deallocate(task_self, exception_port); 681a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 682b9c1b51eSKate Stone err.LogThreaded("::mach_port_deallocate ( task = 0x%4.4x, name = 0x%4.4x )", 683b9c1b51eSKate Stone task_self, exception_port); 684a332978bSJason Molenda 68583a131b2SJason Molenda m_exec_will_be_suspended = false; 68683a131b2SJason Molenda m_do_double_resume = false; 68783a131b2SJason Molenda 68897206d57SZachary Turner return err.Status(); 689a332978bSJason Molenda} 690a332978bSJason Molenda 691b9c1b51eSKate Stonevoid *MachTask::ExceptionThread(void *arg) { 692a332978bSJason Molenda if (arg == NULL) 693a332978bSJason Molenda return NULL; 694a332978bSJason Molenda 695a332978bSJason Molenda MachTask *mach_task = (MachTask *)arg; 696a332978bSJason Molenda MachProcess *mach_proc = mach_task->Process(); 697b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, 698b9c1b51eSKate Stone "MachTask::%s ( arg = %p ) starting thread...", __FUNCTION__, 699b9c1b51eSKate Stone arg); 700a332978bSJason Molenda 70136a216eeSJason Molenda#if defined(__APPLE__) 70236a216eeSJason Molenda pthread_setname_np("exception monitoring thread"); 70336a216eeSJason Molenda#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 70436a216eeSJason Molenda struct sched_param thread_param; 70536a216eeSJason Molenda int thread_sched_policy; 706b9c1b51eSKate Stone if (pthread_getschedparam(pthread_self(), &thread_sched_policy, 707b9c1b51eSKate Stone &thread_param) == 0) { 70836a216eeSJason Molenda thread_param.sched_priority = 47; 70936a216eeSJason Molenda pthread_setschedparam(pthread_self(), thread_sched_policy, &thread_param); 71036a216eeSJason Molenda } 71136a216eeSJason Molenda#endif 71236a216eeSJason Molenda#endif 71336a216eeSJason Molenda 714a332978bSJason Molenda // We keep a count of the number of consecutive exceptions received so 715a332978bSJason Molenda // we know to grab all exceptions without a timeout. We do this to get a 716a332978bSJason Molenda // bunch of related exceptions on our exception port so we can process 717a332978bSJason Molenda // then together. When we have multiple threads, we can get an exception 718a332978bSJason Molenda // per thread and they will come in consecutively. The main loop in this 719a332978bSJason Molenda // thread can stop periodically if needed to service things related to this 720a332978bSJason Molenda // process. 721a332978bSJason Molenda // flag set in the options, so we will wait forever for an exception on 722a332978bSJason Molenda // our exception port. After we get one exception, we then will use the 723a332978bSJason Molenda // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current 724a332978bSJason Molenda // exceptions for our process. After we have received the last pending 725a332978bSJason Molenda // exception, we will get a timeout which enables us to then notify 726d93c4a33SBruce Mitchener // our main thread that we have an exception bundle available. We then wait 727a332978bSJason Molenda // for the main thread to tell this exception thread to start trying to get 728a332978bSJason Molenda // exceptions messages again and we start again with a mach_msg read with 729a332978bSJason Molenda // infinite timeout. 730a332978bSJason Molenda uint32_t num_exceptions_received = 0; 731a332978bSJason Molenda DNBError err; 732a332978bSJason Molenda task_t task = mach_task->TaskPort(); 733a332978bSJason Molenda mach_msg_timeout_t periodic_timeout = 0; 734a332978bSJason Molenda 735a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 736a332978bSJason Molenda mach_msg_timeout_t watchdog_elapsed = 0; 737a332978bSJason Molenda mach_msg_timeout_t watchdog_timeout = 60 * 1000; 738a332978bSJason Molenda pid_t pid = mach_proc->ProcessID(); 739a332978bSJason Molenda CFReleaser<SBSWatchdogAssertionRef> watchdog; 740a332978bSJason Molenda 741b9c1b51eSKate Stone if (mach_proc->ProcessUsingSpringBoard()) { 742a332978bSJason Molenda // Request a renewal for every 60 seconds if we attached using SpringBoard 743a332978bSJason Molenda watchdog.reset(::SBSWatchdogAssertionCreateForPID(NULL, pid, 60)); 744b9c1b51eSKate Stone DNBLogThreadedIf( 745b9c1b51eSKate Stone LOG_TASK, "::SBSWatchdogAssertionCreateForPID (NULL, %4.4x, 60 ) => %p", 746b9c1b51eSKate Stone pid, watchdog.get()); 747a332978bSJason Molenda 748b9c1b51eSKate Stone if (watchdog.get()) { 749a332978bSJason Molenda ::SBSWatchdogAssertionRenew(watchdog.get()); 750a332978bSJason Molenda 751b9c1b51eSKate Stone CFTimeInterval watchdogRenewalInterval = 752b9c1b51eSKate Stone ::SBSWatchdogAssertionGetRenewalInterval(watchdog.get()); 753b9c1b51eSKate Stone DNBLogThreadedIf( 754b9c1b51eSKate Stone LOG_TASK, 755b9c1b51eSKate Stone "::SBSWatchdogAssertionGetRenewalInterval ( %p ) => %g seconds", 756b9c1b51eSKate Stone watchdog.get(), watchdogRenewalInterval); 757b9c1b51eSKate Stone if (watchdogRenewalInterval > 0.0) { 758a332978bSJason Molenda watchdog_timeout = (mach_msg_timeout_t)watchdogRenewalInterval * 1000; 759a332978bSJason Molenda if (watchdog_timeout > 3000) 760a332978bSJason Molenda watchdog_timeout -= 1000; // Give us a second to renew our timeout 761a332978bSJason Molenda else if (watchdog_timeout > 1000) 762b9c1b51eSKate Stone watchdog_timeout -= 763b9c1b51eSKate Stone 250; // Give us a quarter of a second to renew our timeout 764a332978bSJason Molenda } 765a332978bSJason Molenda } 766a332978bSJason Molenda if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout) 767a332978bSJason Molenda periodic_timeout = watchdog_timeout; 768a332978bSJason Molenda } 769a332978bSJason Molenda#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) 770a332978bSJason Molenda 771a332978bSJason Molenda#ifdef WITH_BKS 772a332978bSJason Molenda CFReleaser<BKSWatchdogAssertionRef> watchdog; 773b9c1b51eSKate Stone if (mach_proc->ProcessUsingBackBoard()) { 774a332978bSJason Molenda pid_t pid = mach_proc->ProcessID(); 775a332978bSJason Molenda CFAllocatorRef alloc = kCFAllocatorDefault; 776a332978bSJason Molenda watchdog.reset(::BKSWatchdogAssertionCreateForPID(alloc, pid)); 777a332978bSJason Molenda } 778a332978bSJason Molenda#endif // #ifdef WITH_BKS 779a332978bSJason Molenda 780b9c1b51eSKate Stone while (mach_task->ExceptionPortIsValid()) { 781a332978bSJason Molenda ::pthread_testcancel(); 782a332978bSJason Molenda 783a332978bSJason Molenda MachException::Message exception_message; 784a332978bSJason Molenda 785b9c1b51eSKate Stone if (num_exceptions_received > 0) { 786b9c1b51eSKate Stone // No timeout, just receive as many exceptions as we can since we already 787b9c1b51eSKate Stone // have one and we want 788a332978bSJason Molenda // to get all currently available exceptions for this task 789b9c1b51eSKate Stone err = exception_message.Receive( 790b9c1b51eSKate Stone mach_task->ExceptionPort(), 791cc5b509bSFrederic Riss MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, 1); 792b9c1b51eSKate Stone } else if (periodic_timeout > 0) { 793b9c1b51eSKate Stone // We need to stop periodically in this loop, so try and get a mach 794b9c1b51eSKate Stone // message with a valid timeout (ms) 795b9c1b51eSKate Stone err = exception_message.Receive(mach_task->ExceptionPort(), 796b9c1b51eSKate Stone MACH_RCV_MSG | MACH_RCV_INTERRUPT | 797b9c1b51eSKate Stone MACH_RCV_TIMEOUT, 798b9c1b51eSKate Stone periodic_timeout); 799b9c1b51eSKate Stone } else { 800a332978bSJason Molenda // We don't need to parse all current exceptions or stop periodically, 801a332978bSJason Molenda // just wait for an exception forever. 802b9c1b51eSKate Stone err = exception_message.Receive(mach_task->ExceptionPort(), 803b9c1b51eSKate Stone MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0); 804a332978bSJason Molenda } 805a332978bSJason Molenda 80697206d57SZachary Turner if (err.Status() == MACH_RCV_INTERRUPTED) { 807a332978bSJason Molenda // If we have no task port we should exit this thread 808b9c1b51eSKate Stone if (!mach_task->ExceptionPortIsValid()) { 809a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "thread cancelled..."); 810a332978bSJason Molenda break; 811a332978bSJason Molenda } 812a332978bSJason Molenda 813a332978bSJason Molenda // Make sure our task is still valid 814b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 815a332978bSJason Molenda // Task is still ok 816b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, 817b9c1b51eSKate Stone "interrupted, but task still valid, continuing..."); 818a332978bSJason Molenda continue; 819b9c1b51eSKate Stone } else { 820a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 821a332978bSJason Molenda mach_proc->SetState(eStateExited); 822a332978bSJason Molenda // Our task has died, exit the thread. 823a332978bSJason Molenda break; 824a332978bSJason Molenda } 82597206d57SZachary Turner } else if (err.Status() == MACH_RCV_TIMED_OUT) { 826b9c1b51eSKate Stone if (num_exceptions_received > 0) { 827a332978bSJason Molenda // We were receiving all current exceptions with a timeout of zero 828a332978bSJason Molenda // it is time to go back to our normal looping mode 829a332978bSJason Molenda num_exceptions_received = 0; 830a332978bSJason Molenda 831a332978bSJason Molenda // Notify our main thread we have a complete exception message 832bb492890SGreg Clayton // bundle available and get the possibly updated task port back 833bb492890SGreg Clayton // from the process in case we exec'ed and our task port changed 834bb492890SGreg Clayton task = mach_proc->ExceptionMessageBundleComplete(); 835a332978bSJason Molenda 836a332978bSJason Molenda // in case we use a timeout value when getting exceptions... 837a332978bSJason Molenda // Make sure our task is still valid 838b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 839a332978bSJason Molenda // Task is still ok 840a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "got a timeout, continuing..."); 841a332978bSJason Molenda continue; 842b9c1b51eSKate Stone } else { 843a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 844a332978bSJason Molenda mach_proc->SetState(eStateExited); 845a332978bSJason Molenda // Our task has died, exit the thread. 846a332978bSJason Molenda break; 847a332978bSJason Molenda } 848a332978bSJason Molenda } 849a332978bSJason Molenda 850a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 851b9c1b51eSKate Stone if (watchdog.get()) { 852a332978bSJason Molenda watchdog_elapsed += periodic_timeout; 853b9c1b51eSKate Stone if (watchdog_elapsed >= watchdog_timeout) { 854b9c1b51eSKate Stone DNBLogThreadedIf(LOG_TASK, "SBSWatchdogAssertionRenew ( %p )", 855b9c1b51eSKate Stone watchdog.get()); 856a332978bSJason Molenda ::SBSWatchdogAssertionRenew(watchdog.get()); 857a332978bSJason Molenda watchdog_elapsed = 0; 858a332978bSJason Molenda } 859a332978bSJason Molenda } 860a332978bSJason Molenda#endif 86197206d57SZachary Turner } else if (err.Status() != KERN_SUCCESS) { 862b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, "got some other error, do something " 863b9c1b51eSKate Stone "about it??? nah, continuing for " 864b9c1b51eSKate Stone "now..."); 865a332978bSJason Molenda // TODO: notify of error? 866b9c1b51eSKate Stone } else { 867b9c1b51eSKate Stone if (exception_message.CatchExceptionRaise(task)) { 8687ae1a67eSGreg Clayton if (exception_message.state.task_port != task) { 8697ae1a67eSGreg Clayton if (exception_message.state.IsValid()) { 8707ae1a67eSGreg Clayton // We exec'ed and our task port changed on us. 8717ae1a67eSGreg Clayton DNBLogThreadedIf(LOG_EXCEPTIONS, 8727ae1a67eSGreg Clayton "task port changed from 0x%4.4x to 0x%4.4x", 8737ae1a67eSGreg Clayton task, exception_message.state.task_port); 8747ae1a67eSGreg Clayton task = exception_message.state.task_port; 8757ae1a67eSGreg Clayton mach_task->TaskPortChanged(exception_message.state.task_port); 8767ae1a67eSGreg Clayton } 8777ae1a67eSGreg Clayton } 878a332978bSJason Molenda ++num_exceptions_received; 879a332978bSJason Molenda mach_proc->ExceptionMessageReceived(exception_message); 880a332978bSJason Molenda } 881a332978bSJason Molenda } 882a332978bSJason Molenda } 883a332978bSJason Molenda 884a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 885b9c1b51eSKate Stone if (watchdog.get()) { 886b9c1b51eSKate Stone // TODO: change SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel 887b9c1b51eSKate Stone // when we 888b9c1b51eSKate Stone // all are up and running on systems that support it. The SBS framework has 889b9c1b51eSKate Stone // a #define 890b9c1b51eSKate Stone // that will forward SBSWatchdogAssertionRelease to 891b9c1b51eSKate Stone // SBSWatchdogAssertionCancel for now 892a332978bSJason Molenda // so it should still build either way. 893b9c1b51eSKate Stone DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", 894b9c1b51eSKate Stone watchdog.get()); 895a332978bSJason Molenda ::SBSWatchdogAssertionRelease(watchdog.get()); 896a332978bSJason Molenda } 897a332978bSJason Molenda#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) 898a332978bSJason Molenda 899b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s (%p): thread exiting...", 900b9c1b51eSKate Stone __FUNCTION__, arg); 901a332978bSJason Molenda return NULL; 902a332978bSJason Molenda} 903a332978bSJason Molenda 904a332978bSJason Molenda// So the TASK_DYLD_INFO used to just return the address of the all image infos 905a332978bSJason Molenda// as a single member called "all_image_info". Then someone decided it would be 906a332978bSJason Molenda// a good idea to rename this first member to "all_image_info_addr" and add a 907a332978bSJason Molenda// size member called "all_image_info_size". This of course can not be detected 908a332978bSJason Molenda// using code or #defines. So to hack around this problem, we define our own 909b9c1b51eSKate Stone// version of the TASK_DYLD_INFO structure so we can guarantee what is inside 910b9c1b51eSKate Stone// it. 911a332978bSJason Molenda 912a332978bSJason Molendastruct hack_task_dyld_info { 913a332978bSJason Molenda mach_vm_address_t all_image_info_addr; 914a332978bSJason Molenda mach_vm_size_t all_image_info_size; 915a332978bSJason Molenda}; 916a332978bSJason Molenda 917b9c1b51eSKate Stonenub_addr_t MachTask::GetDYLDAllImageInfosAddress(DNBError &err) { 918a332978bSJason Molenda struct hack_task_dyld_info dyld_info; 919a332978bSJason Molenda mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; 920b9c1b51eSKate Stone // Make sure that COUNT isn't bigger than our hacked up struct 921b9c1b51eSKate Stone // hack_task_dyld_info. 922a332978bSJason Molenda // If it is, then make COUNT smaller to match. 923a332978bSJason Molenda if (count > (sizeof(struct hack_task_dyld_info) / sizeof(natural_t))) 924a332978bSJason Molenda count = (sizeof(struct hack_task_dyld_info) / sizeof(natural_t)); 925a332978bSJason Molenda 926a332978bSJason Molenda task_t task = TaskPortForProcessID(err); 927b9c1b51eSKate Stone if (err.Success()) { 928a332978bSJason Molenda err = ::task_info(task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count); 929b9c1b51eSKate Stone if (err.Success()) { 930a332978bSJason Molenda // We now have the address of the all image infos structure 931a332978bSJason Molenda return dyld_info.all_image_info_addr; 932a332978bSJason Molenda } 933a332978bSJason Molenda } 934a332978bSJason Molenda return INVALID_NUB_ADDRESS; 935a332978bSJason Molenda} 936a332978bSJason Molenda 937a332978bSJason Molenda//---------------------------------------------------------------------- 938a332978bSJason Molenda// MachTask::AllocateMemory 939a332978bSJason Molenda//---------------------------------------------------------------------- 940b9c1b51eSKate Stonenub_addr_t MachTask::AllocateMemory(size_t size, uint32_t permissions) { 941a332978bSJason Molenda mach_vm_address_t addr; 942a332978bSJason Molenda task_t task = TaskPort(); 943a332978bSJason Molenda if (task == TASK_NULL) 944a332978bSJason Molenda return INVALID_NUB_ADDRESS; 945a332978bSJason Molenda 946a332978bSJason Molenda DNBError err; 947a332978bSJason Molenda err = ::mach_vm_allocate(task, &addr, size, TRUE); 94897206d57SZachary Turner if (err.Status() == KERN_SUCCESS) { 949a332978bSJason Molenda // Set the protections: 950a332978bSJason Molenda vm_prot_t mach_prot = VM_PROT_NONE; 951a332978bSJason Molenda if (permissions & eMemoryPermissionsReadable) 952a332978bSJason Molenda mach_prot |= VM_PROT_READ; 953a332978bSJason Molenda if (permissions & eMemoryPermissionsWritable) 954a332978bSJason Molenda mach_prot |= VM_PROT_WRITE; 955a332978bSJason Molenda if (permissions & eMemoryPermissionsExecutable) 956a332978bSJason Molenda mach_prot |= VM_PROT_EXECUTE; 957a332978bSJason Molenda 958a332978bSJason Molenda err = ::mach_vm_protect(task, addr, size, 0, mach_prot); 95997206d57SZachary Turner if (err.Status() == KERN_SUCCESS) { 960a332978bSJason Molenda m_allocations.insert(std::make_pair(addr, size)); 961a332978bSJason Molenda return addr; 962a332978bSJason Molenda } 963a332978bSJason Molenda ::mach_vm_deallocate(task, addr, size); 964a332978bSJason Molenda } 965a332978bSJason Molenda return INVALID_NUB_ADDRESS; 966a332978bSJason Molenda} 967a332978bSJason Molenda 968a332978bSJason Molenda//---------------------------------------------------------------------- 969a332978bSJason Molenda// MachTask::DeallocateMemory 970a332978bSJason Molenda//---------------------------------------------------------------------- 971b9c1b51eSKate Stonenub_bool_t MachTask::DeallocateMemory(nub_addr_t addr) { 972a332978bSJason Molenda task_t task = TaskPort(); 973a332978bSJason Molenda if (task == TASK_NULL) 974a332978bSJason Molenda return false; 975a332978bSJason Molenda 976a332978bSJason Molenda // We have to stash away sizes for the allocations... 977a332978bSJason Molenda allocation_collection::iterator pos, end = m_allocations.end(); 978b9c1b51eSKate Stone for (pos = m_allocations.begin(); pos != end; pos++) { 979b9c1b51eSKate Stone if ((*pos).first == addr) { 980a332978bSJason Molenda m_allocations.erase(pos); 981a332978bSJason Molenda#define ALWAYS_ZOMBIE_ALLOCATIONS 0 982b9c1b51eSKate Stone if (ALWAYS_ZOMBIE_ALLOCATIONS || 983b9c1b51eSKate Stone getenv("DEBUGSERVER_ZOMBIE_ALLOCATIONS")) { 984a332978bSJason Molenda ::mach_vm_protect(task, (*pos).first, (*pos).second, 0, VM_PROT_NONE); 985a332978bSJason Molenda return true; 986b9c1b51eSKate Stone } else 987b9c1b51eSKate Stone return ::mach_vm_deallocate(task, (*pos).first, (*pos).second) == 988b9c1b51eSKate Stone KERN_SUCCESS; 989a332978bSJason Molenda } 990a332978bSJason Molenda } 991a332978bSJason Molenda return false; 992a332978bSJason Molenda} 993a332978bSJason Molenda 9947ae1a67eSGreg Claytonvoid MachTask::TaskPortChanged(task_t task) 9957ae1a67eSGreg Clayton{ 9967ae1a67eSGreg Clayton m_task = task; 99783a131b2SJason Molenda 99883a131b2SJason Molenda // If we've just exec'd to a new process, and it 99983a131b2SJason Molenda // is started suspended, we'll need to do two 100083a131b2SJason Molenda // task_resume's to get the inferior process to 100183a131b2SJason Molenda // continue. 100283a131b2SJason Molenda if (m_exec_will_be_suspended) 100383a131b2SJason Molenda m_do_double_resume = true; 100483a131b2SJason Molenda else 100583a131b2SJason Molenda m_do_double_resume = false; 100683a131b2SJason Molenda m_exec_will_be_suspended = false; 10077ae1a67eSGreg Clayton} 1008