1 //===-- NSString.cpp ----------------------------------------------*- C++ 2 //-*-===// 3 // 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 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "NSString.h" 11 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" 24 25 using namespace lldb; 26 using namespace lldb_private; 27 using namespace lldb_private::formatters; 28 29 std::map<ConstString, CXXFunctionSummaryFormat::Callback> & 30 NSString_Additionals::GetAdditionalSummaries() { 31 static std::map<ConstString, CXXFunctionSummaryFormat::Callback> g_map; 32 return g_map; 33 } 34 35 static CompilerType GetNSPathStore2Type(Target &target) { 36 static ConstString g_type_name("__lldb_autogen_nspathstore2"); 37 38 ClangASTContext *ast_ctx = ClangASTContext::GetScratch(target); 39 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}}); 51 } 52 53 bool lldb_private::formatters::NSStringSummaryProvider( 54 ValueObject &valobj, Stream &stream, 55 const TypeSummaryOptions &summary_options) { 56 static ConstString g_TypeHint("NSString"); 57 58 ProcessSP process_sp = valobj.GetProcessSP(); 59 if (!process_sp) 60 return false; 61 62 ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process_sp); 63 64 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(); 81 llvm::StringRef class_name = class_name_cs.GetStringRef(); 82 83 if (class_name.empty()) 84 return false; 85 86 bool is_tagged_ptr = class_name == "NSTaggedPointerString" && 87 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 103 Status error; 104 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; 114 bool is_path_store = class_name == "NSPathStore2"; 115 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; 128 else 129 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); 135 } 136 } 137 138 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) { 144 // not one of us - but tell me class name 145 stream.Printf("class name = %s", class_name_cs.GetCString()); 146 return true; 147 } 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) { 212 return false; 213 } 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); 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())); 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); 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 257 Status error; 258 explicit_length = 259 process_sp->ReadUnsignedIntegerFromMemory(location, 1, 0, error); 260 has_explicit_length = !(error.Fail() || explicit_length == 0); 261 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 298 bool 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; 315 Status error; 316 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 327 bool lldb_private::formatters::NSMutableAttributedStringSummaryProvider( 328 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { 329 return NSAttributedStringSummaryProvider(valobj, stream, options); 330 } 331 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"); 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 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()); 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; 396 } 397