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