]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/ValueObjectMemory.cpp
Update to ELF Tool Chain r3668
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / ValueObjectMemory.cpp
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/Scalar.h" // for Scalar, operator!=
12 #include "lldb/Core/Value.h"
13 #include "lldb/Core/ValueObject.h"
14 #include "lldb/Symbol/Type.h"
15 #include "lldb/Target/ExecutionContext.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/DataExtractor.h" // for DataExtractor
18 #include "lldb/Utility/Status.h"        // for Status
19 #include "lldb/lldb-types.h"            // for addr_t
20 #include "llvm/Support/ErrorHandling.h" // for llvm_unreachable
21
22 #include <assert.h> // for assert
23 #include <memory>   // for shared_ptr
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   const bool omit_empty_base_classes = true;
132   auto child_count = m_compiler_type.GetNumChildren(omit_empty_base_classes);
133   return child_count <= max ? child_count : max;
134 }
135
136 uint64_t ValueObjectMemory::GetByteSize() {
137   if (m_type_sp)
138     return m_type_sp->GetByteSize();
139   return m_compiler_type.GetByteSize(nullptr);
140 }
141
142 lldb::ValueType ValueObjectMemory::GetValueType() const {
143   // RETHINK: Should this be inherited from somewhere?
144   return lldb::eValueTypeVariableGlobal;
145 }
146
147 bool ValueObjectMemory::UpdateValue() {
148   SetValueIsValid(false);
149   m_error.Clear();
150
151   ExecutionContext exe_ctx(GetExecutionContextRef());
152
153   Target *target = exe_ctx.GetTargetPtr();
154   if (target) {
155     m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
156     m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
157   }
158
159   Value old_value(m_value);
160   if (m_address.IsValid()) {
161     Value::ValueType value_type = m_value.GetValueType();
162
163     switch (value_type) {
164     default:
165       llvm_unreachable("Unhandled expression result value kind...");
166
167     case Value::eValueTypeScalar:
168       // The variable value is in the Scalar value inside the m_value. We can
169       // point our m_data right to it.
170       m_error = m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
171       break;
172
173     case Value::eValueTypeFileAddress:
174     case Value::eValueTypeLoadAddress:
175     case Value::eValueTypeHostAddress:
176       // The DWARF expression result was an address in the inferior process. If
177       // this variable is an aggregate type, we just need the address as the
178       // main value as all child variable objects will rely upon this location
179       // and add an offset and then read their own values as needed. If this
180       // variable is a simple type, we read all data for it into m_data. Make
181       // sure this type has a value before we try and read it
182
183       // If we have a file address, convert it to a load address if we can.
184       if (value_type == Value::eValueTypeFileAddress &&
185           exe_ctx.GetProcessPtr()) {
186         lldb::addr_t load_addr = m_address.GetLoadAddress(target);
187         if (load_addr != LLDB_INVALID_ADDRESS) {
188           m_value.SetValueType(Value::eValueTypeLoadAddress);
189           m_value.GetScalar() = load_addr;
190         }
191       }
192
193       if (!CanProvideValue()) {
194         // this value object represents an aggregate type whose children have
195         // values, but this object does not. So we say we are changed if our
196         // location has changed.
197         SetValueDidChange(value_type != old_value.GetValueType() ||
198                           m_value.GetScalar() != old_value.GetScalar());
199       } else {
200         // Copy the Value and set the context to use our Variable so it can
201         // extract read its value into m_data appropriately
202         Value value(m_value);
203         if (m_type_sp)
204           value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get());
205         else {
206           // value.SetContext(Value::eContextTypeClangType,
207           // m_compiler_type.GetOpaqueQualType());
208           value.SetCompilerType(m_compiler_type);
209         }
210
211         m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
212       }
213       break;
214     }
215
216     SetValueIsValid(m_error.Success());
217   }
218   return m_error.Success();
219 }
220
221 bool ValueObjectMemory::IsInScope() {
222   // FIXME: Maybe try to read the memory address, and if that works, then
223   // we are in scope?
224   return true;
225 }
226
227 lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }