54

I am using Visual Studio 2010, and I know this feature is available in C++.

I need to debug some code, that changes a variable to several values. I want to debug the code in a specific case, when the variable getting a specific value. I know I can add if(var == value), but is there any elegant way to do it?

Another question, can I set a breakpoint when a variable is changed in general?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Delashmate
  • 2,344
  • 5
  • 26
  • 40

8 Answers8

106

It is certainly possible to set a condition like a variable receiving a certain value. This is known as a breakpoint condition. To create one, do the following.

  • Set a break point at the point the variable changes
  • Right click on the break point and select "Condition"
  • Type in the conditional like "theNewValue == 42"

Now the breakpoint will only hit when your conditional evaluates to true.

The second item you asked for, breaking when a variable's value changes for any reason, is known as a data breakpoint. These are only available for C++ code. It's not an option in C#, VB.NET or any other managed language.

InteXX
  • 6,135
  • 6
  • 43
  • 80
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • do you know why data break point not available in managed languages? – Delashmate Sep 20 '11 at 16:06
  • 4
    @Delashmate it's a limitation in the CLR's debugging infrastructure. Visual Studio's hands are essentially tied until the CLR provides the capability – JaredPar Sep 20 '11 at 16:06
  • Ok, I hope it isn't so complicated question, why the CLR don't support this option? – Delashmate Sep 20 '11 at 16:11
  • 3
    @Delashmate it's just a time / value trade off. There isn't any functional reason they can't support it. Thus far the time it would take to implement it thus far has been to big compared to the value they believe it would provide. FWIW: I wish they would support it. But I would also prefer they provide ENC 64 bit over this. – JaredPar Sep 20 '11 at 16:12
  • Data breakpoints are availble since Visual Studio 2019 Preview 2. See my answer for the link to the devblog. – Lukas Oct 08 '21 at 10:31
6

So long as you are using a Visual Studio edition other than Express, you can achieve this in C# using a breakpoint condition.

In the Breakpoint Condition dialog box, enter a valid expression in the Condition box, such as myLocalVariable > 1

and

...choose Has changed if you want to break when the value of the expression has changed.

To get to the Has changed option, right-click your breakpoint in the Breakpoints window and select Condition..., then check the screenshot below.

Has Changed option for breakpoint conditions

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
  • Ok, now I understand, what I was looked for is to get notification when variable got changed in general, means to create data break point.. that way it's more general.. – Delashmate Sep 20 '11 at 16:18
  • you could have a timer that checks for this – beppe9000 Dec 28 '14 at 18:55
4

Add a breakpoint with F9 - right click it and select "Condition..." - now you can add a boolean condition and the breakpoint will only get hit if that condition evaluates to true.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
3

It depends on the scope of your breakpoint. If the variable is not local or not static you won't be able to.

To set the condition of a breakpoint, right click it and you should get this screen:

Enter image description here

Pick Condition...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pedro Costa
  • 2,968
  • 2
  • 18
  • 30
2

You can use conditional breakpoints. I know your question was specific to VS2010, but be aware that from VS2012 on, you have to switch to the Managed Compatibility Mode, to use conditional breakpoints in Visual Basic. Why and how is described here:

switching-to-managed-compatibility-mode-in-visual-studio-2013

thewhiteambit
  • 1,365
  • 16
  • 31
1

VSCode

In VisualStudio Code, you can set conditional breakpoints as follows:

  1. Click in gutter to create a red-dot breakpoint

  2. Choose Debug from left-side toolbar (icon: circle-slash over bug)

  3. There are four sections: Variables, Watch, Call Stack and Breakpoints

  4. Expand Breakpoints section so you can see the breakpoints

  5. Right-Click on the desired breakpoint

  6. Choose Edit Breakpoint...

  7. Set your condition and press <Enter>. For example:
    myvar == 1234
    or
    'stophere' in myvar
    etc

References:

https://code.visualstudio.com/docs/editor/debugging#_conditional-breakpoints

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

Conditional breakpoints are possible as other answers already pointed out. As JaredPar explains you can set a breakpoint, right click on it, select "Conditions" and type your condition(s).

Since Visual Studio 2019 Preview 2 the so called "Data Breakpoints" are availble. While in debugging mode you can select a variable in your "Autos" or "Locals" window and with right click "Break Whan Value Changes" you can archive just that.

This article from Microsoft DevBlogs explains it pretty good: Break When Value Changes: Data Breakpoints for .NET Core in Visual Studio 2019

Lukas
  • 103
  • 2
  • 9
0

You can do both of these things.

  1. Set the breakpoint in VS. Right click on the red dot in the margin and select Add Condition. In there you can say var==value and select "Is True".
  2. You can probably achieve this with the "Has Changed" option in the dialog above.
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222