modules.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 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 next(self):
  33. return self.__next__()
  34. def find_module_by_name(name):
  35. for module in ModuleList():
  36. if module['name'].string() == name:
  37. return module
  38. return None
  39. class LxModule(gdb.Function):
  40. """Find module by name and return the module variable.
  41. $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
  42. of the target and return that module variable which MODULE matches."""
  43. def __init__(self):
  44. super(LxModule, self).__init__("lx_module")
  45. def invoke(self, mod_name):
  46. mod_name = mod_name.string()
  47. module = find_module_by_name(mod_name)
  48. if module:
  49. return module.dereference()
  50. else:
  51. raise gdb.GdbError("Unable to find MODULE " + mod_name)
  52. LxModule()
  53. class LxLsmod(gdb.Command):
  54. """List currently loaded modules."""
  55. _module_use_type = utils.CachedType("struct module_use")
  56. def __init__(self):
  57. super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
  58. def invoke(self, arg, from_tty):
  59. gdb.write(
  60. "Address{0} Module Size Used by\n".format(
  61. " " if utils.get_long_type().sizeof == 8 else ""))
  62. for module in ModuleList():
  63. ref = 0
  64. module_refptr = module['refptr']
  65. for cpu in cpus.CpuList("cpu_possible_mask"):
  66. refptr = cpus.per_cpu(module_refptr, cpu)
  67. ref += refptr['incs']
  68. ref -= refptr['decs']
  69. gdb.write("{address} {name:<19} {size:>8} {ref}".format(
  70. address=str(module['module_core']).split()[0],
  71. name=module['name'].string(),
  72. size=str(module['core_size']),
  73. ref=str(ref)))
  74. source_list = module['source_list']
  75. t = self._module_use_type.get_type().pointer()
  76. entry = source_list['next']
  77. first = True
  78. while entry != source_list.address:
  79. use = utils.container_of(entry, t, "source_list")
  80. gdb.write("{separator}{name}".format(
  81. separator=" " if first else ",",
  82. name=use['source']['name'].string()))
  83. first = False
  84. entry = entry['next']
  85. gdb.write("\n")
  86. LxLsmod()