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

Commit 20daa3b

Browse files
committed
Improve attaching to unity processes.
Updated regex to support PS4, Xbox One, and Switch. Added additional log outputs for locating issues in future releases.
1 parent 43bf68a commit 20daa3b

File tree

5 files changed

+103
-74
lines changed

5 files changed

+103
-74
lines changed

MonoDebug

UnityDebug/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,19 @@ static void Main(string[] argv)
6868
{
6969
RunSession(Console.OpenStandardInput(), Console.OpenStandardOutput());
7070
}
71-
catch(Exception e)
71+
catch(Exception e)
7272
{
7373
Log.Write ("Exception: " + e);
7474
}
7575
}
7676

7777
static void RunSession(Stream inputStream, Stream outputStream)
7878
{
79+
Log.Write("Running session");
7980
DebugSession debugSession = new UnityDebugSession();
8081
DebuggerLoggingService.CustomLogger = new CustomLogger();
8182
debugSession.Start(inputStream, outputStream).Wait();
83+
Log.Write("Session Terminated");
8284
}
8385

8486
static string GetUnityProcesses()

UnityDebug/UnityAttach.cs

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,51 @@
66

77
namespace UnityDebug
88
{
9-
public static class UnityAttach
10-
{
11-
static readonly Dictionary<string, string> targetNameToProcessName = new Dictionary<string,string>
12-
{
13-
{ "unity editor", "Unity Editor" },
14-
{ "osx player", "OSXPlayer" },
15-
{ "windows player", "WindowsPlayer" },
16-
{ "linux player", "LinuxPlayer" },
17-
{ "ios player", "iPhonePlayer" },
18-
{ "android player", "AndroidPlayer" }
19-
};
20-
21-
22-
public static IEnumerable<UnityProcessInfo> GetAttachableProcesses(string targetName)
23-
{
24-
var match = Regex.Match(targetName, "\\(([0-9]+)\\)");
25-
var processId = -1;
26-
if (match.Success)
27-
{
28-
processId = Convert.ToInt32(match.Groups[1].Value);
29-
targetName = targetName.Substring(0, targetName.IndexOf("(") - 1);
30-
}
31-
string processName;
32-
33-
UnityProcessDiscovery.GetProcessOptions options = UnityProcessDiscovery.GetProcessOptions.All;
34-
35-
if (!targetNameToProcessName.TryGetValue (targetName.ToLower (), out processName)) {
36-
processName = targetName;
37-
} else {
38-
if (processName == "Unity Editor")
39-
options = UnityProcessDiscovery.GetProcessOptions.Editor;
40-
else
41-
options = UnityProcessDiscovery.GetProcessOptions.Players;
42-
}
43-
44-
var processes = UnityProcessDiscovery.GetAttachableProcesses (options);
45-
46-
processes.ForEach (p => Log.Write ("Found Unity process: " + p.Name + " (" + p.Id + ")"));
47-
48-
return processId == -1
49-
? processes.Where (p => p.Name.Contains(processName))
50-
: processes.Where(p => p.Name.Contains(processName) && p.Id == processId);
51-
}
52-
}
9+
public static class UnityAttach
10+
{
11+
static readonly Dictionary<string, string> targetNameToProcessName = new Dictionary<string, string>
12+
{
13+
{ "unity editor", "Unity Editor" },
14+
{ "osx player", "OSXPlayer" },
15+
{ "windows player", "WindowsPlayer" },
16+
{ "linux player", "LinuxPlayer" },
17+
{ "ios player", "iPhonePlayer" },
18+
{ "android player", "AndroidPlayer" },
19+
{ "ps4 player", "PS4Player" },
20+
{ "xbox one player", "XboxOnePlayer" },
21+
{ "switch player", "SwitchPlayer" },
22+
};
23+
24+
public static IEnumerable<UnityProcessInfo> GetAttachableProcesses(string targetName)
25+
{
26+
var match = Regex.Match(targetName, "\\(([0-9]+)\\)");
27+
var processId = -1;
28+
if (match.Success)
29+
{
30+
processId = Convert.ToInt32(match.Groups[1].Value);
31+
targetName = targetName.Substring(0, targetName.IndexOf("(") - 1);
32+
}
33+
34+
UnityProcessDiscovery.GetProcessOptions options = UnityProcessDiscovery.GetProcessOptions.All;
35+
36+
if (!targetNameToProcessName.TryGetValue(targetName.ToLower(), out var processName))
37+
{
38+
processName = targetName;
39+
}
40+
else
41+
{
42+
options = processName == "Unity Editor"
43+
? UnityProcessDiscovery.GetProcessOptions.Editor
44+
: UnityProcessDiscovery.GetProcessOptions.Players;
45+
}
46+
47+
var processes = UnityProcessDiscovery.GetAttachableProcesses(options);
48+
49+
processes.ForEach(p => Log.Write("Found Unity process: " + p.Name + " (" + p.Id + ")"));
50+
51+
return processId == -1
52+
? processes.Where(p => p.Name.Contains(processName))
53+
: processes.Where(p => p.Name.Contains(processName) && p.Id == processId);
54+
}
55+
}
5356
}
54-

