xref: /openbmc/qemu/scripts/qemugdb/coroutine.py (revision f4e343b6559eda19efe972b9dcd52e479320e388)
1#
2# GDB debugging support
3#
4# Copyright 2012 Red Hat, Inc. and/or its affiliates
5#
6# Authors:
7#  Avi Kivity <avi@redhat.com>
8#
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.
11
12import gdb
13
14VOID_PTR = gdb.lookup_type('void').pointer()
15
16def pthread_self():
17    '''Fetch the base address of TLS.'''
18    return gdb.parse_and_eval("$fs_base")
19
20def get_glibc_pointer_guard():
21    '''Fetch glibc pointer guard value'''
22    fs_base = pthread_self()
23    return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
24
25def glibc_ptr_demangle(val, pointer_guard):
26    '''Undo effect of glibc's PTR_MANGLE()'''
27    return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
28
29def get_jmpbuf_regs(jmpbuf):
30    JB_RBX  = 0
31    JB_RBP  = 1
32    JB_R12  = 2
33    JB_R13  = 3
34    JB_R14  = 4
35    JB_R15  = 5
36    JB_RSP  = 6
37    JB_PC   = 7
38
39    pointer_guard = get_glibc_pointer_guard()
40    return {'rbx': jmpbuf[JB_RBX],
41        'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard),
42        'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard),
43        'r12': jmpbuf[JB_R12],
44        'r13': jmpbuf[JB_R13],
45        'r14': jmpbuf[JB_R14],
46        'r15': jmpbuf[JB_R15],
47        'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) }
48
49def bt_jmpbuf(jmpbuf):
50    '''Backtrace a jmpbuf'''
51    regs = get_jmpbuf_regs(jmpbuf)
52    old = dict()
53
54    # remember current stack frame and select the topmost
55    # so that register modifications don't wreck it
56    selected_frame = gdb.selected_frame()
57    gdb.newest_frame().select()
58
59    for i in regs:
60        old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
61
62    for i in regs:
63        gdb.execute('set $%s = %s' % (i, regs[i]))
64
65    gdb.execute('bt')
66
67    for i in regs:
68        gdb.execute('set $%s = %s' % (i, old[i]))
69
70    selected_frame.select()
71
72def co_cast(co):
73    return co.cast(gdb.lookup_type('CoroutineUContext').pointer())
74
75def coroutine_to_jmpbuf(co):
76    coroutine_pointer = co_cast(co)
77    return coroutine_pointer['env']['__jmpbuf']
78
79
80class CoroutineCommand(gdb.Command):
81    '''Display coroutine backtrace'''
82    def __init__(self):
83        gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
84                             gdb.COMPLETE_NONE)
85
86    def invoke(self, arg, from_tty):
87        argv = gdb.string_to_argv(arg)
88        if len(argv) != 1:
89            gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
90            return
91
92        bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
93
94class CoroutineBt(gdb.Command):
95    '''Display backtrace including coroutine switches'''
96    def __init__(self):
97        gdb.Command.__init__(self, 'qemu bt', gdb.COMMAND_STACK,
98                             gdb.COMPLETE_NONE)
99
100    def invoke(self, arg, from_tty):
101
102        gdb.execute("bt")
103
104        if gdb.parse_and_eval("qemu_in_coroutine()") == False:
105            return
106
107        co_ptr = gdb.parse_and_eval("qemu_coroutine_self()")
108
109        while True:
110            co = co_cast(co_ptr)
111            co_ptr = co["base"]["caller"]
112            if co_ptr == 0:
113                break
114            gdb.write("Coroutine at " + str(co_ptr) + ":\n")
115            bt_jmpbuf(coroutine_to_jmpbuf(co_ptr))
116
117class CoroutineSPFunction(gdb.Function):
118    def __init__(self):
119        gdb.Function.__init__(self, 'qemu_coroutine_sp')
120
121    def invoke(self, addr):
122        return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
123
124class CoroutinePCFunction(gdb.Function):
125    def __init__(self):
126        gdb.Function.__init__(self, 'qemu_coroutine_pc')
127
128    def invoke(self, addr):
129        return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)
130