1 //===-- FormatManager.cpp ----------------------------------------*- C++-*-===// 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 "llvm/ADT/STLExtras.h" 12 13 14 #include "lldb/Core/Debugger.h" 15 #include "lldb/DataFormatters/FormattersHelpers.h" 16 #include "lldb/DataFormatters/LanguageCategory.h" 17 #include "lldb/Target/ExecutionContext.h" 18 #include "lldb/Target/Language.h" 19 #include "lldb/Utility/Log.h" 20 21 using namespace lldb; 22 using namespace lldb_private; 23 using namespace lldb_private::formatters; 24 25 struct FormatInfo { 26 Format format; 27 const char format_char; // One or more format characters that can be used for 28 // this format. 29 const char *format_name; // Long format name that can be used to specify the 30 // current format 31 }; 32 33 static constexpr FormatInfo g_format_infos[] = { 34 {eFormatDefault, '\0', "default"}, 35 {eFormatBoolean, 'B', "boolean"}, 36 {eFormatBinary, 'b', "binary"}, 37 {eFormatBytes, 'y', "bytes"}, 38 {eFormatBytesWithASCII, 'Y', "bytes with ASCII"}, 39 {eFormatChar, 'c', "character"}, 40 {eFormatCharPrintable, 'C', "printable character"}, 41 {eFormatComplexFloat, 'F', "complex float"}, 42 {eFormatCString, 's', "c-string"}, 43 {eFormatDecimal, 'd', "decimal"}, 44 {eFormatEnum, 'E', "enumeration"}, 45 {eFormatHex, 'x', "hex"}, 46 {eFormatHexUppercase, 'X', "uppercase hex"}, 47 {eFormatFloat, 'f', "float"}, 48 {eFormatOctal, 'o', "octal"}, 49 {eFormatOSType, 'O', "OSType"}, 50 {eFormatUnicode16, 'U', "unicode16"}, 51 {eFormatUnicode32, '\0', "unicode32"}, 52 {eFormatUnsigned, 'u', "unsigned decimal"}, 53 {eFormatPointer, 'p', "pointer"}, 54 {eFormatVectorOfChar, '\0', "char[]"}, 55 {eFormatVectorOfSInt8, '\0', "int8_t[]"}, 56 {eFormatVectorOfUInt8, '\0', "uint8_t[]"}, 57 {eFormatVectorOfSInt16, '\0', "int16_t[]"}, 58 {eFormatVectorOfUInt16, '\0', "uint16_t[]"}, 59 {eFormatVectorOfSInt32, '\0', "int32_t[]"}, 60 {eFormatVectorOfUInt32, '\0', "uint32_t[]"}, 61 {eFormatVectorOfSInt64, '\0', "int64_t[]"}, 62 {eFormatVectorOfUInt64, '\0', "uint64_t[]"}, 63 {eFormatVectorOfFloat16, '\0', "float16[]"}, 64 {eFormatVectorOfFloat32, '\0', "float32[]"}, 65 {eFormatVectorOfFloat64, '\0', "float64[]"}, 66 {eFormatVectorOfUInt128, '\0', "uint128_t[]"}, 67 {eFormatComplexInteger, 'I', "complex integer"}, 68 {eFormatCharArray, 'a', "character array"}, 69 {eFormatAddressInfo, 'A', "address"}, 70 {eFormatHexFloat, '\0', "hex float"}, 71 {eFormatInstruction, 'i', "instruction"}, 72 {eFormatVoid, 'v', "void"}, 73 {eFormatUnicode8, 'u', "unicode8"}, 74 }; 75 76 static_assert((sizeof(g_format_infos) / sizeof(g_format_infos[0])) == 77 kNumFormats, 78 "All formats must have a corresponding info entry."); 79 80 static uint32_t g_num_format_infos = llvm::array_lengthof(g_format_infos); 81 82 static bool GetFormatFromFormatChar(char format_char, Format &format) { 83 for (uint32_t i = 0; i < g_num_format_infos; ++i) { 84 if (g_format_infos[i].format_char == format_char) { 85 format = g_format_infos[i].format; 86 return true; 87 } 88 } 89 format = eFormatInvalid; 90 return false; 91 } 92 93 static bool GetFormatFromFormatName(const char *format_name, 94 bool partial_match_ok, Format &format) { 95 uint32_t i; 96 for (i = 0; i < g_num_format_infos; ++i) { 97 if (strcasecmp(g_format_infos[i].format_name, format_name) == 0) { 98 format = g_format_infos[i].format; 99 return true; 100 } 101 } 102 103 if (partial_match_ok) { 104 for (i = 0; i < g_num_format_infos; ++i) { 105 if (strcasestr(g_format_infos[i].format_name, format_name) == 106 g_format_infos[i].format_name) { 107 format = g_format_infos[i].format; 108 return true; 109 } 110 } 111 } 112 format = eFormatInvalid; 113 return false; 114 } 115 116 void FormatManager::Changed() { 117 ++m_last_revision; 118 m_format_cache.Clear(); 119 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 120 for (auto &iter : m_language_categories_map) { 121 if (iter.second) 122 iter.second->GetFormatCache().Clear(); 123 } 124 } 125 126 bool FormatManager::GetFormatFromCString(const char *format_cstr, 127 bool partial_match_ok, 128 lldb::Format &format) { 129 bool success = false; 130 if (format_cstr && format_cstr[0]) { 131 if (format_cstr[1] == '\0') { 132 success = GetFormatFromFormatChar(format_cstr[0], format); 133 if (success) 134 return true; 135 } 136 137 success = GetFormatFromFormatName(format_cstr, partial_match_ok, format); 138 } 139 if (!success) 140 format = eFormatInvalid; 141 return success; 142 } 143 144 char FormatManager::GetFormatAsFormatChar(lldb::Format format) { 145 for (uint32_t i = 0; i < g_num_format_infos; ++i) { 146 if (g_format_infos[i].format == format) 147 return g_format_infos[i].format_char; 148 } 149 return '\0'; 150 } 151 152 const char *FormatManager::GetFormatAsCString(Format format) { 153 if (format >= eFormatDefault && format < kNumFormats) 154 return g_format_infos[format].format_name; 155 return nullptr; 156 } 157 158 void FormatManager::EnableAllCategories() { 159 m_categories_map.EnableAllCategories(); 160 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 161 for (auto &iter : m_language_categories_map) { 162 if (iter.second) 163 iter.second->Enable(); 164 } 165 } 166 167 void FormatManager::DisableAllCategories() { 168 m_categories_map.DisableAllCategories(); 169 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 170 for (auto &iter : m_language_categories_map) { 171 if (iter.second) 172 iter.second->Disable(); 173 } 174 } 175 176 void FormatManager::GetPossibleMatches( 177 ValueObject &valobj, CompilerType compiler_type, uint32_t reason, 178 lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries, 179 bool did_strip_ptr, bool did_strip_ref, bool did_strip_typedef, 180 bool root_level) { 181 compiler_type = compiler_type.GetTypeForFormatters(); 182 ConstString type_name(compiler_type.GetConstTypeName()); 183 if (valobj.GetBitfieldBitSize() > 0) { 184 StreamString sstring; 185 sstring.Printf("%s:%d", type_name.AsCString(), valobj.GetBitfieldBitSize()); 186 ConstString bitfieldname(sstring.GetString()); 187 entries.push_back( 188 {bitfieldname, 0, did_strip_ptr, did_strip_ref, did_strip_typedef}); 189 reason |= lldb_private::eFormatterChoiceCriterionStrippedBitField; 190 } 191 192 if (!compiler_type.IsMeaninglessWithoutDynamicResolution()) { 193 entries.push_back( 194 {type_name, reason, did_strip_ptr, did_strip_ref, did_strip_typedef}); 195 196 ConstString display_type_name(compiler_type.GetDisplayTypeName()); 197 if (display_type_name != type_name) 198 entries.push_back({display_type_name, reason, did_strip_ptr, 199 did_strip_ref, did_strip_typedef}); 200 } 201 202 for (bool is_rvalue_ref = true, j = true; 203 j && compiler_type.IsReferenceType(nullptr, &is_rvalue_ref); j = false) { 204 CompilerType non_ref_type = compiler_type.GetNonReferenceType(); 205 GetPossibleMatches( 206 valobj, non_ref_type, 207 reason | 208 lldb_private::eFormatterChoiceCriterionStrippedPointerReference, 209 use_dynamic, entries, did_strip_ptr, true, did_strip_typedef); 210 if (non_ref_type.IsTypedefType()) { 211 CompilerType deffed_referenced_type = non_ref_type.GetTypedefedType(); 212 deffed_referenced_type = 213 is_rvalue_ref ? deffed_referenced_type.GetRValueReferenceType() 214 : deffed_referenced_type.GetLValueReferenceType(); 215 GetPossibleMatches( 216 valobj, deffed_referenced_type, 217 reason | lldb_private::eFormatterChoiceCriterionNavigatedTypedefs, 218 use_dynamic, entries, did_strip_ptr, did_strip_ref, 219 true); // this is not exactly the usual meaning of stripping typedefs 220 } 221 } 222 223 if (compiler_type.IsPointerType()) { 224 CompilerType non_ptr_type = compiler_type.GetPointeeType(); 225 GetPossibleMatches( 226 valobj, non_ptr_type, 227 reason | 228 lldb_private::eFormatterChoiceCriterionStrippedPointerReference, 229 use_dynamic, entries, true, did_strip_ref, did_strip_typedef); 230 if (non_ptr_type.IsTypedefType()) { 231 CompilerType deffed_pointed_type = 232 non_ptr_type.GetTypedefedType().GetPointerType(); 233 GetPossibleMatches( 234 valobj, deffed_pointed_type, 235 reason | lldb_private::eFormatterChoiceCriterionNavigatedTypedefs, 236 use_dynamic, entries, did_strip_ptr, did_strip_ref, 237 true); // this is not exactly the usual meaning of stripping typedefs 238 } 239 } 240 241 for (lldb::LanguageType language_type : GetCandidateLanguages(valobj)) { 242 if (Language *language = Language::FindPlugin(language_type)) { 243 for (ConstString candidate : 244 language->GetPossibleFormattersMatches(valobj, use_dynamic)) { 245 entries.push_back( 246 {candidate, 247 reason | lldb_private::eFormatterChoiceCriterionLanguagePlugin, 248 did_strip_ptr, did_strip_ref, did_strip_typedef}); 249 } 250 } 251 } 252 253 // try to strip typedef chains 254 if (compiler_type.IsTypedefType()) { 255 CompilerType deffed_type = compiler_type.GetTypedefedType(); 256 GetPossibleMatches( 257 valobj, deffed_type, 258 reason | lldb_private::eFormatterChoiceCriterionNavigatedTypedefs, 259 use_dynamic, entries, did_strip_ptr, did_strip_ref, true); 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, reason, 274 use_dynamic, entries, did_strip_ptr, did_strip_ref, 275 did_strip_typedef); 276 } while (false); 277 278 // if all else fails, go to static type 279 if (valobj.IsDynamic()) { 280 lldb::ValueObjectSP static_value_sp(valobj.GetStaticValue()); 281 if (static_value_sp) 282 GetPossibleMatches( 283 *static_value_sp.get(), static_value_sp->GetCompilerType(), 284 reason | lldb_private::eFormatterChoiceCriterionWentToStaticValue, 285 use_dynamic, entries, did_strip_ptr, did_strip_ref, 286 did_strip_typedef, true); 287 } 288 } 289 } 290 291 lldb::TypeFormatImplSP 292 FormatManager::GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp) { 293 if (!type_sp) 294 return lldb::TypeFormatImplSP(); 295 lldb::TypeFormatImplSP format_chosen_sp; 296 uint32_t num_categories = m_categories_map.GetCount(); 297 lldb::TypeCategoryImplSP category_sp; 298 uint32_t prio_category = UINT32_MAX; 299 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 300 category_sp = GetCategoryAtIndex(category_id); 301 if (!category_sp->IsEnabled()) 302 continue; 303 lldb::TypeFormatImplSP format_current_sp = 304 category_sp->GetFormatForType(type_sp); 305 if (format_current_sp && 306 (format_chosen_sp.get() == nullptr || 307 (prio_category > category_sp->GetEnabledPosition()))) { 308 prio_category = category_sp->GetEnabledPosition(); 309 format_chosen_sp = format_current_sp; 310 } 311 } 312 return format_chosen_sp; 313 } 314 315 lldb::TypeSummaryImplSP 316 FormatManager::GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp) { 317 if (!type_sp) 318 return lldb::TypeSummaryImplSP(); 319 lldb::TypeSummaryImplSP summary_chosen_sp; 320 uint32_t num_categories = m_categories_map.GetCount(); 321 lldb::TypeCategoryImplSP category_sp; 322 uint32_t prio_category = UINT32_MAX; 323 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 324 category_sp = GetCategoryAtIndex(category_id); 325 if (!category_sp->IsEnabled()) 326 continue; 327 lldb::TypeSummaryImplSP summary_current_sp = 328 category_sp->GetSummaryForType(type_sp); 329 if (summary_current_sp && 330 (summary_chosen_sp.get() == nullptr || 331 (prio_category > category_sp->GetEnabledPosition()))) { 332 prio_category = category_sp->GetEnabledPosition(); 333 summary_chosen_sp = summary_current_sp; 334 } 335 } 336 return summary_chosen_sp; 337 } 338 339 lldb::TypeFilterImplSP 340 FormatManager::GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp) { 341 if (!type_sp) 342 return lldb::TypeFilterImplSP(); 343 lldb::TypeFilterImplSP filter_chosen_sp; 344 uint32_t num_categories = m_categories_map.GetCount(); 345 lldb::TypeCategoryImplSP category_sp; 346 uint32_t prio_category = UINT32_MAX; 347 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 348 category_sp = GetCategoryAtIndex(category_id); 349 if (!category_sp->IsEnabled()) 350 continue; 351 lldb::TypeFilterImplSP filter_current_sp( 352 (TypeFilterImpl *)category_sp->GetFilterForType(type_sp).get()); 353 if (filter_current_sp && 354 (filter_chosen_sp.get() == nullptr || 355 (prio_category > category_sp->GetEnabledPosition()))) { 356 prio_category = category_sp->GetEnabledPosition(); 357 filter_chosen_sp = filter_current_sp; 358 } 359 } 360 return filter_chosen_sp; 361 } 362 363 lldb::ScriptedSyntheticChildrenSP 364 FormatManager::GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp) { 365 if (!type_sp) 366 return lldb::ScriptedSyntheticChildrenSP(); 367 lldb::ScriptedSyntheticChildrenSP synth_chosen_sp; 368 uint32_t num_categories = m_categories_map.GetCount(); 369 lldb::TypeCategoryImplSP category_sp; 370 uint32_t prio_category = UINT32_MAX; 371 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 372 category_sp = GetCategoryAtIndex(category_id); 373 if (!category_sp->IsEnabled()) 374 continue; 375 lldb::ScriptedSyntheticChildrenSP synth_current_sp( 376 (ScriptedSyntheticChildren *)category_sp->GetSyntheticForType(type_sp) 377 .get()); 378 if (synth_current_sp && 379 (synth_chosen_sp.get() == nullptr || 380 (prio_category > category_sp->GetEnabledPosition()))) { 381 prio_category = category_sp->GetEnabledPosition(); 382 synth_chosen_sp = synth_current_sp; 383 } 384 } 385 return synth_chosen_sp; 386 } 387 388 lldb::TypeValidatorImplSP 389 FormatManager::GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp) { 390 if (!type_sp) 391 return lldb::TypeValidatorImplSP(); 392 lldb::TypeValidatorImplSP validator_chosen_sp; 393 uint32_t num_categories = m_categories_map.GetCount(); 394 lldb::TypeCategoryImplSP category_sp; 395 uint32_t prio_category = UINT32_MAX; 396 for (uint32_t category_id = 0; category_id < num_categories; category_id++) { 397 category_sp = GetCategoryAtIndex(category_id); 398 if (!category_sp->IsEnabled()) 399 continue; 400 lldb::TypeValidatorImplSP validator_current_sp( 401 category_sp->GetValidatorForType(type_sp).get()); 402 if (validator_current_sp && 403 (validator_chosen_sp.get() == nullptr || 404 (prio_category > category_sp->GetEnabledPosition()))) { 405 prio_category = category_sp->GetEnabledPosition(); 406 validator_chosen_sp = validator_current_sp; 407 } 408 } 409 return validator_chosen_sp; 410 } 411 412 void FormatManager::ForEachCategory(TypeCategoryMap::ForEachCallback callback) { 413 m_categories_map.ForEach(callback); 414 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 415 for (const auto &entry : m_language_categories_map) { 416 if (auto category_sp = entry.second->GetCategory()) { 417 if (!callback(category_sp)) 418 break; 419 } 420 } 421 } 422 423 lldb::TypeCategoryImplSP 424 FormatManager::GetCategory(ConstString category_name, bool can_create) { 425 if (!category_name) 426 return GetCategory(m_default_category_name); 427 lldb::TypeCategoryImplSP category; 428 if (m_categories_map.Get(category_name, category)) 429 return category; 430 431 if (!can_create) 432 return lldb::TypeCategoryImplSP(); 433 434 m_categories_map.Add( 435 category_name, 436 lldb::TypeCategoryImplSP(new TypeCategoryImpl(this, category_name))); 437 return GetCategory(category_name); 438 } 439 440 lldb::Format FormatManager::GetSingleItemFormat(lldb::Format vector_format) { 441 switch (vector_format) { 442 case eFormatVectorOfChar: 443 return eFormatCharArray; 444 445 case eFormatVectorOfSInt8: 446 case eFormatVectorOfSInt16: 447 case eFormatVectorOfSInt32: 448 case eFormatVectorOfSInt64: 449 return eFormatDecimal; 450 451 case eFormatVectorOfUInt8: 452 case eFormatVectorOfUInt16: 453 case eFormatVectorOfUInt32: 454 case eFormatVectorOfUInt64: 455 case eFormatVectorOfUInt128: 456 return eFormatHex; 457 458 case eFormatVectorOfFloat16: 459 case eFormatVectorOfFloat32: 460 case eFormatVectorOfFloat64: 461 return eFormatFloat; 462 463 default: 464 return lldb::eFormatInvalid; 465 } 466 } 467 468 bool FormatManager::ShouldPrintAsOneLiner(ValueObject &valobj) { 469 // if settings say no oneline whatsoever 470 if (valobj.GetTargetSP().get() && 471 !valobj.GetTargetSP()->GetDebugger().GetAutoOneLineSummaries()) 472 return false; // then don't oneline 473 474 // if this object has a summary, then ask the summary 475 if (valobj.GetSummaryFormat().get() != nullptr) 476 return valobj.GetSummaryFormat()->IsOneLiner(); 477 478 // no children, no party 479 if (valobj.GetNumChildren() == 0) 480 return false; 481 482 // ask the type if it has any opinion about this eLazyBoolCalculate == no 483 // opinion; other values should be self explanatory 484 CompilerType compiler_type(valobj.GetCompilerType()); 485 if (compiler_type.IsValid()) { 486 switch (compiler_type.ShouldPrintAsOneLiner(&valobj)) { 487 case eLazyBoolNo: 488 return false; 489 case eLazyBoolYes: 490 return true; 491 case eLazyBoolCalculate: 492 break; 493 } 494 } 495 496 size_t total_children_name_len = 0; 497 498 for (size_t idx = 0; idx < valobj.GetNumChildren(); idx++) { 499 bool is_synth_val = false; 500 ValueObjectSP child_sp(valobj.GetChildAtIndex(idx, true)); 501 // something is wrong here - bail out 502 if (!child_sp) 503 return false; 504 505 // also ask the child's type if it has any opinion 506 CompilerType child_compiler_type(child_sp->GetCompilerType()); 507 if (child_compiler_type.IsValid()) { 508 switch (child_compiler_type.ShouldPrintAsOneLiner(child_sp.get())) { 509 case eLazyBoolYes: 510 // an opinion of yes is only binding for the child, so keep going 511 case eLazyBoolCalculate: 512 break; 513 case eLazyBoolNo: 514 // but if the child says no, then it's a veto on the whole thing 515 return false; 516 } 517 } 518 519 // if we decided to define synthetic children for a type, we probably care 520 // enough to show them, but avoid nesting children in children 521 if (child_sp->GetSyntheticChildren().get() != nullptr) { 522 ValueObjectSP synth_sp(child_sp->GetSyntheticValue()); 523 // wait.. wat? just get out of here.. 524 if (!synth_sp) 525 return false; 526 // but if we only have them to provide a value, keep going 527 if (!synth_sp->MightHaveChildren() && 528 synth_sp->DoesProvideSyntheticValue()) 529 is_synth_val = true; 530 else 531 return false; 532 } 533 534 total_children_name_len += child_sp->GetName().GetLength(); 535 536 // 50 itself is a "randomly" chosen number - the idea is that 537 // overly long structs should not get this treatment 538 // FIXME: maybe make this a user-tweakable setting? 539 if (total_children_name_len > 50) 540 return false; 541 542 // if a summary is there.. 543 if (child_sp->GetSummaryFormat()) { 544 // and it wants children, then bail out 545 if (child_sp->GetSummaryFormat()->DoesPrintChildren(child_sp.get())) 546 return false; 547 } 548 549 // if this child has children.. 550 if (child_sp->GetNumChildren()) { 551 // ...and no summary... 552 // (if it had a summary and the summary wanted children, we would have 553 // bailed out anyway 554 // so this only makes us bail out if this has no summary and we would 555 // then print children) 556 if (!child_sp->GetSummaryFormat() && !is_synth_val) // but again only do 557 // that if not a 558 // synthetic valued 559 // child 560 return false; // then bail out 561 } 562 } 563 return true; 564 } 565 566 ConstString FormatManager::GetValidTypeName(ConstString type) { 567 return ::GetValidTypeName_Impl(type); 568 } 569 570 ConstString FormatManager::GetTypeForCache(ValueObject &valobj, 571 lldb::DynamicValueType use_dynamic) { 572 ValueObjectSP valobj_sp = valobj.GetQualifiedRepresentationIfAvailable( 573 use_dynamic, valobj.IsSynthetic()); 574 if (valobj_sp && valobj_sp->GetCompilerType().IsValid()) { 575 if (!valobj_sp->GetCompilerType().IsMeaninglessWithoutDynamicResolution()) 576 return valobj_sp->GetQualifiedTypeName(); 577 } 578 return ConstString(); 579 } 580 581 std::vector<lldb::LanguageType> 582 FormatManager::GetCandidateLanguages(ValueObject &valobj) { 583 lldb::LanguageType lang_type = valobj.GetObjectRuntimeLanguage(); 584 return GetCandidateLanguages(lang_type); 585 } 586 587 std::vector<lldb::LanguageType> 588 FormatManager::GetCandidateLanguages(lldb::LanguageType lang_type) { 589 switch (lang_type) { 590 case lldb::eLanguageTypeC: 591 case lldb::eLanguageTypeC89: 592 case lldb::eLanguageTypeC99: 593 case lldb::eLanguageTypeC11: 594 case lldb::eLanguageTypeC_plus_plus: 595 case lldb::eLanguageTypeC_plus_plus_03: 596 case lldb::eLanguageTypeC_plus_plus_11: 597 case lldb::eLanguageTypeC_plus_plus_14: 598 return {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC}; 599 default: 600 return {lang_type}; 601 } 602 } 603 604 LanguageCategory * 605 FormatManager::GetCategoryForLanguage(lldb::LanguageType lang_type) { 606 std::lock_guard<std::recursive_mutex> guard(m_language_categories_mutex); 607 auto iter = m_language_categories_map.find(lang_type), 608 end = m_language_categories_map.end(); 609 if (iter != end) 610 return iter->second.get(); 611 LanguageCategory *lang_category = new LanguageCategory(lang_type); 612 m_language_categories_map[lang_type] = 613 LanguageCategory::UniquePointer(lang_category); 614 return lang_category; 615 } 616 617 lldb::TypeFormatImplSP 618 FormatManager::GetHardcodedFormat(FormattersMatchData &match_data) { 619 TypeFormatImplSP retval_sp; 620 621 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 622 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 623 if (lang_category->GetHardcoded(*this, match_data, retval_sp)) 624 break; 625 } 626 } 627 628 return retval_sp; 629 } 630 631 lldb::TypeFormatImplSP 632 FormatManager::GetFormat(ValueObject &valobj, 633 lldb::DynamicValueType use_dynamic) { 634 FormattersMatchData match_data(valobj, use_dynamic); 635 636 TypeFormatImplSP retval; 637 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS)); 638 if (match_data.GetTypeForCache()) { 639 LLDB_LOGF(log, 640 "\n\n[FormatManager::GetFormat] Looking into cache for type %s", 641 match_data.GetTypeForCache().AsCString("<invalid>")); 642 if (m_format_cache.GetFormat(match_data.GetTypeForCache(), retval)) { 643 if (log) { 644 LLDB_LOGF( 645 log, "[FormatManager::GetFormat] Cache search success. Returning."); 646 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 647 m_format_cache.GetCacheHits(), 648 m_format_cache.GetCacheMisses()); 649 } 650 return retval; 651 } 652 LLDB_LOGF( 653 log, 654 "[FormatManager::GetFormat] Cache search failed. Going normal route"); 655 } 656 657 retval = m_categories_map.GetFormat(match_data); 658 if (!retval) { 659 LLDB_LOGF(log, 660 "[FormatManager::GetFormat] Search failed. Giving language a " 661 "chance."); 662 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 663 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 664 if (lang_category->Get(match_data, retval)) 665 break; 666 } 667 } 668 if (retval) { 669 LLDB_LOGF( 670 log, 671 "[FormatManager::GetFormat] Language search success. Returning."); 672 return retval; 673 } 674 } 675 if (!retval) { 676 LLDB_LOGF(log, "[FormatManager::GetFormat] Search failed. Giving hardcoded " 677 "a chance."); 678 retval = GetHardcodedFormat(match_data); 679 } 680 681 if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) { 682 LLDB_LOGF(log, "[FormatManager::GetFormat] Caching %p for type %s", 683 static_cast<void *>(retval.get()), 684 match_data.GetTypeForCache().AsCString("<invalid>")); 685 m_format_cache.SetFormat(match_data.GetTypeForCache(), retval); 686 } 687 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 688 m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses()); 689 return retval; 690 } 691 692 lldb::TypeSummaryImplSP 693 FormatManager::GetHardcodedSummaryFormat(FormattersMatchData &match_data) { 694 TypeSummaryImplSP retval_sp; 695 696 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 697 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 698 if (lang_category->GetHardcoded(*this, match_data, retval_sp)) 699 break; 700 } 701 } 702 703 return retval_sp; 704 } 705 706 lldb::TypeSummaryImplSP 707 FormatManager::GetSummaryFormat(ValueObject &valobj, 708 lldb::DynamicValueType use_dynamic) { 709 FormattersMatchData match_data(valobj, use_dynamic); 710 711 TypeSummaryImplSP retval; 712 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS)); 713 if (match_data.GetTypeForCache()) { 714 LLDB_LOGF(log, 715 "\n\n[FormatManager::GetSummaryFormat] Looking into cache " 716 "for type %s", 717 match_data.GetTypeForCache().AsCString("<invalid>")); 718 if (m_format_cache.GetSummary(match_data.GetTypeForCache(), retval)) { 719 if (log) { 720 LLDB_LOGF(log, 721 "[FormatManager::GetSummaryFormat] Cache search success. " 722 "Returning."); 723 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 724 m_format_cache.GetCacheHits(), 725 m_format_cache.GetCacheMisses()); 726 } 727 return retval; 728 } 729 LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Cache search failed. " 730 "Going normal route"); 731 } 732 733 retval = m_categories_map.GetSummaryFormat(match_data); 734 if (!retval) { 735 LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Search failed. Giving " 736 "language a chance."); 737 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 738 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 739 if (lang_category->Get(match_data, retval)) 740 break; 741 } 742 } 743 if (retval) { 744 LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Language search " 745 "success. Returning."); 746 return retval; 747 } 748 } 749 if (!retval) { 750 LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Search failed. Giving " 751 "hardcoded a chance."); 752 retval = GetHardcodedSummaryFormat(match_data); 753 } 754 755 if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) { 756 LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Caching %p for type %s", 757 static_cast<void *>(retval.get()), 758 match_data.GetTypeForCache().AsCString("<invalid>")); 759 m_format_cache.SetSummary(match_data.GetTypeForCache(), retval); 760 } 761 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 762 m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses()); 763 return retval; 764 } 765 766 lldb::SyntheticChildrenSP 767 FormatManager::GetHardcodedSyntheticChildren(FormattersMatchData &match_data) { 768 SyntheticChildrenSP retval_sp; 769 770 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 771 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 772 if (lang_category->GetHardcoded(*this, match_data, retval_sp)) 773 break; 774 } 775 } 776 777 return retval_sp; 778 } 779 780 lldb::SyntheticChildrenSP 781 FormatManager::GetSyntheticChildren(ValueObject &valobj, 782 lldb::DynamicValueType use_dynamic) { 783 FormattersMatchData match_data(valobj, use_dynamic); 784 785 SyntheticChildrenSP retval; 786 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS)); 787 if (match_data.GetTypeForCache()) { 788 LLDB_LOGF(log, 789 "\n\n[FormatManager::GetSyntheticChildren] Looking into " 790 "cache for type %s", 791 match_data.GetTypeForCache().AsCString("<invalid>")); 792 if (m_format_cache.GetSynthetic(match_data.GetTypeForCache(), retval)) { 793 if (log) { 794 LLDB_LOGF(log, "[FormatManager::GetSyntheticChildren] Cache search " 795 "success. Returning."); 796 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 797 m_format_cache.GetCacheHits(), 798 m_format_cache.GetCacheMisses()); 799 } 800 return retval; 801 } 802 LLDB_LOGF(log, "[FormatManager::GetSyntheticChildren] Cache search failed. " 803 "Going normal route"); 804 } 805 806 retval = m_categories_map.GetSyntheticChildren(match_data); 807 if (!retval) { 808 LLDB_LOGF(log, 809 "[FormatManager::GetSyntheticChildren] Search failed. Giving " 810 "language a chance."); 811 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 812 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 813 if (lang_category->Get(match_data, retval)) 814 break; 815 } 816 } 817 if (retval) { 818 LLDB_LOGF(log, "[FormatManager::GetSyntheticChildren] Language search " 819 "success. Returning."); 820 return retval; 821 } 822 } 823 if (!retval) { 824 LLDB_LOGF(log, 825 "[FormatManager::GetSyntheticChildren] Search failed. Giving " 826 "hardcoded a chance."); 827 retval = GetHardcodedSyntheticChildren(match_data); 828 } 829 830 if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) { 831 LLDB_LOGF(log, 832 "[FormatManager::GetSyntheticChildren] Caching %p for type %s", 833 static_cast<void *>(retval.get()), 834 match_data.GetTypeForCache().AsCString("<invalid>")); 835 m_format_cache.SetSynthetic(match_data.GetTypeForCache(), retval); 836 } 837 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 838 m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses()); 839 return retval; 840 } 841 842 lldb::TypeValidatorImplSP 843 FormatManager::GetValidator(ValueObject &valobj, 844 lldb::DynamicValueType use_dynamic) { 845 FormattersMatchData match_data(valobj, use_dynamic); 846 847 TypeValidatorImplSP retval; 848 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS)); 849 if (match_data.GetTypeForCache()) { 850 LLDB_LOGF( 851 log, "\n\n[FormatManager::GetValidator] Looking into cache for type %s", 852 match_data.GetTypeForCache().AsCString("<invalid>")); 853 if (m_format_cache.GetValidator(match_data.GetTypeForCache(), retval)) { 854 if (log) { 855 LLDB_LOGF( 856 log, 857 "[FormatManager::GetValidator] Cache search success. Returning."); 858 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 859 m_format_cache.GetCacheHits(), 860 m_format_cache.GetCacheMisses()); 861 } 862 return retval; 863 } 864 LLDB_LOGF(log, "[FormatManager::GetValidator] Cache search failed. Going " 865 "normal route"); 866 } 867 868 retval = m_categories_map.GetValidator(match_data); 869 if (!retval) { 870 LLDB_LOGF(log, "[FormatManager::GetValidator] Search failed. Giving " 871 "language a chance."); 872 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 873 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 874 if (lang_category->Get(match_data, retval)) 875 break; 876 } 877 } 878 if (retval) { 879 LLDB_LOGF(log, "[FormatManager::GetValidator] Language search success. " 880 "Returning."); 881 return retval; 882 } 883 } 884 if (!retval) { 885 LLDB_LOGF(log, "[FormatManager::GetValidator] Search failed. Giving " 886 "hardcoded a chance."); 887 retval = GetHardcodedValidator(match_data); 888 } 889 890 if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) { 891 LLDB_LOGF(log, "[FormatManager::GetValidator] Caching %p for type %s", 892 static_cast<void *>(retval.get()), 893 match_data.GetTypeForCache().AsCString("<invalid>")); 894 m_format_cache.SetValidator(match_data.GetTypeForCache(), retval); 895 } 896 LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}", 897 m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses()); 898 return retval; 899 } 900 901 lldb::TypeValidatorImplSP 902 FormatManager::GetHardcodedValidator(FormattersMatchData &match_data) { 903 TypeValidatorImplSP retval_sp; 904 905 for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) { 906 if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) { 907 if (lang_category->GetHardcoded(*this, match_data, retval_sp)) 908 break; 909 } 910 } 911 912 return retval_sp; 913 } 914 915 FormatManager::FormatManager() 916 : m_last_revision(0), m_format_cache(), m_language_categories_mutex(), 917 m_language_categories_map(), m_named_summaries_map(this), 918 m_categories_map(this), m_default_category_name(ConstString("default")), 919 m_system_category_name(ConstString("system")), 920 m_vectortypes_category_name(ConstString("VectorTypes")) { 921 LoadSystemFormatters(); 922 LoadVectorFormatters(); 923 924 EnableCategory(m_vectortypes_category_name, TypeCategoryMap::Last, 925 lldb::eLanguageTypeObjC_plus_plus); 926 EnableCategory(m_system_category_name, TypeCategoryMap::Last, 927 lldb::eLanguageTypeObjC_plus_plus); 928 } 929 930 void FormatManager::LoadSystemFormatters() { 931 TypeSummaryImpl::Flags string_flags; 932 string_flags.SetCascades(true) 933 .SetSkipPointers(true) 934 .SetSkipReferences(false) 935 .SetDontShowChildren(true) 936 .SetDontShowValue(false) 937 .SetShowMembersOneLiner(false) 938 .SetHideItemNames(false); 939 940 TypeSummaryImpl::Flags string_array_flags; 941 string_array_flags.SetCascades(true) 942 .SetSkipPointers(true) 943 .SetSkipReferences(false) 944 .SetDontShowChildren(true) 945 .SetDontShowValue(true) 946 .SetShowMembersOneLiner(false) 947 .SetHideItemNames(false); 948 949 lldb::TypeSummaryImplSP string_format( 950 new StringSummaryFormat(string_flags, "${var%s}")); 951 952 lldb::TypeSummaryImplSP string_array_format( 953 new StringSummaryFormat(string_array_flags, "${var%s}")); 954 955 RegularExpression any_size_char_arr(llvm::StringRef("char \\[[0-9]+\\]")); 956 957 TypeCategoryImpl::SharedPointer sys_category_sp = 958 GetCategory(m_system_category_name); 959 960 sys_category_sp->GetTypeSummariesContainer()->Add(ConstString("char *"), 961 string_format); 962 sys_category_sp->GetTypeSummariesContainer()->Add( 963 ConstString("unsigned char *"), string_format); 964 sys_category_sp->GetRegexTypeSummariesContainer()->Add( 965 std::move(any_size_char_arr), string_array_format); 966 967 lldb::TypeSummaryImplSP ostype_summary( 968 new StringSummaryFormat(TypeSummaryImpl::Flags() 969 .SetCascades(false) 970 .SetSkipPointers(true) 971 .SetSkipReferences(true) 972 .SetDontShowChildren(true) 973 .SetDontShowValue(false) 974 .SetShowMembersOneLiner(false) 975 .SetHideItemNames(false), 976 "${var%O}")); 977 978 sys_category_sp->GetTypeSummariesContainer()->Add(ConstString("OSType"), 979 ostype_summary); 980 981 TypeFormatImpl::Flags fourchar_flags; 982 fourchar_flags.SetCascades(true).SetSkipPointers(true).SetSkipReferences( 983 true); 984 985 AddFormat(sys_category_sp, lldb::eFormatOSType, ConstString("FourCharCode"), 986 fourchar_flags); 987 } 988 989 void FormatManager::LoadVectorFormatters() { 990 TypeCategoryImpl::SharedPointer vectors_category_sp = 991 GetCategory(m_vectortypes_category_name); 992 993 TypeSummaryImpl::Flags vector_flags; 994 vector_flags.SetCascades(true) 995 .SetSkipPointers(true) 996 .SetSkipReferences(false) 997 .SetDontShowChildren(true) 998 .SetDontShowValue(false) 999 .SetShowMembersOneLiner(true) 1000 .SetHideItemNames(true); 1001 1002 AddStringSummary(vectors_category_sp, "${var.uint128}", 1003 ConstString("builtin_type_vec128"), vector_flags); 1004 1005 AddStringSummary(vectors_category_sp, "", ConstString("float [4]"), 1006 vector_flags); 1007 AddStringSummary(vectors_category_sp, "", ConstString("int32_t [4]"), 1008 vector_flags); 1009 AddStringSummary(vectors_category_sp, "", ConstString("int16_t [8]"), 1010 vector_flags); 1011 AddStringSummary(vectors_category_sp, "", ConstString("vDouble"), 1012 vector_flags); 1013 AddStringSummary(vectors_category_sp, "", ConstString("vFloat"), 1014 vector_flags); 1015 AddStringSummary(vectors_category_sp, "", ConstString("vSInt8"), 1016 vector_flags); 1017 AddStringSummary(vectors_category_sp, "", ConstString("vSInt16"), 1018 vector_flags); 1019 AddStringSummary(vectors_category_sp, "", ConstString("vSInt32"), 1020 vector_flags); 1021 AddStringSummary(vectors_category_sp, "", ConstString("vUInt16"), 1022 vector_flags); 1023 AddStringSummary(vectors_category_sp, "", ConstString("vUInt8"), 1024 vector_flags); 1025 AddStringSummary(vectors_category_sp, "", ConstString("vUInt16"), 1026 vector_flags); 1027 AddStringSummary(vectors_category_sp, "", ConstString("vUInt32"), 1028 vector_flags); 1029 AddStringSummary(vectors_category_sp, "", ConstString("vBool32"), 1030 vector_flags); 1031 } 1032