public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v4 1/2] gdbreplay: Calculate the checksum if missing
@ 2024-11-22 11:39 Alexandra Hájková
  2024-11-22 16:07 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandra Hájková @ 2024-11-22 11:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: ahajkova

If the checksum is missing at the end of the packet,
recalculate it and add at the end of the packet, then
replay the packet with the checksum added.

The motivation for this change is that I'd like to add a TCL test
which starts a communication with gdbsever setting the remotelog file.
Then, it modifies the remotelog, injects an error message instead of
the expected replay to some packet in order to test GDB reacts to
the error response properly.
---
v4: - Only recalculate the checksum if it's missing.
 gdbserver/gdbreplay.cc | 45 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/gdbserver/gdbreplay.cc b/gdbserver/gdbreplay.cc
index a2c77ce9588..94c8c4f5fb7 100644
--- a/gdbserver/gdbreplay.cc
+++ b/gdbserver/gdbreplay.cc
@@ -402,6 +402,20 @@ expect (FILE *fp)
     }
 }
 
+/* Calculate checksum for the packet stored in buffer buf.  Store
+   the checksum in a hexadecimal format in a checksum_hex variable.  */
+static void
+recalculate_csum (const std::string &buf, int cnt, unsigned char *checksum_hex)
+{
+  unsigned char csum = 0;
+
+  for (int i = 0; i < cnt; i++)
+    csum += buf[i];
+
+  checksum_hex[0] = tohex ((csum >> 4) & 0xf);
+  checksum_hex[1] = tohex (csum & 0xf);
+}
+
 /* Play data back to gdb from fp (after skipping leading blank) up until a
    \n is read from fp (which is discarded and not sent to gdb). */
 
@@ -409,7 +423,10 @@ static void
 play (FILE *fp)
 {
   int fromlog;
-  char ch;
+  int where_csum = 0, offset = 1;
+  unsigned char checksum[2] = {0, 0};
+  std::string line;
+
 
   if ((fromlog = logchar (fp, false)) != ' ')
     {
@@ -418,10 +435,30 @@ play (FILE *fp)
     }
   while ((fromlog = logchar (fp, false)) != EOL)
     {
-      ch = fromlog;
-      if (write (remote_desc_out, &ch, 1) != 1)
-	remote_error ("Error during write to gdb");
+      line.push_back (fromlog);
+      if (line[line.length ()] == '#')
+	where_csum = line.length ();
+    }
+
+  /* Packet starts with '+$' or '$', we don't want to calculate those
+     to the checksum, substract the offset to adjust the line length.
+     If the line starts with '$', the offset remains set to 1.  */
+  if (line[0] == '+')
+    offset = 2;
+
+  /* If '#' is missing at the end of the line, add it and adjust the line
+     length.  */
+  if (where_csum == 0)
+    {
+      where_csum = line.length ();
+      line.push_back ('#');
+      recalculate_csum (line.substr (offset), where_csum - offset, checksum);
+      line.push_back (checksum[0]);
+      line.push_back (checksum[1]);
     }
+
+  if (write (remote_desc_out, line.data (), line.size ()) != line.size ())
+    remote_error ("Error during write to gdb");
 }
 
 static void
-- 
2.47.0


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

* Re: [PATCH v4 1/2] gdbreplay: Calculate the checksum if missing
  2024-11-22 11:39 [PATCH v4 1/2] gdbreplay: Calculate the checksum if missing Alexandra Hájková
@ 2024-11-22 16:07 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2024-11-22 16:07 UTC (permalink / raw)
  To: Alexandra Hájková; +Cc: gdb-patches

>>>>> "Alexandra" == Alexandra Hájková <ahajkova@redhat.com> writes:

Alexandra> v4: - Only recalculate the checksum if it's missing.

I think v3 is probably preferable.
Sorry about that.

Tom

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

end of thread, other threads:[~2024-11-22 16:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-22 11:39 [PATCH v4 1/2] gdbreplay: Calculate the checksum if missing Alexandra Hájková
2024-11-22 16:07 ` Tom Tromey

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