-
Notifications
You must be signed in to change notification settings - Fork 0
/
strace2js.py
executable file
·362 lines (299 loc) · 9.12 KB
/
strace2js.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/python2
#
# pystrace -- Python tools for parsing and analysing strace output files
#
#
# Copyright 2012
# The President and Fellows of Harvard College.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the University nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# Contributor(s):
# Peter Macko (http://eecs.harvard.edu/~pmacko)
#
import getopt
import os.path
import sys
from strace import *
document_template = \
'''
<html>
<head>
<script src="./vis.min.js"></script>
<link href="./vis.min.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#mynetwork {{
width: 100%;
height: 100%;
border: 1px solid lightgray;
}}
</style>
</head>
<body>
<div id="mynetwork"></div>
<script type="text/javascript">
// create an array with nodes
var nodes = new vis.DataSet([ {nodes} ]);
// create an array with edges
var edges = new vis.DataSet([ {edges} ]);
// create a network
var container = document.getElementById('mynetwork');
// provide the data in the vis format
var data = {{
nodes: nodes,
edges: edges
}};
var options = {{
physics: {{
enabled: false
}}
}};
// initialize your network!
var network = new vis.Network(container, data, options);
</script>
</body>
</html>
'''
execve_node_template = \
"{{id: {ts}, label: 'execve', title: '{arguments}', x: {xx}, y: {yy}, size: 25, color: 'orange', shape: 'diamond' }},"
default_node_template = \
"{{ id: {ts}, label: '{syscall}', x: {xx}, y: {yy}, size: 10, color: 'grey' }},"
fork_node_template = \
"{{ id: {ts}, label: '{syscall}', title: '{arguments}', x: {xx}, y: {yy}, size: 25 }},"
exited_ok_node_template = \
"{{ id: {ts}, label: 'EXITED', title: '{arguments}', x: {xx}, y: {yy}, size: 25, color: 'green', shape: 'box' }},"
exited_ko_node_template = \
"{{ id: {ts}, label: 'EXITED', title: '{arguments}', x: {xx}, y: {yy}, size: 25, color: 'red', shape: 'box' }},"
killed_node_template = \
"{{ id: {ts}, label: '{arguments}', x: {xx}, y: {yy}, size: 25, color: 'red', shape: 'box' }},"
assert_node_template = \
"{{ id: {ts}, label: '{syscall}', title: '{arguments}', x: {xx}, y: {yy}, size: 25, color: 'red', shape: 'triangle' }},"
edge_template = "{{from: {f}, to: {t} }},"
label_node_template = \
"{{id: {pid}, label: '{label}', x: {xx}, y: {yy}, size: 25, shape: 'text' }},"
#
# Convert to a .js
#
def convert2js(input_file, output_file=None, skip_nonproc=False, varan=False):
'''
Convert to a .html with vis.js
Arguments:
input_file - the input file, or None for standard input
output_file - the output file, or None for standard output
'''
# Open the files
if input_file is not None:
f_in = open(input_file, "r")
else:
f_in = sys.stdin
if output_file is not None:
f_out = open(output_file, "w")
else:
f_out = sys.stdout
# Process the file
strace_stream = StraceInputStream(f_in)
nodes = ""
edges = ""
pid_to_y = {}
pid_to_node = {}
x = 0
max_y = 0
pid_to_first_x = {}
# Varan variables
monitors = set()
zygote = None
leaders = set()
followers = set()
for entry in strace_stream:
# Get yy from PID
pid = entry.pid
if pid in pid_to_y:
y = pid_to_y[pid]
else:
y = max_y
pid_to_y[pid] = y
max_y += 100
# Which template to use?
if entry.syscall_name in ('fork', 'clone'):
entry_template = fork_node_template
# Register the fork/clone syscall as predecessor of the first
# syscall of the new process
if entry.return_value in pid_to_node:
# Sometimes the first syscall of a new process appears in the strace
# before the fork/clone that creates that process
edge = edge_template.format(
f=entry.timestamp,
t=pid_to_node[entry.return_value]
)
edges = edges + edge
else:
pid_to_node[entry.return_value] = entry.timestamp
if varan:
if pid in monitors:
if len(monitors) == 1 and zygote is None:
zygote = entry.return_value
else:
monitors.add(entry.return_value)
elif pid == zygote:
if len(leaders) == 0:
leaders.add(entry.return_value)
else:
followers.add(entry.return_value)
elif pid in leaders:
leaders.add(entry.return_value)
elif pid in followers:
followers.add(entry.return_value)
elif entry.syscall_name == 'execve':
entry_template = execve_node_template
if varan and pid not in monitors and entry.syscall_arguments[0].endswith('vx"'):
monitors.add(pid)
elif entry.syscall_name == 'EXIT':
if entry.return_value == '0':
entry_template = exited_ok_node_template
else:
entry_template = exited_ko_node_template
elif entry.syscall_name == 'KILL':
entry_template = killed_node_template
elif varan and entry.syscall_name == 'writev':
entry_template = assert_node_template
else:
entry_template = default_node_template
# Get/update the predecessor timestamp
if pid in pid_to_node:
pred = pid_to_node[pid]
else:
pred = None
if pid not in pid_to_first_x:
pid_to_first_x[pid] = x
elif entry_template == default_node_template and skip_nonproc:
continue
pid_to_node[pid] = entry.timestamp
# Generate node
if not len(entry.syscall_arguments) == 0:
args = str(entry.syscall_arguments)
args = args.replace('\'', '"')
args = args + ' / ' + str(entry.return_value)
else:
args = entry.return_value
node = entry_template.format(
ts=entry.timestamp,
syscall=entry.syscall_name,
arguments=args,
xx=x,
yy=y,
)
nodes = nodes + node
x = x + 25
# Generate edge
if not pred is None:
edge = edge_template.format(
f=pred,
t=entry.timestamp,
)
edges = edges + edge
for pid,x in pid_to_first_x.iteritems():
if varan:
if pid in monitors:
name = 'monitor'
elif pid in leaders:
name = 'leader'
elif pid in followers:
name = 'follower'
elif pid == zygote:
name = 'zygote'
else:
name = pid
#raise Exception("Process {} with unkown role".format(pid))
else:
name = pid
label = label_node_template.format(
pid=pid,
label=name,
xx=(x - 50),
yy=pid_to_y[pid],
)
nodes = nodes + label
f_out.write(document_template.format(
nodes=nodes,
edges=edges,
))
# Close the files
if f_out is not sys.stdout:
f_out.close()
strace_stream.close()
#
# Print the usage information
#
def usage():
sys.stderr.write('Usage: %s [OPTIONS] [FILE]\n\n'
% os.path.basename(sys.argv[0]))
sys.stderr.write('Options:\n')
sys.stderr.write(' -h, --help Print this help message and exit\n')
sys.stderr.write(' -o, --output FILE Print to file instead of the standard output\n')
sys.stderr.write(' -c, --compress Skip non process related syscalls\n')
#
# The main function
#
# Arguments:
# argv - the list of command-line arguments, excluding the executable name
#
def main(argv):
input_file = None
output_file = None
# Parse the command-line options
try:
options, remainder = getopt.gnu_getopt(argv, 'hcvo:',
['help', 'compress', 'varan', 'output='])
skip = False
varan = False
for opt, arg in options:
if opt in ('-h', '--help'):
usage()
return
elif opt in ('-o', '--output'):
output_file = arg
elif opt in ('-c', '--compress'):
skip = True
elif opt in ('-v', '--varan'):
varan = True
if len(remainder) > 1:
raise Exception("Too many options")
elif len(remainder) == 1:
input_file = remainder[0]
except Exception as e:
sys.stderr.write("%s: %s\n" % (os.path.basename(sys.argv[0]), e))
sys.exit(1)
# Convert to .html
try:
convert2js(input_file, output_file, skip, varan)
except IOError as e:
sys.stderr.write("%s: %s\n" % (os.path.basename(sys.argv[0]), e))
sys.exit(1)
#
# Entry point to the application
#
if __name__ == "__main__":
main(sys.argv[1:])