1 | //===-- ValueObjectVariable.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/ValueObject/ValueObjectVariable.h" |
10 | |
11 | #include "lldb/Core/Address.h" |
12 | #include "lldb/Core/AddressRange.h" |
13 | #include "lldb/Core/Declaration.h" |
14 | #include "lldb/Core/Module.h" |
15 | #include "lldb/Core/Value.h" |
16 | #include "lldb/Expression/DWARFExpressionList.h" |
17 | #include "lldb/Symbol/Function.h" |
18 | #include "lldb/Symbol/ObjectFile.h" |
19 | #include "lldb/Symbol/SymbolContext.h" |
20 | #include "lldb/Symbol/SymbolContextScope.h" |
21 | #include "lldb/Symbol/Type.h" |
22 | #include "lldb/Symbol/Variable.h" |
23 | #include "lldb/Target/ExecutionContext.h" |
24 | #include "lldb/Target/Process.h" |
25 | #include "lldb/Target/RegisterContext.h" |
26 | #include "lldb/Target/Target.h" |
27 | #include "lldb/Utility/DataExtractor.h" |
28 | #include "lldb/Utility/RegisterValue.h" |
29 | #include "lldb/Utility/Scalar.h" |
30 | #include "lldb/Utility/Status.h" |
31 | #include "lldb/lldb-private-enumerations.h" |
32 | #include "lldb/lldb-types.h" |
33 | |
34 | #include "llvm/ADT/StringRef.h" |
35 | |
36 | #include <cassert> |
37 | #include <memory> |
38 | #include <optional> |
39 | |
40 | namespace lldb_private { |
41 | class ExecutionContextScope; |
42 | } |
43 | namespace lldb_private { |
44 | class StackFrame; |
45 | } |
46 | namespace lldb_private { |
47 | struct RegisterInfo; |
48 | } |
49 | using namespace lldb_private; |
50 | |
51 | lldb::ValueObjectSP |
52 | ValueObjectVariable::Create(ExecutionContextScope *exe_scope, |
53 | const lldb::VariableSP &var_sp) { |
54 | auto manager_sp = ValueObjectManager::Create(); |
55 | return (new ValueObjectVariable(exe_scope, *manager_sp, var_sp))->GetSP(); |
56 | } |
57 | |
58 | ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope, |
59 | ValueObjectManager &manager, |
60 | const lldb::VariableSP &var_sp) |
61 | : ValueObject(exe_scope, manager), m_variable_sp(var_sp) { |
62 | // Do not attempt to construct one of these objects with no variable! |
63 | assert(m_variable_sp.get() != nullptr); |
64 | m_name = var_sp->GetName(); |
65 | } |
66 | |
67 | ValueObjectVariable::~ValueObjectVariable() = default; |
68 | |
69 | CompilerType ValueObjectVariable::GetCompilerTypeImpl() { |
70 | Type *var_type = m_variable_sp->GetType(); |
71 | if (var_type) |
72 | return var_type->GetForwardCompilerType(); |
73 | return CompilerType(); |
74 | } |
75 | |
76 | ConstString ValueObjectVariable::GetTypeName() { |
77 | Type *var_type = m_variable_sp->GetType(); |
78 | if (var_type) |
79 | return var_type->GetName(); |
80 | return ConstString(); |
81 | } |
82 | |
83 | ConstString ValueObjectVariable::GetDisplayTypeName() { |
84 | Type *var_type = m_variable_sp->GetType(); |
85 | if (var_type) |
86 | return var_type->GetForwardCompilerType().GetDisplayTypeName(); |
87 | return ConstString(); |
88 | } |
89 | |
90 | ConstString ValueObjectVariable::GetQualifiedTypeName() { |
91 | Type *var_type = m_variable_sp->GetType(); |
92 | if (var_type) |
93 | return var_type->GetQualifiedName(); |
94 | return ConstString(); |
95 | } |
96 | |
97 | llvm::Expected<uint32_t> |
98 | ValueObjectVariable::CalculateNumChildren(uint32_t max) { |
99 | CompilerType type(GetCompilerType()); |
100 | |
101 | if (!type.IsValid()) |
102 | return llvm::make_error<llvm::StringError>(Args: "invalid type" , |
103 | Args: llvm::inconvertibleErrorCode()); |
104 | |
105 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
106 | const bool omit_empty_base_classes = true; |
107 | auto child_count = type.GetNumChildren(omit_empty_base_classes, exe_ctx: &exe_ctx); |
108 | if (!child_count) |
109 | return child_count; |
110 | return *child_count <= max ? *child_count : max; |
111 | } |
112 | |
113 | llvm::Expected<uint64_t> ValueObjectVariable::GetByteSize() { |
114 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
115 | |
116 | CompilerType type(GetCompilerType()); |
117 | return type.GetByteSize(exe_scope: exe_ctx.GetBestExecutionContextScope()); |
118 | } |
119 | |
120 | lldb::ValueType ValueObjectVariable::GetValueType() const { |
121 | if (m_variable_sp) |
122 | return m_variable_sp->GetScope(); |
123 | return lldb::eValueTypeInvalid; |
124 | } |
125 | |
126 | bool ValueObjectVariable::UpdateValue() { |
127 | SetValueIsValid(false); |
128 | m_error.Clear(); |
129 | |
130 | Variable *variable = m_variable_sp.get(); |
131 | DWARFExpressionList &expr_list = variable->LocationExpressionList(); |
132 | |
133 | if (variable->GetLocationIsConstantValueData()) { |
134 | // expr doesn't contain DWARF bytes, it contains the constant variable |
135 | // value bytes themselves... |
136 | if (expr_list.GetExpressionData(data&: m_data)) { |
137 | if (m_data.GetDataStart() && m_data.GetByteSize()) |
138 | m_value.SetBytes(bytes: m_data.GetDataStart(), len: m_data.GetByteSize()); |
139 | m_value.SetContext(context_type: Value::ContextType::Variable, p: variable); |
140 | } else |
141 | m_error = Status::FromErrorString(str: "empty constant data" ); |
142 | // constant bytes can't be edited - sorry |
143 | m_resolved_value.SetContext(context_type: Value::ContextType::Invalid, p: nullptr); |
144 | } else { |
145 | lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS; |
146 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
147 | |
148 | Target *target = exe_ctx.GetTargetPtr(); |
149 | if (target) { |
150 | m_data.SetByteOrder(target->GetArchitecture().GetByteOrder()); |
151 | m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); |
152 | } |
153 | |
154 | if (!expr_list.IsAlwaysValidSingleExpr()) { |
155 | SymbolContext sc; |
156 | variable->CalculateSymbolContext(sc: &sc); |
157 | if (sc.function) |
158 | loclist_base_load_addr = |
159 | sc.function->GetAddress().GetLoadAddress(target); |
160 | } |
161 | Value old_value(m_value); |
162 | llvm::Expected<Value> maybe_value = expr_list.Evaluate( |
163 | exe_ctx: &exe_ctx, reg_ctx: nullptr, func_load_addr: loclist_base_load_addr, initial_value_ptr: nullptr, object_address_ptr: nullptr); |
164 | |
165 | if (maybe_value) { |
166 | m_value = *maybe_value; |
167 | m_resolved_value = m_value; |
168 | m_value.SetContext(context_type: Value::ContextType::Variable, p: variable); |
169 | |
170 | CompilerType compiler_type = GetCompilerType(); |
171 | if (compiler_type.IsValid()) |
172 | m_value.SetCompilerType(compiler_type); |
173 | |
174 | Value::ValueType value_type = m_value.GetValueType(); |
175 | |
176 | // The size of the buffer within m_value can be less than the size |
177 | // prescribed by its type. E.g. this can happen when an expression only |
178 | // partially describes an object (say, because it contains DW_OP_piece). |
179 | // |
180 | // In this case, grow m_value to the expected size. An alternative way to |
181 | // handle this is to teach Value::GetValueAsData() and ValueObjectChild |
182 | // not to read past the end of a host buffer, but this gets impractically |
183 | // complicated as a Value's host buffer may be shared with a distant |
184 | // ancestor or sibling in the ValueObject hierarchy. |
185 | // |
186 | // FIXME: When we grow m_value, we should represent the added bits as |
187 | // undefined somehow instead of as 0's. |
188 | if (value_type == Value::ValueType::HostAddress && |
189 | compiler_type.IsValid()) { |
190 | if (size_t value_buf_size = m_value.GetBuffer().GetByteSize()) { |
191 | size_t value_size = m_value.GetValueByteSize(error_ptr: &m_error, exe_ctx: &exe_ctx); |
192 | if (m_error.Success() && value_buf_size < value_size) |
193 | m_value.ResizeData(len: value_size); |
194 | } |
195 | } |
196 | |
197 | Process *process = exe_ctx.GetProcessPtr(); |
198 | const bool process_is_alive = process && process->IsAlive(); |
199 | |
200 | switch (value_type) { |
201 | case Value::ValueType::Invalid: |
202 | m_error = Status::FromErrorString(str: "invalid value" ); |
203 | break; |
204 | case Value::ValueType::Scalar: |
205 | // The variable value is in the Scalar value inside the m_value. We can |
206 | // point our m_data right to it. |
207 | m_error = m_value.GetValueAsData(exe_ctx: &exe_ctx, data&: m_data, module: GetModule().get()); |
208 | break; |
209 | |
210 | case Value::ValueType::FileAddress: |
211 | case Value::ValueType::LoadAddress: |
212 | case Value::ValueType::HostAddress: |
213 | // The DWARF expression result was an address in the inferior process. |
214 | // If this variable is an aggregate type, we just need the address as |
215 | // the main value as all child variable objects will rely upon this |
216 | // location and add an offset and then read their own values as needed. |
217 | // If this variable is a simple type, we read all data for it into |
218 | // m_data. Make sure this type has a value before we try and read it |
219 | |
220 | // If we have a file address, convert it to a load address if we can. |
221 | if (value_type == Value::ValueType::FileAddress && process_is_alive) |
222 | m_value.ConvertToLoadAddress(module: GetModule().get(), target); |
223 | |
224 | if (!CanProvideValue()) { |
225 | // this value object represents an aggregate type whose children have |
226 | // values, but this object does not. So we say we are changed if our |
227 | // location has changed. |
228 | SetValueDidChange(value_type != old_value.GetValueType() || |
229 | m_value.GetScalar() != old_value.GetScalar()); |
230 | } else { |
231 | // Copy the Value and set the context to use our Variable so it can |
232 | // extract read its value into m_data appropriately |
233 | Value value(m_value); |
234 | value.SetContext(context_type: Value::ContextType::Variable, p: variable); |
235 | m_error = value.GetValueAsData(exe_ctx: &exe_ctx, data&: m_data, module: GetModule().get()); |
236 | |
237 | SetValueDidChange(value_type != old_value.GetValueType() || |
238 | m_value.GetScalar() != old_value.GetScalar()); |
239 | } |
240 | break; |
241 | } |
242 | |
243 | SetValueIsValid(m_error.Success()); |
244 | } else { |
245 | m_error = Status::FromError(error: maybe_value.takeError()); |
246 | // could not find location, won't allow editing |
247 | m_resolved_value.SetContext(context_type: Value::ContextType::Invalid, p: nullptr); |
248 | } |
249 | } |
250 | |
251 | return m_error.Success(); |
252 | } |
253 | |
254 | void ValueObjectVariable::DoUpdateChildrenAddressType(ValueObject &valobj) { |
255 | Value::ValueType value_type = valobj.GetValue().GetValueType(); |
256 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
257 | Process *process = exe_ctx.GetProcessPtr(); |
258 | const bool process_is_alive = process && process->IsAlive(); |
259 | const uint32_t type_info = valobj.GetCompilerType().GetTypeInfo(); |
260 | const bool is_pointer_or_ref = |
261 | (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0; |
262 | |
263 | switch (value_type) { |
264 | case Value::ValueType::Invalid: |
265 | break; |
266 | case Value::ValueType::FileAddress: |
267 | // If this type is a pointer, then its children will be considered load |
268 | // addresses if the pointer or reference is dereferenced, but only if |
269 | // the process is alive. |
270 | // |
271 | // There could be global variables like in the following code: |
272 | // struct LinkedListNode { Foo* foo; LinkedListNode* next; }; |
273 | // Foo g_foo1; |
274 | // Foo g_foo2; |
275 | // LinkedListNode g_second_node = { &g_foo2, NULL }; |
276 | // LinkedListNode g_first_node = { &g_foo1, &g_second_node }; |
277 | // |
278 | // When we aren't running, we should be able to look at these variables |
279 | // using the "target variable" command. Children of the "g_first_node" |
280 | // always will be of the same address type as the parent. But children |
281 | // of the "next" member of LinkedListNode will become load addresses if |
282 | // we have a live process, or remain a file address if it was a file |
283 | // address. |
284 | if (process_is_alive && is_pointer_or_ref) |
285 | valobj.SetAddressTypeOfChildren(eAddressTypeLoad); |
286 | else |
287 | valobj.SetAddressTypeOfChildren(eAddressTypeFile); |
288 | break; |
289 | case Value::ValueType::HostAddress: |
290 | // Same as above for load addresses, except children of pointer or refs |
291 | // are always load addresses. Host addresses are used to store freeze |
292 | // dried variables. If this type is a struct, the entire struct |
293 | // contents will be copied into the heap of the |
294 | // LLDB process, but we do not currently follow any pointers. |
295 | if (is_pointer_or_ref) |
296 | valobj.SetAddressTypeOfChildren(eAddressTypeLoad); |
297 | else |
298 | valobj.SetAddressTypeOfChildren(eAddressTypeHost); |
299 | break; |
300 | case Value::ValueType::LoadAddress: |
301 | case Value::ValueType::Scalar: |
302 | valobj.SetAddressTypeOfChildren(eAddressTypeLoad); |
303 | break; |
304 | } |
305 | } |
306 | |
307 | bool ValueObjectVariable::IsInScope() { |
308 | const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef(); |
309 | if (exe_ctx_ref.HasFrameRef()) { |
310 | ExecutionContext exe_ctx(exe_ctx_ref); |
311 | StackFrame *frame = exe_ctx.GetFramePtr(); |
312 | if (frame) { |
313 | return m_variable_sp->IsInScope(frame); |
314 | } else { |
315 | // This ValueObject had a frame at one time, but now we can't locate it, |
316 | // so return false since we probably aren't in scope. |
317 | return false; |
318 | } |
319 | } |
320 | // We have a variable that wasn't tied to a frame, which means it is a global |
321 | // and is always in scope. |
322 | return true; |
323 | } |
324 | |
325 | lldb::ModuleSP ValueObjectVariable::GetModule() { |
326 | if (m_variable_sp) { |
327 | SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope(); |
328 | if (sc_scope) { |
329 | return sc_scope->CalculateSymbolContextModule(); |
330 | } |
331 | } |
332 | return lldb::ModuleSP(); |
333 | } |
334 | |
335 | SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() { |
336 | if (m_variable_sp) |
337 | return m_variable_sp->GetSymbolContextScope(); |
338 | return nullptr; |
339 | } |
340 | |
341 | bool ValueObjectVariable::GetDeclaration(Declaration &decl) { |
342 | if (m_variable_sp) { |
343 | decl = m_variable_sp->GetDeclaration(); |
344 | return true; |
345 | } |
346 | return false; |
347 | } |
348 | |
349 | const char *ValueObjectVariable::GetLocationAsCString() { |
350 | if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) |
351 | return GetLocationAsCStringImpl(value: m_resolved_value, data: m_data); |
352 | else |
353 | return ValueObject::GetLocationAsCString(); |
354 | } |
355 | |
356 | bool ValueObjectVariable::SetValueFromCString(const char *value_str, |
357 | Status &error) { |
358 | if (!UpdateValueIfNeeded()) { |
359 | error = Status::FromErrorString(str: "unable to update value before writing" ); |
360 | return false; |
361 | } |
362 | |
363 | if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) { |
364 | RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); |
365 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
366 | RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); |
367 | RegisterValue reg_value; |
368 | if (!reg_info || !reg_ctx) { |
369 | error = Status::FromErrorString(str: "unable to retrieve register info" ); |
370 | return false; |
371 | } |
372 | error = reg_value.SetValueFromString(reg_info, value_str: llvm::StringRef(value_str)); |
373 | if (error.Fail()) |
374 | return false; |
375 | if (reg_ctx->WriteRegister(reg_info, reg_value)) { |
376 | SetNeedsUpdate(); |
377 | return true; |
378 | } else { |
379 | error = Status::FromErrorString(str: "unable to write back to register" ); |
380 | return false; |
381 | } |
382 | } else |
383 | return ValueObject::SetValueFromCString(value_str, error); |
384 | } |
385 | |
386 | bool ValueObjectVariable::(DataExtractor &data, Status &error) { |
387 | if (!UpdateValueIfNeeded()) { |
388 | error = Status::FromErrorString(str: "unable to update value before writing" ); |
389 | return false; |
390 | } |
391 | |
392 | if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) { |
393 | RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); |
394 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
395 | RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); |
396 | RegisterValue reg_value; |
397 | if (!reg_info || !reg_ctx) { |
398 | error = Status::FromErrorString(str: "unable to retrieve register info" ); |
399 | return false; |
400 | } |
401 | error = reg_value.SetValueFromData(reg_info: *reg_info, data, offset: 0, partial_data_ok: true); |
402 | if (error.Fail()) |
403 | return false; |
404 | if (reg_ctx->WriteRegister(reg_info, reg_value)) { |
405 | SetNeedsUpdate(); |
406 | return true; |
407 | } else { |
408 | error = Status::FromErrorString(str: "unable to write back to register" ); |
409 | return false; |
410 | } |
411 | } else |
412 | return ValueObject::SetData(data, error); |
413 | } |
414 | |