From 774cb4539d15982dad2fc9cde5dcdcaa4e81432f Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 8 Feb 2024 20:55:50 +0100 Subject: [PATCH] gdb: print_list can take the next element name in param --- custom_gdb_extension.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/custom_gdb_extension.py b/custom_gdb_extension.py index dd405b2..6c0bfd3 100644 --- a/custom_gdb_extension.py +++ b/custom_gdb_extension.py @@ -184,18 +184,19 @@ class ListDumpCmd(gdb.Command): "list_dump", gdb.COMMAND_USER ) - def _print_list(self, val): + def _print_list(self, val, next_name): """Walk through the linked list. - We will simply follow the 'next' pointers until we encounter the HEAD again """ idx = 0 head = val thread_ptr = val result = "" + 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) - thread_ptr = thread_ptr["next"] + result += gdb.execute('p *({}){}'.format(str(thread_ptr.type), + thread_ptr), to_string=True) + thread_ptr = thread_ptr[next_name] idx += 1 result = ("Found a Linked List with %d items:" % idx) + "\n" + result return result @@ -209,17 +210,21 @@ class ListDumpCmd(gdb.Command): # We can pass args here and use Python CLI utilities like argparse # to do argument parsing print("Args Passed: %s" % args) - if args: - ptr_val = gdb.parse_and_eval(args) - else: - ptr_val = gdb.parse_and_eval("currentThread") + thread_name = "currentThread" + next_name = "next" + if(args): + 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: - ptr_val["next"] + ptr_val[next_name] except: - print("Expected pointer argument with a next field") + print("Expected pointer argument with a %s field" % next_name) return - print(self._print_list(ptr_val)) + print(self._print_list(ptr_val, next_name)) register_pretty_printer(None, CustomPrettyPrinterLocator(), replace=True)