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 67a332978bSJason Molenda//---------------------------------------------------------------------- 68a332978bSJason Molenda// MachTask constructor 69a332978bSJason Molenda//---------------------------------------------------------------------- 70b9c1b51eSKate StoneMachTask::MachTask(MachProcess *process) 71b9c1b51eSKate Stone : m_process(process), m_task(TASK_NULL), m_vm_memory(), 72*83a131b2SJason Molenda m_exception_thread(0), m_exception_port(MACH_PORT_NULL), 73*83a131b2SJason Molenda m_exec_will_be_suspended(false), m_do_double_resume(false) { 74a332978bSJason Molenda memset(&m_exc_port_info, 0, sizeof(m_exc_port_info)); 75a332978bSJason Molenda} 76a332978bSJason Molenda 77a332978bSJason Molenda//---------------------------------------------------------------------- 78a332978bSJason Molenda// Destructor 79a332978bSJason Molenda//---------------------------------------------------------------------- 80b9c1b51eSKate StoneMachTask::~MachTask() { Clear(); } 81a332978bSJason Molenda 82a332978bSJason Molenda//---------------------------------------------------------------------- 83a332978bSJason Molenda// MachTask::Suspend 84a332978bSJason Molenda//---------------------------------------------------------------------- 85b9c1b51eSKate Stonekern_return_t MachTask::Suspend() { 86a332978bSJason Molenda DNBError err; 87a332978bSJason Molenda task_t task = TaskPort(); 88a332978bSJason Molenda err = ::task_suspend(task); 89a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 90a332978bSJason Molenda err.LogThreaded("::task_suspend ( target_task = 0x%4.4x )", task); 9197206d57SZachary Turner return err.Status(); 92a332978bSJason Molenda} 93a332978bSJason Molenda 94a332978bSJason Molenda//---------------------------------------------------------------------- 95a332978bSJason Molenda// MachTask::Resume 96a332978bSJason Molenda//---------------------------------------------------------------------- 97b9c1b51eSKate Stonekern_return_t MachTask::Resume() { 98a332978bSJason Molenda struct task_basic_info task_info; 99a332978bSJason Molenda task_t task = TaskPort(); 100a332978bSJason Molenda if (task == TASK_NULL) 101a332978bSJason Molenda return KERN_INVALID_ARGUMENT; 102a332978bSJason Molenda 103a332978bSJason Molenda DNBError err; 104a332978bSJason Molenda err = BasicInfo(task, &task_info); 105a332978bSJason Molenda 106b9c1b51eSKate Stone if (err.Success()) { 107*83a131b2SJason Molenda if (m_do_double_resume && task_info.suspend_count == 2) { 108*83a131b2SJason Molenda err = ::task_resume(task); 109*83a131b2SJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 110*83a131b2SJason Molenda err.LogThreaded("::task_resume double-resume after exec-start-stopped " 111*83a131b2SJason Molenda "( target_task = 0x%4.4x )", task); 112*83a131b2SJason Molenda } 113*83a131b2SJason Molenda m_do_double_resume = false; 114*83a131b2SJason Molenda 115a332978bSJason Molenda // task_resume isn't counted like task_suspend calls are, are, so if the 116a332978bSJason Molenda // task is not suspended, don't try and resume it since it is already 117a332978bSJason Molenda // running 118b9c1b51eSKate Stone if (task_info.suspend_count > 0) { 119a332978bSJason Molenda err = ::task_resume(task); 120a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 121a332978bSJason Molenda err.LogThreaded("::task_resume ( target_task = 0x%4.4x )", task); 122a332978bSJason Molenda } 123a332978bSJason Molenda } 12497206d57SZachary Turner return err.Status(); 125a332978bSJason Molenda} 126a332978bSJason Molenda 127a332978bSJason Molenda//---------------------------------------------------------------------- 128a332978bSJason Molenda// MachTask::ExceptionPort 129a332978bSJason Molenda//---------------------------------------------------------------------- 130b9c1b51eSKate Stonemach_port_t MachTask::ExceptionPort() const { return m_exception_port; } 131a332978bSJason Molenda 132a332978bSJason Molenda//---------------------------------------------------------------------- 133a332978bSJason Molenda// MachTask::ExceptionPortIsValid 134a332978bSJason Molenda//---------------------------------------------------------------------- 135b9c1b51eSKate Stonebool MachTask::ExceptionPortIsValid() const { 136a332978bSJason Molenda return MACH_PORT_VALID(m_exception_port); 137a332978bSJason Molenda} 138a332978bSJason Molenda 139a332978bSJason Molenda//---------------------------------------------------------------------- 140a332978bSJason Molenda// MachTask::Clear 141a332978bSJason Molenda//---------------------------------------------------------------------- 142b9c1b51eSKate Stonevoid MachTask::Clear() { 143a332978bSJason Molenda // Do any cleanup needed for this task 144a332978bSJason Molenda m_task = TASK_NULL; 145a332978bSJason Molenda m_exception_thread = 0; 146a332978bSJason Molenda m_exception_port = MACH_PORT_NULL; 147*83a131b2SJason Molenda m_exec_will_be_suspended = false; 148*83a131b2SJason Molenda m_do_double_resume = false; 149a332978bSJason Molenda} 150a332978bSJason Molenda 151a332978bSJason Molenda//---------------------------------------------------------------------- 152a332978bSJason Molenda// MachTask::SaveExceptionPortInfo 153a332978bSJason Molenda//---------------------------------------------------------------------- 154b9c1b51eSKate Stonekern_return_t MachTask::SaveExceptionPortInfo() { 155a332978bSJason Molenda return m_exc_port_info.Save(TaskPort()); 156a332978bSJason Molenda} 157a332978bSJason Molenda 158a332978bSJason Molenda//---------------------------------------------------------------------- 159a332978bSJason Molenda// MachTask::RestoreExceptionPortInfo 160a332978bSJason Molenda//---------------------------------------------------------------------- 161b9c1b51eSKate Stonekern_return_t MachTask::RestoreExceptionPortInfo() { 162a332978bSJason Molenda return m_exc_port_info.Restore(TaskPort()); 163a332978bSJason Molenda} 164a332978bSJason Molenda 165a332978bSJason Molenda//---------------------------------------------------------------------- 166a332978bSJason Molenda// MachTask::ReadMemory 167a332978bSJason Molenda//---------------------------------------------------------------------- 168b9c1b51eSKate Stonenub_size_t MachTask::ReadMemory(nub_addr_t addr, nub_size_t size, void *buf) { 169a332978bSJason Molenda nub_size_t n = 0; 170a332978bSJason Molenda task_t task = TaskPort(); 171b9c1b51eSKate Stone if (task != TASK_NULL) { 172a332978bSJason Molenda n = m_vm_memory.Read(task, addr, buf, size); 173a332978bSJason Molenda 174b9c1b51eSKate Stone DNBLogThreadedIf(LOG_MEMORY, "MachTask::ReadMemory ( addr = 0x%8.8llx, " 175b9c1b51eSKate Stone "size = %llu, buf = %p) => %llu bytes read", 176b9c1b51eSKate Stone (uint64_t)addr, (uint64_t)size, buf, (uint64_t)n); 177b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_MEMORY_DATA_LONG) || 178b9c1b51eSKate Stone (DNBLogCheckLogBit(LOG_MEMORY_DATA_SHORT) && size <= 8)) { 179a332978bSJason Molenda DNBDataRef data((uint8_t *)buf, n, false); 180b9c1b51eSKate Stone data.Dump(0, static_cast<DNBDataRef::offset_t>(n), addr, 181b9c1b51eSKate Stone DNBDataRef::TypeUInt8, 16); 182a332978bSJason Molenda } 183a332978bSJason Molenda } 184a332978bSJason Molenda return n; 185a332978bSJason Molenda} 186a332978bSJason Molenda 187a332978bSJason Molenda//---------------------------------------------------------------------- 188a332978bSJason Molenda// MachTask::WriteMemory 189a332978bSJason Molenda//---------------------------------------------------------------------- 190b9c1b51eSKate Stonenub_size_t MachTask::WriteMemory(nub_addr_t addr, nub_size_t size, 191b9c1b51eSKate Stone const void *buf) { 192a332978bSJason Molenda nub_size_t n = 0; 193a332978bSJason Molenda task_t task = TaskPort(); 194b9c1b51eSKate Stone if (task != TASK_NULL) { 195a332978bSJason Molenda n = m_vm_memory.Write(task, addr, buf, size); 196b9c1b51eSKate Stone DNBLogThreadedIf(LOG_MEMORY, "MachTask::WriteMemory ( addr = 0x%8.8llx, " 197b9c1b51eSKate Stone "size = %llu, buf = %p) => %llu bytes written", 198b9c1b51eSKate Stone (uint64_t)addr, (uint64_t)size, buf, (uint64_t)n); 199b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_MEMORY_DATA_LONG) || 200b9c1b51eSKate Stone (DNBLogCheckLogBit(LOG_MEMORY_DATA_SHORT) && size <= 8)) { 20107d95614SVedant Kumar DNBDataRef data((const uint8_t *)buf, n, false); 202b9c1b51eSKate Stone data.Dump(0, static_cast<DNBDataRef::offset_t>(n), addr, 203b9c1b51eSKate Stone DNBDataRef::TypeUInt8, 16); 204a332978bSJason Molenda } 205a332978bSJason Molenda } 206a332978bSJason Molenda return n; 207a332978bSJason Molenda} 208a332978bSJason Molenda 209a332978bSJason Molenda//---------------------------------------------------------------------- 210a332978bSJason Molenda// MachTask::MemoryRegionInfo 211a332978bSJason Molenda//---------------------------------------------------------------------- 212b9c1b51eSKate Stoneint MachTask::GetMemoryRegionInfo(nub_addr_t addr, DNBRegionInfo *region_info) { 213a332978bSJason Molenda task_t task = TaskPort(); 214a332978bSJason Molenda if (task == TASK_NULL) 215a332978bSJason Molenda return -1; 216a332978bSJason Molenda 217a332978bSJason Molenda int ret = m_vm_memory.GetMemoryRegionInfo(task, addr, region_info); 218b9c1b51eSKate Stone DNBLogThreadedIf(LOG_MEMORY, "MachTask::MemoryRegionInfo ( addr = 0x%8.8llx " 219b9c1b51eSKate Stone ") => %i (start = 0x%8.8llx, size = 0x%8.8llx, " 220b9c1b51eSKate Stone "permissions = %u)", 221b9c1b51eSKate Stone (uint64_t)addr, ret, (uint64_t)region_info->addr, 222b9c1b51eSKate Stone (uint64_t)region_info->size, region_info->permissions); 223a332978bSJason Molenda return ret; 224a332978bSJason Molenda} 225a332978bSJason Molenda 226b9c1b51eSKate Stone#define TIME_VALUE_TO_TIMEVAL(a, r) \ 227b9c1b51eSKate Stone do { \ 228a332978bSJason Molenda (r)->tv_sec = (a)->seconds; \ 229a332978bSJason Molenda (r)->tv_usec = (a)->microseconds; \ 230a332978bSJason Molenda } while (0) 231a332978bSJason Molenda 232a332978bSJason Molenda// We should consider moving this into each MacThread. 233b9c1b51eSKate Stonestatic void get_threads_profile_data(DNBProfileDataScanType scanType, 234b9c1b51eSKate Stone task_t task, nub_process_t pid, 235b9c1b51eSKate Stone std::vector<uint64_t> &threads_id, 236b9c1b51eSKate Stone std::vector<std::string> &threads_name, 237b9c1b51eSKate Stone std::vector<uint64_t> &threads_used_usec) { 238a332978bSJason Molenda kern_return_t kr; 239a332978bSJason Molenda thread_act_array_t threads; 240a332978bSJason Molenda mach_msg_type_number_t tcnt; 241a332978bSJason Molenda 242a332978bSJason Molenda kr = task_threads(task, &threads, &tcnt); 243a332978bSJason Molenda if (kr != KERN_SUCCESS) 244a332978bSJason Molenda return; 245a332978bSJason Molenda 246b9c1b51eSKate Stone for (mach_msg_type_number_t i = 0; i < tcnt; i++) { 247a332978bSJason Molenda thread_identifier_info_data_t identifier_info; 248a332978bSJason Molenda mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT; 249b9c1b51eSKate Stone kr = ::thread_info(threads[i], THREAD_IDENTIFIER_INFO, 250b9c1b51eSKate Stone (thread_info_t)&identifier_info, &count); 251b9c1b51eSKate Stone if (kr != KERN_SUCCESS) 252b9c1b51eSKate Stone continue; 253a332978bSJason Molenda 254a332978bSJason Molenda thread_basic_info_data_t basic_info; 255a332978bSJason Molenda count = THREAD_BASIC_INFO_COUNT; 256b9c1b51eSKate Stone kr = ::thread_info(threads[i], THREAD_BASIC_INFO, 257b9c1b51eSKate Stone (thread_info_t)&basic_info, &count); 258b9c1b51eSKate Stone if (kr != KERN_SUCCESS) 259b9c1b51eSKate Stone continue; 260a332978bSJason Molenda 261b9c1b51eSKate Stone if ((basic_info.flags & TH_FLAGS_IDLE) == 0) { 262b9c1b51eSKate Stone nub_thread_t tid = 263b9c1b51eSKate Stone MachThread::GetGloballyUniqueThreadIDForMachPortID(threads[i]); 264a332978bSJason Molenda threads_id.push_back(tid); 265a332978bSJason Molenda 266b9c1b51eSKate Stone if ((scanType & eProfileThreadName) && 267b9c1b51eSKate Stone (identifier_info.thread_handle != 0)) { 268a332978bSJason Molenda struct proc_threadinfo proc_threadinfo; 269b9c1b51eSKate Stone int len = ::proc_pidinfo(pid, PROC_PIDTHREADINFO, 270b9c1b51eSKate Stone identifier_info.thread_handle, 271b9c1b51eSKate Stone &proc_threadinfo, PROC_PIDTHREADINFO_SIZE); 272b9c1b51eSKate Stone if (len && proc_threadinfo.pth_name[0]) { 273a332978bSJason Molenda threads_name.push_back(proc_threadinfo.pth_name); 274b9c1b51eSKate Stone } else { 275a332978bSJason Molenda threads_name.push_back(""); 276a332978bSJason Molenda } 277b9c1b51eSKate Stone } else { 278a332978bSJason Molenda threads_name.push_back(""); 279a332978bSJason Molenda } 280a332978bSJason Molenda struct timeval tv; 281a332978bSJason Molenda struct timeval thread_tv; 282a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&basic_info.user_time, &thread_tv); 283a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&basic_info.system_time, &tv); 284a332978bSJason Molenda timeradd(&thread_tv, &tv, &thread_tv); 285a332978bSJason Molenda uint64_t used_usec = thread_tv.tv_sec * 1000000ULL + thread_tv.tv_usec; 286a332978bSJason Molenda threads_used_usec.push_back(used_usec); 287a332978bSJason Molenda } 288a332978bSJason Molenda 289aeb9a06fSJason Molenda mach_port_deallocate(mach_task_self(), threads[i]); 290a332978bSJason Molenda } 291b9c1b51eSKate Stone mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)(uintptr_t)threads, 292b9c1b51eSKate Stone tcnt * sizeof(*threads)); 293a332978bSJason Molenda} 294a332978bSJason Molenda 295a332978bSJason Molenda#define RAW_HEXBASE std::setfill('0') << std::hex << std::right 296a332978bSJason Molenda#define DECIMAL std::dec << std::setfill(' ') 297b9c1b51eSKate Stonestd::string MachTask::GetProfileData(DNBProfileDataScanType scanType) { 298a332978bSJason Molenda std::string result; 299a332978bSJason Molenda 300a332978bSJason Molenda static int32_t numCPU = -1; 301a332978bSJason Molenda struct host_cpu_load_info host_info; 302b9c1b51eSKate Stone if (scanType & eProfileHostCPU) { 303a332978bSJason Molenda int32_t mib[] = {CTL_HW, HW_AVAILCPU}; 304a332978bSJason Molenda size_t len = sizeof(numCPU); 305b9c1b51eSKate Stone if (numCPU == -1) { 306b9c1b51eSKate Stone if (sysctl(mib, sizeof(mib) / sizeof(int32_t), &numCPU, &len, NULL, 0) != 307b9c1b51eSKate Stone 0) 308a332978bSJason Molenda return result; 309a332978bSJason Molenda } 310a332978bSJason Molenda 311a332978bSJason Molenda mach_port_t localHost = mach_host_self(); 312a332978bSJason Molenda mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT; 313b9c1b51eSKate Stone kern_return_t kr = host_statistics(localHost, HOST_CPU_LOAD_INFO, 314b9c1b51eSKate Stone (host_info_t)&host_info, &count); 315a332978bSJason Molenda if (kr != KERN_SUCCESS) 316a332978bSJason Molenda return result; 317a332978bSJason Molenda } 318a332978bSJason Molenda 319a332978bSJason Molenda task_t task = TaskPort(); 320a332978bSJason Molenda if (task == TASK_NULL) 321a332978bSJason Molenda return result; 322a332978bSJason Molenda 323705b1809SJason Molenda pid_t pid = m_process->ProcessID(); 324705b1809SJason Molenda 325a332978bSJason Molenda struct task_basic_info task_info; 326a332978bSJason Molenda DNBError err; 327a332978bSJason Molenda err = BasicInfo(task, &task_info); 328a332978bSJason Molenda 329a332978bSJason Molenda if (!err.Success()) 330a332978bSJason Molenda return result; 331a332978bSJason Molenda 332a332978bSJason Molenda uint64_t elapsed_usec = 0; 333a332978bSJason Molenda uint64_t task_used_usec = 0; 334b9c1b51eSKate Stone if (scanType & eProfileCPU) { 335a332978bSJason Molenda // Get current used time. 336a332978bSJason Molenda struct timeval current_used_time; 337a332978bSJason Molenda struct timeval tv; 338a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&task_info.user_time, ¤t_used_time); 339a332978bSJason Molenda TIME_VALUE_TO_TIMEVAL(&task_info.system_time, &tv); 340a332978bSJason Molenda timeradd(¤t_used_time, &tv, ¤t_used_time); 341b9c1b51eSKate Stone task_used_usec = 342b9c1b51eSKate Stone current_used_time.tv_sec * 1000000ULL + current_used_time.tv_usec; 343a332978bSJason Molenda 344a332978bSJason Molenda struct timeval current_elapsed_time; 345a332978bSJason Molenda int res = gettimeofday(¤t_elapsed_time, NULL); 346b9c1b51eSKate Stone if (res == 0) { 347b9c1b51eSKate Stone elapsed_usec = current_elapsed_time.tv_sec * 1000000ULL + 348b9c1b51eSKate Stone current_elapsed_time.tv_usec; 349a332978bSJason Molenda } 350a332978bSJason Molenda } 351a332978bSJason Molenda 352a332978bSJason Molenda std::vector<uint64_t> threads_id; 353a332978bSJason Molenda std::vector<std::string> threads_name; 354a332978bSJason Molenda std::vector<uint64_t> threads_used_usec; 355a332978bSJason Molenda 356b9c1b51eSKate Stone if (scanType & eProfileThreadsCPU) { 357b9c1b51eSKate Stone get_threads_profile_data(scanType, task, pid, threads_id, threads_name, 358b9c1b51eSKate Stone threads_used_usec); 359a332978bSJason Molenda } 360a332978bSJason Molenda 36174c14819SHan Ming Ong vm_statistics64_data_t vminfo; 36241423692SHan Ming Ong uint64_t physical_memory = 0; 36341423692SHan Ming Ong uint64_t anonymous = 0; 36441423692SHan Ming Ong uint64_t phys_footprint = 0; 36541423692SHan Ming Ong uint64_t memory_cap = 0; 366b9c1b51eSKate Stone if (m_vm_memory.GetMemoryProfile(scanType, task, task_info, 367b9c1b51eSKate Stone m_process->GetCPUType(), pid, vminfo, 36841423692SHan Ming Ong physical_memory, anonymous, 36941423692SHan Ming Ong phys_footprint, memory_cap)) { 370a332978bSJason Molenda std::ostringstream profile_data_stream; 371a332978bSJason Molenda 372b9c1b51eSKate Stone if (scanType & eProfileHostCPU) { 373a332978bSJason Molenda profile_data_stream << "num_cpu:" << numCPU << ';'; 374b9c1b51eSKate Stone profile_data_stream << "host_user_ticks:" 375b9c1b51eSKate Stone << host_info.cpu_ticks[CPU_STATE_USER] << ';'; 376b9c1b51eSKate Stone profile_data_stream << "host_sys_ticks:" 377b9c1b51eSKate Stone << host_info.cpu_ticks[CPU_STATE_SYSTEM] << ';'; 378b9c1b51eSKate Stone profile_data_stream << "host_idle_ticks:" 379b9c1b51eSKate Stone << host_info.cpu_ticks[CPU_STATE_IDLE] << ';'; 380a332978bSJason Molenda } 381a332978bSJason Molenda 382b9c1b51eSKate Stone if (scanType & eProfileCPU) { 383a332978bSJason Molenda profile_data_stream << "elapsed_usec:" << elapsed_usec << ';'; 384a332978bSJason Molenda profile_data_stream << "task_used_usec:" << task_used_usec << ';'; 385a332978bSJason Molenda } 386a332978bSJason Molenda 387b9c1b51eSKate Stone if (scanType & eProfileThreadsCPU) { 388ee2ed525SGreg Clayton const size_t num_threads = threads_id.size(); 389b9c1b51eSKate Stone for (size_t i = 0; i < num_threads; i++) { 390b9c1b51eSKate Stone profile_data_stream << "thread_used_id:" << std::hex << threads_id[i] 391b9c1b51eSKate Stone << std::dec << ';'; 392b9c1b51eSKate Stone profile_data_stream << "thread_used_usec:" << threads_used_usec[i] 393b9c1b51eSKate Stone << ';'; 394a332978bSJason Molenda 395b9c1b51eSKate Stone if (scanType & eProfileThreadName) { 396a332978bSJason Molenda profile_data_stream << "thread_used_name:"; 397ee2ed525SGreg Clayton const size_t len = threads_name[i].size(); 398b9c1b51eSKate Stone if (len) { 399a332978bSJason Molenda const char *thread_name = threads_name[i].c_str(); 400a332978bSJason Molenda // Make sure that thread name doesn't interfere with our delimiter. 401a332978bSJason Molenda profile_data_stream << RAW_HEXBASE << std::setw(2); 402a332978bSJason Molenda const uint8_t *ubuf8 = (const uint8_t *)(thread_name); 403b9c1b51eSKate Stone for (size_t j = 0; j < len; j++) { 404a332978bSJason Molenda profile_data_stream << (uint32_t)(ubuf8[j]); 405a332978bSJason Molenda } 406a332978bSJason Molenda // Reset back to DECIMAL. 407a332978bSJason Molenda profile_data_stream << DECIMAL; 408a332978bSJason Molenda } 409a332978bSJason Molenda profile_data_stream << ';'; 410a332978bSJason Molenda } 411a332978bSJason Molenda } 412a332978bSJason Molenda } 413a332978bSJason Molenda 414a332978bSJason Molenda if (scanType & eProfileHostMemory) 415a332978bSJason Molenda profile_data_stream << "total:" << physical_memory << ';'; 416a332978bSJason Molenda 417b9c1b51eSKate Stone if (scanType & eProfileMemory) { 41874c14819SHan Ming Ong static vm_size_t pagesize = vm_kernel_page_size; 419a332978bSJason Molenda 42074c14819SHan Ming Ong // This mimicks Activity Monitor. 421b9c1b51eSKate Stone uint64_t total_used_count = 422b9c1b51eSKate Stone (physical_memory / pagesize) - 423b9c1b51eSKate Stone (vminfo.free_count - vminfo.speculative_count) - 424b9c1b51eSKate Stone vminfo.external_page_count - vminfo.purgeable_count; 425a332978bSJason Molenda profile_data_stream << "used:" << total_used_count * pagesize << ';'; 426a332978bSJason Molenda 427b9c1b51eSKate Stone if (scanType & eProfileMemoryAnonymous) { 428a332978bSJason Molenda profile_data_stream << "anonymous:" << anonymous << ';'; 429a332978bSJason Molenda } 430017edbddSHan Ming Ong 431017edbddSHan Ming Ong profile_data_stream << "phys_footprint:" << phys_footprint << ';'; 432a332978bSJason Molenda } 433a332978bSJason Molenda 43441423692SHan Ming Ong if (scanType & eProfileMemoryCap) { 43541423692SHan Ming Ong profile_data_stream << "mem_cap:" << memory_cap << ';'; 43641423692SHan Ming Ong } 43741423692SHan Ming Ong 438705b1809SJason Molenda#ifdef LLDB_ENERGY 43983d178deSHan Ming Ong if (scanType & eProfileEnergy) { 440705b1809SJason Molenda struct rusage_info_v2 info; 441705b1809SJason Molenda int rc = proc_pid_rusage(pid, RUSAGE_INFO_V2, (rusage_info_t *)&info); 442b9c1b51eSKate Stone if (rc == 0) { 443705b1809SJason Molenda uint64_t now = mach_absolute_time(); 444705b1809SJason Molenda pm_task_energy_data_t pm_energy; 445705b1809SJason Molenda memset(&pm_energy, 0, sizeof(pm_energy)); 446705b1809SJason Molenda /* 447705b1809SJason Molenda * Disable most features of pm_sample_pid. It will gather 448705b1809SJason Molenda * network/GPU/WindowServer information; fill in the rest. 449705b1809SJason Molenda */ 450b9c1b51eSKate Stone pm_sample_task_and_pid(task, pid, &pm_energy, now, 451b9c1b51eSKate Stone PM_SAMPLE_ALL & ~PM_SAMPLE_NAME & 452b9c1b51eSKate Stone ~PM_SAMPLE_INTERVAL & ~PM_SAMPLE_CPU & 453b9c1b51eSKate Stone ~PM_SAMPLE_DISK); 454705b1809SJason Molenda pm_energy.sti.total_user = info.ri_user_time; 455705b1809SJason Molenda pm_energy.sti.total_system = info.ri_system_time; 456705b1809SJason Molenda pm_energy.sti.task_interrupt_wakeups = info.ri_interrupt_wkups; 457705b1809SJason Molenda pm_energy.sti.task_platform_idle_wakeups = info.ri_pkg_idle_wkups; 458705b1809SJason Molenda pm_energy.diskio_bytesread = info.ri_diskio_bytesread; 459705b1809SJason Molenda pm_energy.diskio_byteswritten = info.ri_diskio_byteswritten; 460705b1809SJason Molenda pm_energy.pageins = info.ri_pageins; 461705b1809SJason Molenda 462b9c1b51eSKate Stone uint64_t total_energy = 463b9c1b51eSKate Stone (uint64_t)(pm_energy_impact(&pm_energy) * NSEC_PER_SEC); 464705b1809SJason Molenda // uint64_t process_age = now - info.ri_proc_start_abstime; 465b9c1b51eSKate Stone // uint64_t avg_energy = 100.0 * (double)total_energy / 466b9c1b51eSKate Stone // (double)process_age; 467705b1809SJason Molenda 468705b1809SJason Molenda profile_data_stream << "energy:" << total_energy << ';'; 469705b1809SJason Molenda } 470705b1809SJason Molenda } 471705b1809SJason Molenda#endif 472705b1809SJason Molenda 473a332978bSJason Molenda profile_data_stream << "--end--;"; 474a332978bSJason Molenda 475a332978bSJason Molenda result = profile_data_stream.str(); 476a332978bSJason Molenda } 477a332978bSJason Molenda 478a332978bSJason Molenda return result; 479a332978bSJason Molenda} 480a332978bSJason Molenda 481a332978bSJason Molenda//---------------------------------------------------------------------- 482a332978bSJason Molenda// MachTask::TaskPortForProcessID 483a332978bSJason Molenda//---------------------------------------------------------------------- 484b9c1b51eSKate Stonetask_t MachTask::TaskPortForProcessID(DNBError &err, bool force) { 485bb492890SGreg Clayton if (((m_task == TASK_NULL) || force) && m_process != NULL) 486a332978bSJason Molenda m_task = MachTask::TaskPortForProcessID(m_process->ProcessID(), err); 487a332978bSJason Molenda return m_task; 488a332978bSJason Molenda} 489a332978bSJason Molenda 490a332978bSJason Molenda//---------------------------------------------------------------------- 491a332978bSJason Molenda// MachTask::TaskPortForProcessID 492a332978bSJason Molenda//---------------------------------------------------------------------- 493b9c1b51eSKate Stonetask_t MachTask::TaskPortForProcessID(pid_t pid, DNBError &err, 494b9c1b51eSKate Stone uint32_t num_retries, 495b9c1b51eSKate Stone uint32_t usec_interval) { 496b9c1b51eSKate Stone if (pid != INVALID_NUB_PROCESS) { 497a332978bSJason Molenda DNBError err; 498a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 499a332978bSJason Molenda task_t task = TASK_NULL; 500b9c1b51eSKate Stone for (uint32_t i = 0; i < num_retries; i++) { 501a332978bSJason Molenda err = ::task_for_pid(task_self, pid, &task); 502a332978bSJason Molenda 503b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) { 504a332978bSJason Molenda char str[1024]; 505b9c1b51eSKate Stone ::snprintf(str, sizeof(str), "::task_for_pid ( target_tport = 0x%4.4x, " 506b9c1b51eSKate Stone "pid = %d, &task ) => err = 0x%8.8x (%s)", 50797206d57SZachary Turner task_self, pid, err.Status(), 508a332978bSJason Molenda err.AsString() ? err.AsString() : "success"); 5093bf883eaSJason Molenda if (err.Fail()) { 510a332978bSJason Molenda err.SetErrorString(str); 5113bf883eaSJason Molenda DNBLogError ("MachTask::TaskPortForProcessID task_for_pid failed: %s", str); 5123bf883eaSJason Molenda } 513a332978bSJason Molenda err.LogThreaded(str); 514a332978bSJason Molenda } 515a332978bSJason Molenda 516a332978bSJason Molenda if (err.Success()) 517a332978bSJason Molenda return task; 518a332978bSJason Molenda 519a332978bSJason Molenda // Sleep a bit and try again 520a332978bSJason Molenda ::usleep(usec_interval); 521a332978bSJason Molenda } 522a332978bSJason Molenda } 523a332978bSJason Molenda return TASK_NULL; 524a332978bSJason Molenda} 525a332978bSJason Molenda 526a332978bSJason Molenda//---------------------------------------------------------------------- 527a332978bSJason Molenda// MachTask::BasicInfo 528a332978bSJason Molenda//---------------------------------------------------------------------- 529b9c1b51eSKate Stonekern_return_t MachTask::BasicInfo(struct task_basic_info *info) { 530a332978bSJason Molenda return BasicInfo(TaskPort(), info); 531a332978bSJason Molenda} 532a332978bSJason Molenda 533a332978bSJason Molenda//---------------------------------------------------------------------- 534a332978bSJason Molenda// MachTask::BasicInfo 535a332978bSJason Molenda//---------------------------------------------------------------------- 536b9c1b51eSKate Stonekern_return_t MachTask::BasicInfo(task_t task, struct task_basic_info *info) { 537a332978bSJason Molenda if (info == NULL) 538a332978bSJason Molenda return KERN_INVALID_ARGUMENT; 539a332978bSJason Molenda 540a332978bSJason Molenda DNBError err; 541a332978bSJason Molenda mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT; 542a332978bSJason Molenda err = ::task_info(task, TASK_BASIC_INFO, (task_info_t)info, &count); 543a332978bSJason Molenda const bool log_process = DNBLogCheckLogBit(LOG_TASK); 544a332978bSJason Molenda if (log_process || err.Fail()) 545b9c1b51eSKate Stone err.LogThreaded("::task_info ( target_task = 0x%4.4x, flavor = " 546b9c1b51eSKate Stone "TASK_BASIC_INFO, task_info_out => %p, task_info_outCnt => " 547b9c1b51eSKate Stone "%u )", 548b9c1b51eSKate Stone task, info, count); 549b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_TASK) && DNBLogCheckLogBit(LOG_VERBOSE) && 550b9c1b51eSKate Stone err.Success()) { 551b9c1b51eSKate Stone float user = (float)info->user_time.seconds + 552b9c1b51eSKate Stone (float)info->user_time.microseconds / 1000000.0f; 553b9c1b51eSKate Stone float system = (float)info->user_time.seconds + 554b9c1b51eSKate Stone (float)info->user_time.microseconds / 1000000.0f; 555b9c1b51eSKate Stone DNBLogThreaded("task_basic_info = { suspend_count = %i, virtual_size = " 556b9c1b51eSKate Stone "0x%8.8llx, resident_size = 0x%8.8llx, user_time = %f, " 557b9c1b51eSKate Stone "system_time = %f }", 558b9c1b51eSKate Stone info->suspend_count, (uint64_t)info->virtual_size, 559b9c1b51eSKate Stone (uint64_t)info->resident_size, user, system); 560a332978bSJason Molenda } 56197206d57SZachary Turner return err.Status(); 562a332978bSJason Molenda} 563a332978bSJason Molenda 564b9c1b51eSKate Stone//---------------------------------------------------------------------- 565b9c1b51eSKate Stone// MachTask::IsValid 566b9c1b51eSKate Stone// 567b9c1b51eSKate Stone// Returns true if a task is a valid task port for a current process. 568b9c1b51eSKate Stone//---------------------------------------------------------------------- 569b9c1b51eSKate Stonebool MachTask::IsValid() const { return MachTask::IsValid(TaskPort()); } 570a332978bSJason Molenda 571a332978bSJason Molenda//---------------------------------------------------------------------- 572a332978bSJason Molenda// MachTask::IsValid 573a332978bSJason Molenda// 574a332978bSJason Molenda// Returns true if a task is a valid task port for a current process. 575a332978bSJason Molenda//---------------------------------------------------------------------- 576b9c1b51eSKate Stonebool MachTask::IsValid(task_t task) { 577b9c1b51eSKate Stone if (task != TASK_NULL) { 578a332978bSJason Molenda struct task_basic_info task_info; 579a332978bSJason Molenda return BasicInfo(task, &task_info) == KERN_SUCCESS; 580a332978bSJason Molenda } 581a332978bSJason Molenda return false; 582a332978bSJason Molenda} 583a332978bSJason Molenda 584b9c1b51eSKate Stonebool MachTask::StartExceptionThread(DNBError &err) { 585a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s ( )", __FUNCTION__); 58636a216eeSJason Molenda 587a332978bSJason Molenda task_t task = TaskPortForProcessID(err); 588b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 589a332978bSJason Molenda // Got the mach port for the current process 590a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 591a332978bSJason Molenda 592a332978bSJason Molenda // Allocate an exception port that we will use to track our child process 593b9c1b51eSKate Stone err = ::mach_port_allocate(task_self, MACH_PORT_RIGHT_RECEIVE, 594b9c1b51eSKate Stone &m_exception_port); 595a332978bSJason Molenda if (err.Fail()) 596a332978bSJason Molenda return false; 597a332978bSJason Molenda 598a332978bSJason Molenda // Add the ability to send messages on the new exception port 599b9c1b51eSKate Stone err = ::mach_port_insert_right(task_self, m_exception_port, 600b9c1b51eSKate Stone m_exception_port, MACH_MSG_TYPE_MAKE_SEND); 601a332978bSJason Molenda if (err.Fail()) 602a332978bSJason Molenda return false; 603a332978bSJason Molenda 604a332978bSJason Molenda // Save the original state of the exception ports for our child process 605a332978bSJason Molenda SaveExceptionPortInfo(); 606a332978bSJason Molenda 607a332978bSJason Molenda // We weren't able to save the info for our exception ports, we must stop... 608b9c1b51eSKate Stone if (m_exc_port_info.mask == 0) { 609a332978bSJason Molenda err.SetErrorString("failed to get exception port info"); 610a332978bSJason Molenda return false; 611a332978bSJason Molenda } 612a332978bSJason Molenda 613a332978bSJason Molenda // Set the ability to get all exceptions on this port 614b9c1b51eSKate Stone err = ::task_set_exception_ports( 615b9c1b51eSKate Stone task, m_exc_port_info.mask, m_exception_port, 616b9c1b51eSKate Stone EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE); 617b9c1b51eSKate Stone if (DNBLogCheckLogBit(LOG_EXCEPTIONS) || err.Fail()) { 618b9c1b51eSKate Stone err.LogThreaded("::task_set_exception_ports ( task = 0x%4.4x, " 619b9c1b51eSKate Stone "exception_mask = 0x%8.8x, new_port = 0x%4.4x, behavior " 620b9c1b51eSKate Stone "= 0x%8.8x, new_flavor = 0x%8.8x )", 621b9c1b51eSKate Stone task, m_exc_port_info.mask, m_exception_port, 622a332978bSJason Molenda (EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES), 623a332978bSJason Molenda THREAD_STATE_NONE); 624a332978bSJason Molenda } 625a332978bSJason Molenda 626a332978bSJason Molenda if (err.Fail()) 627a332978bSJason Molenda return false; 628a332978bSJason Molenda 629a332978bSJason Molenda // Create the exception thread 630b9c1b51eSKate Stone err = ::pthread_create(&m_exception_thread, NULL, MachTask::ExceptionThread, 631b9c1b51eSKate Stone this); 632a332978bSJason Molenda return err.Success(); 633b9c1b51eSKate Stone } else { 634b9c1b51eSKate Stone DNBLogError("MachTask::%s (): task invalid, exception thread start failed.", 635b9c1b51eSKate Stone __FUNCTION__); 636a332978bSJason Molenda } 637a332978bSJason Molenda return false; 638a332978bSJason Molenda} 639a332978bSJason Molenda 640b9c1b51eSKate Stonekern_return_t MachTask::ShutDownExcecptionThread() { 641a332978bSJason Molenda DNBError err; 642a332978bSJason Molenda 643a332978bSJason Molenda err = RestoreExceptionPortInfo(); 644a332978bSJason Molenda 645a332978bSJason Molenda // NULL our our exception port and let our exception thread exit 646a332978bSJason Molenda mach_port_t exception_port = m_exception_port; 647a026de05SBruce Mitchener m_exception_port = 0; 648a332978bSJason Molenda 649a332978bSJason Molenda err.SetError(::pthread_cancel(m_exception_thread), DNBError::POSIX); 650a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 651a332978bSJason Molenda err.LogThreaded("::pthread_cancel ( thread = %p )", m_exception_thread); 652a332978bSJason Molenda 653a332978bSJason Molenda err.SetError(::pthread_join(m_exception_thread, NULL), DNBError::POSIX); 654a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 655b9c1b51eSKate Stone err.LogThreaded("::pthread_join ( thread = %p, value_ptr = NULL)", 656b9c1b51eSKate Stone m_exception_thread); 657a332978bSJason Molenda 658a332978bSJason Molenda // Deallocate our exception port that we used to track our child process 659a332978bSJason Molenda mach_port_t task_self = mach_task_self(); 660a332978bSJason Molenda err = ::mach_port_deallocate(task_self, exception_port); 661a332978bSJason Molenda if (DNBLogCheckLogBit(LOG_TASK) || err.Fail()) 662b9c1b51eSKate Stone err.LogThreaded("::mach_port_deallocate ( task = 0x%4.4x, name = 0x%4.4x )", 663b9c1b51eSKate Stone task_self, exception_port); 664a332978bSJason Molenda 665*83a131b2SJason Molenda m_exec_will_be_suspended = false; 666*83a131b2SJason Molenda m_do_double_resume = false; 667*83a131b2SJason Molenda 66897206d57SZachary Turner return err.Status(); 669a332978bSJason Molenda} 670a332978bSJason Molenda 671b9c1b51eSKate Stonevoid *MachTask::ExceptionThread(void *arg) { 672a332978bSJason Molenda if (arg == NULL) 673a332978bSJason Molenda return NULL; 674a332978bSJason Molenda 675a332978bSJason Molenda MachTask *mach_task = (MachTask *)arg; 676a332978bSJason Molenda MachProcess *mach_proc = mach_task->Process(); 677b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, 678b9c1b51eSKate Stone "MachTask::%s ( arg = %p ) starting thread...", __FUNCTION__, 679b9c1b51eSKate Stone arg); 680a332978bSJason Molenda 68136a216eeSJason Molenda#if defined(__APPLE__) 68236a216eeSJason Molenda pthread_setname_np("exception monitoring thread"); 68336a216eeSJason Molenda#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 68436a216eeSJason Molenda struct sched_param thread_param; 68536a216eeSJason Molenda int thread_sched_policy; 686b9c1b51eSKate Stone if (pthread_getschedparam(pthread_self(), &thread_sched_policy, 687b9c1b51eSKate Stone &thread_param) == 0) { 68836a216eeSJason Molenda thread_param.sched_priority = 47; 68936a216eeSJason Molenda pthread_setschedparam(pthread_self(), thread_sched_policy, &thread_param); 69036a216eeSJason Molenda } 69136a216eeSJason Molenda#endif 69236a216eeSJason Molenda#endif 69336a216eeSJason Molenda 694a332978bSJason Molenda // We keep a count of the number of consecutive exceptions received so 695a332978bSJason Molenda // we know to grab all exceptions without a timeout. We do this to get a 696a332978bSJason Molenda // bunch of related exceptions on our exception port so we can process 697a332978bSJason Molenda // then together. When we have multiple threads, we can get an exception 698a332978bSJason Molenda // per thread and they will come in consecutively. The main loop in this 699a332978bSJason Molenda // thread can stop periodically if needed to service things related to this 700a332978bSJason Molenda // process. 701a332978bSJason Molenda // flag set in the options, so we will wait forever for an exception on 702a332978bSJason Molenda // our exception port. After we get one exception, we then will use the 703a332978bSJason Molenda // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current 704a332978bSJason Molenda // exceptions for our process. After we have received the last pending 705a332978bSJason Molenda // exception, we will get a timeout which enables us to then notify 706d93c4a33SBruce Mitchener // our main thread that we have an exception bundle available. We then wait 707a332978bSJason Molenda // for the main thread to tell this exception thread to start trying to get 708a332978bSJason Molenda // exceptions messages again and we start again with a mach_msg read with 709a332978bSJason Molenda // infinite timeout. 710a332978bSJason Molenda uint32_t num_exceptions_received = 0; 711a332978bSJason Molenda DNBError err; 712a332978bSJason Molenda task_t task = mach_task->TaskPort(); 713a332978bSJason Molenda mach_msg_timeout_t periodic_timeout = 0; 714a332978bSJason Molenda 715a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 716a332978bSJason Molenda mach_msg_timeout_t watchdog_elapsed = 0; 717a332978bSJason Molenda mach_msg_timeout_t watchdog_timeout = 60 * 1000; 718a332978bSJason Molenda pid_t pid = mach_proc->ProcessID(); 719a332978bSJason Molenda CFReleaser<SBSWatchdogAssertionRef> watchdog; 720a332978bSJason Molenda 721b9c1b51eSKate Stone if (mach_proc->ProcessUsingSpringBoard()) { 722a332978bSJason Molenda // Request a renewal for every 60 seconds if we attached using SpringBoard 723a332978bSJason Molenda watchdog.reset(::SBSWatchdogAssertionCreateForPID(NULL, pid, 60)); 724b9c1b51eSKate Stone DNBLogThreadedIf( 725b9c1b51eSKate Stone LOG_TASK, "::SBSWatchdogAssertionCreateForPID (NULL, %4.4x, 60 ) => %p", 726b9c1b51eSKate Stone pid, watchdog.get()); 727a332978bSJason Molenda 728b9c1b51eSKate Stone if (watchdog.get()) { 729a332978bSJason Molenda ::SBSWatchdogAssertionRenew(watchdog.get()); 730a332978bSJason Molenda 731b9c1b51eSKate Stone CFTimeInterval watchdogRenewalInterval = 732b9c1b51eSKate Stone ::SBSWatchdogAssertionGetRenewalInterval(watchdog.get()); 733b9c1b51eSKate Stone DNBLogThreadedIf( 734b9c1b51eSKate Stone LOG_TASK, 735b9c1b51eSKate Stone "::SBSWatchdogAssertionGetRenewalInterval ( %p ) => %g seconds", 736b9c1b51eSKate Stone watchdog.get(), watchdogRenewalInterval); 737b9c1b51eSKate Stone if (watchdogRenewalInterval > 0.0) { 738a332978bSJason Molenda watchdog_timeout = (mach_msg_timeout_t)watchdogRenewalInterval * 1000; 739a332978bSJason Molenda if (watchdog_timeout > 3000) 740a332978bSJason Molenda watchdog_timeout -= 1000; // Give us a second to renew our timeout 741a332978bSJason Molenda else if (watchdog_timeout > 1000) 742b9c1b51eSKate Stone watchdog_timeout -= 743b9c1b51eSKate Stone 250; // Give us a quarter of a second to renew our timeout 744a332978bSJason Molenda } 745a332978bSJason Molenda } 746a332978bSJason Molenda if (periodic_timeout == 0 || periodic_timeout > watchdog_timeout) 747a332978bSJason Molenda periodic_timeout = watchdog_timeout; 748a332978bSJason Molenda } 749a332978bSJason Molenda#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) 750a332978bSJason Molenda 751a332978bSJason Molenda#ifdef WITH_BKS 752a332978bSJason Molenda CFReleaser<BKSWatchdogAssertionRef> watchdog; 753b9c1b51eSKate Stone if (mach_proc->ProcessUsingBackBoard()) { 754a332978bSJason Molenda pid_t pid = mach_proc->ProcessID(); 755a332978bSJason Molenda CFAllocatorRef alloc = kCFAllocatorDefault; 756a332978bSJason Molenda watchdog.reset(::BKSWatchdogAssertionCreateForPID(alloc, pid)); 757a332978bSJason Molenda } 758a332978bSJason Molenda#endif // #ifdef WITH_BKS 759a332978bSJason Molenda 760b9c1b51eSKate Stone while (mach_task->ExceptionPortIsValid()) { 761a332978bSJason Molenda ::pthread_testcancel(); 762a332978bSJason Molenda 763a332978bSJason Molenda MachException::Message exception_message; 764a332978bSJason Molenda 765b9c1b51eSKate Stone if (num_exceptions_received > 0) { 766b9c1b51eSKate Stone // No timeout, just receive as many exceptions as we can since we already 767b9c1b51eSKate Stone // have one and we want 768a332978bSJason Molenda // to get all currently available exceptions for this task 769b9c1b51eSKate Stone err = exception_message.Receive( 770b9c1b51eSKate Stone mach_task->ExceptionPort(), 771cc5b509bSFrederic Riss MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_TIMEOUT, 1); 772b9c1b51eSKate Stone } else if (periodic_timeout > 0) { 773b9c1b51eSKate Stone // We need to stop periodically in this loop, so try and get a mach 774b9c1b51eSKate Stone // message with a valid timeout (ms) 775b9c1b51eSKate Stone err = exception_message.Receive(mach_task->ExceptionPort(), 776b9c1b51eSKate Stone MACH_RCV_MSG | MACH_RCV_INTERRUPT | 777b9c1b51eSKate Stone MACH_RCV_TIMEOUT, 778b9c1b51eSKate Stone periodic_timeout); 779b9c1b51eSKate Stone } else { 780a332978bSJason Molenda // We don't need to parse all current exceptions or stop periodically, 781a332978bSJason Molenda // just wait for an exception forever. 782b9c1b51eSKate Stone err = exception_message.Receive(mach_task->ExceptionPort(), 783b9c1b51eSKate Stone MACH_RCV_MSG | MACH_RCV_INTERRUPT, 0); 784a332978bSJason Molenda } 785a332978bSJason Molenda 78697206d57SZachary Turner if (err.Status() == MACH_RCV_INTERRUPTED) { 787a332978bSJason Molenda // If we have no task port we should exit this thread 788b9c1b51eSKate Stone if (!mach_task->ExceptionPortIsValid()) { 789a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "thread cancelled..."); 790a332978bSJason Molenda break; 791a332978bSJason Molenda } 792a332978bSJason Molenda 793a332978bSJason Molenda // Make sure our task is still valid 794b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 795a332978bSJason Molenda // Task is still ok 796b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, 797b9c1b51eSKate Stone "interrupted, but task still valid, continuing..."); 798a332978bSJason Molenda continue; 799b9c1b51eSKate Stone } else { 800a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 801a332978bSJason Molenda mach_proc->SetState(eStateExited); 802a332978bSJason Molenda // Our task has died, exit the thread. 803a332978bSJason Molenda break; 804a332978bSJason Molenda } 80597206d57SZachary Turner } else if (err.Status() == MACH_RCV_TIMED_OUT) { 806b9c1b51eSKate Stone if (num_exceptions_received > 0) { 807a332978bSJason Molenda // We were receiving all current exceptions with a timeout of zero 808a332978bSJason Molenda // it is time to go back to our normal looping mode 809a332978bSJason Molenda num_exceptions_received = 0; 810a332978bSJason Molenda 811a332978bSJason Molenda // Notify our main thread we have a complete exception message 812bb492890SGreg Clayton // bundle available and get the possibly updated task port back 813bb492890SGreg Clayton // from the process in case we exec'ed and our task port changed 814bb492890SGreg Clayton task = mach_proc->ExceptionMessageBundleComplete(); 815a332978bSJason Molenda 816a332978bSJason Molenda // in case we use a timeout value when getting exceptions... 817a332978bSJason Molenda // Make sure our task is still valid 818b9c1b51eSKate Stone if (MachTask::IsValid(task)) { 819a332978bSJason Molenda // Task is still ok 820a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "got a timeout, continuing..."); 821a332978bSJason Molenda continue; 822b9c1b51eSKate Stone } else { 823a332978bSJason Molenda DNBLogThreadedIf(LOG_EXCEPTIONS, "task has exited..."); 824a332978bSJason Molenda mach_proc->SetState(eStateExited); 825a332978bSJason Molenda // Our task has died, exit the thread. 826a332978bSJason Molenda break; 827a332978bSJason Molenda } 828a332978bSJason Molenda } 829a332978bSJason Molenda 830a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 831b9c1b51eSKate Stone if (watchdog.get()) { 832a332978bSJason Molenda watchdog_elapsed += periodic_timeout; 833b9c1b51eSKate Stone if (watchdog_elapsed >= watchdog_timeout) { 834b9c1b51eSKate Stone DNBLogThreadedIf(LOG_TASK, "SBSWatchdogAssertionRenew ( %p )", 835b9c1b51eSKate Stone watchdog.get()); 836a332978bSJason Molenda ::SBSWatchdogAssertionRenew(watchdog.get()); 837a332978bSJason Molenda watchdog_elapsed = 0; 838a332978bSJason Molenda } 839a332978bSJason Molenda } 840a332978bSJason Molenda#endif 84197206d57SZachary Turner } else if (err.Status() != KERN_SUCCESS) { 842b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, "got some other error, do something " 843b9c1b51eSKate Stone "about it??? nah, continuing for " 844b9c1b51eSKate Stone "now..."); 845a332978bSJason Molenda // TODO: notify of error? 846b9c1b51eSKate Stone } else { 847b9c1b51eSKate Stone if (exception_message.CatchExceptionRaise(task)) { 8487ae1a67eSGreg Clayton if (exception_message.state.task_port != task) { 8497ae1a67eSGreg Clayton if (exception_message.state.IsValid()) { 8507ae1a67eSGreg Clayton // We exec'ed and our task port changed on us. 8517ae1a67eSGreg Clayton DNBLogThreadedIf(LOG_EXCEPTIONS, 8527ae1a67eSGreg Clayton "task port changed from 0x%4.4x to 0x%4.4x", 8537ae1a67eSGreg Clayton task, exception_message.state.task_port); 8547ae1a67eSGreg Clayton task = exception_message.state.task_port; 8557ae1a67eSGreg Clayton mach_task->TaskPortChanged(exception_message.state.task_port); 8567ae1a67eSGreg Clayton } 8577ae1a67eSGreg Clayton } 858a332978bSJason Molenda ++num_exceptions_received; 859a332978bSJason Molenda mach_proc->ExceptionMessageReceived(exception_message); 860a332978bSJason Molenda } 861a332978bSJason Molenda } 862a332978bSJason Molenda } 863a332978bSJason Molenda 864a332978bSJason Molenda#if defined(WITH_SPRINGBOARD) && !defined(WITH_BKS) 865b9c1b51eSKate Stone if (watchdog.get()) { 866b9c1b51eSKate Stone // TODO: change SBSWatchdogAssertionRelease to SBSWatchdogAssertionCancel 867b9c1b51eSKate Stone // when we 868b9c1b51eSKate Stone // all are up and running on systems that support it. The SBS framework has 869b9c1b51eSKate Stone // a #define 870b9c1b51eSKate Stone // that will forward SBSWatchdogAssertionRelease to 871b9c1b51eSKate Stone // SBSWatchdogAssertionCancel for now 872a332978bSJason Molenda // so it should still build either way. 873b9c1b51eSKate Stone DNBLogThreadedIf(LOG_TASK, "::SBSWatchdogAssertionRelease(%p)", 874b9c1b51eSKate Stone watchdog.get()); 875a332978bSJason Molenda ::SBSWatchdogAssertionRelease(watchdog.get()); 876a332978bSJason Molenda } 877a332978bSJason Molenda#endif // #if defined (WITH_SPRINGBOARD) && !defined (WITH_BKS) 878a332978bSJason Molenda 879b9c1b51eSKate Stone DNBLogThreadedIf(LOG_EXCEPTIONS, "MachTask::%s (%p): thread exiting...", 880b9c1b51eSKate Stone __FUNCTION__, arg); 881a332978bSJason Molenda return NULL; 882a332978bSJason Molenda} 883a332978bSJason Molenda 884a332978bSJason Molenda// So the TASK_DYLD_INFO used to just return the address of the all image infos 885a332978bSJason Molenda// as a single member called "all_image_info". Then someone decided it would be 886a332978bSJason Molenda// a good idea to rename this first member to "all_image_info_addr" and add a 887a332978bSJason Molenda// size member called "all_image_info_size". This of course can not be detected 888a332978bSJason Molenda// using code or #defines. So to hack around this problem, we define our own 889b9c1b51eSKate Stone// version of the TASK_DYLD_INFO structure so we can guarantee what is inside 890b9c1b51eSKate Stone// it. 891a332978bSJason Molenda 892a332978bSJason Molendastruct hack_task_dyld_info { 893a332978bSJason Molenda mach_vm_address_t all_image_info_addr; 894a332978bSJason Molenda mach_vm_size_t all_image_info_size; 895a332978bSJason Molenda}; 896a332978bSJason Molenda 897b9c1b51eSKate Stonenub_addr_t MachTask::GetDYLDAllImageInfosAddress(DNBError &err) { 898a332978bSJason Molenda struct hack_task_dyld_info dyld_info; 899a332978bSJason Molenda mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; 900b9c1b51eSKate Stone // Make sure that COUNT isn't bigger than our hacked up struct 901b9c1b51eSKate Stone // hack_task_dyld_info. 902a332978bSJason Molenda // If it is, then make COUNT smaller to match. 903a332978bSJason Molenda if (count > (sizeof(struct hack_task_dyld_info) / sizeof(natural_t))) 904a332978bSJason Molenda count = (sizeof(struct hack_task_dyld_info) / sizeof(natural_t)); 905a332978bSJason Molenda 906a332978bSJason Molenda task_t task = TaskPortForProcessID(err); 907b9c1b51eSKate Stone if (err.Success()) { 908a332978bSJason Molenda err = ::task_info(task, TASK_DYLD_INFO, (task_info_t)&dyld_info, &count); 909b9c1b51eSKate Stone if (err.Success()) { 910a332978bSJason Molenda // We now have the address of the all image infos structure 911a332978bSJason Molenda return dyld_info.all_image_info_addr; 912a332978bSJason Molenda } 913a332978bSJason Molenda } 914a332978bSJason Molenda return INVALID_NUB_ADDRESS; 915a332978bSJason Molenda} 916a332978bSJason Molenda 917a332978bSJason Molenda//---------------------------------------------------------------------- 918a332978bSJason Molenda// MachTask::AllocateMemory 919a332978bSJason Molenda//---------------------------------------------------------------------- 920b9c1b51eSKate Stonenub_addr_t MachTask::AllocateMemory(size_t size, uint32_t permissions) { 921a332978bSJason Molenda mach_vm_address_t addr; 922a332978bSJason Molenda task_t task = TaskPort(); 923a332978bSJason Molenda if (task == TASK_NULL) 924a332978bSJason Molenda return INVALID_NUB_ADDRESS; 925a332978bSJason Molenda 926a332978bSJason Molenda DNBError err; 927a332978bSJason Molenda err = ::mach_vm_allocate(task, &addr, size, TRUE); 92897206d57SZachary Turner if (err.Status() == KERN_SUCCESS) { 929a332978bSJason Molenda // Set the protections: 930a332978bSJason Molenda vm_prot_t mach_prot = VM_PROT_NONE; 931a332978bSJason Molenda if (permissions & eMemoryPermissionsReadable) 932a332978bSJason Molenda mach_prot |= VM_PROT_READ; 933a332978bSJason Molenda if (permissions & eMemoryPermissionsWritable) 934a332978bSJason Molenda mach_prot |= VM_PROT_WRITE; 935a332978bSJason Molenda if (permissions & eMemoryPermissionsExecutable) 936a332978bSJason Molenda mach_prot |= VM_PROT_EXECUTE; 937a332978bSJason Molenda 938a332978bSJason Molenda err = ::mach_vm_protect(task, addr, size, 0, mach_prot); 93997206d57SZachary Turner if (err.Status() == KERN_SUCCESS) { 940a332978bSJason Molenda m_allocations.insert(std::make_pair(addr, size)); 941a332978bSJason Molenda return addr; 942a332978bSJason Molenda } 943a332978bSJason Molenda ::mach_vm_deallocate(task, addr, size); 944a332978bSJason Molenda } 945a332978bSJason Molenda return INVALID_NUB_ADDRESS; 946a332978bSJason Molenda} 947a332978bSJason Molenda 948a332978bSJason Molenda//---------------------------------------------------------------------- 949a332978bSJason Molenda// MachTask::DeallocateMemory 950a332978bSJason Molenda//---------------------------------------------------------------------- 951b9c1b51eSKate Stonenub_bool_t MachTask::DeallocateMemory(nub_addr_t addr) { 952a332978bSJason Molenda task_t task = TaskPort(); 953a332978bSJason Molenda if (task == TASK_NULL) 954a332978bSJason Molenda return false; 955a332978bSJason Molenda 956a332978bSJason Molenda // We have to stash away sizes for the allocations... 957a332978bSJason Molenda allocation_collection::iterator pos, end = m_allocations.end(); 958b9c1b51eSKate Stone for (pos = m_allocations.begin(); pos != end; pos++) { 959b9c1b51eSKate Stone if ((*pos).first == addr) { 960a332978bSJason Molenda m_allocations.erase(pos); 961a332978bSJason Molenda#define ALWAYS_ZOMBIE_ALLOCATIONS 0 962b9c1b51eSKate Stone if (ALWAYS_ZOMBIE_ALLOCATIONS || 963b9c1b51eSKate Stone getenv("DEBUGSERVER_ZOMBIE_ALLOCATIONS")) { 964a332978bSJason Molenda ::mach_vm_protect(task, (*pos).first, (*pos).second, 0, VM_PROT_NONE); 965a332978bSJason Molenda return true; 966b9c1b51eSKate Stone } else 967b9c1b51eSKate Stone return ::mach_vm_deallocate(task, (*pos).first, (*pos).second) == 968b9c1b51eSKate Stone KERN_SUCCESS; 969a332978bSJason Molenda } 970a332978bSJason Molenda } 971a332978bSJason Molenda return false; 972a332978bSJason Molenda} 973a332978bSJason Molenda 9747ae1a67eSGreg Claytonvoid MachTask::TaskPortChanged(task_t task) 9757ae1a67eSGreg Clayton{ 9767ae1a67eSGreg Clayton m_task = task; 977*83a131b2SJason Molenda 978*83a131b2SJason Molenda // If we've just exec'd to a new process, and it 979*83a131b2SJason Molenda // is started suspended, we'll need to do two 980*83a131b2SJason Molenda // task_resume's to get the inferior process to 981*83a131b2SJason Molenda // continue. 982*83a131b2SJason Molenda if (m_exec_will_be_suspended) 983*83a131b2SJason Molenda m_do_double_resume = true; 984*83a131b2SJason Molenda else 985*83a131b2SJason Molenda m_do_double_resume = false; 986*83a131b2SJason Molenda m_exec_will_be_suspended = false; 9877ae1a67eSGreg Clayton} 988