Search code examples
c++lldb

LLDB summary strings without quotes


Lets say that I have a c++ class that contains two c strings like below.

class PathExample {
    char* partA; // Eg: "/some/folder/"
    char* partB; // Eg: "SomeFile.txt"
}

I can make an lldb summary string for it:

type summary add PathExample --summary-string "${var.partA}${var.partB}"

However this adds unnecessary and confusing quotes "/some/folder/""SomeFile.txt". How can I format the type summary string to not use quotes, or at least append the strings before adding quotes? Eg: "/some/folder/SomeFile.txt"


Solution

  • "Remove leading or trailing quote in the summary value" before adding to the output is a not supported by the summary string formatting options. We're trying to keep those options fairly streamlined, and that's a bit too much of a special purpose feature.

    The thing that allows us to keep the summary string version fairly restrained is that you can always write a Python summary, which allows you to format up the output in whatever way you like. There's an example that's somewhat like what you want in the section on Python scripting:

    https://lldb.llvm.org/use/variable.html#python-scripting

    You wouldn't use GetValueAsUnsigned as that example does. The C-string rendering of char * types is actually done by a built-in summary, so you would use "SBValue.GetSummary" to get the string value. That's actually the same thing that's substituted into the summary string so it also has the quotes on it. But in Python it's trivial to strip the leading and trailing quotes before concatenating the two strings.

    Note, though it's convenient for playing around with, you don't have to define the Python summary callback inline as shown in the example. You can put a function with the correct signature in a .py file somewhere, use command script import <path to .py file> and then import it using the -F option to type summary add. Remember to use the full name of the function (module_name.func_name) when you specify it. I have a bunch of these in a ~/.lldb directory and command script import them in my ~/.lldbinit.

    help type summary add has some more details on how to do this.