blob: 1614877ab5ea00817780143bc5ab0bec8c536dd5 [file] [log] [blame]
Kostya Serebryanyb4150322013-11-15 11:51:08 +00001#!/usr/bin/env python
2# Merge or print the coverage data collected by asan's coverage.
3# Input files are sequences of 4-byte integers.
4# We need to merge these integers into a set and then
5# either print them (as hex) or dump them into another file.
6import array
Sergey Matveev6cb47a082014-05-19 12:53:03 +00007import struct
Kostya Serebryanyb4150322013-11-15 11:51:08 +00008import sys
Evgeniy Stepanov567e5162014-05-27 12:37:52 +00009import bisect
10import os.path
Kostya Serebryanyb4150322013-11-15 11:51:08 +000011
12prog_name = "";
13
14def Usage():
15 print >> sys.stderr, "Usage: \n" + \
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000016 " " + prog_name + " [32|64] merge file1 [file2 ...] > output\n" \
17 " " + prog_name + " [32|64] print file1 [file2 ...]\n" \
18 " " + prog_name + " [32|64] unpack file1 [file2 ...]\n" \
19 " " + prog_name + " [32|64] rawunpack file1 [file2 ...]\n"
Kostya Serebryanyb4150322013-11-15 11:51:08 +000020 exit(1)
21
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000022def TypeCodeForBits(bits):
23 if bits == 64:
24 return 'L'
25 else:
26 return 'I'
27
28def ReadOneFile(path, bits):
Sergey Matveev6cb47a082014-05-19 12:53:03 +000029 with open(path, mode="rb") as f:
30 f.seek(0, 2)
31 size = f.tell()
32 f.seek(0, 0)
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000033 s = set(array.array(TypeCodeForBits(bits), f.read(size)))
34 print >>sys.stderr, "%s: read %d PCs from %s" % (prog_name, size * 8 / bits, path)
Kostya Serebryanyb4150322013-11-15 11:51:08 +000035 return s
36
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000037def Merge(files, bits):
Kostya Serebryanyb4150322013-11-15 11:51:08 +000038 s = set()
39 for f in files:
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000040 s = s.union(ReadOneFile(f, bits))
Kostya Serebryanyb4150322013-11-15 11:51:08 +000041 print >> sys.stderr, "%s: %d files merged; %d PCs total" % \
42 (prog_name, len(files), len(s))
43 return sorted(s)
44
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000045def PrintFiles(files, bits):
46 s = Merge(files, bits)
Kostya Serebryanyb4150322013-11-15 11:51:08 +000047 for i in s:
48 print "0x%x" % i
49
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000050def MergeAndPrint(files, bits):
Kostya Serebryanyb4150322013-11-15 11:51:08 +000051 if sys.stdout.isatty():
52 Usage()
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000053 s = Merge(files, bits)
54 a = array.array(TypeCodeForBits(bits), s)
Kostya Serebryanyb4150322013-11-15 11:51:08 +000055 a.tofile(sys.stdout)
56
Sergey Matveev6cb47a082014-05-19 12:53:03 +000057
58def UnpackOneFile(path):
59 with open(path, mode="rb") as f:
60 print >> sys.stderr, "%s: unpacking %s" % (prog_name, path)
61 while True:
62 header = f.read(12)
63 if not header: return
64 if len(header) < 12:
65 break
66 pid, module_length, blob_size = struct.unpack('iII', header)
67 module = f.read(module_length)
68 blob = f.read(blob_size)
69 assert(len(module) == module_length)
70 assert(len(blob) == blob_size)
71 extracted_file = "%s.%d.sancov" % (module, pid)
72 print >> sys.stderr, "%s: extracting %s" % \
73 (prog_name, extracted_file)
74 # The packed file may contain multiple blobs for the same pid/module
75 # pair. Append to the end of the file instead of overwriting.
76 with open(extracted_file, 'ab') as f2:
77 f2.write(blob)
78 # fail
79 raise Exception('Error reading file %s' % path)
80
81
82def Unpack(files):
83 for f in files:
84 UnpackOneFile(f)
85
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000086def UnpackOneRawFile(path, map_path, bits):
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000087 mem_map = []
88 with open(map_path, mode="rt") as f_map:
89 print >> sys.stderr, "%s: reading map %s" % (prog_name, map_path)
Kostya Serebryany9f1243e2015-03-17 22:09:19 +000090 if bits != int(f_map.readline()):
91 raise Exception('Wrong bits size in the map')
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000092 for line in f_map:
93 parts = line.rstrip().split()
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000094 mem_map.append((int(parts[0], 16),
95 int(parts[1], 16),
96 int(parts[2], 16),
Evgeniy Stepanov937afa12014-06-03 15:25:43 +000097 ' '.join(parts[3:])))
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000098 mem_map.sort(key=lambda m : m[0])
99 mem_map_keys = [m[0] for m in mem_map]
100
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000101 with open(path, mode="rb") as f:
102 print >> sys.stderr, "%s: unpacking %s" % (prog_name, path)
103
104 f.seek(0, 2)
105 size = f.tell()
106 f.seek(0, 0)
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000107 pcs = array.array(TypeCodeForBits(bits), f.read(size))
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000108 mem_map_pcs = [[] for i in range(0, len(mem_map))]
109
110 for pc in pcs:
111 if pc == 0: continue
112 map_idx = bisect.bisect(mem_map_keys, pc) - 1
113 (start, end, base, module_path) = mem_map[map_idx]
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000114 assert pc >= start
115 if pc >= end:
116 print >> sys.stderr, "warning: %s: pc %x outside of any known mapping" % (prog_name, pc)
117 continue
118 mem_map_pcs[map_idx].append(pc - base)
119
120 for ((start, end, base, module_path), pc_list) in zip(mem_map, mem_map_pcs):
121 if len(pc_list) == 0: continue
122 assert path.endswith('.sancov.raw')
123 dst_path = module_path + '.' + os.path.basename(path)[:-4]
Evgeniy Stepanovb7238342014-12-25 16:03:24 +0000124 print >> sys.stderr, "%s: writing %d PCs to %s" % (prog_name, len(pc_list), dst_path)
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000125 arr = array.array(TypeCodeForBits(bits))
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000126 arr.fromlist(sorted(pc_list))
127 with open(dst_path, 'ab') as f2:
128 arr.tofile(f2)
129
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000130def RawUnpack(files, bits):
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000131 for f in files:
132 if not f.endswith('.sancov.raw'):
133 raise Exception('Unexpected raw file name %s' % f)
134 f_map = f[:-3] + 'map'
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000135 UnpackOneRawFile(f, f_map, bits)
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000136
Kostya Serebryanyb4150322013-11-15 11:51:08 +0000137if __name__ == '__main__':
138 prog_name = sys.argv[0]
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000139 if len(sys.argv) <= 3:
Kostya Serebryanyb4150322013-11-15 11:51:08 +0000140 Usage();
Kostya Serebryany9f1243e2015-03-17 22:09:19 +0000141
142 if sys.argv[1] == "32":
143 bits = 32
144 elif sys.argv[1] == "64":
145 bits = 64
146 else:
147 Usage();
148
149 if sys.argv[2] == "print":
150 PrintFiles(sys.argv[3:], bits)
151 elif sys.argv[2] == "merge":
152 MergeAndPrint(sys.argv[3:], bits)
153 elif sys.argv[2] == "unpack":
154 Unpack(sys.argv[3:])
155 elif sys.argv[2] == "rawunpack":
156 RawUnpack(sys.argv[3:], bits)
Kostya Serebryanyb4150322013-11-15 11:51:08 +0000157 else:
158 Usage()