3
3
import sys
4
4
from ..common .messaging import JsonMessageChannel
5
5
from ..common .constants import (
6
- CMD_INITIALIZE , CMD_LAUNCH , CMD_ATTACH , CMD_SET_BREAKPOINTS ,
7
- CMD_CONTINUE , CMD_NEXT , CMD_STEP_IN , CMD_STEP_OUT , CMD_PAUSE ,
8
- CMD_STACK_TRACE , CMD_SCOPES , CMD_VARIABLES , CMD_EVALUATE , CMD_DISCONNECT ,
9
- CMD_CONFIGURATION_DONE , CMD_THREADS , CMD_SOURCE , EVENT_INITIALIZED , EVENT_STOPPED , EVENT_CONTINUED , EVENT_TERMINATED ,
10
- STOP_REASON_BREAKPOINT , STOP_REASON_STEP , STOP_REASON_PAUSE ,
11
- TRACE_CALL , TRACE_LINE , TRACE_RETURN , TRACE_EXCEPTION
6
+ CMD_INITIALIZE ,
7
+ CMD_LAUNCH ,
8
+ CMD_ATTACH ,
9
+ CMD_SET_BREAKPOINTS ,
10
+ CMD_CONTINUE ,
11
+ CMD_NEXT ,
12
+ CMD_STEP_IN ,
13
+ CMD_STEP_OUT ,
14
+ CMD_PAUSE ,
15
+ CMD_STACK_TRACE ,
16
+ CMD_SCOPES ,
17
+ CMD_VARIABLES ,
18
+ CMD_EVALUATE ,
19
+ CMD_DISCONNECT ,
20
+ CMD_CONFIGURATION_DONE ,
21
+ CMD_THREADS ,
22
+ CMD_SOURCE ,
23
+ EVENT_INITIALIZED ,
24
+ EVENT_STOPPED ,
25
+ EVENT_CONTINUED ,
26
+ EVENT_TERMINATED ,
27
+ STOP_REASON_BREAKPOINT ,
28
+ STOP_REASON_STEP ,
29
+ STOP_REASON_PAUSE ,
30
+ TRACE_CALL ,
31
+ TRACE_LINE ,
32
+ TRACE_RETURN ,
33
+ TRACE_EXCEPTION ,
12
34
)
13
35
from .pdb_adapter import PdbAdapter
14
36
@@ -34,7 +56,7 @@ def _debug_print(self, message):
34
56
35
57
@property
36
58
def _baremetal (self ) -> bool :
37
- return sys .platform not in ("linux" ) # to be expanded
59
+ return sys .platform not in ("linux" ) # to be expanded
38
60
39
61
def start (self ):
40
62
"""Start the debug session message loop."""
@@ -77,7 +99,7 @@ def initialize_connection(self):
77
99
message_count += 1
78
100
79
101
# Just wait for attach, then we can return control
80
- if message .get (' command' ) == ' attach' :
102
+ if message .get (" command" ) == " attach" :
81
103
attached = True
82
104
print ("[DAP] ✅ Attach received - returning control to main thread" )
83
105
break
@@ -191,12 +213,12 @@ def _handle_request(self, message):
191
213
elif command == CMD_SOURCE :
192
214
self ._handle_source (seq , args )
193
215
else :
194
- self .channel .send_response (command , seq , success = False ,
195
- message = f"Unknown command: { command } " )
216
+ self .channel .send_response (
217
+ command , seq , success = False , message = f"Unknown command: { command } "
218
+ )
196
219
197
220
except Exception as e :
198
- self .channel .send_response (command , seq , success = False ,
199
- message = str (e ))
221
+ self .channel .send_response (command , seq , success = False , message = str (e ))
200
222
201
223
def _handle_initialize (self , seq , args ):
202
224
"""Handle initialize request."""
@@ -251,16 +273,15 @@ def _handle_attach(self, seq, args):
251
273
self .debug_logging = args .get ("logToFile" , False )
252
274
253
275
self ._debug_print (f"[DAP] Processing attach request with args: { args } " )
254
- print (f"[DAP] Debug logging { 'enabled' if self .debug_logging else 'disabled' } (logToFile={ self .debug_logging } )" )
255
-
276
+ print (
277
+ f"[DAP] Debug logging { 'enabled' if self .debug_logging else 'disabled' } (logToFile={ self .debug_logging } )"
278
+ )
279
+
256
280
# get debugger root and debugee root from pathMappings
257
- for pm in args .get ("pathMappings" ,[]):
281
+ for pm in args .get ("pathMappings" , []):
258
282
# debugee - debugger
259
- self .pdb .path_mappings .append (
260
- (pm .get ("remoteRoot" , "./" ),
261
- pm .get ("localRoot" , "./" ))
262
- )
263
- # # TODO: justMyCode, debugOptions ,
283
+ self .pdb .path_mappings .append ((pm .get ("remoteRoot" , "./" ), pm .get ("localRoot" , "./" )))
284
+ # # TODO: justMyCode, debugOptions ,
264
285
265
286
# Enable trace function
266
287
self .pdb .set_trace_function (self ._trace_function )
@@ -282,8 +303,9 @@ def _handle_set_breakpoints(self, seq, args):
282
303
# Set breakpoints in pdb adapter
283
304
actual_breakpoints = self .pdb .set_breakpoints (filename , breakpoints )
284
305
285
- self .channel .send_response (CMD_SET_BREAKPOINTS , seq ,
286
- body = {"breakpoints" : actual_breakpoints })
306
+ self .channel .send_response (
307
+ CMD_SET_BREAKPOINTS , seq , body = {"breakpoints" : actual_breakpoints }
308
+ )
287
309
288
310
def _handle_continue (self , seq , args ):
289
311
"""Handle continue request."""
@@ -322,8 +344,11 @@ def _handle_pause(self, seq, args):
322
344
def _handle_stack_trace (self , seq , args ):
323
345
"""Handle stackTrace request."""
324
346
stack_frames = self .pdb .get_stack_trace ()
325
- self .channel .send_response (CMD_STACK_TRACE , seq ,
326
- body = {"stackFrames" : stack_frames , "totalFrames" : len (stack_frames )})
347
+ self .channel .send_response (
348
+ CMD_STACK_TRACE ,
349
+ seq ,
350
+ body = {"stackFrames" : stack_frames , "totalFrames" : len (stack_frames )},
351
+ )
327
352
328
353
def _handle_scopes (self , seq , args ):
329
354
"""Handle scopes request."""
@@ -345,18 +370,17 @@ def _handle_evaluate(self, seq, args):
345
370
frame_id = args .get ("frameId" )
346
371
context = args .get ("context" , "watch" )
347
372
if not expression :
348
- self .channel .send_response (CMD_EVALUATE , seq , success = False ,
349
- message = "No expression provided" )
373
+ self .channel .send_response (
374
+ CMD_EVALUATE , seq , success = False , message = "No expression provided"
375
+ )
350
376
return
351
377
try :
352
378
result = self .pdb .evaluate_expression (expression , frame_id )
353
- self .channel .send_response (CMD_EVALUATE , seq , body = {
354
- "result" : str (result ),
355
- "variablesReference" : 0
356
- })
379
+ self .channel .send_response (
380
+ CMD_EVALUATE , seq , body = {"result" : str (result ), "variablesReference" : 0 }
381
+ )
357
382
except Exception as e :
358
- self .channel .send_response (CMD_EVALUATE , seq , success = False ,
359
- message = str (e ))
383
+ self .channel .send_response (CMD_EVALUATE , seq , success = False , message = str (e ))
360
384
361
385
def _handle_disconnect (self , seq , args ):
362
386
"""Handle disconnect request."""
@@ -372,10 +396,7 @@ def _handle_configuration_done(self, seq, args):
372
396
def _handle_threads (self , seq , args ):
373
397
"""Handle threads request."""
374
398
# MicroPython is single-threaded, so return one thread
375
- threads = [{
376
- "id" : self .thread_id ,
377
- "name" : "main"
378
- }]
399
+ threads = [{"id" : self .thread_id , "name" : "main" }]
379
400
self .channel .send_response (CMD_THREADS , seq , body = {"threads" : threads })
380
401
381
402
def _handle_source (self , seq , args ):
@@ -395,10 +416,13 @@ def _handle_source(self, seq, args):
395
416
content = f .read ()
396
417
self .channel .send_response (CMD_SOURCE , seq , body = {"content" : content })
397
418
except Exception :
398
- self .channel .send_response (CMD_SOURCE , seq , success = False ,
399
- message = "cancelled"
400
- # message=f"Could not read source: {e}"
401
- )
419
+ self .channel .send_response (
420
+ CMD_SOURCE ,
421
+ seq ,
422
+ success = False ,
423
+ message = "cancelled" ,
424
+ # message=f"Could not read source: {e}"
425
+ )
402
426
403
427
def _trace_function (self , frame , event , arg ):
404
428
"""Trace function called by sys.settrace."""
@@ -407,19 +431,23 @@ def _trace_function(self, frame, event, arg):
407
431
408
432
# Handle breakpoints and stepping
409
433
if self .pdb .should_stop (frame , event , arg ):
410
- self ._send_stopped_event (STOP_REASON_BREAKPOINT if self .pdb .hit_breakpoint else
411
- STOP_REASON_STEP if self .stepping else STOP_REASON_PAUSE )
434
+ self ._send_stopped_event (
435
+ STOP_REASON_BREAKPOINT
436
+ if self .pdb .hit_breakpoint
437
+ else STOP_REASON_STEP
438
+ if self .stepping
439
+ else STOP_REASON_PAUSE
440
+ )
412
441
# Wait for continue command
413
442
self .pdb .wait_for_continue ()
414
443
415
444
return self ._trace_function
416
445
417
446
def _send_stopped_event (self , reason ):
418
447
"""Send stopped event to client."""
419
- self .channel .send_event (EVENT_STOPPED ,
420
- reason = reason ,
421
- threadId = self .thread_id ,
422
- allThreadsStopped = True )
448
+ self .channel .send_event (
449
+ EVENT_STOPPED , reason = reason , threadId = self .thread_id , allThreadsStopped = True
450
+ )
423
451
424
452
def wait_for_client (self ):
425
453
"""Wait for client to initialize."""
@@ -433,7 +461,7 @@ def trigger_breakpoint(self):
433
461
434
462
def debug_this_thread (self ):
435
463
"""Enable debugging for current thread."""
436
- if hasattr (sys , ' settrace' ):
464
+ if hasattr (sys , " settrace" ):
437
465
sys .settrace (self ._trace_function )
438
466
439
467
def is_connected (self ):
@@ -443,7 +471,7 @@ def is_connected(self):
443
471
def disconnect (self ):
444
472
"""Disconnect from client."""
445
473
self .connected = False
446
- if hasattr (sys , ' settrace' ):
474
+ if hasattr (sys , " settrace" ):
447
475
sys .settrace (None )
448
476
self .pdb .cleanup ()
449
477
self .channel .close ()
0 commit comments