| Kostya Serebryany | 106cb08 | 2013-11-15 11:51:08 +0000 | [diff] [blame^] | 1 | #!/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. |
| 6 | import array |
| 7 | import sys |
| 8 | |
| 9 | prog_name = ""; |
| 10 | |
| 11 | def Usage(): |
| 12 | print >> sys.stderr, "Usage: \n" + \ |
| 13 | " " + prog_name + " merge file1 [file2 ...] > output\n" \ |
| 14 | " " + prog_name + " print file1 [file2 ...]\n" |
| 15 | exit(1) |
| 16 | |
| 17 | def ReadOneFile(path): |
| 18 | f = open(path, mode="rb") |
| 19 | f.seek(0, 2) |
| 20 | size = f.tell() |
| 21 | f.seek(0, 0) |
| 22 | s = set(array.array('I', f.read(size))) |
| 23 | f.close() |
| 24 | print >>sys.stderr, "%s: read %d PCs from %s" % (prog_name, size / 4, path) |
| 25 | return s |
| 26 | |
| 27 | def Merge(files): |
| 28 | s = set() |
| 29 | for f in files: |
| 30 | s = s.union(ReadOneFile(f)) |
| 31 | print >> sys.stderr, "%s: %d files merged; %d PCs total" % \ |
| 32 | (prog_name, len(files), len(s)) |
| 33 | return sorted(s) |
| 34 | |
| 35 | def PrintFiles(files): |
| 36 | s = Merge(files) |
| 37 | for i in s: |
| 38 | print "0x%x" % i |
| 39 | |
| 40 | def MergeAndPrint(files): |
| 41 | if sys.stdout.isatty(): |
| 42 | Usage() |
| 43 | s = Merge(files) |
| 44 | a = array.array('I', s) |
| 45 | a.tofile(sys.stdout) |
| 46 | |
| 47 | if __name__ == '__main__': |
| 48 | prog_name = sys.argv[0] |
| 49 | if len(sys.argv) <= 2: |
| 50 | Usage(); |
| 51 | if sys.argv[1] == "print": |
| 52 | PrintFiles(sys.argv[2:]) |
| 53 | elif sys.argv[1] == "merge": |
| 54 | MergeAndPrint(sys.argv[2:]) |
| 55 | else: |
| 56 | Usage() |