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 
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-public.h"
16 
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Target.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 TypeSummaryOptions::TypeSummaryOptions() = default;
30 
31 lldb::LanguageType TypeSummaryOptions::GetLanguage() const { return m_lang; }
32 
33 lldb::TypeSummaryCapping TypeSummaryOptions::GetCapping() const {
34  return m_capping;
35 }
36 
37 TypeSummaryOptions &TypeSummaryOptions::SetLanguage(lldb::LanguageType lang) {
38  m_lang = lang;
39  return *this;
40 }
41 
43 TypeSummaryOptions::SetCapping(lldb::TypeSummaryCapping cap) {
44  m_capping = cap;
45  return *this;
46 }
47 
48 TypeSummaryImpl::TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags)
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 
57 void StringSummaryFormat::SetSummaryString(const char *format_cstr) {
58  m_format.Clear();
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 
69  const TypeSummaryOptions &options) {
70  if (!valobj) {
71  retval.assign("NULL ValueObject");
72  return false;
73  }
74 
75  StreamString s;
76  ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
77  SymbolContext sc;
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 
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 }
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::CXXFunctionSummaryFormat::FormatObject
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
Definition: TypeSummary.cpp:121
lldb-public.h
lldb_private::TypeSummaryImpl::Cascades
bool Cascades() const
Definition: TypeSummary.h:202
lldb_private::TypeSummaryImpl
Definition: TypeSummary.h:45
lldb_private::ScriptSummaryFormat::m_python_script
std::string m_python_script
Definition: TypeSummary.h:354
lldb_private::TypeSummaryImpl::Flags
Definition: TypeSummary.h:53
lldb_private::CXXFunctionSummaryFormat::m_impl
Callback m_impl
Definition: TypeSummary.h:313
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb_private::ExecutionContext::GetFramePtr
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Definition: ExecutionContext.h:408
StackFrame.h
lldb_private::StringSummaryFormat::StringSummaryFormat
StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f)
Definition: TypeSummary.cpp:51
Debugger.h
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
lldb_private::ValueObjectPrinter::PrintChildrenOneLiner
bool PrintChildrenOneLiner(bool hide_names)
Definition: ValueObjectPrinter.cpp:712
lldb_private::CXXFunctionSummaryFormat::m_description
std::string m_description
Definition: TypeSummary.h:314
lldb_private::TypeSummaryImpl::DoesPrintChildren
virtual bool DoesPrintChildren(ValueObject *valobj) const
Definition: TypeSummary.h:210
lldb_private::StringSummaryFormat::m_format_str
std::string m_format_str
Definition: TypeSummary.h:279
lldb_private::ValueObjectPrinter
Definition: ValueObjectPrinter.h:23
Target.h
lldb_private::ScriptSummaryFormat::GetDescription
std::string GetDescription() override
Definition: TypeSummary.cpp:181
lldb_private::ValueObject::GetSP
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:555
lldb_private::ScriptSummaryFormat::ScriptSummaryFormat
ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *function_name, const char *python_script=nullptr)
Definition: TypeSummary.cpp:145
lldb_private::Status::Fail
bool Fail() const
Test for error condition.
Definition: Status.cpp:182
lldb_private::TypeSummaryImpl::SkipsReferences
bool SkipsReferences() const
Definition: TypeSummary.h:206
StreamString.h
lldb::TypeSummaryCapping
TypeSummaryCapping
Whether a summary should cap how much data it returns to users or not.
Definition: lldb-enumerations.h:1116
lldb_private::StreamString
Definition: StreamString.h:23
lldb-enumerations.h
ValueObjectPrinter.h
lldb_private::ValueObject::GetExecutionContextRef
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldb_private::ScriptInterpreter::GetScriptedSummary
virtual bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj, StructuredData::ObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options, std::string &retval)
Definition: ScriptInterpreter.h:410
lldb_private::TypeSummaryImpl::IsOneLiner
bool IsOneLiner() const
Definition: TypeSummary.h:222
CompilerType.h
ValueObject.h
lldb_private::FormatEntity::Parse
static Status Parse(const llvm::StringRef &format, Entry &entry)
Definition: FormatEntity.cpp:290
lldb_private::FormatEntity::Format
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)
Definition: FormatEntity.cpp:1080
lldb_private::StringSummaryFormat::m_format
FormatEntity::Entry m_format
Definition: TypeSummary.h:280
lldb_private::TypeSummaryImpl::DoesPrintValue
virtual bool DoesPrintValue(ValueObject *valobj) const
Definition: TypeSummary.h:218
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
TypeSummary.h
lldb_private::StringSummaryFormat::FormatObject
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
Definition: TypeSummary.cpp:68
lldb_private::StringSummaryFormat::m_error
Status m_error
Definition: TypeSummary.h:281
lldb_private::LineEntry::range
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:139
lldb_private::CXXFunctionSummaryFormat::Callback
std::function< bool(ValueObject &, Stream &, const TypeSummaryOptions &)> Callback
Definition: TypeSummary.h:311
lldb_private::TypeSummaryOptions
Definition: TypeSummary.h:26
lldb_private::ScriptInterpreter
Definition: ScriptInterpreter.h:125
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::StringSummaryFormat::SetSummaryString
void SetSummaryString(const char *f)
Definition: TypeSummary.cpp:57
lldb_private::DumpValueObjectOptions
Definition: DumpValueObjectOptions.h:22
lldb_private::TypeSummaryImpl::Kind
Kind
Definition: TypeSummary.h:47
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::TypeSummaryImpl::SkipsPointers
bool SkipsPointers() const
Definition: TypeSummary.h:204
lldb_private::Status::Clear
void Clear()
Clear the object state.
Definition: Status.cpp:168
CommandInterpreter.h
lldb_private::ValueObject::GetTargetSP
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
lldb_private::ScriptSummaryFormat::m_function_name
std::string m_function_name
Definition: TypeSummary.h:353
lldb_private::ScriptSummaryFormat::FormatObject
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
Definition: TypeSummary.cpp:156
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::StringSummaryFormat::GetDescription
std::string GetDescription() override
Definition: TypeSummary.cpp:100
lldb_private::SymbolContext::line_entry
LineEntry line_entry
The LineEntry for a given query.
Definition: SymbolContext.h:322
lldb_private::ScriptSummaryFormat::m_script_function_sp
StructuredData::ObjectSP m_script_function_sp
Definition: TypeSummary.h:355
lldb_private::StackFrame
Definition: StackFrame.h:40
lldb_private::FormatEntity::Entry::Clear
void Clear()
Definition: FormatEntity.h:164
lldb_private::CXXFunctionSummaryFormat::GetDescription
std::string GetDescription() override
Definition: TypeSummary.cpp:132
lldb
Definition: SBAddress.h:15
lldb_private::CXXFunctionSummaryFormat::CXXFunctionSummaryFormat
CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl, const char *description)
Definition: TypeSummary.cpp:116
lldb_private::StackFrame::GetSymbolContext
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
Definition: StackFrame.cpp:298
lldb_private::TypeSummaryImpl::HideNames
virtual bool HideNames(ValueObject *valobj) const
Definition: TypeSummary.h:224
lldb_private::Status::AsCString
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:131