This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit d8eccbc0f23105c71719685be7b8d087f21ee675
Author: xuxingliang <[email protected]>
AuthorDate: Tue Oct 29 10:59:18 2024 +0800

    gdb/lists: add foreach array command to dump array
    
    This command can dump any array with auto length detection or specified
    length.
    
    An optional `element` parameter is used to only dump this element in
    array when array is in type of struct.
    
    E.g.
    
    (gdb) foreach array g_mmheap->mm_nodelist
    0: {preceding = 0, size = 0, pid = 0, seqno = 0, backtrace = {0x0, 0x0, 
0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, flink = 0x41692cb8, blink = 0x0}
    1: {preceding = 0, size = 0, pid = 0, seqno = 0, backtrace = {0x0, 0x0, 
0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, flink = 0x40c378a0, blink = 0x40605f58}
    
    (gdb) foreach array g_mmheap->mm_nodelist -e "flink"
    0: 0x41692cb8
    1: 0x40c378a0
    
    Signed-off-by: xuxingliang <[email protected]>
---
 tools/gdb/nuttxgdb/lists.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/tools/gdb/nuttxgdb/lists.py b/tools/gdb/nuttxgdb/lists.py
index 42dc90f354..b7e999dcd0 100644
--- a/tools/gdb/nuttxgdb/lists.py
+++ b/tools/gdb/nuttxgdb/lists.py
@@ -372,3 +372,42 @@ class ForeachListEntry(gdb.Command):
             node = node[args.next]
             if node == pointer:
                 break
+
+
+class ForeachArray(gdb.Command):
+    """Dump array members."""
+
+    def __init__(self):
+        super().__init__("foreach array", gdb.COMMAND_DATA, 
gdb.COMPLETE_EXPRESSION)
+
+    def invoke(self, arg, from_tty):
+        argv = gdb.string_to_argv(arg)
+
+        parser = argparse.ArgumentParser(description="Iterate the items in 
array")
+        parser.add_argument("head", type=str, help="List head")
+        parser.add_argument(
+            "-l",
+            "--length",
+            type=int,
+            help="The array length",
+            default=None,
+        )
+        parser.add_argument(
+            "-e",
+            "--element",
+            type=str,
+            help="Only dump this element in array member struct.",
+            default=None,
+        )
+        try:
+            args = parser.parse_args(argv)
+        except SystemExit:
+            gdb.write("Invalid arguments\n")
+            return
+
+        pointer = gdb.parse_and_eval(args.head)
+        node = pointer
+        len = args.length if args.length else utils.nitems(pointer)
+        for i in range(len):
+            entry = node[i][args.element] if arg.element else node[i]
+            gdb.write(f"{i}: {entry.format_string(styling=True)}\n")

Reply via email to