This is an archive of the discontinued LLVM Phabricator instance.

[lldb] [lldb-gdbserver] Unify listen/connect code to use ConnectionFileDescriptor
ClosedPublic

Authored by mgorny on Oct 17 2021, 7:34 AM.

Details

Summary

Unify the listen and connect code inside lldb-server to use
ConnectionFileDescriptor uniformly rather than a mix of it and Acceptor.
This involves:

  • adding a function to map legacy values of host:port parameter (including legacy server URLs) into CFD-style URLs
  • adding a callback to return "local socket id" (i.e. UNIX socket path or TCP port number) between listen() and accept() calls in CFD
  • adding a "unix-abstract-accept" scheme to CFD

As an additional advantage, this permits lldb-server to accept any URL
known to CFD including the new serial:// scheme. Effectively,
lldb-server can now listen on the serial port. Tests for connecting
over a pty are added to test that.

Diff Detail

Event Timeline

mgorny requested review of this revision.Oct 17 2021, 7:34 AM
mgorny created this revision.

What's the test strategy for this?

This is not fully functional yet, as lldb-server
crashes when attempting to send long packets (e.g. target.xml contents).

I am guessing you'll also want to disable QStartNoAckMode for these connections (?)

lldb/tools/lldb-server/lldb-gdbserver.cpp
259–260

Maybe we should have this accept any lldb "URL", and have the host:port support be a special case of "legacy" connection strings?

mgorny updated this revision to Diff 380502.Oct 18 2021, 1:01 PM
mgorny marked an inline comment as done.
mgorny retitled this revision from [lldb] [lldb-server] Support listening on serial:// to [lldb] [lldb-server] Allow any protocol supported by lldb.
mgorny edited the summary of this revision. (Show Details)

What's the test strategy for this?

Still thinking about it. I'm mostly PoC-ing this while other changes are waiting.

This is not fully functional yet, as lldb-server
crashes when attempting to send long packets (e.g. target.xml contents).

I am guessing you'll also want to disable QStartNoAckMode for these connections (?)

I suppose this makes sense. Are you suggesting special-casing serial:// or do you have something else in mind?

This is not fully functional yet, as lldb-server
crashes when attempting to send long packets (e.g. target.xml contents).

I am guessing you'll also want to disable QStartNoAckMode for these connections (?)

I suppose this makes sense. Are you suggesting special-casing serial:// or do you have something else in mind?

I think we could introduce something like Connection::IsReliable and key that behavior off of that.

Although I can't say I actually understand how the "ack" mode is supposed to work. It seems like it would be very hard to recover from transmission errors that affect the $ or # parts of the packet.

I think we could introduce something like Connection::IsReliable and key that behavior off of that.

Although I can't say I actually understand how the "ack" mode is supposed to work. It seems like it would be very hard to recover from transmission errors that affect the $ or # parts of the packet.

FWICS, gdb uses "noack" mode unconditionally. I somewhat suspect that the "ack" mode might just be a historical thing that doesn't actually serve any purpose.

What's the test strategy for this?

Still thinking about it. I'm mostly PoC-ing this while other changes are waiting.

For the current patch, I guess it would be sufficient to establish a connection using _any_ url.

lldb/tools/lldb-server/lldb-gdbserver.cpp
259–260

I am wondering what's the impact of removing this. If we get some semi-reasonable message, then it'd be nice to get rid of it. Otherwise, we'll have a weird difference between the user typing host:0 and connect://host:0.

259–260

If we don't get a reasonable error message, we could probably create one by pushing this check into the lower layers.

mgorny updated this revision to Diff 380998.Oct 20 2021, 9:34 AM

Add a PoC for testing lldb-server over a pty.

mgorny updated this revision to Diff 381158.Oct 21 2021, 12:18 AM

Make the test verify that (potentially large) target.xml can be successfully transferred.

mgorny updated this revision to Diff 381645.Oct 22 2021, 1:27 PM
mgorny retitled this revision from [lldb] [lldb-server] Allow any protocol supported by lldb to [lldb] [lldb-gdbserver] Unify listen/connect code to use ConnectionFileDescriptor.
mgorny edited the summary of this revision. (Show Details)

Turned it into an überpatch upgrading CFD and refactoring lldb-server to reduce duplication of code.

I like this. For lack of a better place, I'd place the LLGSArgToURL function into GDBRemoteCommunicationServerLLGS.h. That is the ~highest level file that can still be unit tested.

lldb/include/lldb/Host/posix/ConnectionFileDescriptorPosix.h
49

I'd use llvm::function_ref here

lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
546–550

you can merge these two

577–608

This is identical to NamedSocketAccept modulo the socket type constant, right? Can you merge these?

638

Can this ever be zero ?

854

I'd "reverse" this condition :P

872

Can this string be empty? Maybe use url_arg.startswith(), just in case.

mgorny marked 4 inline comments as done.Oct 25 2021, 1:20 AM
mgorny added inline comments.
lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
577–608

Yes; in fact, most of the functions are pretty similar. However, I'd prefer to defer merging them until after the port predicate removal patch, as that should also open the possibility of combining them with the TCP socket methods too.

638

Yes, when getsockname() fails. I suppose we could modify it to use llvm::Optional<...> or even llvm::Expected<...> but this is one place where I don't think we need to care much about proper error handling.

872

I don't think it can, or at least I wasn't able to get an empty string in (the argument parser seems to reject them) but sure, no harm in being extra secure.

mgorny updated this revision to Diff 381876.Oct 25 2021, 1:31 AM
mgorny marked an inline comment as done.

Move to gdb-remote. Update per requests.

I've also added a typedef for the callback and optimized the URI parsing a bit to hopefully involve less temporary strings.

labath added inline comments.Oct 25 2021, 1:37 AM
lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
577–608

Ok, sounds good.

638

But how can it fail? This is a socket that we've just created and its totally under our control...

mgorny added inline comments.Oct 25 2021, 1:57 AM
lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
638

Yeah, exactly, that's why I said proper error handling is not very important there — but technically the function provides some potential error conditions. Are you suggesting we just pass zero as-is then?

mgorny updated this revision to Diff 381916.Oct 25 2021, 3:39 AM

Remove error handling from socket name getters.

mgorny updated this revision to Diff 381918.Oct 25 2021, 3:47 AM

Remove error handling from URI mapping to make the code simpler. In the end, the only possible error here was '--reverse-connect without valid host:port' and we can just treat that as UNIX connect for symmetry with non-reverse-connect mode.

labath accepted this revision.Oct 25 2021, 6:36 AM
This revision is now accepted and ready to land.Oct 25 2021, 6:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 26 2021, 4:06 AM