Skip to content

Instantly share code, notes, and snippets.

@jbevain
Last active October 23, 2022 15:23
Show Gist options
  • Save jbevain/37d5b1ad0cd3f0ff0eec223df6f28fd8 to your computer and use it in GitHub Desktop.
Save jbevain/37d5b1ad0cd3f0ff0eec223df6f28fd8 to your computer and use it in GitHub Desktop.
using System.Net;
using Mono.Debugger.Soft;
using static System.Console;
static int PidToPort(int pid) => 56000 + (pid % 1000);
var pid = 49149; // process ID of the Unity Editor or the IL2CPP Game
var ip = IPAddress.Loopback; // IP where the Unity Editor/Game is running
var endPoint = new IPEndPoint(ip, PidToPort(pid));
var vm = VirtualMachineManager.Connect(endPoint);
var exceptionRequest = vm.CreateExceptionRequest(exc_type: null, caught: true, uncaught: true);
exceptionRequest.Enable();
var pumpSignal = true;
var eventPump = new Thread(() =>
{
while (pumpSignal)
{
var eventSet = vm.GetNextEventSet();
if (eventSet[0] is ExceptionEvent exceptionEvent)
{
WriteLine($"Exception: {exceptionEvent.Exception.Type.FullName}");
WriteLine($"StackTrace:");
foreach (var frame in exceptionEvent.Thread.GetFrames())
{
WriteLine(frame.Location);
}
vm.Resume();
}
}
});
eventPump.Start();
WriteLine("Press enter to quit");
ReadLine();
pumpSignal = false;
vm.Detach();
WriteLine("Bye now");
@jbevain
Copy link
Author

jbevain commented Oct 22, 2022

Compile against the Mono.Debugger.Soft.dll assembly that comes with your Mono installation. Unity ships with a local Mono installation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment