tools/gdb: add array iterator

Use array iterator where possible.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
This commit is contained in:
xuxingliang 2024-09-26 17:43:20 +08:00 • committed by Xiang Xiao
parent 0cda6278d3
commit 69613d5199
5 changed files with 47 additions and 17 deletions

View file

@ -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(

View file

@ -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,

View file

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

View file

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

View file

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