1 //===-- NSString.cpp ----------------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
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/Symbol/ClangASTContext.h"
17 #include "lldb/Target/Language.h"
18 #include "lldb/Target/ProcessStructReader.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/DataBufferHeap.h"
21 #include "lldb/Utility/Endian.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/Utility/Stream.h"
26 using namespace lldb_private;
27 using namespace lldb_private::formatters;
29 std::map<ConstString, CXXFunctionSummaryFormat::Callback> &
30 NSString_Additionals::GetAdditionalSummaries() {
31 static std::map<ConstString, CXXFunctionSummaryFormat::Callback> g_map;
35 static CompilerType GetNSPathStore2Type(Target &target) {
36 static ConstString g_type_name("__lldb_autogen_nspathstore2");
38 ClangASTContext *ast_ctx = target.GetScratchClangASTContext();
41 return CompilerType();
43 CompilerType voidstar =
44 ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
46 ast_ctx->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
48 return ast_ctx->GetOrCreateStructForIdentifier(
50 {{"isa", voidstar}, {"lengthAndRef", uint32}, {"buffer", voidstar}});
53 bool lldb_private::formatters::NSStringSummaryProvider(
54 ValueObject &valobj, Stream &stream,
55 const TypeSummaryOptions &summary_options) {
56 static ConstString g_TypeHint("NSString");
58 ProcessSP process_sp = valobj.GetProcessSP();
62 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
67 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
68 runtime->GetClassDescriptor(valobj));
70 if (!descriptor.get() || !descriptor->IsValid())
73 uint32_t ptr_size = process_sp->GetAddressByteSize();
75 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
80 ConstString class_name_cs = descriptor->GetClassName();
81 const char *class_name = class_name_cs.GetCString();
83 if (!class_name || !*class_name)
86 bool is_tagged_ptr = (0 == strcmp(class_name, "NSTaggedPointerString")) &&
87 descriptor->GetTaggedPointerInfo();
88 // for a tagged pointer, the descriptor has everything we need
90 return NSTaggedString_SummaryProvider(valobj, descriptor, stream,
93 auto &additionals_map(NSString_Additionals::GetAdditionalSummaries());
94 auto iter = additionals_map.find(class_name_cs), end = additionals_map.end();
96 return iter->second(valobj, stream, summary_options);
98 // if not a tagged pointer that we know about, try the normal route
99 uint64_t info_bits_location = valobj_addr + ptr_size;
100 if (process_sp->GetByteOrder() != lldb::eByteOrderLittle)
101 info_bits_location += 3;
105 uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(
106 info_bits_location, 1, 0, error);
110 bool is_mutable = (info_bits & 1) == 1;
111 bool is_inline = (info_bits & 0x60) == 0;
112 bool has_explicit_length = (info_bits & (1 | 4)) != 4;
113 bool is_unicode = (info_bits & 0x10) == 0x10;
114 bool is_path_store = strcmp(class_name, "NSPathStore2") == 0;
115 bool has_null = (info_bits & 8) == 8;
117 size_t explicit_length = 0;
118 if (!has_null && has_explicit_length && !is_path_store) {
119 lldb::addr_t explicit_length_offset = 2 * ptr_size;
120 if (is_mutable && !is_inline)
121 explicit_length_offset =
122 explicit_length_offset + ptr_size; // notInlineMutable.length;
124 explicit_length = explicit_length + 0; // inline1.length;
125 else if (!is_inline && !is_mutable)
126 explicit_length_offset =
127 explicit_length_offset + ptr_size; // notInlineImmutable1.length;
129 explicit_length_offset = 0;
131 if (explicit_length_offset) {
132 explicit_length_offset = valobj_addr + explicit_length_offset;
133 explicit_length = process_sp->ReadUnsignedIntegerFromMemory(
134 explicit_length_offset, 4, 0, error);
138 if (strcmp(class_name, "NSString") && strcmp(class_name, "CFStringRef") &&
139 strcmp(class_name, "CFMutableStringRef") &&
140 strcmp(class_name, "__NSCFConstantString") &&
141 strcmp(class_name, "__NSCFString") &&
142 strcmp(class_name, "NSCFConstantString") &&
143 strcmp(class_name, "NSCFString") && strcmp(class_name, "NSPathStore2")) {
144 // not one of us - but tell me class name
145 stream.Printf("class name = %s", class_name);
149 std::string prefix, suffix;
150 if (Language *language =
151 Language::FindPlugin(summary_options.GetLanguage())) {
152 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
159 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
160 options.SetPrefixToken(prefix);
161 options.SetSuffixToken(suffix);
164 uint64_t location = 2 * ptr_size + valobj_addr;
165 location = process_sp->ReadPointerFromMemory(location, error);
168 if (has_explicit_length && is_unicode) {
169 options.SetLocation(location);
170 options.SetProcessSP(process_sp);
171 options.SetStream(&stream);
172 options.SetQuote('"');
173 options.SetSourceSize(explicit_length);
174 options.SetNeedsZeroTermination(false);
175 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
176 TypeSummaryCapping::eTypeSummaryUncapped);
177 options.SetBinaryZeroIsTerminator(false);
178 options.SetLanguage(summary_options.GetLanguage());
179 return StringPrinter::ReadStringAndDumpToStream<
180 StringPrinter::StringElementType::UTF16>(options);
182 options.SetLocation(location + 1);
183 options.SetProcessSP(process_sp);
184 options.SetStream(&stream);
185 options.SetSourceSize(explicit_length);
186 options.SetNeedsZeroTermination(false);
187 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
188 TypeSummaryCapping::eTypeSummaryUncapped);
189 options.SetBinaryZeroIsTerminator(false);
190 options.SetLanguage(summary_options.GetLanguage());
191 return StringPrinter::ReadStringAndDumpToStream<
192 StringPrinter::StringElementType::ASCII>(options);
194 } else if (is_inline && has_explicit_length && !is_unicode &&
195 !is_path_store && !is_mutable) {
196 uint64_t location = 3 * ptr_size + valobj_addr;
198 options.SetLocation(location);
199 options.SetProcessSP(process_sp);
200 options.SetStream(&stream);
201 options.SetQuote('"');
202 options.SetSourceSize(explicit_length);
203 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
204 TypeSummaryCapping::eTypeSummaryUncapped);
205 options.SetLanguage(summary_options.GetLanguage());
206 return StringPrinter::ReadStringAndDumpToStream<
207 StringPrinter::StringElementType::ASCII>(options);
208 } else if (is_unicode) {
209 uint64_t location = valobj_addr + 2 * ptr_size;
211 if (!has_explicit_length) {
214 location += ptr_size;
216 location = process_sp->ReadPointerFromMemory(location, error);
220 options.SetLocation(location);
221 options.SetProcessSP(process_sp);
222 options.SetStream(&stream);
223 options.SetQuote('"');
224 options.SetSourceSize(explicit_length);
225 options.SetNeedsZeroTermination(!has_explicit_length);
226 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
227 TypeSummaryCapping::eTypeSummaryUncapped);
228 options.SetBinaryZeroIsTerminator(!has_explicit_length);
229 options.SetLanguage(summary_options.GetLanguage());
230 return StringPrinter::ReadStringAndDumpToStream<
231 StringPrinter::StringElementType::UTF16>(options);
232 } else if (is_path_store) {
233 ProcessStructReader reader(valobj.GetProcessSP().get(),
234 valobj.GetValueAsUnsigned(0),
235 GetNSPathStore2Type(*valobj.GetTargetSP()));
237 reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20;
238 lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
240 options.SetLocation(location);
241 options.SetProcessSP(process_sp);
242 options.SetStream(&stream);
243 options.SetQuote('"');
244 options.SetSourceSize(explicit_length);
245 options.SetNeedsZeroTermination(!has_explicit_length);
246 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
247 TypeSummaryCapping::eTypeSummaryUncapped);
248 options.SetBinaryZeroIsTerminator(!has_explicit_length);
249 options.SetLanguage(summary_options.GetLanguage());
250 return StringPrinter::ReadStringAndDumpToStream<
251 StringPrinter::StringElementType::UTF16>(options);
252 } else if (is_inline) {
253 uint64_t location = valobj_addr + 2 * ptr_size;
254 if (!has_explicit_length) {
255 // in this kind of string, the byte before the string content is a length
256 // byte so let's try and use it to handle the embedded NUL case
259 process_sp->ReadUnsignedIntegerFromMemory(location, 1, 0, error);
260 has_explicit_length = !(error.Fail() || explicit_length == 0);
263 options.SetLocation(location);
264 options.SetProcessSP(process_sp);
265 options.SetStream(&stream);
266 options.SetSourceSize(explicit_length);
267 options.SetNeedsZeroTermination(!has_explicit_length);
268 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
269 TypeSummaryCapping::eTypeSummaryUncapped);
270 options.SetBinaryZeroIsTerminator(!has_explicit_length);
271 options.SetLanguage(summary_options.GetLanguage());
272 if (has_explicit_length)
273 return StringPrinter::ReadStringAndDumpToStream<
274 StringPrinter::StringElementType::UTF8>(options);
276 return StringPrinter::ReadStringAndDumpToStream<
277 StringPrinter::StringElementType::ASCII>(options);
279 uint64_t location = valobj_addr + 2 * ptr_size;
280 location = process_sp->ReadPointerFromMemory(location, error);
283 if (has_explicit_length && !has_null)
284 explicit_length++; // account for the fact that there is no NULL and we
285 // need to have one added
286 options.SetLocation(location);
287 options.SetProcessSP(process_sp);
288 options.SetStream(&stream);
289 options.SetSourceSize(explicit_length);
290 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
291 TypeSummaryCapping::eTypeSummaryUncapped);
292 options.SetLanguage(summary_options.GetLanguage());
293 return StringPrinter::ReadStringAndDumpToStream<
294 StringPrinter::StringElementType::ASCII>(options);
298 bool lldb_private::formatters::NSAttributedStringSummaryProvider(
299 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
300 TargetSP target_sp(valobj.GetTargetSP());
303 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
304 uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
307 pointer_value += addr_size;
308 CompilerType type(valobj.GetCompilerType());
309 ExecutionContext exe_ctx(target_sp, false);
310 ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress(
311 "string_ptr", pointer_value, exe_ctx, type));
316 child_ptr_sp->GetData(data, error);
319 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData(
320 "string_data", data, exe_ctx, type));
321 child_sp->GetValueAsUnsigned(0);
323 return NSStringSummaryProvider(*child_sp, stream, options);
327 bool lldb_private::formatters::NSMutableAttributedStringSummaryProvider(
328 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
329 return NSAttributedStringSummaryProvider(valobj, stream, options);
332 bool lldb_private::formatters::NSTaggedString_SummaryProvider(
333 ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
334 Stream &stream, const TypeSummaryOptions &summary_options) {
335 static ConstString g_TypeHint("NSString");
339 uint64_t len_bits = 0, data_bits = 0;
340 if (!descriptor->GetTaggedPointerInfo(&len_bits, &data_bits, nullptr))
343 static const int g_MaxNonBitmaskedLen = 7; // TAGGED_STRING_UNPACKED_MAXLEN
344 static const int g_SixbitMaxLen = 9;
345 static const int g_fiveBitMaxLen = 11;
347 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013"
348 "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
350 if (len_bits > g_fiveBitMaxLen)
353 std::string prefix, suffix;
354 if (Language *language =
355 Language::FindPlugin(summary_options.GetLanguage())) {
356 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
363 // this is a fairly ugly trick - pretend that the numeric value is actually a
364 // char* this works under a few assumptions: little endian architecture
365 // sizeof(uint64_t) > g_MaxNonBitmaskedLen
366 if (len_bits <= g_MaxNonBitmaskedLen) {
367 stream.Printf("%s", prefix.c_str());
368 stream.Printf("\"%s\"", (const char *)&data_bits);
369 stream.Printf("%s", suffix.c_str());
373 // if the data is bitmasked, we need to actually process the bytes
375 uint8_t shift_offset = 0;
377 if (len_bits <= g_SixbitMaxLen) {
385 std::vector<uint8_t> bytes;
386 bytes.resize(len_bits);
387 for (; len_bits > 0; data_bits >>= shift_offset, --len_bits) {
388 uint8_t packed = data_bits & bitmask;
389 bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
392 stream.Printf("%s", prefix.c_str());
393 stream.Printf("\"%s\"", &bytes[0]);
394 stream.Printf("%s", suffix.c_str());