Skip to content

Commit e061a85

Browse files
committed
[lldb] Fixed the error unable to launch a GDB server in API tests
TestPlatformLaunchGDBServer.py runs ldb-server w/o --min-gdbserver-port, --max-gdbserver-port or --gdbserver-port, so gdbserver_portmap is empty and gdbserver_portmap.GetNextAvailablePort() will return 0. Do not pass 0 to portmap_for_child in this case. Added few asserts in GDBRemoteCommunicationServerPlatform::PortMap to avoid this in the future. This patch fixes llvm#97537. It seems StartDebugserverProcess() ignores the port anyway and parameters --min-gdbserver-port, --max-gdbserver-port and --gdbserver-port are useless at all, but it is out of scope of this patch.
1 parent 93d2b23 commit e061a85

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ using namespace lldb_private;
4646

4747
GDBRemoteCommunicationServerPlatform::PortMap::PortMap(uint16_t min_port,
4848
uint16_t max_port) {
49+
assert(min_port);
4950
for (; min_port < max_port; ++min_port)
5051
m_port_map[min_port] = LLDB_INVALID_PROCESS_ID;
5152
}
5253

5354
void GDBRemoteCommunicationServerPlatform::PortMap::AllowPort(uint16_t port) {
55+
assert(port);
5456
// Do not modify existing mappings
5557
m_port_map.insert({port, LLDB_INVALID_PROCESS_ID});
5658
}

lldb/tools/lldb-server/lldb-platform.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,11 @@ int main_platform(int argc, char *argv[]) {
313313
GDBRemoteCommunicationServerPlatform::PortMap portmap_for_child;
314314
llvm::Expected<uint16_t> available_port =
315315
gdbserver_portmap.GetNextAvailablePort();
316-
if (available_port)
317-
portmap_for_child.AllowPort(*available_port);
318-
else {
316+
if (available_port) {
317+
// GetNextAvailablePort() may return 0 if gdbserver_portmap is empty.
318+
if (*available_port)
319+
portmap_for_child.AllowPort(*available_port);
320+
} else {
319321
llvm::consumeError(available_port.takeError());
320322
fprintf(stderr,
321323
"no available gdbserver port for connection - dropping...\n");
@@ -352,7 +354,7 @@ int main_platform(int argc, char *argv[]) {
352354
if (platform.IsConnected()) {
353355
if (inferior_arguments.GetArgumentCount() > 0) {
354356
lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
355-
std::optional<uint16_t> port = 0;
357+
std::optional<uint16_t> port;
356358
std::string socket_name;
357359
Status error = platform.LaunchGDBServer(inferior_arguments,
358360
"", // hostname

0 commit comments

Comments
 (0)