modules.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import gdb
  14. from linux import cpus, utils, lists
  15. module_type = utils.CachedType("struct module")
  16. def module_list():
  17. global module_type
  18. modules = utils.gdb_eval_or_none("modules")
  19. if modules is None:
  20. return
  21. module_ptr_type = module_type.get_type().pointer()
  22. for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
  23. yield module
  24. def find_module_by_name(name):
  25. for module in module_list():
  26. if module['name'].string() == name:
  27. return module
  28. return None
  29. class LxModule(gdb.Function):
  30. """Find module by name and return the module variable.
  31. $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
  32. of the target and return that module variable which MODULE matches."""
  33. def __init__(self):
  34. super(LxModule, self).__init__("lx_module")
  35. def invoke(self, mod_name):
  36. mod_name = mod_name.string()
  37. module = find_module_by_name(mod_name)
  38. if module:
  39. return module.dereference()
  40. else:
  41. raise gdb.GdbError("Unable to find MODULE " + mod_name)
  42. LxModule()
  43. class LxLsmod(gdb.Command):
  44. """List currently loaded modules."""
  45. _module_use_type = utils.CachedType("struct module_use")
  46. def __init__(self):
  47. super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
  48. def invoke(self, arg, from_tty):
  49. gdb.write(
  50. "Address{0} Module Size Used by\n".format(
  51. " " if utils.get_long_type().sizeof == 8 else ""))
  52. for module in module_list():
  53. layout = module['core_layout']
  54. gdb.write("{address} {name:<19} {size:>8} {ref}".format(
  55. address=str(layout['base']).split()[0],
  56. name=module['name'].string(),
  57. size=str(layout['size']),
  58. ref=str(module['refcnt']['counter'] - 1)))
  59. t = self._module_use_type.get_type().pointer()
  60. first = True
  61. sources = module['source_list']
  62. for use in lists.list_for_each_entry(sources, t, "source_list"):
  63. gdb.write("{separator}{name}".format(
  64. separator=" " if first else ",",
  65. name=use['source']['name'].string()))
  66. first = False
  67. gdb.write("\n")
  68. LxLsmod()