public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@ericsson.com>
To: <gdb-patches@sourceware.org>
Cc: Simon Marchi <simon.marchi@ericsson.com>
Subject: [PATCH v2 7/7] MI: consider addressable unit size when reading/writing memory
Date: Wed, 15 Apr 2015 19:48:00 -0000	[thread overview]
Message-ID: <1429127258-1033-8-git-send-email-simon.marchi@ericsson.com> (raw)
In-Reply-To: <1429127258-1033-1-git-send-email-simon.marchi@ericsson.com>

As a user of the target memory read/write interface, the MI code must
adjust its memory allocations to take into account the addressable memory
unitsize of the target.

gdb/ChangeLog:

	mi/mi-main.c (mi_cmd_data_read_memory_bytes): Consider byte
	size.
	(mi_cmd_data_write_memory_bytes): Same.
---
 gdb/mi/mi-main.c | 56 ++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 22 deletions(-)

diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 2733e80..ddfc9d9 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1595,6 +1595,7 @@ mi_cmd_data_read_memory_bytes (char *command, char **argv, int argc)
   int ix;
   VEC(memory_read_result_s) *result;
   long offset = 0;
+  int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
   int oind = 0;
   char *oarg;
   enum opt
@@ -1650,10 +1651,11 @@ mi_cmd_data_read_memory_bytes (char *command, char **argv, int argc)
 			      - addr);
       ui_out_field_core_addr (uiout, "end", gdbarch, read_result->end);
 
