September 22, 2015

Debugging return values in Xcode

I noticed today when watching a coworker present some app automation code in Xcode 7 that he was utilizing some daisy-chain method call in Swift. It looked something like:

myView.moveLeft()
      .moveRight()
      .moveLeft()
      .moveUp()

This launched a discussion on debugging code that had multiple method calls on a single line. One coworker mentioned how he tends to pull code apart into multiple lines using temp variables so that he can expect it while stepping through with the debugger. Eg:

- (NSArray<Class *> *)getClasses {
    School *school = [self getSchool];
    
    return [school getClasses];
}

I realized there should be a better way of doing this and, after a bit of googling, sure enough there was. Because we can access the registers directly while in LLDB, and we know that the return value of a function invoccation is stored in eax (x86) or r0 (ARM), we can just print the object referenced by the return value register:

(lldb) po $rax
(lldb) p $rax

With this, we can step through each method call on that one line of code and inspect the current state of execution.