1 //===-- GDBRemoteCommunicationHistory.h--------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_GDBRemoteCommunicationHistory_h_
10 #define liblldb_GDBRemoteCommunicationHistory_h_
15 #include "lldb/Utility/GDBRemote.h"
16 #include "lldb/Utility/Reproducer.h"
17 #include "lldb/lldb-public.h"
18 #include "llvm/Support/YAMLTraits.h"
19 #include "llvm/Support/raw_ostream.h"
21 namespace lldb_private {
25 namespace process_gdb_remote {
27 /// The history keeps a circular buffer of GDB remote packets. The history is
28 /// used for logging and replaying GDB remote packets.
29 class GDBRemoteCommunicationHistory {
31 friend llvm::yaml::MappingTraits<GDBRemoteCommunicationHistory>;
33 GDBRemoteCommunicationHistory(uint32_t size = 0);
35 ~GDBRemoteCommunicationHistory();
37 // For single char packets for ack, nack and /x03
38 void AddPacket(char packet_char, GDBRemotePacket::Type type,
39 uint32_t bytes_transmitted);
41 void AddPacket(const std::string &src, uint32_t src_len,
42 GDBRemotePacket::Type type, uint32_t bytes_transmitted);
44 void Dump(Stream &strm) const;
45 void Dump(Log *log) const;
46 bool DidDumpToLog() const { return m_dumped_to_log; }
48 void SetRecorder(repro::PacketRecorder *recorder) { m_recorder = recorder; }
51 uint32_t GetFirstSavedPacketIndex() const {
52 if (m_total_packet_count < m_packets.size())
55 return m_curr_idx + 1;
58 uint32_t GetNumPacketsInHistory() const {
59 if (m_total_packet_count < m_packets.size())
60 return m_total_packet_count;
62 return (uint32_t)m_packets.size();
65 uint32_t GetNextIndex() {
66 ++m_total_packet_count;
67 const uint32_t idx = m_curr_idx;
68 m_curr_idx = NormalizeIndex(idx + 1);
72 uint32_t NormalizeIndex(uint32_t i) const {
73 return m_packets.empty() ? 0 : i % m_packets.size();
76 std::vector<GDBRemotePacket> m_packets;
78 uint32_t m_total_packet_count;
79 mutable bool m_dumped_to_log;
80 repro::PacketRecorder *m_recorder = nullptr;
83 } // namespace process_gdb_remote
84 } // namespace lldb_private
86 #endif // liblldb_GDBRemoteCommunicationHistory_h_