Heads up... You're reading this book for free, with parts of this chapter shown beyond this point astext.
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.
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:
[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
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.
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
.lldbinitfile, so this particular implementation will be ignored throughout the book.
Creating the .lldbinit file
In this section you’re going to create your first
command alias -- Yay_Autolayout expression -l objc -O -- [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] recursiveDescription]
(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
(lldb) po 0x7fc8295071a0
(lldb) cpo 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.