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. 169 // We can 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 177 // process. If this variable is an aggregate type, we just need 178 // the address as the main value as all child variable objects 179 // will rely upon this location and add an offset and then read 180 // their own values as needed. If this variable is a simple 181 // type, we read all data for it into m_data. 182 // Make sure this type has a value before we try and read it 183 184 // If we have a file address, convert it to a load address if we can. 185 if (value_type == Value::eValueTypeFileAddress && 186 exe_ctx.GetProcessPtr()) { 187 lldb::addr_t load_addr = m_address.GetLoadAddress(target); 188 if (load_addr != LLDB_INVALID_ADDRESS) { 189 m_value.SetValueType(Value::eValueTypeLoadAddress); 190 m_value.GetScalar() = load_addr; 191 } 192 } 193 194 if (!CanProvideValue()) { 195 // this value object represents an aggregate type whose 196 // children have values, but this object does not. So we 197 // say we are changed if our location has changed. 198 SetValueDidChange(value_type != old_value.GetValueType() || 199 m_value.GetScalar() != old_value.GetScalar()); 200 } else { 201 // Copy the Value and set the context to use our Variable 202 // so it can extract read its value into m_data appropriately 203 Value value(m_value); 204 if (m_type_sp) 205 value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get()); 206 else { 207 // value.SetContext(Value::eContextTypeClangType, 208 // m_compiler_type.GetOpaqueQualType()); 209 value.SetCompilerType(m_compiler_type); 210 } 211 212 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get()); 213 } 214 break; 215 } 216 217 SetValueIsValid(m_error.Success()); 218 } 219 return m_error.Success(); 220 } 221 222 bool ValueObjectMemory::IsInScope() { 223 // FIXME: Maybe try to read the memory address, and if that works, then 224 // we are in scope? 225 return true; 226 } 227 228 lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); } 229