scripts: add 'qemu coroutine' command to qemu-gdb.py

The 'qemu coroutine <coroutine-address>' GDB command prints the
backtrace for a CoroutineUContext.  This is useful for peeking inside
yielded coroutines that are waiting for file descriptor events, timers,
etc.

For example:

  $ gdb tests/test-coroutine
  (gdb) b test_yield
  (gdb) r
  (gdb) b qemu_coroutine_enter
  (gdb) c
  (gdb) c
  Continuing.

  Breakpoint 2, qemu_coroutine_enter (co=0x555555c66520, opaque=0x0) at qemu-coroutine.c:103
  103	{
  (gdb) source scripts/qemu-gdb.py
  (gdb) qemu coroutine 0x555555c66520
  #0  0x000055555557a740 in qemu_coroutine_switch (from_=<optimized out>, to_=0x7ffff7f90a70, action=COROUTINE_YIELD) at coroutine-ucontext.c:177
  #1  0x0000555555566af9 in yield_5_times (opaque=0x7fffffffdbb7) at tests/test-coroutine.c:107
  #2  0x000055555557a7aa in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at coroutine-ucontext.c:80
  #3  0x00007ffff08de000 in __start_context () at /lib64/libc.so.6

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 1427409754-8556-1-git-send-email-stefanha@redhat.com
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Stefan Hajnoczi 2015-03-26 22:42:34 +00:00 committed by Kevin Wolf
parent 1faa5bb732
commit 9eddd6a4b3
1 changed files with 75 additions and 0 deletions

View File

@ -22,12 +22,86 @@ def isnull(ptr):
def int128(p):
return long(p['lo']) + (long(p['hi']) << 64)
def get_fs_base():
'''Fetch %fs base value using arch_prctl(ARCH_GET_FS)'''
# %rsp - 120 is scratch space according to the SystemV ABI
old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
gdb.execute('call arch_prctl(0x1003, $rsp - 120)', False, True)
fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True)
return fs_base
def get_glibc_pointer_guard():
'''Fetch glibc pointer guard value'''
fs_base = get_fs_base()
return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
def glibc_ptr_demangle(val, pointer_guard):
'''Undo effect of glibc's PTR_MANGLE()'''
return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
def bt_jmpbuf(jmpbuf):
'''Backtrace a jmpbuf'''
JB_RBX = 0
JB_RBP = 1
JB_R12 = 2
JB_R13 = 3
JB_R14 = 4
JB_R15 = 5
JB_RSP = 6
JB_PC = 7
old_rbx = gdb.parse_and_eval('(uint64_t)$rbx')
old_rbp = gdb.parse_and_eval('(uint64_t)$rbp')
old_rsp = gdb.parse_and_eval('(uint64_t)$rsp')
old_r12 = gdb.parse_and_eval('(uint64_t)$r12')
old_r13 = gdb.parse_and_eval('(uint64_t)$r13')
old_r14 = gdb.parse_and_eval('(uint64_t)$r14')
old_r15 = gdb.parse_and_eval('(uint64_t)$r15')
old_rip = gdb.parse_and_eval('(uint64_t)$rip')
pointer_guard = get_glibc_pointer_guard()
gdb.execute('set $rbx = %s' % jmpbuf[JB_RBX])
gdb.execute('set $rbp = %s' % glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard))
gdb.execute('set $rsp = %s' % glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard))
gdb.execute('set $r12 = %s' % jmpbuf[JB_R12])
gdb.execute('set $r13 = %s' % jmpbuf[JB_R13])
gdb.execute('set $r14 = %s' % jmpbuf[JB_R14])
gdb.execute('set $r15 = %s' % jmpbuf[JB_R15])
gdb.execute('set $rip = %s' % glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard))
gdb.execute('bt')
gdb.execute('set $rbx = %s' % old_rbx)
gdb.execute('set $rbp = %s' % old_rbp)
gdb.execute('set $rsp = %s' % old_rsp)
gdb.execute('set $r12 = %s' % old_r12)
gdb.execute('set $r13 = %s' % old_r13)
gdb.execute('set $r14 = %s' % old_r14)
gdb.execute('set $r15 = %s' % old_r15)
gdb.execute('set $rip = %s' % old_rip)
class QemuCommand(gdb.Command):
'''Prefix for QEMU debug support commands'''
def __init__(self):
gdb.Command.__init__(self, 'qemu', gdb.COMMAND_DATA,
gdb.COMPLETE_NONE, True)
class CoroutineCommand(gdb.Command):
'''Display coroutine backtrace'''
def __init__(self):
gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
gdb.COMPLETE_NONE)
def invoke(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 1:
gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
return
coroutine_pointer = gdb.parse_and_eval(argv[0]).cast(gdb.lookup_type('CoroutineUContext').pointer())
bt_jmpbuf(coroutine_pointer['env']['__jmpbuf'])
class MtreeCommand(gdb.Command):
'''Display the memory tree hierarchy'''
def __init__(self):
@ -86,4 +160,5 @@ class MtreeCommand(gdb.Command):
subregion = subregion['subregions_link']['tqe_next']
QemuCommand()
CoroutineCommand()
MtreeCommand()