Tuesday, July 28, 2009

A Neat visual C++ debugging trick...




Its almost impossible nowadays to write programs without a debugger, and placing a breakpoint is considered to be the most basic thing to do when debugging an application, sometimes though, we don’t have the luxury of simply placing those nice little red circles directly where you want to break, for example, I have worked on a multithreaded applications that never stopped at breakpoints, so I had to look for an alternative.

One neat trick is to use the (__asm) directive to write assembly code inside your C/C++ code, so it would be like this:

int X = 5;//this is C++
X += 3; //this is C++
__asm int 3// this is Assembly
X--;//this is C++

Now this statement (__asm int 3) is an assembly instruction which means interrupt 3, the inline assembler in MS Visual C++ takes that and places it in the final binary, and once the debugger hits this statement it breaks ALWAYS as if it’s a regular breakpoint.

One thing to keep in mind though is that this only works when the debugger is on and you’re actually trying to debug the application.

Enjoy ;)

No comments:

Post a Comment

Followers