]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/TypeSummary.cpp
Merge latest (commit c8c1b3a77934768c7f7a4a9c10140c8bec529059) files
[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 TypeSummaryImpl::TypeSummaryImpl (const TypeSummaryImpl::Flags& flags) :
38 m_flags(flags)
39 {
40 }
41
42
43 StringSummaryFormat::StringSummaryFormat (const TypeSummaryImpl::Flags& flags,
44                                           const char *format_cstr) :
45 TypeSummaryImpl(flags),
46 m_format()
47 {
48     if (format_cstr)
49         m_format.assign(format_cstr);
50 }
51
52 bool
53 StringSummaryFormat::FormatObject (ValueObject *valobj,
54                                    std::string& retval)
55 {
56     if (!valobj)
57     {
58         retval.assign("NULL ValueObject");
59         return false;
60     }
61     
62     StreamString s;
63     ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
64     SymbolContext sc;
65     StackFrame *frame = exe_ctx.GetFramePtr();
66     if (frame)
67         sc = frame->GetSymbolContext(lldb::eSymbolContextEverything);
68     
69     if (IsOneLiner())
70     {
71         ValueObjectPrinter printer(valobj,&s,DumpValueObjectOptions());
72         printer.PrintChildrenOneLiner(HideNames(valobj));
73         retval.assign(s.GetData());
74         return true;
75     }
76     else
77     {
78         if (Debugger::FormatPrompt(m_format.c_str(), &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, valobj))
79         {
80             retval.assign(s.GetString());
81             return true;
82         }
83         else
84         {
85             retval.assign("error: summary string parsing error");
86             return false;
87         }
88     }
89 }
90
91 std::string
92 StringSummaryFormat::GetDescription ()
93 {
94     StreamString sstr;
95     
96     sstr.Printf ("`%s`%s%s%s%s%s%s%s",      m_format.c_str(),
97                  Cascades() ? "" : " (not cascading)",
98                  !DoesPrintChildren(nullptr) ? "" : " (show children)",
99                  !DoesPrintValue(nullptr) ? " (hide value)" : "",
100                  IsOneLiner() ? " (one-line printout)" : "",
101                  SkipsPointers() ? " (skip pointers)" : "",
102                  SkipsReferences() ? " (skip references)" : "",
103                  HideNames(nullptr) ? " (hide member names)" : "");
104     return sstr.GetString();
105 }
106
107 CXXFunctionSummaryFormat::CXXFunctionSummaryFormat (const TypeSummaryImpl::Flags& flags,
108                                                     Callback impl,
109                                                     const char* description) :
110 TypeSummaryImpl(flags),
111 m_impl(impl),
112 m_description(description ? description : "")
113 {
114 }
115
116 bool
117 CXXFunctionSummaryFormat::FormatObject (ValueObject *valobj,
118                                         std::string& dest)
119 {
120     dest.clear();
121     StreamString stream;
122     if (!m_impl || m_impl(*valobj,stream) == false)
123         return false;
124     dest.assign(stream.GetData());
125     return true;
126 }
127
128 std::string
129 CXXFunctionSummaryFormat::GetDescription ()
130 {
131     StreamString sstr;
132     sstr.Printf ("`%s (%p) `%s%s%s%s%s%s%s", m_description.c_str(),
133                  static_cast<void*>(&m_impl),
134                  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     return sstr.GetString();
142 }
143
144 #ifndef LLDB_DISABLE_PYTHON
145
146
147 ScriptSummaryFormat::ScriptSummaryFormat (const TypeSummaryImpl::Flags& flags,
148                                           const char * function_name,
149                                           const char * python_script) :
150 TypeSummaryImpl(flags),
151 m_function_name(),
152 m_python_script(),
153 m_script_function_sp()
154 {
155     if (function_name)
156         m_function_name.assign(function_name);
157     if (python_script)
158         m_python_script.assign(python_script);
159 }
160
161 bool
162 ScriptSummaryFormat::FormatObject (ValueObject *valobj,
163                                    std::string& retval)
164 {
165     Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
166     
167     if (!valobj)
168         return false;
169     
170     Host::SetCrashDescriptionWithFormat("[Python summary] Name: %s - Function: %s",
171                                         valobj->GetName().AsCString("unknown"),
172                                         m_function_name.c_str());
173
174     TargetSP target_sp(valobj->GetTargetSP());
175     
176     if (!target_sp)
177     {
178         retval.assign("error: no target");
179         return false;
180     }
181     
182     ScriptInterpreter *script_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
183     
184     if (!script_interpreter)
185     {
186         retval.assign("error: no ScriptInterpreter");
187         return false;
188     }
189     
190     return script_interpreter->GetScriptedSummary(m_function_name.c_str(),
191                                                   valobj->GetSP(),
192                                                   m_script_function_sp,
193                                                   retval);
194     
195 }
196
197 std::string
198 ScriptSummaryFormat::GetDescription ()
199 {
200     StreamString sstr;
201     sstr.Printf ("%s%s%s%s%s%s%s\n%s",       Cascades() ? "" : " (not cascading)",
202                  !DoesPrintChildren(nullptr) ? "" : " (show children)",
203                  !DoesPrintValue(nullptr) ? " (hide value)" : "",
204                  IsOneLiner() ? " (one-line printout)" : "",
205                  SkipsPointers() ? " (skip pointers)" : "",
206                  SkipsReferences() ? " (skip references)" : "",
207                  HideNames(nullptr) ? " (hide member names)" : "",
208                  m_python_script.c_str());
209     return sstr.GetString();
210     
211 }
212
213 #endif // #ifndef LLDB_DISABLE_PYTHON