blob: 6d497229d026aa1937b07df825f7bf3ff6db2fb0 [file] [log] [blame]
Jan Kiszka850202e2015-02-17 13:46:44 -08001#
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
14import gdb
15
Jan Kiszka54037272015-02-17 13:47:29 -080016from linux import cpus, utils
Jan Kiszka850202e2015-02-17 13:46:44 -080017
18
19module_type = utils.CachedType("struct module")
20
21
Jan Kiszkafffb9442015-02-17 13:47:44 -080022def module_list():
23 global module_type
24 module_ptr_type = module_type.get_type().pointer()
25 modules = gdb.parse_and_eval("modules")
26 entry = modules['next']
27 end_of_list = modules.address
Jan Kiszka850202e2015-02-17 13:46:44 -080028
Jan Kiszkafffb9442015-02-17 13:47:44 -080029 while entry != end_of_list:
30 yield utils.container_of(entry, module_ptr_type, "list")
31 entry = entry['next']
Pantelis Koukousoulas276d97d2015-02-17 13:47:35 -080032
Jan Kiszka7b599ef2015-02-17 13:46:55 -080033
34def find_module_by_name(name):
Jan Kiszkafffb9442015-02-17 13:47:44 -080035 for module in module_list():
Jan Kiszka7b599ef2015-02-17 13:46:55 -080036 if module['name'].string() == name:
37 return module
38 return None
39
40
41class LxModule(gdb.Function):
42 """Find module by name and return the module variable.
43
44$lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
45of the target and return that module variable which MODULE matches."""
46
47 def __init__(self):
48 super(LxModule, self).__init__("lx_module")
49
50 def invoke(self, mod_name):
51 mod_name = mod_name.string()
52 module = find_module_by_name(mod_name)
53 if module:
54 return module.dereference()
55 else:
56 raise gdb.GdbError("Unable to find MODULE " + mod_name)
57
58
59LxModule()
Jan Kiszka54037272015-02-17 13:47:29 -080060
61
62class LxLsmod(gdb.Command):
63 """List currently loaded modules."""
64
65 _module_use_type = utils.CachedType("struct module_use")
66
67 def __init__(self):
68 super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
69
70 def invoke(self, arg, from_tty):
71 gdb.write(
72 "Address{0} Module Size Used by\n".format(
73 " " if utils.get_long_type().sizeof == 8 else ""))
74
Jan Kiszkafffb9442015-02-17 13:47:44 -080075 for module in module_list():
Jan Kiszka54037272015-02-17 13:47:29 -080076 ref = 0
77 module_refptr = module['refptr']
78 for cpu in cpus.CpuList("cpu_possible_mask"):
79 refptr = cpus.per_cpu(module_refptr, cpu)
80 ref += refptr['incs']
81 ref -= refptr['decs']
82
83 gdb.write("{address} {name:<19} {size:>8} {ref}".format(
84 address=str(module['module_core']).split()[0],
85 name=module['name'].string(),
Pantelis Koukousoulas276d97d2015-02-17 13:47:35 -080086 size=str(module['core_size']),
87 ref=str(ref)))
Jan Kiszka54037272015-02-17 13:47:29 -080088
89 source_list = module['source_list']
90 t = self._module_use_type.get_type().pointer()
91 entry = source_list['next']
92 first = True
93 while entry != source_list.address:
94 use = utils.container_of(entry, t, "source_list")
95 gdb.write("{separator}{name}".format(
96 separator=" " if first else ",",
97 name=use['source']['name'].string()))
98 first = False
99 entry = entry['next']
100 gdb.write("\n")
101
102
103LxLsmod()