Skip to content

Commit ef08bad

Browse files
committed
[lldb-dap] Make connection URLs match lldb
Use the same scheme as ConnectionFileDescriptor::Connect and use "listen" and "accept". Addresses feedback from a Pavel in a different PR [1]. [1] #143628 (comment)
1 parent 6f4add3 commit ef08bad

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

lldb/include/lldb/Host/Socket.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class Socket : public IOObject {
7474
ProtocolUnixAbstract
7575
};
7676

77+
enum SocketMode {
78+
ModeAccept,
79+
ModeConnect,
80+
};
81+
7782
struct HostAndPort {
7883
std::string hostname;
7984
uint16_t port;
@@ -83,6 +88,10 @@ class Socket : public IOObject {
8388
}
8489
};
8590

91+
using ProtocolModePair = std::pair<SocketProtocol, SocketMode>;
92+
static llvm::Expected<ProtocolModePair>
93+
GetProtocolAndMode(llvm::StringRef scheme);
94+
8695
static const NativeSocket kInvalidSocketValue;
8796

8897
~Socket() override;

lldb/source/Host/common/Socket.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,28 @@ llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &OS,
476476
const Socket::HostAndPort &HP) {
477477
return OS << '[' << HP.hostname << ']' << ':' << HP.port;
478478
}
479+
480+
llvm::Expected<Socket::ProtocolModePair>
481+
Socket::GetProtocolAndMode(llvm::StringRef scheme) {
482+
// Keep in sync with ConnectionFileDescriptor::Connect.
483+
return llvm::StringSwitch<llvm::Expected<ProtocolModePair>>(scheme)
484+
.Case("listen", ProtocolModePair{SocketProtocol::ProtocolTcp,
485+
SocketMode::ModeAccept})
486+
.Cases("accept", "unix-accept",
487+
ProtocolModePair{SocketProtocol::ProtocolUnixDomain,
488+
SocketMode::ModeAccept})
489+
.Case("unix-abstract-accept",
490+
ProtocolModePair{SocketProtocol::ProtocolUnixAbstract,
491+
SocketMode::ModeAccept})
492+
.Cases("connect", "tcp-connect",
493+
ProtocolModePair{SocketProtocol::ProtocolTcp,
494+
SocketMode::ModeConnect})
495+
.Case("udp", ProtocolModePair{SocketProtocol::ProtocolTcp,
496+
SocketMode::ModeConnect})
497+
.Case("unix-connect", ProtocolModePair{SocketProtocol::ProtocolUnixDomain,
498+
SocketMode::ModeConnect})
499+
.Case("unix-abstract-connect",
500+
ProtocolModePair{SocketProtocol::ProtocolUnixAbstract,
501+
SocketMode::ModeConnect})
502+
.Default(llvm::createStringError("unsupported scheme"));
503+
}

lldb/test/API/tools/lldb-dap/server/TestDAP_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_server_port(self):
5454
Test launching a binary with a lldb-dap in server mode on a specific port.
5555
"""
5656
self.build()
57-
(_, connection) = self.start_server(connection="tcp://localhost:0")
57+
(_, connection) = self.start_server(connection="listen://localhost:0")
5858
self.run_debug_session(connection, "Alice")
5959
self.run_debug_session(connection, "Bob")
6060

@@ -72,7 +72,7 @@ def cleanup():
7272
self.addTearDownHook(cleanup)
7373

7474
self.build()
75-
(_, connection) = self.start_server(connection="unix://" + name)
75+
(_, connection) = self.start_server(connection="accept://" + name)
7676
self.run_debug_session(connection, "Alice")
7777
self.run_debug_session(connection, "Bob")
7878

@@ -82,7 +82,7 @@ def test_server_interrupt(self):
8282
Test launching a binary with lldb-dap in server mode and shutting down the server while the debug session is still active.
8383
"""
8484
self.build()
85-
(process, connection) = self.start_server(connection="tcp://localhost:0")
85+
(process, connection) = self.start_server(connection="listen://localhost:0")
8686
self.dap_server = dap_server.DebugAdapterServer(
8787
connection=connection,
8888
)

lldb/tools/lldb-dap/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def connection
2828
MetaVarName<"<connection>">,
2929
HelpText<
3030
"Communicate with the lldb-dap tool over the specified connection. "
31-
"Connections are specified like 'tcp://[host]:port' or "
32-
"'unix:///path'.">;
31+
"Connections are specified like 'listen://[host]:port' or "
32+
"'accept:///path'.">;
3333

3434
def launch_target: S<"launch-target">,
3535
MetaVarName<"<target>">,

lldb/tools/lldb-dap/tool/lldb-dap.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,34 @@ static llvm::Expected<std::pair<Socket::SocketProtocol, std::string>>
226226
validateConnection(llvm::StringRef conn) {
227227
auto uri = lldb_private::URI::Parse(conn);
228228

229-
if (uri && (uri->scheme == "tcp" || uri->scheme == "connect" ||
230-
!uri->hostname.empty() || uri->port)) {
229+
auto make_error = [conn]() -> llvm::Error {
230+
return llvm::createStringError(
231+
"Unsupported connection specifier, expected 'accept:///path' or "
232+
"'listen://[host]:port', got '%s'.",
233+
conn.str().c_str());
234+
};
235+
236+
if (!uri)
237+
return make_error();
238+
239+
llvm::Expected<Socket::ProtocolModePair> protocol_and_mode =
240+
Socket::GetProtocolAndMode(uri->scheme);
241+
if (!protocol_and_mode)
242+
return protocol_and_mode.takeError();
243+
if (protocol_and_mode->second != Socket::ModeAccept)
244+
return make_error();
245+
246+
if (protocol_and_mode->first == Socket::ProtocolTcp) {
231247
return std::make_pair(
232248
Socket::ProtocolTcp,
233249
formatv("[{0}]:{1}", uri->hostname.empty() ? "0.0.0.0" : uri->hostname,
234250
uri->port.value_or(0)));
235251
}
236252

237-
if (uri && (uri->scheme == "unix" || uri->scheme == "unix-connect" ||
238-
uri->path != "/")) {
253+
if (protocol_and_mode->first == Socket::ProtocolUnixDomain)
239254
return std::make_pair(Socket::ProtocolUnixDomain, uri->path.str());
240-
}
241255

242-
return llvm::createStringError(
243-
"Unsupported connection specifier, expected 'unix-connect:///path' or "
244-
"'connect://[host]:port', got '%s'.",
245-
conn.str().c_str());
256+
return make_error();
246257
}
247258

248259
static llvm::Error

0 commit comments

Comments
 (0)