Software Developers
Showing results for 
Search instead for 
Do you mean 

8 Visual Studio tips for a better debugging experience

amitbet on ‎05-13-2013 03:33 AM


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’



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).




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.

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).


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.


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:


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


.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.



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


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:

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:

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]

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



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

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 into c# about 7 years ago, where i spent a good amount of my time in Performance tweaking & memory debugging, as well as designing new solutions and hacking at old ones to stay in line. Computers never cease to amaze me, and i'm glad to have found a field where i get paid to do what i enjoy. some of my tools and thoughts are also stored in codeproject, and you are welcome to check them out there.

Nov 29 - Dec 1
Discover 2016 London
Learn how to thrive in a world of digital transformation at our biggest event of the year, Discover 2016 London, November 29 - December 1.
Read more
Each Month in 2016
Software Expert Days - 2016
Join us online to talk directly with our Software experts during online Expert Days. Find information here about past, current, and upcoming Expert Da...
Read more
View all