Jan Kiszka | 850202e | 2015-02-17 13:46:44 -0800 | [diff] [blame] | 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 | |
Kieran Bingham | 619ccaf | 2016-05-23 16:24:45 -0700 | [diff] [blame] | 16 | from linux import cpus, utils, lists |
Jan Kiszka | 850202e | 2015-02-17 13:46:44 -0800 | [diff] [blame] | 17 | |
| 18 | |
| 19 | module_type = utils.CachedType("struct module") |
| 20 | |
| 21 | |
Jan Kiszka | fffb944 | 2015-02-17 13:47:44 -0800 | [diff] [blame] | 22 | def module_list(): |
| 23 | global module_type |
Kieran Bingham | 958ef8a | 2016-05-23 16:24:51 -0700 | [diff] [blame] | 24 | modules = utils.gdb_eval_or_none("modules") |
| 25 | if modules is None: |
| 26 | return |
| 27 | |
Jan Kiszka | fffb944 | 2015-02-17 13:47:44 -0800 | [diff] [blame] | 28 | module_ptr_type = module_type.get_type().pointer() |
Jan Kiszka | 850202e | 2015-02-17 13:46:44 -0800 | [diff] [blame] | 29 | |
Kieran Bingham | 619ccaf | 2016-05-23 16:24:45 -0700 | [diff] [blame] | 30 | for module in lists.list_for_each_entry(modules, module_ptr_type, "list"): |
| 31 | yield module |
Pantelis Koukousoulas | 276d97d | 2015-02-17 13:47:35 -0800 | [diff] [blame] | 32 | |
Jan Kiszka | 7b599ef | 2015-02-17 13:46:55 -0800 | [diff] [blame] | 33 | |
| 34 | def find_module_by_name(name): |
Jan Kiszka | fffb944 | 2015-02-17 13:47:44 -0800 | [diff] [blame] | 35 | for module in module_list(): |
Jan Kiszka | 7b599ef | 2015-02-17 13:46:55 -0800 | [diff] [blame] | 36 | if module['name'].string() == name: |
| 37 | return module |
| 38 | return None |
| 39 | |
| 40 | |
| 41 | class 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 |
| 45 | of 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 | |
| 59 | LxModule() |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 60 | |
| 61 | |
| 62 | class 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 Kiszka | fffb944 | 2015-02-17 13:47:44 -0800 | [diff] [blame] | 75 | for module in module_list(): |
Jan Kiszka | ad4db3b | 2016-03-22 14:27:39 -0700 | [diff] [blame] | 76 | layout = module['core_layout'] |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 77 | gdb.write("{address} {name:<19} {size:>8} {ref}".format( |
Jan Kiszka | ad4db3b | 2016-03-22 14:27:39 -0700 | [diff] [blame] | 78 | address=str(layout['base']).split()[0], |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 79 | name=module['name'].string(), |
Jan Kiszka | ad4db3b | 2016-03-22 14:27:39 -0700 | [diff] [blame] | 80 | size=str(layout['size']), |
Jan Kiszka | 0c22fde | 2016-05-23 16:24:37 -0700 | [diff] [blame] | 81 | ref=str(module['refcnt']['counter'] - 1))) |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 82 | |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 83 | t = self._module_use_type.get_type().pointer() |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 84 | first = True |
Kieran Bingham | 619ccaf | 2016-05-23 16:24:45 -0700 | [diff] [blame] | 85 | sources = module['source_list'] |
| 86 | for use in lists.list_for_each_entry(sources, t, "source_list"): |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 87 | gdb.write("{separator}{name}".format( |
| 88 | separator=" " if first else ",", |
| 89 | name=use['source']['name'].string())) |
| 90 | first = False |
Kieran Bingham | 619ccaf | 2016-05-23 16:24:45 -0700 | [diff] [blame] | 91 | |
Jan Kiszka | 5403727 | 2015-02-17 13:47:29 -0800 | [diff] [blame] | 92 | gdb.write("\n") |
| 93 | |
| 94 | |
| 95 | LxLsmod() |