Skip to content

Commit 08cfd81

Browse files
authored
Merge pull request #7078 from jasonmolenda/r101152233-improve-error-reporting-on-partial-attach
Improve error messaging when debugserver fails to complete attaching
2 parents 06da450 + 0e2439b commit 08cfd81

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

lldb/tools/debugserver/source/DNB.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,3 +1846,11 @@ bool DNBGetAddressingBits(uint32_t &addressing_bits) {
18461846

18471847
return addressing_bits > 0;
18481848
}
1849+
1850+
nub_process_t DNBGetParentProcessID(nub_process_t child_pid) {
1851+
return MachProcess::GetParentProcessID(child_pid);
1852+
}
1853+
1854+
bool DNBProcessIsBeingDebugged(nub_process_t pid) {
1855+
return MachProcess::ProcessIsBeingDebugged(pid);
1856+
}

lldb/tools/debugserver/source/DNB.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,9 @@ std::string DNBGetMacCatalystVersionString();
247247
bool DNBDebugserverIsTranslated();
248248

249249
bool DNBGetAddressingBits(uint32_t &addressing_bits);
250+
251+
nub_process_t DNBGetParentProcessID(nub_process_t child_pid);
252+
253+
bool DNBProcessIsBeingDebugged(nub_process_t pid);
254+
250255
#endif

lldb/tools/debugserver/source/MacOSX/MachProcess.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ class MachProcess {
126126
static bool GetOSVersionNumbers(uint64_t *major, uint64_t *minor,
127127
uint64_t *patch);
128128
static std::string GetMacCatalystVersionString();
129+
130+
static nub_process_t GetParentProcessID(nub_process_t child_pid);
131+
132+
static bool ProcessIsBeingDebugged(nub_process_t pid);
133+
129134
#ifdef WITH_BKS
130135
static void BKSCleanupAfterAttach(const void *attach_token,
131136
DNBError &err_str);

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

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,16 +2821,21 @@ static uint64_t bits(uint64_t value, uint32_t msbit, uint32_t lsbit) {
28212821
"attach to pid %d",
28222822
getpid(), pid);
28232823

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);
28302835
DNBLogError(
28312836
"[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);
28342839
}
28352840
}
28362841
}
@@ -2879,6 +2884,26 @@ static uint64_t bits(uint64_t value, uint32_t msbit, uint32_t lsbit) {
28792884
return {};
28802885
}
28812886

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+
28822907
#if defined(WITH_SPRINGBOARD) || defined(WITH_BKS) || defined(WITH_FBS)
28832908
/// Get the app bundle from the given path. Returns the empty string if the
28842909
/// 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) {
34013426
"%d (err = %i, errno = %i (%s))",
34023427
m_pid, err, ptrace_err.Status(),
34033428
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);
34053436
}
34063437
} else {
34073438
launch_err.Clear();
@@ -3777,6 +3808,10 @@ static CFStringRef CopyBundleIDForPath(const char *app_bundle_path,
37773808
m_flags |= eMachProcessFlagsAttached;
37783809
DNBLogThreadedIf(LOG_PROCESS, "successfully attached to pid %d", m_pid);
37793810
} else {
3811+
launch_err.SetErrorString(
3812+
"Failed to attach to pid %d, SBLaunchForDebug() unable to "
3813+
"ptrace(PT_ATTACHEXC)",
3814+
m_pid);
37803815
SetState(eStateExited);
37813816
DNBLogThreadedIf(LOG_PROCESS, "error: failed to attach to pid %d", m_pid);
37823817
}
@@ -3996,6 +4031,10 @@ static CFStringRef CopyBundleIDForPath(const char *app_bundle_path,
39964031
m_flags |= eMachProcessFlagsAttached;
39974032
DNBLog("[LaunchAttach] successfully attached to pid %d", m_pid);
39984033
} else {
4034+
launch_err.SetErrorString(
4035+
"Failed to attach to pid %d, BoardServiceLaunchForDebug() unable to "
4036+
"ptrace(PT_ATTACHEXC)",
4037+
m_pid);
39994038
SetState(eStateExited);
40004039
DNBLog("[LaunchAttach] END (%d) error: failed to attach to pid %d",
40014040
getpid(), m_pid);

lldb/tools/debugserver/source/RNBRemote.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,14 +3630,8 @@ static bool attach_failed_due_to_uid_mismatch (nub_process_t pid,
36303630
// processes and step through to find the one we're looking for
36313631
// (as process_does_not_exist() does).
36323632
static bool process_is_already_being_debugged (nub_process_t pid) {
3633-
struct kinfo_proc kinfo;
3634-
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
3635-
size_t len = sizeof(struct kinfo_proc);
3636-
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &kinfo, &len, NULL, 0) != 0) {
3637-
return false; // pid doesn't exist? well, it's not being debugged...
3638-
}
3639-
if (kinfo.kp_proc.p_flag & P_TRACED)
3640-
return true; // is being debugged already
3633+
if (DNBProcessIsBeingDebugged(pid) && DNBGetParentProcessID(pid) != getpid())
3634+
return true;
36413635
else
36423636
return false;
36433637
}

0 commit comments

Comments
 (0)