LLDB debugger in Xcode — 3

👍
· Jan 23, 2024 · 4 mins read
LLDB debugger in Xcode — 3

Manage watchpoints

From the the first and the second articles of this series, we have known how to set breakpoints to pause the code and explore the app runtime. Apart from breakpoints, LLDB offers another way to pause the code called watchpoint, which will be the topics of this article.

Watchpoint

Watchpoint is a memory address that we specify and lldb will pause the app when there’s read or write operation to this address. Please note, unlike the breakpoints, there is usually a limit for the number of watchpoints.

Set watchpoints in Xcode

In the Xcode variable viewer, Command-click any variable, we can see a context menu like below:

If select Watch menu item, this will add a watchpoint to the memory address of this variable, which will cause the code to stop on write operations on this piece of memory.

LLDB commands for watchpoints

1. Set watchpoints

# Watch memory of variable `a` on write operation  
(lldb) watch set variable a  
# Or  
(lldb) watch set variable -w write a  
  
# Watch memory of variable `a` on read operation  
(lldb) watch set variable -w read a  
  
# Watch memory of variable `a` on read & write operation  
(lldb) watch set variable -w read_write a  
  
# For a reference type in Swift, can also use the following command  
(lldb) watch set expression -- someClassInstance

It will generate the output like this:

Watchpoint created: Watchpoint 2: addr = 0x10a01a3c0 size = 8 state = enabled type = w  
    declare @ '/Users/tim.wang/.../ViewController.swift:74'  
    watchpoint spec = 'a'

Pay attention to the watchpoint number in the output, because we need this number for other operations. Of course. We can also use list command below to retrieve the number in case we forget.

2. List watchpoints

(lldb) watch list

This command will generate output like the following:

Current watchpoints:  
Watchpoint 2: addr = 0x10e0163c0 size = 8 state = enabled type = rw  
    declare @ '/Users/tim.wang/.../ViewController.swift:74'  
    watchpoint spec = 'a'

As mentioned earlier, we can use this command to retrieve the watchpoint any time needed.

3. Delete watchpoints

# Delete all the watchpoints  
(lldb) watch delete  
  
# Delete one watchpoint of the ID  
(lldb) watch delete 1  
  
# Delete two or more watchpoints using ID list  
(lldb) watch delete 1 2

4. Modify watchpoints

Like breakpoints, if we only want to pause the execution when some conditions are met. We can use modify command like below:

# Modify conditions for all watchpoints  
(lldb) watch modify -c '(a==5)'  
  
# Modify conditions for watchpoint of ID or ID list  
(lldb) watch modify -c '(a==5)' 2  
(lldb) watch modify -c '(a==5)' 2 3

5. Add commands to watchpoints

Commands are the code to run when the execution is paused at the watchpoints. We can manage commands for watchpoints in the follow ways:

# Add command to all watchpoints  
(lldb) watch command add -o 'po a'  
  
# Add command to watchpoint of ID  
(lldb) watch command add -o 'po a' 2  
  
  
# List command that attached to the watchpoint of ID  
(lldb) watch command list 2  
  
# Delete command that attached to the watchpoint of ID  
(lldb) watch command delete 2

This works very similar to the actions for breakpoints. Please note, there is another way to run certain code when execution is paused, which is target stop-hook . Let’s cover this in the future articles.

6. Disable/Enable/Ignore watchpoints

Like breakpoints, we can disable/enable and ignore them temporarily using the follow scripts:

# Disable watchpoint of ID or ID list  
(lldb) watchpoint disable 1  
(lldb) watchpoint disable 1 2  
  
# Enable watchpoint of ID or ID list  
(lldb) watchpoint enable 1  
(lldb) watchpoint enable 1 2  
  
# Ignore watchpoint  
# ignore 5 times on watchpoint 1  
(lldb) watchpoint ignore -i 5 1   
  
# ignore 5 times on watchpoint 1 & 2  
(lldb) watchpoint ignore -i 5 1 2 

Conclusion

Breakpoints and watchpoints are great tools in Xcode LLDB, through which, we can explore the execution of our code. To me, not only can they help debug our code, but give me aha moments often that would spice up our coding life. How do you think about them?

Comments

Sharing is caring!