@@ -2821,16 +2821,21 @@ static uint64_t bits(uint64_t value, uint32_t msbit, uint32_t lsbit) {
2821
2821
" attach to pid %d" ,
2822
2822
getpid (), pid);
2823
2823
2824
- struct kinfo_proc kinfo;
2825
- int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
2826
- size_t len = sizeof (struct kinfo_proc );
2827
- if (sysctl (mib, sizeof (mib) / sizeof (mib[0 ]), &kinfo, &len, NULL , 0 ) == 0 && len > 0 ) {
2828
- if (kinfo.kp_proc .p_flag & P_TRACED) {
2829
- ::snprintf (err_str, err_len, " %s - process %d is already being debugged" , err.AsString(), pid);
2824
+ if (ProcessIsBeingDebugged (pid)) {
2825
+ nub_process_t ppid = GetParentProcessID (pid);
2826
+ if (ppid == getpid ()) {
2827
+ snprintf (err_str, err_len,
2828
+ " %s - Failed to attach to pid %d, AttachForDebug() "
2829
+ " unable to ptrace(PT_ATTACHEXC)" ,
2830
+ err.AsString (), m_pid);
2831
+ } else {
2832
+ snprintf (err_str, err_len,
2833
+ " %s - process %d is already being debugged by pid %d" ,
2834
+ err.AsString (), pid, ppid);
2830
2835
DNBLogError (
2831
2836
" [LaunchAttach] (%d) MachProcess::AttachForDebug pid %d is "
2832
- " already being debugged" ,
2833
- getpid (), pid);
2837
+ " already being debugged by pid %d " ,
2838
+ getpid (), pid, ppid );
2834
2839
}
2835
2840
}
2836
2841
}
@@ -2879,6 +2884,26 @@ static uint64_t bits(uint64_t value, uint32_t msbit, uint32_t lsbit) {
2879
2884
return {};
2880
2885
}
2881
2886
2887
+ nub_process_t MachProcess::GetParentProcessID (nub_process_t child_pid) {
2888
+ struct proc_bsdshortinfo proc;
2889
+ if (proc_pidinfo (child_pid, PROC_PIDT_SHORTBSDINFO, 0 , &proc,
2890
+ PROC_PIDT_SHORTBSDINFO_SIZE) == sizeof (proc)) {
2891
+ return proc.pbsi_ppid ;
2892
+ }
2893
+ return INVALID_NUB_PROCESS;
2894
+ }
2895
+
2896
+ bool MachProcess::ProcessIsBeingDebugged (nub_process_t pid) {
2897
+ struct kinfo_proc kinfo;
2898
+ int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
2899
+ size_t len = sizeof (struct kinfo_proc );
2900
+ if (sysctl (mib, sizeof (mib) / sizeof (mib[0 ]), &kinfo, &len, NULL , 0 ) == 0 &&
2901
+ (kinfo.kp_proc .p_flag & P_TRACED))
2902
+ return true ;
2903
+ else
2904
+ return false ;
2905
+ }
2906
+
2882
2907
#if defined(WITH_SPRINGBOARD) || defined(WITH_BKS) || defined(WITH_FBS)
2883
2908
// / Get the app bundle from the given path. Returns the empty string if the
2884
2909
// / path doesn't appear to be an app bundle.
@@ -3401,7 +3426,13 @@ static uint64_t bits(uint64_t value, uint32_t msbit, uint32_t lsbit) {
3401
3426
" %d (err = %i, errno = %i (%s))" ,
3402
3427
m_pid, err, ptrace_err.Status (),
3403
3428
ptrace_err.AsString ());
3404
- launch_err.SetError (NUB_GENERIC_ERROR, DNBError::Generic);
3429
+ char err_msg[PATH_MAX];
3430
+
3431
+ snprintf (err_msg, sizeof (err_msg),
3432
+ " Failed to attach to pid %d, LaunchForDebug() unable to "
3433
+ " ptrace(PT_ATTACHEXC)" ,
3434
+ m_pid);
3435
+ launch_err.SetErrorString (err_msg);
3405
3436
}
3406
3437
} else {
3407
3438
launch_err.Clear ();
@@ -3777,6 +3808,10 @@ static CFStringRef CopyBundleIDForPath(const char *app_bundle_path,
3777
3808
m_flags |= eMachProcessFlagsAttached;
3778
3809
DNBLogThreadedIf (LOG_PROCESS, " successfully attached to pid %d" , m_pid);
3779
3810
} else {
3811
+ launch_err.SetErrorString (
3812
+ " Failed to attach to pid %d, SBLaunchForDebug() unable to "
3813
+ " ptrace(PT_ATTACHEXC)" ,
3814
+ m_pid);
3780
3815
SetState (eStateExited);
3781
3816
DNBLogThreadedIf (LOG_PROCESS, " error: failed to attach to pid %d" , m_pid);
3782
3817
}
@@ -3996,6 +4031,10 @@ static CFStringRef CopyBundleIDForPath(const char *app_bundle_path,
3996
4031
m_flags |= eMachProcessFlagsAttached;
3997
4032
DNBLog (" [LaunchAttach] successfully attached to pid %d" , m_pid);
3998
4033
} else {
4034
+ launch_err.SetErrorString (
4035
+ " Failed to attach to pid %d, BoardServiceLaunchForDebug() unable to "
4036
+ " ptrace(PT_ATTACHEXC)" ,
4037
+ m_pid);
3999
4038
SetState (eStateExited);
4000
4039
DNBLog (" [LaunchAttach] END (%d) error: failed to attach to pid %d" ,
4001
4040
getpid (), m_pid);
0 commit comments