qemu with hax to log dma reads & writes
jcs.org/2018/11/12/vfio
1#!/usr/bin/python
2
3# GDB debugging support
4#
5# Copyright 2012 Red Hat, Inc. and/or its affiliates
6#
7# Authors:
8# Avi Kivity <avi@redhat.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2. See
11# the COPYING file in the top-level directory.
12#
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.
15
16import gdb
17
18VOID_PTR = gdb.lookup_type('void').pointer()
19
20def 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
29
30def 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()
37
38 try:
39 return f.read_var("arg")
40 except ValueError:
41 return get_fs_base()
42
43def 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)
47
48def 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))
51
52def 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
61
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) }
71
72def bt_jmpbuf(jmpbuf):
73 '''Backtrace a jmpbuf'''
74 regs = get_jmpbuf_regs(jmpbuf)
75 old = dict()
76
77 for i in regs:
78 old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)
79
80 for i in regs:
81 gdb.execute('set $%s = %s' % (i, regs[i]))
82
83 gdb.execute('bt')
84
85 for i in regs:
86 gdb.execute('set $%s = %s' % (i, old[i]))
87
88def coroutine_to_jmpbuf(co):
89 coroutine_pointer = co.cast(gdb.lookup_type('CoroutineUContext').pointer())
90 return coroutine_pointer['env']['__jmpbuf']
91
92
93class 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)
98
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
104
105 bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0])))
106
107class CoroutineSPFunction(gdb.Function):
108 def __init__(self):
109 gdb.Function.__init__(self, 'qemu_coroutine_sp')
110
111 def invoke(self, addr):
112 return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR)
113
114class CoroutinePCFunction(gdb.Function):
115 def __init__(self):
116 gdb.Function.__init__(self, 'qemu_coroutine_pc')
117
118 def invoke(self, addr):
119 return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR)