utils.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #
  2. # gdb helper commands and functions for Linux kernel debugging
  3. #
  4. # common utilities
  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. class CachedType:
  15. def __init__(self, name):
  16. self._type = None
  17. self._name = name
  18. def _new_objfile_handler(self, event):
  19. self._type = None
  20. gdb.events.new_objfile.disconnect(self._new_objfile_handler)
  21. def get_type(self):
  22. if self._type is None:
  23. self._type = gdb.lookup_type(self._name)
  24. if self._type is None:
  25. raise gdb.GdbError(
  26. "cannot resolve type '{0}'".format(self._name))
  27. if hasattr(gdb, 'events') and hasattr(gdb.events, 'new_objfile'):
  28. gdb.events.new_objfile.connect(self._new_objfile_handler)
  29. return self._type
  30. long_type = CachedType("long")
  31. def get_long_type():
  32. global long_type
  33. return long_type.get_type()
  34. def offset_of(typeobj, field):
  35. element = gdb.Value(0).cast(typeobj)
  36. return int(str(element[field].address).split()[0], 16)
  37. def container_of(ptr, typeobj, member):
  38. return (ptr.cast(get_long_type()) -
  39. offset_of(typeobj, member)).cast(typeobj)
  40. class ContainerOf(gdb.Function):
  41. """Return pointer to containing data structure.
  42. $container_of(PTR, "TYPE", "ELEMENT"): Given PTR, return a pointer to the
  43. data structure of the type TYPE in which PTR is the address of ELEMENT.
  44. Note that TYPE and ELEMENT have to be quoted as strings."""
  45. def __init__(self):
  46. super(ContainerOf, self).__init__("container_of")
  47. def invoke(self, ptr, typename, elementname):
  48. return container_of(ptr, gdb.lookup_type(typename.string()).pointer(),
  49. elementname.string())
  50. ContainerOf()
  51. BIG_ENDIAN = 0
  52. LITTLE_ENDIAN = 1
  53. target_endianness = None
  54. def get_target_endianness():
  55. global target_endianness
  56. if target_endianness is None:
  57. endian = gdb.execute("show endian", to_string=True)
  58. if "little endian" in endian:
  59. target_endianness = LITTLE_ENDIAN
  60. elif "big endian" in endian:
  61. target_endianness = BIG_ENDIAN
  62. else:
  63. raise gdb.GdbError("unknown endianness '{0}'".format(str(endian)))
  64. return target_endianness
  65. def read_u16(buffer):
  66. if get_target_endianness() == LITTLE_ENDIAN:
  67. return ord(buffer[0]) + (ord(buffer[1]) << 8)
  68. else:
  69. return ord(buffer[1]) + (ord(buffer[0]) << 8)
  70. def read_u32(buffer):
  71. if get_target_endianness() == LITTLE_ENDIAN:
  72. return read_u16(buffer[0:2]) + (read_u16(buffer[2:4]) << 16)
  73. else:
  74. return read_u16(buffer[2:4]) + (read_u16(buffer[0:2]) << 16)
  75. def read_u64(buffer):
  76. if get_target_endianness() == LITTLE_ENDIAN:
  77. return read_u32(buffer[0:4]) + (read_u32(buffer[4:8]) << 32)
  78. else:
  79. return read_u32(buffer[4:8]) + (read_u32(buffer[0:4]) << 32)
  80. target_arch = None
  81. def is_target_arch(arch):
  82. if hasattr(gdb.Frame, 'architecture'):
  83. return arch in gdb.newest_frame().architecture().name()
  84. else:
  85. global target_arch
  86. if target_arch is None:
  87. target_arch = gdb.execute("show architecture", to_string=True)
  88. return arch in target_arch
  89. GDBSERVER_QEMU = 0
  90. GDBSERVER_KGDB = 1
  91. gdbserver_type = None
  92. def get_gdbserver_type():
  93. def exit_handler(event):
  94. global gdbserver_type
  95. gdbserver_type = None
  96. gdb.events.exited.disconnect(exit_handler)
  97. def probe_qemu():
  98. try:
  99. return gdb.execute("monitor info version", to_string=True) != ""
  100. except:
  101. return False
  102. def probe_kgdb():
  103. try:
  104. thread_info = gdb.execute("info thread 2", to_string=True)
  105. return "shadowCPU0" in thread_info
  106. except:
  107. return False
  108. global gdbserver_type
  109. if gdbserver_type is None:
  110. if probe_qemu():
  111. gdbserver_type = GDBSERVER_QEMU
  112. elif probe_kgdb():
  113. gdbserver_type = GDBSERVER_KGDB
  114. if gdbserver_type is not None and hasattr(gdb, 'events'):
  115. gdb.events.exited.connect(exit_handler)
  116. return gdbserver_type