I would like to know how vsjitdebugger.exe can cause a particular running instance of Visual Studio to be used to debug a process.
Background: currently vsjitdebugger.exe is set as the executable run whenever there is a "debug break" - i.e. in C# System.Diagnostics.Debugger.Launch();. Whenever this happens I see a dialog with a list of available debuggers - this includes all running instances of Visual Studio.
My question is how vsjitdebugger.exe activates a particular running instance of Visual Studio. Please note that I am particularly interested in existing running instances of VS and not in starting a new instance.
Maybe you can attach the process to vs debugger through VS sdk.
public static void Attach(DTE dte)
{
EnvDTE.Processes processes = dte.Debugger.LocalProcesses;
foreach(EnvDTE.Process proc in processes)
if(proc.Name.IndexOf("Target.exe") != -1)
proc.Attach();
}
The document: Process Interface
Related
As I understand it now, the only way to use the remote debugger is to start the target application, and then attach to it through Visual Studio. Is there a way to capture all of the breakpoints from the very beginning of the program?
There is code within my program that I need to debug, and I can never get the debugger attached fast enough to capture that executing code.
If you can change the code, try injecting this line of code in the starting point of your app:
System.Diagnostics.Debugger.Launch();
When this line is hit it will prompt you to attach a debugger, effectively waiting for you to respond. Since you are using a remote debugger you should be able to attach at that point and then just cancel the dialog. Hope this helps.
The solution
System.Diagnostics.Debugger.Launch
didn't work for me either. However, I managed to solve my problem writing in my application start up the following:
while (!System.Diagnostics.Debugger.IsAttached)
System.Threading.Thread.Sleep(100);
That way the application will be waiting until a debugger gets attached.
On the target machine set up the Visual Studio Remote Debugger that matches the year of Visual Studio on your local machine.
Note the line that gives you the server name.
On your local machine in visual studio, open the properties of your startup project and then open the debug section.
Check the box for "use remote machine" and then enter into the text field the server name you got from the Visual Studio Remote Debugger.
Under "Start Action", select "Start External Program". Then place into the field the path to the .exe you wish to start on your target machine.
Now when you press the start button from you local machine it will start the program on the target machine with the debugger attached.
With Visual Studio Pro 2010 building a .NET 4 application, this doesn't work for me.
Apparently this is a known bug:
https://connect.microsoft.com/VisualStudio/feedback/details/611486/debugger-launch-is-now-crashing-my-net-application-after-upgrading-to-net-4-0
A (somewhat hacky) workaround for the moment which is working for me is just to have the app throw up a MessageBox() right at the start of main window initialisation:
public partial class MainWindow : Form
{
public MainWindow()
{
// To allow you time to attach a remote debugger ...
MessageBox.Show("Please attach debugger");
InitializeComponent();
...
Now you can attach the VS remote debugger at your leisure, and then hit OK on the message box.
Ugly but functional.
The correct solution for me was a combination of the answers.
The while loop will check if the debugger is attached from Visual Studio and exit the loop when it is attached.
System.Diagnostics.Debugger.Launch();
while (!System.Diagnostics.Debugger.IsAttached)
{
System.Threading.Thread.Sleep(100);
}
I'm doing some visual studio extension development in Visual Studio 2010.
It would be useful to debug while developing so I have it configured to open another instance of VS when debugger for F5 ( http://donovanbrown.com/post/How-to-debug-a-Visual-Studio-Extension.aspx). This all works fine but is there a way to attach a debugger to an existing instance of VS2010, I have tried and the breakpoints aren't being hit. There are no errors but wondering if there is a way?
I should add I do know how to attach to a debugger and I have used it before to attach to ASP.net code.
Under Debug there is a item called Attach to Process. This will do exactly what you want it to do.
Use the Title column to tell which instance of devenv.exe you want to connect to (notice that I started the attach on BinaryFileSearch, but I am attaching to FixClientNoteRTF).
It does not let you attach to yourself because if you hit a breakepoint the UI would stop responding and how would you tell it to step or continue?
OK managed to solve it.
What I was doing was when opening an instance of Visual studio, following the usual method, i.e. open a normal instance ( devenv.exe).
What you have to is open a experiemental instance, using the parameters ( cmd line mode):
/rootsuffix Exp
Then use the attach to debugger mode to attach to this instance.
When running a process under the debugger, I would like to start a child process in the same debugger.
Currently, I use
Process.Start("sample.exe");
I want it to be something like this:
if (Debugger.IsAttached)
// start "sample.exe" in the same debugging session
else
Process.Start("sample.exe");
I could pass a flag to the child process that instructs it to call Debugger.Launch(), but that won't catch start up errors, and it results in a debugging session where some features are not enabled (such as edit and continue, etc). It's preferable to have the debugger launch the process directly.
You should attach debugger to process you are starting. This could be done:
Manually from Visual Studio after starting "sample.exe" select it menu Debug > Attach to process..
Programmatically attach debugger inside "sample.exe"
Attaching to a Process using VS.NET Automation Model
UPDATE: You can setup windows environment to attach debugger every time "sample.exe" starts: Launch the Debugger Automatically (you will need to call Debugger.Break anyway)
Some external tool maybe
Here is code for "sample.exe" to attach debugger:
if (!Debugger.IsAttached)
Debugger.Launch();
Debugger.Break();
You should pass some parameter to "sample.exe" to verify if you need to attach debugger.
Process.Start("sample.exe", "Debug=true");
you can change the properties of your solution to start multiple apps.
an explanation is here Run Multiple projects
The MSDN article is here MSDN Article on running multiple projects
Assuming that sample.exe would be the host for code you want to debug, you can use project properties -> debug settings - choose action as an external program and provide path to sample.exe. The VS will start this executable and attach the debugger to it.
Visual Studio Debugger Team created an extension that does that automagically:
https://marketplace.visualstudio.com/items?itemName=vsdbgplat.MicrosoftChildProcessDebuggingPowerTool
Under my solution I have 2 projects Project A console application , Project B Winforms Window.
How I can From Project B to start new process with console application from Project A ?
I tried to making it via
Process note = new Process();
note.StartInfo.FileName = "note.exe";
note.StartInfo.Arguments = "123";
note.Start();
But in that way i cant debug both processes from one vs instance . Any idea ?
Thanks
If the only reason that you want to start the two projects is that you want to be able to debug both of them you can also configure your solution to have multiple startup projects:
Solution -> Properties -> Set Startup Projects...
If you are going to start the other project as a separate project anyway, you could use
Debug -> Attach to Process
to attach to the newly started process. Then you will also be able to debug both processes in Visual Studio. Attaching the debugger can also be achieved programmatically. In project A you can add the following code to the Main method:
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
In the dialog that will then pop up you can select the already running instance of Visual Studio.
Another option is to start the Startup Project via F5 (or Debug > Start Debugging) and the second one by right-clicking the project and going Debug > Start New Instance.
You can start debugger in the application launched by the following statement
System.Diagnostics.Debugger.Break()
then you can choose the visual studio instance and start debugging.
Is there a way to write an application that can connect to a running instance of Visual Studio and issue commands to it? For example, could I write a WPF app with a button that, when clicked, issues a "Build.BuildSolution" command to an already-open instance of Visual Studio, causing it to start a build?
I'm sure I could use SendKeys to send Ctrl+Shift+B, but I want to know if there's a way to write to an actual API to automate Visual Studio, and invoke commands by name.
Here's a C# program that connects to a running Visual Studio and issues a Build command. The DTE.9 part means "Visual Studio 2008" - use DTE.8 for VS 2005, or DTE.10 for VS 2010.
using System;
using System.Runtime.InteropServices;
using EnvDTE80;
namespace SORemoteBuild
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Get an instance of the currently running Visual Studio IDE.
EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.9.0");
dte2.Solution.SolutionBuild.Build(true);
}
}
public class MessageFilter : IOleMessageFilter
{
// ... Continues at http://msdn.microsoft.com/en-us/library/ms228772.aspx
(The nonsense with STAThread and MessageFilter is "due to threading contention issues between external multi-threaded applications and Visual Studio", whatever that means. Pasting in the code from http://msdn.microsoft.com/en-us/library/ms228772.aspx makes it work.)
If you are just trying to automate builds/packaging of your apps, you should look into MSBuild -- it is Microsoft's build engine that lets you script automation of most of the functions of Visual Studio.