• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::repro;
18 using namespace lldb_private;
19 using namespace llvm;
20 
StreamGDBRemote()21 StreamGDBRemote::StreamGDBRemote() : StreamString() {}
22 
StreamGDBRemote(uint32_t flags,uint32_t addr_size,ByteOrder byte_order)23 StreamGDBRemote::StreamGDBRemote(uint32_t flags, uint32_t addr_size,
24                                  ByteOrder byte_order)
25     : StreamString(flags, addr_size, byte_order) {}
26 
~StreamGDBRemote()27 StreamGDBRemote::~StreamGDBRemote() {}
28 
PutEscapedBytes(const void * s,size_t src_len)29 int StreamGDBRemote::PutEscapedBytes(const void *s, size_t src_len) {
30   int bytes_written = 0;
31   const uint8_t *src = static_cast<const uint8_t *>(s);
32   bool binary_is_set = m_flags.Test(eBinary);
33   m_flags.Clear(eBinary);
34   while (src_len) {
35     uint8_t byte = *src;
36     src++;
37     src_len--;
38     if (byte == 0x23 || byte == 0x24 || byte == 0x7d || byte == 0x2a) {
39       bytes_written += PutChar(0x7d);
40       byte ^= 0x20;
41     }
42     bytes_written += PutChar(byte);
43   };
44   if (binary_is_set)
45     m_flags.Set(eBinary);
46   return bytes_written;
47 }
48 
GetTypeStr() const49 llvm::StringRef GDBRemotePacket::GetTypeStr() const {
50   switch (type) {
51   case GDBRemotePacket::ePacketTypeSend:
52     return "send";
53   case GDBRemotePacket::ePacketTypeRecv:
54     return "read";
55   case GDBRemotePacket::ePacketTypeInvalid:
56     return "invalid";
57   }
58   llvm_unreachable("All enum cases should be handled");
59 }
60 
Dump(Stream & strm) const61 void GDBRemotePacket::Dump(Stream &strm) const {
62   strm.Printf("tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n", tid,
63               bytes_transmitted, GetTypeStr().data(), packet.data.c_str());
64 }
65 
enumeration(IO & io,GDBRemotePacket::Type & value)66 void yaml::ScalarEnumerationTraits<GDBRemotePacket::Type>::enumeration(
67     IO &io, GDBRemotePacket::Type &value) {
68   io.enumCase(value, "Invalid", GDBRemotePacket::ePacketTypeInvalid);
69   io.enumCase(value, "Send", GDBRemotePacket::ePacketTypeSend);
70   io.enumCase(value, "Recv", GDBRemotePacket::ePacketTypeRecv);
71 }
72 
output(const GDBRemotePacket::BinaryData & Val,void *,raw_ostream & Out)73 void yaml::ScalarTraits<GDBRemotePacket::BinaryData>::output(
74     const GDBRemotePacket::BinaryData &Val, void *, raw_ostream &Out) {
75   Out << toHex(Val.data);
76 }
77 
input(StringRef Scalar,void *,GDBRemotePacket::BinaryData & Val)78 StringRef yaml::ScalarTraits<GDBRemotePacket::BinaryData>::input(
79     StringRef Scalar, void *, GDBRemotePacket::BinaryData &Val) {
80   Val.data = fromHex(Scalar);
81   return {};
82 }
83 
mapping(IO & io,GDBRemotePacket & Packet)84 void yaml::MappingTraits<GDBRemotePacket>::mapping(IO &io,
85                                                    GDBRemotePacket &Packet) {
86   io.mapRequired("packet", Packet.packet);
87   io.mapRequired("type", Packet.type);
88   io.mapRequired("bytes", Packet.bytes_transmitted);
89   io.mapRequired("index", Packet.packet_idx);
90   io.mapRequired("tid", Packet.tid);
91 }
92 
93 StringRef
validate(IO & io,GDBRemotePacket & Packet)94 yaml::MappingTraits<GDBRemotePacket>::validate(IO &io,
95                                                GDBRemotePacket &Packet) {
96   return {};
97 }
98 
Keep()99 void GDBRemoteProvider::Keep() {
100   std::vector<std::string> files;
101   for (auto &recorder : m_packet_recorders) {
102     files.push_back(recorder->GetFilename().GetPath());
103   }
104 
105   FileSpec file = GetRoot().CopyByAppendingPathComponent(Info::file);
106   std::error_code ec;
107   llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
108   if (ec)
109     return;
110   yaml::Output yout(os);
111   yout << files;
112 }
113 
Discard()114 void GDBRemoteProvider::Discard() { m_packet_recorders.clear(); }
115 
116 llvm::Expected<std::unique_ptr<PacketRecorder>>
Create(const FileSpec & filename)117 PacketRecorder::Create(const FileSpec &filename) {
118   std::error_code ec;
119   auto recorder = std::make_unique<PacketRecorder>(std::move(filename), ec);
120   if (ec)
121     return llvm::errorCodeToError(ec);
122   return std::move(recorder);
123 }
124 
GetNewPacketRecorder()125 PacketRecorder *GDBRemoteProvider::GetNewPacketRecorder() {
126   std::size_t i = m_packet_recorders.size() + 1;
127   std::string filename = (llvm::Twine(Info::name) + llvm::Twine("-") +
128                           llvm::Twine(i) + llvm::Twine(".yaml"))
129                              .str();
130   auto recorder_or_error =
131       PacketRecorder::Create(GetRoot().CopyByAppendingPathComponent(filename));
132   if (!recorder_or_error) {
133     llvm::consumeError(recorder_or_error.takeError());
134     return nullptr;
135   }
136 
137   m_packet_recorders.push_back(std::move(*recorder_or_error));
138   return m_packet_recorders.back().get();
139 }
140 
Record(const GDBRemotePacket & packet)141 void PacketRecorder::Record(const GDBRemotePacket &packet) {
142   if (!m_record)
143     return;
144   yaml::Output yout(m_os);
145   yout << const_cast<GDBRemotePacket &>(packet);
146   m_os.flush();
147 }
148 
GetHistoryStream()149 llvm::raw_ostream *GDBRemoteProvider::GetHistoryStream() {
150   FileSpec history_file = GetRoot().CopyByAppendingPathComponent(Info::file);
151 
152   std::error_code EC;
153   m_stream_up = std::make_unique<raw_fd_ostream>(history_file.GetPath(), EC,
154                                                  sys::fs::OpenFlags::OF_Text);
155   return m_stream_up.get();
156 }
157 
158 char GDBRemoteProvider::ID = 0;
159 const char *GDBRemoteProvider::Info::file = "gdb-remote.yaml";
160 const char *GDBRemoteProvider::Info::name = "gdb-remote";
161