2 # GDB debugging support
4 # Copyright 2012 Red Hat, Inc. and/or its affiliates
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.
14 VOID_PTR
= gdb
.lookup_type('void').pointer()
17 '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is
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)
27 '''Fetch pthread_self() from the glibc start_thread function.'''
28 f
= gdb
.newest_frame()
29 while f
.name() != 'start_thread':
35 return f
.read_var("arg")
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
):
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
)
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()
79 old
[i
] = gdb
.parse_and_eval('(uint64_t)$%s' % i
)
82 gdb
.execute('set $%s = %s' % (i
, regs
[i
]))
87 gdb
.execute('set $%s = %s' % (i
, old
[i
]))
89 selected_frame
.select()
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'''
102 gdb
.Command
.__init
__(self
, 'qemu coroutine', gdb
.COMMAND_DATA
,
105 def invoke(self
, arg
, from_tty
):
106 argv
= gdb
.string_to_argv(arg
)
108 gdb
.write('usage: qemu coroutine <coroutine-pointer>\n')
111 bt_jmpbuf(coroutine_to_jmpbuf(gdb
.parse_and_eval(argv
[0])))
113 class CoroutineBt(gdb
.Command
):
114 '''Display backtrace including coroutine switches'''
116 gdb
.Command
.__init
__(self
, 'qemu bt', gdb
.COMMAND_STACK
,
119 def invoke(self
, arg
, from_tty
):
123 if gdb
.parse_and_eval("qemu_in_coroutine()") == False:
126 co_ptr
= gdb
.parse_and_eval("qemu_coroutine_self()")
130 co_ptr
= co
["base"]["caller"]
133 gdb
.write("Coroutine at " + str(co_ptr
) + ":\n")
134 bt_jmpbuf(coroutine_to_jmpbuf(co_ptr
))
136 class CoroutineSPFunction(gdb
.Function
):
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
):
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
)