public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFA] gdbserver support for qCRC: (compare-sections)
@ 2010-03-19 23:27 Michael Snyder
  2010-03-22 17:59 ` Michael Snyder
  0 siblings, 1 reply; 10+ messages in thread
From: Michael Snyder @ 2010-03-19 23:27 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 292 bytes --]

This patch adds support to gdbserver for the "qCRC:" request.

This in turn supports the gdb "compare-sections" command, which
basically lets you verify that the binary image on the target
matches the executable that you're debugging.

I borrowed code from gdb/remote.c, I assume that's OK.


[-- Attachment #2: qcrc.txt --]
[-- Type: text/plain, Size: 1954 bytes --]

2010-03-19  Michael Snyder  <msnyder@vmware.com>

	* server.c (crc32): New function.
	(handle_query): Add handling for 'qCRC:' request.

Index: server.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/server.c,v
retrieving revision 1.108
diff -u -p -r1.108 server.c
--- server.c	20 Jan 2010 22:55:38 -0000	1.108
+++ server.c	19 Mar 2010 23:23:15 -0000
@@ -788,6 +788,40 @@ handle_threads_qxfer (const char *annex,
 
 }
 
+/* Table used by the crc32 function to calcuate the checksum.  */
+
+static unsigned long crc32_table[256] =
+{0, 0};
+
+static unsigned long
+crc32 (CORE_ADDR base, int len, unsigned int crc)
+{
+  if (!crc32_table[1])
+    {
+      /* Initialize the CRC table and the decoding table.  */
+      int i, j;
+      unsigned int c;
+
+      for (i = 0; i < 256; i++)
+	{
+	  for (c = i << 24, j = 8; j > 0; --j)
+	    c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
+	  crc32_table[i] = c;
+	}
+    }
+
+  while (len--)
+    {
+      unsigned char byte = 0;
+
+      /* Fixme -- no error checking.  */
+      read_inferior_memory (base, &byte, 1);
+      crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
+      base++;
+    }
+  return crc;
+}
+
 /* Handle all of the extended 'q' packets.  */
 void
 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
@@ -1421,6 +1455,23 @@ handle_query (char *own_buf, int packet_
       return;
     }
 
+  if (strncmp ("qCRC:", own_buf, 5) == 0)
+    {
+      /* CRC check (compare-segment).  */
+      char *comma;
+      CORE_ADDR base = strtoul (own_buf + 5, &comma, 16);
+      int len;
+
+      if (*comma++ != ',')
+	{
+	  write_enn (own_buf);
+	  return;
+	}
+      len = strtoul (comma, NULL, 16);
+      sprintf (own_buf, "C%lx", crc32 (base, len, 0xffffffff));
+      return;
+    }
+
   /* Otherwise we didn't know what packet it was.  Say we didn't
      understand it.  */
   own_buf[0] = 0;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-19 23:27 [RFA] gdbserver support for qCRC: (compare-sections) Michael Snyder
@ 2010-03-22 17:59 ` Michael Snyder
  2010-03-23 16:26   ` Pedro Alves
  0 siblings, 1 reply; 10+ messages in thread
From: Michael Snyder @ 2010-03-22 17:59 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 456 bytes --]

Michael Snyder wrote:
> This patch adds support to gdbserver for the "qCRC:" request.
> 
> This in turn supports the gdb "compare-sections" command, which
> basically lets you verify that the binary image on the target
> matches the executable that you're debugging.
> 
> I borrowed code from gdb/remote.c, I assume that's OK.

Fixed a "fixme" comment, now will return "E01" if the request
tries to read memory that isn't there.

Revised patch attached.



[-- Attachment #2: qcrc32.txt --]
[-- Type: text/plain, Size: 2363 bytes --]

2010-03-19  Michael Snyder  <msnyder@vmware.com>

	* server.c (crc32): New function.
	(handle_query): Add handling for 'qCRC:' request.

Index: server.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/server.c,v
retrieving revision 1.108
diff -u -p -r1.108 server.c
--- server.c	20 Jan 2010 22:55:38 -0000	1.108
+++ server.c	22 Mar 2010 17:57:32 -0000
@@ -788,6 +788,47 @@ handle_threads_qxfer (const char *annex,
 
 }
 
+/* Table used by the crc32 function to calcuate the checksum.  */
+
+static unsigned long crc32_table[256] =
+{0, 0};
+
+/* Compute 32 bit CRC from inferior memory.
+
+   On success, return 32 bit CRC.
+   On failure, return (unsigned long long) -1.  */
+
+static unsigned long long
+crc32 (CORE_ADDR base, int len, unsigned int crc)
+{
+  if (!crc32_table[1])
+    {
+      /* Initialize the CRC table and the decoding table.  */
+      int i, j;
+      unsigned int c;
+
+      for (i = 0; i < 256; i++)
+	{
+	  for (c = i << 24, j = 8; j > 0; --j)
+	    c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
+	  crc32_table[i] = c;
+	}
+    }
+
+  while (len--)
+    {
+      unsigned char byte = 0;
+
+      /* Return failure if memory read fails.  */
+      if (read_inferior_memory (base, &byte, 1) != 0)
+	return (unsigned long long) -1;
+
+      crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
+      base++;
+    }
+  return (unsigned long long) crc;
+}
+
 /* Handle all of the extended 'q' packets.  */
 void
 handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
@@ -1421,6 +1462,31 @@ handle_query (char *own_buf, int packet_
       return;
     }
 
+  if (strncmp ("qCRC:", own_buf, 5) == 0)
+    {
+      /* CRC check (compare-segment).  */
+      char *comma;
+      CORE_ADDR base = strtoul (own_buf + 5, &comma, 16);
+      int len;
+      unsigned long long crc;
+
+      if (*comma++ != ',')
+	{
+	  write_enn (own_buf);
+	  return;
+	}
+      len = strtoul (comma, NULL, 16);
+      crc = crc32 (base, len, 0xffffffff);
+      /* Check for memory failure.  */
+      if (crc == (unsigned long long) -1)
+	{
+	  write_enn (own_buf);
+	  return;
+	}
+      sprintf (own_buf, "C%lx", (unsigned long) crc);
+      return;
+    }
+
   /* Otherwise we didn't know what packet it was.  Say we didn't
      understand it.  */
   own_buf[0] = 0;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-22 17:59 ` Michael Snyder
