Skip to content

Commit 4c7a706

Browse files
authored
[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`.
1 parent 40275a4 commit 4c7a706

File tree

3 files changed

+17
-138
lines changed

3 files changed

+17
-138
lines changed

lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,38 +1344,31 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13441344
.SetShowMembersOneLiner(false)
13451345
.SetHideItemNames(false);
13461346

1347-
lldb::TypeSummaryImplSP std_string_summary_sp(
1348-
new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p}"));
1349-
1350-
lldb::TypeSummaryImplSP cxx11_string_summary_sp(new CXXFunctionSummaryFormat(
1347+
lldb::TypeSummaryImplSP string_summary_sp(new CXXFunctionSummaryFormat(
13511348
stl_summary_flags, LibStdcppStringSummaryProvider,
1352-
"libstdc++ c++11 std::string summary provider"));
1353-
lldb::TypeSummaryImplSP cxx11_wstring_summary_sp(new CXXFunctionSummaryFormat(
1354-
stl_summary_flags, LibStdcppWStringSummaryProvider,
1355-
"libstdc++ c++11 std::wstring summary provider"));
1349+
"libstdc++ std::(w)string summary provider"));
13561350

13571351
cpp_category_sp->AddTypeSummary("std::string", eFormatterMatchExact,
1358-
std_string_summary_sp);
1352+
string_summary_sp);
13591353
cpp_category_sp->AddTypeSummary("std::basic_string<char>",
1360-
eFormatterMatchExact, std_string_summary_sp);
1354+
eFormatterMatchExact, string_summary_sp);
13611355
cpp_category_sp->AddTypeSummary(
13621356
"std::basic_string<char,std::char_traits<char>,std::allocator<char> >",
1363-
eFormatterMatchExact, std_string_summary_sp);
1357+
eFormatterMatchExact, string_summary_sp);
13641358
cpp_category_sp->AddTypeSummary(
13651359
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
1366-
eFormatterMatchExact, std_string_summary_sp);
1360+
eFormatterMatchExact, string_summary_sp);
13671361

13681362
cpp_category_sp->AddTypeSummary("std::__cxx11::string", eFormatterMatchExact,
1369-
cxx11_string_summary_sp);
1363+
string_summary_sp);
13701364
cpp_category_sp->AddTypeSummary(
13711365
"std::__cxx11::basic_string<char, std::char_traits<char>, "
13721366
"std::allocator<char> >",
1373-
eFormatterMatchExact, cxx11_string_summary_sp);
1367+
eFormatterMatchExact, string_summary_sp);
13741368
cpp_category_sp->AddTypeSummary("std::__cxx11::basic_string<unsigned char, "
13751369
"std::char_traits<unsigned char>, "
13761370
"std::allocator<unsigned char> >",
1377-
eFormatterMatchExact,
1378-
cxx11_string_summary_sp);
1371+
eFormatterMatchExact, string_summary_sp);
13791372

13801373
// making sure we force-pick the summary for printing wstring (_M_p is a
13811374
// wchar_t*)
@@ -1395,11 +1388,11 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13951388
eFormatterMatchExact, std_wstring_summary_sp);
13961389

13971390
cpp_category_sp->AddTypeSummary("std::__cxx11::wstring", eFormatterMatchExact,
1398-
cxx11_wstring_summary_sp);
1391+
string_summary_sp);
13991392
cpp_category_sp->AddTypeSummary(
14001393
"std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, "
14011394
"std::allocator<wchar_t> >",
1402-
eFormatterMatchExact, cxx11_wstring_summary_sp);
1395+
eFormatterMatchExact, string_summary_sp);
14031396

14041397
SyntheticChildren::Flags stl_synth_flags;
14051398
stl_synth_flags.SetCascades(true).SetSkipPointers(false).SetSkipReferences(

lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp

Lines changed: 5 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -239,122 +239,12 @@ VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName(ConstString name) {
239239

240240
bool lldb_private::formatters::LibStdcppStringSummaryProvider(
241241
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
242-
const bool scalar_is_load_addr = true;
243-
auto [addr_of_string, addr_type] =
244-
valobj.IsPointerOrReferenceType()
245-
? valobj.GetPointerValue()
246-
: valobj.GetAddressOf(scalar_is_load_addr);
247-
if (addr_of_string != LLDB_INVALID_ADDRESS) {
248-
switch (addr_type) {
249-
case eAddressTypeLoad: {
250-
ProcessSP process_sp(valobj.GetProcessSP());
251-
if (!process_sp)
252-
return false;
253-
254-
StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
255-
Status error;
256-
lldb::addr_t addr_of_data =
257-
process_sp->ReadPointerFromMemory(addr_of_string, error);
258-
if (error.Fail() || addr_of_data == 0 ||
259-
addr_of_data == LLDB_INVALID_ADDRESS)
260-
return false;
261-
options.SetLocation(addr_of_data);
262-
options.SetTargetSP(valobj.GetTargetSP());
263-
options.SetStream(&stream);
264-
options.SetNeedsZeroTermination(false);
265-
options.SetBinaryZeroIsTerminator(true);
266-
lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
267-
addr_of_string + process_sp->GetAddressByteSize(), error);
268-
if (error.Fail())
269-
return false;
270-
options.SetSourceSize(size_of_data);
271-
options.SetHasSourceSize(true);
272-
273-
if (!StringPrinter::ReadStringAndDumpToStream<
274-
StringPrinter::StringElementType::UTF8>(options)) {
275-
stream.Printf("Summary Unavailable");
276-
return true;
277-
} else
278-
return true;
279-
} break;
280-
case eAddressTypeHost:
281-
break;
282-
case eAddressTypeInvalid:
283-
case eAddressTypeFile:
284-
break;
285-
}
286-
}
287-
return false;
288-
}
242+
ValueObjectSP ptr = valobj.GetChildAtNamePath({"_M_dataplus", "_M_p"});
243+
if (!ptr)
244+
return false;
289245

290-
bool lldb_private::formatters::LibStdcppWStringSummaryProvider(
291-
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
292-
const bool scalar_is_load_addr = true;
293-
auto [addr_of_string, addr_type] = valobj.GetAddressOf(scalar_is_load_addr);
294-
if (addr_of_string != LLDB_INVALID_ADDRESS) {
295-
switch (addr_type) {
296-
case eAddressTypeLoad: {
297-
ProcessSP process_sp(valobj.GetProcessSP());
298-
if (!process_sp)
299-
return false;
300-
301-
CompilerType wchar_compiler_type =
302-
valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar);
303-
304-
if (!wchar_compiler_type)
305-
return false;
306-
307-
// Safe to pass nullptr for exe_scope here.
308-
std::optional<uint64_t> size =
309-
llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr));
310-
if (!size)
311-
return false;
312-
const uint32_t wchar_size = *size;
313-
314-
StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
315-
Status error;
316-
lldb::addr_t addr_of_data =
317-
process_sp->ReadPointerFromMemory(addr_of_string, error);
318-
if (error.Fail() || addr_of_data == 0 ||
319-
addr_of_data == LLDB_INVALID_ADDRESS)
320-
return false;
321-
options.SetLocation(addr_of_data);
322-
options.SetTargetSP(valobj.GetTargetSP());
323-
options.SetStream(&stream);
324-
options.SetNeedsZeroTermination(false);
325-
options.SetBinaryZeroIsTerminator(false);
326-
lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
327-
addr_of_string + process_sp->GetAddressByteSize(), error);
328-
if (error.Fail())
329-
return false;
330-
options.SetSourceSize(size_of_data);
331-
options.SetHasSourceSize(true);
332-
options.SetPrefixToken("L");
333-
334-
switch (wchar_size) {
335-
case 8:
336-
return StringPrinter::ReadStringAndDumpToStream<
337-
StringPrinter::StringElementType::UTF8>(options);
338-
case 16:
339-
return StringPrinter::ReadStringAndDumpToStream<
340-
StringPrinter::StringElementType::UTF16>(options);
341-
case 32:
342-
return StringPrinter::ReadStringAndDumpToStream<
343-
StringPrinter::StringElementType::UTF32>(options);
344-
default:
345-
stream.Printf("size for wchar_t is not valid");
346-
return true;
347-
}
348-
return true;
349-
} break;
350-
case eAddressTypeHost:
351-
break;
352-
case eAddressTypeInvalid:
353-
case eAddressTypeFile:
354-
break;
355-
}
356-
}
357-
return false;
246+
stream << ptr->GetSummaryAsCString();
247+
return true;
358248
}
359249

360250
LibStdcppSharedPtrSyntheticFrontEnd::LibStdcppSharedPtrSyntheticFrontEnd(

lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ namespace lldb_private {
1818
namespace formatters {
1919
bool LibStdcppStringSummaryProvider(
2020
ValueObject &valobj, Stream &stream,
21-
const TypeSummaryOptions &options); // libcstdc++ c++11 std::string
22-
23-
bool LibStdcppWStringSummaryProvider(
24-
ValueObject &valobj, Stream &stream,
25-
const TypeSummaryOptions &options); // libcstdc++ c++11 std::wstring
21+
const TypeSummaryOptions &options); // libstdc++ std::string
2622

2723
bool LibStdcppSmartPointerSummaryProvider(
2824
ValueObject &valobj, Stream &stream,

0 commit comments

Comments
 (0)