breakpoint function executed x times

Viewed 52

I want to use a breakpoint when a certain code (function) is executed for a certain number of times. The problem is that my easy code is optimalized out and the breakpoint is never reached. How can i break a code after it excecuted a certain number of times

static int count = 0;
static int test = 0;
if (count == 5) {
    //Break here before getting stuck
    count++;
    test++;
    count--;
};
3 Answers

You answered on your question: Breakpoint is never reached. And your code never execured requested number of times.

So, you can disable optimization.

Always when compiling for debugging, switch off ( or keep it at minimal ) all the optimisations ( -O0 ).

Alternatively when using gcc, you can use function attributes to tell compiler to not optimise a function ( using attribute((optimize(0))) )

you can try __debugbreak(); function when your condition is fulfilled.

Related