This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Breakpoint Macro
  Submitted by



This is a simple macro for triggering a breakpoint on a string match.

The debugging facilities in VC++ 6.0 are very helpful, particularly conditional breakpoints; I use these all of the time, but when I need to break on a string condition, they are not very helpful. I found myself doing things like this:

#ifdef _DEBUG
    if(strstr(testString, "matchString") != NULL) {
         int __x = 1; // Dummy statement to prevent it optimizing out the conditional
                  // and to give me somewhere to break
  }
#endif 

Then placing a breakpoint on the line inside the conditional so I would get a breakpoint when the string matched.

After doing this three or four times, I decided that I should just wrap this in a macro and be done with it. What I came up with was:

#ifdef _DEBUG
#define _BreakOnString(input, matchwith)    \
    if(strstr(input,matchwith) != NULL){    \
        __asm {                             \
            int 3                           \
        }                                   \
    }
#else
#define _BreakOnString(x,y)
#endif 

Keep in mind that this will probably only work with Visual C++, and I have only tested it with 6.0. The "int 3" is a debugger breakpoint. There should be an equivalent to "int 3" in gcc and other compilers. If anyone knows what any of them are, please post and let us know. After defining this, the above code would now be

_BreakOnString(testString, "matchString"); 



As a note, unlike the above example, this will break every time there is a match, and the only way to prevent it is to comment out the line.

I have considered several extensions to this macro, e.g. a std::string version, an active/inactive parameter and a couple of other ideas, but I have not needed them enough to bother implementing them.

This is not very complicated, but it can be very useful.

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.