Visual Studio--Alter Variable While Debugging - visual-studio-2010

Is it possible to alter a variable while debugging?
Say for example I have this code:
string x = "foo"; //would actually be a passed-in variable in the real world.
var y = "X equals " + x;
==>[BREAKPOINT] return x;
Is there a way to manually enter or otherwise change the value of "x" when I hit the break point? Also , is it possible to "step back" in the code in the same way that you can press F11 and step through it?

Yes, there are 2 ways to change a variable:
Use the Immediate window. Simply typing x = "a new value" will change it. Also, if you want to check the value of x use ?x
You can hover over the variable, and when the value displays in the quickwatch pop-up thing, just click on it an manually change it. (You can also add the variable to watch, or select quickwatch to change it).
If you would like to step to a different instruction, there are 2 ways to do that too:
You can right-click on the line you want to go to and select 'Set next statement'. This even works if you have hit an exception while debugging
You can drag the yellow arrow that indicates the current instruction to wherever you need it

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.

Visual Studio Jump to Beginning of Function

I know that Ctrl+} will take you to the corresponding brace in Visual Studio, but say I'm in the middle of a gigantic function and I don't know where the top or the bottom is, is there a shortcut to get directly to the function declaration?
void function()
{
//so many lines of code
//can't see the top or the bottom curly brace
//can i get to the top of the function with a shortcut?
}
I have a fresh install of VS2017. As of 15.9.1, the default for me is Alt+Shift+[.
This is the shortcut for EditorContextMenus.Navigate.GoToContainingBlock. So you may have to execute this shortcut multiple times if you are a few block layers deep, but it'll get you where you want to go.
Alt+Ctrl+UP,Tab,Tab,Enter
This sequence will move you through Project selctor > Scope selector > Function selector > Current Function.
Ctrl+M,Ctrl+M
This sequence will toggle between collapse/expand current block.
Place cursor at any line that is immediately enclosed by the function. Collapse. Place cursor at the end of the collapsed function, i.e after { ... }. Expand the function to get to its last brace.
Note:
If you have difficulty in finding a line immediately enclosed by the function(for example, when the function has lots of nested blocks), you can always goto the beginning to collapse the function.
Update
With last updates Visual Studio, now default keyboard shortcut for EditorContextMenus.Navigate.GoToContainingBlock is Shift+Alt+[
Old Answer:
Visual Studio 2017 version 15.8.0 comes with a new shortcut Ctrl + Alt + UpArrow - Go to Enclosing Block.
Go to Enclosing Block (Ctrl + Alt + UpArrow) allows you to quickly
navigate up to the beginning of the enclosing code block.
Source
This command allows also move to function declaration if you are inside function.
If shortcut doesn't work for you
For the VSCode lovers, this key combination will bring you to the top of the function:
Ctrl-Shift-.
followed by ENTER
and for MAC users:
Cmd-Shift-.
followed by ENTER
I usually double press the white line that is located left of the code.
It closes the function but it also takes you to the declaration of the function.
You can do it with Macros for Visual Studio extension.
Here's the code for macros:
// BeginningOfFunction moves the caret to the beginning of the containing definition.
var textSelection = dte.ActiveDocument.Selection;
// Define Visual Studio constants
var vsCMElementFunction = 2;
var codeElement = textSelection.ActivePoint.CodeElement(vsCMElementFunction);
if (codeElement != null)
{
textSelection.MoveToPoint(codeElement.GetStartPoint());
dte.ActiveDocument.Activate();
}
It is one of the sample macros of the extension. Edited it a little, because for some reason sample didn't work for me. You can get to the end of the function by changing codeElement.GetStartPoint() to codeElement.GetEndPoint().
I found one trick in visual studio:
Place the cursor on the empty to get the context (name of the function), copy the name of the function, then click the drop down arrow where all functions will be listed, paste the function name, enter. Then you are at the beginning of that function!
Another alternative would be to use Edit.PreviousMethod. I prefer this option because even if your cursor lies in multiple nested block, you can get to the method definition in single keystroke! I have mapped Edit.PreviousMethod to ctrl + alt + , and Edit.NextMethod to ctrl + alt + . but you can set it to whatever you prefer.
To setup key binding, goto Tools.Options.Environment.Keyboard, then in Show Commands Containing textbox type edit.previousmethod, set focus on Press Shortcut Keys textbox and press the key combination you want, the hit Assign. Repeat for edit.nextmethod, then Ok.

How to see type of a C++ variable?

Suppose I have the following C++ code in Xcode 4.4.1.
int Func();
...
void Test()
{
auto Variable = Func();
...
DoSomething(Variable);
}
Suppose I want to find out the type of Variable in the 2nd last line. How can I do it in Xcode?
IIRC, in Visual Studio you put the mouse cursor over the variable and a tooltip appears. This doesn't happen in Xcode.
I know these 3 methods, but I want to know if there's something better I'm not aware of.
If you hold alt and click Variable, it will say "defined in SomeFile.h".
If you hold cmd and click Variable, it will take you to the line where Variable is defined. But then you have to find out the return type of Func() which involves more steps. I'm looking for the fastest way to do this.
If you put your cursor at the end of Variable, and press Ctrl+Space it will show auto-complete with the type of Variable listed in there. This works, but it seems awfully indirect (e.g. if you put the cursor elsewhere, you might get a lot of entries in auto-complete list, forcing you to search for Variable). Is there a more direct way to do this?
How can I do find out the type of Variable in the 2nd last line in Xcode?
You can set a breakpoint at the variable for which you want the type, then run the code. Make sure you have the debugger tool open, Xcode will show you something like "Variable = (int) 0" or "Variable = (std::__1::string) 'variableIsString'."

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

add watch not working in Visual Studio 2010

I right-click in the Watch 1 window and select Add Watch but nothing happens.
It is very unintuitive. The command doesn't do anything beyond adding a new row and selecting it. You next type the name of the variable. More intuitive is right-clicking an identifier name in the editor window + Add Watch. Or drag + drop it into the Watch window.
Right click (in your code) on the variable or expression (select it) you want to watch. It will be added to the Watch window.
Little question, large answer! You give us few details of what is happening...
Summarizing, be sure that current statement is around the place where your desired watched variables are, so you can see them on debug mode, i.e.:
Place a break point near there (press F9). Remember that it must be where the variable exists (remember that on C/C++/C# a common variable created inside brackets ({ and }) will be inaccessible outside it - something like that occurs on other languages);
Run the program (F5) and do something that let you on the right place (like pressing a button that launches the function where there are variables you want to watch);
To choose a variable to watch, on debug you must press mouse right button on it and "add watch". "Walk towards" using debug functions step into (F11), step out (shift+F11), step over (F10) or run-until-next-break-point (F5).

Resources