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"
15
16#include "llvm/Support/Casting.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
23
24 m_opaque_up = std::make_unique<TypeSummaryOptions>();
25}
26
33
35
38 return this->operator bool();
39}
40SBTypeSummaryOptions::operator bool() const {
42
43 return m_opaque_up.get();
44}
45
53
61
63 LLDB_INSTRUMENT_VA(this, l);
64
65 if (IsValid())
66 m_opaque_up->SetLanguage(l);
67}
68
70 LLDB_INSTRUMENT_VA(this, c);
71
72 if (IsValid())
73 m_opaque_up->SetCapping(c);
74}
75
79
84
88
92
96
98 const lldb_private::TypeSummaryOptions &lldb_object)
99 : m_opaque_up(std::make_unique<TypeSummaryOptions>(lldb_object)) {
100 LLDB_INSTRUMENT_VA(this, lldb_object);
101}
102
104
106 uint32_t options) {
107 LLDB_INSTRUMENT_VA(data, options);
108
109 if (!data || data[0] == 0)
110 return SBTypeSummary();
111
112 return SBTypeSummary(
113 TypeSummaryImplSP(new StringSummaryFormat(options, data)));
114}
115
117 uint32_t options) {
118 LLDB_INSTRUMENT_VA(data, options);
119
120 if (!data || data[0] == 0)
121 return SBTypeSummary();
122
123 return SBTypeSummary(
124 TypeSummaryImplSP(new ScriptSummaryFormat(options, data)));
125}
126
128 uint32_t options) {
129 LLDB_INSTRUMENT_VA(data, options);
130
131 if (!data || data[0] == 0)
132 return SBTypeSummary();
133
134 return SBTypeSummary(
135 TypeSummaryImplSP(new ScriptSummaryFormat(options, "", data)));
136}
137
139 uint32_t options) {
140 LLDB_INSTRUMENT_VA(data, options);
141
142 if (!data || data[0] == 0)
143 return SBTypeSummary();
144
145 return SBTypeSummary(
146 TypeSummaryImplSP(new ScriptedSummaryFormat(options, data)));
147}
148
150 uint32_t options,
151 const char *description) {
152 LLDB_INSTRUMENT_VA(cb, options, description);
153
154 SBTypeSummary retval;
155 if (cb) {
157 options,
158 [cb](ValueObject &valobj, Stream &stm,
159 const TypeSummaryOptions &opt) -> bool {
160 SBStream stream;
161 SBValue sb_value(valobj.GetSP());
162 SBTypeSummaryOptions options(opt);
163 if (!cb(sb_value, options, stream))
164 return false;
165 llvm::StringRef sdata = stream.GetString();
166 stm.Write(sdata.data(), sdata.size());
167 return true;
168 },
169 description ? description : "callback summary formatter")));
170 }
171
172 return retval;
173}
174
179
181
183 LLDB_INSTRUMENT_VA(this);
184 return this->operator bool();
185}
186SBTypeSummary::operator bool() const {
187 LLDB_INSTRUMENT_VA(this);
188
189 return m_opaque_sp.get() != nullptr;
190}
191
193 LLDB_INSTRUMENT_VA(this);
194
195 if (!IsValid())
196 return false;
197 if (ScriptSummaryFormat *script_summary_ptr =
198 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
199 const char *ftext = script_summary_ptr->GetPythonScript();
200 return (ftext && *ftext != 0);
201 }
202 return false;
203}
204
206 LLDB_INSTRUMENT_VA(this);
207
208 if (!IsValid())
209 return false;
210 if (ScriptSummaryFormat *script_summary_ptr =
211 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
212 const char *ftext = script_summary_ptr->GetPythonScript();
213 return (!ftext || *ftext == 0);
214 }
215 return false;
216}
217
219 LLDB_INSTRUMENT_VA(this);
220
221 if (!IsValid())
222 return false;
223
225}
226
228 LLDB_INSTRUMENT_VA(this);
229
230 if (!IsValid())
231 return nullptr;
232 if (ScriptSummaryFormat *script_summary_ptr =
233 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
234 const char *fname = script_summary_ptr->GetFunctionName();
235 const char *ftext = script_summary_ptr->GetPythonScript();
236 if (ftext && *ftext)
237 return ConstString(ftext).GetCString();
238 return ConstString(fname).GetCString();
239 } else if (StringSummaryFormat *string_summary_ptr =
240 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get()))
241 return ConstString(string_summary_ptr->GetSummaryString()).GetCString();
242 return nullptr;
243}
244
246 LLDB_INSTRUMENT_VA(this);
247
248 if (!IsValid())
249 return 0;
250 return m_opaque_sp->GetPtrMatchDepth();
251}
252
253void SBTypeSummary::SetPtrMatchDepth(uint32_t ptr_match_depth) {
254 LLDB_INSTRUMENT_VA(this);
255
256 if (!IsValid())
257 return;
258 return m_opaque_sp->SetPtrMatchDepth(ptr_match_depth);
259}
260
262 LLDB_INSTRUMENT_VA(this);
263
264 if (!IsValid())
265 return lldb::eTypeOptionNone;
266 return m_opaque_sp->GetOptions();
267}
268
269void SBTypeSummary::SetOptions(uint32_t value) {
270 LLDB_INSTRUMENT_VA(this, value);
271
272 if (!CopyOnWrite_Impl())
273 return;
274 m_opaque_sp->SetOptions(value);
275}
276
277void SBTypeSummary::SetSummaryString(const char *data) {
278 LLDB_INSTRUMENT_VA(this, data);
279
280 if (!IsValid())
281 return;
282 if (!llvm::isa<StringSummaryFormat>(m_opaque_sp.get()))
283 ChangeSummaryType(false);
284 if (StringSummaryFormat *string_summary_ptr =
285 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get()))
286 string_summary_ptr->SetSummaryString(data);
287}
288
289void SBTypeSummary::SetFunctionName(const char *data) {
290 LLDB_INSTRUMENT_VA(this, data);
291
292 if (!IsValid())
293 return;
294 if (!llvm::isa<ScriptSummaryFormat>(m_opaque_sp.get()))
295 ChangeSummaryType(true);
296 if (ScriptSummaryFormat *script_summary_ptr =
297 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get()))
298 script_summary_ptr->SetFunctionName(data);
299}
300
301void SBTypeSummary::SetFunctionCode(const char *data) {
302 LLDB_INSTRUMENT_VA(this, data);
303
304 if (!IsValid())
305 return;
306 if (!llvm::isa<ScriptSummaryFormat>(m_opaque_sp.get()))
307 ChangeSummaryType(true);
308 if (ScriptSummaryFormat *script_summary_ptr =
309 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get()))
310 script_summary_ptr->SetPythonScript(data);
311}
312
314 lldb::DescriptionLevel description_level) {
315 LLDB_INSTRUMENT_VA(this, description, description_level);
316
317 if (!CopyOnWrite_Impl())
318 return false;
319 else {
320 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
321 return true;
322 }
323}
324
326 LLDB_INSTRUMENT_VA(this, value);
327
328 if (!IsValid())
329 return false;
330 lldb::ValueObjectSP value_sp = value.GetSP();
331 return m_opaque_sp->DoesPrintValue(value_sp.get());
332}
333
335 LLDB_INSTRUMENT_VA(this, rhs);
336
337 if (this != &rhs) {
339 }
340 return *this;
341}
342
344 LLDB_INSTRUMENT_VA(this, rhs);
345
346 if (!IsValid())
347 return !rhs.IsValid();
348 return m_opaque_sp == rhs.m_opaque_sp;
349}
350
352 LLDB_INSTRUMENT_VA(this, rhs);
353
354 if (IsValid()) {
355 // valid and invalid are different
356 if (!rhs.IsValid())
357 return false;
358 } else {
359 // invalid and valid are different
360 if (rhs.IsValid())
361 return false;
362 else
363 // both invalid are the same
364 return true;
365 }
366
367 if (m_opaque_sp->GetKind() != rhs.m_opaque_sp->GetKind())
368 return false;
369
370 switch (m_opaque_sp->GetKind()) {
372 return llvm::dyn_cast<CXXFunctionSummaryFormat>(m_opaque_sp.get()) ==
373 llvm::dyn_cast<CXXFunctionSummaryFormat>(rhs.m_opaque_sp.get());
376 if (IsFunctionCode() != rhs.IsFunctionCode())
377 return false;
378 if (IsFunctionName() != rhs.IsFunctionName())
379 return false;
380 return GetOptions() == rhs.GetOptions();
382 if (IsSummaryString() != rhs.IsSummaryString())
383 return false;
384 return GetOptions() == rhs.GetOptions();
386 return (m_opaque_sp.get() == rhs.m_opaque_sp.get());
388 ScriptedSummaryFormat *lhs_ptr =
389 llvm::dyn_cast<ScriptedSummaryFormat>(m_opaque_sp.get());
390 ScriptedSummaryFormat *rhs_ptr =
391 llvm::dyn_cast<ScriptedSummaryFormat>(rhs.m_opaque_sp.get());
392 if (!lhs_ptr || !rhs_ptr)
393 return false;
394 return strcmp(lhs_ptr->GetClassName(), rhs_ptr->GetClassName()) == 0 &&
395 GetOptions() == rhs.GetOptions();
396 }
397 }
398
399 return false;
400}
401
403 LLDB_INSTRUMENT_VA(this, rhs);
404
405 if (!IsValid())
406 return !rhs.IsValid();
407 return m_opaque_sp != rhs.m_opaque_sp;
408}
409
411
412void SBTypeSummary::SetSP(const lldb::TypeSummaryImplSP &typesummary_impl_sp) {
413 m_opaque_sp = typesummary_impl_sp;
414}
415
417 : m_opaque_sp(typesummary_impl_sp) {}
418
420 if (!IsValid())
421 return false;
422
423 if (m_opaque_sp.use_count() == 1)
424 return true;
425
426 TypeSummaryImplSP new_sp;
427
428 if (CXXFunctionSummaryFormat *current_summary_ptr =
429 llvm::dyn_cast<CXXFunctionSummaryFormat>(m_opaque_sp.get())) {
431 GetOptions(), current_summary_ptr->m_impl,
432 current_summary_ptr->m_description.c_str()));
433 } else if (ScriptSummaryFormat *current_summary_ptr =
434 llvm::dyn_cast<ScriptSummaryFormat>(m_opaque_sp.get())) {
436 GetOptions(), current_summary_ptr->GetFunctionName(),
437 current_summary_ptr->GetPythonScript()));
438 } else if (StringSummaryFormat *current_summary_ptr =
439 llvm::dyn_cast<StringSummaryFormat>(m_opaque_sp.get())) {
441 GetOptions(), current_summary_ptr->GetSummaryString()));
442 } else if (ScriptedSummaryFormat *current_summary_ptr =
443 llvm::dyn_cast<ScriptedSummaryFormat>(m_opaque_sp.get())) {
445 GetOptions(), current_summary_ptr->GetClassName()));
446 }
447
448 SetSP(new_sp);
449
450 return nullptr != new_sp.get();
451}
452
453bool SBTypeSummary::ChangeSummaryType(bool want_script) {
454 if (!IsValid())
455 return false;
456
457 TypeSummaryImplSP new_sp;
458
459 if (want_script ==
461 if (m_opaque_sp->GetKind() ==
463 !want_script)
465 else
466 return CopyOnWrite_Impl();
467 }
468
469 if (!new_sp) {
470 if (want_script)
471 new_sp = TypeSummaryImplSP(new ScriptSummaryFormat(GetOptions(), "", ""));
472 else
474 }
475
476 SetSP(new_sp);
477
478 return true;
479}
#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:1058
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
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
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:17
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