LLDB  mainline
GDBRemoteCommunicationServer.cpp
Go to the documentation of this file.
1 //===-- GDBRemoteCommunicationServer.cpp ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <errno.h>
10 
11 #include "lldb/Host/Config.h"
12 
14 
15 #include <cstring>
16 
17 #include "ProcessGDBRemoteLog.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 using namespace lldb_private::process_gdb_remote;
24 
25 GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(
26  const char *comm_name, const char *listener_name)
27  : GDBRemoteCommunication(comm_name, listener_name), m_exit_now(false) {
30  [this](StringExtractorGDBRemote packet, Status &error, bool &interrupt,
31  bool &quit) { return this->Handle_QErrorStringEnable(packet); });
32 }
33 
35 
38  PacketHandler handler) {
39  m_packet_handlers[packet_type] = std::move(handler);
40 }
41 
44  Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
46 
47  PacketResult packet_result = WaitForPacketNoLock(packet, timeout, false);
48  if (packet_result == PacketResult::Success) {
50  packet.GetServerPacketType();
51  switch (packet_type) {
54  break;
55 
57  error.SetErrorString("invalid packet");
58  quit = true;
59  break;
60 
62  packet_result = SendUnimplementedResponse(packet.GetStringRef().c_str());
63  break;
64 
65  default:
66  auto handler_it = m_packet_handlers.find(packet_type);
67  if (handler_it == m_packet_handlers.end())
68  packet_result =
69  SendUnimplementedResponse(packet.GetStringRef().c_str());
70  else
71  packet_result = handler_it->second(packet, error, interrupt, quit);
72  break;
73  }
74  } else {
75  if (!IsConnected()) {
76  error.SetErrorString("lost connection");
77  quit = true;
78  } else {
79  error.SetErrorString("timeout");
80  }
81  }
82 
83  // Check if anything occurred that would force us to want to exit.
84  if (m_exit_now)
85  quit = true;
86 
87  return packet_result;
88 }
89 
92  // TODO: Log the packet we aren't handling...
93  return SendPacketNoLock("");
94 }
95 
98  char packet[16];
99  int packet_len = ::snprintf(packet, sizeof(packet), "E%2.2x", err);
100  assert(packet_len < (int)sizeof(packet));
101  return SendPacketNoLock(llvm::StringRef(packet, packet_len));
102 }
103 
106  if (m_send_error_strings) {
108  packet.Printf("E%2.2x;", static_cast<uint8_t>(error.GetError()));
109  packet.PutStringAsRawHex8(error.AsCString());
110  return SendPacketNoLock(packet.GetString());
111  } else
112  return SendErrorResponse(error.GetError());
113 }
114 
117  StringExtractorGDBRemote &packet) {
118  m_send_error_strings = true;
119  return SendOKResponse();
120 }
121 
124  const StringExtractorGDBRemote &failed_packet, const char *message) {
126  if (log)
127  log->Printf("GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
128  __FUNCTION__, failed_packet.GetStringRef().c_str(),
129  message ? message : "");
130  return SendErrorResponse(0x03);
131 }
132 
135  return SendPacketNoLock("OK");
136 }
137 
139  return GetAck() == PacketResult::Success;
140 }
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
PacketResult WaitForPacketNoLock(StringExtractorGDBRemote &response, Timeout< std::micro > timeout, bool sync_on_timeout)
std::map< StringExtractorGDBRemote::ServerPacketType, PacketHandler > m_packet_handlers
PacketResult SendIllFormedResponse(const StringExtractorGDBRemote &packet, const char *error_message)
size_t PutStringAsRawHex8(llvm::StringRef s)
Definition: Stream.cpp:432
ServerPacketType GetServerPacketType() const
void RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type, PacketHandler handler)
llvm::StringRef GetString() const
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
bool IsConnected() const
Check if the connection is valid.
PacketResult Handle_QErrorStringEnable(StringExtractorGDBRemote &packet)
Definition: SBAddress.h:15
PacketResult GetPacketAndSendResponse(Timeout< std::micro > timeout, Status &error, bool &interrupt, bool &quit)
std::string & GetStringRef()
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
ValueType GetError() const
Access the error value.
Definition: Status.cpp:174
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
#define GDBR_LOG_PACKETS
std::function< PacketResult(StringExtractorGDBRemote &packet, Status &error, bool &interrupt, bool &quit)> PacketHandler
An error handling class.
Definition: Status.h:44