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