summaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/scripts
diff options
context:
space:
mode:
authorKostya Serebryany <kcc@google.com>2015-03-18 22:03:39 +0000
committerKostya Serebryany <kcc@google.com>2015-03-18 22:03:39 +0000
commit103a7f0784d8040537c016bb1e9d9101319cc736 (patch)
treebe150f95bd37615e8339bd3b09771a203b51d2fa /lib/sanitizer_common/scripts
parente9db3b421db09ac3541070a92d4c17134b1447d3 (diff)
[sanitizer] change the sanitizer coverage format once again, this time adding a magic to the beginning of the file
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@232679 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/sanitizer_common/scripts')
-rwxr-xr-xlib/sanitizer_common/scripts/sancov.py87
1 files changed, 53 insertions, 34 deletions
diff --git a/lib/sanitizer_common/scripts/sancov.py b/lib/sanitizer_common/scripts/sancov.py
index 443521633..a09dabbe1 100755
--- a/lib/sanitizer_common/scripts/sancov.py
+++ b/lib/sanitizer_common/scripts/sancov.py
@@ -9,7 +9,7 @@ import sys
import bisect
import os.path
-prog_name = "";
+prog_name = ""
def Usage():
print >> sys.stderr, "Usage: \n" + \
@@ -19,41 +19,65 @@ def Usage():
" " + prog_name + " [32|64] rawunpack file1 [file2 ...]\n"
exit(1)
+def CheckBits(bits):
+ if bits != 32 and bits != 64:
+ raise Exception("Wrond bitness: %d" % bits)
+
def TypeCodeForBits(bits):
- if bits == 64:
- return 'L'
- else:
- return 'I'
+ CheckBits(bits)
+ return 'L' if bits == 64 else 'I'
+
+kMagic64 = 0xC0BFFFFFFFFFFF64
+kMagic32 = 0xC0BFFFFFFFFFFF32
-def ReadOneFile(path, bits):
+def MagicForBits(bits):
+ CheckBits(bits)
+ return kMagic64 if bits == 64 else kMagic32
+
+def ReadOneFile(path):
with open(path, mode="rb") as f:
f.seek(0, 2)
size = f.tell()
f.seek(0, 0)
+ if size <= 8:
+ raise Exception('File %s is short (> 8 bytes)' % path)
+ magic_word = struct.unpack('L', f.read(8))[0];
+ if magic_word == kMagic64:
+ bits = 64
+ elif magic_word == kMagic32:
+ bits = 32
+ else:
+ raise Exception('Bad magic word in %s' % path)
+ size -= 8
s = array.array(TypeCodeForBits(bits), f.read(size))
- print >>sys.stderr, "%s: read %d PCs from %s" % (prog_name, size * 8 / bits, path)
+ print >>sys.stderr, "%s: read %d %d-bit PCs from %s" % (prog_name, size * 8 / bits, bits, path)
return s
-def Merge(files, bits):
+def Merge(files):
s = set()
for f in files:
- s = s.union(set(ReadOneFile(f, bits)))
+ s = s.union(set(ReadOneFile(f)))
print >> sys.stderr, "%s: %d files merged; %d PCs total" % \
(prog_name, len(files), len(s))
return sorted(s)
-def PrintFiles(files, bits):
+def PrintFiles(files):
if len(files) > 1:
- s = Merge(files, bits)
+ s = Merge(files)
else: # If there is just on file, print the PCs in order.
- s = ReadOneFile(files[0], bits)
+ s = ReadOneFile(files[0])
for i in s:
print "0x%x" % i
-def MergeAndPrint(files, bits):
+def MergeAndPrint(files):
if sys.stdout.isatty():
Usage()
- s = Merge(files, bits)
+ s = Merge(files)
+ bits = 32
+ magic = kMagic32
+ if max(s) > 0xFFFFFFFF:
+ bits = 64
+ magic = kMagic64
a = array.array(TypeCodeForBits(bits), s)
a.tofile(sys.stdout)
@@ -86,11 +110,12 @@ def Unpack(files):
for f in files:
UnpackOneFile(f)
-def UnpackOneRawFile(path, map_path, bits):
+def UnpackOneRawFile(path, map_path):
mem_map = []
with open(map_path, mode="rt") as f_map:
print >> sys.stderr, "%s: reading map %s" % (prog_name, map_path)
- if bits != int(f_map.readline()):
+ bits = int(f_map.readline())
+ if bits != 32 and bits != 64:
raise Exception('Wrong bits size in the map')
for line in f_map:
parts = line.rstrip().split()
@@ -128,34 +153,28 @@ def UnpackOneRawFile(path, map_path, bits):
arr = array.array(TypeCodeForBits(bits))
arr.fromlist(sorted(pc_list))
with open(dst_path, 'ab') as f2:
+ array.array('L', [MagicForBits(bits)]).tofile(f2)
arr.tofile(f2)
-def RawUnpack(files, bits):
+def RawUnpack(files):
for f in files:
if not f.endswith('.sancov.raw'):
raise Exception('Unexpected raw file name %s' % f)
f_map = f[:-3] + 'map'
- UnpackOneRawFile(f, f_map, bits)
+ UnpackOneRawFile(f, f_map)
if __name__ == '__main__':
prog_name = sys.argv[0]
- if len(sys.argv) <= 3:
- Usage();
-
- if sys.argv[1] == "32":
- bits = 32
- elif sys.argv[1] == "64":
- bits = 64
- else:
+ if len(sys.argv) <= 2:
Usage();
- if sys.argv[2] == "print":
- PrintFiles(sys.argv[3:], bits)
- elif sys.argv[2] == "merge":
- MergeAndPrint(sys.argv[3:], bits)
- elif sys.argv[2] == "unpack":
- Unpack(sys.argv[3:])
- elif sys.argv[2] == "rawunpack":
- RawUnpack(sys.argv[3:], bits)
+ if sys.argv[1] == "print":
+ PrintFiles(sys.argv[2:])
+ elif sys.argv[1] == "merge":
+ MergeAndPrint(sys.argv[2:])
+ elif sys.argv[1] == "unpack":
+ Unpack(sys.argv[2:])
+ elif sys.argv[1] == "rawunpack":
+ RawUnpack(sys.argv[2:])
else:
Usage()