2020-05-12 13:32:33 +03:00
|
|
|
#
|
2015-08-14 20:46:30 +03:00
|
|
|
# GDB debugging support
|
|
|
|
#
|
|
|
|
# Copyright 2012 Red Hat, Inc. and/or its affiliates
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Avi Kivity <avi@redhat.com>
|
|
|
|
#
|
2019-03-11 15:13:53 +03:00
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2
|
|
|
|
# or later. See the COPYING file in the top-level directory.
|
2015-08-14 20:46:30 +03:00
|
|
|
|
|
|
|
import gdb
|
|
|
|
|
2015-10-12 11:02:54 +03:00
|
|
|
VOID_PTR = gdb.lookup_type('void').pointer()
|
|
|
|
|
2015-08-14 20:46:30 +03:00
|
|
|
def get_fs_base():
|
2015-10-12 11:02:52 +03:00
|
|
|
'''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is
|
|
|
|
pthread_self().'''
|
2015-08-14 20:46:30 +03:00
|
|
|
# %rsp - 120 is scratch space according to the SystemV ABI
|
|
|
|
old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
|
2019-02-06 18:14:25 +03:00
|
|
|
gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True)
|
2015-08-14 20:46:30 +03:00
|
|
|
fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
|
|
|
|
gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True)
|
|
|
|
return fs_base
|
|
|
|
|
2015-10-12 11:02:52 +03:00
|
|
|
def pthread_self():
|
|
|
|
'''Fetch pthread_self() from the glibc start_thread function.'''
|
|
|
|
f = gdb.newest_frame()
|
|
|
|
while f.name() != 'start_thread':
|
|
|
|
f = f.older()
|
|
|
|
if f is None:
|
|
|
|
return get_fs_base()
|
|
|
|
|
|
|
|
try:
|
|
|
|
return f.read_var("arg")
|
|
|
|
except ValueError:
|
|
|
|
return get_fs_base()
|
|
|
|
|
2015-08-14 20:46:30 +03:00
|
|
|
def get_glibc_pointer_guard():
|
|
|
|
'''Fetch glibc pointer guard value'''
|
2015-10-12 11:02:52 +03:00
|
|
|
fs_base = pthread_self()
|
2015-08-14 20:46:30 +03:00
|
|
|
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))
|
|
|
|
|
2015-10-12 11:02:53 +03:00
|
|
|
def get_jmpbuf_regs(jmpbuf):
|
2015-08-14 20:46:30 +03:00
|
|
|
JB_RBX = 0
|
|
|
|
JB_RBP = 1
|
|
|
|
JB_R12 = 2
|
|
|
|
JB_R13 = 3
|
|
|
|
JB_R14 = 4
|
|
|
|
JB_R15 = 5
|
|
|
|
JB_RSP = 6
|
|
|
|
JB_PC = 7
|
|
|
|
|
|
|
|
pointer_guard = get_glibc_pointer_guard()
|
2015-10-12 11:02:53 +03:00
|
|
|
return {'rbx': jmpbuf[JB_RBX],
|
|
|
|
'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard),
|
|
|
|
'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard),
|
|
|
|
'r12': jmpbuf[JB_R12],
|
|
|
|
'r13': jmpbuf[JB_R13],
|
|
|
|
'r14': jmpbuf[JB_R14],
|
|
|
|
'r15': jmpbuf[JB_R15],
|
|
|
|
'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) }
|
|
|
|
|
|
|
|
def bt_jmpbuf(jmpbuf):
|
|
|
|
'''Backtrace a jmpbuf'''
|
|
|
|
regs = get_jmpbuf_regs(jmpbuf)
|
|
|
|
old = dict()
|
|
|
|
|
2020-12-17 18:54:35 +03:00
|
|
|
# remember current stack frame and select the topmost
|
|
|
|
# so that register modifications don't wreck it
|
|
|
|
selected_frame = gdb.selected_frame()
|
|
|
|
gdb.newest_frame().select()
|
|
|
|
|
2015-10-12 11:02:53 +03:00
|
|
|
for i in regs:
|
|
|
|
old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
|
|
|
|
|
|
|
|
for i in regs:
|
|
|
|
gdb.execute('set $%s = %s' % (i, regs[i]))
|
2015-08-14 20:46:30 +03:00
|
|
|
|
|
|
|
gdb.execute('bt')
|
|
|
|
|
2015-10-12 11:02:53 +03:00
|
|
|
for i in regs:
|
|
|
|
gdb.execute('set $%s = %s' % (i, old[i]))
|
|
|
|
|
2020-12-17 18:54:35 +03:00
|
|
|
selected_frame.select()
|
|
|
|
|
2020-12-17 18:54:36 +03:00
|
|
|
def co_cast(co):
|
|
|
|
return co.cast(gdb.lookup_type('CoroutineUContext').pointer())
|
|
|
|
|
2015-10-12 11:02:53 +03:00
|
|
|
def coroutine_to_jmpbuf(co):
|
2020-12-17 18:54:36 +03:00
|
|
|
coroutine_pointer = co_cast(co)
|
2015-10-12 11:02:53 +03:00
|
|
|
return coroutine_pointer['env']['__jmpbuf']
|
2015-08-14 20:46:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-10-12 11:02:53 +03:00
|
|
|
bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
|
2015-10-12 11:02:54 +03:00
|
|
|
|
2020-12-17 18:54:36 +03:00
|
|
|
class CoroutineBt(gdb.Command):
|
|
|
|
'''Display backtrace including coroutine switches'''
|
|
|
|
def __init__(self):
|
|
|
|
gdb.Command.__init__(self, 'qemu bt', gdb.COMMAND_STACK,
|
|
|
|
gdb.COMPLETE_NONE)
|
|
|
|
|
|
|
|
def invoke(self, arg, from_tty):
|
|
|
|
|
|
|
|
gdb.execute("bt")
|
|
|
|
|
|
|
|
if gdb.parse_and_eval("qemu_in_coroutine()") == False:
|
|
|
|
return
|
|
|
|
|
|
|
|
co_ptr = gdb.parse_and_eval("qemu_coroutine_self()")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
co = co_cast(co_ptr)
|
|
|
|
co_ptr = co["base"]["caller"]
|
|
|
|
if co_ptr == 0:
|
|
|
|
break
|
|
|
|
gdb.write("Coroutine at " + str(co_ptr) + ":\n")
|
|
|
|
bt_jmpbuf(coroutine_to_jmpbuf(co_ptr))
|
|
|
|
|
2015-10-12 11:02:54 +03:00
|
|
|
class CoroutineSPFunction(gdb.Function):
|
|
|
|
def __init__(self):
|
|
|
|
gdb.Function.__init__(self, 'qemu_coroutine_sp')
|
|
|
|
|
|
|
|
def invoke(self, addr):
|
|
|
|
return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
|
|
|
|
|
|
|
|
class CoroutinePCFunction(gdb.Function):
|
|
|
|
def __init__(self):
|
|
|
|
gdb.Function.__init__(self, 'qemu_coroutine_pc')
|
|
|
|
|
|
|
|
def invoke(self, addr):
|
|
|
|
return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)
|