2
0

coroutine.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. for i in regs:
  63. old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
  64. for i in regs:
  65. gdb.execute('set $%s = %s' % (i, regs[i]))
  66. gdb.execute('bt')
  67. for i in regs:
  68. gdb.execute('set $%s = %s' % (i, old[i]))
  69. def coroutine_to_jmpbuf(co):
  70. coroutine_pointer = co.cast(gdb.lookup_type('CoroutineUContext').pointer())
  71. return coroutine_pointer['env']['__jmpbuf']
  72. class CoroutineCommand(gdb.Command):
  73. '''Display coroutine backtrace'''
  74. def __init__(self):
  75. gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
  76. gdb.COMPLETE_NONE)
  77. def invoke(self, arg, from_tty):
  78. argv = gdb.string_to_argv(arg)
  79. if len(argv) != 1:
  80. gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
  81. return
  82. bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
  83. class CoroutineSPFunction(gdb.Function):
  84. def __init__(self):
  85. gdb.Function.__init__(self, 'qemu_coroutine_sp')
  86. def invoke(self, addr):
  87. return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
  88. class CoroutinePCFunction(gdb.Function):
  89. def __init__(self):
  90. gdb.Function.__init__(self, 'qemu_coroutine_pc')
  91. def invoke(self, addr):
  92. return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)