[Lldb-commits] [lldb] [llvm] [lldb] Update StreamGDBRemote to support llvm::ArrayRef (PR #203175)

via lldb-commits lldb-commits at lists.llvm.org
Wed Jun 10 23:00:04 PDT 2026


https://github.com/qcoelh0 updated https://github.com/llvm/llvm-project/pull/203175

>From b5e2a2b7075dc7859628a29b76325247bc57783a Mon Sep 17 00:00:00 2001
From: Quentin Coelho <qcoelho at nvidia.com>
Date: Wed, 10 Jun 2026 20:53:51 -0700
Subject: [PATCH] [lldb] Update StreamGDBRemote to support llvm::ArrayRef

Replace the raw ptr/size and std::vector interface with llvm::ArrayRef,
which bundles the pointer and length, make iteration safer, and accepts
any contiguous byte container without copying.
A small overload is retained for backward compatibility.
Also fix the stale llvm::arrayRefFromStringRef doc comment, which
described the inverse conversion.
NFC to GDB-remote packet output.
---
 lldb/include/lldb/Utility/GDBRemote.h         | 26 ++++++++++---------
 .../GDBRemoteCommunicationServerLLGS.cpp      |  3 ++-
 .../Process/gdb-remote/ProcessGDBRemote.cpp   |  3 ++-
 lldb/source/Utility/GDBRemote.cpp             | 12 ++-------
 llvm/include/llvm/ADT/StringExtras.h          |  2 +-
 5 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/lldb/include/lldb/Utility/GDBRemote.h b/lldb/include/lldb/Utility/GDBRemote.h
index e8d4f1220f56c..8027ee37e8831 100644
--- a/lldb/include/lldb/Utility/GDBRemote.h
+++ b/lldb/include/lldb/Utility/GDBRemote.h
@@ -13,6 +13,7 @@
 #include "lldb/Utility/StreamString.h"
 #include "lldb/lldb-enumerations.h"
 #include "lldb/lldb-public.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/JSON.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -33,19 +34,20 @@ class StreamGDBRemote : public StreamString {
 
   /// Output a block of data to the stream performing GDB-remote escaping.
   ///
-  /// \param[in] s
+  /// \param[in] bytes
   ///     A block of data.
   ///
-  /// \param[in] src_len
-  ///     The amount of data to write.
-  ///
   /// \return
   ///     Number of bytes written.
-  // TODO: Convert this function to take ArrayRef<uint8_t>
-  int PutEscapedBytes(const void *s, size_t src_len);
-
-  /// Equivalent to PutEscapedBytes(str.data(), str.size());
-  int PutEscapedBytes(llvm::StringRef str);
+  int PutEscapedBytes(llvm::ArrayRef<uint8_t> bytes);
+
+  /// \overload
+  /// This overload is provided for backward compatibility with the existing
+  /// code. The newer interface is to use the ArrayRef<uint8_t> overload.
+  int PutEscapedBytes(const void *src, size_t len) {
+    return PutEscapedBytes(
+        llvm::ArrayRef<uint8_t>(static_cast<const uint8_t *>(src), len));
+  }
 
   template <class T> int PutAsJSON(const T &obj, bool hex_ascii) {
     std::string json_string;
@@ -53,11 +55,11 @@ class StreamGDBRemote : public StreamString {
     os << llvm::json::Value(toJSON(obj));
     if (hex_ascii)
       return PutStringAsRawHex8(json_string);
-    return PutEscapedBytes(json_string.c_str(), json_string.size());
+    return PutEscapedBytes(llvm::arrayRefFromStringRef(json_string));
   }
 
   template <class T>
-  int PutAsJSONArray(const std::vector<T> &array, bool hex_ascii) {
+  int PutAsJSONArray(llvm::ArrayRef<T> array, bool hex_ascii) {
     llvm::json::Array json_array;
     for (const auto &obj : array)
       json_array.push_back(toJSON(obj));
@@ -66,7 +68,7 @@ class StreamGDBRemote : public StreamString {
     os << llvm::json::Value(std::move(json_array));
     if (hex_ascii)
       return PutStringAsRawHex8(json_string);
-    return PutEscapedBytes(json_string.data(), json_string.size());
+    return PutEscapedBytes(llvm::arrayRefFromStringRef(json_string));
   }
 };
 
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
index 0f14231cadbd5..5dc1639f303fc 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
@@ -4592,7 +4592,8 @@ GDBRemoteCommunicationServerLLGS::Handle_jAcceleratorPluginInitialize(
       accelerator_actions.push_back(std::move(*actions));
   }
   StreamGDBRemote response;
-  response.PutAsJSONArray(accelerator_actions, /*hex_ascii=*/false);
+  response.PutAsJSONArray<AcceleratorActions>(accelerator_actions,
+                                              /*hex_ascii=*/false);
   return SendPacketNoLock(response.GetString());
 }
 
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index 2fc6dbb546f79..496002239e95b 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -6784,7 +6784,8 @@ llvm::Error ProcessGDBRemote::UpdateBreakpointSites(
   packet_dict.Dump(stream, false);
 
   StreamGDBRemote escaped_stream;
-  escaped_stream.PutEscapedBytes(stream.GetString());
+  escaped_stream.PutEscapedBytes(
+      llvm::arrayRefFromStringRef(stream.GetString()));
   llvm::Expected<StringExtractorGDBRemote> response =
       m_gdb_comm.SendPacketAndExpectResponse(escaped_stream.GetString(),
                                              GetInterruptTimeout());
diff --git a/lldb/source/Utility/GDBRemote.cpp b/lldb/source/Utility/GDBRemote.cpp
index bd322a5e9e540..7d0bfaa61f872 100644
--- a/lldb/source/Utility/GDBRemote.cpp
+++ b/lldb/source/Utility/GDBRemote.cpp
@@ -24,19 +24,11 @@ StreamGDBRemote::StreamGDBRemote(uint32_t flags, ByteOrder byte_order)
 
 StreamGDBRemote::~StreamGDBRemote() = default;
 
-int StreamGDBRemote::PutEscapedBytes(llvm::StringRef str) {
-  return PutEscapedBytes(str.data(), str.size());
-}
-
-int StreamGDBRemote::PutEscapedBytes(const void *s, size_t src_len) {
+int StreamGDBRemote::PutEscapedBytes(llvm::ArrayRef<uint8_t> bytes) {
   int bytes_written = 0;
-  const uint8_t *src = static_cast<const uint8_t *>(s);
   bool binary_is_set = m_flags.Test(eBinary);
   m_flags.Clear(eBinary);
-  while (src_len) {
-    uint8_t byte = *src;
-    src++;
-    src_len--;
+  for (uint8_t byte : bytes) {
     if (byte == 0x23 || byte == 0x24 || byte == 0x7d || byte == 0x2a) {
       bytes_written += PutChar(0x7d);
       byte ^= 0x20;
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 017859d3d8c3c..9ce22df32c54d 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -63,7 +63,7 @@ inline StringRef toStringRef(ArrayRef<char> Input) {
   return StringRef(Input.begin(), Input.size());
 }
 
-/// Construct a string ref from an array ref of unsigned chars.
+/// Construct an array ref of unsigned bytes from a string ref.
 template <class CharT = uint8_t>
 inline ArrayRef<CharT> arrayRefFromStringRef(StringRef Input) {
   static_assert(std::is_same<CharT, char>::value ||



More information about the lldb-commits mailing list