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 llvm::Expected<ChildTypeInfo> child_or_err =
102 m_block_struct_type.GetChildAtIndex(&exe_ctx, idx, transparent_pointers,
103 omit_empty_base_classes,
104 ignore_array_bounds, value_object);
105 if (!child_or_err)
108 Status::FromError(child_or_err.takeError()));
109 const ChildTypeInfo &child = *child_or_err;
110
111 ValueObjectSP struct_pointer_sp =
112 m_backend.Cast(m_block_struct_type.GetPointerType());
113
114 if (!struct_pointer_sp) {
115 return lldb::ValueObjectSP();
116 }
117
118 Status err;
119 ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err);
120
121 if (!struct_sp || !err.Success()) {
122 return lldb::ValueObjectSP();
123 }
124
125 ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset(
126 child.byte_offset, child.type, true, ConstString(child.name)));
127
128 return child_sp;
129 }
130
131 // return true if this object is now safe to use forever without ever
132 // updating again; the typical (and tested) answer here is 'false'
136
137 llvm::Expected<size_t>
138 GetIndexOfChildWithName(llvm::StringRef name) override {
139 if (!m_block_struct_type.IsValid())
140 return llvm::createStringErrorV("type has no child named '{0}'", name);
141
142 const bool omit_empty_base_classes = false;
143 return m_block_struct_type.GetIndexOfChildWithName(name,
144 omit_empty_base_classes);
145 }
146
147private:
149};
150
151} // namespace formatters
152} // namespace lldb_private
153
155 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
156 lldb_private::SyntheticChildrenFrontEnd *synthetic_children =
158 if (!synthetic_children) {
159 return false;
160 }
161
162 synthetic_children->Update();
163
164 static constexpr llvm::StringLiteral s_FuncPtr_name("__FuncPtr");
165
166 auto index_or_err =
167 synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name);
168
169 if (!index_or_err) {
170 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), index_or_err.takeError(),
171 "{0}");
172 return false;
173 }
174
175 lldb::ValueObjectSP child_sp =
176 synthetic_children->GetChildAtIndex(*index_or_err);
177
178 if (!child_sp) {
179 return false;
180 }
181
182 lldb::ValueObjectSP qualified_child_representation_sp =
183 child_sp->GetQualifiedRepresentationIfAvailable(
185
186 const char *child_value =
187 qualified_child_representation_sp->GetValueAsCString();
188
189 s.Printf("%s", child_value);
190
191 return true;
192}
193
197 if (!valobj_sp)
198 return nullptr;
199 return new BlockPointerSyntheticFrontEnd(valobj_sp);
200}
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
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
"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:134
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...
virtual llvm::Expected< size_t > GetIndexOfChildWithName(llvm::StringRef name)
Determine the index of a named child.
SyntheticChildrenFrontEnd(ValueObject &backend)
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, ValueObjectManager *manager=nullptr)
These routines create ValueObjectConstResult ValueObjects from various data sources.
lldb::ValueObjectSP GetSP()
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
llvm::Expected< size_t > GetIndexOfChildWithName(llvm::StringRef name) override
Determine the index of a named child.
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:338
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
A child of a type, as returned by CompilerType::GetChildAtIndex.