^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # gdb helper commands and functions for Linux kernel debugging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # module tools
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # Copyright (c) Siemens AG, 2013
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) # Jan Kiszka <jan.kiszka@siemens.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # This work is licensed under the terms of the GNU GPL version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) import gdb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) from linux import cpus, utils, lists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) module_type = utils.CachedType("struct module")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) def module_list():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) global module_type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) modules = utils.gdb_eval_or_none("modules")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if modules is None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) module_ptr_type = module_type.get_type().pointer()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) yield module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) def find_module_by_name(name):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) for module in module_list():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if module['name'].string() == name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return None
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) class LxModule(gdb.Function):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) """Find module by name and return the module variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) of the target and return that module variable which MODULE matches."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) super(LxModule, self).__init__("lx_module")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) def invoke(self, mod_name):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) mod_name = mod_name.string()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) module = find_module_by_name(mod_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if module:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return module.dereference()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) raise gdb.GdbError("Unable to find MODULE " + mod_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) LxModule()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) class LxLsmod(gdb.Command):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) """List currently loaded modules."""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) _module_use_type = utils.CachedType("struct module_use")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) def __init__(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) def invoke(self, arg, from_tty):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) gdb.write(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) "Address{0} Module Size Used by\n".format(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) " " if utils.get_long_type().sizeof == 8 else ""))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) for module in module_list():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) layout = module['core_layout']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) gdb.write("{address} {name:<19} {size:>8} {ref}".format(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) address=str(layout['base']).split()[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) name=module['name'].string(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) size=str(layout['size']),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ref=str(module['refcnt']['counter'] - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) t = self._module_use_type.get_type().pointer()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) first = True
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) sources = module['source_list']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for use in lists.list_for_each_entry(sources, t, "source_list"):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) gdb.write("{separator}{name}".format(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) separator=" " if first else ",",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) name=use['source']['name'].string()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) first = False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) gdb.write("\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) LxLsmod()