diff --git a/python-ecosys/debugpy/dap_monitor.py b/python-ecosys/debugpy/dap_monitor.py index 3af4eba16..b323a61cb 100644 --- a/python-ecosys/debugpy/dap_monitor.py +++ b/python-ecosys/debugpy/dap_monitor.py @@ -9,6 +9,7 @@ class DAPMonitor: def __init__(self, listen_port=5679, target_host='127.0.0.1', target_port=5678): + self.disconnect = False self.listen_port = listen_port self.target_host = target_host self.target_port = target_port @@ -44,9 +45,9 @@ def start(self): threading.Thread(target=self.forward_server_to_client, daemon=True).start() print("DAP Monitor active - press Ctrl+C to stop") - while True: + while not self.disconnect: time.sleep(1) - + except KeyboardInterrupt: print("\nStopping DAP Monitor...") except Exception as e: @@ -106,43 +107,55 @@ def receive_dap_message(self, sock, source): return None content += chunk - # Log the message - try: - message = json.loads(content.decode('utf-8')) - msg_type = message.get('type', 'unknown') - command = message.get('command', message.get('event', 'unknown')) - seq = message.get('seq', 0) - - print(f"\n[{source}] {msg_type.upper()}: {command} (seq={seq})") - - if msg_type == 'request': - args = message.get('arguments', {}) - if args: - print(f" Arguments: {json.dumps(args, indent=2)}") - elif msg_type == 'response': - success = message.get('success', False) - req_seq = message.get('request_seq', 0) - print(f" Success: {success}, Request Seq: {req_seq}") - body = message.get('body') - if body: - print(f" Body: {json.dumps(body, indent=2)}") - msg = message.get('message') - if msg: - print(f" Message: {msg}") - elif msg_type == 'event': - body = message.get('body', {}) - if body: - print(f" Body: {json.dumps(body, indent=2)}") - - except json.JSONDecodeError: - print(f"\n[{source}] Invalid JSON: {content}") - + # 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: + 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') + command = message.get('command', message.get('event', 'unknown')) + seq = message.get('seq', 0) + + print(f"\n[{source}] {msg_type.upper()}: {command} (seq={seq})") + + if msg_type == 'request': + args = message.get('arguments', {}) + if args: + print(f" Arguments: {json.dumps(args, indent=2)}") + elif msg_type == 'response': + success = message.get('success', False) + req_seq = message.get('request_seq', 0) + print(f" Success: {success}, Request Seq: {req_seq}") + body = message.get('body') + if body: + print(f" Body: {json.dumps(body, indent=2)}") + msg = message.get('message') + if msg: + print(f" Message: {msg}") + elif msg_type == 'event': + body = message.get('body', {}) + if body: + print(f" Body: {json.dumps(body, indent=2)}") + def send_raw_data(self, sock, data): """Send raw data to socket.""" try: diff --git a/python-ecosys/debugpy/debugpy/server/debug_session.py b/python-ecosys/debugpy/debugpy/server/debug_session.py index 4f60ee358..624467522 100644 --- a/python-ecosys/debugpy/debugpy/server/debug_session.py +++ b/python-ecosys/debugpy/debugpy/server/debug_session.py @@ -328,7 +328,10 @@ def _handle_evaluate(self, seq, args): expression = args.get("expression", "") frame_id = args.get("frameId") context = args.get("context", "watch") - + if not expression: + self.channel.send_response(CMD_EVALUATE, seq, success=False, + message="No expression provided") + return try: result = self.pdb.evaluate_expression(expression, frame_id) self.channel.send_response(CMD_EVALUATE, seq, body={ diff --git a/python-ecosys/debugpy/debugpy/server/pdb_adapter.py b/python-ecosys/debugpy/debugpy/server/pdb_adapter.py index 83693c65c..a33cf6655 100644 --- a/python-ecosys/debugpy/debugpy/server/pdb_adapter.py +++ b/python-ecosys/debugpy/debugpy/server/pdb_adapter.py @@ -235,7 +235,7 @@ def get_variables(self, variables_ref): continue try: - value_str = str(value) + value_str = repr(value) type_str = type(value).__name__ variables.append({ diff --git a/python-ecosys/debugpy/test_vscode.py b/python-ecosys/debugpy/test_vscode.py index aca063baf..2dca82d34 100644 --- a/python-ecosys/debugpy/test_vscode.py +++ b/python-ecosys/debugpy/test_vscode.py @@ -2,10 +2,14 @@ """Test script for VS Code debugging with MicroPython debugpy.""" import sys + sys.path.insert(0, '.') import debugpy +foo = 42 +bar = "Hello, MicroPython!" + def fibonacci(n): """Calculate fibonacci number (iterative for efficiency).""" if n <= 1: @@ -17,6 +21,7 @@ def fibonacci(n): def debuggable_code(): """The actual code we want to debug - wrapped in a function so sys.settrace will trace it.""" + global foo print("Starting debuggable code...") # Test data - set breakpoint here (using smaller numbers to avoid slow fibonacci) @@ -24,6 +29,7 @@ def debuggable_code(): for i, num in enumerate(numbers): print(f"Calculating fibonacci({num})...") result = fibonacci(num) # <-- SET BREAKPOINT HERE (line 26) + foo += result # Modify foo to see if it gets traced print(f"fibonacci({num}) = {result}") print(sys.implementation) import machine