[Reproducer] Move GDB Remote Packet into Utility. (NFC)

To support dumping the reproducer's GDB remote packets, we need the
(de)serialization logic to live in Utility rather than the GDB remote
plugin. This patch renames StreamGDBRemote to GDBRemote and moves the
relevant packet code there.

Its uses in the GDBRemoteCommunicationHistory and the
GDBRemoteCommunicationReplayServer are updated as well.

Differential revision: https://reviews.llvm.org/D67523

llvm-svn: 371907
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
index bb467df..144ae10 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -103,8 +103,7 @@
   char ch = '+';
   const size_t bytes_written = Write(&ch, 1, status, nullptr);
   LLDB_LOGF(log, "<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
-  m_history.AddPacket(ch, GDBRemoteCommunicationHistory::ePacketTypeSend,
-                      bytes_written);
+  m_history.AddPacket(ch, GDBRemotePacket::ePacketTypeSend, bytes_written);
   return bytes_written;
 }
 
@@ -114,8 +113,7 @@
   char ch = '-';
   const size_t bytes_written = Write(&ch, 1, status, nullptr);
   LLDB_LOGF(log, "<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
-  m_history.AddPacket(ch, GDBRemoteCommunicationHistory::ePacketTypeSend,
-                      bytes_written);
+  m_history.AddPacket(ch, GDBRemotePacket::ePacketTypeSend, bytes_written);
   return bytes_written;
 }
 
@@ -178,8 +176,7 @@
     }
 
     m_history.AddPacket(packet.str(), packet_length,
-                        GDBRemoteCommunicationHistory::ePacketTypeSend,
-                        bytes_written);
+                        GDBRemotePacket::ePacketTypeSend, bytes_written);
 
     if (bytes_written == packet_length) {
       if (!skip_ack && GetSendAcks())
@@ -809,8 +806,7 @@
       }
 
       m_history.AddPacket(m_bytes, total_length,
-                          GDBRemoteCommunicationHistory::ePacketTypeRecv,
-                          total_length);
+                          GDBRemotePacket::ePacketTypeRecv, total_length);
 
       // Copy the packet from m_bytes to packet_str expanding the run-length
       // encoding in the process. Reserve enough byte for the most common case
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
index daee680..898942b 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
@@ -18,7 +18,7 @@
 #include <vector>
 
 #include "lldb/Utility/ArchSpec.h"
-#include "lldb/Utility/StreamGDBRemote.h"
+#include "lldb/Utility/GDBRemote.h"
 #include "lldb/Utility/StructuredData.h"
 #if defined(_WIN32)
 #include "lldb/Host/windows/PosixApi.h"
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.cpp
index f9f67fc..898a6ec 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.cpp
@@ -18,12 +18,6 @@
 using namespace lldb_private;
 using namespace lldb_private::process_gdb_remote;
 
