LLDB mainline
TypeSummary.h
Go to the documentation of this file.
1//===-- TypeSummary.h -------------------------------------------*- C++ -*-===//
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#ifndef LLDB_DATAFORMATTERS_TYPESUMMARY_H
10#define LLDB_DATAFORMATTERS_TYPESUMMARY_H
11
12#include <cstdint>
13
14#include <functional>
15#include <memory>
16#include <string>
17
19#include "lldb/lldb-public.h"
20
22#include "lldb/Utility/Status.h"
24
25namespace llvm {
26class MemoryBuffer;
27}
28
29namespace lldb_private {
31public:
33
35
37
39
41
43
44private:
47};
48
50public:
52
53 virtual ~TypeSummaryImpl() = default;
54
55 Kind GetKind() const { return m_kind; }
56
57 class Flags {
58 public:
59 Flags() = default;
60
61 Flags(const Flags &other) : m_flags(other.m_flags) {}
62
63 Flags(uint32_t value) : m_flags(value) {}
64
65 Flags &operator=(const Flags &rhs) {
66 if (&rhs != this)
67 m_flags = rhs.m_flags;
68
69 return *this;
70 }
71
72 Flags &operator=(const uint32_t &rhs) {
73 m_flags = rhs;
74 return *this;
75 }
76
78 m_flags = 0;
79 return *this;
80 }
81
82 bool GetCascades() const {
83 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
84 }
85
86 Flags &SetCascades(bool value = true) {
87 if (value)
88 m_flags |= lldb::eTypeOptionCascade;
89 else
90 m_flags &= ~lldb::eTypeOptionCascade;
91 return *this;
92 }
93
94 bool GetSkipPointers() const {
95 return (m_flags & lldb::eTypeOptionSkipPointers) ==
96 lldb::eTypeOptionSkipPointers;
97 }
98
99 Flags &SetSkipPointers(bool value = true) {
100 if (value)
101 m_flags |= lldb::eTypeOptionSkipPointers;
102 else
103 m_flags &= ~lldb::eTypeOptionSkipPointers;
104 return *this;
105 }
106
107 bool GetSkipReferences() const {
108 return (m_flags & lldb::eTypeOptionSkipReferences) ==
109 lldb::eTypeOptionSkipReferences;
110 }
111
112 Flags &SetSkipReferences(bool value = true) {
113 if (value)
114 m_flags |= lldb::eTypeOptionSkipReferences;
115 else
116 m_flags &= ~lldb::eTypeOptionSkipReferences;
117 return *this;
118 }
119
120 bool GetDontShowChildren() const {
121 return (m_flags & lldb::eTypeOptionHideChildren) ==
122 lldb::eTypeOptionHideChildren;
123 }
124
125 Flags &SetDontShowChildren(bool value = true) {
126 if (value)
127 m_flags |= lldb::eTypeOptionHideChildren;
128 else
129 m_flags &= ~lldb::eTypeOptionHideChildren;
130 return *this;
131 }
132
134 return (m_flags & lldb::eTypeOptionHideEmptyAggregates) ==
135 lldb::eTypeOptionHideEmptyAggregates;
136 }
137
138 Flags &SetHideEmptyAggregates(bool value = true) {
139 if (value)
140 m_flags |= lldb::eTypeOptionHideEmptyAggregates;
141 else
142 m_flags &= ~lldb::eTypeOptionHideEmptyAggregates;
143 return *this;
144 }
145
146 bool GetDontShowValue() const {
147 return (m_flags & lldb::eTypeOptionHideValue) ==
148 lldb::eTypeOptionHideValue;
149 }
150
151 Flags &SetDontShowValue(bool value = true) {
152 if (value)
153 m_flags |= lldb::eTypeOptionHideValue;
154 else
155 m_flags &= ~lldb::eTypeOptionHideValue;
156 return *this;
157 }
158
160 return (m_flags & lldb::eTypeOptionShowOneLiner) ==
161 lldb::eTypeOptionShowOneLiner;
162 }
163
164 Flags &SetShowMembersOneLiner(bool value = true) {
165 if (value)
166 m_flags |= lldb::eTypeOptionShowOneLiner;
167 else
168 m_flags &= ~lldb::eTypeOptionShowOneLiner;
169 return *this;
170 }
171
172 bool GetHideItemNames() const {
173 return (m_flags & lldb::eTypeOptionHideNames) ==
174 lldb::eTypeOptionHideNames;
175 }
176
177 Flags &SetHideItemNames(bool value = true) {
178 if (value)
179 m_flags |= lldb::eTypeOptionHideNames;
180 else
181 m_flags &= ~lldb::eTypeOptionHideNames;
182 return *this;
183 }
184
185 bool GetNonCacheable() const {
186 return (m_flags & lldb::eTypeOptionNonCacheable) ==
187 lldb::eTypeOptionNonCacheable;
188 }
189
190 Flags &SetNonCacheable(bool value = true) {
191 if (value)
192 m_flags |= lldb::eTypeOptionNonCacheable;
193 else
194 m_flags &= ~lldb::eTypeOptionNonCacheable;
195 return *this;
196 }
197
198 uint32_t GetValue() { return m_flags; }
199
200 void SetValue(uint32_t value) { m_flags = value; }
201
202 private:
203 uint32_t m_flags = lldb::eTypeOptionCascade;
204 };
205
206 bool Cascades() const { return m_flags.GetCascades(); }
207
208 bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
209
210 bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
211
212 bool NonCacheable() const { return m_flags.GetNonCacheable(); }
213
214 virtual bool DoesPrintChildren(ValueObject *valobj) const {
216 }
217
218 virtual bool DoesPrintEmptyAggregates() const {
220 }
221
222 virtual bool DoesPrintValue(ValueObject *valobj) const {
223 return !m_flags.GetDontShowValue();
224 }
225
226 bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); }
227
228 virtual bool HideNames(ValueObject *valobj) const {
229 return m_flags.GetHideItemNames();
230 }
231
232 void SetCascades(bool value) { m_flags.SetCascades(value); }
233
234 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
235
236 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
237
238 virtual void SetDoesPrintChildren(bool value) {
240 }
241
242 virtual void SetDoesPrintValue(bool value) {
244 }
245
246 void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); }
247
248 virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); }
249
250 virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
251
252 uint32_t GetOptions() { return m_flags.GetValue(); }
253
254 void SetOptions(uint32_t value) { m_flags.SetValue(value); }
255
256 // we are using a ValueObject* instead of a ValueObjectSP because we do not
257 // need to hold on to this for extended periods of time and we trust the
258 // ValueObject to stay around for as long as it is required for us to
259 // generate its summary
260 virtual bool FormatObject(ValueObject *valobj, std::string &dest,
261 const TypeSummaryOptions &options) = 0;
262
263 virtual std::string GetDescription() = 0;
264
265 /// Get the name of the Type Summary Provider, either a C++ class, a summary
266 /// string, or a script function name.
267 virtual std::string GetName() = 0;
268
269 /// Get the name of the kind of Summary Provider, either c++, summary string,
270 /// script or python.
271 virtual std::string GetSummaryKindName();
272
273 uint32_t &GetRevision() { return m_my_revision; }
274
275 typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
276
277protected:
278 uint32_t m_my_revision = 0;
280
281 TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags);
282
283private:
286 const TypeSummaryImpl &operator=(const TypeSummaryImpl &) = delete;
287};
288
289// simple string-based summaries, using ${var to show data
291 std::string m_format_str;
294
295 StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f);
296
297 ~StringSummaryFormat() override = default;
298
299 const char *GetSummaryString() const { return m_format_str.c_str(); }
300
301 void SetSummaryString(const char *f);
302
303 bool FormatObject(ValueObject *valobj, std::string &dest,
304 const TypeSummaryOptions &options) override;
305
306 std::string GetDescription() override;
307
308 std::string GetName() override;
309
310 static bool classof(const TypeSummaryImpl *S) {
311 return S->GetKind() == Kind::eSummaryString;
312 }
313
314private:
317};
318
319// summaries implemented via a C++ function
321 // we should convert these to SBValue and SBStream if we ever cross the
322 // boundary towards the external world
323 typedef std::function<bool(ValueObject &, Stream &,
324 const TypeSummaryOptions &)>
326
328 std::string m_description;
329
331 const char *description);
332
333 ~CXXFunctionSummaryFormat() override = default;
334
336
337 const char *GetTextualInfo() const { return m_description.c_str(); }
338
339 void SetBackendFunction(Callback cb_func) { m_impl = std::move(cb_func); }
340
341 void SetTextualInfo(const char *descr) {
342 if (descr)
343 m_description.assign(descr);
344 else
345 m_description.clear();
346 }
347
348 bool FormatObject(ValueObject *valobj, std::string &dest,
349 const TypeSummaryOptions &options) override;
350
351 std::string GetDescription() override;
352
353 static bool classof(const TypeSummaryImpl *S) {
354 return S->GetKind() == Kind::eCallback;
355 }
356
357 std::string GetName() override;
358
359 typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer;
360
361private:
365};
366
367// Python-based summaries, running script code to show data
369 std::string m_function_name;
370 std::string m_python_script;
373
375 const char *function_name,
376 const char *python_script = nullptr);
377
378 ~ScriptSummaryFormat() override = default;
379
380 const char *GetFunctionName() const { return m_function_name.c_str(); }
381
382 const char *GetPythonScript() const { return m_python_script.c_str(); }
383
384 void SetFunctionName(const char *function_name) {
385 if (function_name)
386 m_function_name.assign(function_name);
387 else
388 m_function_name.clear();
389 m_python_script.clear();
390 }
391
392 void SetPythonScript(const char *script) {
393 if (script)
394 m_python_script.assign(script);
395 else
396 m_python_script.clear();
397 }
398
399 bool FormatObject(ValueObject *valobj, std::string &dest,
400 const TypeSummaryOptions &options) override;
401
402 std::string GetDescription() override;
403
404 std::string GetName() override;
405
406 static bool classof(const TypeSummaryImpl *S) {
407 return S->GetKind() == Kind::eScript;
408 }
409
410 typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer;
411
412private:
415};
416
417/// A summary formatter that is defined in LLDB formmater bytecode.
419 std::unique_ptr<llvm::MemoryBuffer> m_bytecode;
420
421public:
423 std::unique_ptr<llvm::MemoryBuffer> bytecode);
424 bool FormatObject(ValueObject *valobj, std::string &dest,
425 const TypeSummaryOptions &options) override;
426 std::string GetDescription() override;
427 std::string GetName() override;
428 static bool classof(const TypeSummaryImpl *S) {
429 return S->GetKind() == Kind::eBytecode;
430 }
431};
432
433} // namespace lldb_private
434
435#endif // LLDB_DATAFORMATTERS_TYPESUMMARY_H
A summary formatter that is defined in LLDB formmater bytecode.
Definition: TypeSummary.h:418
static bool classof(const TypeSummaryImpl *S)
Definition: TypeSummary.h:428
std::string GetDescription() override
std::unique_ptr< llvm::MemoryBuffer > m_bytecode
Definition: TypeSummary.h:419
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
An error handling class.
Definition: Status.h:118
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
std::shared_ptr< Object > ObjectSP
Flags & SetNonCacheable(bool value=true)
Definition: TypeSummary.h:190
Flags & operator=(const uint32_t &rhs)
Definition: TypeSummary.h:72
Flags & SetCascades(bool value=true)
Definition: TypeSummary.h:86
Flags & SetHideEmptyAggregates(bool value=true)
Definition: TypeSummary.h:138
Flags & SetSkipPointers(bool value=true)
Definition: TypeSummary.h:99
Flags & SetHideItemNames(bool value=true)
Definition: TypeSummary.h:177
Flags & operator=(const Flags &rhs)
Definition: TypeSummary.h:65
Flags & SetDontShowChildren(bool value=true)
Definition: TypeSummary.h:125
Flags & SetSkipReferences(bool value=true)
Definition: TypeSummary.h:112
Flags & SetShowMembersOneLiner(bool value=true)
Definition: TypeSummary.h:164
Flags & SetDontShowValue(bool value=true)
Definition: TypeSummary.h:151
void SetIsOneLiner(bool value)
Definition: TypeSummary.h:246
virtual bool HideNames(ValueObject *valobj) const
Definition: TypeSummary.h:228
TypeSummaryImpl(const TypeSummaryImpl &)=delete
virtual ~TypeSummaryImpl()=default
virtual std::string GetName()=0
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
virtual std::string GetSummaryKindName()
Get the name of the kind of Summary Provider, either c++, summary string, script or python.
Definition: TypeSummary.cpp:49
virtual void SetDoesPrintValue(bool value)
Definition: TypeSummary.h:242
void SetSkipsReferences(bool value)
Definition: TypeSummary.h:236
virtual bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options)=0
void SetSkipsPointers(bool value)
Definition: TypeSummary.h:234
virtual bool DoesPrintEmptyAggregates() const
Definition: TypeSummary.h:218
virtual std::string GetDescription()=0
void SetOptions(uint32_t value)
Definition: TypeSummary.h:254
void SetCascades(bool value)
Definition: TypeSummary.h:232
virtual void SetDoesPrintChildren(bool value)
Definition: TypeSummary.h:238
const TypeSummaryImpl & operator=(const TypeSummaryImpl &)=delete
virtual void SetNonCacheable(bool value)
Definition: TypeSummary.h:250
virtual void SetHideNames(bool value)
Definition: TypeSummary.h:248
virtual bool DoesPrintValue(ValueObject *valobj) const
Definition: TypeSummary.h:222
std::shared_ptr< TypeSummaryImpl > SharedPointer
Definition: TypeSummary.h:275
virtual bool DoesPrintChildren(ValueObject *valobj) const
Definition: TypeSummary.h:214
lldb::TypeSummaryCapping m_capping
Definition: TypeSummary.h:46
lldb::LanguageType GetLanguage() const
Definition: TypeSummary.cpp:29
TypeSummaryOptions & SetCapping(lldb::TypeSummaryCapping)
Definition: TypeSummary.cpp:41
TypeSummaryOptions & SetLanguage(lldb::LanguageType)
Definition: TypeSummary.cpp:35
lldb::TypeSummaryCapping GetCapping() const
Definition: TypeSummary.cpp:31
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
TypeSummaryCapping
Whether a summary should cap how much data it returns to users or not.
@ eTypeSummaryCapped
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
Definition: Debugger.h:54
std::shared_ptr< CXXFunctionSummaryFormat > SharedPointer
Definition: TypeSummary.h:359
CXXFunctionSummaryFormat(const CXXFunctionSummaryFormat &)=delete
std::function< bool(ValueObject &, Stream &, const TypeSummaryOptions &)> Callback
Definition: TypeSummary.h:325
void SetTextualInfo(const char *descr)
Definition: TypeSummary.h:341
~CXXFunctionSummaryFormat() override=default
const CXXFunctionSummaryFormat & operator=(const CXXFunctionSummaryFormat &)=delete
std::string GetDescription() override
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
void SetBackendFunction(Callback cb_func)
Definition: TypeSummary.h:339
static bool classof(const TypeSummaryImpl *S)
Definition: TypeSummary.h:353
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
static bool classof(const TypeSummaryImpl *S)
Definition: TypeSummary.h:406
~ScriptSummaryFormat() override=default
const char * GetPythonScript() const
Definition: TypeSummary.h:382
std::string GetDescription() override
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
const char * GetFunctionName() const
Definition: TypeSummary.h:380
ScriptSummaryFormat(const ScriptSummaryFormat &)=delete
void SetPythonScript(const char *script)
Definition: TypeSummary.h:392
const ScriptSummaryFormat & operator=(const ScriptSummaryFormat &)=delete
std::shared_ptr< ScriptSummaryFormat > SharedPointer
Definition: TypeSummary.h:410
StructuredData::ObjectSP m_script_function_sp
Definition: TypeSummary.h:372
void SetFunctionName(const char *function_name)
Definition: TypeSummary.h:384
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
const StringSummaryFormat & operator=(const StringSummaryFormat &)=delete
~StringSummaryFormat() override=default
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
Definition: TypeSummary.cpp:81
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
FormatEntity::Entry m_format
Definition: TypeSummary.h:292
static bool classof(const TypeSummaryImpl *S)
Definition: TypeSummary.h:310
StringSummaryFormat(const StringSummaryFormat &)=delete
const char * GetSummaryString() const
Definition: TypeSummary.h:299
void SetSummaryString(const char *f)
Definition: TypeSummary.cpp:70
std::string GetDescription() override