-
Notifications
You must be signed in to change notification settings - Fork 0
/
spandebug.py
48 lines (37 loc) · 1.48 KB
/
spandebug.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
import sys
import inspect
import json
SYSDIG_DEVICE = '/dev/sysdig'
tracenum = 0 # Use "t" instead when Tracers support this in JSON
class SysdigTrace():
def __enter__(self):
global tracenum
tracenum += 1
self.tnum = tracenum
self.d = open(SYSDIG_DEVICE, 'w', 0)
self.tags = []
sys.settrace(self.emit_markers)
return self
def emit_markers(self, frame, event, arg):
prior = inspect.currentframe().f_back
span = [ '>', self.tnum ]
e = [
{ 'file': prior.f_code.co_filename },
{ 'event': event }
]
if event == "call":
self.tags.append(prior.f_code.co_name + '()')
e.append({ 'lineno': str(inspect.currentframe().f_back.f_lineno) })
self.d.write(json.dumps(['>', self.tnum] + [self.tags] + [e]))
# elif event == "line":
# e.append({ 'line': linecache.getline(__file__, inspect.currentframe().f_back.f_lineno - 1) })
# e.append({ 'lineno': str(inspect.currentframe().f_back.f_lineno - 1) })
# f.write(json.dumps(span + [e]))
elif event == "return":
e.append({ 'lineno': str(inspect.currentframe().f_back.f_lineno) })
self.d.write(json.dumps(['<', self.tnum] + [self.tags] + [e]))
self.tags.pop()
return self.emit_markers
def __exit__(self, ext_type, exc_value, traceback):
sys.settrace(None)
self.d.close()