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