modules.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 utils
  15. module_type = utils.CachedType("struct module")
  16. class ModuleList:
  17. def __init__(self):
  18. global module_type
  19. self.module_ptr_type = module_type.get_type().pointer()
  20. modules = gdb.parse_and_eval("modules")
  21. self.curr_entry = modules['next']
  22. self.end_of_list = modules.address
  23. def __iter__(self):
  24. return self
  25. def next(self):
  26. entry = self.curr_entry
  27. if entry != self.end_of_list:
  28. self.curr_entry = entry['next']
  29. return utils.container_of(entry, self.module_ptr_type, "list")
  30. else:
  31. raise StopIteration
  32. def find_module_by_name(name):
  33. for module in ModuleList():
  34. if module['name'].string() == name:
  35. return module
  36. return None
  37. class LxModule(gdb.Function):
  38. """Find module by name and return the module variable.
  39. $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
  40. of the target and return that module variable which MODULE matches."""
  41. def __init__(self):
  42. super(LxModule, self).__init__("lx_module")
  43. def invoke(self, mod_name):
  44. mod_name = mod_name.string()
  45. module = find_module_by_name(mod_name)
  46. if module:
  47. return module.dereference()
  48. else:
  49. raise gdb.GdbError("Unable to find MODULE " + mod_name)
  50. LxModule()