How get a breakpoint on variable write in Visual S

2019-04-19 06:45发布

问题:

How I can set breakpoint on variable change (I think this is write access) in Visual Studio?

回答1:

This is referred to as a Data Breakpoint in Visual Studio. To create one you'll need the address of the variable in question (just add &variableName) to the watch or immediate window. Then do the following

  1. Debug -> New Breakpoint -> New Data Breakpoint
  2. Enter the address in and size of the value in bytes

Note: This is only supported for C++ applications. Managed languages don't support data break points.



回答2:

You need to add "Has Changed" condition to your breakpoint. To do this:

  1. Set breakpoint on the line you want it to break when your variable is changed.
  2. Right-click red dot icon, select "Condition".
  3. Enter your variable name and select "Has Changed" option.

You may find more information in this MSDN how-to.



回答3:

This is now supported in VS2019 for . NET Core 3.0 or higher check this out

How do I set a data breakpoint?

Setting a data breakpoint is as easy as right-clicking on the property you’re interested in watching inside the watch, autos, or locals window and selecting “Break when value changes” in the context menu. All data breakpoints are displayed in the Breakpoints window. They are also represented by the standard, red breakpoint circle next to the specified property.



回答4:

If you right click on the break point you can can set Conditions... This lets you specify a if a variable value is true or if its changed.
Break point conditions



回答5:

You can add a conditional breakpoint by:

  1. Add a normal breakpoint
  2. Right-Click on it and select "Condition"
  3. Select "Has changed"

The breakpoint will only be hit when the condition inside the textbox has changed.

As far as I'm aware, the condition inside the textbox needs to be written in the language you are debugging. I.e. in C#: x >= 5

If you are just looking for the change of a variable, you can simply add the variable itself to the TextBox and the breakpoint will be hit when the variable changes.

HTH, Christian