LLDB mainline
BlockPointer.cpp
Go to the documentation of this file.
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
17#include "lldb/Target/Target.h"
20#include "lldb/Utility/Log.h"
23#include "llvm/Support/ErrorExtras.h"
24
25using namespace lldb;
26using namespace lldb_private;
27using namespace lldb_private::formatters;
28
29namespace lldb_private {
30namespace formatters {
31
33public:
36 CompilerType block_pointer_type(m_backend.GetCompilerType());
37 CompilerType function_pointer_type;
38 block_pointer_type.IsBlockPointerType(&function_pointer_type);
39
40 TargetSP target_sp(m_backend.GetTargetSP());
41
42 if (!target_sp) {
43 return;
44 }
45
46 auto type_system_or_err = target_sp->GetScratchTypeSystemForLanguage(
48 if (auto err = type_system_or_err.takeError()) {
50 "Failed to get scratch TypeSystemClang: {0}");
51 return;
52 }
53
54 auto clang_ast_context =
55 block_pointer_type.GetTypeSystem<TypeSystemClang>();
56 if (!clang_ast_context)
57 return;
58
59 const char *const isa_name("__isa");
60 const CompilerType isa_type =
61 clang_ast_context->GetBasicType(lldb::eBasicTypeObjCClass);
62 const char *const flags_name("__flags");
63 const CompilerType flags_type =
64 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
65 const char *const reserved_name("__reserved");
66 const CompilerType reserved_type =
67 clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
68 const char *const FuncPtr_name("__FuncPtr");
69
70 m_block_struct_type = clang_ast_context->CreateStructForIdentifier(
71 llvm::StringRef(), {{isa_name, isa_type},
72 {flags_name, flags_type},
73 {reserved_name, reserved_type},
74 {FuncPtr_name, function_pointer_type}});
75 }
76
77 ~BlockPointerSyntheticFrontEnd() override = default;
78
79 llvm::Expected<uint32_t> CalculateNumChildren() override {
80 const bool omit_empty_base_classes = false;
81 return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr);
82 }
83
84 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
85 if (!m_block_struct_type.IsValid()) {
86 return lldb::ValueObjectSP();
87 }
88
90 return lldb::ValueObjectSP();
91 }
92
93 const bool thread_and_frame_only_if_stopped = true;
94 ExecutionContext exe_ctx = m_backend.GetExecutionContextRef().Lock(
95 thread_and_frame_only_if_stopped);
96 const bool transparent_pointers = false;
97 const bool omit_empty_base_classes = false;
98 const bool ignore_array_bounds = false;
99 ValueObject *value_object = nullptr;
100
101 std::string child_name;
102 uint32_t child_byte_size = 0;
103 int32_t child_byte_offset = 0;
104 uint32_t child_bitfield_bit_size = 0;
105 uint32_t child_bitfield_bit_offset = 0;
106 bool child_is_base_class = false;
107 bool child_is_deref_of_parent = false;
108 uint64_t language_flags = 0;
109
110 auto child_type_or_err = m_block_struct_type.GetChildCompilerTypeAtIndex(
111 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
112 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
113 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
114 child_is_deref_of_parent, value_object, language_flags);
115 if (!child_type_or_err)
118 Status::FromError(child_type_or_err.takeError()));
119 CompilerType child_type = *child_type_or_err;
120
121 ValueObjectSP struct_pointer_sp =
122 m_backend.Cast(m_block_struct_type.GetPointerType());
123
124 if (!struct_pointer_sp) {
125 return lldb::ValueObjectSP();
126 }
127
128 Status err;
129 ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err);
130
131 if (!struct_sp || !err.Success()) {
132 return lldb::ValueObjectSP();
133 }
134
135 ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset(
136 child_byte_offset, child_type, true,
137 ConstString(child_name.c_str(), child_name.size())));
138
139 return child_sp;
140 }
141
142 // return true if this object is now safe to use forever without ever
143 // updating again; the typical (and tested) answer here is 'false'
147
148 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
149 if (!m_block_struct_type.IsValid())
150 return llvm::createStringErrorV("type has no child named '{0}'", name);
151
152 const bool omit_empty_base_classes = false;
153 return m_block_struct_type.GetIndexOfChildWithName(name.AsCString(nullptr),
154 omit_empty_base_classes);
155 }
156
157private:
159};
160
161} // namespace formatters
162} // namespace lldb_private
163
165 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
166 lldb_private::SyntheticChildrenFrontEnd *synthetic_children =
168 if (!synthetic_children) {
169 return false;
170 }
171
172 synthetic_children->Update();
173
174 static const ConstString s_FuncPtr_name("__FuncPtr");
175
176 auto index_or_err =
177 synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name);
178
179 if (!index_or_err) {
180 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), index_or_err.takeError(),
181 "{0}");
182 return false;
183 }
184
185 lldb::ValueObjectSP child_sp =
186 synthetic_children->GetChildAtIndex(*index_or_err);
187
188 if (!child_sp) {
189 return false;
190 }
191
192 lldb::ValueObjectSP qualified_child_representation_sp =
193 child_sp->GetQualifiedRepresentationIfAvailable(
195
196 const char *child_value =
197 qualified_child_representation_sp->GetValueAsCString();
198
199 s.Printf("%s", child_value);
200
201 return true;
202}
203
207 if (!valobj_sp)
208 return nullptr;
209 return new BlockPointerSyntheticFrontEnd(valobj_sp);
210}
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
Generic representation of a type in a programming language.
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
bool IsBlockPointerType(CompilerType *function_pointer_type_ptr=nullptr) const
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
An error handling class.
Definition Status.h:118
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
bool Success() const
Test for success condition.
Definition Status.cpp:303
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
uint32_t CalculateNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx)=0
virtual lldb::ChildCacheState Update()=0
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
SyntheticChildrenFrontEnd(ValueObject &backend)
virtual llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name)
Determine the index of a named child.
A TypeSystem implementation based on Clang.
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address=LLDB_INVALID_ADDRESS)
lldb::ValueObjectSP GetSP()
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
BlockPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
llvm::Expected< uint32_t > CalculateNumChildren() override
SyntheticChildrenFrontEnd * BlockPointerSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
bool BlockPointerSummaryProvider(ValueObject &, Stream &, const TypeSummaryOptions &)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
@ eBasicTypeObjCClass
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Target > TargetSP
@ eDynamicDontRunTarget