0

I am currently experimenting with Visual Studio Extensions. I need to subscribe to an event that gets called before the actual Debugger is stopped. Basically I am just Attaching to Managed Process (not running via F5). The problem is that Stop Debugging simply "detaches" the process, and the process continues running after that. I plan to use this event to notify our process to exit

I have a class that implements IDebugEventCallback2, IVsDebuggerEvents and IVsDebugProcessNotify.

class MyDebugger : IDebugEventCallback2, IVsDebuggerEvents, IVsDebugProcessNotify

Inside this class, there is a member that subscribes to Debugger Events using IVsDebugger's AdviseDebugEventCallback() and AdviseDebuggerEvents() events.

_debugger = Package.GetGlobalService(typeof(SVsShellDebugger)) as IVsDebugger;
if (_debugger != null)
{
    _debugger.AdviseDebugEventCallback(this);
    _debugger.AdviseDebuggerEvents(this, out _debuggerEventsCookie);
}

I noticed however that the events fired from AdviseDebugEventCallback's Event() handler does not always gets called before the actual Stop Debugging (next few lines after the breakpoint are still executed after I clicked Stopped Debugging). Around 4 or out of 5 times, the event from IDebugCustomEvent110 (riidEvent of 2615D9BC-1948-4D21-81EE-7A963F20CF59) gets called before any line from the attached process gets further executed. I still have to digest the details of the events fired in the Event() handler, but looking at the breakpoints, seems like I could not rely on this as it only works as per my expectation around 4 out of 5 times.

I am currently looking at the BeforeStopDebuggingProcess() method inside IVsDebugProcessNotify. However, I don't know how to "Subscribe" or "Advice" from this interface. Any advice how? There isn't much Google result about this topic. Thank you!

remondo
  • 318
  • 2
  • 7

1 Answers1

0

I found something hacky, please comment if this is recommended or not. I got some hint about the CommandEvents from this post: How do I know from my VSIX that a build will be followed by a Debug session?

First, I subscribed to the commandEvents of DTE.

DTE dte = await serviceProvider.GetServiceAsync(typeof(DTE)) as DTE;
if (dte != null)
{
    events = dte.Events;
    commandEvents = events.CommandEvents;
    commandEvents.BeforeExecute += OnBeforeExecute;
}

Then, inside OnBeforeExecute, I am hardcoding this particular GUID and ID which I observed to be fired whenever Stop Debugging is clicked (amongst many other events). If I put Thread.Sleep() of 30 seconds inside this handler, the Stop Button of the Visual Studio freezes for 30 seconds (eventually the entire Visual Studio) :-) The code will resume after 30 seconds of wait.

private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
    if (Guid == "{5EFC7975-14BC-11CF-9B2B-00AA00573819}" && ID == 179)
    {
        // Stop command is detected
        Trace.WriteLine($"[OnBeforeExecute] {Guid} --- {ID} Stop Command Detected");
        System.Threading.Thread.Sleep(30000);
    }
}
remondo
  • 318
  • 2
  • 7
  • 1
    You could also use `EnvDTE.DebuggerEvents.OnEnterDesignMode`, though I don't know if it fires too late for what you're trying to accomplish. – Cameron Jul 22 '21 at 20:03
  • @Cameron thank you for answer. OnEnterDesignMode fires too late for my scenario though. I just used the workaround I posted above. – remondo Aug 07 '21 at 13:01