2 # gdb helper commands and functions for Linux kernel debugging
6 # Copyright (c) Siemens AG, 2013
9 # Jan Kiszka <jan.kiszka@siemens.com>
11 # This work is licensed under the terms of the GNU GPL version 2.
16 from linux import cpus, utils, lists
19 module_type = utils.CachedType("struct module")
24 module_ptr_type = module_type.get_type().pointer()
25 modules = gdb.parse_and_eval("modules")
27 for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
31 def find_module_by_name(name):
32 for module in module_list():
33 if module['name'].string() == name:
38 class LxModule(gdb.Function):
39 """Find module by name and return the module variable.
41 $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
42 of the target and return that module variable which MODULE matches."""
45 super(LxModule, self).__init__("lx_module")
47 def invoke(self, mod_name):
48 mod_name = mod_name.string()
49 module = find_module_by_name(mod_name)
51 return module.dereference()
53 raise gdb.GdbError("Unable to find MODULE " + mod_name)
59 class LxLsmod(gdb.Command):
60 """List currently loaded modules."""
62 _module_use_type = utils.CachedType("struct module_use")
65 super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
67 def invoke(self, arg, from_tty):
69 "Address{0} Module Size Used by\n".format(
70 " " if utils.get_long_type().sizeof == 8 else ""))
72 for module in module_list():
73 layout = module['core_layout']
74 gdb.write("{address} {name:<19} {size:>8} {ref}".format(
75 address=str(layout['base']).split()[0],
76 name=module['name'].string(),
77 size=str(layout['size']),
78 ref=str(module['refcnt']['counter'] - 1)))
80 t = self._module_use_type.get_type().pointer()
82 sources = module['source_list']
83 for use in lists.list_for_each_entry(sources, t, "source_list"):
84 gdb.write("{separator}{name}".format(
85 separator=" " if first else ",",
86 name=use['source']['name'].string()))