blob: bfbc8cd847980a74dd9319f13ebe1eb1be7117df [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- NSString.cpp ----------------------------------------------*- C++
2//-*-===//
Enrico Granata7de855c2015-10-02 20:59:58 +00003//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// 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
Enrico Granata7de855c2015-10-02 20:59:58 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "NSString.h"
11
Enrico Granata7de855c2015-10-02 20:59:58 +000012#include "lldb/Core/ValueObject.h"
13#include "lldb/Core/ValueObjectConstResult.h"
14#include "lldb/DataFormatters/FormattersHelpers.h"
15#include "lldb/DataFormatters/StringPrinter.h"
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +010016#include "lldb/Symbol/TypeSystemClang.h"
Enrico Granata675f49b2015-10-07 18:36:53 +000017#include "lldb/Target/Language.h"
Zachary Turner01c32432017-02-14 19:06:07 +000018#include "lldb/Target/ProcessStructReader.h"
Enrico Granata7de855c2015-10-02 20:59:58 +000019#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000020#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner01c32432017-02-14 19:06:07 +000021#include "lldb/Utility/Endian.h"
Zachary Turner97206d52017-05-12 04:51:55 +000022#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000023#include "lldb/Utility/Stream.h"
Enrico Granata7de855c2015-10-02 20:59:58 +000024
25using namespace lldb;
26using namespace lldb_private;
27using namespace lldb_private::formatters;
28
Kate Stoneb9c1b512016-09-06 20:57:50 +000029std::map<ConstString, CXXFunctionSummaryFormat::Callback> &
30NSString_Additionals::GetAdditionalSummaries() {
31 static std::map<ConstString, CXXFunctionSummaryFormat::Callback> g_map;
32 return g_map;
Enrico Granata7de855c2015-10-02 20:59:58 +000033}
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035static CompilerType GetNSPathStore2Type(Target &target) {
36 static ConstString g_type_name("__lldb_autogen_nspathstore2");
37
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +010038 TypeSystemClang *ast_ctx = TypeSystemClang::GetScratch(target);
Kate Stoneb9c1b512016-09-06 20:57:50 +000039
40 if (!ast_ctx)
41 return CompilerType();
42
43 CompilerType voidstar =
44 ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
45 CompilerType uint32 =
46 ast_ctx->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
47
48 return ast_ctx->GetOrCreateStructForIdentifier(
49 g_type_name,
50 {{"isa", voidstar}, {"lengthAndRef", uint32}, {"buffer", voidstar}});
Enrico Granata7de855c2015-10-02 20:59:58 +000051}
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053bool lldb_private::formatters::NSStringSummaryProvider(
54 ValueObject &valobj, Stream &stream,
55 const TypeSummaryOptions &summary_options) {
56 static ConstString g_TypeHint("NSString");
Enrico Granata7de855c2015-10-02 20:59:58 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 ProcessSP process_sp = valobj.GetProcessSP();
59 if (!process_sp)
Enrico Granata7de855c2015-10-02 20:59:58 +000060 return false;
Enrico Granata7de855c2015-10-02 20:59:58 +000061
Alex Langforde823bbe2019-06-10 20:53:23 +000062 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp);
Enrico Granata7de855c2015-10-02 20:59:58 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 if (!runtime)
65 return false;
66
67 ObjCLanguageRuntime::ClassDescriptorSP descriptor(
68 runtime->GetClassDescriptor(valobj));
69
70 if (!descriptor.get() || !descriptor->IsValid())
71 return false;
72
73 uint32_t ptr_size = process_sp->GetAddressByteSize();
74
75 lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
76
77 if (!valobj_addr)
78 return false;
79
80 ConstString class_name_cs = descriptor->GetClassName();
Raphael Isemann6bac09a2019-09-24 11:00:37 +000081 llvm::StringRef class_name = class_name_cs.GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +000082
Raphael Isemann6bac09a2019-09-24 11:00:37 +000083 if (class_name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 return false;
85
Raphael Isemann6bac09a2019-09-24 11:00:37 +000086 bool is_tagged_ptr = class_name == "NSTaggedPointerString" &&
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 descriptor->GetTaggedPointerInfo();
88 // for a tagged pointer, the descriptor has everything we need
89 if (is_tagged_ptr)
90 return NSTaggedString_SummaryProvider(valobj, descriptor, stream,
91 summary_options);
92
93 auto &additionals_map(NSString_Additionals::GetAdditionalSummaries());
94 auto iter = additionals_map.find(class_name_cs), end = additionals_map.end();
95 if (iter != end)
96 return iter->second(valobj, stream, summary_options);
97
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;
102
Zachary Turner97206d52017-05-12 04:51:55 +0000103 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104
105 uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(
106 info_bits_location, 1, 0, error);
107 if (error.Fail())
108 return false;
109
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;
Raphael Isemann6bac09a2019-09-24 11:00:37 +0000114 bool is_path_store = class_name == "NSPathStore2";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 bool has_null = (info_bits & 8) == 8;
116
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;
123 else if (is_inline)
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;
Enrico Granata7de855c2015-10-02 20:59:58 +0000128 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 explicit_length_offset = 0;
130
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);
Enrico Granata7de855c2015-10-02 20:59:58 +0000135 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 }
137
Raphael Isemann6bac09a2019-09-24 11:00:37 +0000138 const llvm::StringSet<> supported_string_classes = {
139 "NSString", "CFMutableStringRef",
140 "CFStringRef", "__NSCFConstantString",
141 "__NSCFString", "NSCFConstantString",
142 "NSCFString", "NSPathStore2"};
143 if (supported_string_classes.count(class_name) == 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 // not one of us - but tell me class name
Raphael Isemann6bac09a2019-09-24 11:00:37 +0000145 stream.Printf("class name = %s", class_name_cs.GetCString());
Enrico Granata7de855c2015-10-02 20:59:58 +0000146 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 }
148
149 std::string prefix, suffix;
150 if (Language *language =
151 Language::FindPlugin(summary_options.GetLanguage())) {
152 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
153 suffix)) {
154 prefix.clear();
155 suffix.clear();
156 }
157 }
158
159 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
160 options.SetPrefixToken(prefix);
161 options.SetSuffixToken(suffix);
162
163 if (is_mutable) {
164 uint64_t location = 2 * ptr_size + valobj_addr;
165 location = process_sp->ReadPointerFromMemory(location, error);
166 if (error.Fail())
167 return false;
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);
181 } else {
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);
193 }
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;
197
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;
210 if (is_inline) {
211 if (!has_explicit_length) {
Enrico Granatac8af52d2016-10-20 22:05:21 +0000212 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 } else
214 location += ptr_size;
215 } else {
216 location = process_sp->ReadPointerFromMemory(location, error);
217 if (error.Fail())
218 return false;
219 }
220 options.SetLocation(location);
221 options.SetProcessSP(process_sp);
222 options.SetStream(&stream);
223 options.SetQuote('"');
224 options.SetSourceSize(explicit_length);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000225 options.SetNeedsZeroTermination(!has_explicit_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
227 TypeSummaryCapping::eTypeSummaryUncapped);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000228 options.SetBinaryZeroIsTerminator(!has_explicit_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 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()));
236 explicit_length =
237 reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20;
238 lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
239
240 options.SetLocation(location);
241 options.SetProcessSP(process_sp);
242 options.SetStream(&stream);
243 options.SetQuote('"');
244 options.SetSourceSize(explicit_length);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000245 options.SetNeedsZeroTermination(!has_explicit_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 options.SetIgnoreMaxLength(summary_options.GetCapping() ==
247 TypeSummaryCapping::eTypeSummaryUncapped);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000248 options.SetBinaryZeroIsTerminator(!has_explicit_length);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 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
Adrian Prantl05097242018-04-30 16:49:04 +0000256 // byte so let's try and use it to handle the embedded NUL case
Zachary Turner97206d52017-05-12 04:51:55 +0000257 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 explicit_length =
259 process_sp->ReadUnsignedIntegerFromMemory(location, 1, 0, error);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000260 has_explicit_length = !(error.Fail() || explicit_length == 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 location++;
262 }
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);
275 else
276 return StringPrinter::ReadStringAndDumpToStream<
277 StringPrinter::StringElementType::ASCII>(options);
278 } else {
279 uint64_t location = valobj_addr + 2 * ptr_size;
280 location = process_sp->ReadPointerFromMemory(location, error);
281 if (error.Fail())
282 return false;
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);
295 }
296}
297
298bool lldb_private::formatters::NSAttributedStringSummaryProvider(
299 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
300 TargetSP target_sp(valobj.GetTargetSP());
301 if (!target_sp)
302 return false;
303 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
304 uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
305 if (!pointer_value)
306 return false;
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));
312 if (!child_ptr_sp)
313 return false;
314 DataExtractor data;
Zachary Turner97206d52017-05-12 04:51:55 +0000315 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 child_ptr_sp->GetData(data, error);
317 if (error.Fail())
318 return false;
319 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData(
320 "string_data", data, exe_ctx, type));
321 child_sp->GetValueAsUnsigned(0);
322 if (child_sp)
323 return NSStringSummaryProvider(*child_sp, stream, options);
324 return false;
325}
326
327bool lldb_private::formatters::NSMutableAttributedStringSummaryProvider(
328 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
329 return NSAttributedStringSummaryProvider(valobj, stream, options);
330}
331
332bool lldb_private::formatters::NSTaggedString_SummaryProvider(
333 ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
334 Stream &stream, const TypeSummaryOptions &summary_options) {
335 static ConstString g_TypeHint("NSString");
336
337 if (!descriptor)
338 return false;
339 uint64_t len_bits = 0, data_bits = 0;
340 if (!descriptor->GetTaggedPointerInfo(&len_bits, &data_bits, nullptr))
341 return false;
342
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;
346
347 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013"
348 "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
349
350 if (len_bits > g_fiveBitMaxLen)
351 return false;
352
353 std::string prefix, suffix;
354 if (Language *language =
355 Language::FindPlugin(summary_options.GetLanguage())) {
356 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
357 suffix)) {
358 prefix.clear();
359 suffix.clear();
360 }
361 }
362
363 // this is a fairly ugly trick - pretend that the numeric value is actually a
Adrian Prantl05097242018-04-30 16:49:04 +0000364 // char* this works under a few assumptions: little endian architecture
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 // 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());
370 return true;
371 }
372
373 // if the data is bitmasked, we need to actually process the bytes
374 uint8_t bitmask = 0;
375 uint8_t shift_offset = 0;
376
377 if (len_bits <= g_SixbitMaxLen) {
378 bitmask = 0x03f;
379 shift_offset = 6;
380 } else {
381 bitmask = 0x01f;
382 shift_offset = 5;
383 }
384
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]);
390 }
391
392 stream.Printf("%s", prefix.c_str());
393 stream.Printf("\"%s\"", &bytes[0]);
394 stream.Printf("%s", suffix.c_str());
395 return true;
Enrico Granata7de855c2015-10-02 20:59:58 +0000396}