dap_monitor: Exit session on debugger disconnect.

Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
pull/1022/head
Jos Verlinde 2025-06-12 22:42:35 +02:00 zatwierdzone przez Andrew Leech
rodzic 3ed2d89a86
commit c4202e42da
1 zmienionych plików z 48 dodań i 35 usunięć

Wyświetl plik

@ -9,6 +9,7 @@ import sys
class DAPMonitor: class DAPMonitor:
def __init__(self, listen_port=5679, target_host='127.0.0.1', target_port=5678): def __init__(self, listen_port=5679, target_host='127.0.0.1', target_port=5678):
self.disconnect = False
self.listen_port = listen_port self.listen_port = listen_port
self.target_host = target_host self.target_host = target_host
self.target_port = target_port self.target_port = target_port
@ -44,7 +45,7 @@ class DAPMonitor:
threading.Thread(target=self.forward_server_to_client, daemon=True).start() threading.Thread(target=self.forward_server_to_client, daemon=True).start()
print("DAP Monitor active - press Ctrl+C to stop") print("DAP Monitor active - press Ctrl+C to stop")
while True: while not self.disconnect:
time.sleep(1) time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
@ -106,9 +107,30 @@ class DAPMonitor:
return None return None
content += chunk content += chunk
# Log the message # Parse and Log the message
message = self.parse_dap(source, content)
self.log_dap_message(source, message)
# Check for disconnect command
if message:
if "disconnect" == message.get('command', message.get('event', 'unknown')):
print(f"\n[{source}] Disconnect command received, stopping monitor.")
self.disconnect = True
return header + content
except Exception as e:
print(f"Error receiving from {source}: {e}")
return None
def parse_dap(self, source, content):
"""Parse DAP message and log it."""
try: try:
message = json.loads(content.decode('utf-8')) message = json.loads(content.decode('utf-8'))
return message
except json.JSONDecodeError:
print(f"\n[{source}] Invalid JSON: {content}")
return None
def log_dap_message(self, source, message):
"""Log DAP message details."""
msg_type = message.get('type', 'unknown') msg_type = message.get('type', 'unknown')
command = message.get('command', message.get('event', 'unknown')) command = message.get('command', message.get('event', 'unknown'))
seq = message.get('seq', 0) seq = message.get('seq', 0)
@ -134,15 +156,6 @@ class DAPMonitor:
if body: if body:
print(f" Body: {json.dumps(body, indent=2)}") print(f" Body: {json.dumps(body, indent=2)}")
except json.JSONDecodeError:
print(f"\n[{source}] Invalid JSON: {content}")
return header + content
except Exception as e:
print(f"Error receiving from {source}: {e}")
return None
def send_raw_data(self, sock, data): def send_raw_data(self, sock, data):
"""Send raw data to socket.""" """Send raw data to socket."""
try: try: