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