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