Page 1 of 1

Problem using '__asm int 3;' to issue breakpoint

PostPosted: 08 Dec 2009, 21:30
by Don Cross
Hi everyone,

I am trying to use an old trick of mine to catch a bug while running my chess engine under WinBoard 4.4.0. In the past, I have often put the following line of code in a debug build of a C++ program running under Windows, and it triggers a breakpoint on that line that allows me to bring up the debugger (Microsoft Visual Studio):

Code: Select all
__asm int 3;


I also tried this, to no avail...

Code: Select all
#include <assert.h>
// ... blah blah blah
assert(false);


Either of these works great if I run my chess engine directly in a command prompt, but when hosted inside WinBoard, the above line of code just causes my engine to hang and stop responding. When I close WinBoard after that, the dialog box I was expecting all along suddenly appears and just as quickly vanishes. It is like WinBoard is somehow suppressing the debugger interrupt.

All I am trying to do is trap a very rare special case with a breakpoint so I can see the stack trace if/when it happens. It would be fine however I do it, so long as I can run it under WinBoard and not have to type in the correct sequence of WinBoard protocol commands manually.

Any advice would be greatly appreciated. Thanks in advance!

- Don

Re: Problem using '__asm int 3;' to issue breakpoint

PostPosted: 09 Dec 2009, 10:03
by Teemu Pudas
Does Debug menu->Attach to process work?

Re: Problem using '__asm int 3;' to issue breakpoint

PostPosted: 09 Dec 2009, 16:40
by Don Cross
Thanks, Teemu, that is exactly what I needed! I knew "Attach to Process" was in there, but had forgotten about it.

For others reading this, after I opened the Debug / Attach To Process dialog box in Microsoft Visual Studio 2008, I had to check the box that enabled debuging "Native" code, otherwise I got an error when I tried to do Debug / Break All that said it wasn't the right kind of executable. Now can use the debugger and set breakpoints while running my engine hosted by WinBoard.

Re: Problem using '__asm int 3;' to issue breakpoint

PostPosted: 10 Dec 2009, 14:27
by xcomponent
It is only possible under 32-bit compile.
Besides, you have to try it that way:
_asm { int 3 }

intsead of:
__asm int 3;

Plus, you have to set your default debugger before that.

Re: Problem using '__asm int 3;' to issue breakpoint

PostPosted: 11 Dec 2009, 06:55
by Aleks Peshkov
Code: Select all
#if defined NDEBUG
#   define assert(test) ((void)0)
#elif defined _MSC_VER && defined ASSERT_DEBUG_BREAK
#   pragma intrinsic(__debugbreak)
#   if defined assert
#       undef assert
#   endif
#   define assert(test) (void)( (!!(test)) || (__debugbreak(), 0) )
#elif defined __GNUC__ && defined ASSERT_DEBUG_BREAK
#   define assert(test) do { if (!(test)) __asm__("int $3"); } while(0)
#else
#   include <cassert>
#endif