Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit e523734

Browse files
committedJul 2, 2018
Fix missing EditorInstance.json will result in error and warnings and
the user will be told to use the command palette instead. Cleanup of code style issues in UnityDebugSession
1 parent c79d8b9 commit e523734

File tree

1 file changed

+87
-110
lines changed

1 file changed

+87
-110
lines changed
 

‎UnityDebug/UnityDebugSession.cs

+87-110
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,39 @@ namespace UnityDebug
3737
{
3838
internal class UnityDebugSession : DebugSession
3939
{
40-
private const string MONO = "mono";
41-
private readonly string[] MONO_EXTENSIONS = new String[] {
40+
readonly string[] MONO_EXTENSIONS = {
4241
".cs", ".csx",
4342
".cake",
4443
".fs", ".fsi", ".ml", ".mli", ".fsx", ".fsscript",
4544
".hx"
4645
};
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;
5655
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;
6564
IUnityDbgConnector unityDebugConnector;
66-
67-
public UnityDebugSession() : base()
65+
66+
public UnityDebugSession()
6867
{
6968
m_ResumeEvent = new AutoResetEvent(false);
7069
m_Breakpoints = new SourceBreakpoint[0];
7170
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>();
7473

7574
m_DebuggerSessionOptions = new DebuggerSessionOptions {
7675
EvaluationOptions = EvaluationOptions.DefaultOptions
@@ -159,15 +158,15 @@ public UnityDebugSession() : base()
159158
};
160159

161160
m_Session.TargetThreadStarted += (sender, e) => {
162-
int tid = (int)e.Thread.Id;
161+
var tid = (int)e.Thread.Id;
163162
lock (m_SeenThreads) {
164163
m_SeenThreads[tid] = new Thread(tid, e.Thread.Name);
165164
}
166165
SendEvent(new ThreadEvent("started", tid));
167166
};
168167

169168
m_Session.TargetThreadStopped += (sender, e) => {
170-
int tid = (int)e.Thread.Id;
169+
var tid = (int)e.Thread.Id;
171170
lock (m_SeenThreads) {
172171
m_SeenThreads.Remove(tid);
173172
}
@@ -225,7 +224,7 @@ public override void Launch(Response response, dynamic args)
225224

226225
public override void Attach(Response response, dynamic args)
227226
{
228-
string name = getString (args, "name");
227+
string name = GetString (args, "name");
229228

230229
SetExceptionBreakpoints(args.__exceptionOptions);
231230

@@ -245,26 +244,22 @@ public override void Attach(Response response, dynamic args)
245244
}
246245
else
247246
{
248-
if (name.Contains("Editor"))
247+
if (!name.Contains("Editor"))
249248
{
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;
254251
}
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");
265252

253+
string pathToEditorInstanceJson = GetString(args, "path");
254+
if (!File.Exists(pathToEditorInstanceJson))
255+
{
256+
TooManyInstances(response, name, processes);
266257
return;
267258
}
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);
268263
}
269264

270265
var attachInfo = UnityProcessDiscovery.GetUnityAttachInfo (process.Id, ref unityDebugConnector);
@@ -275,7 +270,19 @@ public override void Attach(Response response, dynamic args)
275270
SendResponse(response);
276271
}
277272

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)
279286
{
280287
lock (m_Lock) {
281288

@@ -291,7 +298,7 @@ private void Connect(IPAddress address, int port)
291298
}
292299

293300
//---- private ------------------------------------------
294-
private void SetExceptionBreakpoints(dynamic exceptionOptions)
301+
void SetExceptionBreakpoints(dynamic exceptionOptions)
295302
{
296303
if (exceptionOptions == null)
297304
{
@@ -305,7 +312,7 @@ private void SetExceptionBreakpoints(dynamic exceptionOptions)
305312
m_Catchpoints.Clear();
306313

307314
var exceptions = exceptionOptions.ToObject<dynamic[]>();
308-
for (int i = 0; i < exceptions.Length; i++) {
315+
for (var i = 0; i < exceptions.Length; i++) {
309316

310317
var exception = exceptions[i];
311318

@@ -355,10 +362,6 @@ public override void Disconnect(Response response, dynamic args)
355362
public override void SetFunctionBreakpoints(Response response, dynamic arguments)
356363
{
357364
var breakpoints = new List<ResponseBreakpoint>();
358-
foreach (var breakpoint in arguments.breakpoints)
359-
{
360-
//new ResponseBreakpoint(true,);
361-
}
362365
SendResponse(response, new SetFunctionBreakpointsResponse(breakpoints));
363366
}
364367

@@ -374,7 +377,7 @@ public override void Continue(Response response, dynamic args)
374377
}
375378
}
376379

377-
private void WaitForSuspend()
380+
void WaitForSuspend()
378381
{
379382
if (!m_DebuggeeExecuting) return;
380383

@@ -424,7 +427,7 @@ public override void Pause(Response response, dynamic args)
424427
PauseDebugger();
425428
}
426429

427-
private void PauseDebugger()
430+
void PauseDebugger()
428431
{
429432
lock (m_Lock) {
430433
if (m_Session != null && m_Session.IsRunning)
@@ -434,14 +437,13 @@ private void PauseDebugger()
434437

435438
protected override void SetVariable(Response response, object args)
436439
{
437-
int reference = getInt(args, "variablesReference", -1);
440+
var reference = GetInt(args, "variablesReference", -1);
438441
if (reference == -1) {
439442
SendErrorResponse(response, 3009, "variables: property 'variablesReference' is missing", null, false, true);
440443
return;
441444
}
442445

443-
string value = getString(args, "value");
444-
string type = "";
446+
var value = GetString(args, "value");
445447
if (m_VariableHandles.TryGet(reference, out var children)) {
446448
if (children != null && children.Length > 0) {
447449
if (children.Length > MAX_CHILDREN) {
@@ -453,7 +455,7 @@ protected override void SetVariable(Response response, object args)
453455
continue;
454456
v.WaitHandle.WaitOne();
455457
var variable = CreateVariable(v);
456-
if (variable.name == getString(args, "name"))
458+
if (variable.name == GetString(args, "name"))
457459
{
458460
v.Value = value;
459461
SendResponse(response, new SetVariablesResponseBody(value, variable.type, variable.variablesReference));
@@ -465,10 +467,6 @@ protected override void SetVariable(Response response, object args)
465467

466468
public override void SetExceptionBreakpoints(Response response, dynamic args)
467469
{
468-
if (args["filters"].Contains("all_exceptions"))
469-
{
470-
471-
}
472470
SetExceptionBreakpoints(args.exceptionOptions);
473471
SendResponse(response);
474472
}
@@ -528,8 +526,8 @@ public override void SetBreakpoints(Response response, dynamic args)
528526

529527
public override void StackTrace(Response response, dynamic args)
530528
{
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);
533531

534532
WaitForSuspend();
535533

@@ -543,7 +541,7 @@ public override void StackTrace(Response response, dynamic args)
543541
}
544542

545543
var stackFrames = new List<VSCodeDebug.StackFrame>();
546-
int totalFrames = 0;
544+
var totalFrames = 0;
547545

548546
var bt = thread.Backtrace;
549547
if (bt != null && bt.FrameCount >= 0) {
@@ -580,10 +578,10 @@ public override void StackTrace(Response response, dynamic args)
580578
SendResponse(response, new StackTraceResponseBody(stackFrames, totalFrames));
581579
}
582580

583-
private ThreadInfo DebuggerActiveThread()
581+
ThreadInfo DebuggerActiveThread()
584582
{
585583
lock (m_Lock) {
586-
return m_Session == null ? null : m_Session.ActiveThread;
584+
return m_Session?.ActiveThread;
587585
}
588586
}
589587

@@ -593,13 +591,13 @@ public override void Source(Response response, dynamic arguments) {
593591

594592
public override void Scopes(Response response, dynamic args) {
595593

596-
int frameId = getInt(args, "frameId", 0);
594+
int frameId = GetInt(args, "frameId", 0);
597595
var frame = m_FrameHandles.Get(frameId, null);
598596

599597
var scopes = new List<Scope>();
600598

601599
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 })));
603601
}
604602

605603
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) {
612610

613611
public override void Variables(Response response, dynamic args)
614612
{
615-
int reference = getInt(args, "variablesReference", -1);
613+
int reference = GetInt(args, "variablesReference", -1);
616614
if (reference == -1) {
617615
SendErrorResponse(response, 3009, "variables: property 'variablesReference' is missing", null, false, true);
618616
return;
@@ -621,24 +619,20 @@ public override void Variables(Response response, dynamic args)
621619
WaitForSuspend();
622620
var variables = new List<Variable>();
623621

624-
ObjectValue[] children;
625-
if (m_VariableHandles.TryGet(reference, out children)) {
622+
if (m_VariableHandles.TryGet(reference, out var children)) {
626623
if (children != null && children.Length > 0) {
627624

628-
bool more = false;
625+
var more = false;
629626
if (children.Length > MAX_CHILDREN) {
630627
children = children.Take(MAX_CHILDREN).ToArray();
631628
more = true;
632629
}
633630

634-
if (children.Length < 20) {
631+
if (children.Length < 20)
632+
{
635633
// Wait for all values at once.
636634
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));
642636
}
643637
else {
644638
foreach (var v in children) {
@@ -678,7 +672,7 @@ public override void Threads(Response response, dynamic args)
678672

679673
public override void Evaluate(Response response, dynamic args)
680674
{
681-
var expression = getString(args, "expression");
675+
var expression = GetString(args, "expression");
682676

683677
if (expression == null) {
684678
SendError(response, "expression missing");
@@ -722,7 +716,7 @@ public override void Evaluate(Response response, dynamic args)
722716
SendError(response, "not available");
723717
return;
724718
}
725-
719+
726720
int handle = 0;
727721
if (val.HasChildren)
728722
{
@@ -739,28 +733,28 @@ void SendError(Response response, string error)
739733

740734
//---- private ------------------------------------------
741735

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)) {
744738
if (data[data.Length-1] != '\n') {
745739
data += '\n';
746740
}
747741
SendEvent(new OutputEvent(category, data));
748742
}
749743
}
750744

751-
private void Terminate(string reason) {
745+
void Terminate(string reason) {
752746
if (!m_Terminated) {
753747
SendEvent(new TerminatedEvent());
754748
m_Terminated = true;
755749
}
756750
}
757751

758-
private StoppedEvent CreateStoppedEvent(string reason, ThreadInfo ti, string text = null)
752+
StoppedEvent CreateStoppedEvent(string reason, ThreadInfo ti, string text = null)
759753
{
760754
return new StoppedEvent((int)ti.Id, reason, text);
761755
}
762756

763-
private ThreadInfo FindThread(int threadReference)
757+
ThreadInfo FindThread(int threadReference)
764758
{
765759
if (m_ActiveProcess != null) {
766760
foreach (var t in m_ActiveProcess.GetThreads()) {
@@ -772,7 +766,7 @@ private ThreadInfo FindThread(int threadReference)
772766
return null;
773767
}
774768

775-
private void Stopped()
769+
void Stopped()
776770
{
777771
m_Exception = null;
778772
m_VariableHandles.Reset();
@@ -785,7 +779,7 @@ private void Stopped()
785779
return new Variable(pname, v.DisplayValue, v.HasChildren ? _variableHandles.Create(v.GetAllChildren()) : 0);
786780
}*/
787781

788-
private Variable CreateVariable(ObjectValue v)
782+
Variable CreateVariable(ObjectValue v)
789783
{
790784
var dv = v.DisplayValue;
791785
if (dv.Length > 1 && dv [0] == '{' && dv [dv.Length - 1] == '}') {
@@ -794,38 +788,22 @@ private Variable CreateVariable(ObjectValue v)
794788
return new Variable(v.Name, dv, v.TypeName, v.HasChildren ? m_VariableHandles.Create(v.GetAllChildren()) : 0);
795789
}
796790

797-
private Backtrace DebuggerActiveBacktrace() {
791+
Backtrace DebuggerActiveBacktrace() {
798792
var thr = DebuggerActiveThread();
799793
return thr == null ? null : thr.Backtrace;
800794
}
801795

802-
private ExceptionInfo DebuggerActiveException() {
796+
ExceptionInfo DebuggerActiveException() {
803797
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();
815799
}
816800

817-
private static bool getBool(dynamic container, string propertyName, bool dflt = false)
801+
bool HasMonoExtension(string path)
818802
{
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);
826804
}
827805

828-
private static int getInt(dynamic container, string propertyName, int dflt = 0)
806+
static int GetInt(dynamic container, string propertyName, int dflt = 0)
829807
{
830808
try {
831809
return (int)container[propertyName];
@@ -836,7 +814,7 @@ private static int getInt(dynamic container, string propertyName, int dflt = 0)
836814
return dflt;
837815
}
838816

839-
private static string getString(dynamic args, string property, string dflt = null)
817+
static string GetString(dynamic args, string property, string dflt = null)
840818
{
841819
var s = (string)args[property];
842820
if (s == null) {
@@ -855,8 +833,8 @@ static SourceBreakpoint[] getBreakpoints(dynamic args, string property)
855833
var breakpoints = jsonBreakpoints.ToObject<SourceBreakpoint[]>();
856834
return breakpoints ?? new SourceBreakpoint[0];
857835
}
858-
859-
private void DebuggerKill()
836+
837+
void DebuggerKill()
860838
{
861839
lock (m_Lock) {
862840
if (m_Session != null) {
@@ -873,4 +851,3 @@ private void DebuggerKill()
873851
}
874852
}
875853
}
876-

0 commit comments

Comments
 (0)
This repository has been archived.