-void GDBRemoteCommunicationHistory::Entry::Serialize(raw_ostream &strm) const {
-  yaml::Output yout(strm);
-  yout << const_cast<GDBRemoteCommunicationHistory::Entry &>(*this);
-  strm.flush();
-}
-
 GDBRemoteCommunicationHistory::GDBRemoteCommunicationHistory(uint32_t size)
     : m_packets(), m_curr_idx(0), m_total_packet_count(0),
       m_dumped_to_log(false) {
@@ -33,7 +27,8 @@
 
 GDBRemoteCommunicationHistory::~GDBRemoteCommunicationHistory() {}
 
-void GDBRemoteCommunicationHistory::AddPacket(char packet_char, PacketType type,
+void GDBRemoteCommunicationHistory::AddPacket(char packet_char,
+                                              GDBRemotePacket::Type type,
                                               uint32_t bytes_transmitted) {
   const size_t size = m_packets.size();
   if (size == 0)
@@ -50,7 +45,8 @@
 }
 
 void GDBRemoteCommunicationHistory::AddPacket(const std::string &src,
-                                              uint32_t src_len, PacketType type,
+                                              uint32_t src_len,
+                                              GDBRemotePacket::Type type,
                                               uint32_t bytes_transmitted) {
   const size_t size = m_packets.size();
   if (size == 0)
@@ -72,12 +68,14 @@
   const uint32_t stop_idx = m_curr_idx + size;
   for (uint32_t i = first_idx; i < stop_idx; ++i) {
     const uint32_t idx = NormalizeIndex(i);
-    const Entry &entry = m_packets[idx];
-    if (entry.type == ePacketTypeInvalid || entry.packet.data.empty())
+    const GDBRemotePacket &entry = m_packets[idx];
+    if (entry.type == GDBRemotePacket::ePacketTypeInvalid ||
+        entry.packet.data.empty())
       break;
     strm.Printf("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n",
                 entry.packet_idx, entry.tid, entry.bytes_transmitted,
-                (entry.type == ePacketTypeSend) ? "send" : "read",
+                (entry.type == GDBRemotePacket::ePacketTypeSend) ? "send"
+                                                                 : "read",
                 entry.packet.data.c_str());
   }
 }
@@ -92,51 +90,15 @@
   const uint32_t stop_idx = m_curr_idx + size;
   for (uint32_t i = first_idx; i < stop_idx; ++i) {
     const uint32_t idx = NormalizeIndex(i);
-    const Entry &entry = m_packets[idx];
-    if (entry.type == ePacketTypeInvalid || entry.packet.data.empty())
+    const GDBRemotePacket &entry = m_packets[idx];
+    if (entry.type == GDBRemotePacket::ePacketTypeInvalid ||
+        entry.packet.data.empty())
       break;
     LLDB_LOGF(log, "history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s",
               entry.packet_idx, entry.tid, entry.bytes_transmitted,
-              (entry.type == ePacketTypeSend) ? "send" : "read",
+              (entry.type == GDBRemotePacket::ePacketTypeSend) ? "send"
+                                                               : "read",
               entry.packet.data.c_str());
   }
 }
 
-void yaml::ScalarEnumerationTraits<GDBRemoteCommunicationHistory::PacketType>::
-    enumeration(IO &io, GDBRemoteCommunicationHistory::PacketType &value) {
-  io.enumCase(value, "Invalid",
-              GDBRemoteCommunicationHistory::ePacketTypeInvalid);
-  io.enumCase(value, "Send", GDBRemoteCommunicationHistory::ePacketTypeSend);
-  io.enumCase(value, "Recv", GDBRemoteCommunicationHistory::ePacketTypeRecv);
-}
-
-void yaml::ScalarTraits<GDBRemoteCommunicationHistory::Entry::BinaryData>::
-    output(const GDBRemoteCommunicationHistory::Entry::BinaryData &Val, void *,
-           raw_ostream &Out) {
-  Out << toHex(Val.data);
-}
-
-StringRef
-yaml::ScalarTraits<GDBRemoteCommunicationHistory::Entry::BinaryData>::input(
-    StringRef Scalar, void *,
-    GDBRemoteCommunicationHistory::Entry::BinaryData &Val) {
-  Val.data = fromHex(Scalar);
-  return {};
-}
-
-void yaml::MappingTraits<GDBRemoteCommunicationHistory::Entry>::mapping(
-    IO &io, GDBRemoteCommunicationHistory::Entry &Entry) {
-  io.mapRequired("packet", Entry.packet);
-  io.mapRequired("type", Entry.type);
-  io.mapRequired("bytes", Entry.bytes_transmitted);
-  io.mapRequired("index", Entry.packet_idx);
-  io.mapRequired("tid", Entry.tid);
-}
-
-StringRef yaml::MappingTraits<GDBRemoteCommunicationHistory::Entry>::validate(
-    IO &io, GDBRemoteCommunicationHistory::Entry &Entry) {
-  if (Entry.bytes_transmitted != Entry.packet.data.size())
-    return "BinaryData size doesn't match bytes transmitted";
-
-  return {};
-}
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.h
index 85f112b..c006fbd 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.h
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationHistory.h
@@ -12,6 +12,7 @@
 #include <string>
 #include <vector>
 
+#include "lldb/Utility/GDBRemote.h"
 #include "lldb/lldb-public.h"
 #include "llvm/Support/YAMLTraits.h"
 #include "llvm/Support/raw_ostream.h"
@@ -25,46 +26,17 @@
 public:
   friend llvm::yaml::MappingTraits<GDBRemoteCommunicationHistory>;
 
-  enum PacketType { ePacketTypeInvalid = 0, ePacketTypeSend, ePacketTypeRecv };
-
-  /// Entry in the ring buffer containing the packet data, its type, size and
-  /// index. Entries can be serialized to file.
-  struct Entry {
-    Entry()
-        : packet(), type(ePacketTypeInvalid), bytes_transmitted(0),
-          packet_idx(0), tid(LLDB_INVALID_THREAD_ID) {}
-
-    void Clear() {
-      packet.data.clear();
-      type = ePacketTypeInvalid;
-      bytes_transmitted = 0;
-      packet_idx = 0;
-      tid = LLDB_INVALID_THREAD_ID;
-    }
-
-    struct BinaryData {
-      std::string data;
-    };
-
-    void Serialize(llvm::raw_ostream &strm) const;
-
-    BinaryData packet;
-    PacketType type;
-    uint32_t bytes_transmitted;
-    uint32_t packet_idx;
-    lldb::tid_t tid;
-  };
-
   GDBRemoteCommunicationHistory(uint32_t size = 0);
 
   ~GDBRemoteCommunicationHistory();
 
   // For single char packets for ack, nack and /x03
-  void AddPacket(char packet_char, PacketType type, uint32_t bytes_transmitted);
-
-  void AddPacket(const std::string &src, uint32_t src_len, PacketType type,
+  void AddPacket(char packet_char, GDBRemotePacket::Type type,
                  uint32_t bytes_transmitted);
 
+  void AddPacket(const std::string &src, uint32_t src_len,
+                 GDBRemotePacket::Type type, uint32_t bytes_transmitted);
+
   void Dump(Stream &strm) const;
   void Dump(Log *log) const;
   bool DidDumpToLog() const { return m_dumped_to_log; }
@@ -97,7 +69,7 @@
     return m_packets.empty() ? 0 : i % m_packets.size();
   }
 
-  std::vector<Entry> m_packets;
+  std::vector<GDBRemotePacket> m_packets;
   uint32_t m_curr_idx;
   uint32_t m_total_packet_count;
   mutable bool m_dumped_to_log;
@@ -107,49 +79,4 @@
 } // namespace process_gdb_remote
 } // namespace lldb_private
 
-LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(
-    lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry)
-
-namespace llvm {
-namespace yaml {
-
-template <>
-struct ScalarEnumerationTraits<lldb_private::process_gdb_remote::
-                                   GDBRemoteCommunicationHistory::PacketType> {
-  static void enumeration(IO &io,
-                          lldb_private::process_gdb_remote::
-                              GDBRemoteCommunicationHistory::PacketType &value);
-};
-
-template <>
-struct ScalarTraits<lldb_private::process_gdb_remote::
-                        GDBRemoteCommunicationHistory::Entry::BinaryData> {
-  static void output(const lldb_private::process_gdb_remote::
-                         GDBRemoteCommunicationHistory::Entry::BinaryData &,
-                     void *, raw_ostream &);
-
-  static StringRef
-  input(StringRef, void *,
-        lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry::
-            BinaryData &);
-
-  static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
-};
-
-template <>
-struct MappingTraits<
-    lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry> {
-  static void
-  mapping(IO &io,
-          lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry
-              &Entry);
-
-  static StringRef validate(
-      IO &io,
-      lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry &);
-};
-
-} // namespace yaml
-} // namespace llvm
-
 #endif // liblldb_GDBRemoteCommunicationHistory_h_
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp
index 39304a6..2d26c55 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.cpp
@@ -128,7 +128,7 @@
   Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
   while (!m_packet_history.empty()) {
     // Pop last packet from the history.
-    GDBRemoteCommunicationHistory::Entry entry = m_packet_history.back();
+    GDBRemotePacket entry = m_packet_history.back();
     m_packet_history.pop_back();
 
     // We've handled the handshake implicitly before. Skip the packet and move
@@ -136,7 +136,7 @@
     if (entry.packet.data == "+")
       continue;
 
-    if (entry.type == GDBRemoteCommunicationHistory::ePacketTypeSend) {
+    if (entry.type == GDBRemotePacket::ePacketTypeSend) {
       if (unexpected(entry.packet.data, packet.GetStringRef())) {
         LLDB_LOG(log,
                  "GDBRemoteCommunicationReplayServer expected packet: '{0}'",
@@ -150,14 +150,14 @@
       // Ignore QEnvironment packets as they're handled earlier.
       if (entry.packet.data.find("QEnvironment") == 1) {
         assert(m_packet_history.back().type ==
-               GDBRemoteCommunicationHistory::ePacketTypeRecv);
+               GDBRemotePacket::ePacketTypeRecv);
         m_packet_history.pop_back();
       }
 
       continue;
     }
 
-    if (entry.type == GDBRemoteCommunicationHistory::ePacketTypeInvalid) {
+    if (entry.type == GDBRemotePacket::ePacketTypeInvalid) {
       LLDB_LOG(
           log,
           "GDBRemoteCommunicationReplayServer skipped invalid packet: '{0}'",
@@ -176,10 +176,6 @@
   return packet_result;
 }
 
-LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(
-    std::vector<
-        lldb_private::process_gdb_remote::GDBRemoteCommunicationHistory::Entry>)
-
 llvm::Error
 GDBRemoteCommunicationReplayServer::LoadReplayHistory(const FileSpec &path) {
   auto error_or_file = MemoryBuffer::getFile(path.GetPath());
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.h
index 26d65e2..0b5e910 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.h
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.h
@@ -62,7 +62,7 @@
   static lldb::thread_result_t AsyncThread(void *arg);
 
   /// Replay history with the oldest packet at the end.
-  std::vector<GDBRemoteCommunicationHistory::Entry> m_packet_history;
+  std::vector<GDBRemotePacket> m_packet_history;
 
   /// Server thread.
   Broadcaster m_async_broadcaster;
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
index d0ae519..4284bc6 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
@@ -29,9 +29,9 @@
 #include "lldb/Symbol/ObjectFile.h"
 #include "lldb/Target/Platform.h"
 #include "lldb/Utility/Endian.h"
+#include "lldb/Utility/GDBRemote.h"
 #include "lldb/Utility/JSON.h"
 #include "lldb/Utility/Log.h"
-#include "lldb/Utility/StreamGDBRemote.h"
 #include "lldb/Utility/StreamString.h"
 #include "lldb/Utility/StructuredData.h"
 #include "llvm/ADT/Triple.h"
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
index bf7860a..ac579ef 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
@@ -11,7 +11,7 @@
 #include "lldb/Host/Config.h"
 
 #include "GDBRemoteCommunicationServerLLGS.h"
-#include "lldb/Utility/StreamGDBRemote.h"
+#include "lldb/Utility/GDBRemote.h"
 
 #include <chrono>
 #include <cstring>
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
index 677f348..83370bd 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
@@ -27,9 +27,9 @@
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Target/Platform.h"
 #include "lldb/Target/UnixSignals.h"
+#include "lldb/Utility/GDBRemote.h"
 #include "lldb/Utility/JSON.h"
 #include "lldb/Utility/Log.h"
-#include "lldb/Utility/StreamGDBRemote.h"
 #include "lldb/Utility/StreamString.h"
 #include "lldb/Utility/StructuredData.h"
 #include "lldb/Utility/UriParser.h"
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
index 84004c1..0e3e3b3 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
@@ -24,8 +24,8 @@
 #include "lldb/Utility/ArchSpec.h"
 #include "lldb/Utility/Broadcaster.h"
 #include "lldb/Utility/ConstString.h"
+#include "lldb/Utility/GDBRemote.h"
 #include "lldb/Utility/Status.h"
-#include "lldb/Utility/StreamGDBRemote.h"
 #include "lldb/Utility/StreamString.h"
 #include "lldb/Utility/StringExtractor.h"
 #include "lldb/Utility/StringList.h"