2. Software Bugs r Expensive
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code
as cleverly as possible, you are, by definition, not smart enough to debug it.
Brian Kernighan
4. Debugging Basics
What are you trying to find and fix?
◦ Two main types of code errors
◦ Syntax: Compiler catches most if not all of these for you.
◦ Semantic or logical: Syntactically correct yet program may “crash and burn” at run-time!
Autos
Locals
Watch
Call Stack
Command Window
QuickWatch Dialog
Breakpoints window
Threads
Modules
Processes
Memory
Disassembly
Registers
Debugging Tools for Inspection Your Program
5. Execution Control: Breakpoints
Stepping through your code
Starting / Stopping
Breaking
Stepping through your
application
(F10, F11 or Toolbar buttons)
Run to a specific location
Run To Cursor (right-click menu)
Situations under which breakpoints
are not feasible
Timing sensitive issues
Breakpoint triggers too often
Live Debug not possible
Debugging production systems
6. Debug Vs Release Build
Debug:
The Debug configuration of your program is compiled with full symbolic debug information and no
optimization. Optimization complicates debugging, because the relationship between source code and
generated instructions is more complex.
Release:
The Release configuration of your program contains no symbolic debug information and is fully
optimized. Debug information can be generated in PDB Files, depending on the compiler options that are
used. Creating PDB files can be very useful if you later have to debug your release version.
7. Condition on Debug
Using If Block:
Using Attribute:
#if DEBUG
public void SetPrivateValue(int value)
{
...
...
}
#endif
[System.Diagnostics.Conditional("DEBUG")]
public void SetPrivateValue(int value)
{
...
...
...
}
[System.Diagnostics.Conditional("DEBUG")] public void SetPrivateValue(int value) { ... }
#if DEBUG public void SetPrivateValue(int value) { ... } #endif
8. Condition on Debug
Using If Block:
The code in here won't even reach the IL on release.
So can’t verify syntax (compile time issues)
Using Attribute:
Code will be translated to IL, but will not execute
Good to avoid typos/syntax errors
9. Just-In-Time Debugging
Just-In-Time debugging launches Visual Studio automatically when an exception or crash
occurs in an application running outside Visual Studio.
This enables you to test your application when Visual Studio is not running and begin
debugging with Visual Studio when a problem occurs.
See Details: http://msdn.microsoft.com/en-us/library/5hs4b7a6.aspx