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

9. Persisting & Customizing Commands
Written by Walter Tyree

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

With watchpoints covered, you now have an excellent foundation with the basic workflows within lldb, but there’s two problems that haven’t been addressed: persisting your lldb commands and creating shortcuts for them!

In this chapter, you’ll learn how to create simple, custom commands and save them for future lldb sessions!

Persisting… How?

Whenever lldb is invoked, it searches several directories for special initialization files. If found, these files will be loaded into lldb as soon as lldb launches but before lldb attaches to a process. This is an important detail if you’re trying to execute arbitrary code in the init file.

You can use these files to specify settings or create custom commands to do your debugging bidding.

lldb searches for an initialization file in the following places:

  1. ~/.lldbinit-[context] where [context] is Xcode, if you are debugging with Xcode, or lldb if you are using the command line incarnation of lldb.

    For example, if you wanted commands that were only available in lldb while debugging in the Terminal, you’d add content to ~/.lldbinit-lldb, while if you wanted to have commands only available to Xcode you’d use ~/.lldbinit-Xcode.

  2. Next, lldb searches for content found in ~/.lldbinit. This is the ideal file for most of your logic, since you want to use commands in both Xcode and Terminal sessions of lldb.

  3. Finally, lldb will search the directory where it was invoked. Unfortunately, when Xcode launches lldb, it’ll launch lldb at the / root directory. This isn’t an ideal place to stick an .lldbinit file, so this particular implementation will be ignored throughout the book.

Creating the .lldbinit File

In this section you’re going to create your first .lldbinit file.

nano ~/.lldbinit
command alias -- Yay_Autolayout expression -l objc -O -- [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] recursiveDescription]
(lldb) Yay_Autolayout
(lldb) help Yay_Autolayout
command alias -H "Yay_Autolayout will get the root view and recursively dump all the subviews and their frames" -h "Recursively dump views" -- Yay_Autolayout expression -l objc -O -- [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] recursiveDescription]

Command Aliases With Arguments

You’ve just created a standalone command alias that doesn’t require any arguments. However, you’ll often want to create aliases to which you can supply input.

command alias cpo expression -l objc -O --

(lldb) po self
<Signals.MainViewController: 0x15210c180>
(lldb) po 0x15210c180
(lldb) cpo 0x15210c180
<Signals.MainViewController: 0x15210c180>
command alias lpo expression -l %1 -O --
(lldb) lpo swift self
<Signals.MainViewController: 0x152b0bc20>
(lldb) lpo swift 0x152b0bc20
(lldb) lpo objc 0x152b0bc20
<Signals.MainViewController: 0x152b0bc20>

Key Points

  • Use command alias to make shortcuts for commands you use often.
  • lldb will load initialization commands from a contextual .lldbinit file for Xcode (.lldbinit-Xcode) or for terminal (.lldbinit-lldb).
  • lldb will also load initialization commands from a generic .lldbinit file on launch. This file is read after the contextual one mentioned above.
  • You can specify aliases within an lldb session, but they will only live for that session.
  • For aliases, be sure to add -h and -H help text to remind future you why you made them and how to use them.

Where to Go From Here?

You’ve learned how to create aliases for simple commands as well as persist them in the .lldbinit file. This will work across both Xcode and Terminal invocations of lldb.

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.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now