xref: /llvm-project/lldb/include/lldb/DataFormatters/TypeSummary.h (revision 87659a17d0703c1244211d9f8d1f0c21e816f0e1)
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 
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/lldb-public.h"
20 
21 #include "lldb/Core/FormatEntity.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/Utility/StructuredData.h"
24 
25 namespace llvm {
26 class MemoryBuffer;
27 }
28 
29 namespace lldb_private {
30 class TypeSummaryOptions {
31 public:
32   TypeSummaryOptions();
33 
34   ~TypeSummaryOptions() = default;
35 
36   lldb::LanguageType GetLanguage() const;
37 
38   lldb::TypeSummaryCapping GetCapping() const;
39 
40   TypeSummaryOptions &SetLanguage(lldb::LanguageType);
41 
42   TypeSummaryOptions &SetCapping(lldb::TypeSummaryCapping);
43 
44 private:
45   lldb::LanguageType m_lang = lldb::eLanguageTypeUnknown;
46   lldb::TypeSummaryCapping m_capping = lldb::eTypeSummaryCapped;
47 };
48 
49 class TypeSummaryImpl {
50 public:
51   enum class Kind { eSummaryString, eScript, eBytecode, eCallback, eInternal };
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 
77     Flags &Clear() {
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 
133     bool GetHideEmptyAggregates() const {
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 
159     bool GetShowMembersOneLiner() const {
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 {
215     return !m_flags.GetDontShowChildren();
216   }
217 
218   virtual bool DoesPrintEmptyAggregates() const {
219     return !m_flags.GetHideEmptyAggregates();
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) {
239     m_flags.SetDontShowChildren(!value);
240   }
241 
242   virtual void SetDoesPrintValue(bool value) {
243     m_flags.SetDontShowValue(!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 
277 protected:
278   uint32_t m_my_revision = 0;
279   Flags m_flags;
280 
281   TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags);
282 
283 private:
284   Kind m_kind;
285   TypeSummaryImpl(const TypeSummaryImpl &) = delete;
286   const TypeSummaryImpl &operator=(const TypeSummaryImpl &) = delete;
287 };
288 
289 // simple string-based summaries, using ${var to show data
290 struct StringSummaryFormat : public TypeSummaryImpl {
291   std::string m_format_str;
292   FormatEntity::Entry m_format;
293   Status m_error;
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 
314 private:
315   StringSummaryFormat(const StringSummaryFormat &) = delete;
316   const StringSummaryFormat &operator=(const StringSummaryFormat &) = delete;
317 };
318 
319 // summaries implemented via a C++ function
320 struct CXXFunctionSummaryFormat : public TypeSummaryImpl {
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 &)>
325       Callback;
326 
327   Callback m_impl;
328   std::string m_description;
329 
330   CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl,
331                            const char *description);
332 
333   ~CXXFunctionSummaryFormat() override = default;
334 
335   Callback GetBackendFunction() const { return m_impl; }
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 
361 private:
362   CXXFunctionSummaryFormat(const CXXFunctionSummaryFormat &) = delete;
363   const CXXFunctionSummaryFormat &
364   operator=(const CXXFunctionSummaryFormat &) = delete;
365 };
366 
367 // Python-based summaries, running script code to show data
368 struct ScriptSummaryFormat : public TypeSummaryImpl {
369   std::string m_function_name;
370   std::string m_python_script;
371   std::string m_script_formatter_name;
372   StructuredData::ObjectSP m_script_function_sp;
373 
374   ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
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 
412 private:
413   ScriptSummaryFormat(const ScriptSummaryFormat &) = delete;
414   const ScriptSummaryFormat &operator=(const ScriptSummaryFormat &) = delete;
415 };
416 
417 /// A summary formatter that is defined in LLDB formmater bytecode.
418 class BytecodeSummaryFormat : public TypeSummaryImpl {
419   std::unique_ptr<llvm::MemoryBuffer> m_bytecode;
420 
421 public:
422   BytecodeSummaryFormat(const TypeSummaryImpl::Flags &flags,
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
436