c o d i n g f r o g s

croaking about programming, programming languages, software engineering, and the business of software

8Apr/100

Hard Coded Breakpoints

This little line of code totally saved me this week:

__asm int 3;

I'm writing a plugin to an existing Windows application and I could not, for the life of me, figure out if my plugin was even being loaded or if it was executing properly.  All I knew was that I wasn't seeing the behavior from my plugin I expected.  I needed to step through my plugin with a debugger, but since it loads as part of another app I didn't know how to get the debugger to break.  Setting breakpoints in Visual Studio wasn't working.

Fortunately for me I have smart teammates, and one of them taught me this little trick to programmatically insert a hard coded breakpoint into your code.  If I add this line, when my code runs the breakpoint is triggered and the application stops executing, with a dialog giving me the option to debug it in Visual Studio.  When I get in there, behold:  The cursor is pointing at that line of code and I can step through my plugin.

Since then I've done a bit more research and found out that, in truth,

__asm int 3;

is really only meant for x86 architectures.  A more portable way to do this for Windows is:

__debugbreak();

which invokes the correct assembly interrupt for the current architecture.

On Linux, apparently you can do this:

__asm__("int $0x03");

although I haven't tried it. It looks enough like the x86-only version for Windows that it makes me wonder if it would work on x64 architectures; I'd love to hear back if anyone knows for sure.

Also, interested to know how to do this on OS X. Comment or mail if you know.