You are on page 1of 27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

8,470,891 members and growing!

Email

Password

Sig i

J i

Remember me? Lost password?

Home

Articles
C#

Quick Answers
Beginners

Discussions

Learning Zones

Features

Help!

The Lounge

ho

e c ha p deb gge i

al

dio

Languages

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide


By Abhijit Jana | 6 May 2010

Licence First Posted Views Bookmarked

C POL 6 Ma 2010 175,864 303 times

See Also
More like this More by this author

WinXP Win2003 Vista C #3.0 C # ASP.NET Windows Visual-Studio Architect Dev , +

Describes all debugging features like Breakpoints, DataTips, Watch Windows, Multithreaded Debugging, Parallel Program Debugging and IntelliTrace Debugging
Article Browse Code Stats Revisions (6)
128

Table of Contents
Introduction How to Start? Breakpoints Debugging with Breakpoints Step Over Step Into Step Out Continue Set Next Statement Show Next Statement Labeling in Break Point Conditional Breakpoint Import / Export Breakpoint Breakpoint Hit Count Breakpoint When Hit Breakpoint Filter Data Tip Pin Inspect Value During Debugging Drag-Drop Pin Data Tip Adding Comments Last Session Debugging Value Import Export Data Tips Change Value Using Data Tips Clear Data Tips Watch Windows Locals Autos Watch Creating Object ID Immediate Window Call Stack Debugging Multithreaded Program Exploring Threads Window Flag Just My Code Break Point Filter - Multithread Debugging Debugging Parallel Program Parallel Task and Parallel Stacks Debugging with IntelliTrace Overview Mapping with IntelliTrace Filter IntelliTrace Data Useful Shortcut Keys For VS Debugging Further Study Summary
codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

Related Articles
Beginner's Walk - Web Development Excellent Debugging Features in Visual Studio 2010 Beta 2 Implementing a Basic Hello World WC F Service Setting Up Kernel Mode Debugger in Windows

1/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj


Debugging C lassic ASP ( VBScript ) in Visual Studio 2008 XML Visualizer v.2 Advanced Debugging in Visual Studio A Generic List and Dictionary Debugger Visualizer for VS.NET Remote IIS Debugging : Debug your ASP.NET Application which is hosted on "Remote IIS Server" Lexware Assembly Reference Tool for Visual Studio 2005 / 2008 Debugging Made Easier with Mole 2010 Excerpt from Microsoft Visual Studio 2010 Unleashed Tips on Debugging : Using DebuggerHidden attribute SQL Server Schema C omparison Tool in Visual Studio 2010 List of freely available programming books ZoomSliderVSX - Visual Studio Extension (VSX) 2010 A Brief Discussion On Visual Studio 2010 Top Features Numbered Bookmarks - Visual Studio Extension VSX 2010 C omObject Visualizer Visual Studio Project MRU List Editor III

Introduction
In the software development life cycle, testing and defect fixing take more time than actually code writing. In general, debugging is a process of finding out defects in the program and fixing them. Defect fixing comes after the debugging, or you can say they are co-related. When you have some defects in your code, first of all you need to identify the root cause of the defect, which is called the debugging. When you have the root cause, you can fix the defect to make the program behavior as expected. Now how to debug the code? Visual Studio IDE gives us a lot of tools to debug our application. Sometimes debugging activity takes a very long time to identify the root cause. But VS IDE provides a lot of handy tools which help to debug code in a better way. Debugger features include error listing, adding breakpoints, visualize the program flow, control the flow of execution, data tips, watch variables and many more. Many of them are very common for many developers and many are not. In this article, I have discussed all the important features of VS IDE for debugging like Breakpoint, labeling and saving breakpoints, putting conditions and filter on breakpoints, DataTips, Watch windows, Multithreaded debugging, Thread window, overview of parallel debugging and overview of IntelliTrace Debugging. I hope this will be very helpful for beginners to start up with and for becoming an expert on debugging. Please note, targeted Visual Studio version is Visual Studio 2010. Many things are common in older versions, but many features such as Labeling breakpoint, Pinned DataTip, Multithreaded Debugging, Parallel debugging and IntelliTrace are added in VS 2010. Please provide your valuable suggestions and feedback to improve my article.

How to Start?
You can start debugging from the Debug menu of VS IDE. From the Debug Menu, you can select "Start Debugging" or just press F5 to start the program. If you have placed breakpoints in your code, then execution will begin automatically.

The Dail Insider


30 free programming books Daily News: Signup now.

Fig re: S ar Deb gging

There is another way to start the debugging by "Attach Process". Attach process will start a debug session for the application. Mainly we are very much familiar with the attach process debugging for ASP.NET Web Application. I have published two different articles on the same on CodeProject. You may have a look into this. Debug Your ASP.NET Application that Hosted on IIS Remote IIS Debugging: Debug your ASP.NET Application which is hosted on "Remote IIS Server" We generally start debugging any application just by putting breakpoint on code where we think the problem may occur. So, let s start with breakpoints.

