symbols.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #
  2. # gdb helper commands and functions for Linux kernel debugging
  3. #
  4. # load kernel and module symbols
  5. #
  6. # Copyright (c) Siemens AG, 2011-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. import os
  15. import re
  16. import string
  17. from linux import modules, utils
  18. if hasattr(gdb, 'Breakpoint'):
  19. class LoadModuleBreakpoint(gdb.Breakpoint):
  20. def __init__(self, spec, gdb_command):
  21. super(LoadModuleBreakpoint, self).__init__(spec, internal=True)
  22. self.silent = True
  23. self.gdb_command = gdb_command
  24. def stop(self):
  25. module = gdb.parse_and_eval("mod")
  26. module_name = module['name'].string()
  27. cmd = self.gdb_command
  28. # enforce update if object file is not found
  29. cmd.module_files_updated = False
  30. if module_name in cmd.loaded_modules:
  31. gdb.write("refreshing all symbols to reload module "
  32. "'{0}'\n".format(module_name))
  33. cmd.load_all_symbols()
  34. else:
  35. cmd.load_module_symbols(module)
  36. return False
  37. class LxSymbols(gdb.Command):
  38. """(Re-)load symbols of Linux kernel and currently loaded modules.
  39. The kernel (vmlinux) is taken from the current working directly. Modules (.ko)
  40. are scanned recursively, starting in the same directory. Optionally, the module
  41. search path can be extended by a space separated list of paths passed to the
  42. lx-symbols command."""
  43. module_paths = []
  44. module_files = []
  45. module_files_updated = False
  46. loaded_modules = []
  47. breakpoint = None
  48. def __init__(self):
  49. super(LxSymbols, self).__init__("lx-symbols", gdb.COMMAND_FILES,
  50. gdb.COMPLETE_FILENAME)
  51. def _update_module_files(self):
  52. self.module_files = []
  53. for path in self.module_paths:
  54. gdb.write("scanning for modules in {0}\n".format(path))
  55. for root, dirs, files in os.walk(path):
  56. for name in files:
  57. if name.endswith(".ko"):
  58. self.module_files.append(root + "/" + name)
  59. self.module_files_updated = True
  60. def _get_module_file(self, module_name):
  61. module_pattern = ".*/{0}\.ko$".format(
  62. module_name.replace("_", r"[_\-]"))
  63. for name in self.module_files:
  64. if re.match(module_pattern, name) and os.path.exists(name):
  65. return name
  66. return None
  67. def _section_arguments(self, module):
  68. try:
  69. sect_attrs = module['sect_attrs'].dereference()
  70. except gdb.error:
  71. return ""
  72. attrs = sect_attrs['attrs']
  73. section_name_to_address = {
  74. attrs[n]['name'].string() : attrs[n]['address']
  75. for n in range(int(sect_attrs['nsections']))}
  76. args = []
  77. for section_name in [".data", ".data..read_mostly", ".rodata", ".bss"]:
  78. address = section_name_to_address.get(section_name)
  79. if address:
  80. args.append(" -s {name} {addr}".format(
  81. name=section_name, addr=str(address)))
  82. return "".join(args)
  83. def load_module_symbols(self, module):
  84. module_name = module['name'].string()
  85. module_addr = str(module['module_core']).split()[0]
  86. module_file = self._get_module_file(module_name)
  87. if not module_file and not self.module_files_updated:
  88. self._update_module_files()
  89. module_file = self._get_module_file(module_name)
  90. if module_file:
  91. gdb.write("loading @{addr}: {filename}\n".format(
  92. addr=module_addr, filename=module_file))
  93. cmdline = "add-symbol-file {filename} {addr}{sections}".format(
  94. filename=module_file,
  95. addr=module_addr,
  96. sections=self._section_arguments(module))
  97. gdb.execute(cmdline, to_string=True)
  98. if not module_name in self.loaded_modules:
  99. self.loaded_modules.append(module_name)
  100. else:
  101. gdb.write("no module object found for '{0}'\n".format(module_name))
  102. def load_all_symbols(self):
  103. gdb.write("loading vmlinux\n")
  104. # Dropping symbols will disable all breakpoints. So save their states
  105. # and restore them afterward.
  106. saved_states = []
  107. if hasattr(gdb, 'breakpoints') and not gdb.breakpoints() is None:
  108. for bp in gdb.breakpoints():
  109. saved_states.append({'breakpoint': bp, 'enabled': bp.enabled})
  110. # drop all current symbols and reload vmlinux
  111. gdb.execute("symbol-file", to_string=True)
  112. gdb.execute("symbol-file vmlinux")
  113. self.loaded_modules = []
  114. module_list = modules.module_list()
  115. if not module_list:
  116. gdb.write("no modules found\n")
  117. else:
  118. [self.load_module_symbols(module) for module in module_list]
  119. for saved_state in saved_states:
  120. saved_state['breakpoint'].enabled = saved_state['enabled']
  121. def invoke(self, arg, from_tty):
  122. self.module_paths = arg.split()
  123. self.module_paths.append(os.getcwd())
  124. # enforce update
  125. self.module_files = []
  126. self.module_files_updated = False
  127. self.load_all_symbols()
  128. if hasattr(gdb, 'Breakpoint'):
  129. if not self.breakpoint is None:
  130. self.breakpoint.delete()
  131. self.breakpoint = None
  132. self.breakpoint = LoadModuleBreakpoint(
  133. "kernel/module.c:do_init_module", self)
  134. else:
  135. gdb.write("Note: symbol update on module loading not supported "
  136. "with this gdb version\n")
  137. LxSymbols()