To improve development efficiency, how to add swift code as a custom lldb command

Time:2022-5-6

The most common lldb used by most developers are Po, P, etc., but in addition to these lldb, it also provides many functions
In this article, I’ll show you how to define swift code as an lldb command

  • Add your first lldb command
  • Add lldb command with parameters
  • Convert complex swift code into lldb commands

Understand lldb command structure

In order to add custom lldb commands, we must usecommand aliasLldb command. It has the following structure:

command alias [command_name] expr -l Swift -O -- [swift_code]

Let’s break down the command in detail:

  • Command alias: an lldb command that uses the name swift to add an alias to the code
  • [command_name]: custom command name
  • expr -l Swift -O --: the lldb debugger is required to interpret all the following contents as swift code
  • [swift_code]: swift code that defines custom command logic

For example, if we want to add a command named customph, it prints “Hello world!” For the statement on the console, the lldb command will be as follows:

command alias ph expr -l Swift -O -- print("Hello World!")

Now we have constructed the alias command forph, it’s time to add it to the lldb debugger.

2021-06-24 11.15.23.gif

Looks like he’s working, right~

Add custom command

However, doing so will only make thephThe command is available in this particular debug session. In other words, each time we start a new debugging session, we need to retype the same alias command.

To avoid this, we can use.lldbinitFiles located in the home directory. Note that this is a hidden file. If you can’t see it, you can use the following shortcut to show the hidden file in your Finder:

Shift + Command +.

Once you enable finder to display hidden files, but you still cannot find the file, please continue to create one in your home directory using the following terminal command:

touch ~/.lldbinit

After that, open the file and paste the entire alias command into the Lldbinit is in the file you just created. In this way, Xcode will execute our alias command every time we start a new debugging session.

Add lldb command with parameters

In this section, let’s go a step further and add a command that can accept parameters. For demonstration purposes, let’s modify ourphCommand so that it can accept a string and print a greeting message accordingly.

command regex [command_name] 's/[regex]/expr -l Swift -O -- [swift_code]/'

I won’t introduce the working principle of regex command in detail. You can google it yourself, hahaha. Generally speaking, all you need to do is [regex] replace (. +) with regular expression statement, and then%1 use it to represent parameters in swift code.

With this in mind, we can update it accordinglyphCommand, as follows:

command regex ph 's/(.+)/expr -l Swift -O -- print("hello \(%1)!")/'

This is how it works (assuming name = “666”):

2021-06-24 11.47.36.gif

Now you know how to add a custom lldb command with parameters.

Convert complex swift code into lldb commands

One warning of adding custom swift code as an lldb command is that everything must be done on one line. Therefore, if we have a multi line swift function, we must convert it to a single line before we can add it to Lldbinit file.

Suppose we want to add the following swift function that converts RGB values to hexadecimal values:

func hex(r: Int, g: Int, b: Int) {

    /* Make sure RGB value within range */
    if (r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255) {

        let rgb:Int = r<<16 | g<<8 | b<<0
        let hex = String(format:"#%06x", rgb)

        print(hex)
    } else {
        print("Invalid input value")
    }
}

Notice how I use code comments/* */Grammar instead of//, this is to ensure ourSwiftThe code does not break after it is later converted to a single line.

Most importantly, we need to make some adjustments to the swift code before we can convert it to one line. Here’s what we need to do:

  • Define one parameter for each function.
  • Will% 1,% 2,% 3 Assigned to each defined variable.
  • add to; At the end of each statement.

This is what our swift code looks like after adjustment:

let r = %1;
let g = %2;
let b = %3;

/* Make sure RGB value within range */
if (r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255) {

    let rgb:Int = r<<16 | g<<8 | b<<0;
    let hex = String(format:"#%06x", rgb);

    print(hex);
} else {
    print("Invalid input value");
}

With this, we can continue to convert our swift code into one line. I personally like to use thisFree online toolsPerform single line conversion. However, if you know any great tools to recommend, please feel free to let me know.

let r = %1; let g = %2; let b = %3; /* Make sure RGB value within range */ if (r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255) { let rgb:Int = r<<16 | g<<8 | b<<0; let hex = String(format:"#%06x", rgb); print(hex); } else { print("Invalid input value"); }

Once we convert swift code into one line, we can construct regex commands like this:

command regex hex 's/(.+) (.+) (.+)/expr -l Swift -O -- let r = %1; let g = %2; let b = %3; if (r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255) { let rgb:Int = r<<16 | g<<8 | b<<0; let hex = String(format:"#%06x", rgb); print(hex); } else { print("Invalid input value"); }/'

Continue and paste the command into Lldbinit file, and then we can start the project breakpoint.

Tips: don't forget source

command source ~/.lldbinit

Use the custom lldb command to convert RGB to hexadecimal values gif

Practical custom lldb commands

Now that you have learned how to add custom commands to the lldb debugger, what custom lldb commands should you add? For example, dictionary, array, data, etc., such as JSON string on Xcode console. It can be realized by yourself. If you like, you can automatically enter your password and log in. Welcome to communicate