little known vc++ debugging tricks

35
Little Known Native Debugging Tricks Ofek Shilon Algorithms Team Leader @ Sarin Technologies www.thetweaker.wordpress.com

Upload: ofek-shilon

Post on 24-May-2015

8.142 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Little Known VC++ Debugging Tricks

Little KnownNative Debugging Tricks

Ofek ShilonAlgorithms Team Leader @ Sarin Technologies

www.thetweaker.wordpress.com

Page 2: Little Known VC++ Debugging Tricks
Page 3: Little Known VC++ Debugging Tricks

DISCLAIMER

Page 4: Little Known VC++ Debugging Tricks

VS AS AN INTERPRETER

Page 5: Little Known VC++ Debugging Tricks

Expression Evaluator

• Scope–Watch, QuickWatch,– Immediate,– BP conditions, ‘when hit’

• Call Functions from the debugger!• Demo

Page 6: Little Known VC++ Debugging Tricks

Expression Evaluator

• Context Operator– Documentation is wrong.– VS2010 blocked WIN-API, but eased usage of

others.

• Thread context• Access insensitive• Blind to inlined functions

Page 7: Little Known VC++ Debugging Tricks

Expression Evaluator

• Things to try:– Very explicit casting,– Explicit addresses.

Page 8: Little Known VC++ Debugging Tricks

Edit and Continue

• Demo• Stale code

Page 9: Little Known VC++ Debugging Tricks

Edit and Continue• Preparation: /ZI, /INCREMENTAL

Page 10: Little Known VC++ Debugging Tricks

Edit and Continue

• Limitations:– Inconsistent with /RELEASE, /OPT:REF,

/OPT:ICF, /ORDER, /FORCE,– Does not invoke custom build steps,– Isn’t supported on attached processes,– Can’t change object layout,– Doesn’t regenerate IDL files,– Can’t change resource files,– Does not build static libs.

Page 11: Little Known VC++ Debugging Tricks

CLICK SAVERS

Page 12: Little Known VC++ Debugging Tricks

StepOver

• Demo

Page 13: Little Known VC++ Debugging Tricks

StepOver

• VC6: autoexp section [ExecutionControl]• VC7: Reg key -

HKCU\Software\Microsoft\VisualStudio\7.0\NativeDE\StepOver

• VC8: HKLM\Software\Microsoft\VisualStudio\8.0\NativeDE\StepOver

• VC10: HKCU\Software\Microsoft\VisualStudio\10.0_Config\NativeDE\StepOverDropped ‘=NoStepInto’ syntax

Page 14: Little Known VC++ Debugging Tricks

StepOver

• Warning!–When 2 or more instances of VS2010 are active,

occasionally it would create and use a duplicate reg key:HKCU\Software\Microsoft\VisualStudio\10.0_Config_%PID%(e.g., …\VisualStudio\10.0_Config_6172) … and all ’10.0_Config’ modifications would have no effect !

• Credit: Andy Pennell http://blogs.msdn.com/b/andypennell/archive/2004/02/06/69004.aspx

Page 15: Little Known VC++ Debugging Tricks

autoexp

• How’s the debugger ‘aware’ of STL containers?

• autoexp.dat– By default at

%VS folder %\Common7\Packages\Debugger– Provide alternate path in environment variable:

_vcee_autoexp

Page 16: Little Known VC++ Debugging Tricks

autoexp

• Demo

• Specialized syntax for common containers: Array, List, Map, Tree.

• Re-parsed on every debugging session• Extremely fragile, yet excellent investment

Page 17: Little Known VC++ Debugging Tricks

autoexp

• Status limbo:

Page 18: Little Known VC++ Debugging Tricks

autoexp

• Bypass locally: xxx,!• Bypass globally in Tools/Options:

Page 19: Little Known VC++ Debugging Tricks

autoexp

• Resources:– Avery Lee

http://virtualdub.org/blog/pivot/entry.php?id=120

– Stephan T. Lavavej BoostCon presentationhttp://filetolink.com/d/?h=a456fc7fd3d35644edcbe5e764d1ba63&t=1315076450&f=17ad36ef

–My blog: complete MFC containers visualization, Matrix 2D visualization..www.thetweaker.wordpress.org

Page 20: Little Known VC++ Debugging Tricks

Sound Alerts

• Win7:Control Panel \ Hardware and Sounds \ Sound \ Change system sounds

• WinXP:Control Panel \Sounds and Audio... \Program Events – Microsoft Developer

Page 21: Little Known VC++ Debugging Tricks

TRACING ERRORS

Page 22: Little Known VC++ Debugging Tricks

GetLastError Interactively

• GetLastError implementation:

• (int*)($fs)+0x34• (int*)($tib)+0x34• $err !• Credit: msdn

http://msdn.microsoft.com/en-us/library/dtw169z6.aspx

Page 23: Little Known VC++ Debugging Tricks

Extensions• Customizing ‘,hr’ display in autoexp.dat:

• Other TEB data: (_TEB*)$tib

Page 24: Little Known VC++ Debugging Tricks

Breaking on Errors

• BP location: (int*)($tib)+0x34• Specific error: BP condition– Slooooooooow

• g_dwLastErrorToBreakOn !– (int*){,,ntdll.dll}_g_dwLastErrorToBreakOn– Seems to fail for Win7.

• Credit: Daniel Pearson, http://blogs.msdn.com/b/danpear/archive/2007/04/06/2033100.aspx

Page 25: Little Known VC++ Debugging Tricks

INVESTIGATING CODE

Page 26: Little Known VC++ Debugging Tricks

Breaking on all class methods

• Break at function: Class::*

• Credit: Habib Heydarianhttp://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

Page 27: Little Known VC++ Debugging Tricks

Naming Native Threads

• Demo

• ‘Documented hack’Credit: MSDNhttp://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx

Page 28: Little Known VC++ Debugging Tricks

Breaking on Data Read

• Write-to BPs are implemented in hardware – dedicated x86 debug registers.

• Read-from debugs registers are available too, but were never exposed in VS.

• Some code required to set the register values to the desired address.

• Links to 4 implementations:http://thetweaker.wordpress.com/2011/05/22/breaking-on-data-read/

Page 29: Little Known VC++ Debugging Tricks

Breaking on Data Read

• Usage:

• Interactive:

Page 30: Little Known VC++ Debugging Tricks

Static Object Layout

• /d1reportSingleClassLayout• /d1reportAllClassLayout

• Credit: Andy Richhttp://blogs.msdn.com/b/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx

Page 31: Little Known VC++ Debugging Tricks

ENHANCED DIAGNOSIS

Page 32: Little Known VC++ Debugging Tricks

Useful Pseudo-registers

• $tid-> very useful as a BP condition.

• $env=0-> view debugee environment variables

• $cmdline = 0-> view launching command line

Page 33: Little Known VC++ Debugging Tricks

Useful Pseudo-registers

• $handles-> narrows down handle leaks

• $clk-> rough profiling

• $user-> view permissions info

• Credits: Steve Steiner, Gregg Miskelly, et. al.http://blogs.msdn.com/b/stevejs/http://blogs.msdn.com/b/greggm/

Page 34: Little Known VC++ Debugging Tricks

Searching Through Memory

• Credit: msdnhttp://msdn.microsoft.com/en-us/library/ms171363.aspx

Page 35: Little Known VC++ Debugging Tricks

THANK YOU.

[email protected]