LLDB  mainline
ValueObjectChild.cpp
Go to the documentation of this file.
1 //===-- ValueObjectChild.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 #include "lldb/Core/Value.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Utility/Flags.h"
16 #include "lldb/Utility/Scalar.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/lldb-forward.h"
19 
20 #include <functional>
21 #include <memory>
22 #include <vector>
23 
24 #include <cstdio>
25 #include <cstring>
26 
27 using namespace lldb_private;
28 
30  ValueObject &parent, const CompilerType &compiler_type,
31  ConstString name, uint64_t byte_size, int32_t byte_offset,
32  uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
33  bool is_base_class, bool is_deref_of_parent,
34  AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)
35  : ValueObject(parent), m_compiler_type(compiler_type),
36  m_byte_size(byte_size), m_byte_offset(byte_offset),
37  m_bitfield_bit_size(bitfield_bit_size),
38  m_bitfield_bit_offset(bitfield_bit_offset),
39  m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),
40  m_can_update_with_invalid_exe_ctx() {
41  m_name = name;
42  SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);
43  SetLanguageFlags(language_flags);
44 }
45 
47 
49  return m_parent->GetValueType();
50 }
51 
54  auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
55  return children_count <= max ? children_count : max;
56 }
57 
59  uint8_t bitfield_bit_size) {
60  if (name && bitfield_bit_size)
61  name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());
62 }
63 
65  if (m_type_name.IsEmpty()) {
68  }
69  return m_type_name;
70 }
71 
73  ConstString qualified_name = GetCompilerType().GetTypeName();
75  return qualified_name;
76 }
77 
81  return display_name;
82 }
83 
85  if (m_can_update_with_invalid_exe_ctx.hasValue())
86  return m_can_update_with_invalid_exe_ctx.getValue();
87  if (m_parent) {
88  ValueObject *opinionated_parent =
89  m_parent->FollowParentChain([](ValueObject *valobj) -> bool {
90  return (valobj->CanUpdateWithInvalidExecutionContext() ==
92  });
93  if (opinionated_parent)
95  opinionated_parent->CanUpdateWithInvalidExecutionContext())
96  .getValue();
97  }
100  .getValue();
101 }
102 
104  m_error.Clear();
105  SetValueIsValid(false);
106  ValueObject *parent = m_parent;
107  if (parent) {
108  if (parent->UpdateValueIfNeeded(false)) {
110 
111  CompilerType parent_type(parent->GetCompilerType());
112  // Copy the parent scalar value and the scalar value type
113  m_value.GetScalar() = parent->GetValue().GetScalar();
115 
116  Flags parent_type_flags(parent_type.GetTypeInfo());
117  const bool is_instance_ptr_base =
118  ((m_is_base_class) &&
119  (parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));
120 
122  m_value.GetScalar() = parent->GetPointerValue();
123 
124  switch (parent->GetAddressTypeOfChildren()) {
125  case eAddressTypeFile: {
126  lldb::ProcessSP process_sp(GetProcessSP());
127  if (process_sp && process_sp->IsAlive())
129  else
131  } break;
132  case eAddressTypeLoad:
133  m_value.SetValueType(is_instance_ptr_base
136  break;
137  case eAddressTypeHost:
139  break;
140  case eAddressTypeInvalid:
141  // TODO: does this make sense?
143  break;
144  }
145  }
146  switch (m_value.GetValueType()) {
148  break;
153  if (addr == LLDB_INVALID_ADDRESS) {
154  m_error.SetErrorString("parent address is invalid.");
155  } else if (addr == 0) {
156  m_error.SetErrorString("parent is NULL");
157  } else {
158  // If a bitfield doesn't fit into the child_byte_size'd window at
159  // child_byte_offset, move the window forward until it fits. The
160  // problem here is that Value has no notion of bitfields and thus the
161  // Value's DataExtractor is sized like the bitfields CompilerType; a
162  // sequence of bitfields, however, can be larger than their underlying
163  // type.
164  if (m_bitfield_bit_offset) {
165  const bool thread_and_frame_only_if_stopped = true;
167  thread_and_frame_only_if_stopped));
168  if (auto type_bit_size = GetCompilerType().GetBitSize(
169  exe_ctx.GetBestExecutionContextScope())) {
170  uint64_t bitfield_end =
172  if (bitfield_end > *type_bit_size) {
173  uint64_t overhang_bytes =
174  (bitfield_end - *type_bit_size + 7) / 8;
175  m_byte_offset += overhang_bytes;
176  m_bitfield_bit_offset -= overhang_bytes * 8;
177  }
178  }
179  }
180 
181  // Set this object's scalar value to the address of its value by
182  // adding its byte offset to the parent address
184  }
185  } break;
186 
188  // try to extract the child value from the parent's scalar value
189  {
190  Scalar scalar(m_value.GetScalar());
191  scalar.ExtractBitfield(8 * m_byte_size, 8 * m_byte_offset);
192  m_value.GetScalar() = scalar;
193  }
194  break;
195  }
196 
197  if (m_error.Success()) {
198  const bool thread_and_frame_only_if_stopped = true;
199  ExecutionContext exe_ctx(
200  GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
201  if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {
202  Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;
203  m_error =
204  value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
205  } else {
206  m_error.Clear(); // No value so nothing to read...
207  }
208  }
209 
210  } else {
211  m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",
212  parent->GetError().AsCString());
213  }
214  } else {
215  m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
216  }
217 
218  return m_error.Success();
219 }
220 
222  ValueObject *root(GetRoot());
223  if (root)
224  return root->IsInScope();
225  return false;
226 }
lldb_private::ValueObjectChild::m_byte_size
uint64_t m_byte_size
Definition: ValueObjectChild.h:66
lldb_private::Value::ValueType::Scalar
@ Scalar
A raw scalar value.
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
Scalar.h
lldb_private::ValueObject::IsInScope
virtual bool IsInScope()
Definition: ValueObject.h:420
lldb_private::CompilerType::ShouldTreatScalarValueAsAddress
bool ShouldTreatScalarValueAsAddress() const
Definition: CompilerType.cpp:185
lldb_private::ValueObject::SetValueIsValid
void SetValueIsValid(bool valid)
Definition: ValueObject.h:980
lldb_private::ValueObject::SetLanguageFlags
virtual void SetLanguageFlags(uint64_t flags)
Definition: ValueObject.h:791
lldb_private::Flags::AnySet
bool AnySet(ValueType mask) const
Test one or more flags.
Definition: Flags.h:90
lldb_private::ValueObject::GetValue
const Value & GetValue() const
Definition: ValueObject.h:497
lldb_private::ValueObjectChild::~ValueObjectChild
~ValueObjectChild() override
lldb_private::Value
Definition: Value.h:38
lldb_private::Value::SetValueType
void SetValueType(ValueType value_type)
Definition: Value.h:89
lldb_private::Value::GetValueAsData
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:301
lldb_private::Value::ValueType::Invalid
@ Invalid
lldb_private::Value::ValueType::FileAddress
@ FileAddress
A file address value.
lldb_private::Value::GetValueType
ValueType GetValueType() const
Definition: Value.cpp:109
lldb_private::ValueObjectChild::m_bitfield_bit_size
uint8_t m_bitfield_bit_size
Definition: ValueObjectChild.h:68
lldb_private::AddressType
AddressType
Definition: lldb-private-enumerations.h:30
lldb_private::ValueObject::m_name
ConstString m_name
The name of this object.
Definition: ValueObject.h:849
lldb_private::Scalar
Definition: Scalar.h:34
lldb_private::ValueObject::FollowParentChain
ValueObject * FollowParentChain(std::function< bool(ValueObject *)>)
Given a ValueObject, loop over itself and its parent, and its parent's parent, .
Definition: ValueObject.cpp:3040
lldb_private::ValueObjectChild::GetDisplayTypeName
ConstString GetDisplayTypeName() override
Definition: ValueObjectChild.cpp:78
lldb_private::eAddressTypeHost
@ eAddressTypeHost
Address is an address in the process that is running this code.
Definition: lldb-private-enumerations.h:36
lldb_private::ValueObjectChild::GetTypeName
ConstString GetTypeName() override
Definition: ValueObjectChild.cpp:64
lldb_private::ValueObject::GetPointerValue
lldb::addr_t GetPointerValue(AddressType *address_type=nullptr)
Definition: ValueObject.cpp:1436
lldb_private::Flags
Definition: Flags.h:22
lldb_private::ValueObjectChild::CanUpdateWithInvalidExecutionContext
LazyBool CanUpdateWithInvalidExecutionContext() override
Definition: ValueObjectChild.cpp:84
lldb_private::Value::ValueType::LoadAddress
@ LoadAddress
A load address value.
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::ValueObject::GetRoot
ValueObject * GetRoot()
Definition: ValueObject.cpp:3031
lldb_private::LazyBool
LazyBool
Definition: lldb-private-enumerations.h:115
AdjustForBitfieldness
static void AdjustForBitfieldness(ConstString &name, uint8_t bitfield_bit_size)
Definition: ValueObjectChild.cpp:58
Process.h
lldb_private::ValueObject::GetError
const Status & GetError()
Definition: ValueObject.cpp:283
lldb_private::Value::SetCompilerType
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:254
lldb_private::ValueObject::m_data
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:851
lldb_private::ValueObjectChild::GetValueType
lldb::ValueType GetValueType() const override
Definition: ValueObjectChild.cpp:48
lldb_private::ValueObject::GetProcessSP
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:338
lldb_private::Value::GetScalar
const Scalar & GetScalar() const
Definition: Value.h:112
lldb_private::ValueObject::ValueObjectChild
friend class ValueObjectChild
Definition: ValueObject.h:931
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:288
lldb_private::CompilerType::GetDisplayTypeName
ConstString GetDisplayTypeName() const
Definition: CompilerType.cpp:284
lldb_private::ValueObjectChild::m_bitfield_bit_offset
uint8_t m_bitfield_bit_offset
Definition: ValueObjectChild.h:69
lldb_private::ConstString::SetString
void SetString(const llvm::StringRef &s)
Definition: ConstString.cpp:306
lldb_private::eAddressTypeInvalid
@ eAddressTypeInvalid
Definition: lldb-private-enumerations.h:31
lldb_private::Scalar::ULongLong
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition: Scalar.cpp:334
lldb_private::ConstString::IsEmpty
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:304
lldb_private::ValueObject::SetAddressTypeOfChildren
void SetAddressTypeOfChildren(AddressType at)
Definition: ValueObject.h:759
lldb_private::Status::SetErrorStringWithFormat
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:256
lldb_private::ValueObjectChild::m_type_name
ConstString m_type_name
Definition: ValueObjectChild.h:65
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::ValueObject::UpdateValueIfNeeded
bool UpdateValueIfNeeded(bool update_format=true)
Definition: ValueObject.cpp:111
lldb_private::Scalar::ExtractBitfield
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition: Scalar.cpp:798
lldb_private::ValueObject::GetExecutionContextRef
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
lldb_private::eAddressTypeLoad
@ eAddressTypeLoad
Address is an address as in the current target inferior process.
Definition: lldb-private-enumerations.h:34
lldb_private::ValueObjectChild::GetQualifiedTypeName
ConstString GetQualifiedTypeName() override
Definition: ValueObjectChild.cpp:72
CompilerType.h
lldb_private::CompilerType::GetNumChildren
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
Definition: CompilerType.cpp:515
lldb_private::ValueObject::GetValueType
virtual lldb::ValueType GetValueType() const =0
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb::ValueType
ValueType
Definition: lldb-enumerations.h:315
uint32_t
lldb_private::ExecutionContext::GetBestExecutionContextScope
ExecutionContextScope * GetBestExecutionContextScope() const
Definition: ExecutionContext.cpp:216
lldb_private::ValueObjectChild::CalculateNumChildren
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
Definition: ValueObjectChild.cpp:52
lldb_private::ValueObject::m_value
Value m_value
Definition: ValueObject.h:852
lldb_private::Value::ValueType::HostAddress
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
lldb_private::ValueObject::m_parent
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
Definition: ValueObject.h:840
lldb_private::ValueObjectChild::UpdateValue
bool UpdateValue() override
Definition: ValueObjectChild.cpp:103
lldb_private::CompilerType::GetTypeName
ConstString GetTypeName() const
Definition: CompilerType.cpp:277
lldb_private::eAddressTypeFile
@ eAddressTypeFile
Address is an address as found in an object or symbol file.
Definition: lldb-private-enumerations.h:32
lldb_private::CompilerType::GetTypeInfo
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
Definition: CompilerType.cpp:290
lldb_private::Status::SetErrorString
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
lldb_private::ValueObject::GetCompilerType
CompilerType GetCompilerType()
Definition: ValueObject.h:352
ValueObjectChild.h
lldb_private::ValueObjectChild::IsInScope
bool IsInScope() override
Definition: ValueObjectChild.cpp:221
lldb_private::ValueObject::GetModule
virtual lldb::ModuleSP GetModule()
Return the module associated with this value object in case the value is from an executable file and ...
Definition: ValueObject.cpp:3024
lldb_private::ValueObject::GetTypeInfo
virtual uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr)
Definition: ValueObject.h:378
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb-forward.h
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::eLazyBoolCalculate
@ eLazyBoolCalculate
Definition: lldb-private-enumerations.h:115
lldb_private::ValueObjectChild::m_can_update_with_invalid_exe_ctx
llvm::Optional< LazyBool > m_can_update_with_invalid_exe_ctx
Definition: ValueObjectChild.h:72
lldb_private::ValueObjectChild::m_is_base_class
bool m_is_base_class
Definition: ValueObjectChild.h:70
lldb_private::Status::Clear
void Clear()
Clear the object state.
Definition: Status.cpp:168
Flags.h
lldb_private::ValueObject::CanUpdateWithInvalidExecutionContext
virtual LazyBool CanUpdateWithInvalidExecutionContext()
Definition: ValueObject.h:950
lldb_private::ValueObject::m_error
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:855
Value.h
ExecutionContext.h
lldb_private::ValueObject::GetAddressTypeOfChildren
AddressType GetAddressTypeOfChildren()
Definition: ValueObject.cpp:3050
lldb_private::ValueObjectChild::m_byte_offset
int32_t m_byte_offset
Definition: ValueObjectChild.h:67
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