blob: 43b2cadb19197d66132f321da529b1a945746c6b [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- BlockPointer.cpp --------------------------------------------------===//
Sean Callananc530ba92016-05-02 21:15:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Callananc530ba92016-05-02 21:15:31 +00006//
7//===----------------------------------------------------------------------===//
8
Sean Callananc530ba92016-05-02 21:15:31 +00009#include "BlockPointer.h"
10
Alex Langfordc4f6fbe2020-01-23 13:49:54 -080011#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Sean Callananc530ba92016-05-02 21:15:31 +000012#include "lldb/Core/ValueObject.h"
13#include "lldb/DataFormatters/FormattersHelpers.h"
Sean Callananc530ba92016-05-02 21:15:31 +000014#include "lldb/Symbol/ClangASTImporter.h"
15#include "lldb/Symbol/CompilerType.h"
16#include "lldb/Symbol/TypeSystem.h"
Alex Langfordc4f6fbe2020-01-23 13:49:54 -080017#include "lldb/Symbol/TypeSystemClang.h"
Sean Callananc530ba92016-05-02 21:15:31 +000018#include "lldb/Target/Target.h"
Sean Callananc530ba92016-05-02 21:15:31 +000019#include "lldb/Utility/LLDBAssert.h"
Alex Langford0e252e32019-07-30 22:12:34 +000020#include "lldb/Utility/Log.h"
Sean Callananc530ba92016-05-02 21:15:31 +000021
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::formatters;
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026namespace lldb_private {
27namespace formatters {
Sean Callananc530ba92016-05-02 21:15:31 +000028
Kate Stoneb9c1b512016-09-06 20:57:50 +000029class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
Sean Callananc530ba92016-05-02 21:15:31 +000030public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 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 Callananc530ba92016-05-02 21:15:31 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 TargetSP target_sp(m_backend.GetTargetSP());
38
39 if (!target_sp) {
40 return;
Sean Callananc530ba92016-05-02 21:15:31 +000041 }
42
Alex Langford0e252e32019-07-30 22:12:34 +000043 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 Isemann6e3b0cc2020-01-23 10:04:13 +010048 std::move(err), "Failed to get scratch TypeSystemClang");
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 return;
Sean Callananc530ba92016-05-02 21:15:31 +000050 }
51
Raphael Isemann6e3b0cc2020-01-23 10:04:13 +010052 TypeSystemClang *clang_ast_context =
53 llvm::dyn_cast<TypeSystemClang>(&type_system_or_err.get());
Sean Callananc530ba92016-05-02 21:15:31 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 if (!clang_ast_context) {
56 return;
Sean Callananc530ba92016-05-02 21:15:31 +000057 }
58
Alex Langfordc4f6fbe2020-01-23 13:49:54 -080059 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 Stoneb9c1b512016-09-06 20:57:50 +000066
67 if (!clang_ast_importer) {
68 return;
Sean Callananc530ba92016-05-02 21:15:31 +000069 }
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 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 Prantleca07c52018-11-05 20:49:07 +000095 return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 }
97
98 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
99 if (!m_block_struct_type.IsValid()) {
100 return lldb::ValueObjectSP();
Sean Callananc530ba92016-05-02 21:15:31 +0000101 }
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (idx >= CalculateNumChildren()) {
104 return lldb::ValueObjectSP();
Sean Callananc530ba92016-05-02 21:15:31 +0000105 }
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 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 Turner97206d52017-05-12 04:51:55 +0000139 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 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 Prantl05097242018-04-30 16:49:04 +0000153 // 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 Stoneb9c1b512016-09-06 20:57:50 +0000155 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 Prantl0e4c4822019-03-06 21:22:25 +0000160 size_t GetIndexOfChildWithName(ConstString name) override {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 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 Callananc530ba92016-05-02 21:15:31 +0000169private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 CompilerType m_block_struct_type;
Sean Callananc530ba92016-05-02 21:15:31 +0000171};
172
173} // namespace formatters
174} // namespace lldb_private
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176bool 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 Callananc530ba92016-05-02 21:15:31 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 synthetic_children->Update();
Sean Callananc530ba92016-05-02 21:15:31 +0000185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 static const ConstString s_FuncPtr_name("__FuncPtr");
Sean Callananc530ba92016-05-02 21:15:31 +0000187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 lldb::ValueObjectSP child_sp = synthetic_children->GetChildAtIndex(
189 synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name));
Sean Callananc530ba92016-05-02 21:15:31 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 if (!child_sp) {
192 return false;
193 }
Sean Callananc530ba92016-05-02 21:15:31 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 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 Callananc530ba92016-05-02 21:15:31 +0000205}
206
207lldb_private::SyntheticChildrenFrontEnd *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208lldb_private::formatters::BlockPointerSyntheticFrontEndCreator(
209 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
210 if (!valobj_sp)
211 return nullptr;
212 return new BlockPointerSyntheticFrontEnd(valobj_sp);
Sean Callananc530ba92016-05-02 21:15:31 +0000213}