LLDB debugger in Xcode — 1

👍
· Jan 23, 2024 · 7 mins read
LLDB debugger in Xcode — 1

Get help & manage breakpoints

LLDB is the current debugger tool that is integrated into Xcode. Even though lldb is a command line tool but Xcode provides UIs allowing us to access to certain lldb functionalities, which made it easier to use. However, not all of the lldb functions have a corresponding Xcode UI and learning the knowledge of lldb can definitely help us fully leverage this awesome tool in both tackling bugs and understanding how certain functions work as well, which will surely bring us to a next level as iOS developers.

In this series of articles, we are going to explore the common functionalities of lldb that help us in our daily development and how we get them done using Xcode UIs and their corresponding commands. If you are keen to learn the full power of the lldb including python scripting, please refer to the lldb website.

Get help

LLDB offers great help document in the terminal. Usually the help and apropos can guide us to the right directions. The explanation of these two commands comes from the help output.

help — Show a list of all debugger commands, or give details about a

specific command.

apropos — List debugger commands related to a word or subject.

The help command

The help command in terminal gives us the output like below that we can learn and follow.

We can also use help on sub commands too. For example, help breakpoint will give us the following.

The apropos command

If we don’t even know which command we should start with, we can use apropos. Using it with one keyword that indicates what we want to do, it will list the related commands that could potentially help us. For example, apropos breakpoint will give us the all the breakpoint related commands like below:

Manage breakpoints

1. Add breakpoints:

In breakpoint panel of Xcode:

In code view of Xcode by clicking line number area or through context menu item:

From Debug/Breakpoints menu:

Using lldb command:

# Swift Error Breakpoint  
(lldb) br s -E swift  
  
# Exception Breakpoint  
(lldb) br s -E c++  
(lldb) br s -E objective-c  
  
# Symbolic Breakpoint, using symbol `viewDidLoad` as an example  
(lldb) b viewDidLoad  
  
# Runtime Issue breakpoint  
(lldb) b os_log_fault_default_callback  
  
# Constraint Error breakpoint  
(lldb) b UIViewAlertForUnsatisfiableConstraints  
  
# Test Failure Breakpoint  
(lldb) b _XCTFailureBreakpoint

Even though we can create breakpoints conveniently through Xcode UIs, adding breakpoints using lldb commands can be very powerful and helpful sometimes. For example:

# Create breakpoints on all places of "return true"   
# in source code using regular expression  
(lldb) breakpoint set -p "return true"  
  
# Create breakpoints on all function named "viewDidLoad"  
(lldb) breakpoint set -n viewDidLoad

2. List breakpoints

Xcode has a debug panel to show all the breakpoints. Please note that not all breakpoints that created using commands will be shown there. The following command will definitely show all the breakpoints we have set.

(lldb) breakpoint list

The output will be like the following:

Please note, in the above screenshot, 5 is the breakpoint ID and the number after 5. is the location ID. This screenshot shows that we have this breakpoint with ID of 5 that happens in 79 locations with the ID from 1 to 79. If we don’t want to show all the locations, we can use -b option to turn it off like below:

(lldb) breakpoint list -b

Both breakpoint ID and location ID will be used in many commands in the following sections. For example, list breakpoint with ID of 5 using the following command.

(lldb) breakpoint list 5

3. Delete breakpoints

Delete breakpoints from breakpoint panel:

Delete breakpoints from code view:

Delete breakpoints from Debug menu:

Delete breakpoints using lldb command:

# Delete all breakpoints  
(lldb) breakpoint delete  
  
# Delete breakpoints with breakpoint ID  
(lldb) breakpoint delete 5  
  
# Delete breakpoints with breakpoint ID and locations ID  
# Since the breakpoint with this location ID cannot deleted separatedly,  
# this command will just disable 5.1 breakpoint  
(lldb) breakpoint delete 5.1

4. Disable breakpoints

While we can use the similar Xcode UIs to disable breakpoints like deleting breakpoints in Xcode, we can use the following lldb command to achieve the same goal:

# Disable all breakpoints  
(lldb) breakpoint disable  
  
# Disable breakpoints with breakpoint ID  
(lldb) breakpoint disable 5  
  
# Disable breakpoints with breakpoint ID and locations ID  
(lldb) breakpoint disable 5.1

5. Enable breakpoints

While we can use the similar Xcode UIs to enable breakpoints like disabling breakpoints in Xcode, we can use the following lldb command to achieve the same goal:

# Enable all breakpoints  
(lldb) breakpoint enable  
  
# Enable breakpoints with breakpoint ID  
(lldb) breakpoint enable 5  
  
# Enable breakpoints with breakpoint ID and locations ID  
(lldb) breakpoint enable 5.1

6. Manage conditions & behaviours of breakpoints

We all know we can edit breakpoints using the Xcode content menu item below, we can do the same in lldb command.

The edit breakpoints UI in Xcode is show below, where we can set breakpoint condition, ignore times, actions, and choose if continue automatically.

LLDB commands for editing breakpoints can do the similar things. Please note, we can set up all these option as well in creating breakpoints command.

# Set condition  
(lldb) breakpoint modify 5 -c "self != nil"  
  
# Set ignore times  
(lldb) breakpoint modify 5 -i 10  
  
# We cannot add actions to a breakpoint in lldb.  
# We can set action when we create the breakpoint  
(lldb) rb viewDidLoad -C "po self"  
  
# Set continue automatically after evaluating actions  
(lldb) breakpoint modify 5.1 -G true

7. Scoping options for setting breakpoints

When we use lldb command to create breakpoints, sometime, it will create many breakpoints because the condition we set in the command will apply to all the linked code. If we want to limit the scope of the breakpoints we are going to set, we can use the following options:

# Limit the breakpoints in certain file  
(lldb) rb viewDidLoad -f "ViewController.swift"  
  
# Limit the breakpoints in certain module  
(lldb) rb viewDidLoad -s Sample  
  
# Limit the breakpoints in the code of the specified language  
(lldb) rb viewDidLoad -L swift

8. Share breakpoints with others

If we want to share the breakpoint settings with others, we can do it using Share Breakpoint context menu item in the breakpoint panel. This will save the the selected breakpoint into a file suffixed with .xcbkptlist that can be shared in the source code.

We can achieve the similar goal using lldb command. Firstly, we export the setting to a file then the others can load the breakpoint settings from the exported file like below:

# Export breakpoint settings to file /tmp/breakpoints.json  
(lldb) breakpoint write -f /tmp/breakpoints.json  
  
# Load breakpoint settings from file /tmp/breakpoints.json  
(lldb) breakpoint read -f /tmp/breakpoints.json

Conclusion

In this article, we have covered some basic topics of using lldb including getting help and managing breakpoints. Hopefully this will help your future debugging journey. We are going to discuss more topics of lldb in the rest of this series, please stay tuned.

Comments

Sharing is caring!