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 ValueObjectPrinter printer(valobj, &s, DumpValueObjectOptions());
84 printer.PrintChildrenOneLiner(HideNames(valobj));
85 retval = std::string(s.GetString());
86 return true;
87 } else {
88 if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
89 &sc.line_entry.range.GetBaseAddress(), valobj,
90 false, false)) {
91 retval.assign(std::string(s.GetString()));
92 return true;
93 } else {
94 retval.assign("error: summary string parsing error");
95 return false;
96 }
97 }
98}
99
101 StreamString sstr;
102
103 sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
104 m_error.Fail() ? " error: " : "",
105 m_error.Fail() ? m_error.AsCString() : "",
106 Cascades() ? "" : " (not cascading)",
107 !DoesPrintChildren(nullptr) ? "" : " (show children)",
108 !DoesPrintValue(nullptr) ? " (hide value)" : "",
109 IsOneLiner() ? " (one-line printout)" : "",
110 SkipsPointers() ? " (skip pointers)" : "",
111 SkipsReferences() ? " (skip references)" : "",
112 HideNames(nullptr) ? " (hide member names)" : "");
113 return std::string(sstr.GetString());
114}
115
117 const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
118 : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
119 m_description(description ? description : "") {}
120
122 std::string &dest,
123 const TypeSummaryOptions &options) {
124 dest.clear();
125 StreamString stream;
126 if (!m_impl || !m_impl(*valobj, stream, options))
127 return false;
128 dest = std::string(stream.GetString());
129 return true;
130}
131
133 StreamString sstr;
134 sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
135 !DoesPrintChildren(nullptr) ? "" : " (show children)",
136 !DoesPrintValue(nullptr) ? " (hide value)" : "",
137 IsOneLiner() ? " (one-line printout)" : "",
138 SkipsPointers() ? " (skip pointers)" : "",
139 SkipsReferences() ? " (skip references)" : "",
140 HideNames(nullptr) ? " (hide member names)" : "",
141 m_description.c_str());
142 return std::string(sstr.GetString());
143}
144
146 const char *function_name,
147 const char *python_script)
148 : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
149 m_python_script(), m_script_function_sp() {
150 if (function_name)
151 m_function_name.assign(function_name);
152 if (python_script)
153 m_python_script.assign(python_script);
154}
155
156bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
157 const TypeSummaryOptions &options) {
158 if (!valobj)
159 return false;
160
161 TargetSP target_sp(valobj->GetTargetSP());
162
163 if (!target_sp) {
164 retval.assign("error: no target");
165 return false;
166 }
167
168 ScriptInterpreter *script_interpreter =
169 target_sp->GetDebugger().GetScriptInterpreter();
170
171 if (!script_interpreter) {
172 retval.assign("error: no ScriptInterpreter");
173 return false;
174 }
175
176 return script_interpreter->GetScriptedSummary(
177 m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
178 retval);
179}
180
182 StreamString sstr;
183 sstr.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
184 !DoesPrintChildren(nullptr) ? "" : " (show children)",
185 !DoesPrintValue(nullptr) ? " (hide value)" : "",
186 IsOneLiner() ? " (one-line printout)" : "",
187 SkipsPointers() ? " (skip pointers)" : "",
188 SkipsReferences() ? " (skip references)" : "",
189 HideNames(nullptr) ? " (hide member names)" : "");
190 if (m_python_script.empty()) {
191 if (m_function_name.empty()) {
192 sstr.PutCString("no backing script");
193 } else {
195 }
196 } else {
198 }
199 return std::string(sstr.GetString());
200}
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.
static Status Parse(const llvm::StringRef &format, Entry &entry)
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)
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:41
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:167
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
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:554
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
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::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:139
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