2 # gdb helper commands and functions for Linux kernel debugging
6 # Copyright (c) Siemens AG, 2013
11 # This work is licensed under the terms of the GNU GPL version 2.
16 from linux import cpus, utils, lists, constants
19 module_type = utils.CachedType("struct module")
24 modules = utils.gdb_eval_or_none("modules")
28 module_ptr_type = module_type.get_type().pointer()
30 for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
34 def find_module_by_name(name):
35 for module in module_list():
36 if module['name'].string() == name:
41 class LxModule(gdb.Function):
42 """Find module by name and return the module variable.
44 $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
45 of the target and return that module variable which MODULE matches."""
48 super(LxModule, self).__init__("lx_module")
50 def invoke(self, mod_name):
51 mod_name = mod_name.string()
52 module = find_module_by_name(mod_name)
54 return module.dereference()
56 raise gdb.GdbError("Unable to find MODULE " + mod_name)
62 class LxLsmod(gdb.Command):
63 """List currently loaded modules."""
65 _module_use_type = utils.CachedType("struct module_use")
68 super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
70 def invoke(self, arg, from_tty):
72 "Address{0} Module Size Used by\n".format(
73 " " if utils.get_long_type().sizeof == 8 else ""))
75 for module in module_list():
76 text = module['mem'][constants.LX_MOD_TEXT]
77 text_addr = str(text['base']).split()[0]
80 for i in range(constants.LX_MOD_TEXT, constants.LX_MOD_RO_AFTER_INIT + 1):
81 total_size += module['mem'][i]['size']
83 gdb.write("{address} {name:<19} {size:>8} {ref}".format(
85 name=module['name'].string(),
87 ref=str(module['refcnt']['counter'] - 1)))
89 t = self._module_use_type.get_type().pointer()
91 sources = module['source_list']
92 for use in lists.list_for_each_entry(sources, t, "source_list"):
93 gdb.write("{separator}{name}".format(
94 separator=" " if first else ",",
95 name=use['source']['name'].string()))
103 t = """Usage: lx-getmod-by-textaddr [Heximal Address]
104 Example: lx-getmod-by-textaddr 0xffff800002d305ac\n"""
105 gdb.write("Unrecognized command\n")
106 raise gdb.GdbError(t)
108 class LxFindTextAddrinMod(gdb.Command):
109 '''Look up loaded kernel module by text address.'''
112 super(LxFindTextAddrinMod, self).__init__('lx-getmod-by-textaddr', gdb.COMMAND_SUPPORT)
114 def invoke(self, arg, from_tty):
115 args = gdb.string_to_argv(arg)
120 addr = gdb.Value(int(args[0], 16)).cast(utils.get_ulong_type())
121 for mod in module_list():
122 mod_text_start = mod['mem'][constants.LX_MOD_TEXT]['base']
123 mod_text_end = mod_text_start + mod['mem'][constants.LX_MOD_TEXT]['size'].cast(utils.get_ulong_type())
125 if addr >= mod_text_start and addr < mod_text_end:
126 s = "0x%x" % addr + " is in " + mod['name'].string() + ".ko\n"
129 gdb.write("0x%x is not in any module text section\n" % addr)
131 LxFindTextAddrinMod()