modules.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 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()
  51. class LxLsmod(gdb.Command):
  52. """List currently loaded modules."""
  53. _module_use_type = utils.CachedType("struct module_use")
  54. def __init__(self):
  55. super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
  56. def invoke(self, arg, from_tty):
  57. gdb.write(
  58. "Address{0} Module Size Used by\n".format(
  59. " " if utils.get_long_type().sizeof == 8 else ""))
  60. for module in ModuleList():
  61. ref = 0
  62. module_refptr = module['refptr']
  63. for cpu in cpus.CpuList("cpu_possible_mask"):
  64. refptr = cpus.per_cpu(module_refptr, cpu)
  65. ref += refptr['incs']
  66. ref -= refptr['decs']
  67. gdb.write("{address} {name:<19} {size:>8} {ref}".format(
  68. address=str(module['module_core']).split()[0],
  69. name=module['name'].string(),
  70. size=module['core_size'],
  71. ref=ref))
  72. source_list = module['source_list']
  73. t = self._module_use_type.get_type().pointer()
  74. entry = source_list['next']
  75. first = True
  76. while entry != source_list.address:
  77. use = utils.container_of(entry, t, "source_list")
  78. gdb.write("{separator}{name}".format(
  79. separator=" " if first else ",",
  80. name=use['source']['name'].string()))
  81. first = False
  82. entry = entry['next']
  83. gdb.write("\n")
  84. LxLsmod()