xref: /freebsd-src/contrib/llvm-project/lldb/source/Utility/GDBRemote.cpp (revision 9dba64be9536c28e4800e06512b7f29b43ade345)
1 //===-- GDBRemote.cpp -----------------------------------------------------===//
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 "lldb/Utility/GDBRemote.h"
10 
11 #include "lldb/Utility/Flags.h"
12 #include "lldb/Utility/Stream.h"
13 
14 #include <stdio.h>
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 using namespace llvm;
19 
20 StreamGDBRemote::StreamGDBRemote() : StreamString() {}
21 
22 StreamGDBRemote::StreamGDBRemote(uint32_t flags, uint32_t addr_size,
23                                  ByteOrder byte_order)
24     : StreamString(flags, addr_size, byte_order) {}
25 
26 StreamGDBRemote::~StreamGDBRemote() {}
27 
28 int StreamGDBRemote::PutEscapedBytes(const void *s, size_t src_len) {
29   int bytes_written = 0;
30   const uint8_t *src = static_cast<const uint8_t *>(s);
31   bool binary_is_set = m_flags.Test(eBinary);
32   m_flags.Clear(eBinary);
33   while (src_len) {
34     uint8_t byte = *src;
35     src++;
36     src_len--;
37     if (byte == 0x23 || byte == 0x24 || byte == 0x7d || byte == 0x2a) {
38       bytes_written += PutChar(0x7d);
39       byte ^= 0x20;
40     }
41     bytes_written += PutChar(byte);
42   };
43   if (binary_is_set)
44     m_flags.Set(eBinary);
45   return bytes_written;
46 }
47 
48 void GDBRemotePacket::Serialize(raw_ostream &strm) const {
49   yaml::Output yout(strm);
50   yout << const_cast<GDBRemotePacket &>(*this);
51   strm.flush();
52 }
53 
54 llvm::StringRef GDBRemotePacket::GetTypeStr() const {
55   switch (type) {
56   case GDBRemotePacket::ePacketTypeSend:
57     return "send";
58   case GDBRemotePacket::ePacketTypeRecv:
59     return "read";
60   case GDBRemotePacket::ePacketTypeInvalid:
61     return "invalid";
62   }
63   llvm_unreachable("All enum cases should be handled");
64 }
65 
66 void GDBRemotePacket::Dump(Stream &strm) const {
67   strm.Printf("tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n", tid,
68               bytes_transmitted, GetTypeStr().data(), packet.data.c_str());
69 }
70 
71 void yaml::ScalarEnumerationTraits<GDBRemotePacket::Type>::enumeration(
72     IO &io, GDBRemotePacket::Type &value) {
73   io.enumCase(value, "Invalid", GDBRemotePacket::ePacketTypeInvalid);
74   io.enumCase(value, "Send", GDBRemotePacket::ePacketTypeSend);
75   io.enumCase(value, "Recv", GDBRemotePacket::ePacketTypeRecv);
76 }
77 
78 void yaml::ScalarTraits<GDBRemotePacket::BinaryData>::output(
79     const GDBRemotePacket::BinaryData &Val, void *, raw_ostream &Out) {
80   Out << toHex(Val.data);
81 }
82 
83 StringRef yaml::ScalarTraits<GDBRemotePacket::BinaryData>::input(
84     StringRef Scalar, void *, GDBRemotePacket::BinaryData &Val) {
85   Val.data = fromHex(Scalar);
86   return {};
87 }
88 
89 void yaml::MappingTraits<GDBRemotePacket>::mapping(IO &io,
90                                                    GDBRemotePacket &Packet) {
91   io.mapRequired("packet", Packet.packet);
92   io.mapRequired("type", Packet.type);
93   io.mapRequired("bytes", Packet.bytes_transmitted);
94   io.mapRequired("index", Packet.packet_idx);
95   io.mapRequired("tid", Packet.tid);
96 }
97 
98 StringRef
99 yaml::MappingTraits<GDBRemotePacket>::validate(IO &io,
100                                                GDBRemotePacket &Packet) {
101   if (Packet.bytes_transmitted != Packet.packet.data.size())
102     return "BinaryData size doesn't match bytes transmitted";
103 
104   return {};
105 }
106