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++) { 515edde2eb1SJason Molenda DNBLog("[LaunchAttach] (%d) about to task_for_pid(%d)", getpid(), pid); 516a332978bSJason Molenda err = ::task_for_pid(task_self, pid, &task); 517a332978bSJason Molenda 518b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) { 519a332978bSJason Molenda char str[1024]; 520b9c1b51eSKate Stone ::snprintf(str, sizeof(str), "::task_for_pid ( target_tport = 0x%4.4x, " 521b9c1b51eSKate Stone "pid = %d, &task ) => err = 0x%8.8x (%s)", 52297206d57SZachary Turner task_self, pid, err.Status(), 523a332978bSJason Molenda err.AsString() ? err.AsString() : "success"); 5243bf883eaSJason Molenda if (err.Fail()) { 525a332978bSJason Molenda err.SetErrorString(str); 526edde2eb1SJason Molenda DNBLogError( 527edde2eb1SJason Molenda "[LaunchAttach] MachTask::TaskPortForProcessID task_for_pid(%d) " 528edde2eb1SJason Molenda "failed: %s", 529edde2eb1SJason Molenda pid, str); 5303bf883eaSJason Molenda } 531a332978bSJason Molenda err.LogThreaded(str); 532a332978bSJason Molenda } 533a332978bSJason Molenda 534edde2eb1SJason Molenda if (err.Success()) { 535edde2eb1SJason Molenda DNBLog("[LaunchAttach] (%d) successfully task_for_pid(%d)'ed", getpid(), 536edde2eb1SJason Molenda pid); 537a332978bSJason Molenda return task; 538edde2eb1SJason Molenda } 539a332978bSJason Molenda 540a332978bSJason Molenda // Sleep a bit and try again 541a332978bSJason Molenda ::usleep(usec_interval); 542a332978bSJason Molenda } 543a332978bSJason Molenda } 544a332978bSJason Molenda return TASK_NULL; 545a332978bSJason Molenda} 546a332978bSJason Molenda 547a332978bSJason Molenda//---------------------------------------------------------------------- 548a332978bSJason Molenda// MachTask::BasicInfo 549a332978bSJason Molenda//---------------------------------------------------------------------- 550b9c1b51eSKate Stonekern_return_t MachTask::BasicInfo(struct task_basic_info *info) { 551a332978bSJason Molenda return BasicInfo(TaskPort(), info); 552a332978bSJason Molenda} 553a332978bSJason Molenda 554a332978bSJason Molenda//---------------------------------------------------------------------- 555a332978bSJason Molenda// MachTask::BasicInfo 556a332978bSJason Molenda//---------------------------------------------------------------------- 557b9c1b51eSKate Stonekern_return_t MachTask::BasicInfo(task_t task, struct task_basic_info *info) { 558a332978bSJason Molenda if (info == NULL) 559a332978bSJason Molenda return KERN_INVALID_ARGUMENT; 560a332978bSJason Molenda 561a332978bSJason Molenda DNBError err; 562a332978bSJason Molenda mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT; 563a332978bSJason Molenda err = ::task_info(task, TASK_BASIC_INFO, (task_info_t)info, &count); 564a332978bSJason Molenda const bool log_process = DNBLogCheckLogBit(LOG_TASK); 565a332978bSJason Molenda if (log_process || err.Fail()) 566b9c1b51eSKate Stone err.LogThreaded("::task_info ( target_task = 0x%4.4x, flavor = " 567b9c1b51eSKate Stone "TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => " 568b9c1b51eSKate Stone "%u )", 569b9c1b51eSKate Stone task, info, count); 570b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_TASK) && DNBLogCheckLogBit(LOG_VERBOSE) && 571b9c1b51eSKate Stone err.Success()) { 572b9c1b51eSKate Stone float user = (float)info->user_time.seconds + 573b9c1b51eSKate Stone (float)info->user_time.microseconds / 1000000.0f; 574b9c1b51eSKate Stone float system = (float)info->user_time.seconds + 575b9c1b51eSKate Stone (float)info->user_time.microseconds / 1000000.0f; 576b9c1b51eSKate Stone DNBLogThreaded("task_basic_info = { suspend_count = %i, virtual_size = " 577b9c1b51eSKate Stone "0x%8.8llx, resident_size = 0x%8.8llx, user_time = %f, " 578b9c1b51eSKate Stone "system_time = %f }", 579b9c1b51eSKate Stone info->suspend_count, (uint64_t)info->virtual_size, 580b9c1b51eSKate Stone (uint64_t)info->resident_size, user, system); 581a332978bSJason Molenda } 58297206d57SZachary Turner return err.Status(); 583a332978bSJason Molenda} 584a332978bSJason Molenda 585b9c1b51eSKate Stone//---------------------------------------------------------------------- 586b9c1b51eSKate Stone// MachTask::IsValid 587b9c1b51eSKate Stone// 588b9c1b51eSKate Stone// Returns true if a task is a valid task port for a current process. 589b9c1b51eSKate Stone//---------------------------------------------------------------------- 590b9c1b51eSKate Stonebool MachTask::IsValid() const { return MachTask::IsValid(TaskPort()); } 591a332978bSJason Molenda 592a332978bSJason Molenda//---------------------------------------------------------------------- 593a332978bSJason Molenda// MachTask::IsValid 594a332978bSJason Molenda// 595a332978bSJason Molenda// Returns true if a task is a valid task port for a current process. 596a332978bSJason Molenda//---------------------------------------------------------------------- 597b9c1b51eSKate Stonebool MachTask::IsValid(task_t task) { 598b9c1b51eSKate Stone if (task != TASK_NULL) { 599a332978bSJason Molenda struct task_basic_info task_info; 600a332978bSJason Molenda return BasicInfo(task, &task_info) == KERN_SUCCESS; 601a332978bSJason Molenda } 602a332978bSJason Molenda return false; 603a332978bSJason Molenda} 604a332978bSJason Molenda 60527012c0fSAlessandro Arzillibool MachTask::StartExceptionThread(bool unmask_signals, DNBError &err) { 606a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__); 60736a216eeSJason Molenda 608a332978bSJason Molenda task_t task = TaskPortForProcessID(err); 609b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 610a332978bSJason Molenda // Got the mach port for the current process 611a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 612a332978bSJason Molenda 613a332978bSJason Molenda // Allocate an exception port that we will use to track our child process 614b9c1b51eSKate Stone err = ::mach_port_allocate(task_self, MACH_PORT_RIGHT_RECEIVE, 615b9c1b51eSKate Stone &m_exception_port); 616a332978bSJason Molenda if (err.Fail()) 617a332978bSJason Molenda return false; 618a332978bSJason Molenda 619a332978bSJason Molenda // Add the ability to send messages on the new exception port 620b9c1b51eSKate Stone err = ::mach_port_insert_right(task_self, m_exception_port, 621b9c1b51eSKate Stone m_exception_port, MACH_MSG_TYPE_MAKE_SEND); 622a332978bSJason Molenda if (err.Fail()) 623a332978bSJason Molenda return false; 624a332978bSJason Molenda 625a332978bSJason Molenda // Save the original state of the exception ports for our child process 626a332978bSJason Molenda SaveExceptionPortInfo(); 627a332978bSJason Molenda 628a332978bSJason Molenda // We weren't able to save the info for our exception ports, we must stop... 629b9c1b51eSKate Stone if (m_exc_port_info.mask == 0) { 630a332978bSJason Molenda err.SetErrorString("failed to get exception port info"); 631a332978bSJason Molenda return false; 632a332978bSJason Molenda } 633a332978bSJason Molenda 63427012c0fSAlessandro Arzilli if (unmask_signals) { 63527012c0fSAlessandro Arzilli m_exc_port_info.mask = m_exc_port_info.mask & 63627012c0fSAlessandro Arzilli ~(EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | 63727012c0fSAlessandro Arzilli EXC_MASK_ARITHMETIC); 63827012c0fSAlessandro Arzilli } 63927012c0fSAlessandro Arzilli 640a332978bSJason Molenda // Set the ability to get all exceptions on this port 641b9c1b51eSKate Stone err = ::task_set_exception_ports( 642b9c1b51eSKate Stone task, m_exc_port_info.mask, m_exception_port, 643b9c1b51eSKate Stone EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE); 644b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_EXCEPTIONS) || err.Fail()) { 645b9c1b51eSKate Stone err.LogThreaded("::task_set_exception_ports ( task = 0x%4.4x, " 646b9c1b51eSKate Stone "exception_mask = 0x%8.8x, new_port = 0x%4.4x, behavior " 647b9c1b51eSKate Stone "= 0x%8.8x, new_flavor = 0x%8.8x )", 648b9c1b51eSKate Stone task, m_exc_port_info.mask, m_exception_port, 649a332978bSJason Molenda (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 650a332978bSJason Molenda THREAD_STATE_NONE); 651a332978bSJason Molenda } 652a332978bSJason Molenda 653a332978bSJason Molenda if (err.Fail()) 654a332978bSJason Molenda return false; 655a332978bSJason Molenda 656a332978bSJason Molenda // Create the exception thread 657b9c1b51eSKate Stone err = ::pthread_create(&m_exception_thread, NULL, MachTask::ExceptionThread, 658b9c1b51eSKate Stone this); 659a332978bSJason Molenda return err.Success(); 660b9c1b51eSKate Stone } else { 661b9c1b51eSKate Stone DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", 662b9c1b51eSKate Stone __FUNCTION__); 663a332978bSJason Molenda } 664a332978bSJason Molenda return false; 665a332978bSJason Molenda} 666a332978bSJason Molenda 667b9c1b51eSKate Stonekern_return_t MachTask::ShutDownExcecptionThread() { 668a332978bSJason Molenda DNBError err; 669a332978bSJason Molenda 670a332978bSJason Molenda err = RestoreExceptionPortInfo(); 671a332978bSJason Molenda 672a332978bSJason Molenda // NULL our our exception port and let our exception thread exit 673a332978bSJason Molenda mach_port_t exception_port = m_exception_port; 674a026de05SBruce Mitchener m_exception_port = 0; 675a332978bSJason Molenda 676a332978bSJason Molenda err.SetError(::pthread_cancel(m_exception_thread), DNBError::POSIX); 677a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 678a332978bSJason Molenda err.LogThreaded("::pthread_cancel ( thread = %p )", m_exception_thread); 679a332978bSJason Molenda 680a332978bSJason Molenda err.SetError(::pthread_join(m_exception_thread, NULL), DNBError::POSIX); 681a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 682b9c1b51eSKate Stone err.LogThreaded("::pthread_join ( thread = %p, value_ptr = NULL)", 683b9c1b51eSKate Stone m_exception_thread); 684a332978bSJason Molenda 685a332978bSJason Molenda // Deallocate our exception port that we used to track our child process 686a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 687a332978bSJason Molenda err = ::mach_port_deallocate(task_self, exception_port); 688a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 689b9c1b51eSKate Stone err.LogThreaded("::mach_port_deallocate ( task = 0x%4.4x, name = 0x%4.4x )", 690b9c1b51eSKate Stone task_self, exception_port); 691a332978bSJason Molenda 69283a131b2SJason Molenda m_exec_will_be_suspended = false; 69383a131b2SJason Molenda m_do_double_resume = false; 69483a131b2SJason Molenda 69597206d57SZachary Turner return err.Status(); 696a332978bSJason Molenda} 697a332978bSJason Molenda 698b9c1b51eSKate Stonevoid *MachTask::ExceptionThread(void *arg) { 699a332978bSJason Molenda if (arg == NULL) 700a332978bSJason Molenda return NULL; 701a332978bSJason Molenda 702a332978bSJason Molenda MachTask *mach_task = (MachTask *)arg; 703a332978bSJason Molenda MachProcess *mach_proc = mach_task->Process(); 704b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, 705b9c1b51eSKate Stone "MachTask::%s ( arg = %p ) starting thread...", __FUNCTION__, 706b9c1b51eSKate Stone arg); 707a332978bSJason Molenda 70836a216eeSJason Molenda#if defined(__APPLE__) 70936a216eeSJason Molenda pthread_setname_np("exception monitoring thread"); 71036a216eeSJason Molenda#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 71136a216eeSJason Molenda struct sched_param thread_param; 71236a216eeSJason Molenda int thread_sched_policy; 713b9c1b51eSKate Stone if (pthread_getschedparam(pthread_self(), &thread_sched_policy, 714b9c1b51eSKate Stone &thread_param) == 0) { 71536a216eeSJason Molenda thread_param.sched_priority = 47; 71636a216eeSJason Molenda pthread_setschedparam(pthread_self(), thread_sched_policy, &thread_param); 71736a216eeSJason Molenda } 71836a216eeSJason Molenda#endif 71936a216eeSJason Molenda#endif 72036a216eeSJason Molenda 721a332978bSJason Molenda // We keep a count of the number of consecutive exceptions received so 722a332978bSJason Molenda // we know to grab all exceptions without a timeout. We do this to get a 723a332978bSJason Molenda // bunch of related exceptions on our exception port so we can process 724a332978bSJason Molenda // then together. When we have multiple threads, we can get an exception 725a332978bSJason Molenda // per thread and they will come in consecutively. The main loop in this 726a332978bSJason Molenda // thread can stop periodically if needed to service things related to this 727a332978bSJason Molenda // process. 728a332978bSJason Molenda // flag set in the options, so we will wait forever for an exception on 729a332978bSJason Molenda // our exception port. After we get one exception, we then will use the 730a332978bSJason Molenda // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current 731a332978bSJason Molenda // exceptions for our process. After we have received the last pending 732a332978bSJason Molenda // exception, we will get a timeout which enables us to then notify 733d93c4a33SBruce Mitchener // our main thread that we have an exception bundle available. We then wait 734a332978bSJason Molenda // for the main thread to tell this exception thread to start trying to get 735a332978bSJason Molenda // exceptions messages again and we start again with a mach_msg read with 736a332978bSJason Molenda // infinite timeout. 737a332978bSJason Molenda uint32_t num_exceptions_received = 0; 738a332978bSJason Molenda DNBError err; 739a332978bSJason Molenda task_t task = mach_task->TaskPort(); 740a332978bSJason Molenda mach_msg_timeout_t periodic_timeout = 0; 741a332978bSJason Molenda 742a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 743a332978bSJason Molenda mach_msg_timeout_t watchdog_elapsed = 0; 744a332978bSJason Molenda mach_msg_timeout_t watchdog_timeout = 60 * 1000; 745a332978bSJason Molenda pid_t pid = mach_proc->ProcessID(); 746a332978bSJason Molenda CFReleaser<SBSWatchdogAssertionRef> watchdog; 747a332978bSJason Molenda 748b9c1b51eSKate Stone if (mach_proc->ProcessUsingSpringBoard()) { 749a332978bSJason Molenda // Request a renewal for every 60 seconds if we attached using SpringBoard 750a332978bSJason Molenda watchdog.reset(::SBSWatchdogAssertionCreateForPID(NULL, pid, 60)); 751b9c1b51eSKate Stone DNBLogThreadedIf( 752b9c1b51eSKate Stone LOG_TASK, "::SBSWatchdogAssertionCreateForPID (NULL, %4.4x, 60 ) => %p", 753b9c1b51eSKate Stone pid, watchdog.get()); 754a332978bSJason Molenda 755b9c1b51eSKate Stone if (watchdog.get()) { 756a332978bSJason Molenda ::SBSWatchdogAssertionRenew(watchdog.get()); 757a332978bSJason Molenda 758b9c1b51eSKate Stone CFTimeInterval watchdogRenewalInterval = 759b9c1b51eSKate Stone ::SBSWatchdogAssertionGetRenewalInterval(watchdog.get()); 760b9c1b51eSKate Stone DNBLogThreadedIf( 761b9c1b51eSKate Stone LOG_TASK, 762b9c1b51eSKate Stone "::SBSWatchdogAssertionGetRenewalInterval ( %p ) => %g seconds", 763b9c1b51eSKate Stone watchdog.get(), watchdogRenewalInterval); 764b9c1b51eSKate Stone if (watchdogRenewalInterval > 0.0) { 765a332978bSJason Molenda watchdog_timeout = (mach_msg_timeout_t)watchdogRenewalInterval * 1000; 766a332978bSJason Molenda if (watchdog_timeout > 3000) 767a332978bSJason Molenda watchdog_timeout -= 1000; // Give us a second to renew our timeout 768a332978bSJason Molenda else if (watchdog_timeout > 1000) 769b9c1b51eSKate Stone watchdog_timeout -= 770b9c1b51eSKate Stone 250; // Give us a quarter of a second to renew our timeout 771a332978bSJason Molenda } 772a332978bSJason Molenda } 773a332978bSJason Molenda if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout) 774a332978bSJason Molenda periodic_timeout = watchdog_timeout; 775a332978bSJason Molenda } 776a332978bSJason Molenda#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) 777a332978bSJason Molenda 778a332978bSJason Molenda#ifdef WITH_BKS 779a332978bSJason Molenda CFReleaser<BKSWatchdogAssertionRef> watchdog; 780b9c1b51eSKate Stone if (mach_proc->ProcessUsingBackBoard()) { 781a332978bSJason Molenda pid_t pid = mach_proc->ProcessID(); 782a332978bSJason Molenda CFAllocatorRef alloc = kCFAllocatorDefault; 783a332978bSJason Molenda watchdog.reset(::BKSWatchdogAssertionCreateForPID(alloc, pid)); 784a332978bSJason Molenda } 785a332978bSJason Molenda#endif // #ifdef WITH_BKS 786a332978bSJason Molenda 787b9c1b51eSKate Stone while (mach_task->ExceptionPortIsValid()) { 788a332978bSJason Molenda ::pthread_testcancel(); 789a332978bSJason Molenda 790a332978bSJason Molenda MachException::Message exception_message; 791a332978bSJason Molenda 792b9c1b51eSKate Stone if (num_exceptions_received > 0) { 793b9c1b51eSKate Stone // No timeout, just receive as many exceptions as we can since we already 794b9c1b51eSKate Stone // have one and we want 795a332978bSJason Molenda // to get all currently available exceptions for this task 796b9c1b51eSKate Stone err = exception_message.Receive( 797b9c1b51eSKate Stone mach_task->ExceptionPort(), 798cc5b509bSFrederic Riss MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, 1); 799b9c1b51eSKate Stone } else if (periodic_timeout > 0) { 800b9c1b51eSKate Stone // We need to stop periodically in this loop, so try and get a mach 801b9c1b51eSKate Stone // message with a valid timeout (ms) 802b9c1b51eSKate Stone err = exception_message.Receive(mach_task->ExceptionPort(), 803b9c1b51eSKate Stone MACH_RCV_MSG | MACH_RCV_INTERRUPT | 804b9c1b51eSKate Stone MACH_RCV_TIMEOUT, 805b9c1b51eSKate Stone periodic_timeout); 806b9c1b51eSKate Stone } else { 807a332978bSJason Molenda // We don't need to parse all current exceptions or stop periodically, 808a332978bSJason Molenda // just wait for an exception forever. 809b9c1b51eSKate Stone err = exception_message.Receive(mach_task->ExceptionPort(), 810b9c1b51eSKate Stone MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0); 811a332978bSJason Molenda } 812a332978bSJason Molenda 81397206d57SZachary Turner if (err.Status() == MACH_RCV_INTERRUPTED) { 814a332978bSJason Molenda // If we have no task port we should exit this thread 815b9c1b51eSKate Stone if (!mach_task->ExceptionPortIsValid()) { 816a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "thread cancelled..."); 817a332978bSJason Molenda break; 818a332978bSJason Molenda } 819a332978bSJason Molenda 820a332978bSJason Molenda // Make sure our task is still valid 821b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 822a332978bSJason Molenda // Task is still ok 823b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, 824b9c1b51eSKate Stone "interrupted, but task still valid, continuing..."); 825a332978bSJason Molenda continue; 826b9c1b51eSKate Stone } else { 827a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 828a332978bSJason Molenda mach_proc->SetState(eStateExited); 829a332978bSJason Molenda // Our task has died, exit the thread. 830a332978bSJason Molenda break; 831a332978bSJason Molenda } 83297206d57SZachary Turner } else if (err.Status() == MACH_RCV_TIMED_OUT) { 833b9c1b51eSKate Stone if (num_exceptions_received > 0) { 834a332978bSJason Molenda // We were receiving all current exceptions with a timeout of zero 835a332978bSJason Molenda // it is time to go back to our normal looping mode 836a332978bSJason Molenda num_exceptions_received = 0; 837a332978bSJason Molenda 838a332978bSJason Molenda // Notify our main thread we have a complete exception message 839bb492890SGreg Clayton // bundle available and get the possibly updated task port back 840bb492890SGreg Clayton // from the process in case we exec'ed and our task port changed 841bb492890SGreg Clayton task = mach_proc->ExceptionMessageBundleComplete(); 842a332978bSJason Molenda 843a332978bSJason Molenda // in case we use a timeout value when getting exceptions... 844a332978bSJason Molenda // Make sure our task is still valid 845b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 846a332978bSJason Molenda // Task is still ok 847a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "got a timeout, continuing..."); 848a332978bSJason Molenda continue; 849b9c1b51eSKate Stone } else { 850a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 851a332978bSJason Molenda mach_proc->SetState(eStateExited); 852a332978bSJason Molenda // Our task has died, exit the thread. 853a332978bSJason Molenda break; 854a332978bSJason Molenda } 855a332978bSJason Molenda } 856a332978bSJason Molenda 857a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 858b9c1b51eSKate Stone if (watchdog.get()) { 859a332978bSJason Molenda watchdog_elapsed += periodic_timeout; 860b9c1b51eSKate Stone if (watchdog_elapsed >= watchdog_timeout) { 861b9c1b51eSKate Stone DNBLogThreadedIf(LOG_TASK, "SBSWatchdogAssertionRenew ( %p )", 862b9c1b51eSKate Stone watchdog.get()); 863a332978bSJason Molenda ::SBSWatchdogAssertionRenew(watchdog.get()); 864a332978bSJason Molenda watchdog_elapsed = 0; 865a332978bSJason Molenda } 866a332978bSJason Molenda } 867a332978bSJason Molenda#endif 86897206d57SZachary Turner } else if (err.Status() != KERN_SUCCESS) { 869b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, "got some other error, do something " 870b9c1b51eSKate Stone "about it??? nah, continuing for " 871b9c1b51eSKate Stone "now..."); 872a332978bSJason Molenda // TODO: notify of error? 873b9c1b51eSKate Stone } else { 874b9c1b51eSKate Stone if (exception_message.CatchExceptionRaise(task)) { 8757ae1a67eSGreg Clayton if (exception_message.state.task_port != task) { 8767ae1a67eSGreg Clayton if (exception_message.state.IsValid()) { 8777ae1a67eSGreg Clayton // We exec'ed and our task port changed on us. 8787ae1a67eSGreg Clayton DNBLogThreadedIf(LOG_EXCEPTIONS, 8797ae1a67eSGreg Clayton "task port changed from 0x%4.4x to 0x%4.4x", 8807ae1a67eSGreg Clayton task, exception_message.state.task_port); 8817ae1a67eSGreg Clayton task = exception_message.state.task_port; 8827ae1a67eSGreg Clayton mach_task->TaskPortChanged(exception_message.state.task_port); 8837ae1a67eSGreg Clayton } 8847ae1a67eSGreg Clayton } 885a332978bSJason Molenda ++num_exceptions_received; 886a332978bSJason Molenda mach_proc->ExceptionMessageReceived(exception_message); 887a332978bSJason Molenda } 888a332978bSJason Molenda } 889a332978bSJason Molenda } 890a332978bSJason Molenda 891a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 892b9c1b51eSKate Stone if (watchdog.get()) { 893b9c1b51eSKate Stone // TODO: change SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel 894b9c1b51eSKate Stone // when we 895b9c1b51eSKate Stone // all are up and running on systems that support it. The SBS framework has 896b9c1b51eSKate Stone // a #define 897b9c1b51eSKate Stone // that will forward SBSWatchdogAssertionRelease to 898b9c1b51eSKate Stone // SBSWatchdogAssertionCancel for now 899a332978bSJason Molenda // so it should still build either way. 900b9c1b51eSKate Stone DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", 901b9c1b51eSKate Stone watchdog.get()); 902a332978bSJason Molenda ::SBSWatchdogAssertionRelease(watchdog.get()); 903a332978bSJason Molenda } 904a332978bSJason Molenda#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) 905a332978bSJason Molenda 906b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s (%p): thread exiting...", 907b9c1b51eSKate Stone __FUNCTION__, arg); 908a332978bSJason Molenda return NULL; 909a332978bSJason Molenda} 910a332978bSJason Molenda 911a332978bSJason Molenda// So the TASK_DYLD_INFO used to just return the address of the all image infos 912a332978bSJason Molenda// as a single member called "all_image_info". Then someone decided it would be 913a332978bSJason Molenda// a good idea to rename this first member to "all_image_info_addr" and add a 914a332978bSJason Molenda// size member called "all_image_info_size". This of course can not be detected 915a332978bSJason Molenda// using code or #defines. So to hack around this problem, we define our own 916b9c1b51eSKate Stone// version of the TASK_DYLD_INFO structure so we can guarantee what is inside 917b9c1b51eSKate Stone// it. 918a332978bSJason Molenda 919a332978bSJason Molendastruct hack_task_dyld_info { 920a332978bSJason Molenda mach_vm_address_t all_image_info_addr; 921a332978bSJason Molenda mach_vm_size_t all_image_info_size; 922a332978bSJason Molenda}; 923a332978bSJason Molenda 924b9c1b51eSKate Stonenub_addr_t MachTask::GetDYLDAllImageInfosAddress(DNBError &err) { 925a332978bSJason Molenda struct hack_task_dyld_info dyld_info; 926a332978bSJason Molenda mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; 927b9c1b51eSKate Stone // Make sure that COUNT isn't bigger than our hacked up struct 928b9c1b51eSKate Stone // hack_task_dyld_info. 929a332978bSJason Molenda // If it is, then make COUNT smaller to match. 930a332978bSJason Molenda if (count > (sizeof(struct hack_task_dyld_info) / sizeof(natural_t))) 931a332978bSJason Molenda count = (sizeof(struct hack_task_dyld_info) / sizeof(natural_t)); 932a332978bSJason Molenda 933a332978bSJason Molenda task_t task = TaskPortForProcessID(err); 934b9c1b51eSKate Stone if (err.Success()) { 935a332978bSJason Molenda err = ::task_info(task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count); 936b9c1b51eSKate Stone if (err.Success()) { 937a332978bSJason Molenda // We now have the address of the all image infos structure 938a332978bSJason Molenda return dyld_info.all_image_info_addr; 939a332978bSJason Molenda } 940a332978bSJason Molenda } 941a332978bSJason Molenda return INVALID_NUB_ADDRESS; 942a332978bSJason Molenda} 943a332978bSJason Molenda 944a332978bSJason Molenda//---------------------------------------------------------------------- 945a332978bSJason Molenda// MachTask::AllocateMemory 946a332978bSJason Molenda//---------------------------------------------------------------------- 947b9c1b51eSKate Stonenub_addr_t MachTask::AllocateMemory(size_t size, uint32_t permissions) { 948a332978bSJason Molenda mach_vm_address_t addr; 949a332978bSJason Molenda task_t task = TaskPort(); 950a332978bSJason Molenda if (task == TASK_NULL) 951a332978bSJason Molenda return INVALID_NUB_ADDRESS; 952a332978bSJason Molenda 953a332978bSJason Molenda DNBError err; 954a332978bSJason Molenda err = ::mach_vm_allocate(task, &addr, size, TRUE); 95597206d57SZachary Turner if (err.Status() == KERN_SUCCESS) { 956a332978bSJason Molenda // Set the protections: 957a332978bSJason Molenda vm_prot_t mach_prot = VM_PROT_NONE; 958a332978bSJason Molenda if (permissions & eMemoryPermissionsReadable) 959a332978bSJason Molenda mach_prot |= VM_PROT_READ; 960a332978bSJason Molenda if (permissions & eMemoryPermissionsWritable) 961a332978bSJason Molenda mach_prot |= VM_PROT_WRITE; 962a332978bSJason Molenda if (permissions & eMemoryPermissionsExecutable) 963a332978bSJason Molenda mach_prot |= VM_PROT_EXECUTE; 964a332978bSJason Molenda 965a332978bSJason Molenda err = ::mach_vm_protect(task, addr, size, 0, mach_prot); 96697206d57SZachary Turner if (err.Status() == KERN_SUCCESS) { 967a332978bSJason Molenda m_allocations.insert(std::make_pair(addr, size)); 968a332978bSJason Molenda return addr; 969a332978bSJason Molenda } 970a332978bSJason Molenda ::mach_vm_deallocate(task, addr, size); 971a332978bSJason Molenda } 972a332978bSJason Molenda return INVALID_NUB_ADDRESS; 973a332978bSJason Molenda} 974a332978bSJason Molenda 975a332978bSJason Molenda//---------------------------------------------------------------------- 976a332978bSJason Molenda// MachTask::DeallocateMemory 977a332978bSJason Molenda//---------------------------------------------------------------------- 978b9c1b51eSKate Stonenub_bool_t MachTask::DeallocateMemory(nub_addr_t addr) { 979a332978bSJason Molenda task_t task = TaskPort(); 980a332978bSJason Molenda if (task == TASK_NULL) 981a332978bSJason Molenda return false; 982a332978bSJason Molenda 983a332978bSJason Molenda // We have to stash away sizes for the allocations... 984a332978bSJason Molenda allocation_collection::iterator pos, end = m_allocations.end(); 985b9c1b51eSKate Stone for (pos = m_allocations.begin(); pos != end; pos++) { 986b9c1b51eSKate Stone if ((*pos).first == addr) { 987*9d4415d0SJason Molenda size_t size = (*pos).second; 988a332978bSJason Molenda m_allocations.erase(pos); 989a332978bSJason Molenda#define ALWAYS_ZOMBIE_ALLOCATIONS 0 990b9c1b51eSKate Stone if (ALWAYS_ZOMBIE_ALLOCATIONS || 991b9c1b51eSKate Stone getenv("DEBUGSERVER_ZOMBIE_ALLOCATIONS")) { 992*9d4415d0SJason Molenda ::mach_vm_protect(task, addr, size, 0, VM_PROT_NONE); 993a332978bSJason Molenda return true; 994b9c1b51eSKate Stone } else 995*9d4415d0SJason Molenda return ::mach_vm_deallocate(task, addr, size) == KERN_SUCCESS; 996a332978bSJason Molenda } 997a332978bSJason Molenda } 998a332978bSJason Molenda return false; 999a332978bSJason Molenda} 1000a332978bSJason Molenda 10017ae1a67eSGreg Claytonvoid MachTask::TaskPortChanged(task_t task) 10027ae1a67eSGreg Clayton{ 10037ae1a67eSGreg Clayton m_task = task; 100483a131b2SJason Molenda 100583a131b2SJason Molenda // If we've just exec'd to a new process, and it 100683a131b2SJason Molenda // is started suspended, we'll need to do two 100783a131b2SJason Molenda // task_resume's to get the inferior process to 100883a131b2SJason Molenda // continue. 100983a131b2SJason Molenda if (m_exec_will_be_suspended) 101083a131b2SJason Molenda m_do_double_resume = true; 101183a131b2SJason Molenda else 101283a131b2SJason Molenda m_do_double_resume = false; 101383a131b2SJason Molenda m_exec_will_be_suspended = false; 10147ae1a67eSGreg Clayton} 1015