xref: /src/contrib/llvm-project/lldb/source/Core/ValueObjectChild.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- ValueObjectChild.cpp ----------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Core/ValueObjectChild.h"
10f034231aSEd Maste 
1194994d37SDimitry Andric #include "lldb/Core/Value.h"
12e81d9d49SDimitry Andric #include "lldb/Symbol/CompilerType.h"
13f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
14f034231aSEd Maste #include "lldb/Target/Process.h"
1594994d37SDimitry Andric #include "lldb/Utility/Flags.h"
1694994d37SDimitry Andric #include "lldb/Utility/Scalar.h"
1794994d37SDimitry Andric #include "lldb/Utility/Status.h"
1894994d37SDimitry Andric #include "lldb/lldb-forward.h"
1974a628f7SDimitry Andric 
2094994d37SDimitry Andric #include <functional>
2194994d37SDimitry Andric #include <memory>
2294994d37SDimitry Andric #include <vector>
2374a628f7SDimitry Andric 
24344a3780SDimitry Andric #include <cstdio>
25344a3780SDimitry Andric #include <cstring>
26f034231aSEd Maste 
27f034231aSEd Maste using namespace lldb_private;
28f034231aSEd Maste 
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)2914f1b3e8SDimitry Andric ValueObjectChild::ValueObjectChild(
3014f1b3e8SDimitry Andric     ValueObject &parent, const CompilerType &compiler_type,
315f29bb8aSDimitry Andric     ConstString name, uint64_t byte_size, int32_t byte_offset,
3214f1b3e8SDimitry Andric     uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
3314f1b3e8SDimitry Andric     bool is_base_class, bool is_deref_of_parent,
3414f1b3e8SDimitry Andric     AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)
3514f1b3e8SDimitry Andric     : ValueObject(parent), m_compiler_type(compiler_type),
3614f1b3e8SDimitry Andric       m_byte_size(byte_size), m_byte_offset(byte_offset),
37f034231aSEd Maste       m_bitfield_bit_size(bitfield_bit_size),
38f034231aSEd Maste       m_bitfield_bit_offset(bitfield_bit_offset),
3914f1b3e8SDimitry Andric       m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),
4014f1b3e8SDimitry Andric       m_can_update_with_invalid_exe_ctx() {
41f034231aSEd Maste   m_name = name;
42f034231aSEd Maste   SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);
43e81d9d49SDimitry Andric   SetLanguageFlags(language_flags);
44f034231aSEd Maste }
45f034231aSEd Maste 
46344a3780SDimitry Andric ValueObjectChild::~ValueObjectChild() = default;
47f034231aSEd Maste 
GetValueType() const4814f1b3e8SDimitry Andric lldb::ValueType ValueObjectChild::GetValueType() const {
49f034231aSEd Maste   return m_parent->GetValueType();
50f034231aSEd Maste }
51f034231aSEd Maste 
CalculateNumChildren(uint32_t max)52ac9a064cSDimitry Andric llvm::Expected<uint32_t> ValueObjectChild::CalculateNumChildren(uint32_t max) {
5394994d37SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
5494994d37SDimitry Andric   auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
55ac9a064cSDimitry Andric   if (!children_count)
56ac9a064cSDimitry Andric     return children_count;
57ac9a064cSDimitry Andric   return *children_count <= max ? *children_count : max;
58f034231aSEd Maste }
59f034231aSEd Maste 
AdjustForBitfieldness(ConstString & name,uint8_t bitfield_bit_size)6014f1b3e8SDimitry Andric static void AdjustForBitfieldness(ConstString &name,
6114f1b3e8SDimitry Andric                                   uint8_t bitfield_bit_size) {
62b60736ecSDimitry Andric   if (name && bitfield_bit_size)
63b60736ecSDimitry Andric     name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());
640cac4ca3SEd Maste }
650cac4ca3SEd Maste 
GetTypeName()6614f1b3e8SDimitry Andric ConstString ValueObjectChild::GetTypeName() {
6714f1b3e8SDimitry Andric   if (m_type_name.IsEmpty()) {
68cfca06d7SDimitry Andric     m_type_name = GetCompilerType().GetTypeName();
690cac4ca3SEd Maste     AdjustForBitfieldness(m_type_name, m_bitfield_bit_size);
70f034231aSEd Maste   }
71f034231aSEd Maste   return m_type_name;
72f034231aSEd Maste }
73f034231aSEd Maste 
GetQualifiedTypeName()7414f1b3e8SDimitry Andric ConstString ValueObjectChild::GetQualifiedTypeName() {
75cfca06d7SDimitry Andric   ConstString qualified_name = GetCompilerType().GetTypeName();
760cac4ca3SEd Maste   AdjustForBitfieldness(qualified_name, m_bitfield_bit_size);
77f034231aSEd Maste   return qualified_name;
78f034231aSEd Maste }
79f034231aSEd Maste 
GetDisplayTypeName()8014f1b3e8SDimitry Andric ConstString ValueObjectChild::GetDisplayTypeName() {
81e81d9d49SDimitry Andric   ConstString display_name = GetCompilerType().GetDisplayTypeName();
820cac4ca3SEd Maste   AdjustForBitfieldness(display_name, m_bitfield_bit_size);
830cac4ca3SEd Maste   return display_name;
840cac4ca3SEd Maste }
850cac4ca3SEd Maste 
CanUpdateWithInvalidExecutionContext()8614f1b3e8SDimitry Andric LazyBool ValueObjectChild::CanUpdateWithInvalidExecutionContext() {
87145449b1SDimitry Andric   if (m_can_update_with_invalid_exe_ctx)
88e3b55780SDimitry Andric     return *m_can_update_with_invalid_exe_ctx;
8914f1b3e8SDimitry Andric   if (m_parent) {
9014f1b3e8SDimitry Andric     ValueObject *opinionated_parent =
9114f1b3e8SDimitry Andric         m_parent->FollowParentChain([](ValueObject *valobj) -> bool {
9214f1b3e8SDimitry Andric           return (valobj->CanUpdateWithInvalidExecutionContext() ==
9314f1b3e8SDimitry Andric                   eLazyBoolCalculate);
94e81d9d49SDimitry Andric         });
95e81d9d49SDimitry Andric     if (opinionated_parent)
96e3b55780SDimitry Andric       return *(m_can_update_with_invalid_exe_ctx =
97e3b55780SDimitry Andric                    opinionated_parent->CanUpdateWithInvalidExecutionContext());
98e81d9d49SDimitry Andric   }
99e3b55780SDimitry Andric   return *(m_can_update_with_invalid_exe_ctx =
100e3b55780SDimitry Andric                this->ValueObject::CanUpdateWithInvalidExecutionContext());
1015e95aa85SEd Maste }
1025e95aa85SEd Maste 
UpdateValue()10314f1b3e8SDimitry Andric bool ValueObjectChild::UpdateValue() {
104f034231aSEd Maste   m_error.Clear();
105f034231aSEd Maste   SetValueIsValid(false);
106f034231aSEd Maste   ValueObject *parent = m_parent;
10714f1b3e8SDimitry Andric   if (parent) {
10814f1b3e8SDimitry Andric     if (parent->UpdateValueIfNeeded(false)) {
109e81d9d49SDimitry Andric       m_value.SetCompilerType(GetCompilerType());
110f034231aSEd Maste 
111e81d9d49SDimitry Andric       CompilerType parent_type(parent->GetCompilerType());
112f034231aSEd Maste       // Copy the parent scalar value and the scalar value type
113f034231aSEd Maste       m_value.GetScalar() = parent->GetValue().GetScalar();
114b60736ecSDimitry Andric       m_value.SetValueType(parent->GetValue().GetValueType());
115f034231aSEd Maste 
116e81d9d49SDimitry Andric       Flags parent_type_flags(parent_type.GetTypeInfo());
11714f1b3e8SDimitry Andric       const bool is_instance_ptr_base =
11894994d37SDimitry Andric           ((m_is_base_class) &&
11914f1b3e8SDimitry Andric            (parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));
120e81d9d49SDimitry Andric 
12114f1b3e8SDimitry Andric       if (parent->GetCompilerType().ShouldTreatScalarValueAsAddress()) {
122b60736ecSDimitry Andric         m_value.GetScalar() = parent->GetPointerValue();
123f034231aSEd Maste 
124b60736ecSDimitry Andric         switch (parent->GetAddressTypeOfChildren()) {
12514f1b3e8SDimitry Andric         case eAddressTypeFile: {
126f034231aSEd Maste           lldb::ProcessSP process_sp(GetProcessSP());
12794994d37SDimitry Andric           if (process_sp && process_sp->IsAlive())
128344a3780SDimitry Andric             m_value.SetValueType(Value::ValueType::LoadAddress);
129f034231aSEd Maste           else
130344a3780SDimitry Andric             m_value.SetValueType(Value::ValueType::FileAddress);
13114f1b3e8SDimitry Andric         } break;
132f034231aSEd Maste         case eAddressTypeLoad:
13314f1b3e8SDimitry Andric           m_value.SetValueType(is_instance_ptr_base
134344a3780SDimitry Andric                                    ? Value::ValueType::Scalar
135344a3780SDimitry Andric                                    : Value::ValueType::LoadAddress);
136f034231aSEd Maste           break;
137f034231aSEd Maste         case eAddressTypeHost:
138344a3780SDimitry Andric           m_value.SetValueType(Value::ValueType::HostAddress);
139f034231aSEd Maste           break;
140f034231aSEd Maste         case eAddressTypeInvalid:
141f034231aSEd Maste           // TODO: does this make sense?
142344a3780SDimitry Andric           m_value.SetValueType(Value::ValueType::Scalar);
143f034231aSEd Maste           break;
144f034231aSEd Maste         }
145f034231aSEd Maste       }
146b60736ecSDimitry Andric       switch (m_value.GetValueType()) {
147344a3780SDimitry Andric       case Value::ValueType::Invalid:
148344a3780SDimitry Andric         break;
149344a3780SDimitry Andric       case Value::ValueType::LoadAddress:
150344a3780SDimitry Andric       case Value::ValueType::FileAddress:
151344a3780SDimitry Andric       case Value::ValueType::HostAddress: {
152b60736ecSDimitry Andric         lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
15314f1b3e8SDimitry Andric         if (addr == LLDB_INVALID_ADDRESS) {
154f034231aSEd Maste           m_error.SetErrorString("parent address is invalid.");
15514f1b3e8SDimitry Andric         } else if (addr == 0) {
156f034231aSEd Maste           m_error.SetErrorString("parent is NULL");
15714f1b3e8SDimitry Andric         } else {
158b60736ecSDimitry Andric           // If a bitfield doesn't fit into the child_byte_size'd window at
159b60736ecSDimitry Andric           // child_byte_offset, move the window forward until it fits.  The
160b60736ecSDimitry Andric           // problem here is that Value has no notion of bitfields and thus the
161b60736ecSDimitry Andric           // Value's DataExtractor is sized like the bitfields CompilerType; a
162b60736ecSDimitry Andric           // sequence of bitfields, however, can be larger than their underlying
163ead24645SDimitry Andric           // type.
164ead24645SDimitry Andric           if (m_bitfield_bit_offset) {
165ead24645SDimitry Andric             const bool thread_and_frame_only_if_stopped = true;
166ead24645SDimitry Andric             ExecutionContext exe_ctx(GetExecutionContextRef().Lock(
167ead24645SDimitry Andric                 thread_and_frame_only_if_stopped));
168ead24645SDimitry Andric             if (auto type_bit_size = GetCompilerType().GetBitSize(
169ead24645SDimitry Andric                     exe_ctx.GetBestExecutionContextScope())) {
170ead24645SDimitry Andric               uint64_t bitfield_end =
171ead24645SDimitry Andric                   m_bitfield_bit_size + m_bitfield_bit_offset;
172ead24645SDimitry Andric               if (bitfield_end > *type_bit_size) {
173ead24645SDimitry Andric                 uint64_t overhang_bytes =
174ead24645SDimitry Andric                     (bitfield_end - *type_bit_size + 7) / 8;
175b60736ecSDimitry Andric                 m_byte_offset += overhang_bytes;
176ead24645SDimitry Andric                 m_bitfield_bit_offset -= overhang_bytes * 8;
177ead24645SDimitry Andric               }
178ead24645SDimitry Andric             }
179ead24645SDimitry Andric           }
180b60736ecSDimitry Andric 
181b60736ecSDimitry Andric           // Set this object's scalar value to the address of its value by
182b60736ecSDimitry Andric           // adding its byte offset to the parent address
183b60736ecSDimitry Andric           m_value.GetScalar() += m_byte_offset;
184f034231aSEd Maste         }
18514f1b3e8SDimitry Andric       } break;
186f034231aSEd Maste 
187344a3780SDimitry Andric       case Value::ValueType::Scalar:
188e81d9d49SDimitry Andric         // try to extract the child value from the parent's scalar value
189e81d9d49SDimitry Andric         {
190e81d9d49SDimitry Andric           Scalar scalar(m_value.GetScalar());
191e81d9d49SDimitry Andric           scalar.ExtractBitfield(8 * m_byte_size, 8 * m_byte_offset);
192e81d9d49SDimitry Andric           m_value.GetScalar() = scalar;
193e81d9d49SDimitry Andric         }
194e81d9d49SDimitry Andric         break;
195f034231aSEd Maste       }
196f034231aSEd Maste 
19714f1b3e8SDimitry Andric       if (m_error.Success()) {
198866dcdacSEd Maste         const bool thread_and_frame_only_if_stopped = true;
19914f1b3e8SDimitry Andric         ExecutionContext exe_ctx(
20014f1b3e8SDimitry Andric             GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
20114f1b3e8SDimitry Andric         if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {
20294994d37SDimitry Andric           Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;
20314f1b3e8SDimitry Andric           m_error =
204ead24645SDimitry Andric               value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
20514f1b3e8SDimitry Andric         } else {
206866dcdacSEd Maste           m_error.Clear(); // No value so nothing to read...
207f034231aSEd Maste         }
208f034231aSEd Maste       }
209e81d9d49SDimitry Andric 
21014f1b3e8SDimitry Andric     } else {
21114f1b3e8SDimitry Andric       m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",
21214f1b3e8SDimitry Andric                                        parent->GetError().AsCString());
213e81d9d49SDimitry Andric     }
21414f1b3e8SDimitry Andric   } else {
215f034231aSEd Maste     m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
216f034231aSEd Maste   }
217f034231aSEd Maste 
218f034231aSEd Maste   return m_error.Success();
219f034231aSEd Maste }
220f034231aSEd Maste 
IsInScope()22114f1b3e8SDimitry Andric bool ValueObjectChild::IsInScope() {
222f034231aSEd Maste   ValueObject *root(GetRoot());
223f034231aSEd Maste   if (root)
224f034231aSEd Maste     return root->IsInScope();
225f034231aSEd Maste   return false;
226f034231aSEd Maste }
227