]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Language/ObjC/NSString.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / Language / ObjC / NSString.cpp
1 //===-- NSString.cpp ----------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "NSString.h"
12
13 #include "lldb/Core/ValueObject.h"
14 #include "lldb/Core/ValueObjectConstResult.h"
15 #include "lldb/DataFormatters/FormattersHelpers.h"
16 #include "lldb/DataFormatters/StringPrinter.h"
17 #include "lldb/Symbol/ClangASTContext.h"
18 #include "lldb/Target/Language.h"
19 #include "lldb/Target/ProcessStructReader.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/DataBufferHeap.h"
22 #include "lldb/Utility/Endian.h"
23 #include "lldb/Utility/Status.h"
24 #include "lldb/Utility/Stream.h"
25
26 using namespace lldb;
27 using namespace lldb_private;
28 using namespace lldb_private::formatters;
29
30 std::map<ConstString, CXXFunctionSummaryFormat::Callback> &
31 NSString_Additionals::GetAdditionalSummaries() {
32   static std::map<ConstString, CXXFunctionSummaryFormat::Callback> g_map;
33   return g_map;
34 }
35
36 static CompilerType GetNSPathStore2Type(Target &target) {
37   static ConstString g_type_name("__lldb_autogen_nspathstore2");
38
39   ClangASTContext *ast_ctx = target.GetScratchClangASTContext();
40
41   if (!ast_ctx)
42     return CompilerType();
43
44   CompilerType voidstar =
45       ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();
46   CompilerType uint32 =
47       ast_ctx->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
48
49   return ast_ctx->GetOrCreateStructForIdentifier(
50       g_type_name,
51       {{"isa", voidstar}, {"lengthAndRef", uint32}, {"buffer", voidstar}});
52 }
53
54 bool lldb_private::formatters::NSStringSummaryProvider(
55     ValueObject &valobj, Stream &stream,
56     const TypeSummaryOptions &summary_options) {
57   static ConstString g_TypeHint("NSString");
58
59   ProcessSP process_sp = valobj.GetProcessSP();
60   if (!process_sp)
61     return false;
62
63   ObjCLanguageRuntime *runtime =
64       (ObjCLanguageRuntime *)process_sp->GetLanguageRuntime(
65           lldb::eLanguageTypeObjC);
66
67   if (!runtime)
68     return false;
69
70   ObjCLanguageRuntime::ClassDescriptorSP descriptor(
71       runtime->GetClassDescriptor(valobj));
72
73   if (!descriptor.get() || !descriptor->IsValid())
74     return false;
75
76   uint32_t ptr_size = process_sp->GetAddressByteSize();
77
78   lldb::addr_t valobj_addr = valobj.GetValueAsUnsigned(0);
79
80   if (!valobj_addr)
81     return false;
82
83   ConstString class_name_cs = descriptor->GetClassName();
84   const char *class_name = class_name_cs.GetCString();
85
86   if (!class_name || !*class_name)
87     return false;
88
89   bool is_tagged_ptr = (0 == strcmp(class_name, "NSTaggedPointerString")) &&
90                        descriptor->GetTaggedPointerInfo();
91   // for a tagged pointer, the descriptor has everything we need
92   if (is_tagged_ptr)
93     return NSTaggedString_SummaryProvider(valobj, descriptor, stream,
94                                           summary_options);
95
96   auto &additionals_map(NSString_Additionals::GetAdditionalSummaries());
97   auto iter = additionals_map.find(class_name_cs), end = additionals_map.end();
98   if (iter != end)
99     return iter->second(valobj, stream, summary_options);
100
101   // if not a tagged pointer that we know about, try the normal route
102   uint64_t info_bits_location = valobj_addr + ptr_size;
103   if (process_sp->GetByteOrder() != lldb::eByteOrderLittle)
104     info_bits_location += 3;
105
106   Status error;
107
108   uint8_t info_bits = process_sp->ReadUnsignedIntegerFromMemory(
109       info_bits_location, 1, 0, error);
110   if (error.Fail())
111     return false;
112
113   bool is_mutable = (info_bits & 1) == 1;
114   bool is_inline = (info_bits & 0x60) == 0;
115   bool has_explicit_length = (info_bits & (1 | 4)) != 4;
116   bool is_unicode = (info_bits & 0x10) == 0x10;
117   bool is_path_store = strcmp(class_name, "NSPathStore2") == 0;
118   bool has_null = (info_bits & 8) == 8;
119
120   size_t explicit_length = 0;
121   if (!has_null && has_explicit_length && !is_path_store) {
122     lldb::addr_t explicit_length_offset = 2 * ptr_size;
123     if (is_mutable && !is_inline)
124       explicit_length_offset =
125           explicit_length_offset + ptr_size; //  notInlineMutable.length;
126     else if (is_inline)
127       explicit_length = explicit_length + 0; // inline1.length;
128     else if (!is_inline && !is_mutable)
129       explicit_length_offset =
130           explicit_length_offset + ptr_size; // notInlineImmutable1.length;
131     else
132       explicit_length_offset = 0;
133
134     if (explicit_length_offset) {
135       explicit_length_offset = valobj_addr + explicit_length_offset;
136       explicit_length = process_sp->ReadUnsignedIntegerFromMemory(
137           explicit_length_offset, 4, 0, error);
138     }
139   }
140
141   if (strcmp(class_name, "NSString") && strcmp(class_name, "CFStringRef") &&
142       strcmp(class_name, "CFMutableStringRef") &&
143       strcmp(class_name, "__NSCFConstantString") &&
144       strcmp(class_name, "__NSCFString") &&
145       strcmp(class_name, "NSCFConstantString") &&
146       strcmp(class_name, "NSCFString") && strcmp(class_name, "NSPathStore2")) {
147     // not one of us - but tell me class name
148     stream.Printf("class name = %s", class_name);
149     return true;
150   }
151
152   std::string prefix, suffix;
153   if (Language *language =
154           Language::FindPlugin(summary_options.GetLanguage())) {
155     if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
156                                             suffix)) {
157       prefix.clear();
158       suffix.clear();
159     }
160   }
161
162   StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
163   options.SetPrefixToken(prefix);
164   options.SetSuffixToken(suffix);
165
166   if (is_mutable) {
167     uint64_t location = 2 * ptr_size + valobj_addr;
168     location = process_sp->ReadPointerFromMemory(location, error);
169     if (error.Fail())
170       return false;
171     if (has_explicit_length && is_unicode) {
172       options.SetLocation(location);
173       options.SetProcessSP(process_sp);
174       options.SetStream(&stream);
175       options.SetQuote('"');
176       options.SetSourceSize(explicit_length);
177       options.SetNeedsZeroTermination(false);
178       options.SetIgnoreMaxLength(summary_options.GetCapping() ==
179                                  TypeSummaryCapping::eTypeSummaryUncapped);
180       options.SetBinaryZeroIsTerminator(false);
181       options.SetLanguage(summary_options.GetLanguage());
182       return StringPrinter::ReadStringAndDumpToStream<
183           StringPrinter::StringElementType::UTF16>(options);
184     } else {
185       options.SetLocation(location + 1);
186       options.SetProcessSP(process_sp);
187       options.SetStream(&stream);
188       options.SetSourceSize(explicit_length);
189       options.SetNeedsZeroTermination(false);
190       options.SetIgnoreMaxLength(summary_options.GetCapping() ==
191                                  TypeSummaryCapping::eTypeSummaryUncapped);
192       options.SetBinaryZeroIsTerminator(false);
193       options.SetLanguage(summary_options.GetLanguage());
194       return StringPrinter::ReadStringAndDumpToStream<
195           StringPrinter::StringElementType::ASCII>(options);
196     }
197   } else if (is_inline && has_explicit_length && !is_unicode &&
198              !is_path_store && !is_mutable) {
199     uint64_t location = 3 * ptr_size + valobj_addr;
200
201     options.SetLocation(location);
202     options.SetProcessSP(process_sp);
203     options.SetStream(&stream);
204     options.SetQuote('"');
205     options.SetSourceSize(explicit_length);
206     options.SetIgnoreMaxLength(summary_options.GetCapping() ==
207                                TypeSummaryCapping::eTypeSummaryUncapped);
208     options.SetLanguage(summary_options.GetLanguage());
209     return StringPrinter::ReadStringAndDumpToStream<
210         StringPrinter::StringElementType::ASCII>(options);
211   } else if (is_unicode) {
212     uint64_t location = valobj_addr + 2 * ptr_size;
213     if (is_inline) {
214       if (!has_explicit_length) {
215         return false;
216       } else
217         location += ptr_size;
218     } else {
219       location = process_sp->ReadPointerFromMemory(location, error);
220       if (error.Fail())
221         return false;
222     }
223     options.SetLocation(location);
224     options.SetProcessSP(process_sp);
225     options.SetStream(&stream);
226     options.SetQuote('"');
227     options.SetSourceSize(explicit_length);
228     options.SetNeedsZeroTermination(!has_explicit_length);
229     options.SetIgnoreMaxLength(summary_options.GetCapping() ==
230                                TypeSummaryCapping::eTypeSummaryUncapped);
231     options.SetBinaryZeroIsTerminator(!has_explicit_length);
232     options.SetLanguage(summary_options.GetLanguage());
233     return StringPrinter::ReadStringAndDumpToStream<
234         StringPrinter::StringElementType::UTF16>(options);
235   } else if (is_path_store) {
236     ProcessStructReader reader(valobj.GetProcessSP().get(),
237                                valobj.GetValueAsUnsigned(0),
238                                GetNSPathStore2Type(*valobj.GetTargetSP()));
239     explicit_length =
240         reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20;
241     lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4;
242
243     options.SetLocation(location);
244     options.SetProcessSP(process_sp);
245     options.SetStream(&stream);
246     options.SetQuote('"');
247     options.SetSourceSize(explicit_length);
248     options.SetNeedsZeroTermination(!has_explicit_length);
249     options.SetIgnoreMaxLength(summary_options.GetCapping() ==
250                                TypeSummaryCapping::eTypeSummaryUncapped);
251     options.SetBinaryZeroIsTerminator(!has_explicit_length);
252     options.SetLanguage(summary_options.GetLanguage());
253     return StringPrinter::ReadStringAndDumpToStream<
254         StringPrinter::StringElementType::UTF16>(options);
255   } else if (is_inline) {
256     uint64_t location = valobj_addr + 2 * ptr_size;
257     if (!has_explicit_length) {
258       // in this kind of string, the byte before the string content is a length
259       // byte so let's try and use it to handle the embedded NUL case
260       Status error;
261       explicit_length =
262           process_sp->ReadUnsignedIntegerFromMemory(location, 1, 0, error);
263       has_explicit_length = !(error.Fail() || explicit_length == 0);
264       location++;
265     }
266     options.SetLocation(location);
267     options.SetProcessSP(process_sp);
268     options.SetStream(&stream);
269     options.SetSourceSize(explicit_length);
270     options.SetNeedsZeroTermination(!has_explicit_length);
271     options.SetIgnoreMaxLength(summary_options.GetCapping() ==
272                                TypeSummaryCapping::eTypeSummaryUncapped);
273     options.SetBinaryZeroIsTerminator(!has_explicit_length);
274     options.SetLanguage(summary_options.GetLanguage());
275     if (has_explicit_length)
276       return StringPrinter::ReadStringAndDumpToStream<
277           StringPrinter::StringElementType::UTF8>(options);
278     else
279       return StringPrinter::ReadStringAndDumpToStream<
280           StringPrinter::StringElementType::ASCII>(options);
281   } else {
282     uint64_t location = valobj_addr + 2 * ptr_size;
283     location = process_sp->ReadPointerFromMemory(location, error);
284     if (error.Fail())
285       return false;
286     if (has_explicit_length && !has_null)
287       explicit_length++; // account for the fact that there is no NULL and we
288                          // need to have one added
289     options.SetLocation(location);
290     options.SetProcessSP(process_sp);
291     options.SetStream(&stream);
292     options.SetSourceSize(explicit_length);
293     options.SetIgnoreMaxLength(summary_options.GetCapping() ==
294                                TypeSummaryCapping::eTypeSummaryUncapped);
295     options.SetLanguage(summary_options.GetLanguage());
296     return StringPrinter::ReadStringAndDumpToStream<
297         StringPrinter::StringElementType::ASCII>(options);
298   }
299 }
300
301 bool lldb_private::formatters::NSAttributedStringSummaryProvider(
302     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
303   TargetSP target_sp(valobj.GetTargetSP());
304   if (!target_sp)
305     return false;
306   uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize();
307   uint64_t pointer_value = valobj.GetValueAsUnsigned(0);
308   if (!pointer_value)
309     return false;
310   pointer_value += addr_size;
311   CompilerType type(valobj.GetCompilerType());
312   ExecutionContext exe_ctx(target_sp, false);
313   ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress(
314       "string_ptr", pointer_value, exe_ctx, type));
315   if (!child_ptr_sp)
316     return false;
317   DataExtractor data;
318   Status error;
319   child_ptr_sp->GetData(data, error);
320   if (error.Fail())
321     return false;
322   ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData(
323       "string_data", data, exe_ctx, type));
324   child_sp->GetValueAsUnsigned(0);
325   if (child_sp)
326     return NSStringSummaryProvider(*child_sp, stream, options);
327   return false;
328 }
329
330 bool lldb_private::formatters::NSMutableAttributedStringSummaryProvider(
331     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
332   return NSAttributedStringSummaryProvider(valobj, stream, options);
333 }
334
335 bool lldb_private::formatters::NSTaggedString_SummaryProvider(
336     ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor,
337     Stream &stream, const TypeSummaryOptions &summary_options) {
338   static ConstString g_TypeHint("NSString");
339
340   if (!descriptor)
341     return false;
342   uint64_t len_bits = 0, data_bits = 0;
343   if (!descriptor->GetTaggedPointerInfo(&len_bits, &data_bits, nullptr))
344     return false;
345
346   static const int g_MaxNonBitmaskedLen = 7; // TAGGED_STRING_UNPACKED_MAXLEN
347   static const int g_SixbitMaxLen = 9;
348   static const int g_fiveBitMaxLen = 11;
349
350   static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013"
351                                           "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX";
352
353   if (len_bits > g_fiveBitMaxLen)
354     return false;
355
356   std::string prefix, suffix;
357   if (Language *language =
358           Language::FindPlugin(summary_options.GetLanguage())) {
359     if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix,
360                                             suffix)) {
361       prefix.clear();
362       suffix.clear();
363     }
364   }
365
366   // this is a fairly ugly trick - pretend that the numeric value is actually a
367   // char* this works under a few assumptions: little endian architecture
368   // sizeof(uint64_t) > g_MaxNonBitmaskedLen
369   if (len_bits <= g_MaxNonBitmaskedLen) {
370     stream.Printf("%s", prefix.c_str());
371     stream.Printf("\"%s\"", (const char *)&data_bits);
372     stream.Printf("%s", suffix.c_str());
373     return true;
374   }
375
376   // if the data is bitmasked, we need to actually process the bytes
377   uint8_t bitmask = 0;
378   uint8_t shift_offset = 0;
379
380   if (len_bits <= g_SixbitMaxLen) {
381     bitmask = 0x03f;
382     shift_offset = 6;
383   } else {
384     bitmask = 0x01f;
385     shift_offset = 5;
386   }
387
388   std::vector<uint8_t> bytes;
389   bytes.resize(len_bits);
390   for (; len_bits > 0; data_bits >>= shift_offset, --len_bits) {
391     uint8_t packed = data_bits & bitmask;
392     bytes.insert(bytes.begin(), sixBitToCharLookup[packed]);
393   }
394
395   stream.Printf("%s", prefix.c_str());
396   stream.Printf("\"%s\"", &bytes[0]);
397   stream.Printf("%s", suffix.c_str());
398   return true;
399 }