public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 2/7] Don't use struct buffer in handle_qxfer_traceframe_info
Date: Fri, 16 Dec 2022 16:50:12 -0700	[thread overview]
Message-ID: <20221216235017.3722833-3-tom@tromey.com> (raw)
In-Reply-To: <20221216235017.3722833-1-tom@tromey.com>

This changes handle_qxfer_traceframe_info, in gdbserver, to use
std::string rather than struct buffer.
---
 gdbserver/server.cc     | 27 ++++++++-------------------
 gdbserver/tracepoint.cc | 20 ++++++++++----------
 gdbserver/tracepoint.h  |  2 +-
 3 files changed, 19 insertions(+), 30 deletions(-)

diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index aaef38e0062..58aeafe11d5 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -1764,8 +1764,7 @@ handle_qxfer_traceframe_info (const char *annex,
 			      ULONGEST offset, LONGEST len)
 {
   client_state &cs = get_client_state ();
-  static char *result = 0;
-  static unsigned int result_length = 0;
+  static std::string result;
 
   if (writebuf != NULL)
     return -2;
@@ -1775,35 +1774,25 @@ handle_qxfer_traceframe_info (const char *annex,
 
   if (offset == 0)
     {
-      struct buffer buffer;
-
       /* When asked for data at offset 0, generate everything and
 	 store into 'result'.  Successive reads will be served off
 	 'result'.  */
-      free (result);
+      result.clear ();
 
-      buffer_init (&buffer);
-
-      traceframe_read_info (cs.current_traceframe, &buffer);
-
-      result = buffer_finish (&buffer);
-      result_length = strlen (result);
-      buffer_free (&buffer);
+      traceframe_read_info (cs.current_traceframe, &result);
     }
 
-  if (offset >= result_length)
+  if (offset >= result.length ())
     {
       /* We're out of data.  */
-      free (result);
-      result = NULL;
-      result_length = 0;
+      result.clear ();
       return 0;
     }
 
-  if (len > result_length - offset)
-    len = result_length - offset;
+  if (len > result.length () - offset)
+    len = result.length () - offset;
 
-  memcpy (readbuf, result + offset, len);
+  memcpy (readbuf, result.c_str () + offset, len);
   return len;
 }
 
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index d82b8e2c3ce..252f07c581a 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -5388,13 +5388,13 @@ traceframe_read_sdata (int tfnum, ULONGEST offset,
 }
 
 /* Callback for traceframe_walk_blocks.  Builds a traceframe-info
-   object.  DATA is pointer to a struct buffer holding the
-   traceframe-info object being built.  */
+   object.  DATA is pointer to a string holding the traceframe-info
+   object being built.  */
 
 static int
 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
 {
-  struct buffer *buffer = (struct buffer *) data;
+  std::string *buffer = (std::string *) data;
 
   switch (blocktype)
     {
@@ -5407,9 +5407,9 @@ build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
 	dataptr += sizeof (maddr);
 	memcpy (&mlen, dataptr, sizeof (mlen));
 	dataptr += sizeof (mlen);
-	buffer_xml_printf (buffer,
-			   "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
-			   paddress (maddr), phex_nz (mlen, sizeof (mlen)));
+	string_xml_appendf (*buffer,
+			    "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
+			    paddress (maddr), phex_nz (mlen, sizeof (mlen)));
 	break;
       }
     case 'V':
@@ -5417,7 +5417,7 @@ build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
 	int vnum;
 
 	memcpy (&vnum, dataptr, sizeof (vnum));
-	buffer_xml_printf (buffer, "<tvar id=\"%d\"/>\n", vnum);
+	string_xml_appendf (*buffer, "<tvar id=\"%d\"/>\n", vnum);
 	break;
       }
     case 'R':
@@ -5439,7 +5439,7 @@ build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
    BUFFER.  */
 
 int
-traceframe_read_info (int tfnum, struct buffer *buffer)
+traceframe_read_info (int tfnum, std::string *buffer)
 {
   struct traceframe *tframe;
 
@@ -5453,10 +5453,10 @@ traceframe_read_info (int tfnum, struct buffer *buffer)
       return 1;
     }
 
-  buffer_grow_str (buffer, "<traceframe-info>\n");
+  *buffer += "<traceframe-info>\n";
   traceframe_walk_blocks (tframe->data, tframe->data_size,
 			  tfnum, build_traceframe_info_xml, buffer);
-  buffer_grow_str0 (buffer, "</traceframe-info>\n");
+  *buffer += "</traceframe-info>\n";
   return 0;
 }
 
diff --git a/gdbserver/tracepoint.h b/gdbserver/tracepoint.h
index bbffb6228f4..923ef8230ba 100644
--- a/gdbserver/tracepoint.h
+++ b/gdbserver/tracepoint.h
@@ -95,7 +95,7 @@ int traceframe_read_sdata (int tfnum, ULONGEST offset,
 			   unsigned char *buf, ULONGEST length,
 			   ULONGEST *nbytes);
 
-int traceframe_read_info (int tfnum, struct buffer *buffer);
+int traceframe_read_info (int tfnum, std::string *buffer);
 
 /* If a thread is determined to be collecting a fast tracepoint, this
    structure holds the collect status.  */
-- 
2.38.1


  parent reply	other threads:[~2022-12-16 23:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-16 23:50 [PATCH 0/7] Remove struct buffer Tom Tromey
2022-12-16 23:50 ` [PATCH 1/7] Remove struct buffer from tracefile-tfile.c Tom Tromey
2022-12-16 23:50 ` Tom Tromey [this message]
2022-12-16 23:50 ` [PATCH 3/7] Don't use struct buffer in handle_qxfer_btrace Tom Tromey
2022-12-16 23:50 ` [PATCH 4/7] Don't use struct buffer in handle_qxfer_threads Tom Tromey
2022-12-16 23:50 ` [PATCH 5/7] Don't use struct buffer in event-top.c Tom Tromey
2022-12-16 23:50 ` [PATCH 6/7] Don't use struct buffer in top.c Tom Tromey
2022-12-16 23:50 ` [PATCH 7/7] Remove struct buffer Tom Tromey
2023-02-24 19:04 ` [PATCH 0/7] " Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221216235017.3722833-3-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).