Chapters

Hide chapters

Advanced Apple Debugging & Reverse Engineering

Fourth Edition · iOS 16, macOS 13.3 · Swift 5.8, Python 3 · Xcode 14

Section I: Beginning LLDB Commands

Section 1: 10 chapters
Show chapters Hide chapters

Section IV: Custom LLDB Commands

Section 4: 8 chapters
Show chapters Hide chapters

25. SB Examples, Improved Lookup
Written by Walter Tyree

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

For the rest of the chapters in this section, you’ll focus on Python scripts.

As alluded to earlier, the image lookup -rn command is on its way out. Time to make a prettier script to display content.

Here’s what you get right now with the image lookup -rn command:

When you finish this chapter, you’ll have a new script named lookup which queries in a much cleaner way.

In addition, you’ll add a couple of parameters to the lookup command to add some bells and whistles for your new searches.

Automating Script Creation

Included in the starter directory of this project are two Python scripts that will make your life easier when creating LLDB script content. They are as follows:

  • generate_new_script.py: This creates new skeletons script with whatever name you provide it and stick it into the same directory generate_new_script resides in.
  • lldbinit.py: This script will enumerate all scripts (files that end with .py) located within the same directory as itself and try to load them into LLDB. In addition, if there are any files with a txt extension, LLDB will try to load those files’ contents through command import.

Take both of these files found in the starter folder of this chapter and stick them into your ~/lldb/ directory.

Once the files are in their correct locations, jump over to your ~/.lldbinit file and add following line of code:

command script import ~/lldb/lldbinit.py

This will load the lldbinit.py file which will enumerate all .py files and .txt files found in the same directory and load them into LLDB. This means from here on out, simply adding a script file into the ~/lldb directory will load it automatically once LLDB starts.

Creating the Lookup Command

With your new tools properly set up, open up a Terminal window. Launch a new instance of LLDB:

lldbinit Directory Structure Suggestions

The way I’ve structured my own lldbinit files might be insightful to some. This is not a required section, but more of a suggestion on how to organize all of your custom scripts and content for LLDB.

Implementing the Lookup Command

As you saw briefly in the Chapter 7 “Image”, the foundation behind this lookup command is rather simple. But, with the default image lookup, specifying filters and reading the output are difficult. You’ll focus on enhancing how FindGlobalFunctions works.

Adding Options to Lookup

You’ll keep the options nice and simple and implement only two options that don’t require any extra parameters.

Key Points

  • Give some thought to organizing your .lldbinit and loading scripts now, while you’ve only got a few.
  • Experiment with commands in the lldb console before attempting to write scripts so you can experiment and iterate faster.
  • There’s often more than one API in LLDB’s Script Bridge module that will return the same information. Always be on the lookout for alternatives.
  • You can add options to your generateOptionParser that don’t yet do anything. Then add code for each option one-by-one so you can code and test each one separately.

Where to Go From Here?

There are many more options you could add to this lookup command. You could make a -S or -Swift_only query by going after SBSymbolContext’s SBFunction (through the function property) to access the GetLanguage() API. While you’re at it, you should also add a -m or --module option to filter content to a certain module. Also, don’t forget to add some general help text.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now