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