1 //===-- NSString.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12 #include "lldb/Core/ValueObject.h"
13 #include "lldb/Core/ValueObjectConstResult.h"
14 #include "lldb/DataFormatters/FormattersHelpers.h"
15 #include "lldb/DataFormatters/StringPrinter.h"
16 #include "lldb/Target/Language.h"
17 #include "lldb/Target/ProcessStructReader.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Utility/DataBufferHeap.h"
20 #include "lldb/Utility/Endian.h"
21 #include "lldb/Utility/Status.h"
22 #include "lldb/Utility/Stream.h"
25 using namespace lldb_private;
26 using namespace lldb_private::formatters;
28 std::map<ConstString, CXXFunctionSummaryFormat::Callback> &
29 NSString_Additionals::GetAdditionalSummaries() {
30 static std::map<ConstString, CXXFunctionSummaryFormat::Callback> g_map;
34 static CompilerType GetNSPathStore2Type(Target &target) {
35 static ConstString g_type_name("__lldb_autogen_nspathstore2");
37 TypeSystemClang *ast_ctx = TypeSystemClang::GetScratch(target);
40 return CompilerType();
42 CompilerType voidstar =
43 ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
45 ast_ctx->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
47 return ast_ctx->GetOrCreateStructForIdentifier(
49 {{"isa", voidstar}, {"lengthAndRef", uint32}, {"buffer", voidstar}});
52 bool lldb_private::formatters::NSStringSummaryProvider(
53 ValueObject &valobj, Stream &stream,
54 const TypeSummaryOptions &summary_options) {
55 static ConstString g_TypeHint("NSString");
57 ProcessSP process_sp = valobj.GetProcessSP();
61 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
66 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
67 runtime->GetClassDescriptor(valobj));
69 if (!descriptor.get() || !descriptor->IsValid())
72 uint32_t ptr_size = process_sp->GetAddressByteSize();
74 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
79 ConstString class_name_cs = descriptor->GetClassName();
80 llvm::StringRef class_name = class_name_cs.GetStringRef();
82 if (class_name.empty())
85 bool is_tagged_ptr = class_name == "NSTaggedPointerString" &&
86 descriptor->GetTaggedPointerInfo();
87 // for a tagged pointer, the descriptor has everything we need
89 return NSTaggedString_SummaryProvider(valobj, descriptor, stream,
92 auto &additionals_map(NSString_Additionals::GetAdditionalSummaries());
93 auto iter = additionals_map.find(class_name_cs), end = additionals_map.end();
95 return iter->second(valobj, stream, summary_options);
97 // if not a tagged pointer that we know about, try the normal route
98 uint64_t info_bits_location = valobj_addr + ptr_size;
99 if (process_sp->GetByteOrder() != lldb::eByteOrderLittle)
100 info_bits_location += 3;
104 uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(
105 info_bits_location, 1, 0, error);
109 bool is_mutable = (info_bits & 1) == 1;
110 bool is_inline = (info_bits & 0x60) == 0;
111 bool has_explicit_length = (info_bits & (1 | 4)) != 4;
112 bool is_unicode = (info_bits & 0x10) == 0x10;
113 bool is_path_store = class_name == "NSPathStore2";
114 bool has_null = (info_bits & 8) == 8;
116 size_t explicit_length = 0;
117 if (!has_null && has_explicit_length && !is_path_store) {
118 lldb::addr_t explicit_length_offset = 2 * ptr_size;
119 if (is_mutable && !is_inline)
120 explicit_length_offset =
121 explicit_length_offset + ptr_size; // notInlineMutable.length;
123 explicit_length = explicit_length + 0; // inline1.length;
124 else if (!is_inline && !is_mutable)
125 explicit_length_offset =
126 explicit_length_offset + ptr_size; // notInlineImmutable1.length;
128 explicit_length_offset = 0;
130 if (explicit_length_offset) {
131 explicit_length_offset = valobj_addr + explicit_length_offset;
132 explicit_length = process_sp->ReadUnsignedIntegerFromMemory(
133 explicit_length_offset, 4, 0, error);
137 const llvm::StringSet<> supported_string_classes = {
138 "NSString", "CFMutableStringRef",
139 "CFStringRef", "__NSCFConstantString",
140 "__NSCFString", "NSCFConstantString",
141 "NSCFString", "NSPathStore2"};
142 if (supported_string_classes.count(class_name) == 0) {
143 // not one of us - but tell me class name
144 stream.Printf("class name = %s", class_name_cs.GetCString());
148 std::string prefix, suffix;
149 if (Language *language =
150 Language::FindPlugin(summary_options.GetLanguage())) {
151 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
158 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
159 options.SetPrefixToken(prefix);
160 options.SetSuffixToken(suffix);
163 uint64_t location = 2 * ptr_size + valobj_addr;
164 location = process_sp->ReadPointerFromMemory(location, error);
167 if (has_explicit_length && is_unicode) {
168 options.SetLocation(location);
169 options.SetProcessSP(process_sp);
170 options.SetStream(&stream);
171 options.SetQuote('"');
172 options.SetSourceSize(explicit_length);
173 options.SetHasSourceSize(has_explicit_length);
174 options.SetNeedsZeroTermination(false);
175 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
176 TypeSummaryCapping::eTypeSummaryUncapped);
177 options.SetBinaryZeroIsTerminator(false);
178 return StringPrinter::ReadStringAndDumpToStream<
179 StringPrinter::StringElementType::UTF16>(options);
181 options.SetLocation(location + 1);
182 options.SetProcessSP(process_sp);
183 options.SetStream(&stream);
184 options.SetSourceSize(explicit_length);
185 options.SetHasSourceSize(has_explicit_length);
186 options.SetNeedsZeroTermination(false);
187 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
188 TypeSummaryCapping::eTypeSummaryUncapped);
189 options.SetBinaryZeroIsTerminator(false);
190 return StringPrinter::ReadStringAndDumpToStream<
191 StringPrinter::StringElementType::ASCII>(options);
193 } else if (is_inline && has_explicit_length && !is_unicode &&
194 !is_path_store && !is_mutable) {
195 uint64_t location = 3 * ptr_size + valobj_addr;
197 options.SetLocation(location);
198 options.SetProcessSP(process_sp);
199 options.SetStream(&stream);
200 options.SetQuote('"');
201 options.SetSourceSize(explicit_length);
202 options.SetHasSourceSize(has_explicit_length);
203 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
204 TypeSummaryCapping::eTypeSummaryUncapped);
205 return StringPrinter::ReadStringAndDumpToStream<
206 StringPrinter::StringElementType::ASCII>(options);
207 } else if (is_unicode) {
208 uint64_t location = valobj_addr + 2 * ptr_size;
210 if (!has_explicit_length) {
213 location += ptr_size;
215 location = process_sp->ReadPointerFromMemory(location, error);
219 options.SetLocation(location);
220 options.SetProcessSP(process_sp);
221 options.SetStream(&stream);
222 options.SetQuote('"');
223 options.SetSourceSize(explicit_length);
224 options.SetHasSourceSize(has_explicit_length);
225 options.SetNeedsZeroTermination(!has_explicit_length);
226 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
227 TypeSummaryCapping::eTypeSummaryUncapped);
228 options.SetBinaryZeroIsTerminator(!has_explicit_length);
229 return StringPrinter::ReadStringAndDumpToStream<
230 StringPrinter::StringElementType::UTF16>(options);
231 } else if (is_path_store) {
232 ProcessStructReader reader(valobj.GetProcessSP().get(),
233 valobj.GetValueAsUnsigned(0),
234 GetNSPathStore2Type(*valobj.GetTargetSP()));
236 reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20;
237 lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
239 options.SetLocation(location);
240 options.SetProcessSP(process_sp);
241 options.SetStream(&stream);
242 options.SetQuote('"');
243 options.SetSourceSize(explicit_length);
244 options.SetHasSourceSize(has_explicit_length);
245 options.SetNeedsZeroTermination(!has_explicit_length);
246 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
247 TypeSummaryCapping::eTypeSummaryUncapped);
248 options.SetBinaryZeroIsTerminator(!has_explicit_length);
249 return StringPrinter::ReadStringAndDumpToStream<
250 StringPrinter::StringElementType::UTF16>(options);
251 } else if (is_inline) {
252 uint64_t location = valobj_addr + 2 * ptr_size;
253 if (!has_explicit_length) {
254 // in this kind of string, the byte before the string content is a length
255 // byte so let's try and use it to handle the embedded NUL case
258 process_sp->ReadUnsignedIntegerFromMemory(location, 1, 0, error);
259 has_explicit_length = !(error.Fail() || explicit_length == 0);
262 options.SetLocation(location);
263 options.SetProcessSP(process_sp);
264 options.SetStream(&stream);
265 options.SetSourceSize(explicit_length);
266 options.SetHasSourceSize(has_explicit_length);
267 options.SetNeedsZeroTermination(!has_explicit_length);
268 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
269 TypeSummaryCapping::eTypeSummaryUncapped);
270 options.SetBinaryZeroIsTerminator(!has_explicit_length);
271 if (has_explicit_length)
272 return StringPrinter::ReadStringAndDumpToStream<
273 StringPrinter::StringElementType::UTF8>(options);
275 return StringPrinter::ReadStringAndDumpToStream<
276 StringPrinter::StringElementType::ASCII>(options);
278 uint64_t location = valobj_addr + 2 * ptr_size;
279 location = process_sp->ReadPointerFromMemory(location, error);
282 if (has_explicit_length && !has_null)
283 explicit_length++; // account for the fact that there is no NULL and we
284 // need to have one added
285 options.SetLocation(location);
286 options.SetProcessSP(process_sp);
287 options.SetStream(&stream);
288 options.SetSourceSize(explicit_length);
289 options.SetHasSourceSize(has_explicit_length);
290 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
291 TypeSummaryCapping::eTypeSummaryUncapped);
292 return StringPrinter::ReadStringAndDumpToStream<
293 StringPrinter::StringElementType::ASCII>(options);
297 bool lldb_private::formatters::NSAttributedStringSummaryProvider(
298 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
299 TargetSP target_sp(valobj.GetTargetSP());
302 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
303 uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
306 pointer_value += addr_size;
307 CompilerType type(valobj.GetCompilerType());
308 ExecutionContext exe_ctx(target_sp, false);
309 ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress(
310 "string_ptr", pointer_value, exe_ctx, type));
315 child_ptr_sp->GetData(data, error);
318 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData(
319 "string_data", data, exe_ctx, type));
320 child_sp->GetValueAsUnsigned(0);
322 return NSStringSummaryProvider(*child_sp, stream, options);
326 bool lldb_private::formatters::NSMutableAttributedStringSummaryProvider(
327 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
328 return NSAttributedStringSummaryProvider(valobj, stream, options);
331 bool lldb_private::formatters::NSTaggedString_SummaryProvider(
332 ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
333 Stream &stream, const TypeSummaryOptions &summary_options) {
334 static ConstString g_TypeHint("NSString");
338 uint64_t len_bits = 0, data_bits = 0;
339 if (!descriptor->GetTaggedPointerInfo(&len_bits, &data_bits, nullptr))
342 static const int g_MaxNonBitmaskedLen = 7; // TAGGED_STRING_UNPACKED_MAXLEN
343 static const int g_SixbitMaxLen = 9;
344 static const int g_fiveBitMaxLen = 11;
346 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013"
347 "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
349 if (len_bits > g_fiveBitMaxLen)
352 std::string prefix, suffix;
353 if (Language *language =
354 Language::FindPlugin(summary_options.GetLanguage())) {
355 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
362 // this is a fairly ugly trick - pretend that the numeric value is actually a
363 // char* this works under a few assumptions: little endian architecture
364 // sizeof(uint64_t) > g_MaxNonBitmaskedLen
365 if (len_bits <= g_MaxNonBitmaskedLen) {
366 stream.Printf("%s", prefix.c_str());
367 stream.Printf("\"%s\"", (const char *)&data_bits);
368 stream.Printf("%s", suffix.c_str());
372 // if the data is bitmasked, we need to actually process the bytes
374 uint8_t shift_offset = 0;
376 if (len_bits <= g_SixbitMaxLen) {
384 std::vector<uint8_t> bytes;
385 bytes.resize(len_bits);
386 for (; len_bits > 0; data_bits >>= shift_offset, --len_bits) {
387 uint8_t packed = data_bits & bitmask;
388 bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
391 stream.Printf("%s", prefix.c_str());
392 stream.Printf("\"%s\"", &bytes[0]);
393 stream.Printf("%s", suffix.c_str());