I have something analogous to the following code…
void function(int x)
{
// complicated operation on x
blah
blah
}
It all appears to be working fine except when x happens to be a particular value, say “273”. But x being 273 is a rare event, 99.999% of the time it is some other value. Now I wish to observe the events when this function is called with x=273, so I would like to insert a breakpoint that gets hit only with x is that value. Perhaps I could do it like this:
void function(int x)
{
if (x == 273)
{
// put breakpoint on this line.
}
// complicated operation on x
blah
blah
}
The problem is that presumably the compiler will optimise away this “if” statement because it doesn’t do anything. So my question is what should I put within the “if” statement to to make sure it gets compiled in to something… or should I be tracing the x==273 case in some completely different way.
It sounds like what you’re looking for is conditional breakpoints. These are a feature of Visual Studio which allow a break point to only be hit when a very specific condition is true.
To do this, put a break point at the start of the function. Then right click on it and select “Condition”. Then add following expression
Now you can debug this without changing your source binary.