target/mips: Add declarations for generic TCG helpers
[qemu/ar7.git] / scripts / qemugdb / coroutine.py
blob7db46d4b6841acf7e09e8d77f6114fdd1532085d
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 # remember current stack frame and select the topmost
74 # so that register modifications don't wreck it
75 selected_frame = gdb.selected_frame()
76 gdb.newest_frame().select()
78 for i in regs:
79 old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
81 for i in regs:
82 gdb.execute('set $%s = %s' % (i, regs[i]))
84 gdb.execute('bt')
86 for i in regs:
87 gdb.execute('set $%s = %s' % (i, old[i]))
89 selected_frame.select()
91 def co_cast(co):
92 return co.cast(gdb.lookup_type('CoroutineUContext').pointer())
94 def coroutine_to_jmpbuf(co):
95 coroutine_pointer = co_cast(co)
96 return coroutine_pointer['env']['__jmpbuf']
99 class CoroutineCommand(gdb.Command):
100 '''Display coroutine backtrace'''
101 def __init__(self):
102 gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
103 gdb.COMPLETE_NONE)
105 def invoke(self, arg, from_tty):
106 argv = gdb.string_to_argv(arg)
107 if len(argv) != 1:
108 gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
109 return
111 bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
113 class CoroutineBt(gdb.Command):
114 '''Display backtrace including coroutine switches'''
115 def __init__(self):
116 gdb.Command.__init__(self, 'qemu bt', gdb.COMMAND_STACK,
117 gdb.COMPLETE_NONE)
119 def invoke(self, arg, from_tty):
121 gdb.execute("bt")
123 if gdb.parse_and_eval("qemu_in_coroutine()") == False:
124 return
126 co_ptr = gdb.parse_and_eval("qemu_coroutine_self()")
128 while True:
129 co = co_cast(co_ptr)
130 co_ptr = co["base"]["caller"]
131 if co_ptr == 0:
132 break
133 gdb.write("Coroutine at " + str(co_ptr) + ":\n")
134 bt_jmpbuf(coroutine_to_jmpbuf(co_ptr))
136 class CoroutineSPFunction(gdb.Function):
137 def __init__(self):
138 gdb.Function.__init__(self, 'qemu_coroutine_sp')
140 def invoke(self, addr):
141 return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
143 class CoroutinePCFunction(gdb.Function):
144 def __init__(self):
145 gdb.Function.__init__(self, 'qemu_coroutine_pc')
147 def invoke(self, addr):
148 return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)