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