Add gdb custom commands

This commit is contained in:
Mathieu Maret 2020-08-19 15:02:56 +02:00
parent c71b0135cf
commit 1f1f017224
2 changed files with 45 additions and 0 deletions

44
custom_gdb_extension.py Normal file
View File

@ -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()

View File

@ -1,2 +1,3 @@
add-symbol-file kernel.sym
source custom_gdb_extension.py
target remote | qemu-system-i386 -S -gdb stdio -kernel kernel