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