1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
//===-- ValueObjectSynthetic.cpp ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/ValueObject/ValueObjectSynthetic.h"
#include "lldb/Core/Value.h"
#include "lldb/DataFormatters/TypeSynthetic.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Status.h"
#include "lldb/ValueObject/ValueObject.h"
#include "llvm/ADT/STLExtras.h"
#include <optional>
namespace lldb_private {
class Declaration;
}
using namespace lldb_private;
class DummySyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
DummySyntheticFrontEnd(ValueObject &backend)
: SyntheticChildrenFrontEnd(backend) {}
llvm::Expected<uint32_t> CalculateNumChildren() override {
return m_backend.GetNumChildren();
}
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
return m_backend.GetChildAtIndex(idx);
}
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
return m_backend.GetIndexOfChildWithName(name);
}
bool MightHaveChildren() override { return m_backend.MightHaveChildren(); }
lldb::ChildCacheState Update() override {
return lldb::ChildCacheState::eRefetch;
}
};
ValueObjectSynthetic::ValueObjectSynthetic(ValueObject &parent,
lldb::SyntheticChildrenSP filter)
: ValueObject(parent), m_synth_sp(std::move(filter)), m_children_byindex(),
m_name_toindex(), m_synthetic_children_cache(),
m_synthetic_children_count(UINT32_MAX),
m_parent_type_name(parent.GetTypeName()),
m_might_have_children(eLazyBoolCalculate),
m_provides_value(eLazyBoolCalculate) {
SetName(parent.GetName());
// Copying the data of an incomplete type won't work as it has no byte size.
if (m_parent->GetCompilerType().IsCompleteType())
CopyValueData(m_parent);
CreateSynthFilter();
}
ValueObjectSynthetic::~ValueObjectSynthetic() = default;
CompilerType ValueObjectSynthetic::GetCompilerTypeImpl() {
return m_parent->GetCompilerType();
}
ConstString ValueObjectSynthetic::GetTypeName() {
return m_parent->GetTypeName();
}
ConstString ValueObjectSynthetic::GetQualifiedTypeName() {
return m_parent->GetQualifiedTypeName();
}
ConstString ValueObjectSynthetic::GetDisplayTypeName() {
if (ConstString synth_name = m_synth_filter_up->GetSyntheticTypeName())
return synth_name;
return m_parent->GetDisplayTypeName();
}
llvm::Expected<uint32_t>
ValueObjectSynthetic::CalculateNumChildren(uint32_t max) {
Log *log = GetLog(LLDBLog::DataFormatters);
UpdateValueIfNeeded();
if (m_synthetic_children_count < UINT32_MAX)
return m_synthetic_children_count <= max ? m_synthetic_children_count : max;
if (max < UINT32_MAX) {
auto num_children = m_synth_filter_up->CalculateNumChildren(max);
LLDB_LOGF(log,
"[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
"%s and type %s, the filter returned %u child values",
GetName().AsCString(), GetTypeName().AsCString(),
num_children ? *num_children : 0);
return num_children;
} else {
auto num_children_or_err = m_synth_filter_up->CalculateNumChildren(max);
if (!num_children_or_err) {
m_synthetic_children_count = 0;
return num_children_or_err;
}
auto num_children = (m_synthetic_children_count = *num_children_or_err);
LLDB_LOGF(log,
"[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
"%s and type %s, the filter returned %u child values",
GetName().AsCString(), GetTypeName().AsCString(), num_children);
return num_children;
}
}
lldb::ValueObjectSP
ValueObjectSynthetic::GetDynamicValue(lldb::DynamicValueType valueType) {
if (!m_parent)
return lldb::ValueObjectSP();
if (IsDynamic() && GetDynamicValueType() == valueType)
return GetSP();
return m_parent->GetDynamicValue(valueType);
}
bool ValueObjectSynthetic::MightHaveChildren() {
if (m_might_have_children == eLazyBoolCalculate)
m_might_have_children =
(m_synth_filter_up->MightHaveChildren() ? eLazyBoolYes : eLazyBoolNo);
return (m_might_have_children != eLazyBoolNo);
}
llvm::Expected<uint64_t> ValueObjectSynthetic::GetByteSize() {
return m_parent->GetByteSize();
}
lldb::ValueType ValueObjectSynthetic::GetValueType() const {
return m_parent->GetValueType();
}
void ValueObjectSynthetic::CreateSynthFilter() {
ValueObject *valobj_for_frontend = m_parent;
if (m_synth_sp->WantsDereference()) {
CompilerType type = m_parent->GetCompilerType();
if (type.IsValid() && type.IsPointerOrReferenceType()) {
Status error;
lldb::ValueObjectSP deref_sp = m_parent->Dereference(error);
if (error.Success())
valobj_for_frontend = deref_sp.get();
}
}
m_synth_filter_up = (m_synth_sp->GetFrontEnd(*valobj_for_frontend));
if (!m_synth_filter_up)
m_synth_filter_up = std::make_unique<DummySyntheticFrontEnd>(*m_parent);
}
bool ValueObjectSynthetic::UpdateValue() {
Log *log = GetLog(LLDBLog::DataFormatters);
SetValueIsValid(false);
m_error.Clear();
if (!m_parent->UpdateValueIfNeeded(false)) {
// our parent could not update.. as we are meaningless without a parent,
// just stop
if (m_parent->GetError().Fail())
m_error = m_parent->GetError().Clone();
return false;
}
// Regenerate the synthetic filter if our typename changes. When the (dynamic)
// type of an object changes, so does their synthetic filter of choice.
ConstString new_parent_type_name = m_parent->GetTypeName();
if (new_parent_type_name != m_parent_type_name) {
LLDB_LOGF(log,
"[ValueObjectSynthetic::UpdateValue] name=%s, type changed "
"from %s to %s, recomputing synthetic filter",
GetName().AsCString(), m_parent_type_name.AsCString(),
new_parent_type_name.AsCString());
m_parent_type_name = new_parent_type_name;
CreateSynthFilter();
}
// let our backend do its update
if (m_synth_filter_up->Update() == lldb::ChildCacheState::eRefetch) {
LLDB_LOGF(log,
"[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
"filter said caches are stale - clearing",
GetName().AsCString());
// filter said that cached values are stale
{
std::lock_guard<std::mutex> guard(m_child_mutex);
m_children_byindex.clear();
m_name_toindex.clear();
}
// usually, an object's value can change but this does not alter its
// children count for a synthetic VO that might indeed happen, so we need
// to tell the upper echelons that they need to come back to us asking for
// children
m_flags.m_children_count_valid = false;
{
std::lock_guard<std::mutex> guard(m_child_mutex);
m_synthetic_children_cache.clear();
}
m_synthetic_children_count = UINT32_MAX;
m_might_have_children = eLazyBoolCalculate;
} else {
LLDB_LOGF(log,
"[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
"filter said caches are still valid",
GetName().AsCString());
}
m_provides_value = eLazyBoolCalculate;
lldb::ValueObjectSP synth_val(m_synth_filter_up->GetSyntheticValue());
if (synth_val && synth_val->CanProvideValue()) {
LLDB_LOGF(log,
"[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
"filter said it can provide a value",
GetName().AsCString());
m_provides_value = eLazyBoolYes;
CopyValueData(synth_val.get());
} else {
LLDB_LOGF(log,
"[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
"filter said it will not provide a value",
GetName().AsCString());
m_provides_value = eLazyBoolNo;
// Copying the data of an incomplete type won't work as it has no byte size.
if (m_parent->GetCompilerType().IsCompleteType())
CopyValueData(m_parent);
}
SetValueIsValid(true);
return true;
}
lldb::ValueObjectSP ValueObjectSynthetic::GetChildAtIndex(uint32_t idx,
bool can_create) {
Log *log = GetLog(LLDBLog::DataFormatters);
LLDB_LOGF(log,
"[ValueObjectSynthetic::GetChildAtIndex] name=%s, retrieving "
"child at index %u",
GetName().AsCString(), idx);
UpdateValueIfNeeded();
ValueObject *valobj;
bool child_is_cached;
{
std::lock_guard<std::mutex> guard(m_child_mutex);
auto cached_child_it = m_children_byindex.find(idx);
child_is_cached = cached_child_it != m_children_byindex.end();
if (child_is_cached)
valobj = cached_child_it->second;
}
if (!child_is_cached) {
if (can_create && m_synth_filter_up != nullptr) {
LLDB_LOGF(log,
"[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
"index %u not cached and will be created",
GetName().AsCString(), idx);
lldb::ValueObjectSP synth_guy = m_synth_filter_up->GetChildAtIndex(idx);
LLDB_LOGF(
log,
"[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at index "
"%u created as %p (is "
"synthetic: %s)",
GetName().AsCString(), idx, static_cast<void *>(synth_guy.get()),
synth_guy.get()
? (synth_guy->IsSyntheticChildrenGenerated() ? "yes" : "no")
: "no");
if (!synth_guy)
return synth_guy;
{
std::lock_guard<std::mutex> guard(m_child_mutex);
if (synth_guy->IsSyntheticChildrenGenerated())
m_synthetic_children_cache.push_back(synth_guy);
m_children_byindex[idx] = synth_guy.get();
}
synth_guy->SetPreferredDisplayLanguageIfNeeded(
GetPreferredDisplayLanguage());
return synth_guy;
} else {
LLDB_LOGF(log,
"[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
"index %u not cached and cannot "
"be created (can_create = %s, synth_filter = %p)",
GetName().AsCString(), idx, can_create ? "yes" : "no",
static_cast<void *>(m_synth_filter_up.get()));
return lldb::ValueObjectSP();
}
} else {
LLDB_LOGF(log,
"[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
"index %u cached as %p",
GetName().AsCString(), idx, static_cast<void *>(valobj));
return valobj->GetSP();
}
}
lldb::ValueObjectSP
ValueObjectSynthetic::GetChildMemberWithName(llvm::StringRef name,
bool can_create) {
UpdateValueIfNeeded();
auto index_or_err = GetIndexOfChildWithName(name);
if (!index_or_err) {
llvm::consumeError(index_or_err.takeError());
return lldb::ValueObjectSP();
}
return GetChildAtIndex(*index_or_err, can_create);
}
llvm::Expected<size_t>
ValueObjectSynthetic::GetIndexOfChildWithName(llvm::StringRef name_ref) {
UpdateValueIfNeeded();
ConstString name(name_ref);
std::optional<uint32_t> found_index = std::nullopt;
{
std::lock_guard<std::mutex> guard(m_child_mutex);
auto name_to_index = m_name_toindex.find(name.GetCString());
if (name_to_index != m_name_toindex.end())
found_index = name_to_index->second;
}
if (!found_index && m_synth_filter_up != nullptr) {
auto index_or_err = m_synth_filter_up->GetIndexOfChildWithName(name);
if (!index_or_err)
return index_or_err.takeError();
std::lock_guard<std::mutex> guard(m_child_mutex);
m_name_toindex[name.GetCString()] = *index_or_err;
return *index_or_err;
} else if (!found_index && m_synth_filter_up == nullptr) {
return llvm::createStringError("Type has no child named '%s'",
name.AsCString());
} else if (found_index)
return *found_index;
return llvm::createStringError("Type has no child named '%s'",
name.AsCString());
}
bool ValueObjectSynthetic::IsInScope() { return m_parent->IsInScope(); }
lldb::ValueObjectSP ValueObjectSynthetic::GetNonSyntheticValue() {
return m_parent->GetSP();
}
void ValueObjectSynthetic::CopyValueData(ValueObject *source) {
if (!source->UpdateValueIfNeeded())
return;
m_value = source->GetValue();
ExecutionContext exe_ctx(GetExecutionContextRef());
m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
}
bool ValueObjectSynthetic::CanProvideValue() {
if (!UpdateValueIfNeeded())
return false;
if (m_provides_value == eLazyBoolYes)
return true;
return m_parent->CanProvideValue();
}
bool ValueObjectSynthetic::SetValueFromCString(const char *value_str,
Status &error) {
return m_parent->SetValueFromCString(value_str, error);
}
void ValueObjectSynthetic::SetFormat(lldb::Format format) {
if (m_parent) {
m_parent->ClearUserVisibleData(eClearUserVisibleDataItemsAll);
m_parent->SetFormat(format);
}
this->ValueObject::SetFormat(format);
this->ClearUserVisibleData(eClearUserVisibleDataItemsAll);
}
void ValueObjectSynthetic::SetPreferredDisplayLanguage(
lldb::LanguageType lang) {
this->ValueObject::SetPreferredDisplayLanguage(lang);
if (m_parent)
m_parent->SetPreferredDisplayLanguage(lang);
}
lldb::LanguageType ValueObjectSynthetic::GetPreferredDisplayLanguage() {
if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
if (m_parent)
return m_parent->GetPreferredDisplayLanguage();
return lldb::eLanguageTypeUnknown;
} else
return m_preferred_display_language;
}
bool ValueObjectSynthetic::IsSyntheticChildrenGenerated() {
if (m_parent)
return m_parent->IsSyntheticChildrenGenerated();
return false;
}
void ValueObjectSynthetic::SetSyntheticChildrenGenerated(bool b) {
if (m_parent)
m_parent->SetSyntheticChildrenGenerated(b);
this->ValueObject::SetSyntheticChildrenGenerated(b);
}
bool ValueObjectSynthetic::GetDeclaration(Declaration &decl) {
if (m_parent)
return m_parent->GetDeclaration(decl);
return ValueObject::GetDeclaration(decl);
}
uint64_t ValueObjectSynthetic::GetLanguageFlags() {
if (m_parent)
return m_parent->GetLanguageFlags();
return this->ValueObject::GetLanguageFlags();
}
void ValueObjectSynthetic::SetLanguageFlags(uint64_t flags) {
if (m_parent)
m_parent->SetLanguageFlags(flags);
else
this->ValueObject::SetLanguageFlags(flags);
}
|