modules.py 2.7 KB

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