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.
15 print >> sys.stderr, "Usage: \n" + \
16 " " + prog_name + " merge file1 [file2 ...] > output\n" \
17 " " + prog_name + " print file1 [file2 ...]\n" \
18 " " + prog_name + " unpack file1 [file2 ...]\n" \
19 " " + prog_name + " rawunpack file1 [file2 ...]\n"
22 def ReadOneFile(path):
23 with open(path, mode="rb") as f:
27 s = set(array.array('I', f.read(size)))
28 print >>sys.stderr, "%s: read %d PCs from %s" % (prog_name, size / 4, path)
34 s = s.union(ReadOneFile(f))
35 print >> sys.stderr, "%s: %d files merged; %d PCs total" % \
36 (prog_name, len(files), len(s))
39 def PrintFiles(files):
44 def MergeAndPrint(files):
45 if sys.stdout.isatty():
48 a = array.array('I', s)
52 def UnpackOneFile(path):
53 with open(path, mode="rb") as f:
54 print >> sys.stderr, "%s: unpacking %s" % (prog_name, path)
60 pid, module_length, blob_size = struct.unpack('iII', header)
61 module = f.read(module_length)
62 blob = f.read(blob_size)
63 assert(len(module) == module_length)
64 assert(len(blob) == blob_size)
65 extracted_file = "%s.%d.sancov" % (module, pid)
66 print >> sys.stderr, "%s: extracting %s" % \
67 (prog_name, extracted_file)
68 # The packed file may contain multiple blobs for the same pid/module
69 # pair. Append to the end of the file instead of overwriting.
70 with open(extracted_file, 'ab') as f2:
73 raise Exception('Error reading file %s' % path)
80 def UnpackOneRawFile(path, map_path):
82 with open(map_path, mode="rt") as f_map:
83 print >> sys.stderr, "%s: reading map %s" % (prog_name, map_path)
84 bits = int(f_map.readline())
86 parts = line.rstrip().split()
87 mem_map.append((int(parts[0], 16),
91 mem_map.sort(key=lambda m : m[0])
92 mem_map_keys = [m[0] for m in mem_map]
94 with open(path, mode="rb") as f:
95 print >> sys.stderr, "%s: unpacking %s" % (prog_name, path)
104 pcs = array.array(typecode, f.read(size))
105 mem_map_pcs = [[] for i in range(0, len(mem_map))]
109 map_idx = bisect.bisect(mem_map_keys, pc) - 1
110 (start, end, base, module_path) = mem_map[map_idx]
113 print >> sys.stderr, "warning: %s: pc %x outside of any known mapping" % (prog_name, pc)
115 mem_map_pcs[map_idx].append(pc - base)
117 for ((start, end, base, module_path), pc_list) in zip(mem_map, mem_map_pcs):
118 if len(pc_list) == 0: continue
119 assert path.endswith('.sancov.raw')
120 dst_path = module_path + '.' + os.path.basename(path)[:-4]
121 print "writing %d PCs to %s" % (len(pc_list), dst_path)
122 arr = array.array('I')
123 arr.fromlist(sorted(pc_list))
124 with open(dst_path, 'ab') as f2:
127 def RawUnpack(files):
129 if not f.endswith('.sancov.raw'):
130 raise Exception('Unexpected raw file name %s' % f)
131 f_map = f[:-3] + 'map'
132 UnpackOneRawFile(f, f_map)
134 if __name__ == '__main__':
135 prog_name = sys.argv[0]
136 if len(sys.argv) <= 2:
138 if sys.argv[1] == "print":
139 PrintFiles(sys.argv[2:])
140 elif sys.argv[1] == "merge":
141 MergeAndPrint(sys.argv[2:])
142 elif sys.argv[1] == "unpack":
144 elif sys.argv[1] == "rawunpack":
145 RawUnpack(sys.argv[2:])