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). This is 20 pthread_self().''' 21 # %rsp - 120 is scratch space according to the SystemV ABI 22 old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') 23 gdb.execute('call arch_prctl(0x1003, $rsp - 120)', False, True) 24 fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') 25 gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True) 26 return fs_base 27 28def pthread_self(): 29 '''Fetch pthread_self() from the glibc start_thread function.''' 30 f = gdb.newest_frame() 31 while f.name() != 'start_thread': 32 f = f.older() 33 if f is None: 34 return get_fs_base() 35 36 try: 37 return f.read_var("arg") 38 except ValueError: 39 return get_fs_base() 40 41def get_glibc_pointer_guard(): 42 '''Fetch glibc pointer guard value''' 43 fs_base = pthread_self() 44 return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base) 45 46def glibc_ptr_demangle(val, pointer_guard): 47 '''Undo effect of glibc's PTR_MANGLE()''' 48 return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard)) 49 50def bt_jmpbuf(jmpbuf): 51 '''Backtrace a jmpbuf''' 52 JB_RBX = 0 53 JB_RBP = 1 54 JB_R12 = 2 55 JB_R13 = 3 56 JB_R14 = 4 57 JB_R15 = 5 58 JB_RSP = 6 59 JB_PC = 7 60 61 old_rbx = gdb.parse_and_eval('(uint64_t)$rbx') 62 old_rbp = gdb.parse_and_eval('(uint64_t)$rbp') 63 old_rsp = gdb.parse_and_eval('(uint64_t)$rsp') 64 old_r12 = gdb.parse_and_eval('(uint64_t)$r12') 65 old_r13 = gdb.parse_and_eval('(uint64_t)$r13') 66 old_r14 = gdb.parse_and_eval('(uint64_t)$r14') 67 old_r15 = gdb.parse_and_eval('(uint64_t)$r15') 68 old_rip = gdb.parse_and_eval('(uint64_t)$rip') 69 70 pointer_guard = get_glibc_pointer_guard() 71 gdb.execute('set $rbx = %s' % jmpbuf[JB_RBX]) 72 gdb.execute('set $rbp = %s' % glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard)) 73 gdb.execute('set $rsp = %s' % glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard)) 74 gdb.execute('set $r12 = %s' % jmpbuf[JB_R12]) 75 gdb.execute('set $r13 = %s' % jmpbuf[JB_R13]) 76 gdb.execute('set $r14 = %s' % jmpbuf[JB_R14]) 77 gdb.execute('set $r15 = %s' % jmpbuf[JB_R15]) 78 gdb.execute('set $rip = %s' % glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard)) 79 80 gdb.execute('bt') 81 82 gdb.execute('set $rbx = %s' % old_rbx) 83 gdb.execute('set $rbp = %s' % old_rbp) 84 gdb.execute('set $rsp = %s' % old_rsp) 85 gdb.execute('set $r12 = %s' % old_r12) 86 gdb.execute('set $r13 = %s' % old_r13) 87 gdb.execute('set $r14 = %s' % old_r14) 88 gdb.execute('set $r15 = %s' % old_r15) 89 gdb.execute('set $rip = %s' % old_rip) 90 91 92class CoroutineCommand(gdb.Command): 93 '''Display coroutine backtrace''' 94 def __init__(self): 95 gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA, 96 gdb.COMPLETE_NONE) 97 98 def invoke(self, arg, from_tty): 99 argv = gdb.string_to_argv(arg) 100 if len(argv) != 1: 101 gdb.write('usage: qemu coroutine <coroutine-pointer>\n') 102 return 103 104 coroutine_pointer = gdb.parse_and_eval(argv[0]).cast(gdb.lookup_type('CoroutineUContext').pointer()) 105 bt_jmpbuf(coroutine_pointer['env']['__jmpbuf']) 106