ValueObjectVariable.cpp revision 0468bb2077ab25a289514dc474d6b5a7c537202f
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 36using namespace lldb_private; 37 38lldb::ValueObjectSP 39ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) 40{ 41 return (new ValueObjectVariable (exe_scope, var_sp))->GetSP(); 42} 43 44ValueObjectVariable::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 53ValueObjectVariable::~ValueObjectVariable() 54{ 55} 56 57lldb::clang_type_t 58ValueObjectVariable::GetClangTypeImpl () 59{ 60 Type *var_type = m_variable_sp->GetType(); 61 if (var_type) 62 return var_type->GetClangForwardType(); 63 return NULL; 64} 65 66ConstString 67ValueObjectVariable::GetTypeName() 68{ 69 Type * var_type = m_variable_sp->GetType(); 70 if (var_type) 71 return var_type->GetName(); 72 return ConstString(); 73} 74 75ConstString 76ValueObjectVariable::GetQualifiedTypeName() 77{ 78 Type * var_type = m_variable_sp->GetType(); 79 if (var_type) 80 return var_type->GetQualifiedName(); 81 return ConstString(); 82} 83 84size_t 85ValueObjectVariable::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 97clang::ASTContext * 98ValueObjectVariable::GetClangASTImpl () 99{ 100 Type *var_type = m_variable_sp->GetType(); 101 if (var_type) 102 return var_type->GetClangAST(); 103 return 0; 104} 105 106uint64_t 107ValueObjectVariable::GetByteSize() 108{ 109 ClangASTType type(GetClangAST(), GetClangType()); 110 111 if (!type.IsValid()) 112 return 0; 113 114 return type.GetClangTypeByteSize(); 115} 116 117lldb::ValueType 118ValueObjectVariable::GetValueType() const 119{ 120 if (m_variable_sp) 121 return m_variable_sp->GetScope(); 122 return lldb::eValueTypeInvalid; 123} 124 125bool 126ValueObjectVariable::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 default: 190 m_error.SetErrorStringWithFormat("Variable %s has an expression result value %d which is currently unhandled",variable->GetName().GetCString(),value_type); 191 break; 192 193 case Value::eValueTypeVector: 194 // fall through 195 case Value::eValueTypeScalar: 196 // The variable value is in the Scalar value inside the m_value. 197 // We can point our m_data right to it. 198 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get()); 199 break; 200 201 case Value::eValueTypeFileAddress: 202 case Value::eValueTypeLoadAddress: 203 case Value::eValueTypeHostAddress: 204 // The DWARF expression result was an address in the inferior 205 // process. If this variable is an aggregate type, we just need 206 // the address as the main value as all child variable objects 207 // will rely upon this location and add an offset and then read 208 // their own values as needed. If this variable is a simple 209 // type, we read all data for it into m_data. 210 // Make sure this type has a value before we try and read it 211 212 // If we have a file address, convert it to a load address if we can. 213 Process *process = exe_ctx.GetProcessPtr(); 214 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive()) 215 { 216 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 217 if (file_addr != LLDB_INVALID_ADDRESS) 218 { 219 SymbolContext var_sc; 220 variable->CalculateSymbolContext(&var_sc); 221 if (var_sc.module_sp) 222 { 223 ObjectFile *objfile = var_sc.module_sp->GetObjectFile(); 224 if (objfile) 225 { 226 Address so_addr(file_addr, objfile->GetSectionList()); 227 lldb::addr_t load_addr = so_addr.GetLoadAddress (target); 228 if (load_addr != LLDB_INVALID_ADDRESS) 229 { 230 m_value.SetValueType(Value::eValueTypeLoadAddress); 231 m_value.GetScalar() = load_addr; 232 } 233 } 234 } 235 } 236 } 237 238 if (ClangASTContext::IsAggregateType (GetClangType())) 239 { 240 // this value object represents an aggregate type whose 241 // children have values, but this object does not. So we 242 // say we are changed if our location has changed. 243 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); 244 } 245 else 246 { 247 // Copy the Value and set the context to use our Variable 248 // so it can extract read its value into m_data appropriately 249 Value value(m_value); 250 value.SetContext(Value::eContextTypeVariable, variable); 251 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule().get()); 252 } 253 break; 254 } 255 256 SetValueIsValid (m_error.Success()); 257 } 258 else 259 { 260 // could not find location, won't allow editing 261 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL); 262 } 263 } 264 return m_error.Success(); 265} 266 267 268 269bool 270ValueObjectVariable::IsInScope () 271{ 272 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef(); 273 if (exe_ctx_ref.HasFrameRef()) 274 { 275 ExecutionContext exe_ctx (exe_ctx_ref); 276 StackFrame *frame = exe_ctx.GetFramePtr(); 277 if (frame) 278 { 279 return m_variable_sp->IsInScope (frame); 280 } 281 else 282 { 283 // This ValueObject had a frame at one time, but now we 284 // can't locate it, so return false since we probably aren't 285 // in scope. 286 return false; 287 } 288 } 289 // We have a variable that wasn't tied to a frame, which 290 // means it is a global and is always in scope. 291 return true; 292 293} 294 295lldb::ModuleSP 296ValueObjectVariable::GetModule() 297{ 298 if (m_variable_sp) 299 { 300 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope(); 301 if (sc_scope) 302 { 303 return sc_scope->CalculateSymbolContextModule(); 304 } 305 } 306 return lldb::ModuleSP(); 307} 308 309SymbolContextScope * 310ValueObjectVariable::GetSymbolContextScope() 311{ 312 if (m_variable_sp) 313 return m_variable_sp->GetSymbolContextScope(); 314 return NULL; 315} 316 317bool 318ValueObjectVariable::GetDeclaration (Declaration &decl) 319{ 320 if (m_variable_sp) 321 { 322 decl = m_variable_sp->GetDeclaration(); 323 return true; 324 } 325 return false; 326} 327 328const char * 329ValueObjectVariable::GetLocationAsCString () 330{ 331 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) 332 return GetLocationAsCStringImpl(m_resolved_value, 333 m_data); 334 else 335 return ValueObject::GetLocationAsCString(); 336} 337 338bool 339ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error) 340{ 341 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) 342 { 343 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); 344 ExecutionContext exe_ctx(GetExecutionContextRef()); 345 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); 346 RegisterValue reg_value; 347 if (!reg_info || !reg_ctx) 348 { 349 error.SetErrorString("unable to retrieve register info"); 350 return false; 351 } 352 error = reg_value.SetValueFromCString(reg_info, value_str); 353 if (error.Fail()) 354 return false; 355 if (reg_ctx->WriteRegister (reg_info, reg_value)) 356 { 357 SetNeedsUpdate(); 358 return true; 359 } 360 else 361 { 362 error.SetErrorString("unable to write back to register"); 363 return false; 364 } 365 } 366 else 367 return ValueObject::SetValueFromCString(value_str, error); 368} 369 370bool 371ValueObjectVariable::SetData (DataExtractor &data, Error &error) 372{ 373 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) 374 { 375 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo(); 376 ExecutionContext exe_ctx(GetExecutionContextRef()); 377 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); 378 RegisterValue reg_value; 379 if (!reg_info || !reg_ctx) 380 { 381 error.SetErrorString("unable to retrieve register info"); 382 return false; 383 } 384 error = reg_value.SetValueFromData(reg_info, data, 0, false); 385 if (error.Fail()) 386 return false; 387 if (reg_ctx->WriteRegister (reg_info, reg_value)) 388 { 389 SetNeedsUpdate(); 390 return true; 391 } 392 else 393 { 394 error.SetErrorString("unable to write back to register"); 395 return false; 396 } 397 } 398 else 399 return ValueObject::SetData(data, error); 400} 401