-
Notifications
You must be signed in to change notification settings - Fork 3
/
etrace.py
236 lines (207 loc) · 5.19 KB
/
etrace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# QEMU etrace python class
#
# Copyright (C) Xilinx Inc.
# Written by Edgar E. Iglesias
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import sys
from ctypes import *
class etrace_hdr(Structure):
_pack_ = 1
_fields_ = [
("type", c_uint16),
("unit_id", c_uint16),
("len", c_uint32)
]
class etrace_arch(Structure):
_pack_ = 1
_fields_ = [
("arch_id", c_uint32),
("arch_bits", c_uint8),
("big_endian", c_uint8),
]
class etrace_arch_gh(Structure):
_pack_ = 1
_fields_ = [
("guest", etrace_arch),
("host", etrace_arch)
]
class etrace_exec_entry32(Structure):
_pack_ = 1
_fields_ = [
("duration", c_uint32),
("start", c_uint32),
("end", c_uint32),
]
class etrace_exec_p(Structure):
_pack_ = 1
_fields_ = [
("nr", c_uint64),
("ex32", POINTER(etrace_exec_entry32)),
]
class etrace_exec(Structure):
_pack_ = 1
_fields_ = [
("start_time", c_uint64),
]
class etrace_mem(Structure):
_pack_ = 1
_fields_ = [
("time", c_uint64),
("vaddr", c_uint64),
("paddr", c_uint64),
("value", c_uint64),
("attr", c_uint32),
("size", c_uint8),
("padd", c_uint8 * 3),
]
class etrace_event_u64(Structure):
_pack_ = 1
_fields_ = [
("flags", c_uint32),
("unit_id", c_uint16),
("reserved", c_uint16),
("time", c_uint64),
("val", c_uint64),
("prev_val", c_uint64),
("dev_name_len", c_uint16),
("event_name_len", c_uint16),
]
class etrace_all_subtypes(Union):
_pack_ = 1
_fields_ = [
("p8", POINTER(c_uint8)),
("dev_name", c_char_p),
("event_name", c_char_p),
("ex", etrace_exec_p),
("arch", etrace_arch_gh),
("mem", etrace_mem),
("event_u64", etrace_event_u64),
("texec", etrace_exec)
]
class etrace_pkg(Structure):
_pack_ = 1
_fields_ = [
("hdr", etrace_hdr),
("all", etrace_all_subtypes)
]
class etrace(object):
TYPE_NONE = 0
TYPE_EXEC = 1
TYPE_TB = 2
TYPE_NOTE = 3
TYPE_MEM = 4
TYPE_ARCH = 5
TYPE_BARRIER = 6
TYPE_EVENT_U64_OLD = 7
TYPE_EVENT_U64 = 8
TYPE_INFO = 0x4554
MEM_READ = (0 << 0)
MEM_WRITE = (1 << 0)
ETRACE_EVU64_F_PREV_VAL = (1 << 0)
R_POS_CACHESIZE = 1024
def __init__(self, f):
self.debugf = None
self.f = f
self.reset()
def debug(self, str):
if self.debugf == None:
self.debugf = open(".debug.etrace", 'w+')
self.debugf.write(str)
self.debugf.write("\n")
def type_to_name(self, type):
if type == self.TYPE_INFO:
return "info"
typenames = ["none", "exec", "tb", "note", "mem",
"arch", "barrier", "event_u64"]
return typenames[type]
def push_pos(self):
pos = self.f.tell()
if len(self.r_pos_cache) == 0 or pos > self.r_pos_cache[0]:
if self.r_idx > self.r_max_idx:
self.r_max_idx = self.r_idx
self.r_pos_cache.insert(0, pos)
if len(self.r_pos_cache) > self.R_POS_CACHESIZE:
self.r_pos_cache.pop()
def reset(self):
self.r_idx = 0
self.r_max_idx = 0
self.r_pos_cache = []
self.f.seek(0, 0)
def stepb(self):
if self.r_idx <= 1:
return None
self.r_idx -= 1
cache_idx = self.r_max_idx - self.r_idx + 1
if cache_idx >= 0 and cache_idx < len(self.r_pos_cache):
pos = self.r_pos_cache[cache_idx]
else:
self.r_max_idx = 0
self.r_pos_cache = []
self.f.seek(0, 0)
i = 0
while i < (self.r_idx - 2):
self.push_pos()
i += 1
r = self.decode_record()
if r == None:
break
self.r_idx = i
return r
self.f.seek(pos, 0)
return self.decode_record()
def stepf(self):
self.push_pos()
self.r_idx += 1
return self.decode_record()
def decode_record(self):
pkg = etrace_pkg()
self.f.readinto(pkg.hdr)
pos = self.f.tell()
if pkg.hdr.type == self.TYPE_NONE:
return None
end_pos = pos + pkg.hdr.len
if pkg.hdr.type == self.TYPE_ARCH:
self.f.readinto(pkg.all.arch)
print sizeof(pkg.all.arch)
self.arch = pkg.all.arch
if self.arch.guest.arch_bits == 32:
self.etype = etrace_exec_entry32
else:
assert False, "Unsupported arch bit %d" \
% self.arch.arch_bits
self.f.seek(end_pos, 0)
elif pkg.hdr.type == self.TYPE_MEM:
self.f.readinto(pkg.all.mem)
elif pkg.hdr.type == self.TYPE_EVENT_U64:
self.f.readinto(pkg.all.event_u64)
dev_name = self.f.read(pkg.all.event_u64.dev_name_len - 1)
dummy = self.f.read(1)
ev_name = self.f.read(pkg.all.event_u64.event_name_len - 1)
dummy = self.f.read(1)
pkg.all.event_name = ev_name
pkg.all.dev_name = dev_name
elif pkg.hdr.type == self.TYPE_EXEC:
len = (pkg.hdr.len - sizeof(pkg.all.texec))
len /= sizeof(self.etype)
a = (self.etype * len)()
self.f.readinto(pkg.all.texec)
self.f.readinto(a)
pkg.all.ex.nr = len
if self.arch.guest.arch_bits == 32:
pkg.all.ex.ex32 = cast(a, POINTER(self.etype))
else:
self.f.seek(end_pos, 0)
return pkg