This is an archive of the discontinued LLVM Phabricator instance.

Improve error messaging when debugserver fails to complete attaching to another process on Darwin systems
ClosedPublic

Authored by jasonmolenda on Jul 11 2023, 6:56 PM.

Details

Summary

On Darwin, we first get a process' mach task port with task_for_pid(), and then we do a ptrace(PT_ATTACHEXC). We need to complete both before we are successfully attached. There are unusual failures where we get the task port and the ptrace may not have completed entirely, so lldb is going to exit out. An error message isn't created at the layer where this failure was detected, and then at a higher layer where we try to find a reason why the attach failed, we notice that the process is being "debugged" -- by ourselves -- and emit an error message saying it's being debugged.

This patch adds an error string at all the points where we can fail to complete this two-step attach process, and also adds a refinement to the "last ditch error reporting" scheme to confirm that the process' parent process isn't already this debugserver. (we half-attached to it)

Diff Detail

Event Timeline

jasonmolenda created this revision.Jul 11 2023, 6:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 11 2023, 6:56 PM
jasonmolenda requested review of this revision.Jul 11 2023, 6:56 PM
JDevlieghere accepted this revision.Jul 11 2023, 10:34 PM

LGTM modulo code duplication.

lldb/tools/debugserver/source/RNBRemote.cpp
3639–3644

As this is the exact same code as in MachProcess.mm, is this worth factoring out? Something like

std::optional<int> GetAttachedProcPID() {
  struct proc_bsdshortinfo proc;
  if (proc_pidinfo(pid, PROC_PIDT_SHORTBSDINFO, 0, &proc,
                   PROC_PIDT_SHORTBSDINFO_SIZE) == sizeof(proc)) {
    return proc.pbsi_ppid;
  }
  return std::null opt;
}
This revision is now accepted and ready to land.Jul 11 2023, 10:34 PM

Update to incorporate Jonas' suggestion - adding MachProcess::GetParentProcessID and MachProcess::ProcessIsBeingDebugged, as well as the DNB passthrough functions. I didn't pass back optionals for the cases were we can fail, using "invalid pid number" and "is not being debugged" for when the calls fail, which isn't the correctest but these are only ending up in error messages so i'm not too pressed about it.

Update the patch; that last one included a separate set of changes I was working on with Alex yesterday.