How get a breakpoint on variable write in Visual Studio? - visual-studio

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

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
Debug -> New Breakpoint -> New Data Breakpoint
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.

You need to add "Has Changed" condition to your breakpoint. To do this:
Set breakpoint on the line you want it to break when your variable is changed.
Right-click red dot icon, select "Condition".
Enter your variable name and select "Has Changed" option.
You may find more information in this MSDN how-to.

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.

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

You can add a conditional breakpoint by:
Add a normal breakpoint
Right-Click on it and select "Condition"
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

Related

Xcode variable debug visualized

Is there a way to debug a bunch of variables and see their contents live while running the simulator?
I know I can access variables immediately from the console/debug window if I use breakpoints but what I'm looking for is a bit different.
Is there a way to debug a bunch of variables and see their contents - live - while running the simulator?
Yes in Xcode you can use po to see your object when you are on your breakpoint:
Just type in the console:
po myvariable
I've made a little example for you:
I create a variable, I set it to one, and I put a breakpoint. I access to the console to see the value of my variable
I set the variable to two and I retype po myvariable to see the new value of my variable
Yes, you can do this, by editing breakpoints: At the point where you want to see your variable's value, add a breakpoint. Then right click it to "edit breakpoint." Click "automatically continue after evaluating actions." Click "Add Action" Note that after you do this, there is a + and - control to add more actions. Choose "log message" and type in a string so you'll know what variable value you're about to display. Click the + button, leave it at "Debugger Command" and type "po name-of-your-variable" (replace with name of your variable, of course) Now when your code hits this point, it will print the log message and value in the console and continue execution. Repeat to taste.
Apart from using po to inspect an object in lldb, Xcode provides a nifty feature to print the description in the console.

Is it possible to record variable changes?

I am using Unreal Engine 4 and there is a bug where the mouse is not constrained to the window.
I am trying to debug the mouse position, but it am not sure how I would do it.
Every time I set a break point at the mouse position visual studio will pause the application, it is just really hard to debug because there are so many pauses.
I am wonder if it is possible to record variable changes? For example set a special break point at a variable and visual studio will only very briefly pause the application, look at the variable and put the value into a list.
I could just log the mouse position, but I want to get away from using log all the time.
This can be done by using the "When Breakpoint is Hit" window. Set the breakpoint, right-click it and choose "When hit..."
On this window, check both the "Print a message" and the "Continue execution" checkboxes. Then update the string you want to print to include the value(s) that you want printed (variable values and expressions needs to be wrapped in {} to be interpreted as such. Setting "Continue execution" makes the debugger not break at the line, instead, it will just log and continue. The messages are printed to the Debug >> Output window at runtime.

Breakpoints (linked to a condition) in Xcode

I have read that it is possible to set a conditional breakpoint at some specific line in the code, by setting the breakpoint as usual and then set the condition.
That is fine, but what I need now is different. I want my program to stop when a given variable takes a particular value or simply changes its value. But I do not know where this happens.
So I need a kind of (general) conditional breakpoint, not one at a specific line.
Is this possible? It would be a bit like what is possible with exception breakpoints.
Symbolic breakpoint is what you are looking for. Debug->Breakpoints->Create symbolic breakpoint or follow instructions from apple docs:
In the bottom-left corner of the breakpoint navigator, click the Add button.
Choose Add Symbolic Breakpoint.
Enter the symbol name in the Symbol field.
If the symbol is declared in more than one library, enter the name of the appropriate library in the Module field.
To specify that program execution be suspended only if an expression evaluates to true, enter the expression in the Condition field.
Click Done.
As symbol i would use setter of property you want to track...

Is there any way to set or code breakpoints conditionally?

I've been wondering this for a while - is there a way to code/program breakpoints...? Conditionally? For example, can I specify something like - "when this variable becomes this value, break and open the debugger"? (Would be quite useful, especially in long loops when you want to debug loop execution of a late loop value.)
I suppose this may be IDE-specific since debugging is implemented differently in different IDEs... I'd be interested to know how to do this in any IDE, but specifically in Eclipse and Visual Studio.
This is definitely possible in Visual Studio. You can normally click in the left margin to insert the breakpoint, and then right click on that breakpoint. One of the options in the right click menu is "Condition...", and you can specify a predicate that will tell the debugger only the break on that breakpoint if the predicate is met.
In Visual Studio, you can declaratively set conditional breakpoint, which are like normal breakpoint but will only break when a certain condition is true. The condition can use local variables and whatever is accessible from where the breakpoint is set. Just right click any breakpoint (red dot) and select "Condition...".
In addition, .NET languages can call the Debugger.Break() method to programmatically break the execution. This also can be done within an if statement:
if (count > 8 && Debugger.IsAttached)
Debugger.Break();
If conditional breakpoints aren't supported by your IDE, add in an if statement and break inside of it.
if (variable == 3) {
// Stub code to attach breakpoint.
1 = 1;
}
Setting a conditional breakpoint in Eclipse (thanks for all the Visual Studio answers!):
Set a breakpoint. Right-click and choose "Breakpoint Properties...". Check "Enable Condition" and enter your condition code into the text area.
Most IDEs allow for conditional break points for this very reason. In Visual Studio, you can right click on the red dot for the breakpoint in the margin and open the condition dialog from there. You can also get at the condition dialog from the breakpoint window in Visual Studio. I am not familiar with Eclipse.

Eclipse: Improve debugging and display variable values on mouseOver

Is it possible to view variable values in Eclipse when debugging? Right now when I "mouse over" a variable all I get is the definition.
e.g. for [int mLastView] I get [com.company.samples.MyClass.mLastView] instead of 1. The value that was assigned to it.
Also, is there anyway to improve debugging in Eclipse?
For starter: making the breakpoints visible as in VS (see below)?
Eclipse Break Point
Visual Studio Break Point
I posted this over at Stack Overflow and one of the suggestions was to go into Window -> Preferences -> Java -> Editor -> Hovers and select the Variable Values option and assign a modifier.
When I was first trying to resolve this issue, this was one of the options I looked at, but oddly enough, there was no Variable Values preference available, it was missing. Once my “fix” above was applied, it magically appeared:
Click to see the pictureBroken Link
Actually, since eclipse3.4, not only do you see the value of a variable when you pass the mouse over it, you can actually inspect it:
When debugging, hovers for variables have been enhanced to display an object inspector. The inspector will display logical structures according to the toggle setting in the visible Variables or Expressions view.
If you hit the breakpoint while you are debugging, you do see the value of the variable when you mouse over. You can also select an expression, and inspect the value of it's evaluation using the "Inspect" menu option. You can also use the "Variables" view to see the current value of all in-scope variables.
About breakpoint visibility:
Right-click on the right outline of the editor, you'll see some Preferences, and there in Annotations you can select Breakpoints. I personally added Text as Highlighted and some pinky colour. Shame that the highlighting is really buggy, sticks here and there, breaks between lines, etc. But it somehow works for most cases.
(Another shame is that breakpoint bullet is often hidden behind some suggestion icon or what - why they can't make the gutter wider like Idea does, I don't know.)
I got similar but a little different problem with the thread-starter. Sometimes during debugging, I mouse over a variable, I see it current value. Sometimes it's just the definition, like in coding mode. So what caused the first case, what the second?
PS: Of course I can always choose to view Variables (Alt+Shift+Q,V) but it's faster if you have mouse over value instantly.
Thanks

Resources