Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 440
dog44wgm Avatar asked Jan 27 '12 11:01

dog44wgm


People also ask

How do you set a breakpoint in a function?

You can set a breakpoint at a line number, using the stop at command, where n is a source code line number and filename is an optional program file name qualifier. If the line specified is not an executable line of source code, dbx sets the breakpoint at the next executable line.

How do you set a conditional breakpoint?

To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.

What is a breakpoint in C?

A breakpoint stops the execution of a program and returns control to the debugger, where its variables and memory can be examined before continuing. Breakpoints can be set for specific functions, lines or memory locations with the break command.


2 Answers

An actual no-op instruction:

__asm nop
like image 86
Blorgbeard Avatar answered Oct 17 '22 04:10

Blorgbeard


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.

like image 33
Tony The Lion Avatar answered Oct 17 '22 05:10

Tony The Lion