Little Known VC++ Debugging Tricks

8,909 views
8,658 views

Published on

Presentation by Ofek Shilon, at the Microsoft Israel Windows Platform Developers users meeting

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,909
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • C++ VS2010 ?
  • Many people think that editing variables is the most they can intervene with the debugee Edit variables, move execution around
  • ברור שב WATCH אפשר לשערך משתנים אפשר גם לחשב סכום של מספרים כלומר יש מנוע שמפרסר ומחשב את הטקסט שמקלידים נקרא EXPRESSION-EVALUATOR, סמי - מתועד הכוח המרכזי שלו – מאפשר קריאה לפונקציות , במגבלות שתכף יתוארו . לפחות חלקן
  • אינליין – רלבנטי ב RELEASE
  • Break *before* code change, greatly reduces the chance of stale code Try to use directX sample!
  • Incremental linking: pads functions code, contains jump thunks to handle function relocation Inconsistent with /OPT:REF, /OPT:ICF, /ORDER, /RELEASE, /FORCE Some changes are too great, full linking occurs. All options are on by default in debug
  • 2005: for all users
  • 2005: for all users
  • 2005: for all users
  • comment std::vector רפרוף בסיסי על הסינטקס , רפרנסים טובים בסוף Insert carray children Carray preview Vect3d preview
  • טוב גם לבדוק שם אם נדמה שלא פועל
  • כשיודעים מה לחפש – מוצאים המון
  • אם הבילד ארוך , או אתה מחכה שעות ל BP, אתה נודד ל YNET. ובודק חזרה אחת לכמה דקות . תארו לכם שהיה אפשר לשמוע צפצוף , או קול של דרת ויידר , כשהתנאי הזה מתקיים . לא מוחבא ברג ' יסטרי , יש UI, אבל במקום ממש לא סביר ורחוק מאד מ VS עצמו הועף ב 2005, הוחזר ב 2008 לקול מחאות הציבור
  • Code: ::DeleteFile, GetLastError $fs fakes it... (int*)($fs)+0x18 (int*)($tib)+0x34 $err Formatting strings: 12.34 12.34,g $err,hr (_TEB*)$tib
  • HRESULT customization doesn’t even require debugger restart!! _TEB is not defined publically, but the debugger is aware of it. Contains much useful data for advanced scenarios (fiber-specific data, etc)‏
  • Doesn’t work for templated classes/methods Doesn’t extend to wildcards/regex Good way to find entry points
  • No ‘name’ slot in TEB
  • Maybe historical portability constraints? Dunno 4 very similar implementations
  • Not investigating code *flow* - static feature. After /d1reportSingleClassLayout, type immediatelly your class Test: /d1reportSingleClassLayoutCDialog . show vftable, alignment, adjustors Debug ODR violations Optimize storage Gain deep insight Since VS2010, collides with /showIncludes (?)‏ Watch out for PCH – if your class definition isn’t compiled, its layout won’t be shown
  • $cmdline credit: John Cunningham, http://blogs.msdn.com/b/ms_joc/archive/2004/08/23/218912.aspx $env=0 no longer works in VS2010
  • Demo – on env vars .S -U 0x006fdad0 60 "ALLUSERSPROFILE" שימושי בדיבוג של דמפים מלאים
  • Little Known VC++ Debugging Tricks

    1. 1. Little Known Native Debugging Tricks Ofek Shilon Algorithms Team Leader @ Sarin Technologies www.thetweaker.wordpress.com
    2. 3. DISCLAIMER
    3. 4. VS AS AN INTERPRETER
    4. 5. Expression Evaluator <ul><li>Scope </li></ul><ul><ul><li>Watch, QuickWatch, </li></ul></ul><ul><ul><li>Immediate, </li></ul></ul><ul><ul><li>BP conditions, ‘when hit’ </li></ul></ul><ul><li>Call Functions from the debugger! </li></ul><ul><li>Demo </li></ul>
    5. 6. Expression Evaluator <ul><li>Context Operator </li></ul><ul><ul><li>Documentation is wrong. </li></ul></ul><ul><ul><li>VS2010 blocked WIN-API, but eased usage of others. </li></ul></ul><ul><li>Thread context </li></ul><ul><li>Access insensitive </li></ul><ul><li>Blind to inlined functions </li></ul>
    6. 7. Expression Evaluator <ul><ul><li>Things to try: </li></ul></ul><ul><ul><li>Very explicit casting, </li></ul></ul><ul><ul><li>Explicit addresses. </li></ul></ul>
    7. 8. Edit and Continue <ul><li>Demo </li></ul><ul><li>Stale code </li></ul>
    8. 9. Edit and Continue <ul><li>Preparation: /ZI, /INCREMENTAL </li></ul>
    9. 10. Edit and Continue <ul><li>Limitations: </li></ul><ul><ul><li>Inconsistent with /RELEASE, /OPT:REF, /OPT:ICF, /ORDER, /FORCE, </li></ul></ul><ul><ul><li>Does not invoke custom build steps, </li></ul></ul><ul><ul><li>Isn’t supported on attached processes, </li></ul></ul><ul><ul><li>Can’t change object layout, </li></ul></ul><ul><ul><li>Doesn’t regenerate IDL files, </li></ul></ul><ul><ul><li>Can’t change resource files, </li></ul></ul><ul><ul><li>Does not build static libs. </li></ul></ul>
    10. 11. CLICK SAVERS
    11. 12. StepOver <ul><li>Demo </li></ul>
    12. 13. StepOver <ul><li>VC6: autoexp section [ExecutionControl] </li></ul><ul><li>VC7: Reg key - HKCUSoftwareMicrosoftVisualStudio7.0NativeDEStepOver </li></ul><ul><li>VC8: HK LM SoftwareMicrosoftVisualStudio8.0NativeDEStepOver </li></ul><ul><li>VC10: HK CU SoftwareMicrosoftVisualStudio10.0 _Config NativeDEStepOver Dropped ‘=NoStepInto’ syntax </li></ul>
    13. 14. StepOver <ul><li>Warning! </li></ul><ul><ul><li>When 2 or more instances of VS2010 are active, occasionally it would create and use a duplicate reg key: HKCUSoftwareMicrosoftVisualStudio10.0_Config_%PID% (e.g., …VisualStudio10.0_Config_6172) … and all ’10.0_Config’ modifications would have no effect ! </li></ul></ul><ul><li>Credit: Andy Pennell http://blogs.msdn.com/b/andypennell/archive/2004/02/06/69004.aspx </li></ul>
    14. 15. autoexp <ul><li>How’s the debugger ‘aware’ of STL containers? </li></ul><ul><li>autoexp.dat </li></ul><ul><ul><li>By default at %VS folder %Common7PackagesDebugger </li></ul></ul><ul><ul><li>Provide alternate path in environment variable: _vcee_autoexp </li></ul></ul>
    15. 16. autoexp <ul><li>Demo </li></ul><ul><li>Specialized syntax for common containers: Array, List, Map, Tree. </li></ul><ul><li>Re-parsed on every debugging session </li></ul><ul><li>Extremely fragile, yet excellent investment </li></ul>
    16. 17. autoexp <ul><li>Status limbo: </li></ul>
    17. 18. autoexp <ul><li>Bypass locally: xxx ,! </li></ul><ul><li>Bypass globally in Tools/Options: </li></ul>
    18. 19. autoexp <ul><li>Resources: </li></ul><ul><ul><li>Avery Lee http://virtualdub.org/blog/pivot/entry.php?id=120 </li></ul></ul><ul><ul><li>Stephan T. Lavavej BoostCon presentation http://filetolink.com/d/? h=a456fc7fd3d35644edcbe5e764d1ba63&t=1315076450&f=17ad36ef </li></ul></ul><ul><ul><li>My blog: complete MFC containers visualization, Matrix 2D visualization.. www.thetweaker.wordpress.org </li></ul></ul>
    19. 20. Sound Alerts <ul><li>Win7: Control Panel Hardware and Sounds Sound Change system sounds </li></ul><ul><li>WinXP: Control Panel Sounds and Audio... Program Events – Microsoft Developer </li></ul>
    20. 21. TRACING ERRORS
    21. 22. GetLastError Interactively <ul><li>GetLastError implementation: </li></ul><ul><li>(int*)($fs)+0x34 </li></ul><ul><li>(int*)($tib)+0x34 </li></ul><ul><li>$err ! </li></ul><ul><li>Credit: msdn http:// msdn.microsoft.com/en-us/library/dtw169z6.aspx </li></ul>
    22. 23. Extensions <ul><li>Customizing ‘,hr’ display in autoexp.dat: </li></ul><ul><li>Other TEB data: (_TEB*)$tib </li></ul>
    23. 24. Breaking on Errors <ul><li>BP location: (int*)($tib)+0x34 </li></ul><ul><li>Specific error: BP condition </li></ul><ul><ul><li>Slooooooooow </li></ul></ul><ul><li>g_dwLastErrorToBreakOn ! </li></ul><ul><ul><li>(int*){,,ntdll.dll}_g_dwLastErrorToBreakOn </li></ul></ul><ul><ul><li>Seems to fail for Win7. </li></ul></ul><ul><li>Credit: Daniel Pearson, http:// blogs.msdn.com/b/danpear/archive/2007/04/06/2033100.aspx </li></ul>
    24. 25. INVESTIGATING CODE
    25. 26. Breaking on all class methods <ul><li>Break at function: Class::* </li></ul><ul><li>Credit: Habib Heydarian http :// blogs.msdn.com/b/habibh/archive/2009/09/10/class-breakpoint-how-to-set-a-breakpoint-on-a-c-class-in-the-visual-studio-debugger.aspx </li></ul>
    26. 27. Naming Native Threads <ul><li>Demo </li></ul><ul><li>‘ Documented hack’ Credit: MSDN http:// msdn.microsoft.com/en-us/library/xcb2z8hs.aspx </li></ul>
    27. 28. Breaking on Data Read <ul><li>Write-to BPs are implemented in hardware – dedicated x86 debug registers. </li></ul><ul><li>Read-from debugs registers are available too, but were never exposed in VS. </li></ul><ul><li>Some code required to set the register values to the desired address. </li></ul><ul><li>Links to 4 implementations: http://thetweaker.wordpress.com/2011/05/22/breaking-on-data-read / </li></ul>
    28. 29. Breaking on Data Read <ul><li>Usage: </li></ul><ul><li>Interactive: </li></ul>
    29. 30. Static Object Layout <ul><li>/d1reportSingleClassLayout </li></ul><ul><li>/d1reportAllClassLayout </li></ul><ul><li>Credit: Andy Rich http:// blogs.msdn.com/b/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx </li></ul>
    30. 31. ENHANCED DIAGNOSIS
    31. 32. Useful Pseudo-registers <ul><li>$tid -> very useful as a BP condition. </li></ul><ul><li>$env=0 -> view debugee environment variables </li></ul><ul><li>$cmdline = 0 -> view launching command line </li></ul>
    32. 33. Useful Pseudo-registers <ul><li>$handles -> narrows down handle leaks </li></ul><ul><li>$clk -> rough profiling </li></ul><ul><li>$user -> view permissions info </li></ul><ul><li>Credits: Steve Steiner, Gregg Miskelly, et. al. http ://blogs.msdn.com/b/stevejs / http://blogs.msdn.com/b/greggm / </li></ul>
    33. 34. Searching Through Memory <ul><li>Credit: msdn http:// msdn.microsoft.com/en-us/library/ms171363.aspx </li></ul>
    34. 35. THANK YOU. www.thetweaker.wordpress.com [email_address]

    ×