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