Is it possible to record variable changes? - visual-studio

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.

Related

Visual Studio: Is there a way to skip execution of code between two breakpoints?

I know I can comment the code but would be even better if there is a shortcut to skip execution of code between two breakpoints.
VS has a feature 'set next statement' which moves the program counter to the to that location and continues execution from there (skipping anything in between). You can read more about it at: https://learn.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2017#BKMK_Set_the_next_statement_to_execute. The easiest way to use it is to either use the context menu item "Set Next Statement" (right click on where you want to set it) or hold down the ctrl key which changes the green "Run to click" editor glyphs into yellow "Set next statement" glyphs and just click on where you want to set it.
Set next statement is a great tool but it's really dangerous as a debugging tool. You're using the debugger to execute code in a way which would never happen normally. The results of which could cause crashes or other failures easily. It's real easy to do things like skipping over the initialization of a variable that's later used and will now cause an exception/crash.
It can be used in JS, .NET and native.

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.

Going back to previous line while debugging on visual studio

I am debugging a piece of code on visual studio and I forgot to note down the values I have kept a watch on. Can I go to previous line without rerunning the entire code?
There are similar questions asked on SO but in my case i haven't run through any error or exception. The code is running normally.
After pausing on a break point, right click on the line you want to "go back" to. From the menu that pops up, select "set next statement".
This will adjust the instruction pointer to continue from the specified line of code, but it will not roll back any variables or memory addresses to the values they were at before that line of code was originally executed.
It sounds like what you want to do is rewind / replay your code rather than just move to a specific line. You can move to a specific line, you can just right click and choose set next statement. Unfortunately, this won't rewind the state of the program to some past point (beyond setting the stack and doing a bit of unwinding).
To rewind/replay you need to be a bit trickier. Some options are: -
VMWare replay which will allow you to record and then go back to a certain point in time.
Intellitrace. I haven't tried it, but it allows you to replay to a point.
Which is a bit heavyweight and wont help you right now.
You can use the mouse to drag the yellow arrow pointing to the "next statement to be executed." This actually changes which statement will be executed next. It's not guaranteed to work, but as long as the code is not too complex, it could.

How get a breakpoint on variable write in 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

Visual Studio 2008 - Debugging tips/tricks - Continue "until next function" or "until next file"

Is there any way to tell the debugger to just continue until the next file is accessed, and/or until the next (developer written) function is accessed, without setting debug points ahead of time? I'm kind of new to VS debugging so all I use right now are f5, f10, and f11.
There is currently no way to do what you are asking. The main ways of telling VS to go until something happens are the following
Hit F5 and VS will go until the next user breakpoint or ,depending on your settings and where it occurs, the next exception is raised
Right click and select "Run to cursor"
Shift-F11 breaks out of the current method
Run to cursor doesn't require an explicit break point but it does require that you know where you want to break next.
You can right-click and select "run to cursor" if you just want to run to a specific line ahead in the execution stream.
Another one is Shift-F11 which finishes the current method and breaks again when you get back to the caller.
Actually, there is a way to set conditional break points.
Click in the left margin on the line where you want to break, as usual. (or F9)
Right-click on the red dot. In the context menu, click on "Condition...."
In the dialog, set your condition, e.g., fileName == "foo"
Hit F5 and go until the conditional break is hit.
Looks like there's not a way to do what I wanted to do

Resources