[lldb-dev] Unreliable process attach on Linux

Pavel Labath via lldb-dev lldb-dev at lists.llvm.org
Mon Jan 7 00:16:00 PST 2019


On 04/01/2019 17:38, Florian Weimer via lldb-dev wrote:
> Consider this example program:
> 
> #include <err.h>
> #include <sys/wait.h>
> #include <unistd.h>
> 
> #include <lldb/API/SBDebugger.h>
> #include <lldb/API/SBProcess.h>
> #include <lldb/API/SBTarget.h>
> 
> int
> main(void)
> {
>    // Target process for the debugger.
>    pid_t pid = fork();
>    if (pid < 0)
>      err(1, "fork");
>    if (pid == 0)
>      while (true)
>        pause();
> 
>    lldb::SBDebugger::Initialize();
>    {
>      auto debugger(lldb::SBDebugger::Create());
>      if (!debugger.IsValid())
>        errx(1, "SBDebugger::Create failed");
> 
>      auto target(debugger.CreateTarget(nullptr));
>      if (!target.IsValid())
>        errx(1, "SBDebugger::CreateTarget failed");
> 
>      lldb::SBAttachInfo attachinfo(pid);
>      lldb::SBError error;
>      auto process(target.Attach(attachinfo, error));
>      if (!process.IsValid())
>        errx(1, "SBTarget::Attach failed: %s", error.GetCString());
>      error = process.Detach();
>      if (error.Fail())
>        errx(1, "SBProcess::Detach failed: %s", error.GetCString());
>    }
>    lldb::SBDebugger::Terminate();
> 
>    if (kill(pid, SIGKILL) != 0)
>      err(1, "kill");
>    if (waitpid(pid, NULL, 0) < 0)
>      err(1, "waitpid");
> 
>    return 0;
> }
> 
> Run it in a loop like this:
> 
> $ while ./test-attach ; do date; done
> 
> On Linux x86-64 (Fedora 29), with LLDB 7 (lldb-7.0.0-1.fc29.x86_64) and
> kernel 4.19.12 (kernel-4.19.12-301.fc29.x86_64), after 100 iterations or
> so, attaching to the newly created process fails:
> 
> test-attach: SBTarget::Attach failed: lost connection
> 
> This also reproduces occasionally with LLDB itself (with “lldb -p PID”).
> 
> Any suggestions how to get more information about the cause of this
> error?
> 

I would recommend enabling gdb-remote logging (so something like: 
debugger.HandleCommand("log enable gdb-remote packets")) to see at which 
stage do we actually lose the gdb-server connection.

My best bet would be that on your machine/build the server is slower 
than usual in responding to one of the client packets and that causes 
the connection to be dropped.

cheers,
pavel


More information about the lldb-dev mailing list