From cee4fe67fc874ac3b19310ad42c9318e4c563a53 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sat, 27 Apr 2024 15:41:05 -0700 Subject: [PATCH] [tools] Let GDB quit easily if inferior already quit I had defined hook-quit in the GDB script to kill the inferior. But if the inferior had already terminated, then trying to quit GDB only printed an error. Now hook-quit is smarter and checks if the inferior is running before trying to kill it. --- assets/debugging/jsix.elf-gdb.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/assets/debugging/jsix.elf-gdb.py b/assets/debugging/jsix.elf-gdb.py index bf4052c..3dd2a4b 100644 --- a/assets/debugging/jsix.elf-gdb.py +++ b/assets/debugging/jsix.elf-gdb.py @@ -501,6 +501,18 @@ class LinkedListPrinter: return self.items +class IsRunning(gdb.Function): + def __init__(self): + super(IsRunning, self).__init__("is_running") + + def invoke(self): + inferior = gdb.selected_inferior() + return \ + inferior is not None and \ + inferior.is_valid() and \ + len(inferior.threads()) > 0 + + def build_pretty_printers(): pp = gdb.printing.RegexpCollectionPrettyPrinter("jsix") pp.add_printer("cap table", '^cap_table$', CapTablePrinter) @@ -520,9 +532,10 @@ GetThreadsCommand() PrintProfilesCommand() DumpLogCommand() ShowCurrentProcessCommand() +IsRunning() gdb.execute("display/i $rip") -gdb.execute("define hook-quit\nkill\nend") +gdb.execute("define hook-quit\nif $is_running()\n kill\nend\nend") if not gdb.selected_inferior().was_attached: gdb.execute("add-symbol-file build/panic.serial.elf") gdb.execute("target remote :1234")