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 void yaml::ScalarEnumerationTraits<GDBRemotePacket::Type>::enumeration(
55     IO &io, GDBRemotePacket::Type &value) {
56   io.enumCase(value, "Invalid", GDBRemotePacket::ePacketTypeInvalid);
57   io.enumCase(value, "Send", GDBRemotePacket::ePacketTypeSend);
58   io.enumCase(value, "Recv", GDBRemotePacket::ePacketTypeRecv);
59 }
60 
61 void yaml::ScalarTraits<GDBRemotePacket::BinaryData>::output(
62     const GDBRemotePacket::BinaryData &Val, void *, raw_ostream &Out) {
63   Out << toHex(Val.data);
64 }
65 
66 StringRef yaml::ScalarTraits<GDBRemotePacket::BinaryData>::input(
67     StringRef Scalar, void *, GDBRemotePacket::BinaryData &Val) {
68   Val.data = fromHex(Scalar);
69   return {};
70 }
71 
72 void yaml::MappingTraits<GDBRemotePacket>::mapping(IO &io,
73                                                    GDBRemotePacket &Packet) {
74   io.mapRequired("packet", Packet.packet);
75   io.mapRequired("type", Packet.type);
76   io.mapRequired("bytes", Packet.bytes_transmitted);
77   io.mapRequired("index", Packet.packet_idx);
78   io.mapRequired("tid", Packet.tid);
79 }
80 
81 StringRef
82 yaml::MappingTraits<GDBRemotePacket>::validate(IO &io,
83                                                GDBRemotePacket &Packet) {
84   if (Packet.bytes_transmitted != Packet.packet.data.size())
85     return "BinaryData size doesn't match bytes transmitted";
86 
87   return {};
88 }
89