8 Visual Studio tips for a better debugging experience

Introduction

Debugging is an integral part of the development process. Some of us can even say we spend more time debugging our code than developing.

I’ve gathered a few tricks that I have learned in the last years from colleagues, the Internet, and other sources.

 

Tip #1 How to evaluate an object that is out of scope

There are certain cases in which we want to see our object even when it is out of scope. In doing so, we can see if for some reason its state has changed or if it was garbage collected.

This can be done easily with the Make Object ID feature. This feature is used in the following way:

1)     During debugging, watch your variable in the Watch or Locals pane.

2)     Right-click the object and select ‘Make Object ID’

 

pablo1.png

 

3)     After giving the object an ID using this command, you can see the value of your object by typing 1# at any point during your debugging session, even when you can’t reach the member by other means (i.e. when you’ve moved out of scope).

 

 

pablo2.PNG

 

For subsequent objects added with the Make Object ID command, the number assigned to the object increases sequentially. (2#, 3#, etc)

 

 

Tip#2 How to correctly attach to a spawned process

Usually a debugging session starts by pressing F5 or Attach to Process, However, (unfortunately) the real world is more complicated. Sometimes our application is launched from another process or we want to debug the very beginning of the application/program launch in a production machine and all we have is some source code and Visual Studio (if we are lucky).

In one case, we try to debug when our program/application is launched by another process.

If we control the launch of the process, the solution is quite simple. Since we have Visual Studio, we can create a dummy project and just enter the path of our program in the “Start External Program” text box under the Debug tab of the project properties.

pablo3.PNG

In the other case when our program is launched by another process we can use gflags, a tool that comes with Windows Debugging tools.

To use the gflags tool, do the following:

1)     Open gflags.exe.

2)     Go to the Image Tab.

3)     Type the name of the process and press TAB.

4)     Check the Debugger checkbox and type the Visual Studio path appending the /debugexe switch (for example "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe" /debugexe).
You can also use any other debugging tool (such as windbg).

 

pablo4.png

5)     Press OK, As a result, each time the process starts it will stop on debug, When it stops, just enter your breakpoints and press Run.

Don’t forget to clear the debugger checkbox when you don’t need it anymore or the process will never start normally.

A third approach, if you can quickly compile your code and move it the machine where you debug, is to use the Debugger.Break(); function in the line you want to start debugging.

Each time this line is reached you will be prompted to open Visual Studio or another debugger,

 

 

Tip#3 How to find the location of the symbols

As described above, debugging on a non-development environment is difficult but is even more difficult when you don’t have the matching symbols (pdb). This happens when you have many different builds on different machines or in a customer environment with several patches installed.

Fortunately the path of the pdb can be easily read if we open the dll or exe with a text editor.

 

pablo5.png

Usually this path will be the build server,If it was not moved from the build server, you will be able to find the correct symbols.

 

 

Tip#4 How to attach to Managed Code during debugging.

When attaching to a process we are asked which code type we want:

 

pablo6.png

It can be annoying to discover that you forgot to select Managed Code and selected only Native and need to restart your debugging. In order to avoid this error, you can just open the immediate window pane and enter 

 

.load C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos.dll

 or:

.load sos

 

(As you do in windbg).

Now you can debug Managed code.  Note this will only work if you enable debugging unmanaged code in the project.

 

Tip#5 How to create breakpoints without opening the source code

This tip is for the lazy ones. You can save time when you set a breakpoint by adding a breakpoint by name, instead of opening the file and selecting the line you want. To do so, open the breakpoints pane (CTRL D, B) and click on the New menu, this dialog will appear.

 

 

pablo7.png

Using this dialog, you can:

  • Select a specific method by typing the full namespace, such as Namespace1.Namespace2.Class1.Method1
  • Break on all the methods that have a specific name, such as Method1

Note this feature works both for managed and unmanaged code.

Using this method also helps to break on a method for which we do not have source code (such as a method from a .Net framework or a win32 API if you have Microsoft symbols). As a result, we can use the Call Stack pane to see where we initiated the call to the method we have a breakpoint in our code.

This feature dramatically slows the loading of symbols, so it should be used in moderation. 

 

Tip#6 How to break on Automatic properties

Adding a breakpoint as you do in regular code on an Automatic property is not possible.

 

public string Name
{
       get; 
       set;
}

 

Of course there might be no point of putting a breakpoint on such a property since there is no logic here. However, you may want to why this property does not have the value you expect and discover the source of the wrong value.

Based on the previous tip, you can add a breakpoint by name by adding the get_ or set_ prefix for the getter and setter respectively, for example if we are interested on the setter of the property Name we will add a breakpoint set_Name as shown below:

pablo8.png

And once the code breaks, use the call stack to navigate back to your code.

 

Tip#7 How to add a breakpoint with trace

This is a nice feature that lets you print messages when a breakpoint is reached.

Add a breakpoint and right-click on it and select When Hit menu, you’ll see this dialog:

pablo9.png

In this dialog you can add your message using several special keywords such as $FUNCTION, $CALLER, etc.

 

Tip#8 How to debug optimized code

Ever tried to debug optimized code? This could be frustrating, as you may want to know a value of a variable and instead you get a message saying “Cannot evaluate expression because the code of the current method is optimized”.  One solution would be to compile the code without optimization. However, as mentioned before, trying to debug in a real world situation is complicated and this compilation isn’t always an option. Want we can do is tell the CLR JITTER not to optimize the binaries without rebuilding our code.

To do this, use the following steps:

1)      Create an .ini file with the same name as the assembly (for myassembly.dll create a myassembly.ini, for myexec.exe create myexec.ini)

2)      Open the ini file and add the following text:

[.NET Framework Debugging Control]
GenerateTrackingInfo=1
AllowOptimize=0

3)      Save the file. Then, reload the application and enjoy your debugging session!

 

Conclusions

Visual Studio is a very powerful debugger but like most programs, many of the advanced features are overlooked. Spend some time getting to know your debugger intimately, and it will 

 

Written by Pablo Retyk

Leave a Comment

We encourage you to share your comments on this post. Comments are moderated and will be reviewed
and posted as promptly as possible during regular business hours

To ensure your comment is published, be sure to follow the Community Guidelines.

Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
Type the characters you see in the picture above.Type the words you hear.
Search
Showing results for 
Search instead for 
Do you mean 
About the Author
I've been all over the coding world since earning my degrees have worked several years in c++ and then several in java, finally setteling i...
Featured


Follow Us
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.