Back to home page

OSCL-LXR

 
 

    


0001 #
0002 # gdb helper commands and functions for Linux kernel debugging
0003 #
0004 #  module tools
0005 #
0006 # Copyright (c) Siemens AG, 2013
0007 #
0008 # Authors:
0009 #  Jan Kiszka <jan.kiszka@siemens.com>
0010 #
0011 # This work is licensed under the terms of the GNU GPL version 2.
0012 #
0013 
0014 import gdb
0015 
0016 from linux import cpus, utils, lists
0017 
0018 
0019 module_type = utils.CachedType("struct module")
0020 
0021 
0022 def module_list():
0023     global module_type
0024     modules = utils.gdb_eval_or_none("modules")
0025     if modules is None:
0026         return
0027 
0028     module_ptr_type = module_type.get_type().pointer()
0029 
0030     for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
0031         yield module
0032 
0033 
0034 def find_module_by_name(name):
0035     for module in module_list():
0036         if module['name'].string() == name:
0037             return module
0038     return None
0039 
0040 
0041 class LxModule(gdb.Function):
0042     """Find module by name and return the module variable.
0043 
0044 $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
0045 of the target and return that module variable which MODULE matches."""
0046 
0047     def __init__(self):
0048         super(LxModule, self).__init__("lx_module")
0049 
0050     def invoke(self, mod_name):
0051         mod_name = mod_name.string()
0052         module = find_module_by_name(mod_name)
0053         if module:
0054             return module.dereference()
0055         else:
0056             raise gdb.GdbError("Unable to find MODULE " + mod_name)
0057 
0058 
0059 LxModule()
0060 
0061 
0062 class LxLsmod(gdb.Command):
0063     """List currently loaded modules."""
0064 
0065     _module_use_type = utils.CachedType("struct module_use")
0066 
0067     def __init__(self):
0068         super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
0069 
0070     def invoke(self, arg, from_tty):
0071         gdb.write(
0072             "Address{0}    Module                  Size  Used by\n".format(
0073                 "        " if utils.get_long_type().sizeof == 8 else ""))
0074 
0075         for module in module_list():
0076             layout = module['core_layout']
0077             gdb.write("{address} {name:<19} {size:>8}  {ref}".format(
0078                 address=str(layout['base']).split()[0],
0079                 name=module['name'].string(),
0080                 size=str(layout['size']),
0081                 ref=str(module['refcnt']['counter'] - 1)))
0082 
0083             t = self._module_use_type.get_type().pointer()
0084             first = True
0085             sources = module['source_list']
0086             for use in lists.list_for_each_entry(sources, t, "source_list"):
0087                 gdb.write("{separator}{name}".format(
0088                     separator=" " if first else ",",
0089                     name=use['source']['name'].string()))
0090                 first = False
0091 
0092             gdb.write("\n")
0093 
0094 
0095 LxLsmod()