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