1 //===-- BlockPointer.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 "BlockPointer.h" 10 11 #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h" 12 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" 13 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 14 #include "lldb/Core/ValueObject.h" 15 #include "lldb/DataFormatters/FormattersHelpers.h" 16 #include "lldb/Symbol/CompilerType.h" 17 #include "lldb/Symbol/TypeSystem.h" 18 #include "lldb/Target/Target.h" 19 #include "lldb/Utility/LLDBAssert.h" 20 #include "lldb/Utility/Log.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 using namespace lldb_private::formatters; 25 26 namespace lldb_private { 27 namespace formatters { 28 29 class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd { 30 public: 31 BlockPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) 32 : SyntheticChildrenFrontEnd(*valobj_sp), m_block_struct_type() { 33 CompilerType block_pointer_type(m_backend.GetCompilerType()); 34 CompilerType function_pointer_type; 35 block_pointer_type.IsBlockPointerType(&function_pointer_type); 36 37 TargetSP target_sp(m_backend.GetTargetSP()); 38 39 if (!target_sp) { 40 return; 41 } 42 43 auto type_system_or_err = target_sp->GetScratchTypeSystemForLanguage( 44 lldb::eLanguageTypeC_plus_plus); 45 if (auto err = type_system_or_err.takeError()) { 46 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), std::move(err), 47 "Failed to get scratch TypeSystemClang"); 48 return; 49 } 50 51 TypeSystemClang *clang_ast_context = 52 llvm::cast<TypeSystemClang>(block_pointer_type.GetTypeSystem()); 53 54 std::shared_ptr<ClangASTImporter> clang_ast_importer; 55 auto *state = target_sp->GetPersistentExpressionStateForLanguage( 56 lldb::eLanguageTypeC_plus_plus); 57 if (state) { 58 auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state); 59 clang_ast_importer = persistent_vars->GetClangASTImporter(); 60 } 61 62 if (!clang_ast_importer) { 63 return; 64 } 65 66 const char *const isa_name("__isa"); 67 const CompilerType isa_type = 68 clang_ast_context->GetBasicType(lldb::eBasicTypeObjCClass); 69 const char *const flags_name("__flags"); 70 const CompilerType flags_type = 71 clang_ast_context->GetBasicType(lldb::eBasicTypeInt); 72 const char *const reserved_name("__reserved"); 73 const CompilerType reserved_type = 74 clang_ast_context->GetBasicType(lldb::eBasicTypeInt); 75 const char *const FuncPtr_name("__FuncPtr"); 76 77 m_block_struct_type = clang_ast_context->CreateStructForIdentifier( 78 ConstString(), {{isa_name, isa_type}, 79 {flags_name, flags_type}, 80 {reserved_name, reserved_type}, 81 {FuncPtr_name, function_pointer_type}}); 82 } 83 84 ~BlockPointerSyntheticFrontEnd() override = default; 85 86 size_t CalculateNumChildren() override { 87 const bool omit_empty_base_classes = false; 88 return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr); 89 } 90 91 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override { 92 if (!m_block_struct_type.IsValid()) { 93 return lldb::ValueObjectSP(); 94 } 95 96 if (idx >= CalculateNumChildren()) { 97 return lldb::ValueObjectSP(); 98 } 99 100 const bool thread_and_frame_only_if_stopped = true; 101 ExecutionContext exe_ctx = m_backend.GetExecutionContextRef().Lock( 102 thread_and_frame_only_if_stopped); 103 const bool transparent_pointers = false; 104 const bool omit_empty_base_classes = false; 105 const bool ignore_array_bounds = false; 106 ValueObject *value_object = nullptr; 107 108 std::string child_name; 109 uint32_t child_byte_size = 0; 110 int32_t child_byte_offset = 0; 111 uint32_t child_bitfield_bit_size = 0; 112 uint32_t child_bitfield_bit_offset = 0; 113 bool child_is_base_class = false; 114 bool child_is_deref_of_parent = false; 115 uint64_t language_flags = 0; 116 117 const CompilerType child_type = 118 m_block_struct_type.GetChildCompilerTypeAtIndex( 119 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes, 120 ignore_array_bounds, child_name, child_byte_size, child_byte_offset, 121 child_bitfield_bit_size, child_bitfield_bit_offset, 122 child_is_base_class, child_is_deref_of_parent, value_object, 123 language_flags); 124 125 ValueObjectSP struct_pointer_sp = 126 m_backend.Cast(m_block_struct_type.GetPointerType()); 127 128 if (!struct_pointer_sp) { 129 return lldb::ValueObjectSP(); 130 } 131 132 Status err; 133 ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err); 134 135 if (!struct_sp || !err.Success()) { 136 return lldb::ValueObjectSP(); 137 } 138 139 ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset( 140 child_byte_offset, child_type, true, 141 ConstString(child_name.c_str(), child_name.size()))); 142 143 return child_sp; 144 } 145 146 // return true if this object is now safe to use forever without ever 147 // updating again; the typical (and tested) answer here is 'false' 148 bool Update() override { return false; } 149 150 // maybe return false if the block pointer is, say, null 151 bool MightHaveChildren() override { return true; } 152 153 size_t GetIndexOfChildWithName(ConstString name) override { 154 if (!m_block_struct_type.IsValid()) 155 return UINT32_MAX; 156 157 const bool omit_empty_base_classes = false; 158 return m_block_struct_type.GetIndexOfChildWithName(name.AsCString(), 159 omit_empty_base_classes); 160 } 161 162 private: 163 CompilerType m_block_struct_type; 164 }; 165 166 } // namespace formatters 167 } // namespace lldb_private 168 169 bool lldb_private::formatters::BlockPointerSummaryProvider( 170 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) { 171 lldb_private::SyntheticChildrenFrontEnd *synthetic_children = 172 BlockPointerSyntheticFrontEndCreator(nullptr, valobj.GetSP()); 173 if (!synthetic_children) { 174 return false; 175 } 176 177 synthetic_children->Update(); 178 179 static const ConstString s_FuncPtr_name("__FuncPtr"); 180 181 lldb::ValueObjectSP child_sp = synthetic_children->GetChildAtIndex( 182 synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name)); 183 184 if (!child_sp) { 185 return false; 186 } 187 188 lldb::ValueObjectSP qualified_child_representation_sp = 189 child_sp->GetQualifiedRepresentationIfAvailable( 190 lldb::eDynamicDontRunTarget, true); 191 192 const char *child_value = 193 qualified_child_representation_sp->GetValueAsCString(); 194 195 s.Printf("%s", child_value); 196 197 return true; 198 } 199 200 lldb_private::SyntheticChildrenFrontEnd * 201 lldb_private::formatters::BlockPointerSyntheticFrontEndCreator( 202 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { 203 if (!valobj_sp) 204 return nullptr; 205 return new BlockPointerSyntheticFrontEnd(valobj_sp); 206 } 207