Advanced+apple+debugging (8)

Source: Internet
Author: User
Tags print object

You might notice that in your developer career, it's really bad to enter the same thing over and over again. If you use a command that is cumbersome to enter, there is no reason why you should enter the complete instruction. Just because you learned how to create a regular expression breakpoint, You have to enter the full name of the Swift function crazily.
The same view can be applied to all commands, settings, or code execution on lldb. However, as of now, there are two unresolved issues: Create shortcut keys for your instructions and save them! Every time you run a new LLDB session, all the commands you've executed before will disappear!
In this chapter, you will learn how to make these commands persistent through the. lldbinit file. By persisting you create convenient commands for yourself, your debugging process will become smooth and efficient. This is also a very important concept because from now on you will be using. lldbinit files on a regular basis.

How to persist?

When Lldb is called, it searches for the initialization file specified under several directories. If found, these files will be loaded into lldb before attaching to the specified process (if you want to execute any code at the time of initialization) and you need to know that. You can use these files to specify some settings or to create custom debug commands.
LLDB will find the initialization file in the following places:

The content of the ~/.lldbinit-[context] [context] location is Xcode, if you use Xcode debugging, or Lldb if you debug with Lldb through the command line. For example, if you debug in a terminal but want the command to take effect only in Lldb, The content you need to add is ~/.lldbinit-lldb, and you need to use ~/.lldbinit-xcode if you want the command to be effective only in Xcode.
Next, LLDB will search for content found in ~/.lldbinit. This is the ideal file to handle most of the logic, if you want to use the LLDB command in the terminal or Xcode.
Finally, LLDB will search for the directory where it was invoked. Unfortunately, when Xcode is Lldb, the/directory is lowered. This is not the ideal location for placing. lldbinit files, so this particular implementation is ignored in this book.
Create a. lldbinit file

In this chapter you will create your first. lldbinit file.
First, open the terminal and enter:

Nano ~/.lldbinit
This will open your. lldbinit file with the Nano editor. If the file already exists in that location, Nano opens the. lldbinit file instead of creating a new one.

Note: You should really use a few similar vi and editor edits emacs .lldbinit , and then angrily write out how other editors are unconventional in your blog. I suggest that the use nano is deliberate
After you open it in the Nano Editor, add the following code at the end of the. lldbinit file:

Command Alias--yay_autolayout expression-l Objc-o--
[[[[[[UIApplication sharedapplication] Keywindow] Rootviewcontroller]
View] recursivedescription]
The purpose of this code is to create an alias-that is, an abbreviation for a long expression command. The name of the alias is called Yay_autolayout and it executes an expression command to get the root of the UIView (which only takes effect in iOS) and extracts the location of all the child views of the root view and re-layouts all the child views.
Press CTRL + O to save your changes, but you have not exited the nano yet.
Open the signals Xcode project and build and run the signals program. After running, stop execution and enter the alias in the debugger:

(LLDB) Yay_autolayout
This will extract all the views in the application! It's exciting!

Note: This cool command, whether or not you own the app's source code, does the same thing. You can attach the LLDB to the emulator SpringBoard and extract all the views by performing the same method.
Now, try to get help with this new command:

(LLDB) Help Yay_autolayout
The content of the output looks a bit boring. You can do a little better. Go back to the Nano terminal and add some useful information to the alias of the command. It looks like this:

Command Alias-h "Yay_autolayout'll 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]
Save the file by pressing CTRL + O. Next, build and run the signals project.
Now when you stop the debugger and type in Help Yay_autolayout, you'll see the output of these helpful text at the bottom. Can be viewed with the-H command. You can also get some profiles simply by typing help, which gives a description of-H and some other commands.
These may seem meaningless now, but when you have many custom commands in the. lldbinit file, you will appreciate the documentation you have provided for yourself.

Alias with parameter command

You just created an alias for a single-line command that doesn't require any arguments. However, you will often need to create aliases for some commands that support input.
Return to the terminal window that opens the nano. At the bottom of the file, enter the following:

Command alias CPO Expression-l Objc-o--
You create a new command called CPO. The CPO will do an ordinary Po (Print object) operation, but it will use the OBJECTIVE-C environment. This is an ideal command when you are in the SWIFT environment but you want to print out a memory address or a register or the available Objective-c object you know.
Save the changes you made in the Nano, and then return to the signals project. Find Masterviewcontroller's viewdidload and then create a breakpoint at the top of the function. Build and run the application:


The best way to understand the importance of the CPO command is to get the Masterviewcontroller reference first.
(LLDB) PO Self
You will get output similar to the following:

Take out the memory address at the end of the output (typically, the memory address you get is different from the above) and try to print it out in the debugger:

(LLDB) PO 0x7fc8295071a0
This will not produce any meaningful output, because you are stuck in a swift file, and Swift is a type-safe language. Simply printing the memory address in Swift is capturing and doing anything. This is why the OBJECTIVE-C environment is so useful when debugging, especially when dealing with assembly code with only one memory address.
Now, apply the new command you just created to the address:

(LLDB) CPO 0x7fc8295071a0
You will see the same output as you do with the PO Self command:

This is a useful command to get the description of an object, whether the object was created with Objective-c or by using Swift.
This is a very reluctant example, because you will only use the PO Self command. However, if you have only one memory address, then you can not use the PO, this time the CPO comes in handy.

Why are we learning this?

You have learned how to wear a simple command alias and save them in a. lldbinit file. This can be effective in Xcode's lldb, and also in the lldb of the terminal.
As an exercise, add the Help information for your newly created CPO command in the ~/.lldbinit file, so that you can still remember the role of this command when you create many custom commands. Remember that the-H option is a concise help message, The-h option is a long help message. Remember to use--split the parameters you want to enter into the command.
Also, create an alias for your frequently used commands. Put this alias in the ~/.lldbinit file and use it!

Advanced+apple+debugging (8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.