简体   繁体   中英

How can I set a breakpoint on an empty statement in C++?

Specifically, I want to write a macro that

1) allows me to set a breakpoint
2) does nothing else
3) causes no compiler warnings

#define NO_OP   ((void)0)

void main()
{
    bool b = true;
    if (b)
        NO_OP;  // I try to set a breakpoint here, but

}               // it jumps to here (in Visual Studio 2010)

I also tried

#define NO_OP   (assert(1))             // doesn't work
#define NO_OP   (sizeof(int))           // doesn't work
#define NO_OP   __asm{}                 // doesn't work
#define NO_OP   do {(void)0;} while(0)  // warning: conditional is constant

The only thing that works so far is the cringe-worthy

#define NO_OP   { int x = 0; x = x; }

There has to be a better way.

EDIT
Thanks Blorgbeard , __asm{ nop } does indeed work. But I just realized that anything with braces is less than perfect (problematic?) because it leaves a useless semi-colon hanging there after it. (Later) I don't know squat about assembler but I tried removing the braces, and voila! There's the answer: __asm nop Thanks!

FOR THE CURIOUS
Here's a slightly less absurd example:

string token = GetNextToken();
if (!Ignore(token))
{
    // process token
    DoThis(token);
    DoThat(token);
}

This code is complete -- as long as the program works correctly I don't care to know anything about ignored tokens. But at any given time (and without changing the code) I want to make sure that I'm not rejecting good tokens

string token = GetNextToken();
if (Ignore(token))
{
    NO_OP; // when desired, set breakpoint here to monitor ignored tokens
}
else
{
    // process token
    DoThis(token);
    DoThat(token);
}

一个实际的无操作指令:

__asm nop

Perhaps you can do this:

#define BREAKPOINT __asm { int 3; }

This will call interrupt 3, which is the breakpoint interrupt. This will set a breakpoint in your code, which is compiled as part of your code.

Now, if you want just some operation that you can set a breakpoint on, which essentially does nothing, besides allowing you to break on that line. I think you have to compile your code without optimization for one thing, as a NO_OP as you've implemented is likely to be optimized out of the code by an optimizing compiler, with the optimization switches on.

The other point is, that this seems like a very strange thing to do. From my knowledge, one normally sets a breakpoint on a line of code one wants to look at. See what variable state's are, step one line at a time, etc. I don't really see how setting a breakpoint on a line of code with essentially no significance in your program, will help you debug anything.

在 msvc x64 上有一个内在的:

__nop;

C++03:

inline void __dummy_function_for_NO_OP () {}
#define NO_OP __dummy_function_for_NO_OP ()

int main () {
    NO_OP;
}

C++11:

#define NO_OP [](){}()

int main () {
    NO_OP;
}

How about __asm int 3 ? Also, are optimizations enabled? That could be the reason for the others failing (actually never tried to break on them).

Define in myassert.h and include everywhere in your app (force include in Visual Studio?).

// Works cross-platform. No overhead in release builds
#ifdef DEBUG

    // This function may need to be implemented in a cxx module if
    // your compiler optimizes this away in debug builds
    inline bool my_assert_func(const bool b)
    {
        // can set true/false breakpoints as needed
        if (b) {
            return true;
        }
        else {
            return false;
        }
    }

    #define myassert(b) assert(my_assert_func(b))

#else // RELEASE

    // In release builds this is a NOP
    #define myassert(b)    ((void)0)

#endif

#define DEBUG_BREAKPOINT myassert(true)

Now you can:

string token = GetNextToken();
if (Ignore(token)) 
    DEBUG_BREAKPOINT;
}
else {
    // process token
    DoThis(token);
    DoThat(token);
}

You could define a global unsigned int debug_counter , then your "no-op" macro can be debug_counter++ . Pretty sure the compiler won't remove that, but to be absolutely sure, put some code somewhere to print the value of the counter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM