1 //===-- FormatManager.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/DataFormatters/FormatManager.h" 10 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/DataFormatters/FormattersHelpers.h" 13 #include "lldb/DataFormatters/LanguageCategory.h" 14 #include "lldb/Target/ExecutionContext.h" 15 #include "lldb/Target/Language.h" 16 #include "lldb/Utility/LLDBLog.h" 17 #include "lldb/Utility/Log.h" 18 #include "llvm/ADT/STLExtras.h" 19 20 using namespace lldb; 21 using namespace lldb_private; 22 using namespace lldb_private::formatters; 23 24 struct FormatInfo { 25 Format format; 26 const char format_char; // One or more format characters that can be used for 27 // this format. 28 const char *format_name; // Long format name that can be used to specify the 29 // current format 30 }; 31 32 static constexpr FormatInfo g_format_infos[] = { 33 {eFormatDefault, '\0', "default"}, 34 {eFormatBoolean, 'B', "boolean"}, 35 {eFormatBinary, 'b', "binary"}, 36 {eFormatBytes, 'y', "bytes"}, 37 {eFormatBytesWithASCII, 'Y', "bytes with ASCII"}, 38 {eFormatChar, 'c', "character"}, 39 {eFormatCharPrintable, 'C', "printable character"}, 40 {eFormatComplexFloat, 'F', "complex float"}, 41 {eFormatCString, 's', "c-string"}, 42 {eFormatDecimal, 'd', "decimal"}, 43 {eFormatEnum, 'E', "enumeration"}, 44 {eFormatHex, 'x', "hex"}, 45 {eFormatHexUppercase, 'X', "uppercase hex"}, 46 {eFormatFloat, 'f', "float"}, 47 {eFormatOctal, 'o', "octal"}, 48 {eFormatOSType, 'O', "OSType"}, 49 {eFormatUnicode16, 'U', "unicode16"}, 50 {eFormatUnicode32, '\0', "unicode32"}, 51 {eFormatUnsigned, 'u', "unsigned decimal"}, 52 {eFormatPointer, 'p', "pointer"}, 53 {eFormatVectorOfChar, '\0', "char[]"}, 54 {eFormatVectorOfSInt8, '\0', "int8_t[]"}, 55 {eFormatVectorOfUInt8, '\0', "uint8_t[]"}, 56 {eFormatVectorOfSInt16, '\0', "int16_t[]"}, 57 {eFormatVectorOfUInt16, '\0', "uint16_t[]"}, 58 {eFormatVectorOfSInt32, '\0', "int32_t[]"}, 59 {eFormatVectorOfUInt32, '\0', "uint32_t[]"}, 60 {eFormatVectorOfSInt64, '\0', "int64_t[]"}, 61 {eFormatVectorOfUInt64, '\0', "uint64_t[]"}, 62 {eFormatVectorOfFloat16, '\0', "float16[]"}, 63 {eFormatVectorOfFloat32, '\0', "float32[]"}, 64 {eFormatVectorOfFloat64, '\0', "float64[]"}, 65 {eFormatVectorOfUInt128, '\0', "uint128_t[]"}, 66 {eFormatComplexInteger, 'I', "complex integer"}, 67 {eFormatCharArray, 'a', "character array"}, 68 {eFormatAddressInfo, 'A', "address"}, 69 {eFormatHexFloat, '\0', "hex float"}, 70 {eFormatInstruction, 'i', "instruction"}, 71 {eFormatVoid, 'v', "void"}, 72 {eFormatUnicode8, 'u', "unicode8"}, 73 }; 74 75 static_assert((sizeof(g_format_infos) / sizeof(g_format_infos[0])) == 76 kNumFormats, 77 "All formats must have a corresponding info entry."); 78 79 static uint32_t g_num_format_infos = std::size(g_format_infos); 80 81 static bool GetFormatFromFormatChar(char format_char, Format &format) { 82 for (uint32_t i = 0; i < g_num_format_infos; ++i) { 83 if (g_format_infos[i].format_char == format_char) { 84 format = g_format_infos[i].format; 85 return true; 86 } 87 } 88 format = eFormatInvalid; 89 return false; 90 } 91 92 static bool GetFormatFromFormatName(llvm::StringRef format_name, 93 bool partial_match_ok, Format &format) { 94 uint32_t i; 95 for (i = 0; i < g_num_format_infos; ++i) { 96 if (format_name.equals_insensitive(g_format_infos[i].format_name)) { 97 format = g_format_infos[i].format; 98 return true; 99 } 100 } 101 102 if (partial_match_ok) { 103 for (i = 0; i < g_num_format_infos; ++i) { 104 if (llvm::StringRef(g_format_infos[i].format_name) 105 .startswith_insensitive(format_name)) { 106 format = g_format_infos[i].format; 107 return true; 108 } 109 } 110 } 111 format = eFormatInvalid; 112 return false; 113 } 114 115 void FormatManager::Changed() { 116 ++m_last_revision; 117 m_format_cache.Clear(); 118 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 119 for (auto &iter : m_language_categories_map) { 120 if (iter.second) 121 iter.second->GetFormatCache().Clear(); 122 } 123 } 124 125 bool FormatManager::GetFormatFromCString(const char *format_cstr, 126 bool partial_match_ok, 127 lldb::Format &format) { 128 bool success = false; 129 if (format_cstr && format_cstr[0]) { 130 if (format_cstr[1] == '\0') { 131 success = GetFormatFromFormatChar(format_cstr[0], format); 132 if (success) 133 return true; 134 } 135 136 success = GetFormatFromFormatName(format_cstr, partial_match_ok, format); 137 } 138 if (!success) 139 format = eFormatInvalid; 140 return success; 141 } 142 143 char FormatManager::GetFormatAsFormatChar(lldb::Format format) { 144 for (uint32_t i = 0; i < g_num_format_infos; ++i) { 145 if (g_format_infos[i].format == format) 146 return g_format_infos[i].format_char; 147 } 148 return '\0'; 149 } 150 151 const char *FormatManager::GetFormatAsCString(Format format) { 152 if (format >= eFormatDefault && format < kNumFormats) 153 return g_format_infos[format].format_name; 154 return nullptr; 155 } 156 157 void FormatManager::EnableAllCategories() { 158 m_categories_map.EnableAllCategories(); 159 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 160 for (auto &iter : m_language_categories_map) { 161 if (iter.second) 162 iter.second->Enable(); 163 } 164 } 165 166 void FormatManager::DisableAllCategories() { 167 m_categories_map.DisableAllCategories(); 168 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 169 for (auto &iter : m_language_categories_map) { 170 if (iter.second) 171 iter.second->Disable(); 172 } 173 } 174 175 void FormatManager::GetPossibleMatches( 176 ValueObject &valobj, CompilerType compiler_type, 177 lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries, 178 FormattersMatchCandidate::Flags current_flags, bool root_level) { 179 compiler_type = compiler_type.GetTypeForFormatters(); 180 ConstString type_name(compiler_type.GetTypeName()); 181 if (valobj.GetBitfieldBitSize() > 0) { 182 StreamString sstring; 183 sstring.Printf("%s:%d", type_name.AsCString(), valobj.GetBitfieldBitSize()); 184 ConstString bitfieldname(sstring.GetString()); 185 entries.push_back({bitfieldname, current_flags}); 186 } 187 188 if (!compiler_type.IsMeaninglessWithoutDynamicResolution()) { 189 entries.push_back({type_name, current_flags}); 190 191 ConstString display_type_name(compiler_type.GetTypeName()); 192 if (display_type_name != type_name) 193 entries.push_back({display_type_name, current_flags}); 194 } 195 196 for (bool is_rvalue_ref = true, j = true; 197 j && compiler_type.IsReferenceType(nullptr, &is_rvalue_ref); j = false) { 198 CompilerType non_ref_type = compiler_type.GetNonReferenceType(); 199 GetPossibleMatches(valobj, non_ref_type, use_dynamic, entries, 200 current_flags.WithStrippedReference()); 201 if (non_ref_type.IsTypedefType()) { 202 CompilerType deffed_referenced_type = non_ref_type.GetTypedefedType(); 203 deffed_referenced_type = 204 is_rvalue_ref ? deffed_referenced_type.GetRValueReferenceType() 205 : deffed_referenced_type.GetLValueReferenceType(); 206 // this is not exactly the usual meaning of stripping typedefs 207 GetPossibleMatches( 208 valobj, deffed_referenced_type, 209 use_dynamic, entries, current_flags.WithStrippedTypedef()); 210 } 211 } 212 213 if (compiler_type.IsPointerType()) { 214 CompilerType non_ptr_type = compiler_type.GetPointeeType(); 215 GetPossibleMatches(valobj, non_ptr_type, use_dynamic, entries, 216 current_flags.WithStrippedPointer()); 217 if (non_ptr_type.IsTypedefType()) { 218 CompilerType deffed_pointed_type = 219 non_ptr_type.GetTypedefedType().GetPointerType(); 220 // this is not exactly the usual meaning of stripping typedefs 221 GetPossibleMatches(valobj, deffed_pointed_type, use_dynamic, entries, 222 current_flags.WithStrippedTypedef()); 223 } 224 } 225 226 // For arrays with typedef-ed elements, we add a candidate with the typedef 227 // stripped. 228 uint64_t array_size; 229 if (compiler_type.IsArrayType(nullptr, &array_size, nullptr)) { 230 ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); 231 CompilerType element_type = compiler_type.GetArrayElementType( 232 exe_ctx.GetBestExecutionContextScope()); 233 if (element_type.IsTypedefType()) { 234 // Get the stripped element type and compute the stripped array type 235 // from it. 236 CompilerType deffed_array_type = 237 element_type.GetTypedefedType().GetArrayType(array_size); 238 // this is not exactly the usual meaning of stripping typedefs 239 GetPossibleMatches( 240 valobj, deffed_array_type, 241 use_dynamic, entries, current_flags.WithStrippedTypedef()); 242 } 243 } 244 245 for (lldb::LanguageType language_type : 246 GetCandidateLanguages(valobj.GetObjectRuntimeLanguage())) { 247 if (Language *language = Language::FindPlugin(language_type)) { 248 for (ConstString candidate : 249 language->GetPossibleFormattersMatches(valobj, use_dynamic)) { 250 entries.push_back({candidate, current_flags}); 251 } 252 } 253 } 254 255 // try to strip typedef chains 256 if (compiler_type.IsTypedefType()) { 257 CompilerType deffed_type = compiler_type.GetTypedefedType(); 258 GetPossibleMatches(valobj, deffed_type, use_dynamic, entries, 259 current_flags.WithStrippedTypedef()); 260 } 261 262 if (root_level) { 263 do { 264 if (!compiler_type.IsValid()) 265 break; 266 267 CompilerType unqual_compiler_ast_type = 268 compiler_type.GetFullyUnqualifiedType(); 269 if (!unqual_compiler_ast_type.IsValid()) 270 break; 271 if (unqual_compiler_ast_type.GetOpaqueQualType() != 272 compiler_type.GetOpaqueQualType()) 273 GetPossibleMatches(valobj, unqual_compiler_ast_type, use_dynamic, 274 entries, current_flags); 275 } while (false); 276 277 // if all else fails, go to static type 278 if (valobj.IsDynamic()) { 279 lldb::ValueObjectSP static_value_sp(valobj.GetStaticValue()); 280 if (static_value_sp) 281 GetPossibleMatches(*static_value_sp.get(), 282 static_value_sp->GetCompilerType(), use_dynamic, 283 entries, current_flags, true); 284 } 285 } 286 } 287 288 lldb::TypeFormatImplSP 289 FormatManager::GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp) { 290 if (!type_sp) 291 return lldb::TypeFormatImplSP(); 292 lldb::TypeFormatImplSP format_chosen_sp; 293 uint32_t num_categories = m_categories_map.GetCount(); 294 lldb::TypeCategoryImplSP category_sp; 295 uint32_t prio_category = UINT32_MAX; 296 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 297 category_sp = GetCategoryAtIndex(category_id); 298 if (!category_sp->IsEnabled()) 299 continue; 300 lldb::TypeFormatImplSP format_current_sp = 301 category_sp->GetFormatForType(type_sp); 302 if (format_current_sp && 303 (format_chosen_sp.get() == nullptr || 304 (prio_category > category_sp->GetEnabledPosition()))) { 305 prio_category = category_sp->GetEnabledPosition(); 306 format_chosen_sp = format_current_sp; 307 } 308 } 309 return format_chosen_sp; 310 } 311 312 lldb::TypeSummaryImplSP 313 FormatManager::GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp) { 314 if (!type_sp) 315 return lldb::TypeSummaryImplSP(); 316 lldb::TypeSummaryImplSP summary_chosen_sp; 317 uint32_t num_categories = m_categories_map.GetCount(); 318 lldb::TypeCategoryImplSP category_sp; 319 uint32_t prio_category = UINT32_MAX; 320 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 321 category_sp = GetCategoryAtIndex(category_id); 322 if (!category_sp->IsEnabled()) 323 continue; 324 lldb::TypeSummaryImplSP summary_current_sp = 325 category_sp->GetSummaryForType(type_sp); 326 if (summary_current_sp && 327 (summary_chosen_sp.get() == nullptr || 328 (prio_category > category_sp->GetEnabledPosition()))) { 329 prio_category = category_sp->GetEnabledPosition(); 330 summary_chosen_sp = summary_current_sp; 331 } 332 } 333 return summary_chosen_sp; 334 } 335 336 lldb::TypeFilterImplSP 337 FormatManager::GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp) { 338 if (!type_sp) 339 return lldb::TypeFilterImplSP(); 340 lldb::TypeFilterImplSP filter_chosen_sp; 341 uint32_t num_categories = m_categories_map.GetCount(); 342 lldb::TypeCategoryImplSP category_sp; 343 uint32_t prio_category = UINT32_MAX; 344 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 345 category_sp = GetCategoryAtIndex(category_id); 346 if (!category_sp->IsEnabled()) 347 continue; 348 lldb::TypeFilterImplSP filter_current_sp( 349 (TypeFilterImpl *)category_sp->GetFilterForType(type_sp).get()); 350 if (filter_current_sp && 351 (filter_chosen_sp.get() == nullptr || 352 (prio_category > category_sp->GetEnabledPosition()))) { 353 prio_category = category_sp->GetEnabledPosition(); 354 filter_chosen_sp = filter_current_sp; 355 } 356 } 357 return filter_chosen_sp; 358 } 359 360 lldb::ScriptedSyntheticChildrenSP 361 FormatManager::GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp) { 362 if (!type_sp) 363 return lldb::ScriptedSyntheticChildrenSP(); 364 lldb::ScriptedSyntheticChildrenSP synth_chosen_sp; 365 uint32_t num_categories = m_categories_map.GetCount(); 366 lldb::TypeCategoryImplSP category_sp; 367 uint32_t prio_category = UINT32_MAX; 368 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 369 category_sp = GetCategoryAtIndex(category_id); 370 if (!category_sp->IsEnabled()) 371 continue; 372 lldb::ScriptedSyntheticChildrenSP synth_current_sp( 373 (ScriptedSyntheticChildren *)category_sp->GetSyntheticForType(type_sp) 374 .get()); 375 if (synth_current_sp && 376 (synth_chosen_sp.get() == nullptr || 377 (prio_category > category_sp->GetEnabledPosition()))) { 378 prio_category = category_sp->GetEnabledPosition(); 379 synth_chosen_sp = synth_current_sp; 380 } 381 } 382 return synth_chosen_sp; 383 } 384 385 void FormatManager::ForEachCategory(TypeCategoryMap::ForEachCallback callback) { 386 m_categories_map.ForEach(callback); 387 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 388 for (const auto &entry : m_language_categories_map) { 389 if (auto category_sp = entry.second->GetCategory()) { 390 if (!callback(category_sp)) 391 break; 392 } 393 } 394 } 395 396 lldb::TypeCategoryImplSP 397 FormatManager::GetCategory(ConstString category_name, bool can_create) { 398 if (!category_name) 399 return GetCategory(m_default_category_name); 400 lldb::TypeCategoryImplSP category; 401 if (m_categories_map.Get(category_name, category)) 402 return category; 403 404 if (!can_create) 405 return lldb::TypeCategoryImplSP(); 406 407 m_categories_map.Add( 408 category_name, 409 lldb::TypeCategoryImplSP(new TypeCategoryImpl(this, category_name))); 410 return GetCategory(category_name); 411 } 412 413 lldb::Format FormatManager::GetSingleItemFormat(lldb::Format vector_format) { 414 switch (vector_format) { 415 case eFormatVectorOfChar: 416 return eFormatCharArray; 417 418 case eFormatVectorOfSInt8: 419 case eFormatVectorOfSInt16: 420 case eFormatVectorOfSInt32: 421 case eFormatVectorOfSInt64: 422 return eFormatDecimal; 423 424 case eFormatVectorOfUInt8: 425 case eFormatVectorOfUInt16: 426 case eFormatVectorOfUInt32: 427 case eFormatVectorOfUInt64: 428 case eFormatVectorOfUInt128: 429 return eFormatHex; 430 431 case eFormatVectorOfFloat16: 432 case eFormatVectorOfFloat32: 433 case eFormatVectorOfFloat64: 434 return eFormatFloat; 435 436 default: 437 return lldb::eFormatInvalid; 438 } 439 } 440 441 bool FormatManager::ShouldPrintAsOneLiner(ValueObject &valobj) { 442 // if settings say no oneline whatsoever 443 if (valobj.GetTargetSP().get() && 444 !valobj.GetTargetSP()->GetDebugger().GetAutoOneLineSummaries()) 445 return false; // then don't oneline 446 447 // if this object has a summary, then ask the summary 448 if (valobj.GetSummaryFormat().get() != nullptr) 449 return valobj.GetSummaryFormat()->IsOneLiner(); 450 451 // no children, no party 452 if (valobj.GetNumChildren() == 0) 453 return false; 454 455 // ask the type if it has any opinion about this eLazyBoolCalculate == no 456 // opinion; other values should be self explanatory 457 CompilerType compiler_type(valobj.GetCompilerType()); 458 if (compiler_type.IsValid()) { 459 switch (compiler_type.ShouldPrintAsOneLiner(&valobj)) { 460 case eLazyBoolNo: 461 return false; 462 case eLazyBoolYes: 463 return true; 464 case eLazyBoolCalculate: 465 break; 466 } 467 } 468 469 size_t total_children_name_len = 0; 470 471 for (size_t idx = 0; idx < valobj.GetNumChildren(); idx++) { 472 bool is_synth_val = false; 473 ValueObjectSP child_sp(valobj.GetChildAtIndex(idx, true)); 474 // something is wrong here - bail out 475 if (!child_sp) 476 return false; 477 478 // also ask the child's type if it has any opinion 479 CompilerType child_compiler_type(child_sp->GetCompilerType()); 480 if (child_compiler_type.IsValid()) { 481 switch (child_compiler_type.ShouldPrintAsOneLiner(child_sp.get())) { 482 case eLazyBoolYes: 483 // an opinion of yes is only binding for the child, so keep going 484 case eLazyBoolCalculate: 485 break; 486 case eLazyBoolNo: 487 // but if the child says no, then it's a veto on the whole thing 488 return false; 489 } 490 } 491 492 // if we decided to define synthetic children for a type, we probably care 493 // enough to show them, but avoid nesting children in children 494 if (child_sp->GetSyntheticChildren().get() != nullptr) { 495 ValueObjectSP synth_sp(child_sp->GetSyntheticValue()); 496 // wait.. wat? just get out of here.. 497 if (!synth_sp) 498 return false; 499 // but if we only have them to provide a value, keep going 500 if (!synth_sp->MightHaveChildren() && 501 synth_sp->DoesProvideSyntheticValue()) 502 is_synth_val = true; 503 else 504 return false; 505 } 506 507 total_children_name_len += child_sp->GetName().GetLength(); 508 509 // 50 itself is a "randomly" chosen number - the idea is that 510 // overly long structs should not get this treatment 511 // FIXME: maybe make this a user-tweakable setting? 512 if (total_children_name_len > 50) 513 return false; 514 515 // if a summary is there.. 516 if (child_sp->GetSummaryFormat()) { 517 // and it wants children, then bail out 518 if (child_sp->GetSummaryFormat()->DoesPrintChildren(child_sp.get())) 519 return false; 520 } 521 522 // if this child has children.. 523 if (child_sp->GetNumChildren()) { 524 // ...and no summary... 525 // (if it had a summary and the summary wanted children, we would have 526 // bailed out anyway 527 // so this only makes us bail out if this has no summary and we would 528 // then print children) 529 if (!child_sp->GetSummaryFormat() && !is_synth_val) // but again only do 530 // that if not a 531 // synthetic valued 532 // child 533 return false; // then bail out 534 } 535 } 536 return true; 537 } 538 539 ConstString FormatManager::GetTypeForCache(ValueObject &valobj, 540 lldb::DynamicValueType use_dynamic) { 541 ValueObjectSP valobj_sp = valobj.GetQualifiedRepresentationIfAvailable( 542 use_dynamic, valobj.IsSynthetic()); 543 if (valobj_sp && valobj_sp->GetCompilerType().IsValid()) { 544 if (!valobj_sp->GetCompilerType().IsMeaninglessWithoutDynamicResolution()) 545 return valobj_sp->GetQualifiedTypeName(); 546 } 547 return ConstString(); 548 } 549 550 std::vector<lldb::LanguageType> 551 FormatManager::GetCandidateLanguages(lldb::LanguageType lang_type) { 552 switch (lang_type) { 553 case lldb::eLanguageTypeC: 554 case lldb::eLanguageTypeC89: 555 case lldb::eLanguageTypeC99: 556 case lldb::eLanguageTypeC11: 557 case lldb::eLanguageTypeC_plus_plus: 558 case lldb::eLanguageTypeC_plus_plus_03: 559 case lldb::eLanguageTypeC_plus_plus_11: 560 case lldb::eLanguageTypeC_plus_plus_14: 561 return {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC}; 562 default: 563 return {lang_type}; 564 } 565 llvm_unreachable("Fully covered switch"); 566 } 567 568 LanguageCategory * 569 FormatManager::GetCategoryForLanguage(lldb::LanguageType lang_type) { 570 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 571 auto iter = m_language_categories_map.find(lang_type), 572 end = m_language_categories_map.end(); 573 if (iter != end) 574 return iter->second.get(); 575 LanguageCategory *lang_category = new LanguageCategory(lang_type); 576 m_language_categories_map[lang_type] = 577 LanguageCategory::UniquePointer(lang_category); 578 return lang_category; 579 } 580 581 template <typename ImplSP> 582 ImplSP FormatManager::GetHardcoded(FormattersMatchData &match_data) { 583 ImplSP retval_sp; 584 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 585 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 586 if (lang_category->GetHardcoded(*this, match_data, retval_sp)) 587 return retval_sp; 588 } 589 } 590 return retval_sp; 591 } 592 593 template <typename ImplSP> 594 ImplSP FormatManager::Get(ValueObject &valobj, 595 lldb::DynamicValueType use_dynamic) { 596 FormattersMatchData match_data(valobj, use_dynamic); 597 if (ImplSP retval_sp = GetCached<ImplSP>(match_data)) 598 return retval_sp; 599 600 Log *log = GetLog(LLDBLog::DataFormatters); 601 602 LLDB_LOGF(log, "[%s] Search failed. Giving language a chance.", __FUNCTION__); 603 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 604 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 605 ImplSP retval_sp; 606 if (lang_category->Get(match_data, retval_sp)) 607 if (retval_sp) { 608 LLDB_LOGF(log, "[%s] Language search success. Returning.", 609 __FUNCTION__); 610 return retval_sp; 611 } 612 } 613 } 614 615 LLDB_LOGF(log, "[%s] Search failed. Giving hardcoded a chance.", 616 __FUNCTION__); 617 return GetHardcoded<ImplSP>(match_data); 618 } 619 620 template <typename ImplSP> 621 ImplSP FormatManager::GetCached(FormattersMatchData &match_data) { 622 ImplSP retval_sp; 623 Log *log = GetLog(LLDBLog::DataFormatters); 624 if (match_data.GetTypeForCache()) { 625 LLDB_LOGF(log, "\n\n[%s] Looking into cache for type %s", __FUNCTION__, 626 match_data.GetTypeForCache().AsCString("<invalid>")); 627 if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp)) { 628 if (log) { 629 LLDB_LOGF(log, "[%s] Cache search success. Returning.", __FUNCTION__); 630 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 631 m_format_cache.GetCacheHits(), 632 m_format_cache.GetCacheMisses()); 633 } 634 return retval_sp; 635 } 636 LLDB_LOGF(log, "[%s] Cache search failed. Going normal route", 637 __FUNCTION__); 638 } 639 640 m_categories_map.Get(match_data, retval_sp); 641 if (match_data.GetTypeForCache() && (!retval_sp || !retval_sp->NonCacheable())) { 642 LLDB_LOGF(log, "[%s] Caching %p for type %s", __FUNCTION__, 643 static_cast<void *>(retval_sp.get()), 644 match_data.GetTypeForCache().AsCString("<invalid>")); 645 m_format_cache.Set(match_data.GetTypeForCache(), retval_sp); 646 } 647 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 648 m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses()); 649 return retval_sp; 650 } 651 652 lldb::TypeFormatImplSP 653 FormatManager::GetFormat(ValueObject &valobj, 654 lldb::DynamicValueType use_dynamic) { 655 return Get<lldb::TypeFormatImplSP>(valobj, use_dynamic); 656 } 657 658 lldb::TypeSummaryImplSP 659 FormatManager::GetSummaryFormat(ValueObject &valobj, 660 lldb::DynamicValueType use_dynamic) { 661 return Get<lldb::TypeSummaryImplSP>(valobj, use_dynamic); 662 } 663 664 lldb::SyntheticChildrenSP 665 FormatManager::GetSyntheticChildren(ValueObject &valobj, 666 lldb::DynamicValueType use_dynamic) { 667 return Get<lldb::SyntheticChildrenSP>(valobj, use_dynamic); 668 } 669 670 FormatManager::FormatManager() 671 : m_last_revision(0), m_format_cache(), m_language_categories_mutex(), 672 m_language_categories_map(), m_named_summaries_map(this), 673 m_categories_map(this), m_default_category_name(ConstString("default")), 674 m_system_category_name(ConstString("system")), 675 m_vectortypes_category_name(ConstString("VectorTypes")) { 676 LoadSystemFormatters(); 677 LoadVectorFormatters(); 678 679 EnableCategory(m_vectortypes_category_name, TypeCategoryMap::Last, 680 lldb::eLanguageTypeObjC_plus_plus); 681 EnableCategory(m_system_category_name, TypeCategoryMap::Last, 682 lldb::eLanguageTypeObjC_plus_plus); 683 } 684 685 void FormatManager::LoadSystemFormatters() { 686 TypeSummaryImpl::Flags string_flags; 687 string_flags.SetCascades(true) 688 .SetSkipPointers(true) 689 .SetSkipReferences(false) 690 .SetDontShowChildren(true) 691 .SetDontShowValue(false) 692 .SetShowMembersOneLiner(false) 693 .SetHideItemNames(false); 694 695 TypeSummaryImpl::Flags string_array_flags; 696 string_array_flags.SetCascades(true) 697 .SetSkipPointers(true) 698 .SetSkipReferences(false) 699 .SetDontShowChildren(true) 700 .SetDontShowValue(true) 701 .SetShowMembersOneLiner(false) 702 .SetHideItemNames(false); 703 704 lldb::TypeSummaryImplSP string_format( 705 new StringSummaryFormat(string_flags, "${var%s}")); 706 707 lldb::TypeSummaryImplSP string_array_format( 708 new StringSummaryFormat(string_array_flags, "${var%char[]}")); 709 710 TypeCategoryImpl::SharedPointer sys_category_sp = 711 GetCategory(m_system_category_name); 712 713 sys_category_sp->AddTypeSummary(R"(^(unsigned )?char ?(\*|\[\])$)", 714 eFormatterMatchRegex, string_format); 715 716 sys_category_sp->AddTypeSummary(R"(^((un)?signed )?char ?\[[0-9]+\]$)", 717 eFormatterMatchRegex, string_array_format); 718 719 lldb::TypeSummaryImplSP ostype_summary( 720 new StringSummaryFormat(TypeSummaryImpl::Flags() 721 .SetCascades(false) 722 .SetSkipPointers(true) 723 .SetSkipReferences(true) 724 .SetDontShowChildren(true) 725 .SetDontShowValue(false) 726 .SetShowMembersOneLiner(false) 727 .SetHideItemNames(false), 728 "${var%O}")); 729 730 sys_category_sp->AddTypeSummary("OSType", eFormatterMatchExact, 731 ostype_summary); 732 733 TypeFormatImpl::Flags fourchar_flags; 734 fourchar_flags.SetCascades(true).SetSkipPointers(true).SetSkipReferences( 735 true); 736 737 AddFormat(sys_category_sp, lldb::eFormatOSType, ConstString("FourCharCode"), 738 fourchar_flags); 739 } 740 741 void FormatManager::LoadVectorFormatters() { 742 TypeCategoryImpl::SharedPointer vectors_category_sp = 743 GetCategory(m_vectortypes_category_name); 744 745 TypeSummaryImpl::Flags vector_flags; 746 vector_flags.SetCascades(true) 747 .SetSkipPointers(true) 748 .SetSkipReferences(false) 749 .SetDontShowChildren(true) 750 .SetDontShowValue(false) 751 .SetShowMembersOneLiner(true) 752 .SetHideItemNames(true); 753 754 AddStringSummary(vectors_category_sp, "${var.uint128}", 755 ConstString("builtin_type_vec128"), vector_flags); 756 AddStringSummary(vectors_category_sp, "", ConstString("float[4]"), 757 vector_flags); 758 AddStringSummary(vectors_category_sp, "", ConstString("int32_t[4]"), 759 vector_flags); 760 AddStringSummary(vectors_category_sp, "", ConstString("int16_t[8]"), 761 vector_flags); 762 AddStringSummary(vectors_category_sp, "", ConstString("vDouble"), 763 vector_flags); 764 AddStringSummary(vectors_category_sp, "", ConstString("vFloat"), 765 vector_flags); 766 AddStringSummary(vectors_category_sp, "", ConstString("vSInt8"), 767 vector_flags); 768 AddStringSummary(vectors_category_sp, "", ConstString("vSInt16"), 769 vector_flags); 770 AddStringSummary(vectors_category_sp, "", ConstString("vSInt32"), 771 vector_flags); 772 AddStringSummary(vectors_category_sp, "", ConstString("vUInt16"), 773 vector_flags); 774 AddStringSummary(vectors_category_sp, "", ConstString("vUInt8"), 775 vector_flags); 776 AddStringSummary(vectors_category_sp, "", ConstString("vUInt16"), 777 vector_flags); 778 AddStringSummary(vectors_category_sp, "", ConstString("vUInt32"), 779 vector_flags); 780 AddStringSummary(vectors_category_sp, "", ConstString("vBool32"), 781 vector_flags); 782 } 783