Skip to content

Commit

Permalink
Add /waitfordebugger argument to MGCB. (#8227)
Browse files Browse the repository at this point in the history
* Add `/waitfordebugger` argument to MGCB.

The call to `System.Diagnostics.Debugger.Launch` is
not supported on Unix based platforms. This makes
debugging `MGCB.exe` and Content Pipeline extensions
difficult.

So lets add a new argument which will make `MGCB.exe`
wait for a debugger to attach. Also add a extra
update to the `launch.json` to demonstrate how to
attach the `dotnet` debugger to an existing process.

This new system will also work on Windows.

To use this in VSCode you can add the following to the
`launch.json`

```json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Process",
            "type": "coreclr",
            "request": "attach",
            "processId": "${input.processid}",
        }
    ],
    "inputs": [
        {
            "id": "processid",
            "type": "promptString",
            "default": "0",
            "description": "Enter Process Id of process to attach to."
        }
    ]
}
```

you can then add the following to your `csproj`.

```
<MonoGameMGCBAdditionalArguments>/waitfordebugger</MonoGameMGCBAdditionalArguments>
```

This will cause the `MGCB.exe` to print the ProcessId
to the console during the build and wait for the debugger
to attach. Once you have the ProcessId you can run the
VSCode `Attach to Process` and enter the ProcessId when prompted.
The debugger should then attach.

If you want to prcess without attaching the debugger just press any
key in the console or terminal windows where `MGCB.exe` is running.
  • Loading branch information
dellis1972 committed Jun 10, 2024
1 parent 3159daa commit b1244f1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
"cwd": "${workspaceFolder}/Artifacts/MonoGame.Content.Builder.Editor/Mac/Debug",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": "Attach to Process",
"type": "coreclr",
"request": "attach",
"processId": "${input.processid}",
}
],
"inputs": [
{
"id": "processid",
"type": "promptString",
"default": "0",
"description": "Enter Process Id of process to attach to."
}
]
}
32 changes: 22 additions & 10 deletions Tools/MonoGame.Content.Builder/BuildContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class BuildContent
[CommandLineParameter(
Name = "launchdebugger",
Flag = "d",
Description = "Wait for debugger to attach before building content.")]
Description = "Launch the debugger before building content.")]
public bool LaunchDebugger = false;

[CommandLineParameter(
Name = "waitfordebugger",
Flag = "a",
Description = "Wait for debugger to attach before building content.")]
public bool WaitForDebuggerToAttach = false;

[CommandLineParameter(
Name = "quiet",
Flag = "q",
Expand Down Expand Up @@ -299,7 +305,7 @@ public void Build(out int successCount, out int errorCount)

// If the intent is to debug build, break at the original location
// of any exception, eg, within the actual importer/processor.
if (LaunchDebugger)
if (LaunchDebugger || WaitForDebuggerToAttach)
_manager.RethrowExceptions = false;

// Feed all the assembly references to the pipeline manager
Expand Down Expand Up @@ -450,10 +456,13 @@ public void Build(out int successCount, out int errorCount)
var dstTime = File.GetLastWriteTimeUtc(dest);
if (srcTime <= dstTime)
{
if (string.IsNullOrEmpty(c.Link))
Console.WriteLine("Skipping {0}", c.SourceFile);
else
Console.WriteLine("Skipping {0} => {1}", c.SourceFile, c.Link);
if (!Quiet)
{
if (string.IsNullOrEmpty(c.Link))
Console.WriteLine("Skipping {0}", c.SourceFile);
else
Console.WriteLine("Skipping {0} => {1}", c.SourceFile, c.Link);
}

// Copy the stats from the previous stats collection.
_manager.ContentStats.CopyPreviousStats(c.SourceFile);
Expand All @@ -477,10 +486,13 @@ public void Build(out int successCount, out int errorCount)

var buildTime = DateTime.UtcNow - startTime;

if (string.IsNullOrEmpty(c.Link))
Console.WriteLine("{0}", c.SourceFile);
else
Console.WriteLine("{0} => {1}", c.SourceFile, c.Link);
if (!Quiet)
{
if (string.IsNullOrEmpty(c.Link))
Console.WriteLine("{0}", c.SourceFile);
else
Console.WriteLine("{0} => {1}", c.SourceFile, c.Link);
}

// Record content stats on the copy.
_manager.ContentStats.RecordStats(c.SourceFile, dest, "CopyItem", typeof(File), (float)buildTime.TotalSeconds);
Expand Down
15 changes: 15 additions & 0 deletions Tools/MonoGame.Content.Builder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -45,6 +47,19 @@ static int Main(string[] args)
Console.Error.WriteLine("The debugger is not implemented under Mono and thus is not supported on your platform.");
}
}
if (content.WaitForDebuggerToAttach)
{
var currentProcess = Process.GetCurrentProcess();
Console.WriteLine($"Waiting for debugger to attach ({currentProcess.MainModule.FileName} PID {currentProcess.Id}). Press any key to continue without debugger.");
while (!Debugger.IsAttached)
{
if (Console.KeyAvailable)
{
break;
}
Thread.Sleep(100);
}
}

// Print a startup message.
var buildStarted = DateTime.Now;
Expand Down

0 comments on commit b1244f1

Please sign in to comment.