xref: /openbmc/linux/scripts/gdb/linux/stackdepot.py (revision 8957261c)
1# SPDX-License-Identifier: GPL-2.0
2#
3# Copyright (c) 2023 MediaTek Inc.
4#
5# Authors:
6#  Kuan-Ying Lee <Kuan-Ying.Lee@mediatek.com>
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    pool_index_cached = gdb.parse_and_eval('pool_index')
29
30    if parts['pool_index'] > pool_index_cached:
31        gdb.write("pool index %d out of bounds (%d) for stack id 0x%08x\n" % (parts['pool_index'], pool_index_cached, handle))
32        return gdb.Value(0), 0
33
34    stack_pools = gdb.parse_and_eval('stack_pools')
35
36    try:
37        pool = stack_pools[parts['pool_index']]
38        stack = (pool + gdb.Value(offset).cast(utils.get_size_t_type())).cast(stack_record_type.get_type().pointer())
39        size = int(stack['size'].cast(utils.get_ulong_type()))
40        return stack['entries'], size
41    except Exception as e:
42        gdb.write("%s\n" % e)
43        return gdb.Value(0), 0
44
45def stack_depot_print(handle):
46    if not constants.LX_CONFIG_STACKDEPOT:
47        raise gdb.GdbError("CONFIG_STACKDEPOT is not enabled")
48
49    entries, nr_entries = stack_depot_fetch(handle)
50    if nr_entries > 0:
51        for i in range(0, nr_entries):
52            try:
53                gdb.execute("x /i 0x%x" % (int(entries[i])))
54            except Exception as e:
55                gdb.write("%s\n" % e)
56