coroutine.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python
  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. See
  10. # the COPYING file in the top-level directory.
  11. #
  12. # Contributions after 2012-01-13 are licensed under the terms of the
  13. # GNU GPL, version 2 or (at your option) any later version.
  14. import gdb
  15. def get_fs_base():
  16. '''Fetch %fs base value using arch_prctl(ARCH_GET_FS)'''
  17. # %rsp - 120 is scratch space according to the SystemV ABI
  18. old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
  19. gdb.execute('call arch_prctl(0x1003, $rsp - 120)', False, True)
  20. fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
  21. gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True)
  22. return fs_base
  23. def get_glibc_pointer_guard():
  24. '''Fetch glibc pointer guard value'''
  25. fs_base = get_fs_base()
  26. return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
  27. def glibc_ptr_demangle(val, pointer_guard):
  28. '''Undo effect of glibc's PTR_MANGLE()'''
  29. return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
  30. def bt_jmpbuf(jmpbuf):
  31. '''Backtrace a jmpbuf'''
  32. JB_RBX = 0
  33. JB_RBP = 1
  34. JB_R12 = 2
  35. JB_R13 = 3
  36. JB_R14 = 4
  37. JB_R15 = 5
  38. JB_RSP = 6
  39. JB_PC = 7
  40. old_rbx = gdb.parse_and_eval('(uint64_t)$rbx')
  41. old_rbp = gdb.parse_and_eval('(uint64_t)$rbp')
  42. old_rsp = gdb.parse_and_eval('(uint64_t)$rsp')
  43. old_r12 = gdb.parse_and_eval('(uint64_t)$r12')
  44. old_r13 = gdb.parse_and_eval('(uint64_t)$r13')
  45. old_r14 = gdb.parse_and_eval('(uint64_t)$r14')
  46. old_r15 = gdb.parse_and_eval('(uint64_t)$r15')
  47. old_rip = gdb.parse_and_eval('(uint64_t)$rip')
  48. pointer_guard = get_glibc_pointer_guard()
  49. gdb.execute('set $rbx = %s' % jmpbuf[JB_RBX])
  50. gdb.execute('set $rbp = %s' % glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard))
  51. gdb.execute('set $rsp = %s' % glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard))
  52. gdb.execute('set $r12 = %s' % jmpbuf[JB_R12])
  53. gdb.execute('set $r13 = %s' % jmpbuf[JB_R13])
  54. gdb.execute('set $r14 = %s' % jmpbuf[JB_R14])
  55. gdb.execute('set $r15 = %s' % jmpbuf[JB_R15])
  56. gdb.execute('set $rip = %s' % glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard))
  57. gdb.execute('bt')
  58. gdb.execute('set $rbx = %s' % old_rbx)
  59. gdb.execute('set $rbp = %s' % old_rbp)
  60. gdb.execute('set $rsp = %s' % old_rsp)
  61. gdb.execute('set $r12 = %s' % old_r12)
  62. gdb.execute('set $r13 = %s' % old_r13)
  63. gdb.execute('set $r14 = %s' % old_r14)
  64. gdb.execute('set $r15 = %s' % old_r15)
  65. gdb.execute('set $rip = %s' % old_rip)
  66. class CoroutineCommand(gdb.Command):
  67. '''Display coroutine backtrace'''
  68. def __init__(self):
  69. gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
  70. gdb.COMPLETE_NONE)
  71. def invoke(self, arg, from_tty):
  72. argv = gdb.string_to_argv(arg)
  73. if len(argv) != 1:
  74. gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
  75. return
  76. coroutine_pointer = gdb.parse_and_eval(argv[0]).cast(gdb.lookup_type('CoroutineUContext').pointer())
  77. bt_jmpbuf(coroutine_pointer['env']['__jmpbuf'])