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);
}
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.
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.
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.
An actual no-op instruction:
__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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With