blob: c006fbd34a4b94ea5c72f908827b2a843ee8c984 [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
9#ifndef liblldb_GDBRemoteCommunicationHistory_h_
10#define liblldb_GDBRemoteCommunicationHistory_h_
11
12#include <string>
13#include <vector>
14
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000015#include "lldb/Utility/GDBRemote.h"
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000016#include "lldb/lldb-public.h"
17#include "llvm/Support/YAMLTraits.h"
18#include "llvm/Support/raw_ostream.h"
19
20namespace lldb_private {
21namespace process_gdb_remote {
22
23/// The history keeps a circular buffer of GDB remote packets. The history is
24/// used for logging and replaying GDB remote packets.
25class GDBRemoteCommunicationHistory {
26public:
27 friend llvm::yaml::MappingTraits<GDBRemoteCommunicationHistory>;
28
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000029 GDBRemoteCommunicationHistory(uint32_t size = 0);
30
31 ~GDBRemoteCommunicationHistory();
32
33 // For single char packets for ack, nack and /x03
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000034 void AddPacket(char packet_char, GDBRemotePacket::Type type,
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000035 uint32_t bytes_transmitted);
36
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000037 void AddPacket(const std::string &src, uint32_t src_len,
38 GDBRemotePacket::Type type, uint32_t bytes_transmitted);
39
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000040 void Dump(Stream &strm) const;
41 void Dump(Log *log) const;
42 bool DidDumpToLog() const { return m_dumped_to_log; }
43
Eric Christopherc9e0b352019-12-10 12:19:30 -080044 void SetStream(llvm::raw_ostream *strm) { m_stream = strm; }
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000045
46private:
47 uint32_t GetFirstSavedPacketIndex() const {
48 if (m_total_packet_count < m_packets.size())
49 return 0;
50 else
51 return m_curr_idx + 1;
52 }
53
54 uint32_t GetNumPacketsInHistory() const {
55 if (m_total_packet_count < m_packets.size())
56 return m_total_packet_count;
57 else
58 return (uint32_t)m_packets.size();
59 }
60
61 uint32_t GetNextIndex() {
62 ++m_total_packet_count;
63 const uint32_t idx = m_curr_idx;
64 m_curr_idx = NormalizeIndex(idx + 1);
65 return idx;
66 }
67
68 uint32_t NormalizeIndex(uint32_t i) const {
69 return m_packets.empty() ? 0 : i % m_packets.size();
70 }
71
Jonas Devlieghereff5225b2019-09-13 23:14:10 +000072 std::vector<GDBRemotePacket> m_packets;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000073 uint32_t m_curr_idx;
74 uint32_t m_total_packet_count;
75 mutable bool m_dumped_to_log;
Eric Christopherc9e0b352019-12-10 12:19:30 -080076 llvm::raw_ostream *m_stream = nullptr;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000077};
78
79} // namespace process_gdb_remote
80} // namespace lldb_private
81
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000082#endif // liblldb_GDBRemoteCommunicationHistory_h_