3 # GDB debugging support
5 # Copyright 2012 Red Hat, Inc. and/or its affiliates
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.
18 VOID_PTR
= gdb
.lookup_type('void').pointer()
21 '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is
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)
31 '''Fetch pthread_self() from the glibc start_thread function.'''
32 f
= gdb
.newest_frame()
33 while f
.name() != 'start_thread':
39 return f
.read_var("arg")
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
):
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
)
78 old
[i
] = gdb
.parse_and_eval('(uint64_t)$%s' % i
)
81 gdb
.execute('set $%s = %s' % (i
, regs
[i
]))
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'''
96 gdb
.Command
.__init
__(self
, 'qemu coroutine', gdb
.COMMAND_DATA
,
99 def invoke(self
, arg
, from_tty
):
100 argv
= gdb
.string_to_argv(arg
)
102 gdb
.write('usage: qemu coroutine <coroutine-pointer>\n')
105 bt_jmpbuf(coroutine_to_jmpbuf(gdb
.parse_and_eval(argv
[0])))
107 class CoroutineSPFunction(gdb
.Function
):
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
):
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
)