0

I am trying to define a custom summary for Boosts static_string class template. Example source file:

#include <boost/static_string/static_string.hpp>

const boost::static_string<5> s{"abc"};

Without any formatters, frame variable -R s gives me

(boost::static_strings::static_string<5>) s = {
  boost::static_strings::detail::static_string_base<5, char, std::__1::char_traits<char> > = {
    size_ = '\x03'
    data_ = {
      [0] = 'a'
      [1] = 'b'
      [2] = 'c'
      [3] = '\0'
      [4] = '\0'
      [5] = '\0'
    }
  }
}

I get very close with the builtin formatting for zero-terminated C-strings, i.e., frame variable -f s &s.data_:

(boost::static_strings::detail::static_string_base<...>::value_type (*)[6]) &data_ = "abc"

but trying to automate that with

type summary add -x "^boost::static_strings::static_string<.+>$" --summary "${&var.data_%s}"

doesn't work (also tried "${var.&data_%s}") - the custom format string cannot be parsed. According to the docs, an indirection *var works with the special var placeholder, but & doesn't seem to work (it's also not mentioned in the docs, so that was a wild guess).

lubgr
  • 37,368
  • 3
  • 66
  • 117

1 Answers1

0

The python API can be of help here. For summary strings, you can register a custom python function that returns the desired output. This function accepts an instance of SBValue (and some internal dictionary that you shouldn't touch). Exemplary boost.py:

import lldb

def staticStringSummary(valobj, unused):
    data = valobj.GetChildMemberWithName('data_')
    err = lldb.SBError()
    string = data.GetData().GetString(err, 0)
    return string

def __lldb_init_module(debugger, internalDict):
    debugger.HandleCommand('type summary add -x "^boost::static_strings::static_string<.+>$" -F boost.staticStringSummary')

If you load the above file in a (possibly project-local) .lldbinit as

command script import path/to/boost.py

for the above example, fr v will yield

(boost::static_strings::static_string<5>) s = abc

Note that Boost's static_string class makes sure there is always a zero-byte at the end, which makes sure this solution works. Not sure whether this is the best approach, though (I came up with it while formulating the question).

lubgr
  • 37,368
  • 3
  • 66
  • 117