2
0

coroutine.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 get_fs_base():
  14. '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is
  15. pthread_self().'''
  16. # %rsp - 120 is scratch space according to the SystemV ABI
  17. old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
  18. gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True)
  19. fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
  20. gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True)
  21. return fs_base
  22. def pthread_self():
  23. '''Fetch pthread_self() from the glibc start_thread function.'''
  24. f = gdb.newest_frame()
  25. while f.name() != 'start_thread':
  26. f = f.older()
  27. if f is None:
  28. return get_fs_base()
  29. try:
  30. return f.read_var("arg")
  31. except ValueError:
  32. return get_fs_base()
  33. def get_glibc_pointer_guard():
  34. '''Fetch glibc pointer guard value'''
  35. fs_base = pthread_self()
  36. return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
  37. def glibc_ptr_demangle(val, pointer_guard):
  38. '''Undo effect of glibc's PTR_MANGLE()'''
  39. return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
  40. def get_jmpbuf_regs(jmpbuf):
  41. JB_RBX = 0
  42. JB_RBP = 1
  43. JB_R12 = 2
  44. JB_R13 = 3
  45. JB_R14 = 4
  46. JB_R15 = 5
  47. JB_RSP = 6
  48. JB_PC = 7
  49. pointer_guard = get_glibc_pointer_guard()
  50. return {'rbx': jmpbuf[JB_RBX],
  51. 'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard),
  52. 'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard),
  53. 'r12': jmpbuf[JB_R12],
  54. 'r13': jmpbuf[JB_R13],
  55. 'r14': jmpbuf[JB_R14],
  56. 'r15': jmpbuf[JB_R15],
  57. 'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) }
  58. def bt_jmpbuf(jmpbuf):
  59. '''Backtrace a jmpbuf'''
  60. regs = get_jmpbuf_regs(jmpbuf)
  61. old = dict()
  62. # remember current stack frame and select the topmost
  63. # so that register modifications don't wreck it
  64. selected_frame = gdb.selected_frame()
  65. gdb.newest_frame().select()
  66. for i in regs:
  67. old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
  68. for i in regs:
  69. gdb.execute('set $%s = %s' % (i, regs[i]))
  70. gdb.execute('bt')
  71. for i in regs:
  72. gdb.execute('set $%s = %s' % (i, old[i]))
  73. selected_frame.select()
  74. def co_cast(co):
  75. return co.cast(gdb.lookup_type('CoroutineUContext').pointer())
  76. def coroutine_to_jmpbuf(co):
  77. coroutine_pointer = co_cast(co)
  78. return coroutine_pointer['env']['__jmpbuf']
  79. class CoroutineCommand(gdb.Command):
  80. '''Display coroutine backtrace'''
  81. def __init__(self):
  82. gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
  83. gdb.COMPLETE_NONE)
  84. def invoke(self, arg, from_tty):
  85. argv = gdb.string_to_argv(arg)
  86. if len(argv) != 1:
  87. gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
  88. return
  89. bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
  90. class CoroutineBt(gdb.Command):
  91. '''Display backtrace including coroutine switches'''
  92. def __init__(self):
  93. gdb.Command.__init__(self, 'qemu bt', gdb.COMMAND_STACK,
  94. gdb.COMPLETE_NONE)
  95. def invoke(self, arg, from_tty):
  96. gdb.execute("bt")
  97. if gdb.parse_and_eval("qemu_in_coroutine()") == False:
  98. return
  99. co_ptr = gdb.parse_and_eval("qemu_coroutine_self()")
  100. while True:
  101. co = co_cast(co_ptr)
  102. co_ptr = co["base"]["caller"]
  103. if co_ptr == 0:
  104. break
  105. gdb.write("Coroutine at " + str(co_ptr) + ":\n")
  106. bt_jmpbuf(coroutine_to_jmpbuf(co_ptr))
  107. class CoroutineSPFunction(gdb.Function):
  108. def __init__(self):
  109. gdb.Function.__init__(self, 'qemu_coroutine_sp')
  110. def invoke(self, addr):
  111. return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
  112. class CoroutinePCFunction(gdb.Function):
  113. def __init__(self):
  114. gdb.Function.__init__(self, 'qemu_coroutine_pc')
  115. def invoke(self, addr):
  116. return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)