This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
coverage_parse.py
executable file
·530 lines (313 loc) · 11.6 KB
/
coverage_parse.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
'''
=========================================================================
Code coverage analysis tool:
Log parsing program.
Usage:
coverage_parse.py <log_file_path> [options]
... where:
<log_file_path> - Path to the log file, that has been generated by
Coverager.dll (PIN toolkit instrumentation module).
Walid options are:
--outfile <output_file_path> - Write output into the text file,
instead console.
--dump-blocks - Parse basic blocks information.
--dump-routines - Parse functions calls information.
--order-by-names - Sort output by symbol name (default mode).
--order-by-calls - Sort output by number of function/block calls.
--modules <module_name> - Print information only for the specified modules.
--skip-symbols - Don't use PDB loading and parsing for executable modules.
You must specify --dump-blocks or --dump-routines option, but not a both.
Example:
coverage_parse.py Coverager.log --dump-routines --modules "ieframe,iexplore" --outfile routines.txt
Developed by:
Oleksiuk Dmitry, eSage Lab
mailto:dmitry@esagelab.com
http://www.esagelab.com/
=========================================================================
'''
import sys, os, time
ver = sys.version[:3]
# load python specified version of symlib module
if ver == "2.5":
from symlib25 import *
elif ver == "2.6":
from symlib import *
else:
print "[!] Only Python 2.5 and 2.6 are supported by symlib module"
# if end
APP_NAME = '''
Code Coverage Analysis Tool for PIN
by Oleksiuk Dmitry, eSage Lab (dmitry@esagelab.com)
'''
def sortproc_names(a, b):
a = a['name'].lower()
b = b['name'].lower()
if a > b:
return 1
if a < b:
return -1
return 0
# def end
def sortproc_calls(a, b):
a = a['calls']
b = b['calls']
if a > b:
return -1
if a < b:
return 1
return 0
# def end
m_modules_list = {}
m_logfile = None
m_sortproc = sortproc_names
m_modules_to_process = []
m_skip_symbols = False
def log_write(text):
global m_logfile
if m_logfile:
m_logfile.write(text + "\r\n")
else:
print text
# def end
def read_modules_list(file_name):
global m_modules_list
# open input file
f = open(file_name)
content = f.readline()
# read file contents line by line
while content != "":
content = content.replace("\n", "")
entry = content.split(":")
if content[:1] != "#" and len(entry) >= 3:
module_path = ":".join(entry[2:])
module_name = os.path.basename(module_path).lower()
m_modules_list[module_name] = { 'path': module_path, 'processed_items': 0 }
# if end
# read the next line
content = f.readline()
# while end
f.close()
# def end
def parse_symbol(string):
global m_modules_list, m_modules_to_process, m_skip_symbols
# parse 'name+offset' string
info = string.split("+")
if len(info) >= 2:
info[1] = int(info[1], 16)
module_path = info[0].lower()
if m_modules_list.has_key(module_path):
m_modules_list[module_path]['processed_items'] += 1
# if end
skip_module = False
if len(m_modules_to_process) > 0:
skip_module = True
for module_flt in m_modules_to_process:
if module_path.find(module_flt) >= 0:
# don't skip this module
skip_module = False
# if end
# for end
# if end
if skip_module:
return False
if m_skip_symbols:
return string
if m_modules_list.has_key(module_path):
module_path = m_modules_list[module_path]['path']
# if end
# lookup debug symbol for address
symbol = bestbyaddr(module_path, info[1])
if symbol != None:
addr_s = "%s!%s" % (info[0], symbol[0])
if symbol[1] > 0:
addr_s += "+0x%x" % symbol[1]
return addr_s
# if end
elif string[0] == "?" and len(m_modules_to_process) > 0:
if "?" not in m_modules_to_process:
return False
# if end
return string
# def end
def print_routines(file_name):
global m_sortproc
# open input file
f = open(file_name)
content = f.readline()
print "[+] Parsing routines list, please wait...\n"
info_list = []
i = 0
# read file contents line by line
while content != "":
sys.stdout.write(["-", "\\", "|", "/"][i])
sys.stdout.write("\r")
i = (i + 1) & 3
content = content.replace("\n", "")
entry = content.split(":")
if content[:1] != "#" and len(entry) >= 3:
rtn_addr = int(entry[0], 16) # routinr virtual address
rtn_calls = int(entry[2])
# parse symbol name
rtn_name = parse_symbol(entry[1])
if rtn_name != False:
info_list.append({'addr': rtn_addr, 'name': rtn_name, 'calls': rtn_calls })
# if end
# read the next line
content = f.readline()
# while end
# sort entries list
info_list.sort(m_sortproc)
log_write("#")
log_write("# %13s -- %s" % ("Calls count", "Function Name"))
log_write("#")
for entry in info_list:
# print single log file entry information
log_write("%15d -- %s" % (entry['calls'], entry['name']))
# for end
f.close()
# def end
def print_blocks(file_name):
global m_sortproc
# open input file
f = open(file_name)
content = f.readline()
print "[+] Parsing basic blocks list, please wait...\n"
info_list = []
instructions = 0
i = 0
# read file contents line by line
while content != "":
sys.stdout.write(["-", "\\", "|", "/"][i])
sys.stdout.write("\r")
i = (i + 1) & 3
content = content.replace("\n", "")
entry = content.split(":")
if content[:1] != "#" and len(entry) >= 4:
# parse log entry
bb_addr = int(entry[0], 16) # block virtual address
bb_size = int(entry[1], 16) # block size
bb_calls = int(entry[4]) # calls count
bb_insts = int(entry[2]) # instructions count
# parse symbol name
bb_name = parse_symbol(entry[3])
if bb_name != False:
info_list.append({'addr': bb_addr, 'name': bb_name, 'calls': bb_calls, 'size': bb_size })
instructions += bb_insts * bb_calls
# if end
# read the next line
content = f.readline()
# while end
# sort entries list
info_list.sort(m_sortproc)
log_write("#")
log_write("# %13s -- Block Size -- %s" % ("Calls count", "Function Name"))
log_write("#")
for entry in info_list:
# print single log file entry information
log_write("%15d -- 0x%.8x -- %s" % (entry['calls'], entry['size'], entry['name']))
# for end
log_write("#")
log_write("# %d total instructions executed in given basic blocks" % instructions)
log_write("#")
f.close()
# def end
if __name__ == "__main__":
print APP_NAME
if len(sys.argv) < 2:
print "USAGE: coverage_parse.py <LogFilePath> [options]"
sys.exit()
# if end
dump_blocks = False
dump_routines = False
logfile = None
fname = sys.argv[1]
fname_blocks = fname + ".blocks"
fname_routines = fname + ".routines"
fname_modules = fname + ".modules"
# parse command line arguments
if len(sys.argv) > 2:
for i in range(2, len(sys.argv)):
if sys.argv[i] == "--outfile" and i < len(sys.argv) - 1:
# save information into the logfile
logfile = sys.argv[i + 1]
elif sys.argv[i] == "--modules" and i < len(sys.argv) - 1:
# filter by module name is specified
modlist = sys.argv[i + 1].split(",")
for mod in modlist:
mod = mod.lstrip()
m_modules_to_process.append(mod.lower())
print "Filtering by module name \"%s\"" % (mod)
# for end
elif sys.argv[i] == "--dump-blocks":
# parse basic blocks log file
dump_blocks = True
elif sys.argv[i] == "--dump-routines":
# parse routines log file
dump_routines = True
elif sys.argv[i] == "--order-by-names":
print "[+] Ordering list by symbol name"
m_sortproc = sortproc_names
elif sys.argv[i] == "--order-by-calls":
print "[+] Ordering list by number of calls"
m_sortproc = sortproc_calls
elif sys.argv[i] == "--skip-symbols":
m_skip_symbols = True
# if end
# for end
# if end
if (not dump_blocks) and (not dump_routines):
print "[!] You must specify '--dump-blocks' or '--dump-routines' option"
sys.exit()
# if end
if dump_blocks and dump_routines:
print "[!] You must specify only '--dump-blocks' or '--dump-routines' option (not both)"
sys.exit()
# if end
if not os.path.isfile(fname):
print "[!] Error while opening input file"
sys.exit(-1)
# if end
if not os.path.isfile(fname_modules):
print "[!] Error while opening modules log"
sys.exit(-1)
# if end
if logfile:
# create output file
m_logfile = open(logfile, "wb+")
print "[+] Output file: \"%s\"" % (logfile)
# if end
# read target application modules list
read_modules_list(fname_modules)
if dump_blocks:
if not os.path.isfile(fname_blocks):
print "[!] Error while opening basic blocks log"
sys.exit(-1)
# if end
print_blocks(fname_blocks)
# if end
if dump_routines:
if not os.path.isfile(fname_routines):
print "[!] Error while opening routines log"
sys.exit(-1)
# if end
print_routines(fname_routines)
# if end
if logfile:
m_logfile.close()
# print processed modules information
print "\n[+] Processed modules list:\n"
print "#"
if dump_routines:
print "# %13s -- %s" % ("Routines count", "Module Name")
elif dump_blocks:
print "# %13s -- %s" % ("Basic blocks count", "Module Name")
print "#"
for module_name in m_modules_list:
count = m_modules_list[module_name]['processed_items']
print "%15d -- %s" % (count, module_name)
# for end
print "\n[+] DONE\n"
# if end
#
# EoF
#