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