xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- BlockPointer.cpp --------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "BlockPointer.h"
10061da546Spatrick 
11dda28197Spatrick #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
12dda28197Spatrick #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
13dda28197Spatrick #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
14061da546Spatrick #include "lldb/Core/ValueObject.h"
15061da546Spatrick #include "lldb/DataFormatters/FormattersHelpers.h"
16061da546Spatrick #include "lldb/Symbol/CompilerType.h"
17061da546Spatrick #include "lldb/Symbol/TypeSystem.h"
18061da546Spatrick #include "lldb/Target/Target.h"
19061da546Spatrick #include "lldb/Utility/LLDBAssert.h"
20*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
21061da546Spatrick #include "lldb/Utility/Log.h"
22061da546Spatrick 
23061da546Spatrick using namespace lldb;
24061da546Spatrick using namespace lldb_private;
25061da546Spatrick using namespace lldb_private::formatters;
26061da546Spatrick 
27061da546Spatrick namespace lldb_private {
28061da546Spatrick namespace formatters {
29061da546Spatrick 
30061da546Spatrick class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
31061da546Spatrick public:
BlockPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)32061da546Spatrick   BlockPointerSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
33061da546Spatrick       : SyntheticChildrenFrontEnd(*valobj_sp), m_block_struct_type() {
34061da546Spatrick     CompilerType block_pointer_type(m_backend.GetCompilerType());
35061da546Spatrick     CompilerType function_pointer_type;
36061da546Spatrick     block_pointer_type.IsBlockPointerType(&function_pointer_type);
37061da546Spatrick 
38061da546Spatrick     TargetSP target_sp(m_backend.GetTargetSP());
39061da546Spatrick 
40061da546Spatrick     if (!target_sp) {
41061da546Spatrick       return;
42061da546Spatrick     }
43061da546Spatrick 
44061da546Spatrick     auto type_system_or_err = target_sp->GetScratchTypeSystemForLanguage(
45061da546Spatrick         lldb::eLanguageTypeC_plus_plus);
46061da546Spatrick     if (auto err = type_system_or_err.takeError()) {
47*f6aab3d8Srobert       LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), std::move(err),
48*f6aab3d8Srobert                      "Failed to get scratch TypeSystemClang");
49061da546Spatrick       return;
50061da546Spatrick     }
51061da546Spatrick 
52*f6aab3d8Srobert     auto ts = block_pointer_type.GetTypeSystem();
53*f6aab3d8Srobert     auto clang_ast_context = ts.dyn_cast_or_null<TypeSystemClang>();
54*f6aab3d8Srobert     if (!clang_ast_context)
55*f6aab3d8Srobert       return;
56061da546Spatrick 
57dda28197Spatrick     std::shared_ptr<ClangASTImporter> clang_ast_importer;
58dda28197Spatrick     auto *state = target_sp->GetPersistentExpressionStateForLanguage(
59dda28197Spatrick         lldb::eLanguageTypeC_plus_plus);
60dda28197Spatrick     if (state) {
61dda28197Spatrick       auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
62dda28197Spatrick       clang_ast_importer = persistent_vars->GetClangASTImporter();
63dda28197Spatrick     }
64061da546Spatrick 
65061da546Spatrick     if (!clang_ast_importer) {
66061da546Spatrick       return;
67061da546Spatrick     }
68061da546Spatrick 
69061da546Spatrick     const char *const isa_name("__isa");
70061da546Spatrick     const CompilerType isa_type =
71061da546Spatrick         clang_ast_context->GetBasicType(lldb::eBasicTypeObjCClass);
72061da546Spatrick     const char *const flags_name("__flags");
73061da546Spatrick     const CompilerType flags_type =
74061da546Spatrick         clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
75061da546Spatrick     const char *const reserved_name("__reserved");
76061da546Spatrick     const CompilerType reserved_type =
77061da546Spatrick         clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
78061da546Spatrick     const char *const FuncPtr_name("__FuncPtr");
79061da546Spatrick 
80061da546Spatrick     m_block_struct_type = clang_ast_context->CreateStructForIdentifier(
81061da546Spatrick         ConstString(), {{isa_name, isa_type},
82061da546Spatrick                         {flags_name, flags_type},
83061da546Spatrick                         {reserved_name, reserved_type},
84be691f3bSpatrick                         {FuncPtr_name, function_pointer_type}});
85061da546Spatrick   }
86061da546Spatrick 
87061da546Spatrick   ~BlockPointerSyntheticFrontEnd() override = default;
88061da546Spatrick 
CalculateNumChildren()89061da546Spatrick   size_t CalculateNumChildren() override {
90061da546Spatrick     const bool omit_empty_base_classes = false;
91061da546Spatrick     return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr);
92061da546Spatrick   }
93061da546Spatrick 
GetChildAtIndex(size_t idx)94061da546Spatrick   lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
95061da546Spatrick     if (!m_block_struct_type.IsValid()) {
96061da546Spatrick       return lldb::ValueObjectSP();
97061da546Spatrick     }
98061da546Spatrick 
99061da546Spatrick     if (idx >= CalculateNumChildren()) {
100061da546Spatrick       return lldb::ValueObjectSP();
101061da546Spatrick     }
102061da546Spatrick 
103061da546Spatrick     const bool thread_and_frame_only_if_stopped = true;
104061da546Spatrick     ExecutionContext exe_ctx = m_backend.GetExecutionContextRef().Lock(
105061da546Spatrick         thread_and_frame_only_if_stopped);
106061da546Spatrick     const bool transparent_pointers = false;
107061da546Spatrick     const bool omit_empty_base_classes = false;
108061da546Spatrick     const bool ignore_array_bounds = false;
109061da546Spatrick     ValueObject *value_object = nullptr;
110061da546Spatrick 
111061da546Spatrick     std::string child_name;
112061da546Spatrick     uint32_t child_byte_size = 0;
113061da546Spatrick     int32_t child_byte_offset = 0;
114061da546Spatrick     uint32_t child_bitfield_bit_size = 0;
115061da546Spatrick     uint32_t child_bitfield_bit_offset = 0;
116061da546Spatrick     bool child_is_base_class = false;
117061da546Spatrick     bool child_is_deref_of_parent = false;
118061da546Spatrick     uint64_t language_flags = 0;
119061da546Spatrick 
120061da546Spatrick     const CompilerType child_type =
121061da546Spatrick         m_block_struct_type.GetChildCompilerTypeAtIndex(
122061da546Spatrick             &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
123061da546Spatrick             ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
124061da546Spatrick             child_bitfield_bit_size, child_bitfield_bit_offset,
125061da546Spatrick             child_is_base_class, child_is_deref_of_parent, value_object,
126061da546Spatrick             language_flags);
127061da546Spatrick 
128061da546Spatrick     ValueObjectSP struct_pointer_sp =
129061da546Spatrick         m_backend.Cast(m_block_struct_type.GetPointerType());
130061da546Spatrick 
131061da546Spatrick     if (!struct_pointer_sp) {
132061da546Spatrick       return lldb::ValueObjectSP();
133061da546Spatrick     }
134061da546Spatrick 
135061da546Spatrick     Status err;
136061da546Spatrick     ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err);
137061da546Spatrick 
138061da546Spatrick     if (!struct_sp || !err.Success()) {
139061da546Spatrick       return lldb::ValueObjectSP();
140061da546Spatrick     }
141061da546Spatrick 
142061da546Spatrick     ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset(
143061da546Spatrick         child_byte_offset, child_type, true,
144061da546Spatrick         ConstString(child_name.c_str(), child_name.size())));
145061da546Spatrick 
146061da546Spatrick     return child_sp;
147061da546Spatrick   }
148061da546Spatrick 
149061da546Spatrick   // return true if this object is now safe to use forever without ever
150061da546Spatrick   // updating again; the typical (and tested) answer here is 'false'
Update()151061da546Spatrick   bool Update() override { return false; }
152061da546Spatrick 
153061da546Spatrick   // maybe return false if the block pointer is, say, null
MightHaveChildren()154061da546Spatrick   bool MightHaveChildren() override { return true; }
155061da546Spatrick 
GetIndexOfChildWithName(ConstString name)156061da546Spatrick   size_t GetIndexOfChildWithName(ConstString name) override {
157061da546Spatrick     if (!m_block_struct_type.IsValid())
158061da546Spatrick       return UINT32_MAX;
159061da546Spatrick 
160061da546Spatrick     const bool omit_empty_base_classes = false;
161061da546Spatrick     return m_block_struct_type.GetIndexOfChildWithName(name.AsCString(),
162061da546Spatrick                                                        omit_empty_base_classes);
163061da546Spatrick   }
164061da546Spatrick 
165061da546Spatrick private:
166061da546Spatrick   CompilerType m_block_struct_type;
167061da546Spatrick };
168061da546Spatrick 
169061da546Spatrick } // namespace formatters
170061da546Spatrick } // namespace lldb_private
171061da546Spatrick 
BlockPointerSummaryProvider(ValueObject & valobj,Stream & s,const TypeSummaryOptions &)172061da546Spatrick bool lldb_private::formatters::BlockPointerSummaryProvider(
173061da546Spatrick     ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
174061da546Spatrick   lldb_private::SyntheticChildrenFrontEnd *synthetic_children =
175061da546Spatrick       BlockPointerSyntheticFrontEndCreator(nullptr, valobj.GetSP());
176061da546Spatrick   if (!synthetic_children) {
177061da546Spatrick     return false;
178061da546Spatrick   }
179061da546Spatrick 
180061da546Spatrick   synthetic_children->Update();
181061da546Spatrick 
182061da546Spatrick   static const ConstString s_FuncPtr_name("__FuncPtr");
183061da546Spatrick 
184061da546Spatrick   lldb::ValueObjectSP child_sp = synthetic_children->GetChildAtIndex(
185061da546Spatrick       synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name));
186061da546Spatrick 
187061da546Spatrick   if (!child_sp) {
188061da546Spatrick     return false;
189061da546Spatrick   }
190061da546Spatrick 
191061da546Spatrick   lldb::ValueObjectSP qualified_child_representation_sp =
192061da546Spatrick       child_sp->GetQualifiedRepresentationIfAvailable(
193061da546Spatrick           lldb::eDynamicDontRunTarget, true);
194061da546Spatrick 
195061da546Spatrick   const char *child_value =
196061da546Spatrick       qualified_child_representation_sp->GetValueAsCString();
197061da546Spatrick 
198061da546Spatrick   s.Printf("%s", child_value);
199061da546Spatrick 
200061da546Spatrick   return true;
201061da546Spatrick }
202061da546Spatrick 
203061da546Spatrick lldb_private::SyntheticChildrenFrontEnd *
BlockPointerSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)204061da546Spatrick lldb_private::formatters::BlockPointerSyntheticFrontEndCreator(
205061da546Spatrick     CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
206061da546Spatrick   if (!valobj_sp)
207061da546Spatrick     return nullptr;
208061da546Spatrick   return new BlockPointerSyntheticFrontEnd(valobj_sp);
209061da546Spatrick }
210