Sometimes you find yourself in a situation where you have to fix a bug or add a new features to a codebase you know little about. Wether you moved to a new job, downloaded an open source project, or came back to one of your old projects, you may not know where things are.

In these situations, in order to start editing code, you need to understand the codebase and find which parts of the code are responsible for which screens/features.

Imagine you are trying to find the method that is executed when a table cell is selected on a given screen.

You could use a brute force approach, by searching for the didSelectRow and narrow the result down to the file that seems closer to the screen at hand, and then probably add breakpoints and run the action to make sure you got the correct location. This approach, that I am guilty of using, is error prone and on the long run, it teaches nothing.

A more sane approach would be to use the debugger, lldb, and Xcode view debugging to easily and more precisely discovering the code.

Using lldb as a discovery mechanism has some advantages:

It tests and improve your knowledge of cocoa touch framework and objective-c runtime.

It improves your general debugging skills.

It is a more scientific approach, since you can quickly make assumptions and accept or reject them.

It is much faster to add and remove breakpoints, than is to search text and read code.

For this article, I am going to discover and fix a couple of issues in an unknown codebase, by using lldb and Xcode view debugging and never going to the source code, except when breakpoints are hit.

We will use my own fork of hniosreader by Marcin Kmiec, a Hacker news reader app. In this fork I introduced some bugs that we are going to fix.

Lets start by cloning the project and installing the pods



cd hniosreader

pod install git clone https://github.com/oarrabi/hniosreader cd hniosreaderpod install

We will use Xcode view debugging, that means we need Xcode 6 and iOS 8+, and we are going to use a 32-bit iPhone simulator device, since we will stick with x86 ABI calling conventions for lldb.

Lets start by opening the project and looking at the first bug.

First Bug:

Switch to bug1 branch of the git repo.

The bug description is:

Selecting any table row always displays the first row content.

Since it happens when we select a row, we start by adding a break point on didSelectRow. To do that, Run the app, then pause the execution using Debug -> Pause menu item, and write the following in the console.

breakpoint set -r “didSelectRow”

The above adds a regular expression breakpoint for any method that contains the string didSelectRow.

Next, continue the app execution by writing continue (and hit enter) in lldb or Debug -> Continue menu item. Tap on any cell to reproduce the issue, Xcode now should be pointing at EntryListController line 242.

self.selectedRow = indexPath;

[self showWebViewAtIndexPath:nil];

Now since this issue was introduced by me, I know that I should pass indexPath instead of nil, lets fix the bug by passing indexPath.