1 //===-- TypeSummary.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/DataFormatters/TypeSummary.h"
11
12
13
14
15 #include "lldb/lldb-enumerations.h"
16 #include "lldb/lldb-public.h"
17
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/ValueObject.h"
20 #include "lldb/DataFormatters/ValueObjectPrinter.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Symbol/CompilerType.h"
23 #include "lldb/Target/StackFrame.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/StreamString.h"
26
27 using namespace lldb;
28 using namespace lldb_private;
29
TypeSummaryOptions()30 TypeSummaryOptions::TypeSummaryOptions()
31 : m_lang(eLanguageTypeUnknown), m_capping(eTypeSummaryCapped) {}
32
TypeSummaryOptions(const TypeSummaryOptions & rhs)33 TypeSummaryOptions::TypeSummaryOptions(const TypeSummaryOptions &rhs)
34 : m_lang(rhs.m_lang), m_capping(rhs.m_capping) {}
35
36 TypeSummaryOptions &TypeSummaryOptions::
operator =(const TypeSummaryOptions & rhs)37 operator=(const TypeSummaryOptions &rhs) {
38 m_lang = rhs.m_lang;
39 m_capping = rhs.m_capping;
40 return *this;
41 }
42
GetLanguage() const43 lldb::LanguageType TypeSummaryOptions::GetLanguage() const { return m_lang; }
44
GetCapping() const45 lldb::TypeSummaryCapping TypeSummaryOptions::GetCapping() const {
46 return m_capping;
47 }
48
SetLanguage(lldb::LanguageType lang)49 TypeSummaryOptions &TypeSummaryOptions::SetLanguage(lldb::LanguageType lang) {
50 m_lang = lang;
51 return *this;
52 }
53
54 TypeSummaryOptions &
SetCapping(lldb::TypeSummaryCapping cap)55 TypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping cap) {
56 m_capping = cap;
57 return *this;
58 }
59
TypeSummaryImpl(Kind kind,const TypeSummaryImpl::Flags & flags)60 TypeSummaryImpl::TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
61 : m_flags(flags), m_kind(kind) {}
62
StringSummaryFormat(const TypeSummaryImpl::Flags & flags,const char * format_cstr)63 StringSummaryFormat::StringSummaryFormat(const TypeSummaryImpl::Flags &flags,
64 const char *format_cstr)
65 : TypeSummaryImpl(Kind::eSummaryString, flags), m_format_str() {
66 SetSummaryString(format_cstr);
67 }
68
SetSummaryString(const char * format_cstr)69 void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
70 m_format.Clear();
71 if (format_cstr && format_cstr[0]) {
72 m_format_str = format_cstr;
73 m_error = FormatEntity::Parse(format_cstr, m_format);
74 } else {
75 m_format_str.clear();
76 m_error.Clear();
77 }
78 }
79
FormatObject(ValueObject * valobj,std::string & retval,const TypeSummaryOptions & options)80 bool StringSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
81 const TypeSummaryOptions &options) {
82 if (!valobj) {
83 retval.assign("NULL ValueObject");
84 return false;
85 }
86
87 StreamString s;
88 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
89 SymbolContext sc;
90 StackFrame *frame = exe_ctx.GetFramePtr();
91 if (frame)
92 sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
93
94 if (IsOneLiner()) {
95 ValueObjectPrinter printer(valobj, &s, DumpValueObjectOptions());
96 printer.PrintChildrenOneLiner(HideNames(valobj));
97 retval = s.GetString();
98 return true;
99 } else {
100 if (FormatEntity::Format(m_format, s, &sc, &exe_ctx,
101 &sc.line_entry.range.GetBaseAddress(), valobj,
102 false, false)) {
103 retval.assign(s.GetString());
104 return true;
105 } else {
106 retval.assign("error: summary string parsing error");
107 return false;
108 }
109 }
110 }
111
GetDescription()112 std::string StringSummaryFormat::GetDescription() {
113 StreamString sstr;
114
115 sstr.Printf("`%s`%s%s%s%s%s%s%s%s%s", m_format_str.c_str(),
116 m_error.Fail() ? " error: " : "",
117 m_error.Fail() ? m_error.AsCString() : "",
118 Cascades() ? "" : " (not cascading)",
119 !DoesPrintChildren(nullptr) ? "" : " (show children)",
120 !DoesPrintValue(nullptr) ? " (hide value)" : "",
121 IsOneLiner() ? " (one-line printout)" : "",
122 SkipsPointers() ? " (skip pointers)" : "",
123 SkipsReferences() ? " (skip references)" : "",
124 HideNames(nullptr) ? " (hide member names)" : "");
125 return sstr.GetString();
126 }
127
CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags & flags,Callback impl,const char * description)128 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat(
129 const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
130 : TypeSummaryImpl(Kind::eCallback, flags), m_impl(impl),
131 m_description(description ? description : "") {}
132
FormatObject(ValueObject * valobj,std::string & dest,const TypeSummaryOptions & options)133 bool CXXFunctionSummaryFormat::FormatObject(ValueObject *valobj,
134 std::string &dest,
135 const TypeSummaryOptions &options) {
136 dest.clear();
137 StreamString stream;
138 if (!m_impl || !m_impl(*valobj, stream, options))
139 return false;
140 dest = stream.GetString();
141 return true;
142 }
143
GetDescription()144 std::string CXXFunctionSummaryFormat::GetDescription() {
145 StreamString sstr;
146 sstr.Printf("%s%s%s%s%s%s%s %s", Cascades() ? "" : " (not cascading)",
147 !DoesPrintChildren(nullptr) ? "" : " (show children)",
148 !DoesPrintValue(nullptr) ? " (hide value)" : "",
149 IsOneLiner() ? " (one-line printout)" : "",
150 SkipsPointers() ? " (skip pointers)" : "",
151 SkipsReferences() ? " (skip references)" : "",
152 HideNames(nullptr) ? " (hide member names)" : "",
153 m_description.c_str());
154 return sstr.GetString();
155 }
156
ScriptSummaryFormat(const TypeSummaryImpl::Flags & flags,const char * function_name,const char * python_script)157 ScriptSummaryFormat::ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
158 const char *function_name,
159 const char *python_script)
160 : TypeSummaryImpl(Kind::eScript, flags), m_function_name(),
161 m_python_script(), m_script_function_sp() {
162 if (function_name)
163 m_function_name.assign(function_name);
164 if (python_script)
165 m_python_script.assign(python_script);
166 }
167
FormatObject(ValueObject * valobj,std::string & retval,const TypeSummaryOptions & options)168 bool ScriptSummaryFormat::FormatObject(ValueObject *valobj, std::string &retval,
169 const TypeSummaryOptions &options) {
170 if (!valobj)
171 return false;
172
173 TargetSP target_sp(valobj->GetTargetSP());
174
175 if (!target_sp) {
176 retval.assign("error: no target");
177 return false;
178 }
179
180 ScriptInterpreter *script_interpreter =
181 target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
182
183 if (!script_interpreter) {
184 retval.assign("error: no ScriptInterpreter");
185 return false;
186 }
187
188 return script_interpreter->GetScriptedSummary(
189 m_function_name.c_str(), valobj->GetSP(), m_script_function_sp, options,
190 retval);
191 }
192
GetDescription()193 std::string ScriptSummaryFormat::GetDescription() {
194 StreamString sstr;
195 sstr.Printf("%s%s%s%s%s%s%s\n ", Cascades() ? "" : " (not cascading)",
196 !DoesPrintChildren(nullptr) ? "" : " (show children)",
197 !DoesPrintValue(nullptr) ? " (hide value)" : "",
198 IsOneLiner() ? " (one-line printout)" : "",
199 SkipsPointers() ? " (skip pointers)" : "",
200 SkipsReferences() ? " (skip references)" : "",
201 HideNames(nullptr) ? " (hide member names)" : "");
202 if (m_python_script.empty()) {
203 if (m_function_name.empty()) {
204 sstr.PutCString("no backing script");
205 } else {
206 sstr.PutCString(m_function_name);
207 }
208 } else {
209 sstr.PutCString(m_python_script);
210 }
211 return sstr.GetString();
212 }
213