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/DataBufferHeap.h" 14 #include "lldb/Core/Error.h" 15 #include "lldb/Core/Stream.h" 16 #include "lldb/Core/ValueObject.h" 17 #include "lldb/Core/ValueObjectConstResult.h" 18 #include "lldb/DataFormatters/FormattersHelpers.h" 19 #include "lldb/DataFormatters/StringPrinter.h" 20 #include "lldb/Host/Endian.h" 21 #include "lldb/Symbol/ClangASTContext.h" 22 #include "lldb/Target/Language.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Utility/ProcessStructReader.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 Error 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 stream.Printf("found new combo"); 216 return true; 217 } else 218 location += ptr_size; 219 } else { 220 location = process_sp->ReadPointerFromMemory(location, error); 221 if (error.Fail()) 222 return false; 223 } 224 options.SetLocation(location); 225 options.SetProcessSP(process_sp); 226 options.SetStream(&stream); 227 options.SetQuote('"'); 228 options.SetSourceSize(explicit_length); 229 options.SetNeedsZeroTermination(has_explicit_length == false); 230 options.SetIgnoreMaxLength(summary_options.GetCapping() == 231 TypeSummaryCapping::eTypeSummaryUncapped); 232 options.SetBinaryZeroIsTerminator(has_explicit_length == false); 233 options.SetLanguage(summary_options.GetLanguage()); 234 return StringPrinter::ReadStringAndDumpToStream< 235 StringPrinter::StringElementType::UTF16>(options); 236 } else if (is_path_store) { 237 ProcessStructReader reader(valobj.GetProcessSP().get(), 238 valobj.GetValueAsUnsigned(0), 239 GetNSPathStore2Type(*valobj.GetTargetSP())); 240 explicit_length = 241 reader.GetField<uint32_t>(ConstString("lengthAndRef")) >> 20; 242 lldb::addr_t location = valobj.GetValueAsUnsigned(0) + ptr_size + 4; 243 244 options.SetLocation(location); 245 options.SetProcessSP(process_sp); 246 options.SetStream(&stream); 247 options.SetQuote('"'); 248 options.SetSourceSize(explicit_length); 249 options.SetNeedsZeroTermination(has_explicit_length == false); 250 options.SetIgnoreMaxLength(summary_options.GetCapping() == 251 TypeSummaryCapping::eTypeSummaryUncapped); 252 options.SetBinaryZeroIsTerminator(has_explicit_length == false); 253 options.SetLanguage(summary_options.GetLanguage()); 254 return StringPrinter::ReadStringAndDumpToStream< 255 StringPrinter::StringElementType::UTF16>(options); 256 } else if (is_inline) { 257 uint64_t location = valobj_addr + 2 * ptr_size; 258 if (!has_explicit_length) { 259 // in this kind of string, the byte before the string content is a length 260 // byte 261 // so let's try and use it to handle the embedded NUL case 262 Error error; 263 explicit_length = 264 process_sp->ReadUnsignedIntegerFromMemory(location, 1, 0, error); 265 if (error.Fail() || explicit_length == 0) 266 has_explicit_length = false; 267 else 268 has_explicit_length = true; 269 location++; 270 } 271 options.SetLocation(location); 272 options.SetProcessSP(process_sp); 273 options.SetStream(&stream); 274 options.SetSourceSize(explicit_length); 275 options.SetNeedsZeroTermination(!has_explicit_length); 276 options.SetIgnoreMaxLength(summary_options.GetCapping() == 277 TypeSummaryCapping::eTypeSummaryUncapped); 278 options.SetBinaryZeroIsTerminator(!has_explicit_length); 279 options.SetLanguage(summary_options.GetLanguage()); 280 if (has_explicit_length) 281 return StringPrinter::ReadStringAndDumpToStream< 282 StringPrinter::StringElementType::UTF8>(options); 283 else 284 return StringPrinter::ReadStringAndDumpToStream< 285 StringPrinter::StringElementType::ASCII>(options); 286 } else { 287 uint64_t location = valobj_addr + 2 * ptr_size; 288 location = process_sp->ReadPointerFromMemory(location, error); 289 if (error.Fail()) 290 return false; 291 if (has_explicit_length && !has_null) 292 explicit_length++; // account for the fact that there is no NULL and we 293 // need to have one added 294 options.SetLocation(location); 295 options.SetProcessSP(process_sp); 296 options.SetStream(&stream); 297 options.SetSourceSize(explicit_length); 298 options.SetIgnoreMaxLength(summary_options.GetCapping() == 299 TypeSummaryCapping::eTypeSummaryUncapped); 300 options.SetLanguage(summary_options.GetLanguage()); 301 return StringPrinter::ReadStringAndDumpToStream< 302 StringPrinter::StringElementType::ASCII>(options); 303 } 304 } 305 306 bool lldb_private::formatters::NSAttributedStringSummaryProvider( 307 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { 308 TargetSP target_sp(valobj.GetTargetSP()); 309 if (!target_sp) 310 return false; 311 uint32_t addr_size = target_sp->GetArchitecture().GetAddressByteSize(); 312 uint64_t pointer_value = valobj.GetValueAsUnsigned(0); 313 if (!pointer_value) 314 return false; 315 pointer_value += addr_size; 316 CompilerType type(valobj.GetCompilerType()); 317 ExecutionContext exe_ctx(target_sp, false); 318 ValueObjectSP child_ptr_sp(valobj.CreateValueObjectFromAddress( 319 "string_ptr", pointer_value, exe_ctx, type)); 320 if (!child_ptr_sp) 321 return false; 322 DataExtractor data; 323 Error error; 324 child_ptr_sp->GetData(data, error); 325 if (error.Fail()) 326 return false; 327 ValueObjectSP child_sp(child_ptr_sp->CreateValueObjectFromData( 328 "string_data", data, exe_ctx, type)); 329 child_sp->GetValueAsUnsigned(0); 330 if (child_sp) 331 return NSStringSummaryProvider(*child_sp, stream, options); 332 return false; 333 } 334 335 bool lldb_private::formatters::NSMutableAttributedStringSummaryProvider( 336 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { 337 return NSAttributedStringSummaryProvider(valobj, stream, options); 338 } 339 340 bool lldb_private::formatters::NSTaggedString_SummaryProvider( 341 ValueObject &valobj, ObjCLanguageRuntime::ClassDescriptorSP descriptor, 342 Stream &stream, const TypeSummaryOptions &summary_options) { 343 static ConstString g_TypeHint("NSString"); 344 345 if (!descriptor) 346 return false; 347 uint64_t len_bits = 0, data_bits = 0; 348 if (!descriptor->GetTaggedPointerInfo(&len_bits, &data_bits, nullptr)) 349 return false; 350 351 static const int g_MaxNonBitmaskedLen = 7; // TAGGED_STRING_UNPACKED_MAXLEN 352 static const int g_SixbitMaxLen = 9; 353 static const int g_fiveBitMaxLen = 11; 354 355 static const char *sixBitToCharLookup = "eilotrm.apdnsIc ufkMShjTRxgC4013" 356 "bDNvwyUL2O856P-B79AFKEWV_zGJ/HYX"; 357 358 if (len_bits > g_fiveBitMaxLen) 359 return false; 360 361 std::string prefix, suffix; 362 if (Language *language = 363 Language::FindPlugin(summary_options.GetLanguage())) { 364 if (!language->GetFormatterPrefixSuffix(valobj, g_TypeHint, prefix, 365 suffix)) { 366 prefix.clear(); 367 suffix.clear(); 368 } 369 } 370 371 // this is a fairly ugly trick - pretend that the numeric value is actually a 372 // char* 373 // this works under a few assumptions: 374 // little endian architecture 375 // sizeof(uint64_t) > g_MaxNonBitmaskedLen 376 if (len_bits <= g_MaxNonBitmaskedLen) { 377 stream.Printf("%s", prefix.c_str()); 378 stream.Printf("\"%s\"", (const char *)&data_bits); 379 stream.Printf("%s", suffix.c_str()); 380 return true; 381 } 382 383 // if the data is bitmasked, we need to actually process the bytes 384 uint8_t bitmask = 0; 385 uint8_t shift_offset = 0; 386 387 if (len_bits <= g_SixbitMaxLen) { 388 bitmask = 0x03f; 389 shift_offset = 6; 390 } else { 391 bitmask = 0x01f; 392 shift_offset = 5; 393 } 394 395 std::vector<uint8_t> bytes; 396 bytes.resize(len_bits); 397 for (; len_bits > 0; data_bits >>= shift_offset, --len_bits) { 398 uint8_t packed = data_bits & bitmask; 399 bytes.insert(bytes.begin(), sixBitToCharLookup[packed]); 400 } 401 402 stream.Printf("%s", prefix.c_str()); 403 stream.Printf("\"%s\"", &bytes[0]); 404 stream.Printf("%s", suffix.c_str()); 405 return true; 406 } 407