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 <cstdio>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace llvm;
19
20StreamGDBRemote::StreamGDBRemote() : StreamString() {}
21
22StreamGDBRemote::StreamGDBRemote(uint32_t flags, uint32_t addr_size,
23                                 ByteOrder byte_order)
24    : StreamString(flags, addr_size, byte_order) {}
25
26StreamGDBRemote::~StreamGDBRemote() = default;
27
28int 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
48llvm::StringRef GDBRemotePacket::GetTypeStr() const {
49  switch (type) {
50  case GDBRemotePacket::ePacketTypeSend:
51    return "send";
52  case GDBRemotePacket::ePacketTypeRecv:
53    return "read";
54  case GDBRemotePacket::ePacketTypeInvalid:
55    return "invalid";
56  }
57  llvm_unreachable("All enum cases should be handled");
58}
59
60void GDBRemotePacket::Dump(Stream &strm) const {
61  strm.Printf("tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n", tid,
62              bytes_transmitted, GetTypeStr().data(), packet.data.c_str());
63}
64