LLDB mainline
SBTypeSummary.cpp
Go to the documentation of this file.
1//===-- SBTypeSummary.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#include "Utils.h"
11#include "lldb/API/SBStream.h"
12#include "lldb/API/SBValue.h"
16
17#include "llvm/Support/Casting.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
24
25 m_opaque_up = std::make_unique<TypeSummaryOptions>();
26}
27
34
36
39 return this->operator bool();
40}
41SBTypeSummaryOptions::operator bool() const {
43
44 return m_opaque_up.get();
45}
46
54
62
64 LLDB_INSTRUMENT_VA(this, l);
65
66 if (IsValid())
67 m_opaque_up->SetLanguage(l);
68}
69
71 LLDB_INSTRUMENT_VA(this, c);
72
73 if (IsValid())
74 m_opaque_up->SetCapping(c);
75}
76
80
85
89
93
97
99 const lldb_private::TypeSummaryOptions &lldb_object)
100 : m_opaque_up(std::make_unique<TypeSummaryOptions>(lldb_object)) {
101 LLDB_INSTRUMENT_VA(this, lldb_object);
102}
103
105
107 uint32_t options) {
108 LLDB_INSTRUMENT_VA(data, options);
109
110 if (!data || data[0] == 0)
111 return SBTypeSummary();
112
113 return SBTypeSummary(
114 TypeSummaryImplSP(new StringSummaryFormat(options, data)));
115}
116
118 uint32_t options) {
119 LLDB_INSTRUMENT_VA(data, options);
120
121 if (!data || data[0] == 0)
122 return SBTypeSummary();
123
124 return SBTypeSummary(
125 TypeSummaryImplSP(new ScriptSummaryFormat(options, data)));
126}
127
129 uint32_t options) {
130 LLDB_INSTRUMENT_VA(data, options);
131
132 if (!data || data[0] == 0)
133 return SBTypeSummary();
134
135 return SBTypeSummary(
136 TypeSummaryImplSP(new ScriptSummaryFormat(options, "", data)));
137}
138
140 uint32_t options) {
141 LLDB_INSTRUMENT_VA(data, options);
142
143 if (!data || data[0] == 0)
144 return SBTypeSummary();
145
146 return SBTypeSummary(
147 TypeSummaryImplSP(new ScriptedSummaryFormat(options, data)));
148}
149
151 uint32_t options,
152 const char *description) {
153 LLDB_INSTRUMENT_VA(cb, options, description);
154
155 SBTypeSummary retval;
156 if (cb) {
158 options,
159 [cb](ValueObject &valobj, Stream &stm,
160 const TypeSummaryOptions &opt) -> bool {
161 SBStream stream;
162 SBValue sb_value(valobj.GetSP());
163 SBTypeSummaryOptions options(opt);
164 if (!cb(sb_value, options, stream))
165 return false;
166 llvm::StringRef sdata = stream.GetString();
167 stm.Write(sdata.data(), sdata.size());
168 return true;
169 },
170 description ? description : "callback summary formatter")));
171 }
172
173 return retval;
174}
175
180
182
184 LLDB_INSTRUMENT_VA(this);
185 return this->operator bool();
186}
187SBTypeSummary::operator bool() const {
188 LLDB_INSTRUMENT_VA(this);
189
190 return m_opaque_sp.get() != nullptr;
191}
192
194 LLDB_INSTRUMENT_VA(this);
195
196 if (!IsValid())
197 return false;
198 if (ScriptSummaryFormat *script_summary_ptr =
199 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
200 const char *ftext = script_summary_ptr->GetPythonScript();
201 return (ftext && *ftext != 0);
202 }
203 return false;
204}
205
207 LLDB_INSTRUMENT_VA(this);
208
209 if (!IsValid())
210 return false;
211 if (ScriptSummaryFormat *script_summary_ptr =
212 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
213 const char *ftext = script_summary_ptr->GetPythonScript();
214 return (!ftext || *ftext == 0);
215 }
216 return false;
217}
218
220 LLDB_INSTRUMENT_VA(this);
221
222 if (!IsValid())
223 return false;
224
226}
227
229 LLDB_INSTRUMENT_VA(this);
230
231 if (!IsValid())
232 return nullptr;
233 if (ScriptSummaryFormat *script_summary_ptr =
234 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
235 const char *fname = script_summary_ptr->GetFunctionName();
236 const char *ftext = script_summary_ptr->GetPythonScript();
237 if (ftext && *ftext)
238 return StringPool::GetSystemPool().Intern(ftext);
239 return StringPool::GetSystemPool().Intern(fname);
240 } else if (StringSummaryFormat *string_summary_ptr =
241 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get()))
243 string_summary_ptr->GetSummaryString());
244 return nullptr;
245}
246
248 LLDB_INSTRUMENT_VA(this);
249
250 if (!IsValid())
251 return 0;
252 return m_opaque_sp->GetPtrMatchDepth();
253}
254
255void SBTypeSummary::SetPtrMatchDepth(uint32_t ptr_match_depth) {
256 LLDB_INSTRUMENT_VA(this);
257
258 if (!IsValid())
259 return;
260 return m_opaque_sp->SetPtrMatchDepth(ptr_match_depth);
261}
262
264 LLDB_INSTRUMENT_VA(this);
265
266 if (!IsValid())
267 return lldb::eTypeOptionNone;
268 return m_opaque_sp->GetOptions();
269}
270
271void SBTypeSummary::SetOptions(uint32_t value) {
272 LLDB_INSTRUMENT_VA(this, value);
273
274 if (!CopyOnWrite_Impl())
275 return;
276 m_opaque_sp->SetOptions(value);
277}
278
279void SBTypeSummary::SetSummaryString(const char *data) {
280 LLDB_INSTRUMENT_VA(this, data);
281
282 if (!IsValid())
283 return;
284 if (!llvm::isa<StringSummaryFormat>(m_opaque_sp.get()))
285 ChangeSummaryType(false);
286 if (StringSummaryFormat *string_summary_ptr =
287 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get()))
288 string_summary_ptr->SetSummaryString(data);
289}
290
291void SBTypeSummary::SetFunctionName(const char *data) {
292 LLDB_INSTRUMENT_VA(this, data);
293
294 if (!IsValid())
295 return;
296 if (!llvm::isa<ScriptSummaryFormat>(m_opaque_sp.get()))
297 ChangeSummaryType(true);
298 if (ScriptSummaryFormat *script_summary_ptr =
299 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get()))
300 script_summary_ptr->SetFunctionName(data);
301}
302
303void SBTypeSummary::SetFunctionCode(const char *data) {
304 LLDB_INSTRUMENT_VA(this, data);
305
306 if (!IsValid())
307 return;
308 if (!llvm::isa<ScriptSummaryFormat>(m_opaque_sp.get()))
309 ChangeSummaryType(true);
310 if (ScriptSummaryFormat *script_summary_ptr =
311 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get()))
312 script_summary_ptr->SetPythonScript(data);
313}
314
316 lldb::DescriptionLevel description_level) {
317 LLDB_INSTRUMENT_VA(this, description, description_level);
318
319 if (!CopyOnWrite_Impl())
320 return false;
321 else {
322 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
323 return true;
324 }
325}
326
328 LLDB_INSTRUMENT_VA(this, value);
329
330 if (!IsValid())
331 return false;
332 lldb::ValueObjectSP value_sp = value.GetSP();
333 return m_opaque_sp->DoesPrintValue(value_sp.get());
334}
335
337 LLDB_INSTRUMENT_VA(this, rhs);
338
339 if (this != &rhs) {
341 }
342 return *this;
343}
344
346 LLDB_INSTRUMENT_VA(this, rhs);
347
348 if (!IsValid())
349 return !rhs.IsValid();
350 return m_opaque_sp == rhs.m_opaque_sp;
351}
352
354 LLDB_INSTRUMENT_VA(this, rhs);
355
356 if (IsValid()) {
357 // valid and invalid are different
358 if (!rhs.IsValid())
359 return false;
360 } else {
361 // invalid and valid are different
362 if (rhs.IsValid())
363 return false;
364 else
365 // both invalid are the same
366 return true;
367 }
368
369 if (m_opaque_sp->GetKind() != rhs.m_opaque_sp->GetKind())
370 return false;
371
372 switch (m_opaque_sp->GetKind()) {
374 return llvm::dyn_cast<CXXFunctionSummaryFormat>(m_opaque_sp.get()) ==
375 llvm::dyn_cast<CXXFunctionSummaryFormat>(rhs.m_opaque_sp.get());
378 if (IsFunctionCode() != rhs.IsFunctionCode())
379 return false;
380 if (IsFunctionName() != rhs.IsFunctionName())
381 return false;
382 return GetOptions() == rhs.GetOptions();
384 if (IsSummaryString() != rhs.IsSummaryString())
385 return false;
386 return GetOptions() == rhs.GetOptions();
388 return (m_opaque_sp.get() == rhs.m_opaque_sp.get());
390 ScriptedSummaryFormat *lhs_ptr =
391 llvm::dyn_cast<ScriptedSummaryFormat>(m_opaque_sp.get());
392 ScriptedSummaryFormat *rhs_ptr =
393 llvm::dyn_cast<ScriptedSummaryFormat>(rhs.m_opaque_sp.get());
394 if (!lhs_ptr || !rhs_ptr)
395 return false;
396 return strcmp(lhs_ptr->GetClassName(), rhs_ptr->GetClassName()) == 0 &&
397 GetOptions() == rhs.GetOptions();
398 }
399 }
400
401 return false;
402}
403
405 LLDB_INSTRUMENT_VA(this, rhs);
406
407 if (!IsValid())
408 return rhs.IsValid();
409 return m_opaque_sp != rhs.m_opaque_sp;
410}
411
413
414void SBTypeSummary::SetSP(const lldb::TypeSummaryImplSP &typesummary_impl_sp) {
415 m_opaque_sp = typesummary_impl_sp;
416}
417
419 : m_opaque_sp(typesummary_impl_sp) {}
420
422 if (!IsValid())
423 return false;
424
425 if (m_opaque_sp.use_count() == 1)
426 return true;
427
428 TypeSummaryImplSP new_sp;
429
430 if (CXXFunctionSummaryFormat *current_summary_ptr =
431 llvm::dyn_cast<CXXFunctionSummaryFormat>(m_opaque_sp.get())) {
433 GetOptions(), current_summary_ptr->m_impl,
434 current_summary_ptr->m_description.c_str()));
435 } else if (ScriptSummaryFormat *current_summary_ptr =
436 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
438 GetOptions(), current_summary_ptr->GetFunctionName(),
439 current_summary_ptr->GetPythonScript()));
440 } else if (StringSummaryFormat *current_summary_ptr =
441 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get())) {
443 GetOptions(), current_summary_ptr->GetSummaryString()));
444 } else if (ScriptedSummaryFormat *current_summary_ptr =
445 llvm::dyn_cast<ScriptedSummaryFormat>(m_opaque_sp.get())) {
447 GetOptions(), current_summary_ptr->GetClassName()));
448 }
449
450 SetSP(new_sp);
451
452 return nullptr != new_sp.get();
453}
454
455bool SBTypeSummary::ChangeSummaryType(bool want_script) {
456 if (!IsValid())
457 return false;
458
459 TypeSummaryImplSP new_sp;
460
461 if (want_script ==
463 if (m_opaque_sp->GetKind() ==
465 !want_script)
467 else
468 return CopyOnWrite_Impl();
469 }
470
471 if (!new_sp) {
472 if (want_script)
473 new_sp = TypeSummaryImplSP(new ScriptSummaryFormat(GetOptions(), "", ""));
474 else
476 }
477
478 SetSP(new_sp);
479
480 return true;
481}
#define LLDB_INSTRUMENT_VA(...)
const char * GetString() const
Exists so friend classes can use the StringString's data without putting it in the ConstString pool.
Definition SBStream.cpp:201
lldb::LanguageType GetLanguage()
void SetLanguage(lldb::LanguageType)
lldb_private::TypeSummaryOptions & ref()
lldb::TypeSummaryCapping GetCapping()
void SetCapping(lldb::TypeSummaryCapping)
std::unique_ptr< lldb_private::TypeSummaryOptions > m_opaque_up
lldb_private::TypeSummaryOptions * get()
lldb_private::TypeSummaryOptions * operator->()
static SBTypeSummary CreateWithFunctionName(const char *data, uint32_t options=0)
void SetSummaryString(const char *data)
void SetOptions(uint32_t)
lldb::SBTypeSummary & operator=(const lldb::SBTypeSummary &rhs)
static SBTypeSummary CreateWithCallback(FormatCallback cb, uint32_t options=0, const char *description=nullptr)
static SBTypeSummary CreateWithSummaryString(const char *data, uint32_t options=0)
bool operator==(lldb::SBTypeSummary &rhs)
bool ChangeSummaryType(bool want_script)
void SetSP(const lldb::TypeSummaryImplSP &typefilter_impl_sp)
bool(* FormatCallback)(SBValue, SBTypeSummaryOptions, SBStream &)
void SetPtrMatchDepth(uint32_t ptr_match_depth)
void SetFunctionCode(const char *data)
static SBTypeSummary CreateWithClassName(const char *data, uint32_t options=0)
static SBTypeSummary CreateWithScriptCode(const char *data, uint32_t options=0)
bool operator!=(lldb::SBTypeSummary &rhs)
bool IsEqualTo(lldb::SBTypeSummary &rhs)
lldb::TypeSummaryImplSP m_opaque_sp
const char * GetData()
bool DoesPrintValue(lldb::SBValue value)
void SetFunctionName(const char *data)
bool GetDescription(lldb::SBStream &description, lldb::DescriptionLevel description_level)
lldb::TypeSummaryImplSP GetSP()
lldb::ValueObjectSP GetSP() const
Same as the protected version of GetSP that takes a locker, except that we make the locker locally in...
Definition SBValue.cpp:1084
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition Stream.h:111
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
lldb::ValueObjectSP GetSP()
A class that represents a running process on the host machine.
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition Utils.h:25
TypeSummaryCapping
Whether a summary should cap how much data it returns to users or not.
std::shared_ptr< lldb_private::TypeSummaryImpl > TypeSummaryImplSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
const char * GetClassName() const