blob: 026f78117a0c4d37d948990ce2fa2621becbc2af [file] [log] [blame]
Greg Clayton576d8832011-03-22 04:00:09 +00001//===-- GDBRemoteCommunicationServer.cpp ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Sylvestre Ledrud28b9932013-09-28 15:23:41 +000010#include <errno.h>
Greg Clayton576d8832011-03-22 04:00:09 +000011
Zachary Turner0ec7baa2014-07-01 00:18:46 +000012#include "lldb/Host/Config.h"
13
Greg Clayton576d8832011-03-22 04:00:09 +000014#include "GDBRemoteCommunicationServer.h"
15
Todd Fialaaf245d12014-06-30 21:05:18 +000016#include <cstring>
Greg Clayton576d8832011-03-22 04:00:09 +000017
Greg Clayton576d8832011-03-22 04:00:09 +000018#include "ProcessGDBRemoteLog.h"
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +000019#include "lldb/Utility/StreamString.h"
Pavel Labath9af71b32018-03-20 16:14:00 +000020#include "lldb/Utility/StringExtractorGDBRemote.h"
Greg Clayton576d8832011-03-22 04:00:09 +000021
22using namespace lldb;
23using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000024using namespace lldb_private::process_gdb_remote;
Greg Clayton576d8832011-03-22 04:00:09 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(
27 const char *comm_name, const char *listener_name)
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +000028 : GDBRemoteCommunication(comm_name, listener_name), m_exit_now(false) {
29 RegisterPacketHandler(
30 StringExtractorGDBRemote::eServerPacketType_QEnableErrorStrings,
31 [this](StringExtractorGDBRemote packet, Status &error, bool &interrupt,
32 bool &quit) { return this->Handle_QErrorStringEnable(packet); });
33}
Todd Fialaaf245d12014-06-30 21:05:18 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() {}
Greg Clayton576d8832011-03-22 04:00:09 +000036
Tamas Berghammere13c2732015-02-11 10:29:30 +000037void GDBRemoteCommunicationServer::RegisterPacketHandler(
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 StringExtractorGDBRemote::ServerPacketType packet_type,
39 PacketHandler handler) {
40 m_packet_handlers[packet_type] = std::move(handler);
Tamas Berghammere13c2732015-02-11 10:29:30 +000041}
42
Todd Fialaaf245d12014-06-30 21:05:18 +000043GDBRemoteCommunication::PacketResult
Pavel Labath1eff73c2016-11-24 10:54:49 +000044GDBRemoteCommunicationServer::GetPacketAndSendResponse(
Zachary Turner97206d52017-05-12 04:51:55 +000045 Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 StringExtractorGDBRemote packet;
Todd Fialaaf245d12014-06-30 21:05:18 +000047
Pavel Labath1eff73c2016-11-24 10:54:49 +000048 PacketResult packet_result = WaitForPacketNoLock(packet, timeout, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 if (packet_result == PacketResult::Success) {
50 const StringExtractorGDBRemote::ServerPacketType packet_type =
51 packet.GetServerPacketType();
52 switch (packet_type) {
53 case StringExtractorGDBRemote::eServerPacketType_nack:
54 case StringExtractorGDBRemote::eServerPacketType_ack:
55 break;
Greg Clayton576d8832011-03-22 04:00:09 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 case StringExtractorGDBRemote::eServerPacketType_invalid:
58 error.SetErrorString("invalid packet");
59 quit = true;
60 break;
Greg Claytond314e812011-03-23 00:09:55 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 case StringExtractorGDBRemote::eServerPacketType_unimplemented:
63 packet_result = SendUnimplementedResponse(packet.GetStringRef().c_str());
64 break;
Greg Clayton576d8832011-03-22 04:00:09 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 default:
67 auto handler_it = m_packet_handlers.find(packet_type);
68 if (handler_it == m_packet_handlers.end())
69 packet_result =
70 SendUnimplementedResponse(packet.GetStringRef().c_str());
71 else
72 packet_result = handler_it->second(packet, error, interrupt, quit);
73 break;
Greg Clayton576d8832011-03-22 04:00:09 +000074 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 } else {
76 if (!IsConnected()) {
77 error.SetErrorString("lost connection");
78 quit = true;
79 } else {
80 error.SetErrorString("timeout");
Greg Clayton1cb64962011-03-24 04:28:38 +000081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
Todd Fialaaf245d12014-06-30 21:05:18 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 // Check if anything occurred that would force us to want to exit.
85 if (m_exit_now)
86 quit = true;
Todd Fialaaf245d12014-06-30 21:05:18 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 return packet_result;
Greg Clayton576d8832011-03-22 04:00:09 +000089}
90
Greg Clayton3dedae12013-12-06 21:45:27 +000091GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +000092GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) {
93 // TODO: Log the packet we aren't handling...
94 return SendPacketNoLock("");
Greg Clayton32e0a752011-03-30 18:16:51 +000095}
96
Todd Fialaaf245d12014-06-30 21:05:18 +000097GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +000098GDBRemoteCommunicationServer::SendErrorResponse(uint8_t err) {
99 char packet[16];
100 int packet_len = ::snprintf(packet, sizeof(packet), "E%2.2x", err);
101 assert(packet_len < (int)sizeof(packet));
102 return SendPacketNoLock(llvm::StringRef(packet, packet_len));
Todd Fialaaf245d12014-06-30 21:05:18 +0000103}
Greg Clayton32e0a752011-03-30 18:16:51 +0000104
Greg Clayton3dedae12013-12-06 21:45:27 +0000105GDBRemoteCommunication::PacketResult
Ravitheja Addepallydab1d5f2017-07-12 11:15:34 +0000106GDBRemoteCommunicationServer::SendErrorResponse(const Status &error) {
107 if (m_send_error_strings) {
108 lldb_private::StreamString packet;
109 packet.Printf("E%2.2x;", static_cast<uint8_t>(error.GetError()));
110 packet.PutCStringAsRawHex8(error.AsCString());
111 return SendPacketNoLock(packet.GetString());
112 } else
113 return SendErrorResponse(error.GetError());
114}
115
116GDBRemoteCommunication::PacketResult
117GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
118 StringExtractorGDBRemote &packet) {
119 m_send_error_strings = true;
120 return SendOKResponse();
121}
122
123GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124GDBRemoteCommunicationServer::SendIllFormedResponse(
125 const StringExtractorGDBRemote &failed_packet, const char *message) {
126 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
127 if (log)
128 log->Printf("GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
129 __FUNCTION__, failed_packet.GetStringRef().c_str(),
130 message ? message : "");
131 return SendErrorResponse(0x03);
Greg Clayton1cb64962011-03-24 04:28:38 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134GDBRemoteCommunication::PacketResult
135GDBRemoteCommunicationServer::SendOKResponse() {
136 return SendPacketNoLock("OK");
137}
138
139bool GDBRemoteCommunicationServer::HandshakeWithClient() {
140 return GetAck() == PacketResult::Success;
Greg Clayton1cb64962011-03-24 04:28:38 +0000141}