Skip to content

[lldb/platform-gdb] Do not assume a persistent connection #131736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,7 @@ bool PlatformRemoteGDBServer::SetRemoteWorkingDirectory(
}

bool PlatformRemoteGDBServer::IsConnected() const {
if (m_gdb_client_up) {
assert(m_gdb_client_up->IsConnected());
return true;
}
return false;
return m_gdb_client_up && m_gdb_client_up->IsConnected();
}

Status PlatformRemoteGDBServer::ConnectRemote(Args &args) {
Expand Down
1 change: 1 addition & 0 deletions lldb/unittests/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ add_lldb_unittest(LLDBPlatformTests
)

add_subdirectory(Android)
add_subdirectory(gdb-server)
7 changes: 7 additions & 0 deletions lldb/unittests/Platform/gdb-server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_lldb_unittest(PlatformGdbRemoteTests
PlatformRemoteGDBServerTest.cpp

LINK_LIBS
lldbPluginPlatformGDB
LLVMTestingSupport
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//===-- PlatformRemoteGDBServerTest.cpp -----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
#include "lldb/Utility/Connection.h"
#include "gmock/gmock.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::platform_gdb_server;
using namespace lldb_private::process_gdb_remote;
using namespace testing;

namespace {

class PlatformRemoteGDBServerHack : public PlatformRemoteGDBServer {
public:
void
SetGDBClient(std::unique_ptr<GDBRemoteCommunicationClient> gdb_client_up) {
m_gdb_client_up = std::move(gdb_client_up);
}
};

class MockConnection : public lldb_private::Connection {
public:
MOCK_METHOD(lldb::ConnectionStatus, Connect,
(llvm::StringRef url, Status *error_ptr), (override));
MOCK_METHOD(lldb::ConnectionStatus, Disconnect, (Status * error_ptr),
(override));
MOCK_METHOD(bool, IsConnected, (), (const, override));
MOCK_METHOD(size_t, Read,
(void *dst, size_t dst_len, const Timeout<std::micro> &timeout,
lldb::ConnectionStatus &status, Status *error_ptr),
(override));
MOCK_METHOD(size_t, Write,
(const void *dst, size_t dst_len, lldb::ConnectionStatus &status,
Status *error_ptr),
(override));
MOCK_METHOD(std::string, GetURI, (), (override));
MOCK_METHOD(bool, InterruptRead, (), (override));
};

} // namespace

TEST(PlatformRemoteGDBServerTest, IsConnected) {
bool is_connected = true;

auto connection = std::make_unique<NiceMock<MockConnection>>();
ON_CALL(*connection, IsConnected())
.WillByDefault(ReturnPointee(&is_connected));

auto client = std::make_unique<GDBRemoteCommunicationClient>();
client->SetConnection(std::move(connection));

PlatformRemoteGDBServerHack server;
EXPECT_FALSE(server.IsConnected());

server.SetGDBClient(std::move(client));
EXPECT_TRUE(server.IsConnected());

is_connected = false;
EXPECT_FALSE(server.IsConnected());
}