1
0
Fork 0
forked from nuttx/nuttx-update

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 <xuxingliang@xiaomi.com>
This commit is contained in:
xuxingliang 2024-10-29 10:59:18 +08:00 committed by Xiang Xiao
parent cd3d639153
commit d8eccbc0f2

View file

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