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