1//===-- MachProcess.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//  Created by Greg Clayton on 6/15/07.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DNB.h"
14#include "MacOSX/CFUtils.h"
15#include "SysSignal.h"
16#include <dlfcn.h>
17#include <inttypes.h>
18#include <mach-o/loader.h>
19#include <mach/mach.h>
20#include <mach/task.h>
21#include <pthread.h>
22#include <signal.h>
23#include <spawn.h>
24#include <sys/fcntl.h>
25#include <sys/ptrace.h>
26#include <sys/stat.h>
27#include <sys/sysctl.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <unistd.h>
31#include <uuid/uuid.h>
32
33#include <algorithm>
34#include <chrono>
35#include <map>
36
37#include <TargetConditionals.h>
38#import <Foundation/Foundation.h>
39
40#include "DNBDataRef.h"
41#include "DNBLog.h"
42#include "DNBThreadResumeActions.h"
43#include "DNBTimer.h"
44#include "MachProcess.h"
45#include "PseudoTerminal.h"
46
47#include "CFBundle.h"
48#include "CFString.h"
49
50#ifndef PLATFORM_BRIDGEOS
51#define PLATFORM_BRIDGEOS 5
52#endif
53
54#ifndef PLATFORM_MACCATALYST
55#define PLATFORM_MACCATALYST 6
56#endif
57
58#ifndef PLATFORM_IOSSIMULATOR
59#define PLATFORM_IOSSIMULATOR 7
60#endif
61
62#ifndef PLATFORM_TVOSSIMULATOR
63#define PLATFORM_TVOSSIMULATOR 8
64#endif
65
66#ifndef PLATFORM_WATCHOSSIMULATOR
67#define PLATFORM_WATCHOSSIMULATOR 9
68#endif
69
70#ifndef PLATFORM_DRIVERKIT
71#define PLATFORM_DRIVERKIT 10
72#endif
73
74#ifdef WITH_SPRINGBOARD
75
76#include <CoreFoundation/CoreFoundation.h>
77#include <SpringBoardServices/SBSWatchdogAssertion.h>
78#include <SpringBoardServices/SpringBoardServer.h>
79
80#endif // WITH_SPRINGBOARD
81
82#if WITH_CAROUSEL
83// For definition of CSLSOpenApplicationOptionForClockKit.
84#include <CarouselServices/CSLSOpenApplicationOptions.h>
85#endif // WITH_CAROUSEL
86
87#if defined(WITH_SPRINGBOARD) || defined(WITH_BKS) || defined(WITH_FBS)
88// This returns a CFRetained pointer to the Bundle ID for app_bundle_path,
89// or NULL if there was some problem getting the bundle id.
90static CFStringRef CopyBundleIDForPath(const char *app_bundle_path,
91                                       DNBError &err_str);
92#endif
93
94#if defined(WITH_BKS) || defined(WITH_FBS)
95#import <Foundation/Foundation.h>
96static const int OPEN_APPLICATION_TIMEOUT_ERROR = 111;
97typedef void (*SetErrorFunction)(NSInteger, std::string, DNBError &);
98typedef bool (*CallOpenApplicationFunction)(NSString *bundleIDNSStr,
99                                            NSDictionary *options,
100                                            DNBError &error, pid_t *return_pid);
101
102// This function runs the BKSSystemService (or FBSSystemService) method
103// openApplication:options:clientPort:withResult,
104// messaging the app passed in bundleIDNSStr.
105// The function should be run inside of an NSAutoReleasePool.
106//
107// It will use the "options" dictionary passed in, and fill the error passed in
108// if there is an error.
109// If return_pid is not NULL, we'll fetch the pid that was made for the
110// bundleID.
111// If bundleIDNSStr is NULL, then the system application will be messaged.
112
113template <typename OpenFlavor, typename ErrorFlavor,
114          ErrorFlavor no_error_enum_value, SetErrorFunction error_function>
115static bool CallBoardSystemServiceOpenApplication(NSString *bundleIDNSStr,
116                                                  NSDictionary *options,
117                                                  DNBError &error,
118                                                  pid_t *return_pid) {
119  // Now make our systemService:
120  OpenFlavor *system_service = [[OpenFlavor alloc] init];
121
122  if (bundleIDNSStr == nil) {
123    bundleIDNSStr = [system_service systemApplicationBundleIdentifier];
124    if (bundleIDNSStr == nil) {
125      // Okay, no system app...
126      error.SetErrorString("No system application to message.");
127      return false;
128    }
129  }
130
131  mach_port_t client_port = [system_service createClientPort];
132  __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
133  __block ErrorFlavor open_app_error = no_error_enum_value;
134  __block std::string open_app_error_string;
135  bool wants_pid = (return_pid != NULL);
136  __block pid_t pid_in_block;
137
138  const char *cstr = [bundleIDNSStr UTF8String];
139  if (!cstr)
140    cstr = "<Unknown Bundle ID>";
141
142  NSString *description = [options description];
143  DNBLog("About to launch process for bundle ID: %s - options:\n%s", cstr,
144    [description UTF8String]);
145  [system_service
146      openApplication:bundleIDNSStr
147              options:options
148           clientPort:client_port
149           withResult:^(NSError *bks_error) {
150             // The system service will cleanup the client port we created for
151             // us.
152             if (bks_error)
153               open_app_error = (ErrorFlavor)[bks_error code];
154
155             if (open_app_error == no_error_enum_value) {
156               if (wants_pid) {
157                 pid_in_block =
158                     [system_service pidForApplication:bundleIDNSStr];
159                 DNBLog(
160                     "In completion handler, got pid for bundle id, pid: %d.",
161                     pid_in_block);
162                 DNBLogThreadedIf(
163                     LOG_PROCESS,
164                     "In completion handler, got pid for bundle id, pid: %d.",
165                     pid_in_block);
166               } else
167                 DNBLogThreadedIf(LOG_PROCESS,
168                                  "In completion handler: success.");
169             } else {
170               const char *error_str =
171                   [(NSString *)[bks_error localizedDescription] UTF8String];
172               if (error_str) {
173                 open_app_error_string = error_str;
174                 DNBLogError("In app launch attempt, got error "
175                             "localizedDescription '%s'.", error_str);
176                 const char *obj_desc =
177                      [NSString stringWithFormat:@"%@", bks_error].UTF8String;
178                 DNBLogError("In app launch attempt, got error "
179                             "NSError object description: '%s'.",
180                             obj_desc);
181               }
182               DNBLogThreadedIf(LOG_PROCESS, "In completion handler for send "
183                                             "event, got error \"%s\"(%ld).",
184                                error_str ? error_str : "<unknown error>",
185                                open_app_error);
186             }
187
188             [system_service release];
189             dispatch_semaphore_signal(semaphore);
190           }
191
192  ];
193
194  const uint32_t timeout_secs = 30;
195
196  dispatch_time_t timeout =
197      dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC);
198
199  long success = dispatch_semaphore_wait(semaphore, timeout) == 0;
200
201  dispatch_release(semaphore);
202
203  if (!success) {
204    DNBLogError("timed out trying to send openApplication to %s.", cstr);
205    error.SetError(OPEN_APPLICATION_TIMEOUT_ERROR, DNBError::Generic);
206    error.SetErrorString("timed out trying to launch app");
207  } else if (open_app_error != no_error_enum_value) {
208    error_function(open_app_error, open_app_error_string, error);
209    DNBLogError("unable to launch the application with CFBundleIdentifier '%s' "
210                "bks_error = %u",
211                cstr, open_app_error);
212    success = false;
213  } else if (wants_pid) {
214    *return_pid = pid_in_block;
215    DNBLogThreadedIf(
216        LOG_PROCESS,
217        "Out of completion handler, pid from block %d and passing out: %d",
218        pid_in_block, *return_pid);
219  }
220
221  return success;
222}
223#endif
224
225#if defined(WITH_BKS) || defined(WITH_FBS)
226static void SplitEventData(const char *data, std::vector<std::string> &elements)
227{
228  elements.clear();
229  if (!data)
230    return;
231
232  const char *start = data;
233
234  while (*start != '\0') {
235    const char *token = strchr(start, ':');
236    if (!token) {
237      elements.push_back(std::string(start));
238      return;
239    }
240    if (token != start)
241      elements.push_back(std::string(start, token - start));
242    start = ++token;
243  }
244}
245#endif
246
247#ifdef WITH_BKS
248#import <Foundation/Foundation.h>
249extern "C" {
250#import <BackBoardServices/BKSOpenApplicationConstants_Private.h>
251#import <BackBoardServices/BKSSystemService_LaunchServices.h>
252#import <BackBoardServices/BackBoardServices.h>
253}
254
255static bool IsBKSProcess(nub_process_t pid) {
256  BKSApplicationStateMonitor *state_monitor =
257      [[BKSApplicationStateMonitor alloc] init];
258  BKSApplicationState app_state =
259      [state_monitor mostElevatedApplicationStateForPID:pid];
260  return app_state != BKSApplicationStateUnknown;
261}
262
263static void SetBKSError(NSInteger error_code,
264                        std::string error_description,
265                        DNBError &error) {
266  error.SetError(error_code, DNBError::BackBoard);
267  NSString *err_nsstr = ::BKSOpenApplicationErrorCodeToString(
268      (BKSOpenApplicationErrorCode)error_code);
269  std::string err_str = "unknown BKS error";
270  if (error_description.empty() == false) {
271    err_str = error_description;
272  } else if (err_nsstr != nullptr) {
273    err_str = [err_nsstr UTF8String];
274  }
275  error.SetErrorString(err_str.c_str());
276}
277
278static bool BKSAddEventDataToOptions(NSMutableDictionary *options,
279                                     const char *event_data,
280                                     DNBError &option_error) {
281  std::vector<std::string> values;
282  SplitEventData(event_data, values);
283  bool found_one = false;
284  for (std::string value : values)
285  {
286      if (value.compare("BackgroundContentFetching") == 0) {
287        DNBLog("Setting ActivateForEvent key in options dictionary.");
288        NSDictionary *event_details = [NSDictionary dictionary];
289        NSDictionary *event_dictionary = [NSDictionary
290            dictionaryWithObject:event_details
291                          forKey:
292                              BKSActivateForEventOptionTypeBackgroundContentFetching];
293        [options setObject:event_dictionary
294                    forKey:BKSOpenApplicationOptionKeyActivateForEvent];
295        found_one = true;
296      } else if (value.compare("ActivateSuspended") == 0) {
297        DNBLog("Setting ActivateSuspended key in options dictionary.");
298        [options setObject:@YES forKey: BKSOpenApplicationOptionKeyActivateSuspended];
299        found_one = true;
300      } else {
301        DNBLogError("Unrecognized event type: %s.  Ignoring.", value.c_str());
302        option_error.SetErrorString("Unrecognized event data");
303      }
304  }
305  return found_one;
306}
307
308static NSMutableDictionary *BKSCreateOptionsDictionary(
309    const char *app_bundle_path, NSMutableArray *launch_argv,
310    NSMutableDictionary *launch_envp, NSString *stdio_path, bool disable_aslr,
311    const char *event_data) {
312  NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
313  if (launch_argv != nil)
314    [debug_options setObject:launch_argv forKey:BKSDebugOptionKeyArguments];
315  if (launch_envp != nil)
316    [debug_options setObject:launch_envp forKey:BKSDebugOptionKeyEnvironment];
317
318  [debug_options setObject:stdio_path forKey:BKSDebugOptionKeyStandardOutPath];
319  [debug_options setObject:stdio_path
320                    forKey:BKSDebugOptionKeyStandardErrorPath];
321  [debug_options setObject:[NSNumber numberWithBool:YES]
322                    forKey:BKSDebugOptionKeyWaitForDebugger];
323  if (disable_aslr)
324    [debug_options setObject:[NSNumber numberWithBool:YES]
325                      forKey:BKSDebugOptionKeyDisableASLR];
326
327  // That will go in the overall dictionary:
328
329  NSMutableDictionary *options = [NSMutableDictionary dictionary];
330  [options setObject:debug_options
331              forKey:BKSOpenApplicationOptionKeyDebuggingOptions];
332  // And there are some other options at the top level in this dictionary:
333  [options setObject:[NSNumber numberWithBool:YES]
334              forKey:BKSOpenApplicationOptionKeyUnlockDevice];
335
336  DNBError error;
337  BKSAddEventDataToOptions(options, event_data, error);
338
339  return options;
340}
341
342static CallOpenApplicationFunction BKSCallOpenApplicationFunction =
343    CallBoardSystemServiceOpenApplication<
344        BKSSystemService, BKSOpenApplicationErrorCode,
345        BKSOpenApplicationErrorCodeNone, SetBKSError>;
346#endif // WITH_BKS
347
348#ifdef WITH_FBS
349#import <Foundation/Foundation.h>
350extern "C" {
351#import <FrontBoardServices/FBSOpenApplicationConstants_Private.h>
352#import <FrontBoardServices/FBSSystemService_LaunchServices.h>
353#import <FrontBoardServices/FrontBoardServices.h>
354#import <MobileCoreServices/LSResourceProxy.h>
355#import <MobileCoreServices/MobileCoreServices.h>
356}
357
358#ifdef WITH_BKS
359static bool IsFBSProcess(nub_process_t pid) {
360  BKSApplicationStateMonitor *state_monitor =
361      [[BKSApplicationStateMonitor alloc] init];
362  BKSApplicationState app_state =
363      [state_monitor mostElevatedApplicationStateForPID:pid];
364  return app_state != BKSApplicationStateUnknown;
365}
366#else
367static bool IsFBSProcess(nub_process_t pid) {
368  // FIXME: What is the FBS equivalent of BKSApplicationStateMonitor
369  return false;
370}
371#endif
372
373static void SetFBSError(NSInteger error_code,
374                        std::string error_description,
375                        DNBError &error) {
376  error.SetError((DNBError::ValueType)error_code, DNBError::FrontBoard);
377  NSString *err_nsstr = ::FBSOpenApplicationErrorCodeToString(
378      (FBSOpenApplicationErrorCode)error_code);
379  std::string err_str = "unknown FBS error";
380  if (error_description.empty() == false) {
381    err_str = error_description;
382  } else if (err_nsstr != nullptr) {
383    err_str = [err_nsstr UTF8String];
384  }
385  error.SetErrorString(err_str.c_str());
386}
387
388static bool FBSAddEventDataToOptions(NSMutableDictionary *options,
389                                     const char *event_data,
390                                     DNBError &option_error) {
391  std::vector<std::string> values;
392  SplitEventData(event_data, values);
393  bool found_one = false;
394  for (std::string value : values)
395  {
396      if (value.compare("BackgroundContentFetching") == 0) {
397        DNBLog("Setting ActivateForEvent key in options dictionary.");
398        NSDictionary *event_details = [NSDictionary dictionary];
399        NSDictionary *event_dictionary = [NSDictionary
400            dictionaryWithObject:event_details
401                          forKey:
402                              FBSActivateForEventOptionTypeBackgroundContentFetching];
403        [options setObject:event_dictionary
404                    forKey:FBSOpenApplicationOptionKeyActivateForEvent];
405        found_one = true;
406      } else if (value.compare("ActivateSuspended") == 0) {
407        DNBLog("Setting ActivateSuspended key in options dictionary.");
408        [options setObject:@YES forKey: FBSOpenApplicationOptionKeyActivateSuspended];
409        found_one = true;
410#if WITH_CAROUSEL
411      } else if (value.compare("WatchComplicationLaunch") == 0) {
412        DNBLog("Setting FBSOpenApplicationOptionKeyActivateSuspended key in options dictionary.");
413        [options setObject:@YES forKey: CSLSOpenApplicationOptionForClockKit];
414        found_one = true;
415#endif // WITH_CAROUSEL
416      } else {
417        DNBLogError("Unrecognized event type: %s.  Ignoring.", value.c_str());
418        option_error.SetErrorString("Unrecognized event data.");
419      }
420  }
421  return found_one;
422}
423
424static NSMutableDictionary *
425FBSCreateOptionsDictionary(const char *app_bundle_path,
426                           NSMutableArray *launch_argv,
427                           NSDictionary *launch_envp, NSString *stdio_path,
428                           bool disable_aslr, const char *event_data) {
429  NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
430
431  if (launch_argv != nil)
432    [debug_options setObject:launch_argv forKey:FBSDebugOptionKeyArguments];
433  if (launch_envp != nil)
434    [debug_options setObject:launch_envp forKey:FBSDebugOptionKeyEnvironment];
435
436  [debug_options setObject:stdio_path forKey:FBSDebugOptionKeyStandardOutPath];
437  [debug_options setObject:stdio_path
438                    forKey:FBSDebugOptionKeyStandardErrorPath];
439  [debug_options setObject:[NSNumber numberWithBool:YES]
440                    forKey:FBSDebugOptionKeyWaitForDebugger];
441  if (disable_aslr)
442    [debug_options setObject:[NSNumber numberWithBool:YES]
443                      forKey:FBSDebugOptionKeyDisableASLR];
444
445  // That will go in the overall dictionary:
446
447  NSMutableDictionary *options = [NSMutableDictionary dictionary];
448  [options setObject:debug_options
449              forKey:FBSOpenApplicationOptionKeyDebuggingOptions];
450  // And there are some other options at the top level in this dictionary:
451  [options setObject:[NSNumber numberWithBool:YES]
452              forKey:FBSOpenApplicationOptionKeyUnlockDevice];
453
454  // We have to get the "sequence ID & UUID" for this app bundle path and send
455  // them to FBS:
456
457  NSURL *app_bundle_url =
458      [NSURL fileURLWithPath:[NSString stringWithUTF8String:app_bundle_path]
459                 isDirectory:YES];
460  LSApplicationProxy *app_proxy =
461      [LSApplicationProxy applicationProxyForBundleURL:app_bundle_url];
462  if (app_proxy) {
463    DNBLog("Sending AppProxy info: sequence no: %lu, GUID: %s.",
464           app_proxy.sequenceNumber,
465           [app_proxy.cacheGUID.UUIDString UTF8String]);
466    [options
467        setObject:[NSNumber numberWithUnsignedInteger:app_proxy.sequenceNumber]
468           forKey:FBSOpenApplicationOptionKeyLSSequenceNumber];
469    [options setObject:app_proxy.cacheGUID.UUIDString
470                forKey:FBSOpenApplicationOptionKeyLSCacheGUID];
471  }
472
473  DNBError error;
474  FBSAddEventDataToOptions(options, event_data, error);
475
476  return options;
477}
478static CallOpenApplicationFunction FBSCallOpenApplicationFunction =
479    CallBoardSystemServiceOpenApplication<
480        FBSSystemService, FBSOpenApplicationErrorCode,
481        FBSOpenApplicationErrorCodeNone, SetFBSError>;
482#endif // WITH_FBS
483
484#if 0
485#define DEBUG_LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
486#else
487#define DEBUG_LOG(fmt, ...)
488#endif
489
490#ifndef MACH_PROCESS_USE_POSIX_SPAWN
491#define MACH_PROCESS_USE_POSIX_SPAWN 1
492#endif
493
494#ifndef _POSIX_SPAWN_DISABLE_ASLR
495#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
496#endif
497
498
499MachProcess::MachProcess()
500    : m_pid(0), m_cpu_type(0), m_child_stdin(-1), m_child_stdout(-1),
501      m_child_stderr(-1), m_path(), m_args(), m_task(this),
502      m_flags(eMachProcessFlagsNone), m_stdio_thread(0),
503      m_stdio_mutex(PTHREAD_MUTEX_RECURSIVE), m_stdout_data(),
504      m_profile_enabled(false), m_profile_interval_usec(0), m_profile_thread(0),
505      m_profile_data_mutex(PTHREAD_MUTEX_RECURSIVE), m_profile_data(),
506      m_profile_events(0, eMachProcessProfileCancel),
507      m_thread_actions(), m_exception_messages(),
508      m_exception_messages_mutex(PTHREAD_MUTEX_RECURSIVE), m_thread_list(),
509      m_activities(), m_state(eStateUnloaded),
510      m_state_mutex(PTHREAD_MUTEX_RECURSIVE), m_events(0, kAllEventsMask),
511      m_private_events(0, kAllEventsMask), m_breakpoints(), m_watchpoints(),
512      m_name_to_addr_callback(NULL), m_name_to_addr_baton(NULL),
513      m_image_infos_callback(NULL), m_image_infos_baton(NULL),
514      m_sent_interrupt_signo(0), m_auto_resume_signo(0), m_did_exec(false),
515      m_dyld_process_info_create(nullptr),
516      m_dyld_process_info_for_each_image(nullptr),
517      m_dyld_process_info_release(nullptr),
518      m_dyld_process_info_get_cache(nullptr) {
519  m_dyld_process_info_create =
520      (void *(*)(task_t task, uint64_t timestamp, kern_return_t * kernelError))
521          dlsym(RTLD_DEFAULT, "_dyld_process_info_create");
522  m_dyld_process_info_for_each_image =
523      (void (*)(void *info, void (^)(uint64_t machHeaderAddress,
524                                     const uuid_t uuid, const char *path)))
525          dlsym(RTLD_DEFAULT, "_dyld_process_info_for_each_image");
526  m_dyld_process_info_release =
527      (void (*)(void *info))dlsym(RTLD_DEFAULT, "_dyld_process_info_release");
528  m_dyld_process_info_get_cache = (void (*)(void *info, void *cacheInfo))dlsym(
529      RTLD_DEFAULT, "_dyld_process_info_get_cache");
530  m_dyld_process_info_get_platform = (uint32_t (*)(void *info))dlsym(
531      RTLD_DEFAULT, "_dyld_process_info_get_platform");
532
533  DNBLogThreadedIf(LOG_PROCESS | LOG_VERBOSE, "%s", __PRETTY_FUNCTION__);
534}
535
536MachProcess::~MachProcess() {
537  DNBLogThreadedIf(LOG_PROCESS | LOG_VERBOSE, "%s", __PRETTY_FUNCTION__);
538  Clear();
539}
540
541pid_t MachProcess::SetProcessID(pid_t pid) {
542  // Free any previous process specific data or resources
543  Clear();
544  // Set the current PID appropriately
545  if (pid == 0)
546    m_pid = ::getpid();
547  else
548    m_pid = pid;
549  return m_pid; // Return actually PID in case a zero pid was passed in
550}
551
552nub_state_t MachProcess::GetState() {
553  // If any other threads access this we will need a mutex for it
554  PTHREAD_MUTEX_LOCKER(locker, m_state_mutex);
555  return m_state;
556}
557
558const char *MachProcess::ThreadGetName(nub_thread_t tid) {
559  return m_thread_list.GetName(tid);
560}
561
562nub_state_t MachProcess::ThreadGetState(nub_thread_t tid) {
563  return m_thread_list.GetState(tid);
564}
565
566nub_size_t MachProcess::GetNumThreads() const {
567  return m_thread_list.NumThreads();
568}
569
570nub_thread_t MachProcess::GetThreadAtIndex(nub_size_t thread_idx) const {
571  return m_thread_list.ThreadIDAtIndex(thread_idx);
572}
573
574nub_thread_t
575MachProcess::GetThreadIDForMachPortNumber(thread_t mach_port_number) const {
576  return m_thread_list.GetThreadIDByMachPortNumber(mach_port_number);
577}
578
579nub_bool_t MachProcess::SyncThreadState(nub_thread_t tid) {
580  MachThreadSP thread_sp(m_thread_list.GetThreadByID(tid));
581  if (!thread_sp)
582    return false;
583  kern_return_t kret = ::thread_abort_safely(thread_sp->MachPortNumber());
584  DNBLogThreadedIf(LOG_THREAD, "thread = 0x%8.8" PRIx32
585                               " calling thread_abort_safely (tid) => %u "
586                               "(GetGPRState() for stop_count = %u)",
587                   thread_sp->MachPortNumber(), kret,
588                   thread_sp->Process()->StopCount());
589
590  if (kret == KERN_SUCCESS)
591    return true;
592  else
593    return false;
594}
595
596ThreadInfo::QoS MachProcess::GetRequestedQoS(nub_thread_t tid, nub_addr_t tsd,
597                                             uint64_t dti_qos_class_index) {
598  return m_thread_list.GetRequestedQoS(tid, tsd, dti_qos_class_index);
599}
600
601nub_addr_t MachProcess::GetPThreadT(nub_thread_t tid) {
602  return m_thread_list.GetPThreadT(tid);
603}
604
605nub_addr_t MachProcess::GetDispatchQueueT(nub_thread_t tid) {
606  return m_thread_list.GetDispatchQueueT(tid);
607}
608
609nub_addr_t MachProcess::GetTSDAddressForThread(
610    nub_thread_t tid, uint64_t plo_pthread_tsd_base_address_offset,
611    uint64_t plo_pthread_tsd_base_offset, uint64_t plo_pthread_tsd_entry_size) {
612  return m_thread_list.GetTSDAddressForThread(
613      tid, plo_pthread_tsd_base_address_offset, plo_pthread_tsd_base_offset,
614      plo_pthread_tsd_entry_size);
615}
616
617MachProcess::DeploymentInfo
618MachProcess::GetDeploymentInfo(const struct load_command &lc,
619                               uint64_t load_command_address,
620                               bool is_executable) {
621  DeploymentInfo info;
622  uint32_t cmd = lc.cmd & ~LC_REQ_DYLD;
623
624  // Handle the older LC_VERSION load commands, which don't
625  // distinguish between simulator and real hardware.
626  auto handle_version_min = [&](char platform) {
627    struct version_min_command vers_cmd;
628    if (ReadMemory(load_command_address, sizeof(struct version_min_command),
629                   &vers_cmd) != sizeof(struct version_min_command))
630      return;
631    info.platform = platform;
632    info.major_version = vers_cmd.version >> 16;
633    info.minor_version = (vers_cmd.version >> 8) & 0xffu;
634    info.patch_version = vers_cmd.version & 0xffu;
635
636    // Disambiguate legacy simulator platforms.
637#if (defined(__x86_64__) || defined(__i386__))
638    // If we are running on Intel macOS, it is safe to assume this is
639    // really a back-deploying simulator binary.
640    switch (info.platform) {
641    case PLATFORM_IOS:
642      info.platform = PLATFORM_IOSSIMULATOR;
643      break;
644    case PLATFORM_TVOS:
645      info.platform = PLATFORM_TVOSSIMULATOR;
646      break;
647    case PLATFORM_WATCHOS:
648      info.platform = PLATFORM_WATCHOSSIMULATOR;
649      break;
650    }
651#else
652    // On an Apple Silicon macOS host, there is no ambiguity. The only
653    // binaries that use legacy load commands are back-deploying
654    // native iOS binaries. All simulator binaries use the newer,
655    // unambiguous LC_BUILD_VERSION load commands.
656#endif
657  };
658
659  switch (cmd) {
660  case LC_VERSION_MIN_IPHONEOS:
661    handle_version_min(PLATFORM_IOS);
662    break;
663  case LC_VERSION_MIN_MACOSX:
664    handle_version_min(PLATFORM_MACOS);
665    break;
666  case LC_VERSION_MIN_TVOS:
667    handle_version_min(PLATFORM_TVOS);
668    break;
669  case LC_VERSION_MIN_WATCHOS:
670    handle_version_min(PLATFORM_WATCHOS);
671    break;
672#if defined(LC_BUILD_VERSION)
673  case LC_BUILD_VERSION: {
674    struct build_version_command build_vers;
675    if (ReadMemory(load_command_address, sizeof(struct build_version_command),
676                   &build_vers) != sizeof(struct build_version_command))
677      break;
678    info.platform = build_vers.platform;
679    info.major_version = build_vers.minos >> 16;
680    info.minor_version = (build_vers.minos >> 8) & 0xffu;
681    info.patch_version = build_vers.minos & 0xffu;
682    break;
683  }
684#endif
685  }
686
687  // The xctest binary is a pure macOS binary but is launched with
688  // DYLD_FORCE_PLATFORM=6. In that case, force the platform to
689  // macCatalyst and use the macCatalyst version of the host OS
690  // instead of the macOS deployment target.
691  if (is_executable && GetProcessPlatformViaDYLDSPI() == PLATFORM_MACCATALYST) {
692    info.platform = PLATFORM_MACCATALYST;
693    std::string catalyst_version = GetMacCatalystVersionString();
694    const char *major = catalyst_version.c_str();
695    char *minor = nullptr;
696    char *patch = nullptr;
697    info.major_version = std::strtoul(major, &minor, 10);
698    info.minor_version = 0;
699    info.patch_version = 0;
700    if (minor && *minor == '.') {
701      info.minor_version = std::strtoul(++minor, &patch, 10);
702      if (patch && *patch == '.')
703        info.patch_version = std::strtoul(++patch, nullptr, 10);
704    }
705  }
706
707  return info;
708}
709
710const char *MachProcess::GetPlatformString(unsigned char platform) {
711  switch (platform) {
712  case PLATFORM_MACOS:
713    return "macosx";
714  case PLATFORM_MACCATALYST:
715    return "maccatalyst";
716  case PLATFORM_IOS:
717    return "ios";
718  case PLATFORM_IOSSIMULATOR:
719    return "iossimulator";
720  case PLATFORM_TVOS:
721    return "tvos";
722  case PLATFORM_TVOSSIMULATOR:
723    return "tvossimulator";
724  case PLATFORM_WATCHOS:
725    return "watchos";
726  case PLATFORM_WATCHOSSIMULATOR:
727    return "watchossimulator";
728  case PLATFORM_BRIDGEOS:
729    return "bridgeos";
730  case PLATFORM_DRIVERKIT:
731    return "driverkit";
732  }
733  return nullptr;
734}
735
736// Given an address, read the mach-o header and load commands out of memory to
737// fill in
738// the mach_o_information "inf" object.
739//
740// Returns false if there was an error in reading this mach-o file header/load
741// commands.
742
743bool MachProcess::GetMachOInformationFromMemory(
744    uint32_t dyld_platform, nub_addr_t mach_o_header_addr, int wordsize,
745    struct mach_o_information &inf) {
746  uint64_t load_cmds_p;
747  if (wordsize == 4) {
748    struct mach_header header;
749    if (ReadMemory(mach_o_header_addr, sizeof(struct mach_header), &header) !=
750        sizeof(struct mach_header)) {
751      return false;
752    }
753    load_cmds_p = mach_o_header_addr + sizeof(struct mach_header);
754    inf.mach_header.magic = header.magic;
755    inf.mach_header.cputype = header.cputype;
756    // high byte of cpusubtype is used for "capability bits", v.
757    // CPU_SUBTYPE_MASK, CPU_SUBTYPE_LIB64 in machine.h
758    inf.mach_header.cpusubtype = header.cpusubtype & 0x00ffffff;
759    inf.mach_header.filetype = header.filetype;
760    inf.mach_header.ncmds = header.ncmds;
761    inf.mach_header.sizeofcmds = header.sizeofcmds;
762    inf.mach_header.flags = header.flags;
763  } else {
764    struct mach_header_64 header;
765    if (ReadMemory(mach_o_header_addr, sizeof(struct mach_header_64),
766                   &header) != sizeof(struct mach_header_64)) {
767      return false;
768    }
769    load_cmds_p = mach_o_header_addr + sizeof(struct mach_header_64);
770    inf.mach_header.magic = header.magic;
771    inf.mach_header.cputype = header.cputype;
772    // high byte of cpusubtype is used for "capability bits", v.
773    // CPU_SUBTYPE_MASK, CPU_SUBTYPE_LIB64 in machine.h
774    inf.mach_header.cpusubtype = header.cpusubtype & 0x00ffffff;
775    inf.mach_header.filetype = header.filetype;
776    inf.mach_header.ncmds = header.ncmds;
777    inf.mach_header.sizeofcmds = header.sizeofcmds;
778    inf.mach_header.flags = header.flags;
779  }
780  for (uint32_t j = 0; j < inf.mach_header.ncmds; j++) {
781    struct load_command lc;
782    if (ReadMemory(load_cmds_p, sizeof(struct load_command), &lc) !=
783        sizeof(struct load_command)) {
784      return false;
785    }
786    if (lc.cmd == LC_SEGMENT) {
787      struct segment_command seg;
788      if (ReadMemory(load_cmds_p, sizeof(struct segment_command), &seg) !=
789          sizeof(struct segment_command)) {
790        return false;
791      }
792      struct mach_o_segment this_seg;
793      char name[17];
794      ::memset(name, 0, sizeof(name));
795      memcpy(name, seg.segname, sizeof(seg.segname));
796      this_seg.name = name;
797      this_seg.vmaddr = seg.vmaddr;
798      this_seg.vmsize = seg.vmsize;
799      this_seg.fileoff = seg.fileoff;
800      this_seg.filesize = seg.filesize;
801      this_seg.maxprot = seg.maxprot;
802      this_seg.initprot = seg.initprot;
803      this_seg.nsects = seg.nsects;
804      this_seg.flags = seg.flags;
805      inf.segments.push_back(this_seg);
806      if (this_seg.name == "ExecExtraSuspend")
807        m_task.TaskWillExecProcessesSuspended();
808    }
809    if (lc.cmd == LC_SEGMENT_64) {
810      struct segment_command_64 seg;
811      if (ReadMemory(load_cmds_p, sizeof(struct segment_command_64), &seg) !=
812          sizeof(struct segment_command_64)) {
813        return false;
814      }
815      struct mach_o_segment this_seg;
816      char name[17];
817      ::memset(name, 0, sizeof(name));
818      memcpy(name, seg.segname, sizeof(seg.segname));
819      this_seg.name = name;
820      this_seg.vmaddr = seg.vmaddr;
821      this_seg.vmsize = seg.vmsize;
822      this_seg.fileoff = seg.fileoff;
823      this_seg.filesize = seg.filesize;
824      this_seg.maxprot = seg.maxprot;
825      this_seg.initprot = seg.initprot;
826      this_seg.nsects = seg.nsects;
827      this_seg.flags = seg.flags;
828      inf.segments.push_back(this_seg);
829      if (this_seg.name == "ExecExtraSuspend")
830        m_task.TaskWillExecProcessesSuspended();
831    }
832    if (lc.cmd == LC_UUID) {
833      struct uuid_command uuidcmd;
834      if (ReadMemory(load_cmds_p, sizeof(struct uuid_command), &uuidcmd) ==
835          sizeof(struct uuid_command))
836        uuid_copy(inf.uuid, uuidcmd.uuid);
837    }
838    if (DeploymentInfo deployment_info = GetDeploymentInfo(
839            lc, load_cmds_p, inf.mach_header.filetype == MH_EXECUTE)) {
840      const char *lc_platform = GetPlatformString(deployment_info.platform);
841      if (dyld_platform != PLATFORM_MACCATALYST &&
842          inf.min_version_os_name == "macosx") {
843        // macCatalyst support.
844        //
845        // This the special case of "zippered" frameworks that have both
846        // a PLATFORM_MACOS and a PLATFORM_MACCATALYST load command.
847        //
848        // When we are in this block, this is a binary with both
849        // PLATFORM_MACOS and PLATFORM_MACCATALYST load commands and
850        // the process is not running as PLATFORM_MACCATALYST. Stick
851        // with the "macosx" load command that we've already
852        // processed, ignore this one, which is presumed to be a
853        // PLATFORM_MACCATALYST one.
854      } else {
855        inf.min_version_os_name = lc_platform;
856        inf.min_version_os_version = "";
857        inf.min_version_os_version +=
858            std::to_string(deployment_info.major_version);
859        inf.min_version_os_version += ".";
860        inf.min_version_os_version +=
861            std::to_string(deployment_info.minor_version);
862        if (deployment_info.patch_version != 0) {
863          inf.min_version_os_version += ".";
864          inf.min_version_os_version +=
865              std::to_string(deployment_info.patch_version);
866        }
867      }
868    }
869
870    load_cmds_p += lc.cmdsize;
871  }
872  return true;
873}
874
875// Given completely filled in array of binary_image_information structures,
876// create a JSONGenerator object
877// with all the details we want to send to lldb.
878JSONGenerator::ObjectSP MachProcess::FormatDynamicLibrariesIntoJSON(
879    const std::vector<struct binary_image_information> &image_infos) {
880
881  JSONGenerator::ArraySP image_infos_array_sp(new JSONGenerator::Array());
882
883  const size_t image_count = image_infos.size();
884
885  for (size_t i = 0; i < image_count; i++) {
886    JSONGenerator::DictionarySP image_info_dict_sp(
887        new JSONGenerator::Dictionary());
888    image_info_dict_sp->AddIntegerItem("load_address",
889                                       image_infos[i].load_address);
890    image_info_dict_sp->AddIntegerItem("mod_date", image_infos[i].mod_date);
891    image_info_dict_sp->AddStringItem("pathname", image_infos[i].filename);
892
893    uuid_string_t uuidstr;
894    uuid_unparse_upper(image_infos[i].macho_info.uuid, uuidstr);
895    image_info_dict_sp->AddStringItem("uuid", uuidstr);
896
897    if (!image_infos[i].macho_info.min_version_os_name.empty() &&
898        !image_infos[i].macho_info.min_version_os_version.empty()) {
899      image_info_dict_sp->AddStringItem(
900          "min_version_os_name", image_infos[i].macho_info.min_version_os_name);
901      image_info_dict_sp->AddStringItem(
902          "min_version_os_sdk",
903          image_infos[i].macho_info.min_version_os_version);
904    }
905
906    JSONGenerator::DictionarySP mach_header_dict_sp(
907        new JSONGenerator::Dictionary());
908    mach_header_dict_sp->AddIntegerItem(
909        "magic", image_infos[i].macho_info.mach_header.magic);
910    mach_header_dict_sp->AddIntegerItem(
911        "cputype", (uint32_t)image_infos[i].macho_info.mach_header.cputype);
912    mach_header_dict_sp->AddIntegerItem(
913        "cpusubtype",
914        (uint32_t)image_infos[i].macho_info.mach_header.cpusubtype);
915    mach_header_dict_sp->AddIntegerItem(
916        "filetype", image_infos[i].macho_info.mach_header.filetype);
917    mach_header_dict_sp->AddIntegerItem ("flags",
918                         image_infos[i].macho_info.mach_header.flags);
919
920    //          DynamicLoaderMacOSX doesn't currently need these fields, so
921    //          don't send them.
922    //            mach_header_dict_sp->AddIntegerItem ("ncmds",
923    //            image_infos[i].macho_info.mach_header.ncmds);
924    //            mach_header_dict_sp->AddIntegerItem ("sizeofcmds",
925    //            image_infos[i].macho_info.mach_header.sizeofcmds);
926    image_info_dict_sp->AddItem("mach_header", mach_header_dict_sp);
927
928    JSONGenerator::ArraySP segments_sp(new JSONGenerator::Array());
929    for (size_t j = 0; j < image_infos[i].macho_info.segments.size(); j++) {
930      JSONGenerator::DictionarySP segment_sp(new JSONGenerator::Dictionary());
931      segment_sp->AddStringItem("name",
932                                image_infos[i].macho_info.segments[j].name);
933      segment_sp->AddIntegerItem("vmaddr",
934                                 image_infos[i].macho_info.segments[j].vmaddr);
935      segment_sp->AddIntegerItem("vmsize",
936                                 image_infos[i].macho_info.segments[j].vmsize);
937      segment_sp->AddIntegerItem("fileoff",
938                                 image_infos[i].macho_info.segments[j].fileoff);
939      segment_sp->AddIntegerItem(
940          "filesize", image_infos[i].macho_info.segments[j].filesize);
941      segment_sp->AddIntegerItem("maxprot",
942                                 image_infos[i].macho_info.segments[j].maxprot);
943
944      //              DynamicLoaderMacOSX doesn't currently need these fields,
945      //              so don't send them.
946      //                segment_sp->AddIntegerItem ("initprot",
947      //                image_infos[i].macho_info.segments[j].initprot);
948      //                segment_sp->AddIntegerItem ("nsects",
949      //                image_infos[i].macho_info.segments[j].nsects);
950      //                segment_sp->AddIntegerItem ("flags",
951      //                image_infos[i].macho_info.segments[j].flags);
952      segments_sp->AddItem(segment_sp);
953    }
954    image_info_dict_sp->AddItem("segments", segments_sp);
955
956    image_infos_array_sp->AddItem(image_info_dict_sp);
957  }
958
959  JSONGenerator::DictionarySP reply_sp(new JSONGenerator::Dictionary());
960  ;
961  reply_sp->AddItem("images", image_infos_array_sp);
962
963  return reply_sp;
964}
965
966// Get the shared library information using the old (pre-macOS 10.12, pre-iOS
967// 10, pre-tvOS 10, pre-watchOS 3)
968// code path.  We'll be given the address of an array of structures in the form
969// {void* load_addr, void* mod_date, void* pathname}
970//
971// In macOS 10.12 etc and newer, we'll use SPI calls into dyld to gather this
972// information.
973JSONGenerator::ObjectSP MachProcess::GetLoadedDynamicLibrariesInfos(
974    nub_process_t pid, nub_addr_t image_list_address, nub_addr_t image_count) {
975  JSONGenerator::DictionarySP reply_sp;
976
977  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
978  struct kinfo_proc processInfo;
979  size_t bufsize = sizeof(processInfo);
980  if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo, &bufsize,
981             NULL, 0) == 0 &&
982      bufsize > 0) {
983    uint32_t pointer_size = 4;
984    if (processInfo.kp_proc.p_flag & P_LP64)
985      pointer_size = 8;
986
987    std::vector<struct binary_image_information> image_infos;
988    size_t image_infos_size = image_count * 3 * pointer_size;
989
990    uint8_t *image_info_buf = (uint8_t *)malloc(image_infos_size);
991    if (image_info_buf == NULL) {
992      return reply_sp;
993    }
994    if (ReadMemory(image_list_address, image_infos_size, image_info_buf) !=
995        image_infos_size) {
996      return reply_sp;
997    }
998
999    ////  First the image_infos array with (load addr, pathname, mod date)
1000    ///tuples
1001
1002    for (size_t i = 0; i < image_count; i++) {
1003      struct binary_image_information info;
1004      nub_addr_t pathname_address;
1005      if (pointer_size == 4) {
1006        uint32_t load_address_32;
1007        uint32_t pathname_address_32;
1008        uint32_t mod_date_32;
1009        ::memcpy(&load_address_32, image_info_buf + (i * 3 * pointer_size), 4);
1010        ::memcpy(&pathname_address_32,
1011                 image_info_buf + (i * 3 * pointer_size) + pointer_size, 4);
1012        ::memcpy(&mod_date_32, image_info_buf + (i * 3 * pointer_size) +
1013                                   pointer_size + pointer_size,
1014                 4);
1015        info.load_address = load_address_32;
1016        info.mod_date = mod_date_32;
1017        pathname_address = pathname_address_32;
1018      } else {
1019        uint64_t load_address_64;
1020        uint64_t pathname_address_64;
1021        uint64_t mod_date_64;
1022        ::memcpy(&load_address_64, image_info_buf + (i * 3 * pointer_size), 8);
1023        ::memcpy(&pathname_address_64,
1024                 image_info_buf + (i * 3 * pointer_size) + pointer_size, 8);
1025        ::memcpy(&mod_date_64, image_info_buf + (i * 3 * pointer_size) +
1026                                   pointer_size + pointer_size,
1027                 8);
1028        info.load_address = load_address_64;
1029        info.mod_date = mod_date_64;
1030        pathname_address = pathname_address_64;
1031      }
1032      char strbuf[17];
1033      info.filename = "";
1034      uint64_t pathname_ptr = pathname_address;
1035      bool still_reading = true;
1036      while (still_reading &&
1037             ReadMemory(pathname_ptr, sizeof(strbuf) - 1, strbuf) ==
1038                 sizeof(strbuf) - 1) {
1039        strbuf[sizeof(strbuf) - 1] = '\0';
1040        info.filename += strbuf;
1041        pathname_ptr += sizeof(strbuf) - 1;
1042        // Stop if we found nul byte indicating the end of the string
1043        for (size_t i = 0; i < sizeof(strbuf) - 1; i++) {
1044          if (strbuf[i] == '\0') {
1045            still_reading = false;
1046            break;
1047          }
1048        }
1049      }
1050      uuid_clear(info.macho_info.uuid);
1051      image_infos.push_back(info);
1052    }
1053    if (image_infos.size() == 0) {
1054      return reply_sp;
1055    }
1056
1057    free(image_info_buf);
1058
1059    ////  Second, read the mach header / load commands for all the dylibs
1060
1061    for (size_t i = 0; i < image_count; i++) {
1062      // The SPI to provide platform is not available on older systems.
1063      uint32_t platform = 0;
1064      if (!GetMachOInformationFromMemory(platform,
1065                                         image_infos[i].load_address,
1066                                         pointer_size,
1067                                         image_infos[i].macho_info)) {
1068        return reply_sp;
1069      }
1070    }
1071
1072    ////  Third, format all of the above in the JSONGenerator object.
1073
1074    return FormatDynamicLibrariesIntoJSON(image_infos);
1075  }
1076
1077  return reply_sp;
1078}
1079
1080/// From dyld SPI header dyld_process_info.h
1081typedef void *dyld_process_info;
1082struct dyld_process_cache_info {
1083  /// UUID of cache used by process.
1084  uuid_t cacheUUID;
1085  /// Load address of dyld shared cache.
1086  uint64_t cacheBaseAddress;
1087  /// Process is running without a dyld cache.
1088  bool noCache;
1089  /// Process is using a private copy of its dyld cache.
1090  bool privateCache;
1091};
1092
1093uint32_t MachProcess::GetProcessPlatformViaDYLDSPI() {
1094  kern_return_t kern_ret;
1095  uint32_t platform = 0;
1096  if (m_dyld_process_info_create) {
1097    dyld_process_info info =
1098        m_dyld_process_info_create(m_task.TaskPort(), 0, &kern_ret);
1099    if (info) {
1100      if (m_dyld_process_info_get_platform)
1101        platform = m_dyld_process_info_get_platform(info);
1102      m_dyld_process_info_release(info);
1103    }
1104  }
1105  return platform;
1106}
1107
1108void MachProcess::GetAllLoadedBinariesViaDYLDSPI(
1109    std::vector<struct binary_image_information> &image_infos) {
1110  kern_return_t kern_ret;
1111  if (m_dyld_process_info_create) {
1112    dyld_process_info info =
1113        m_dyld_process_info_create(m_task.TaskPort(), 0, &kern_ret);
1114    if (info) {
1115      m_dyld_process_info_for_each_image(
1116          info,
1117          ^(uint64_t mach_header_addr, const uuid_t uuid, const char *path) {
1118            struct binary_image_information image;
1119            image.filename = path;
1120            uuid_copy(image.macho_info.uuid, uuid);
1121            image.load_address = mach_header_addr;
1122            image_infos.push_back(image);
1123          });
1124      m_dyld_process_info_release(info);
1125    }
1126  }
1127}
1128
1129// Fetch information about all shared libraries using the dyld SPIs that exist
1130// in
1131// macOS 10.12, iOS 10, tvOS 10, watchOS 3 and newer.
1132JSONGenerator::ObjectSP
1133MachProcess::GetAllLoadedLibrariesInfos(nub_process_t pid) {
1134  JSONGenerator::DictionarySP reply_sp;
1135
1136  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
1137  struct kinfo_proc processInfo;
1138  size_t bufsize = sizeof(processInfo);
1139  if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo, &bufsize,
1140             NULL, 0) == 0 &&
1141      bufsize > 0) {
1142    uint32_t pointer_size = 4;
1143    if (processInfo.kp_proc.p_flag & P_LP64)
1144      pointer_size = 8;
1145
1146    std::vector<struct binary_image_information> image_infos;
1147    GetAllLoadedBinariesViaDYLDSPI(image_infos);
1148    uint32_t platform = GetProcessPlatformViaDYLDSPI();
1149    const size_t image_count = image_infos.size();
1150    for (size_t i = 0; i < image_count; i++) {
1151      GetMachOInformationFromMemory(platform,
1152                                    image_infos[i].load_address, pointer_size,
1153                                    image_infos[i].macho_info);
1154    }
1155    return FormatDynamicLibrariesIntoJSON(image_infos);
1156  }
1157  return reply_sp;
1158}
1159
1160// Fetch information about the shared libraries at the given load addresses
1161// using the
1162// dyld SPIs that exist in macOS 10.12, iOS 10, tvOS 10, watchOS 3 and newer.
1163JSONGenerator::ObjectSP MachProcess::GetLibrariesInfoForAddresses(
1164    nub_process_t pid, std::vector<uint64_t> &macho_addresses) {
1165  JSONGenerator::DictionarySP reply_sp;
1166
1167  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
1168  struct kinfo_proc processInfo;
1169  size_t bufsize = sizeof(processInfo);
1170  if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo, &bufsize,
1171             NULL, 0) == 0 &&
1172      bufsize > 0) {
1173    uint32_t pointer_size = 4;
1174    if (processInfo.kp_proc.p_flag & P_LP64)
1175      pointer_size = 8;
1176
1177    std::vector<struct binary_image_information> all_image_infos;
1178    GetAllLoadedBinariesViaDYLDSPI(all_image_infos);
1179    uint32_t platform = GetProcessPlatformViaDYLDSPI();
1180
1181    std::vector<struct binary_image_information> image_infos;
1182    const size_t macho_addresses_count = macho_addresses.size();
1183    const size_t all_image_infos_count = all_image_infos.size();
1184    for (size_t i = 0; i < macho_addresses_count; i++) {
1185      for (size_t j = 0; j < all_image_infos_count; j++) {
1186        if (all_image_infos[j].load_address == macho_addresses[i]) {
1187          image_infos.push_back(all_image_infos[j]);
1188        }
1189      }
1190    }
1191
1192    const size_t image_infos_count = image_infos.size();
1193    for (size_t i = 0; i < image_infos_count; i++) {
1194      GetMachOInformationFromMemory(platform,
1195                                    image_infos[i].load_address, pointer_size,
1196                                    image_infos[i].macho_info);
1197    }
1198    return FormatDynamicLibrariesIntoJSON(image_infos);
1199  }
1200  return reply_sp;
1201}
1202
1203// From dyld's internal podyld_process_info.h:
1204
1205JSONGenerator::ObjectSP MachProcess::GetSharedCacheInfo(nub_process_t pid) {
1206  JSONGenerator::DictionarySP reply_sp(new JSONGenerator::Dictionary());
1207
1208  kern_return_t kern_ret;
1209  if (m_dyld_process_info_create && m_dyld_process_info_get_cache) {
1210    dyld_process_info info =
1211        m_dyld_process_info_create(m_task.TaskPort(), 0, &kern_ret);
1212    if (info) {
1213      struct dyld_process_cache_info shared_cache_info;
1214      m_dyld_process_info_get_cache(info, &shared_cache_info);
1215
1216      reply_sp->AddIntegerItem("shared_cache_base_address",
1217                               shared_cache_info.cacheBaseAddress);
1218
1219      uuid_string_t uuidstr;
1220      uuid_unparse_upper(shared_cache_info.cacheUUID, uuidstr);
1221      reply_sp->AddStringItem("shared_cache_uuid", uuidstr);
1222
1223      reply_sp->AddBooleanItem("no_shared_cache", shared_cache_info.noCache);
1224      reply_sp->AddBooleanItem("shared_cache_private_cache",
1225                               shared_cache_info.privateCache);
1226
1227      m_dyld_process_info_release(info);
1228    }
1229  }
1230  return reply_sp;
1231}
1232
1233nub_thread_t MachProcess::GetCurrentThread() {
1234  return m_thread_list.CurrentThreadID();
1235}
1236
1237nub_thread_t MachProcess::GetCurrentThreadMachPort() {
1238  return m_thread_list.GetMachPortNumberByThreadID(
1239      m_thread_list.CurrentThreadID());
1240}
1241
1242nub_thread_t MachProcess::SetCurrentThread(nub_thread_t tid) {
1243  return m_thread_list.SetCurrentThread(tid);
1244}
1245
1246bool MachProcess::GetThreadStoppedReason(nub_thread_t tid,
1247                                         struct DNBThreadStopInfo *stop_info) {
1248  if (m_thread_list.GetThreadStoppedReason(tid, stop_info)) {
1249    if (m_did_exec)
1250      stop_info->reason = eStopTypeExec;
1251    return true;
1252  }
1253  return false;
1254}
1255
1256void MachProcess::DumpThreadStoppedReason(nub_thread_t tid) const {
1257  return m_thread_list.DumpThreadStoppedReason(tid);
1258}
1259
1260const char *MachProcess::GetThreadInfo(nub_thread_t tid) const {
1261  return m_thread_list.GetThreadInfo(tid);
1262}
1263
1264uint32_t MachProcess::GetCPUType() {
1265  if (m_cpu_type == 0 && m_pid != 0)
1266    m_cpu_type = MachProcess::GetCPUTypeForLocalProcess(m_pid);
1267  return m_cpu_type;
1268}
1269
1270const DNBRegisterSetInfo *
1271MachProcess::GetRegisterSetInfo(nub_thread_t tid,
1272                                nub_size_t *num_reg_sets) const {
1273  MachThreadSP thread_sp(m_thread_list.GetThreadByID(tid));
1274  if (thread_sp) {
1275    DNBArchProtocol *arch = thread_sp->GetArchProtocol();
1276    if (arch)
1277      return arch->GetRegisterSetInfo(num_reg_sets);
1278  }
1279  *num_reg_sets = 0;
1280  return NULL;
1281}
1282
1283bool MachProcess::GetRegisterValue(nub_thread_t tid, uint32_t set, uint32_t reg,
1284                                   DNBRegisterValue *value) const {
1285  return m_thread_list.GetRegisterValue(tid, set, reg, value);
1286}
1287
1288bool MachProcess::SetRegisterValue(nub_thread_t tid, uint32_t set, uint32_t reg,
1289                                   const DNBRegisterValue *value) const {
1290  return m_thread_list.SetRegisterValue(tid, set, reg, value);
1291}
1292
1293void MachProcess::SetState(nub_state_t new_state) {
1294  // If any other threads access this we will need a mutex for it
1295  uint32_t event_mask = 0;
1296
1297  // Scope for mutex locker
1298  {
1299    PTHREAD_MUTEX_LOCKER(locker, m_state_mutex);
1300    const nub_state_t old_state = m_state;
1301
1302    if (old_state == eStateExited) {
1303      DNBLogThreadedIf(LOG_PROCESS, "MachProcess::SetState(%s) ignoring new "
1304                                    "state since current state is exited",
1305                       DNBStateAsString(new_state));
1306    } else if (old_state == new_state) {
1307      DNBLogThreadedIf(
1308          LOG_PROCESS,
1309          "MachProcess::SetState(%s) ignoring redundant state change...",
1310          DNBStateAsString(new_state));
1311    } else {
1312      if (NUB_STATE_IS_STOPPED(new_state))
1313        event_mask = eEventProcessStoppedStateChanged;
1314      else
1315        event_mask = eEventProcessRunningStateChanged;
1316
1317      DNBLogThreadedIf(
1318          LOG_PROCESS, "MachProcess::SetState(%s) upating state (previous "
1319                       "state was %s), event_mask = 0x%8.8x",
1320          DNBStateAsString(new_state), DNBStateAsString(old_state), event_mask);
1321
1322      m_state = new_state;
1323      if (new_state == eStateStopped)
1324        m_stop_count++;
1325    }
1326  }
1327
1328  if (event_mask != 0) {
1329    m_events.SetEvents(event_mask);
1330    m_private_events.SetEvents(event_mask);
1331    if (event_mask == eEventProcessStoppedStateChanged)
1332      m_private_events.ResetEvents(eEventProcessRunningStateChanged);
1333    else
1334      m_private_events.ResetEvents(eEventProcessStoppedStateChanged);
1335
1336    // Wait for the event bit to reset if a reset ACK is requested
1337    m_events.WaitForResetAck(event_mask);
1338  }
1339}
1340
1341void MachProcess::Clear(bool detaching) {
1342  // Clear any cached thread list while the pid and task are still valid
1343
1344  m_task.Clear();
1345  // Now clear out all member variables
1346  m_pid = INVALID_NUB_PROCESS;
1347  if (!detaching)
1348    CloseChildFileDescriptors();
1349
1350  m_path.clear();
1351  m_args.clear();
1352  SetState(eStateUnloaded);
1353  m_flags = eMachProcessFlagsNone;
1354  m_stop_count = 0;
1355  m_thread_list.Clear();
1356  {
1357    PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
1358    m_exception_messages.clear();
1359  }
1360  m_activities.Clear();
1361  StopProfileThread();
1362}
1363
1364bool MachProcess::StartSTDIOThread() {
1365  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( )", __FUNCTION__);
1366  // Create the thread that watches for the child STDIO
1367  return ::pthread_create(&m_stdio_thread, NULL, MachProcess::STDIOThread,
1368                          this) == 0;
1369}
1370
1371void MachProcess::SetEnableAsyncProfiling(bool enable, uint64_t interval_usec,
1372                                          DNBProfileDataScanType scan_type) {
1373  m_profile_enabled = enable;
1374  m_profile_interval_usec = static_cast<useconds_t>(interval_usec);
1375  m_profile_scan_type = scan_type;
1376
1377  if (m_profile_enabled && (m_profile_thread == NULL)) {
1378    StartProfileThread();
1379  } else if (!m_profile_enabled && m_profile_thread) {
1380    StopProfileThread();
1381  }
1382}
1383
1384void MachProcess::StopProfileThread() {
1385  if (m_profile_thread == NULL)
1386    return;
1387  m_profile_events.SetEvents(eMachProcessProfileCancel);
1388  pthread_join(m_profile_thread, NULL);
1389  m_profile_thread = NULL;
1390  m_profile_events.ResetEvents(eMachProcessProfileCancel);
1391}
1392
1393bool MachProcess::StartProfileThread() {
1394  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( )", __FUNCTION__);
1395  // Create the thread that profiles the inferior and reports back if enabled
1396  return ::pthread_create(&m_profile_thread, NULL, MachProcess::ProfileThread,
1397                          this) == 0;
1398}
1399
1400nub_addr_t MachProcess::LookupSymbol(const char *name, const char *shlib) {
1401  if (m_name_to_addr_callback != NULL && name && name[0])
1402    return m_name_to_addr_callback(ProcessID(), name, shlib,
1403                                   m_name_to_addr_baton);
1404  return INVALID_NUB_ADDRESS;
1405}
1406
1407bool MachProcess::Resume(const DNBThreadResumeActions &thread_actions) {
1408  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Resume ()");
1409  nub_state_t state = GetState();
1410
1411  if (CanResume(state)) {
1412    m_thread_actions = thread_actions;
1413    PrivateResume();
1414    return true;
1415  } else if (state == eStateRunning) {
1416    DNBLog("Resume() - task 0x%x is already running, ignoring...",
1417           m_task.TaskPort());
1418    return true;
1419  }
1420  DNBLog("Resume() - task 0x%x has state %s, can't continue...",
1421         m_task.TaskPort(), DNBStateAsString(state));
1422  return false;
1423}
1424
1425bool MachProcess::Kill(const struct timespec *timeout_abstime) {
1426  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill ()");
1427  nub_state_t state = DoSIGSTOP(true, false, NULL);
1428  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Kill() DoSIGSTOP() state = %s",
1429                   DNBStateAsString(state));
1430  errno = 0;
1431  DNBLog("Sending ptrace PT_KILL to terminate inferior process.");
1432  ::ptrace(PT_KILL, m_pid, 0, 0);
1433  DNBError err;
1434  err.SetErrorToErrno();
1435  if (DNBLogCheckLogBit(LOG_PROCESS) || err.Fail()) {
1436    err.LogThreaded("MachProcess::Kill() DoSIGSTOP() ::ptrace "
1437            "(PT_KILL, pid=%u, 0, 0) => 0x%8.8x (%s)",
1438            m_pid, err.Status(), err.AsString());
1439  }
1440  m_thread_actions = DNBThreadResumeActions(eStateRunning, 0);
1441  PrivateResume();
1442
1443  // Try and reap the process without touching our m_events since
1444  // we want the code above this to still get the eStateExited event
1445  const uint32_t reap_timeout_usec =
1446      1000000; // Wait 1 second and try to reap the process
1447  const uint32_t reap_interval_usec = 10000; //
1448  uint32_t reap_time_elapsed;
1449  for (reap_time_elapsed = 0; reap_time_elapsed < reap_timeout_usec;
1450       reap_time_elapsed += reap_interval_usec) {
1451    if (GetState() == eStateExited)
1452      break;
1453    usleep(reap_interval_usec);
1454  }
1455  DNBLog("Waited %u ms for process to be reaped (state = %s)",
1456         reap_time_elapsed / 1000, DNBStateAsString(GetState()));
1457  return true;
1458}
1459
1460bool MachProcess::Interrupt() {
1461  nub_state_t state = GetState();
1462  if (IsRunning(state)) {
1463    if (m_sent_interrupt_signo == 0) {
1464      m_sent_interrupt_signo = SIGSTOP;
1465      if (Signal(m_sent_interrupt_signo)) {
1466        DNBLogThreadedIf(
1467            LOG_PROCESS,
1468            "MachProcess::Interrupt() - sent %i signal to interrupt process",
1469            m_sent_interrupt_signo);
1470        return true;
1471      } else {
1472        m_sent_interrupt_signo = 0;
1473        DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Interrupt() - failed to "
1474                                      "send %i signal to interrupt process",
1475                         m_sent_interrupt_signo);
1476      }
1477    } else {
1478      DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Interrupt() - previously "
1479                                    "sent an interrupt signal %i that hasn't "
1480                                    "been received yet, interrupt aborted",
1481                       m_sent_interrupt_signo);
1482    }
1483  } else {
1484    DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Interrupt() - process already "
1485                                  "stopped, no interrupt sent");
1486  }
1487  return false;
1488}
1489
1490bool MachProcess::Signal(int signal, const struct timespec *timeout_abstime) {
1491  DNBLogThreadedIf(LOG_PROCESS,
1492                   "MachProcess::Signal (signal = %d, timeout = %p)", signal,
1493                   static_cast<const void *>(timeout_abstime));
1494  nub_state_t state = GetState();
1495  if (::kill(ProcessID(), signal) == 0) {
1496    // If we were running and we have a timeout, wait for the signal to stop
1497    if (IsRunning(state) && timeout_abstime) {
1498      DNBLogThreadedIf(LOG_PROCESS,
1499                       "MachProcess::Signal (signal = %d, timeout "
1500                       "= %p) waiting for signal to stop "
1501                       "process...",
1502                       signal, static_cast<const void *>(timeout_abstime));
1503      m_private_events.WaitForSetEvents(eEventProcessStoppedStateChanged,
1504                                        timeout_abstime);
1505      state = GetState();
1506      DNBLogThreadedIf(
1507          LOG_PROCESS,
1508          "MachProcess::Signal (signal = %d, timeout = %p) state = %s", signal,
1509          static_cast<const void *>(timeout_abstime), DNBStateAsString(state));
1510      return !IsRunning(state);
1511    }
1512    DNBLogThreadedIf(
1513        LOG_PROCESS,
1514        "MachProcess::Signal (signal = %d, timeout = %p) not waiting...",
1515        signal, static_cast<const void *>(timeout_abstime));
1516    return true;
1517  }
1518  DNBError err(errno, DNBError::POSIX);
1519  err.LogThreadedIfError("kill (pid = %d, signo = %i)", ProcessID(), signal);
1520  return false;
1521}
1522
1523bool MachProcess::SendEvent(const char *event, DNBError &send_err) {
1524  DNBLogThreadedIf(LOG_PROCESS,
1525                   "MachProcess::SendEvent (event = %s) to pid: %d", event,
1526                   m_pid);
1527  if (m_pid == INVALID_NUB_PROCESS)
1528    return false;
1529// FIXME: Shouldn't we use the launch flavor we were started with?
1530#if defined(WITH_FBS) || defined(WITH_BKS)
1531  return BoardServiceSendEvent(event, send_err);
1532#endif
1533  return true;
1534}
1535
1536nub_state_t MachProcess::DoSIGSTOP(bool clear_bps_and_wps, bool allow_running,
1537                                   uint32_t *thread_idx_ptr) {
1538  nub_state_t state = GetState();
1539  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::DoSIGSTOP() state = %s",
1540                   DNBStateAsString(state));
1541
1542  if (!IsRunning(state)) {
1543    if (clear_bps_and_wps) {
1544      DisableAllBreakpoints(true);
1545      DisableAllWatchpoints(true);
1546      clear_bps_and_wps = false;
1547    }
1548
1549    // If we already have a thread stopped due to a SIGSTOP, we don't have
1550    // to do anything...
1551    uint32_t thread_idx =
1552        m_thread_list.GetThreadIndexForThreadStoppedWithSignal(SIGSTOP);
1553    if (thread_idx_ptr)
1554      *thread_idx_ptr = thread_idx;
1555    if (thread_idx != UINT32_MAX)
1556      return GetState();
1557
1558    // No threads were stopped with a SIGSTOP, we need to run and halt the
1559    // process with a signal
1560    DNBLogThreadedIf(LOG_PROCESS,
1561                     "MachProcess::DoSIGSTOP() state = %s -- resuming process",
1562                     DNBStateAsString(state));
1563    if (allow_running)
1564      m_thread_actions = DNBThreadResumeActions(eStateRunning, 0);
1565    else
1566      m_thread_actions = DNBThreadResumeActions(eStateSuspended, 0);
1567
1568    PrivateResume();
1569
1570    // Reset the event that says we were indeed running
1571    m_events.ResetEvents(eEventProcessRunningStateChanged);
1572    state = GetState();
1573  }
1574
1575  // We need to be stopped in order to be able to detach, so we need
1576  // to send ourselves a SIGSTOP
1577
1578  DNBLogThreadedIf(LOG_PROCESS,
1579                   "MachProcess::DoSIGSTOP() state = %s -- sending SIGSTOP",
1580                   DNBStateAsString(state));
1581  struct timespec sigstop_timeout;
1582  DNBTimer::OffsetTimeOfDay(&sigstop_timeout, 2, 0);
1583  Signal(SIGSTOP, &sigstop_timeout);
1584  if (clear_bps_and_wps) {
1585    DisableAllBreakpoints(true);
1586    DisableAllWatchpoints(true);
1587    // clear_bps_and_wps = false;
1588  }
1589  uint32_t thread_idx =
1590      m_thread_list.GetThreadIndexForThreadStoppedWithSignal(SIGSTOP);
1591  if (thread_idx_ptr)
1592    *thread_idx_ptr = thread_idx;
1593  return GetState();
1594}
1595
1596bool MachProcess::Detach() {
1597  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Detach()");
1598
1599  uint32_t thread_idx = UINT32_MAX;
1600  nub_state_t state = DoSIGSTOP(true, true, &thread_idx);
1601  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Detach() DoSIGSTOP() returned %s",
1602                   DNBStateAsString(state));
1603
1604  {
1605    m_thread_actions.Clear();
1606    m_activities.Clear();
1607    DNBThreadResumeAction thread_action;
1608    thread_action.tid = m_thread_list.ThreadIDAtIndex(thread_idx);
1609    thread_action.state = eStateRunning;
1610    thread_action.signal = -1;
1611    thread_action.addr = INVALID_NUB_ADDRESS;
1612
1613    m_thread_actions.Append(thread_action);
1614    m_thread_actions.SetDefaultThreadActionIfNeeded(eStateRunning, 0);
1615
1616    PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
1617
1618    ReplyToAllExceptions();
1619  }
1620
1621  m_task.ShutDownExcecptionThread();
1622
1623  // Detach from our process
1624  errno = 0;
1625  nub_process_t pid = m_pid;
1626  int ret = ::ptrace(PT_DETACH, pid, (caddr_t)1, 0);
1627  DNBError err(errno, DNBError::POSIX);
1628  if (DNBLogCheckLogBit(LOG_PROCESS) || err.Fail() || (ret != 0))
1629    err.LogThreaded("::ptrace (PT_DETACH, %u, (caddr_t)1, 0)", pid);
1630
1631  // Resume our task
1632  m_task.Resume();
1633
1634  // NULL our task out as we have already restored all exception ports
1635  m_task.Clear();
1636
1637  // Clear out any notion of the process we once were
1638  const bool detaching = true;
1639  Clear(detaching);
1640
1641  SetState(eStateDetached);
1642
1643  return true;
1644}
1645
1646//----------------------------------------------------------------------
1647// ReadMemory from the MachProcess level will always remove any software
1648// breakpoints from the memory buffer before returning. If you wish to
1649// read memory and see those traps, read from the MachTask
1650// (m_task.ReadMemory()) as that version will give you what is actually
1651// in inferior memory.
1652//----------------------------------------------------------------------
1653nub_size_t MachProcess::ReadMemory(nub_addr_t addr, nub_size_t size,
1654                                   void *buf) {
1655  // We need to remove any current software traps (enabled software
1656  // breakpoints) that we may have placed in our tasks memory.
1657
1658  // First just read the memory as is
1659  nub_size_t bytes_read = m_task.ReadMemory(addr, size, buf);
1660
1661  // Then place any opcodes that fall into this range back into the buffer
1662  // before we return this to callers.
1663  if (bytes_read > 0)
1664    m_breakpoints.RemoveTrapsFromBuffer(addr, bytes_read, buf);
1665  return bytes_read;
1666}
1667
1668//----------------------------------------------------------------------
1669// WriteMemory from the MachProcess level will always write memory around
1670// any software breakpoints. Any software breakpoints will have their
1671// opcodes modified if they are enabled. Any memory that doesn't overlap
1672// with software breakpoints will be written to. If you wish to write to
1673// inferior memory without this interference, then write to the MachTask
1674// (m_task.WriteMemory()) as that version will always modify inferior
1675// memory.
1676//----------------------------------------------------------------------
1677nub_size_t MachProcess::WriteMemory(nub_addr_t addr, nub_size_t size,
1678                                    const void *buf) {
1679  // We need to write any data that would go where any current software traps
1680  // (enabled software breakpoints) any software traps (breakpoints) that we
1681  // may have placed in our tasks memory.
1682
1683  std::vector<DNBBreakpoint *> bps;
1684
1685  const size_t num_bps =
1686      m_breakpoints.FindBreakpointsThatOverlapRange(addr, size, bps);
1687  if (num_bps == 0)
1688    return m_task.WriteMemory(addr, size, buf);
1689
1690  nub_size_t bytes_written = 0;
1691  nub_addr_t intersect_addr;
1692  nub_size_t intersect_size;
1693  nub_size_t opcode_offset;
1694  const uint8_t *ubuf = (const uint8_t *)buf;
1695
1696  for (size_t i = 0; i < num_bps; ++i) {
1697    DNBBreakpoint *bp = bps[i];
1698
1699    const bool intersects = bp->IntersectsRange(
1700        addr, size, &intersect_addr, &intersect_size, &opcode_offset);
1701    UNUSED_IF_ASSERT_DISABLED(intersects);
1702    assert(intersects);
1703    assert(addr <= intersect_addr && intersect_addr < addr + size);
1704    assert(addr < intersect_addr + intersect_size &&
1705           intersect_addr + intersect_size <= addr + size);
1706    assert(opcode_offset + intersect_size <= bp->ByteSize());
1707
1708    // Check for bytes before this breakpoint
1709    const nub_addr_t curr_addr = addr + bytes_written;
1710    if (intersect_addr > curr_addr) {
1711      // There are some bytes before this breakpoint that we need to
1712      // just write to memory
1713      nub_size_t curr_size = intersect_addr - curr_addr;
1714      nub_size_t curr_bytes_written =
1715          m_task.WriteMemory(curr_addr, curr_size, ubuf + bytes_written);
1716      bytes_written += curr_bytes_written;
1717      if (curr_bytes_written != curr_size) {
1718        // We weren't able to write all of the requested bytes, we
1719        // are done looping and will return the number of bytes that
1720        // we have written so far.
1721        break;
1722      }
1723    }
1724
1725    // Now write any bytes that would cover up any software breakpoints
1726    // directly into the breakpoint opcode buffer
1727    ::memcpy(bp->SavedOpcodeBytes() + opcode_offset, ubuf + bytes_written,
1728             intersect_size);
1729    bytes_written += intersect_size;
1730  }
1731
1732  // Write any remaining bytes after the last breakpoint if we have any left
1733  if (bytes_written < size)
1734    bytes_written += m_task.WriteMemory(
1735        addr + bytes_written, size - bytes_written, ubuf + bytes_written);
1736
1737  return bytes_written;
1738}
1739
1740void MachProcess::ReplyToAllExceptions() {
1741  PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
1742  if (!m_exception_messages.empty()) {
1743    MachException::Message::iterator pos;
1744    MachException::Message::iterator begin = m_exception_messages.begin();
1745    MachException::Message::iterator end = m_exception_messages.end();
1746    for (pos = begin; pos != end; ++pos) {
1747      DNBLogThreadedIf(LOG_EXCEPTIONS, "Replying to exception %u...",
1748                       (uint32_t)std::distance(begin, pos));
1749      int thread_reply_signal = 0;
1750
1751      nub_thread_t tid =
1752          m_thread_list.GetThreadIDByMachPortNumber(pos->state.thread_port);
1753      const DNBThreadResumeAction *action = NULL;
1754      if (tid != INVALID_NUB_THREAD) {
1755        action = m_thread_actions.GetActionForThread(tid, false);
1756      }
1757
1758      if (action) {
1759        thread_reply_signal = action->signal;
1760        if (thread_reply_signal)
1761          m_thread_actions.SetSignalHandledForThread(tid);
1762      }
1763
1764      DNBError err(pos->Reply(this, thread_reply_signal));
1765      if (DNBLogCheckLogBit(LOG_EXCEPTIONS))
1766        err.LogThreadedIfError("Error replying to exception");
1767    }
1768
1769    // Erase all exception message as we should have used and replied
1770    // to them all already.
1771    m_exception_messages.clear();
1772  }
1773}
1774void MachProcess::PrivateResume() {
1775  PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
1776
1777  m_auto_resume_signo = m_sent_interrupt_signo;
1778  if (m_auto_resume_signo)
1779    DNBLogThreadedIf(LOG_PROCESS, "MachProcess::PrivateResume() - task 0x%x "
1780                                  "resuming (with unhandled interrupt signal "
1781                                  "%i)...",
1782                     m_task.TaskPort(), m_auto_resume_signo);
1783  else
1784    DNBLogThreadedIf(LOG_PROCESS,
1785                     "MachProcess::PrivateResume() - task 0x%x resuming...",
1786                     m_task.TaskPort());
1787
1788  ReplyToAllExceptions();
1789  //    bool stepOverBreakInstruction = step;
1790
1791  // Let the thread prepare to resume and see if any threads want us to
1792  // step over a breakpoint instruction (ProcessWillResume will modify
1793  // the value of stepOverBreakInstruction).
1794  m_thread_list.ProcessWillResume(this, m_thread_actions);
1795
1796  // Set our state accordingly
1797  if (m_thread_actions.NumActionsWithState(eStateStepping))
1798    SetState(eStateStepping);
1799  else
1800    SetState(eStateRunning);
1801
1802  // Now resume our task.
1803  m_task.Resume();
1804}
1805
1806DNBBreakpoint *MachProcess::CreateBreakpoint(nub_addr_t addr, nub_size_t length,
1807                                             bool hardware) {
1808  DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::CreateBreakpoint ( addr = "
1809                                    "0x%8.8llx, length = %llu, hardware = %i)",
1810                   (uint64_t)addr, (uint64_t)length, hardware);
1811
1812  DNBBreakpoint *bp = m_breakpoints.FindByAddress(addr);
1813  if (bp)
1814    bp->Retain();
1815  else
1816    bp = m_breakpoints.Add(addr, length, hardware);
1817
1818  if (EnableBreakpoint(addr)) {
1819    DNBLogThreadedIf(LOG_BREAKPOINTS,
1820                     "MachProcess::CreateBreakpoint ( addr = "
1821                     "0x%8.8llx, length = %llu) => %p",
1822                     (uint64_t)addr, (uint64_t)length, static_cast<void *>(bp));
1823    return bp;
1824  } else if (bp->Release() == 0) {
1825    m_breakpoints.Remove(addr);
1826  }
1827  // We failed to enable the breakpoint
1828  return NULL;
1829}
1830
1831DNBBreakpoint *MachProcess::CreateWatchpoint(nub_addr_t addr, nub_size_t length,
1832                                             uint32_t watch_flags,
1833                                             bool hardware) {
1834  DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = "
1835                                    "0x%8.8llx, length = %llu, flags = "
1836                                    "0x%8.8x, hardware = %i)",
1837                   (uint64_t)addr, (uint64_t)length, watch_flags, hardware);
1838
1839  DNBBreakpoint *wp = m_watchpoints.FindByAddress(addr);
1840  // since the Z packets only send an address, we can only have one watchpoint
1841  // at
1842  // an address. If there is already one, we must refuse to create another
1843  // watchpoint
1844  if (wp)
1845    return NULL;
1846
1847  wp = m_watchpoints.Add(addr, length, hardware);
1848  wp->SetIsWatchpoint(watch_flags);
1849
1850  if (EnableWatchpoint(addr)) {
1851    DNBLogThreadedIf(LOG_WATCHPOINTS,
1852                     "MachProcess::CreateWatchpoint ( addr = "
1853                     "0x%8.8llx, length = %llu) => %p",
1854                     (uint64_t)addr, (uint64_t)length, static_cast<void *>(wp));
1855    return wp;
1856  } else {
1857    DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = "
1858                                      "0x%8.8llx, length = %llu) => FAILED",
1859                     (uint64_t)addr, (uint64_t)length);
1860    m_watchpoints.Remove(addr);
1861  }
1862  // We failed to enable the watchpoint
1863  return NULL;
1864}
1865
1866void MachProcess::DisableAllBreakpoints(bool remove) {
1867  DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::%s (remove = %d )",
1868                   __FUNCTION__, remove);
1869
1870  m_breakpoints.DisableAllBreakpoints(this);
1871
1872  if (remove)
1873    m_breakpoints.RemoveDisabled();
1874}
1875
1876void MachProcess::DisableAllWatchpoints(bool remove) {
1877  DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::%s (remove = %d )",
1878                   __FUNCTION__, remove);
1879
1880  m_watchpoints.DisableAllWatchpoints(this);
1881
1882  if (remove)
1883    m_watchpoints.RemoveDisabled();
1884}
1885
1886bool MachProcess::DisableBreakpoint(nub_addr_t addr, bool remove) {
1887  DNBBreakpoint *bp = m_breakpoints.FindByAddress(addr);
1888  if (bp) {
1889    // After "exec" we might end up with a bunch of breakpoints that were
1890    // disabled
1891    // manually, just ignore them
1892    if (!bp->IsEnabled()) {
1893      // Breakpoint might have been disabled by an exec
1894      if (remove && bp->Release() == 0) {
1895        m_thread_list.NotifyBreakpointChanged(bp);
1896        m_breakpoints.Remove(addr);
1897      }
1898      return true;
1899    }
1900
1901    // We have multiple references to this breakpoint, decrement the ref count
1902    // and if it isn't zero, then return true;
1903    if (remove && bp->Release() > 0)
1904      return true;
1905
1906    DNBLogThreadedIf(
1907        LOG_BREAKPOINTS | LOG_VERBOSE,
1908        "MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d )",
1909        (uint64_t)addr, remove);
1910
1911    if (bp->IsHardware()) {
1912      bool hw_disable_result = m_thread_list.DisableHardwareBreakpoint(bp);
1913
1914      if (hw_disable_result) {
1915        bp->SetEnabled(false);
1916        // Let the thread list know that a breakpoint has been modified
1917        if (remove) {
1918          m_thread_list.NotifyBreakpointChanged(bp);
1919          m_breakpoints.Remove(addr);
1920        }
1921        DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::DisableBreakpoint ( "
1922                                          "addr = 0x%8.8llx, remove = %d ) "
1923                                          "(hardware) => success",
1924                         (uint64_t)addr, remove);
1925        return true;
1926      }
1927
1928      return false;
1929    }
1930
1931    const nub_size_t break_op_size = bp->ByteSize();
1932    assert(break_op_size > 0);
1933    const uint8_t *const break_op =
1934        DNBArchProtocol::GetBreakpointOpcode(bp->ByteSize());
1935    if (break_op_size > 0) {
1936      // Clear a software breakpoint instruction
1937      uint8_t curr_break_op[break_op_size];
1938      bool break_op_found = false;
1939
1940      // Read the breakpoint opcode
1941      if (m_task.ReadMemory(addr, break_op_size, curr_break_op) ==
1942          break_op_size) {
1943        bool verify = false;
1944        if (bp->IsEnabled()) {
1945          // Make sure a breakpoint opcode exists at this address
1946          if (memcmp(curr_break_op, break_op, break_op_size) == 0) {
1947            break_op_found = true;
1948            // We found a valid breakpoint opcode at this address, now restore
1949            // the saved opcode.
1950            if (m_task.WriteMemory(addr, break_op_size,
1951                                   bp->SavedOpcodeBytes()) == break_op_size) {
1952              verify = true;
1953            } else {
1954              DNBLogError("MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, "
1955                          "remove = %d ) memory write failed when restoring "
1956                          "original opcode",
1957                          (uint64_t)addr, remove);
1958            }
1959          } else {
1960            DNBLogWarning("MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, "
1961                          "remove = %d ) expected a breakpoint opcode but "
1962                          "didn't find one.",
1963                          (uint64_t)addr, remove);
1964            // Set verify to true and so we can check if the original opcode has
1965            // already been restored
1966            verify = true;
1967          }
1968        } else {
1969          DNBLogThreadedIf(LOG_BREAKPOINTS | LOG_VERBOSE,
1970                           "MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, "
1971                           "remove = %d ) is not enabled",
1972                           (uint64_t)addr, remove);
1973          // Set verify to true and so we can check if the original opcode is
1974          // there
1975          verify = true;
1976        }
1977
1978        if (verify) {
1979          uint8_t verify_opcode[break_op_size];
1980          // Verify that our original opcode made it back to the inferior
1981          if (m_task.ReadMemory(addr, break_op_size, verify_opcode) ==
1982              break_op_size) {
1983            // compare the memory we just read with the original opcode
1984            if (memcmp(bp->SavedOpcodeBytes(), verify_opcode, break_op_size) ==
1985                0) {
1986              // SUCCESS
1987              bp->SetEnabled(false);
1988              // Let the thread list know that a breakpoint has been modified
1989              if (remove && bp->Release() == 0) {
1990                m_thread_list.NotifyBreakpointChanged(bp);
1991                m_breakpoints.Remove(addr);
1992              }
1993              DNBLogThreadedIf(LOG_BREAKPOINTS,
1994                               "MachProcess::DisableBreakpoint ( addr = "
1995                               "0x%8.8llx, remove = %d ) => success",
1996                               (uint64_t)addr, remove);
1997              return true;
1998            } else {
1999              if (break_op_found)
2000                DNBLogError("MachProcess::DisableBreakpoint ( addr = "
2001                            "0x%8.8llx, remove = %d ) : failed to restore "
2002                            "original opcode",
2003                            (uint64_t)addr, remove);
2004              else
2005                DNBLogError("MachProcess::DisableBreakpoint ( addr = "
2006                            "0x%8.8llx, remove = %d ) : opcode changed",
2007                            (uint64_t)addr, remove);
2008            }
2009          } else {
2010            DNBLogWarning("MachProcess::DisableBreakpoint: unable to disable "
2011                          "breakpoint 0x%8.8llx",
2012                          (uint64_t)addr);
2013          }
2014        }
2015      } else {
2016        DNBLogWarning("MachProcess::DisableBreakpoint: unable to read memory "
2017                      "at 0x%8.8llx",
2018                      (uint64_t)addr);
2019      }
2020    }
2021  } else {
2022    DNBLogError("MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = "
2023                "%d ) invalid breakpoint address",
2024                (uint64_t)addr, remove);
2025  }
2026  return false;
2027}
2028
2029bool MachProcess::DisableWatchpoint(nub_addr_t addr, bool remove) {
2030  DNBLogThreadedIf(LOG_WATCHPOINTS,
2031                   "MachProcess::%s(addr = 0x%8.8llx, remove = %d)",
2032                   __FUNCTION__, (uint64_t)addr, remove);
2033  DNBBreakpoint *wp = m_watchpoints.FindByAddress(addr);
2034  if (wp) {
2035    // If we have multiple references to a watchpoint, removing the watchpoint
2036    // shouldn't clear it
2037    if (remove && wp->Release() > 0)
2038      return true;
2039
2040    nub_addr_t addr = wp->Address();
2041    DNBLogThreadedIf(
2042        LOG_WATCHPOINTS,
2043        "MachProcess::DisableWatchpoint ( addr = 0x%8.8llx, remove = %d )",
2044        (uint64_t)addr, remove);
2045
2046    if (wp->IsHardware()) {
2047      bool hw_disable_result = m_thread_list.DisableHardwareWatchpoint(wp);
2048
2049      if (hw_disable_result) {
2050        wp->SetEnabled(false);
2051        if (remove)
2052          m_watchpoints.Remove(addr);
2053        DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::Disablewatchpoint ( "
2054                                          "addr = 0x%8.8llx, remove = %d ) "
2055                                          "(hardware) => success",
2056                         (uint64_t)addr, remove);
2057        return true;
2058      }
2059    }
2060
2061    // TODO: clear software watchpoints if we implement them
2062  } else {
2063    DNBLogError("MachProcess::DisableWatchpoint ( addr = 0x%8.8llx, remove = "
2064                "%d ) invalid watchpoint ID",
2065                (uint64_t)addr, remove);
2066  }
2067  return false;
2068}
2069
2070uint32_t MachProcess::GetNumSupportedHardwareWatchpoints() const {
2071  return m_thread_list.NumSupportedHardwareWatchpoints();
2072}
2073
2074bool MachProcess::EnableBreakpoint(nub_addr_t addr) {
2075  DNBLogThreadedIf(LOG_BREAKPOINTS,
2076                   "MachProcess::EnableBreakpoint ( addr = 0x%8.8llx )",
2077                   (uint64_t)addr);
2078  DNBBreakpoint *bp = m_breakpoints.FindByAddress(addr);
2079  if (bp) {
2080    if (bp->IsEnabled()) {
2081      DNBLogWarning("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): "
2082                    "breakpoint already enabled.",
2083                    (uint64_t)addr);
2084      return true;
2085    } else {
2086      if (bp->HardwarePreferred()) {
2087        bp->SetHardwareIndex(m_thread_list.EnableHardwareBreakpoint(bp));
2088        if (bp->IsHardware()) {
2089          bp->SetEnabled(true);
2090          return true;
2091        }
2092      }
2093
2094      const nub_size_t break_op_size = bp->ByteSize();
2095      assert(break_op_size != 0);
2096      const uint8_t *const break_op =
2097          DNBArchProtocol::GetBreakpointOpcode(break_op_size);
2098      if (break_op_size > 0) {
2099        // Save the original opcode by reading it
2100        if (m_task.ReadMemory(addr, break_op_size, bp->SavedOpcodeBytes()) ==
2101            break_op_size) {
2102          // Write a software breakpoint in place of the original opcode
2103          if (m_task.WriteMemory(addr, break_op_size, break_op) ==
2104              break_op_size) {
2105            uint8_t verify_break_op[4];
2106            if (m_task.ReadMemory(addr, break_op_size, verify_break_op) ==
2107                break_op_size) {
2108              if (memcmp(break_op, verify_break_op, break_op_size) == 0) {
2109                bp->SetEnabled(true);
2110                // Let the thread list know that a breakpoint has been modified
2111                m_thread_list.NotifyBreakpointChanged(bp);
2112                DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::"
2113                                                  "EnableBreakpoint ( addr = "
2114                                                  "0x%8.8llx ) : SUCCESS.",
2115                                 (uint64_t)addr);
2116                return true;
2117              } else {
2118                DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx "
2119                            "): breakpoint opcode verification failed.",
2120                            (uint64_t)addr);
2121              }
2122            } else {
2123              DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): "
2124                          "unable to read memory to verify breakpoint opcode.",
2125                          (uint64_t)addr);
2126            }
2127          } else {
2128            DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): "
2129                        "unable to write breakpoint opcode to memory.",
2130                        (uint64_t)addr);
2131          }
2132        } else {
2133          DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): "
2134                      "unable to read memory at breakpoint address.",
2135                      (uint64_t)addr);
2136        }
2137      } else {
2138        DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ) no "
2139                    "software breakpoint opcode for current architecture.",
2140                    (uint64_t)addr);
2141      }
2142    }
2143  }
2144  return false;
2145}
2146
2147bool MachProcess::EnableWatchpoint(nub_addr_t addr) {
2148  DNBLogThreadedIf(LOG_WATCHPOINTS,
2149                   "MachProcess::EnableWatchpoint(addr = 0x%8.8llx)",
2150                   (uint64_t)addr);
2151  DNBBreakpoint *wp = m_watchpoints.FindByAddress(addr);
2152  if (wp) {
2153    nub_addr_t addr = wp->Address();
2154    if (wp->IsEnabled()) {
2155      DNBLogWarning("MachProcess::EnableWatchpoint(addr = 0x%8.8llx): "
2156                    "watchpoint already enabled.",
2157                    (uint64_t)addr);
2158      return true;
2159    } else {
2160      // Currently only try and set hardware watchpoints.
2161      wp->SetHardwareIndex(m_thread_list.EnableHardwareWatchpoint(wp));
2162      if (wp->IsHardware()) {
2163        wp->SetEnabled(true);
2164        return true;
2165      }
2166      // TODO: Add software watchpoints by doing page protection tricks.
2167    }
2168  }
2169  return false;
2170}
2171
2172// Called by the exception thread when an exception has been received from
2173// our process. The exception message is completely filled and the exception
2174// data has already been copied.
2175void MachProcess::ExceptionMessageReceived(
2176    const MachException::Message &exceptionMessage) {
2177  PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
2178
2179  if (m_exception_messages.empty())
2180    m_task.Suspend();
2181
2182  DNBLogThreadedIf(LOG_EXCEPTIONS, "MachProcess::ExceptionMessageReceived ( )");
2183
2184  // Use a locker to automatically unlock our mutex in case of exceptions
2185  // Add the exception to our internal exception stack
2186  m_exception_messages.push_back(exceptionMessage);
2187}
2188
2189task_t MachProcess::ExceptionMessageBundleComplete() {
2190  // We have a complete bundle of exceptions for our child process.
2191  PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
2192  DNBLogThreadedIf(LOG_EXCEPTIONS, "%s: %llu exception messages.",
2193                   __PRETTY_FUNCTION__, (uint64_t)m_exception_messages.size());
2194  bool auto_resume = false;
2195  if (!m_exception_messages.empty()) {
2196    m_did_exec = false;
2197    // First check for any SIGTRAP and make sure we didn't exec
2198    const task_t task = m_task.TaskPort();
2199    size_t i;
2200    if (m_pid != 0) {
2201      bool received_interrupt = false;
2202      uint32_t num_task_exceptions = 0;
2203      for (i = 0; i < m_exception_messages.size(); ++i) {
2204        if (m_exception_messages[i].state.task_port == task) {
2205          ++num_task_exceptions;
2206          const int signo = m_exception_messages[i].state.SoftSignal();
2207          if (signo == SIGTRAP) {
2208            // SIGTRAP could mean that we exec'ed. We need to check the
2209            // dyld all_image_infos.infoArray to see if it is NULL and if
2210            // so, say that we exec'ed.
2211            const nub_addr_t aii_addr = GetDYLDAllImageInfosAddress();
2212            if (aii_addr != INVALID_NUB_ADDRESS) {
2213              const nub_addr_t info_array_count_addr = aii_addr + 4;
2214              uint32_t info_array_count = 0;
2215              if (m_task.ReadMemory(info_array_count_addr, 4,
2216                                    &info_array_count) == 4) {
2217                if (info_array_count == 0) {
2218                  m_did_exec = true;
2219                  // Force the task port to update itself in case the task port
2220                  // changed after exec
2221                  DNBError err;
2222                  const task_t old_task = m_task.TaskPort();
2223                  const task_t new_task =
2224                      m_task.TaskPortForProcessID(err, true);
2225                  if (old_task != new_task)
2226                    DNBLogThreadedIf(
2227                        LOG_PROCESS,
2228                        "exec: task changed from 0x%4.4x to 0x%4.4x", old_task,
2229                        new_task);
2230                }
2231              } else {
2232                DNBLog("error: failed to read all_image_infos.infoArrayCount "
2233                       "from 0x%8.8llx",
2234                       (uint64_t)info_array_count_addr);
2235              }
2236            }
2237            break;
2238          } else if (m_sent_interrupt_signo != 0 &&
2239                     signo == m_sent_interrupt_signo) {
2240            received_interrupt = true;
2241          }
2242        }
2243      }
2244
2245      if (m_did_exec) {
2246        cpu_type_t process_cpu_type =
2247            MachProcess::GetCPUTypeForLocalProcess(m_pid);
2248        if (m_cpu_type != process_cpu_type) {
2249          DNBLog("arch changed from 0x%8.8x to 0x%8.8x", m_cpu_type,
2250                 process_cpu_type);
2251          m_cpu_type = process_cpu_type;
2252          DNBArchProtocol::SetArchitecture(process_cpu_type);
2253        }
2254        m_thread_list.Clear();
2255        m_activities.Clear();
2256        m_breakpoints.DisableAll();
2257      }
2258
2259      if (m_sent_interrupt_signo != 0) {
2260        if (received_interrupt) {
2261          DNBLogThreadedIf(LOG_PROCESS,
2262                           "MachProcess::ExceptionMessageBundleComplete(): "
2263                           "process successfully interrupted with signal %i",
2264                           m_sent_interrupt_signo);
2265
2266          // Mark that we received the interrupt signal
2267          m_sent_interrupt_signo = 0;
2268          // Not check if we had a case where:
2269          // 1 - We called MachProcess::Interrupt() but we stopped for another
2270          // reason
2271          // 2 - We called MachProcess::Resume() (but still haven't gotten the
2272          // interrupt signal)
2273          // 3 - We are now incorrectly stopped because we are handling the
2274          // interrupt signal we missed
2275          // 4 - We might need to resume if we stopped only with the interrupt
2276          // signal that we never handled
2277          if (m_auto_resume_signo != 0) {
2278            // Only auto_resume if we stopped with _only_ the interrupt signal
2279            if (num_task_exceptions == 1) {
2280              auto_resume = true;
2281              DNBLogThreadedIf(LOG_PROCESS, "MachProcess::"
2282                                            "ExceptionMessageBundleComplete(): "
2283                                            "auto resuming due to unhandled "
2284                                            "interrupt signal %i",
2285                               m_auto_resume_signo);
2286            }
2287            m_auto_resume_signo = 0;
2288          }
2289        } else {
2290          DNBLogThreadedIf(LOG_PROCESS, "MachProcess::"
2291                                        "ExceptionMessageBundleComplete(): "
2292                                        "didn't get signal %i after "
2293                                        "MachProcess::Interrupt()",
2294                           m_sent_interrupt_signo);
2295        }
2296      }
2297    }
2298
2299    // Let all threads recover from stopping and do any clean up based
2300    // on the previous thread state (if any).
2301    m_thread_list.ProcessDidStop(this);
2302    m_activities.Clear();
2303
2304    // Let each thread know of any exceptions
2305    for (i = 0; i < m_exception_messages.size(); ++i) {
2306      // Let the thread list figure use the MachProcess to forward all
2307      // exceptions
2308      // on down to each thread.
2309      if (m_exception_messages[i].state.task_port == task)
2310        m_thread_list.NotifyException(m_exception_messages[i].state);
2311      if (DNBLogCheckLogBit(LOG_EXCEPTIONS))
2312        m_exception_messages[i].Dump();
2313    }
2314
2315    if (DNBLogCheckLogBit(LOG_THREAD))
2316      m_thread_list.Dump();
2317
2318    bool step_more = false;
2319    if (m_thread_list.ShouldStop(step_more) && !auto_resume) {
2320      // Wait for the eEventProcessRunningStateChanged event to be reset
2321      // before changing state to stopped to avoid race condition with
2322      // very fast start/stops
2323      struct timespec timeout;
2324      // DNBTimer::OffsetTimeOfDay(&timeout, 0, 250 * 1000);   // Wait for 250
2325      // ms
2326      DNBTimer::OffsetTimeOfDay(&timeout, 1, 0); // Wait for 250 ms
2327      m_events.WaitForEventsToReset(eEventProcessRunningStateChanged, &timeout);
2328      SetState(eStateStopped);
2329    } else {
2330      // Resume without checking our current state.
2331      PrivateResume();
2332    }
2333  } else {
2334    DNBLogThreadedIf(
2335        LOG_EXCEPTIONS, "%s empty exception messages bundle (%llu exceptions).",
2336        __PRETTY_FUNCTION__, (uint64_t)m_exception_messages.size());
2337  }
2338  return m_task.TaskPort();
2339}
2340
2341nub_size_t
2342MachProcess::CopyImageInfos(struct DNBExecutableImageInfo **image_infos,
2343                            bool only_changed) {
2344  if (m_image_infos_callback != NULL)
2345    return m_image_infos_callback(ProcessID(), image_infos, only_changed,
2346                                  m_image_infos_baton);
2347  return 0;
2348}
2349
2350void MachProcess::SharedLibrariesUpdated() {
2351  uint32_t event_bits = eEventSharedLibsStateChange;
2352  // Set the shared library event bit to let clients know of shared library
2353  // changes
2354  m_events.SetEvents(event_bits);
2355  // Wait for the event bit to reset if a reset ACK is requested
2356  m_events.WaitForResetAck(event_bits);
2357}
2358
2359void MachProcess::SetExitInfo(const char *info) {
2360  if (info && info[0]) {
2361    DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s(\"%s\")", __FUNCTION__,
2362                     info);
2363    m_exit_info.assign(info);
2364  } else {
2365    DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s(NULL)", __FUNCTION__);
2366    m_exit_info.clear();
2367  }
2368}
2369
2370void MachProcess::AppendSTDOUT(char *s, size_t len) {
2371  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (<%llu> %s) ...", __FUNCTION__,
2372                   (uint64_t)len, s);
2373  PTHREAD_MUTEX_LOCKER(locker, m_stdio_mutex);
2374  m_stdout_data.append(s, len);
2375  m_events.SetEvents(eEventStdioAvailable);
2376
2377  // Wait for the event bit to reset if a reset ACK is requested
2378  m_events.WaitForResetAck(eEventStdioAvailable);
2379}
2380
2381size_t MachProcess::GetAvailableSTDOUT(char *buf, size_t buf_size) {
2382  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (&%p[%llu]) ...", __FUNCTION__,
2383                   static_cast<void *>(buf), (uint64_t)buf_size);
2384  PTHREAD_MUTEX_LOCKER(locker, m_stdio_mutex);
2385  size_t bytes_available = m_stdout_data.size();
2386  if (bytes_available > 0) {
2387    if (bytes_available > buf_size) {
2388      memcpy(buf, m_stdout_data.data(), buf_size);
2389      m_stdout_data.erase(0, buf_size);
2390      bytes_available = buf_size;
2391    } else {
2392      memcpy(buf, m_stdout_data.data(), bytes_available);
2393      m_stdout_data.clear();
2394    }
2395  }
2396  return bytes_available;
2397}
2398
2399nub_addr_t MachProcess::GetDYLDAllImageInfosAddress() {
2400  DNBError err;
2401  return m_task.GetDYLDAllImageInfosAddress(err);
2402}
2403
2404size_t MachProcess::GetAvailableSTDERR(char *buf, size_t buf_size) { return 0; }
2405
2406void *MachProcess::STDIOThread(void *arg) {
2407  MachProcess *proc = (MachProcess *)arg;
2408  DNBLogThreadedIf(LOG_PROCESS,
2409                   "MachProcess::%s ( arg = %p ) thread starting...",
2410                   __FUNCTION__, arg);
2411
2412#if defined(__APPLE__)
2413  pthread_setname_np("stdio monitoring thread");
2414#endif
2415
2416  // We start use a base and more options so we can control if we
2417  // are currently using a timeout on the mach_msg. We do this to get a
2418  // bunch of related exceptions on our exception port so we can process
2419  // then together. When we have multiple threads, we can get an exception
2420  // per thread and they will come in consecutively. The main thread loop
2421  // will start by calling mach_msg to without having the MACH_RCV_TIMEOUT
2422  // flag set in the options, so we will wait forever for an exception on
2423  // our exception port. After we get one exception, we then will use the
2424  // MACH_RCV_TIMEOUT option with a zero timeout to grab all other current
2425  // exceptions for our process. After we have received the last pending
2426  // exception, we will get a timeout which enables us to then notify
2427  // our main thread that we have an exception bundle available. We then wait
2428  // for the main thread to tell this exception thread to start trying to get
2429  // exceptions messages again and we start again with a mach_msg read with
2430  // infinite timeout.
2431  DNBError err;
2432  int stdout_fd = proc->GetStdoutFileDescriptor();
2433  int stderr_fd = proc->GetStderrFileDescriptor();
2434  if (stdout_fd == stderr_fd)
2435    stderr_fd = -1;
2436
2437  while (stdout_fd >= 0 || stderr_fd >= 0) {
2438    ::pthread_testcancel();
2439
2440    fd_set read_fds;
2441    FD_ZERO(&read_fds);
2442    if (stdout_fd >= 0)
2443      FD_SET(stdout_fd, &read_fds);
2444    if (stderr_fd >= 0)
2445      FD_SET(stderr_fd, &read_fds);
2446    int nfds = std::max<int>(stdout_fd, stderr_fd) + 1;
2447
2448    int num_set_fds = select(nfds, &read_fds, NULL, NULL, NULL);
2449    DNBLogThreadedIf(LOG_PROCESS,
2450                     "select (nfds, &read_fds, NULL, NULL, NULL) => %d",
2451                     num_set_fds);
2452
2453    if (num_set_fds < 0) {
2454      int select_errno = errno;
2455      if (DNBLogCheckLogBit(LOG_PROCESS)) {
2456        err.SetError(select_errno, DNBError::POSIX);
2457        err.LogThreadedIfError(
2458            "select (nfds, &read_fds, NULL, NULL, NULL) => %d", num_set_fds);
2459      }
2460
2461      switch (select_errno) {
2462      case EAGAIN: // The kernel was (perhaps temporarily) unable to allocate
2463                   // the requested number of file descriptors, or we have
2464                   // non-blocking IO
2465        break;
2466      case EBADF: // One of the descriptor sets specified an invalid descriptor.
2467        return NULL;
2468        break;
2469      case EINTR:  // A signal was delivered before the time limit expired and
2470                   // before any of the selected events occurred.
2471      case EINVAL: // The specified time limit is invalid. One of its components
2472                   // is negative or too large.
2473      default:     // Other unknown error
2474        break;
2475      }
2476    } else if (num_set_fds == 0) {
2477    } else {
2478      char s[1024];
2479      s[sizeof(s) - 1] = '\0'; // Ensure we have NULL termination
2480      ssize_t bytes_read = 0;
2481      if (stdout_fd >= 0 && FD_ISSET(stdout_fd, &read_fds)) {
2482        do {
2483          bytes_read = ::read(stdout_fd, s, sizeof(s) - 1);
2484          if (bytes_read < 0) {
2485            int read_errno = errno;
2486            DNBLogThreadedIf(LOG_PROCESS,
2487                             "read (stdout_fd, ) => %zd   errno: %d (%s)",
2488                             bytes_read, read_errno, strerror(read_errno));
2489          } else if (bytes_read == 0) {
2490            // EOF...
2491            DNBLogThreadedIf(
2492                LOG_PROCESS,
2493                "read (stdout_fd, ) => %zd  (reached EOF for child STDOUT)",
2494                bytes_read);
2495            stdout_fd = -1;
2496          } else if (bytes_read > 0) {
2497            proc->AppendSTDOUT(s, bytes_read);
2498          }
2499
2500        } while (bytes_read > 0);
2501      }
2502
2503      if (stderr_fd >= 0 && FD_ISSET(stderr_fd, &read_fds)) {
2504        do {
2505          bytes_read = ::read(stderr_fd, s, sizeof(s) - 1);
2506          if (bytes_read < 0) {
2507            int read_errno = errno;
2508            DNBLogThreadedIf(LOG_PROCESS,
2509                             "read (stderr_fd, ) => %zd   errno: %d (%s)",
2510                             bytes_read, read_errno, strerror(read_errno));
2511          } else if (bytes_read == 0) {
2512            // EOF...
2513            DNBLogThreadedIf(
2514                LOG_PROCESS,
2515                "read (stderr_fd, ) => %zd  (reached EOF for child STDERR)",
2516                bytes_read);
2517            stderr_fd = -1;
2518          } else if (bytes_read > 0) {
2519            proc->AppendSTDOUT(s, bytes_read);
2520          }
2521
2522        } while (bytes_read > 0);
2523      }
2524    }
2525  }
2526  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (%p): thread exiting...",
2527                   __FUNCTION__, arg);
2528  return NULL;
2529}
2530
2531void MachProcess::SignalAsyncProfileData(const char *info) {
2532  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (%s) ...", __FUNCTION__, info);
2533  PTHREAD_MUTEX_LOCKER(locker, m_profile_data_mutex);
2534  m_profile_data.push_back(info);
2535  m_events.SetEvents(eEventProfileDataAvailable);
2536
2537  // Wait for the event bit to reset if a reset ACK is requested
2538  m_events.WaitForResetAck(eEventProfileDataAvailable);
2539}
2540
2541size_t MachProcess::GetAsyncProfileData(char *buf, size_t buf_size) {
2542  DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (&%p[%llu]) ...", __FUNCTION__,
2543                   static_cast<void *>(buf), (uint64_t)buf_size);
2544  PTHREAD_MUTEX_LOCKER(locker, m_profile_data_mutex);
2545  if (m_profile_data.empty())
2546    return 0;
2547
2548  size_t bytes_available = m_profile_data.front().size();
2549  if (bytes_available > 0) {
2550    if (bytes_available > buf_size) {
2551      memcpy(buf, m_profile_data.front().data(), buf_size);
2552      m_profile_data.front().erase(0, buf_size);
2553      bytes_available = buf_size;
2554    } else {
2555      memcpy(buf, m_profile_data.front().data(), bytes_available);
2556      m_profile_data.erase(m_profile_data.begin());
2557    }
2558  }
2559  return bytes_available;
2560}
2561
2562void *MachProcess::ProfileThread(void *arg) {
2563  MachProcess *proc = (MachProcess *)arg;
2564  DNBLogThreadedIf(LOG_PROCESS,
2565                   "MachProcess::%s ( arg = %p ) thread starting...",
2566                   __FUNCTION__, arg);
2567
2568#if defined(__APPLE__)
2569  pthread_setname_np("performance profiling thread");
2570#endif
2571
2572  while (proc->IsProfilingEnabled()) {
2573    nub_state_t state = proc->GetState();
2574    if (state == eStateRunning) {
2575      std::string data =
2576          proc->Task().GetProfileData(proc->GetProfileScanType());
2577      if (!data.empty()) {
2578        proc->SignalAsyncProfileData(data.c_str());
2579      }
2580    } else if ((state == eStateUnloaded) || (state == eStateDetached) ||
2581               (state == eStateUnloaded)) {
2582      // Done. Get out of this thread.
2583      break;
2584    }
2585    timespec ts;
2586    {
2587      using namespace std::chrono;
2588      std::chrono::microseconds dur(proc->ProfileInterval());
2589      const auto dur_secs = duration_cast<seconds>(dur);
2590      const auto dur_usecs = dur % std::chrono::seconds(1);
2591      DNBTimer::OffsetTimeOfDay(&ts, dur_secs.count(),
2592                                dur_usecs.count());
2593    }
2594    uint32_t bits_set =
2595        proc->m_profile_events.WaitForSetEvents(eMachProcessProfileCancel, &ts);
2596    // If we got bits back, we were told to exit.  Do so.
2597    if (bits_set & eMachProcessProfileCancel)
2598      break;
2599  }
2600  return NULL;
2601}
2602
2603pid_t MachProcess::AttachForDebug(pid_t pid, char *err_str, size_t err_len) {
2604  // Clear out and clean up from any current state
2605  Clear();
2606  if (pid != 0) {
2607    DNBError err;
2608    // Make sure the process exists...
2609    if (::getpgid(pid) < 0) {
2610      err.SetErrorToErrno();
2611      const char *err_cstr = err.AsString();
2612      ::snprintf(err_str, err_len, "%s",
2613                 err_cstr ? err_cstr : "No such process");
2614      DNBLogError ("MachProcess::AttachForDebug pid %d does not exist", pid);
2615      return INVALID_NUB_PROCESS;
2616    }
2617
2618    SetState(eStateAttaching);
2619    m_pid = pid;
2620    if (!m_task.StartExceptionThread(err)) {
2621      const char *err_cstr = err.AsString();
2622      ::snprintf(err_str, err_len, "%s",
2623                 err_cstr ? err_cstr : "unable to start the exception thread");
2624      DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", pid);
2625      DNBLogError ("MachProcess::AttachForDebug failed to start exception thread: %s", err_str);
2626      m_pid = INVALID_NUB_PROCESS;
2627      return INVALID_NUB_PROCESS;
2628    }
2629
2630    errno = 0;
2631    if (::ptrace(PT_ATTACHEXC, pid, 0, 0)) {
2632      err.SetError(errno);
2633      DNBLogError ("MachProcess::AttachForDebug failed to ptrace(PT_ATTACHEXC): %s", err.AsString());
2634    } else {
2635      err.Clear();
2636    }
2637
2638    if (err.Success()) {
2639      m_flags |= eMachProcessFlagsAttached;
2640      // Sleep a bit to let the exception get received and set our process
2641      // status
2642      // to stopped.
2643      ::usleep(250000);
2644      DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", pid);
2645      return m_pid;
2646    } else {
2647      ::snprintf(err_str, err_len, "%s", err.AsString());
2648      DNBLogError ("MachProcess::AttachForDebug error: failed to attach to pid %d", pid);
2649
2650      struct kinfo_proc kinfo;
2651      int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
2652      size_t len = sizeof(struct kinfo_proc);
2653      if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &kinfo, &len, NULL, 0) == 0 && len > 0) {
2654        if (kinfo.kp_proc.p_flag & P_TRACED) {
2655          ::snprintf(err_str, err_len, "%s - process %d is already being debugged", err.AsString(), pid);
2656          DNBLogError ("MachProcess::AttachForDebug pid %d is already being debugged", pid);
2657        }
2658      }
2659    }
2660  }
2661  return INVALID_NUB_PROCESS;
2662}
2663
2664Genealogy::ThreadActivitySP
2665MachProcess::GetGenealogyInfoForThread(nub_thread_t tid, bool &timed_out) {
2666  return m_activities.GetGenealogyInfoForThread(m_pid, tid, m_thread_list,
2667                                                m_task.TaskPort(), timed_out);
2668}
2669
2670Genealogy::ProcessExecutableInfoSP
2671MachProcess::GetGenealogyImageInfo(size_t idx) {
2672  return m_activities.GetProcessExecutableInfosAtIndex(idx);
2673}
2674
2675bool MachProcess::GetOSVersionNumbers(uint64_t *major, uint64_t *minor,
2676                                      uint64_t *patch) {
2677#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) &&                  \
2678    (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101000)
2679  return false;
2680#else
2681  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2682
2683  NSOperatingSystemVersion vers =
2684      [[NSProcessInfo processInfo] operatingSystemVersion];
2685  if (major)
2686    *major = vers.majorVersion;
2687  if (minor)
2688    *minor = vers.minorVersion;
2689  if (patch)
2690    *patch = vers.patchVersion;
2691
2692  [pool drain];
2693
2694  return true;
2695#endif
2696}
2697
2698std::string MachProcess::GetMacCatalystVersionString() {
2699  @autoreleasepool {
2700    NSDictionary *version_info =
2701      [NSDictionary dictionaryWithContentsOfFile:
2702       @"/System/Library/CoreServices/SystemVersion.plist"];
2703    NSString *version_value = [version_info objectForKey: @"iOSSupportVersion"];
2704    if (const char *version_str = [version_value UTF8String])
2705      return version_str;
2706  }
2707  return {};
2708}
2709
2710// Do the process specific setup for attach.  If this returns NULL, then there's
2711// no
2712// platform specific stuff to be done to wait for the attach.  If you get
2713// non-null,
2714// pass that token to the CheckForProcess method, and then to
2715// CleanupAfterAttach.
2716
2717//  Call PrepareForAttach before attaching to a process that has not yet
2718//  launched
2719// This returns a token that can be passed to CheckForProcess, and to
2720// CleanupAfterAttach.
2721// You should call CleanupAfterAttach to free the token, and do whatever other
2722// cleanup seems good.
2723
2724const void *MachProcess::PrepareForAttach(const char *path,
2725                                          nub_launch_flavor_t launch_flavor,
2726                                          bool waitfor, DNBError &attach_err) {
2727#if defined(WITH_SPRINGBOARD) || defined(WITH_BKS) || defined(WITH_FBS)
2728  // Tell SpringBoard to halt the next launch of this application on startup.
2729
2730  if (!waitfor)
2731    return NULL;
2732
2733  const char *app_ext = strstr(path, ".app");
2734  const bool is_app =
2735      app_ext != NULL && (app_ext[4] == '\0' || app_ext[4] == '/');
2736  if (!is_app) {
2737    DNBLogThreadedIf(
2738        LOG_PROCESS,
2739        "MachProcess::PrepareForAttach(): path '%s' doesn't contain .app, "
2740        "we can't tell springboard to wait for launch...",
2741        path);
2742    return NULL;
2743  }
2744
2745#if defined(WITH_FBS)
2746  if (launch_flavor == eLaunchFlavorDefault)
2747    launch_flavor = eLaunchFlavorFBS;
2748  if (launch_flavor != eLaunchFlavorFBS)
2749    return NULL;
2750#elif defined(WITH_BKS)
2751  if (launch_flavor == eLaunchFlavorDefault)
2752    launch_flavor = eLaunchFlavorBKS;
2753  if (launch_flavor != eLaunchFlavorBKS)
2754    return NULL;
2755#elif defined(WITH_SPRINGBOARD)
2756  if (launch_flavor == eLaunchFlavorDefault)
2757    launch_flavor = eLaunchFlavorSpringBoard;
2758  if (launch_flavor != eLaunchFlavorSpringBoard)
2759    return NULL;
2760#endif
2761
2762  std::string app_bundle_path(path, app_ext + strlen(".app"));
2763
2764  CFStringRef bundleIDCFStr =
2765      CopyBundleIDForPath(app_bundle_path.c_str(), attach_err);
2766  std::string bundleIDStr;
2767  CFString::UTF8(bundleIDCFStr, bundleIDStr);
2768  DNBLogThreadedIf(LOG_PROCESS,
2769                   "CopyBundleIDForPath (%s, err_str) returned @\"%s\"",
2770                   app_bundle_path.c_str(), bundleIDStr.c_str());
2771
2772  if (bundleIDCFStr == NULL) {
2773    return NULL;
2774  }
2775
2776#if defined(WITH_FBS)
2777  if (launch_flavor == eLaunchFlavorFBS) {
2778    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2779
2780    NSString *stdio_path = nil;
2781    NSFileManager *file_manager = [NSFileManager defaultManager];
2782    const char *null_path = "/dev/null";
2783    stdio_path =
2784        [file_manager stringWithFileSystemRepresentation:null_path
2785                                                  length:strlen(null_path)];
2786
2787    NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
2788    NSMutableDictionary *options = [NSMutableDictionary dictionary];
2789
2790    DNBLogThreadedIf(LOG_PROCESS, "Calling BKSSystemService openApplication: "
2791                                  "@\"%s\",options include stdio path: \"%s\", "
2792                                  "BKSDebugOptionKeyDebugOnNextLaunch & "
2793                                  "BKSDebugOptionKeyWaitForDebugger )",
2794                     bundleIDStr.c_str(), null_path);
2795
2796    [debug_options setObject:stdio_path
2797                      forKey:FBSDebugOptionKeyStandardOutPath];
2798    [debug_options setObject:stdio_path
2799                      forKey:FBSDebugOptionKeyStandardErrorPath];
2800    [debug_options setObject:[NSNumber numberWithBool:YES]
2801                      forKey:FBSDebugOptionKeyWaitForDebugger];
2802    [debug_options setObject:[NSNumber numberWithBool:YES]
2803                      forKey:FBSDebugOptionKeyDebugOnNextLaunch];
2804
2805    [options setObject:debug_options
2806                forKey:FBSOpenApplicationOptionKeyDebuggingOptions];
2807
2808    FBSSystemService *system_service = [[FBSSystemService alloc] init];
2809
2810    mach_port_t client_port = [system_service createClientPort];
2811    __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
2812    __block FBSOpenApplicationErrorCode attach_error_code =
2813        FBSOpenApplicationErrorCodeNone;
2814
2815    NSString *bundleIDNSStr = (NSString *)bundleIDCFStr;
2816
2817    [system_service openApplication:bundleIDNSStr
2818                            options:options
2819                         clientPort:client_port
2820                         withResult:^(NSError *error) {
2821                           // The system service will cleanup the client port we
2822                           // created for us.
2823                           if (error)
2824                             attach_error_code =
2825                                 (FBSOpenApplicationErrorCode)[error code];
2826
2827                           [system_service release];
2828                           dispatch_semaphore_signal(semaphore);
2829                         }];
2830
2831    const uint32_t timeout_secs = 9;
2832
2833    dispatch_time_t timeout =
2834        dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC);
2835
2836    long success = dispatch_semaphore_wait(semaphore, timeout) == 0;
2837
2838    if (!success) {
2839      DNBLogError("timed out trying to launch %s.", bundleIDStr.c_str());
2840      attach_err.SetErrorString(
2841          "debugserver timed out waiting for openApplication to complete.");
2842      attach_err.SetError(OPEN_APPLICATION_TIMEOUT_ERROR, DNBError::Generic);
2843    } else if (attach_error_code != FBSOpenApplicationErrorCodeNone) {
2844      std::string empty_str;
2845      SetFBSError(attach_error_code, empty_str, attach_err);
2846      DNBLogError("unable to launch the application with CFBundleIdentifier "
2847                  "'%s' bks_error = %ld",
2848                  bundleIDStr.c_str(), (NSInteger)attach_error_code);
2849    }
2850    dispatch_release(semaphore);
2851    [pool drain];
2852  }
2853#endif
2854#if defined(WITH_BKS)
2855  if (launch_flavor == eLaunchFlavorBKS) {
2856    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2857
2858    NSString *stdio_path = nil;
2859    NSFileManager *file_manager = [NSFileManager defaultManager];
2860    const char *null_path = "/dev/null";
2861    stdio_path =
2862        [file_manager stringWithFileSystemRepresentation:null_path
2863                                                  length:strlen(null_path)];
2864
2865    NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
2866    NSMutableDictionary *options = [NSMutableDictionary dictionary];
2867
2868    DNBLogThreadedIf(LOG_PROCESS, "Calling BKSSystemService openApplication: "
2869                                  "@\"%s\",options include stdio path: \"%s\", "
2870                                  "BKSDebugOptionKeyDebugOnNextLaunch & "
2871                                  "BKSDebugOptionKeyWaitForDebugger )",
2872                     bundleIDStr.c_str(), null_path);
2873
2874    [debug_options setObject:stdio_path
2875                      forKey:BKSDebugOptionKeyStandardOutPath];
2876    [debug_options setObject:stdio_path
2877                      forKey:BKSDebugOptionKeyStandardErrorPath];
2878    [debug_options setObject:[NSNumber numberWithBool:YES]
2879                      forKey:BKSDebugOptionKeyWaitForDebugger];
2880    [debug_options setObject:[NSNumber numberWithBool:YES]
2881                      forKey:BKSDebugOptionKeyDebugOnNextLaunch];
2882
2883    [options setObject:debug_options
2884                forKey:BKSOpenApplicationOptionKeyDebuggingOptions];
2885
2886    BKSSystemService *system_service = [[BKSSystemService alloc] init];
2887
2888    mach_port_t client_port = [system_service createClientPort];
2889    __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
2890    __block BKSOpenApplicationErrorCode attach_error_code =
2891        BKSOpenApplicationErrorCodeNone;
2892
2893    NSString *bundleIDNSStr = (NSString *)bundleIDCFStr;
2894
2895    [system_service openApplication:bundleIDNSStr
2896                            options:options
2897                         clientPort:client_port
2898                         withResult:^(NSError *error) {
2899                           // The system service will cleanup the client port we
2900                           // created for us.
2901                           if (error)
2902                             attach_error_code =
2903                                 (BKSOpenApplicationErrorCode)[error code];
2904
2905                           [system_service release];
2906                           dispatch_semaphore_signal(semaphore);
2907                         }];
2908
2909    const uint32_t timeout_secs = 9;
2910
2911    dispatch_time_t timeout =
2912        dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC);
2913
2914    long success = dispatch_semaphore_wait(semaphore, timeout) == 0;
2915
2916    if (!success) {
2917      DNBLogError("timed out trying to launch %s.", bundleIDStr.c_str());
2918      attach_err.SetErrorString(
2919          "debugserver timed out waiting for openApplication to complete.");
2920      attach_err.SetError(OPEN_APPLICATION_TIMEOUT_ERROR, DNBError::Generic);
2921    } else if (attach_error_code != BKSOpenApplicationErrorCodeNone) {
2922      std::string empty_str;
2923      SetBKSError(attach_error_code, empty_str, attach_err);
2924      DNBLogError("unable to launch the application with CFBundleIdentifier "
2925                  "'%s' bks_error = %ld",
2926                  bundleIDStr.c_str(), attach_error_code);
2927    }
2928    dispatch_release(semaphore);
2929    [pool drain];
2930  }
2931#endif
2932
2933#if defined(WITH_SPRINGBOARD)
2934  if (launch_flavor == eLaunchFlavorSpringBoard) {
2935    SBSApplicationLaunchError sbs_error = 0;
2936
2937    const char *stdout_err = "/dev/null";
2938    CFString stdio_path;
2939    stdio_path.SetFileSystemRepresentation(stdout_err);
2940
2941    DNBLogThreadedIf(LOG_PROCESS, "SBSLaunchApplicationForDebugging ( @\"%s\" "
2942                                  ", NULL, NULL, NULL, @\"%s\", @\"%s\", "
2943                                  "SBSApplicationDebugOnNextLaunch | "
2944                                  "SBSApplicationLaunchWaitForDebugger )",
2945                     bundleIDStr.c_str(), stdout_err, stdout_err);
2946
2947    sbs_error = SBSLaunchApplicationForDebugging(
2948        bundleIDCFStr,
2949        (CFURLRef)NULL, // openURL
2950        NULL,           // launch_argv.get(),
2951        NULL,           // launch_envp.get(),  // CFDictionaryRef environment
2952        stdio_path.get(), stdio_path.get(),
2953        SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger);
2954
2955    if (sbs_error != SBSApplicationLaunchErrorSuccess) {
2956      attach_err.SetError(sbs_error, DNBError::SpringBoard);
2957      return NULL;
2958    }
2959  }
2960#endif // WITH_SPRINGBOARD
2961
2962  DNBLogThreadedIf(LOG_PROCESS, "Successfully set DebugOnNextLaunch.");
2963  return bundleIDCFStr;
2964#else // !(defined (WITH_SPRINGBOARD) || defined (WITH_BKS) || defined
2965      // (WITH_FBS))
2966  return NULL;
2967#endif
2968}
2969
2970// Pass in the token you got from PrepareForAttach.  If there is a process
2971// for that token, then the pid will be returned, otherwise INVALID_NUB_PROCESS
2972// will be returned.
2973
2974nub_process_t MachProcess::CheckForProcess(const void *attach_token,
2975                                           nub_launch_flavor_t launch_flavor) {
2976  if (attach_token == NULL)
2977    return INVALID_NUB_PROCESS;
2978
2979#if defined(WITH_FBS)
2980  if (launch_flavor == eLaunchFlavorFBS) {
2981    NSString *bundleIDNSStr = (NSString *)attach_token;
2982    FBSSystemService *systemService = [[FBSSystemService alloc] init];
2983    pid_t pid = [systemService pidForApplication:bundleIDNSStr];
2984    [systemService release];
2985    if (pid == 0)
2986      return INVALID_NUB_PROCESS;
2987    else
2988      return pid;
2989  }
2990#endif
2991
2992#if defined(WITH_BKS)
2993  if (launch_flavor == eLaunchFlavorBKS) {
2994    NSString *bundleIDNSStr = (NSString *)attach_token;
2995    BKSSystemService *systemService = [[BKSSystemService alloc] init];
2996    pid_t pid = [systemService pidForApplication:bundleIDNSStr];
2997    [systemService release];
2998    if (pid == 0)
2999      return INVALID_NUB_PROCESS;
3000    else
3001      return pid;
3002  }
3003#endif
3004
3005#if defined(WITH_SPRINGBOARD)
3006  if (launch_flavor == eLaunchFlavorSpringBoard) {
3007    CFStringRef bundleIDCFStr = (CFStringRef)attach_token;
3008    Boolean got_it;
3009    nub_process_t attach_pid;
3010    got_it = SBSProcessIDForDisplayIdentifier(bundleIDCFStr, &attach_pid);
3011    if (got_it)
3012      return attach_pid;
3013    else
3014      return INVALID_NUB_PROCESS;
3015  }
3016#endif
3017  return INVALID_NUB_PROCESS;
3018}
3019
3020// Call this to clean up after you have either attached or given up on the
3021// attach.
3022// Pass true for success if you have attached, false if you have not.
3023// The token will also be freed at this point, so you can't use it after calling
3024// this method.
3025
3026void MachProcess::CleanupAfterAttach(const void *attach_token,
3027                                     nub_launch_flavor_t launch_flavor,
3028                                     bool success, DNBError &err_str) {
3029  if (attach_token == NULL)
3030    return;
3031
3032#if defined(WITH_FBS)
3033  if (launch_flavor == eLaunchFlavorFBS) {
3034    if (!success) {
3035      FBSCleanupAfterAttach(attach_token, err_str);
3036    }
3037    CFRelease((CFStringRef)attach_token);
3038  }
3039#endif
3040
3041#if defined(WITH_BKS)
3042
3043  if (launch_flavor == eLaunchFlavorBKS) {
3044    if (!success) {
3045      BKSCleanupAfterAttach(attach_token, err_str);
3046    }
3047    CFRelease((CFStringRef)attach_token);
3048  }
3049#endif
3050
3051#if defined(WITH_SPRINGBOARD)
3052  // Tell SpringBoard to cancel the debug on next launch of this application
3053  // if we failed to attach
3054  if (launch_flavor == eMachProcessFlagsUsingSpringBoard) {
3055    if (!success) {
3056      SBSApplicationLaunchError sbs_error = 0;
3057      CFStringRef bundleIDCFStr = (CFStringRef)attach_token;
3058
3059      sbs_error = SBSLaunchApplicationForDebugging(
3060          bundleIDCFStr, (CFURLRef)NULL, NULL, NULL, NULL, NULL,
3061          SBSApplicationCancelDebugOnNextLaunch);
3062
3063      if (sbs_error != SBSApplicationLaunchErrorSuccess) {
3064        err_str.SetError(sbs_error, DNBError::SpringBoard);
3065        return;
3066      }
3067    }
3068
3069    CFRelease((CFStringRef)attach_token);
3070  }
3071#endif
3072}
3073
3074pid_t MachProcess::LaunchForDebug(
3075    const char *path, char const *argv[], char const *envp[],
3076    const char *working_directory, // NULL => don't change, non-NULL => set
3077                                   // working directory for inferior to this
3078    const char *stdin_path, const char *stdout_path, const char *stderr_path,
3079    bool no_stdio, nub_launch_flavor_t launch_flavor, int disable_aslr,
3080    const char *event_data, DNBError &launch_err) {
3081  // Clear out and clean up from any current state
3082  Clear();
3083
3084  DNBLogThreadedIf(LOG_PROCESS,
3085                   "%s( path = '%s', argv = %p, envp = %p, "
3086                   "launch_flavor = %u, disable_aslr = %d )",
3087                   __FUNCTION__, path, static_cast<const void *>(argv),
3088                   static_cast<const void *>(envp), launch_flavor,
3089                   disable_aslr);
3090
3091  // Fork a child process for debugging
3092  SetState(eStateLaunching);
3093
3094  switch (launch_flavor) {
3095  case eLaunchFlavorForkExec:
3096    m_pid = MachProcess::ForkChildForPTraceDebugging(path, argv, envp, this,
3097                                                     launch_err);
3098    break;
3099#ifdef WITH_FBS
3100  case eLaunchFlavorFBS: {
3101    const char *app_ext = strstr(path, ".app");
3102    if (app_ext && (app_ext[4] == '\0' || app_ext[4] == '/')) {
3103      std::string app_bundle_path(path, app_ext + strlen(".app"));
3104      m_flags |= (eMachProcessFlagsUsingFBS | eMachProcessFlagsBoardCalculated);
3105      if (BoardServiceLaunchForDebug(app_bundle_path.c_str(), argv, envp,
3106                                     no_stdio, disable_aslr, event_data,
3107                                     launch_err) != 0)
3108        return m_pid; // A successful SBLaunchForDebug() returns and assigns a
3109                      // non-zero m_pid.
3110      else
3111        break; // We tried a FBS launch, but didn't succeed lets get out
3112    }
3113  } break;
3114#endif
3115#ifdef WITH_BKS
3116  case eLaunchFlavorBKS: {
3117    const char *app_ext = strstr(path, ".app");
3118    if (app_ext && (app_ext[4] == '\0' || app_ext[4] == '/')) {
3119      std::string app_bundle_path(path, app_ext + strlen(".app"));
3120      m_flags |= (eMachProcessFlagsUsingBKS | eMachProcessFlagsBoardCalculated);
3121      if (BoardServiceLaunchForDebug(app_bundle_path.c_str(), argv, envp,
3122                                     no_stdio, disable_aslr, event_data,
3123                                     launch_err) != 0)
3124        return m_pid; // A successful SBLaunchForDebug() returns and assigns a
3125                      // non-zero m_pid.
3126      else
3127        break; // We tried a BKS launch, but didn't succeed lets get out
3128    }
3129  } break;
3130#endif
3131#ifdef WITH_SPRINGBOARD
3132
3133  case eLaunchFlavorSpringBoard: {
3134    //  .../whatever.app/whatever ?
3135    //  Or .../com.apple.whatever.app/whatever -- be careful of ".app" in
3136    //  "com.apple.whatever" here
3137    const char *app_ext = strstr(path, ".app/");
3138    if (app_ext == NULL) {
3139      // .../whatever.app ?
3140      int len = strlen(path);
3141      if (len > 5) {
3142        if (strcmp(path + len - 4, ".app") == 0) {
3143          app_ext = path + len - 4;
3144        }
3145      }
3146    }
3147    if (app_ext) {
3148      std::string app_bundle_path(path, app_ext + strlen(".app"));
3149      if (SBLaunchForDebug(app_bundle_path.c_str(), argv, envp, no_stdio,
3150                           disable_aslr, launch_err) != 0)
3151        return m_pid; // A successful SBLaunchForDebug() returns and assigns a
3152                      // non-zero m_pid.
3153      else
3154        break; // We tried a springboard launch, but didn't succeed lets get out
3155    }
3156  } break;
3157
3158#endif
3159
3160  case eLaunchFlavorPosixSpawn:
3161    m_pid = MachProcess::PosixSpawnChildForPTraceDebugging(
3162        path, DNBArchProtocol::GetArchitecture(), argv, envp, working_directory,
3163        stdin_path, stdout_path, stderr_path, no_stdio, this, disable_aslr,
3164        launch_err);
3165    break;
3166
3167  default:
3168    // Invalid  launch
3169    launch_err.SetError(NUB_GENERIC_ERROR, DNBError::Generic);
3170    return INVALID_NUB_PROCESS;
3171  }
3172
3173  if (m_pid == INVALID_NUB_PROCESS) {
3174    // If we don't have a valid process ID and no one has set the error,
3175    // then return a generic error
3176    if (launch_err.Success())
3177      launch_err.SetError(NUB_GENERIC_ERROR, DNBError::Generic);
3178  } else {
3179    m_path = path;
3180    size_t i;
3181    char const *arg;
3182    for (i = 0; (arg = argv[i]) != NULL; i++)
3183      m_args.push_back(arg);
3184
3185    m_task.StartExceptionThread(launch_err);
3186    if (launch_err.Fail()) {
3187      if (launch_err.AsString() == NULL)
3188        launch_err.SetErrorString("unable to start the exception thread");
3189      DNBLog("Could not get inferior's Mach exception port, sending ptrace "
3190             "PT_KILL and exiting.");
3191      ::ptrace(PT_KILL, m_pid, 0, 0);
3192      m_pid = INVALID_NUB_PROCESS;
3193      return INVALID_NUB_PROCESS;
3194    }
3195
3196    StartSTDIOThread();
3197
3198    if (launch_flavor == eLaunchFlavorPosixSpawn) {
3199
3200      SetState(eStateAttaching);
3201      errno = 0;
3202      int err = ::ptrace(PT_ATTACHEXC, m_pid, 0, 0);
3203      if (err == 0) {
3204        m_flags |= eMachProcessFlagsAttached;
3205        DNBLogThreadedIf(LOG_PROCESS, "successfully spawned pid %d", m_pid);
3206        launch_err.Clear();
3207      } else {
3208        SetState(eStateExited);
3209        DNBError ptrace_err(errno, DNBError::POSIX);
3210        DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to spawned pid "
3211                                      "%d (err = %i, errno = %i (%s))",
3212                         m_pid, err, ptrace_err.Status(),
3213                         ptrace_err.AsString());
3214        launch_err.SetError(NUB_GENERIC_ERROR, DNBError::Generic);
3215      }
3216    } else {
3217      launch_err.Clear();
3218    }
3219  }
3220  return m_pid;
3221}
3222
3223pid_t MachProcess::PosixSpawnChildForPTraceDebugging(
3224    const char *path, cpu_type_t cpu_type, char const *argv[],
3225    char const *envp[], const char *working_directory, const char *stdin_path,
3226    const char *stdout_path, const char *stderr_path, bool no_stdio,
3227    MachProcess *process, int disable_aslr, DNBError &err) {
3228  posix_spawnattr_t attr;
3229  short flags;
3230  DNBLogThreadedIf(LOG_PROCESS,
3231                   "%s ( path='%s', argv=%p, envp=%p, "
3232                   "working_dir=%s, stdin=%s, stdout=%s "
3233                   "stderr=%s, no-stdio=%i)",
3234                   __FUNCTION__, path, static_cast<const void *>(argv),
3235                   static_cast<const void *>(envp), working_directory,
3236                   stdin_path, stdout_path, stderr_path, no_stdio);
3237
3238  err.SetError(::posix_spawnattr_init(&attr), DNBError::POSIX);
3239  if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3240    err.LogThreaded("::posix_spawnattr_init ( &attr )");
3241  if (err.Fail())
3242    return INVALID_NUB_PROCESS;
3243
3244  flags = POSIX_SPAWN_START_SUSPENDED | POSIX_SPAWN_SETSIGDEF |
3245          POSIX_SPAWN_SETSIGMASK;
3246  if (disable_aslr)
3247    flags |= _POSIX_SPAWN_DISABLE_ASLR;
3248
3249  sigset_t no_signals;
3250  sigset_t all_signals;
3251  sigemptyset(&no_signals);
3252  sigfillset(&all_signals);
3253  ::posix_spawnattr_setsigmask(&attr, &no_signals);
3254  ::posix_spawnattr_setsigdefault(&attr, &all_signals);
3255
3256  err.SetError(::posix_spawnattr_setflags(&attr, flags), DNBError::POSIX);
3257  if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3258    err.LogThreaded(
3259        "::posix_spawnattr_setflags ( &attr, POSIX_SPAWN_START_SUSPENDED%s )",
3260        flags & _POSIX_SPAWN_DISABLE_ASLR ? " | _POSIX_SPAWN_DISABLE_ASLR"
3261                                          : "");
3262  if (err.Fail())
3263    return INVALID_NUB_PROCESS;
3264
3265// Don't do this on SnowLeopard, _sometimes_ the TASK_BASIC_INFO will fail
3266// and we will fail to continue with our process...
3267
3268// On SnowLeopard we should set "DYLD_NO_PIE" in the inferior environment....
3269
3270#if !defined(__arm__)
3271
3272  // We don't need to do this for ARM, and we really shouldn't now that we
3273  // have multiple CPU subtypes and no posix_spawnattr call that allows us
3274  // to set which CPU subtype to launch...
3275  if (cpu_type != 0) {
3276    size_t ocount = 0;
3277    err.SetError(::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
3278                 DNBError::POSIX);
3279    if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3280      err.LogThreaded("::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = "
3281                      "0x%8.8x, count => %llu )",
3282                      cpu_type, (uint64_t)ocount);
3283
3284    if (err.Fail() != 0 || ocount != 1)
3285      return INVALID_NUB_PROCESS;
3286  }
3287#endif
3288
3289  PseudoTerminal pty;
3290
3291  posix_spawn_file_actions_t file_actions;
3292  err.SetError(::posix_spawn_file_actions_init(&file_actions), DNBError::POSIX);
3293  int file_actions_valid = err.Success();
3294  if (!file_actions_valid || DNBLogCheckLogBit(LOG_PROCESS))
3295    err.LogThreaded("::posix_spawn_file_actions_init ( &file_actions )");
3296  int pty_error = -1;
3297  pid_t pid = INVALID_NUB_PROCESS;
3298  if (file_actions_valid) {
3299    if (stdin_path == NULL && stdout_path == NULL && stderr_path == NULL &&
3300        !no_stdio) {
3301      pty_error = pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY);
3302      if (pty_error == PseudoTerminal::success) {
3303        stdin_path = stdout_path = stderr_path = pty.SecondaryName();
3304      }
3305    }
3306
3307    // if no_stdio or std paths not supplied, then route to "/dev/null".
3308    if (no_stdio || stdin_path == NULL || stdin_path[0] == '\0')
3309      stdin_path = "/dev/null";
3310    if (no_stdio || stdout_path == NULL || stdout_path[0] == '\0')
3311      stdout_path = "/dev/null";
3312    if (no_stdio || stderr_path == NULL || stderr_path[0] == '\0')
3313      stderr_path = "/dev/null";
3314
3315    err.SetError(::posix_spawn_file_actions_addopen(&file_actions, STDIN_FILENO,
3316                                                    stdin_path,
3317                                                    O_RDONLY | O_NOCTTY, 0),
3318                 DNBError::POSIX);
3319    if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3320      err.LogThreaded("::posix_spawn_file_actions_addopen (&file_actions, "
3321                      "filedes=STDIN_FILENO, path='%s')",
3322                      stdin_path);
3323
3324    err.SetError(::posix_spawn_file_actions_addopen(
3325                     &file_actions, STDOUT_FILENO, stdout_path,
3326                     O_WRONLY | O_NOCTTY | O_CREAT, 0640),
3327                 DNBError::POSIX);
3328    if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3329      err.LogThreaded("::posix_spawn_file_actions_addopen (&file_actions, "
3330                      "filedes=STDOUT_FILENO, path='%s')",
3331                      stdout_path);
3332
3333    err.SetError(::posix_spawn_file_actions_addopen(
3334                     &file_actions, STDERR_FILENO, stderr_path,
3335                     O_WRONLY | O_NOCTTY | O_CREAT, 0640),
3336                 DNBError::POSIX);
3337    if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3338      err.LogThreaded("::posix_spawn_file_actions_addopen (&file_actions, "
3339                      "filedes=STDERR_FILENO, path='%s')",
3340                      stderr_path);
3341
3342    // TODO: Verify if we can set the working directory back immediately
3343    // after the posix_spawnp call without creating a race condition???
3344    if (working_directory)
3345      ::chdir(working_directory);
3346
3347    err.SetError(::posix_spawnp(&pid, path, &file_actions, &attr,
3348                                const_cast<char *const *>(argv),
3349                                const_cast<char *const *>(envp)),
3350                 DNBError::POSIX);
3351    if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3352      err.LogThreaded("::posix_spawnp ( pid => %i, path = '%s', file_actions = "
3353                      "%p, attr = %p, argv = %p, envp = %p )",
3354                      pid, path, &file_actions, &attr, argv, envp);
3355  } else {
3356    // TODO: Verify if we can set the working directory back immediately
3357    // after the posix_spawnp call without creating a race condition???
3358    if (working_directory)
3359      ::chdir(working_directory);
3360
3361    err.SetError(::posix_spawnp(&pid, path, NULL, &attr,
3362                                const_cast<char *const *>(argv),
3363                                const_cast<char *const *>(envp)),
3364                 DNBError::POSIX);
3365    if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3366      err.LogThreaded("::posix_spawnp ( pid => %i, path = '%s', file_actions = "
3367                      "%p, attr = %p, argv = %p, envp = %p )",
3368                      pid, path, NULL, &attr, argv, envp);
3369  }
3370
3371  // We have seen some cases where posix_spawnp was returning a valid
3372  // looking pid even when an error was returned, so clear it out
3373  if (err.Fail())
3374    pid = INVALID_NUB_PROCESS;
3375
3376  if (pty_error == 0) {
3377    if (process != NULL) {
3378      int primary_fd = pty.ReleasePrimaryFD();
3379      process->SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
3380    }
3381  }
3382  ::posix_spawnattr_destroy(&attr);
3383
3384  if (pid != INVALID_NUB_PROCESS) {
3385    cpu_type_t pid_cpu_type = MachProcess::GetCPUTypeForLocalProcess(pid);
3386    DNBLogThreadedIf(LOG_PROCESS,
3387                     "MachProcess::%s ( ) pid=%i, cpu_type=0x%8.8x",
3388                     __FUNCTION__, pid, pid_cpu_type);
3389    if (pid_cpu_type)
3390      DNBArchProtocol::SetArchitecture(pid_cpu_type);
3391  }
3392
3393  if (file_actions_valid) {
3394    DNBError err2;
3395    err2.SetError(::posix_spawn_file_actions_destroy(&file_actions),
3396                  DNBError::POSIX);
3397    if (err2.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3398      err2.LogThreaded("::posix_spawn_file_actions_destroy ( &file_actions )");
3399  }
3400
3401  return pid;
3402}
3403
3404uint32_t MachProcess::GetCPUTypeForLocalProcess(pid_t pid) {
3405  int mib[CTL_MAXNAME] = {
3406      0,
3407  };
3408  size_t len = CTL_MAXNAME;
3409  if (::sysctlnametomib("sysctl.proc_cputype", mib, &len))
3410    return 0;
3411
3412  mib[len] = pid;
3413  len++;
3414
3415  cpu_type_t cpu;
3416  size_t cpu_len = sizeof(cpu);
3417  if (::sysctl(mib, static_cast<u_int>(len), &cpu, &cpu_len, 0, 0))
3418    cpu = 0;
3419  return cpu;
3420}
3421
3422pid_t MachProcess::ForkChildForPTraceDebugging(const char *path,
3423                                               char const *argv[],
3424                                               char const *envp[],
3425                                               MachProcess *process,
3426                                               DNBError &launch_err) {
3427  PseudoTerminal::Status pty_error = PseudoTerminal::success;
3428
3429  // Use a fork that ties the child process's stdin/out/err to a pseudo
3430  // terminal so we can read it in our MachProcess::STDIOThread
3431  // as unbuffered io.
3432  PseudoTerminal pty;
3433  pid_t pid = pty.Fork(pty_error);
3434
3435  if (pid < 0) {
3436    //--------------------------------------------------------------
3437    // Status during fork.
3438    //--------------------------------------------------------------
3439    return pid;
3440  } else if (pid == 0) {
3441    //--------------------------------------------------------------
3442    // Child process
3443    //--------------------------------------------------------------
3444    ::ptrace(PT_TRACE_ME, 0, 0, 0); // Debug this process
3445    ::ptrace(PT_SIGEXC, 0, 0, 0);   // Get BSD signals as mach exceptions
3446
3447    // If our parent is setgid, lets make sure we don't inherit those
3448    // extra powers due to nepotism.
3449    if (::setgid(getgid()) == 0) {
3450
3451      // Let the child have its own process group. We need to execute
3452      // this call in both the child and parent to avoid a race condition
3453      // between the two processes.
3454      ::setpgid(0, 0); // Set the child process group to match its pid
3455
3456      // Sleep a bit to before the exec call
3457      ::sleep(1);
3458
3459      // Turn this process into
3460      ::execv(path, const_cast<char *const *>(argv));
3461    }
3462    // Exit with error code. Child process should have taken
3463    // over in above exec call and if the exec fails it will
3464    // exit the child process below.
3465    ::exit(127);
3466  } else {
3467    //--------------------------------------------------------------
3468    // Parent process
3469    //--------------------------------------------------------------
3470    // Let the child have its own process group. We need to execute
3471    // this call in both the child and parent to avoid a race condition
3472    // between the two processes.
3473    ::setpgid(pid, pid); // Set the child process group to match its pid
3474
3475    if (process != NULL) {
3476      // Release our primary pty file descriptor so the pty class doesn't
3477      // close it and so we can continue to use it in our STDIO thread
3478      int primary_fd = pty.ReleasePrimaryFD();
3479      process->SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
3480    }
3481  }
3482  return pid;
3483}
3484
3485#if defined(WITH_SPRINGBOARD) || defined(WITH_BKS) || defined(WITH_FBS)
3486// This returns a CFRetained pointer to the Bundle ID for app_bundle_path,
3487// or NULL if there was some problem getting the bundle id.
3488static CFStringRef CopyBundleIDForPath(const char *app_bundle_path,
3489                                       DNBError &err_str) {
3490  CFBundle bundle(app_bundle_path);
3491  CFStringRef bundleIDCFStr = bundle.GetIdentifier();
3492  std::string bundleID;
3493  if (CFString::UTF8(bundleIDCFStr, bundleID) == NULL) {
3494    struct stat app_bundle_stat;
3495    char err_msg[PATH_MAX];
3496
3497    if (::stat(app_bundle_path, &app_bundle_stat) < 0) {
3498      err_str.SetError(errno, DNBError::POSIX);
3499      snprintf(err_msg, sizeof(err_msg), "%s: \"%s\"", err_str.AsString(),
3500               app_bundle_path);
3501      err_str.SetErrorString(err_msg);
3502      DNBLogThreadedIf(LOG_PROCESS, "%s() error: %s", __FUNCTION__, err_msg);
3503    } else {
3504      err_str.SetError(-1, DNBError::Generic);
3505      snprintf(err_msg, sizeof(err_msg),
3506               "failed to extract CFBundleIdentifier from %s", app_bundle_path);
3507      err_str.SetErrorString(err_msg);
3508      DNBLogThreadedIf(
3509          LOG_PROCESS,
3510          "%s() error: failed to extract CFBundleIdentifier from '%s'",
3511          __FUNCTION__, app_bundle_path);
3512    }
3513    return NULL;
3514  }
3515
3516  DNBLogThreadedIf(LOG_PROCESS, "%s() extracted CFBundleIdentifier: %s",
3517                   __FUNCTION__, bundleID.c_str());
3518  CFRetain(bundleIDCFStr);
3519
3520  return bundleIDCFStr;
3521}
3522#endif // #if defined (WITH_SPRINGBOARD) || defined (WITH_BKS) || defined
3523       // (WITH_FBS)
3524#ifdef WITH_SPRINGBOARD
3525
3526pid_t MachProcess::SBLaunchForDebug(const char *path, char const *argv[],
3527                                    char const *envp[], bool no_stdio,
3528                                    bool disable_aslr, DNBError &launch_err) {
3529  // Clear out and clean up from any current state
3530  Clear();
3531
3532  DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv)", __FUNCTION__, path);
3533
3534  // Fork a child process for debugging
3535  SetState(eStateLaunching);
3536  m_pid = MachProcess::SBForkChildForPTraceDebugging(path, argv, envp, no_stdio,
3537                                                     this, launch_err);
3538  if (m_pid != 0) {
3539    m_path = path;
3540    size_t i;
3541    char const *arg;
3542    for (i = 0; (arg = argv[i]) != NULL; i++)
3543      m_args.push_back(arg);
3544    m_task.StartExceptionThread(launch_err);
3545
3546    if (launch_err.Fail()) {
3547      if (launch_err.AsString() == NULL)
3548        launch_err.SetErrorString("unable to start the exception thread");
3549      DNBLog("Could not get inferior's Mach exception port, sending ptrace "
3550             "PT_KILL and exiting.");
3551      ::ptrace(PT_KILL, m_pid, 0, 0);
3552      m_pid = INVALID_NUB_PROCESS;
3553      return INVALID_NUB_PROCESS;
3554    }
3555
3556    StartSTDIOThread();
3557    SetState(eStateAttaching);
3558    int err = ::ptrace(PT_ATTACHEXC, m_pid, 0, 0);
3559    if (err == 0) {
3560      m_flags |= eMachProcessFlagsAttached;
3561      DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", m_pid);
3562    } else {
3563      SetState(eStateExited);
3564      DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", m_pid);
3565    }
3566  }
3567  return m_pid;
3568}
3569
3570#include <servers/bootstrap.h>
3571
3572pid_t MachProcess::SBForkChildForPTraceDebugging(
3573    const char *app_bundle_path, char const *argv[], char const *envp[],
3574    bool no_stdio, MachProcess *process, DNBError &launch_err) {
3575  DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv, %p)", __FUNCTION__,
3576                   app_bundle_path, process);
3577  CFAllocatorRef alloc = kCFAllocatorDefault;
3578
3579  if (argv[0] == NULL)
3580    return INVALID_NUB_PROCESS;
3581
3582  size_t argc = 0;
3583  // Count the number of arguments
3584  while (argv[argc] != NULL)
3585    argc++;
3586
3587  // Enumerate the arguments
3588  size_t first_launch_arg_idx = 1;
3589  CFReleaser<CFMutableArrayRef> launch_argv;
3590
3591  if (argv[first_launch_arg_idx]) {
3592    size_t launch_argc = argc > 0 ? argc - 1 : 0;
3593    launch_argv.reset(
3594        ::CFArrayCreateMutable(alloc, launch_argc, &kCFTypeArrayCallBacks));
3595    size_t i;
3596    char const *arg;
3597    CFString launch_arg;
3598    for (i = first_launch_arg_idx; (i < argc) && ((arg = argv[i]) != NULL);
3599         i++) {
3600      launch_arg.reset(
3601          ::CFStringCreateWithCString(alloc, arg, kCFStringEncodingUTF8));
3602      if (launch_arg.get() != NULL)
3603        CFArrayAppendValue(launch_argv.get(), launch_arg.get());
3604      else
3605        break;
3606    }
3607  }
3608
3609  // Next fill in the arguments dictionary.  Note, the envp array is of the form
3610  // Variable=value but SpringBoard wants a CF dictionary.  So we have to
3611  // convert
3612  // this here.
3613
3614  CFReleaser<CFMutableDictionaryRef> launch_envp;
3615
3616  if (envp[0]) {
3617    launch_envp.reset(
3618        ::CFDictionaryCreateMutable(alloc, 0, &kCFTypeDictionaryKeyCallBacks,
3619                                    &kCFTypeDictionaryValueCallBacks));
3620    const char *value;
3621    int name_len;
3622    CFString name_string, value_string;
3623
3624    for (int i = 0; envp[i] != NULL; i++) {
3625      value = strstr(envp[i], "=");
3626
3627      // If the name field is empty or there's no =, skip it.  Somebody's
3628      // messing with us.
3629      if (value == NULL || value == envp[i])
3630        continue;
3631
3632      name_len = value - envp[i];
3633
3634      // Now move value over the "="
3635      value++;
3636
3637      name_string.reset(
3638          ::CFStringCreateWithBytes(alloc, (const UInt8 *)envp[i], name_len,
3639                                    kCFStringEncodingUTF8, false));
3640      value_string.reset(
3641          ::CFStringCreateWithCString(alloc, value, kCFStringEncodingUTF8));
3642      CFDictionarySetValue(launch_envp.get(), name_string.get(),
3643                           value_string.get());
3644    }
3645  }
3646
3647  CFString stdio_path;
3648
3649  PseudoTerminal pty;
3650  if (!no_stdio) {
3651    PseudoTerminal::Status pty_err =
3652        pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY);
3653    if (pty_err == PseudoTerminal::success) {
3654      const char *secondary_name = pty.SecondaryName();
3655      DNBLogThreadedIf(LOG_PROCESS,
3656                       "%s() successfully opened primary pty, secondary is %s",
3657                       __FUNCTION__, secondary_name);
3658      if (secondary_name && secondary_name[0]) {
3659        ::chmod(secondary_name, S_IRWXU | S_IRWXG | S_IRWXO);
3660        stdio_path.SetFileSystemRepresentation(secondary_name);
3661      }
3662    }
3663  }
3664
3665  if (stdio_path.get() == NULL) {
3666    stdio_path.SetFileSystemRepresentation("/dev/null");
3667  }
3668
3669  CFStringRef bundleIDCFStr = CopyBundleIDForPath(app_bundle_path, launch_err);
3670  if (bundleIDCFStr == NULL)
3671    return INVALID_NUB_PROCESS;
3672
3673  // This is just for logging:
3674  std::string bundleID;
3675  CFString::UTF8(bundleIDCFStr, bundleID);
3676
3677  DNBLogThreadedIf(LOG_PROCESS, "%s() serialized launch arg array",
3678                   __FUNCTION__);
3679
3680  // Find SpringBoard
3681  SBSApplicationLaunchError sbs_error = 0;
3682  sbs_error = SBSLaunchApplicationForDebugging(
3683      bundleIDCFStr,
3684      (CFURLRef)NULL, // openURL
3685      launch_argv.get(),
3686      launch_envp.get(), // CFDictionaryRef environment
3687      stdio_path.get(), stdio_path.get(),
3688      SBSApplicationLaunchWaitForDebugger | SBSApplicationLaunchUnlockDevice);
3689
3690  launch_err.SetError(sbs_error, DNBError::SpringBoard);
3691
3692  if (sbs_error == SBSApplicationLaunchErrorSuccess) {
3693    static const useconds_t pid_poll_interval = 200000;
3694    static const useconds_t pid_poll_timeout = 30000000;
3695
3696    useconds_t pid_poll_total = 0;
3697
3698    nub_process_t pid = INVALID_NUB_PROCESS;
3699    Boolean pid_found = SBSProcessIDForDisplayIdentifier(bundleIDCFStr, &pid);
3700    // Poll until the process is running, as long as we are getting valid
3701    // responses and the timeout hasn't expired
3702    // A return PID of 0 means the process is not running, which may be because
3703    // it hasn't been (asynchronously) started
3704    // yet, or that it died very quickly (if you weren't using waitForDebugger).
3705    while (!pid_found && pid_poll_total < pid_poll_timeout) {
3706      usleep(pid_poll_interval);
3707      pid_poll_total += pid_poll_interval;
3708      DNBLogThreadedIf(LOG_PROCESS,
3709                       "%s() polling Springboard for pid for %s...",
3710                       __FUNCTION__, bundleID.c_str());
3711      pid_found = SBSProcessIDForDisplayIdentifier(bundleIDCFStr, &pid);
3712    }
3713
3714    CFRelease(bundleIDCFStr);
3715    if (pid_found) {
3716      if (process != NULL) {
3717        // Release our primary pty file descriptor so the pty class doesn't
3718        // close it and so we can continue to use it in our STDIO thread
3719        int primary_fd = pty.ReleasePrimaryFD();
3720        process->SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
3721      }
3722      DNBLogThreadedIf(LOG_PROCESS, "%s() => pid = %4.4x", __FUNCTION__, pid);
3723    } else {
3724      DNBLogError("failed to lookup the process ID for CFBundleIdentifier %s.",
3725                  bundleID.c_str());
3726    }
3727    return pid;
3728  }
3729
3730  DNBLogError("unable to launch the application with CFBundleIdentifier '%s' "
3731              "sbs_error = %u",
3732              bundleID.c_str(), sbs_error);
3733  return INVALID_NUB_PROCESS;
3734}
3735
3736#endif // #ifdef WITH_SPRINGBOARD
3737
3738#if defined(WITH_BKS) || defined(WITH_FBS)
3739pid_t MachProcess::BoardServiceLaunchForDebug(
3740    const char *path, char const *argv[], char const *envp[], bool no_stdio,
3741    bool disable_aslr, const char *event_data, DNBError &launch_err) {
3742  DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv)", __FUNCTION__, path);
3743
3744  // Fork a child process for debugging
3745  SetState(eStateLaunching);
3746  m_pid = BoardServiceForkChildForPTraceDebugging(
3747      path, argv, envp, no_stdio, disable_aslr, event_data, launch_err);
3748  if (m_pid != 0) {
3749    m_path = path;
3750    size_t i;
3751    char const *arg;
3752    for (i = 0; (arg = argv[i]) != NULL; i++)
3753      m_args.push_back(arg);
3754    m_task.StartExceptionThread(launch_err);
3755
3756    if (launch_err.Fail()) {
3757      if (launch_err.AsString() == NULL)
3758        launch_err.SetErrorString("unable to start the exception thread");
3759      DNBLog("Could not get inferior's Mach exception port, sending ptrace "
3760             "PT_KILL and exiting.");
3761      ::ptrace(PT_KILL, m_pid, 0, 0);
3762      m_pid = INVALID_NUB_PROCESS;
3763      return INVALID_NUB_PROCESS;
3764    }
3765
3766    StartSTDIOThread();
3767    SetState(eStateAttaching);
3768    int err = ::ptrace(PT_ATTACHEXC, m_pid, 0, 0);
3769    if (err == 0) {
3770      m_flags |= eMachProcessFlagsAttached;
3771      DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", m_pid);
3772    } else {
3773      SetState(eStateExited);
3774      DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", m_pid);
3775    }
3776  }
3777  return m_pid;
3778}
3779
3780pid_t MachProcess::BoardServiceForkChildForPTraceDebugging(
3781    const char *app_bundle_path, char const *argv[], char const *envp[],
3782    bool no_stdio, bool disable_aslr, const char *event_data,
3783    DNBError &launch_err) {
3784  if (argv[0] == NULL)
3785    return INVALID_NUB_PROCESS;
3786
3787  DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv, %p)", __FUNCTION__,
3788                   app_bundle_path, this);
3789
3790  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3791
3792  size_t argc = 0;
3793  // Count the number of arguments
3794  while (argv[argc] != NULL)
3795    argc++;
3796
3797  // Enumerate the arguments
3798  size_t first_launch_arg_idx = 1;
3799
3800  NSMutableArray *launch_argv = nil;
3801
3802  if (argv[first_launch_arg_idx]) {
3803    size_t launch_argc = argc > 0 ? argc - 1 : 0;
3804    launch_argv = [NSMutableArray arrayWithCapacity:launch_argc];
3805    size_t i;
3806    char const *arg;
3807    NSString *launch_arg;
3808    for (i = first_launch_arg_idx; (i < argc) && ((arg = argv[i]) != NULL);
3809         i++) {
3810      launch_arg = [NSString stringWithUTF8String:arg];
3811      // FIXME: Should we silently eat an argument that we can't convert into a
3812      // UTF8 string?
3813      if (launch_arg != nil)
3814        [launch_argv addObject:launch_arg];
3815      else
3816        break;
3817    }
3818  }
3819
3820  NSMutableDictionary *launch_envp = nil;
3821  if (envp[0]) {
3822    launch_envp = [[NSMutableDictionary alloc] init];
3823    const char *value;
3824    int name_len;
3825    NSString *name_string, *value_string;
3826
3827    for (int i = 0; envp[i] != NULL; i++) {
3828      value = strstr(envp[i], "=");
3829
3830      // If the name field is empty or there's no =, skip it.  Somebody's
3831      // messing with us.
3832      if (value == NULL || value == envp[i])
3833        continue;
3834
3835      name_len = value - envp[i];
3836
3837      // Now move value over the "="
3838      value++;
3839      name_string = [[NSString alloc] initWithBytes:envp[i]
3840                                             length:name_len
3841                                           encoding:NSUTF8StringEncoding];
3842      value_string = [NSString stringWithUTF8String:value];
3843      [launch_envp setObject:value_string forKey:name_string];
3844    }
3845  }
3846
3847  NSString *stdio_path = nil;
3848  NSFileManager *file_manager = [NSFileManager defaultManager];
3849
3850  PseudoTerminal pty;
3851  if (!no_stdio) {
3852    PseudoTerminal::Status pty_err =
3853        pty.OpenFirstAvailablePrimary(O_RDWR | O_NOCTTY);
3854    if (pty_err == PseudoTerminal::success) {
3855      const char *secondary_name = pty.SecondaryName();
3856      DNBLogThreadedIf(LOG_PROCESS,
3857                       "%s() successfully opened primary pty, secondary is %s",
3858                       __FUNCTION__, secondary_name);
3859      if (secondary_name && secondary_name[0]) {
3860        ::chmod(secondary_name, S_IRWXU | S_IRWXG | S_IRWXO);
3861        stdio_path = [file_manager
3862            stringWithFileSystemRepresentation:secondary_name
3863                                        length:strlen(secondary_name)];
3864      }
3865    }
3866  }
3867
3868  if (stdio_path == nil) {
3869    const char *null_path = "/dev/null";
3870    stdio_path =
3871        [file_manager stringWithFileSystemRepresentation:null_path
3872                                                  length:strlen(null_path)];
3873  }
3874
3875  CFStringRef bundleIDCFStr = CopyBundleIDForPath(app_bundle_path, launch_err);
3876  if (bundleIDCFStr == NULL) {
3877    [pool drain];
3878    return INVALID_NUB_PROCESS;
3879  }
3880
3881  // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use
3882  // toll-free bridging here:
3883  NSString *bundleIDNSStr = (NSString *)bundleIDCFStr;
3884
3885  // Okay, now let's assemble all these goodies into the BackBoardServices
3886  // options mega-dictionary:
3887
3888  NSMutableDictionary *options = nullptr;
3889  pid_t return_pid = INVALID_NUB_PROCESS;
3890  bool success = false;
3891
3892#ifdef WITH_BKS
3893  if (ProcessUsingBackBoard()) {
3894    options =
3895        BKSCreateOptionsDictionary(app_bundle_path, launch_argv, launch_envp,
3896                                   stdio_path, disable_aslr, event_data);
3897    success = BKSCallOpenApplicationFunction(bundleIDNSStr, options, launch_err,
3898                                             &return_pid);
3899  }
3900#endif
3901#ifdef WITH_FBS
3902  if (ProcessUsingFrontBoard()) {
3903    options =
3904        FBSCreateOptionsDictionary(app_bundle_path, launch_argv, launch_envp,
3905                                   stdio_path, disable_aslr, event_data);
3906    success = FBSCallOpenApplicationFunction(bundleIDNSStr, options, launch_err,
3907                                             &return_pid);
3908  }
3909#endif
3910
3911  if (success) {
3912    int primary_fd = pty.ReleasePrimaryFD();
3913    SetChildFileDescriptors(primary_fd, primary_fd, primary_fd);
3914    CFString::UTF8(bundleIDCFStr, m_bundle_id);
3915  }
3916
3917  [pool drain];
3918
3919  return return_pid;
3920}
3921
3922bool MachProcess::BoardServiceSendEvent(const char *event_data,
3923                                        DNBError &send_err) {
3924  bool return_value = true;
3925
3926  if (event_data == NULL || *event_data == '\0') {
3927    DNBLogError("SendEvent called with NULL event data.");
3928    send_err.SetErrorString("SendEvent called with empty event data");
3929    return false;
3930  }
3931
3932  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3933
3934  if (strcmp(event_data, "BackgroundApplication") == 0) {
3935// This is an event I cooked up.  What you actually do is foreground the system
3936// app, so:
3937#ifdef WITH_BKS
3938    if (ProcessUsingBackBoard()) {
3939      return_value = BKSCallOpenApplicationFunction(nil, nil, send_err, NULL);
3940    }
3941#endif
3942#ifdef WITH_FBS
3943    if (ProcessUsingFrontBoard()) {
3944      return_value = FBSCallOpenApplicationFunction(nil, nil, send_err, NULL);
3945    }
3946#endif
3947    if (!return_value) {
3948      DNBLogError("Failed to background application, error: %s.",
3949                  send_err.AsString());
3950    }
3951  } else {
3952    if (m_bundle_id.empty()) {
3953      // See if we can figure out the bundle ID for this PID:
3954
3955      DNBLogError(
3956          "Tried to send event \"%s\" to a process that has no bundle ID.",
3957          event_data);
3958      return false;
3959    }
3960
3961    NSString *bundleIDNSStr =
3962        [NSString stringWithUTF8String:m_bundle_id.c_str()];
3963
3964    NSMutableDictionary *options = [NSMutableDictionary dictionary];
3965
3966#ifdef WITH_BKS
3967    if (ProcessUsingBackBoard()) {
3968      if (!BKSAddEventDataToOptions(options, event_data, send_err)) {
3969        [pool drain];
3970        return false;
3971      }
3972      return_value = BKSCallOpenApplicationFunction(bundleIDNSStr, options,
3973                                                    send_err, NULL);
3974      DNBLogThreadedIf(LOG_PROCESS,
3975                       "Called BKSCallOpenApplicationFunction to send event.");
3976    }
3977#endif
3978#ifdef WITH_FBS
3979    if (ProcessUsingFrontBoard()) {
3980      if (!FBSAddEventDataToOptions(options, event_data, send_err)) {
3981        [pool drain];
3982        return false;
3983      }
3984      return_value = FBSCallOpenApplicationFunction(bundleIDNSStr, options,
3985                                                    send_err, NULL);
3986      DNBLogThreadedIf(LOG_PROCESS,
3987                       "Called FBSCallOpenApplicationFunction to send event.");
3988    }
3989#endif
3990
3991    if (!return_value) {
3992      DNBLogError("Failed to send event: %s, error: %s.", event_data,
3993                  send_err.AsString());
3994    }
3995  }
3996
3997  [pool drain];
3998  return return_value;
3999}
4000#endif // defined(WITH_BKS) || defined (WITH_FBS)
4001
4002#ifdef WITH_BKS
4003void MachProcess::BKSCleanupAfterAttach(const void *attach_token,
4004                                        DNBError &err_str) {
4005  bool success;
4006
4007  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4008
4009  // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use
4010  // toll-free bridging here:
4011  NSString *bundleIDNSStr = (NSString *)attach_token;
4012
4013  // Okay, now let's assemble all these goodies into the BackBoardServices
4014  // options mega-dictionary:
4015
4016  // First we have the debug sub-dictionary:
4017  NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
4018  [debug_options setObject:[NSNumber numberWithBool:YES]
4019                    forKey:BKSDebugOptionKeyCancelDebugOnNextLaunch];
4020
4021  // That will go in the overall dictionary:
4022
4023  NSMutableDictionary *options = [NSMutableDictionary dictionary];
4024  [options setObject:debug_options
4025              forKey:BKSOpenApplicationOptionKeyDebuggingOptions];
4026
4027  success =
4028      BKSCallOpenApplicationFunction(bundleIDNSStr, options, err_str, NULL);
4029
4030  if (!success) {
4031    DNBLogError("error trying to cancel debug on next launch for %s: %s",
4032                [bundleIDNSStr UTF8String], err_str.AsString());
4033  }
4034
4035  [pool drain];
4036}
4037#endif // WITH_BKS
4038
4039#ifdef WITH_FBS
4040void MachProcess::FBSCleanupAfterAttach(const void *attach_token,
4041                                        DNBError &err_str) {
4042  bool success;
4043
4044  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4045
4046  // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use
4047  // toll-free bridging here:
4048  NSString *bundleIDNSStr = (NSString *)attach_token;
4049
4050  // Okay, now let's assemble all these goodies into the BackBoardServices
4051  // options mega-dictionary:
4052
4053  // First we have the debug sub-dictionary:
4054  NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
4055  [debug_options setObject:[NSNumber numberWithBool:YES]
4056                    forKey:FBSDebugOptionKeyCancelDebugOnNextLaunch];
4057
4058  // That will go in the overall dictionary:
4059
4060  NSMutableDictionary *options = [NSMutableDictionary dictionary];
4061  [options setObject:debug_options
4062              forKey:FBSOpenApplicationOptionKeyDebuggingOptions];
4063
4064  success =
4065      FBSCallOpenApplicationFunction(bundleIDNSStr, options, err_str, NULL);
4066
4067  if (!success) {
4068    DNBLogError("error trying to cancel debug on next launch for %s: %s",
4069                [bundleIDNSStr UTF8String], err_str.AsString());
4070  }
4071
4072  [pool drain];
4073}
4074#endif // WITH_FBS
4075
4076
4077void MachProcess::CalculateBoardStatus()
4078{
4079  if (m_flags & eMachProcessFlagsBoardCalculated)
4080    return;
4081  if (m_pid == 0)
4082    return;
4083
4084#if defined (WITH_FBS) || defined (WITH_BKS)
4085    bool found_app_flavor = false;
4086#endif
4087
4088#if defined(WITH_FBS)
4089    if (!found_app_flavor && IsFBSProcess(m_pid)) {
4090      found_app_flavor = true;
4091      m_flags |= eMachProcessFlagsUsingFBS;
4092    }
4093#endif
4094#if defined(WITH_BKS)
4095    if (!found_app_flavor && IsBKSProcess(m_pid)) {
4096      found_app_flavor = true;
4097      m_flags |= eMachProcessFlagsUsingBKS;
4098    }
4099#endif
4100
4101    m_flags |= eMachProcessFlagsBoardCalculated;
4102}
4103
4104bool MachProcess::ProcessUsingBackBoard() {
4105  CalculateBoardStatus();
4106  return (m_flags & eMachProcessFlagsUsingBKS) != 0;
4107}
4108
4109bool MachProcess::ProcessUsingFrontBoard() {
4110  CalculateBoardStatus();
4111  return (m_flags & eMachProcessFlagsUsingFBS) != 0;
4112}
4113