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