]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/ValueObjectVariable.cpp
MFV r348548: 9617 too-frequent TXG sync causes excessive write inflation
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / ValueObjectVariable.cpp
1 //===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/ValueObjectVariable.h"
11
12 #include "lldb/Core/Address.h"
13 #include "lldb/Core/AddressRange.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Value.h"
16 #include "lldb/Expression/DWARFExpression.h"
17 #include "lldb/Symbol/Declaration.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/SymbolContextScope.h"
22 #include "lldb/Symbol/Type.h"
23 #include "lldb/Symbol/Variable.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/RegisterContext.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/DataExtractor.h"
29 #include "lldb/Utility/RegisterValue.h"
30 #include "lldb/Utility/Scalar.h"
31 #include "lldb/Utility/Status.h"
32 #include "lldb/lldb-private-enumerations.h"
33 #include "lldb/lldb-types.h"
34
35 #include "llvm/ADT/StringRef.h"
36
37 #include <assert.h>
38 #include <memory>
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   return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
55 }
56
57 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
58                                          const lldb::VariableSP &var_sp)
59     : ValueObject(exe_scope), m_variable_sp(var_sp) {
60   // Do not attempt to construct one of these objects with no variable!
61   assert(m_variable_sp.get() != NULL);
62   m_name = var_sp->GetName();
63 }
64
65 ValueObjectVariable::~ValueObjectVariable() {}
66
67 CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
68   Type *var_type = m_variable_sp->GetType();
69   if (var_type)
70     return var_type->GetForwardCompilerType();
71   return CompilerType();
72 }
73
74 ConstString ValueObjectVariable::GetTypeName() {
75   Type *var_type = m_variable_sp->GetType();
76   if (var_type)
77     return var_type->GetName();
78   return ConstString();
79 }
80
81 ConstString ValueObjectVariable::GetDisplayTypeName() {
82   Type *var_type = m_variable_sp->GetType();
83   if (var_type)
84     return var_type->GetForwardCompilerType().GetDisplayTypeName();
85   return ConstString();
86 }
87
88 ConstString ValueObjectVariable::GetQualifiedTypeName() {
89   Type *var_type = m_variable_sp->GetType();
90   if (var_type)
91     return var_type->GetQualifiedName();
92   return ConstString();
93 }
94
95 size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
96   CompilerType type(GetCompilerType());
97
98   if (!type.IsValid())
99     return 0;
100
101   ExecutionContext exe_ctx(GetExecutionContextRef());
102   const bool omit_empty_base_classes = true;
103   auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
104   return child_count <= max ? child_count : max;
105 }
106
107 uint64_t ValueObjectVariable::GetByteSize() {
108   ExecutionContext exe_ctx(GetExecutionContextRef());
109
110   CompilerType type(GetCompilerType());
111
112   if (!type.IsValid())
113     return 0;
114
115   return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()).getValueOr(0);
116 }
117
118 lldb::ValueType ValueObjectVariable::GetValueType() const {
119   if (m_variable_sp)
120     return m_variable_sp->GetScope();
121   return lldb::eValueTypeInvalid;
122 }
123
124 bool ValueObjectVariable::UpdateValue() {
125   SetValueIsValid(false);
126   m_error.Clear();
127
128   Variable *variable = m_variable_sp.get();
129   DWARFExpression &expr = variable->LocationExpression();
130
131   if (variable->GetLocationIsConstantValueData()) {
132     // expr doesn't contain DWARF bytes, it contains the constant variable
133     // value bytes themselves...
134     if (expr.GetExpressionData(m_data))
135       m_value.SetContext(Value::eContextTypeVariable, variable);
136     else
137       m_error.SetErrorString("empty constant data");
138     // constant bytes can't be edited - sorry
139     m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
140   } else {
141     lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
142     ExecutionContext exe_ctx(GetExecutionContextRef());
143
144     Target *target = exe_ctx.GetTargetPtr();
145     if (target) {
146       m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
147       m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
148     }
149
150     if (expr.IsLocationList()) {
151       SymbolContext sc;
152       variable->CalculateSymbolContext(&sc);
153       if (sc.function)
154         loclist_base_load_addr =
155             sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
156                 target);
157     }
158     Value old_value(m_value);
159     if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr,
160                       nullptr, m_value, &m_error)) {
161       m_resolved_value = m_value;
162       m_value.SetContext(Value::eContextTypeVariable, variable);
163
164       CompilerType compiler_type = GetCompilerType();
165       if (compiler_type.IsValid())
166         m_value.SetCompilerType(compiler_type);
167
168       Value::ValueType value_type = m_value.GetValueType();
169
170       Process *process = exe_ctx.GetProcessPtr();
171       const bool process_is_alive = process && process->IsAlive();
172       const uint32_t type_info = compiler_type.GetTypeInfo();
173       const bool is_pointer_or_ref =
174           (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
175
176       switch (value_type) {
177       case Value::eValueTypeFileAddress:
178         // If this type is a pointer, then its children will be considered load
179         // addresses if the pointer or reference is dereferenced, but only if
180         // the process is alive.
181         //
182         // There could be global variables like in the following code:
183         // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
184         // Foo g_foo1;
185         // Foo g_foo2;
186         // LinkedListNode g_second_node = { &g_foo2, NULL };
187         // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
188         //
189         // When we aren't running, we should be able to look at these variables
190         // using the "target variable" command. Children of the "g_first_node"
191         // always will be of the same address type as the parent. But children
192         // of the "next" member of LinkedListNode will become load addresses if
193         // we have a live process, or remain what a file address if it what a
194         // file address.
195         if (process_is_alive && is_pointer_or_ref)
196           SetAddressTypeOfChildren(eAddressTypeLoad);
197         else
198           SetAddressTypeOfChildren(eAddressTypeFile);
199         break;
200       case Value::eValueTypeHostAddress:
201         // Same as above for load addresses, except children of pointer or refs
202         // are always load addresses. Host addresses are used to store freeze
203         // dried variables. If this type is a struct, the entire struct
204         // contents will be copied into the heap of the
205         // LLDB process, but we do not currently follow any pointers.
206         if (is_pointer_or_ref)
207           SetAddressTypeOfChildren(eAddressTypeLoad);
208         else
209           SetAddressTypeOfChildren(eAddressTypeHost);
210         break;
211       case Value::eValueTypeLoadAddress:
212       case Value::eValueTypeScalar:
213       case Value::eValueTypeVector:
214         SetAddressTypeOfChildren(eAddressTypeLoad);
215         break;
216       }
217
218       switch (value_type) {
219       case Value::eValueTypeVector:
220       // fall through
221       case Value::eValueTypeScalar:
222         // The variable value is in the Scalar value inside the m_value. We can
223         // point our m_data right to it.
224         m_error =
225             m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
226         break;
227
228       case Value::eValueTypeFileAddress:
229       case Value::eValueTypeLoadAddress:
230       case Value::eValueTypeHostAddress:
231         // The DWARF expression result was an address in the inferior process.
232         // If this variable is an aggregate type, we just need the address as
233         // the main value as all child variable objects will rely upon this
234         // location and add an offset and then read their own values as needed.
235         // If this variable is a simple type, we read all data for it into
236         // m_data. Make sure this type has a value before we try and read it
237
238         // If we have a file address, convert it to a load address if we can.
239         if (value_type == Value::eValueTypeFileAddress && process_is_alive)
240           m_value.ConvertToLoadAddress(GetModule().get(), target);
241
242         if (!CanProvideValue()) {
243           // this value object represents an aggregate type whose children have
244           // values, but this object does not. So we say we are changed if our
245           // location has changed.
246           SetValueDidChange(value_type != old_value.GetValueType() ||
247                             m_value.GetScalar() != old_value.GetScalar());
248         } else {
249           // Copy the Value and set the context to use our Variable so it can
250           // extract read its value into m_data appropriately
251           Value value(m_value);
252           value.SetContext(Value::eContextTypeVariable, variable);
253           m_error =
254               value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
255
256           SetValueDidChange(value_type != old_value.GetValueType() ||
257                             m_value.GetScalar() != old_value.GetScalar());
258         }
259         break;
260       }
261
262       SetValueIsValid(m_error.Success());
263     } else {
264       // could not find location, won't allow editing
265       m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
266     }
267   }
268   return m_error.Success();
269 }
270
271 bool ValueObjectVariable::IsInScope() {
272   const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
273   if (exe_ctx_ref.HasFrameRef()) {
274     ExecutionContext exe_ctx(exe_ctx_ref);
275     StackFrame *frame = exe_ctx.GetFramePtr();
276     if (frame) {
277       return m_variable_sp->IsInScope(frame);
278     } else {
279       // This ValueObject had a frame at one time, but now we can't locate it,
280       // so return false since we probably aren't in scope.
281       return false;
282     }
283   }
284   // We have a variable that wasn't tied to a frame, which means it is a global
285   // and is always in scope.
286   return true;
287 }
288
289 lldb::ModuleSP ValueObjectVariable::GetModule() {
290   if (m_variable_sp) {
291     SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
292     if (sc_scope) {
293       return sc_scope->CalculateSymbolContextModule();
294     }
295   }
296   return lldb::ModuleSP();
297 }
298
299 SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
300   if (m_variable_sp)
301     return m_variable_sp->GetSymbolContextScope();
302   return NULL;
303 }
304
305 bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
306   if (m_variable_sp) {
307     decl = m_variable_sp->GetDeclaration();
308     return true;
309   }
310   return false;
311 }
312
313 const char *ValueObjectVariable::GetLocationAsCString() {
314   if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
315     return GetLocationAsCStringImpl(m_resolved_value, m_data);
316   else
317     return ValueObject::GetLocationAsCString();
318 }
319
320 bool ValueObjectVariable::SetValueFromCString(const char *value_str,
321                                               Status &error) {
322   if (!UpdateValueIfNeeded()) {
323     error.SetErrorString("unable to update value before writing");
324     return false;
325   }
326
327   if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
328     RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
329     ExecutionContext exe_ctx(GetExecutionContextRef());
330     RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
331     RegisterValue reg_value;
332     if (!reg_info || !reg_ctx) {
333       error.SetErrorString("unable to retrieve register info");
334       return false;
335     }
336     error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
337     if (error.Fail())
338       return false;
339     if (reg_ctx->WriteRegister(reg_info, reg_value)) {
340       SetNeedsUpdate();
341       return true;
342     } else {
343       error.SetErrorString("unable to write back to register");
344       return false;
345     }
346   } else
347     return ValueObject::SetValueFromCString(value_str, error);
348 }
349
350 bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
351   if (!UpdateValueIfNeeded()) {
352     error.SetErrorString("unable to update value before writing");
353     return false;
354   }
355
356   if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
357     RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
358     ExecutionContext exe_ctx(GetExecutionContextRef());
359     RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
360     RegisterValue reg_value;
361     if (!reg_info || !reg_ctx) {
362       error.SetErrorString("unable to retrieve register info");
363       return false;
364     }
365     error = reg_value.SetValueFromData(reg_info, data, 0, true);
366     if (error.Fail())
367       return false;
368     if (reg_ctx->WriteRegister(reg_info, reg_value)) {
369       SetNeedsUpdate();
370       return true;
371     } else {
372       error.SetErrorString("unable to write back to register");
373       return false;
374     }
375   } else
376     return ValueObject::SetData(data, error);
377 }