@ 2010-03-23 16:26   ` Pedro Alves
  2010-03-23 18:16     ` Michael Snyder
  0 siblings, 1 reply; 10+ messages in thread
From: Pedro Alves @ 2010-03-23 16:26 UTC (permalink / raw)
  To: gdb-patches; +Cc: Michael Snyder

On Monday 22 March 2010 17:59:25, Michael Snyder wrote:
> 2010-03-19  Michael Snyder  <msnyder@vmware.com>
> 
>         * server.c (crc32): New function.
>         (handle_query): Add handling for 'qCRC:' request.
> 
> Index: server.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/gdbserver/server.c,v
> retrieving revision 1.108
> diff -u -p -r1.108 server.c
> --- server.c    20 Jan 2010 22:55:38 -0000      1.108
> +++ server.c    22 Mar 2010 17:57:32 -0000
> @@ -788,6 +788,47 @@ handle_threads_qxfer (const char *annex,
>  
>  }
>  
> +/* Table used by the crc32 function to calcuate the checksum.  */
> +
> +static unsigned long crc32_table[256] =
> +{0, 0};
> +

I know you've copied this from remote.c, but, can't
we make this an `unsigned int' table?  longs are 64-bit
on most 64-bit archs, so this uses up double more memory
than needed.

> +/* Compute 32 bit CRC from inferior memory.
> +
> +   On success, return 32 bit CRC.
> +   On failure, return (unsigned long long) -1.  */
> +
> +static unsigned long long
> +crc32 (CORE_ADDR base, int len, unsigned int crc)

Can't say I'm thrilled by assuming
sizeof (long long) > sizeof (long), but I guess
it works on all targets gdbserver cares for presently.

> +{
> +  if (!crc32_table[1])
> +    {
> +      /* Initialize the CRC table and the decoding table.  */
> +      int i, j;
> +      unsigned int c;
> +
> +      for (i = 0; i < 256; i++)
> +       {
> +         for (c = i << 24, j = 8; j > 0; --j)
> +           c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
> +         crc32_table[i] = c;
> +       }
> +    }
> +
> +  while (len--)
> +    {
> +      unsigned char byte = 0;
> +
> +      /* Return failure if memory read fails.  */
> +      if (read_inferior_memory (base, &byte, 1) != 0)
> +       return (unsigned long long) -1;
> +
> +      crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ byte) & 255];
> +      base++;
> +    }
> +  return (unsigned long long) crc;
> +}
> +
>  /* Handle all of the extended 'q' packets.  */
>  void
>  handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
> @@ -1421,6 +1462,31 @@ handle_query (char *own_buf, int packet_
>        return;
>      }
>  
> +  if (strncmp ("qCRC:", own_buf, 5) == 0)
> +    {
> +      /* CRC check (compare-segment).  */
> +      char *comma;
> +      CORE_ADDR base = strtoul (own_buf + 5, &comma, 16);
> +      int len;
> +      unsigned long long crc;
> +

A `require_running' call is missing here.


