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