LLDB  mainline
TypeSummary.cpp
Go to the documentation of this file.
1 //===-- TypeSummary.cpp ----------------------------------------*- 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 
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/Core/ValueObject.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Target.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 TypeSummaryOptions::TypeSummaryOptions()
30  : m_lang(eLanguageTypeUnknown), m_capping(eTypeSummaryCapped) {}
31 
33  : m_lang(rhs.m_lang), m_capping(rhs.m_capping) {}
34 
37  m_lang = rhs.m_lang;
38  m_capping = rhs.m_capping;
39  return *this;
40 }
41 
43 
45  return m_capping;
46 }
47 
49  m_lang = lang;
50  return *this;
51 }
52 
55  m_capping = cap;
56  return *this;
57 }
58 
60  : m_flags(flags), m_kind(kind) {}
61 
63  const char *format_cstr)
64  : TypeSummaryImpl(Kind::eSummaryString, flags), m_format_str() {
65  SetSummaryString(format_cstr);
66 }
67 
68 void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
69  m_format.Clear();
70  if (format_cstr && format_cstr[0]) {
71  m_format_str = format_cstr;
72  m_error = FormatEntity::Parse(format_cstr, m_format);
73  } else {
74  m_format_str.clear();
75  m_error.Clear();
76  }
77 }
78 
79 bool StringSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
80  const TypeSummaryOptions &options) {
81  if (!valobj) {
82  retval.assign("NULL ValueObject");
83  return false;
84  }
85 
86  StreamString s;
87  ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
88  SymbolContext sc;
89  StackFrame *frame = exe_ctx.GetFramePtr();
90  if (frame)
91  sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
92 
93  if (IsOneLiner()) {
94  ValueObjectPrinter printer(valobj, &s, DumpValueObjectOptions());
95  printer.PrintChildrenOneLiner(HideNames(valobj));
96  retval = s.GetString();
97  return true;
98  } else {
99  if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
100  &sc.line_entry.range.GetBaseAddress(), valobj,
101  false, false)) {
102  retval.assign(s.GetString());
103  return true;
104  } else {
105  retval.assign("error: summary string parsing error");
106  return false;
107  }
108  }
109 }
110 
112  StreamString sstr;
113 
114  sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
115  m_error.Fail() ? " error: " : "",
116  m_error.Fail() ? m_error.AsCString() : "",
117  Cascades() ? "" : " (not cascading)",
118  !DoesPrintChildren(nullptr) ? "" : " (show children)",
119  !DoesPrintValue(nullptr) ? " (hide value)" : "",
120  IsOneLiner() ? " (one-line printout)" : "",
121  SkipsPointers() ? " (skip pointers)" : "",
122  SkipsReferences() ? " (skip references)" : "",
123  HideNames(nullptr) ? " (hide member names)" : "");
124  return sstr.GetString();
125 }
126 
128  const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
129  : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
130  m_description(description ? description : "") {}
131 
133  std::string &dest,
134  const TypeSummaryOptions &options) {
135  dest.clear();
136  StreamString stream;
137  if (!m_impl || !m_impl(*valobj, stream, options))
138  return false;
139  dest = stream.GetString();
140  return true;
141 }
142 
144  StreamString sstr;
145  sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
146  !DoesPrintChildren(nullptr) ? "" : " (show children)",
147  !DoesPrintValue(nullptr) ? " (hide value)" : "",
148  IsOneLiner() ? " (one-line printout)" : "",
149  SkipsPointers() ? " (skip pointers)" : "",
150  SkipsReferences() ? " (skip references)" : "",
151  HideNames(nullptr) ? " (hide member names)" : "",
152  m_description.c_str());
153  return sstr.GetString();
154 }
155 
157  const char *function_name,
158  const char *python_script)
159  : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
160  m_python_script(), m_script_function_sp() {
161  if (function_name)
162  m_function_name.assign(function_name);
163  if (python_script)
164  m_python_script.assign(python_script);
165 }
166 
167 bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
168  const TypeSummaryOptions &options) {
169  if (!valobj)
170  return false;
171 
172  TargetSP target_sp(valobj->GetTargetSP());
173 
174  if (!target_sp) {
175  retval.assign("error: no target");
176  return false;
177  }
178 
179  ScriptInterpreter *script_interpreter =
180  target_sp->GetDebugger().GetScriptInterpreter();
181 
182  if (!script_interpreter) {
183  retval.assign("error: no ScriptInterpreter");
184  return false;
185  }
186 
187  return script_interpreter->GetScriptedSummary(
188  m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
189  retval);
190 }
191 
193  StreamString sstr;
194  sstr.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
195  !DoesPrintChildren(nullptr) ? "" : " (show children)",
196  !DoesPrintValue(nullptr) ? " (hide value)" : "",
197  IsOneLiner() ? " (one-line printout)" : "",
198  SkipsPointers() ? " (skip pointers)" : "",
199  SkipsReferences() ? " (skip references)" : "",
200  HideNames(nullptr) ? " (hide member names)" : "");
201  if (m_python_script.empty()) {
202  if (m_function_name.empty()) {
203  sstr.PutCString("no backing script");
204  } else {
206  }
207  } else {
209  }
210  return sstr.GetString();
211 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
static Status Parse(const llvm::StringRef &format, Entry &entry)
virtual bool DoesPrintValue(ValueObject *valobj) const
Definition: TypeSummary.h:221
std::function< bool(ValueObject &, Stream &, const TypeSummaryOptions &)> Callback
Definition: TypeSummary.h:312
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *function_name, const char *python_script=nullptr)
StructuredData::ObjectSP m_script_function_sp
Definition: TypeSummary.h:354
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:357
lldb::TypeSummaryCapping GetCapping() const
Definition: TypeSummary.cpp:44
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
lldb::LanguageType GetLanguage() const
Definition: TypeSummary.cpp:42
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame&#39;s current pc value.
Definition: StackFrame.cpp:267
TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
Definition: TypeSummary.cpp:59
FormatEntity::Entry m_format
Definition: TypeSummary.h:282
TypeSummaryOptions & operator=(const TypeSummaryOptions &rhs)
Definition: TypeSummary.cpp:36
LanguageType
Programming language type.
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
Definition: TypeSummary.cpp:79
StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f)
Definition: TypeSummary.cpp:62
void Clear()
Clear the object state.
Definition: Status.cpp:167
std::string GetDescription() override
std::string GetDescription() override
TypeSummaryOptions & SetLanguage(lldb::LanguageType)
Definition: TypeSummary.cpp:48
virtual bool HideNames(ValueObject *valobj) const
Definition: TypeSummary.h:227
llvm::StringRef GetString() const
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:353
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
bool PrintChildrenOneLiner(bool hide_names)
std::string GetDescription() override
static bool Format(const Entry &entry, Stream &s, const SymbolContext *sc, const ExecutionContext *exe_ctx, const Address *addr, ValueObject *valobj, bool function_changed, bool initial_function)
void SetSummaryString(const char *f)
Definition: TypeSummary.cpp:68
TypeSummaryOptions & SetCapping(lldb::TypeSummaryCapping)
Definition: TypeSummary.cpp:54
Unknown or invalid language value.
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
Definition: SBAddress.h:15
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:565
CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
virtual bool DoesPrintChildren(ValueObject *valobj) const
Definition: TypeSummary.h:213
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
This base class provides an interface to stack frames.
Definition: StackFrame.h:40
virtual bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, StructuredData::ObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options, std::string &retval)