xref: /freebsd-src/contrib/llvm-project/lldb/source/Core/ValueObjectMemory.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
1 //===-- ValueObjectMemory.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/Core/ValueObjectMemory.h"
10 #include "lldb/Core/Value.h"
11 #include "lldb/Core/ValueObject.h"
12 #include "lldb/Symbol/Type.h"
13 #include "lldb/Target/ExecutionContext.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Utility/DataExtractor.h"
16 #include "lldb/Utility/Scalar.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/lldb-types.h"
19 #include "llvm/Support/ErrorHandling.h"
20 
21 #include <assert.h>
22 #include <memory>
23 
24 namespace lldb_private {
25 class ExecutionContextScope;
26 }
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
32                                         llvm::StringRef name,
33                                         const Address &address,
34                                         lldb::TypeSP &type_sp) {
35   auto manager_sp = ValueObjectManager::Create();
36   return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
37       ->GetSP();
38 }
39 
40 ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
41                                         llvm::StringRef name,
42                                         const Address &address,
43                                         const CompilerType &ast_type) {
44   auto manager_sp = ValueObjectManager::Create();
45   return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
46                                 ast_type))
47       ->GetSP();
48 }
49 
50 ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
51                                      ValueObjectManager &manager,
52                                      llvm::StringRef name,
53                                      const Address &address,
54                                      lldb::TypeSP &type_sp)
55     : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
56       m_compiler_type() {
57   // Do not attempt to construct one of these objects with no variable!
58   assert(m_type_sp.get() != nullptr);
59   SetName(ConstString(name));
60   m_value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get());
61   TargetSP target_sp(GetTargetSP());
62   lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
63   if (load_address != LLDB_INVALID_ADDRESS) {
64     m_value.SetValueType(Value::eValueTypeLoadAddress);
65     m_value.GetScalar() = load_address;
66   } else {
67     lldb::addr_t file_address = m_address.GetFileAddress();
68     if (file_address != LLDB_INVALID_ADDRESS) {
69       m_value.SetValueType(Value::eValueTypeFileAddress);
70       m_value.GetScalar() = file_address;
71     } else {
72       m_value.GetScalar() = m_address.GetOffset();
73       m_value.SetValueType(Value::eValueTypeScalar);
74     }
75   }
76 }
77 
78 ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
79                                      ValueObjectManager &manager,
80                                      llvm::StringRef name,
81                                      const Address &address,
82                                      const CompilerType &ast_type)
83     : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
84       m_compiler_type(ast_type) {
85   // Do not attempt to construct one of these objects with no variable!
86   assert(m_compiler_type.GetTypeSystem());
87   assert(m_compiler_type.GetOpaqueQualType());
88 
89   TargetSP target_sp(GetTargetSP());
90 
91   SetName(ConstString(name));
92   m_value.SetCompilerType(m_compiler_type);
93   lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
94   if (load_address != LLDB_INVALID_ADDRESS) {
95     m_value.SetValueType(Value::eValueTypeLoadAddress);
96     m_value.GetScalar() = load_address;
97   } else {
98     lldb::addr_t file_address = m_address.GetFileAddress();
99     if (file_address != LLDB_INVALID_ADDRESS) {
100       m_value.SetValueType(Value::eValueTypeFileAddress);
101       m_value.GetScalar() = file_address;
102     } else {
103       m_value.GetScalar() = m_address.GetOffset();
104       m_value.SetValueType(Value::eValueTypeScalar);
105     }
106   }
107 }
108 
109 ValueObjectMemory::~ValueObjectMemory() {}
110 
111 CompilerType ValueObjectMemory::GetCompilerTypeImpl() {
112   if (m_type_sp)
113     return m_type_sp->GetForwardCompilerType();
114   return m_compiler_type;
115 }
116 
117 ConstString ValueObjectMemory::GetTypeName() {
118   if (m_type_sp)
119     return m_type_sp->GetName();
120   return m_compiler_type.GetTypeName();
121 }
122 
123 ConstString ValueObjectMemory::GetDisplayTypeName() {
124   if (m_type_sp)
125     return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
126   return m_compiler_type.GetDisplayTypeName();
127 }
128 
129 size_t ValueObjectMemory::CalculateNumChildren(uint32_t max) {
130   if (m_type_sp) {
131     auto child_count = m_type_sp->GetNumChildren(true);
132     return child_count <= max ? child_count : max;
133   }
134 
135   ExecutionContext exe_ctx(GetExecutionContextRef());
136   const bool omit_empty_base_classes = true;
137   auto child_count =
138       m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
139   return child_count <= max ? child_count : max;
140 }
141 
142 uint64_t ValueObjectMemory::GetByteSize() {
143   if (m_type_sp)
144     return m_type_sp->GetByteSize().getValueOr(0);
145   return m_compiler_type.GetByteSize(nullptr).getValueOr(0);
146 }
147 
148 lldb::ValueType ValueObjectMemory::GetValueType() const {
149   // RETHINK: Should this be inherited from somewhere?
150   return lldb::eValueTypeVariableGlobal;
151 }
152 
153 bool ValueObjectMemory::UpdateValue() {
154   SetValueIsValid(false);
155   m_error.Clear();
156 
157   ExecutionContext exe_ctx(GetExecutionContextRef());
158 
159   Target *target = exe_ctx.GetTargetPtr();
160   if (target) {
161     m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
162     m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
163   }
164 
165   Value old_value(m_value);
166   if (m_address.IsValid()) {
167     Value::ValueType value_type = m_value.GetValueType();
168 
169     switch (value_type) {
170     default:
171       llvm_unreachable("Unhandled expression result value kind...");
172 
173     case Value::eValueTypeScalar:
174       // The variable value is in the Scalar value inside the m_value. We can
175       // point our m_data right to it.
176       m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
177       break;
178 
179     case Value::eValueTypeFileAddress:
180     case Value::eValueTypeLoadAddress:
181     case Value::eValueTypeHostAddress:
182       // The DWARF expression result was an address in the inferior process. If
183       // this variable is an aggregate type, we just need the address as the
184       // main value as all child variable objects will rely upon this location
185       // and add an offset and then read their own values as needed. If this
186       // variable is a simple type, we read all data for it into m_data. Make
187       // sure this type has a value before we try and read it
188 
189       // If we have a file address, convert it to a load address if we can.
190       if (value_type == Value::eValueTypeFileAddress &&
191           exe_ctx.GetProcessPtr()) {
192         lldb::addr_t load_addr = m_address.GetLoadAddress(target);
193         if (load_addr != LLDB_INVALID_ADDRESS) {
194           m_value.SetValueType(Value::eValueTypeLoadAddress);
195           m_value.GetScalar() = load_addr;
196         }
197       }
198 
199       if (!CanProvideValue()) {
200         // this value object represents an aggregate type whose children have
201         // values, but this object does not. So we say we are changed if our
202         // location has changed.
203         SetValueDidChange(value_type != old_value.GetValueType() ||
204                           m_value.GetScalar() != old_value.GetScalar());
205       } else {
206         // Copy the Value and set the context to use our Variable so it can
207         // extract read its value into m_data appropriately
208         Value value(m_value);
209         if (m_type_sp)
210           value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get());
211         else {
212           value.SetCompilerType(m_compiler_type);
213         }
214 
215         m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
216       }
217       break;
218     }
219 
220     SetValueIsValid(m_error.Success());
221   }
222   return m_error.Success();
223 }
224 
225 bool ValueObjectMemory::IsInScope() {
226   // FIXME: Maybe try to read the memory address, and if that works, then
227   // we are in scope?
228   return true;
229 }
230 
231 lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }
232