@@ -37,40 +37,39 @@ namespace UnityDebug
37
37
{
38
38
internal class UnityDebugSession : DebugSession
39
39
{
40
- private const string MONO = "mono" ;
41
- private readonly string [ ] MONO_EXTENSIONS = new String [ ] {
40
+ readonly string [ ] MONO_EXTENSIONS = {
42
41
".cs" , ".csx" ,
43
42
".cake" ,
44
43
".fs" , ".fsi" , ".ml" , ".mli" , ".fsx" , ".fsscript" ,
45
44
".hx"
46
45
} ;
47
- private const int MAX_CHILDREN = 100 ;
48
- private const int MAX_CONNECTION_ATTEMPTS = 10 ;
49
- private const int CONNECTION_ATTEMPT_INTERVAL = 500 ;
50
-
51
- private AutoResetEvent m_ResumeEvent ;
52
- private bool m_DebuggeeExecuting ;
53
- private readonly object m_Lock = new object ( ) ;
54
- private SoftDebuggerSession m_Session ;
55
- private ProcessInfo m_ActiveProcess ;
46
+ const int MAX_CHILDREN = 100 ;
47
+ const int MAX_CONNECTION_ATTEMPTS = 10 ;
48
+ const int CONNECTION_ATTEMPT_INTERVAL = 500 ;
49
+
50
+ AutoResetEvent m_ResumeEvent ;
51
+ bool m_DebuggeeExecuting ;
52
+ readonly object m_Lock = new object ( ) ;
53
+ SoftDebuggerSession m_Session ;
54
+ ProcessInfo m_ActiveProcess ;
56
55
SourceBreakpoint [ ] m_Breakpoints ;
57
- private List < Catchpoint > m_Catchpoints ;
58
- private DebuggerSessionOptions m_DebuggerSessionOptions ;
59
-
60
- private Handles < ObjectValue [ ] > m_VariableHandles ;
61
- private Handles < StackFrame > m_FrameHandles ;
62
- private ObjectValue m_Exception ;
63
- private Dictionary < int , Thread > m_SeenThreads ;
64
- private bool m_Terminated ;
56
+ List < Catchpoint > m_Catchpoints ;
57
+ DebuggerSessionOptions m_DebuggerSessionOptions ;
58
+
59
+ Handles < ObjectValue [ ] > m_VariableHandles ;
60
+ Handles < StackFrame > m_FrameHandles ;
61
+ ObjectValue m_Exception ;
62
+ Dictionary < int , Thread > m_SeenThreads ;
63
+ bool m_Terminated ;
65
64
IUnityDbgConnector unityDebugConnector ;
66
-
67
- public UnityDebugSession ( ) : base ( )
65
+
66
+ public UnityDebugSession ( )
68
67
{
69
68
m_ResumeEvent = new AutoResetEvent ( false ) ;
70
69
m_Breakpoints = new SourceBreakpoint [ 0 ] ;
71
70
m_VariableHandles = new Handles < ObjectValue [ ] > ( ) ;
72
- m_FrameHandles = new Handles < Mono . Debugging . Client . StackFrame > ( ) ;
73
- m_SeenThreads = new Dictionary < int , VSCodeDebug . Thread > ( ) ;
71
+ m_FrameHandles = new Handles < StackFrame > ( ) ;
72
+ m_SeenThreads = new Dictionary < int , Thread > ( ) ;
74
73
75
74
m_DebuggerSessionOptions = new DebuggerSessionOptions {
76
75
EvaluationOptions = EvaluationOptions . DefaultOptions
@@ -159,15 +158,15 @@ public UnityDebugSession() : base()
159
158
} ;
160
159
161
160
m_Session . TargetThreadStarted += ( sender , e ) => {
162
- int tid = ( int ) e . Thread . Id ;
161
+ var tid = ( int ) e . Thread . Id ;
163
162
lock ( m_SeenThreads ) {
164
163
m_SeenThreads [ tid ] = new Thread ( tid , e . Thread . Name ) ;
165
164
}
166
165
SendEvent ( new ThreadEvent ( "started" , tid ) ) ;
167
166
} ;
168
167
169
168
m_Session . TargetThreadStopped += ( sender , e ) => {
170
- int tid = ( int ) e . Thread . Id ;
169
+ var tid = ( int ) e . Thread . Id ;
171
170
lock ( m_SeenThreads ) {
172
171
m_SeenThreads . Remove ( tid ) ;
173
172
}
@@ -225,7 +224,7 @@ public override void Launch(Response response, dynamic args)
225
224
226
225
public override void Attach ( Response response , dynamic args )
227
226
{
228
- string name = getString ( args , "name" ) ;
227
+ string name = GetString ( args , "name" ) ;
229
228
230
229
SetExceptionBreakpoints ( args . __exceptionOptions ) ;
231
230
@@ -245,26 +244,22 @@ public override void Attach(Response response, dynamic args)
245
244
}
246
245
else
247
246
{
248
- if ( name . Contains ( "Editor" ) )
247
+ if ( ! name . Contains ( "Editor" ) )
249
248
{
250
- string pathToEditorInstanceJson = getString ( args , "path" ) ;
251
- var jObject = JObject . Parse ( File . ReadAllText ( pathToEditorInstanceJson . TrimStart ( '/' ) ) ) ;
252
- var processId = jObject [ "process_id" ] . ToObject < int > ( ) ;
253
- process = processes . First ( p => p . Id == processId ) ;
249
+ TooManyInstances ( response , name , processes ) ;
250
+ return ;
254
251
}
255
- else
256
- {
257
- SendErrorResponse ( response , 8002 , "Multiple targets with name '{_name}' running. Unable to connect.\n " +
258
- "Use \" Unity Attach Debugger\" from the command palette (View > Command Palette...) to specify which process to attach to." , new { _name = name } ) ;
259
-
260
- SendOutput ( "stdout" , "UnityDebug: Multiple targets with name '" + name + "' running. Unable to connect.\n " +
261
- "Use \" Unity Attach Debugger\" from the command palette (View > Command Palette...) to specify which process to attach to." ) ;
262
-
263
- foreach ( var p in processes )
264
- SendOutput ( "stdout" , "UnityDebug: Found Unity process '" + p . Name + "' (" + p . Id + ")\n " ) ;
265
252
253
+ string pathToEditorInstanceJson = GetString ( args , "path" ) ;
254
+ if ( ! File . Exists ( pathToEditorInstanceJson ) )
255
+ {
256
+ TooManyInstances ( response , name , processes ) ;
266
257
return ;
267
258
}
259
+
260
+ var jObject = JObject . Parse ( File . ReadAllText ( pathToEditorInstanceJson . TrimStart ( '/' ) ) ) ;
261
+ var processId = jObject [ "process_id" ] . ToObject < int > ( ) ;
262
+ process = processes . First ( p => p . Id == processId ) ;
268
263
}
269
264
270
265
var attachInfo = UnityProcessDiscovery . GetUnityAttachInfo ( process . Id , ref unityDebugConnector ) ;
@@ -275,7 +270,19 @@ public override void Attach(Response response, dynamic args)
275
270
SendResponse ( response ) ;
276
271
}
277
272
278
- private void Connect ( IPAddress address , int port )
273
+ void TooManyInstances ( Response response , string name , UnityProcessInfo [ ] processes )
274
+ {
275
+ SendErrorResponse ( response , 8002 , "Multiple targets with name '{_name}' running. Unable to connect.\n " +
276
+ "Use \" Unity Attach Debugger\" from the command palette (View > Command Palette...) to specify which process to attach to." , new { _name = name } ) ;
277
+
278
+ SendOutput ( "stdout" , "UnityDebug: Multiple targets with name '" + name + "' running. Unable to connect.\n " +
279
+ "Use \" Unity Attach Debugger\" from the command palette (View > Command Palette...) to specify which process to attach to." ) ;
280
+
281
+ foreach ( var p in processes )
282
+ SendOutput ( "stdout" , "UnityDebug: Found Unity process '" + p . Name + "' (" + p . Id + ")\n " ) ;
283
+ }
284
+
285
+ void Connect ( IPAddress address , int port )
279
286
{
280
287
lock ( m_Lock ) {
281
288
@@ -291,7 +298,7 @@ private void Connect(IPAddress address, int port)
291
298
}
292
299
293
300
//---- private ------------------------------------------
294
- private void SetExceptionBreakpoints ( dynamic exceptionOptions )
301
+ void SetExceptionBreakpoints ( dynamic exceptionOptions )
295
302
{
296
303
if ( exceptionOptions == null )
297
304
{
@@ -305,7 +312,7 @@ private void SetExceptionBreakpoints(dynamic exceptionOptions)
305
312
m_Catchpoints . Clear ( ) ;
306
313
307
314
var exceptions = exceptionOptions . ToObject < dynamic [ ] > ( ) ;
308
- for ( int i = 0 ; i < exceptions . Length ; i ++ ) {
315
+ for ( var i = 0 ; i < exceptions . Length ; i ++ ) {
309
316
310
317
var exception = exceptions [ i ] ;
311
318
@@ -355,10 +362,6 @@ public override void Disconnect(Response response, dynamic args)
355
362
public override void SetFunctionBreakpoints ( Response response , dynamic arguments )
356
363
{
357
364
var breakpoints = new List < ResponseBreakpoint > ( ) ;
358
- foreach ( var breakpoint in arguments . breakpoints )
359
- {
360
- //new ResponseBreakpoint(true,);
361
- }
362
365
SendResponse ( response , new SetFunctionBreakpointsResponse ( breakpoints ) ) ;
363
366
}
364
367
@@ -374,7 +377,7 @@ public override void Continue(Response response, dynamic args)
374
377
}
375
378
}
376
379
377
- private void WaitForSuspend ( )
380
+ void WaitForSuspend ( )
378
381
{
379
382
if ( ! m_DebuggeeExecuting ) return ;
380
383
@@ -424,7 +427,7 @@ public override void Pause(Response response, dynamic args)
424
427
PauseDebugger ( ) ;
425
428
}
426
429
427
- private void PauseDebugger ( )
430
+ void PauseDebugger ( )
428
431
{
429
432
lock ( m_Lock ) {
430
433
if ( m_Session != null && m_Session . IsRunning )
@@ -434,14 +437,13 @@ private void PauseDebugger()
434
437
435
438
protected override void SetVariable ( Response response , object args )
436
439
{
437
- int reference = getInt ( args , "variablesReference" , - 1 ) ;
440
+ var reference = GetInt ( args , "variablesReference" , - 1 ) ;
438
441
if ( reference == - 1 ) {
439
442
SendErrorResponse ( response , 3009 , "variables: property 'variablesReference' is missing" , null , false , true ) ;
440
443
return ;
441
444
}
442
445
443
- string value = getString ( args , "value" ) ;
444
- string type = "" ;
446
+ var value = GetString ( args , "value" ) ;
445
447
if ( m_VariableHandles . TryGet ( reference , out var children ) ) {
446
448
if ( children != null && children . Length > 0 ) {
447
449
if ( children . Length > MAX_CHILDREN ) {
@@ -453,7 +455,7 @@ protected override void SetVariable(Response response, object args)
453
455
continue ;
454
456
v . WaitHandle . WaitOne ( ) ;
455
457
var variable = CreateVariable ( v ) ;
456
- if ( variable . name == getString ( args , "name" ) )
458
+ if ( variable . name == GetString ( args , "name" ) )
457
459
{
458
460
v . Value = value ;
459
461
SendResponse ( response , new SetVariablesResponseBody ( value , variable . type , variable . variablesReference ) ) ;
@@ -465,10 +467,6 @@ protected override void SetVariable(Response response, object args)
465
467
466
468
public override void SetExceptionBreakpoints ( Response response , dynamic args )
467
469
{
468
- if ( args [ "filters" ] . Contains ( "all_exceptions" ) )
469
- {
470
-
471
- }
472
470
SetExceptionBreakpoints ( args . exceptionOptions ) ;
473
471
SendResponse ( response ) ;
474
472
}
@@ -528,8 +526,8 @@ public override void SetBreakpoints(Response response, dynamic args)
528
526
529
527
public override void StackTrace ( Response response , dynamic args )
530
528
{
531
- int maxLevels = getInt ( args , "levels" , 10 ) ;
532
- int threadReference = getInt ( args , "threadId" , 0 ) ;
529
+ int maxLevels = GetInt ( args , "levels" , 10 ) ;
530
+ int threadReference = GetInt ( args , "threadId" , 0 ) ;
533
531
534
532
WaitForSuspend ( ) ;
535
533
@@ -543,7 +541,7 @@ public override void StackTrace(Response response, dynamic args)
543
541
}
544
542
545
543
var stackFrames = new List < VSCodeDebug . StackFrame > ( ) ;
546
- int totalFrames = 0 ;
544
+ var totalFrames = 0 ;
547
545
548
546
var bt = thread . Backtrace ;
549
547
if ( bt != null && bt . FrameCount >= 0 ) {
@@ -580,10 +578,10 @@ public override void StackTrace(Response response, dynamic args)
580
578
SendResponse ( response , new StackTraceResponseBody ( stackFrames , totalFrames ) ) ;
581
579
}
582
580
583
- private ThreadInfo DebuggerActiveThread ( )
581
+ ThreadInfo DebuggerActiveThread ( )
584
582
{
585
583
lock ( m_Lock ) {
586
- return m_Session == null ? null : m_Session . ActiveThread ;
584
+ return m_Session ? . ActiveThread ;
587
585
}
588
586
}
589
587
@@ -593,13 +591,13 @@ public override void Source(Response response, dynamic arguments) {
593
591
594
592
public override void Scopes ( Response response , dynamic args ) {
595
593
596
- int frameId = getInt ( args , "frameId" , 0 ) ;
594
+ int frameId = GetInt ( args , "frameId" , 0 ) ;
597
595
var frame = m_FrameHandles . Get ( frameId , null ) ;
598
596
599
597
var scopes = new List < Scope > ( ) ;
600
598
601
599
if ( frame . Index == 0 && m_Exception != null ) {
602
- scopes . Add ( new Scope ( "Exception" , m_VariableHandles . Create ( new ObjectValue [ ] { m_Exception } ) ) ) ;
600
+ scopes . Add ( new Scope ( "Exception" , m_VariableHandles . Create ( new [ ] { m_Exception } ) ) ) ;
603
601
}
604
602
605
603
var locals = new [ ] { frame . GetThisReference ( ) } . Concat ( frame . GetParameters ( ) ) . Concat ( frame . GetLocalVariables ( ) ) . Where ( x => x != null ) . ToArray ( ) ;
@@ -612,7 +610,7 @@ public override void Scopes(Response response, dynamic args) {
612
610
613
611
public override void Variables ( Response response , dynamic args )
614
612
{
615
- int reference = getInt ( args , "variablesReference" , - 1 ) ;
613
+ int reference = GetInt ( args , "variablesReference" , - 1 ) ;
616
614
if ( reference == - 1 ) {
617
615
SendErrorResponse ( response , 3009 , "variables: property 'variablesReference' is missing" , null , false , true ) ;
618
616
return ;
@@ -621,24 +619,20 @@ public override void Variables(Response response, dynamic args)
621
619
WaitForSuspend ( ) ;
622
620
var variables = new List < Variable > ( ) ;
623
621
624
- ObjectValue [ ] children ;
625
- if ( m_VariableHandles . TryGet ( reference , out children ) ) {
622
+ if ( m_VariableHandles . TryGet ( reference , out var children ) ) {
626
623
if ( children != null && children . Length > 0 ) {
627
624
628
- bool more = false ;
625
+ var more = false ;
629
626
if ( children . Length > MAX_CHILDREN ) {
630
627
children = children . Take ( MAX_CHILDREN ) . ToArray ( ) ;
631
628
more = true ;
632
629
}
633
630
634
- if ( children . Length < 20 ) {
631
+ if ( children . Length < 20 )
632
+ {
635
633
// Wait for all values at once.
636
634
WaitHandle . WaitAll ( children . Select ( x => x . WaitHandle ) . ToArray ( ) ) ;
637
- foreach ( var v in children ) {
638
- if ( v . IsError )
639
- continue ;
640
- variables . Add ( CreateVariable ( v ) ) ;
641
- }
635
+ variables . AddRange ( from v in children where ! v . IsError select CreateVariable ( v ) ) ;
642
636
}
643
637
else {
644
638
foreach ( var v in children ) {
@@ -678,7 +672,7 @@ public override void Threads(Response response, dynamic args)
678
672
679
673
public override void Evaluate ( Response response , dynamic args )
680
674
{
681
- var expression = getString ( args , "expression" ) ;
675
+ var expression = GetString ( args , "expression" ) ;
682
676
683
677
if ( expression == null ) {
684
678
SendError ( response , "expression missing" ) ;
@@ -722,7 +716,7 @@ public override void Evaluate(Response response, dynamic args)
722
716
SendError ( response , "not available" ) ;
723
717
return ;
724
718
}
725
-
719
+
726
720
int handle = 0 ;
727
721
if ( val . HasChildren )
728
722
{
@@ -739,28 +733,28 @@ void SendError(Response response, string error)
739
733
740
734
//---- private ------------------------------------------
741
735
742
- private void SendOutput ( string category , string data ) {
743
- if ( ! String . IsNullOrEmpty ( data ) ) {
736
+ void SendOutput ( string category , string data ) {
737
+ if ( ! string . IsNullOrEmpty ( data ) ) {
744
738
if ( data [ data . Length - 1 ] != '\n ' ) {
745
739
data += '\n ' ;
746
740
}
747
741
SendEvent ( new OutputEvent ( category , data ) ) ;
748
742
}
749
743
}
750
744
751
- private void Terminate ( string reason ) {
745
+ void Terminate ( string reason ) {
752
746
if ( ! m_Terminated ) {
753
747
SendEvent ( new TerminatedEvent ( ) ) ;
754
748
m_Terminated = true ;
755
749
}
756
750
}
757
751
758
- private StoppedEvent CreateStoppedEvent ( string reason , ThreadInfo ti , string text = null )
752
+ StoppedEvent CreateStoppedEvent ( string reason , ThreadInfo ti , string text = null )
759
753
{
760
754
return new StoppedEvent ( ( int ) ti . Id , reason , text ) ;
761
755
}
762
756
763
- private ThreadInfo FindThread ( int threadReference )
757
+ ThreadInfo FindThread ( int threadReference )
764
758
{
765
759
if ( m_ActiveProcess != null ) {
766
760
foreach ( var t in m_ActiveProcess . GetThreads ( ) ) {
@@ -772,7 +766,7 @@ private ThreadInfo FindThread(int threadReference)
772
766
return null ;
773
767
}
774
768
775
- private void Stopped ( )
769
+ void Stopped ( )
776
770
{
777
771
m_Exception = null ;
778
772
m_VariableHandles . Reset ( ) ;
@@ -785,7 +779,7 @@ private void Stopped()
785
779
return new Variable(pname, v.DisplayValue, v.HasChildren ? _variableHandles.Create(v.GetAllChildren()) : 0);
786
780
}*/
787
781
788
- private Variable CreateVariable ( ObjectValue v )
782
+ Variable CreateVariable ( ObjectValue v )
789
783
{
790
784
var dv = v . DisplayValue ;
791
785
if ( dv . Length > 1 && dv [ 0 ] == '{' && dv [ dv . Length - 1 ] == '}' ) {
@@ -794,38 +788,22 @@ private Variable CreateVariable(ObjectValue v)
794
788
return new Variable ( v . Name , dv , v . TypeName , v . HasChildren ? m_VariableHandles . Create ( v . GetAllChildren ( ) ) : 0 ) ;
795
789
}
796
790
797
- private Backtrace DebuggerActiveBacktrace ( ) {
791
+ Backtrace DebuggerActiveBacktrace ( ) {
798
792
var thr = DebuggerActiveThread ( ) ;
799
793
return thr == null ? null : thr . Backtrace ;
800
794
}
801
795
802
- private ExceptionInfo DebuggerActiveException ( ) {
796
+ ExceptionInfo DebuggerActiveException ( ) {
803
797
var bt = DebuggerActiveBacktrace ( ) ;
804
- return bt == null ? null : bt . GetFrame ( 0 ) . GetException ( ) ;
805
- }
806
-
807
- private bool HasMonoExtension ( string path )
808
- {
809
- foreach ( var e in MONO_EXTENSIONS ) {
810
- if ( path . EndsWith ( e ) ) {
811
- return true ;
812
- }
813
- }
814
- return false ;
798
+ return bt ? . GetFrame ( 0 ) . GetException ( ) ;
815
799
}
816
800
817
- private static bool getBool ( dynamic container , string propertyName , bool dflt = false )
801
+ bool HasMonoExtension ( string path )
818
802
{
819
- try {
820
- return ( bool ) container [ propertyName ] ;
821
- }
822
- catch ( Exception ) {
823
- // ignore and return default value
824
- }
825
- return dflt ;
803
+ return MONO_EXTENSIONS . Any ( path . EndsWith ) ;
826
804
}
827
805
828
- private static int getInt ( dynamic container , string propertyName , int dflt = 0 )
806
+ static int GetInt ( dynamic container , string propertyName , int dflt = 0 )
829
807
{
830
808
try {
831
809
return ( int ) container [ propertyName ] ;
@@ -836,7 +814,7 @@ private static int getInt(dynamic container, string propertyName, int dflt = 0)
836
814
return dflt ;
837
815
}
838
816
839
- private static string getString ( dynamic args , string property , string dflt = null )
817
+ static string GetString ( dynamic args , string property , string dflt = null )
840
818
{
841
819
var s = ( string ) args [ property ] ;
842
820
if ( s == null ) {
@@ -855,8 +833,8 @@ static SourceBreakpoint[] getBreakpoints(dynamic args, string property)
855
833
var breakpoints = jsonBreakpoints . ToObject < SourceBreakpoint [ ] > ( ) ;
856
834
return breakpoints ?? new SourceBreakpoint [ 0 ] ;
857
835
}
858
-
859
- private void DebuggerKill ( )
836
+
837
+ void DebuggerKill ( )
860
838
{
861
839
lock ( m_Lock ) {
862
840
if ( m_Session != null ) {
@@ -873,4 +851,3 @@ private void DebuggerKill()
873
851
}
874
852
}
875
853
}
876
-
0 commit comments