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" // for Address 13 #include "lldb/Core/AddressRange.h" // for AddressRange 14 #include "lldb/Core/ArchSpec.h" // for ArchSpec 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/RegisterValue.h" 17 #include "lldb/Core/Scalar.h" // for Scalar, operator!= 18 #include "lldb/Core/Value.h" 19 #include "lldb/Expression/DWARFExpression.h" // for DWARFExpression 20 #include "lldb/Symbol/Declaration.h" // for Declaration 21 #include "lldb/Symbol/Function.h" 22 #include "lldb/Symbol/ObjectFile.h" 23 #include "lldb/Symbol/SymbolContext.h" 24 #include "lldb/Symbol/SymbolContextScope.h" 25 #include "lldb/Symbol/Type.h" 26 #include "lldb/Symbol/Variable.h" 27 #include "lldb/Target/ExecutionContext.h" 28 #include "lldb/Target/Process.h" 29 #include "lldb/Target/RegisterContext.h" 30 #include "lldb/Target/Target.h" 31 #include "lldb/Utility/DataExtractor.h" // for DataExtractor 32 #include "lldb/Utility/Status.h" // for Status 33 #include "lldb/lldb-private-enumerations.h" // for AddressType::eAddressTy... 34 #include "lldb/lldb-types.h" // for addr_t 35 36 #include "llvm/ADT/StringRef.h" // for StringRef 37 38 #include <assert.h> // for assert 39 #include <memory> // for shared_ptr 40 41 namespace lldb_private { 42 class ExecutionContextScope; 43 } 44 namespace lldb_private { 45 class StackFrame; 46 } 47 namespace lldb_private { 48 struct RegisterInfo; 49 } 50 using namespace lldb_private; 51 52 lldb::ValueObjectSP 53 ValueObjectVariable::Create(ExecutionContextScope *exe_scope, 54 const lldb::VariableSP &var_sp) { 55 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP(); 56 } 57 58 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope, 59 const lldb::VariableSP &var_sp) 60 : ValueObject(exe_scope), m_variable_sp(var_sp) { 61 // Do not attempt to construct one of these objects with no variable! 62 assert(m_variable_sp.get() != NULL); 63 m_name = var_sp->GetName(); 64 } 65 66 ValueObjectVariable::~ValueObjectVariable() {} 67 68 CompilerType ValueObjectVariable::GetCompilerTypeImpl() { 69 Type *var_type = m_variable_sp->GetType(); 70 if (var_type) 71 return var_type->GetForwardCompilerType(); 72 return CompilerType(); 73 } 74 75 ConstString ValueObjectVariable::GetTypeName() { 76 Type *var_type = m_variable_sp->GetType(); 77 if (var_type) 78 return var_type->GetName(); 79 return ConstString(); 80 } 81 82 ConstString ValueObjectVariable::GetDisplayTypeName() { 83 Type *var_type = m_variable_sp->GetType(); 84 if (var_type) 85 return var_type->GetForwardCompilerType().GetDisplayTypeName(); 86 return ConstString(); 87 } 88 89 ConstString ValueObjectVariable::GetQualifiedTypeName() { 90 Type *var_type = m_variable_sp->GetType(); 91 if (var_type) 92 return var_type->GetQualifiedName(); 93 return ConstString(); 94 } 95 96 size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) { 97 CompilerType type(GetCompilerType()); 98 99 if (!type.IsValid()) 100 return 0; 101 102 const bool omit_empty_base_classes = true; 103 auto child_count = type.GetNumChildren(omit_empty_base_classes); 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()); 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, nullptr, nullptr, 160 loclist_base_load_addr, nullptr, nullptr, m_value, 161 &m_error)) { 162 m_resolved_value = m_value; 163 m_value.SetContext(Value::eContextTypeVariable, variable); 164 165 CompilerType compiler_type = GetCompilerType(); 166 if (compiler_type.IsValid()) 167 m_value.SetCompilerType(compiler_type); 168 169 Value::ValueType value_type = m_value.GetValueType(); 170 171 Process *process = exe_ctx.GetProcessPtr(); 172 const bool process_is_alive = process && process->IsAlive(); 173 const uint32_t type_info = compiler_type.GetTypeInfo(); 174 const bool is_pointer_or_ref = 175 (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0; 176 177 switch (value_type) { 178 case Value::eValueTypeFileAddress: 179 // If this type is a pointer, then its children will be considered load 180 // addresses 181 // if the pointer or reference is dereferenced, but only if the process 182 // is alive. 183 // 184 // There could be global variables like in the following code: 185 // struct LinkedListNode { Foo* foo; LinkedListNode* next; }; 186 // Foo g_foo1; 187 // Foo g_foo2; 188 // LinkedListNode g_second_node = { &g_foo2, NULL }; 189 // LinkedListNode g_first_node = { &g_foo1, &g_second_node }; 190 // 191 // When we aren't running, we should be able to look at these variables 192 // using 193 // the "target variable" command. Children of the "g_first_node" always 194 // will 195 // be of the same address type as the parent. But children of the "next" 196 // member of 197 // LinkedListNode will become load addresses if we have a live process, 198 // or remain 199 // what a file address if it what a file address. 200 if (process_is_alive && is_pointer_or_ref) 201 SetAddressTypeOfChildren(eAddressTypeLoad); 202 else 203 SetAddressTypeOfChildren(eAddressTypeFile); 204 break; 205 case Value::eValueTypeHostAddress: 206 // Same as above for load addresses, except children of pointer or refs 207 // are always 208 // load addresses. Host addresses are used to store freeze dried 209 // variables. If this 210 // type is a struct, the entire struct contents will be copied into the 211 // heap of the 212 // LLDB process, but we do not currrently follow any pointers. 213 if (is_pointer_or_ref) 214 SetAddressTypeOfChildren(eAddressTypeLoad); 215 else 216 SetAddressTypeOfChildren(eAddressTypeHost); 217 break; 218 case Value::eValueTypeLoadAddress: 219 case Value::eValueTypeScalar: 220 case Value::eValueTypeVector: 221 SetAddressTypeOfChildren(eAddressTypeLoad); 222 break; 223 } 224 225 switch (value_type) { 226 case Value::eValueTypeVector: 227 // fall through 228 case Value::eValueTypeScalar: 229 // The variable value is in the Scalar value inside the m_value. 230 // We can point our m_data right to it. 231 m_error = 232 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get()); 233 break; 234 235 case Value::eValueTypeFileAddress: 236 case Value::eValueTypeLoadAddress: 237 case Value::eValueTypeHostAddress: 238 // The DWARF expression result was an address in the inferior 239 // process. If this variable is an aggregate type, we just need 240 // the address as the main value as all child variable objects 241 // will rely upon this location and add an offset and then read 242 // their own values as needed. If this variable is a simple 243 // type, we read all data for it into m_data. 244 // Make sure this type has a value before we try and read it 245 246 // If we have a file address, convert it to a load address if we can. 247 if (value_type == Value::eValueTypeFileAddress && process_is_alive) { 248 lldb::addr_t file_addr = 249 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 250 if (file_addr != LLDB_INVALID_ADDRESS) { 251 SymbolContext var_sc; 252 variable->CalculateSymbolContext(&var_sc); 253 if (var_sc.module_sp) { 254 ObjectFile *objfile = var_sc.module_sp->GetObjectFile(); 255 if (objfile) { 256 Address so_addr(file_addr, objfile->GetSectionList()); 257 lldb::addr_t load_addr = so_addr.GetLoadAddress(target); 258 if (load_addr != LLDB_INVALID_ADDRESS) { 259 m_value.SetValueType(Value::eValueTypeLoadAddress); 260 m_value.GetScalar() = load_addr; 261 } 262 } 263 } 264 } 265 } 266 267 if (!CanProvideValue()) { 268 // this value object represents an aggregate type whose 269 // children have values, but this object does not. So we 270 // say we are changed if our location has changed. 271 SetValueDidChange(value_type != old_value.GetValueType() || 272 m_value.GetScalar() != old_value.GetScalar()); 273 } else { 274 // Copy the Value and set the context to use our Variable 275 // so it can extract read its value into m_data appropriately 276 Value value(m_value); 277 value.SetContext(Value::eContextTypeVariable, variable); 278 m_error = 279 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get()); 280 281 SetValueDidChange(value_type != old_value.GetValueType() || 282 m_value.GetScalar() != old_value.GetScalar()); 283 } 284 break; 285 } 286 287 SetValueIsValid(m_error.Success()); 288 } else { 289 // could not find location, won't allow editing 290 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL); 291 } 292 } 293 return m_error.Success(); 294 } 295 296 bool ValueObjectVariable::IsInScope() { 297 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef(); 298 if (exe_ctx_ref.HasFrameRef()) { 299 ExecutionContext exe_ctx(exe_ctx_ref); 300 StackFrame *frame = exe_ctx.GetFramePtr(); 301 if (frame) { 302 return m_variable_sp->IsInScope(frame); 303 } else { 304 // This ValueObject had a frame at one time, but now we 305 // can't locate it, so return false since we probably aren't 306 // in scope. 307 return false; 308 } 309 } 310 // We have a variable that wasn't tied to a frame, which 311 // means it is a global and is always in scope. 312 return true; 313 } 314 315 lldb::ModuleSP ValueObjectVariable::GetModule() { 316 if (m_variable_sp) { 317 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope(); 318 if (sc_scope) { 319 return sc_scope->CalculateSymbolContextModule(); 320 } 321 } 322 return lldb::ModuleSP(); 323 } 324 325 SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() { 326 if (m_variable_sp) 327 return m_variable_sp->GetSymbolContextScope(); 328 return NULL; 329 } 330 331 bool ValueObjectVariable::GetDeclaration(Declaration &decl) { 332 if (m_variable_sp) { 333 decl = m_variable_sp->GetDeclaration(); 334 return true; 335 } 336 return false; 337 } 338 339 const char *ValueObjectVariable::GetLocationAsCString() { 340 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) 341 return GetLocationAsCStringImpl(m_resolved_value, m_data); 342 else 343 return ValueObject::GetLocationAsCString(); 344 } 345 346 bool ValueObjectVariable::SetValueFromCString(const char *value_str, 347 Status &error) { 348 if (!UpdateValueIfNeeded()) { 349 error.SetErrorString("unable to update value before writing"); 350 return false; 351 } 352 353 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) { 354 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); 355 ExecutionContext exe_ctx(GetExecutionContextRef()); 356 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); 357 RegisterValue reg_value; 358 if (!reg_info || !reg_ctx) { 359 error.SetErrorString("unable to retrieve register info"); 360 return false; 361 } 362 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str)); 363 if (error.Fail()) 364 return false; 365 if (reg_ctx->WriteRegister(reg_info, reg_value)) { 366 SetNeedsUpdate(); 367 return true; 368 } else { 369 error.SetErrorString("unable to write back to register"); 370 return false; 371 } 372 } else 373 return ValueObject::SetValueFromCString(value_str, error); 374 } 375 376 bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) { 377 if (!UpdateValueIfNeeded()) { 378 error.SetErrorString("unable to update value before writing"); 379 return false; 380 } 381 382 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) { 383 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); 384 ExecutionContext exe_ctx(GetExecutionContextRef()); 385 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); 386 RegisterValue reg_value; 387 if (!reg_info || !reg_ctx) { 388 error.SetErrorString("unable to retrieve register info"); 389 return false; 390 } 391 error = reg_value.SetValueFromData(reg_info, data, 0, true); 392 if (error.Fail()) 393 return false; 394 if (reg_ctx->WriteRegister(reg_info, reg_value)) { 395 SetNeedsUpdate(); 396 return true; 397 } else { 398 error.SetErrorString("unable to write back to register"); 399 return false; 400 } 401 } else 402 return ValueObject::SetData(data, error); 403 } 404