mercredi 6 décembre 2017

Trigger a Breakpoint and attach a debugger (Just-In-time Debugger)

Debugging program made to procress stdin data to stdout may be a mess, because piping data while running the debugger from Visual Studio is not easy.

A well known trick was to insert an interruption 3 into the code to trigger a break, see My favorite interruption

__asm int 3;

But as inline assembly is not supported by the x64 visual studio compiler, you may consider using the _debugbreak() function instead.

https://msdn.microsoft.com/fr-fr/library/f408b4et.aspx

#include
....

main() {  
   __debugbreak();  
}  

But I was facing an interesting  issue where that instruction was finally just terminate the program, instead of triggering a message box like the following



I found that there is several registry key which can be use to configure what we want to do in case of crash, break, etc...


  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug 
  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
By default, in the AeDebug key, you should find a value "Debugger" set to "C:\WINDOWS\system32\vsjitdebugger.exe" -p %ld -e %ld.

So if you see that a break isnot trigger or catch by the Just-in-Time Debugger, just add another value called Auto, of type REG_SZ  and set its value to 1.

Regards,