File Index
Files and directories
Sign in
FILE

modules.py

File preview with syntax highlighting.

linux-6.19.14/scripts/gdb/linux/modules.py PYTHON
  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 <jan.kiszka@siemens.com>
 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, lists, constants
 17
 18
 19module_type = utils.CachedType("struct module")
 20
 21
 22def has_modules():
 23    return utils.gdb_eval_or_none("modules") is not None
 24
 25def module_list():
 26    global module_type
 27    modules = utils.gdb_eval_or_none("modules")
 28    if modules is None:
 29        return
 30
 31    module_ptr_type = module_type.get_type().pointer()
 32
 33    for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
 34        yield module
 35
 36
 37def find_module_by_name(name):
 38    for module in module_list():
 39        if module['name'].string() == name:
 40            return module
 41    return None
 42
 43
 44class LxModule(gdb.Function):
 45    """Find module by name and return the module variable.
 46
 47$lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
 48of the target and return that module variable which MODULE matches."""
 49
 50    def __init__(self):
 51        super(LxModule, self).__init__("lx_module")
 52
 53    def invoke(self, mod_name):
 54        mod_name = mod_name.string()
 55        module = find_module_by_name(mod_name)
 56        if module:
 57            return module.dereference()
 58        else:
 59            raise gdb.GdbError("Unable to find MODULE " + mod_name)
 60
 61
 62LxModule()
 63
 64
 65class LxLsmod(gdb.Command):
 66    """List currently loaded modules."""
 67
 68    _module_use_type = utils.CachedType("struct module_use")
 69
 70    def __init__(self):
 71        super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
 72
 73    def invoke(self, arg, from_tty):
 74        gdb.write(
 75            "Address{0}    Module                  Size  Used by\n".format(
 76                "        " if utils.get_long_type().sizeof == 8 else ""))
 77
 78        for module in module_list():
 79            text = module['mem'][constants.LX_MOD_TEXT]
 80            text_addr = str(text['base']).split()[0]
 81            total_size = 0
 82
 83            for i in range(constants.LX_MOD_TEXT, constants.LX_MOD_RO_AFTER_INIT + 1):
 84                total_size += module['mem'][i]['size']
 85
 86            gdb.write("{address} {name:<19} {size:>8}  {ref}".format(
 87                address=text_addr,
 88                name=module['name'].string(),
 89                size=str(total_size),
 90                ref=str(module['refcnt']['counter'] - 1)))
 91
 92            t = self._module_use_type.get_type().pointer()
 93            first = True
 94            sources = module['source_list']
 95            for use in lists.list_for_each_entry(sources, t, "source_list"):
 96                gdb.write("{separator}{name}".format(
 97                    separator=" " if first else ",",
 98                    name=use['source']['name'].string()))
 99                first = False
100
101            gdb.write("\n")
102
103LxLsmod()
104
105def help():
106    t = """Usage: lx-getmod-by-textaddr [Heximal Address]
107    Example: lx-getmod-by-textaddr 0xffff800002d305ac\n"""
108    gdb.write("Unrecognized command\n")
109    raise gdb.GdbError(t)
110
111class LxFindTextAddrinMod(gdb.Command):
112    '''Look up loaded kernel module by text address.'''
113
114    def __init__(self):
115        super(LxFindTextAddrinMod, self).__init__('lx-getmod-by-textaddr', gdb.COMMAND_SUPPORT)
116
117    def invoke(self, arg, from_tty):
118        args = gdb.string_to_argv(arg)
119
120        if len(args) != 1:
121            help()
122
123        addr = gdb.Value(int(args[0], 16)).cast(utils.get_ulong_type())
124        for mod in module_list():
125            mod_text_start = mod['mem'][constants.LX_MOD_TEXT]['base']
126            mod_text_end = mod_text_start + mod['mem'][constants.LX_MOD_TEXT]['size'].cast(utils.get_ulong_type())
127
128            if addr >= mod_text_start and addr < mod_text_end:
129                s = "0x%x" % addr + " is in " + mod['name'].string() + ".ko\n"
130                gdb.write(s)
131                return
132        gdb.write("0x%x is not in any module text section\n" % addr)
133
134LxFindTextAddrinMod()
~/ Read-only file preview.