public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 2/4] Rewrite target_read_string
Date: Fri, 12 Jun 2020 15:53:54 -0600	[thread overview]
Message-ID: <20200612215356.22145-3-tromey@adacore.com> (raw)
In-Reply-To: <20200612215356.22145-1-tromey@adacore.com>

This rewrites target_read_string in terms of read_string.

gdb/ChangeLog
2020-06-02  Tom Tromey  <tromey@adacore.com>

	* valprint.c (read_string): Update comment.
	* target.c (MIN): Remove.
	(target_read_string): Rewrite.
---
 gdb/ChangeLog  |  6 +++++
 gdb/target.c   | 71 +++++++-------------------------------------------
 gdb/valprint.c |  8 +-----
 3 files changed, 16 insertions(+), 69 deletions(-)

diff --git a/gdb/target.c b/gdb/target.c
index 82c405a8491..14c494688e0 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -803,9 +803,6 @@ target_xfer_status_to_string (enum target_xfer_status status)
 };
 
 
-#undef	MIN
-#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
-
 /* target_read_string -- read a null terminated string, up to LEN bytes,
    from MEMADDR in target.  Set *ERRNOP to the errno code, or 0 if successful.
    Set *STRING to a pointer to malloc'd memory containing the data; the caller
@@ -816,68 +813,18 @@ int
 target_read_string (CORE_ADDR memaddr, gdb::unique_xmalloc_ptr<char> *string,
 		    int len, int *errnop)
 {
-  int tlen, offset, i;
-  gdb_byte buf[4];
-  int errcode = 0;
-  char *buffer;
-  int buffer_allocated;
-  char *bufptr;
-  unsigned int nbytes_read = 0;
-
-  gdb_assert (string);
-
-  /* Small for testing.  */
-  buffer_allocated = 4;
-  buffer = (char *) xmalloc (buffer_allocated);
-  bufptr = buffer;
-
-  while (len > 0)
-    {
-      tlen = MIN (len, 4 - (memaddr & 3));
-      offset = memaddr & 3;
+  int bytes_read;
+  gdb::unique_xmalloc_ptr<gdb_byte> buffer;
 
-      errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
-      if (errcode != 0)
-	{
-	  /* The transfer request might have crossed the boundary to an
-	     unallocated region of memory.  Retry the transfer, requesting
-	     a single byte.  */
-	  tlen = 1;
-	  offset = 0;
-	  errcode = target_read_memory (memaddr, buf, 1);
-	  if (errcode != 0)
-	    goto done;
-	}
-
-      if (bufptr - buffer + tlen > buffer_allocated)
-	{
-	  unsigned int bytes;
+  /* Note that the endian-ness does not matter here.  */
+  int errcode = read_string (memaddr, -1, 1, len, BFD_ENDIAN_LITTLE,
+			     &buffer, &bytes_read);
 
-	  bytes = bufptr - buffer;
-	  buffer_allocated *= 2;
-	  buffer = (char *) xrealloc (buffer, buffer_allocated);
-	  bufptr = buffer + bytes;
-	}
-
-      for (i = 0; i < tlen; i++)
-	{
-	  *bufptr++ = buf[i + offset];
-	  if (buf[i + offset] == '\000')
-	    {
-	      nbytes_read += i + 1;
-	      goto done;
-	    }
-	}
-
-      memaddr += tlen;
-      len -= tlen;
-      nbytes_read += tlen;
-    }
-done:
-  string->reset (buffer);
-  if (errnop != NULL)
+  if (errnop != nullptr)
     *errnop = errcode;
-  return nbytes_read;
+
+  string->reset ((char *) buffer.release ());
+  return bytes_read;
 }
 
 struct target_section_table *
diff --git a/gdb/valprint.c b/gdb/valprint.c
index d5490898b9b..f2549805268 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -2027,13 +2027,7 @@ partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
 
    Unless an exception is thrown, BUFFER will always be allocated, even on
    failure.  In this case, some characters might have been read before the
-   failure happened.  Check BYTES_READ to recognize this situation.
-
-   Note: There was a FIXME asking to make this code use target_read_string,
-   but this function is more general (can read past null characters, up to
-   given LEN).  Besides, it is used much more often than target_read_string
-   so it is more tested.  Perhaps callers of target_read_string should use
-   this function instead?  */
+   failure happened.  Check BYTES_READ to recognize this situation.  */
 
 int
 read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
-- 
2.21.3


  parent reply	other threads:[~2020-06-12 21:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-12 21:53 [PATCH 0/4] Unify string-reading APIs Tom Tromey
2020-06-12 21:53 ` [PATCH 1/4] Remove read_memory_string Tom Tromey
2020-06-13 14:19   ` Simon Marchi
2020-06-15 12:37   ` Pedro Alves
2020-06-12 21:53 ` Tom Tromey [this message]
2020-06-12 21:53 ` [PATCH 3/4] Remove a use of target_read_string Tom Tromey
2020-06-13  3:04   ` Sergio Durigan Junior
2020-06-15 12:13     ` Tom Tromey
2020-06-12 21:53 ` [PATCH 4/4] Change target_read_string API Tom Tromey
2020-06-13 14:40   ` Simon Marchi
2020-06-13 14:40 ` [PATCH 0/4] Unify string-reading APIs Simon Marchi
2020-06-15 12:27   ` 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=20200612215356.22145-3-tromey@adacore.com \
    --to=tromey@adacore.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).