Page 1 of 1

QEMU / GDB long mode workaround

Posted: Wed Apr 24, 2019 4:00 pm
by EternalEclipse
This page describes different methods to work around GDB not handling the transition to long mode:
https://wiki.osdev.org/QEMU_and_GDB_in_long_mode

The patch offered there works for latest GDB (8.2.1) with a very slight modification:

Code: Select all

--- gdb/remote.c        2019-04-25 00:49:04.238196076 +0300
+++ gdb/remote.c        2019-04-25 00:49:08.038221311 +0300
@@ -8035,8 +8035,23 @@

   /* Further sanity checks, with knowledge of the architecture.  */
   if (buf_len > 2 * rsa->sizeof_g_packet)
-    error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
-            "bytes): %s"), rsa->sizeof_g_packet, buf_len / 2, rs->buf);
+  {
+    warning (_("Assuming long-mode change. [Remote 'g' packet reply is too long: %s]"), rs->buf);
+    rsa->sizeof_g_packet = buf_len ;
+    for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
+    {
+      if (rsa->regs[i].pnum == -1)
+        continue;
+      if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
+        rsa->regs[i].in_g_packet = 0;
+      else
+        rsa->regs[i].in_g_packet = 1;
+    }
+    
+    // HACKFIX: Make sure at least the lower half of EIP is set correctly, so the proper
+    // breakpoint is recognized (and triggered).
+    rsa->regs[8].offset = 16*8;
+  }

   /* Save the size of the packet sent to us by the target.  It is used
      as a heuristic when determining the max size of packets that the

Code: Select all

cd gdb/gdb-8.2.1
patch -p0 < gdb-longmode.patch