1 / 40

5 . Advanced Debugger Usage With Visual Studio.NET

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.

jerrye
Download Presentation

5 . Advanced Debugger Usage With Visual Studio.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 5.Advanced Debugger Usage With Visual Studio.NET Book: Debugging Applications for Microsoft.NET and Microsoft Windows

  2. Agenda • Breakpoints • Advanced Breakpoints • Breakpoint Tips • Quickly Breaking Any Function. • Location Breakpoint Modifiers. • Multiple Breakpoints on a Single Line

  3. Agenda (cont’d) • The Watch Window • Calling methods in a watch window • The Set Next Statement Command • Summary

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

  5. DEBUGGER • When to use it? • Never on self-code….. • Code you write is undoubtedly PERFECT. • Always on co-worker’s code.

  6. THE WINDOWS DEBUGGER • Finally ONE debugger that handles • SCRIPT • ASP, ASP.NET • XML Web Services • Native Code • SQL debugging.

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

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

  9. BREAKPOINTS • A simple, yet effective breakpoint is extremely powerful ,and with just that it alone, 99.46% of all bugs are solved.

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

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

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

  13. WHAT IF? • What if there was some way to tell the debugger to execute the code 9999 times before stopping? • ENTER SMART BREAKPOINTS

  14. BREAKPOINT WINDOW CODES

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

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

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

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

  19. THE BREAKPOINT WINDOW

  20. ADD FUNCTION BREAKPOINT

  21. Microsoft ! WE HAVE A PROBLEM!! • In the breakpoint window, to set a breakpoint • Type “<classname>.” • VS.NET 2003 CRASHES

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

  23. Adding breakpoints to the FCL

  24. LOCATION BREAKPOINT MODIFIERS, .ie., SMART BREAKPOINTS • HIT COUNTS • CONDITIONAL EXPRESSIONS

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

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

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

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

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

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

  31. WATCH WINDOW • Complete Expression Evaluator….. BUILT IN !!! • Ability to replace looooooong expressions by sample simple relevant values.

  32. CODE COVERAGE • A goal to strive while UNIT TESTING !! • Consider if ((eRunning == m_eState)|| (eException == m_eState) && (TRUE == m_bSeenLoader))

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

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

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

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

  37. SET NEXT….BUT BEWARE • Changing the state of the instruction pointer can easily send the program to CRASH.

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

  39. MORAL OF THE STORY USE THE .NET DEBUGGER MOST EFFECTIVELY TO USE IT THE LEAST NUMBER OF TIMES.

  40. End of Presentation

More Related