utils.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_memoryview(inf, start, length):
  66. return memoryview(inf.read_memory(start, length))
  67. def read_u16(buffer):
  68. value = [0, 0]
  69. if type(buffer[0]) is str:
  70. value[0] = ord(buffer[0])
  71. value[1] = ord(buffer[1])
  72. else:
  73. value[0] = buffer[0]
  74. value[1] = buffer[1]
  75. if get_target_endianness() == LITTLE_ENDIAN:
  76. return value[0] + (value[1] << 8)
  77. else:
  78. return value[1] + (value[0] << 8)
  79. def read_u32(buffer):
  80. if get_target_endianness() == LITTLE_ENDIAN:
  81. return read_u16(buffer[0:2]) + (read_u16(buffer[2:4]) << 16)
  82. else:
  83. return read_u16(buffer[2:4]) + (read_u16(buffer[0:2]) << 16)
  84. def read_u64(buffer):
  85. if get_target_endianness() == LITTLE_ENDIAN:
  86. return read_u32(buffer[0:4]) + (read_u32(buffer[4:8]) << 32)
  87. else:
  88. return read_u32(buffer[4:8]) + (read_u32(buffer[0:4]) << 32)
  89. target_arch = None
  90. def is_target_arch(arch):
  91. if hasattr(gdb.Frame, 'architecture'):
  92. return arch in gdb.newest_frame().architecture().name()
  93. else:
  94. global target_arch
  95. if target_arch is None:
  96. target_arch = gdb.execute("show architecture", to_string=True)
  97. return arch in target_arch
  98. GDBSERVER_QEMU = 0
  99. GDBSERVER_KGDB = 1
  100. gdbserver_type = None
  101. def get_gdbserver_type():
  102. def exit_handler(event):
  103. global gdbserver_type
  104. gdbserver_type = None
  105. gdb.events.exited.disconnect(exit_handler)
  106. def probe_qemu():
  107. try:
  108. return gdb.execute("monitor info version", to_string=True) != ""
  109. except:
  110. return False
  111. def probe_kgdb():
  112. try:
  113. thread_info = gdb.execute("info thread 2", to_string=True)
  114. return "shadowCPU0" in thread_info
  115. except:
  116. return False
  117. global gdbserver_type
  118. if gdbserver_type is None:
  119. if probe_qemu():
  120. gdbserver_type = GDBSERVER_QEMU
  121. elif probe_kgdb():
  122. gdbserver_type = GDBSERVER_KGDB
  123. if gdbserver_type is not None and hasattr(gdb, 'events'):
  124. gdb.events.exited.connect(exit_handler)
  125. return gdbserver_type
  126. def gdb_eval_or_none(expresssion):
  127. try:
  128. return gdb.parse_and_eval(expresssion)
  129. except:
  130. return None
  131. def dentry_name(d):
  132. parent = d['d_parent']
  133. if parent == d or parent == 0:
  134. return ""
  135. p = dentry_name(d['d_parent']) + "/"
  136. return p + d['d_iname'].string()