2
0

coroutine.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #
  2. # GDB debugging support
  3. #
  4. # Copyright 2012 Red Hat, Inc. and/or its affiliates
  5. #
  6. # Authors:
  7. # Avi Kivity <avi@redhat.com>
  8. #
  9. # This work is licensed under the terms of the GNU GPL, version 2
  10. # or later. See the COPYING file in the top-level directory.
  11. import gdb
  12. VOID_PTR = gdb.lookup_type('void').pointer()
  13. def pthread_self():
  14. '''Fetch the base address of TLS.'''
  15. return gdb.parse_and_eval("$fs_base")
  16. def get_glibc_pointer_guard():
  17. '''Fetch glibc pointer guard value'''
  18. fs_base = pthread_self()
  19. return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
  20. def glibc_ptr_demangle(val, pointer_guard):
  21. '''Undo effect of glibc's PTR_MANGLE()'''
  22. return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
  23. def get_jmpbuf_regs(jmpbuf):
  24. JB_RBX = 0
  25. JB_RBP = 1
  26. JB_R12 = 2
  27. JB_R13 = 3
  28. JB_R14 = 4
  29. JB_R15 = 5
  30. JB_RSP = 6
  31. JB_PC = 7
  32. pointer_guard = get_glibc_pointer_guard()
  33. return {'rbx': jmpbuf[JB_RBX],
  34. 'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard),
  35. 'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard),
  36. 'r12': jmpbuf[JB_R12],
  37. 'r13': jmpbuf[JB_R13],
  38. 'r14': jmpbuf[JB_R14],
  39. 'r15': jmpbuf[JB_R15],
  40. 'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) }
  41. def bt_jmpbuf(jmpbuf):
  42. '''Backtrace a jmpbuf'''
  43. regs = get_jmpbuf_regs(jmpbuf)
  44. old = dict()
  45. # remember current stack frame and select the topmost
  46. # so that register modifications don't wreck it
  47. selected_frame = gdb.selected_frame()
  48. gdb.newest_frame().select()
  49. for i in regs:
  50. old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
  51. for i in regs:
  52. gdb.execute('set $%s = %s' % (i, regs[i]))
  53. gdb.execute('bt')
  54. for i in regs:
  55. gdb.execute('set $%s = %s' % (i, old[i]))
  56. selected_frame.select()
  57. def co_cast(co):
  58. return co.cast(gdb.lookup_type('CoroutineUContext').pointer())
  59. def coroutine_to_jmpbuf(co):
  60. coroutine_pointer = co_cast(co)
  61. return coroutine_pointer['env']['__jmpbuf']
  62. class CoroutineCommand(gdb.Command):
  63. '''Display coroutine backtrace'''
  64. def __init__(self):
  65. gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
  66. gdb.COMPLETE_NONE)
  67. def invoke(self, arg, from_tty):
  68. argv = gdb.string_to_argv(arg)
  69. if len(argv) != 1:
  70. gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
  71. return
  72. bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
  73. class CoroutineBt(gdb.Command):
  74. '''Display backtrace including coroutine switches'''
  75. def __init__(self):
  76. gdb.Command.__init__(self, 'qemu bt', gdb.COMMAND_STACK,
  77. gdb.COMPLETE_NONE)
  78. def invoke(self, arg, from_tty):
  79. gdb.execute("bt")
  80. if gdb.parse_and_eval("qemu_in_coroutine()") == False:
  81. return
  82. co_ptr = gdb.parse_and_eval("qemu_coroutine_self()")
  83. while True:
  84. co = co_cast(co_ptr)
  85. co_ptr = co["base"]["caller"]
  86. if co_ptr == 0:
  87. break
  88. gdb.write("Coroutine at " + str(co_ptr) + ":\n")
  89. bt_jmpbuf(coroutine_to_jmpbuf(co_ptr))
  90. class CoroutineSPFunction(gdb.Function):
  91. def __init__(self):
  92. gdb.Function.__init__(self, 'qemu_coroutine_sp')
  93. def invoke(self, addr):
  94. return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
  95. class CoroutinePCFunction(gdb.Function):
  96. def __init__(self):
  97. gdb.Function.__init__(self, 'qemu_coroutine_pc')
  98. def invoke(self, addr):
  99. return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)