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