Xcode Stepping to Random Lines

Filed under Apple on Friday, 10 October, 2008 1:46 am

Lately I've been having some issues with Xcode's debugger not stepping through in sequential order.

Note: You can skip to the bottom to see the settings you need to set, that stop the debugger to jump around the place randomly.

The point of the debugger is that you can examine variables and program logic, "stepping through" the code, a single line at a time.

In the picture below, you can see a sample from Xcode's debugger.
The way the debugger works is when a breakpoint stops the program's execution, you can view the current values that all the variables hold.
The line that Xcode highlights blue hasn't actually been executed yet, it is just stopped on the line so you can process the logic yourself and then you press continue and check if it executes as you expect.

Xcode variable hover.png

"

"

"

"

In the image above, you can see that the variable "number1" still holds the value of 2. This is because as I said above the debugger has stopped, the blue highlighted line has not been executed and will not be until the debugger moves to the line after it.

On a side note, DO NOT ENABLE ZEROLINK, or for that matter any other compiling optimisations such as SSE3 or Fast, etc. If you do, you will have weird problems with the debugger stepping through lines of code in the random orders.
What I mean by this is that the debugger will not step from the current line to the next line and the next. It will go to random lines in a seemingly random order. The reason this happens is because zerolink causes optimisations in the compiled application. The problem with this is the human understandable order of line by line gets thrown away and you therefore cannot debug using your logic.

You can check if Zerolink is enable by selecting:
"Project > Edit Active Target".
"

EditActiveTargetXcode.png

"

"

"

"

"

"

"

"

"

"

"

"

"

"

"

You can disable Zerolink by double clicking on the word "YES", or whatever it says then simply type NO.

Zerolink.png

"

"

"

"

"

"

"

"

"

"
"

"

"

"

"

"

"

"

"

"

"

Update:

Sorry about the long article on something so simple. I've decided not to delete the above as there are some parts that you may find useful (and pretty pictures!). After playing around with different settings I believe i've figured out the settings that actually matter."

Make sure the following have been set:

1) Instruction Scheduling = None
2) Optimization Level " " " = None [-O0]
3)
ZERO_LINK " " " " " " " " "= None

The second thing you need to be careful of is that Xcode has 2 areas these settings can be set:

Project Settings: "Project > Edit Project Settings"
Target Settings: ""Project > Edit Active Target"

It is useful being able to have separate settings for the Project and the Target. However, the Target settings OVERRIDE the Project settings. So it is best to change the Target settings, not the Project settings.

If I discover any other settings that apply, I will update this post. If you see any mistakes or would like to add anything, leave a comment and i will update it :)