Skip to content

Commit 0907b9d

Browse files
slydimanyuxuanchen1997
authored andcommitted
[lldb] Fixed the error unable to launch a GDB server in API tests (#98833)
Summary: TestPlatformLaunchGDBServer.py runs `ldb-server` w/o parameters `--min-gdbserver-port`, `--max-gdbserver-port` or `--gdbserver-port`. So `gdbserver_portmap` is empty and `gdbserver_portmap.GetNextAvailablePort()` will return 0. Do not call `portmap_for_child.AllowPort(0)` in this case. Otherwise `portmap_for_child.GetNextAvailablePort()` will allocate and never free the port 0 and next call `portmap_for_child.GetNextAvailablePort()` will fail. Added few asserts in `GDBRemoteCommunicationServerPlatform::PortMap` to avoid such issue in the future. This patch fixes a bug added in #88845. The behaviour is very close to #97537 w/o parameters `--min-gdbserver-port`, `--max-gdbserver-port` and `--gdbserver-port`. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250877
1 parent 9e419a2 commit 0907b9d

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)