ValueObject.h revision 82f0746880b4a6b18bcf8666670140f5b4a56791
1//===-- ValueObject.h -------------------------------------------*- 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#ifndef liblldb_ValueObject_h_
11#define liblldb_ValueObject_h_
12
13// C Includes
14// C++ Includes
15#include <map>
16#include <vector>
17// Other libraries and framework includes
18// Project includes
19
20#include "lldb/lldb-private.h"
21#include "lldb/Core/DataExtractor.h"
22#include "lldb/Core/Error.h"
23#include "lldb/Core/Flags.h"
24#include "lldb/Core/ConstString.h"
25#include "lldb/Core/UserID.h"
26#include "lldb/Core/Value.h"
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/ExecutionContextScope.h"
29#include "lldb/Target/StackID.h"
30#include "lldb/Utility/SharedCluster.h"
31
32namespace lldb_private {
33
34/// ValueObject:
35///
36/// This abstract class provides an interface to a particular value, be it a register, a local or global variable,
37/// that is evaluated in some particular scope.  The ValueObject also has the capibility of being the "child" of
38/// some other variable object, and in turn of having children.
39/// If a ValueObject is a root variable object - having no parent - then it must be constructed with respect to some
40/// particular ExecutionContextScope.  If it is a child, it inherits the ExecutionContextScope from its parent.
41/// The ValueObject will update itself if necessary before fetching its value, summary, object description, etc.
42/// But it will always update itself in the ExecutionContextScope with which it was originally created.
43
44/// A brief note on life cycle management for ValueObjects.  This is a little tricky because a ValueObject can contain
45/// various other ValueObjects - the Dynamic Value, its children, the dereference value, etc.  Any one of these can be
46/// handed out as a shared pointer, but for that contained value object to be valid, the root object and potentially other
47/// of the value objects need to stay around.
48/// We solve this problem by handing out shared pointers to the Value Object and any of its dependents using a shared
49/// ClusterManager.  This treats each shared pointer handed out for the entire cluster as a reference to the whole
50/// cluster.  The whole cluster will stay around until the last reference is released.
51///
52/// The ValueObject mostly handle this automatically, if a value object is made with a Parent ValueObject, then it adds
53/// itself to the ClusterManager of the parent.
54
55/// It does mean that external to the ValueObjects we should only ever make available ValueObjectSP's, never ValueObjects
56/// or pointers to them.  So all the "Root level" ValueObject derived constructors should be private, and
57/// should implement a Create function that new's up object and returns a Shared Pointer that it gets from the GetSP() method.
58///
59/// However, if you are making an derived ValueObject that will be contained in a parent value object, you should just
60/// hold onto a pointer to it internally, and by virtue of passing the parent ValueObject into its constructor, it will
61/// be added to the ClusterManager for the parent.  Then if you ever hand out a Shared Pointer to the contained ValueObject,
62/// just do so by calling GetSP() on the contained object.
63
64class ValueObject : public UserID
65{
66public:
67
68    class EvaluationPoint
69    {
70    public:
71
72        EvaluationPoint ();
73
74        EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected = false);
75
76        EvaluationPoint (const EvaluationPoint &rhs);
77
78        ~EvaluationPoint ();
79
80        ExecutionContextScope *
81        GetExecutionContextScope ();
82
83        Target *
84        GetTarget () const
85        {
86            return m_target_sp.get();
87        }
88
89        Process *
90        GetProcess () const
91        {
92            return m_process_sp.get();
93        }
94
95        // Set the EvaluationPoint to the values in exe_scope,
96        // Return true if the Evaluation Point changed.
97        // Since the ExecutionContextScope is always going to be valid currently,
98        // the Updated Context will also always be valid.
99
100        bool
101        SetContext (ExecutionContextScope *exe_scope);
102
103        void
104        SetIsConstant ()
105        {
106            SetUpdated();
107            m_stop_id = LLDB_INVALID_UID;
108        }
109
110        bool
111        IsConstant () const
112        {
113            return m_stop_id == LLDB_INVALID_UID;
114        }
115
116        lldb::user_id_t
117        GetUpdateID () const
118        {
119            return m_stop_id;
120        }
121
122        void
123        SetUpdateID (lldb::user_id_t new_id)
124        {
125            m_stop_id = new_id;
126        }
127
128        bool
129        IsFirstEvaluation () const
130        {
131            return m_first_update;
132        }
133
134        void
135        SetNeedsUpdate ()
136        {
137            m_needs_update = true;
138        }
139
140        void
141        SetUpdated ();
142
143        bool
144        NeedsUpdating()
145        {
146            SyncWithProcessState();
147            return m_needs_update;
148        }
149
150        bool
151        IsValid ()
152        {
153            if (m_stop_id == LLDB_INVALID_UID)
154                return false;
155            else if (SyncWithProcessState ())
156            {
157                if (m_stop_id == LLDB_INVALID_UID)
158                    return false;
159            }
160            return true;
161        }
162
163        void
164        SetInvalid ()
165        {
166            // Use the stop id to mark us as invalid, leave the thread id and the stack id around for logging and
167            // history purposes.
168            m_stop_id = LLDB_INVALID_UID;
169
170            // Can't update an invalid state.
171            m_needs_update = false;
172
173//            m_thread_id = LLDB_INVALID_THREAD_ID;
174//            m_stack_id.Clear();
175        }
176
177    private:
178        bool
179        SyncWithProcessState ();
180
181        ExecutionContextScope *m_exe_scope;   // This is not the way to store the evaluation point state, it is just
182                                            // a cache of the lookup, and gets thrown away when we update.
183        bool             m_needs_update;
184        bool             m_first_update;
185
186        lldb::TargetSP   m_target_sp;
187        lldb::ProcessSP  m_process_sp;
188        lldb::user_id_t  m_thread_id;
189        StackID          m_stack_id;
190        lldb::user_id_t  m_stop_id; // This is the stop id when this ValueObject was last evaluated.
191    };
192
193    const EvaluationPoint &
194    GetUpdatePoint () const
195    {
196        return m_update_point;
197    }
198
199    EvaluationPoint &
200    GetUpdatePoint ()
201    {
202        return m_update_point;
203    }
204
205    ExecutionContextScope *
206    GetExecutionContextScope ()
207    {
208        return m_update_point.GetExecutionContextScope();
209    }
210
211    virtual ~ValueObject();
212
213    //------------------------------------------------------------------
214    // Sublasses must implement the functions below.
215    //------------------------------------------------------------------
216    virtual size_t
217    GetByteSize() = 0;
218
219    virtual clang::ASTContext *
220    GetClangAST () = 0;
221
222    virtual lldb::clang_type_t
223    GetClangType () = 0;
224
225    virtual lldb::ValueType
226    GetValueType() const = 0;
227
228    virtual ConstString
229    GetTypeName() = 0;
230
231    virtual lldb::LanguageType
232    GetObjectRuntimeLanguage();
233
234    virtual bool
235    IsPointerType ();
236
237    virtual bool
238    IsPointerOrReferenceType ();
239
240    virtual bool
241    IsPossibleCPlusPlusDynamicType ();
242
243    virtual bool
244    IsBaseClass ()
245    {
246        return false;
247    }
248
249    virtual bool
250    IsDereferenceOfParent ()
251    {
252        return false;
253    }
254
255    bool
256    IsIntegerType (bool &is_signed);
257
258    virtual bool
259    GetBaseClassPath (Stream &s);
260
261    virtual void
262    GetExpressionPath (Stream &s, bool qualify_cxx_base_classes);
263
264    virtual bool
265    IsInScope ()
266    {
267        return true;
268    }
269
270    virtual off_t
271    GetByteOffset()
272    {
273        return 0;
274    }
275
276    virtual uint32_t
277    GetBitfieldBitSize()
278    {
279        return 0;
280    }
281
282    virtual uint32_t
283    GetBitfieldBitOffset()
284    {
285        return 0;
286    }
287
288    virtual bool
289    SetClangAST (clang::ASTContext *ast)
290    {
291        return false;
292    }
293
294    virtual const char *
295    GetValueAsCString ();
296
297    virtual bool
298    SetValueFromCString (const char *value_str);
299
300    //------------------------------------------------------------------
301    // The functions below should NOT be modified by sublasses
302    //------------------------------------------------------------------
303    const Error &
304    GetError() const;
305
306    const ConstString &
307    GetName() const;
308
309    lldb::ValueObjectSP
310    GetChildAtIndex (uint32_t idx, bool can_create);
311
312    virtual lldb::ValueObjectSP
313    GetChildMemberWithName (const ConstString &name, bool can_create);
314
315    virtual uint32_t
316    GetIndexOfChildWithName (const ConstString &name);
317
318    uint32_t
319    GetNumChildren ();
320
321    const Value &
322    GetValue() const;
323
324    Value &
325    GetValue();
326
327    bool
328    ResolveValue (Scalar &scalar);
329
330    const char *
331    GetLocationAsCString ();
332
333    const char *
334    GetSummaryAsCString ();
335
336    const char *
337    GetObjectDescription ();
338
339    bool
340    GetValueIsValid () const;
341
342    bool
343    GetValueDidChange ();
344
345    bool
346    UpdateValueIfNeeded ();
347
348    DataExtractor &
349    GetDataExtractor ();
350
351    bool
352    Write ();
353
354    lldb::ValueObjectSP
355    GetSP ()
356    {
357        return m_manager->GetSharedPointer(this);
358    }
359
360protected:
361    void
362    AddSyntheticChild (const ConstString &key,
363                       ValueObject *valobj);
364public:
365    lldb::ValueObjectSP
366    GetSyntheticChild (const ConstString &key) const;
367
368    lldb::ValueObjectSP
369    GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create);
370
371    lldb::ValueObjectSP
372    GetDynamicValue (lldb::DynamicValueType valueType);
373
374    virtual lldb::ValueObjectSP
375    CreateConstantValue (const ConstString &name);
376
377    virtual lldb::ValueObjectSP
378    Dereference (Error &error);
379
380    virtual lldb::ValueObjectSP
381    AddressOf (Error &error);
382
383    virtual lldb::ValueObjectSP
384    CastPointerType (const char *name,
385                     ClangASTType &ast_type);
386
387    virtual lldb::ValueObjectSP
388    CastPointerType (const char *name,
389                     lldb::TypeSP &type_sp);
390
391    // The backing bits of this value object were updated, clear any value
392    // values, summaries or descriptions so we refetch them.
393    virtual void
394    ValueUpdated ()
395    {
396        m_value_str.clear();
397        m_summary_str.clear();
398        m_object_desc_str.clear();
399    }
400
401    virtual bool
402    IsDynamic ()
403    {
404        return false;
405    }
406
407    static void
408    DumpValueObject (Stream &s,
409                     ValueObject *valobj,
410                     const char *root_valobj_name,
411                     uint32_t ptr_depth,
412                     uint32_t curr_depth,
413                     uint32_t max_depth,
414                     bool show_types,
415                     bool show_location,
416                     bool use_objc,
417                     lldb::DynamicValueType use_dynamic,
418                     bool scope_already_checked,
419                     bool flat_output);
420
421    bool
422    GetIsConstant () const
423    {
424        return m_update_point.IsConstant();
425    }
426
427    void
428    SetIsConstant ()
429    {
430        m_update_point.SetIsConstant();
431    }
432
433    lldb::Format
434    GetFormat () const
435    {
436        if (m_parent && m_format == lldb::eFormatDefault)
437            return m_parent->GetFormat();
438        return m_format;
439    }
440
441    void
442    SetFormat (lldb::Format format)
443    {
444        if (format != m_format)
445            m_value_str.clear();
446        m_format = format;
447    }
448
449    // Use GetParent for display purposes, but if you want to tell the parent to update itself
450    // then use m_parent.  The ValueObjectDynamicValue's parent is not the correct parent for
451    // displaying, they are really siblings, so for display it needs to route through to its grandparent.
452    virtual ValueObject *
453    GetParent()
454    {
455        return m_parent;
456    }
457
458    virtual const ValueObject *
459    GetParent() const
460    {
461        return m_parent;
462    }
463
464    ValueObject *
465    GetNonBaseClassParent();
466
467    void
468    SetPointersPointToLoadAddrs (bool b)
469    {
470        m_pointers_point_to_load_addrs = b;
471    }
472
473protected:
474    typedef ClusterManager<ValueObject> ValueObjectManager;
475
476    //------------------------------------------------------------------
477    // Classes that inherit from ValueObject can see and modify these
478    //------------------------------------------------------------------
479    ValueObject  *      m_parent;       // The parent value object, or NULL if this has no parent
480    EvaluationPoint     m_update_point; // Stores both the stop id and the full context at which this value was last
481                                        // updated.  When we are asked to update the value object, we check whether
482                                        // the context & stop id are the same before updating.
483    ConstString         m_name;         // The name of this object
484    DataExtractor       m_data;         // A data extractor that can be used to extract the value.
485    Value               m_value;
486    Error               m_error;        // An error object that can describe any errors that occur when updating values.
487    std::string         m_value_str;    // Cached value string that will get cleared if/when the value is updated.
488    std::string         m_old_value_str;// Cached old value string from the last time the value was gotten
489    std::string         m_location_str; // Cached location string that will get cleared if/when the value is updated.
490    std::string         m_summary_str;  // Cached summary string that will get cleared if/when the value is updated.
491    std::string         m_object_desc_str; // Cached result of the "object printer".  This differs from the summary
492                                              // in that the summary is consed up by us, the object_desc_string is builtin.
493
494    ValueObjectManager *m_manager;      // This object is managed by the root object (any ValueObject that gets created
495                                        // without a parent.)  The manager gets passed through all the generations of
496                                        // dependent objects, and will keep the whole cluster of objects alive as long
497                                        // as a shared pointer to any of them has been handed out.  Shared pointers to
498                                        // value objects must always be made with the GetSP method.
499
500    std::vector<ValueObject *> m_children;
501    std::map<ConstString, ValueObject *> m_synthetic_children;
502    ValueObject *m_dynamic_value;
503    lldb::ValueObjectSP m_addr_of_valobj_sp; // We have to hold onto a shared pointer to this one because it is created
504                                             // as an independent ValueObjectConstResult, which isn't managed by us.
505    ValueObject *m_deref_valobj;
506
507    lldb::Format        m_format;
508    bool                m_value_is_valid:1,
509                        m_value_did_change:1,
510                        m_children_count_valid:1,
511                        m_old_value_valid:1,
512                        m_pointers_point_to_load_addrs:1,
513                        m_is_deref_of_parent:1;
514
515    friend class ClangExpressionDeclMap;  // For GetValue
516    friend class ClangExpressionVariable; // For SetName
517    friend class Target;                  // For SetName
518
519    //------------------------------------------------------------------
520    // Constructors and Destructors
521    //------------------------------------------------------------------
522
523    // Use the no-argument constructor to make a constant variable object (with no ExecutionContextScope.)
524
525    ValueObject();
526
527    // Use this constructor to create a "root variable object".  The ValueObject will be locked to this context
528    // through-out its lifespan.
529
530    ValueObject (ExecutionContextScope *exe_scope);
531
532    // Use this constructor to create a ValueObject owned by another ValueObject.  It will inherit the ExecutionContext
533    // of its parent.
534
535    ValueObject (ValueObject &parent);
536
537    ValueObjectManager *
538    GetManager()
539    {
540        return m_manager;
541    }
542
543    virtual bool
544    UpdateValue () = 0;
545
546    virtual void
547    CalculateDynamicValue (lldb::DynamicValueType use_dynamic);
548
549    // Should only be called by ValueObject::GetChildAtIndex()
550    // Returns a ValueObject managed by this ValueObject's manager.
551    virtual ValueObject *
552    CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index);
553
554    // Should only be called by ValueObject::GetNumChildren()
555    virtual uint32_t
556    CalculateNumChildren() = 0;
557
558    void
559    SetName (const char *name);
560
561    void
562    SetName (const ConstString &name);
563
564    void
565    SetNumChildren (uint32_t num_children);
566
567    void
568    SetValueDidChange (bool value_changed);
569
570    void
571    SetValueIsValid (bool valid);
572
573public:
574    lldb::addr_t
575    GetPointerValue (AddressType &address_type,
576                     bool scalar_is_load_address);
577
578    lldb::addr_t
579    GetAddressOf (AddressType &address_type,
580                  bool scalar_is_load_address);
581private:
582    //------------------------------------------------------------------
583    // For ValueObject only
584    //------------------------------------------------------------------
585    DISALLOW_COPY_AND_ASSIGN (ValueObject);
586
587};
588
589} // namespace lldb_private
590
591#endif  // liblldb_ValueObject_h_
592