-
Notifications
You must be signed in to change notification settings - Fork 110
/
callee.py
394 lines (343 loc) · 12.9 KB
/
callee.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python
#
# Copyright 2015 vonnyfly([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import commands
import sys
import string
import random
import os
import hashlib
from optparse import OptionParser
import re
bt_threshold = 3
callgraph_threshold = 3
keep_dot_files = False
is_dtrace = False
output_format = "png"
is_simplify = False
callgraph_level = 0
black_lists_stap = ("kretprobe_trampoline",)
lastest_id=0
def get_random_id():
global lastest_id
lastest_id = lastest_id + 1
return str(lastest_id)
def get_random_id_obsolete():
return ''.join(random.SystemRandom().choice(string.digits + string.ascii_lowercase) for _ in range(6))
def write_file(basename, suffix, content):
#outfile = basename + suffix
#if os.path.exists(outfile):
outfile = basename + "_" + get_random_id() + suffix
with open(outfile, "w") as f:
f.write(content)
return outfile
def filter_name(funcs):
newfuncs = []
for f in funcs:
if "%" in f or "-" in f[2:]:
print "Rename func %s => " % (f,) ,
if f.startswith('->'):
f = '->' + f[2:].replace('-', '__').replace('%', '__')
if f.startswith('<-'):
f = '<-' + f[2:].replace('-', '__').replace('%', '__')
print f
newfuncs.append(f)
return newfuncs
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def deduplicate(files):
container = {}
for f in files:
hash = hashlib.sha256(open(f).read()).hexdigest()
if hash in container:
try:
os.remove(f)
except OSError:
pass
print "Remove duplicate %s" % (f[0:-4], )
else:
container[hash] =f
return container.values()
def draw_backtrace(funcs):
funcs = filter_name(funcs)
if len(funcs) < bt_threshold:
return None
header = """digraph backtrace{
\tnode [shape="record", width=4, height=.3, fixedsize=true];
"""
footer = "\n\t label=\"backtrace of %s\"}\n" %(funcs[0],)
nodes = ""
links = ""
for i, func in enumerate(reversed(funcs)):
nodes += "\t a%d[label=\"%s\"];\n" %(i, func)
links = "\t" + ' -> '.join(["a%d" %(i,) for i in range(0, len(funcs))]) + ";\n"
content = "%s%s%s%s" %(header, nodes, links, footer)
return write_file(funcs[0], ".bt.dot", content)
class Tree:
class Node:
def __init__(self, data, name):
self.data = data
self.parent = None
self.children = []
self.head = self
self.id = 0
self.next_id = 0
self.name = name
self.is_head = False
self.first_child = None
self.is_root = False
def link(self):
return self.data + "_" + str(self.id)
def __init__(self, root = None):
self.root = root;
self.core_content = ""
self.id = -1
def create_node(self, data):
self.id += 1
return Tree.Node(data, "a" + str(self.id))
def clean(self):
self.root = None
self.core_content = ""
self.id = -1
def same_subtree(self, node1, node2):
if len(node1.children) != len(node2.children):
return False
if len(node1.children) == 0 and node1.data == node2.data:
return True
for i in range(0, len(node1.children)):
if not self.same_subtree(node1.children[i], node2.children[i]):
return False
return True
def _split_node_children(self, children, is_simplify=False):
uniq_keys = {}
for child in children:
if child.data not in uniq_keys:
uniq_keys[child.data] = child
else:
# same, if children are the same
if is_simplify:
if self.same_subtree(uniq_keys[child.data], child):
if len(child.children) != 0:
child.children = []
child.first_child = uniq_keys[child.data].first_child
child.is_root = False
child.is_head = False
#print "Same tree:", child.id,child.name,child.data
#print "->", uniq_keys[child.data].id,uniq_keys[child.data].name,uniq_keys[child.data].data
uniq_keys[child.data].next_id += 1
child.id = uniq_keys[child.data].next_id
return children
def travel_tree(self):
# skip the first same node
if len(self.root.children) == 1 and self.root.data == self.root.children[0].data:
self.root = self.root.children[0]
self.root.parent = None
self.root.is_root = True
self._travel_tree(self.root, 1)
def _travel_tree(self, node, level):
uniq_children = self._split_node_children(node.children, is_simplify)
if node.first_child:
if node.is_root:
new_root = "\t %s [label=\"<%s>%s\", color=red];\n" %(node.name, node.data, node.data)
self.core_content += new_root
#print new_root
# Hacker: uniq_children = None && node.first_child !=None,
# means, this node is simplified, and point to the old one.
if len(uniq_children) != 0:
new_node = "\t %s [label=\"%s\"];\n" %(node.first_child.name, " | ".join(["<" + i.link() + ">" + i.data for i in uniq_children]))
self.core_content += new_node
#print new_node
left = "\t %s:%s" % (node.head.name, node.link())
right = "%s:%s" % (node.first_child.name, node.first_child.link())
style = "[dir=both, arrowtail=dot];\n"
new_link = "%s -> %s%s" %(left, right, style)
self.core_content += new_link
#print new_link
if level == callgraph_level:
return
for child in uniq_children:
self._travel_tree(child, level + 1)
def draw_callgraph(funcs):
funcs = filter_name(funcs)
if len(funcs) < 2 * callgraph_threshold:
return None
header = """digraph callee {
\tsize="30,40";
\tcenter=true;
\tmargin=0.1;
\tnodesep=2;
\tranksep=0.5;
\trankdir=LR;
\tedge[arrowsize=1.0, arrowhead=vee];
\tnode[shape = record,fontsize=20, width=1, height=1, fixedsize=false];
"""
is_first = True
root = None
index = None
tree = Tree()
for label in funcs:
sign = label[0:2]
func = label[2:]
if sign == "->":
new_node = tree.create_node(func)
if is_first:
root = new_node
index = new_node
is_first = False
index.is_root = True
else:
new_node.parent = index
if len(index.children) == 0:
new_node.is_first = True
index.first_child = new_node
else:
new_node.head = index.first_child
index.children.append(new_node)
#print "%s -> %s\n" %(index.data, new_node.data)
index = new_node
elif sign == "<-":
if not index:
print "[-]except: ", label
return None
#print "from return : %s" %(index.data, )
index = index.parent
if not index:
break
#print "Begin travel:"
tree.root = root
tree.travel_tree()
footer = "\n\t label=\"callgraph of %s\";\n}\n" %(tree.root.data,)
content = "%s%s%s" % (header, tree.core_content, footer)
outfile = write_file(tree.root.data, ".cg.dot", content)
tree.clean()
return outfile
def generate_pngs(dotfiles):
dotfiles = deduplicate(dotfiles)
if which("dot") is None:
sys.exit("please install graphviz\n")
for f in dotfiles:
cmd = 'filename=%s; dot $filename -T%s >${filename%%.*}.%s' % (f, output_format, output_format)
commands.getstatusoutput(cmd)
if not keep_dot_files:
os.remove(f)
print "Generated %s.%s" % (f[0:-4], output_format)
def main():
global callgraph_threshold, bt_threshold, keep_dot_files, is_dtrace
global output_format, is_simplify, callgraph_level
parser = OptionParser(usage='%prog [options] log_file',
description='Generate pngs from Dtrace or Systemtap log')
parser.add_option('-k', '--keep-dot', action = 'store_true',
help = 'keep dot file, default delect it')
parser.add_option('-o', '--output-format', type = "string",
help = 'output file format, could be ["png", "jpg", "svg"], '
' default is png')
parser.add_option('-d', '--is_dtrace_log', action = 'store_true',
help = 'default is systemtap log, -d stand for dtrace log')
parser.add_option('-c', '--threshold_cg', type = "int",
help = 'only generate call graph when the call link'
' extend to threshold_cg')
parser.add_option('-b', '--threshold_bt', type = "int",
help = 'only generate backtrace graph when the call link'
' extend to threshold_bt')
parser.add_option('-s', '--is_simplify', action = 'store_true',
help = 'output simplified version, remove the same node, loop node')
parser.add_option('-l', '--level', type = 'int',
help = 'the depth of output call tree')
(options, args) = parser.parse_args()
if options.keep_dot:
keep_dot_files = True
if options.is_simplify:
is_simplify = True
if options.is_dtrace_log:
is_dtrace = True
if options.output_format and options.output_format in ["png", "jpg", "svg"]:
output_format = options.output_format
if options.threshold_cg and options.threshold_cg > 0:
callgraph_threshold = options.threshold_cg
if options.threshold_bt and options.threshold_bt > 0:
bt_threshold = options.threshold_bt
if options.level and options.level > 0:
callgraph_level = options.level
if len(args) != 1:
parser.error("incorrect number of arguments")
try:
with open(args[0]) as f:
content = f.readlines()
except:
sys.exit("No file!")
bt_list = []
callgraph_list = []
dotfiles = []
hex_pat = re.compile(r'^0x([0-9a-f]+)$')
for l in content:
if '+' not in l:
if not is_dtrace: # stap, only address
if re.search(hex_pat, l.strip(' \n\t')):
continue
outfile = draw_backtrace(bt_list)
if outfile:
dotfiles.append(outfile)
bt_list = []
if '->' not in l and '<-' not in l and '|' not in l:
outfile = draw_callgraph(callgraph_list);
if outfile:
dotfiles.append(outfile)
callgraph_list = []
if '+' in l:
if is_dtrace:
func_name = l.split('+')[0].strip().split('`')[-1]
else:
if ':' in l:
func_name = l.split(':')[-1].split('+')[0].strip()
if func_name not in black_lists_stap:
bt_list.append(func_name)
else:
continue
if '|' in l and is_dtrace:
if 'entry' in l:
func_name = "->" + l.strip(' \n\t').split('|')[-1].split(':')[0].strip(' \n\t')
elif ':return' in l:
continue
else:
func_name = l.strip(' \n\t').split('|')[-1].strip(' \n\t')
callgraph_list.append(func_name)
if '->' in l or '<-' in l:
if is_dtrace:
func_name = ''.join(l.strip(' \n\t').split(' ')[-2:])
else:
func_name = l.split(':')[-1].strip().split(' ')[0]
callgraph_list.append(func_name)
outfile = draw_backtrace(bt_list)
if outfile:
dotfiles.append(outfile)
outfile = draw_callgraph(callgraph_list)
if outfile:
dotfiles.append(outfile)
generate_pngs(dotfiles)
if __name__ == "__main__":
main()