[PATCH 5/9] scripts/qemugdb: coroutine: Fix patching pt_regs in the coredump

Andrey Drobyshev <[email protected]> Tue, 4 Aug 2026 18:17:39 +0300
Newsgroups gmane.comp.emulators.qemu
Message-ID <[email protected]>
patch_regs() and restore_regs() open the coredump with 'ab', i.e. with
O_APPEND.  This repositions the file offset to the end of the file before
every write.  The seek() to pt_regs therefore has no effect: instead of
patching the saved registers, we just append the binary blob with patched
regs at the end of the file.

Nothing fails, and the gdb subprocess still prints a plausible backtrace.
However it's a backtrace of the crashed thread rather than of the
requested coroutine.  The .ptregs blob kept for recovery is dead for the
same reason as there's never anything to restore.

Open the file with 'r+b' so that the writes actually land where seek()
points.

Fixes: 42f3c143c3a0 ("scripts/qemugdb: coroutine: Add option for obtaining detailed trace in coredump")
Signed-off-by: Andrey Drobyshev <[email protected]>
---
 scripts/qemugdb/coroutine.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/qemugdb/coroutine.py b/scripts/qemugdb/coroutine.py
index da395a1a13e..4803914dde0 100644
--- a/scripts/qemugdb/coroutine.py
+++ b/scripts/qemugdb/coroutine.py
@@ -90,7 +90,7 @@ def patch_regs(self, regs):
         int_regs = {k: int(v) for k, v in regs.items()}
         patched_ptregs.update(int_regs)
 
-        with open(self.coredump, 'ab') as f:
+        with open(self.coredump, 'r+b') as f:
             gdb.write(f'assume pt_regs at 0x{self._ptregs_offset:x}\n')
             f.seek(self._ptregs_offset, 0)
             gdb.write('writing regs:\n')
@@ -106,7 +106,7 @@ def restore_regs(self):
             return
 
         gdb.write(f'\nrestoring original regs in core file {self.coredump}\n')
-        with open(self.coredump, 'ab') as f:
+        with open(self.coredump, 'r+b') as f:
             gdb.write(f'assume pt_regs at 0x{self._ptregs_offset:x}\n')
             f.seek(self._ptregs_offset, 0)
             f.write(struct.pack(f"={len(PT_REGS)}q",
-- 
2.47.1