ValueObject.h revision 56bbdaf817cb19a2f133e8501473f499be447c2d
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            m_first_update = false;
144            m_needs_update = false;
145        }
146
147        bool
148        NeedsUpdating()
149        {
150            SyncWithProcessState();
151            return m_needs_update;
152        }
153
154        bool
155        IsValid ()
156        {
157            if (m_stop_id == LLDB_INVALID_UID)
158                return false;
159            else if (SyncWithProcessState ())
160            {
161                if (m_stop_id == LLDB_INVALID_UID)
162                    return false;
163            }
164            return true;
165        }
166
167        void
168        SetInvalid ()
169        {
170            // Use the stop id to mark us as invalid, leave the thread id and the stack id around for logging and
171            // history purposes.
172            m_stop_id = LLDB_INVALID_UID;
173
174            // Can't update an invalid state.
175            m_needs_update = false;
176
177//            m_thread_id = LLDB_INVALID_THREAD_ID;
178//            m_stack_id.Clear();
179        }
180
181    private:
182        bool
183        SyncWithProcessState ();
184
185        ExecutionContextScope *m_exe_scope;   // This is not the way to store the evaluation point state, it is just
186                                            // a cache of the lookup, and gets thrown away when we update.
187        bool             m_needs_update;
188        bool             m_first_update;
189
190        lldb::TargetSP   m_target_sp;
191        lldb::ProcessSP  m_process_sp;
192        lldb::user_id_t  m_thread_id;
193        StackID          m_stack_id;
194        lldb::user_id_t  m_stop_id; // This is the stop id when this ValueObject was last evaluated.
195    };
196
197    const EvaluationPoint &
198    GetUpdatePoint () const
199    {
200        return m_update_point;
201    }
202
203    EvaluationPoint &
204    GetUpdatePoint ()
205    {
206        return m_update_point;
207    }
208
209    ExecutionContextScope *
210    GetExecutionContextScope ()
211    {
212        return m_update_point.GetExecutionContextScope();
213    }
214
215    virtual ~ValueObject();
216
217    //------------------------------------------------------------------
218    // Sublasses must implement the functions below.
219    //------------------------------------------------------------------
220    virtual size_t
221    GetByteSize() = 0;
222
223    virtual clang::ASTContext *
224    GetClangAST () = 0;
225
226    virtual lldb::clang_type_t
227    GetClangType () = 0;
228
229    virtual lldb::ValueType
230    GetValueType() const = 0;
231
232    virtual ConstString
233    GetTypeName() = 0;
234
235    virtual lldb::LanguageType
236    GetObjectRuntimeLanguage();
237
238    virtual bool
239    IsPointerType ();
240
241    virtual bool
242    IsPointerOrReferenceType ();
243
244    virtual bool
245    IsBaseClass ()
246    {
247        return false;
248    }
249
250    virtual bool
251    IsDereferenceOfParent ()
252    {
253        return false;
254    }
255
256    bool
257    IsIntegerType (bool &is_signed);
258
259    virtual bool
260    GetBaseClassPath (Stream &s);
261
262    virtual void
263    GetExpressionPath (Stream &s, bool qualify_cxx_base_classes);
264
265    virtual bool
266    IsInScope ()
267    {
268        return true;
269    }
270
271    virtual off_t
272    GetByteOffset()
273    {
274        return 0;
275    }
276
277    virtual uint32_t
278    GetBitfieldBitSize()
279    {
280        return 0;
281    }
282
283    virtual uint32_t
284    GetBitfieldBitOffset()
285    {
286        return 0;
287    }
288
289    virtual bool
290    SetClangAST (clang::ASTContext *ast)
291    {
292        return false;
293    }
294
295    virtual const char *
296    GetValueAsCString ();
297
298    virtual bool
299    SetValueFromCString (const char *value_str);
300
301    //------------------------------------------------------------------
302    // The functions below should NOT be modified by sublasses
303    //------------------------------------------------------------------
304    const Error &
305    GetError() const;
306
307    const ConstString &
308    GetName() const;
309
310    lldb::ValueObjectSP
311    GetChildAtIndex (uint32_t idx, bool can_create);
312
313    virtual lldb::ValueObjectSP
314    GetChildMemberWithName (const ConstString &name, bool can_create);
315
316    virtual uint32_t
317    GetIndexOfChildWithName (const ConstString &name);
318
319    uint32_t
320    GetNumChildren ();
321
322    const Value &
323    GetValue() const;
324
325    Value &
326    GetValue();
327
328    bool
329    ResolveValue (Scalar &scalar);
330
331    const char *
332    GetLocationAsCString ();
333
334    const char *
335    GetSummaryAsCString ();
336
337    const char *
338    GetObjectDescription ();
339
340    bool
341    GetValueIsValid () const;
342
343    bool
344    GetValueDidChange ();
345
346    bool
347    UpdateValueIfNeeded ();
348
349    DataExtractor &
350    GetDataExtractor ();
351
352    bool
353    Write ();
354
355    lldb::ValueObjectSP
356    GetSP ()
357    {
358        return m_manager->GetSharedPointer(this);
359    }
360
361protected:
362    void
363    AddSyntheticChild (const ConstString &key,
364                       ValueObject *valobj);
365public:
366    lldb::ValueObjectSP
367    GetSyntheticChild (const ConstString &key) const;
368
369    lldb::ValueObjectSP
370    GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create);
371
372    lldb::ValueObjectSP
373    GetDynamicValue (bool can_create);
374
375    virtual lldb::ValueObjectSP
376    CreateConstantValue (const ConstString &name);
377
378    virtual lldb::ValueObjectSP
379    Dereference (Error &error);
380
381    virtual lldb::ValueObjectSP
382    AddressOf (Error &error);
383
384    // The backing bits of this value object were updated, clear any value
385    // values, summaries or descriptions so we refetch them.
386    virtual void
387    ValueUpdated ()
388    {
389        m_value_str.clear();
390        m_summary_str.clear();
391        m_object_desc_str.clear();
392    }
393
394    virtual bool
395    IsDynamic ()
396    {
397        return false;
398    }
399
400    static void
401    DumpValueObject (Stream &s,
402                     ValueObject *valobj,
403                     const char *root_valobj_name,
404                     uint32_t ptr_depth,
405                     uint32_t curr_depth,
406                     uint32_t max_depth,
407                     bool show_types,
408                     bool show_location,
409                     bool use_objc,
410                     bool use_dynamic,
411                     bool scope_already_checked,
412                     bool flat_output);
413
414    bool
415    GetIsConstant () const
416    {
417        return m_update_point.IsConstant();
418    }
419
420    void
421    SetIsConstant ()
422    {
423        m_update_point.SetIsConstant();
424    }
425
426    lldb::Format
427    GetFormat () const
428    {
429        if (m_parent && m_format == lldb::eFormatDefault)
430            return m_parent->GetFormat();
431        return m_format;
432    }
433
434    void
435    SetFormat (lldb::Format format)
436    {
437        if (format != m_format)
438            m_value_str.clear();
439        m_format = format;
440    }
441
442    // Use GetParent for display purposes, but if you want to tell the parent to update itself
443    // then use m_parent.  The ValueObjectDynamicValue's parent is not the correct parent for
444    // displaying, they are really siblings, so for display it needs to route through to its grandparent.
445    virtual ValueObject *
446    GetParent()
447    {
448        return m_parent;
449    }
450
451    virtual const ValueObject *
452    GetParent() const
453    {
454        return m_parent;
455    }
456
457    ValueObject *
458    GetNonBaseClassParent();
459
460    void
461    SetPointersPointToLoadAddrs (bool b)
462    {
463        m_pointers_point_to_load_addrs = b;
464    }
465
466protected:
467    typedef ClusterManager<ValueObject> ValueObjectManager;
468
469    //------------------------------------------------------------------
470    // Classes that inherit from ValueObject can see and modify these
471    //------------------------------------------------------------------
472    ValueObject  *      m_parent;       // The parent value object, or NULL if this has no parent
473    EvaluationPoint     m_update_point; // Stores both the stop id and the full context at which this value was last
474                                        // updated.  When we are asked to update the value object, we check whether
475                                        // the context & stop id are the same before updating.
476    ConstString         m_name;         // The name of this object
477    DataExtractor       m_data;         // A data extractor that can be used to extract the value.
478    Value               m_value;
479    Error               m_error;        // An error object that can describe any errors that occur when updating values.
480    std::string         m_value_str;    // Cached value string that will get cleared if/when the value is updated.
481    std::string         m_old_value_str;// Cached old value string from the last time the value was gotten
482    std::string         m_location_str; // Cached location string that will get cleared if/when the value is updated.
483    std::string         m_summary_str;  // Cached summary string that will get cleared if/when the value is updated.
484    std::string         m_object_desc_str; // Cached result of the "object printer".  This differs from the summary
485                                              // in that the summary is consed up by us, the object_desc_string is builtin.
486
487    ValueObjectManager *m_manager;      // This object is managed by the root object (any ValueObject that gets created
488                                        // without a parent.)  The manager gets passed through all the generations of
489                                        // dependent objects, and will keep the whole cluster of objects alive as long
490                                        // as a shared pointer to any of them has been handed out.  Shared pointers to
491                                        // value objects must always be made with the GetSP method.
492
493    std::vector<ValueObject *> m_children;
494    std::map<ConstString, ValueObject *> m_synthetic_children;
495    ValueObject *m_dynamic_value;
496    lldb::ValueObjectSP m_addr_of_valobj_sp; // We have to hold onto a shared pointer to this one because it is created
497                                             // as an independent ValueObjectConstResult, which isn't managed by us.
498    ValueObject *m_deref_valobj;
499
500    lldb::Format        m_format;
501    bool                m_value_is_valid:1,
502                        m_value_did_change:1,
503                        m_children_count_valid:1,
504                        m_old_value_valid:1,
505                        m_pointers_point_to_load_addrs:1,
506                        m_is_deref_of_parent:1;
507
508    friend class ClangExpressionDeclMap;  // For GetValue
509    friend class ClangExpressionVariable; // For SetName
510    friend class Target;                  // For SetName
511
512    //------------------------------------------------------------------
513    // Constructors and Destructors
514    //------------------------------------------------------------------
515
516    // Use the no-argument constructor to make a constant variable object (with no ExecutionContextScope.)
517
518    ValueObject();
519
520    // Use this constructor to create a "root variable object".  The ValueObject will be locked to this context
521    // through-out its lifespan.
522
523    ValueObject (ExecutionContextScope *exe_scope);
524
525    // Use this constructor to create a ValueObject owned by another ValueObject.  It will inherit the ExecutionContext
526    // of its parent.
527
528    ValueObject (ValueObject &parent);
529
530    ValueObjectManager *
531    GetManager()
532    {
533        return m_manager;
534    }
535
536    virtual bool
537    UpdateValue () = 0;
538
539    virtual void
540    CalculateDynamicValue ();
541
542    // Should only be called by ValueObject::GetChildAtIndex()
543    // Returns a ValueObject managed by this ValueObject's manager.
544    virtual ValueObject *
545    CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index);
546
547    // Should only be called by ValueObject::GetNumChildren()
548    virtual uint32_t
549    CalculateNumChildren() = 0;
550
551    void
552    SetName (const char *name);
553
554    void
555    SetName (const ConstString &name);
556
557    void
558    SetNumChildren (uint32_t num_children);
559
560    void
561    SetValueDidChange (bool value_changed);
562
563    void
564    SetValueIsValid (bool valid);
565
566public:
567    lldb::addr_t
568    GetPointerValue (AddressType &address_type,
569                     bool scalar_is_load_address);
570
571    lldb::addr_t
572    GetAddressOf (AddressType &address_type,
573                  bool scalar_is_load_address);
574private:
575    //------------------------------------------------------------------
576    // For ValueObject only
577    //------------------------------------------------------------------
578    DISALLOW_COPY_AND_ASSIGN (ValueObject);
579
580};
581
582} // namespace lldb_private
583
584#endif  // liblldb_ValueObject_h_
585