symbols.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. class LxSymbols(gdb.Command):
  19. """(Re-)load symbols of Linux kernel and currently loaded modules.
  20. The kernel (vmlinux) is taken from the current working directly. Modules (.ko)
  21. are scanned recursively, starting in the same directory. Optionally, the module
  22. search path can be extended by a space separated list of paths passed to the
  23. lx-symbols command."""
  24. module_paths = []
  25. module_files = []
  26. module_files_updated = False
  27. def __init__(self):
  28. super(LxSymbols, self).__init__("lx-symbols", gdb.COMMAND_FILES,
  29. gdb.COMPLETE_FILENAME)
  30. def _update_module_files(self):
  31. self.module_files = []
  32. for path in self.module_paths:
  33. gdb.write("scanning for modules in {0}\n".format(path))
  34. for root, dirs, files in os.walk(path):
  35. for name in files:
  36. if name.endswith(".ko"):
  37. self.module_files.append(root + "/" + name)
  38. self.module_files_updated = True
  39. def _get_module_file(self, module_name):
  40. module_pattern = ".*/{0}\.ko$".format(
  41. string.replace(module_name, "_", r"[_\-]"))
  42. for name in self.module_files:
  43. if re.match(module_pattern, name) and os.path.exists(name):
  44. return name
  45. return None
  46. def _section_arguments(self, module):
  47. try:
  48. sect_attrs = module['sect_attrs'].dereference()
  49. except gdb.error:
  50. return ""
  51. attrs = sect_attrs['attrs']
  52. section_name_to_address = {
  53. attrs[n]['name'].string() : attrs[n]['address']
  54. for n in range(sect_attrs['nsections'])}
  55. args = []
  56. for section_name in [".data", ".data..read_mostly", ".rodata", ".bss"]:
  57. address = section_name_to_address.get(section_name)
  58. if address:
  59. args.append(" -s {name} {addr}".format(
  60. name=section_name, addr=str(address)))
  61. return "".join(args)
  62. def load_module_symbols(self, module):
  63. module_name = module['name'].string()
  64. module_addr = str(module['module_core']).split()[0]
  65. module_file = self._get_module_file(module_name)
  66. if not module_file and not self.module_files_updated:
  67. self._update_module_files()
  68. module_file = self._get_module_file(module_name)
  69. if module_file:
  70. gdb.write("loading @{addr}: {filename}\n".format(
  71. addr=module_addr, filename=module_file))
  72. cmdline = "add-symbol-file {filename} {addr}{sections}".format(
  73. filename=module_file,
  74. addr=module_addr,
  75. sections=self._section_arguments(module))
  76. gdb.execute(cmdline, to_string=True)
  77. else:
  78. gdb.write("no module object found for '{0}'\n".format(module_name))
  79. def load_all_symbols(self):
  80. gdb.write("loading vmlinux\n")
  81. # Dropping symbols will disable all breakpoints. So save their states
  82. # and restore them afterward.
  83. saved_states = []
  84. if hasattr(gdb, 'breakpoints') and not gdb.breakpoints() is None:
  85. for bp in gdb.breakpoints():
  86. saved_states.append({'breakpoint': bp, 'enabled': bp.enabled})
  87. # drop all current symbols and reload vmlinux
  88. gdb.execute("symbol-file", to_string=True)
  89. gdb.execute("symbol-file vmlinux")
  90. module_list = modules.ModuleList()
  91. if not module_list:
  92. gdb.write("no modules found\n")
  93. else:
  94. [self.load_module_symbols(module) for module in module_list]
  95. for saved_state in saved_states:
  96. saved_state['breakpoint'].enabled = saved_state['enabled']
  97. def invoke(self, arg, from_tty):
  98. self.module_paths = arg.split()
  99. self.module_paths.append(os.getcwd())
  100. # enforce update
  101. self.module_files = []
  102. self.module_files_updated = False
  103. self.load_all_symbols()
  104. LxSymbols()