Improved Debugging Experience in Visual Studio 2015

Scope

This article is about the improvements in Visual Studio 2015 that improves the debugging experience by making it easier and quicker to identify bugs.

IntelliTrace

IntelliTrace can be used to record events and method calls to your application that allows you to examine its state (call stack and local variable values) at various points of the execution.



In Visual Studio 2015, the presentation of IntelliTrace has improved considerably. You can view IntelliTrace events just as you start debugging and also now you can view a timeline of all events in your application just as you break all the events.


IntelliTrace showing a list of all events and at which time they occurred.



The debugger events present all the events that occurred in the application using a timeline.

Historical Debugging

Historical Debugging allows you to view the state of your application when the code executes.

To activate historical debugging, double-click on the Diamond icon in the events windows.



New Exception Settings Windows

In previous versions of Visual Studio, when you wanted to configure exception settings you would need to go to the modal, slow-opening, hard-to-search Exceptions Dialog.

Press Ctrl + Alt + E to open the Exception Settings Window in the previous versions of Visual Studio:

Exception Settings image

In Visual Studio 2015, it's just at the bottom-right menu, go to the “Exception Settings” tab that opens instantaneously compared with the previous Visual Studio versions.

Exception Settings

You can use the Exception Settings window to specify which exceptions (or sets of exceptions) will cause the debugger to break and at which point you want it to break. You can add or delete exceptions, or specify exceptions to break on.

This feature is really handy in applications where all exceptions are caught that makes it difficult to identify which piece of code is causing the error. Thus, by selecting the appropriate exception in the Exceptions Window, this can make the application break and therefore identify which code is causing the error straight away.

Breakpoint Configuration

If you use Visual Studio to develop code, the chances are good that you set breakpoints on a regular basis as part of debugging.

In Visual Studio 2015, it is now easier to find, use and configure breakpoints associated with a specific line of source code.

source code

The first noticeable change is that when hovering on a breakpoint, there is now a small menu that allows you to open the new breakpoint window and a toggle button that allows you to turn a breakpoint on/off.

Breakpoint Settings

The Breakpoint settings shows the following two categories:
  • Conditions: Control when the breakpoint is hit. When to break in the code.
  • Actions: Control what happens when all of the breakpoint conditions are satisfied.

    Breakpoint settings

Conditions

There are the following 3 conditions that can be used to allow the code to break:

  1. Conditional Expression: Break only when conditions you specify are met.

  2. Hit Count: Break only after the breakpoint has been hit a certain number of times.

  3. Filter: Break when the breakpoint is hit on a specific thread, process, or machine and are useful for debugging code running in parallel.

    debugging code

Example: Debugging in a loop.

  1. for (int i = 0; i < 5; i++)  
  2. {  
  3.    Console.WriteLine("Do Stuff");  
  4. }  
Assume that you need to debug only when I = 2. In this scenario, Hit Count can be used as in the following image:

debug

The preceding condition will make the code break only after it has been executed 3 times, thus, at I = 2.

code

Actions

Actions print a message to the output window and are capable of automatically resuming execution. These are useful for doing temporary logging when you need to trace something and don't want to have to break and manually track values.

Let's say we need to log the name of the person and its occupation only when MaritalStatus is 2.
  1. clubMemberModel.Id = 0;  
  2. clubMemberModel.Name = txtName.Text.Trim();  
  3. clubMemberModel.DateOfBirth = dtDateOfBirth.Value;  
  4. clubMemberModel.Occupation = (int)cmbOccupation.SelectedValue;  
  5. clubMemberModel.HealthStatus = (int)cmbHealthStatus.SelectedValue;  
  6. clubMemberModel.MaritalStatus = (int)cmbMaritalStatus.SelectedValue;  
  7. clubMemberModel.Salary = txtSalary.Text.Trim();  
We can configure the breakpoints as in the following image.

Notice that the checkbox Continue execution is checked to allow the application to resume automatically after the logging I completed.

Also, to access variables when logging the messages, they should be written between { }.

PerfTips

Performance Tools

The new set of diagnostics tools are great in helping solve performance issues. The new tools include PerfTips, Memory and CPU usage monitoring.

PerfTips

When debugging, you can view the amount of time each line of code takes to execute. This helps considerably when troubleshooting performance issues and doing optimizations.

CPU and memory usage

The diagnostics not only allows you to monitor events on your application, it also allows you to monitor the CPU and memory usage.

diagnostics window

A cool thing is that you can also go into your code line-by-line and view the memory/CPU usage as each line of code executes.

Monitor Change in Memory Usage

One of the new features is the ability to view the memory usage on a timeline in the diagnostics window when debugging.

memory image

But you can also compare the memory usage at 2 points in time using snapshots.

To take a snapshot, go to the Memory Usage tap and click on “take snapshot”.



This will show the time the snapshot was taken, the number of objects in memory and also the Heap Size, then you can take another snapshot at a different point of time. For example, after a method that you suspect is using a lot of memory.



The second snapshot was taken after 0.68 seconds and we can see an increase in memory usage.

To view the objects that are in memory, click on the number of increased objects to the left of the Red arrow.

objects that are in memory

This will show you the count difference of each object in memory and also their difference in size. From here you may also go to the definition of each object to try to identify what is exactly causing the memory leak.

Advanced Diagnostics

To use more diagnostics tools, click on Debug > Start Diagnostics Tools without Debugging.

 

From here, there is a wider range of tools that can be used, such as CPU and GPU monitoring. Select the one that you need and click on Start.

 

Once you click on Start, use the application and Visual Studio will record all the necessary information that it needs.

 

Once you are done, click on Stop and Visual Studio will generate a report of all the events that occurred and the amount of CPU that each of them used.

 
 
References
  1. IntelliTrace Features
  2. The New Exception Settings Window in Visual Studio 2015
  3. Managing Exceptions with the Debugger
  4. New Breakpoint Configuration Experience in Visual Studio 2015

Up Next
    Ebook Download
    View all
    Learn
    View all