Skip to content

Commit db4c6e6

Browse files
qxy11clayborg
authored andcommitted
Fix Mock GPU Plugin disconnect issue
1 parent 4bae3a6 commit db4c6e6

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed

lldb/tools/lldb-server/Plugins/MockGPU/LLDBServerPluginMockGPU.cpp

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <sys/types.h>
1919
#include <sys/uio.h>
2020
#include <unistd.h>
21-
#include <thread>
2221

2322
using namespace lldb;
2423
using namespace lldb_private;
@@ -95,41 +94,6 @@ bool LLDBServerPluginMockGPU::HandleEventFileDescriptorEvent(int fd) {
9594
return false;
9695
}
9796

98-
void LLDBServerPluginMockGPU::AcceptAndMainLoopThread(
99-
std::unique_ptr<TCPSocket> listen_socket_up) {
100-
Log *log = GetLog(GDBRLog::Plugin);
101-
LLDB_LOGF(log, "%s spawned", __PRETTY_FUNCTION__);
102-
Socket *socket = nullptr;
103-
Status error = listen_socket_up->Accept(std::chrono::seconds(30), socket);
104-
// Scope for lock guard.
105-
{
106-
// Protect access to m_is_listening and m_is_connected.
107-
std::lock_guard<std::mutex> guard(m_connect_mutex);
108-
m_is_listening = false;
109-
if (error.Fail()) {
110-
LLDB_LOGF(log, "%s error returned from Accept(): %s", __PRETTY_FUNCTION__,
111-
error.AsCString());
112-
return;
113-
}
114-
m_is_connected = true;
115-
}
116-
117-
LLDB_LOGF(log, "%s initializing connection", __PRETTY_FUNCTION__);
118-
std::unique_ptr<Connection> connection_up(
119-
new ConnectionFileDescriptor(std::unique_ptr<Socket>(socket)));
120-
m_gdb_server->InitializeConnection(std::move(connection_up));
121-
LLDB_LOGF(log, "%s running main loop", __PRETTY_FUNCTION__);
122-
m_main_loop_status = m_main_loop.Run();
123-
LLDB_LOGF(log, "%s main loop exited!", __PRETTY_FUNCTION__);
124-
if (m_main_loop_status.Fail()) {
125-
LLDB_LOGF(log, "%s main loop exited with an error: %s", __PRETTY_FUNCTION__,
126-
m_main_loop_status.AsCString());
127-
128-
}
129-
// Protect access to m_is_connected.
130-
std::lock_guard<std::mutex> guard(m_connect_mutex);
131-
m_is_connected = false;
132-
}
13397

13498
std::optional<GPUPluginConnectionInfo> LLDBServerPluginMockGPU::CreateConnection() {
13599
std::lock_guard<std::mutex> guard(m_connect_mutex);
@@ -153,9 +117,20 @@ std::optional<GPUPluginConnectionInfo> LLDBServerPluginMockGPU::CreateConnection
153117
connection_info.connect_url = llvm::formatv("connect://localhost:{}",
154118
listen_port);
155119
LLDB_LOGF(log, "%s listening to %u", __PRETTY_FUNCTION__, listen_port);
156-
std::thread t(&LLDBServerPluginMockGPU::AcceptAndMainLoopThread, this,
157-
std::move(*sock));
158-
t.detach();
120+
121+
m_listen_socket = std::move(*sock);
122+
m_is_connected = false;
123+
auto res = m_listen_socket->Accept(m_main_loop, [this](std::unique_ptr<Socket> socket) {
124+
std::unique_ptr<Connection> connection_up(new ConnectionFileDescriptor(std::move(socket)));
125+
this->m_gdb_server->InitializeConnection(std::move(connection_up));
126+
m_is_connected = true;
127+
});
128+
if (res) {
129+
m_read_handles = std::move(*res);
130+
} else {
131+
LLDB_LOGF(log, "MockGPU failed to accept: %s", llvm::toString(res.takeError()).c_str());
132+
}
133+
159134
return connection_info;
160135
} else {
161136
std::string error = llvm::toString(sock.takeError());

lldb/tools/lldb-server/Plugins/MockGPU/LLDBServerPluginMockGPU.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#define LLDB_TOOLS_LLDB_SERVER_LLDBSERVERPLUGINMOCKGPU_H
1111

1212
#include "Plugins/Process/gdb-remote/LLDBServerPlugin.h"
13+
#include "lldb/Host/MainLoopBase.h"
1314
#include "lldb/Utility/Status.h"
15+
#include <mutex>
1416

1517
// This is a mock GPU plugin that is used for testing the LLDBServerPlugin. It
1618
// should be run with the following code as the main binary:
@@ -61,7 +63,8 @@ namespace lldb_server {
6163

6264
class LLDBServerPluginMockGPU : public LLDBServerPlugin {
6365
public:
64-
LLDBServerPluginMockGPU(LLDBServerPlugin::GDBServer &native_process, MainLoop &main_loop);
66+
LLDBServerPluginMockGPU(LLDBServerPlugin::GDBServer &native_process,
67+
MainLoop &main_loop);
6568
~LLDBServerPluginMockGPU() override;
6669
llvm::StringRef GetPluginName() override;
6770
int GetEventFileDescriptorAtIndex(size_t idx) override;
@@ -74,11 +77,14 @@ class LLDBServerPluginMockGPU : public LLDBServerPlugin {
7477
private:
7578
std::optional<GPUPluginConnectionInfo> CreateConnection();
7679
void CloseFDs();
77-
void AcceptAndMainLoopThread(std::unique_ptr<TCPSocket> listen_socket_up);
7880

7981
// Used with a socketpair to get events on the native ptrace event queue.
8082
int m_fds[2] = {-1, -1};
8183
Status m_main_loop_status;
84+
85+
// Connection state tracking
86+
std::unique_ptr<TCPSocket> m_listen_socket;
87+
std::vector<MainLoopBase::ReadHandleUP> m_read_handles;
8288
};
8389

8490
} // namespace lldb_server

0 commit comments

Comments
 (0)