qemu-gdb.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import os, sys
  16. # Annoyingly, gdb doesn't put the directory of scripts onto the
  17. # module search path. Do it manually.
  18. sys.path.append(os.path.dirname(__file__))
  19. from qemugdb import mtree
  20. def get_fs_base():
  21. '''Fetch %fs base value using arch_prctl(ARCH_GET_FS)'''
  22. # %rsp - 120 is scratch space according to the SystemV ABI
  23. old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
  24. gdb.execute('call arch_prctl(0x1003, $rsp - 120)', False, True)
  25. fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
  26. gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True)
  27. return fs_base
  28. def get_glibc_pointer_guard():
  29. '''Fetch glibc pointer guard value'''
  30. fs_base = get_fs_base()
  31. return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
  32. def glibc_ptr_demangle(val, pointer_guard):
  33. '''Undo effect of glibc's PTR_MANGLE()'''
  34. return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
  35. def bt_jmpbuf(jmpbuf):
  36. '''Backtrace a jmpbuf'''
  37. JB_RBX = 0
  38. JB_RBP = 1
  39. JB_R12 = 2
  40. JB_R13 = 3
  41. JB_R14 = 4
  42. JB_R15 = 5
  43. JB_RSP = 6
  44. JB_PC = 7
  45. old_rbx = gdb.parse_and_eval('(uint64_t)$rbx')
  46. old_rbp = gdb.parse_and_eval('(uint64_t)$rbp')
  47. old_rsp = gdb.parse_and_eval('(uint64_t)$rsp')
  48. old_r12 = gdb.parse_and_eval('(uint64_t)$r12')
  49. old_r13 = gdb.parse_and_eval('(uint64_t)$r13')
  50. old_r14 = gdb.parse_and_eval('(uint64_t)$r14')
  51. old_r15 = gdb.parse_and_eval('(uint64_t)$r15')
  52. old_rip = gdb.parse_and_eval('(uint64_t)$rip')
  53. pointer_guard = get_glibc_pointer_guard()
  54. gdb.execute('set $rbx = %s' % jmpbuf[JB_RBX])
  55. gdb.execute('set $rbp = %s' % glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard))
  56. gdb.execute('set $rsp = %s' % glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard))
  57. gdb.execute('set $r12 = %s' % jmpbuf[JB_R12])
  58. gdb.execute('set $r13 = %s' % jmpbuf[JB_R13])
  59. gdb.execute('set $r14 = %s' % jmpbuf[JB_R14])
  60. gdb.execute('set $r15 = %s' % jmpbuf[JB_R15])
  61. gdb.execute('set $rip = %s' % glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard))
  62. gdb.execute('bt')
  63. gdb.execute('set $rbx = %s' % old_rbx)
  64. gdb.execute('set $rbp = %s' % old_rbp)
  65. gdb.execute('set $rsp = %s' % old_rsp)
  66. gdb.execute('set $r12 = %s' % old_r12)
  67. gdb.execute('set $r13 = %s' % old_r13)
  68. gdb.execute('set $r14 = %s' % old_r14)
  69. gdb.execute('set $r15 = %s' % old_r15)
  70. gdb.execute('set $rip = %s' % old_rip)
  71. class QemuCommand(gdb.Command):
  72. '''Prefix for QEMU debug support commands'''
  73. def __init__(self):
  74. gdb.Command.__init__(self, 'qemu', gdb.COMMAND_DATA,
  75. gdb.COMPLETE_NONE, True)
  76. class CoroutineCommand(gdb.Command):
  77. '''Display coroutine backtrace'''
  78. def __init__(self):
  79. gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
  80. gdb.COMPLETE_NONE)
  81. def invoke(self, arg, from_tty):
  82. argv = gdb.string_to_argv(arg)
  83. if len(argv) != 1:
  84. gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
  85. return
  86. coroutine_pointer = gdb.parse_and_eval(argv[0]).cast(gdb.lookup_type('CoroutineUContext').pointer())
  87. bt_jmpbuf(coroutine_pointer['env']['__jmpbuf'])
  88. QemuCommand()
  89. CoroutineCommand()
  90. mtree.MtreeCommand()