I am building an internal development tool to manage different processes commonly used in our development environment. The tool shows the list of the monitored processes, indicating their running state and allows to start or stop each process.
I'd like to add the functionality of attaching a debugger to a monitored process from my tool instead of going in Debug -> Attach to process in Visual Studio and finding the process.
My goal is to have something like Debugger.Launch()
that would show a list of the available Visual Studio. I can't use Debugger.Launch()
, because it launches the debugger on the process that makes the call. I would need something like Debugger.Launch(processId)
.
How do I achieve this functionality?
A solution could be to implement a command in each monitored process to call Debugger.Launch()
when the command is received from the monitoring tool, but I would prefer something that does not require to modify the code of the monitored processes.
Side question:
When using Debugger.Launch()
, instances of Visual Studio that already have a debugger attached are not listed. Visual Studio is not limited to one attached debugger, you can attach on multiple process when using Debug → Attach to process.
How do I bypass this limitation when using Debugger.Launch()
or an alternative?
A coworker ended up with a solution using DTE, and I posted the code on PasteBin.
The methods of interest are
AttachVisualStudioToProcess
andTryGetVsInstance
Source Code
WinDbg does the chain debugging for native code by default. If you want to launch another instance of Visual Studio, check Launch the Debugger Automatically on MSDN:
To automate the existing debugger, use Marshal.GetActiveObject to get the current EnvDTE.Debugger then let it attach to the process you just created.
Here is some information about how you can programmatically attach the debugger to multiple processes:
Attach to locally running processes
Attach to remotely running processes