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 69613d5199e0ee813552ad9af2046b53e35d6ee2 Author: xuxingliang <[email protected]> AuthorDate: Thu Sep 26 17:43:20 2024 +0800 tools/gdb: add array iterator Use array iterator where possible. Signed-off-by: xuxingliang <[email protected]> --- tools/gdb/nuttxgdb/fs.py | 4 +--- tools/gdb/nuttxgdb/memdump.py | 12 +++++------- tools/gdb/nuttxgdb/net.py | 6 ++++-- tools/gdb/nuttxgdb/thread.py | 9 ++++----- tools/gdb/nuttxgdb/utils.py | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/tools/gdb/nuttxgdb/fs.py b/tools/gdb/nuttxgdb/fs.py index a82ab25cb7..dd3a793c91 100644 --- a/tools/gdb/nuttxgdb/fs.py +++ b/tools/gdb/nuttxgdb/fs.py @@ -159,9 +159,7 @@ class Fdinfo(gdb.Command): output = [] if CONFIG_FS_BACKTRACE: - backtrace = utils.backtrace( - file["f_backtrace"][i] for i in range(CONFIG_FS_BACKTRACE) - ) + backtrace = utils.backtrace(utils.ArrayIterator(file["f_backtrace"])) backtrace = [ backtrace_formatter.format( diff --git a/tools/gdb/nuttxgdb/memdump.py b/tools/gdb/nuttxgdb/memdump.py index 4f76ab8552..d65734781f 100644 --- a/tools/gdb/nuttxgdb/memdump.py +++ b/tools/gdb/nuttxgdb/memdump.py @@ -112,15 +112,13 @@ def mm_dumpnode(node, count, align, simple, detail, alive): ) if node.type.has_key("backtrace"): - max = node["backtrace"].type.range()[1] firstrow = True - for x in range(0, max): - backtrace = int(node["backtrace"][x]) - if backtrace == 0: + for backtrace in utils.ArrayIterator(node["backtrace"]): + if int(backtrace) == 0: break if simple: - gdb.write(" %0#*x" % (align, backtrace)) + gdb.write(" %0#*x" % (align, int(backtrace))) else: if firstrow: firstrow = False @@ -132,8 +130,8 @@ def mm_dumpnode(node, count, align, simple, detail, alive): " [%0#*x] %-20s %s:%d\n" % ( align, - backtrace, - node["backtrace"][x].format_string( + int(backtrace), + backtrace.format_string( raw=False, symbols=True, address=False ), gdb.find_pc_line(backtrace).symtab, diff --git a/tools/gdb/nuttxgdb/net.py b/tools/gdb/nuttxgdb/net.py index e10ce82321..256267af50 100644 --- a/tools/gdb/nuttxgdb/net.py +++ b/tools/gdb/nuttxgdb/net.py @@ -102,8 +102,10 @@ def tcp_ofoseg_bufsize(conn): total = 0 if utils.get_symbol_value("CONFIG_NET_TCP_OUT_OF_ORDER"): - for i in range(conn["nofosegs"]): - total += conn["ofosegs"][i]["data"]["io_pktlen"] + total = sum( + seg["data"]["io_pktlen"] + for seg in utils.ArrayIterator(conn["ofosegs"], conn["nofosegs"]) + ) return total diff --git a/tools/gdb/nuttxgdb/thread.py b/tools/gdb/nuttxgdb/thread.py index 0fb7829a67..8759024b46 100644 --- a/tools/gdb/nuttxgdb/thread.py +++ b/tools/gdb/nuttxgdb/thread.py @@ -234,8 +234,7 @@ class Nxinfothreads(gdb.Command): % ("Index", "Tid", "Pid", "Thread", "Info", "Frame") ) - for i in range(0, npidhash): - tcb = pidhash[i] + for i, tcb in enumerate(utils.ArrayIterator(pidhash, npidhash)): if not tcb: continue @@ -326,11 +325,11 @@ class Nxthread(gdb.Command): gdb.write("Please specify a command following the thread ID list\n") elif arg[1] == "all": - for i in range(0, npidhash): - if pidhash[i] == 0: + for i, tcb in enumerate(utils.ArrayIterator(pidhash, npidhash)): + if tcb == 0: continue try: - gdb.write(f"Thread {i} {pidhash[i]['name'].string()}\n") + gdb.write(f"Thread {i} {tcb['name'].string()}\n") except gdb.error and UnicodeDecodeError: gdb.write(f"Thread {i}\n") diff --git a/tools/gdb/nuttxgdb/utils.py b/tools/gdb/nuttxgdb/utils.py index e38c88a64f..8ae16c7b58 100644 --- a/tools/gdb/nuttxgdb/utils.py +++ b/tools/gdb/nuttxgdb/utils.py @@ -671,6 +671,39 @@ def jsonify(obj, indent=None): return json.dumps(obj, default=dumper, indent=indent) +class ArrayIterator: + """An iterator for gdb array or pointer.""" + + def __init__(self, array: gdb.Value, maxlen=None): + type_code = array.type.code + if type_code not in (gdb.TYPE_CODE_ARRAY, gdb.TYPE_CODE_PTR): + raise gdb.error(f"Not an array: {array}, type: {array.type}") + + if type_code == gdb.TYPE_CODE_ARRAY: + if maxlen is None: + maxlen = nitems(array) + else: + maxlen = min(nitems(array), maxlen) + + if maxlen is None: + raise gdb.error("Need to provide array length.") + + self.array = array + self.maxlen = maxlen + self.index = 0 + + def __iter__(self): + return self + + def __next__(self) -> gdb.Value: + if self.index >= self.maxlen: + raise StopIteration + + value = self.array[self.index] + self.index += 1 + return value + + class Hexdump(gdb.Command): """hexdump address/symbol <size>"""