-      data = xmalloc ((read_result->end - read_result->begin) * 2 + 1);
+      data = xmalloc (
+	  (read_result->end - read_result->begin) * 2 * unit_size + 1);
 
       for (i = 0, p = data;
-	   i < (read_result->end - read_result->begin);
+	   i < ((read_result->end - read_result->begin) * unit_size);
 	   ++i, p += 2)
 	{
 	  sprintf (p, "%02x", read_result->data[i]);
@@ -1762,29 +1764,36 @@ mi_cmd_data_write_memory_bytes (char *command, char **argv, int argc)
   char *cdata;
   gdb_byte *data;
   gdb_byte *databuf;
-  size_t len, i, steps, remainder;
-  long int count, j;
+  size_t len_hex, len_bytes, len_units, i, steps, remaining_units;
+  long int count_units;
   struct cleanup *back_to;
+  int unit_size;
 
   if (argc != 2 && argc != 3)
     error (_("Usage: ADDR DATA [COUNT]."));
 
   addr = parse_and_eval_address (argv[0]);
   cdata = argv[1];
-  if (strlen (cdata) % 2)
-    error (_("Hex-encoded '%s' must have an even number of characters."),
+  len_hex = strlen (cdata);
+  unit_size = gdbarch_addressable_memory_unit_size (get_current_arch ());
+
+  if (len_hex % (unit_size * 2) != 0)
+    error (_("Hex-encoded '%s' must represent an integral number of "
+	     "addressable memory units."),
 	   cdata);
 
-  len = strlen (cdata)/2;
+  len_bytes = len_hex / 2;
+  len_units = len_bytes / unit_size;
+
   if (argc == 3)
-    count = strtoul (argv[2], NULL, 10);
+    count_units = strtoul (argv[2], NULL, 10);
   else
-    count = len;
+    count_units = len_units;
 
-  databuf = xmalloc (len * sizeof (gdb_byte));
+  databuf = xmalloc (len_bytes * sizeof (gdb_byte));
   back_to = make_cleanup (xfree, databuf);
 
-  for (i = 0; i < len; ++i)
+  for (i = 0; i < len_bytes; ++i)
     {
       int x;
       if (sscanf (cdata + i * 2, "%02x", &x) != 1)
@@ -1792,29 +1801,32 @@ mi_cmd_data_write_memory_bytes (char *command, char **argv, int argc)
       databuf[i] = (gdb_byte) x;
     }
 
-  if (len < count)
+  if (len_units < count_units)
     {
-      /* Pattern is made of less bytes than count:
+      /* Pattern is made of less units than count:
          repeat pattern to fill memory.  */
-      data = xmalloc (count);
+      data = xmalloc (count_units * unit_size);
       make_cleanup (xfree, data);
 
-      steps = count / len;
-      remainder = count % len;
-      for (j = 0; j < steps; j++)
-        memcpy (data + j * len, databuf, len);
+      /* Number of times the pattern is entirely repeated.  */
+      steps = count_units / len_units;
+      /* Number of remaining addressable memory units.  */
+      remaining_units = count_units % len_units;
+      for (i = 0; i < steps; i++)
+        memcpy (data + i * len_bytes, databuf, len_bytes);
 
-      if (remainder > 0)
-        memcpy (data + steps * len, databuf, remainder);
+      if (remaining_units > 0)
+        memcpy (data + steps * len_bytes, databuf,
+		remaining_units * unit_size);
     }
   else
     {
       /* Pattern is longer than or equal to count:
-         just copy count bytes.  */
+         just copy count addressable memory units.  */
       data = databuf;
     }
 
-  write_memory_with_notification (addr, data, count);
+  write_memory_with_notification (addr, data, count_units);
 
   do_cleanups (back_to);
 }
-- 
2.1.4

  parent reply	other threads:[~2015-04-15 19:48 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-15 19:47 [PATCH v2 0/7] Support reading/writing memory on architectures with non 8-bits addressable memory Simon Marchi
2015-04-15 19:47 ` [PATCH v2 3/7] Clarify doc about memory read/write and non-8-bits addressable memory unit sizes Simon Marchi
2015-04-16 14:48   ` Eli Zaretskii
2015-06-12 20:28     ` Simon Marchi
2015-06-13  6:49       ` Eli Zaretskii
2015-06-15 17:40         ` Simon Marchi
2015-06-15 18:09           ` Eli Zaretskii
2015-06-15 19:38             ` Simon Marchi
2015-04-15 19:47 ` [PATCH v2 1/7] Various cleanups in target read/write code Simon Marchi
2015-05-21 17:45   ` Pedro Alves
2015-06-12 17:09     ` Simon Marchi
2015-04-15 19:47 ` [PATCH v2 5/7] target: consider addressable unit size when reading/writing memory Simon Marchi
2015-05-21 17:46   ` Pedro Alves
2015-06-12 21:07     ` Simon Marchi
2015-04-15 19:48 ` [PATCH v2 2/7] Cleanup some docs about memory write Simon Marchi
2015-05-21 17:45   ` Pedro Alves
2015-06-12 19:17     ` Simon Marchi
2015-06-15  9:57       ` Pedro Alves
2015-06-15 17:36         ` Simon Marchi
2015-04-15 19:48 ` Simon Marchi [this message]
2015-05-21 17:52   ` [PATCH v2 7/7] MI: consider addressable unit size when reading/writing memory Pedro Alves
2015-06-15 19:51     ` Simon Marchi
2015-04-15 19:48 ` [PATCH v2 4/7] gdbarch: add addressable_memory_unit_size method Simon Marchi
2015-05-21 17:46   ` Pedro Alves
2015-06-12 20:54     ` Simon Marchi
2015-04-15 19:48 ` [PATCH v2 6/7] remote: consider addressable unit size when reading/writing memory Simon Marchi
2015-05-21 17:48   ` Pedro Alves
2015-06-15 19:28     ` Simon Marchi
2015-06-17 11:55       ` Pedro Alves
2015-06-18 17:14         ` Simon Marchi
2015-05-21 17:44 ` [PATCH v2 0/7] Support reading/writing memory on architectures with non 8-bits addressable memory Pedro Alves
2015-06-11 21:06   ` Simon Marchi
2015-06-11 21:10     ` Simon Marchi
2015-06-12 12:00     ` Pedro Alves

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=1429127258-1033-8-git-send-email-simon.marchi@ericsson.com \
    --to=simon.marchi@ericsson.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).