Debugger Windows In Visual Studio : Advanced Debugging

In our previous post we learned about basics of debugging, now in this post let's take our learning one step further. In this article we will be discussing what are various debugger windows while debugging, or how can we setup conditional breakpoint and many more. So fasten up your seatbelts, here we go


advanced debugging in visual studio





Various Debugger Windows


Visual studio provides various features while in debugging mode. Whenever a breakpoint is hit, you can go to menu bar > debug > windows as shown in below screenshot

debugger windows in visual studio


These are plenty, but we will discuss only about few which are commonly used. Let's get a brief idea about what are these and why they are used.

Immediate Window

This window is used to execute statements, evaluate expressions or print variable values. Consider below screenshot where breakpoint has been hit.

breakpoint hit debugging in visual studio


Now at this point if I evaluate the value of i the it will show up as 0. Also if I add 1 to i or evaluate some expression then immediate window will show up its result without changing any code. You just have to type in your expression or statement and press enter. Shown below are few results.

immediate window in visual studio debugging


Call Stack Window

This window shows up the sequence of all methods that were executed before hitting up the current breakpoint. This comes helpful when you have a breakpoint inside a method of yours and you want to identify from which method it has been called. You can double click any function in call stack window to see from which line current function has been called.

Locals Window 

This window shows up all the variables currently used in scope. Consider earlier debugger image which is showing breakpoint hit. Now at this point shown below is locals window for this, and it is showing all the variables present in the current scope.

locals window in visual studio debugging


Breakpoints Window

This window shows up all the breakpoints present in your current solution with all the information like filename, line number etc. You can enable/disable a breakpoint from here or can even remove a breakpoint but don't get too excited it doesn't allow you to create breakpoint. Shown below is breakpoints window.

breakpoints window in visual studio debugging


Watch Window

This window can be used to keep an eye over a variable throughout the debugging session. Suppose we have a long code block and we want to keep an eye over a variable to understand when its value changes or we can watch an expression to evaluate everytime we debug line by line. This can easily be done using watch window, just select an expression or any variable and right click it. From context menu select Add Watch, this adds that expression to watch window. 
Watch window evaluates its entries at every line of code while debugging. Its tabular form and thus you can edit the entries and even type in new expressions to evaluate. Shown below is an active watch window

watch window in visual studio debugging



All these windows are accessible through Menu Bar>Debug>Windows. Remember most of them are available while debugging is active and code execution is paused.


How To Set Up A Conditional Breakpoint?


When we setup a breakpoint it shows up a red circle. Now if you right click that circle, you will get a context menu like shown below

breakpoint context menu in visual studio debugging


From here you can delete or enable/disable the breakpoint. However there are two options through which we can set conditional breakpoint. These are

  • Hit Count
  • Condition

Hit Count

Using this option we can configure at what count should a breakpoint be hit, like we want breakpoint to hit when it hits the line of code for the second time, then we should set the breakpoint to break at 2. Shown below is the menu that we get after we select Hit Count option from context menu. Try it yourself, and better way to check this is inside a loop.

breakpoint hitcount menu in visual studio debugging

Condition

Second option is setting a condition for breakpoint to hit. For example we can set the condition as index == 2, this will make breakpoint to hit when value of index variable is 2. Shown below is Condition window.

breakpoint condition menu in visual studio debugging


That's all about debugging in visual studio. Visual Studio is so vast that you will end up knowing so much even after you feel like knowing everything. We will come up with a video shortly to show these things in a different way.
Till then stay tuned. Keep showing up your love to us.

Keep trying out yourself, visual studio will amaze you every-time.

A video tutorial to teach you better:


Happy Learning!!!

Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example