14
14
prog_name = ""
15
15
16
16
def Usage ():
17
- print >> sys .stderr , "Usage: \n " + \
18
- " " + prog_name + " merge FILE [FILE...] > OUTPUT\n " \
19
- " " + prog_name + " print FILE [FILE...]\n " \
20
- " " + prog_name + " unpack FILE [FILE...]\n " \
21
- " " + prog_name + " rawunpack FILE [FILE ...]\n " \
22
- " " + prog_name + " missing BINARY < LIST_OF_PCS\n "
17
+ sys .stderr .write (
18
+ "Usage: \n " + \
19
+ " " + prog_name + " merge FILE [FILE...] > OUTPUT\n " \
20
+ " " + prog_name + " print FILE [FILE...]\n " \
21
+ " " + prog_name + " unpack FILE [FILE...]\n " \
22
+ " " + prog_name + " rawunpack FILE [FILE ...]\n " \
23
+ " " + prog_name + " missing BINARY < LIST_OF_PCS\n " \
24
+ "\n " )
23
25
exit (1 )
24
26
25
27
def CheckBits (bits ):
@@ -78,19 +80,19 @@ def Merge(files):
78
80
s = set ()
79
81
for f in files :
80
82
s = s .union (set (ReadOneFile (f )))
81
- print >> sys .stderr , "%s: %d files merged; %d PCs total" % \
82
- (prog_name , len (files ), len (s ))
83
+ sys .stderr .write (
84
+ "%s: %d files merged; %d PCs total\n " % (prog_name , len (files ), len (s ))
85
+ )
83
86
return sorted (s )
84
87
85
88
def PrintFiles (files ):
86
89
if len (files ) > 1 :
87
90
s = Merge (files )
88
91
else : # If there is just on file, print the PCs in order.
89
92
s = ReadOneFile (files [0 ])
90
- print >> sys .stderr , "%s: 1 file merged; %d PCs total" % \
91
- (prog_name , len (s ))
93
+ sys .stderr .write ("%s: 1 file merged; %d PCs total\n " % (prog_name , len (s )))
92
94
for i in s :
93
- print "0x%x" % i
95
+ print ( "0x%x" % i )
94
96
95
97
def MergeAndPrint (files ):
96
98
if sys .stdout .isatty ():
@@ -107,7 +109,7 @@ def MergeAndPrint(files):
107
109
108
110
def UnpackOneFile (path ):
109
111
with open (path , mode = "rb" ) as f :
110
- print >> sys .stderr , "%s: unpacking %s" % (prog_name , path )
112
+ sys .stderr . write ( "%s: unpacking %s\n " % (prog_name , path ) )
111
113
while True :
112
114
header = f .read (12 )
113
115
if not header : return
@@ -119,8 +121,7 @@ def UnpackOneFile(path):
119
121
assert (len (module ) == module_length )
120
122
assert (len (blob ) == blob_size )
121
123
extracted_file = "%s.%d.sancov" % (module , pid )
122
- print >> sys .stderr , "%s: extracting %s" % \
123
- (prog_name , extracted_file )
124
+ sys .stderr .write ("%s: extracting %s\n " % (prog_name , extracted_file ))
124
125
# The packed file may contain multiple blobs for the same pid/module
125
126
# pair. Append to the end of the file instead of overwriting.
126
127
with open (extracted_file , 'ab' ) as f2 :
@@ -136,7 +137,7 @@ def Unpack(files):
136
137
def UnpackOneRawFile (path , map_path ):
137
138
mem_map = []
138
139
with open (map_path , mode = "rt" ) as f_map :
139
- print >> sys .stderr , "%s: reading map %s" % (prog_name , map_path )
140
+ sys .stderr . write ( "%s: reading map %s\n " % (prog_name , map_path ) )
140
141
bits = int (f_map .readline ())
141
142
if bits != 32 and bits != 64 :
142
143
raise Exception ('Wrong bits size in the map' )
@@ -150,7 +151,7 @@ def UnpackOneRawFile(path, map_path):
150
151
mem_map_keys = [m [0 ] for m in mem_map ]
151
152
152
153
with open (path , mode = "rb" ) as f :
153
- print >> sys .stderr , "%s: unpacking %s" % (prog_name , path )
154
+ sys .stderr . write ( "%s: unpacking %s\n " % (prog_name , path ) )
154
155
155
156
f .seek (0 , 2 )
156
157
size = f .tell ()
@@ -164,15 +165,15 @@ def UnpackOneRawFile(path, map_path):
164
165
(start , end , base , module_path ) = mem_map [map_idx ]
165
166
assert pc >= start
166
167
if pc >= end :
167
- print >> sys .stderr , "warning: %s: pc %x outside of any known mapping" % (prog_name , pc )
168
+ sys .stderr . write ( "warning: %s: pc %x outside of any known mapping\n " % (prog_name , pc ) )
168
169
continue
169
170
mem_map_pcs [map_idx ].append (pc - base )
170
171
171
172
for ((start , end , base , module_path ), pc_list ) in zip (mem_map , mem_map_pcs ):
172
173
if len (pc_list ) == 0 : continue
173
174
assert path .endswith ('.sancov.raw' )
174
175
dst_path = module_path + '.' + os .path .basename (path )[:- 4 ]
175
- print >> sys .stderr , "%s: writing %d PCs to %s" % (prog_name , len (pc_list ), dst_path )
176
+ sys .stderr . write ( "%s: writing %d PCs to %s\n " % (prog_name , len (pc_list ), dst_path ) )
176
177
sorted_pc_list = sorted (pc_list )
177
178
pc_buffer = struct .pack (TypeCodeForStruct (bits ) * len (pc_list ), * sorted_pc_list )
178
179
with open (dst_path , 'ab+' ) as f2 :
@@ -207,18 +208,19 @@ def PrintMissing(binary):
207
208
if not os .path .isfile (binary ):
208
209
raise Exception ('File not found: %s' % binary )
209
210
instrumented = GetInstrumentedPCs (binary )
210
- print >> sys .stderr , "%s: found %d instrumented PCs in %s" % (prog_name ,
211
- len (instrumented ),
212
- binary )
211
+ sys .stderr . write ( "%s: found %d instrumented PCs in %s\n " % (prog_name ,
212
+ len (instrumented ),
213
+ binary ) )
213
214
covered = set (int (line , 16 ) for line in sys .stdin )
214
- print >> sys .stderr , "%s: read %d PCs from stdin" % (prog_name , len (covered ))
215
+ sys .stderr . write ( "%s: read %d PCs from stdin\n " % (prog_name , len (covered ) ))
215
216
missing = instrumented - covered
216
- print >> sys .stderr , "%s: %d PCs missing from coverage" % (prog_name , len (missing ))
217
+ sys .stderr . write ( "%s: %d PCs missing from coverage\n " % (prog_name , len (missing ) ))
217
218
if (len (missing ) > len (instrumented ) - len (covered )):
218
- print >> sys .stderr , \
219
- "%s: WARNING: stdin contains PCs not found in binary" % prog_name
219
+ sys .stderr .write (
220
+ "%s: WARNING: stdin contains PCs not found in binary\n " % prog_name
221
+ )
220
222
for pc in sorted (missing ):
221
- print "0x%x" % pc
223
+ print ( "0x%x" % pc )
222
224
223
225
if __name__ == '__main__' :
224
226
prog_name = sys .argv [0 ]
0 commit comments