1 //===-- SBValue.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 9 #include "lldb/API/SBValue.h" 10 #include "lldb/Utility/Instrumentation.h" 11 12 #include "lldb/API/SBDeclaration.h" 13 #include "lldb/API/SBStream.h" 14 #include "lldb/API/SBTypeFilter.h" 15 #include "lldb/API/SBTypeFormat.h" 16 #include "lldb/API/SBTypeSummary.h" 17 #include "lldb/API/SBTypeSynthetic.h" 18 19 #include "lldb/Breakpoint/Watchpoint.h" 20 #include "lldb/Core/Declaration.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Core/Section.h" 23 #include "lldb/Core/Value.h" 24 #include "lldb/Core/ValueObject.h" 25 #include "lldb/Core/ValueObjectConstResult.h" 26 #include "lldb/DataFormatters/DataVisualization.h" 27 #include "lldb/DataFormatters/DumpValueObjectOptions.h" 28 #include "lldb/Symbol/Block.h" 29 #include "lldb/Symbol/ObjectFile.h" 30 #include "lldb/Symbol/Type.h" 31 #include "lldb/Symbol/Variable.h" 32 #include "lldb/Symbol/VariableList.h" 33 #include "lldb/Target/ExecutionContext.h" 34 #include "lldb/Target/Process.h" 35 #include "lldb/Target/StackFrame.h" 36 #include "lldb/Target/Target.h" 37 #include "lldb/Target/Thread.h" 38 #include "lldb/Utility/DataExtractor.h" 39 #include "lldb/Utility/Scalar.h" 40 #include "lldb/Utility/Stream.h" 41 42 #include "lldb/API/SBDebugger.h" 43 #include "lldb/API/SBExpressionOptions.h" 44 #include "lldb/API/SBFrame.h" 45 #include "lldb/API/SBProcess.h" 46 #include "lldb/API/SBTarget.h" 47 #include "lldb/API/SBThread.h" 48 49 #include <memory> 50 51 using namespace lldb; 52 using namespace lldb_private; 53 54 class ValueImpl { 55 public: 56 ValueImpl() = default; 57 58 ValueImpl(lldb::ValueObjectSP in_valobj_sp, 59 lldb::DynamicValueType use_dynamic, bool use_synthetic, 60 const char *name = nullptr) 61 : m_use_dynamic(use_dynamic), m_use_synthetic(use_synthetic), 62 m_name(name) { 63 if (in_valobj_sp) { 64 if ((m_valobj_sp = in_valobj_sp->GetQualifiedRepresentationIfAvailable( 65 lldb::eNoDynamicValues, false))) { 66 if (!m_name.IsEmpty()) 67 m_valobj_sp->SetName(m_name); 68 } 69 } 70 } 71 72 ValueImpl(const ValueImpl &rhs) = default; 73 74 ValueImpl &operator=(const ValueImpl &rhs) { 75 if (this != &rhs) { 76 m_valobj_sp = rhs.m_valobj_sp; 77 m_use_dynamic = rhs.m_use_dynamic; 78 m_use_synthetic = rhs.m_use_synthetic; 79 m_name = rhs.m_name; 80 } 81 return *this; 82 } 83 84 bool IsValid() { 85 if (m_valobj_sp.get() == nullptr) 86 return false; 87 else { 88 // FIXME: This check is necessary but not sufficient. We for sure don't 89 // want to touch SBValues whose owning 90 // targets have gone away. This check is a little weak in that it 91 // enforces that restriction when you call IsValid, but since IsValid 92 // doesn't lock the target, you have no guarantee that the SBValue won't 93 // go invalid after you call this... Also, an SBValue could depend on 94 // data from one of the modules in the target, and those could go away 95 // independently of the target, for instance if a module is unloaded. 96 // But right now, neither SBValues nor ValueObjects know which modules 97 // they depend on. So I have no good way to make that check without 98 // tracking that in all the ValueObject subclasses. 99 TargetSP target_sp = m_valobj_sp->GetTargetSP(); 100 return target_sp && target_sp->IsValid(); 101 } 102 } 103 104 lldb::ValueObjectSP GetRootSP() { return m_valobj_sp; } 105 106 lldb::ValueObjectSP GetSP(Process::StopLocker &stop_locker, 107 std::unique_lock<std::recursive_mutex> &lock, 108 Status &error) { 109 if (!m_valobj_sp) { 110 error.SetErrorString("invalid value object"); 111 return m_valobj_sp; 112 } 113 114 lldb::ValueObjectSP value_sp = m_valobj_sp; 115 116 Target *target = value_sp->GetTargetSP().get(); 117 // If this ValueObject holds an error, then it is valuable for that. 118 if (value_sp->GetError().Fail()) 119 return value_sp; 120 121 if (!target) 122 return ValueObjectSP(); 123 124 lock = std::unique_lock<std::recursive_mutex>(target->GetAPIMutex()); 125 126 ProcessSP process_sp(value_sp->GetProcessSP()); 127 if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock())) { 128 // We don't allow people to play around with ValueObject if the process 129 // is running. If you want to look at values, pause the process, then 130 // look. 131 error.SetErrorString("process must be stopped."); 132 return ValueObjectSP(); 133 } 134 135 if (m_use_dynamic != eNoDynamicValues) { 136 ValueObjectSP dynamic_sp = value_sp->GetDynamicValue(m_use_dynamic); 137 if (dynamic_sp) 138 value_sp = dynamic_sp; 139 } 140 141 if (m_use_synthetic) { 142 ValueObjectSP synthetic_sp = value_sp->GetSyntheticValue(); 143 if (synthetic_sp) 144 value_sp = synthetic_sp; 145 } 146 147 if (!value_sp) 148 error.SetErrorString("invalid value object"); 149 if (!m_name.IsEmpty()) 150 value_sp->SetName(m_name); 151 152 return value_sp; 153 } 154 155 void SetUseDynamic(lldb::DynamicValueType use_dynamic) { 156 m_use_dynamic = use_dynamic; 157 } 158 159 void SetUseSynthetic(bool use_synthetic) { m_use_synthetic = use_synthetic; } 160 161 lldb::DynamicValueType GetUseDynamic() { return m_use_dynamic; } 162 163 bool GetUseSynthetic() { return m_use_synthetic; } 164 165 // All the derived values that we would make from the m_valobj_sp will share 166 // the ExecutionContext with m_valobj_sp, so we don't need to do the 167 // calculations in GetSP to return the Target, Process, Thread or Frame. It 168 // is convenient to provide simple accessors for these, which I do here. 169 TargetSP GetTargetSP() { 170 if (m_valobj_sp) 171 return m_valobj_sp->GetTargetSP(); 172 else 173 return TargetSP(); 174 } 175 176 ProcessSP GetProcessSP() { 177 if (m_valobj_sp) 178 return m_valobj_sp->GetProcessSP(); 179 else 180 return ProcessSP(); 181 } 182 183 ThreadSP GetThreadSP() { 184 if (m_valobj_sp) 185 return m_valobj_sp->GetThreadSP(); 186 else 187 return ThreadSP(); 188 } 189 190 StackFrameSP GetFrameSP() { 191 if (m_valobj_sp) 192 return m_valobj_sp->GetFrameSP(); 193 else 194 return StackFrameSP(); 195 } 196 197 private: 198 lldb::ValueObjectSP m_valobj_sp; 199 lldb::DynamicValueType m_use_dynamic; 200 bool m_use_synthetic; 201 ConstString m_name; 202 }; 203 204 class ValueLocker { 205 public: 206 ValueLocker() = default; 207 208 ValueObjectSP GetLockedSP(ValueImpl &in_value) { 209 return in_value.GetSP(m_stop_locker, m_lock, m_lock_error); 210 } 211 212 Status &GetError() { return m_lock_error; } 213 214 private: 215 Process::StopLocker m_stop_locker; 216 std::unique_lock<std::recursive_mutex> m_lock; 217 Status m_lock_error; 218 }; 219 220 SBValue::SBValue() { LLDB_INSTRUMENT_VA(this); } 221 222 SBValue::SBValue(const lldb::ValueObjectSP &value_sp) { 223 LLDB_INSTRUMENT_VA(this, value_sp); 224 225 SetSP(value_sp); 226 } 227 228 SBValue::SBValue(const SBValue &rhs) { 229 LLDB_INSTRUMENT_VA(this, rhs); 230 231 SetSP(rhs.m_opaque_sp); 232 } 233 234 SBValue &SBValue::operator=(const SBValue &rhs) { 235 LLDB_INSTRUMENT_VA(this, rhs); 236 237 if (this != &rhs) { 238 SetSP(rhs.m_opaque_sp); 239 } 240 return *this; 241 } 242 243 SBValue::~SBValue() = default; 244 245 bool SBValue::IsValid() { 246 LLDB_INSTRUMENT_VA(this); 247 return this->operator bool(); 248 } 249 SBValue::operator bool() const { 250 LLDB_INSTRUMENT_VA(this); 251 252 // If this function ever changes to anything that does more than just check 253 // if the opaque shared pointer is non NULL, then we need to update all "if 254 // (m_opaque_sp)" code in this file. 255 return m_opaque_sp.get() != nullptr && m_opaque_sp->IsValid() && 256 m_opaque_sp->GetRootSP().get() != nullptr; 257 } 258 259 void SBValue::Clear() { 260 LLDB_INSTRUMENT_VA(this); 261 262 m_opaque_sp.reset(); 263 } 264 265 SBError SBValue::GetError() { 266 LLDB_INSTRUMENT_VA(this); 267 268 SBError sb_error; 269 270 ValueLocker locker; 271 lldb::ValueObjectSP value_sp(GetSP(locker)); 272 if (value_sp) 273 sb_error.SetError(value_sp->GetError()); 274 else 275 sb_error.SetErrorStringWithFormat("error: %s", 276 locker.GetError().AsCString()); 277 278 return sb_error; 279 } 280 281 user_id_t SBValue::GetID() { 282 LLDB_INSTRUMENT_VA(this); 283 284 ValueLocker locker; 285 lldb::ValueObjectSP value_sp(GetSP(locker)); 286 if (value_sp) 287 return value_sp->GetID(); 288 return LLDB_INVALID_UID; 289 } 290 291 const char *SBValue::GetName() { 292 LLDB_INSTRUMENT_VA(this); 293 294 ValueLocker locker; 295 lldb::ValueObjectSP value_sp(GetSP(locker)); 296 if (!value_sp) 297 return nullptr; 298 299 return value_sp->GetName().GetCString(); 300 } 301 302 const char *SBValue::GetTypeName() { 303 LLDB_INSTRUMENT_VA(this); 304 305 ValueLocker locker; 306 lldb::ValueObjectSP value_sp(GetSP(locker)); 307 if (!value_sp) 308 return nullptr; 309 310 return value_sp->GetQualifiedTypeName().GetCString(); 311 } 312 313 const char *SBValue::GetDisplayTypeName() { 314 LLDB_INSTRUMENT_VA(this); 315 316 ValueLocker locker; 317 lldb::ValueObjectSP value_sp(GetSP(locker)); 318 if (!value_sp) 319 return nullptr; 320 321 return value_sp->GetDisplayTypeName().GetCString(); 322 } 323 324 size_t SBValue::GetByteSize() { 325 LLDB_INSTRUMENT_VA(this); 326 327 size_t result = 0; 328 329 ValueLocker locker; 330 lldb::ValueObjectSP value_sp(GetSP(locker)); 331 if (value_sp) { 332 result = value_sp->GetByteSize().value_or(0); 333 } 334 335 return result; 336 } 337 338 bool SBValue::IsInScope() { 339 LLDB_INSTRUMENT_VA(this); 340 341 bool result = false; 342 343 ValueLocker locker; 344 lldb::ValueObjectSP value_sp(GetSP(locker)); 345 if (value_sp) { 346 result = value_sp->IsInScope(); 347 } 348 349 return result; 350 } 351 352 const char *SBValue::GetValue() { 353 LLDB_INSTRUMENT_VA(this); 354 355 ValueLocker locker; 356 lldb::ValueObjectSP value_sp(GetSP(locker)); 357 if (!value_sp) 358 return nullptr; 359 return ConstString(value_sp->GetValueAsCString()).GetCString(); 360 } 361 362 ValueType SBValue::GetValueType() { 363 LLDB_INSTRUMENT_VA(this); 364 365 ValueType result = eValueTypeInvalid; 366 ValueLocker locker; 367 lldb::ValueObjectSP value_sp(GetSP(locker)); 368 if (value_sp) 369 result = value_sp->GetValueType(); 370 371 return result; 372 } 373 374 const char *SBValue::GetObjectDescription() { 375 LLDB_INSTRUMENT_VA(this); 376 377 ValueLocker locker; 378 lldb::ValueObjectSP value_sp(GetSP(locker)); 379 if (!value_sp) 380 return nullptr; 381 382 llvm::Expected<std::string> str = value_sp->GetObjectDescription(); 383 if (!str) 384 return ConstString("error: " + toString(str.takeError())).AsCString(); 385 return ConstString(*str).AsCString(); 386 } 387 388 SBType SBValue::GetType() { 389 LLDB_INSTRUMENT_VA(this); 390 391 SBType sb_type; 392 ValueLocker locker; 393 lldb::ValueObjectSP value_sp(GetSP(locker)); 394 TypeImplSP type_sp; 395 if (value_sp) { 396 type_sp = std::make_shared<TypeImpl>(value_sp->GetTypeImpl()); 397 sb_type.SetSP(type_sp); 398 } 399 400 return sb_type; 401 } 402 403 bool SBValue::GetValueDidChange() { 404 LLDB_INSTRUMENT_VA(this); 405 406 bool result = false; 407 ValueLocker locker; 408 lldb::ValueObjectSP value_sp(GetSP(locker)); 409 if (value_sp) { 410 if (value_sp->UpdateValueIfNeeded(false)) 411 result = value_sp->GetValueDidChange(); 412 } 413 414 return result; 415 } 416 417 const char *SBValue::GetSummary() { 418 LLDB_INSTRUMENT_VA(this); 419 420 ValueLocker locker; 421 lldb::ValueObjectSP value_sp(GetSP(locker)); 422 if (!value_sp) 423 return nullptr; 424 425 return ConstString(value_sp->GetSummaryAsCString()).GetCString(); 426 } 427 428 const char *SBValue::GetSummary(lldb::SBStream &stream, 429 lldb::SBTypeSummaryOptions &options) { 430 LLDB_INSTRUMENT_VA(this, stream, options); 431 432 ValueLocker locker; 433 lldb::ValueObjectSP value_sp(GetSP(locker)); 434 if (value_sp) { 435 std::string buffer; 436 if (value_sp->GetSummaryAsCString(buffer, options.ref()) && !buffer.empty()) 437 stream.Printf("%s", buffer.c_str()); 438 } 439 return ConstString(stream.GetData()).GetCString(); 440 } 441 442 const char *SBValue::GetLocation() { 443 LLDB_INSTRUMENT_VA(this); 444 445 ValueLocker locker; 446 lldb::ValueObjectSP value_sp(GetSP(locker)); 447 if (!value_sp) 448 return nullptr; 449 450 return ConstString(value_sp->GetLocationAsCString()).GetCString(); 451 } 452 453 // Deprecated - use the one that takes an lldb::SBError 454 bool SBValue::SetValueFromCString(const char *value_str) { 455 LLDB_INSTRUMENT_VA(this, value_str); 456 457 lldb::SBError dummy; 458 return SetValueFromCString(value_str, dummy); 459 } 460 461 bool SBValue::SetValueFromCString(const char *value_str, lldb::SBError &error) { 462 LLDB_INSTRUMENT_VA(this, value_str, error); 463 464 bool success = false; 465 ValueLocker locker; 466 lldb::ValueObjectSP value_sp(GetSP(locker)); 467 if (value_sp) { 468 success = value_sp->SetValueFromCString(value_str, error.ref()); 469 } else 470 error.SetErrorStringWithFormat("Could not get value: %s", 471 locker.GetError().AsCString()); 472 473 return success; 474 } 475 476 lldb::SBTypeFormat SBValue::GetTypeFormat() { 477 LLDB_INSTRUMENT_VA(this); 478 479 lldb::SBTypeFormat format; 480 ValueLocker locker; 481 lldb::ValueObjectSP value_sp(GetSP(locker)); 482 if (value_sp) { 483 if (value_sp->UpdateValueIfNeeded(true)) { 484 lldb::TypeFormatImplSP format_sp = value_sp->GetValueFormat(); 485 if (format_sp) 486 format.SetSP(format_sp); 487 } 488 } 489 return format; 490 } 491 492 lldb::SBTypeSummary SBValue::GetTypeSummary() { 493 LLDB_INSTRUMENT_VA(this); 494 495 lldb::SBTypeSummary summary; 496 ValueLocker locker; 497 lldb::ValueObjectSP value_sp(GetSP(locker)); 498 if (value_sp) { 499 if (value_sp->UpdateValueIfNeeded(true)) { 500 lldb::TypeSummaryImplSP summary_sp = value_sp->GetSummaryFormat(); 501 if (summary_sp) 502 summary.SetSP(summary_sp); 503 } 504 } 505 return summary; 506 } 507 508 lldb::SBTypeFilter SBValue::GetTypeFilter() { 509 LLDB_INSTRUMENT_VA(this); 510 511 lldb::SBTypeFilter filter; 512 ValueLocker locker; 513 lldb::ValueObjectSP value_sp(GetSP(locker)); 514 if (value_sp) { 515 if (value_sp->UpdateValueIfNeeded(true)) { 516 lldb::SyntheticChildrenSP synthetic_sp = value_sp->GetSyntheticChildren(); 517 518 if (synthetic_sp && !synthetic_sp->IsScripted()) { 519 TypeFilterImplSP filter_sp = 520 std::static_pointer_cast<TypeFilterImpl>(synthetic_sp); 521 filter.SetSP(filter_sp); 522 } 523 } 524 } 525 return filter; 526 } 527 528 lldb::SBTypeSynthetic SBValue::GetTypeSynthetic() { 529 LLDB_INSTRUMENT_VA(this); 530 531 lldb::SBTypeSynthetic synthetic; 532 ValueLocker locker; 533 lldb::ValueObjectSP value_sp(GetSP(locker)); 534 if (value_sp) { 535 if (value_sp->UpdateValueIfNeeded(true)) { 536 lldb::SyntheticChildrenSP children_sp = value_sp->GetSyntheticChildren(); 537 538 if (children_sp && children_sp->IsScripted()) { 539 ScriptedSyntheticChildrenSP synth_sp = 540 std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp); 541 synthetic.SetSP(synth_sp); 542 } 543 } 544 } 545 return synthetic; 546 } 547 548 lldb::SBValue SBValue::CreateChildAtOffset(const char *name, uint32_t offset, 549 SBType type) { 550 LLDB_INSTRUMENT_VA(this, name, offset, type); 551 552 lldb::SBValue sb_value; 553 ValueLocker locker; 554 lldb::ValueObjectSP value_sp(GetSP(locker)); 555 lldb::ValueObjectSP new_value_sp; 556 if (value_sp) { 557 TypeImplSP type_sp(type.GetSP()); 558 if (type.IsValid()) { 559 sb_value.SetSP(value_sp->GetSyntheticChildAtOffset( 560 offset, type_sp->GetCompilerType(false), true), 561 GetPreferDynamicValue(), GetPreferSyntheticValue(), name); 562 } 563 } 564 return sb_value; 565 } 566 567 lldb::SBValue SBValue::Cast(SBType type) { 568 LLDB_INSTRUMENT_VA(this, type); 569 570 lldb::SBValue sb_value; 571 ValueLocker locker; 572 lldb::ValueObjectSP value_sp(GetSP(locker)); 573 TypeImplSP type_sp(type.GetSP()); 574 if (value_sp && type_sp) 575 sb_value.SetSP(value_sp->Cast(type_sp->GetCompilerType(false)), 576 GetPreferDynamicValue(), GetPreferSyntheticValue()); 577 return sb_value; 578 } 579 580 lldb::SBValue SBValue::CreateValueFromExpression(const char *name, 581 const char *expression) { 582 LLDB_INSTRUMENT_VA(this, name, expression); 583 584 SBExpressionOptions options; 585 options.ref().SetKeepInMemory(true); 586 return CreateValueFromExpression(name, expression, options); 587 } 588 589 lldb::SBValue SBValue::CreateValueFromExpression(const char *name, 590 const char *expression, 591 SBExpressionOptions &options) { 592 LLDB_INSTRUMENT_VA(this, name, expression, options); 593 594 lldb::SBValue sb_value; 595 ValueLocker locker; 596 lldb::ValueObjectSP value_sp(GetSP(locker)); 597 lldb::ValueObjectSP new_value_sp; 598 if (value_sp) { 599 ExecutionContext exe_ctx(value_sp->GetExecutionContextRef()); 600 new_value_sp = ValueObject::CreateValueObjectFromExpression( 601 name, expression, exe_ctx, options.ref()); 602 if (new_value_sp) 603 new_value_sp->SetName(ConstString(name)); 604 } 605 sb_value.SetSP(new_value_sp); 606 return sb_value; 607 } 608 609 lldb::SBValue SBValue::CreateValueFromAddress(const char *name, 610 lldb::addr_t address, 611 SBType sb_type) { 612 LLDB_INSTRUMENT_VA(this, name, address, sb_type); 613 614 lldb::SBValue sb_value; 615 ValueLocker locker; 616 lldb::ValueObjectSP value_sp(GetSP(locker)); 617 lldb::ValueObjectSP new_value_sp; 618 lldb::TypeImplSP type_impl_sp(sb_type.GetSP()); 619 if (value_sp && type_impl_sp) { 620 CompilerType ast_type(type_impl_sp->GetCompilerType(true)); 621 ExecutionContext exe_ctx(value_sp->GetExecutionContextRef()); 622 new_value_sp = ValueObject::CreateValueObjectFromAddress(name, address, 623 exe_ctx, ast_type); 624 } 625 sb_value.SetSP(new_value_sp); 626 return sb_value; 627 } 628 629 lldb::SBValue SBValue::CreateValueFromData(const char *name, SBData data, 630 SBType sb_type) { 631 LLDB_INSTRUMENT_VA(this, name, data, sb_type); 632 633 lldb::SBValue sb_value; 634 lldb::ValueObjectSP new_value_sp; 635 ValueLocker locker; 636 lldb::ValueObjectSP value_sp(GetSP(locker)); 637 lldb::TypeImplSP type_impl_sp(sb_type.GetSP()); 638 if (value_sp && type_impl_sp) { 639 ExecutionContext exe_ctx(value_sp->GetExecutionContextRef()); 640 new_value_sp = ValueObject::CreateValueObjectFromData( 641 name, **data, exe_ctx, type_impl_sp->GetCompilerType(true)); 642 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad); 643 } 644 sb_value.SetSP(new_value_sp); 645 return sb_value; 646 } 647 648 SBValue SBValue::GetChildAtIndex(uint32_t idx) { 649 LLDB_INSTRUMENT_VA(this, idx); 650 651 const bool can_create_synthetic = false; 652 lldb::DynamicValueType use_dynamic = eNoDynamicValues; 653 TargetSP target_sp; 654 if (m_opaque_sp) 655 target_sp = m_opaque_sp->GetTargetSP(); 656 657 if (target_sp) 658 use_dynamic = target_sp->GetPreferDynamicValue(); 659 660 return GetChildAtIndex(idx, use_dynamic, can_create_synthetic); 661 } 662 663 SBValue SBValue::GetChildAtIndex(uint32_t idx, 664 lldb::DynamicValueType use_dynamic, 665 bool can_create_synthetic) { 666 LLDB_INSTRUMENT_VA(this, idx, use_dynamic, can_create_synthetic); 667 668 lldb::ValueObjectSP child_sp; 669 670 ValueLocker locker; 671 lldb::ValueObjectSP value_sp(GetSP(locker)); 672 if (value_sp) { 673 const bool can_create = true; 674 child_sp = value_sp->GetChildAtIndex(idx); 675 if (can_create_synthetic && !child_sp) { 676 child_sp = value_sp->GetSyntheticArrayMember(idx, can_create); 677 } 678 } 679 680 SBValue sb_value; 681 sb_value.SetSP(child_sp, use_dynamic, GetPreferSyntheticValue()); 682 683 return sb_value; 684 } 685 686 uint32_t SBValue::GetIndexOfChildWithName(const char *name) { 687 LLDB_INSTRUMENT_VA(this, name); 688 689 uint32_t idx = UINT32_MAX; 690 ValueLocker locker; 691 lldb::ValueObjectSP value_sp(GetSP(locker)); 692 if (value_sp) { 693 idx = value_sp->GetIndexOfChildWithName(name); 694 } 695 return idx; 696 } 697 698 SBValue SBValue::GetChildMemberWithName(const char *name) { 699 LLDB_INSTRUMENT_VA(this, name); 700 701 lldb::DynamicValueType use_dynamic_value = eNoDynamicValues; 702 TargetSP target_sp; 703 if (m_opaque_sp) 704 target_sp = m_opaque_sp->GetTargetSP(); 705 706 if (target_sp) 707 use_dynamic_value = target_sp->GetPreferDynamicValue(); 708 return GetChildMemberWithName(name, use_dynamic_value); 709 } 710 711 SBValue 712 SBValue::GetChildMemberWithName(const char *name, 713 lldb::DynamicValueType use_dynamic_value) { 714 LLDB_INSTRUMENT_VA(this, name, use_dynamic_value); 715 716 lldb::ValueObjectSP child_sp; 717 718 ValueLocker locker; 719 lldb::ValueObjectSP value_sp(GetSP(locker)); 720 if (value_sp) { 721 child_sp = value_sp->GetChildMemberWithName(name); 722 } 723 724 SBValue sb_value; 725 sb_value.SetSP(child_sp, use_dynamic_value, GetPreferSyntheticValue()); 726 727 return sb_value; 728 } 729 730 lldb::SBValue SBValue::GetDynamicValue(lldb::DynamicValueType use_dynamic) { 731 LLDB_INSTRUMENT_VA(this, use_dynamic); 732 733 SBValue value_sb; 734 if (IsValid()) { 735 ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(), use_dynamic, 736 m_opaque_sp->GetUseSynthetic())); 737 value_sb.SetSP(proxy_sp); 738 } 739 return value_sb; 740 } 741 742 lldb::SBValue SBValue::GetStaticValue() { 743 LLDB_INSTRUMENT_VA(this); 744 745 SBValue value_sb; 746 if (IsValid()) { 747 ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(), 748 eNoDynamicValues, 749 m_opaque_sp->GetUseSynthetic())); 750 value_sb.SetSP(proxy_sp); 751 } 752 return value_sb; 753 } 754 755 lldb::SBValue SBValue::GetNonSyntheticValue() { 756 LLDB_INSTRUMENT_VA(this); 757 758 SBValue value_sb; 759 if (IsValid()) { 760 ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(), 761 m_opaque_sp->GetUseDynamic(), false)); 762 value_sb.SetSP(proxy_sp); 763 } 764 return value_sb; 765 } 766 767 lldb::SBValue SBValue::GetSyntheticValue() { 768 LLDB_INSTRUMENT_VA(this); 769 770 SBValue value_sb; 771 if (IsValid()) { 772 ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(), 773 m_opaque_sp->GetUseDynamic(), true)); 774 value_sb.SetSP(proxy_sp); 775 if (!value_sb.IsSynthetic()) { 776 return {}; 777 } 778 } 779 return value_sb; 780 } 781 782 lldb::DynamicValueType SBValue::GetPreferDynamicValue() { 783 LLDB_INSTRUMENT_VA(this); 784 785 if (!IsValid()) 786 return eNoDynamicValues; 787 return m_opaque_sp->GetUseDynamic(); 788 } 789 790 void SBValue::SetPreferDynamicValue(lldb::DynamicValueType use_dynamic) { 791 LLDB_INSTRUMENT_VA(this, use_dynamic); 792 793 if (IsValid()) 794 return m_opaque_sp->SetUseDynamic(use_dynamic); 795 } 796 797 bool SBValue::GetPreferSyntheticValue() { 798 LLDB_INSTRUMENT_VA(this); 799 800 if (!IsValid()) 801 return false; 802 return m_opaque_sp->GetUseSynthetic(); 803 } 804 805 void SBValue::SetPreferSyntheticValue(bool use_synthetic) { 806 LLDB_INSTRUMENT_VA(this, use_synthetic); 807 808 if (IsValid()) 809 return m_opaque_sp->SetUseSynthetic(use_synthetic); 810 } 811 812 bool SBValue::IsDynamic() { 813 LLDB_INSTRUMENT_VA(this); 814 815 ValueLocker locker; 816 lldb::ValueObjectSP value_sp(GetSP(locker)); 817 if (value_sp) 818 return value_sp->IsDynamic(); 819 return false; 820 } 821 822 bool SBValue::IsSynthetic() { 823 LLDB_INSTRUMENT_VA(this); 824 825 ValueLocker locker; 826 lldb::ValueObjectSP value_sp(GetSP(locker)); 827 if (value_sp) 828 return value_sp->IsSynthetic(); 829 return false; 830 } 831 832 bool SBValue::IsSyntheticChildrenGenerated() { 833 LLDB_INSTRUMENT_VA(this); 834 835 ValueLocker locker; 836 lldb::ValueObjectSP value_sp(GetSP(locker)); 837 if (value_sp) 838 return value_sp->IsSyntheticChildrenGenerated(); 839 return false; 840 } 841 842 void SBValue::SetSyntheticChildrenGenerated(bool is) { 843 LLDB_INSTRUMENT_VA(this, is); 844 845 ValueLocker locker; 846 lldb::ValueObjectSP value_sp(GetSP(locker)); 847 if (value_sp) 848 return value_sp->SetSyntheticChildrenGenerated(is); 849 } 850 851 lldb::SBValue SBValue::GetValueForExpressionPath(const char *expr_path) { 852 LLDB_INSTRUMENT_VA(this, expr_path); 853 854 lldb::ValueObjectSP child_sp; 855 ValueLocker locker; 856 lldb::ValueObjectSP value_sp(GetSP(locker)); 857 if (value_sp) { 858 // using default values for all the fancy options, just do it if you can 859 child_sp = value_sp->GetValueForExpressionPath(expr_path); 860 } 861 862 SBValue sb_value; 863 sb_value.SetSP(child_sp, GetPreferDynamicValue(), GetPreferSyntheticValue()); 864 865 return sb_value; 866 } 867 868 int64_t SBValue::GetValueAsSigned(SBError &error, int64_t fail_value) { 869 LLDB_INSTRUMENT_VA(this, error, fail_value); 870 871 error.Clear(); 872 ValueLocker locker; 873 lldb::ValueObjectSP value_sp(GetSP(locker)); 874 if (value_sp) { 875 bool success = true; 876 uint64_t ret_val = fail_value; 877 ret_val = value_sp->GetValueAsSigned(fail_value, &success); 878 if (!success) 879 error.SetErrorString("could not resolve value"); 880 return ret_val; 881 } else 882 error.SetErrorStringWithFormat("could not get SBValue: %s", 883 locker.GetError().AsCString()); 884 885 return fail_value; 886 } 887 888 uint64_t SBValue::GetValueAsUnsigned(SBError &error, uint64_t fail_value) { 889 LLDB_INSTRUMENT_VA(this, error, fail_value); 890 891 error.Clear(); 892 ValueLocker locker; 893 lldb::ValueObjectSP value_sp(GetSP(locker)); 894 if (value_sp) { 895 bool success = true; 896 uint64_t ret_val = fail_value; 897 ret_val = value_sp->GetValueAsUnsigned(fail_value, &success); 898 if (!success) 899 error.SetErrorString("could not resolve value"); 900 return ret_val; 901 } else 902 error.SetErrorStringWithFormat("could not get SBValue: %s", 903 locker.GetError().AsCString()); 904 905 return fail_value; 906 } 907 908 int64_t SBValue::GetValueAsSigned(int64_t fail_value) { 909 LLDB_INSTRUMENT_VA(this, fail_value); 910 911 ValueLocker locker; 912 lldb::ValueObjectSP value_sp(GetSP(locker)); 913 if (value_sp) { 914 return value_sp->GetValueAsSigned(fail_value); 915 } 916 return fail_value; 917 } 918 919 uint64_t SBValue::GetValueAsUnsigned(uint64_t fail_value) { 920 LLDB_INSTRUMENT_VA(this, fail_value); 921 922 ValueLocker locker; 923 lldb::ValueObjectSP value_sp(GetSP(locker)); 924 if (value_sp) { 925 return value_sp->GetValueAsUnsigned(fail_value); 926 } 927 return fail_value; 928 } 929 930 lldb::addr_t SBValue::GetValueAsAddress() { 931 addr_t fail_value = LLDB_INVALID_ADDRESS; 932 ValueLocker locker; 933 lldb::ValueObjectSP value_sp(GetSP(locker)); 934 if (value_sp) { 935 bool success = true; 936 uint64_t ret_val = fail_value; 937 ret_val = value_sp->GetValueAsUnsigned(fail_value, &success); 938 if (!success) 939 return fail_value; 940 ProcessSP process_sp = m_opaque_sp->GetProcessSP(); 941 if (!process_sp) 942 return ret_val; 943 return process_sp->FixDataAddress(ret_val); 944 } 945 946 return fail_value; 947 } 948 949 bool SBValue::MightHaveChildren() { 950 LLDB_INSTRUMENT_VA(this); 951 952 bool has_children = false; 953 ValueLocker locker; 954 lldb::ValueObjectSP value_sp(GetSP(locker)); 955 if (value_sp) 956 has_children = value_sp->MightHaveChildren(); 957 958 return has_children; 959 } 960 961 bool SBValue::IsRuntimeSupportValue() { 962 LLDB_INSTRUMENT_VA(this); 963 964 bool is_support = false; 965 ValueLocker locker; 966 lldb::ValueObjectSP value_sp(GetSP(locker)); 967 if (value_sp) 968 is_support = value_sp->IsRuntimeSupportValue(); 969 970 return is_support; 971 } 972 973 uint32_t SBValue::GetNumChildren() { 974 LLDB_INSTRUMENT_VA(this); 975 976 return GetNumChildren(UINT32_MAX); 977 } 978 979 uint32_t SBValue::GetNumChildren(uint32_t max) { 980 LLDB_INSTRUMENT_VA(this, max); 981 982 uint32_t num_children = 0; 983 984 ValueLocker locker; 985 lldb::ValueObjectSP value_sp(GetSP(locker)); 986 if (value_sp) 987 num_children = value_sp->GetNumChildrenIgnoringErrors(max); 988 989 return num_children; 990 } 991 992 SBValue SBValue::Dereference() { 993 LLDB_INSTRUMENT_VA(this); 994 995 SBValue sb_value; 996 ValueLocker locker; 997 lldb::ValueObjectSP value_sp(GetSP(locker)); 998 if (value_sp) { 999 Status error; 1000 sb_value = value_sp->Dereference(error); 1001 } 1002 1003 return sb_value; 1004 } 1005 1006 // Deprecated - please use GetType().IsPointerType() instead. 1007 bool SBValue::TypeIsPointerType() { 1008 LLDB_INSTRUMENT_VA(this); 1009 1010 return GetType().IsPointerType(); 1011 } 1012 1013 void *SBValue::GetOpaqueType() { 1014 LLDB_INSTRUMENT_VA(this); 1015 1016 ValueLocker locker; 1017 lldb::ValueObjectSP value_sp(GetSP(locker)); 1018 if (value_sp) 1019 return value_sp->GetCompilerType().GetOpaqueQualType(); 1020 return nullptr; 1021 } 1022 1023 lldb::SBTarget SBValue::GetTarget() { 1024 LLDB_INSTRUMENT_VA(this); 1025 1026 SBTarget sb_target; 1027 TargetSP target_sp; 1028 if (m_opaque_sp) { 1029 target_sp = m_opaque_sp->GetTargetSP(); 1030 sb_target.SetSP(target_sp); 1031 } 1032 1033 return sb_target; 1034 } 1035 1036 lldb::SBProcess SBValue::GetProcess() { 1037 LLDB_INSTRUMENT_VA(this); 1038 1039 SBProcess sb_process; 1040 ProcessSP process_sp; 1041 if (m_opaque_sp) { 1042 process_sp = m_opaque_sp->GetProcessSP(); 1043 sb_process.SetSP(process_sp); 1044 } 1045 1046 return sb_process; 1047 } 1048 1049 lldb::SBThread SBValue::GetThread() { 1050 LLDB_INSTRUMENT_VA(this); 1051 1052 SBThread sb_thread; 1053 ThreadSP thread_sp; 1054 if (m_opaque_sp) { 1055 thread_sp = m_opaque_sp->GetThreadSP(); 1056 sb_thread.SetThread(thread_sp); 1057 } 1058 1059 return sb_thread; 1060 } 1061 1062 lldb::SBFrame SBValue::GetFrame() { 1063 LLDB_INSTRUMENT_VA(this); 1064 1065 SBFrame sb_frame; 1066 StackFrameSP frame_sp; 1067 if (m_opaque_sp) { 1068 frame_sp = m_opaque_sp->GetFrameSP(); 1069 sb_frame.SetFrameSP(frame_sp); 1070 } 1071 1072 return sb_frame; 1073 } 1074 1075 lldb::ValueObjectSP SBValue::GetSP(ValueLocker &locker) const { 1076 // IsValid means that the SBValue has a value in it. But that's not the 1077 // only time that ValueObjects are useful. We also want to return the value 1078 // if there's an error state in it. 1079 if (!m_opaque_sp || (!m_opaque_sp->IsValid() 1080 && (m_opaque_sp->GetRootSP() 1081 && !m_opaque_sp->GetRootSP()->GetError().Fail()))) { 1082 locker.GetError().SetErrorString("No value"); 1083 return ValueObjectSP(); 1084 } 1085 return locker.GetLockedSP(*m_opaque_sp.get()); 1086 } 1087 1088 lldb::ValueObjectSP SBValue::GetSP() const { 1089 LLDB_INSTRUMENT_VA(this); 1090 1091 ValueLocker locker; 1092 return GetSP(locker); 1093 } 1094 1095 void SBValue::SetSP(ValueImplSP impl_sp) { m_opaque_sp = impl_sp; } 1096 1097 void SBValue::SetSP(const lldb::ValueObjectSP &sp) { 1098 if (sp) { 1099 lldb::TargetSP target_sp(sp->GetTargetSP()); 1100 if (target_sp) { 1101 lldb::DynamicValueType use_dynamic = target_sp->GetPreferDynamicValue(); 1102 bool use_synthetic = 1103 target_sp->TargetProperties::GetEnableSyntheticValue(); 1104 m_opaque_sp = ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic)); 1105 } else 1106 m_opaque_sp = ValueImplSP(new ValueImpl(sp, eNoDynamicValues, true)); 1107 } else 1108 m_opaque_sp = ValueImplSP(new ValueImpl(sp, eNoDynamicValues, false)); 1109 } 1110 1111 void SBValue::SetSP(const lldb::ValueObjectSP &sp, 1112 lldb::DynamicValueType use_dynamic) { 1113 if (sp) { 1114 lldb::TargetSP target_sp(sp->GetTargetSP()); 1115 if (target_sp) { 1116 bool use_synthetic = 1117 target_sp->TargetProperties::GetEnableSyntheticValue(); 1118 SetSP(sp, use_dynamic, use_synthetic); 1119 } else 1120 SetSP(sp, use_dynamic, true); 1121 } else 1122 SetSP(sp, use_dynamic, false); 1123 } 1124 1125 void SBValue::SetSP(const lldb::ValueObjectSP &sp, bool use_synthetic) { 1126 if (sp) { 1127 lldb::TargetSP target_sp(sp->GetTargetSP()); 1128 if (target_sp) { 1129 lldb::DynamicValueType use_dynamic = target_sp->GetPreferDynamicValue(); 1130 SetSP(sp, use_dynamic, use_synthetic); 1131 } else 1132 SetSP(sp, eNoDynamicValues, use_synthetic); 1133 } else 1134 SetSP(sp, eNoDynamicValues, use_synthetic); 1135 } 1136 1137 void SBValue::SetSP(const lldb::ValueObjectSP &sp, 1138 lldb::DynamicValueType use_dynamic, bool use_synthetic) { 1139 m_opaque_sp = ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic)); 1140 } 1141 1142 void SBValue::SetSP(const lldb::ValueObjectSP &sp, 1143 lldb::DynamicValueType use_dynamic, bool use_synthetic, 1144 const char *name) { 1145 m_opaque_sp = 1146 ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic, name)); 1147 } 1148 1149 bool SBValue::GetExpressionPath(SBStream &description) { 1150 LLDB_INSTRUMENT_VA(this, description); 1151 1152 ValueLocker locker; 1153 lldb::ValueObjectSP value_sp(GetSP(locker)); 1154 if (value_sp) { 1155 value_sp->GetExpressionPath(description.ref()); 1156 return true; 1157 } 1158 return false; 1159 } 1160 1161 bool SBValue::GetExpressionPath(SBStream &description, 1162 bool qualify_cxx_base_classes) { 1163 LLDB_INSTRUMENT_VA(this, description, qualify_cxx_base_classes); 1164 1165 ValueLocker locker; 1166 lldb::ValueObjectSP value_sp(GetSP(locker)); 1167 if (value_sp) { 1168 value_sp->GetExpressionPath(description.ref()); 1169 return true; 1170 } 1171 return false; 1172 } 1173 1174 lldb::SBValue SBValue::EvaluateExpression(const char *expr) const { 1175 LLDB_INSTRUMENT_VA(this, expr); 1176 1177 ValueLocker locker; 1178 lldb::ValueObjectSP value_sp(GetSP(locker)); 1179 if (!value_sp) 1180 return SBValue(); 1181 1182 lldb::TargetSP target_sp = value_sp->GetTargetSP(); 1183 if (!target_sp) 1184 return SBValue(); 1185 1186 lldb::SBExpressionOptions options; 1187 options.SetFetchDynamicValue(target_sp->GetPreferDynamicValue()); 1188 options.SetUnwindOnError(true); 1189 options.SetIgnoreBreakpoints(true); 1190 1191 return EvaluateExpression(expr, options, nullptr); 1192 } 1193 1194 lldb::SBValue 1195 SBValue::EvaluateExpression(const char *expr, 1196 const SBExpressionOptions &options) const { 1197 LLDB_INSTRUMENT_VA(this, expr, options); 1198 1199 return EvaluateExpression(expr, options, nullptr); 1200 } 1201 1202 lldb::SBValue SBValue::EvaluateExpression(const char *expr, 1203 const SBExpressionOptions &options, 1204 const char *name) const { 1205 LLDB_INSTRUMENT_VA(this, expr, options, name); 1206 1207 if (!expr || expr[0] == '\0') { 1208 return SBValue(); 1209 } 1210 1211 1212 ValueLocker locker; 1213 lldb::ValueObjectSP value_sp(GetSP(locker)); 1214 if (!value_sp) { 1215 return SBValue(); 1216 } 1217 1218 lldb::TargetSP target_sp = value_sp->GetTargetSP(); 1219 if (!target_sp) { 1220 return SBValue(); 1221 } 1222 1223 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 1224 ExecutionContext exe_ctx(target_sp.get()); 1225 1226 StackFrame *frame = exe_ctx.GetFramePtr(); 1227 if (!frame) { 1228 return SBValue(); 1229 } 1230 1231 ValueObjectSP res_val_sp; 1232 target_sp->EvaluateExpression(expr, frame, res_val_sp, options.ref(), nullptr, 1233 value_sp.get()); 1234 1235 if (name) 1236 res_val_sp->SetName(ConstString(name)); 1237 1238 SBValue result; 1239 result.SetSP(res_val_sp, options.GetFetchDynamicValue()); 1240 return result; 1241 } 1242 1243 bool SBValue::GetDescription(SBStream &description) { 1244 LLDB_INSTRUMENT_VA(this, description); 1245 1246 Stream &strm = description.ref(); 1247 1248 ValueLocker locker; 1249 lldb::ValueObjectSP value_sp(GetSP(locker)); 1250 if (value_sp) { 1251 DumpValueObjectOptions options; 1252 options.SetUseDynamicType(m_opaque_sp->GetUseDynamic()); 1253 options.SetUseSyntheticValue(m_opaque_sp->GetUseSynthetic()); 1254 if (llvm::Error error = value_sp->Dump(strm, options)) { 1255 strm << "error: " << toString(std::move(error)); 1256 return false; 1257 } 1258 } else { 1259 strm.PutCString("No value"); 1260 } 1261 1262 return true; 1263 } 1264 1265 lldb::Format SBValue::GetFormat() { 1266 LLDB_INSTRUMENT_VA(this); 1267 1268 ValueLocker locker; 1269 lldb::ValueObjectSP value_sp(GetSP(locker)); 1270 if (value_sp) 1271 return value_sp->GetFormat(); 1272 return eFormatDefault; 1273 } 1274 1275 void SBValue::SetFormat(lldb::Format format) { 1276 LLDB_INSTRUMENT_VA(this, format); 1277 1278 ValueLocker locker; 1279 lldb::ValueObjectSP value_sp(GetSP(locker)); 1280 if (value_sp) 1281 value_sp->SetFormat(format); 1282 } 1283 1284 lldb::SBValue SBValue::AddressOf() { 1285 LLDB_INSTRUMENT_VA(this); 1286 1287 SBValue sb_value; 1288 ValueLocker locker; 1289 lldb::ValueObjectSP value_sp(GetSP(locker)); 1290 if (value_sp) { 1291 Status error; 1292 sb_value.SetSP(value_sp->AddressOf(error), GetPreferDynamicValue(), 1293 GetPreferSyntheticValue()); 1294 } 1295 1296 return sb_value; 1297 } 1298 1299 lldb::addr_t SBValue::GetLoadAddress() { 1300 LLDB_INSTRUMENT_VA(this); 1301 1302 lldb::addr_t value = LLDB_INVALID_ADDRESS; 1303 ValueLocker locker; 1304 lldb::ValueObjectSP value_sp(GetSP(locker)); 1305 if (value_sp) 1306 return value_sp->GetLoadAddress(); 1307 1308 return value; 1309 } 1310 1311 lldb::SBAddress SBValue::GetAddress() { 1312 LLDB_INSTRUMENT_VA(this); 1313 1314 Address addr; 1315 ValueLocker locker; 1316 lldb::ValueObjectSP value_sp(GetSP(locker)); 1317 if (value_sp) { 1318 TargetSP target_sp(value_sp->GetTargetSP()); 1319 if (target_sp) { 1320 lldb::addr_t value = LLDB_INVALID_ADDRESS; 1321 const bool scalar_is_load_address = true; 1322 AddressType addr_type; 1323 value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type); 1324 if (addr_type == eAddressTypeFile) { 1325 ModuleSP module_sp(value_sp->GetModule()); 1326 if (module_sp) 1327 module_sp->ResolveFileAddress(value, addr); 1328 } else if (addr_type == eAddressTypeLoad) { 1329 // no need to check the return value on this.. if it can actually do 1330 // the resolve addr will be in the form (section,offset), otherwise it 1331 // will simply be returned as (NULL, value) 1332 addr.SetLoadAddress(value, target_sp.get()); 1333 } 1334 } 1335 } 1336 1337 return SBAddress(addr); 1338 } 1339 1340 lldb::SBData SBValue::GetPointeeData(uint32_t item_idx, uint32_t item_count) { 1341 LLDB_INSTRUMENT_VA(this, item_idx, item_count); 1342 1343 lldb::SBData sb_data; 1344 ValueLocker locker; 1345 lldb::ValueObjectSP value_sp(GetSP(locker)); 1346 if (value_sp) { 1347 TargetSP target_sp(value_sp->GetTargetSP()); 1348 if (target_sp) { 1349 DataExtractorSP data_sp(new DataExtractor()); 1350 value_sp->GetPointeeData(*data_sp, item_idx, item_count); 1351 if (data_sp->GetByteSize() > 0) 1352 *sb_data = data_sp; 1353 } 1354 } 1355 1356 return sb_data; 1357 } 1358 1359 lldb::SBData SBValue::GetData() { 1360 LLDB_INSTRUMENT_VA(this); 1361 1362 lldb::SBData sb_data; 1363 ValueLocker locker; 1364 lldb::ValueObjectSP value_sp(GetSP(locker)); 1365 if (value_sp) { 1366 DataExtractorSP data_sp(new DataExtractor()); 1367 Status error; 1368 value_sp->GetData(*data_sp, error); 1369 if (error.Success()) 1370 *sb_data = data_sp; 1371 } 1372 1373 return sb_data; 1374 } 1375 1376 bool SBValue::SetData(lldb::SBData &data, SBError &error) { 1377 LLDB_INSTRUMENT_VA(this, data, error); 1378 1379 ValueLocker locker; 1380 lldb::ValueObjectSP value_sp(GetSP(locker)); 1381 bool ret = true; 1382 1383 if (value_sp) { 1384 DataExtractor *data_extractor = data.get(); 1385 1386 if (!data_extractor) { 1387 error.SetErrorString("No data to set"); 1388 ret = false; 1389 } else { 1390 Status set_error; 1391 1392 value_sp->SetData(*data_extractor, set_error); 1393 1394 if (!set_error.Success()) { 1395 error.SetErrorStringWithFormat("Couldn't set data: %s", 1396 set_error.AsCString()); 1397 ret = false; 1398 } 1399 } 1400 } else { 1401 error.SetErrorStringWithFormat( 1402 "Couldn't set data: could not get SBValue: %s", 1403 locker.GetError().AsCString()); 1404 ret = false; 1405 } 1406 1407 return ret; 1408 } 1409 1410 lldb::SBValue SBValue::Clone(const char *new_name) { 1411 LLDB_INSTRUMENT_VA(this, new_name); 1412 1413 ValueLocker locker; 1414 lldb::ValueObjectSP value_sp(GetSP(locker)); 1415 1416 if (value_sp) 1417 return lldb::SBValue(value_sp->Clone(ConstString(new_name))); 1418 else 1419 return lldb::SBValue(); 1420 } 1421 1422 lldb::SBDeclaration SBValue::GetDeclaration() { 1423 LLDB_INSTRUMENT_VA(this); 1424 1425 ValueLocker locker; 1426 lldb::ValueObjectSP value_sp(GetSP(locker)); 1427 SBDeclaration decl_sb; 1428 if (value_sp) { 1429 Declaration decl; 1430 if (value_sp->GetDeclaration(decl)) 1431 decl_sb.SetDeclaration(decl); 1432 } 1433 return decl_sb; 1434 } 1435 1436 lldb::SBWatchpoint SBValue::Watch(bool resolve_location, bool read, bool write, 1437 SBError &error) { 1438 LLDB_INSTRUMENT_VA(this, resolve_location, read, write, error); 1439 1440 SBWatchpoint sb_watchpoint; 1441 1442 // If the SBValue is not valid, there's no point in even trying to watch it. 1443 ValueLocker locker; 1444 lldb::ValueObjectSP value_sp(GetSP(locker)); 1445 TargetSP target_sp(GetTarget().GetSP()); 1446 if (value_sp && target_sp) { 1447 // Read and Write cannot both be false. 1448 if (!read && !write) 1449 return sb_watchpoint; 1450 1451 // If the value is not in scope, don't try and watch and invalid value 1452 if (!IsInScope()) 1453 return sb_watchpoint; 1454 1455 addr_t addr = GetLoadAddress(); 1456 if (addr == LLDB_INVALID_ADDRESS) 1457 return sb_watchpoint; 1458 size_t byte_size = GetByteSize(); 1459 if (byte_size == 0) 1460 return sb_watchpoint; 1461 1462 uint32_t watch_type = 0; 1463 if (read) { 1464 watch_type |= LLDB_WATCH_TYPE_READ; 1465 // read + write, the most likely intention 1466 // is to catch all writes to this, not just 1467 // value modifications. 1468 if (write) 1469 watch_type |= LLDB_WATCH_TYPE_WRITE; 1470 } else { 1471 if (write) 1472 watch_type |= LLDB_WATCH_TYPE_MODIFY; 1473 } 1474 1475 Status rc; 1476 CompilerType type(value_sp->GetCompilerType()); 1477 WatchpointSP watchpoint_sp = 1478 target_sp->CreateWatchpoint(addr, byte_size, &type, watch_type, rc); 1479 error.SetError(rc); 1480 1481 if (watchpoint_sp) { 1482 sb_watchpoint.SetSP(watchpoint_sp); 1483 Declaration decl; 1484 if (value_sp->GetDeclaration(decl)) { 1485 if (decl.GetFile()) { 1486 StreamString ss; 1487 // True to show fullpath for declaration file. 1488 decl.DumpStopContext(&ss, true); 1489 watchpoint_sp->SetDeclInfo(std::string(ss.GetString())); 1490 } 1491 } 1492 } 1493 } else if (target_sp) { 1494 error.SetErrorStringWithFormat("could not get SBValue: %s", 1495 locker.GetError().AsCString()); 1496 } else { 1497 error.SetErrorString("could not set watchpoint, a target is required"); 1498 } 1499 1500 return sb_watchpoint; 1501 } 1502 1503 // FIXME: Remove this method impl (as well as the decl in .h) once it is no 1504 // longer needed. 1505 // Backward compatibility fix in the interim. 1506 lldb::SBWatchpoint SBValue::Watch(bool resolve_location, bool read, 1507 bool write) { 1508 LLDB_INSTRUMENT_VA(this, resolve_location, read, write); 1509 1510 SBError error; 1511 return Watch(resolve_location, read, write, error); 1512 } 1513 1514 lldb::SBWatchpoint SBValue::WatchPointee(bool resolve_location, bool read, 1515 bool write, SBError &error) { 1516 LLDB_INSTRUMENT_VA(this, resolve_location, read, write, error); 1517 1518 SBWatchpoint sb_watchpoint; 1519 if (IsInScope() && GetType().IsPointerType()) 1520 sb_watchpoint = Dereference().Watch(resolve_location, read, write, error); 1521 return sb_watchpoint; 1522 } 1523 1524 lldb::SBValue SBValue::Persist() { 1525 LLDB_INSTRUMENT_VA(this); 1526 1527 ValueLocker locker; 1528 lldb::ValueObjectSP value_sp(GetSP(locker)); 1529 SBValue persisted_sb; 1530 if (value_sp) { 1531 persisted_sb.SetSP(value_sp->Persist()); 1532 } 1533 return persisted_sb; 1534 } 1535 1536 lldb::SBValue SBValue::GetVTable() { 1537 SBValue vtable_sb; 1538 ValueLocker locker; 1539 lldb::ValueObjectSP value_sp(GetSP(locker)); 1540 if (!value_sp) 1541 return vtable_sb; 1542 1543 vtable_sb.SetSP(value_sp->GetVTable()); 1544 return vtable_sb; 1545 } 1546