UnityDebug/UnityDebugSession.cs

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ internal class UnityDebugSession : DebugSession
6565

6666
public UnityDebugSession()
6767
{
68+
Log.Write("Constructing UnityDebugSession");
6869
m_ResumeEvent = new AutoResetEvent(false);
6970
m_Breakpoints = new SourceBreakpoint[0];
7071
m_VariableHandles = new Handles<ObjectValue[]>();
@@ -176,6 +177,8 @@ public UnityDebugSession()
176177
m_Session.OutputWriter = (isStdErr, text) => {
177178
SendOutput(isStdErr ? "stderr" : "stdout", text);
178179
};
180+
181+
Log.Write("Done constructing UnityDebugSession");
179182
}
180183

181184
public StackFrame Frame { get; set; }
@@ -224,15 +227,18 @@ public override void Launch(Response response, dynamic args)
224227

225228
public override void Attach(Response response, dynamic args)
226229
{
230+
Log.Write($"UnityDebug: Attach: {response} ; {args}");
227231
string name = GetString (args, "name");
228232

229233
SetExceptionBreakpoints(args.__exceptionOptions);
230234

235+
Log.Write($"UnityDebug: Searching for Unity process '{name}'");
231236
SendOutput("stdout", "UnityDebug: Searching for Unity process '" + name + "'");
232237

233238
var processes = UnityAttach.GetAttachableProcesses(name).ToArray ();
234239

235240
if (processes.Length == 0) {
241+
Log.Write($"Could not find target name '{name}'.");
236242
SendErrorResponse (response, 8001, "Could not find target name '{_name}'. Is it running?", new { _name = name});
237243
return;
238244
}
@@ -266,24 +272,31 @@ public override void Attach(Response response, dynamic args)
266272

267273
Connect(attachInfo.Address, attachInfo.Port);
268274

275+
Log.Write($"UnityDebug: Attached to Unity process '{process.Name}' ({process.Id})");
269276
SendOutput("stdout", "UnityDebug: Attached to Unity process '" + process.Name + "' (" + process.Id + ")\n");
270277
SendResponse(response);
271278
}
272279

273280
void TooManyInstances(Response response, string name, UnityProcessInfo[] processes)
274281
{
282+
Log.Write($"Multiple targets with name '{name}' running. Unable to connect.");
275283
SendErrorResponse(response, 8002, "Multiple targets with name '{_name}' running. Unable to connect.\n" +
276284
"Use \"Unity Attach Debugger\" from the command palette (View > Command Palette...) to specify which process to attach to.", new { _name = name });
277285

286+
Log.Write($"UnityDebug: Multiple targets with name '{name}' running. Unable to connect.)");
278287
SendOutput("stdout", "UnityDebug: Multiple targets with name '" + name + "' running. Unable to connect.\n" +
279288
"Use \"Unity Attach Debugger\" from the command palette (View > Command Palette...) to specify which process to attach to.");
280289

281290
foreach (var p in processes)
291+
{
292+
Log.Write($"UnityDebug: Found Unity process '{p.Name}' ({p.Id})");
282293
SendOutput("stdout", "UnityDebug: Found Unity process '" + p.Name + "' (" + p.Id + ")\n");
294+
}
283295
}
284296

