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