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