-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_trace.py
executable file
·163 lines (144 loc) · 5.16 KB
/
print_trace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
INDENT_WITH = "| "
COUNT_MAX = 75
import sys
import os
import argparse
from src_tracer.trace import Trace
from src_tracer.database import Database
# arguments
ap = argparse.ArgumentParser()
ap.add_argument("trace_file",
help="file containing the trace")
ap.add_argument("--seek", type=int, default=0,
help="skip bytes in the beginning of the tracefile")
ap.add_argument("--count", type=int, default=-1,
help="read count bytes from the trace (default: read all)")
ap.add_argument("--count-elems", metavar="ELEMS", type=int, default=1,
help="read extra elems after count (default: 1)")
ap.add_argument("--database",
help="path to the function database")
ap.add_argument("--pretty", type=int, default=3,
help="5,4: indent, 5,3: function names, 2: basic, 1: compact, 0: no newline, -1: informative, default: 3")
ap.add_argument("--p01", action='store_true',
help="alternative bit trace format with only 0/1 for O/I trace elements")
ap.add_argument("--show-pos", action='store_true',
help="for each element show its count offset in the trace")
ap.add_argument("--extra-indent", metavar="IND", type=int, default=0,
help="print extra indent")
args = ap.parse_args()
# create connection to database
if not args.p01 and args.pretty in (5,3):
if args.database is None:
database_dir = os.path.dirname(args.trace_file)
database_path = os.path.join(database_dir, 'function_database.db')
else:
database_path = args.database
if not os.path.exists(database_path):
error = f"Could not open database from {database_path}, try --pretty 4 or --database"
raise Exception(error)
database = Database(store_dir=None, path=database_path)
else:
database = None
if args.pretty in (0,1) and args.show_pos:
error = "--show_pos is only available for pretty > 1"
raise Exception(error)
trace = Trace.from_file(args.trace_file, seek_bytes=args.seek, count_bytes=args.count, count_elems=args.count_elems)
previous_newline = True
indent = args.extra_indent
count = 0
def print_indent():
global previous_newline, indent, count, INDENT_WITH, COUNT_MAX
if args.pretty == 1 and count < COUNT_MAX:
previous_newline = False
return
if not previous_newline:
print_newline()
previous_newline = False
if args.pretty < 4:
return
for i in range(indent):
print(INDENT_WITH, end='')
count = len(INDENT_WITH)*indent
def print_newline():
global previous_newline, indent, count, INDENT_WITH, COUNT_MAX
if args.pretty > 0:
print(end='\n')
count = 0
previous_newline = True
def print_with_count(s):
global previous_newline, indent, count, INDENT_WITH, COUNT_MAX
if previous_newline or count >= COUNT_MAX:
print_indent()
count += len(s)
print(s, end='')
def print_extra(s):
global previous_newline, indent, count, INDENT_WITH, COUNT_MAX
print_indent()
print(s, end='')
count += len(s)
if args.pretty > 1:
print_newline()
if args.pretty == -1:
for elem in trace.full_iter(trace._trace):
print(elem)
sys.exit()
if args.p01:
for elem in trace:
if elem.letter == 'O':
print('0', end='')
elif elem.letter == 'I':
print('1', end='')
sys.exit()
setjmp_indent = []
for elem in trace:
if elem.letter == 'I' or elem.letter == 'O':
print_with_count(f"{elem.letter}")
elif elem.letter in ('R', 'S'):
indent -= 1
print_extra(elem.pretty(show_pos=args.show_pos))
elif elem.letter == 'A':
# anonymous function call
print_extra(elem.pretty(show_pos=args.show_pos))
indent += 1
elif elem.letter == 'T':
# setjmp, try
# save current indent
setjmp_indent.append(indent)
# print as usual
print_extra(elem.pretty(show_pos=args.show_pos))
elif elem.letter == 'U':
# try end
setjmp_indent.pop()
# print as usual
print_extra(elem.pretty(show_pos=args.show_pos))
elif elem.letter == 'J':
# longjmp, catch
# restore indent
indent = setjmp_indent[-elem.num -1]
# print as usual
print_extra(elem.pretty(show_pos=args.show_pos))
elif elem.bs == b'':
print_extra(elem.pretty(show_pos=args.show_pos))
if elem.letter == 'C':
indent += 1
else:
num = elem.num
if elem.letter == 'C':
if args.pretty in (5,3):
name = database.get_name(num)
# All upper case letters in the trace are treated as elem,
# so we have to print name.lower() instead of name
if name:
print_extra(elem.pretty(show_pos=args.show_pos, name=name.lower()))
else:
print_extra(elem.pretty(show_pos=args.show_pos))
else:
print_extra(elem.pretty(show_pos=args.show_pos))
indent += 1
else:
print_extra(elem.pretty(show_pos=args.show_pos))
if not previous_newline:
print_newline()
if database:
database.close_connection()