1 //===-- GDBRemoteCommunicationReplayServer.cpp ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/Host/Config.h"
14 #include "GDBRemoteCommunicationReplayServer.h"
15 #include "ProcessGDBRemoteLog.h"
22 #include "lldb/Host/ThreadLauncher.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/Utility/Event.h"
25 #include "lldb/Utility/FileSpec.h"
26 #include "lldb/Utility/StreamString.h"
27 #include "lldb/Utility/StringExtractorGDBRemote.h"
31 using namespace lldb_private;
32 using namespace lldb_private::process_gdb_remote;
34 GDBRemoteCommunicationReplayServer::GDBRemoteCommunicationReplayServer()
35 : GDBRemoteCommunication("gdb-remote.server",
36 "gdb-remote.server.rx_packet"),
37 m_async_broadcaster(nullptr, "lldb.gdb-remote.server.async-broadcaster"),
39 Listener::MakeListener("lldb.gdb-remote.server.async-listener")),
40 m_async_thread_state_mutex(), m_skip_acks(false) {
41 m_async_broadcaster.SetEventName(eBroadcastBitAsyncContinue,
42 "async thread continue");
43 m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadShouldExit,
44 "async thread should exit");
46 const uint32_t async_event_mask =
47 eBroadcastBitAsyncContinue | eBroadcastBitAsyncThreadShouldExit;
48 m_async_listener_sp->StartListeningForEvents(&m_async_broadcaster,
52 GDBRemoteCommunicationReplayServer::~GDBRemoteCommunicationReplayServer() {
56 GDBRemoteCommunication::PacketResult
57 GDBRemoteCommunicationReplayServer::GetPacketAndSendResponse(
58 Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
59 StringExtractorGDBRemote packet;
60 PacketResult packet_result = WaitForPacketNoLock(packet, timeout, false);
62 if (packet_result != PacketResult::Success) {
64 error.SetErrorString("lost connection");
67 error.SetErrorString("timeout");
72 m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncContinue);
75 const StringExtractorGDBRemote::ServerPacketType packet_type =
76 packet.GetServerPacketType();
77 switch (packet_type) {
78 case StringExtractorGDBRemote::eServerPacketType_nack:
79 case StringExtractorGDBRemote::eServerPacketType_ack:
80 return PacketResult::Success;
84 } else if (packet.GetStringRef() == "QStartNoAckMode") {
89 while (!m_packet_history.empty()) {
90 // Pop last packet from the history.
91 GDBRemoteCommunicationHistory::Entry entry = m_packet_history.back();
92 m_packet_history.pop_back();
94 // We only care about what we received from the server. Skip everything
96 if (entry.type != GDBRemoteCommunicationHistory::ePacketTypeRecv)
99 return SendRawPacketNoLock(entry.packet.data, true);
104 return packet_result;
107 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(
109 lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry>)
112 GDBRemoteCommunicationReplayServer::LoadReplayHistory(const FileSpec &path) {
113 auto error_or_file = MemoryBuffer::getFile(path.GetPath());
114 if (auto err = error_or_file.getError())
115 return errorCodeToError(err);
117 yaml::Input yin((*error_or_file)->getBuffer());
118 yin >> m_packet_history;
120 if (auto err = yin.error())
121 return errorCodeToError(err);
123 // We want to manipulate the vector like a stack so we need to reverse the
124 // order of the packets to have the oldest on at the back.
125 std::reverse(m_packet_history.begin(), m_packet_history.end());
127 return Error::success();
130 bool GDBRemoteCommunicationReplayServer::StartAsyncThread() {
131 std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex);
132 if (!m_async_thread.IsJoinable()) {
133 // Create a thread that watches our internal state and controls which
134 // events make it to clients (into the DCProcess event queue).
135 m_async_thread = ThreadLauncher::LaunchThread(
136 "<lldb.gdb-remote.server.async>",
137 GDBRemoteCommunicationReplayServer::AsyncThread, this, nullptr);
140 // Wait for handshake.
141 m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncContinue);
143 return m_async_thread.IsJoinable();
146 void GDBRemoteCommunicationReplayServer::StopAsyncThread() {
147 std::lock_guard<std::recursive_mutex> guard(m_async_thread_state_mutex);
149 if (!m_async_thread.IsJoinable())
152 // Request thread to stop.
153 m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncThreadShouldExit);
155 // Disconnect client.
159 m_async_thread.Join(nullptr);
160 m_async_thread.Reset();
163 void GDBRemoteCommunicationReplayServer::ReceivePacket(
164 GDBRemoteCommunicationReplayServer &server, bool &done) {
167 auto packet_result = server.GetPacketAndSendResponse(std::chrono::seconds(1),
168 error, interrupt, done);
169 if (packet_result != GDBRemoteCommunication::PacketResult::Success &&
171 GDBRemoteCommunication::PacketResult::ErrorReplyTimeout) {
174 server.m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncContinue);
178 thread_result_t GDBRemoteCommunicationReplayServer::AsyncThread(void *arg) {
179 GDBRemoteCommunicationReplayServer *server =
180 (GDBRemoteCommunicationReplayServer *)arg;
186 if (server->m_async_listener_sp->GetEvent(event_sp, llvm::None)) {
187 const uint32_t event_type = event_sp->GetType();
188 if (event_sp->BroadcasterIs(&server->m_async_broadcaster)) {
189 switch (event_type) {
190 case eBroadcastBitAsyncContinue:
191 ReceivePacket(*server, done);
195 case eBroadcastBitAsyncThreadShouldExit: