[v4,1/2] gdbreplay: Calculate the checksum if missing

Message ID 20241122114020.1608796-1-ahajkova@redhat.com
State New
Headers
Series [v4,1/2] gdbreplay: Calculate the checksum if missing |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Alexandra Petlanova Hajkova Nov. 22, 2024, 11:39 a.m. UTC
  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(-)
  

Comments

Tom Tromey Nov. 22, 2024, 4:07 p.m. UTC | #1
>>>>> "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
  

Patch

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