How To Display Registers In Visual Studios 2015
In the previous tutorial, we saw some of the basics of debugging in Visual Studio. This included Breakpoints, Navigation through lawmaking, and Investigating variables with the DataTip and QuickWatch.
In this tutorial we will get over all the windows Visual Studio has for debugging.
The Lookout Window
The Lookout Window allows you lot to see value of variables and expressions while debugging. Information technology'southward kind of like the DataTip you lot get when hovering over a variable, except that you tin write any expression you want. It's available from Debug | Windows | Lookout | Watch one or Ctrl + Alt + West + 1.
Here's an case:
There are iv watch windows in Visual Studio, which you can employ in dissimilar contexts (Watch one, Scout ii, etc.).
Any expression tin can be entered into the sentinel window. The aforementioned rules apply to expressions as to lawmaking. So if you lot write an illegal expression, you'll encounter the same compiler fault.
To add together items to watch do any of the following:
- Write them manually in a new row of the Watch window
- Correct-click on a variable choose "Add Watch" in the context bill of fare
- Right-click on a variable in the DataTip and choose "Add Scout"
- "Add together Sentry" push button from QuickWatch
When to Employ the Scout Window:
While the DataTip and QuickWatch are more popular, the lookout man window is very useful when yous demand to re-evaluate the same variables and expressions multiple times. This happens if you lot hit the aforementioned breakpoint over and once more, or dissimilar breakpoints in the aforementioned class. The near common scenario is with a breakpoint inside a loop.
Yous can use spotter window evaluate the Object Id instance we talked about in Function 1. That's where y'all create a weak reference to any object, which tin be evaluated from anywhere with $1, $2, and so on.
Watch Window Rules and Behaviors
There are some behaviors, rules and features to the watch window.
- When an expression tin can't compile in the current telescopic and context, you will see a compiler error.
- You can click the refresh button to effort and re-evaluate the expression. Maybe subsequently you lot've edited it and stock-still the trouble.
- When a value has changed since the last breakpoint, information technology'due south shown in cherry.
- You tin can evaluate methods and properties in the watch windows. These can have side furnishings on the application state. For example, in this lawmaking the
IncreaseStatic()
method volition modify a static variable. Side effects are usually unwanted in a debugging session, so Visual Studio notifies y'all about it before evaluating. Clicking on the refresh button volition evaluate the expression, suffering the side effects. - VS 2019 introduced a new characteristic: Search in the Watch windows, Locals, and Autos windows. It searches through Name, Value, and Type and stops on first match. Search Depth is useful for nested fields and properties in classes.
The Firsthand Window
The immediate window is bachelor in the menu from Debug | Windows | Firsthand or Ctrl + Alt + i. You tin can type in any expression and the immediate window will evaluate. Information technology's kind of similar the Watch window, but information technology acts more than like a command line window in Windows or Linux.
For me, the immediate window was always more convenient than the Lookout or QuickWatch windows.
- Like in control line interfaces, you can use the Up/Down arrow keys to paste previous expressions.
- To clear the window, correct-click on it and select "Clear All".
Side Effects in Watch Window, QuickWatch, and Immediate Window
We saw before that evaluating the expression can cause side furnishings. For example, consider this form:
class MyClass { public int number = 0 ; public int Foo ( int a ) { number ++ ; render a + 1 ; } } |
If I evaluate Foo()
in the Immediate Window, number
will increase and my application state volition exist inverse:
We tin see here that yard.number
increased from 1 to 2.
You can add the , nse
(No Side Effects) postfix to an expression, and it will evaluate without changing the application state.
This works both in the Immediate Window and in the Watch Windows.
As you might find out, not all expressions are supported in no-side-effects way. For case, yous can't practice a network request in this mode because there's no fashion to guarantee it volition have no side-effects.
Locals and Autos Windows
VS offers 2 automatic-picket tool windows: The Locals and Autos windows.
The Locals will evidence local variables of the electric current scope. Information technology usually starts with this
, which represents the electric current form.
The Autos window will testify all variables used in the current line and in the previous line. These could be local variables, class members or static variables. The Autos as well shows values returned from methods, which is useful at times.
- In both Locals and Autos, y'all tin can double click on any Value field and change the variable's value. This will really its value, causing a possibly unwanted side upshot.
- Search works same as in the Sentry window.
Phone call Stack Window
One of the nigh useful tool windows is the Call Stack Window. Information technology shows the chain of methods that chosen one some other, up to the the currently debugged method. For example, suppose you have the post-obit Panel Awarding:
one two 3 iv 5 half-dozen 7 8 9 10 11 12 xiii fourteen fifteen 16 17 18 nineteen 20 21 22 23 24 25 26 27 28 29 thirty | static void Main ( cord [ ] args ) { var customer = GetCustomer ( args ) ; var cashier = new Cashier ( ) ; var newPrice = cashier . PriceAfterDiscount ( customer , 250 ) ; // ... public form Cashier { private double _vipDiscountPercent = x ; public int PriceAfterDiscount ( Customer client , int initialPrice ) { if ( customer . IsVIPCustomer ) { var discount = GetDiscountForVip ( initialPrice ) ; return initialPrice - ( int ) discount ; } else { return initialPrice ; } } private double GetDiscountForVip ( int initialPrice ) { return initialPrice * ( _vipDiscountPercent / 100.0 ) ; } } |
When you place a breakpoint int GetDiscountForVip()
the call stack volition be:
The topmost method is always the current method, which is GetDiscountForVip()
. Below it is the method that called information technology, which is PriceAfterDiscount()
. Beneath that is the method Main
, which called PriceAfterDiscount
.
Yous can double-click any method (aka Frame), and you will be navigated to that part in the method:
Note that there's a green pointer on the left. This shows that this is the currently debugged method, but also that it's not the topmost Frame. While in this mode, you tin can hover over variables in the electric current Frame (method) and come across their values.
Call Stack Options and Features
Besides the ability to switch frames past double-clicking, you tin open the Call Stack's context carte past mouse right-clicking on any frame.
Let's get over the more interesting options:
- Switch To Frame – Same every bit double-clicking, switches to selected frame
- Become To Source Code – Navigates the editor to the method's source lawmaking without actually switching the Frame.
- Go To Disassembly – Opens the Disassembly window and shows the selected method. That'south a bit more advanced, but we'll talk near it afterward on.
- Go To Module – Opens the Modules Window and select the module in which the selected method is stored. We'll talk about the Modules window in future tutorials.
- Unwind To This Frame – When an exception is hitting, you can "Unwind the telephone call stack". That'due south sort of like going one step dorsum in time, before the exception happened so you can make apply of a feature chosen Edit and Continue. This feature allows to alter code live while debugging. In this fashion, you can theoretically change the code so as to prevent the exception from happening. We'll talk more than almost exceptions in the next part of the tutorial.
- Breakpoint – Allows to add a breakpoint where this method was called (in the invocation). Or, alternatively, insert a Tracepoint. That'due south a breakpoint that prints to the output window and continues execution.
- Show External Code – When checked, you will meet Frames from Modules that are not your own source code. This might the .NET framework itself, or from some 3rd party library you lot are using. Very useful feature for advanced scenarios.
- Evidence X – Allows to include a lot of interesting information within the window itself. You tin usually get this by switching to Frame, but it's a useful visualization.
Threads Window
The Threads Window is the final tool window in what I phone call the Truly Vital Debugging Windows group. This window shows all the currently active threads, and let to switch between them.
Consider this code:
one two 3 4 five six 7 8 9 10 11 12 xiii fourteen 15 xvi 17 xviii 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 | class Counter { private int _counter ; public void StartCount ( ) { _counter = 1 ; Chore . Run ( IncreaseCounter ) ; Job . Run ( DoubleCounter ) ; Task . Run ( LogCounter ) ; } private void IncreaseCounter ( ) { while ( true ) { Interlocked . Add together ( ref _counter , 1 ) ; Thread . Sleep ( 15 ) ; } } private void DoubleCounter ( ) { while ( true ) { var counterNow = _counter ; Interlocked . Add ( ref _counter , counterNow ) ; Thread . Sleep ( 200 ) ; } } individual void LogCounter ( ) { while ( true ) { Console . WriteLine ( _counter ) ; Thread . Slumber ( 500 ) ; } } } |
When run, three methods will execute in parallel. IncreaseCounter
volition keep increasing _counter
by 1. DoubleCounter
will multiply _counter
by ii. LogCounter
will impress its value every 500 milliseconds.
When stopped on a breakpoint in LogCounter
, you volition exist able to see all iii running thread.
- You tin switch between threads by double clicking the row.
- To see the Phone call Stack of a thread, you can switch to a threads and then open up the Telephone call Stack window. Alternatively, the Threads window has a footling arrow between the Name and Location column that allows you to see the call stack.
- You tin can Search the call stack for namespaces, names of methods, thread ID, and anything else that can exist seen in the columns.
There are as well other visualization and pocket-sized capabilities which are less important.
Finishing up and Coming soon
This is it for this tutorial. Nosotros saw the fundamental must-know debugging tool windows that Visual Studio has to offer. There's another tool window which I consider a fundamental one, and that is the Exceptions window. This is a rather big bailiwick, which is covered in the adjacent part of the tutorial.
How To Display Registers In Visual Studios 2015,
Source: https://michaelscodingspot.com/debugging-part2/
Posted by: chiltonowayll.blogspot.com
0 Response to "How To Display Registers In Visual Studios 2015"
Post a Comment