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