From nobody Sun Nov 9 23:42:57 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552308084908798.0536464685807; Mon, 11 Mar 2019 05:41:24 -0700 (PDT) Received: from localhost ([127.0.0.1]:32931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3KF3-0006i0-OQ for importer@patchew.org; Mon, 11 Mar 2019 08:41:17 -0400 Received: from eggs.gnu.org ([209.51.188.92]:60311) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3K9S-0002Ls-4c for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:35:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3K9Q-0006Et-JI for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:35:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49418) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h3K9B-000611-Np; Mon, 11 Mar 2019 08:35:14 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 016FA3082DDD; Mon, 11 Mar 2019 12:35:13 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-64.ams2.redhat.com [10.36.112.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id C71B119731; Mon, 11 Mar 2019 12:35:11 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 11 Mar 2019 13:35:06 +0100 Message-Id: <20190311123507.24867-3-pbonzini@redhat.com> In-Reply-To: <20190311123507.24867-1-pbonzini@redhat.com> References: <20190311123507.24867-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Mon, 11 Mar 2019 12:35:13 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/3] qemugdb: allow adding support for other coroutine backends X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Split the jmpbuf access to a separate module and dispatch based on which CoroutineXYZ type is defined. Signed-off-by: Paolo Bonzini --- scripts/qemugdb/coroutine.py | 106 ++++++++------------------ scripts/qemugdb/coroutine_ucontext.py | 69 +++++++++++++++++ 2 files changed, 100 insertions(+), 75 deletions(-) create mode 100644 scripts/qemugdb/coroutine_ucontext.py diff --git a/scripts/qemugdb/coroutine.py b/scripts/qemugdb/coroutine.py index 41e079d0e2..db2753d949 100644 --- a/scripts/qemugdb/coroutine.py +++ b/scripts/qemugdb/coroutine.py @@ -1,6 +1,6 @@ #!/usr/bin/python =20 -# GDB debugging support +# GDB debugging support, coroutine dispatch # # Copyright 2012 Red Hat, Inc. and/or its affiliates # @@ -10,82 +10,25 @@ # This work is licensed under the terms of the GNU GPL, version 2 # or later. See the COPYING file in the top-level directory. =20 +from . import coroutine_ucontext import gdb =20 VOID_PTR =3D gdb.lookup_type('void').pointer() +UINTPTR_T =3D gdb.lookup_type('uintptr_t') =20 -def get_fs_base(): - '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is - pthread_self().''' - # %rsp - 120 is scratch space according to the SystemV ABI - old =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True) - fs_base =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('set *(uint64_t*)($rsp - 120) =3D %s' % old, False, True) - return fs_base - -def pthread_self(): - '''Fetch pthread_self() from the glibc start_thread function.''' - f =3D gdb.newest_frame() - while f.name() !=3D 'start_thread': - f =3D f.older() - if f is None: - return get_fs_base() - - try: - return f.read_var("arg") - except ValueError: - return get_fs_base() - -def get_glibc_pointer_guard(): - '''Fetch glibc pointer guard value''' - fs_base =3D pthread_self() - return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_bas= e) - -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 get_jmpbuf_regs(jmpbuf): - JB_RBX =3D 0 - JB_RBP =3D 1 - JB_R12 =3D 2 - JB_R13 =3D 3 - JB_R14 =3D 4 - JB_R15 =3D 5 - JB_RSP =3D 6 - JB_PC =3D 7 - - pointer_guard =3D get_glibc_pointer_guard() - 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 =3D get_jmpbuf_regs(jmpbuf) - old =3D dict() - - for i in regs: - old[i] =3D gdb.parse_and_eval('(uint64_t)$%s' % i) - - for i in regs: - gdb.execute('set $%s =3D %s' % (i, regs[i])) - - gdb.execute('bt') - - for i in regs: - gdb.execute('set $%s =3D %s' % (i, old[i])) - -def coroutine_to_jmpbuf(co): - coroutine_pointer =3D co.cast(gdb.lookup_type('CoroutineUContext').poi= nter()) - return coroutine_pointer['env']['__jmpbuf'] +backends =3D { + 'CoroutineUContext': coroutine_ucontext +} =20 +def coroutine_backend(): + for k, v in backends.items(): + try: + gdb.lookup_type(k) + except: + continue + return v + + raise Exception('could not find coroutine backend') =20 class CoroutineCommand(gdb.Command): '''Display coroutine backtrace''' @@ -99,18 +42,31 @@ class CoroutineCommand(gdb.Command): gdb.write('usage: qemu coroutine \n') return =20 - bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0]))) + addr =3D gdb.parse_and_eval(argv[0]) + regs =3D coroutine_backend().get_coroutine_regs(addr) + old =3D dict() + + for i in regs: + old[i] =3D gdb.parse_and_eval('(uint64_t)$%s' % i) + + for i in regs: + gdb.execute('set $%s =3D %s' % (i, regs[i].cast(UINTPTR_T))) + + gdb.execute('bt') + + for i in regs: + gdb.execute('set $%s =3D %s' % (i, old[i].cast(UINTPTR_T))) =20 class CoroutineSPFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_sp') =20 def invoke(self, addr): - return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID= _PTR) + return coroutine_backend().get_coroutine_regs(addr)['rsp'].cast(VO= ID_PTR) =20 class CoroutinePCFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_pc') =20 def invoke(self, addr): - return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID= _PTR) + return coroutine_backend().get_coroutine_regs(addr)['rip'].cast(VO= ID_PTR) diff --git a/scripts/qemugdb/coroutine_ucontext.py b/scripts/qemugdb/corout= ine_ucontext.py new file mode 100644 index 0000000000..a2f8c1dbbf --- /dev/null +++ b/scripts/qemugdb/coroutine_ucontext.py @@ -0,0 +1,69 @@ +#!/usr/bin/python + +# GDB debugging support +# +# Copyright 2012 Red Hat, Inc. and/or its affiliates +# +# Authors: +# Avi Kivity +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +import gdb + +def get_fs_base(): + '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is + pthread_self().''' + # %rsp - 120 is scratch space according to the SystemV ABI + old =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') + gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True) + fs_base =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') + gdb.execute('set *(uint64_t*)($rsp - 120) =3D %s' % old, False, True) + return fs_base + +def pthread_self(): + '''Fetch pthread_self() from the glibc start_thread function.''' + f =3D gdb.newest_frame() + while f.name() !=3D 'start_thread': + f =3D f.older() + if f is None: + return get_fs_base() + + try: + return f.read_var("arg") + except ValueError: + return get_fs_base() + +def get_glibc_pointer_guard(): + '''Fetch glibc pointer guard value''' + fs_base =3D pthread_self() + return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_bas= e) + +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 get_jmpbuf_regs(jmpbuf): + JB_RBX =3D 0 + JB_RBP =3D 1 + JB_R12 =3D 2 + JB_R13 =3D 3 + JB_R14 =3D 4 + JB_R15 =3D 5 + JB_RSP =3D 6 + JB_PC =3D 7 + + pointer_guard =3D get_glibc_pointer_guard() + 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 get_coroutine_regs(addr): + co =3D addr.cast(gdb.lookup_type('CoroutineUContext').pointer()) + return get_jmpbuf_regs(co['env']['__jmpbuf']) --=20 2.20.1