xref: /linux-6.15/scripts/gdb/linux/stackdepot.py (revision c1ccbbaa)
1# SPDX-License-Identifier: GPL-2.0
2#
3# Copyright (c) 2023 MediaTek Inc.
4#
5# Authors:
6#  Kuan-Ying Lee <[email protected]>
7#
8
9import gdb
10from linux import utils, constants
11
12if constants.LX_CONFIG_STACKDEPOT:
13    stack_record_type = utils.CachedType('struct stack_record')
14    DEPOT_STACK_ALIGN = 4
15
16def stack_depot_fetch(handle):
17    global DEPOT_STACK_ALIGN
18    global stack_record_type
19
20    stack_depot_disabled = gdb.parse_and_eval('stack_depot_disabled')
21
22    if stack_depot_disabled:
23        raise gdb.GdbError("stack_depot_disabled\n")
24
25    handle_parts_t = gdb.lookup_type("union handle_parts")
26    parts = handle.cast(handle_parts_t)
27    offset = parts['offset'] << DEPOT_STACK_ALIGN
28    pools_num = gdb.parse_and_eval('pools_num')
29
30    if handle == 0:
31        raise gdb.GdbError("handle is 0\n")
32
33    pool_index = parts['pool_index_plus_1'] - 1
34    if pool_index >= pools_num:
35        gdb.write("pool index %d out of bounds (%d) for stack id 0x%08x\n" % (parts['pool_index'], pools_num, handle))
36        return gdb.Value(0), 0
37
38    stack_pools = gdb.parse_and_eval('stack_pools')
39
40    try:
41        pool = stack_pools[pool_index]
42        stack = (pool + gdb.Value(offset).cast(utils.get_size_t_type())).cast(stack_record_type.get_type().pointer())
43        size = int(stack['size'].cast(utils.get_ulong_type()))
44        return stack['entries'], size
45    except Exception as e:
46        gdb.write("%s\n" % e)
47        return gdb.Value(0), 0
48
49def stack_depot_print(handle):
50    if not constants.LX_CONFIG_STACKDEPOT:
51        raise gdb.GdbError("CONFIG_STACKDEPOT is not enabled")
52
53    entries, nr_entries = stack_depot_fetch(handle)
54    if nr_entries > 0:
55        for i in range(0, nr_entries):
56            try:
57                gdb.execute("x /i 0x%x" % (int(entries[i])))
58            except Exception as e:
59                gdb.write("%s\n" % e)
60