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

source code of lldb/tools/debugserver/source/MacOSX/MachProcess.mm