xref: /linux-6.15/scripts/gdb/linux/vmalloc.py (revision 0040f2c5)
1852622bfSKuan-Ying Lee# SPDX-License-Identifier: GPL-2.0
2852622bfSKuan-Ying Lee#
3852622bfSKuan-Ying Lee# Copyright (c) 2023 MediaTek Inc.
4852622bfSKuan-Ying Lee#
5852622bfSKuan-Ying Lee# Authors:
6852622bfSKuan-Ying Lee#  Kuan-Ying Lee <[email protected]>
7852622bfSKuan-Ying Lee#
8852622bfSKuan-Ying Lee
9852622bfSKuan-Ying Leeimport gdb
10852622bfSKuan-Ying Leeimport re
11852622bfSKuan-Ying Leefrom linux import lists, utils, stackdepot, constants, mm
12852622bfSKuan-Ying Lee
136620999fSBen Wolsiefferif constants.LX_CONFIG_MMU:
14852622bfSKuan-Ying Lee    vmap_area_type = utils.CachedType('struct vmap_area')
15852622bfSKuan-Ying Lee    vmap_area_ptr_type = vmap_area_type.get_type().pointer()
16852622bfSKuan-Ying Lee
17852622bfSKuan-Ying Leedef is_vmalloc_addr(x):
18852622bfSKuan-Ying Lee    pg_ops = mm.page_ops().ops
19852622bfSKuan-Ying Lee    addr = pg_ops.kasan_reset_tag(x)
20852622bfSKuan-Ying Lee    return addr >= pg_ops.VMALLOC_START and addr < pg_ops.VMALLOC_END
21852622bfSKuan-Ying Lee
22852622bfSKuan-Ying Leeclass LxVmallocInfo(gdb.Command):
23852622bfSKuan-Ying Lee    """Show vmallocinfo"""
24852622bfSKuan-Ying Lee
25852622bfSKuan-Ying Lee    def __init__(self):
26852622bfSKuan-Ying Lee        super(LxVmallocInfo, self).__init__("lx-vmallocinfo", gdb.COMMAND_DATA)
27852622bfSKuan-Ying Lee
28852622bfSKuan-Ying Lee    def invoke(self, arg, from_tty):
296620999fSBen Wolsieffer        if not constants.LX_CONFIG_MMU:
306620999fSBen Wolsieffer            raise gdb.GdbError("Requires MMU support")
316620999fSBen Wolsieffer
32*0040f2c5SKuan-Ying Lee        nr_vmap_nodes = gdb.parse_and_eval('nr_vmap_nodes')
33*0040f2c5SKuan-Ying Lee        for i in range(0, nr_vmap_nodes):
34*0040f2c5SKuan-Ying Lee            vn = gdb.parse_and_eval('&vmap_nodes[%d]' % i)
35*0040f2c5SKuan-Ying Lee            for vmap_area in lists.list_for_each_entry(vn['busy']['head'], vmap_area_ptr_type, "list"):
36852622bfSKuan-Ying Lee                if not vmap_area['vm']:
37852622bfSKuan-Ying Lee                    gdb.write("0x%x-0x%x %10d vm_map_ram\n" % (vmap_area['va_start'], vmap_area['va_end'],
38852622bfSKuan-Ying Lee                        vmap_area['va_end'] - vmap_area['va_start']))
39852622bfSKuan-Ying Lee                    continue
40852622bfSKuan-Ying Lee                v = vmap_area['vm']
41852622bfSKuan-Ying Lee                gdb.write("0x%x-0x%x %10d" % (v['addr'], v['addr'] + v['size'], v['size']))
42852622bfSKuan-Ying Lee                if v['caller']:
43852622bfSKuan-Ying Lee                    gdb.write(" %s" % str(v['caller']).split(' ')[-1])
44852622bfSKuan-Ying Lee                if v['nr_pages']:
45852622bfSKuan-Ying Lee                    gdb.write(" pages=%d" % v['nr_pages'])
46852622bfSKuan-Ying Lee                if v['phys_addr']:
47852622bfSKuan-Ying Lee                    gdb.write(" phys=0x%x" % v['phys_addr'])
48852622bfSKuan-Ying Lee                if v['flags'] & constants.LX_VM_IOREMAP:
49852622bfSKuan-Ying Lee                    gdb.write(" ioremap")
50852622bfSKuan-Ying Lee                if v['flags'] & constants.LX_VM_ALLOC:
51852622bfSKuan-Ying Lee                    gdb.write(" vmalloc")
52852622bfSKuan-Ying Lee                if v['flags'] & constants.LX_VM_MAP:
53852622bfSKuan-Ying Lee                    gdb.write(" vmap")
54852622bfSKuan-Ying Lee                if v['flags'] & constants.LX_VM_USERMAP:
55852622bfSKuan-Ying Lee                    gdb.write(" user")
56852622bfSKuan-Ying Lee                if v['flags'] & constants.LX_VM_DMA_COHERENT:
57852622bfSKuan-Ying Lee                    gdb.write(" dma-coherent")
58852622bfSKuan-Ying Lee                if is_vmalloc_addr(v['pages']):
59852622bfSKuan-Ying Lee                    gdb.write(" vpages")
60852622bfSKuan-Ying Lee                gdb.write("\n")
61852622bfSKuan-Ying Lee
62852622bfSKuan-Ying LeeLxVmallocInfo()
63