Visual Studio remote debugging on application startup - c#

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);
}

Related

How to disable break on Debugger.Launch();?

Currently, when calling Debugger.Launch(); from a running application, VisualStudio breaks execution at the line where the debugger is launched and then I have to manually continue code execution.
Is there a way to stop it from doing that and to just attach the debugger without breaking at the line where it's launched?
If you run your app in Visual Studio using F5 (Debug|Start Debugging) then the debugger will start attached, no need for Debugger.Launch().
If your app is running externally then you can attach the VS debugger whenever you want to by using Debug|Attach to Process... and (based on a very quick test I've just done) this does not break into the process.
If you want to programatically attach the debugger from within a running process then this question and it's answers might help Attach debugger in C# to another process. Note in particular Dave Hillier's self-answer where he mentions it might be possible to use Process.Start to run vsjitdebugger.exe -p ProcessId.

Hololens: no debug output in Visual Studio 2017

My task is very simple, I put in the Main method two lines:
Debug.Log("App START Debug.Log");
Debug.unityLogger.Log("App", "START Debug.unityLogger.Log");
and want to see these logs in the output if the app is running. If I set the run option as Releasethe app starts, but I don't see these logs. If I set the run option as Debug and try to start, I get an error popup:
Unable to activate Windows Store app ´Template3D_pzq3xp76mxafg!App´
and the app doesn't start at all.
EDIT: according to comment of #Perazim, I have also tried:
System.Diagnostics.Debug.WriteLine("App START Debug.Log");
No effect in Release and Debug mode.
EDIT 2:
System.Diagnostics.Debug.WriteLine("App START Debug.Log");
works, if I put it in other methods, but doesn't work in the Main() method.
Debugger.Log for the output to debugger. Debug.WriteLine prints to listeners. If there is no listeners, it will not work.
My application stopped writing output to the output window with the System.Diagnostics.Debug.Writeline in debug mode. This all happened after the latest Microsoft Updates.
I created a new application just to see if it was my current application, and the new application would not write to the output window either.
Then I decided to right click on the Output Pane in Visual Studio and what do I see, Program Output unchecked.
I checked this and my Debug.Writeline works again.
In unity you can use/see debug.log(); in Visual Studio you need to use Console.WriteLine();

Visual Studio clears output file at end of debugging

I am experiencing a weird behaviour from Visual Studio 2013. I've got a C# program which writes to the standard output, let's say
using System;
using System.Threading;
namespace CsSandbox
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
Thread.Sleep(10000);
}
}
}
In the Debug tab of the project's properties I have redirected the output to a file, like this:
If I open the file within those 10s when my application is still running, the file does contain "Hello world!". However, as soon as the program exits, the file is cleared. This does not happen when I run the program from the command line.
Is there a rationale why Visual Studio does it? Is there a way to bypass this behaviour?
I believe this is due to the way Visual Studio hosts your application, in order to reduce startup time when debugging.
What happens is that your application (Program.exe) will actually be hosted in another process (Program.vshost.exe), which is started with the same command line arguments. When your application ends, this process is immediately restarted. You should be able to see this within Task Manager - look in the details tab so you can see the PID of the processes, and run your app - you'll see one Program.vshost.exe instance which ends when your app finishes, and another one come up immediately. That's then overwriting your output file.
One fix for this is to give your application a command line argument for the file to write to - and open that file within your Main method. You won't get there until you start running the app again.
Another option would be to simply stop using the hosting process - in the Debug part of your project properties, at the bottom you should see a checkbox for "Enable the Visual Studio hosting process". Uncheck this and I think your problem will go away - but it'll take longer to start debugging.
See this blog post for more information about the hosting process.

Attach debugger onto another Visual Studio instance

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.

How to start a child process in the same Visual Studio debugging session as the parent, programmatically?

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

Categories

Resources