modules.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, constants
  15. module_type = utils.CachedType("struct module")
  16. def has_modules():
  17. return utils.gdb_eval_or_none("modules") is not None
  18. def module_list():
  19. global module_type
  20. modules = utils.gdb_eval_or_none("modules")
  21. if modules is None:
  22. return
  23. module_ptr_type = module_type.get_type().pointer()
  24. for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
  25. yield module
  26. def find_module_by_name(name):
  27. for module in module_list():
  28. if module['name'].string() == name:
  29. return module
  30. return None
  31. class LxModule(gdb.Function):
  32. """Find module by name and return the module variable.
  33. $lx_module("MODULE"): Given the name MODULE, iterate over all loaded modules
  34. of the target and return that module variable which MODULE matches."""
  35. def __init__(self):
  36. super(LxModule, self).__init__("lx_module")
  37. def invoke(self, mod_name):
  38. mod_name = mod_name.string()
  39. module = find_module_by_name(mod_name)
  40. if module:
  41. return module.dereference()
  42. else:
  43. raise gdb.GdbError("Unable to find MODULE " + mod_name)
  44. LxModule()
  45. class LxLsmod(gdb.Command):
  46. """List currently loaded modules."""
  47. _module_use_type = utils.CachedType("struct module_use")
  48. def __init__(self):
  49. super(LxLsmod, self).__init__("lx-lsmod", gdb.COMMAND_DATA)
  50. def invoke(self, arg, from_tty):
  51. gdb.write(
  52. "Address{0} Module Size Used by\n".format(
  53. " " if utils.get_long_type().sizeof == 8 else ""))
  54. for module in module_list():
  55. text = module['mem'][constants.LX_MOD_TEXT]
  56. text_addr = str(text['base']).split()[0]
  57. total_size = 0
  58. for i in range(constants.LX_MOD_TEXT, constants.LX_MOD_RO_AFTER_INIT + 1):
  59. total_size += module['mem'][i]['size']
  60. gdb.write("{address} {name:<19} {size:>8} {ref}".format(
  61. address=text_addr,
  62. name=module['name'].string(),
  63. size=str(total_size),
  64. ref=str(module['refcnt']['counter'] - 1)))
  65. t = self._module_use_type.get_type().pointer()
  66. first = True
  67. sources = module['source_list']
  68. for use in lists.list_for_each_entry(sources, t, "source_list"):
  69. gdb.write("{separator}{name}".format(
  70. separator=" " if first else ",",
  71. name=use['source']['name'].string()))
  72. first = False
  73. gdb.write("\n")
  74. LxLsmod()
  75. def help():
  76. t = """Usage: lx-getmod-by-textaddr [Heximal Address]
  77. Example: lx-getmod-by-textaddr 0xffff800002d305ac\n"""
  78. gdb.write("Unrecognized command\n")
  79. raise gdb.GdbError(t)
  80. class LxFindTextAddrinMod(gdb.Command):
  81. '''Look up loaded kernel module by text address.'''
  82. def __init__(self):
  83. super(LxFindTextAddrinMod, self).__init__('lx-getmod-by-textaddr', gdb.COMMAND_SUPPORT)
  84. def invoke(self, arg, from_tty):
  85. args = gdb.string_to_argv(arg)
  86. if len(args) != 1:
  87. help()
  88. addr = gdb.Value(int(args[0], 16)).cast(utils.get_ulong_type())
  89. for mod in module_list():
  90. mod_text_start = mod['mem'][constants.LX_MOD_TEXT]['base']
  91. mod_text_end = mod_text_start + mod['mem'][constants.LX_MOD_TEXT]['size'].cast(utils.get_ulong_type())
  92. if addr >= mod_text_start and addr < mod_text_end:
  93. s = "0x%x" % addr + " is in " + mod['name'].string() + ".ko\n"
  94. gdb.write(s)
  95. return
  96. gdb.write("0x%x is not in any module text section\n" % addr)
  97. LxFindTextAddrinMod()