1 //===-- GDBRemoteCommunicationHistory.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 "GDBRemoteCommunicationHistory.h"
10 
11 // Other libraries and framework includes
12 #include "lldb/Utility/ConstString.h"
13 #include "lldb/Utility/Log.h"
14 
15 using namespace llvm;
16 using namespace lldb;
17 using namespace lldb_private;
18 using namespace lldb_private::process_gdb_remote;
19 
GDBRemoteCommunicationHistory(uint32_t size)20 GDBRemoteCommunicationHistory::GDBRemoteCommunicationHistory(uint32_t size)
21     : m_packets() {
22   if (size)
23     m_packets.resize(size);
24 }
25 
26 GDBRemoteCommunicationHistory::~GDBRemoteCommunicationHistory() = default;
27 
AddPacket(char packet_char,GDBRemotePacket::Type type,uint32_t bytes_transmitted)28 void GDBRemoteCommunicationHistory::AddPacket(char packet_char,
29                                               GDBRemotePacket::Type type,
30                                               uint32_t bytes_transmitted) {
31   const size_t size = m_packets.size();
32   if (size == 0)
33     return;
34 
35   const uint32_t idx = GetNextIndex();
36   m_packets[idx].packet.data.assign(1, packet_char);
37   m_packets[idx].type = type;
38   m_packets[idx].bytes_transmitted = bytes_transmitted;
39   m_packets[idx].packet_idx = m_total_packet_count;
40   m_packets[idx].tid = llvm::get_threadid();
41 }
42 
AddPacket(const std::string & src,uint32_t src_len,GDBRemotePacket::Type type,uint32_t bytes_transmitted)43 void GDBRemoteCommunicationHistory::AddPacket(const std::string &src,
44                                               uint32_t src_len,
45                                               GDBRemotePacket::Type type,
46                                               uint32_t bytes_transmitted) {
47   const size_t size = m_packets.size();
48   if (size == 0)
49     return;
50 
51   const uint32_t idx = GetNextIndex();
52   m_packets[idx].packet.data.assign(src, 0, src_len);
53   m_packets[idx].type = type;
54   m_packets[idx].bytes_transmitted = bytes_transmitted;
55   m_packets[idx].packet_idx = m_total_packet_count;
56   m_packets[idx].tid = llvm::get_threadid();
57 }
58 
Dump(Stream & strm) const59 void GDBRemoteCommunicationHistory::Dump(Stream &strm) const {
60   const uint32_t size = GetNumPacketsInHistory();
61   const uint32_t first_idx = GetFirstSavedPacketIndex();
62   const uint32_t stop_idx = m_curr_idx + size;
63   for (uint32_t i = first_idx; i < stop_idx; ++i) {
64     const uint32_t idx = NormalizeIndex(i);
65     const GDBRemotePacket &entry = m_packets[idx];
66     if (entry.type == GDBRemotePacket::ePacketTypeInvalid ||
67         entry.packet.data.empty())
68       break;
69     strm.Printf("history[%u] ", entry.packet_idx);
70     entry.Dump(strm);
71   }
72 }
73 
Dump(Log * log) const74 void GDBRemoteCommunicationHistory::Dump(Log *log) const {
75   if (!log || m_dumped_to_log)
76     return;
77 
78   m_dumped_to_log = true;
79   const uint32_t size = GetNumPacketsInHistory();
80   const uint32_t first_idx = GetFirstSavedPacketIndex();
81   const uint32_t stop_idx = m_curr_idx + size;
82   for (uint32_t i = first_idx; i < stop_idx; ++i) {
83     const uint32_t idx = NormalizeIndex(i);
84     const GDBRemotePacket &entry = m_packets[idx];
85     if (entry.type == GDBRemotePacket::ePacketTypeInvalid ||
86         entry.packet.data.empty())
87       break;
88     LLDB_LOGF(log, "history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s",
89               entry.packet_idx, entry.tid, entry.bytes_transmitted,
90               (entry.type == GDBRemotePacket::ePacketTypeSend) ? "send"
91                                                                : "read",
92               entry.packet.data.c_str());
93   }
94 }
95 
96