Click here to Skip to main content
15,879,474 members
Articles / General Programming / Debugging

10+ Powerful Debugging Tricks with Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.79/5 (163 votes)
18 Apr 2012CPOL5 min read 390.2K   314   64
The article describes 10 time-saving debugging techniques available in Visual Studio.

Introduction

Debugging is a major part of the development lifecycle. Sometimes challenging, sometimes puzzling, sometimes annoying, one for sure - it is unavoidable for any not-so-trivial program. The progress of debugging tools over the last years has made many debugging tasks much easier and less time-consuming.

This article summarizes ten debugging tricks and techniques that can save you a lot of time when using Visual Studio.

1. Hover Mouse to Evaluate Expression

Image 1

Debugging can be challenging. Stepping through a function to understand what went wrong, looking through the call stack to see where did that value come from... In either case, adding watch expressions or looking through a list of locals can take quite a time. However, things get easier if you just point your mouse at a variable of interest. Moreover, classes and structures will be expanded with one click, allowing to find the field you need quickly and conveniently.

2. Change Values on-the-fly

Image 2

Debugger is much more than a tool for analyzing crashes and weird behavior. Many bugs can be prevented by stepping through a freshly written function and checking that it behaves as expected. Sometimes, you are curious "would the function behave correctly if this condition was true?". And in most cases, it does not mean changing the code and restarting. Just hover the mouse over a variable, double-click at the value and type in the new one!

3. Set Next Statement

Image 3

One typical debugging scenario is analyzing why a function calls fail by going through the function step-by-step. And what do you do when you've just discovered that a function called another function that returned an error? Restart debugging? There's a better idea: just drag the yellow statement marker to the line you want to be executed next, like the function that has just failed, and then simply step in. Simple, isn't it?

4. Edit and Continue

Image 4

Debugging a complex program, or a plugin? Found an error, but don't want to lose time stopping, rebuilding and restarting again and the function is called too often to use the previous trick each time? No problem, just fix the bug in-place and continue stepping. Visual Studio will modify your program and continue debugging with no need to restart.

Beware, though. Edit-and-continue has a bunch of known limitations. First, it won't work for 64-bit code. If it refuses to work for your C# app, go to project settings, Build page, then select "x86" as Platform Target. Don't worry, the Platform Target for the Release configuration is separate from the debug one and can still be "Any CPU".

Second, the edit-and-continue changes should be local, i.e., within one method. If you change the method signature, add new methods or classes, you'll have to restart the app, or undo the changes to continue. Changing methods containing lambda expressions implies modifying the auto-generated delegate classes and thus prevents continuing.

5. A Convenient Watch Window

Image 5

Probably, every modern debugger has a watch window. However, what's really cool about the Visual Studio one is how easily you can add and remove variables there. Just click at the empty line, type your expression and press Enter. Or simply press delete button to remove an expression that is no longer needed.

Moreover, the information you can get from the watch window is not limited to "normal" variables. You can enter $handles to track the amount of handles opened by your application (and find leaks easier), $err to see the error code of the last function (and then use Tools->Error Lookup to get a meaningful description), or @eax (@rax for 64-bit code) to see the register containing the return value of a function.

6. Annotated Disassembly

Image 6

Optimizing the performance of the critical parts of your program can be much easier using the interactive disassembly mode. Visual Studio shows you the instructions corresponding to every line of your code and allows running the code step-by-step, as well as setting breakpoints at arbitrary locations. And, of course, the expression evaluation and modification will work just like for the C++ code.

7. Threads Window with Stacks

Image 7

Debugging multi-threaded applications can be painful. Or it can be fun. Depends on your debugger. One really nice feature of Visual Studio 2010 is the stack view in the threads window. You can get a convenient overview of all your threads and navigate through their call stacks directly from the window.

8. Conditional Breakpoints

Image 8

If you're trying to reproduce a rare event and getting too many false positives with your breakpoints, you can easily make them conditional! Simply specify the condition for a breakpoint and Visual Studio will automatically ignore the breakpoint when the condition does not hold.

9. Memory Window

Image 9

Some bugs are caused by incorrect structure definitions, missing alignment attributes, etc. Seeing the raw memory contents simplifies locating and fixing those bugs. Visual Studio features a convenient memory window that can interpret the values as 8/16/32/64-bit numbers, as well as floating-point numbers and allows changing them on-the-fly by simply typing the new values over the old ones like in a text editor.

10. Go To Definition

Image 10

One last feature to mention is not directly related to debugging, rather to exploring big projects. If you are trying to find a bug in some code you have not written yourself, having a quick answer to "what is this type" or "what does this function do" can save you a lot of time. And Visual Studio does this with ease via the Go To Definition command.

