7
7
JITLOG_MIN_VERSION = 1
8
8
JITLOG_VERSION = 1
9
9
10
+ class ParseException (Exception ):
11
+ pass
12
+
10
13
def read_jitlog_data (filename ):
11
14
with open (str (filename ), 'rb' ) as fileobj :
12
15
return fileobj .read ()
@@ -20,29 +23,49 @@ def parse_jitlog(filename, data=None):
20
23
return f
21
24
22
25
def _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
+ """
23
35
is_jit_log = fileobj .read (1 ) == const .MARK_JITLOG_HEADER
24
36
version = ord (fileobj .read (1 )) | (ord (fileobj .read (1 )) << 8 )
25
37
is_32bit = ord (fileobj .read (1 ))
26
38
machine = read_string (fileobj , True )
27
39
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 )
32
44
while True :
33
45
marker = fileobj .read (1 )
34
46
if len (marker ) == 0 :
35
47
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
38
53
trace = forest .last_trace
39
54
try :
40
55
read = marks .get_reader (version , marker )
41
56
read (forest , trace , fileobj )
42
57
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
46
69
47
70
return forest
48
71
0 commit comments