Hide chapters

Advanced Apple Debugging & Reverse Engineering

Third Edition · iOS 12 · Swift 4.2 · Xcode 10

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section III: Low Level

Section 3: 7 chapters
Show chapters Hide chapters

Section IV: Custom LLDB Commands

Section 4: 8 chapters
Show chapters Hide chapters

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.

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.
© 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