xref: /linux-6.15/scripts/gdb/linux/stackdepot.py (revision 35249f68)
10e1b240aSKuan-Ying Lee# SPDX-License-Identifier: GPL-2.0
20e1b240aSKuan-Ying Lee#
30e1b240aSKuan-Ying Lee# Copyright (c) 2023 MediaTek Inc.
40e1b240aSKuan-Ying Lee#
50e1b240aSKuan-Ying Lee# Authors:
60e1b240aSKuan-Ying Lee#  Kuan-Ying Lee <[email protected]>
70e1b240aSKuan-Ying Lee#
80e1b240aSKuan-Ying Lee
90e1b240aSKuan-Ying Leeimport gdb
100e1b240aSKuan-Ying Leefrom linux import utils, constants
110e1b240aSKuan-Ying Lee
120e1b240aSKuan-Ying Leeif constants.LX_CONFIG_STACKDEPOT:
130e1b240aSKuan-Ying Lee    stack_record_type = utils.CachedType('struct stack_record')
140e1b240aSKuan-Ying Lee    DEPOT_STACK_ALIGN = 4
150e1b240aSKuan-Ying Lee
16*35249f68SKuan-Ying Leedef help():
17*35249f68SKuan-Ying Lee    t = """Usage: lx-stack_depot_lookup [Hex handle value]
18*35249f68SKuan-Ying Lee    Example:
19*35249f68SKuan-Ying Lee        lx-stack_depot_lookup 0x00c80300\n"""
20*35249f68SKuan-Ying Lee    gdb.write("Unrecognized command\n")
21*35249f68SKuan-Ying Lee    raise gdb.GdbError(t)
22*35249f68SKuan-Ying Lee
230e1b240aSKuan-Ying Leedef stack_depot_fetch(handle):
240e1b240aSKuan-Ying Lee    global DEPOT_STACK_ALIGN
250e1b240aSKuan-Ying Lee    global stack_record_type
260e1b240aSKuan-Ying Lee
270e1b240aSKuan-Ying Lee    stack_depot_disabled = gdb.parse_and_eval('stack_depot_disabled')
280e1b240aSKuan-Ying Lee
290e1b240aSKuan-Ying Lee    if stack_depot_disabled:
300e1b240aSKuan-Ying Lee        raise gdb.GdbError("stack_depot_disabled\n")
310e1b240aSKuan-Ying Lee
320e1b240aSKuan-Ying Lee    handle_parts_t = gdb.lookup_type("union handle_parts")
330e1b240aSKuan-Ying Lee    parts = handle.cast(handle_parts_t)
340e1b240aSKuan-Ying Lee    offset = parts['offset'] << DEPOT_STACK_ALIGN
35125e9987SKuan-Ying Lee    pools_num = gdb.parse_and_eval('pools_num')
360e1b240aSKuan-Ying Lee
379d938f40SKuan-Ying Lee    if handle == 0:
389d938f40SKuan-Ying Lee        raise gdb.GdbError("handle is 0\n")
399d938f40SKuan-Ying Lee
409d938f40SKuan-Ying Lee    pool_index = parts['pool_index_plus_1'] - 1
419d938f40SKuan-Ying Lee    if pool_index >= pools_num:
42125e9987SKuan-Ying Lee        gdb.write("pool index %d out of bounds (%d) for stack id 0x%08x\n" % (parts['pool_index'], pools_num, handle))
430e1b240aSKuan-Ying Lee        return gdb.Value(0), 0
440e1b240aSKuan-Ying Lee
450e1b240aSKuan-Ying Lee    stack_pools = gdb.parse_and_eval('stack_pools')
460e1b240aSKuan-Ying Lee
470e1b240aSKuan-Ying Lee    try:
489d938f40SKuan-Ying Lee        pool = stack_pools[pool_index]
490e1b240aSKuan-Ying Lee        stack = (pool + gdb.Value(offset).cast(utils.get_size_t_type())).cast(stack_record_type.get_type().pointer())
500e1b240aSKuan-Ying Lee        size = int(stack['size'].cast(utils.get_ulong_type()))
510e1b240aSKuan-Ying Lee        return stack['entries'], size
520e1b240aSKuan-Ying Lee    except Exception as e:
530e1b240aSKuan-Ying Lee        gdb.write("%s\n" % e)
540e1b240aSKuan-Ying Lee        return gdb.Value(0), 0
550e1b240aSKuan-Ying Lee
560e1b240aSKuan-Ying Leedef stack_depot_print(handle):
570e1b240aSKuan-Ying Lee    if not constants.LX_CONFIG_STACKDEPOT:
580e1b240aSKuan-Ying Lee        raise gdb.GdbError("CONFIG_STACKDEPOT is not enabled")
590e1b240aSKuan-Ying Lee
600e1b240aSKuan-Ying Lee    entries, nr_entries = stack_depot_fetch(handle)
610e1b240aSKuan-Ying Lee    if nr_entries > 0:
620e1b240aSKuan-Ying Lee        for i in range(0, nr_entries):
630e1b240aSKuan-Ying Lee            try:
640e1b240aSKuan-Ying Lee                gdb.execute("x /i 0x%x" % (int(entries[i])))
650e1b240aSKuan-Ying Lee            except Exception as e:
660e1b240aSKuan-Ying Lee                gdb.write("%s\n" % e)
67*35249f68SKuan-Ying Lee
68*35249f68SKuan-Ying Leeclass StackDepotLookup(gdb.Command):
69*35249f68SKuan-Ying Lee    """Search backtrace by handle"""
70*35249f68SKuan-Ying Lee
71*35249f68SKuan-Ying Lee    def __init__(self):
72*35249f68SKuan-Ying Lee        if constants.LX_CONFIG_STACKDEPOT:
73*35249f68SKuan-Ying Lee            super(StackDepotLookup, self).__init__("lx-stack_depot_lookup", gdb.COMMAND_SUPPORT)
74*35249f68SKuan-Ying Lee
75*35249f68SKuan-Ying Lee    def invoke(self, args, from_tty):
76*35249f68SKuan-Ying Lee        if not constants.LX_CONFIG_STACKDEPOT:
77*35249f68SKuan-Ying Lee            raise gdb.GdbError('CONFIG_STACKDEPOT is not set')
78*35249f68SKuan-Ying Lee
79*35249f68SKuan-Ying Lee        argv = gdb.string_to_argv(args)
80*35249f68SKuan-Ying Lee        if len(argv) == 1:
81*35249f68SKuan-Ying Lee            handle = int(argv[0], 16)
82*35249f68SKuan-Ying Lee            stack_depot_print(gdb.Value(handle).cast(utils.get_uint_type()))
83*35249f68SKuan-Ying Lee        else:
84*35249f68SKuan-Ying Lee            help()
85*35249f68SKuan-Ying Lee
86*35249f68SKuan-Ying LeeStackDepotLookup()
87