1 //===-- MachProcess.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Created by Greg Clayton on 6/15/07.
12 //===----------------------------------------------------------------------===//
17 #include <mach/mach.h>
18 #include <mach/task.h>
21 #include <sys/fcntl.h>
22 #include <sys/types.h>
23 #include <sys/ptrace.h>
25 #include <sys/sysctl.h>
28 #include <mach-o/loader.h>
29 #include <uuid/uuid.h>
30 #include "MacOSX/CFUtils.h"
31 #include "SysSignal.h"
36 #import <Foundation/Foundation.h>
38 #include "DNBDataRef.h"
40 #include "DNBThreadResumeActions.h"
42 #include "MachProcess.h"
43 #include "PseudoTerminal.h"
49 #ifdef WITH_SPRINGBOARD
51 #include <CoreFoundation/CoreFoundation.h>
52 #include <SpringBoardServices/SpringBoardServer.h>
53 #include <SpringBoardServices/SBSWatchdogAssertion.h>
56 IsSBProcess (nub_process_t pid)
58 CFReleaser<CFArrayRef> appIdsForPID (::SBSCopyDisplayIdentifiersForProcessID(pid));
59 return appIdsForPID.get() != NULL;
62 #endif // WITH_SPRINGBOARD
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);
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.
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.
83 template <typename OpenFlavor, typename ErrorFlavor, ErrorFlavor no_error_enum_value, SetErrorFunction error_function>
85 CallBoardSystemServiceOpenApplication (NSString *bundleIDNSStr, NSDictionary *options, DNBError &error, pid_t *return_pid)
87 // Now make our systemService:
88 OpenFlavor *system_service = [[OpenFlavor alloc] init];
90 if (bundleIDNSStr == nil)
92 bundleIDNSStr = [system_service systemApplicationBundleIdentifier];
93 if (bundleIDNSStr == nil)
95 // Okay, no system app...
96 error.SetErrorString("No system application to message.");
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;
107 const char *cstr = [bundleIDNSStr UTF8String];
109 cstr = "<Unknown Bundle ID>";
111 DNBLog ("About to launch process for bundle ID: %s", cstr);
112 [system_service openApplication: bundleIDNSStr
114 clientPort: client_port
115 withResult: ^(NSError *bks_error)
117 // The system service will cleanup the client port we created for us.
119 open_app_error = (ErrorFlavor)[bks_error code];
121 if (open_app_error == no_error_enum_value)
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);
130 DNBLogThreadedIf (LOG_PROCESS, "In completion handler: success.");
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>",
139 DNBLogError ("In completion handler for send event, got error \"%s\"(%ld).",
140 error_str ? error_str : "<unknown error>",
144 [system_service release];
145 dispatch_semaphore_signal(semaphore);
150 const uint32_t timeout_secs = 9;
152 dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC);
154 long success = dispatch_semaphore_wait(semaphore, timeout) == 0;
156 dispatch_release(semaphore);
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");
164 else if (open_app_error != no_error_enum_value)
166 error_function (open_app_error, error);
167 DNBLogError("unable to launch the application with CFBundleIdentifier '%s' bks_error = %u", cstr, open_app_error);
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);
182 #import <Foundation/Foundation.h>
185 #import <BackBoardServices/BackBoardServices.h>
186 #import <BackBoardServices/BKSSystemService_LaunchServices.h>
187 #import <BackBoardServices/BKSOpenApplicationConstants_Private.h>
191 IsBKSProcess (nub_process_t pid)
193 BKSApplicationStateMonitor *state_monitor = [[BKSApplicationStateMonitor alloc] init];
194 BKSApplicationState app_state = [state_monitor mostElevatedApplicationStateForPID: pid];
195 return app_state != BKSApplicationStateUnknown;
199 SetBKSError (NSInteger error_code, DNBError &error)
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";
208 err_str = [err_nsstr UTF8String];
210 err_str = "unknown BKS error";
212 error.SetErrorString(err_str);
216 BKSAddEventDataToOptions (NSMutableDictionary *options, const char *event_data, DNBError &option_error)
218 if (strcmp (event_data, "BackgroundContentFetching") == 0)
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];
228 DNBLogError ("Unrecognized event type: %s. Ignoring.", event_data);
229 option_error.SetErrorString("Unrecognized event data.");
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)
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];
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];
248 [debug_options setObject: [NSNumber numberWithBool: YES] forKey: BKSDebugOptionKeyDisableASLR];
250 // That will go in the overall dictionary:
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];
258 BKSAddEventDataToOptions (options, event_data, error);
263 static CallOpenApplicationFunction BKSCallOpenApplicationFunction = CallBoardSystemServiceOpenApplication<BKSSystemService, BKSOpenApplicationErrorCode, BKSOpenApplicationErrorCodeNone, SetBKSError>;
267 #import <Foundation/Foundation.h>
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>
279 IsFBSProcess (nub_process_t pid)
281 BKSApplicationStateMonitor *state_monitor = [[BKSApplicationStateMonitor alloc] init];
282 BKSApplicationState app_state = [state_monitor mostElevatedApplicationStateForPID: pid];
283 return app_state != BKSApplicationStateUnknown;
287 IsFBSProcess (nub_process_t pid)
289 // FIXME: What is the FBS equivalent of BKSApplicationStateMonitor
295 SetFBSError (NSInteger error_code, DNBError &error)
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";
304 err_str = [err_nsstr UTF8String];
306 err_str = "unknown FBS error";
308 error.SetErrorString(err_str);
312 FBSAddEventDataToOptions (NSMutableDictionary *options, const char *event_data, DNBError &option_error)
314 if (strcmp (event_data, "BackgroundContentFetching") == 0)
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];
324 DNBLogError ("Unrecognized event type: %s. Ignoring.", event_data);
325 option_error.SetErrorString("Unrecognized event data.");
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)
334 NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
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];
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];
345 [debug_options setObject: [NSNumber numberWithBool: YES] forKey: FBSDebugOptionKeyDisableASLR];
347 // That will go in the overall dictionary:
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];
354 // We have to get the "sequence ID & UUID" for this app bundle path and send them to FBS:
356 NSURL *app_bundle_url = [NSURL fileURLWithPath: [NSString stringWithUTF8String: app_bundle_path] isDirectory: YES];
357 LSApplicationProxy *app_proxy = [LSApplicationProxy applicationProxyForBundleURL: app_bundle_url];
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];
366 FBSAddEventDataToOptions (options, event_data, error);
370 static CallOpenApplicationFunction FBSCallOpenApplicationFunction = CallBoardSystemServiceOpenApplication<FBSSystemService, FBSOpenApplicationErrorCode, FBSOpenApplicationErrorCodeNone, SetFBSError>;
374 #define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__)
376 #define DEBUG_LOG(fmt, ...)
379 #ifndef MACH_PROCESS_USE_POSIX_SPAWN
380 #define MACH_PROCESS_USE_POSIX_SPAWN 1
383 #ifndef _POSIX_SPAWN_DISABLE_ASLR
384 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
387 MachProcess::MachProcess() :
396 m_flags (eMachProcessFlagsNone),
398 m_stdio_mutex (PTHREAD_MUTEX_RECURSIVE),
400 m_profile_enabled (false),
401 m_profile_interval_usec (0),
402 m_profile_thread (0),
403 m_profile_data_mutex(PTHREAD_MUTEX_RECURSIVE),
406 m_exception_messages (),
407 m_exception_messages_mutex (PTHREAD_MUTEX_RECURSIVE),
410 m_state (eStateUnloaded),
411 m_state_mutex (PTHREAD_MUTEX_RECURSIVE),
412 m_events (0, kAllEventsMask),
413 m_private_events (0, kAllEventsMask),
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),
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)
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");
433 DNBLogThreadedIf(LOG_PROCESS | LOG_VERBOSE, "%s", __PRETTY_FUNCTION__);
436 MachProcess::~MachProcess()
438 DNBLogThreadedIf(LOG_PROCESS | LOG_VERBOSE, "%s", __PRETTY_FUNCTION__);
443 MachProcess::SetProcessID(pid_t pid)
445 // Free any previous process specific data or resources
447 // Set the current PID appropriately
452 return m_pid; // Return actually PID in case a zero pid was passed in
456 MachProcess::GetState()
458 // If any other threads access this we will need a mutex for it
459 PTHREAD_MUTEX_LOCKER(locker, m_state_mutex);
464 MachProcess::ThreadGetName(nub_thread_t tid)
466 return m_thread_list.GetName(tid);
470 MachProcess::ThreadGetState(nub_thread_t tid)
472 return m_thread_list.GetState(tid);
477 MachProcess::GetNumThreads () const
479 return m_thread_list.NumThreads();
483 MachProcess::GetThreadAtIndex (nub_size_t thread_idx) const
485 return m_thread_list.ThreadIDAtIndex(thread_idx);
489 MachProcess::GetThreadIDForMachPortNumber (thread_t mach_port_number) const
491 return m_thread_list.GetThreadIDByMachPortNumber (mach_port_number);
495 MachProcess::SyncThreadState (nub_thread_t tid)
497 MachThreadSP thread_sp(m_thread_list.GetThreadByID(tid));
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());
503 if (kret == KERN_SUCCESS)
511 MachProcess::GetRequestedQoS (nub_thread_t tid, nub_addr_t tsd, uint64_t dti_qos_class_index)
513 return m_thread_list.GetRequestedQoS (tid, tsd, dti_qos_class_index);
517 MachProcess::GetPThreadT (nub_thread_t tid)
519 return m_thread_list.GetPThreadT (tid);
523 MachProcess::GetDispatchQueueT (nub_thread_t tid)
525 return m_thread_list.GetDispatchQueueT (tid);
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)
531 return m_thread_list.GetTSDAddressForThread (tid, plo_pthread_tsd_base_address_offset, plo_pthread_tsd_base_offset, plo_pthread_tsd_entry_size);
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.
537 // Returns false if there was an error in reading this mach-o file header/load commands.
540 MachProcess::GetMachOInformationFromMemory (nub_addr_t mach_o_header_addr, int wordsize, struct mach_o_information &inf)
542 uint64_t load_cmds_p;
545 struct mach_header header;
546 if (ReadMemory (mach_o_header_addr, sizeof (struct mach_header), &header) != sizeof (struct mach_header))
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;
562 struct mach_header_64 header;
563 if (ReadMemory (mach_o_header_addr, sizeof (struct mach_header_64), &header) != sizeof (struct mach_header_64))
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;
577 for (uint32_t j = 0; j < inf.mach_header.ncmds; j++)
579 struct load_command lc;
580 if (ReadMemory (load_cmds_p, sizeof (struct load_command), &lc) != sizeof (struct load_command))
584 if (lc.cmd == LC_SEGMENT)
586 struct segment_command seg;
587 if (ReadMemory (load_cmds_p, sizeof (struct segment_command), &seg) != sizeof (struct segment_command))
591 struct mach_o_segment this_seg;
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);
606 if (lc.cmd == LC_SEGMENT_64)
608 struct segment_command_64 seg;
609 if (ReadMemory (load_cmds_p, sizeof (struct segment_command_64), &seg) != sizeof (struct segment_command_64))
613 struct mach_o_segment this_seg;
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);
628 if (lc.cmd == LC_UUID)
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);
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;
638 #if defined(LC_VERSION_MIN_WATCHOS)
639 lc_cmd_known |= lc.cmd == LC_VERSION_MIN_WATCHOS;
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))
650 case LC_VERSION_MIN_IPHONEOS:
651 inf.min_version_os_name = "iphoneos";
653 case LC_VERSION_MIN_MACOSX:
654 inf.min_version_os_name = "macosx";
656 #if defined(LC_VERSION_MIN_TVOS)
657 case LC_VERSION_MIN_TVOS:
658 inf.min_version_os_name = "tvos";
661 #if defined(LC_VERSION_MIN_WATCHOS)
662 case LC_VERSION_MIN_WATCHOS:
663 inf.min_version_os_name = "watchos";
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);
678 inf.min_version_os_version += ".";
679 inf.min_version_os_version += std::to_string(zz);
682 load_cmds_p += lc.cmdsize;
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)
693 JSONGenerator::ArraySP image_infos_array_sp (new JSONGenerator::Array());
695 const size_t image_count = image_infos.size();
697 for (size_t i = 0; i < image_count; i++)
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);
704 uuid_string_t uuidstr;
705 uuid_unparse_upper (image_infos[i].macho_info.uuid, uuidstr);
706 image_info_dict_sp->AddStringItem ("uuid", uuidstr);
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)
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);
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);
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);
727 JSONGenerator::ArraySP segments_sp (new JSONGenerator::Array());
728 for (size_t j = 0; j < image_infos[i].macho_info.segments.size(); j++)
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);
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);
744 image_info_dict_sp->AddItem ("segments", segments_sp);
746 image_infos_array_sp->AddItem (image_info_dict_sp);
749 JSONGenerator::DictionarySP reply_sp (new JSONGenerator::Dictionary());;
750 reply_sp->AddItem ("images", image_infos_array_sp);
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}
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)
763 JSONGenerator::DictionarySP reply_sp;
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)
770 uint32_t pointer_size = 4;
771 if (processInfo.kp_proc.p_flag & P_LP64)
774 std::vector<struct binary_image_information> image_infos;
775 size_t image_infos_size = image_count * 3 * pointer_size;
777 uint8_t *image_info_buf = (uint8_t *) malloc (image_infos_size);
778 if (image_info_buf == NULL)
782 if (ReadMemory (image_list_address, image_infos_size, image_info_buf) != image_infos_size)
788 //// First the image_infos array with (load addr, pathname, mod date) tuples
791 for (size_t i = 0; i < image_count; i++)
793 struct binary_image_information info;
794 nub_addr_t pathname_address;
795 if (pointer_size == 4)
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;
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;
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)
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++)
831 if (strbuf[i] == '\0')
833 still_reading = false;
838 uuid_clear (info.macho_info.uuid);
839 image_infos.push_back (info);
841 if (image_infos.size() == 0)
846 free (image_info_buf);
848 //// Second, read the mach header / load commands for all the dylibs
850 for (size_t i = 0; i < image_count; i++)
852 if (!GetMachOInformationFromMemory (image_infos[i].load_address, pointer_size, image_infos[i].macho_info))
859 //// Third, format all of the above in the JSONGenerator object.
862 return FormatDynamicLibrariesIntoJSON (image_infos);
868 // From dyld SPI header dyld_process_info.h
869 typedef void* dyld_process_info;
870 struct dyld_process_cache_info
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
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.
884 MachProcess::GetAllLoadedBinariesViaDYLDSPI (std::vector<struct binary_image_information> &image_infos)
886 kern_return_t kern_ret;
887 if (m_dyld_process_info_create)
889 dyld_process_info info = m_dyld_process_info_create (m_task.TaskPort(), 0, &kern_ret);
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);
899 m_dyld_process_info_release (info);
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)
909 JSONGenerator::DictionarySP reply_sp;
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)
916 uint32_t pointer_size = 4;
917 if (processInfo.kp_proc.p_flag & P_LP64)
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++)
925 GetMachOInformationFromMemory (image_infos[i].load_address, pointer_size, image_infos[i].macho_info);
927 return FormatDynamicLibrariesIntoJSON (image_infos);
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)
937 JSONGenerator::DictionarySP reply_sp;
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)
944 uint32_t pointer_size = 4;
945 if (processInfo.kp_proc.p_flag & P_LP64)
948 std::vector<struct binary_image_information> all_image_infos;
949 GetAllLoadedBinariesViaDYLDSPI (all_image_infos);
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++)
956 for (size_t j = 0; j < all_image_infos_count; j++)
958 if (all_image_infos[j].load_address == macho_addresses[i])
960 image_infos.push_back (all_image_infos[j]);
965 const size_t image_infos_count = image_infos.size();
966 for (size_t i = 0; i < image_infos_count; i++)
968 GetMachOInformationFromMemory (image_infos[i].load_address, pointer_size, image_infos[i].macho_info);
970 return FormatDynamicLibrariesIntoJSON (image_infos);
975 // From dyld's internal podyld_process_info.h:
977 JSONGenerator::ObjectSP
978 MachProcess::GetSharedCacheInfo (nub_process_t pid)
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)
984 dyld_process_info info = m_dyld_process_info_create (m_task.TaskPort(), 0, &kern_ret);
987 struct dyld_process_cache_info shared_cache_info;
988 m_dyld_process_info_get_cache (info, &shared_cache_info);
990 reply_sp->AddIntegerItem ("shared_cache_base_address", shared_cache_info.cacheBaseAddress);
992 uuid_string_t uuidstr;
993 uuid_unparse_upper (shared_cache_info.cacheUUID, uuidstr);
994 reply_sp->AddStringItem ("shared_cache_uuid", uuidstr);
996 reply_sp->AddBooleanItem ("no_shared_cache", shared_cache_info.noCache);
997 reply_sp->AddBooleanItem ("shared_cache_private_cache", shared_cache_info.privateCache);
999 m_dyld_process_info_release (info);
1006 MachProcess::GetCurrentThread ()
1008 return m_thread_list.CurrentThreadID();
1012 MachProcess::GetCurrentThreadMachPort ()
1014 return m_thread_list.GetMachPortNumberByThreadID(m_thread_list.CurrentThreadID());
1018 MachProcess::SetCurrentThread(nub_thread_t tid)
1020 return m_thread_list.SetCurrentThread(tid);
1024 MachProcess::GetThreadStoppedReason(nub_thread_t tid, struct DNBThreadStopInfo *stop_info)
1026 if (m_thread_list.GetThreadStoppedReason(tid, stop_info))
1029 stop_info->reason = eStopTypeExec;
1036 MachProcess::DumpThreadStoppedReason(nub_thread_t tid) const
1038 return m_thread_list.DumpThreadStoppedReason(tid);
1042 MachProcess::GetThreadInfo(nub_thread_t tid) const
1044 return m_thread_list.GetThreadInfo(tid);
1048 MachProcess::GetCPUType ()
1050 if (m_cpu_type == 0 && m_pid != 0)
1051 m_cpu_type = MachProcess::GetCPUTypeForLocalProcess (m_pid);
1055 const DNBRegisterSetInfo *
1056 MachProcess::GetRegisterSetInfo (nub_thread_t tid, nub_size_t *num_reg_sets) const
1058 MachThreadSP thread_sp (m_thread_list.GetThreadByID (tid));
1061 DNBArchProtocol *arch = thread_sp->GetArchProtocol();
1063 return arch->GetRegisterSetInfo (num_reg_sets);
1070 MachProcess::GetRegisterValue ( nub_thread_t tid, uint32_t set, uint32_t reg, DNBRegisterValue *value ) const
1072 return m_thread_list.GetRegisterValue(tid, set, reg, value);
1076 MachProcess::SetRegisterValue ( nub_thread_t tid, uint32_t set, uint32_t reg, const DNBRegisterValue *value ) const
1078 return m_thread_list.SetRegisterValue(tid, set, reg, value);
1082 MachProcess::SetState(nub_state_t new_state)
1084 // If any other threads access this we will need a mutex for it
1085 uint32_t event_mask = 0;
1087 // Scope for mutex locker
1089 PTHREAD_MUTEX_LOCKER(locker, m_state_mutex);
1090 const nub_state_t old_state = m_state;
1092 if (old_state == eStateExited)
1094 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::SetState(%s) ignoring new state since current state is exited", DNBStateAsString(new_state));
1096 else if (old_state == new_state)
1098 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::SetState(%s) ignoring redundant state change...", DNBStateAsString(new_state));
1102 if (NUB_STATE_IS_STOPPED(new_state))
1103 event_mask = eEventProcessStoppedStateChanged;
1105 event_mask = eEventProcessRunningStateChanged;
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);
1109 m_state = new_state;
1110 if (new_state == eStateStopped)
1115 if (event_mask != 0)
1117 m_events.SetEvents (event_mask);
1118 m_private_events.SetEvents (event_mask);
1119 if (event_mask == eEventProcessStoppedStateChanged)
1120 m_private_events.ResetEvents (eEventProcessRunningStateChanged);
1122 m_private_events.ResetEvents (eEventProcessStoppedStateChanged);
1124 // Wait for the event bit to reset if a reset ACK is requested
1125 m_events.WaitForResetAck(event_mask);
1131 MachProcess::Clear(bool detaching)
1133 // Clear any cached thread list while the pid and task are still valid
1136 // Now clear out all member variables
1137 m_pid = INVALID_NUB_PROCESS;
1139 CloseChildFileDescriptors();
1143 SetState(eStateUnloaded);
1144 m_flags = eMachProcessFlagsNone;
1146 m_thread_list.Clear();
1148 PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
1149 m_exception_messages.clear();
1151 m_activities.Clear();
1152 if (m_profile_thread)
1154 pthread_join(m_profile_thread, NULL);
1155 m_profile_thread = NULL;
1161 MachProcess::StartSTDIOThread()
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;
1169 MachProcess::SetEnableAsyncProfiling(bool enable, uint64_t interval_usec, DNBProfileDataScanType scan_type)
1171 m_profile_enabled = enable;
1172 m_profile_interval_usec = static_cast<useconds_t>(interval_usec);
1173 m_profile_scan_type = scan_type;
1175 if (m_profile_enabled && (m_profile_thread == NULL))
1177 StartProfileThread();
1179 else if (!m_profile_enabled && m_profile_thread)
1181 pthread_join(m_profile_thread, NULL);
1182 m_profile_thread = NULL;
1187 MachProcess::StartProfileThread()
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;
1196 MachProcess::LookupSymbol(const char *name, const char *shlib)
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;
1204 MachProcess::Resume (const DNBThreadResumeActions& thread_actions)
1206 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Resume ()");
1207 nub_state_t state = GetState();
1209 if (CanResume(state))
1211 m_thread_actions = thread_actions;
1215 else if (state == eStateRunning)
1217 DNBLog("Resume() - task 0x%x is already running, ignoring...", m_task.TaskPort());
1220 DNBLog("Resume() - task 0x%x has state %s, can't continue...", m_task.TaskPort(), DNBStateAsString(state));
1225 MachProcess::Kill (const struct timespec *timeout_abstime)
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));
1231 DNBLog ("Sending ptrace PT_KILL to terminate inferior process.");
1232 ::ptrace (PT_KILL, m_pid, 0, 0);
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);
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)
1248 if (GetState() == eStateExited)
1250 usleep(reap_interval_usec);
1252 DNBLog ("Waited %u ms for process to be reaped (state = %s)", reap_time_elapsed/1000, DNBStateAsString(GetState()));
1257 MachProcess::Interrupt()
1259 nub_state_t state = GetState();
1260 if (IsRunning(state))
1262 if (m_sent_interrupt_signo == 0)
1264 m_sent_interrupt_signo = SIGSTOP;
1265 if (Signal (m_sent_interrupt_signo))
1267 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Interrupt() - sent %i signal to interrupt process", m_sent_interrupt_signo);
1272 m_sent_interrupt_signo = 0;
1273 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Interrupt() - failed to send %i signal to interrupt process", m_sent_interrupt_signo);
1278 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Interrupt() - previously sent an interrupt signal %i that hasn't been received yet, interrupt aborted", m_sent_interrupt_signo);
1283 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Interrupt() - process already stopped, no interrupt sent");
1289 MachProcess::Signal (int signal, const struct timespec *timeout_abstime)
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)
1295 // If we were running and we have a timeout, wait for the signal to stop
1296 if (IsRunning(state) && timeout_abstime)
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);
1301 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Signal (signal = %d, timeout = %p) state = %s", signal, timeout_abstime, DNBStateAsString(state));
1302 return !IsRunning (state);
1304 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Signal (signal = %d, timeout = %p) not waiting...", signal, timeout_abstime);
1307 DNBError err(errno, DNBError::POSIX);
1308 err.LogThreadedIfError("kill (pid = %d, signo = %i)", ProcessID(), signal);
1314 MachProcess::SendEvent (const char *event, DNBError &send_err)
1316 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::SendEvent (event = %s) to pid: %d", event, m_pid);
1317 if (m_pid == INVALID_NUB_PROCESS)
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);
1327 MachProcess::DoSIGSTOP (bool clear_bps_and_wps, bool allow_running, uint32_t *thread_idx_ptr)
1329 nub_state_t state = GetState();
1330 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::DoSIGSTOP() state = %s", DNBStateAsString (state));
1332 if (!IsRunning(state))
1334 if (clear_bps_and_wps)
1336 DisableAllBreakpoints (true);
1337 DisableAllWatchpoints (true);
1338 clear_bps_and_wps = false;
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);
1345 *thread_idx_ptr = thread_idx;
1346 if (thread_idx != UINT32_MAX)
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));
1353 m_thread_actions = DNBThreadResumeActions (eStateRunning, 0);
1355 m_thread_actions = DNBThreadResumeActions (eStateSuspended, 0);
1359 // Reset the event that says we were indeed running
1360 m_events.ResetEvents(eEventProcessRunningStateChanged);
1364 // We need to be stopped in order to be able to detach, so we need
1365 // to send ourselves a SIGSTOP
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)
1373 DisableAllBreakpoints (true);
1374 DisableAllWatchpoints (true);
1375 //clear_bps_and_wps = false;
1377 uint32_t thread_idx = m_thread_list.GetThreadIndexForThreadStoppedWithSignal (SIGSTOP);
1379 *thread_idx_ptr = thread_idx;
1384 MachProcess::Detach()
1386 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::Detach()");
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));
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;
1401 m_thread_actions.Append (thread_action);
1402 m_thread_actions.SetDefaultThreadActionIfNeeded (eStateRunning, 0);
1404 PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex);
1406 ReplyToAllExceptions ();
1410 m_task.ShutDownExcecptionThread();
1412 // Detach from our process
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);
1423 // NULL our task out as we have already retored all exception ports
1426 // Clear out any notion of the process we once were
1427 const bool detaching = true;
1430 SetState(eStateDetached);
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 //----------------------------------------------------------------------
1443 MachProcess::ReadMemory (nub_addr_t addr, nub_size_t size, void *buf)
1445 // We need to remove any current software traps (enabled software
1446 // breakpoints) that we may have placed in our tasks memory.
1448 // First just read the memory as is
1449 nub_size_t bytes_read = m_task.ReadMemory(addr, size, buf);
1451 // Then place any opcodes that fall into this range back into the buffer
1452 // before we return this to callers.
1454 m_breakpoints.RemoveTrapsFromBuffer (addr, bytes_read, buf);
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
1466 //----------------------------------------------------------------------
1468 MachProcess::WriteMemory (nub_addr_t addr, nub_size_t size, const void *buf)
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.
1474 std::vector<DNBBreakpoint *> bps;
1476 const size_t num_bps = m_breakpoints.FindBreakpointsThatOverlapRange(addr, size, bps);
1478 return m_task.WriteMemory(addr, size, buf);
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;
1486 for (size_t i=0; i<num_bps; ++i)
1488 DNBBreakpoint *bp = bps[i];
1490 const bool intersects = bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset);
1491 UNUSED_IF_ASSERT_DISABLED(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());
1497 // Check for bytes before this breakpoint
1498 const nub_addr_t curr_addr = addr + bytes_written;
1499 if (intersect_addr > curr_addr)
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)
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.
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;
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);
1525 return bytes_written;
1529 MachProcess::ReplyToAllExceptions ()
1531 PTHREAD_MUTEX_LOCKER(locker, m_exception_messages_mutex);
1532 if (m_exception_messages.empty() == false)
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)
1539 DNBLogThreadedIf(LOG_EXCEPTIONS, "Replying to exception %u...", (uint32_t)std::distance(begin, pos));
1540 int thread_reply_signal = 0;
1542 nub_thread_t tid = m_thread_list.GetThreadIDByMachPortNumber (pos->state.thread_port);
1543 const DNBThreadResumeAction *action = NULL;
1544 if (tid != INVALID_NUB_THREAD)
1546 action = m_thread_actions.GetActionForThread (tid, false);
1551 thread_reply_signal = action->signal;
1552 if (thread_reply_signal)
1553 m_thread_actions.SetSignalHandledForThread (tid);
1556 DNBError err (pos->Reply(this, thread_reply_signal));
1557 if (DNBLogCheckLogBit(LOG_EXCEPTIONS))
1558 err.LogThreadedIfError("Error replying to exception");
1561 // Erase all exception message as we should have used and replied
1562 // to them all already.
1563 m_exception_messages.clear();
1567 MachProcess::PrivateResume ()
1569 PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex);
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);
1575 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::PrivateResume() - task 0x%x resuming...", m_task.TaskPort());
1577 ReplyToAllExceptions ();
1578 // bool stepOverBreakInstruction = step;
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);
1585 // Set our state accordingly
1586 if (m_thread_actions.NumActionsWithState(eStateStepping))
1587 SetState (eStateStepping);
1589 SetState (eStateRunning);
1591 // Now resume our task.
1596 MachProcess::CreateBreakpoint(nub_addr_t addr, nub_size_t length, bool hardware)
1598 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::CreateBreakpoint ( addr = 0x%8.8llx, length = %llu, hardware = %i)", (uint64_t)addr, (uint64_t)length, hardware);
1600 DNBBreakpoint *bp = m_breakpoints.FindByAddress(addr);
1604 bp = m_breakpoints.Add(addr, length, hardware);
1606 if (EnableBreakpoint(addr))
1608 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::CreateBreakpoint ( addr = 0x%8.8llx, length = %llu) => %p", (uint64_t)addr, (uint64_t)length, bp);
1611 else if (bp->Release() == 0)
1613 m_breakpoints.Remove(addr);
1615 // We failed to enable the breakpoint
1620 MachProcess::CreateWatchpoint(nub_addr_t addr, nub_size_t length, uint32_t watch_flags, bool hardware)
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);
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
1630 wp = m_watchpoints.Add(addr, length, hardware);
1631 wp->SetIsWatchpoint(watch_flags);
1633 if (EnableWatchpoint(addr))
1635 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = 0x%8.8llx, length = %llu) => %p", (uint64_t)addr, (uint64_t)length, wp);
1640 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::CreateWatchpoint ( addr = 0x%8.8llx, length = %llu) => FAILED", (uint64_t)addr, (uint64_t)length);
1641 m_watchpoints.Remove(addr);
1643 // We failed to enable the watchpoint
1648 MachProcess::DisableAllBreakpoints (bool remove)
1650 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::%s (remove = %d )", __FUNCTION__, remove);
1652 m_breakpoints.DisableAllBreakpoints (this);
1655 m_breakpoints.RemoveDisabled();
1659 MachProcess::DisableAllWatchpoints(bool remove)
1661 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::%s (remove = %d )", __FUNCTION__, remove);
1663 m_watchpoints.DisableAllWatchpoints(this);
1666 m_watchpoints.RemoveDisabled();
1670 MachProcess::DisableBreakpoint(nub_addr_t addr, bool remove)
1672 DNBBreakpoint *bp = m_breakpoints.FindByAddress(addr);
1675 // After "exec" we might end up with a bunch of breakpoints that were disabled
1676 // manually, just ignore them
1677 if (!bp->IsEnabled())
1679 // Breakpoint might have been disabled by an exec
1680 if (remove && bp->Release() == 0)
1682 m_thread_list.NotifyBreakpointChanged(bp);
1683 m_breakpoints.Remove(addr);
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)
1693 DNBLogThreadedIf(LOG_BREAKPOINTS | LOG_VERBOSE, "MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d )", (uint64_t)addr, remove);
1695 if (bp->IsHardware())
1697 bool hw_disable_result = m_thread_list.DisableHardwareBreakpoint (bp);
1699 if (hw_disable_result == true)
1701 bp->SetEnabled(false);
1702 // Let the thread list know that a breakpoint has been modified
1705 m_thread_list.NotifyBreakpointChanged(bp);
1706 m_breakpoints.Remove(addr);
1708 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d ) (hardware) => success", (uint64_t)addr, remove);
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)
1720 // Clear a software breakpoint instruction
1721 uint8_t curr_break_op[break_op_size];
1722 bool break_op_found = false;
1724 // Read the breakpoint opcode
1725 if (m_task.ReadMemory(addr, break_op_size, curr_break_op) == break_op_size)
1727 bool verify = false;
1728 if (bp->IsEnabled())
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)
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)
1742 DNBLogError("MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d ) memory write failed when restoring original opcode", (uint64_t)addr, remove);
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
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
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)
1765 // compare the memory we just read with the original opcode
1766 if (memcmp(bp->SavedOpcodeBytes(), verify_opcode, break_op_size) == 0)
1769 bp->SetEnabled(false);
1770 // Let the thread list know that a breakpoint has been modified
1771 if (remove && bp->Release() == 0)
1773 m_thread_list.NotifyBreakpointChanged(bp);
1774 m_breakpoints.Remove(addr);
1776 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d ) => success", (uint64_t)addr, remove);
1782 DNBLogError("MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d ) : failed to restore original opcode", (uint64_t)addr, remove);
1784 DNBLogError("MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d ) : opcode changed", (uint64_t)addr, remove);
1789 DNBLogWarning("MachProcess::DisableBreakpoint: unable to disable breakpoint 0x%8.8llx", (uint64_t)addr);
1795 DNBLogWarning("MachProcess::DisableBreakpoint: unable to read memory at 0x%8.8llx", (uint64_t)addr);
1801 DNBLogError("MachProcess::DisableBreakpoint ( addr = 0x%8.8llx, remove = %d ) invalid breakpoint address", (uint64_t)addr, remove);
1807 MachProcess::DisableWatchpoint(nub_addr_t addr, bool remove)
1809 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::%s(addr = 0x%8.8llx, remove = %d)", __FUNCTION__, (uint64_t)addr, remove);
1810 DNBBreakpoint *wp = m_watchpoints.FindByAddress(addr);
1813 // If we have multiple references to a watchpoint, removing the watchpoint shouldn't clear it
1814 if (remove && wp->Release() > 0)
1817 nub_addr_t addr = wp->Address();
1818 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::DisableWatchpoint ( addr = 0x%8.8llx, remove = %d )", (uint64_t)addr, remove);
1820 if (wp->IsHardware())
1822 bool hw_disable_result = m_thread_list.DisableHardwareWatchpoint (wp);
1824 if (hw_disable_result == true)
1826 wp->SetEnabled(false);
1828 m_watchpoints.Remove(addr);
1829 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::Disablewatchpoint ( addr = 0x%8.8llx, remove = %d ) (hardware) => success", (uint64_t)addr, remove);
1834 // TODO: clear software watchpoints if we implement them
1838 DNBLogError("MachProcess::DisableWatchpoint ( addr = 0x%8.8llx, remove = %d ) invalid watchpoint ID", (uint64_t)addr, remove);
1845 MachProcess::GetNumSupportedHardwareWatchpoints () const
1847 return m_thread_list.NumSupportedHardwareWatchpoints();
1851 MachProcess::EnableBreakpoint(nub_addr_t addr)
1853 DNBLogThreadedIf(LOG_BREAKPOINTS, "MachProcess::EnableBreakpoint ( addr = 0x%8.8llx )", (uint64_t)addr);
1854 DNBBreakpoint *bp = m_breakpoints.FindByAddress(addr);
1857 if (bp->IsEnabled())
1859 DNBLogWarning("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): breakpoint already enabled.", (uint64_t)addr);
1864 if (bp->HardwarePreferred())
1866 bp->SetHardwareIndex(m_thread_list.EnableHardwareBreakpoint(bp));
1867 if (bp->IsHardware())
1869 bp->SetEnabled(true);
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)
1879 // Save the original opcode by reading it
1880 if (m_task.ReadMemory(addr, break_op_size, bp->SavedOpcodeBytes()) == break_op_size)
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)
1885 uint8_t verify_break_op[4];
1886 if (m_task.ReadMemory(addr, break_op_size, verify_break_op) == break_op_size)
1888 if (memcmp(break_op, verify_break_op, break_op_size) == 0)
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);
1898 DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): breakpoint opcode verification failed.", (uint64_t)addr);
1903 DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): unable to read memory to verify breakpoint opcode.", (uint64_t)addr);
1908 DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): unable to write breakpoint opcode to memory.", (uint64_t)addr);
1913 DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ): unable to read memory at breakpoint address.", (uint64_t)addr);
1918 DNBLogError("MachProcess::EnableBreakpoint ( addr = 0x%8.8llx ) no software breakpoint opcode for current architecture.", (uint64_t)addr);
1926 MachProcess::EnableWatchpoint(nub_addr_t addr)
1928 DNBLogThreadedIf(LOG_WATCHPOINTS, "MachProcess::EnableWatchpoint(addr = 0x%8.8llx)", (uint64_t)addr);
1929 DNBBreakpoint *wp = m_watchpoints.FindByAddress(addr);
1932 nub_addr_t addr = wp->Address();
1933 if (wp->IsEnabled())
1935 DNBLogWarning("MachProcess::EnableWatchpoint(addr = 0x%8.8llx): watchpoint already enabled.", (uint64_t)addr);
1940 // Currently only try and set hardware watchpoints.
1941 wp->SetHardwareIndex(m_thread_list.EnableHardwareWatchpoint(wp));
1942 if (wp->IsHardware())
1944 wp->SetEnabled(true);
1947 // TODO: Add software watchpoints by doing page protection tricks.
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.
1957 MachProcess::ExceptionMessageReceived (const MachException::Message& exceptionMessage)
1959 PTHREAD_MUTEX_LOCKER (locker, m_exception_messages_mutex);
1961 if (m_exception_messages.empty())
1964 DNBLogThreadedIf(LOG_EXCEPTIONS, "MachProcess::ExceptionMessageReceived ( )");
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);
1972 MachProcess::ExceptionMessageBundleComplete()
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())
1981 // First check for any SIGTRAP and make sure we didn't exec
1982 const task_t task = m_task.TaskPort();
1986 bool received_interrupt = false;
1987 uint32_t num_task_exceptions = 0;
1988 for (i=0; i<m_exception_messages.size(); ++i)
1990 if (m_exception_messages[i].state.task_port == task)
1992 ++num_task_exceptions;
1993 const int signo = m_exception_messages[i].state.SoftSignal();
1994 if (signo == SIGTRAP)
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)
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)
2006 if (info_array_count == 0)
2009 // Force the task port to update itself in case the task port changed after exec
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);
2019 DNBLog ("error: failed to read all_image_infos.infoArrayCount from 0x%8.8llx", (uint64_t)info_array_count_addr);
2024 else if (m_sent_interrupt_signo != 0 && signo == m_sent_interrupt_signo)
2026 received_interrupt = true;
2033 cpu_type_t process_cpu_type = MachProcess::GetCPUTypeForLocalProcess (m_pid);
2034 if (m_cpu_type != process_cpu_type)
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);
2040 m_thread_list.Clear();
2041 m_activities.Clear();
2042 m_breakpoints.DisableAll();
2045 if (m_sent_interrupt_signo != 0)
2047 if (received_interrupt)
2049 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::ExceptionMessageBundleComplete(): process successfully interrupted with signal %i", m_sent_interrupt_signo);
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)
2060 // Only auto_resume if we stopped with _only_ the interrupt signal
2061 if (num_task_exceptions == 1)
2064 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::ExceptionMessageBundleComplete(): auto resuming due to unhandled interrupt signal %i", m_auto_resume_signo);
2066 m_auto_resume_signo = 0;
2071 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::ExceptionMessageBundleComplete(): didn't get signal %i after MachProcess::Interrupt()",
2072 m_sent_interrupt_signo);
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();
2082 // Let each thread know of any exceptions
2083 for (i=0; i<m_exception_messages.size(); ++i)
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();
2093 if (DNBLogCheckLogBit(LOG_THREAD))
2094 m_thread_list.Dump();
2096 bool step_more = false;
2097 if (m_thread_list.ShouldStop(step_more) && auto_resume == false)
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);
2110 // Resume without checking our current state.
2116 DNBLogThreadedIf(LOG_EXCEPTIONS, "%s empty exception messages bundle (%llu exceptions).", __PRETTY_FUNCTION__, (uint64_t)m_exception_messages.size());
2118 return m_task.TaskPort();
2122 MachProcess::CopyImageInfos ( struct DNBExecutableImageInfo **image_infos, bool only_changed)
2124 if (m_image_infos_callback != NULL)
2125 return m_image_infos_callback(ProcessID(), image_infos, only_changed, m_image_infos_baton);
2130 MachProcess::SharedLibrariesUpdated ( )
2132 uint32_t event_bits = eEventSharedLibsStateChange;
2133 // Set the shared library event bit to let clients know of shared library
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);
2141 MachProcess::SetExitInfo (const char *info)
2143 if (info && info[0])
2145 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s(\"%s\")", __FUNCTION__, info);
2146 m_exit_info.assign(info);
2150 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s(NULL)", __FUNCTION__);
2151 m_exit_info.clear();
2156 MachProcess::AppendSTDOUT (char* s, size_t len)
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);
2163 // Wait for the event bit to reset if a reset ACK is requested
2164 m_events.WaitForResetAck(eEventStdioAvailable);
2168 MachProcess::GetAvailableSTDOUT (char *buf, size_t buf_size)
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)
2175 if (bytes_available > buf_size)
2177 memcpy(buf, m_stdout_data.data(), buf_size);
2178 m_stdout_data.erase(0, buf_size);
2179 bytes_available = buf_size;
2183 memcpy(buf, m_stdout_data.data(), bytes_available);
2184 m_stdout_data.clear();
2187 return bytes_available;
2191 MachProcess::GetDYLDAllImageInfosAddress ()
2194 return m_task.GetDYLDAllImageInfosAddress(err);
2198 MachProcess::GetAvailableSTDERR (char *buf, size_t buf_size)
2204 MachProcess::STDIOThread(void *arg)
2206 MachProcess *proc = (MachProcess*) arg;
2207 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( arg = %p ) thread starting...", __FUNCTION__, arg);
2209 #if defined (__APPLE__)
2210 pthread_setname_np ("stdio monitoring thread");
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.
2229 int stdout_fd = proc->GetStdoutFileDescriptor();
2230 int stderr_fd = proc->GetStderrFileDescriptor();
2231 if (stdout_fd == stderr_fd)
2234 while (stdout_fd >= 0 || stderr_fd >= 0)
2236 ::pthread_testcancel ();
2239 FD_ZERO (&read_fds);
2241 FD_SET (stdout_fd, &read_fds);
2243 FD_SET (stderr_fd, &read_fds);
2244 int nfds = std::max<int>(stdout_fd, stderr_fd) + 1;
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);
2249 if (num_set_fds < 0)
2251 int select_errno = errno;
2252 if (DNBLogCheckLogBit(LOG_PROCESS))
2254 err.SetError (select_errno, DNBError::POSIX);
2255 err.LogThreadedIfError("select (nfds, &read_fds, NULL, NULL, NULL) => %d", num_set_fds);
2258 switch (select_errno)
2260 case EAGAIN: // The kernel was (perhaps temporarily) unable to allocate the requested number of file descriptors, or we have non-blocking IO
2262 case EBADF: // One of the descriptor sets specified an invalid descriptor.
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
2271 else if (num_set_fds == 0)
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))
2283 bytes_read = ::read (stdout_fd, s, sizeof(s)-1);
2286 int read_errno = errno;
2287 DNBLogThreadedIf(LOG_PROCESS, "read (stdout_fd, ) => %zd errno: %d (%s)", bytes_read, read_errno, strerror(read_errno));
2289 else if (bytes_read == 0)
2292 DNBLogThreadedIf(LOG_PROCESS, "read (stdout_fd, ) => %zd (reached EOF for child STDOUT)", bytes_read);
2295 else if (bytes_read > 0)
2297 proc->AppendSTDOUT(s, bytes_read);
2300 } while (bytes_read > 0);
2303 if (stderr_fd >= 0 && FD_ISSET (stderr_fd, &read_fds))
2307 bytes_read = ::read (stderr_fd, s, sizeof(s)-1);
2310 int read_errno = errno;
2311 DNBLogThreadedIf(LOG_PROCESS, "read (stderr_fd, ) => %zd errno: %d (%s)", bytes_read, read_errno, strerror(read_errno));
2313 else if (bytes_read == 0)
2316 DNBLogThreadedIf(LOG_PROCESS, "read (stderr_fd, ) => %zd (reached EOF for child STDERR)", bytes_read);
2319 else if (bytes_read > 0)
2321 proc->AppendSTDOUT(s, bytes_read);
2324 } while (bytes_read > 0);
2328 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s (%p): thread exiting...", __FUNCTION__, arg);
2334 MachProcess::SignalAsyncProfileData (const char *info)
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);
2341 // Wait for the event bit to reset if a reset ACK is requested
2342 m_events.WaitForResetAck(eEventProfileDataAvailable);
2347 MachProcess::GetAsyncProfileData (char *buf, size_t buf_size)
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())
2354 size_t bytes_available = m_profile_data.front().size();
2355 if (bytes_available > 0)
2357 if (bytes_available > buf_size)
2359 memcpy(buf, m_profile_data.front().data(), buf_size);
2360 m_profile_data.front().erase(0, buf_size);
2361 bytes_available = buf_size;
2365 memcpy(buf, m_profile_data.front().data(), bytes_available);
2366 m_profile_data.erase(m_profile_data.begin());
2369 return bytes_available;
2374 MachProcess::ProfileThread(void *arg)
2376 MachProcess *proc = (MachProcess*) arg;
2377 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::%s ( arg = %p ) thread starting...", __FUNCTION__, arg);
2379 #if defined (__APPLE__)
2380 pthread_setname_np ("performance profiling thread");
2383 while (proc->IsProfilingEnabled())
2385 nub_state_t state = proc->GetState();
2386 if (state == eStateRunning)
2388 std::string data = proc->Task().GetProfileData(proc->GetProfileScanType());
2391 proc->SignalAsyncProfileData(data.c_str());
2394 else if ((state == eStateUnloaded) || (state == eStateDetached) || (state == eStateUnloaded))
2396 // Done. Get out of this thread.
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());
2408 MachProcess::AttachForDebug (pid_t pid, char *err_str, size_t err_len)
2410 // Clear out and clean up from any current state
2415 // Make sure the process exists...
2416 if (::getpgid (pid) < 0)
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;
2424 SetState(eStateAttaching);
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;
2431 #if defined (WITH_FBS)
2432 if (!found_app_flavor && IsFBSProcess (pid))
2434 found_app_flavor = true;
2435 m_flags |= eMachProcessFlagsUsingFBS;
2437 #elif defined (WITH_BKS)
2438 if (!found_app_flavor && IsBKSProcess (pid))
2440 found_app_flavor = true;
2441 m_flags |= eMachProcessFlagsUsingBKS;
2443 #elif defined (WITH_SPRINGBOARD)
2444 if (IsSBProcess(pid))
2445 m_flags |= eMachProcessFlagsUsingSBS;
2447 if (!m_task.StartExceptionThread(err))
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;
2457 if (::ptrace (PT_ATTACHEXC, pid, 0, 0))
2458 err.SetError(errno);
2464 m_flags |= eMachProcessFlagsAttached;
2465 // Sleep a bit to let the exception get received and set our process status
2468 DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", pid);
2473 ::snprintf (err_str, err_len, "%s", err.AsString());
2474 DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", pid);
2477 return INVALID_NUB_PROCESS;
2480 Genealogy::ThreadActivitySP
2481 MachProcess::GetGenealogyInfoForThread (nub_thread_t tid, bool &timed_out)
2483 return m_activities.GetGenealogyInfoForThread (m_pid, tid, m_thread_list, m_task.TaskPort(), timed_out);
2486 Genealogy::ProcessExecutableInfoSP
2487 MachProcess::GetGenealogyImageInfo (size_t idx)
2489 return m_activities.GetProcessExecutableInfosAtIndex (idx);
2493 MachProcess::GetOSVersionNumbers (uint64_t *major, uint64_t *minor, uint64_t *patch)
2495 #if defined (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101000)
2498 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2500 NSOperatingSystemVersion vers = [[NSProcessInfo processInfo] operatingSystemVersion];
2502 *major = vers.majorVersion;
2504 *minor = vers.minorVersion;
2506 *patch = vers.patchVersion;
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.
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.
2524 MachProcess::PrepareForAttach (const char *path, nub_launch_flavor_t launch_flavor, bool waitfor, DNBError &attach_err)
2526 #if defined (WITH_SPRINGBOARD) || defined (WITH_BKS) || defined (WITH_FBS)
2527 // Tell SpringBoard to halt the next launch of this application on startup.
2532 const char *app_ext = strstr(path, ".app");
2533 const bool is_app = app_ext != NULL && (app_ext[4] == '\0' || app_ext[4] == '/');
2536 DNBLogThreadedIf(LOG_PROCESS, "MachProcess::PrepareForAttach(): path '%s' doesn't contain .app, "
2537 "we can't tell springboard to wait for launch...",
2542 #if defined (WITH_FBS)
2543 if (launch_flavor == eLaunchFlavorDefault)
2544 launch_flavor = eLaunchFlavorFBS;
2545 if (launch_flavor != eLaunchFlavorFBS)
2547 #elif defined (WITH_BKS)
2548 if (launch_flavor == eLaunchFlavorDefault)
2549 launch_flavor = eLaunchFlavorBKS;
2550 if (launch_flavor != eLaunchFlavorBKS)
2552 #elif defined (WITH_SPRINGBOARD)
2553 if (launch_flavor == eLaunchFlavorDefault)
2554 launch_flavor = eLaunchFlavorSpringBoard;
2555 if (launch_flavor != eLaunchFlavorSpringBoard)
2559 std::string app_bundle_path(path, app_ext + strlen(".app"));
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());
2569 if (bundleIDCFStr == NULL)
2574 #if defined (WITH_FBS)
2575 if (launch_flavor == eLaunchFlavorFBS)
2577 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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)];
2584 NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
2585 NSMutableDictionary *options = [NSMutableDictionary dictionary];
2587 DNBLogThreadedIf(LOG_PROCESS, "Calling BKSSystemService openApplication: @\"%s\",options include stdio path: \"%s\", "
2588 "BKSDebugOptionKeyDebugOnNextLaunch & BKSDebugOptionKeyWaitForDebugger )",
2589 bundleIDStr.c_str(),
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];
2597 [options setObject: debug_options forKey: FBSOpenApplicationOptionKeyDebuggingOptions];
2599 FBSSystemService *system_service = [[FBSSystemService alloc] init];
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;
2605 NSString *bundleIDNSStr = (NSString *) bundleIDCFStr;
2607 [system_service openApplication: bundleIDNSStr
2609 clientPort: client_port
2610 withResult: ^(NSError *error)
2612 // The system service will cleanup the client port we created for us.
2614 attach_error_code = (FBSOpenApplicationErrorCode)[error code];
2616 [system_service release];
2617 dispatch_semaphore_signal(semaphore);
2621 const uint32_t timeout_secs = 9;
2623 dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC);
2625 long success = dispatch_semaphore_wait(semaphore, timeout) == 0;
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);
2633 else if (attach_error_code != FBSOpenApplicationErrorCodeNone)
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);
2640 dispatch_release(semaphore);
2644 #if defined (WITH_BKS)
2645 if (launch_flavor == eLaunchFlavorBKS)
2647 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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)];
2654 NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
2655 NSMutableDictionary *options = [NSMutableDictionary dictionary];
2657 DNBLogThreadedIf(LOG_PROCESS, "Calling BKSSystemService openApplication: @\"%s\",options include stdio path: \"%s\", "
2658 "BKSDebugOptionKeyDebugOnNextLaunch & BKSDebugOptionKeyWaitForDebugger )",
2659 bundleIDStr.c_str(),
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];
2667 [options setObject: debug_options forKey: BKSOpenApplicationOptionKeyDebuggingOptions];
2669 BKSSystemService *system_service = [[BKSSystemService alloc] init];
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;
2675 NSString *bundleIDNSStr = (NSString *) bundleIDCFStr;
2677 [system_service openApplication: bundleIDNSStr
2679 clientPort: client_port
2680 withResult: ^(NSError *error)
2682 // The system service will cleanup the client port we created for us.
2684 attach_error_code = (BKSOpenApplicationErrorCode)[error code];
2686 [system_service release];
2687 dispatch_semaphore_signal(semaphore);
2691 const uint32_t timeout_secs = 9;
2693 dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, timeout_secs * NSEC_PER_SEC);
2695 long success = dispatch_semaphore_wait(semaphore, timeout) == 0;
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);
2703 else if (attach_error_code != BKSOpenApplicationErrorCodeNone)
2705 SetBKSError (attach_error_code, attach_err);
2706 DNBLogError("unable to launch the application with CFBundleIdentifier '%s' bks_error = %ld",
2707 bundleIDStr.c_str(),
2710 dispatch_release(semaphore);
2715 #if defined (WITH_SPRINGBOARD)
2716 if (launch_flavor == eLaunchFlavorSpringBoard)
2718 SBSApplicationLaunchError sbs_error = 0;
2720 const char *stdout_err = "/dev/null";
2721 CFString stdio_path;
2722 stdio_path.SetFileSystemRepresentation (stdout_err);
2724 DNBLogThreadedIf(LOG_PROCESS, "SBSLaunchApplicationForDebugging ( @\"%s\" , NULL, NULL, NULL, @\"%s\", @\"%s\", "
2725 "SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger )",
2726 bundleIDStr.c_str(),
2730 sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr,
2731 (CFURLRef)NULL, // openURL
2732 NULL, // launch_argv.get(),
2733 NULL, // launch_envp.get(), // CFDictionaryRef environment
2736 SBSApplicationDebugOnNextLaunch | SBSApplicationLaunchWaitForDebugger);
2738 if (sbs_error != SBSApplicationLaunchErrorSuccess)
2740 attach_err.SetError(sbs_error, DNBError::SpringBoard);
2744 #endif // WITH_SPRINGBOARD
2746 DNBLogThreadedIf(LOG_PROCESS, "Successfully set DebugOnNextLaunch.");
2747 return bundleIDCFStr;
2748 # else // !(defined (WITH_SPRINGBOARD) || defined (WITH_BKS) || defined (WITH_FBS))
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.
2758 MachProcess::CheckForProcess (const void *attach_token, nub_launch_flavor_t launch_flavor)
2760 if (attach_token == NULL)
2761 return INVALID_NUB_PROCESS;
2763 #if defined (WITH_FBS)
2764 if (launch_flavor == eLaunchFlavorFBS)
2766 NSString *bundleIDNSStr = (NSString *) attach_token;
2767 FBSSystemService *systemService = [[FBSSystemService alloc] init];
2768 pid_t pid = [systemService pidForApplication: bundleIDNSStr];
2769 [systemService release];
2771 return INVALID_NUB_PROCESS;
2777 #if defined (WITH_BKS)
2778 if (launch_flavor == eLaunchFlavorBKS)
2780 NSString *bundleIDNSStr = (NSString *) attach_token;
2781 BKSSystemService *systemService = [[BKSSystemService alloc] init];
2782 pid_t pid = [systemService pidForApplication: bundleIDNSStr];
2783 [systemService release];
2785 return INVALID_NUB_PROCESS;
2791 #if defined (WITH_SPRINGBOARD)
2792 if (launch_flavor == eLaunchFlavorSpringBoard)
2794 CFStringRef bundleIDCFStr = (CFStringRef) attach_token;
2796 nub_process_t attach_pid;
2797 got_it = SBSProcessIDForDisplayIdentifier(bundleIDCFStr, &attach_pid);
2801 return INVALID_NUB_PROCESS;
2804 return INVALID_NUB_PROCESS;
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
2813 MachProcess::CleanupAfterAttach (const void *attach_token, nub_launch_flavor_t launch_flavor, bool success, DNBError &err_str)
2815 if (attach_token == NULL)
2818 #if defined (WITH_FBS)
2819 if (launch_flavor == eLaunchFlavorFBS)
2823 FBSCleanupAfterAttach (attach_token, err_str);
2825 CFRelease((CFStringRef) attach_token);
2829 #if defined (WITH_BKS)
2831 if (launch_flavor == eLaunchFlavorBKS)
2835 BKSCleanupAfterAttach (attach_token, err_str);
2837 CFRelease((CFStringRef) attach_token);
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)
2848 SBSApplicationLaunchError sbs_error = 0;
2849 CFStringRef bundleIDCFStr = (CFStringRef) attach_token;
2851 sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr,
2857 SBSApplicationCancelDebugOnNextLaunch);
2859 if (sbs_error != SBSApplicationLaunchErrorSuccess)
2861 err_str.SetError(sbs_error, DNBError::SpringBoard);
2866 CFRelease((CFStringRef) attach_token);
2872 MachProcess::LaunchForDebug
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,
2882 nub_launch_flavor_t launch_flavor,
2884 const char *event_data,
2885 DNBError &launch_err
2888 // Clear out and clean up from any current state
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);
2893 // Fork a child process for debugging
2894 SetState(eStateLaunching);
2896 switch (launch_flavor)
2898 case eLaunchFlavorForkExec:
2899 m_pid = MachProcess::ForkChildForPTraceDebugging (path, argv, envp, this, launch_err);
2902 case eLaunchFlavorFBS:
2904 const char *app_ext = strstr(path, ".app");
2905 if (app_ext && (app_ext[4] == '\0' || app_ext[4] == '/'))
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.
2912 break; // We tried a FBS launch, but didn't succeed lets get out
2918 case eLaunchFlavorBKS:
2920 const char *app_ext = strstr(path, ".app");
2921 if (app_ext && (app_ext[4] == '\0' || app_ext[4] == '/'))
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.
2928 break; // We tried a BKS launch, but didn't succeed lets get out
2933 #ifdef WITH_SPRINGBOARD
2935 case eLaunchFlavorSpringBoard:
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)
2942 // .../whatever.app ?
2943 int len = strlen (path);
2946 if (strcmp (path + len - 4, ".app") == 0)
2948 app_ext = path + len - 4;
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.
2958 break; // We tried a springboard launch, but didn't succeed lets get out
2965 case eLaunchFlavorPosixSpawn:
2966 m_pid = MachProcess::PosixSpawnChildForPTraceDebugging (path,
2967 DNBArchProtocol::GetArchitecture (),
2982 launch_err.SetError(NUB_GENERIC_ERROR, DNBError::Generic);
2983 return INVALID_NUB_PROCESS;
2986 if (m_pid == INVALID_NUB_PROCESS)
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);
2998 for (i=0; (arg = argv[i]) != NULL; i++)
2999 m_args.push_back(arg);
3001 m_task.StartExceptionThread(launch_err);
3002 if (launch_err.Fail())
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;
3014 if (launch_flavor == eLaunchFlavorPosixSpawn)
3017 SetState (eStateAttaching);
3019 int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0);
3022 m_flags |= eMachProcessFlagsAttached;
3023 DNBLogThreadedIf(LOG_PROCESS, "successfully spawned pid %d", m_pid);
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);
3043 MachProcess::PosixSpawnChildForPTraceDebugging
3046 cpu_type_t cpu_type,
3049 const char *working_directory,
3050 const char *stdin_path,
3051 const char *stdout_path,
3052 const char *stderr_path,
3054 MachProcess* process,
3059 posix_spawnattr_t attr;
3061 DNBLogThreadedIf(LOG_PROCESS, "%s ( path='%s', argv=%p, envp=%p, working_dir=%s, stdin=%s, stdout=%s stderr=%s, no-stdio=%i)",
3072 err.SetError( ::posix_spawnattr_init (&attr), DNBError::POSIX);
3073 if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3074 err.LogThreaded("::posix_spawnattr_init ( &attr )");
3076 return INVALID_NUB_PROCESS;
3078 flags = POSIX_SPAWN_START_SUSPENDED | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
3080 flags |= _POSIX_SPAWN_DISABLE_ASLR;
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);
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" : "");
3093 return INVALID_NUB_PROCESS;
3095 // Don't do this on SnowLeopard, _sometimes_ the TASK_BASIC_INFO will fail
3096 // and we will fail to continue with our process...
3098 // On SnowLeopard we should set "DYLD_NO_PIE" in the inferior environment....
3100 #if !defined(__arm__)
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...
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);
3112 if (err.Fail() != 0 || ocount != 1)
3113 return INVALID_NUB_PROCESS;
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 )");
3125 pid_t pid = INVALID_NUB_PROCESS;
3126 if (file_actions_valid)
3128 if (stdin_path == NULL && stdout_path == NULL && stderr_path == NULL && !no_stdio)
3130 pty_error = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY);
3131 if (pty_error == PseudoTerminal::success)
3133 stdin_path = stdout_path = stderr_path = pty.SlaveName();
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";
3145 err.SetError( ::posix_spawn_file_actions_addopen (&file_actions,
3148 O_RDONLY | O_NOCTTY,
3151 if (err.Fail() || DNBLogCheckLogBit (LOG_PROCESS))
3152 err.LogThreaded ("::posix_spawn_file_actions_addopen (&file_actions, filedes=STDIN_FILENO, path='%s')", stdin_path);
3154 err.SetError( ::posix_spawn_file_actions_addopen (&file_actions,
3157 O_WRONLY | O_NOCTTY | O_CREAT,
3160 if (err.Fail() || DNBLogCheckLogBit (LOG_PROCESS))
3161 err.LogThreaded ("::posix_spawn_file_actions_addopen (&file_actions, filedes=STDOUT_FILENO, path='%s')", stdout_path);
3163 err.SetError( ::posix_spawn_file_actions_addopen (&file_actions,
3166 O_WRONLY | O_NOCTTY | O_CREAT,
3169 if (err.Fail() || DNBLogCheckLogBit (LOG_PROCESS))
3170 err.LogThreaded ("::posix_spawn_file_actions_addopen (&file_actions, filedes=STDERR_FILENO, path='%s')", stderr_path);
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);
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);
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);
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);
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
3196 pid = INVALID_NUB_PROCESS;
3200 if (process != NULL)
3202 int master_fd = pty.ReleaseMasterFD();
3203 process->SetChildFileDescriptors(master_fd, master_fd, master_fd);
3206 ::posix_spawnattr_destroy (&attr);
3208 if (pid != INVALID_NUB_PROCESS)
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);
3213 DNBArchProtocol::SetArchitecture (pid_cpu_type);
3216 if (file_actions_valid)
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 )");
3228 MachProcess::GetCPUTypeForLocalProcess (pid_t pid)
3230 int mib[CTL_MAXNAME]={0,};
3231 size_t len = CTL_MAXNAME;
3232 if (::sysctlnametomib("sysctl.proc_cputype", mib, &len))
3239 size_t cpu_len = sizeof(cpu);
3240 if (::sysctl (mib, static_cast<u_int>(len), &cpu, &cpu_len, 0, 0))
3246 MachProcess::ForkChildForPTraceDebugging
3251 MachProcess* process,
3252 DNBError& launch_err
3255 PseudoTerminal::Error pty_error = PseudoTerminal::success;
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.
3261 pid_t pid = pty.Fork(pty_error);
3265 //--------------------------------------------------------------
3266 // Error during fork.
3267 //--------------------------------------------------------------
3272 //--------------------------------------------------------------
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
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)
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
3288 // Sleep a bit to before the exec call
3291 // Turn this process into
3292 ::execv (path, (char * const *)argv);
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.
3301 //--------------------------------------------------------------
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
3309 if (process != NULL)
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);
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.
3324 CopyBundleIDForPath (const char *app_bundle_path, DNBError &err_str)
3326 CFBundle bundle(app_bundle_path);
3327 CFStringRef bundleIDCFStr = bundle.GetIdentifier();
3328 std::string bundleID;
3329 if (CFString::UTF8(bundleIDCFStr, bundleID) == NULL)
3331 struct stat app_bundle_stat;
3332 char err_msg[PATH_MAX];
3334 if (::stat (app_bundle_path, &app_bundle_stat) < 0)
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);
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);
3351 DNBLogThreadedIf(LOG_PROCESS, "%s() extracted CFBundleIdentifier: %s", __FUNCTION__, bundleID.c_str());
3352 CFRetain (bundleIDCFStr);
3354 return bundleIDCFStr;
3356 #endif // #if defined (WITH_SPRINGBOARD) || defined (WITH_BKS) || defined (WITH_FBS)
3357 #ifdef WITH_SPRINGBOARD
3360 MachProcess::SBLaunchForDebug (const char *path, char const *argv[], char const *envp[], bool no_stdio, bool disable_aslr, DNBError &launch_err)
3362 // Clear out and clean up from any current state
3365 DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv)", __FUNCTION__, path);
3367 // Fork a child process for debugging
3368 SetState(eStateLaunching);
3369 m_pid = MachProcess::SBForkChildForPTraceDebugging(path, argv, envp, no_stdio, this, launch_err);
3372 m_flags |= eMachProcessFlagsUsingSBS;
3376 for (i=0; (arg = argv[i]) != NULL; i++)
3377 m_args.push_back(arg);
3378 m_task.StartExceptionThread(launch_err);
3380 if (launch_err.Fail())
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;
3391 SetState (eStateAttaching);
3392 int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0);
3395 m_flags |= eMachProcessFlagsAttached;
3396 DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", m_pid);
3400 SetState (eStateExited);
3401 DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", m_pid);
3407 #include <servers/bootstrap.h>
3410 MachProcess::SBForkChildForPTraceDebugging (const char *app_bundle_path, char const *argv[], char const *envp[], bool no_stdio, MachProcess* process, DNBError &launch_err)
3412 DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv, %p)", __FUNCTION__, app_bundle_path, process);
3413 CFAllocatorRef alloc = kCFAllocatorDefault;
3415 if (argv[0] == NULL)
3416 return INVALID_NUB_PROCESS;
3419 // Count the number of arguments
3420 while (argv[argc] != NULL)
3423 // Enumerate the arguments
3424 size_t first_launch_arg_idx = 1;
3425 CFReleaser<CFMutableArrayRef> launch_argv;
3427 if (argv[first_launch_arg_idx])
3429 size_t launch_argc = argc > 0 ? argc - 1 : 0;
3430 launch_argv.reset (::CFArrayCreateMutable (alloc, launch_argc, &kCFTypeArrayCallBacks));
3433 CFString launch_arg;
3434 for (i=first_launch_arg_idx; (i < argc) && ((arg = argv[i]) != NULL); i++)
3436 launch_arg.reset(::CFStringCreateWithCString (alloc, arg, kCFStringEncodingUTF8));
3437 if (launch_arg.get() != NULL)
3438 CFArrayAppendValue(launch_argv.get(), launch_arg.get());
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
3448 CFReleaser<CFMutableDictionaryRef> launch_envp;
3452 launch_envp.reset(::CFDictionaryCreateMutable(alloc, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
3455 CFString name_string, value_string;
3457 for (int i = 0; envp[i] != NULL; i++)
3459 value = strstr (envp[i], "=");
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])
3465 name_len = value - envp[i];
3467 // Now move value over the "="
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());
3476 CFString stdio_path;
3481 PseudoTerminal::Error pty_err = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY);
3482 if (pty_err == PseudoTerminal::success)
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])
3488 ::chmod (slave_name, S_IRWXU | S_IRWXG | S_IRWXO);
3489 stdio_path.SetFileSystemRepresentation (slave_name);
3494 if (stdio_path.get() == NULL)
3496 stdio_path.SetFileSystemRepresentation ("/dev/null");
3499 CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path, launch_err);
3500 if (bundleIDCFStr == NULL)
3501 return INVALID_NUB_PROCESS;
3503 // This is just for logging:
3504 std::string bundleID;
3505 CFString::UTF8(bundleIDCFStr, bundleID);
3507 DNBLogThreadedIf(LOG_PROCESS, "%s() serialized launch arg array", __FUNCTION__);
3510 SBSApplicationLaunchError sbs_error = 0;
3511 sbs_error = SBSLaunchApplicationForDebugging (bundleIDCFStr,
3512 (CFURLRef)NULL, // openURL
3514 launch_envp.get(), // CFDictionaryRef environment
3517 SBSApplicationLaunchWaitForDebugger | SBSApplicationLaunchUnlockDevice);
3520 launch_err.SetError(sbs_error, DNBError::SpringBoard);
3522 if (sbs_error == SBSApplicationLaunchErrorSuccess)
3524 static const useconds_t pid_poll_interval = 200000;
3525 static const useconds_t pid_poll_timeout = 30000000;
3527 useconds_t pid_poll_total = 0;
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)
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);
3542 CFRelease (bundleIDCFStr);
3545 if (process != NULL)
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);
3552 DNBLogThreadedIf(LOG_PROCESS, "%s() => pid = %4.4x", __FUNCTION__, pid);
3556 DNBLogError("failed to lookup the process ID for CFBundleIdentifier %s.", bundleID.c_str());
3561 DNBLogError("unable to launch the application with CFBundleIdentifier '%s' sbs_error = %u", bundleID.c_str(), sbs_error);
3562 return INVALID_NUB_PROCESS;
3565 #endif // #ifdef WITH_SPRINGBOARD
3569 #if defined (WITH_BKS) || defined (WITH_FBS)
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)
3573 DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv)", __FUNCTION__, path);
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);
3583 for (i=0; (arg = argv[i]) != NULL; i++)
3584 m_args.push_back(arg);
3585 m_task.StartExceptionThread(launch_err);
3587 if (launch_err.Fail())
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;
3598 SetState (eStateAttaching);
3599 int err = ::ptrace (PT_ATTACHEXC, m_pid, 0, 0);
3602 m_flags |= eMachProcessFlagsAttached;
3603 DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", m_pid);
3607 SetState (eStateExited);
3608 DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", m_pid);
3615 MachProcess::BoardServiceForkChildForPTraceDebugging (const char *app_bundle_path,
3620 const char *event_data,
3621 DNBError &launch_err)
3623 if (argv[0] == NULL)
3624 return INVALID_NUB_PROCESS;
3626 DNBLogThreadedIf(LOG_PROCESS, "%s( '%s', argv, %p)", __FUNCTION__, app_bundle_path, this);
3628 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3631 // Count the number of arguments
3632 while (argv[argc] != NULL)
3635 // Enumerate the arguments
3636 size_t first_launch_arg_idx = 1;
3638 NSMutableArray *launch_argv = nil;
3640 if (argv[first_launch_arg_idx])
3642 size_t launch_argc = argc > 0 ? argc - 1 : 0;
3643 launch_argv = [NSMutableArray arrayWithCapacity: launch_argc];
3646 NSString *launch_arg;
3647 for (i=first_launch_arg_idx; (i < argc) && ((arg = argv[i]) != NULL); i++)
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];
3658 NSMutableDictionary *launch_envp = nil;
3661 launch_envp = [[NSMutableDictionary alloc] init];
3664 NSString *name_string, *value_string;
3666 for (int i = 0; envp[i] != NULL; i++)
3668 value = strstr (envp[i], "=");
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])
3674 name_len = value - envp[i];
3676 // Now move value over the "="
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];
3684 NSString *stdio_path = nil;
3685 NSFileManager *file_manager = [NSFileManager defaultManager];
3690 PseudoTerminal::Error pty_err = pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY);
3691 if (pty_err == PseudoTerminal::success)
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])
3697 ::chmod (slave_name, S_IRWXU | S_IRWXG | S_IRWXO);
3698 stdio_path = [file_manager stringWithFileSystemRepresentation: slave_name length: strlen(slave_name)];
3703 if (stdio_path == nil)
3705 const char *null_path = "/dev/null";
3706 stdio_path = [file_manager stringWithFileSystemRepresentation: null_path length: strlen(null_path)];
3709 CFStringRef bundleIDCFStr = CopyBundleIDForPath (app_bundle_path, launch_err);
3710 if (bundleIDCFStr == NULL)
3713 return INVALID_NUB_PROCESS;
3716 // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use toll-free bridging here:
3717 NSString *bundleIDNSStr = (NSString *) bundleIDCFStr;
3719 // Okay, now let's assemble all these goodies into the BackBoardServices options mega-dictionary:
3721 NSMutableDictionary *options = nullptr;
3722 pid_t return_pid = INVALID_NUB_PROCESS;
3723 bool success = false;
3726 if (m_flags & eMachProcessFlagsUsingBKS)
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);
3733 if (m_flags & eMachProcessFlagsUsingFBS)
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);
3742 int master_fd = pty.ReleaseMasterFD();
3743 SetChildFileDescriptors(master_fd, master_fd, master_fd);
3744 CFString::UTF8(bundleIDCFStr, m_bundle_id);
3753 MachProcess::BoardServiceSendEvent (const char *event_data, DNBError &send_err)
3755 bool return_value = true;
3757 if (event_data == NULL || *event_data == '\0')
3759 DNBLogError ("SendEvent called with NULL event data.");
3760 send_err.SetErrorString("SendEvent called with empty event data");
3764 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3766 if (strcmp (event_data, "BackgroundApplication") == 0)
3768 // This is an event I cooked up. What you actually do is foreground the system app, so:
3770 if (m_flags & eMachProcessFlagsUsingBKS)
3772 return_value = BKSCallOpenApplicationFunction(nil, nil, send_err, NULL);
3776 if (m_flags & eMachProcessFlagsUsingFBS)
3778 return_value = FBSCallOpenApplicationFunction(nil, nil, send_err, NULL);
3783 DNBLogError ("Failed to background application, error: %s.", send_err.AsString());
3788 if (m_bundle_id.empty())
3790 // See if we can figure out the bundle ID for this PID:
3792 DNBLogError ("Tried to send event \"%s\" to a process that has no bundle ID.", event_data);
3796 NSString *bundleIDNSStr = [NSString stringWithUTF8String:m_bundle_id.c_str()];
3798 NSMutableDictionary *options = [NSMutableDictionary dictionary];
3801 if (m_flags & eMachProcessFlagsUsingBKS)
3803 if (!BKSAddEventDataToOptions(options, event_data, send_err))
3808 return_value = BKSCallOpenApplicationFunction (bundleIDNSStr, options, send_err, NULL);
3809 DNBLogThreadedIf (LOG_PROCESS, "Called BKSCallOpenApplicationFunction to send event.");
3814 if (m_flags & eMachProcessFlagsUsingFBS)
3816 if (!FBSAddEventDataToOptions(options, event_data, send_err))
3821 return_value = FBSCallOpenApplicationFunction (bundleIDNSStr, options, send_err, NULL);
3822 DNBLogThreadedIf (LOG_PROCESS, "Called FBSCallOpenApplicationFunction to send event.");
3828 DNBLogError ("Failed to send event: %s, error: %s.", event_data, send_err.AsString());
3833 return return_value;
3835 #endif // defined(WITH_BKS) || defined (WITH_FBS)
3839 MachProcess::BKSCleanupAfterAttach (const void *attach_token, DNBError &err_str)
3843 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3845 // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use toll-free bridging here:
3846 NSString *bundleIDNSStr = (NSString *) attach_token;
3848 // Okay, now let's assemble all these goodies into the BackBoardServices options mega-dictionary:
3850 // First we have the debug sub-dictionary:
3851 NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
3852 [debug_options setObject: [NSNumber numberWithBool: YES] forKey: BKSDebugOptionKeyCancelDebugOnNextLaunch];
3854 // That will go in the overall dictionary:
3856 NSMutableDictionary *options = [NSMutableDictionary dictionary];
3857 [options setObject: debug_options forKey: BKSOpenApplicationOptionKeyDebuggingOptions];
3859 success = BKSCallOpenApplicationFunction (bundleIDNSStr, options, err_str, NULL);
3863 DNBLogError ("error trying to cancel debug on next launch for %s: %s", [bundleIDNSStr UTF8String], err_str.AsString());
3872 MachProcess::FBSCleanupAfterAttach (const void *attach_token, DNBError &err_str)
3876 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3878 // Instead of rewriting CopyBundleIDForPath for NSStrings, we'll just use toll-free bridging here:
3879 NSString *bundleIDNSStr = (NSString *) attach_token;
3881 // Okay, now let's assemble all these goodies into the BackBoardServices options mega-dictionary:
3883 // First we have the debug sub-dictionary:
3884 NSMutableDictionary *debug_options = [NSMutableDictionary dictionary];
3885 [debug_options setObject: [NSNumber numberWithBool: YES] forKey: FBSDebugOptionKeyCancelDebugOnNextLaunch];
3887 // That will go in the overall dictionary:
3889 NSMutableDictionary *options = [NSMutableDictionary dictionary];
3890 [options setObject: debug_options forKey: FBSOpenApplicationOptionKeyDebuggingOptions];
3892 success = FBSCallOpenApplicationFunction (bundleIDNSStr, options, err_str, NULL);
3896 DNBLogError ("error trying to cancel debug on next launch for %s: %s", [bundleIDNSStr UTF8String], err_str.AsString());