Breakpoints
Breakpoint is used to notify debugger where and when to pause the execution of program. You can put a breakpoint in code by clicking on the side bar of code or by just pressing F9 at the front of the line. So before keeping a breakpoint, you should know what is going wrong in your code and where it has to be stopped. When the debugger reaches the breakpoint, you can check out what's going wrong within the code by using a different debugging tool.

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

2/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Fig re: Se Breakpoin

Deb gging

i h B eakpoin

Yo ha e al ead e a b eakpoin in o code he e o an o pa e he e ec ion. And no a he p og am b p e ing "F5". When he p og am eache he b eakpoin , e ec ion ill a oma icall pa e. No o ha e e e al op ion o check o code. Af e hi ing he b eakpoin , b eakpoin line ill ho a ello colo hich indica e ha hi i he line hich ill e ec e ne . No o ha e e e al command a ailable in b eak mode, f he deb gging. ing hich o can p oceed fo

Fig re: Breakpoin Toolbar

S ep O e Af e deb gge hi he b eakpoin , o ma need o e ec O e " [ F10 ] command i ed o e ec e he code line b c en l highligh ed line and hen pa e. If o elec F10 highligh ed, he e ec ion ill op af e he ne line of he e ec e he en i e me hod a a ime. e he code line b line. "S ep line. Thi ill e ec e he hile a me hod call a emen i calling a emen . S ep O e ill

Fig re: S ep O er - F10

S ep In o Thi i imila o S ep O e . The onl diffe ence i , if he c en highligh ed ec ion i an me hod call, he deb gge ill go in ide he me hod. Sho c ke fo S ep In o i "F11".

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

3/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: Step Into - F11

Step Out This is related when you are debugging inside a method. If you press the Shift - F11 within the current method, then the execution will complete the execution of the method and will pause at the next statement from where it called. Continue It's like run your application again. It will continue the program flow unless it reaches the next breakpoint. The shortcut key for continue is "F5". Set Ne t Statement This is quite an interesting feature. Set Next Statement allows you to change the path of e ecution of program while debugging. If your program paused in a particular line and you want to change the execution path, go to the particular line, Right click on the line and select "Set Ne t Statement" from the context menu. You will see, execution comes to that line without executing the previous lines of code. This is quite useful when you found some line of code may causing breaking your application and you don t want to break at that time. Shortcut key for Set Next Statement is Ctrl + Shift + F10.

Figure: Set Ne t Statement

Sho

Ne t Statement [ Ctrl+* ]

This line is marked as a yellow arrow. These lines indicate that it will be executed next when we continue the program.

Labeling in Break Point


This is the new feature in VS 2010. This is used for better managing breakpoints. It enables us
codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn 4/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

to better group and filter breakpoints. It's kind of categorization of breakpoints. If you are having different types of breakpoints which are related with a particular functionality, you can give their name and can enable, disable, filter based on the requirements. To understand the whole functionality, let's assume that you have the below code block which you want to debug.
C ollapse C opy C ode

