Debugging

Web page by Kevin Harris of Homer IL

Please contact Kevin Harris of Homer IL concerning this web site

Debugging with Visual Studio 2015

"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"Brian W. Kernighan


Reference: Microsoft Virtual Academy: Rapidly Diagnose and Solve Application Problems by Andrew Hall.

Visual Studio is the most powerful development environment available. Even the free Community Edition contains advanced features that help developers write and debug code. The development features in the Community Edition include: Intellisense, code snippets, refactoring, outlining, code searching and navigation, code metrics, and automated code formatting. The debugging features in the Community Edition include: integrated debugger, diagnostic tools, immediate window, autos window, profiling, performance explorer, and remote debugging. As you move up to the Professional, Enterprise, and Ultimate editions the number of development, debugging, testing, and support features increases. The following debugging features are available in Visual Studio 2015.

Tips

  1. Step Into (F11) - Use to start debugging session and it will find the first class which executes in your program and stop the debugger on the first executable statement of the program.

  2. Autos WIndow - (DEBUG -> WINDOWS -> Autos) displays variables in current and previous statements. Allows variables to be changed.

.Autos Window in Visual Studio 2015



Autos Window in Visual Studio 2015



  1. Set Next Statement - (DEBUG -> Set Next Statement) Allows you to move the instruction pointer to a line of code you wish to make the next executable line.

  2. Step into Specific - (DEBUG -> Step into Specific) Shows all the methods a line a code will execute and allows you to pick one which will cause the debugger to execute up to that method and then break.

  3. Run to Cursor - (DEBUG -> Run to Cursor) Sets a one-time break point and runs the application to that break-point line of code. There is no break-point set which will later need to be unset.

  4. Edit and Continue - Allows you to modify the code which is about to be executed. The change is applied immediately without the need to recompile the program.

  5. Call Stack Window - (DEBUG -> WINDOWS -> Call Stack) You can view the call stack and use the "Run to Cursor" on the call stack to run to any selected point in the call stack. You can also use the Step Into and Step Out functions in the debugger to move around the methods in the call chain.

.Call Stack Window


Call Stack Window



  1. Immediate Window - (DEBUG -> WINDOWS -> Immediate Window) Allows you to enter expressions to be evaluated and executed during debugging. Can also be used to debug into library code included in referenced DLLs. (i.e. create an instance of the class defined in the library code and execute to defined breakpoint).

.Immediate Window


Immediate Window



  1. Coding Debug Statements in Program - Wrap "Debugger.Break()" inside "if (Debugger.IsAttached)" to create a hard-coded break point. "Debug.Write" statements can create a log which should in the "Output - Debug" window.

.Debug Statements in Program


Debug Statements Coded in Program



  1. Dataset Visualizers - When at a breakpoint, VS 2015 provides six built in data visualizers to show data in a more human-readable format or to render the data in case of HTML. The four text visualizers are: Text, XML, JSON, and HTML (Renders using IE browser installed). The Dataset Visualizer show a list of all tables in the dataset in a tabular view. The WPF visualizer shows a tree of the XAML elements along with their properties. Visualizers are extensible, more visualizers can be obtained from the Visual Studio Gallery or you can code your own custom data visualizer.

  2. Peek Definition - Opens another small editor window inside the current editor, so you can modify a related file without having to open another editor window.

  3. DebuggerDisplay Attribute - An attribute added to data model class to provide an enhanced formatted display of data through the tool tip mouse-over of the collection of data items.

.DebuggerDisplay Attribute Defined Using Peek Definition Editor


DebuggerDisplay Attribute Defined Using Peek Definition Editor



  1. In Context Data Tips - When you mouse-over data items a display of the data values are shown in the tool tip - these are called Data Tips. You can click on the pin icon inside a data tip to pin the data tip to a desired location inside the editor window. This is like a streamlined watch window which is attached to a specific point in the code. The data tip can also be unpinned and placed anywhere on the screen and remain visible as you move through the code. Hovering over the pin icon will also give you the last value of the variable from the previous debug session. Data Tips also provides an area to record comments. Multiple data tips can be used at one time. By right-clicking in the data tip you have the options added/removed variables and display values in hex.

.In Context Data Tip Pinned to Code Location


In Context Data Tip Pinned to Code Location



  1. Conditional Breakpoints - allows the breakpoint to be triggered based on a conditional expression, hit count, or filter value. Actions can also be specified to occur when a break point occurs, such as logging information. Pseudo variables (which start with $) are available to use in information logging information, such as $PNAME which will give the process name.

.Conditional Breakpoints


Conditional Breakpoints



  1. Perf Tip - Performance Tips display how long it takes to execute the code from one point to the next. The points can be a single statement or a group of statements defined by breakpoints. As you step through the code, the perf tip shows approximate execution time for each statement.

.Perf Tip Showing 4 ms to Load Dataset


Perf Tip Showing 4 ms to Load Dataset



  1. Diagnostics Tools - if you click on the Perf Tip, it brings up the Diagnostic Tools window which shows detailed information about where the execution time is being spent. Diagnostic Tools also shows memory information which allows you to monitor memory usage as you step through the application.

.Diagnostics Tools Window


Diagnostics Tools Window



  1. IntelliTrace - only available on the Enterprise edition, IntelliTrace allows you to use Diagnostic Tools to navigate to any point during the execution and examine details about what occurred at that point in the execution.






Top