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 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 layout
= module
['core_layout']
77 gdb
.write("{address} {name:<19} {size:>8} {ref}".format(
78 address
=str(layout
['base']).split()[0],
79 name
=module
['name'].string(),
80 size
=str(layout
['size']),
81 ref
=str(module
['refcnt']['counter'] - 1)))
83 t
= self
._module
_use
_type
.get_type().pointer()
85 sources
= module
['source_list']
86 for use
in lists
.list_for_each_entry(sources
, t
, "source_list"):
87 gdb
.write("{separator}{name}".format(
88 separator
=" " if first
else ",",
89 name
=use
['source']['name'].string()))