blob: e783e59c34559330768ae69d57f3210b965a1178 [file] [log] [blame]
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00001//===-- GDBRemoteCommunicationHistory.h--------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jonas Devlieghere9e046f02018-11-13 19:18:16 +00006//
7//===----------------------------------------------------------------------===//
8
Jonas Devliegherecdc514e2020-02-17 15:57:45 -08009#ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONHISTORY_H
10#define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONHISTORY_H
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000011
12#include <string>
13#include <vector>
14
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000015#include "lldb/Utility/GDBRemote.h"
Eric Christopher1d41d1b2019-12-10 15:04:02 -080016#include "lldb/Utility/Reproducer.h"
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000017#include "lldb/lldb-public.h"
18#include "llvm/Support/YAMLTraits.h"
19#include "llvm/Support/raw_ostream.h"
20
21namespace lldb_private {
Eric Christopher1d41d1b2019-12-10 15:04:02 -080022namespace repro {
23class PacketRecorder;
24}
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000025namespace process_gdb_remote {
26
27/// The history keeps a circular buffer of GDB remote packets. The history is
28/// used for logging and replaying GDB remote packets.
29class GDBRemoteCommunicationHistory {
30public:
31 friend llvm::yaml::MappingTraits<GDBRemoteCommunicationHistory>;
32
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000033 GDBRemoteCommunicationHistory(uint32_t size = 0);
34
35 ~GDBRemoteCommunicationHistory();
36
37 // For single char packets for ack, nack and /x03
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000038 void AddPacket(char packet_char, GDBRemotePacket::Type type,
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000039 uint32_t bytes_transmitted);
40
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000041 void AddPacket(const std::string &src, uint32_t src_len,
42 GDBRemotePacket::Type type, uint32_t bytes_transmitted);
43
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000044 void Dump(Stream &strm) const;
45 void Dump(Log *log) const;
46 bool DidDumpToLog() const { return m_dumped_to_log; }
47
Eric Christopher1d41d1b2019-12-10 15:04:02 -080048 void SetRecorder(repro::PacketRecorder *recorder) { m_recorder = recorder; }
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000049
50private:
51 uint32_t GetFirstSavedPacketIndex() const {
52 if (m_total_packet_count < m_packets.size())
53 return 0;
54 else
55 return m_curr_idx + 1;
56 }
57
58 uint32_t GetNumPacketsInHistory() const {
59 if (m_total_packet_count < m_packets.size())
60 return m_total_packet_count;
61 else
62 return (uint32_t)m_packets.size();
63 }
64
65 uint32_t GetNextIndex() {
66 ++m_total_packet_count;
67 const uint32_t idx = m_curr_idx;
68 m_curr_idx = NormalizeIndex(idx + 1);
69 return idx;
70 }
71
72 uint32_t NormalizeIndex(uint32_t i) const {
73 return m_packets.empty() ? 0 : i % m_packets.size();
74 }
75
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000076 std::vector<GDBRemotePacket> m_packets;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000077 uint32_t m_curr_idx;
78 uint32_t m_total_packet_count;
79 mutable bool m_dumped_to_log;
Eric Christopher1d41d1b2019-12-10 15:04:02 -080080 repro::PacketRecorder *m_recorder = nullptr;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000081};
82
83} // namespace process_gdb_remote
84} // namespace lldb_private
85
Jonas Devliegherecdc514e2020-02-17 15:57:45 -080086#endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONHISTORY_H