Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert conditional breakpoint in Visual Studio as an atomic action (such that it cannot fire before the condition is set)

I have some running code and I would like to add a conditional breakpoint, but I only know how to add conditions to existing breakpoints. For example if I add a breakpoint to a line of my C# code, using for example F9, I can then right-click on the breakpoint's red dot in the left gutter which gives me this menu

breakpoint context sensitive menu

from which I can choose Condition ... to open up the conditional breakpoint settings

editing a conditional breakpoint

However I want to insert a conditional breakpoint in an often called function with a condition that is rarely true so that I can find out why an occasional error fires. I do not want to stop the code debugging, especially if it is an error that only manifests itself after some time. So the method above is inadequate. When I first insert the breakpoint, but before I have had a chance to add its condition, it will fire and the program will 'break'.

How do I add a breakpoint with an associated condition into code running under the debugger as a single atomic action, i.e. not add the breakpoint and then add a condition to it but add the breakpoint with its condition in one go?

like image 520
dumbledad Avatar asked Sep 30 '22 11:09

dumbledad


2 Answers

I do not want to stop the code debugging […]. When I first insert the breakpoint, but before I have had a chance to add its condition, it will fire and the program will 'break'.

You can prevent this by making sure that the debugger isn't attached to the running process at that time:

  1. Detach the debugger from your running process (via menu item DebugDetach all).

  2. Set a breakpoint.

  3. Define a break condition on the breakpoint.

  4. Re-attach the debugger to your running process (via menu item DebugAttach to process…).

like image 164
stakx - no longer contributing Avatar answered Oct 13 '22 01:10

stakx - no longer contributing


I work on OzCode, a commercial extension to Visual Studio that has two ways of adding a Conditional Breakpoint in one click -

  1. As QuickAction (similar to Alt+Enter in Resharper). This will suggest relevant conditions for your Conditional Breakpoint, based on the type of the variable (ie, > 0 for numbers, == null for reference types, etc. Add Conditional BP

  2. If you are already on a breakpoint and see the invalid value that is causing the bug, and want to add a Conditional Breakpoint on it so that you can return to the same point again after changing your code, you can add one from the DataTip:

BP2

This will automatically created a Conditional Breakpoint on the relevant value, which you can edit before you hit enter to approve:

BP3

like image 25
Omer Raviv Avatar answered Oct 13 '22 01:10

Omer Raviv