> +      if (*comma++ != ',')
> +       {
> +         write_enn (own_buf);
> +         return;
> +       }
> +      len = strtoul (comma, NULL, 16);
> +      crc = crc32 (base, len, 0xffffffff);
> +      /* Check for memory failure.  */
> +      if (crc == (unsigned long long) -1)
> +       {
> +         write_enn (own_buf);
> +         return;
> +       }
> +      sprintf (own_buf, "C%lx", (unsigned long) crc);
> +      return;
> +    }
> +
>    /* Otherwise we didn't know what packet it was.  Say we didn't
>       understand it.  */
>    own_buf[0] = 0;

Otherwise, looks fine to me.

-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-23 16:26   ` Pedro Alves
@ 2010-03-23 18:16     ` Michael Snyder
  2010-03-23 18:32       ` Pedro Alves
  0 siblings, 1 reply; 10+ messages in thread
From: Michael Snyder @ 2010-03-23 18:16 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

Pedro Alves wrote:
> On Monday 22 March 2010 17:59:25, Michael Snyder wrote:
>> 2010-03-19  Michael Snyder  <msnyder@vmware.com>
>>
>>         * server.c (crc32): New function.
>>         (handle_query): Add handling for 'qCRC:' request.
>>
>> Index: server.c
>> ===================================================================
>> RCS file: /cvs/src/src/gdb/gdbserver/server.c,v
>> retrieving revision 1.108
>> diff -u -p -r1.108 server.c
>> --- server.c    20 Jan 2010 22:55:38 -0000      1.108
>> +++ server.c    22 Mar 2010 17:57:32 -0000
>> @@ -788,6 +788,47 @@ handle_threads_qxfer (const char *annex,
>>  
>>  }
>>  
>> +/* Table used by the crc32 function to calcuate the checksum.  */
>> +
>> +static unsigned long crc32_table[256] =
>> +{0, 0};
>> +
> 
> I know you've copied this from remote.c, but, can't
> we make this an `unsigned int' table?  longs are 64-bit
> on most 64-bit archs, so this uses up double more memory
> than needed.

Good idea.  Doesn't seem to break anything.  Will do.



> 
>> +/* Compute 32 bit CRC from inferior memory.
>> +
>> +   On success, return 32 bit CRC.
>> +   On failure, return (unsigned long long) -1.  */
>> +
>> +static unsigned long long
>> +crc32 (CORE_ADDR base, int len, unsigned int crc)
> 
> Can't say I'm thrilled by assuming
> sizeof (long long) > sizeof (long), but I guess
> it works on all targets gdbserver cares for presently.

And with your suggested change, the assumption now is that
sizeof(long long) > sizeof(int), which is even more safe.



