62557ddf56f16519bc50727117cfe0b1b82a4d39
[cascardo/linux.git] / scripts / gdb / linux / modules.py
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
14 import gdb
15
16 from linux import cpus, utils, lists
17
18
19 module_type = utils.CachedType("struct module")
20
21
22 def module_list():
23     global module_type
24     module_ptr_type = module_type.get_type().pointer()
25     modules = gdb.parse_and_eval("modules")
26
27     for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
28         yield module
29
30
31 def find_module_by_name(name):
32     for module in module_list():
33         if module['name'].string() == name:
34             return module
35     return None
36
37
38 class LxModule(gdb.Function):
39     """Find module by name and return the module variable.
40
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."""
43
44     def __init__(self):
45         super(LxModule, self).__init__("lx_module")
46
47     def invoke(self, mod_name):
48         mod_name = mod_name.string()
49         module = find_module_by_name(mod_name)
50         if module:
51             return module.dereference()
52         else:
53             raise gdb.GdbError("Unable to find MODULE " + mod_name)
54
55
56 LxModule()
57
58
59 class LxLsmod(gdb.Command):
60     """List currently loaded modules."""
61
62     _module_use_type = utils.CachedType("struct module_use")
63
64     def __init__(self):
65         super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
66
67     def invoke(self, arg, from_tty):
68         gdb.write(
69             "Address{0}    Module                  Size  Used by\n".format(
70                 "        " if utils.get_long_type().sizeof == 8 else ""))
71
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)))
79
80             t = self._module_use_type.get_type().pointer()
81             first = True
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()))
87                 first = False
88
89             gdb.write("\n")
90
91
92 LxLsmod()