forked from mafemergency/byond-tracy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
replay.py
432 lines (373 loc) · 10 KB
/
replay.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import argparse
import ctypes
import socket
import selectors
import lz4.stream
# file protocol
FileSignature = 0x6D64796361727475
FileVersion = 2
FileEventZoneBegin =15
FileEventZoneEnd = 17
FileEventZoneColor = 62
FileEventFrameMark = 64
class FileHeader(ctypes.Structure):
_fields_ = (
("signature", ctypes.c_ulonglong),
("version", ctypes.c_uint),
("multiplier", ctypes.c_double),
("init_begin", ctypes.c_longlong),
("init_end", ctypes.c_longlong),
("delay", ctypes.c_longlong),
("resolution", ctypes.c_longlong),
("epoch", ctypes.c_longlong),
("exec_time", ctypes.c_longlong),
("pid", ctypes.c_longlong),
("sampling_period", ctypes.c_longlong),
("flags", ctypes.c_byte),
("cpu_arch", ctypes.c_byte),
("cpu_manufacturer", ctypes.c_char * 12),
("cpu_id", ctypes.c_uint),
("program_name", ctypes.c_char * 64),
("host_info", ctypes.c_char * 1024)
)
class FileZoneBegin(ctypes.Structure):
_fields_ = (
("tid", ctypes.c_uint32),
("srcloc", ctypes.c_uint32),
("timestamp", ctypes.c_int64)
)
class FileZoneEnd(ctypes.Structure):
_fields_ = (
("tid", ctypes.c_uint32),
("timestamp", ctypes.c_int64)
)
class FileZoneColor(ctypes.Structure):
_fields_ = (
("tid", ctypes.c_uint32),
("color", ctypes.c_uint32)
)
class FileFrameMark(ctypes.Structure):
_fields_ = (
("name", ctypes.c_uint32),
("timestamp", ctypes.c_int64)
)
class FileEvent(ctypes.Structure):
class Events(ctypes.Union):
_fields_ = (
("zone_begin", FileZoneBegin),
("zone_end", FileZoneEnd),
("zone_color", FileZoneColor),
("frame_mark", FileFrameMark),
)
_anonymous_ = ("event",)
_fields_ = (
("type", ctypes.c_byte),
("event", Events)
)
# network protocol
NetworkMaxFrameSize = 256 * 1024
NetworkHandshakeWelcome = b"\x01"
NetworkHandshakeProtocolMismatch = b"\x02"
NetworkEventZoneBegin = 15
NetworkEventZoneEnd = 17
NetworkEventTerminate = 55
NetworkEventThreadContext = 57
NetworkEventZoneColor = 62
NetworkEventFrameMark = 64
NetworkEventSrcloc = 67
NetworkResponseServerQueryNoop = 87
NetworkResponseSourceCodeNotAvailable = 88
NetworkResponseSymbolCodeNotAvailable = 89
NetworkResponseStringData = 94
NetworkResponseThreadName = 95
NetworkQueryTerminate = 0
NetworkQueryString = 1
NetworkQueryThreadString = 2
NetworkQuerySrcloc = 3
NetworkQueryPlotName = 4
NetworkQueryFrameName = 5
NetworkQueryParameter = 6
NetworkQueryFiberName = 7
NetworkQueryDisconnect = 8
NetworkQueryCallstackFrame = 9
NetworkQueryExternalName = 10
NetworkQuerySymbol = 11
NetworkQuerySymbolCode = 12
NetworkQueryCodeLocation = 13
NetworkQuerySourceCode = 14
NetworkQueryDataTransfer = 15
NetworkQueryDataTransferPart = 16
class NetworkHeader(ctypes.Structure):
_pack_ = 1
_fields_ = (
("multiplier", ctypes.c_double),
("init_begin", ctypes.c_longlong),
("init_end", ctypes.c_longlong),
("delay", ctypes.c_longlong),
("resolution", ctypes.c_longlong),
("epoch", ctypes.c_longlong),
("exec_time", ctypes.c_longlong),
("pid", ctypes.c_longlong),
("sampling_period", ctypes.c_longlong),
("flags", ctypes.c_uint8),
("cpu_arch", ctypes.c_uint8),
("cpu_manufacturer", ctypes.c_char * 12),
("cpu_id", ctypes.c_uint),
("program_name", ctypes.c_char * 64),
("host_info", ctypes.c_char * 1024)
)
class NetworkThreadContext(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("tid", ctypes.c_uint32)
)
class NetworkZoneBegin(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("timestamp", ctypes.c_int64),
("srcloc", ctypes.c_uint64)
)
class NetworkZoneEnd(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("timestamp", ctypes.c_int64)
)
class NetworkZoneColor(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("r", ctypes.c_uint8),
("g", ctypes.c_uint8),
("b", ctypes.c_uint8)
)
class NetworkFrameMark(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("timestamp", ctypes.c_int64),
("name", ctypes.c_uint64)
)
class NetworkSrcloc(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("name", ctypes.c_int64),
("function", ctypes.c_int64),
("file", ctypes.c_int64),
("line", ctypes.c_uint32),
("r", ctypes.c_uint8),
("g", ctypes.c_uint8),
("b", ctypes.c_uint8)
)
class NetworkRequest(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("ptr", ctypes.c_int64),
("extra", ctypes.c_uint32)
)
def main(stream):
def file_read_uint():
ctype = ctypes.c_uint()
stream.readinto(ctype)
return ctype.value
def file_read_chars(size):
if 0 == size:
return None
ctype = (ctypes.c_char * size)()
stream.readinto(ctype)
return ctype.value
header = FileHeader()
stream.readinto(header)
if header.signature != FileSignature:
print("incorrect signature")
return
if header.version != FileVersion:
print("incorrect version")
return
srclocs_len = file_read_uint()
strings = {}
srclocs = [None] * srclocs_len
for i in range(srclocs_len):
name_len = file_read_uint()
name = file_read_chars(name_len)
function_len = file_read_uint()
function = file_read_chars(function_len)
file_len = file_read_uint()
file = file_read_chars(file_len)
line = file_read_uint()
color = file_read_uint()
if name != None:
digest = hash(name)
strings[digest] = name
name = digest
else:
name = 0
if function != None:
digest = hash(function)
strings[digest] = function
function = digest
else:
function = 0
if file != None:
digest = hash(file)
strings[digest] = file
file = digest
else:
file = 0
srclocs[i] = (name, function, file, line, color)
server = socket.create_server(("127.0.0.1", 8086))
server.listen(1)
print("listening on 127.0.0.1:8086...")
client, addr = server.accept()
if client.recv(8) != b"TracyPrf":
print("bad client")
client.close()
return
protocol = ctypes.c_uint()
client.recv_into(protocol)
if protocol.value not in (56, 57):
print("bad protocol")
client.sendall(NetworkHandshakeProtocolMismatch)
client.close()
return
print(f"client accepted from {addr}")
client.sendall(NetworkHandshakeWelcome)
client.sendall(NetworkHeader(
multiplier = header.multiplier,
init_begin = header.init_begin,
init_end = header.init_end,
delay = header.delay,
resolution = header.resolution,
epoch = header.epoch,
exec_time = header.exec_time,
pid = header.pid,
sampling_period = header.sampling_period,
flags = header.flags,
cpu_arch = header.cpu_arch,
cpu_manufacturer = header.cpu_manufacturer,
cpu_id = header.cpu_id,
program_name = header.program_name,
host_info = header.host_info
))
event = FileEvent()
event_sz = ctypes.sizeof(event)
timestamp = 0
tid = None
buffer = bytearray(NetworkMaxFrameSize // event_sz)
offset = 0
compressor = lz4.stream.LZ4StreamCompressor(
"double_buffer",
NetworkMaxFrameSize,
store_comp_size = 4
)
def commit():
nonlocal offset
if offset > 0:
block = compressor.compress(buffer[:offset])
client.sendall(block)
offset = 0
def write_msg(msg):
nonlocal offset, buffer
sz = ctypes.sizeof(msg)
if offset + sz > len(buffer):
commit()
buffer[offset : offset + sz] = msg
offset += sz
def thread_context(event):
nonlocal tid, timestamp
if event.zone_begin.tid != tid:
tid = event.zone_begin.tid
timestamp = 0
write_msg(NetworkThreadContext(
type = NetworkEventThreadContext,
tid = tid
))
while event_sz == stream.readinto(event):
if event.type == FileEventZoneBegin:
thread_context(event)
write_msg(NetworkZoneBegin(
type = NetworkEventZoneBegin,
timestamp = event.zone_begin.timestamp - timestamp,
srcloc = event.zone_begin.srcloc
))
timestamp = event.zone_begin.timestamp
elif event.type == FileEventZoneEnd:
thread_context(event)
write_msg(NetworkZoneEnd(
type = NetworkEventZoneEnd,
timestamp = event.zone_end.timestamp - timestamp
))
timestamp = event.zone_end.timestamp
elif event.type == FileEventZoneColor:
thread_context(event)
write_msg(NetworkZoneColor(
type = NetworkEventZoneColor,
r = (event.zone_color.color >> 0x00) & 0xFF,
g = (event.zone_color.color >> 0x08) & 0xFF,
b = (event.zone_color.color >> 0x10) & 0xFF
))
elif event.type == FileEventFrameMark:
write_msg(NetworkFrameMark(
type = NetworkEventFrameMark,
name = 0,
timestamp = event.frame_mark.timestamp
))
commit()
def respond_string(string, ptr, type):
string_sz = len(string)
class NetworkStringData(ctypes.Structure):
_pack_ = 1
_fields_ = (
("type", ctypes.c_uint8),
("ptr", ctypes.c_uint64),
("len", ctypes.c_uint16),
("str", ctypes.c_char * string_sz)
)
write_msg(NetworkStringData(
type = type,
ptr = req.ptr,
len = string_sz,
str = string
))
req = NetworkRequest()
client.settimeout(1)
try:
while ctypes.sizeof(req) == client.recv_into(req):
if req.type == NetworkQuerySrcloc:
srcloc = srclocs[req.ptr]
write_msg(NetworkSrcloc(
type = NetworkEventSrcloc,
name = srcloc[0],
function = srcloc[1],
file = srcloc[2],
line = srcloc[3],
r = (srcloc[4] >> 0x00) & 0xFF,
g = (srcloc[4] >> 0x08) & 0xFF,
b = (srcloc[4] >> 0x10) & 0xFF
))
elif req.type == NetworkQueryString:
respond_string(strings[req.ptr], req.ptr, NetworkResponseStringData)
elif req.type == NetworkQuerySymbolCode:
write_msg(ctypes.c_uint8(NetworkResponseSymbolCodeNotAvailable))
elif req.type == NetworkQuerySourceCode:
write_msg(ctypes.c_uint8(NetworkResponseSourceCodeNotAvailable))
elif req.type == NetworkQueryDataTransfer:
write_msg(ctypes.c_uint8(NetworkResponseServerQueryNoop))
elif req.type == NetworkQueryDataTransferPart:
write_msg(ctypes.c_uint8(NetworkResponseServerQueryNoop))
elif req.type == NetworkQueryThreadString:
respond_string(b"main", req.ptr, NetworkResponseThreadName)
else:
print("unknown req:", req.type)
commit()
except socket.timeout:
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("file", type=argparse.FileType("rb"))
args = parser.parse_args()
main(args.file)