77JITLOG_MIN_VERSION = 1
88JITLOG_VERSION = 1
99
10+ class ParseException (Exception ):
11+ pass
12+
1013def read_jitlog_data (filename ):
1114 with open (str (filename ), 'rb' ) as fileobj :
1215 return fileobj .read ()
@@ -20,29 +23,49 @@ def parse_jitlog(filename, data=None):
2023 return f
2124
2225def _parse_jitlog (fileobj ):
26+ """ JitLog is parsed. if an exception is raised after
27+ the header has been read, it is saved in the field 'exc' on
28+ the forest returned.
29+
30+ This allows to parse an incomplete log file an still display
31+ everything that was readable!
32+ If a ParseException is directly raised, this is an error that cannot
33+ be recovered from!
34+ """
2335 is_jit_log = fileobj .read (1 ) == const .MARK_JITLOG_HEADER
2436 version = ord (fileobj .read (1 )) | (ord (fileobj .read (1 )) << 8 )
2537 is_32bit = ord (fileobj .read (1 ))
2638 machine = read_string (fileobj , True )
2739 forest = TraceForest (version , is_32bit , machine )
28- assert is_jit_log , "Missing header. Data might not be a jitlog!"
29- assert version >= JITLOG_MIN_VERSION , \
30- "Version does not match. Log is version %d%d is not satisfied" % \
31- ( version , JITLOG_VERSION )
40+ if not is_jit_log :
41+ raise ParseException ( "Missing header. Provided input might not be a jitlog!" )
42+ if version < JITLOG_MIN_VERSION :
43+ raise ParseException ( "Version %d is not supported" % version )
3244 while True :
3345 marker = fileobj .read (1 )
3446 if len (marker ) == 0 :
3547 break # end of file!
36- assert forest .is_jitlog_marker (marker ), \
37- "marker unkown: 0x%x at pos 0x%x" % (ord (marker ), fileobj .tell ())
48+ if not forest .is_jitlog_marker (marker ):
49+ msg = "marker unknown: 0x%x at pos 0x%x" % \
50+ (ord (marker ), fileobj .tell ())
51+ forest .exc = ParseException (msg )
52+ break
3853 trace = forest .last_trace
3954 try :
4055 read = marks .get_reader (version , marker )
4156 read (forest , trace , fileobj )
4257 forest .time_tick ()
43- except KeyError :
44- print ("failed at" , hex (fileobj .tell ()), "with marker" , marker )
45- raise
58+ except KeyError as e :
59+ forest .exc = e
60+ break
61+ except ParseException as e :
62+ forest .exc = e
63+ break
64+ except Exception as e :
65+ msg = "failed at %s with marker %s with exc %s" % \
66+ (hex (fileobj .tell ()), marker , str (e ))
67+ forest .exc = ParseException (msg )
68+ break
4669
4770 return forest
4871
0 commit comments