casPorm ls rga { sai vi Mi(tig]ag) ttc od ansrn[ rs { srn[ srae ={"ae" "ae" "ae" "ae" "ae" "ae" tig] tNms Nm1, Nm2, Nm3, Nm4, Nm5, Nm6 ; frah(tignm i srae) oec srn ae n tNms { CnoeWieienm) osl.rtLn(ae; / Bekon / raPi ittm =4 n ep ; fr(n i=1 i< 1;i+ o it ; = 0 +) { i ( >6 f i ) tm =5 ep ;

pbi sai vi Mto1) ulc ttc od ehd( { CnoeWieie"ra Piti Mto1) osl.rtLn(Bek on n ehd";

/ Bekon / raPi

pbi sai vi Mto2) ulc ttc od ehd( { CnoeWieie"ra Piti Mto2) / Bekon osl.rtLn(Bek on n ehd"; / raPi CnoeWieie"ra Piti Mto2) / Bekon osl.rtLn(Bek on n ehd"; / raPi

pbi sai vi Mto3) ulc ttc od ehd( { CnoeWieie"ra Piti Mto3) / Bekon osl.rtLn(Bek on n ehd"; / rapi

If you run the program, execution will pause on the first breakpoint. Now see the below picture, where you have the list of breakpoints.

Fig

e: B eakpoin Li

In the given picture label column in blank. Now, see how you can set the label on break point and what is the use of it. To set label for any breakpoint, you just need to right click on the breakpoint symbol on the particular line or you can set it directly from breakpoint window.

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

5/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Fig

e: Se ing B eakpoin Label

Righ Click on Breakpoint, Click on the Edi Label link, you can add the label for each and every breakpoints. As per the sample code, I have given very simple understandable names for all the breakpoints.

Fig

e: Adding B eakpoin Label

Let's have a look at how this labeling helps us during debugging. At this time, all the break points are enabled. Now if you don t want to debug the m h d , in a general case you need e o2 to go to the particular method and need to disable the breakpoints one by one, here you can filter/search them by label name and can disable easily by selecting them together.

Fig

e: Fil e B eakpoin U ing Label

This is all about the Breakpoint labeling. The example I have shown to you is very basic, but it is very much useful when you have huge lines of code, multiple projects, etc.

Condi ional B eakpoin


Suppose you are iterating through a large amount of data and you want to debug a few of them. It means you want to pause your program on some specific condition. Vi al S dio
codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn 6/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Breakpoints allow you to put conditional breakpoint. So if and only if that condition is satisfied, the deb gger will pause the execution. To do this, first of all you need to put the breakpoint on a particular line where you want to pause execution. Then just "Right Click" on the "Red" breakpoint icon. From there you just click on "Condition" .

Fig

e: Se B eakpoin Condi ion

By clicking on the "Condition" link from context menu, the below screen will come where you can set the condition for breakpoints.

Fig

e: B eakpoin Condi ion Se ing

Let's assume that you have the following code block:


C ollapse C opy C ode

casPorm ls rga { sai vi Mi(tig]ag) ttc od ansrn[ rs { srn [ srae ={"ae""ae" "ae" "ae" "ae" "ae"; tig ] tNms Nm1,Nm2, Nm3, Nm4, Nm5, Nm6 frahsrn nm i srae) oec(tig ae n tNms { CnoeWieienm) / Bekon i h e osl.rtLn(ae; / api e

You have a breakpoint on C n o e W i e i e ) statement. On running of the program, osl.rtLn( execution will stop every time inside that f r e c statement. Now if you want your code to o- ah break only when n m = N m 3 . What needs to be done? This is very simple, you need to ae"ae" give the condition like n m . q a s " a e " . aeEul(Nm3)

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

7/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: Set Breakpoint Condition

Check he B eakpoin S mbol. I ho ld look like a pl hich indica e he condi ional b eakpoin .

(+)

mbol in ide he b eakpoin ci cle

Figure: Conditional Breakpoint S mbol

Af e e p of he condi ion of o b eakpoin , if o n he applica ion o deb g i , o ill ee e ec ion of p og am i onl pa ed hen i a i fied he gi en condi ion i h b eakpoin . In hi ca e hen a e " a e " =N 3.

Figure: Conditional Breakpoint hit

intellisense In Condition Te t Bo : The b eakpoin condi ion hich I ha e demon a ed he e i e imple and can be i en ea il in ide condi ion e bo . Some ime , o ma need o pecif oo big o comple condi ion al o. Fo ha , o do no need o o , VS IDE p o ide he in elli en e i hin he condi ion e bo al o. So hene e o a e going o pe an hing in ide he condi ion bo , o ill feel like ping in ide he edi o i elf. Ha e a look in o he belo pic e.

Figure: intellisense in condition te tbo

I ha e almo co e ed all abo he condi ional b eakpoin e cep one hing. In condi ion indo o ha e een ha he e a e o op ion a ailable: 1. Is Tr e and 2. Has Changed We ha e al ead een ha i he e of "Is Tr e" op ion. "Has changed" i an o b eak he code if ome al e ha changed fo ome pa ic la al e.
codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

ed

hen o

8/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Import / E port Breakpoint


This is another interesting feature where ou can save breakpoints and can use them in future. Visual Studio saves breakpoints in an XML Format. To save the breakpoints, ou just need to click on the "E port" button in breakpoint window.

Fig

e: Sa e B eakpoin

You can use the saved XML file for the future and ou can pass the same to other developers. You can also save breakpoints based on the search on labels. Let's have a quick look inside the content of the XML File. The XML file is collection of B e k o n aPi tag within B e k o n C l e i n Each breakpoints tag contains information about the particular api olc o. breakpoint like line n mber, is enabled, etc.

Fig

e: B eakpoin XML File

If ou delete all the breakpoints from our code at an time, ou can easil import them b just clicking on the "I p m o " breakpoints button. This will restore all of our saved breakpoints. Note: Breakpoint Import depends on the line number where ou have set our breakpoint earlier. If our line number changed, breakpoint will set on the previous line number onl , so
codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn 9/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

you will get breakpoints on unexpected lines.

Breakpoin Hi Co n
Breakpoint Hit Count is used to keep track of how many times the deb gger has paused at some particular breakpoint. You also have some option like to choose when the deb gger will stop. "Breakpoin Hi Co n " window having the following: 1. 2. 3. 4. Break Break Break Break always when the hit count is equal to a specified number when the hit count is a multiple of a specified number when the hit count is greater than or equal to a specified number.

Fig

e: B eakpoin Hi Co n

By default, it's set to always. So, whenever the breakpoints hits, hit count will increase automatically. Now we can set some condition as earlier mentioned. So based on that condition, breakpoint will hit and counter will be increased.

Fig

e: B eakpoin Hi Co n Op ion

Let's explore it with the sample code block below:


C ollapse C opy C ode

fr(n i=0 i< 1;i+ o it ; = 0 +) { CnoeWieieiTSrn() / Bekon osl.rtLn(.otig); / rapi

If you want your code to break when hit count is a multiple of 2, you need to select the third option from the dropdown list. After that, your code line should break only when the hit count is 2,4,6,... etc.

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

10/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Fig

e: Hi Co n Condi ion

Till no , I hope o a e e m ch clea abo b eakpoin , labeling, hi co n , e c. No a look a ha i "Breakpoin When Hi " op ion.

ha e

Breakpoin When Hi
Thi i apa f om o no mal deb g ep , o an o do ome hing el e hile b eakpoin i hi , like p in a me age o n ome mac o . Fo ho e pe of cena io , o ma go fo hi op ion. Yo can open hi indo b j igh clicking on b eakpoin .

Fig

e: When B eakpoin I Hi

The fi hing ha o need o no ice i he mbol of b eakpoin . B eakpoin mbol ha changed o a diamond and o can al o check o he ool ip me age hich indica e ha i i going o do hen e ec ion eache he e.

Fig

e: When B eakpoin I Hi

Breakpoin Fil er
Yo can e ic he b eakpoin o hi fo ce ain p oce e o h ead . Thi i e hile o a e dealing i h m l i h eading p og am. To open he fil e indo , o click on he b eakpoin and elec "Fil er".
codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

emel helpf l need o igh

11/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Fig

e: B eakpoin Fil e

In he fil e c i e ia, o de c ibed i in de ailed

can e he p oce name, Id, Machine name, Th ead ID, e c. I ha e e in M l i h eading deb gging ec ion.

Da a Tip
Da a ip i kind of an ad anced ool ip me age hich i ed o in pec he objec o a iable d ing he deb gging of he applica ion. When deb gge hi he b eakpoin , if o mo e o e o an of he objec o a iable , o can ee hei c en al e . E en o can ge he de ail of ome comple objec like d a e , d a a l , e c. The e i a "+" ign a a be a ocia ed i h he da aTip hich i ed o e pand i child objec o a iable .

Fig

e: Da aTip

ing Deb gging

Fe

mon h ago, I p bli hed one a icle on VS 2010 Da aTip Deb gging Tip . cool fea e ha o can e d ing deb gging of o applica ion.

He e a e a fe

Pin In pec Val e D

ing Deb gging

While deb gging in Vi al S dio, e gene all ed mo e o e on he objec o a iable o in pec he c en al e. Thi ho he c en da a i em held b he in pec ed objec . B hi i fo a limi ed ime, a long a he mo e i poin ed o ha objec ho e al e ill be a ailable. B in Vi al S dio 2010 Be a 2, he e i a g ea fea e o pin and npin hi in pec ed al e. We can pin a man of an objec and hei b objec al e al o. Plea e ha e a look in o he belo pic e:

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

12/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Fig

e: Pin In pec Val e D

ing Deb gging

When you mouse over on the inspect object, you will get pin icon with each and every object's properties, variable. Click on that pin icon to make it pinned. Unless you manually close these pinned items, they will be visible in the IDE.

Drag-Drop Pin Data Tip


You can easily Drag and Drop the data tip inside the Visual Studio IDE. This is quite helpful when you need to see some object value list in the bottom section of code. You can easily drag those pinned data tips over there.

Fig

e: D ag D op Da a Tip

Adding Comments
You can add comments on the pinned data tip. For providing comments, you need to click on E pand to see the comments button. This will brings up an additional textbox to add comments.

Fig

e: Commen

in Da aTip

Below is some demonstration of Adding comments on pinned inspect value:


codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn 13/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: Adding Comments For Datatips

Last Session Debugging Value


Thi i a he g ea fea d i g he deb ggi g, he f c di g, if e a e a h i he he e f Visual Studio 2010 deb ggi g. If i ed e da a i a e f i ed i e i e ai ed i a e i . I a de e he i ic , i i h he de ai f he a deb ggi g e i be ic e:

Figure: Last Session Debug Value

Import E port Data Tips


Thi fea a de e i i i e i ia ed da a i he i /E b ea a e i a XML fi e. i . Li e B ea i , ca i

Figure: E port Data Tips

The e a ed da a i fi e i e be :

ca

be i

ed d i g a

f i e f

he deb ggi g. The XML

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

14/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Fig

e: XML Con en of Da aTip

Yo

can f

he e plo e he XML file if

an

o kno

mo e de ail abo

i . :)

Change Value Using Data Tips


Da aTip i al o ed o changed he al e hile deb gging. Thi mean i can be ed like a a ch indo . F om he li of Pinned objec , o can change hei al e o ee he impac on he p og am.

Fig

e: Change Val e Wi hin Da a Tip

Clear Data Tips


Yo can clea op ion : he da a Tip b elec ing "Clear Data Tips" f om Deb g men . The e a e o

1. Clea All Da a Tip 2. Clea All Da a Tip Pinned To [ File Name.C ] So if o an o clea all he Da a Tip f om all o e o p ojec / ol ion, j elec he fi op ion. B if o an o dele e he pinned da a ip f om a pa ic la file, hen o need o open ha pa ic la file, he e o ha e he econd op ion o elec . Yo can e en dele e pa ic la pinned da a ip b j igh clicking on i and clicking "Clear".

Fig

e: Clea Da a Tip

Watch Windo

Yo can a i i an in e iga ion indo . Af e b eakpoin ha been hi , he ne hing o an o do i o in e iga e he c en objec and a iable al e . When o mo e ho e on he a iable, i ho he info ma ion a a da a ip hich o can e pand, pin, impo hich I ha e al ead e plained. The e a e a io pe of a ch indo like A o , Local, e c. Le ' ha e a look in o hei de ail .
codeproject.com/ /Mastering-Debugging-in-Visual-Studio-2010-A-Beginn 15/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Local
It automaticall displa s the list of variables within the scope of current methods. If our deb gge currentl hits a particular breakpoint and if ou open the "Autos" window, it will show ou the current scope object variable along with the value.

Fig

e: Local Va iable

These variables are automaticall detect b the VS deb gge during the debugging. Vi al S dio determines which objects or variables are important for the current code statement and based on that, it lists down the "A o " variable. Shortcut ke for the Autos Variable is "C l + D + A".

Fig

e: A

- C l+D, A

Wa ch
Watch windows are used for adding variables as per requirement. It displa s variables that ou have added. You can add as man variables as ou want into the watch window. To add variables in the watch window, ou need to "Righ Click" on variable and then select "Add To Wa ch".

Fig

e: A

- C l+D, W

You can also use D ag and D op to add variables in watch windows. If ou want to delete an variable from watch window, just right click on that variable and select "Dele e Wa ch". From the debug window, ou can also edit the variable value at run time. There are 4 different watch windows available which ou can use parallell .

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

16/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: Multiple Watch Windo

If an of he o a iable of he abo e indo hold he objec in ance, o can ha e a "+" mbol i h he a iable o e plo e he p ope ie and membe of ha objec a iable.

Figure: E panding Watched Variable

C ea ing Objec ID Vi al S dio Deb gge ha ano he g ea f nc ionali he e o can c ea e an objec ID fo an pa ic la in ance of objec . Thi i e m ch helpf l hen o an o moni o an objec a an poin of ime e en if i goe o of cope. To c ea e Objec Id, f om a ch indo o need o igh click on a pa ic la objec a iable and hen need o click on "Make Objec ID".

Figure: Creating Object ID

Af e adding Objec Id i h a pa ic la objec a iable, Vi al S dio add a n me ic n mbe i h "#" i h ha objec o indica e ha one Objec ID ha been c ea ed.

Figure: Object ID Added

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

17/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Immediate Windo
Immediate window is very much common and a favorite with all developers. It's very much helpful in debug mode of the application if you want to change the variable values or execute some statement without impacting your current debugging steps. You can open the Immediate window from menu Debug > Windo > Immediate Windo { Ctrl + D, I / Alt + Ctrl - I . Immediate window has a set of commands which can be executed any time during debugging. It also supports Intellisense. During Debug mode, you can execute any command or execute any code statement from here.

Figure: Basic Immediate Windo

These are very much common features for all the developers, so I am not going into details of each and every command of Immediate window.

Call Stack
These calling where shows features also improve the productivity during debugging. If you have multiple method or nested calling all over your application and during debugging, you want to check from this method has invoked, "Call Stack" comes into the picture. The Call Stack Window that current method call nesting.

Figure: Call Stack

In Call Stack window if you clicked on any of the rows, it will point you to the actual code of line of Visual Studio Code Editor. You can also customize the call stack row view by selecting different types of columns. To customize, Right Click on the "Call Stack" window, and from the context menu, you can select or deselect the option.

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

18/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: Call Stack Customi ation

Call stack is very much important when you have multiple methods call all across the application and one particular method throwing an exception on some particular case. At that time, you can use call stack to see from where this method is getting invoked, based on that you can fix the defect.

Debugging Multithreaded Program


As of now, what I have discussed is all about fundamentals of debugging, knowing debugging tools and their uses. Now let's have a look into the multithreaded scenarios. Here you will see how to work with multithreaded program debugging, where is your current thread, what is the thread execution sequence, what is the state of thread. Before continuing with the demo, let's consider you have the following piece of code which you want to debug.
C ollapse C opy C ode

casTraTs ls hedet { sai vi Mi( ttc od an) { Tra t=nwTra(e TraSatG); hed e hednw hedtr(o) tNm ="hed1; .ae Tra " Tra t =nwTra(e TraSatG); hed 1 e hednw hedtr(o) t.ae="hed2; 1Nm Tra " tSat) .tr(; t.tr(; 1Sat) G(; o) sai vi G( ttc od o) { CnoeWieie"el!) osl.rtLn(hlo";

In the sample code, you have three different threads - Main Thread, Thread 1, Thread 2. I have given a thread name to make you understand better. Now set a breakpoint inside "Go()" and run the application. When debugger hits the breakpoint, Press Ctrl+D,T or Navigate through Debug > Windo > Threads. Threads window will appeared on the screen.

E ploring Threads Windo


After selecting the thread window from debug menu, the following screen will come:

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

19/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Fig

e: De ail

ie

of Th ead

indo

B defa l h ead indo ha ing ID, Managed ID, Ca ego , Name, Loca ion and P io i col mn. A he a , e ec ion pa e a "Main Th ead". "Yello A o " indica e he c en e ec able h ead. Ca ego col mn indica e he ca ego of h ead , like main h ead o o ke h ead. If o check he h ead loca ion, i i no hing b Namespace > Class > Method name. In he diag am, i i ho ing ha he Main Thread ill be e ec ed ne . No o e plo e he ne ep b j p e ing "F5" and ee ha a e he change in h ead indo .

Fig

e: De ail

ie

of Th ead

indo

- Fo Ne

S ep

So af e p e ing F5, i j mped o he ne ep o h ead 1. o can al o check he c en loca ion fo Main Th ead. I a "Sleep/ Wait / Join" , mean ai ing fo ome hing o comple e. Simila l he ne ep ill mo e o o h ead 2. F om he Th ead indo , o can nde and ho ea i i o moni o o h ead ing hi debugger ool. The e i ano he g ea fea e a ailable i hin he h ead indo . Yo can e pand/collapse he Thread Loca ion and can ee ha i ne . Fo e ample, if o e pand he loca ion fo "Main Thread", i ill look like he diag am gi en belo :

Fig

e: E panded Loca ion Vie

Fo Th ead

Flag Just M Code The ample code hich I ha e e plained fo he h ead deb gging i e imple. Wha ill happen if o ha e a h ge code block i h m l iple n mbe of h ead . Then i ill be e diffic l fo o o iden if hich h ead i pa of o code o hich one a e no ela ed. Th ead indo gi e o e ea fea e o e he "Flag" fo all he h ead hich a e pa of o code. Fo ha , o need o j flag o h ead b op ion "Flag Just M Code".

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

20/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: Flag Just M

Code

Break Point Filter - Multithread Debugging Whi e di c i g ab b ea i fi e i b ea i ec i , I aid ha b ea i fi e i e ch he f f M i h eaded deb ggi g de. N hi i he i e e e i . I c e e a e, e ha e h ee h ead Mai Th ead, Th ead1 a d Th ead 2. N ha if a breakpoint to hit onl for "Thread 2". Wha i d ? He e i he e f b ea i fi e . Righ c ic he b ea i , e ec "Filter" f he c e e . N i b ea i fi e i d , eed fi he fi e c i e ia. A e e ie e , eed ecif "T e d a e " h e d 2 . h aNm=T a "

Figure: Breakpoint Filter - Multithreaded Debugging

He e T e d a e a h aNm e f he c i e ia b hich ca fi e , b i e c a e i e T e d D P o e N m , P o e I , e c. Af e h aI, c ae c D fi e , he a ica i a d e he "Th ead " i d .

ca fi e e i g he b ea

Figure: Breakpoint Filter - Multithreaded Debugging

i fi d

ga

e ec

ha

ed d i g he e ec ica i a

f "Thread 2" .

Thi i a ab e hi g f
codeproject.com/

he deb ggi g i h i . Le ' a i ha

i h eaded a he i

. H e ha e ea ed ic "Pa a e Deb ggi g".


21/27

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Deb gging Pa allel P og am


This is another great feature added to Vi al S dio 2010 to debug parallel program. Parallel programming is the new feature coming with .NET 4.0. If you want to learn more about parallel programming, please check here. Now Debugging the parallel program is also a big topic. Here I will give you a basic overview to know about the debugging of parallel program. To discuss about it, let's consider you have the following piece of code:
C ollapse C opy C ode

uigSse; sn ytm uigSse.olcin.eei; sn ytmCletosGnrc uigSse.iq sn ytmLn; uigSse.et sn ytmTx; uigSse.hedn; sn ytmTraig uigSse.hedn.ak; sn ytmTraigTss nmsaePrleakeugn aepc aalTsDbgig { casPorm ls rga { sai vi Mi(tig]ag) ttc od ansrn[ rs { vrts_ =Ts.atr.trNw( = DSmWr(00); a aka akFcoySate() > ooeok100) vrts_ =Ts.atr.trNw( = DSmWr(00) a akb akFcoySate() > ooeok50); vrts_ =Ts.atr.trNw( = DSmWr(00) a akc akFcoySate() > ooeok10); Ts.atl(aka ts_,ts_) akWiAlts_, akb akc;

sai vi DSmWr(n tm) ttc od ooeokit ie { Tra.le(ie; hedSeptm)

To understand the parallel program debugging, we need to be aware about two window options: 1. Pa allel Ta k 2. Pa allel S ack

Pa allel Ta k and Pa allel S ack


Before continuing with parallel tasks and parallel stacks, you have to know about Threads Window which I have already covered. In the given code, you have three different tasks which are doing something and after sometime, all the tasks are put on hold. This is done intentionally to check the status of each task. To test, put a breakpoint on D S m W r ( ooeok) method and run the application. You will see your program execution paused on the breakpoint. After the program break, you can go to Deb g > Windo > Open Pa allel Ta k and Pa allel S ack window. I asked to open both at the same time only because you can i alize what is going on.

Fig re: Breakpoin Fil er - M l i hreaded Deb gging

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

22/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Pa allel Ta k window will show you what are the different tasks that have been created for the program and what is their current status. On the other hand, Parallel Stacks will show you the graphical view of all thread creation, containing tasks, how they are related. If you click on the thread from the Parallel Stacks, it will show you the code line related with the thread (as shown in the picture with a Green Arrow). To move ahead, press F5. Let's see what comes next.

Fig re: Parallel Program - Deb gging

In the above diagram, you can find one of the tasks has been executed and the other two are remaining. Current execution point is set to A y c e h d 1 so if you continue, this method snMto_, will execute first and next time the others. When you are working with parallel programming, there are many scenarios which will come like Deadlock, Dependency problem, etc. These topics is very interesting and long to discuss. Please check further study section of the article to know more details.

Deb gging

i h In elliT ace - O e

ie

This is another great feature of Vi al S dio 2010 IDE. IntelliTrace Debugging is sometimes called as historical Debugging. IntelliTrace operates in the background, records what you are doing during debugging. When you want the information of previous event or some particular event, you can easily get it from intelliTrace information, a past state of your application. In this mode, you can navigate to various events, steps that are recorded. In this section, I will give you a basic overview of how to use IntelliTrace. Here I am using one sample program by which I will show you what IntelliTrace does. Below is the sample code block:
C ollapse C opy C ode

casPorm ls rga { sai vi Mi(tig]ag) ttc od ansrn[ rs { CnoeWieie"nelTakret) osl.rtLn(ItlirceTs"; ClTsMto() aletehd5;

pbi sai vi ClTsMto(n TsVle ulc ttc od aletehdit etau) { CnoeWieie"nClTsMto :"+TsVleTSrn() osl.rtLn(I aletehd etau.otig); CnoeWieie"atSaeet..) osl.rtLn(Ls ttmn..";

Run the program and Open In elliT ace window from Debug menu, you will find the below screen added in the right hand side of Vi al S dio.

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

23/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: IntelliTracker First Vie

Mapping

i h In elliT ace

Did you find any relationship with your code and with the In elliT acke Li ie ? I guess you should. If not, let me explain about it. Let's have a look into the below picture:

Figure: Mapping IntelliTrace - Code

From the picture, I am sure you have got the idea what IntelliTrace is doing. Yes, exactly its eco d /cap e what your code is doing. If you call a method, it will capture. If code fired an event, it will trap. Yes at a single statement, I will capture each and everything. Now from the In elliT ace window, you can navigate your code with any part and see what s happened internally. If you can get the call stack, Local variables information are recorded. To navigate, just click any of the events that you want to explore, that block will automatically expand.

Figure: E panded IntelliTrace Block - Mapping

ith Code

When you will select any block, it will expand automatically and that line will be highlighted. As per the given picture, I have highlighted the first console section and you can see the corresponding code block has also been highlight. You can now easily map them. In the expanded block (Ligh O ange), there are two Related View Sections Local and S ack Call. I have already explained Locals and Stack Calls. But can you imagine what is the use of Locals and Call Stack over here. Yes, you guessed it correctly, it will show you the Locals and Call Stack status for that time period when the selected block has been executed. What a nice feature this is.

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

24/27

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Figure: Mapping IntelliTrace - Code - Related Vie

Filter IntelliTrace Data


If o ha e a lo of eco ded info ma ion in he in elli ace indo , o can ea il fil e hem o . Yo can fil e da a ba ed on he Categories o Thread. A fo e ample ppo e o an o ee onl he da a ha i ela ed i h Con ole Rela ed, o j need o check he "Console" F om categories li .

Figure: Filter IntelliTrace Data

Note: B defa l , Visual Studio o ed In elliT ace info ma ion in "\Mic o of Visual Studio\10.0\T aceDeb gging" Loca ion in a iTrace file. Yo ma change he loca ion. Deb gging i h In elliT ace i i elf a big opic. So i i e m ch diffic l fo me o co e hem p i hin hi a icle. I j gi e o he ba ic o e ie o ha o can a lea e plo e i b o elf no . Plea e check he f he d ec ion o kno mo e abo i .

Useful Shortcut Ke For VS Debugging


Shortcut Ke s C l-Al V, A C l-Al -B C l-Al -C Di pla Di pla Di pla he A o indo dialog Descriptions

he B eakpoin he Call S ack

C l-Shif Clea F9 C l-F9 C l-Al -E C l-Al -I C l-Al V, L C l-Al -Q

all of he b eakpoin

in he p ojec en line of code

Enable o di able Di pla Di pla Di pla Di pla

he b eakpoin on he c

he E cep ion dialog he Immedia e he Local indo indo

he Q ick Wa ch dialog ion, eb ild if nece a , and ion a hen i


25/27

C l-Shif - Te mina e he c en deb gging e F5 deb gging e ion. C l-F10


codeproject.com/

a ne

S a o e me e ec ion of o eache he elec ed a emen .

code and hen hal

e ec

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

2/8/12

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Ctrl-ShiftSets the execution point to the line of code you choose F10 Alt-NUM * Highlights the next statement F5 Ctrl-F5 F11 Shift-F11 F10 Shift-F5 Ctrl-Alt-H F9 Ctrl-AltW, 1 Ctrl-Alt-P Ctrl-D,V If not currently debugging, this runs the startup project or projects and attaches the debugger. Runs the code without invoking the debugger Step Into Executes the remaining lines out from procedure Executes the next line of code but does not step into any function calls Available in break and run modes, this terminates the debugging session Displays the Threads window to view all of the threads for the current process Sets or removes a breakpoint at the current line Displays the Watch 1 window to view the values of variables or watch expressions Displays the Processes dialog, which allows you to attach or detach the debugger to one or more running processes IntelliTrace Event

I am stopping here. Hope you have enjoyed the full article. Please share your feedback and suggestions.

Further Stud
Debugging Task-Based Parallel Applications in Visual Studio 2010 By Daniel Moth and Stephen Toub Debugging With IntelliTrace

Summar
This article covers basic fundamentals of debugging procedure. It describes how to debug an application using VS IDE. I have explained almost all important tools and their uses. For Parallel program debugging, I have covered only basics. In further study section, there is a great article on Parallel debugging procedure. If you are interested, please go through the link. My main objective was to cover almost all utilities that are available in Visual Studio debugging. Hope you have learned something new from this article. Please share your valuable feedback and suggestions to improve this.

Histor
6th May, 2010: Initial post

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

About the Author


Abhijit Jana .NET Consultant Former Microsoft MVP - ASP.NET CodeProject MVP, Mentor, Insiders Technology Evangelist Author Speaker Geek Blogger Husband Blog : http://abhijitjana.net Web Site : http://dailydotnettips.com Twitter@AbhijitJana

Software Developer (Senior) India Member

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

26/27

2/8/12
Follow on Twitter

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide - CodeProj

Article Top

Sign Up to vote

Poor

E cellent Vo e

Comments and Discussions


You must Sign In to use this message board. (secure sign-in) Search this forum Profile popups Noise Medi m Layout Normal Refresh M M M M M vote of 5 vote of 5 vote of 5 vote of 5 vote of 5 Varun Sareen Degr se Kris Nicholas_ sandeep.redd 92 Pranit Kothari TheSniper2003eng sunn t ra Mike Hanke Prafulla Hunde Kim Togo jawed.ace Plutoni_vpt89 Anurag Gandhi kamiktk123 nbgangsta ahmadpasha Cristian Rem Thomas Stockwell Adam Mendo a SChristmas Tadesse Abhijit Jana marciomacedo Abhijit Jana MikroDel Last Update: 21:56 7 Feb '12 Suggestion Question Bug Answer Joke Per page 25 Go Upda e First Prev Next 21:02 2 Feb '12 2:38 18 Jan '12 1:46 13 Jan '12 22:37 12 Jan '12 4:18 1 Jan '12 10:48 25 Dec '11 16:20 19 Dec '11 5:46 9 Dec '11 6:54 13 Nov '11 1:50 8 Nov '11 20:54 18 Oct '11 21:17 24 Sep '11 0:23 23 Sep '11 1:40 29 Jun '11 0:36 20 Jun '11 12:42 1 Ma '11

a minor erratum M vote of 5

Nice Job M M M M M M M M vote of 5 vote of 5 vote of 5 vote of 5 vote of 5 vote of 5 vote of 5 vote of 5

Ama ing post M M M M vote of 5 vote of 5 vote of 5 vote of 5 Re: My vote of 5 M vote of 5 Re: My vote of 5 M vote of 5

8:18 15 Mar '11 15:55 2 Mar '11 12:14 9 Feb '11 23:40 8 Feb '11 4:57 12 Jan '11 5:33 12 Jan '11 0:23 10 Jan '11 5:55 12 Jan '11 1:46 4 Jan '11 1 2 3 4 5 6 Next Rant

Last Visit: 19:00 31 Dec '99 General Admin News

Use C trl+Left/Right to switch messages, C trl+Up/Down to switch threads, C trl+Shift+Left/Right to switch pages.
P ermalink | A dvertis e | P rivac y | M obile L ayout: fixed | Web0 4 | 2 .5 .1 2 0 2 0 6 .1 | L as t U pdated 6 M ay 2 0 1 0 fluid A rtic le C opyright 2 0 1 0 by A bhijit J ana E verything els e C opyright C odeP rojec t, 1 9 9 9 - 2 0 1 2 T erms of U s e

codeproject.com/

/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

27/27

You might also like