]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeSummary.cpp
Use the new insecure-lan-zones option instead of listing each AS112 zone
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / DataFormatters / TypeSummary.cpp
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 // C Includes
11
12 // C++ Includes
13
14 // Other libraries and framework includes
15
16 // Project includes
17 #include "lldb/lldb-public.h"
18 #include "lldb/lldb-enumerations.h"
19
20 #include "lldb/Core/Debugger.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Core/Timer.h"
23 #include "lldb/DataFormatters/TypeSummary.h"
24 #include "lldb/DataFormatters/ValueObjectPrinter.h"
25 #include "lldb/Interpreter/CommandInterpreter.h"
26 #include "lldb/Symbol/ClangASTType.h"
27 #include "lldb/Target/StackFrame.h"
28 #include "lldb/Target/Target.h"
29
30 #include "lldb/Host/Host.h"
31
32 using namespace lldb;
33 using namespace lldb_private;
34
35 TypeSummaryOptions::TypeSummaryOptions () :
36     m_lang(eLanguageTypeUnknown),
37     m_capping(eTypeSummaryCapped)
38 {}
39
40 TypeSummaryOptions::TypeSummaryOptions (const TypeSummaryOptions& rhs) :
41     m_lang(rhs.m_lang),
42     m_capping(rhs.m_capping)
43 {}
44
45 TypeSummaryOptions&
46 TypeSummaryOptions::operator = (const TypeSummaryOptions& rhs)
47 {
48     m_lang = rhs.m_lang;
49     m_capping = rhs.m_capping;
50     return *this;
51 }
52
53 lldb::LanguageType
54 TypeSummaryOptions::GetLanguage () const
55 {
56     return m_lang;
57 }
58
59 lldb::TypeSummaryCapping
60 TypeSummaryOptions::GetCapping () const
61 {
62     return m_capping;
63 }
64
65 TypeSummaryOptions&
66 TypeSummaryOptions::SetLanguage (lldb::LanguageType lang)
67 {
68     m_lang = lang;
69     return *this;
70 }
71
72 TypeSummaryOptions&
73 TypeSummaryOptions::SetCapping (lldb::TypeSummaryCapping cap)
74 {
75     m_capping = cap;
76     return *this;
77 }
78
79 TypeSummaryImpl::TypeSummaryImpl (const TypeSummaryImpl::Flags& flags) :
80 m_flags(flags)
81 {
82 }
83
84
85 StringSummaryFormat::StringSummaryFormat (const TypeSummaryImpl::Flags& flags,
86                                           const char *format_cstr) :
87     TypeSummaryImpl(flags),
88     m_format_str()
89 {
90     SetSummaryString (format_cstr);
91 }
92
93 void
94 StringSummaryFormat::SetSummaryString (const char* format_cstr)
95 {
96     m_format.Clear();
97     if (format_cstr && format_cstr[0])
98     {
99         m_format_str = format_cstr;
100         m_error = FormatEntity::Parse(format_cstr, m_format);
101     }
102     else
103     {
104         m_format_str.clear();
105         m_error.Clear();
106     }
107 }
108
109
110
111 bool
112 StringSummaryFormat::FormatObject (ValueObject *valobj,
113                                    std::string& retval,
114                                    const TypeSummaryOptions& options)
115 {
116     if (!valobj)
117     {
118         retval.assign("NULL ValueObject");
119         return false;
120     }
121     
122     StreamString s;
123     ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
124     SymbolContext sc;
125     StackFrame *frame = exe_ctx.GetFramePtr();
126     if (frame)
127         sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
128     
129     if (IsOneLiner())
130     {
131         ValueObjectPrinter printer(valobj,&s,DumpValueObjectOptions());
132         printer.PrintChildrenOneLiner(HideNames(valobj));
133         retval.assign(s.GetData());
134         return true;
135     }
136     else
137     {
138         if (FormatEntity::Format(m_format, s, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), valobj, false, false))
139         {
140             retval.assign(s.GetString());
141             return true;
142         }
143         else
144         {
145             retval.assign("error: summary string parsing error");
146             return false;
147         }
148     }
149 }
150
151 std::string
152 StringSummaryFormat::GetDescription ()
153 {
154     StreamString sstr;
155     
156     sstr.Printf ("`%s`%s%s%s%s%s%s%s%s%s",
157                  m_format_str.c_str(),
158                  m_error.Fail() ? " error: " : "",
159                  m_error.Fail() ? m_error.AsCString() : "",
160                  Cascades() ? "" : " (not cascading)",
161                  !DoesPrintChildren(nullptr) ? "" : " (show children)",
162                  !DoesPrintValue(nullptr) ? " (hide value)" : "",
163                  IsOneLiner() ? " (one-line printout)" : "",
164                  SkipsPointers() ? " (skip pointers)" : "",
165                  SkipsReferences() ? " (skip references)" : "",
166                  HideNames(nullptr) ? " (hide member names)" : "");
167     return sstr.GetString();
168 }
169
170 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat (const TypeSummaryImpl::Flags& flags,
171                                                     Callback impl,
172                                                     const char* description) :
173 TypeSummaryImpl(flags),
174     m_impl(impl),
175     m_description(description ? description : "")
176 {
177 }
178
179 bool
180 CXXFunctionSummaryFormat::FormatObject (ValueObject *valobj,
181                                         std::string& dest,
182                                         const TypeSummaryOptions& options)
183 {
184     dest.clear();
185     StreamString stream;
186     if (!m_impl || m_impl(*valobj,stream,options) == false)
187         return false;
188     dest.assign(stream.GetData());
189     return true;
190 }
191
192 std::string
193 CXXFunctionSummaryFormat::GetDescription ()
194 {
195     StreamString sstr;
196     sstr.Printf ("`%s (%p) `%s%s%s%s%s%s%s", m_description.c_str(),
197                  static_cast<void*>(&m_impl),
198                  Cascades() ? "" : " (not cascading)",
199                  !DoesPrintChildren(nullptr) ? "" : " (show children)",
200                  !DoesPrintValue(nullptr) ? " (hide value)" : "",
201                  IsOneLiner() ? " (one-line printout)" : "",
202                  SkipsPointers() ? " (skip pointers)" : "",
203                  SkipsReferences() ? " (skip references)" : "",
204                  HideNames(nullptr) ? " (hide member names)" : "");
205     return sstr.GetString();
206 }
207
208 #ifndef LLDB_DISABLE_PYTHON
209
210
211 ScriptSummaryFormat::ScriptSummaryFormat (const TypeSummaryImpl::Flags& flags,
212                                           const char * function_name,
213                                           const char * python_script) :
214 TypeSummaryImpl(flags),
215 m_function_name(),
216 m_python_script(),
217 m_script_function_sp()
218 {
219     if (function_name)
220         m_function_name.assign(function_name);
221     if (python_script)
222         m_python_script.assign(python_script);
223 }
224
225 bool
226 ScriptSummaryFormat::FormatObject (ValueObject *valobj,
227                                    std::string& retval,
228                                    const TypeSummaryOptions& options)
229 {
230     Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
231     
232     if (!valobj)
233         return false;
234     
235     Host::SetCrashDescriptionWithFormat("[Python summary] Name: %s - Function: %s",
236                                         valobj->GetName().AsCString("unknown"),
237                                         m_function_name.c_str());
238
239     TargetSP target_sp(valobj->GetTargetSP());
240     
241     if (!target_sp)
242     {
243         retval.assign("error: no target");
244         return false;
245     }
246     
247     ScriptInterpreter *script_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
248     
249     if (!script_interpreter)
250     {
251         retval.assign("error: no ScriptInterpreter");
252         return false;
253     }
254     
255     return script_interpreter->GetScriptedSummary(m_function_name.c_str(),
256                                                   valobj->GetSP(),
257                                                   m_script_function_sp,
258                                                   options,
259                                                   retval);
260     
261 }
262
263 std::string
264 ScriptSummaryFormat::GetDescription ()
265 {
266     StreamString sstr;
267     sstr.Printf ("%s%s%s%s%s%s%s\n%s",       Cascades() ? "" : " (not cascading)",
268                  !DoesPrintChildren(nullptr) ? "" : " (show children)",
269                  !DoesPrintValue(nullptr) ? " (hide value)" : "",
270                  IsOneLiner() ? " (one-line printout)" : "",
271                  SkipsPointers() ? " (skip pointers)" : "",
272                  SkipsReferences() ? " (skip references)" : "",
273                  HideNames(nullptr) ? " (hide member names)" : "",
274                  m_python_script.c_str());
275     return sstr.GetString();
276     
277 }
278
279 #endif // #ifndef LLDB_DISABLE_PYTHON