LLDB  mainline
GDBRemote.cpp
Go to the documentation of this file.
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 
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 
21 StreamGDBRemote::StreamGDBRemote() : StreamString() {}
22 
24  ByteOrder byte_order)
25  : StreamString(flags, addr_size, byte_order) {}
26 
28 
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);
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)
46  return bytes_written;
47 }
48 
49 llvm::StringRef GDBRemotePacket::GetTypeStr() const {
50  switch (type) {
52  return "send";
54  return "read";
56  return "invalid";
57  }
58  llvm_unreachable("All enum cases should be handled");
59 }
60 
61 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 
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 
73 void yaml::ScalarTraits<GDBRemotePacket::BinaryData>::output(
74  const GDBRemotePacket::BinaryData &Val, void *, raw_ostream &Out) {
75  Out << toHex(Val.data);
76 }
77 
78 StringRef yaml::ScalarTraits<GDBRemotePacket::BinaryData>::input(
79  StringRef Scalar, void *, GDBRemotePacket::BinaryData &Val) {
80  Val.data = fromHex(Scalar);
81  return {};
82 }
83 
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
94 yaml::MappingTraits<GDBRemotePacket>::validate(IO &io,
95  GDBRemotePacket &Packet) {
96  return {};
97 }
98 
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_TextWithCRLF);
108  if (ec)
109  return;
110  yaml::Output yout(os);
111  yout << files;
112 }
113 
114 void GDBRemoteProvider::Discard() { m_packet_recorders.clear(); }
115 
116 llvm::Expected<std::unique_ptr<PacketRecorder>>
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 
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 
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 
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>(
154  history_file.GetPath(), EC, sys::fs::OpenFlags::OF_TextWithCRLF);
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";
llvm
Definition: Debugger.h:49
lldb_private::Stream::eBinary
@ eBinary
Get and put data as binary instead of as the default string mode.
Definition: Stream.h:32
lldb_private::GDBRemotePacket::packet
BinaryData packet
Definition: GDBRemote.h:76
lldb_private::GDBRemotePacket::Dump
void Dump(Stream &strm) const
Definition: GDBRemote.cpp:61
lldb_private::Scalar
Definition: Scalar.h:34
lldb_private::Flags::Clear
ValueType Clear(ValueType mask=~static_cast< ValueType >(0))
Clear one or more flags.
Definition: Flags.h:61
lldb_private::GDBRemotePacket::packet_idx
uint32_t packet_idx
Definition: GDBRemote.h:79
lldb_private::Flags::Test
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Stream::m_flags
Flags m_flags
Dump flags.
Definition: Stream.h:363
lldb_private::GDBRemotePacket::Type
Type
Definition: GDBRemote.h:56
lldb_private::FileSpec
Definition: FileSpec.h:56
ID
static char ID
Definition: IRDynamicChecks.cpp:32
x24
@ x24
Definition: CompactUnwindInfo.cpp:1241
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::GDBRemotePacket::type
Type type
Definition: GDBRemote.h:77
GDBRemote.h
lldb_private::GDBRemotePacket::GetTypeStr
llvm::StringRef GetTypeStr() const
Definition: GDBRemote.cpp:49
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::Stream::PutChar
size_t PutChar(char ch)
Definition: Stream.cpp:104
lldb_private::FileSpec::CopyByAppendingPathComponent
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
Definition: FileSpec.cpp:400
x23
@ x23
Definition: CompactUnwindInfo.cpp:1240
lldb_private::GDBRemotePacket::ePacketTypeRecv
@ ePacketTypeRecv
Definition: GDBRemote.h:56
lldb_private::GDBRemotePacket::ePacketTypeSend
@ ePacketTypeSend
Definition: GDBRemote.h:56
uint32_t
lldb_private::StreamGDBRemote::StreamGDBRemote
StreamGDBRemote()
Definition: GDBRemote.cpp:21
lldb_private::GDBRemotePacket
GDB remote packet as used by the reproducer and the GDB remote communication history.
Definition: GDBRemote.h:52
lldb_private::GDBRemotePacket::tid
lldb::tid_t tid
Definition: GDBRemote.h:80
lldb_private::GDBRemotePacket::bytes_transmitted
uint32_t bytes_transmitted
Definition: GDBRemote.h:78
lldb_private::repro
Definition: SBReproducer.h:15
lldb_private::StreamGDBRemote::~StreamGDBRemote
~StreamGDBRemote() override
Definition: GDBRemote.cpp:27
lldb_private::GDBRemotePacket::ePacketTypeInvalid
@ ePacketTypeInvalid
Definition: GDBRemote.h:56
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::repro::PacketRecorder
Definition: GDBRemote.h:87
lldb_private::GDBRemotePacket::BinaryData
Definition: GDBRemote.h:70
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::StreamGDBRemote::PutEscapedBytes
int PutEscapedBytes(const void *s, size_t src_len)
Output a block of data to the stream performing GDB-remote escaping.
Definition: GDBRemote.cpp:29
lldb_private::Flags::Set
ValueType Set(ValueType mask)
Set one or more flags by logical OR'ing mask with the current flags.
Definition: Flags.h:73
Flags.h
lldb_private::GDBRemotePacket::BinaryData::data
std::string data
Definition: GDBRemote.h:71
Stream.h
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb
Definition: SBAddress.h:15
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138