1 //===-- TypeSynthetic.cpp ----------------------------------------*- C++
2 //-*-===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11
12
13
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-public.h"
16
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/DataFormatters/TypeSynthetic.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Interpreter/ScriptInterpreter.h"
21 #include "lldb/Symbol/CompilerType.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/StreamString.h"
24
25 using namespace lldb;
26 using namespace lldb_private;
27
AddExpressionPath(const std::string & path)28 void TypeFilterImpl::AddExpressionPath(const std::string &path) {
29 bool need_add_dot = true;
30 if (path[0] == '.' || (path[0] == '-' && path[1] == '>') || path[0] == '[')
31 need_add_dot = false;
32 // add a '.' symbol to help forgetful users
33 if (!need_add_dot)
34 m_expression_paths.push_back(path);
35 else
36 m_expression_paths.push_back(std::string(".") + path);
37 }
38
SetExpressionPathAtIndex(size_t i,const std::string & path)39 bool TypeFilterImpl::SetExpressionPathAtIndex(size_t i,
40 const std::string &path) {
41 if (i >= GetCount())
42 return false;
43 bool need_add_dot = true;
44 if (path[0] == '.' || (path[0] == '-' && path[1] == '>') || path[0] == '[')
45 need_add_dot = false;
46 // add a '.' symbol to help forgetful users
47 if (!need_add_dot)
48 m_expression_paths[i] = path;
49 else
50 m_expression_paths[i] = std::string(".") + path;
51 return true;
52 }
53
54 size_t
GetIndexOfChildWithName(const ConstString & name)55 TypeFilterImpl::FrontEnd::GetIndexOfChildWithName(const ConstString &name) {
56 const char *name_cstr = name.GetCString();
57 if (name_cstr) {
58 for (size_t i = 0; i < filter->GetCount(); i++) {
59 const char *expr_cstr = filter->GetExpressionPathAtIndex(i);
60 if (expr_cstr) {
61 if (*expr_cstr == '.')
62 expr_cstr++;
63 else if (*expr_cstr == '-' && *(expr_cstr + 1) == '>')
64 expr_cstr += 2;
65 }
66 if (expr_cstr) {
67 if (!::strcmp(name_cstr, expr_cstr))
68 return i;
69 }
70 }
71 }
72 return UINT32_MAX;
73 }
74
GetDescription()75 std::string TypeFilterImpl::GetDescription() {
76 StreamString sstr;
77 sstr.Printf("%s%s%s {\n", Cascades() ? "" : " (not cascading)",
78 SkipsPointers() ? " (skip pointers)" : "",
79 SkipsReferences() ? " (skip references)" : "");
80
81 for (size_t i = 0; i < GetCount(); i++) {
82 sstr.Printf(" %s\n", GetExpressionPathAtIndex(i));
83 }
84
85 sstr.Printf("}");
86 return sstr.GetString();
87 }
88
GetDescription()89 std::string CXXSyntheticChildren::GetDescription() {
90 StreamString sstr;
91 sstr.Printf("%s%s%s %s", Cascades() ? "" : " (not cascading)",
92 SkipsPointers() ? " (skip pointers)" : "",
93 SkipsReferences() ? " (skip references)" : "",
94 m_description.c_str());
95
96 return sstr.GetString();
97 }
98
CreateValueObjectFromExpression(llvm::StringRef name,llvm::StringRef expression,const ExecutionContext & exe_ctx)99 lldb::ValueObjectSP SyntheticChildrenFrontEnd::CreateValueObjectFromExpression(
100 llvm::StringRef name, llvm::StringRef expression,
101 const ExecutionContext &exe_ctx) {
102 ValueObjectSP valobj_sp(
103 ValueObject::CreateValueObjectFromExpression(name, expression, exe_ctx));
104 if (valobj_sp)
105 valobj_sp->SetSyntheticChildrenGenerated(true);
106 return valobj_sp;
107 }
108
CreateValueObjectFromAddress(llvm::StringRef name,uint64_t address,const ExecutionContext & exe_ctx,CompilerType type)109 lldb::ValueObjectSP SyntheticChildrenFrontEnd::CreateValueObjectFromAddress(
110 llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
111 CompilerType type) {
112 ValueObjectSP valobj_sp(
113 ValueObject::CreateValueObjectFromAddress(name, address, exe_ctx, type));
114 if (valobj_sp)
115 valobj_sp->SetSyntheticChildrenGenerated(true);
116 return valobj_sp;
117 }
118
CreateValueObjectFromData(llvm::StringRef name,const DataExtractor & data,const ExecutionContext & exe_ctx,CompilerType type)119 lldb::ValueObjectSP SyntheticChildrenFrontEnd::CreateValueObjectFromData(
120 llvm::StringRef name, const DataExtractor &data,
121 const ExecutionContext &exe_ctx, CompilerType type) {
122 ValueObjectSP valobj_sp(
123 ValueObject::CreateValueObjectFromData(name, data, exe_ctx, type));
124 if (valobj_sp)
125 valobj_sp->SetSyntheticChildrenGenerated(true);
126 return valobj_sp;
127 }
128
129 #ifndef LLDB_DISABLE_PYTHON
130
FrontEnd(std::string pclass,ValueObject & backend)131 ScriptedSyntheticChildren::FrontEnd::FrontEnd(std::string pclass,
132 ValueObject &backend)
133 : SyntheticChildrenFrontEnd(backend), m_python_class(pclass),
134 m_wrapper_sp(), m_interpreter(NULL) {
135 if (backend == LLDB_INVALID_UID)
136 return;
137
138 TargetSP target_sp = backend.GetTargetSP();
139
140 if (!target_sp)
141 return;
142
143 m_interpreter =
144 target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
145
146 if (m_interpreter != NULL)
147 m_wrapper_sp = m_interpreter->CreateSyntheticScriptedProvider(
148 m_python_class.c_str(), backend.GetSP());
149 }
150
~FrontEnd()151 ScriptedSyntheticChildren::FrontEnd::~FrontEnd() {}
152
153 lldb::ValueObjectSP
GetChildAtIndex(size_t idx)154 ScriptedSyntheticChildren::FrontEnd::GetChildAtIndex(size_t idx) {
155 if (!m_wrapper_sp || !m_interpreter)
156 return lldb::ValueObjectSP();
157
158 return m_interpreter->GetChildAtIndex(m_wrapper_sp, idx);
159 }
160
IsValid()161 bool ScriptedSyntheticChildren::FrontEnd::IsValid() {
162 return (m_wrapper_sp && m_wrapper_sp->IsValid() && m_interpreter);
163 }
164
CalculateNumChildren()165 size_t ScriptedSyntheticChildren::FrontEnd::CalculateNumChildren() {
166 if (!m_wrapper_sp || m_interpreter == NULL)
167 return 0;
168 return m_interpreter->CalculateNumChildren(m_wrapper_sp, UINT32_MAX);
169 }
170
CalculateNumChildren(uint32_t max)171 size_t ScriptedSyntheticChildren::FrontEnd::CalculateNumChildren(uint32_t max) {
172 if (!m_wrapper_sp || m_interpreter == NULL)
173 return 0;
174 return m_interpreter->CalculateNumChildren(m_wrapper_sp, max);
175 }
176
Update()177 bool ScriptedSyntheticChildren::FrontEnd::Update() {
178 if (!m_wrapper_sp || m_interpreter == NULL)
179 return false;
180
181 return m_interpreter->UpdateSynthProviderInstance(m_wrapper_sp);
182 }
183
MightHaveChildren()184 bool ScriptedSyntheticChildren::FrontEnd::MightHaveChildren() {
185 if (!m_wrapper_sp || m_interpreter == NULL)
186 return false;
187
188 return m_interpreter->MightHaveChildrenSynthProviderInstance(m_wrapper_sp);
189 }
190
GetIndexOfChildWithName(const ConstString & name)191 size_t ScriptedSyntheticChildren::FrontEnd::GetIndexOfChildWithName(
192 const ConstString &name) {
193 if (!m_wrapper_sp || m_interpreter == NULL)
194 return UINT32_MAX;
195 return m_interpreter->GetIndexOfChildWithName(m_wrapper_sp,
196 name.GetCString());
197 }
198
GetSyntheticValue()199 lldb::ValueObjectSP ScriptedSyntheticChildren::FrontEnd::GetSyntheticValue() {
200 if (!m_wrapper_sp || m_interpreter == NULL)
201 return nullptr;
202
203 return m_interpreter->GetSyntheticValue(m_wrapper_sp);
204 }
205
GetSyntheticTypeName()206 ConstString ScriptedSyntheticChildren::FrontEnd::GetSyntheticTypeName() {
207 if (!m_wrapper_sp || m_interpreter == NULL)
208 return ConstString();
209
210 return m_interpreter->GetSyntheticTypeName(m_wrapper_sp);
211 }
212
GetDescription()213 std::string ScriptedSyntheticChildren::GetDescription() {
214 StreamString sstr;
215 sstr.Printf("%s%s%s Python class %s", Cascades() ? "" : " (not cascading)",
216 SkipsPointers() ? " (skip pointers)" : "",
217 SkipsReferences() ? " (skip references)" : "",
218 m_python_class.c_str());
219
220 return sstr.GetString();
221 }
222
223 #endif // #ifndef LLDB_DISABLE_PYTHON
224