LLDB mainline
CxxStringTypes.cpp
Go to the documentation of this file.
1//===-- CxxStringTypes.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#include "CxxStringTypes.h"
10
11#include "llvm/Support/ConvertUTF.h"
12
17#include "lldb/Host/Time.h"
19#include "lldb/Target/Target.h"
20#include "lldb/Target/Thread.h"
22#include "lldb/Utility/Endian.h"
23#include "lldb/Utility/Status.h"
24#include "lldb/Utility/Stream.h"
27
28#include <algorithm>
29#include <optional>
30
31using namespace lldb;
32using namespace lldb_private;
33using namespace lldb_private::formatters;
34
36
37static constexpr std::pair<const char *, Format>
39 switch (ElemType) {
40 case StringElementType::UTF8:
41 return std::make_pair("u8", lldb::eFormatUnicode8);
42 case StringElementType::UTF16:
43 return std::make_pair("u", lldb::eFormatUnicode16);
44 case StringElementType::UTF32:
45 return std::make_pair("U", lldb::eFormatUnicode32);
46 default:
47 return std::make_pair(nullptr, lldb::eFormatInvalid);
48 }
49}
50
51template <StringElementType ElemType>
52static bool CharStringSummaryProvider(ValueObject &valobj, Stream &stream) {
53 Address valobj_addr = GetArrayAddressOrPointerValue(valobj);
54 if (!valobj_addr.IsValid())
55 return false;
56
58 options.SetLocation(valobj_addr);
59 options.SetTargetSP(valobj.GetTargetSP());
60 options.SetStream(&stream);
61 options.SetPrefixToken(getElementTraits(ElemType).first);
62
64 stream.Printf("Summary Unavailable");
65
66 return true;
67}
68
69template <StringElementType ElemType>
70static bool CharSummaryProvider(ValueObject &valobj, Stream &stream) {
71 DataExtractor data;
73 valobj.GetData(data, error);
74
75 if (error.Fail())
76 return false;
77
78 std::string value;
80
81 constexpr auto ElemTraits = getElementTraits(ElemType);
82 valobj.GetValueAsCString(ElemTraits.second, value);
83
84 if (!value.empty())
85 stream.Printf("%s ", value.c_str());
86
87 options.SetData(std::move(data));
88 options.SetStream(&stream);
89 options.SetPrefixToken(ElemTraits.first);
90 options.SetQuote('\'');
91 options.SetSourceSize(1);
92 options.SetBinaryZeroIsTerminator(false);
93
95}
96
101
106
111
113 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
114 Address valobj_addr = GetArrayAddressOrPointerValue(valobj);
115 if (!valobj_addr.IsValid())
116 return false;
117
118 // Get a wchar_t basic type from the current type system
119 std::optional<uint64_t> size = GetWCharByteSize(valobj);
120 if (!size)
121 return false;
122 const uint32_t wchar_size = *size;
123
125 options.SetLocation(valobj_addr);
126 options.SetTargetSP(valobj.GetTargetSP());
127 options.SetStream(&stream);
128 options.SetPrefixToken("L");
129
130 switch (wchar_size) {
131 case 1:
133 options);
134 case 2:
136 options);
137 case 4:
139 options);
140 default:
141 stream.Printf("size for wchar_t is not valid");
142 return true;
143 }
144 return true;
145}
146
151
156
161
163 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
164 DataExtractor data;
166 valobj.GetData(data, error);
167
168 if (error.Fail())
169 return false;
170
171 // Get a wchar_t basic type from the current type system
172 std::optional<uint64_t> size = GetWCharByteSize(valobj);
173 if (!size)
174 return false;
175 const uint32_t wchar_size = *size;
176
178 options.SetData(std::move(data));
179 options.SetStream(&stream);
180 options.SetPrefixToken("L");
181 options.SetQuote('\'');
182 options.SetSourceSize(1);
183 options.SetBinaryZeroIsTerminator(false);
184
185 switch (wchar_size) {
186 case 1:
188 options);
189 case 2:
191 options);
192 case 4:
194 options);
195 default:
196 stream.Printf("size for wchar_t is not valid");
197 return true;
198 }
199 return true;
200}
201
202std::optional<uint64_t>
204 return llvm::expectedToOptional(
205 valobj.GetCompilerType()
207 .GetByteSize(nullptr));
208}
209
210template <StringPrinter::StringElementType element_type>
212 Stream &stream, const TypeSummaryOptions &summary_options,
213 lldb::ValueObjectSP location_sp, uint64_t size, std::string prefix_token) {
214
215 if (size == 0) {
216 stream.PutCString(prefix_token);
217 stream.PutCString("\"\"");
218 return true;
219 }
220
221 if (!location_sp)
222 return false;
223
225
226 if (summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryCapped) {
227 const auto max_size =
228 location_sp->GetTargetSP()->GetMaximumSizeOfStringSummary();
229 if (size > max_size) {
230 size = max_size;
231 options.SetIsTruncated(true);
232 }
233 }
234
235 {
236 DataExtractor extractor;
237 const size_t bytes_read = location_sp->GetPointeeData(extractor, 0, size);
238 if (bytes_read < size)
239 return false;
240
241 options.SetData(std::move(extractor));
242 }
243 options.SetStream(&stream);
244 if (prefix_token.empty())
245 options.SetPrefixToken(nullptr);
246 else
247 options.SetPrefixToken(prefix_token);
248 options.SetQuote('"');
249 options.SetSourceSize(size);
250 options.SetBinaryZeroIsTerminator(false);
252}
253
254// explicit instantiations for all string element types
255template bool
257 Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t,
258 std::string);
259template bool
261 Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t,
262 std::string);
263template bool
265 Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t,
266 std::string);
267template bool
269 Stream &, const TypeSummaryOptions &, lldb::ValueObjectSP, uint64_t,
270 std::string);
static llvm::raw_ostream & error(Stream &strm)
static constexpr std::pair< const char *, Format > getElementTraits(StringElementType ElemType)
static bool CharSummaryProvider(ValueObject &valobj, Stream &stream)
static bool CharStringSummaryProvider(ValueObject &valobj, Stream &stream)
StringPrinter::StringElementType StringElementType
A section + offset based address class.
Definition Address.h:62
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
llvm::Expected< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
An data extractor class.
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
lldb::TypeSummaryCapping GetCapping() const
virtual uint64_t GetData(DataExtractor &data, Status &error)
CompilerType GetCompilerType()
lldb::TargetSP GetTargetSP() const
virtual const char * GetValueAsCString()
static bool ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions &options)
static bool ReadStringAndDumpToStream(const ReadStringAndDumpToStreamOptions &options)
bool Char32StringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
bool WCharSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
std::optional< uint64_t > GetWCharByteSize(ValueObject &valobj)
bool StringBufferSummaryProvider(Stream &stream, const TypeSummaryOptions &summary_options, lldb::ValueObjectSP location_sp, uint64_t size, std::string prefix_token)
Print a summary for a string buffer to stream.
bool Char8SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
bool Char16SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
bool Char8StringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
bool Char16StringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Address GetArrayAddressOrPointerValue(ValueObject &valobj)
bool WCharStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
bool Char32SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP