Home iOS & Swift Books Advanced Apple Debugging & Reverse Engineering

9
Persisting & Customizing Commands Written by Derek Selander

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

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

As you’ve probably noticed in your development career, typing the same thing over and over really sucks. If you use a particular command that’s difficult to type, there’s no reason you should have to type the whole thing out. Just as you’ve learned when creating breakpoints using regular expressions, you’d go crazy typing out the full names of some of those Swift functions.

The same idea can be applied to any commands, settings, or code executed in LLDB. However, there’s two problems that haven’t been addressed until now: persisting your commands and creating shortcuts for them! Every time you run a new LLDB session, all your previous commands you’ve executed will vanish!

In this chapter, you’ll learn how to persist these choices through the .lldbinit file. By persisting your choices and making convenience commands for yourself, your debugging sessions will run much more smoothly and efficiently. This is also an important concept because from here on out, you’ll use the .lldbinit file on a regular basis.

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 starts up but before LLDB has attached to the process (important to know 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.MasterViewController: 0x7fc8295071a0>
(lldb) po 0x7fc8295071a0
(lldb) cpo 0x7fc8295071a0
<Signals.MasterViewController: 0x7fc8295071a0>

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.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2021 Razeware LLC

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 raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.