summaryrefslogtreecommitdiff
path: root/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.h
blob: 85f112b506236445b244675556e3c6cbdfe3139c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//===-- GDBRemoteCommunicationHistory.h--------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_GDBRemoteCommunicationHistory_h_
#define liblldb_GDBRemoteCommunicationHistory_h_

#include <string>
#include <vector>

#include "lldb/lldb-public.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"

namespace lldb_private {
namespace process_gdb_remote {

/// The history keeps a circular buffer of GDB remote packets. The history is
/// used for logging and replaying GDB remote packets.
class GDBRemoteCommunicationHistory {
public:
  friend llvm::yaml::MappingTraits<GDBRemoteCommunicationHistory>;

  enum PacketType { ePacketTypeInvalid = 0, ePacketTypeSend, ePacketTypeRecv };

  /// Entry in the ring buffer containing the packet data, its type, size and
  /// index. Entries can be serialized to file.
  struct Entry {
    Entry()
        : packet(), type(ePacketTypeInvalid), bytes_transmitted(0),
          packet_idx(0), tid(LLDB_INVALID_THREAD_ID) {}

    void Clear() {
      packet.data.clear();
      type = ePacketTypeInvalid;
      bytes_transmitted = 0;
      packet_idx = 0;
      tid = LLDB_INVALID_THREAD_ID;
    }

    struct BinaryData {
      std::string data;
    };

    void Serialize(llvm::raw_ostream &strm) const;

    BinaryData packet;
    PacketType type;
    uint32_t bytes_transmitted;
    uint32_t packet_idx;
    lldb::tid_t tid;
  };

  GDBRemoteCommunicationHistory(uint32_t size = 0);

  ~GDBRemoteCommunicationHistory();

  // For single char packets for ack, nack and /x03
  void AddPacket(char packet_char, PacketType type, uint32_t bytes_transmitted);

  void AddPacket(const std::string &src, uint32_t src_len, PacketType type,
                 uint32_t bytes_transmitted);

  void Dump(Stream &strm) const;
  void Dump(Log *log) const;
  bool DidDumpToLog() const { return m_dumped_to_log; }

  void SetStream(llvm::raw_ostream *strm) { m_stream = strm; }

private:
  uint32_t GetFirstSavedPacketIndex() const {
    if (m_total_packet_count < m_packets.size())
      return 0;
    else
      return m_curr_idx + 1;
  }

  uint32_t GetNumPacketsInHistory() const {
    if (m_total_packet_count < m_packets.size())
      return m_total_packet_count;
    else
      return (uint32_t)m_packets.size();
  }

  uint32_t GetNextIndex() {
    ++m_total_packet_count;
    const uint32_t idx = m_curr_idx;
    m_curr_idx = NormalizeIndex(idx + 1);
    return idx;
  }

  uint32_t NormalizeIndex(uint32_t i) const {
    return m_packets.empty() ? 0 : i % m_packets.size();
  }

  std::vector<Entry> m_packets;
  uint32_t m_curr_idx;
  uint32_t m_total_packet_count;
  mutable bool m_dumped_to_log;
  llvm::raw_ostream *m_stream = nullptr;
};

} // namespace process_gdb_remote
} // namespace lldb_private

LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(
    lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry)

namespace llvm {
namespace yaml {

template <>
struct ScalarEnumerationTraits<lldb_private::process_gdb_remote::
                                   GDBRemoteCommunicationHistory::PacketType> {
  static void enumeration(IO &io,
                          lldb_private::process_gdb_remote::
                              GDBRemoteCommunicationHistory::PacketType &value);
};

template <>
struct ScalarTraits<lldb_private::process_gdb_remote::
                        GDBRemoteCommunicationHistory::Entry::BinaryData> {
  static void output(const lldb_private::process_gdb_remote::
                         GDBRemoteCommunicationHistory::Entry::BinaryData &,
                     void *, raw_ostream &);

  static StringRef
  input(StringRef, void *,
        lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry::
            BinaryData &);

  static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
};

template <>
struct MappingTraits<
    lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry> {
  static void
  mapping(IO &io,
          lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry
              &Entry);

  static StringRef validate(
      IO &io,
      lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry &);
};

} // namespace yaml
} // namespace llvm

#endif // liblldb_GDBRemoteCommunicationHistory_h_