LLDB  mainline
ValueObjectChild.cpp
Go to the documentation of this file.
1 //===-- ValueObjectChild.cpp ------------------------------------*- C++ -*-===//
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 <stdio.h>
25 #include <string.h>
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  const char *compiler_type_name = name.AsCString();
62  if (compiler_type_name) {
63  std::vector<char> bitfield_type_name(strlen(compiler_type_name) + 32, 0);
64  ::snprintf(&bitfield_type_name.front(), bitfield_type_name.size(),
65  "%s:%u", compiler_type_name, bitfield_bit_size);
66  name.SetCString(&bitfield_type_name.front());
67  }
68  }
69 }
70 
72  if (m_type_name.IsEmpty()) {
75  }
76  return m_type_name;
77 }
78 
80  ConstString qualified_name = GetCompilerType().GetConstTypeName();
82  return qualified_name;
83 }
84 
88  return display_name;
89 }
90 
92  if (m_can_update_with_invalid_exe_ctx.hasValue())
93  return m_can_update_with_invalid_exe_ctx.getValue();
94  if (m_parent) {
95  ValueObject *opinionated_parent =
96  m_parent->FollowParentChain([](ValueObject *valobj) -> bool {
97  return (valobj->CanUpdateWithInvalidExecutionContext() ==
99  });
100  if (opinionated_parent)
102  opinionated_parent->CanUpdateWithInvalidExecutionContext())
103  .getValue();
104  }
107  .getValue();
108 }
109 
111  m_error.Clear();
112  SetValueIsValid(false);
113  ValueObject *parent = m_parent;
114  if (parent) {
115  if (parent->UpdateValueIfNeeded(false)) {
117 
118  CompilerType parent_type(parent->GetCompilerType());
119  // Copy the parent scalar value and the scalar value type
120  m_value.GetScalar() = parent->GetValue().GetScalar();
121  Value::ValueType value_type = parent->GetValue().GetValueType();
122  m_value.SetValueType(value_type);
123 
124  Flags parent_type_flags(parent_type.GetTypeInfo());
125  const bool is_instance_ptr_base =
126  ((m_is_base_class) &&
127  (parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));
128 
130  lldb::addr_t addr = parent->GetPointerValue();
131  m_value.GetScalar() = addr;
132 
133  if (addr == LLDB_INVALID_ADDRESS) {
134  m_error.SetErrorString("parent address is invalid.");
135  } else if (addr == 0) {
136  m_error.SetErrorString("parent is NULL");
137  } else {
139  AddressType addr_type = parent->GetAddressTypeOfChildren();
140 
141  switch (addr_type) {
142  case eAddressTypeFile: {
143  lldb::ProcessSP process_sp(GetProcessSP());
144  if (process_sp && process_sp->IsAlive())
146  else
148  } break;
149  case eAddressTypeLoad:
150  m_value.SetValueType(is_instance_ptr_base
153  break;
154  case eAddressTypeHost:
156  break;
157  case eAddressTypeInvalid:
158  // TODO: does this make sense?
160  break;
161  }
162  }
163  } else {
164  switch (value_type) {
168  lldb::addr_t addr =
170  if (addr == LLDB_INVALID_ADDRESS) {
171  m_error.SetErrorString("parent address is invalid.");
172  } else if (addr == 0) {
173  m_error.SetErrorString("parent is NULL");
174  } else {
175  // Set this object's scalar value to the address of its value by
176  // adding its byte offset to the parent address
178  }
179  } break;
180 
182  // try to extract the child value from the parent's scalar value
183  {
184  Scalar scalar(m_value.GetScalar());
188  else
189  scalar.ExtractBitfield(8 * m_byte_size, 8 * m_byte_offset);
190  m_value.GetScalar() = scalar;
191  }
192  break;
193  default:
194  m_error.SetErrorString("parent has invalid value.");
195  break;
196  }
197  }
198 
199  if (m_error.Success()) {
200  const bool thread_and_frame_only_if_stopped = true;
201  ExecutionContext exe_ctx(
202  GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
203  if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {
204  Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;
205  m_error =
206  value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
207  } else {
208  m_error.Clear(); // No value so nothing to read...
209  }
210  }
211 
212  } else {
213  m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",
214  parent->GetError().AsCString());
215  }
216  } else {
217  m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
218  }
219 
220  return m_error.Success();
221 }
222 
224  ValueObject *root(GetRoot());
225  if (root)
226  return root->IsInScope();
227  return false;
228 }
ValueObjectChild(ValueObject &parent, const CompilerType &compiler_type, ConstString name, uint64_t byte_size, int32_t byte_offset, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool is_base_class, bool is_deref_of_parent, AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)
CompilerType GetCompilerType()
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:224
Address is an address as found in an object or symbol file.
virtual uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr)
bool ShouldTreatScalarValueAsAddress() const
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition: Scalar.cpp:2675
virtual LazyBool CanUpdateWithInvalidExecutionContext()
Definition: ValueObject.h:958
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, uint32_t data_offset, Module *module)
Definition: Value.cpp:316
void SetValueType(ValueType value_type)
Definition: Value.h:154
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const Status & GetError()
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
lldb::addr_t GetPointerValue(AddressType *address_type=nullptr)
lldb::offset_t GetByteOffset() override
void SetAddressTypeOfChildren(AddressType at)
Definition: ValueObject.h:777
virtual lldb::ModuleSP GetModule()
ValueType GetValueType() const
Definition: Value.cpp:114
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
llvm::Optional< LazyBool > m_can_update_with_invalid_exe_ctx
Address is an address in the process that is running this code.
LazyBool CanUpdateWithInvalidExecutionContext() override
lldb::ValueType GetValueType() const override
void Clear()
Clear the object state.
Definition: Status.cpp:167
ConstString GetQualifiedTypeName() override
size_t CalculateNumChildren(uint32_t max) override
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition: Scalar.cpp:1566
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:353
AddressType GetAddressTypeOfChildren()
virtual bool IsInScope()
Definition: ValueObject.h:435
ConstString GetDisplayTypeName() const
bool Success() const
Test for success condition.
Definition: Status.cpp:287
ValueObject * FollowParentChain(std::function< bool(ValueObject *)>)
void SetValueIsValid(bool valid)
A class to manage flags.
Definition: Flags.h:22
ConstString GetDisplayTypeName() override
uint64_t addr_t
Definition: lldb-types.h:83
bool UpdateValueIfNeeded(bool update_format=true)
A uniqued constant string class.
Definition: ConstString.h:38
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:361
static void AdjustForBitfieldness(ConstString &name, uint8_t bitfield_bit_size)
virtual lldb::ValueType GetValueType() const =0
const Value & GetValue() const
virtual void SetLanguageFlags(uint64_t flags)
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:268
const Scalar & GetScalar() const
Definition: Value.h:178
ConstString GetConstTypeName() const
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
Address is an address as in the current target inferior process.
ConstString GetTypeName() override
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:340
void SetCString(const char *cstr)
Set the C string value.