Friday, November 18, 2011

MS Visual Studio: debugging tips

How to cause a debug break of a Windows process.

Sometimes it's impossible to start a process under debugger. Instead, I want the debugger to be started when a process reaches some point.

To cause a debug break from the code, choose any of the following:
  1. Call DebugBreak() API.
  2. Call the built-in function __debugbreak().
  3. __asm int 3;
When any of this instruction is reached, a special window is usually displayed with a possibility to start debugger.

Why it is not working:

1. If you have Windows 7, go to Control Panel->Action Center->Change Action Center Settings->Problem Report Settings and make sure "Each time a problem occurs, ask me before checking for solutions" is checked (thanks to http://stackoverflow.com/questions/890890/debug-program-option-in-windows-7). Actually this is very important option. If it is not selected, the program crash can be silent, which is terrible for developers and testers.
2. In fact, all these commands cause breakpoint exception. So it is possible to ignore these exceptions with the help of Structured Exception Handling (SEH). For instance, the following code ignores debug break:

_try {
    DebugBreak();
} _except(EXCEPTION_EXECUTE_HANDLER) {
}

(of course, _except block can be located anywhere in call stack).

How to launch the Debugger Automatically when the application with the specified name starts.

It is possible to automatically debug an application of the specified name (for instance make "my_test.exe" always run under debugger). For details, see http://msdn.microsoft.com/en-us/library/a329t4ed%28v=vs.80%29.aspx

No comments:

Post a Comment