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