forked from Cr4sh/Code-coverage-analysis-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coverage_parse.py
executable file
·367 lines (258 loc) · 10.8 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
'''
=========================================================================
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/
---------------------------------------------------------------------
0xcpu, 2018
=========================================================================
'''
import sys, os, time
ver = sys.version[:3]
# load python specified version of symlib module
if ver == "3.6":
from symlib import *
else:
print("[!] Only Python 3.6 are supported by symlib module")
sys.exit(1)
APP_NAME = '''
Code Coverage Analysis Tool for PIN
by Oleksiuk Dmitry, eSage Lab (dmitry@esagelab.com)
Python3.6 version by 0xcpu
'''
def sortproc_names(v):
return v["name"].lower()
def sortproc_calls(v):
return v["calls"]
m_modules_list = {}
m_logfile = None
m_sortproc = sortproc_names
m_skip_symbols = False
m_modules_to_process = []
def log_write(text):
global m_logfile
if m_logfile:
m_logfile.write(bytes("".join([text, "\r\n"]).encode("utf-8")))
else:
print(text)
def read_modules_list(file_name):
global m_modules_list
# open input file
with open(file_name) as f:
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 }
# read the next line
content = f.readline()
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 module_path in m_modules_list:
m_modules_list[module_path]["processed_items"] += 1
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 skip_module:
return False
if m_skip_symbols:
return string
if module_path in m_modules_list:
module_path = m_modules_list[module_path]["path"]
# lookup debug symbol for address
symbol = bestbyaddr(module_path, info[1])
if symbol != None:
addr_s = "{0:s}!{1:s}".format(info[0], symbol[0])
if symbol[1] > 0:
addr_s += "+0x{0:x}".format(symbol[1])
return addr_s
elif string[0] == '?' and len(m_modules_to_process) > 0:
if '?' not in m_modules_to_process:
return False
return string
def print_routines(file_name):
global m_sortproc
# open input file
with open(file_name) as f:
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) # 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 })
# read the next line
content = f.readline()
# sort entries list
info_list.sort(key=m_sortproc)
log_write("#")
log_write("# {0:13s} -- {1:s}".format("Calls count", "Function Name"))
log_write("#")
for entry in info_list:
# print single log file entry information
log_write("{0:15d} -- {1:s}".format(entry["calls"], entry["name"]))
def print_blocks(file_name):
global m_sortproc
# open input file
with open(file_name) as f:
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
# read the next line
content = f.readline()
# sort entries list
info_list.sort(key=m_sortproc)
log_write("#")
log_write("# {0:13s} -- Block Size -- {1:s}".format("Calls count", "Function Name"))
log_write("#")
for entry in info_list:
# print single log file entry information
log_write("{0:15d} -- 0x{1:08x} -- {2:s}".format(entry["calls"], entry["size"], entry["name"]))
log_write("#")
log_write("# {0:d} total instructions executed in given basic blocks".format(instructions))
log_write("#")
if __name__ == "__main__":
print(APP_NAME)
if len(sys.argv) < 2:
print("USAGE: coverage_parse.py <LogFilePath> [options]")
sys.exit()
logfile = None
dump_blocks = False
dump_routines = False
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 \"{0:s}\"".format(mod))
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 (not dump_blocks) and (not dump_routines):
print("[!] You must specify '--dump-blocks' or '--dump-routines' option")
sys.exit(1)
if dump_blocks and dump_routines:
print("[!] You must specify only '--dump-blocks' or '--dump-routines' option (not both)")
sys.exit(1)
if not os.path.isfile(fname):
print("[!] Error while opening input file")
sys.exit(-1)
if not os.path.isfile(fname_modules):
print("[!] Error while opening modules log")
sys.exit(-1)
if logfile:
# create output file
m_logfile = open(logfile, "wb+")
print("[+] Output file: \"{0:s}\"".format(logfile))
# 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)
print_blocks(fname_blocks)
if dump_routines:
if not os.path.isfile(fname_routines):
print("[!] Error while opening routines log")
sys.exit(-1)
print_routines(fname_routines)
if logfile:
m_logfile.close()
# print processed modules information
print("\n[+] Processed modules list:\n")
print("#")
if dump_routines:
print("# {0:13s} -- {1:s}".format("Routines count", "Module Name"))
elif dump_blocks:
print("# {0:13s} -- {1:s}".format("Basic blocks count", "Module Name"))
print("#")
for module_name in m_modules_list:
count = m_modules_list[module_name]["processed_items"]
print("{0:15d} -- {1:s}".format(count, module_name))
print("\n[+] DONE\n")