11. Command Window

Image 11

This eleventh trick has been suggested by chaau and it can indeed save you a lot of time. Visual Studio supports a Command Window that can be activated through the View->Other Windows->Command Window menu. Once active, you can type various commands to automate the debugging. For example, you can easily evaluate an MFC COleDateTime variable by running a simple command:

? dt.Format("%Y-%m-%d %H:%M:%S")  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Sysprogs UG (haftungsbescrhänkt)
Germany Germany
Ivan Shcherbakov is a cofounder of the Sysprogs UG (haftungsbeschränkt), the company developing custom software, drivers and firmware and delivering VisualGDB - a Visual Studio plugin that allows using Visual Studio to build applications with GCC and debug them with GDB.

The main goal of VisualGDB is to combine the time-saving Visual Studio debugging experience with the wide application range of GDB, such as Linux applications and Embeddeed firmware.

A special Android Edition of VisualGDB allows building and debugging native Android code with Visual Studio easily and smoothly.

Comments and Discussions

 
GeneralRe: [My vote of 2] Blatant advertising of a commercial product (having free competition BTW.) Pin
coder83422-Dec-12 13:08
coder83422-Dec-12 13:08 
GeneralMy vote of 5 Pin
Purushotham Agaraharam19-Nov-12 18:30
Purushotham Agaraharam19-Nov-12 18:30 
GeneralMy vote of 2 Pin
Lowell Walmsley30-Oct-12 5:41
Lowell Walmsley30-Oct-12 5:41 
GeneralThank you very much! Pin
Ross MacFarlane12-Oct-12 4:56
Ross MacFarlane12-Oct-12 4:56 
GeneralSkilled worker already? Pin
iwbm3-Oct-12 6:21
iwbm3-Oct-12 6:21 
GeneralNice, but almost nothing new Pin
xawari11-Jun-12 5:47
xawari11-Jun-12 5:47 
QuestionMore tips Pin
Andrew Phillips6-Jun-12 15:26
Andrew Phillips6-Jun-12 15:26 
QuestionGood work. Pin
neriacompany1-Jun-12 4:02
neriacompany1-Jun-12 4:02 
Good work.
NERIA COMPANY
info@neriacompany.com
Thank you!
ipad games | kiralık tekneler | rc helicopter | Notebook Tamiri

GeneralMy Vote of 1 Pin
MacSpudster31-May-12 11:20
professionalMacSpudster31-May-12 11:20 
Question$err Pin
Joseph Mccornell29-May-12 1:03
Joseph Mccornell29-May-12 1:03 
QuestionVery useful tips. Thanks Pin
dudoo28-May-12 14:12
dudoo28-May-12 14:12 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey17-May-12 22:51
professionalManoj Kumar Choubey17-May-12 22:51 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA12-May-12 18:31
professionalȘtefan-Mihai MOGA12-May-12 18:31 
GeneralMy vote of 5 Pin
VMAtm11-May-12 21:38
VMAtm11-May-12 21:38 
GeneralMy vote of 5 Pin
Monjurul Habib10-May-12 19:32
professionalMonjurul Habib10-May-12 19:32 
GeneralMy vote of 3 Pin
itaitai8-May-12 1:42
professionalitaitai8-May-12 1:42 
Questiontip Pin
Dave Kerr7-May-12 21:31
mentorDave Kerr7-May-12 21:31 
GeneralGreat tips Pin
BigDaveDev11-Apr-12 19:27
BigDaveDev11-Apr-12 19:27 
QuestionEdit and Continue Pin
asit4u1111-Apr-12 3:01
asit4u1111-Apr-12 3:01 
AnswerRe: Edit and Continue Pin
Ivan Shcherbakov18-Apr-12 21:09
Ivan Shcherbakov18-Apr-12 21:09 
QuestionAttaboy! Pin
David Crow10-Apr-12 5:39
David Crow10-Apr-12 5:39 
QuestionFantastic! Pin
Member 42896139-Apr-12 23:29
Member 42896139-Apr-12 23:29 
AnswerRe: Fantastic! Pin
Member 428961310-Apr-12 0:36
Member 428961310-Apr-12 0:36 
AnswerRe: Fantastic! Pin
Ivan Shcherbakov18-Apr-12 21:10
Ivan Shcherbakov18-Apr-12 21:10 
GeneralExcellent Pin
Suchi Banerjee, Pune8-Apr-12 22:36
Suchi Banerjee, Pune8-Apr-12 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.