2 years ago

#61898

test-img

Reinhard Männer

How to set conditional breakpoints in a computing heavy loop

My app has a recursive loop that runs for maybe 30 sec.
During execution under Xcode, the CPU load is close to 100%.
To debug execution, I set a conditional breakpoint with condition like variable == someValue. If such a breakpoint is enabled, my app slows down roughly by a factor of 100, i.e. the CPU load is about 1%. Therefore, it is no longer possible to debug the app, simply because it will run now for 1 hour.

As a workaround, I replaced the conditional breakpoint by a statement like

    if variable == someValue {
//              noop
    }  

and set the breakpoint at the out commented noop.
In this case, the app runs again at 100% CPU load.

Of course, I don’t like such a hack. There should be a possibility to set conditional breakpoints without slowing down the app by a factor of 100.
But how?

One can check the situation easily. Just try the computing intensive Ackerman function

    func ackerman(_ m: Int, _ n:Int) -> Int {
        if m == 0 {
            return n+1
        } else if n == 0 {
            return ackerman(m-1, 1)
        } else {
            return ackerman(m-1, ackerman(m, n-1))
        }
    }
    
    print(ackerman(4, 1))  

It will run for a very long time and probably eventually crash because of stack overflow. But during run, one can set a conditional breakpoint e.g. at the if instruction.
This will slow down execution by a factor of 100.
If, instead of setting the conditional breakpoint, one adds the following if statement at the beginning of the function

    if m == 100 {
//  noop
    }  

one can set an unconditional breakpoint at the out commented noop, and the loop will continue at full speed.

swift

xcode

breakpoints

lldb

0 Answers

Your Answer

Accepted video resources