-
Notifications
You must be signed in to change notification settings - Fork 3
/
etrace-view.py
executable file
·442 lines (377 loc) · 9.9 KB
/
etrace-view.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env python
#
# 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
import os
import getopt
import time
import etrace
import curses
import addr2line
def usage():
print "--help Show help"
print "--trace filename of trace"
class traceview(object):
def __init__(self, screen, e, elf_name, comp_dir, cfg):
self.cfg = cfg
self.fcache = {}
self.e = e
self.comp_dir = comp_dir
self.screen = screen
self.dw = None
self.dw_loc = None
self.record = None
self.record_idx = 0
self.record_pos = 0
self.symname = ""
self.file = ""
self.line = -1
self.search_sym = ""
self.debugf = None
self.addrloc = {}
self.log = []
self.log_lines =15
if elf_name != None:
self.a2l = addr2line.addr2line(elf_name, comp_dir)
self.screen.clear()
self.screen.addstr(0, 0, "Processing ELF file.")
self.screen.refresh()
self.screen.addstr(1, 0, "Done. Step into first src line")
self.screen.refresh()
def debug(self, str):
if self.debugf == None:
self.debugf = open(".debug", 'w+')
self.debugf.write(str)
self.debugf.write("\n")
def update_file_cache(self, fname):
try:
lines = self.fcache[fname]["lines"]
return lines
except:
pass
# populate the entry
full_fname = fname
if self.comp_dir:
full_fname = "%s/%s" % (self.comp_dir, fname)
try:
f = open(full_fname, 'r')
except:
return None
if len(self.fcache) > 256:
# flush it all
self.fcache = {}
# self.debug("update cahce %s" % fname)
lines = f.readlines()
self.fcache[fname] = { "lines" : lines }
f.close()
# self.debug("close file and got %d lines" % len(lines))
return lines
def show_file_contents(self, filename, line_nr):
lines = self.update_file_cache(filename)
if lines == None:
return
(h, w) = self.screen.getmaxyx()
if (h > 60):
h -= self.log_lines + 1
h -= 4
start = line_nr - h / 2
if start < 0:
start = 0
for i in range(4, h - 1):
try:
str = "%4d:%s" % (start + i, lines[start + i])
except:
break
if (start + i) == line_nr:
self.screen.addstr(i, 0, str, curses.A_REVERSE)
else:
self.screen.addstr(i, 0, str)
def show_log(self):
(h, w) = self.screen.getmaxyx()
if (h <= 60):
return
if len(self.log) == 0:
return
pos = 0
if len(self.log) > self.log_lines:
pos = len(self.log) - self.log_lines - 1
for i in range(0, self.log_lines):
if i >= len(self.log):
break
self.screen.addstr(h - self.log_lines + i - 1, 0, self.log[pos + i])
def step_end_of_subrecord(self, r):
self.record_pos = r.all.ex.ex32[self.record_idx].end - 4
if self.record_pos < r.all.ex.ex32[self.record_idx].start:
self.record_pos = r.all.ex.ex32[self.record_idx].start
def step_end_of_record(self, r):
self.record_idx = r.all.ex.nr - 1
self.step_end_of_subrecord(r)
def step_start_of_record(self, r):
self.record_idx = 0
if r and r.hdr.type == self.e.TYPE_EXEC:
self.record_pos = r.all.ex.ex32[self.record_idx].start
def step_existing_record_back(self, r):
if r == None or r.hdr.type != self.e.TYPE_EXEC:
r = self.e.stepb()
if r and r.hdr.type == self.e.TYPE_EXEC:
self.step_end_of_record(r)
return r
# Now we know we've got an exec record.
# Step back.
if self.record_pos <= r.all.ex.ex32[self.record_idx].start:
if self.record_idx == 0:
r = self.step_existing_record_back(None)
return r
self.record_idx -= 1
self.step_end_of_subrecord(r)
return r
self.record_pos -= 4
return r
def step_record(self, count):
if self.record and count < 0:
r = self.step_existing_record_back(self.record)
elif self.record and count > 0:
r = self.record
if r.hdr.type == self.e.TYPE_EXEC:
self.record_pos += 4
if self.record_pos >= r.all.ex.ex32[self.record_idx].end:
self.record_idx += 1
self.record_pos = r.all.ex.ex32[self.record_idx].start
if self.record_idx == r.all.ex.nr:
self.record = None
r = self.e.stepf()
self.step_start_of_record(r)
else:
r = self.e.stepf()
self.step_start_of_record(r)
else:
if count > 0:
r = self.e.stepf()
self.step_start_of_record(r)
else:
r = self.step_existing_record_back(None)
return r
def map_address_to_loc(self, address):
if 'map_address' in self.cfg.keys():
address = self.cfg['map_address'](address)
loc = self.a2l.map(address)
return loc
def step_trace_record(self, count):
goon = True
r = self.step_record(count)
self.record = r
if r == None:
return r
if r.hdr.type == self.e.TYPE_EXEC:
goon = True
self.file = ""
self.line = -1
# self.debug("lookup %x" % self.record_pos)
try:
loc = self.addrloc[self.record_pos]
except:
loc = self.map_address_to_loc(self.record_pos)
self.addrloc[self.record_pos] = loc
# self.debug(str(loc))
self.symname = loc[0]
self.file = loc[1][0]
if self.file == "??":
self.file = ""
self.line = -1
return r
self.line = int(loc[1][1]) - 1
# self.debug("file=%s" % self.file)
# self.debug("line=%s" % self.line)
elif r.hdr.type == self.e.TYPE_MEM:
str = "read"
if r.all.mem.attr & self.e.MEM_WRITE:
str = "write"
self.log.append("mem %s %lx = %lx" %
(str, r.all.mem.paddr, r.all.mem.value))
if len(self.log) > 100:
self.log.pop(0)
elif r.hdr.type == self.e.TYPE_EVENT_U64:
val = r.all.event_u64.val
prev = r.all.event_u64.prev_val
if count < 0:
val = prev
prev = r.all.event_u64.val
self.log.append("event %s %s %lx prev=%s" %
(r.all.dev_name,
r.all.event_name,
r.all.event_u64.val, r.all.event_u64.prev_val))
return r
def step_new_exec(self, count = 1):
r = None
goon = True
while goon:
pfile = self.file
pline = self.line
r = self.step_trace_record(count)
if not r:
return None
if r.hdr.type == self.e.TYPE_EXEC \
or r.hdr.type == self.e.TYPE_EVENT_U64 \
or r.hdr.type == self.e.TYPE_MEM:
goon = False
return r
def step_new_line(self, count = 1):
r = None
goon = True
while goon:
pfile = self.file
pline = self.line
r = self.step_trace_record(count)
if not r:
return None
if r.hdr.type != self.e.TYPE_EXEC:
return r
if pfile != self.file:
goon = False
if pline != self.line:
goon = False
return r
def step_new_sym(self, newsymname = None, count = 1):
r = None
while True:
if newsymname and newsymname == self.symname:
break
pname = self.symname
r = self.step_new_line(count)
if not r:
return None
if not newsymname and pname != self.symname:
break
return r
def search_for_sym(self):
(h, w) = self.screen.getmaxyx()
curses.echo()
self.screen.addstr(h - 1,0, "/")
str = self.screen.getstr(h - 1, 1)
str = str.rstrip()
curses.noecho()
return str
def loop(self):
c = curses.KEY_DOWN
r = self.step_new_exec(count = 1)
while True:
if c == curses.KEY_DOWN:
r = self.step_new_line()
elif c == curses.KEY_RIGHT:
r = self.step_new_sym(count = 1)
elif c == curses.KEY_LEFT:
r = self.step_new_sym(count = -1)
if c == curses.KEY_UP:
r = self.step_new_line(-1)
elif c == ord('n'):
if self.symname == self.search_sym:
r = self.step_new_sym()
r = self.step_new_sym(self.search_sym)
elif c == ord('/'):
old_r = self.record;
self.search_sym = self.search_for_sym()
r = self.step_new_sym(self.search_sym)
if (r == None):
r = old_r
elif c == ord('g'):
self.e.reset()
r = self.step_new_exec(count = 1)
elif c == ord('G'):
r = True
while r:
r = self.step_trace_record(count = 1)
r = self.step_new_exec(count = -1)
elif c == ord('q'):
return
if r:
self.screen.clear()
if r.hdr.type == self.e.TYPE_EXEC:
self.screen.addstr(0, 0,
"%d: type=%s %x len=%d PC=%x (%x-%x)" \
% (self.e.r_idx, self.e.type_to_name(r.hdr.type),
r.hdr.type, r.hdr.len, self.record_pos,
r.all.ex.ex32[self.record_idx].start,
r.all.ex.ex32[self.record_idx].end),
curses.A_REVERSE)
self.screen.addstr(1, 0,
"file=%s:%s %s" \
% (self.file, self.line, self.symname),
curses.A_REVERSE)
self.screen.addstr(3, 0,
"search=%s" \
% (self.search_sym),
curses.A_REVERSE)
try:
self.show_file_contents(self.file, self.line)
except:
pass
self.show_log()
self.screen.refresh()
else:
self.screen.addstr(3, 0,
"None", curses.A_REVERSE);
self.screen.refresh()
self.prev_file = self.file
self.prev_line = self.line
r = None
c = self.screen.getch()
def main(screen):
args_comp_dir = None
args_trace = None
args_elf = None
try:
opts, args = getopt.getopt(sys.argv[1:],
"h",
["help",
"comp-dir=",
"config=",
"trace=",
"elf=",
]
)
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(1)
cfg = {}
for o, a in opts:
if o == "--trace":
args_trace = a
elif o == "--comp-dir":
args_comp_dir = a
elif o == "--config":
execfile(a, cfg)
elif o == "--elf":
args_elf = a
elif o in ("-h", "--help"):
usage()
sys.exit(0)
else:
assert False, "Unhandled option " + o
if args_trace == None:
print "Missing trace file"
sys.exit(1)
screen.clear()
f = open(args_trace, 'rb')
e = etrace.etrace(f)
tv = traceview(screen, e, args_elf, args_comp_dir, cfg)
tv.loop()
try:
curses.wrapper(main)
except KeyboardInterrupt:
sys.exit(0)