LLDB mainline
TypeSummary.cpp
Go to the documentation of this file.
1//===-- TypeSummary.cpp ---------------------------------------------------===//
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
15#include "lldb/lldb-public.h"
16
17#include "lldb/Core/Debugger.h"
23#include "lldb/Target/Target.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
30
32
34 return m_capping;
35}
36
38 m_lang = lang;
39 return *this;
40}
41
44 m_capping = cap;
45 return *this;
46}
47
49 : m_flags(flags), m_kind(kind) {}
50
52 const char *format_cstr)
53 : TypeSummaryImpl(Kind::eSummaryString, flags), m_format_str() {
54 SetSummaryString(format_cstr);
55}
56
57void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
59 if (format_cstr && format_cstr[0]) {
60 m_format_str = format_cstr;
61 m_error = FormatEntity::Parse(format_cstr, m_format);
62 } else {
63 m_format_str.clear();
64 m_error.Clear();
65 }
66}
67
68bool StringSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
69 const TypeSummaryOptions &options) {
70 if (!valobj) {
71 retval.assign("NULL ValueObject");
72 return false;
73 }
74
78 StackFrame *frame = exe_ctx.GetFramePtr();
79 if (frame)
80 sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
81
82 if (IsOneLiner()) {
83 // We've already checked the case of a NULL valobj above. Let's put in an
84 // assert here to make sure someone doesn't take that out:
85 assert(valobj && "Must have a valid ValueObject to summarize");
86 ValueObjectPrinter printer(*valobj, &s, DumpValueObjectOptions());
87 printer.PrintChildrenOneLiner(HideNames(valobj));
88 retval = std::string(s.GetString());
89 return true;
90 } else {
91 if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
92 &sc.line_entry.range.GetBaseAddress(), valobj,
93 false, false)) {
94 retval.assign(std::string(s.GetString()));
95 return true;
96 } else {
97 retval.assign("error: summary string parsing error");
98 return false;
99 }
100 }
101}
102
104 StreamString sstr;
105
106 sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
107 m_error.Fail() ? " error: " : "",
108 m_error.Fail() ? m_error.AsCString() : "",
109 Cascades() ? "" : " (not cascading)",
110 !DoesPrintChildren(nullptr) ? "" : " (show children)",
111 !DoesPrintValue(nullptr) ? " (hide value)" : "",
112 IsOneLiner() ? " (one-line printout)" : "",
113 SkipsPointers() ? " (skip pointers)" : "",
114 SkipsReferences() ? " (skip references)" : "",
115 HideNames(nullptr) ? " (hide member names)" : "");
116 return std::string(sstr.GetString());
117}
118
120 const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
121 : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
122 m_description(description ? description : "") {}
123
125 std::string &dest,
126 const TypeSummaryOptions &options) {
127 dest.clear();
128 StreamString stream;
129 if (!m_impl || !m_impl(*valobj, stream, options))
130 return false;
131 dest = std::string(stream.GetString());
132 return true;
133}
134
136 StreamString sstr;
137 sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
138 !DoesPrintChildren(nullptr) ? "" : " (show children)",
139 !DoesPrintValue(nullptr) ? " (hide value)" : "",
140 IsOneLiner() ? " (one-line printout)" : "",
141 SkipsPointers() ? " (skip pointers)" : "",
142 SkipsReferences() ? " (skip references)" : "",
143 HideNames(nullptr) ? " (hide member names)" : "",
144 m_description.c_str());
145 return std::string(sstr.GetString());
146}
147
149 const char *function_name,
150 const char *python_script)
151 : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
152 m_python_script(), m_script_function_sp() {
153 if (function_name)
154 m_function_name.assign(function_name);
155 if (python_script)
156 m_python_script.assign(python_script);
157}
158
159bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
160 const TypeSummaryOptions &options) {
161 if (!valobj)
162 return false;
163
164 TargetSP target_sp(valobj->GetTargetSP());
165
166 if (!target_sp) {
167 retval.assign("error: no target");
168 return false;
169 }
170
171 ScriptInterpreter *script_interpreter =
172 target_sp->GetDebugger().GetScriptInterpreter();
173
174 if (!script_interpreter) {
175 retval.assign("error: no ScriptInterpreter");
176 return false;
177 }
178
179 return script_interpreter->GetScriptedSummary(
180 m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
181 retval);
182}
183
185 StreamString sstr;
186 sstr.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
187 !DoesPrintChildren(nullptr) ? "" : " (show children)",
188 !DoesPrintValue(nullptr) ? " (hide value)" : "",
189 IsOneLiner() ? " (one-line printout)" : "",
190 SkipsPointers() ? " (skip pointers)" : "",
191 SkipsReferences() ? " (skip references)" : "",
192 HideNames(nullptr) ? " (hide member names)" : "");
193 if (m_python_script.empty()) {
194 if (m_function_name.empty()) {
195 sstr.PutCString("no backing script");
196 } else {
198 }
199 } else {
201 }
202 return std::string(sstr.GetString());
203}
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
virtual bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, StructuredData::ObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options, std::string &retval)
This base class provides an interface to stack frames.
Definition: StackFrame.h:43
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
Definition: StackFrame.cpp:300
void Clear()
Clear the object state.
Definition: Status.cpp:166
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:129
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
LineEntry line_entry
The LineEntry for a given query.
virtual bool HideNames(ValueObject *valobj) const
Definition: TypeSummary.h:224
virtual bool DoesPrintValue(ValueObject *valobj) const
Definition: TypeSummary.h:218
TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
Definition: TypeSummary.cpp:48
virtual bool DoesPrintChildren(ValueObject *valobj) const
Definition: TypeSummary.h:210
lldb::TypeSummaryCapping m_capping
Definition: TypeSummary.h:42
lldb::LanguageType GetLanguage() const
Definition: TypeSummary.cpp:31
TypeSummaryOptions & SetCapping(lldb::TypeSummaryCapping)
Definition: TypeSummary.cpp:43
TypeSummaryOptions & SetLanguage(lldb::LanguageType)
Definition: TypeSummary.cpp:37
lldb::TypeSummaryCapping GetCapping() const
Definition: TypeSummary.cpp:33
bool PrintChildrenOneLiner(bool hide_names)
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:545
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
Status Parse(const llvm::StringRef &format, Entry &entry)
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)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
TypeSummaryCapping
Whether a summary should cap how much data it returns to users or not.
LanguageType
Programming language type.
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:436
std::function< bool(ValueObject &, Stream &, const TypeSummaryOptions &)> Callback
Definition: TypeSummary.h:311
CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
std::string GetDescription() override
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:137
std::string GetDescription() override
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:355
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
Definition: TypeSummary.cpp:68
FormatEntity::Entry m_format
Definition: TypeSummary.h:280
StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f)
Definition: TypeSummary.cpp:51
void SetSummaryString(const char *f)
Definition: TypeSummary.cpp:57
std::string GetDescription() override