From 1f1f01722424779fccf438d2134534c5909affa5 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 19 Aug 2020 15:02:56 +0200 Subject: [PATCH] Add gdb custom commands --- custom_gdb_extension.py | 44 +++++++++++++++++++++++++++++++++++++++++ debug.gdb | 1 + 2 files changed, 45 insertions(+) create mode 100644 custom_gdb_extension.py diff --git a/custom_gdb_extension.py b/custom_gdb_extension.py new file mode 100644 index 0000000..cd11ba0 --- /dev/null +++ b/custom_gdb_extension.py @@ -0,0 +1,44 @@ +class KthreadListDumpCmd(gdb.Command): + """Prints the kthread list""" + + def __init__(self): + super(KthreadListDumpCmd, self).__init__( + "kthread_list_dump", gdb.COMMAND_USER + ) + + def _kthread_list_to_str(self, val): + """Walk through the Kthread list. + + We will simply follow the 'next' pointers until we encounter the HEAD again + """ + idx = 0 + head = val + kthread_ptr = val + result = "" + while kthread_ptr != 0 and (idx == 0 or kthread_ptr != head) : + name = kthread_ptr["name"] + result += "\n%d: Addr: 0x%x, name: %s" % (idx, kthread_ptr, name) + kthread_ptr = kthread_ptr["next"] + idx += 1 + result = ("Found a Linked List with %d kthread:" % idx) + result + return result + + def complete(self, text, word): + # We expect the argument passed to be a symbol so fallback to the + # internal tab-completion handler for symbols + return gdb.COMPLETE_SYMBOL + + def invoke(self, args, from_tty): + # We can pass args here and use Python CLI utilities like argparse + # to do argument parsing + print("Args Passed: %s" % args) + + kthread_ptr_val = gdb.parse_and_eval(args) + if str(kthread_ptr_val.type) != "struct kthread *": + print("Expected pointer argument of type (struct kthread *)") + return + + print(self._kthread_list_to_str(kthread_ptr_val)) + +KthreadListDumpCmd() + diff --git a/debug.gdb b/debug.gdb index f0b1431..316464e 100644 --- a/debug.gdb +++ b/debug.gdb @@ -1,2 +1,3 @@ add-symbol-file kernel.sym +source custom_gdb_extension.py target remote | qemu-system-i386 -S -gdb stdio -kernel kernel