]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp
MFV r346563:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / Language / CPlusPlus / BlockPointer.cpp
1 //===-- BlockPointer.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "BlockPointer.h"
11
12 #include "lldb/Core/ValueObject.h"
13 #include "lldb/DataFormatters/FormattersHelpers.h"
14 #include "lldb/Symbol/ClangASTContext.h"
15 #include "lldb/Symbol/ClangASTImporter.h"
16 #include "lldb/Symbol/CompilerType.h"
17 #include "lldb/Symbol/TypeSystem.h"
18 #include "lldb/Target/Target.h"
19
20 #include "lldb/Utility/LLDBAssert.h"
21
22 using namespace lldb;
23 using namespace lldb_private;
24 using namespace lldb_private::formatters;
25
26 namespace lldb_private {
27 namespace formatters {
28
29 class BlockPointerSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
30 public:
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);
36
37     TargetSP target_sp(m_backend.GetTargetSP());
38
39     if (!target_sp) {
40       return;
41     }
42
43     Status err;
44     TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage(
45         &err, lldb::eLanguageTypeC_plus_plus);
46
47     if (!err.Success() || !type_system) {
48       return;
49     }
50
51     ClangASTContext *clang_ast_context =
52         llvm::dyn_cast<ClangASTContext>(type_system);
53
54     if (!clang_ast_context) {
55       return;
56     }
57
58     ClangASTImporterSP clang_ast_importer = target_sp->GetClangASTImporter();
59
60     if (!clang_ast_importer) {
61       return;
62     }
63
64     const char *const isa_name("__isa");
65     const CompilerType isa_type =
66         clang_ast_context->GetBasicType(lldb::eBasicTypeObjCClass);
67     const char *const flags_name("__flags");
68     const CompilerType flags_type =
69         clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
70     const char *const reserved_name("__reserved");
71     const CompilerType reserved_type =
72         clang_ast_context->GetBasicType(lldb::eBasicTypeInt);
73     const char *const FuncPtr_name("__FuncPtr");
74     const CompilerType FuncPtr_type =
75         clang_ast_importer->CopyType(*clang_ast_context, function_pointer_type);
76
77     m_block_struct_type = clang_ast_context->CreateStructForIdentifier(
78         ConstString(), {{isa_name, isa_type},
79                         {flags_name, flags_type},
80                         {reserved_name, reserved_type},
81                         {FuncPtr_name, FuncPtr_type}});
82   }
83
84   ~BlockPointerSyntheticFrontEnd() override = default;
85
86   size_t CalculateNumChildren() override {
87     const bool omit_empty_base_classes = false;
88     return m_block_struct_type.GetNumChildren(omit_empty_base_classes, nullptr);
89   }
90
91   lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
92     if (!m_block_struct_type.IsValid()) {
93       return lldb::ValueObjectSP();
94     }
95
96     if (idx >= CalculateNumChildren()) {
97       return lldb::ValueObjectSP();
98     }
99
100     const bool thread_and_frame_only_if_stopped = true;
101     ExecutionContext exe_ctx = m_backend.GetExecutionContextRef().Lock(
102         thread_and_frame_only_if_stopped);
103     const bool transparent_pointers = false;
104     const bool omit_empty_base_classes = false;
105     const bool ignore_array_bounds = false;
106     ValueObject *value_object = nullptr;
107
108     std::string child_name;
109     uint32_t child_byte_size = 0;
110     int32_t child_byte_offset = 0;
111     uint32_t child_bitfield_bit_size = 0;
112     uint32_t child_bitfield_bit_offset = 0;
113     bool child_is_base_class = false;
114     bool child_is_deref_of_parent = false;
115     uint64_t language_flags = 0;
116
117     const CompilerType child_type =
118         m_block_struct_type.GetChildCompilerTypeAtIndex(
119             &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
120             ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
121             child_bitfield_bit_size, child_bitfield_bit_offset,
122             child_is_base_class, child_is_deref_of_parent, value_object,
123             language_flags);
124
125     ValueObjectSP struct_pointer_sp =
126         m_backend.Cast(m_block_struct_type.GetPointerType());
127
128     if (!struct_pointer_sp) {
129       return lldb::ValueObjectSP();
130     }
131
132     Status err;
133     ValueObjectSP struct_sp = struct_pointer_sp->Dereference(err);
134
135     if (!struct_sp || !err.Success()) {
136       return lldb::ValueObjectSP();
137     }
138
139     ValueObjectSP child_sp(struct_sp->GetSyntheticChildAtOffset(
140         child_byte_offset, child_type, true,
141         ConstString(child_name.c_str(), child_name.size())));
142
143     return child_sp;
144   }
145
146   // return true if this object is now safe to use forever without ever
147   // updating again; the typical (and tested) answer here is 'false'
148   bool Update() override { return false; }
149
150   // maybe return false if the block pointer is, say, null
151   bool MightHaveChildren() override { return true; }
152
153   size_t GetIndexOfChildWithName(const ConstString &name) override {
154     if (!m_block_struct_type.IsValid())
155       return UINT32_MAX;
156
157     const bool omit_empty_base_classes = false;
158     return m_block_struct_type.GetIndexOfChildWithName(name.AsCString(),
159                                                        omit_empty_base_classes);
160   }
161
162 private:
163   CompilerType m_block_struct_type;
164 };
165
166 } // namespace formatters
167 } // namespace lldb_private
168
169 bool lldb_private::formatters::BlockPointerSummaryProvider(
170     ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
171   lldb_private::SyntheticChildrenFrontEnd *synthetic_children =
172       BlockPointerSyntheticFrontEndCreator(nullptr, valobj.GetSP());
173   if (!synthetic_children) {
174     return false;
175   }
176
177   synthetic_children->Update();
178
179   static const ConstString s_FuncPtr_name("__FuncPtr");
180
181   lldb::ValueObjectSP child_sp = synthetic_children->GetChildAtIndex(
182       synthetic_children->GetIndexOfChildWithName(s_FuncPtr_name));
183
184   if (!child_sp) {
185     return false;
186   }
187
188   lldb::ValueObjectSP qualified_child_representation_sp =
189       child_sp->GetQualifiedRepresentationIfAvailable(
190           lldb::eDynamicDontRunTarget, true);
191
192   const char *child_value =
193       qualified_child_representation_sp->GetValueAsCString();
194
195   s.Printf("%s", child_value);
196
197   return true;
198 }
199
200 lldb_private::SyntheticChildrenFrontEnd *
201 lldb_private::formatters::BlockPointerSyntheticFrontEndCreator(
202     CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
203   if (!valobj_sp)
204     return nullptr;
205   return new BlockPointerSyntheticFrontEnd(valobj_sp);
206 }