Breakpoint when a variable takes a specific value

I have something similar to the following code ...

void function(int x) { // complicated operation on x blah blah } 

Everything seems to work fine, except when x turns out to be a specific value, say, "273". But x 273 is a rare event, 99.999% of the time is some other value. Now I want to observe the events when this function is called using x = 273, so I would like to insert a breakpoint that only hits with x, this 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, apparently, the compiler will optimize this if statement because it does nothing. So my question is what should I include in the if statement to make sure it compiles into something ... or should I track the case of x == 273 in a completely different way.

+4
source share
5 answers

It looks like you are looking for conditional breakpoints. This is a feature of Visual Studio that allows a breakpoint to only suffer when a very specific condition is met.

To do this, place a breakpoint at the beginning of the function. Then right-click on it and select "Condition". Then add the following expression

 x == 273 

Now you can debug this without modifying the source binary.

+8
source

Maybe just use a conditional breakpoint? See here how to configure it.

+3
source

Create a new conditional breakpoint (right click breakpoint and select "Condition ...") and place

 x == 273 

as a condition.

0
source
 if (x == 273) { volatile int tmp = 0; // This is not get optimized } 
0
source

In cases where I need a real line to set a breakpoint, I use something like this:

 { int i = 42; } 

It is optimized, but I can get a compiler warning for an unused variable. But a conditional breakpoint (other answers) is probably better in this case

0
source

Source: https://habr.com/ru/post/1304788/


All Articles