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