gdb: print_list can take the next element name in param

This commit is contained in:
Mathieu Maret 2024-02-08 20:55:50 +01:00 committed by Mathieu Maret
parent 8d2dd71d66
commit 774cb4539d

View File

@ -184,18 +184,19 @@ class ListDumpCmd(gdb.Command):
"list_dump", gdb.COMMAND_USER "list_dump", gdb.COMMAND_USER
) )
def _print_list(self, val): def _print_list(self, val, next_name):
"""Walk through the linked list. """Walk through the linked list.
We will simply follow the 'next' pointers until we encounter the HEAD again We will simply follow the 'next' pointers until we encounter the HEAD again
""" """
idx = 0 idx = 0
head = val head = val
thread_ptr = val thread_ptr = val
result = "" result = ""
while thread_ptr != 0 and (idx == 0 or thread_ptr != head): while thread_ptr != 0 and (idx == 0 or thread_ptr != head):
result += gdb.execute('p *({}){}'.format(str(thread_ptr.type),thread_ptr), to_string=True) result += gdb.execute('p *({}){}'.format(str(thread_ptr.type),
thread_ptr = thread_ptr["next"] thread_ptr), to_string=True)
thread_ptr = thread_ptr[next_name]
idx += 1 idx += 1
result = ("Found a Linked List with %d items:" % idx) + "\n" + result result = ("Found a Linked List with %d items:" % idx) + "\n" + result
return result return result
@ -209,17 +210,21 @@ class ListDumpCmd(gdb.Command):
# We can pass args here and use Python CLI utilities like argparse # We can pass args here and use Python CLI utilities like argparse
# to do argument parsing # to do argument parsing
print("Args Passed: %s" % args) print("Args Passed: %s" % args)
if args: thread_name = "currentThread"
ptr_val = gdb.parse_and_eval(args) next_name = "next"
else: if(args):
ptr_val = gdb.parse_and_eval("currentThread") args_arr = args.split()
thread_name = args_arr[0]
if(len(args_arr) >= 2):
next_name = args_arr[1]
ptr_val = gdb.parse_and_eval(thread_name)
try: try:
ptr_val["next"] ptr_val[next_name]
except: except:
print("Expected pointer argument with a next field") print("Expected pointer argument with a %s field" % next_name)
return return
print(self._print_list(ptr_val)) print(self._print_list(ptr_val, next_name))
register_pretty_printer(None, CustomPrettyPrinterLocator(), replace=True) register_pretty_printer(None, CustomPrettyPrinterLocator(), replace=True)