xref: /linux-6.15/scripts/gdb/linux/modules.py (revision 276d97d9)
1#
2# gdb helper commands and functions for Linux kernel debugging
3#
4#  module tools
5#
6# Copyright (c) Siemens AG, 2013
7#
8# Authors:
9#  Jan Kiszka <[email protected]>
10#
11# This work is licensed under the terms of the GNU GPL version 2.
12#
13
14import gdb
15
16from linux import cpus, utils
17
18
19module_type = utils.CachedType("struct module")
20
21
22class ModuleList:
23    def __init__(self):
24        global module_type
25        self.module_ptr_type = module_type.get_type().pointer()
26        modules = gdb.parse_and_eval("modules")
27        self.curr_entry = modules['next']
28        self.end_of_list = modules.address
29
30    def __iter__(self):
31        return self
32
33    def __next__(self):
34        entry = self.curr_entry
35        if entry != self.end_of_list:
36            self.curr_entry = entry['next']
37            return utils.container_of(entry, self.module_ptr_type, "list")
38        else:
39            raise StopIteration
40
41    def next(self):
42        return self.__next__()
43
44
45def find_module_by_name(name):
46    for module in ModuleList():
47        if module['name'].string() == name:
48            return module
49    return None
50
51
52class LxModule(gdb.Function):
53    """Find module by name and return the module variable.
54
55$lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
56of the target and return that module variable which MODULE matches."""
57
58    def __init__(self):
59        super(LxModule, self).__init__("lx_module")
60
61    def invoke(self, mod_name):
62        mod_name = mod_name.string()
63        module = find_module_by_name(mod_name)
64        if module:
65            return module.dereference()
66        else:
67            raise gdb.GdbError("Unable to find MODULE " + mod_name)
68
69
70LxModule()
71
72
73class LxLsmod(gdb.Command):
74    """List currently loaded modules."""
75
76    _module_use_type = utils.CachedType("struct module_use")
77
78    def __init__(self):
79        super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
80
81    def invoke(self, arg, from_tty):
82        gdb.write(
83            "Address{0}    Module                  Size  Used by\n".format(
84                "        " if utils.get_long_type().sizeof == 8 else ""))
85
86        for module in ModuleList():
87            ref = 0
88            module_refptr = module['refptr']
89            for cpu in cpus.CpuList("cpu_possible_mask"):
90                refptr = cpus.per_cpu(module_refptr, cpu)
91                ref += refptr['incs']
92                ref -= refptr['decs']
93
94            gdb.write("{address} {name:<19} {size:>8}  {ref}".format(
95                address=str(module['module_core']).split()[0],
96                name=module['name'].string(),
97                size=str(module['core_size']),
98                ref=str(ref)))
99
100            source_list = module['source_list']
101            t = self._module_use_type.get_type().pointer()
102            entry = source_list['next']
103            first = True
104            while entry != source_list.address:
105                use = utils.container_of(entry, t, "source_list")
106                gdb.write("{separator}{name}".format(
107                    separator=" " if first else ",",
108                    name=use['source']['name'].string()))
109                first = False
110                entry = entry['next']
111            gdb.write("\n")
112
113
114LxLsmod()
115