285297
void Connect(IPAddress address, int port)
286298
{
299+
Log.Write($"UnityDebug: Connect to: {address}:{port}");
287300
lock (m_Lock) {
288301

289302
var args0 = new SoftDebuggerConnectArgs(string.Empty, address, port) {
@@ -300,6 +313,7 @@ void Connect(IPAddress address, int port)
300313
//---- private ------------------------------------------
301314
void SetExceptionBreakpoints(dynamic exceptionOptions)
302315
{
316+
Log.Write($"UnityDebug: SetExceptionBreakpoints: {exceptionOptions}");
303317
if (exceptionOptions == null)
304318
{
305319
return;
@@ -338,6 +352,8 @@ void SetExceptionBreakpoints(dynamic exceptionOptions)
338352

339353
public override void Disconnect(Response response, dynamic args)
340354
{
355+
Log.Write($"UnityDebug: Disconnect: {args}");
356+
Log.Write($"UnityDebug: Disconnect: {response}");
341357
if (unityDebugConnector != null) {
342358
unityDebugConnector.OnDisconnect ();
343359
unityDebugConnector = null;
@@ -361,12 +377,14 @@ public override void Disconnect(Response response, dynamic args)
361377

362378
public override void SetFunctionBreakpoints(Response response, dynamic arguments)
363379
{
380+
Log.Write($"UnityDebug: SetFunctionBreakpoints: {response} ; {arguments}");
364381
var breakpoints = new List<ResponseBreakpoint>();
365382
SendResponse(response, new SetFunctionBreakpointsResponse(breakpoints));
366383
}
367384

368-
public override void Continue(Response response, dynamic args)
385+
public override void Continue(Response response, dynamic arguments)
369386
{
387+
Log.Write($"UnityDebug: Continue: {response} ; {arguments}");
370388
WaitForSuspend();
371389
SendResponse(response, new ContinueResponseBody());
372390
lock (m_Lock) {
@@ -377,16 +395,9 @@ public override void Continue(Response response, dynamic args)
377395
}
378396
}
379397

380-
void WaitForSuspend()
381-
{
382-
if (!m_DebuggeeExecuting) return;
383-
384-
m_ResumeEvent.WaitOne();
385-
m_DebuggeeExecuting = false;
386-
}
387-
388-
public override void Next(Response response, dynamic args)
398+
public override void Next(Response response, dynamic arguments)
389399
{
400+
Log.Write($"UnityDebug: Next: {response} ; {arguments}");
390401
WaitForSuspend();
391402
SendResponse(response);
392403
lock (m_Lock) {
@@ -397,8 +408,9 @@ public override void Next(Response response, dynamic args)
397408
}
398409
}
399410

400-
public override void StepIn(Response response, dynamic args)
411+
public override void StepIn(Response response, dynamic arguments)
401412
{
413+
Log.Write($"UnityDebug: StepIn: {response} ; {arguments}");
402414
WaitForSuspend();
403415
SendResponse(response);
404416
lock (m_Lock) {
@@ -409,8 +421,9 @@ public override void StepIn(Response response, dynamic args)
409421
}
410422
}
411423

412-
public override void StepOut(Response response, dynamic args)
424+
public override void StepOut(Response response, dynamic arguments)
413425
{
426+
Log.Write($"UnityDebug: StepIn: {response} ; {arguments}");
414427
WaitForSuspend();
415428
SendResponse(response);
416429
lock (m_Lock) {
@@ -421,8 +434,9 @@ public override void StepOut(Response response, dynamic args)
421434
}
422435
}
423436

424-
public override void Pause(Response response, dynamic args)
437+
public override void Pause(Response response, dynamic arguments)
425438
{
439+
Log.Write($"UnityDebug: StepIn: {response} ; {arguments}");
426440
SendResponse(response);
427441
PauseDebugger();
428442
}
@@ -435,15 +449,15 @@ void PauseDebugger()
435449
}
436450
}
437451

438-
protected override void SetVariable(Response response, object args)
452+
protected override void SetVariable(Response response, object arguments)
439453
{
440-
var reference = GetInt(args, "variablesReference", -1);
454+
var reference = GetInt(arguments, "variablesReference", -1);
441455
if (reference == -1) {
442456
SendErrorResponse(response, 3009, "variables: property 'variablesReference' is missing", null, false, true);
443457
return;
444458
}
445459

446-
var value = GetString(args, "value");
460+
var value = GetString(arguments, "value");
447461
if (m_VariableHandles.TryGet(reference, out var children)) {
448462
if (children != null && children.Length > 0) {
449463
if (children.Length > MAX_CHILDREN) {
@@ -455,7 +469,7 @@ protected override void SetVariable(Response response, object args)
455469
continue;
456470
v.WaitHandle.WaitOne();
457471
var variable = CreateVariable(v);
458-
if (variable.name == GetString(args, "name"))
472+
if (variable.name == GetString(arguments, "name"))
459473
{
460474
v.Value = value;
461475
SendResponse(response, new SetVariablesResponseBody(value, variable.type, variable.variablesReference));
@@ -465,17 +479,19 @@ protected override void SetVariable(Response response, object args)
465479
}
466480
}
467481

468-
public override void SetExceptionBreakpoints(Response response, dynamic args)
482+
public override void SetExceptionBreakpoints(Response response, dynamic arguments)
469483
{
470-
SetExceptionBreakpoints(args.exceptionOptions);
484+
Log.Write($"UnityDebug: StepIn: {response} ; {arguments}");
485+
SetExceptionBreakpoints(arguments.exceptionOptions);
471486
SendResponse(response);
472487
}
473488

474-
public override void SetBreakpoints(Response response, dynamic args)
489+
public override void SetBreakpoints(Response response, dynamic arguments)
475490
{
491+
Log.Write($"UnityDebug: SetBreakpoints: {response} ; {arguments}");
476492
string path = null;
477-
if (args.source != null) {
478-
var p = (string)args.source.path;
493+
if (arguments.source != null) {
494+
var p = (string)arguments.source.path;
479495
if (p != null && p.Trim().Length > 0) {
480496
path = p;
481497
}
@@ -491,7 +507,7 @@ public override void SetBreakpoints(Response response, dynamic args)
491507
return;
492508
}
493509

494-
SourceBreakpoint[] newBreakpoints = getBreakpoints(args, "breakpoints");
510+
SourceBreakpoint[] newBreakpoints = getBreakpoints(arguments, "breakpoints");
495511
var lines = newBreakpoints.Select(bp => bp.line);
496512
var breakpointsToRemove = m_Breakpoints.Where(bp => !lines.Contains(bp.line)).ToArray();
497513
foreach (Breakpoint breakpoint in m_Session.Breakpoints.GetBreakpoints())
@@ -524,10 +540,11 @@ public override void SetBreakpoints(Response response, dynamic args)
524540
SendResponse(response, new SetBreakpointsResponseBody(responseBreakpoints));
525541
}
526542

527-
public override void StackTrace(Response response, dynamic args)
543+
public override void StackTrace(Response response, dynamic arguments)
528544
{
529-
int maxLevels = GetInt(args, "levels", 10);
530-
int threadReference = GetInt(args, "threadId", 0);
545+
Log.Write($"UnityDebug: StackTrace: {response} ; {arguments}");
546+
int maxLevels = GetInt(arguments, "levels", 10);
547+
int threadReference = GetInt(arguments, "threadId", 0);
531548

532549
WaitForSuspend();
533550

@@ -849,5 +866,13 @@ void DebuggerKill()
849866
}
850867
}
851868
}
869+
870+
void WaitForSuspend()
871+
{
872+
if (!m_DebuggeeExecuting) return;
873+
874+
m_ResumeEvent.WaitOne();
875+
m_DebuggeeExecuting = false;
876+
}
852877
}
853878
}

0 commit comments

Comments
 (0)