coroutine: support SafeStack in ucontext backend
[qemu/ar7.git] / scripts / qemugdb / coroutine.py
blobdb6138902262a1ec3ede99e358988e6632d43889
2 # GDB debugging support
4 # Copyright 2012 Red Hat, Inc. and/or its affiliates
6 # Authors:
7 # Avi Kivity <avi@redhat.com>
9 # This work is licensed under the terms of the GNU GPL, version 2
10 # or later. See the COPYING file in the top-level directory.
12 import gdb
14 VOID_PTR = gdb.lookup_type('void').pointer()
16 def get_fs_base():
17 '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is
18 pthread_self().'''
19 # %rsp - 120 is scratch space according to the SystemV ABI
20 old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
21 gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True)
22 fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
23 gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True)
24 return fs_base
26 def pthread_self():
27 '''Fetch pthread_self() from the glibc start_thread function.'''
28 f = gdb.newest_frame()
29 while f.name() != 'start_thread':
30 f = f.older()
31 if f is None:
32 return get_fs_base()
34 try:
35 return f.read_var("arg")
36 except ValueError:
37 return get_fs_base()
39 def get_glibc_pointer_guard():
40 '''Fetch glibc pointer guard value'''
41 fs_base = pthread_self()
42 return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
44 def glibc_ptr_demangle(val, pointer_guard):
45 '''Undo effect of glibc's PTR_MANGLE()'''
46 return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
48 def get_jmpbuf_regs(jmpbuf):
49 JB_RBX = 0
50 JB_RBP = 1
51 JB_R12 = 2
52 JB_R13 = 3
53 JB_R14 = 4
54 JB_R15 = 5
55 JB_RSP = 6
56 JB_PC = 7
58 pointer_guard = get_glibc_pointer_guard()
59 return {'rbx': jmpbuf[JB_RBX],
60 'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard),
61 'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard),
62 'r12': jmpbuf[JB_R12],
63 'r13': jmpbuf[JB_R13],
64 'r14': jmpbuf[JB_R14],
65 'r15': jmpbuf[JB_R15],
66 'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) }
68 def bt_jmpbuf(jmpbuf):
69 '''Backtrace a jmpbuf'''
70 regs = get_jmpbuf_regs(jmpbuf)
71 old = dict()
73 for i in regs:
74 old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
76 for i in regs:
77 gdb.execute('set $%s = %s' % (i, regs[i]))
79 gdb.execute('bt')
81 for i in regs:
82 gdb.execute('set $%s = %s' % (i, old[i]))
84 def coroutine_to_jmpbuf(co):
85 coroutine_pointer = co.cast(gdb.lookup_type('CoroutineUContext').pointer())
86 return coroutine_pointer['env']['__jmpbuf']
89 class CoroutineCommand(gdb.Command):
90 '''Display coroutine backtrace'''
91 def __init__(self):
92 gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
93 gdb.COMPLETE_NONE)
95 def invoke(self, arg, from_tty):
96 argv = gdb.string_to_argv(arg)
97 if len(argv) != 1:
98 gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
99 return
101 bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
103 class CoroutineSPFunction(gdb.Function):
104 def __init__(self):
105 gdb.Function.__init__(self, 'qemu_coroutine_sp')
107 def invoke(self, addr):
108 return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
110 class CoroutinePCFunction(gdb.Function):
111 def __init__(self):
112 gdb.Function.__init__(self, 'qemu_coroutine_pc')
114 def invoke(self, addr):
115 return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)