>> +  if (strncmp ("qCRC:", own_buf, 5) == 0)
>> +    {
>> +      /* CRC check (compare-segment).  */
>> +      char *comma;
>> +      CORE_ADDR base = strtoul (own_buf + 5, &comma, 16);
>> +      int len;
>> +      unsigned long long crc;
>> +
> 
> A `require_running' call is missing here.

Sorry, what's that?  I'm not familiar with it.

> 
> 
>> +      if (*comma++ != ',')
>> +       {
>> +         write_enn (own_buf);
>> +         return;
>> +       }
>> +      len = strtoul (comma, NULL, 16);
>> +      crc = crc32 (base, len, 0xffffffff);
>> +      /* Check for memory failure.  */
>> +      if (crc == (unsigned long long) -1)
>> +       {
>> +         write_enn (own_buf);
>> +         return;
>> +       }
>> +      sprintf (own_buf, "C%lx", (unsigned long) crc);
>> +      return;
>> +    }
>> +
>>    /* Otherwise we didn't know what packet it was.  Say we didn't
>>       understand it.  */
>>    own_buf[0] = 0;
> 
> Otherwise, looks fine to me.

Big thanks for the review!

Michael



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-23 18:16     ` Michael Snyder
@ 2010-03-23 18:32       ` Pedro Alves
  2010-03-23 22:44         ` Michael Snyder
  2010-03-24  1:15         ` Pedro Alves
  0 siblings, 2 replies; 10+ messages in thread
From: Pedro Alves @ 2010-03-23 18:32 UTC (permalink / raw)
  To: Michael Snyder; +Cc: gdb-patches

On Tuesday 23 March 2010 18:16:13, Michael Snyder wrote:
> > A `require_running' call is missing here.
> 
> Sorry, what's that?  I'm not familiar with it.

See the other queries being handled in
server.c:handle_query.  Literally, see the

 require_running (own_buf);

calls.

If you connect with "target extended-remote"
instead of "target remote", and do "compare-sections" before
issueing "run" or "attach" from gdb, hence gdbserver has no
inferior process to debug yet, you want gdbserver to reply
error (E01) instead of trying to read from a non-existing
inferior process (and possibly crashing).

-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-23 18:32       ` Pedro Alves
@ 2010-03-23 22:44         ` Michael Snyder
  2010-03-23 23:15           ` Pedro Alves
  2010-03-24  1:15         ` Pedro Alves
  1 sibling, 1 reply; 10+ messages in thread
From: Michael Snyder @ 2010-03-23 22:44 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

Pedro Alves wrote:
> On Tuesday 23 March 2010 18:16:13, Michael Snyder wrote:
>>> A `require_running' call is missing here.
>> Sorry, what's that?  I'm not familiar with it.
> 
> See the other queries being handled in
> server.c:handle_query.  Literally, see the
> 
>  require_running (own_buf);
> 
> calls.
> 
> If you connect with "target extended-remote"
> instead of "target remote", and do "compare-sections" before
> issueing "run" or "attach" from gdb, hence gdbserver has no
> inferior process to debug yet, you want gdbserver to reply
> error (E01) instead of trying to read from a non-existing
> inferior process (and possibly crashing).

OK, committed, thanks.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-23 22:44         ` Michael Snyder
@ 2010-03-23 23:15           ` Pedro Alves
  2010-03-24  0:16             ` Pedro Alves
  0 siblings, 1 reply; 10+ messages in thread
From: Pedro Alves @ 2010-03-23 23:15 UTC (permalink / raw)
  To: Michael Snyder; +Cc: gdb-patches

On Tuesday 23 March 2010 22:44:20, Michael Snyder wrote:
> OK, committed, thanks.

The checked in version has a new bug:

> +  if (strncmp ("qCRC:", own_buf, 5) == 0)
> +    {
> +      /* CRC check (compare-section).  */
> +      char *comma;
> +      CORE_ADDR base;
> +      int len;
> +      unsigned long long crc;
> +
> +      require_running (own_buf);
> +      base == strtoul (own_buf + 5, &comma, 16);

              ^^

You meant to make that an assignment, not a comparison.
Thou shall always test what you commit.  ;-)

> +      if (*comma++ != ',')
> +	{

-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-23 23:15           ` Pedro Alves
@ 2010-03-24  0:16             ` Pedro Alves
  2010-03-24  0:34               ` Michael Snyder
  0 siblings, 1 reply; 10+ messages in thread
From: Pedro Alves @ 2010-03-24  0:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Michael Snyder

On Tuesday 23 March 2010 23:14:57, Pedro Alves wrote:
> You meant to make that an assignment, not a comparison.
> Thou shall always test what you commit.  ;-)

Since I'm all over gdbserver, I went ahead fixed this.

-- 
Pedro Alves

2010-03-24  Pedro Alves  <pedro@codesourcery.com>

	* server.c (handle_query): Assign, not compare.

---
 gdb/gdbserver/server.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: src/gdb/gdbserver/server.c
===================================================================
--- src.orig/gdb/gdbserver/server.c	2010-03-24 00:08:10.000000000 +0000
+++ src/gdb/gdbserver/server.c	2010-03-24 00:09:49.000000000 +0000
@@ -1471,7 +1471,7 @@ handle_query (char *own_buf, int packet_
       unsigned long long crc;
 
       require_running (own_buf);
-      base == strtoul (own_buf + 5, &comma, 16);
+      base = strtoul (own_buf + 5, &comma, 16);
       if (*comma++ != ',')
 	{
 	  write_enn (own_buf);

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-24  0:16             ` Pedro Alves
@ 2010-03-24  0:34               ` Michael Snyder
  0 siblings, 0 replies; 10+ messages in thread
From: Michael Snyder @ 2010-03-24  0:34 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

Pedro Alves wrote:
> On Tuesday 23 March 2010 23:14:57, Pedro Alves wrote:
>> You meant to make that an assignment, not a comparison.
>> Thou shall always test what you commit.  ;-)
> 
> Since I'm all over gdbserver, I went ahead fixed this.
> 

Thanks.  I swear it worked before I committed it...   ;-/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [RFA] gdbserver support for qCRC: (compare-sections)
  2010-03-23 18:32       ` Pedro Alves
  2010-03-23 22:44         ` Michael Snyder
@ 2010-03-24  1:15         ` Pedro Alves
  1 sibling, 0 replies; 10+ messages in thread
From: Pedro Alves @ 2010-03-24  1:15 UTC (permalink / raw)
  To: gdb-patches

On Tuesday 23 March 2010 18:32:12, Pedro Alves wrote:
> If you connect with "target extended-remote"
> instead of "target remote", and do "compare-sections" before
> issueing "run" or "attach" from gdb, hence gdbserver has no
> inferior process to debug yet, you want gdbserver to reply
> error (E01) instead of trying to read from a non-existing
> inferior process (and possibly crashing).

Actually, the compare-sections command is busted in that
is doesn't work with extended-remote at all:

 (top-gdb) tar extended-remote :9999
 Remote debugging using :9999
 ...
 (top-gdb) compare-sections
 command can only be used with remote target


Note:

> /* FIXME: cagney/1999-10-26: This command should be broken down into a
>   target method (target verify memory) and generic version of the
>   actual command.  This will allow other high-level code (especially
>   generic_load()) to make use of this target functionality.  */

and:

>  if (!current_target.to_shortname ||
>      strcmp (current_target.to_shortname, "remote") != 0)
>    error (_("command can only be used with remote target"));

Ugh!

This patch fixes it.  I've applied it.

-- 
Pedro Alves
2010-03-24  Pedro Alves  <pedro@codesourcery.com>

	gdb/
	* remote.c (crc32): Constify `buf' parameter.
	(remote_verify_memory): New, abstracted out from...
	(compare_sections_command): ... this.  Remove hardcoded target
	checks.
	(init_remote_ops): Install remote_verify_memory.
	* target.c (target_verify_memory): New.
	* target.h (struct target_ops) <to_verify_memory>: New field.
	(target_verify_memory): Declare.

---
 gdb/remote.c |   71 +++++++++++++++++++++++++++++++++--------------------------
 gdb/target.c |   22 ++++++++++++++++++
 gdb/target.h |   15 ++++++++++++
 3 files changed, 77 insertions(+), 31 deletions(-)

Index: src/gdb/remote.c
===================================================================
--- src.orig/gdb/remote.c	2010-03-24 00:38:23.000000000 +0000
+++ src/gdb/remote.c	2010-03-24 01:03:14.000000000 +0000
@@ -173,8 +173,6 @@ static CORE_ADDR remote_address_masked (
 
 static void print_packet (char *);
 
-static unsigned long crc32 (unsigned char *, int, unsigned int);
-
 static void compare_sections_command (char *, int);
 
 static void packet_command (char *, int);
@@ -7542,7 +7540,7 @@ static unsigned long crc32_table[256] =
 {0, 0};
 
 static unsigned long
-crc32 (unsigned char *buf, int len, unsigned int crc)
+crc32 (const unsigned char *buf, int len, unsigned int crc)
 {
   if (!crc32_table[1])
     {
@@ -7566,38 +7564,59 @@ crc32 (unsigned char *buf, int len, unsi
   return crc;
 }
 
+/* Verify memory using the "qCRC:" request.  */
+
+static int
+remote_verify_memory (struct target_ops *ops,
+		      const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
+{
+  struct remote_state *rs = get_remote_state ();
+  unsigned long host_crc, target_crc;
+  char *tmp;
+
+  /* FIXME: assumes lma can fit into long.  */
+  xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
+	     (long) lma, (long) size);
+  putpkt (rs->buf);
+
+  /* Be clever; compute the host_crc before waiting for target
+     reply.  */
+  host_crc = crc32 (data, size, 0xffffffff);
+
+  getpkt (&rs->buf, &rs->buf_size, 0);
+  if (rs->buf[0] == 'E')
+    return -1;
+
+  if (rs->buf[0] != 'C')
+    error (_("remote target does not support this operation"));
+
+  for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
+    target_crc = target_crc * 16 + fromhex (*tmp);
+
+  return (host_crc == target_crc);
+}
+
 /* compare-sections command
 
    With no arguments, compares each loadable section in the exec bfd
    with the same memory range on the target, and reports mismatches.
-   Useful for verifying the image on the target against the exec file.
-   Depends on the target understanding the new "qCRC:" request.  */
-
-/* FIXME: cagney/1999-10-26: This command should be broken down into a
-   target method (target verify memory) and generic version of the
-   actual command.  This will allow other high-level code (especially
-   generic_load()) to make use of this target functionality.  */
+   Useful for verifying the image on the target against the exec file.  */
 
 static void
 compare_sections_command (char *args, int from_tty)
 {
-  struct remote_state *rs = get_remote_state ();
   asection *s;
-  unsigned long host_crc, target_crc;
   struct cleanup *old_chain;
-  char *tmp;
   char *sectdata;
   const char *sectname;
   bfd_size_type size;
   bfd_vma lma;
   int matched = 0;
   int mismatched = 0;
+  int res;
 
   if (!exec_bfd)
     error (_("command cannot be used without an exec file"));
-  if (!current_target.to_shortname ||
-      strcmp (current_target.to_shortname, "remote") != 0)
-    error (_("command can only be used with remote target"));
 
   for (s = exec_bfd->sections; s; s = s->next)
     {
@@ -7614,33 +7633,22 @@ compare_sections_command (char *args, in
 
       matched = 1;		/* do this section */
       lma = s->lma;
-      /* FIXME: assumes lma can fit into long.  */
-      xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
-		 (long) lma, (long) size);
-      putpkt (rs->buf);
 
-      /* Be clever; compute the host_crc before waiting for target
-	 reply.  */
       sectdata = xmalloc (size);
       old_chain = make_cleanup (xfree, sectdata);
       bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
-      host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
 
-      getpkt (&rs->buf, &rs->buf_size, 0);
-      if (rs->buf[0] == 'E')
+      res = target_verify_memory (sectdata, lma, size);
+
+      if (res == -1)
 	error (_("target memory fault, section %s, range %s -- %s"), sectname,
 	       paddress (target_gdbarch, lma),
 	       paddress (target_gdbarch, lma + size));
-      if (rs->buf[0] != 'C')
-	error (_("remote target does not support this operation"));
-
-      for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
-	target_crc = target_crc * 16 + fromhex (*tmp);
 
       printf_filtered ("Section %s, range %s -- %s: ", sectname,
 		       paddress (target_gdbarch, lma),
 		       paddress (target_gdbarch, lma + size));
-      if (host_crc == target_crc)
+      if (res)
 	printf_filtered ("matched.\n");
       else
 	{
@@ -9756,6 +9764,7 @@ Specify the serial device it is connecte
   remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
   remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
   remote_ops.to_core_of_thread = remote_core_of_thread;
+  remote_ops.to_verify_memory = remote_verify_memory;
 }
 
 /* Set up the extended remote vector by making a copy of the standard
Index: src/gdb/target.c
===================================================================
--- src.orig/gdb/target.c	2010-03-24 01:04:17.000000000 +0000
+++ src/gdb/target.c	2010-03-24 01:04:25.000000000 +0000
@@ -3073,6 +3073,28 @@ target_core_of_thread (ptid_t ptid)
   return -1;
 }
 
+int
+target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
+{
+  struct target_ops *t;
+
+  for (t = current_target.beneath; t != NULL; t = t->beneath)
+    {
+      if (t->to_verify_memory != NULL)
+	{
+	  int retval = t->to_verify_memory (t, data, memaddr, size);
+	  if (targetdebug)
+	    fprintf_unfiltered (gdb_stdlog, "target_verify_memory (%s, %s) = %d\n",
+				paddress (target_gdbarch, memaddr),
+				pulongest (size),
+				retval);
+	  return retval;
+	}
+    }
+
+  tcomplain ();
+}
+
 static void
 debug_to_prepare_to_store (struct regcache *regcache)
 {
Index: src/gdb/target.h
===================================================================
--- src.orig/gdb/target.h	2010-03-24 01:04:17.000000000 +0000
+++ src/gdb/target.h	2010-03-24 01:05:54.000000000 +0000
@@ -675,6 +675,13 @@ struct target_ops
        right now, or in this debug session, or for this target -- return -1.  */
     int (*to_core_of_thread) (struct target_ops *, ptid_t ptid);
 
+    /* Verify that the memory in the [MEMADDR, MEMADDR+SIZE) range
+       matches the contents of [DATA,DATA+SIZE).  Returns 1 if there's
+       a match, 0 if there's a mismatch, and -1 if an error is
+       encountered while reading memory.  */
+    int (*to_verify_memory) (struct target_ops *, const gdb_byte *data,
+			     CORE_ADDR memaddr, ULONGEST size);
+
     int to_magic;
     /* Need sub-structure for target machine related rather than comm related?
      */
@@ -1375,6 +1382,14 @@ extern int target_search_memory (CORE_AD
 
 extern int target_core_of_thread (ptid_t ptid);
 
+/* Verify that the memory in the [MEMADDR, MEMADDR+SIZE) range matches
+   the contents of [DATA,DATA+SIZE).  Returns 1 if there's a match, 0
+   if there's a mismatch, and -1 if an error is encountered while
+   reading memory.  Throws an error if the functionality is found not
+   to be supported by the current target.  */
+int target_verify_memory (const gdb_byte *data,
+			  CORE_ADDR memaddr, ULONGEST size);
+
 /* Routines for maintenance of the target structures...
 
    add_target:   Add a target to the list of all possible targets.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-03-24  1:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-19 23:27 [RFA] gdbserver support for qCRC: (compare-sections) Michael Snyder
2010-03-22 17:59 ` Michael Snyder
2010-03-23 16:26   ` Pedro Alves
2010-03-23 18:16     ` Michael Snyder
2010-03-23 18:32       ` Pedro Alves
2010-03-23 22:44         ` Michael Snyder
2010-03-23 23:15           ` Pedro Alves
2010-03-24  0:16             ` Pedro Alves
2010-03-24  0:34               ` Michael Snyder
2010-03-24  1:15         ` Pedro Alves

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).