400 likes | 407 Views
This book provides advanced techniques for using the debugger in Visual Studio.NET to effectively debug applications for Microsoft.NET and Windows. Topics covered include breakpoints, advanced breakpoints, breakpoint tips, using the watch window, calling methods in the watch window, and more.
E N D
5.Advanced Debugger Usage With Visual Studio.NET Book: Debugging Applications for Microsoft.NET and Microsoft Windows
Agenda • Breakpoints • Advanced Breakpoints • Breakpoint Tips • Quickly Breaking Any Function. • Location Breakpoint Modifiers. • Multiple Breakpoints on a Single Line
Agenda (cont’d) • The Watch Window • Calling methods in a watch window • The Set Next Statement Command • Summary
Introduction “The whole key to debugging effectively is to avoid the debugger as much as possible because that’s where you waste all your time.” Says….John Robbins
DEBUGGER • When to use it? • Never on self-code….. • Code you write is undoubtedly PERFECT. • Always on co-worker’s code.
THE WINDOWS DEBUGGER • Finally ONE debugger that handles • SCRIPT • ASP, ASP.NET • XML Web Services • Native Code • SQL debugging.
BREAKPOINTS • First “F9” … then “F10” • Def : • The position in code where your debugger assumes CONTROL of the program. • Location Breakpoint • The type of the breakpoints that we use regularly. (go to the margin and press F9)
BREAKPOINTS • What happens when we set a breakpoint? • When code for such a line executes, the debugger will stop at that location. • So……WHY NOT SET BREAKPOINTS RANDOMLY ? • Absolutely NO HARM…… • Also…..NO BIG USE !!!
BREAKPOINTS • A simple, yet effective breakpoint is extremely powerful ,and with just that it alone, 99.46% of all bugs are solved.
Ok ! First Set a breakpoint…then what??? • What does a breakpoint tell the programmer? • The line where the breakpoint is set ;) • The values of all the relevant variables till that point of code.
Scenario ! • Consider this piece of code { for(int i = 1; i < 10000 ; i++){ • j = j * (i+j); } } • What if I encounter a bug in the 10,000’th iteration?
2 SIMPLE OPTIONS • 1. Code extra to display the value of the variables for each iteration. • 2. Set up a location breakpoint and stop at it each time for all the 10000 iterations. • Consequences • A swollen finger (take your pick). • Hours of unproductive work.
WHAT IF? • What if there was some way to tell the debugger to execute the code 9999 times before stopping? • ENTER SMART BREAKPOINTS
BREAKPOINT TIPS • Breakpoints in the call stack window. • Useful especially to get stopped on recursion or deeply nested stacks. • Works very similar to that of a location breakpoint….just in a another window. • Run To Cursor (Ctrl+F10) • Breakpoint without setting a breakpoint. • Runs the program till where your cursor is and stops there.
BREAKPOINTS • Multiple breakpoints • Windows debugger supports multiple breakpoints (3)… • Take your cursor there and press F9. • Only one RED button appears on the margin. • How does one differentiate between all the breakpoints in the same line? • BREAKPOINTS WINDOW
Quickly Breaking on any Function • In a million line code, how much time does it take to find out where a function is defined and set a breakpoint there? • The longer the code, the tougher it is to find out where a function is defined (spare the use of Ctrl+F & right click on function usage).
How harder does it get if you want to add this breakpoint while you are already stepping through the code? • What if I just knew the class name or just the method name or maybe both? • The breakpoint window provides amazing help in this process. • You can simply set a breakpoint if you just know any of the method details. (not supported inC++ completely)
Microsoft ! WE HAVE A PROBLEM!! • In the breakpoint window, to set a breakpoint • Type “<classname>.” • VS.NET 2003 CRASHES
Setting breakpoints in the FCL • What if we set a breakpoint in a FCL class method by typing in the method with the class name in the break point window? • The WINDBG shows all the overloads for that method and gives you the option of setting breakpoints on any of them. • Ain’t that COOOOOL • Example : Console.WriteLine has 19 overloads.
LOCATION BREAKPOINT MODIFIERS, .ie., SMART BREAKPOINTS • HIT COUNTS • CONDITIONAL EXPRESSIONS
HIT COUNTS • Consider this piece of code { for(int i = 1; i < 10000 ; i++){ j = j * (i+j); } } • What if I encounter a bug in the 10,000’th iteration?
Adding SMARTS to BREAKPOINTS • Hit Count / Skip Count • This tells the debugger to stop at this breakpoint only after this line of code has executed the specified number of times.
HIT COUNT EVALUATION • Break always • Break when hit count is equal to. • Break when the hit count is equal to a multiple of. • Break when hit count is greater than or equal to.
CONDITIONAL EXPRESSIONS • Another SMART to breakpoints. • An alternative to writing extra code. • Set a conditional expression. • Make the breakpoint to occur only when the conditional expression • Makes a switch. • Condition evaluates to TRUE/FALSE
MULTIPLE BREAKPOINTS ON A SINGLE LINE For( I=0,m=0;I<10;I++,m--){ Console.Writeline(“I= {0} m = {1}”,I,m); } • Combining multiple breakpoints with conditional expressions.
WATCH WINDOW • A WATCH WINDOW IS NOT JUST A “WATCH WINDOW” • A watch window is not just for reading/ displaying the variable’s content. • The contents of the variable can be changed/cast into appropriate types at any point in a watch window.
WATCH WINDOW • Complete Expression Evaluator….. BUILT IN !!! • Ability to replace looooooong expressions by sample simple relevant values.
CODE COVERAGE • A goal to strive while UNIT TESTING !! • Consider if ((eRunning == m_eState)|| (eException == m_eState) && (TRUE == m_bSeenLoader))
The watch window interprets this single IF condition as 3 separate lines of code. • The COOL part : • Change values of individual variables • The value of whole expression changes accordingly. • Different values that you want to test for gives you adequate code coverage.
Calling functions from WATCH WINDOW • WHY would I want to do that?? • What if you want to simply check if an element’s value is valid or not? • Option 1 : write a function that checks for that and call it every time when that element is created/used. • Option 2: write the function…and use it only in the watch window (pre-release).
SO ! Where is the catch? • 20 SECONDS • Evaluation timed out! Errors • Killing threads. • Previous versions killed all related threads. • READ MEMORY ONLY FOR DATA VALIDATIONS.
SET NEXT • Available only when debugging. • Allows you to change the instruction pointer to a different place in the program. • Often called when you want to change an input parameter value to a data-structure. • HANDY TOOL during UNIT TESTING.
SET NEXT….BUT BEWARE • Changing the state of the instruction pointer can easily send the program to CRASH.
SUMMARY • VS.NET debugging the state-of-the-art debugger in the market today. • Finally….someone listened to their developers while developing software for them. • Common debugging styles to native code and managed code.
MORAL OF THE STORY USE THE .NET DEBUGGER MOST EFFECTIVELY TO USE IT THE LEAST NUMBER OF TIMES.