-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestclient.py
executable file
·515 lines (430 loc) · 15.2 KB
/
testclient.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python
from __future__ import annotations
import asyncio
import os
import time
from threading import Event, Thread
from collections import defaultdict
import sys
from enum import Enum
from dataclasses import dataclass
import json
import socket
from typing import Any, Literal, TypedDict, cast, Generator
from queue import Queue
import structlog
import logging
logging.basicConfig(level=logging.DEBUG, filename="log.log", filemode="w", format="%(message)s")
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.set_exc_info,
structlog.processors.TimeStamper(),
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.stdlib.BoundLogger,
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=False,
)
LOG = structlog.get_logger()
ServerMessageType = Literal["event", "response"]
class ServerMessage(TypedDict):
seq: int
type: ServerMessageType
class Response(ServerMessage):
request_seq: int
success: bool
command: Literal["initialize"]
body: Any | None
@dataclass
class Header:
content_length: int
def serislise_message(msg: dict) -> bytes:
m = json.dumps(msg)
n = len(m)
header = f"Content-Length: {n}"
message_str = "\r\n".join(
[
header,
"",
m,
]
)
return message_str.encode("utf8")
class ThreadStatus(Enum):
started = "started"
exited = "exited"
@dataclass(frozen=True)
class StackFrame:
id: int
name: str
line: int
column: int
source: StackFrameSource
@classmethod
def from_raw(cls, raw: dict) -> StackFrame:
source = StackFrameSource(path=raw["source"]["path"])
frame = cls(
id=raw["id"], name=raw["name"], line=raw["line"], column=raw["column"], source=source
)
return frame
@dataclass(frozen=True)
class StackFrameSource:
path: str
ThreadId = int
@dataclass(frozen=True)
class Scope:
variables_reference: int
name: str
expensive: bool
@dataclass(frozen=True)
class Variable:
name: str
value: str
typ: str
class Handler:
capabilities: dict
thread_status: dict[ThreadId, ThreadStatus]
stack_frames: dict[ThreadId, list[StackFrame] | None]
scopes: dict[int, list[Scope]]
variables: dict[int, list[Variable]]
wait_event: Event | None
def __init__(self, client: Client, events: Queue):
self.client = client
self.awaiting_response = {}
self.queue = events
self.wait_event = None
# state about the debug adapter
self.capabilities = {}
self.ready = False
self.current_thread = None
self.thread_status = {}
self.stack_frames = {}
self.scopes = {}
self.variables = defaultdict(list)
def wait_for_connect(self):
self.wait_event = Event()
LOG.info("waiting for debugger to stop")
self.wait_event.wait()
LOG.info("debugger stopped")
@property
def initialised(self) -> bool:
return len(self.capabilities) > 0
def loop(self):
while True:
res = self.queue.get()
match res["type"]:
case "response":
self.handle_response(cast(Response, res))
case "event":
self.handle_event(res)
case t:
raise NotImplementedError(t)
def handle_response(self, res: Response):
req = self.awaiting_response.pop(res["request_seq"], None)
if not req:
return
LOG.debug("response", type="response", response=res, request=req)
if not res["success"]:
LOG.warning("failed response", response=res, request=req)
raise RuntimeError(res)
match res["command"]:
case "initialize":
self.capabilities = res["body"]
self.clear_awaiting(res)
self.send_attach()
case "setFunctionBreakpoints":
self.configuration_done()
case "threads":
body = res["body"]
self.stack_frames.clear()
# for thread in body["threads"]:
# self.stack_frames[thread["id"]] = None
# self.send_stack_trace(thread["id"])
# TODO: wait for the results of these requests
# self.send_continue()
case "stackTrace":
body = res["body"]
stack_frames = body["stackFrames"]
frames = []
for raw_frame in stack_frames:
frame = StackFrame.from_raw(raw_frame)
self.send_scopes(frame)
frames.append(frame)
thread_id = req["arguments"]["threadId"]
self.stack_frames[thread_id] = frames
case "scopes":
body = res["body"]
scopes = []
for raw_scope in body["scopes"]:
scope = Scope(
variables_reference=raw_scope["variablesReference"],
name=raw_scope["name"],
expensive=raw_scope["expensive"],
)
scopes.append(scope)
if scope.variables_reference > 0 and not scope.expensive:
self.send_variables(scope.variables_reference)
frame_id = req["arguments"]["frameId"]
self.scopes[frame_id] = scopes
self.clear_awaiting(res)
case "variables":
body = res["body"]
for variable in body["variables"]:
if (ref := variable.get("variablesReference")) > 0:
# TODO decode further
return
self.send_variables(variable["variablesReference"])
self.clear_awaiting(res)
else:
v = Variable(
name=variable["name"],
value=variable["value"],
typ=variable["type"],
)
variable_ref = req["arguments"]["variablesReference"]
# if variable_ref in self.variables:
# raise ValueError(f"Already encountered variable {variable_ref}: {v}")
self.variables[variable_ref].append(v)
LOG.debug(
"variables",
count=sum(len(self.variables[key]) for key in self.variables),
)
self.clear_awaiting(res)
case "disconnect":
self.client.disconnect()
LOG.debug("disconnect")
raise SystemExit(0)
def clear_awaiting(self, response: Response):
self.awaiting_response.pop(response["request_seq"], None)
def handle_event(self, event: ServerMessage):
LOG.debug("event", message=event)
match event["event"]:
case "initialized":
self.initialized = True
self.set_function_breakpoints()
case "stopped":
if self.wait_event and not self.wait_event.is_set():
self.wait_event.set()
self.current_thread = event["body"]["threadId"]
self.reset_state()
self.send_threads()
self.send_stack_trace(self.current_thread)
case "output":
body = event["body"]
match body["category"]:
case "stdout":
print(body["output"])
case "stderr":
print(body["output"], file=sys.stderr)
case "thread":
body = event["body"]
thread_id = body["threadId"]
status = ThreadStatus(body["reason"])
self.thread_status[thread_id] = status
LOG.debug("thread", status=self.thread_status)
case "terminated":
self.send_disconnect()
def reset_state(self):
self.stack_frames.clear()
self.scopes.clear()
def send_initialize(self):
msg = {
"command": "initialize",
"arguments": {
"adapterID": "dap-gui",
"clientName": "DAP GUI",
"pathFormat": "path",
# TODO
"supportsRunInTerminalRequest": False,
"supportsStartDebuggingRequest": False,
},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def send_disconnect(self):
msg = {
"command": "disconnect",
"terminateDebuggee": True,
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def send_variables(self, variables_reference: int):
msg = {
"command": "variables",
"arguments": {"variablesReference": variables_reference},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def send_stack_trace(self, thread_id: ThreadId):
msg = {
"command": "stackTrace",
"arguments": {
"threadId": thread_id,
},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def send_scopes(self, frame: StackFrame):
msg = {
"command": "scopes",
"arguments": {
"frameId": frame.id,
},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def configuration_done(self):
msg = {
"command": "configurationDone",
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def send_threads(self):
msg = {
"command": "threads",
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def send_attach(self):
msg = {
"command": "attach",
"arguments": {
"name": "Python: Remote Attach (ext)",
"type": "python",
"request": "attach",
"justMyCode": False,
},
"type": "request",
"seq": 2,
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def launch(self):
msg = {
"command": "launch",
"arguments": {
"program": os.path.join(os.getcwd(), "test.py"),
},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def send_continue(self):
msg = {
"command": "continue",
"arguments": {
"threadId": self.current_thread,
},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
self.current_thread = None
def set_breakpoints(self):
raise NotImplementedError
msg = {
"command": "setBreakpoints",
"arguments": {
"source": {
"name": "mapping.py",
"path": "/home/simon/work/localstack/localstack-ext/localstack_ext/services/appsync/mapping.py",
},
"lines": [114],
"breakpoints": [{"line": 114}],
"sourceModified": False,
},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def set_function_breakpoints(self):
msg = {
"command": "setFunctionBreakpoints",
"arguments": {
"breakpoints": [
{"name": "main"},
],
},
}
sent_message = self.client.send_request(msg)
self.awaiting_response[sent_message["seq"]] = sent_message
def parse_header(header_text: str) -> Header:
key, value = header_text.split(":")
assert key.strip() == "Content-Length"
return Header(content_length=int(value.strip()))
class Client:
def __init__(self, host: str = "127.0.0.1", port: int = 5678):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
self.seq = 1
self.buf = ""
def send_request(self, msg: dict) -> dict:
full_message = {
**msg,
**{
"seq": self.seq,
"type": "request",
},
}
LOG.debug("request", request=full_message)
buf = serislise_message(full_message)
self.sock.send(buf)
self.seq += 1
return full_message
def receive_message(self) -> Generator[ServerMessage, None, None]:
try:
m = self.sock.recv(8196).decode("utf8")
except OSError:
LOG.warning("nothing read")
# EOF
raise SystemExit(0)
self.buf += m
# TODO: what if more headers are added?
assert self.buf.startswith("Content-Length")
while True:
# try to read a single message
try:
header_str, _, rest = self.buf.split("\r\n", 2)
except ValueError:
break
header = parse_header(header_str)
if header.content_length <= 0:
raise RuntimeError(f"Invalid content length read: {header=}")
if len(rest) < header.content_length:
# not enough data in the buffer so receive again
break
body_str = rest[: header.content_length]
self.buf = rest[header.content_length :]
try:
res = json.loads(body_str)
except json.JSONDecodeError as e:
raise RuntimeError("could not read message body") from e
yield res
def disconnect(self):
self.sock.close()
def receive_messages(client: Client, events: Queue):
try:
while True:
for msg in client.receive_message():
events.put(msg)
except Exception as e:
LOG.warning("background thread error", exc_info=e)
if __name__ == "__main__":
queue = Queue()
client = Client(port=5678)
message_handler = Thread(
target=receive_messages,
name="message-handler",
kwargs=dict(client=client, events=queue),
daemon=True,
)
message_handler.start()
handler = Handler(client, queue)
ht = Thread(target=handler.loop, name="handler-loop", daemon=True)
ht.start()
handler.send_initialize()
handler.wait_for_connect()
breakpoint()
handler.send_continue()
handler.send_disconnect()