/
tracer.py
153 lines (131 loc) · 4.5 KB
/
tracer.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
import tempfile
import os
import subprocess
import re
import json
from dataclasses import dataclass
def to_tef(parsed_events):
events = []
for event in parsed_events:
if type(event) == Syscall:
start_ts = float(event.timestamp) * 1_000_000
duration_us = float(event.duration) * 1_000_000
events.append(
{
"name": event.name,
"ph": "X",
"ts": start_ts,
"dur": duration_us,
"pid": int(event.pid),
"tid": int(event.pid),
"args": {"args": event.args, "retval": event.retval},
}
)
elif type(event) == Signal:
ts = float(event.timestamp) * 1_000_000
events.append(
{
"name": f"Signal: {event.name}",
"ph": "i",
"s": "g",
"ts": ts,
"pid": int(event.pid),
"tid": int(event.pid),
"args": {"details": event.details},
}
)
tef_json = json.dumps(events, indent=2)
return tef_json
@dataclass
class Syscall:
name: str
args: str
retval: str
duration: str
timestamp: str
pid: str
def __str__(self):
return f"{self.name}({self.args}) = {self.retval} <{self.duration}s>"
@dataclass
class Signal:
name: str
details: str
timestamp: str
pid: str
def __str__(self):
return f"{self.name} {{{self.details}}}"
def parse_strace(f):
syscall_pattern = re.compile(r"^(\d+) (\d+\.\d+) (\w+)\((.*?)\) += +(.*?) <(.*?)>$")
signal_pattern = re.compile(r"^(\d+) (\d+\.\d+) --- (\w+) {(.*)} ---$")
unfinished_pattern = re.compile(
r"^(\d+) (\d+\.\d+) (\w+)\((.*?) +<unfinished \.\.\.>$"
)
resumed_pattern = re.compile(
r"^(\d+) (\d+\.\d+) <\.\.\. (\w+) resumed>(.*?)?\) += +(.*?) <(.*?)>$"
)
result = []
unfinished_syscalls = {}
for line in f:
if match := syscall_pattern.match(line):
pid, timestamp, syscall, args, retval, duration = match.groups()
result.append(
Syscall(
name=syscall,
args=args,
retval=retval,
duration=duration,
timestamp=timestamp,
pid=pid,
)
)
elif match := signal_pattern.match(line):
pid, timestamp, signal, details = match.groups()
result.append(
Signal(
name=signal,
details=details,
timestamp=timestamp,
pid=pid,
)
)
elif match := unfinished_pattern.match(line):
pid, timestamp, syscall, args = match.groups()
key = (pid, syscall)
if key not in unfinished_syscalls:
unfinished_syscalls[key] = []
unfinished_syscalls[key].append((timestamp, args))
elif match := resumed_pattern.match(line):
pid, timestamp, syscall, args2, retval, duration = match.groups()
key = (pid, syscall)
if key in unfinished_syscalls and unfinished_syscalls[key]:
start_timestamp, args = unfinished_syscalls[key].pop()
result.append(
Syscall(
name=syscall,
args=args + args2,
retval=retval,
duration=duration,
timestamp=start_timestamp,
pid=pid,
)
)
else:
print(f"Resumed syscall without a start: {line}")
else:
print(f"Attempted to parse unrecognized strace line: {line}")
return result
class Tracer:
def __enter__(self):
parent_pid = os.getpid()
self._trace_file = tempfile.mktemp()
cmd = f"strace --quiet=attach,exit -f -T -ttt -o {self._trace_file} -p {parent_pid}".split()
self._strace_proc = subprocess.Popen(cmd)
while not os.path.exists(self._trace_file):
pass
return self
def __exit__(self, exc_type, exc_value, traceback):
self._strace_proc.terminate()
self._strace_proc.wait()
with open(self._trace_file, "r") as f:
self.trace = parse_strace(f)
os.unlink(self._trace_file)