[LLDB] Simplify libstdc++ string summaries (#146562)

From #143177. This combines the summaries for the pre- and post C++ 11
`std::string` as well as `std::wstring`. In all cases, the data pointer
is reachable through `_M_dataplus._M_p`. It has the correct type (i.e.
`char*`/`wchar_t*`) and it's null terminated, so LLDB knows how to
format it as expected when using `GetSummaryAsCString`.
This commit is contained in:
nerix
2025-07-02 12:21:31 +02:00
committed by GitHub
parent 40275a4ee3
commit 4c7a706589
3 changed files with 17 additions and 138 deletions

View File

@@ -1344,38 +1344,31 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
.SetShowMembersOneLiner(false)
.SetHideItemNames(false);
lldb::TypeSummaryImplSP std_string_summary_sp(
new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p}"));
lldb::TypeSummaryImplSP cxx11_string_summary_sp(new CXXFunctionSummaryFormat(
lldb::TypeSummaryImplSP string_summary_sp(new CXXFunctionSummaryFormat(
stl_summary_flags, LibStdcppStringSummaryProvider,
"libstdc++ c++11 std::string summary provider"));
lldb::TypeSummaryImplSP cxx11_wstring_summary_sp(new CXXFunctionSummaryFormat(
stl_summary_flags, LibStdcppWStringSummaryProvider,
"libstdc++ c++11 std::wstring summary provider"));
"libstdc++ std::(w)string summary provider"));
cpp_category_sp->AddTypeSummary("std::string", eFormatterMatchExact,
std_string_summary_sp);
string_summary_sp);
cpp_category_sp->AddTypeSummary("std::basic_string<char>",
eFormatterMatchExact, std_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::basic_string<char,std::char_traits<char>,std::allocator<char> >",
eFormatterMatchExact, std_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
eFormatterMatchExact, std_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
cpp_category_sp->AddTypeSummary("std::__cxx11::string", eFormatterMatchExact,
cxx11_string_summary_sp);
string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::__cxx11::basic_string<char, std::char_traits<char>, "
"std::allocator<char> >",
eFormatterMatchExact, cxx11_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
cpp_category_sp->AddTypeSummary("std::__cxx11::basic_string<unsigned char, "
"std::char_traits<unsigned char>, "
"std::allocator<unsigned char> >",
eFormatterMatchExact,
cxx11_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
// making sure we force-pick the summary for printing wstring (_M_p is a
// wchar_t*)
@@ -1395,11 +1388,11 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
eFormatterMatchExact, std_wstring_summary_sp);
cpp_category_sp->AddTypeSummary("std::__cxx11::wstring", eFormatterMatchExact,
cxx11_wstring_summary_sp);
string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, "
"std::allocator<wchar_t> >",
eFormatterMatchExact, cxx11_wstring_summary_sp);
eFormatterMatchExact, string_summary_sp);
SyntheticChildren::Flags stl_synth_flags;
stl_synth_flags.SetCascades(true).SetSkipPointers(false).SetSkipReferences(

View File

@@ -239,122 +239,12 @@ VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName(ConstString name) {
bool lldb_private::formatters::LibStdcppStringSummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
const bool scalar_is_load_addr = true;
auto [addr_of_string, addr_type] =
valobj.IsPointerOrReferenceType()
? valobj.GetPointerValue()
: valobj.GetAddressOf(scalar_is_load_addr);
if (addr_of_string != LLDB_INVALID_ADDRESS) {
switch (addr_type) {
case eAddressTypeLoad: {
ProcessSP process_sp(valobj.GetProcessSP());
if (!process_sp)
ValueObjectSP ptr = valobj.GetChildAtNamePath({"_M_dataplus", "_M_p"});
if (!ptr)
return false;
StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
Status error;
lldb::addr_t addr_of_data =
process_sp->ReadPointerFromMemory(addr_of_string, error);
if (error.Fail() || addr_of_data == 0 ||
addr_of_data == LLDB_INVALID_ADDRESS)
return false;
options.SetLocation(addr_of_data);
options.SetTargetSP(valobj.GetTargetSP());
options.SetStream(&stream);
options.SetNeedsZeroTermination(false);
options.SetBinaryZeroIsTerminator(true);
lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
addr_of_string + process_sp->GetAddressByteSize(), error);
if (error.Fail())
return false;
options.SetSourceSize(size_of_data);
options.SetHasSourceSize(true);
if (!StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF8>(options)) {
stream.Printf("Summary Unavailable");
stream << ptr->GetSummaryAsCString();
return true;
} else
return true;
} break;
case eAddressTypeHost:
break;
case eAddressTypeInvalid:
case eAddressTypeFile:
break;
}
}
return false;
}
bool lldb_private::formatters::LibStdcppWStringSummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
const bool scalar_is_load_addr = true;
auto [addr_of_string, addr_type] = valobj.GetAddressOf(scalar_is_load_addr);
if (addr_of_string != LLDB_INVALID_ADDRESS) {
switch (addr_type) {
case eAddressTypeLoad: {
ProcessSP process_sp(valobj.GetProcessSP());
if (!process_sp)
return false;
CompilerType wchar_compiler_type =
valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar);
if (!wchar_compiler_type)
return false;
// Safe to pass nullptr for exe_scope here.
std::optional<uint64_t> size =
llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr));
if (!size)
return false;
const uint32_t wchar_size = *size;
StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
Status error;
lldb::addr_t addr_of_data =
process_sp->ReadPointerFromMemory(addr_of_string, error);
if (error.Fail() || addr_of_data == 0 ||
addr_of_data == LLDB_INVALID_ADDRESS)
return false;
options.SetLocation(addr_of_data);
options.SetTargetSP(valobj.GetTargetSP());
options.SetStream(&stream);
options.SetNeedsZeroTermination(false);
options.SetBinaryZeroIsTerminator(false);
lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
addr_of_string + process_sp->GetAddressByteSize(), error);
if (error.Fail())
return false;
options.SetSourceSize(size_of_data);
options.SetHasSourceSize(true);
options.SetPrefixToken("L");
switch (wchar_size) {
case 8:
return StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF8>(options);
case 16:
return StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF16>(options);
case 32:
return StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF32>(options);
default:
stream.Printf("size for wchar_t is not valid");
return true;
}
return true;
} break;
case eAddressTypeHost:
break;
case eAddressTypeInvalid:
case eAddressTypeFile:
break;
}
}
return false;
}
LibStdcppSharedPtrSyntheticFrontEnd::LibStdcppSharedPtrSyntheticFrontEnd(

View File

@@ -18,11 +18,7 @@ namespace lldb_private {
namespace formatters {
bool LibStdcppStringSummaryProvider(
ValueObject &valobj, Stream &stream,
const TypeSummaryOptions &options); // libcstdc++ c++11 std::string
bool LibStdcppWStringSummaryProvider(
ValueObject &valobj, Stream &stream,
const TypeSummaryOptions &options); // libcstdc++ c++11 std::wstring
const TypeSummaryOptions &options); // libstdc++ std::string
bool LibStdcppSmartPointerSummaryProvider(
ValueObject &valobj, Stream &stream,