Recognition LLDB

What is the LLDB?

“If debugging is about removing bugs, then programming is about introducing bugs.” (Edsger W. Dijkstra)

For Apple developers, LLDB is a debugging tool that everyone knows. However, there are a large number of developers whose knowledge of LLDB is still limited to the use of a few basic commands. Today, let’s take a look at LLDB, both familiar and unfamiliar, and see some of its powerful features that you may not have used before. And how to improve our development efficiency.

Before you start playing around with its features, find out what LLDB is. In short, LLDB is an open source debugger with REPL features and C++ and Python plug-ins.

LLDB is a next generation, high-performance debugger. It is built as a set of reusable components which highly leverage existing libraries in the larger LLVM Project, Such as the Clang expression Parser and LLVM disassembler. LLDB is the default debugger in Xcode on Mac OS X and supports debugging C, Objective-C and C++ on the desktop and iOS devices and simulator.

The above is a short introduction from the official documentation, please refer to the OFFICIAL LLDB documentation for more information.

LLDB command structure

Now that you know what LLDB is, you also need to understand its command structure and syntax so that you can stop memorizing commands and start squeezing LLDB. The general structure of the LLDB is as follows:

<command> [<subcommand> [<subcommand>...]] <action> [-options [option-value]] [argument [argument...]]

Among them:

  • Command, subcommand: LLDB debugging command name. Commands and subcommands are arranged hierarchically: a command object creates a context for the subcommand object that follows it, which creates a context for its subcommands, and so on.
  • Action: Command actions that you want to perform in the context of the previous command sequence.
  • Options: Command options, Action modifiers Usually with some value.
  • Argument: Command arguments, representing various things depending on the context in which the command is used.
  • [] : indicates that the command is optional.

Here’s an example:

Breakpoint set -n main

  • Command: breakpoint Breakpoint command
  • Action: set Sets a breakpoint
  • Option: -n Table sets breakpoints according to method name
  • Arguement: mian Indicates that the method name is mian

About the original command:

The LLDB supports raw commands with no command options. Raw commands treat anything that follows the command as an arguement. However, many raw commands can also have command options. When you use command options, you need to add — to distinguish command options from parameters. For example: expression (p/print/call), expression -o (Po), print a UIView object address:

LLDB introduction


Summary of common LLDB commands

Aided memory: Apropos

When we can’t remember a command completely, we can use Apropos to find all relevant command information by using a certain keyword in the command. For example, we want to use stop-hook, but we can’t remember what stop-hook looks like:

LLDB command map

Breakpoint setting

About breakpoint setting, most people are used to using the graphical interface to do, but in debugging some scenarios only rely on the graphical interface is not enough, for example: how to achieve similar KVO through breakpoints to monitor the changes of member variables? Don’t tell me you’re going to rewrite the set method… Even that is not reliable). Here are some useful breakpoint commands:

  1. Breakpoint List: Displays the list of all breakpoints
  2. Breakpoint delete: Delete all breakpoints
  3. Breakpoint disable/enable: Enables the specified breakpoint
  4. Breakpoint set -r some: Traverses all methods in the project that contain the character some and sets breakpoints
  5. Breakpoint allows you to filter breakpoints by file name, function name, number of lines, and re
  6. Watchpoint set expression 0x10CC64D50: Sets a memory breakpoint in memory for an object at address 0x10CC64d50
  7. Watchpoint set variable xxoo: Set a memory breakpoint for the current object variable xxoo
  8. Target stop-hook add -o “frame variable” : adds a command that you want to execute every time the program stops: frame variable (prints all variables in the current stack)
  9. The add, delete, change, and check commands for Target stop-hook and Watchpoint are the same as those for Breakpoint
  10. More abnormal breakpoint play need custom plug-in support, can’t wait you please directly to the end of the article.

Ii. Process control

These two pictures must be familiar to you:

  1. FIG. 1
  2. Figure 2
  • First button: Continue /c to continue execution
  • Second button:
    • Figure 1: Thread step-over/next/n The next step of the current thread (with a complete subfunction as a step)
    • Figure 2: The next step of thread step-inst-over/ni (with an assembly function as a step)
  • Third button:
    • Figure 1: Thread step-in/step/s The next step of the current thread (enter when it encounters a subfunction and continue to step)
    • Figure 2: Next step of thread step-inst-over/si
  • Button 4: Thread step-out/ Finish Exits the current frame stack

Other commands:

  • Thread Return: It takes an optional parameter, and when executed it loads the optional parameter into the return register and immediately executes the return command to exit the current stack frame. This means that the rest of the function will not be executed. This can cause some problems with ARC reference counting, or it can invalidate the cleanup part of the function. But executing this command at the beginning of a function is a great way to isolate the target function and forge the return value.

Executable file & shared library query commands

These commands are used very frequently to reverse and locate errors.

  1. Image List: Lists the main executables and all dependent shared libraries.
  2. Image lookup –address 0x1EC4: Find the original address information in the executable or any shared library.
  3. Image lookup -v –address 0x1ec4:
  4. Image lookup –type NSString: Searches for information of the corresponding type (NSString) based on the name.

Other common command templates

  1. Register Read: Displays the general register of the current thread.
  2. Register write RAx 123: Writes a new decimal value “123” to the current thread register “rax”.
  3. Memory read –size 4 –format x –count 4 0xBffFF3C0: Read memory from address 0xbffff3C0 and display 4 uint32_t hex values.

Enhanced LLDB — Modified.lldbinitFile & plug-in installation

All of the commands listed above can be found here. If you are interested in the use of more commands, you are advised to explore it carefully. Next, we will stand on the shoulders of giants and use the plug-ins specially written for LLDB to explore its potential.

  1. Chisel is an open source LLDB plugin for Facebook

    Brew install Chisel installation process is not described here. After the installation is successful, insert the corresponding file path into the.lldbinit file in the ~/ directory and add a line: Command script import/usr/local/opt/chisel/libexec/FBLLDB. Save after py, restart Xcode can be used. The list of quick commands and instructions it provides are not repeated here. Take a screenshot to feel its power:

  2. DerekSelander/LLDB

    Chisel and chisel are both written in Python. To install the plugin, you need to manually download the repository and add the path to the dslldb.py file in the repository to.lldbinit in the same way as above.

Conclusion:

See this, you gain only temporary memory, in fact, nothing at all… It also wastes precious minutes, which is not what you want, and the only way to avoid it is to do it. Open Xcode, run a project, refer to the documentation covered in this article, and try to type each command to see how it works and how it differs. Finally, set up plugins for your LLDB and feel its evolution. Trust me, your development efficiency will increase by more than a little. Learning is a kind of ability, refused to operate manual type indoctrination, sharing is mostly in the summary of learning harvest for readers to provide some ideas or direction, this is also my original intention for you to retain a glimmer of exploration room, I wish you have a harvest.

The level is limited, please great god more correct. See you next