1*0b57cec5SDimitry Andric //===-- FormattersHelpers.cpp ---------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric
12*0b57cec5SDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
15*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
16*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/RegularExpression.h"
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric using namespace lldb;
21*0b57cec5SDimitry Andric using namespace lldb_private;
22*0b57cec5SDimitry Andric using namespace lldb_private::formatters;
23*0b57cec5SDimitry Andric
AddFormat(TypeCategoryImpl::SharedPointer category_sp,lldb::Format format,ConstString type_name,TypeFormatImpl::Flags flags,bool regex)24*0b57cec5SDimitry Andric void lldb_private::formatters::AddFormat(
25*0b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,
26*0b57cec5SDimitry Andric ConstString type_name, TypeFormatImpl::Flags flags, bool regex) {
27*0b57cec5SDimitry Andric lldb::TypeFormatImplSP format_sp(new TypeFormatImpl_Format(format, flags));
28*0b57cec5SDimitry Andric
29*0b57cec5SDimitry Andric if (regex)
30*0b57cec5SDimitry Andric category_sp->GetRegexTypeFormatsContainer()->Add(
31*0b57cec5SDimitry Andric RegularExpression(type_name.GetStringRef()), format_sp);
32*0b57cec5SDimitry Andric else
33*0b57cec5SDimitry Andric category_sp->GetTypeFormatsContainer()->Add(std::move(type_name),
34*0b57cec5SDimitry Andric format_sp);
35*0b57cec5SDimitry Andric }
36*0b57cec5SDimitry Andric
AddSummary(TypeCategoryImpl::SharedPointer category_sp,TypeSummaryImplSP summary_sp,ConstString type_name,bool regex)37*0b57cec5SDimitry Andric void lldb_private::formatters::AddSummary(
38*0b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, TypeSummaryImplSP summary_sp,
39*0b57cec5SDimitry Andric ConstString type_name, bool regex) {
40*0b57cec5SDimitry Andric if (regex)
41*0b57cec5SDimitry Andric category_sp->GetRegexTypeSummariesContainer()->Add(
42*0b57cec5SDimitry Andric RegularExpression(type_name.GetStringRef()), summary_sp);
43*0b57cec5SDimitry Andric else
44*0b57cec5SDimitry Andric category_sp->GetTypeSummariesContainer()->Add(std::move(type_name),
45*0b57cec5SDimitry Andric summary_sp);
46*0b57cec5SDimitry Andric }
47*0b57cec5SDimitry Andric
AddStringSummary(TypeCategoryImpl::SharedPointer category_sp,const char * string,ConstString type_name,TypeSummaryImpl::Flags flags,bool regex)48*0b57cec5SDimitry Andric void lldb_private::formatters::AddStringSummary(
49*0b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, const char *string,
50*0b57cec5SDimitry Andric ConstString type_name, TypeSummaryImpl::Flags flags, bool regex) {
51*0b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, string));
52*0b57cec5SDimitry Andric
53*0b57cec5SDimitry Andric if (regex)
54*0b57cec5SDimitry Andric category_sp->GetRegexTypeSummariesContainer()->Add(
55*0b57cec5SDimitry Andric RegularExpression(type_name.GetStringRef()), summary_sp);
56*0b57cec5SDimitry Andric else
57*0b57cec5SDimitry Andric category_sp->GetTypeSummariesContainer()->Add(std::move(type_name),
58*0b57cec5SDimitry Andric summary_sp);
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric
AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp,ConstString type_name,TypeSummaryImpl::Flags flags,bool regex)61*0b57cec5SDimitry Andric void lldb_private::formatters::AddOneLineSummary(
62*0b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp, ConstString type_name,
63*0b57cec5SDimitry Andric TypeSummaryImpl::Flags flags, bool regex) {
64*0b57cec5SDimitry Andric flags.SetShowMembersOneLiner(true);
65*0b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_sp(new StringSummaryFormat(flags, ""));
66*0b57cec5SDimitry Andric
67*0b57cec5SDimitry Andric if (regex)
68*0b57cec5SDimitry Andric category_sp->GetRegexTypeSummariesContainer()->Add(
69*0b57cec5SDimitry Andric RegularExpression(type_name.GetStringRef()), summary_sp);
70*0b57cec5SDimitry Andric else
71*0b57cec5SDimitry Andric category_sp->GetTypeSummariesContainer()->Add(std::move(type_name),
72*0b57cec5SDimitry Andric summary_sp);
73*0b57cec5SDimitry Andric }
74*0b57cec5SDimitry Andric
AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp,CXXFunctionSummaryFormat::Callback funct,const char * description,ConstString type_name,TypeSummaryImpl::Flags flags,bool regex)75*0b57cec5SDimitry Andric void lldb_private::formatters::AddCXXSummary(
76*0b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp,
77*0b57cec5SDimitry Andric CXXFunctionSummaryFormat::Callback funct, const char *description,
78*0b57cec5SDimitry Andric ConstString type_name, TypeSummaryImpl::Flags flags, bool regex) {
79*0b57cec5SDimitry Andric lldb::TypeSummaryImplSP summary_sp(
80*0b57cec5SDimitry Andric new CXXFunctionSummaryFormat(flags, funct, description));
81*0b57cec5SDimitry Andric if (regex)
82*0b57cec5SDimitry Andric category_sp->GetRegexTypeSummariesContainer()->Add(
83*0b57cec5SDimitry Andric RegularExpression(type_name.GetStringRef()), summary_sp);
84*0b57cec5SDimitry Andric else
85*0b57cec5SDimitry Andric category_sp->GetTypeSummariesContainer()->Add(std::move(type_name),
86*0b57cec5SDimitry Andric summary_sp);
87*0b57cec5SDimitry Andric }
88*0b57cec5SDimitry Andric
AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp,CXXSyntheticChildren::CreateFrontEndCallback generator,const char * description,ConstString type_name,ScriptedSyntheticChildren::Flags flags,bool regex)89*0b57cec5SDimitry Andric void lldb_private::formatters::AddCXXSynthetic(
90*0b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp,
91*0b57cec5SDimitry Andric CXXSyntheticChildren::CreateFrontEndCallback generator,
92*0b57cec5SDimitry Andric const char *description, ConstString type_name,
93*0b57cec5SDimitry Andric ScriptedSyntheticChildren::Flags flags, bool regex) {
94*0b57cec5SDimitry Andric lldb::SyntheticChildrenSP synth_sp(
95*0b57cec5SDimitry Andric new CXXSyntheticChildren(flags, description, generator));
96*0b57cec5SDimitry Andric if (regex)
97*0b57cec5SDimitry Andric category_sp->GetRegexTypeSyntheticsContainer()->Add(
98*0b57cec5SDimitry Andric RegularExpression(type_name.GetStringRef()), synth_sp);
99*0b57cec5SDimitry Andric else
100*0b57cec5SDimitry Andric category_sp->GetTypeSyntheticsContainer()->Add(std::move(type_name),
101*0b57cec5SDimitry Andric synth_sp);
102*0b57cec5SDimitry Andric }
103*0b57cec5SDimitry Andric
AddFilter(TypeCategoryImpl::SharedPointer category_sp,std::vector<std::string> children,const char * description,ConstString type_name,ScriptedSyntheticChildren::Flags flags,bool regex)104*0b57cec5SDimitry Andric void lldb_private::formatters::AddFilter(
105*0b57cec5SDimitry Andric TypeCategoryImpl::SharedPointer category_sp,
106*0b57cec5SDimitry Andric std::vector<std::string> children, const char *description,
107*0b57cec5SDimitry Andric ConstString type_name, ScriptedSyntheticChildren::Flags flags, bool regex) {
108*0b57cec5SDimitry Andric TypeFilterImplSP filter_sp(new TypeFilterImpl(flags));
109*0b57cec5SDimitry Andric for (auto child : children)
110*0b57cec5SDimitry Andric filter_sp->AddExpressionPath(child);
111*0b57cec5SDimitry Andric if (regex)
112*0b57cec5SDimitry Andric category_sp->GetRegexTypeFiltersContainer()->Add(
113*0b57cec5SDimitry Andric RegularExpression(type_name.GetStringRef()), filter_sp);
114*0b57cec5SDimitry Andric else
115*0b57cec5SDimitry Andric category_sp->GetTypeFiltersContainer()->Add(std::move(type_name),
116*0b57cec5SDimitry Andric filter_sp);
117*0b57cec5SDimitry Andric }
118*0b57cec5SDimitry Andric
ExtractIndexFromString(const char * item_name)119*0b57cec5SDimitry Andric size_t lldb_private::formatters::ExtractIndexFromString(const char *item_name) {
120*0b57cec5SDimitry Andric if (!item_name || !*item_name)
121*0b57cec5SDimitry Andric return UINT32_MAX;
122*0b57cec5SDimitry Andric if (*item_name != '[')
123*0b57cec5SDimitry Andric return UINT32_MAX;
124*0b57cec5SDimitry Andric item_name++;
125*0b57cec5SDimitry Andric char *endptr = nullptr;
126*0b57cec5SDimitry Andric unsigned long int idx = ::strtoul(item_name, &endptr, 0);
127*0b57cec5SDimitry Andric if (idx == 0 && endptr == item_name)
128*0b57cec5SDimitry Andric return UINT32_MAX;
129*0b57cec5SDimitry Andric if (idx == ULONG_MAX)
130*0b57cec5SDimitry Andric return UINT32_MAX;
131*0b57cec5SDimitry Andric return idx;
132*0b57cec5SDimitry Andric }
133*0b57cec5SDimitry Andric
134*0b57cec5SDimitry Andric lldb::addr_t
GetArrayAddressOrPointerValue(ValueObject & valobj)135*0b57cec5SDimitry Andric lldb_private::formatters::GetArrayAddressOrPointerValue(ValueObject &valobj) {
136*0b57cec5SDimitry Andric lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
137*0b57cec5SDimitry Andric
138*0b57cec5SDimitry Andric if (valobj.IsPointerType())
139*0b57cec5SDimitry Andric data_addr = valobj.GetValueAsUnsigned(0);
140*0b57cec5SDimitry Andric else if (valobj.IsArrayType())
141*0b57cec5SDimitry Andric data_addr = valobj.GetAddressOf();
142*0b57cec5SDimitry Andric
143*0b57cec5SDimitry Andric return data_addr;
144*0b57cec5SDimitry Andric }
145*0b57cec5SDimitry Andric
146 lldb::ValueObjectSP
GetValueOfLibCXXCompressedPair(ValueObject & pair)147 lldb_private::formatters::GetValueOfLibCXXCompressedPair(ValueObject &pair) {
148 ValueObjectSP value =
149 pair.GetChildMemberWithName(ConstString("__value_"), true);
150 if (!value) {
151 // pre-r300140 member name
152 value = pair.GetChildMemberWithName(ConstString("__first_"), true);
153 }
154 return value;
155 }
156