]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/ValueObject.h
Merge lldb trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / ValueObject.h
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 #include "lldb/Core/Value.h"
14 #include "lldb/DataFormatters/DumpValueObjectOptions.h" // for DumpValueObj...
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Symbol/Type.h" // for TypeImpl
17 #include "lldb/Target/ExecutionContext.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/DataExtractor.h"
21 #include "lldb/Utility/Error.h"
22 #include "lldb/Utility/SharedCluster.h"
23 #include "lldb/Utility/UserID.h"
24 #include "lldb/lldb-defines.h"              // for LLDB_INVALID...
25 #include "lldb/lldb-enumerations.h"         // for DynamicValue...
26 #include "lldb/lldb-forward.h"              // for ValueObjectSP
27 #include "lldb/lldb-private-enumerations.h" // for AddressType
28 #include "lldb/lldb-types.h"                // for addr_t, offs...
29
30 #include "llvm/ADT/Optional.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/StringRef.h" // for StringRef
33
34 #include <functional>
35 #include <initializer_list>
36 #include <map>
37 #include <mutex>   // for recursive_mutex
38 #include <string>  // for string
39 #include <utility> // for pair
40 #include <vector>
41
42 #include <stddef.h> // for size_t
43 #include <stdint.h> // for uint32_t
44 namespace lldb_private {
45 class Declaration;
46 }
47 namespace lldb_private {
48 class EvaluateExpressionOptions;
49 }
50 namespace lldb_private {
51 class ExecutionContextScope;
52 }
53 namespace lldb_private {
54 class Log;
55 }
56 namespace lldb_private {
57 class Scalar;
58 }
59 namespace lldb_private {
60 class Stream;
61 }
62 namespace lldb_private {
63 class SymbolContextScope;
64 }
65 namespace lldb_private {
66 class TypeFormatImpl;
67 }
68 namespace lldb_private {
69 class TypeSummaryImpl;
70 }
71 namespace lldb_private {
72 class TypeSummaryOptions;
73 }
74 namespace lldb_private {
75
76 /// ValueObject:
77 ///
78 /// This abstract class provides an interface to a particular value, be it a
79 /// register, a local or global variable,
80 /// that is evaluated in some particular scope.  The ValueObject also has the
81 /// capability of being the "child" of
82 /// some other variable object, and in turn of having children.
83 /// If a ValueObject is a root variable object - having no parent - then it must
84 /// be constructed with respect to some
85 /// particular ExecutionContextScope.  If it is a child, it inherits the
86 /// ExecutionContextScope from its parent.
87 /// The ValueObject will update itself if necessary before fetching its value,
88 /// summary, object description, etc.
89 /// But it will always update itself in the ExecutionContextScope with which it
90 /// was originally created.
91
92 /// A brief note on life cycle management for ValueObjects.  This is a little
93 /// tricky because a ValueObject can contain
94 /// various other ValueObjects - the Dynamic Value, its children, the
95 /// dereference value, etc.  Any one of these can be
96 /// handed out as a shared pointer, but for that contained value object to be
97 /// valid, the root object and potentially other
98 /// of the value objects need to stay around.
99 /// We solve this problem by handing out shared pointers to the Value Object and
100 /// any of its dependents using a shared
101 /// ClusterManager.  This treats each shared pointer handed out for the entire
102 /// cluster as a reference to the whole
103 /// cluster.  The whole cluster will stay around until the last reference is
104 /// released.
105 ///
106 /// The ValueObject mostly handle this automatically, if a value object is made
107 /// with a Parent ValueObject, then it adds
108 /// itself to the ClusterManager of the parent.
109
110 /// It does mean that external to the ValueObjects we should only ever make
111 /// available ValueObjectSP's, never ValueObjects
112 /// or pointers to them.  So all the "Root level" ValueObject derived
113 /// constructors should be private, and
114 /// should implement a Create function that new's up object and returns a Shared
115 /// Pointer that it gets from the GetSP() method.
116 ///
117 /// However, if you are making an derived ValueObject that will be contained in
118 /// a parent value object, you should just
119 /// hold onto a pointer to it internally, and by virtue of passing the parent
120 /// ValueObject into its constructor, it will
121 /// be added to the ClusterManager for the parent.  Then if you ever hand out a
122 /// Shared Pointer to the contained ValueObject,
123 /// just do so by calling GetSP() on the contained object.
124
125 class ValueObject : public UserID {
126 public:
127   enum GetExpressionPathFormat {
128     eGetExpressionPathFormatDereferencePointers = 1,
129     eGetExpressionPathFormatHonorPointers
130   };
131
132   enum ValueObjectRepresentationStyle {
133     eValueObjectRepresentationStyleValue = 1,
134     eValueObjectRepresentationStyleSummary,
135     eValueObjectRepresentationStyleLanguageSpecific,
136     eValueObjectRepresentationStyleLocation,
137     eValueObjectRepresentationStyleChildrenCount,
138     eValueObjectRepresentationStyleType,
139     eValueObjectRepresentationStyleName,
140     eValueObjectRepresentationStyleExpressionPath
141   };
142
143   enum ExpressionPathScanEndReason {
144     eExpressionPathScanEndReasonEndOfString = 1,      // out of data to parse
145     eExpressionPathScanEndReasonNoSuchChild,          // child element not found
146     eExpressionPathScanEndReasonNoSuchSyntheticChild, // (synthetic) child
147                                                       // element not found
148     eExpressionPathScanEndReasonEmptyRangeNotAllowed, // [] only allowed for
149                                                       // arrays
150     eExpressionPathScanEndReasonDotInsteadOfArrow, // . used when -> should be
151                                                    // used
152     eExpressionPathScanEndReasonArrowInsteadOfDot, // -> used when . should be
153                                                    // used
154     eExpressionPathScanEndReasonFragileIVarNotAllowed,   // ObjC ivar expansion
155                                                          // not allowed
156     eExpressionPathScanEndReasonRangeOperatorNotAllowed, // [] not allowed by
157                                                          // options
158     eExpressionPathScanEndReasonRangeOperatorInvalid, // [] not valid on objects
159                                                       // other than scalars,
160                                                       // pointers or arrays
161     eExpressionPathScanEndReasonArrayRangeOperatorMet, // [] is good for arrays,
162                                                        // but I cannot parse it
163     eExpressionPathScanEndReasonBitfieldRangeOperatorMet, // [] is good for
164                                                           // bitfields, but I
165                                                           // cannot parse after
166                                                           // it
167     eExpressionPathScanEndReasonUnexpectedSymbol, // something is malformed in
168                                                   // the expression
169     eExpressionPathScanEndReasonTakingAddressFailed,   // impossible to apply &
170                                                        // operator
171     eExpressionPathScanEndReasonDereferencingFailed,   // impossible to apply *
172                                                        // operator
173     eExpressionPathScanEndReasonRangeOperatorExpanded, // [] was expanded into a
174                                                        // VOList
175     eExpressionPathScanEndReasonSyntheticValueMissing, // getting the synthetic
176                                                        // children failed
177     eExpressionPathScanEndReasonUnknown = 0xFFFF
178   };
179
180   enum ExpressionPathEndResultType {
181     eExpressionPathEndResultTypePlain = 1,       // anything but...
182     eExpressionPathEndResultTypeBitfield,        // a bitfield
183     eExpressionPathEndResultTypeBoundedRange,    // a range [low-high]
184     eExpressionPathEndResultTypeUnboundedRange,  // a range []
185     eExpressionPathEndResultTypeValueObjectList, // several items in a VOList
186     eExpressionPathEndResultTypeInvalid = 0xFFFF
187   };
188
189   enum ExpressionPathAftermath {
190     eExpressionPathAftermathNothing = 1, // just return it
191     eExpressionPathAftermathDereference, // dereference the target
192     eExpressionPathAftermathTakeAddress  // take target's address
193   };
194
195   enum ClearUserVisibleDataItems {
196     eClearUserVisibleDataItemsNothing = 1u << 0,
197     eClearUserVisibleDataItemsValue = 1u << 1,
198     eClearUserVisibleDataItemsSummary = 1u << 2,
199     eClearUserVisibleDataItemsLocation = 1u << 3,
200     eClearUserVisibleDataItemsDescription = 1u << 4,
201     eClearUserVisibleDataItemsSyntheticChildren = 1u << 5,
202     eClearUserVisibleDataItemsValidator = 1u << 6,
203     eClearUserVisibleDataItemsAllStrings =
204         eClearUserVisibleDataItemsValue | eClearUserVisibleDataItemsSummary |
205         eClearUserVisibleDataItemsLocation |
206         eClearUserVisibleDataItemsDescription,
207     eClearUserVisibleDataItemsAll = 0xFFFF
208   };
209
210   struct GetValueForExpressionPathOptions {
211     enum class SyntheticChildrenTraversal {
212       None,
213       ToSynthetic,
214       FromSynthetic,
215       Both
216     };
217
218     bool m_check_dot_vs_arrow_syntax;
219     bool m_no_fragile_ivar;
220     bool m_allow_bitfields_syntax;
221     SyntheticChildrenTraversal m_synthetic_children_traversal;
222
223     GetValueForExpressionPathOptions(
224         bool dot = false, bool no_ivar = false, bool bitfield = true,
225         SyntheticChildrenTraversal synth_traverse =
226             SyntheticChildrenTraversal::ToSynthetic)
227         : m_check_dot_vs_arrow_syntax(dot), m_no_fragile_ivar(no_ivar),
228           m_allow_bitfields_syntax(bitfield),
229           m_synthetic_children_traversal(synth_traverse) {}
230
231     GetValueForExpressionPathOptions &DoCheckDotVsArrowSyntax() {
232       m_check_dot_vs_arrow_syntax = true;
233       return *this;
234     }
235
236     GetValueForExpressionPathOptions &DontCheckDotVsArrowSyntax() {
237       m_check_dot_vs_arrow_syntax = false;
238       return *this;
239     }
240
241     GetValueForExpressionPathOptions &DoAllowFragileIVar() {
242       m_no_fragile_ivar = false;
243       return *this;
244     }
245
246     GetValueForExpressionPathOptions &DontAllowFragileIVar() {
247       m_no_fragile_ivar = true;
248       return *this;
249     }
250
251     GetValueForExpressionPathOptions &DoAllowBitfieldSyntax() {
252       m_allow_bitfields_syntax = true;
253       return *this;
254     }
255
256     GetValueForExpressionPathOptions &DontAllowBitfieldSyntax() {
257       m_allow_bitfields_syntax = false;
258       return *this;
259     }
260
261     GetValueForExpressionPathOptions &
262     SetSyntheticChildrenTraversal(SyntheticChildrenTraversal traverse) {
263       m_synthetic_children_traversal = traverse;
264       return *this;
265     }
266
267     static const GetValueForExpressionPathOptions DefaultOptions() {
268       static GetValueForExpressionPathOptions g_default_options;
269
270       return g_default_options;
271     }
272   };
273
274   class EvaluationPoint {
275   public:
276     EvaluationPoint();
277
278     EvaluationPoint(ExecutionContextScope *exe_scope,
279                     bool use_selected = false);
280
281     EvaluationPoint(const EvaluationPoint &rhs);
282
283     ~EvaluationPoint();
284
285     const ExecutionContextRef &GetExecutionContextRef() const {
286       return m_exe_ctx_ref;
287     }
288
289     // Set the EvaluationPoint to the values in exe_scope,
290     // Return true if the Evaluation Point changed.
291     // Since the ExecutionContextScope is always going to be valid currently,
292     // the Updated Context will also always be valid.
293
294     //        bool
295     //        SetContext (ExecutionContextScope *exe_scope);
296
297     void SetIsConstant() {
298       SetUpdated();
299       m_mod_id.SetInvalid();
300     }
301
302     bool IsConstant() const { return !m_mod_id.IsValid(); }
303
304     ProcessModID GetModID() const { return m_mod_id; }
305
306     void SetUpdateID(ProcessModID new_id) { m_mod_id = new_id; }
307
308     void SetNeedsUpdate() { m_needs_update = true; }
309
310     void SetUpdated();
311
312     bool NeedsUpdating(bool accept_invalid_exe_ctx) {
313       SyncWithProcessState(accept_invalid_exe_ctx);
314       return m_needs_update;
315     }
316
317     bool IsValid() {
318       const bool accept_invalid_exe_ctx = false;
319       if (!m_mod_id.IsValid())
320         return false;
321       else if (SyncWithProcessState(accept_invalid_exe_ctx)) {
322         if (!m_mod_id.IsValid())
323           return false;
324       }
325       return true;
326     }
327
328     void SetInvalid() {
329       // Use the stop id to mark us as invalid, leave the thread id and the
330       // stack id around for logging and
331       // history purposes.
332       m_mod_id.SetInvalid();
333
334       // Can't update an invalid state.
335       m_needs_update = false;
336     }
337
338   private:
339     bool SyncWithProcessState(bool accept_invalid_exe_ctx);
340
341     ProcessModID m_mod_id; // This is the stop id when this ValueObject was last
342                            // evaluated.
343     ExecutionContextRef m_exe_ctx_ref;
344     bool m_needs_update;
345   };
346
347   virtual ~ValueObject();
348
349   const EvaluationPoint &GetUpdatePoint() const { return m_update_point; }
350
351   EvaluationPoint &GetUpdatePoint() { return m_update_point; }
352
353   const ExecutionContextRef &GetExecutionContextRef() const {
354     return m_update_point.GetExecutionContextRef();
355   }
356
357   lldb::TargetSP GetTargetSP() const {
358     return m_update_point.GetExecutionContextRef().GetTargetSP();
359   }
360
361   lldb::ProcessSP GetProcessSP() const {
362     return m_update_point.GetExecutionContextRef().GetProcessSP();
363   }
364
365   lldb::ThreadSP GetThreadSP() const {
366     return m_update_point.GetExecutionContextRef().GetThreadSP();
367   }
368
369   lldb::StackFrameSP GetFrameSP() const {
370     return m_update_point.GetExecutionContextRef().GetFrameSP();
371   }
372
373   void SetNeedsUpdate();
374
375   CompilerType GetCompilerType();
376
377   // this vends a TypeImpl that is useful at the SB API layer
378   virtual TypeImpl GetTypeImpl();
379
380   virtual bool CanProvideValue();
381
382   //------------------------------------------------------------------
383   // Subclasses must implement the functions below.
384   //------------------------------------------------------------------
385   virtual uint64_t GetByteSize() = 0;
386
387   virtual lldb::ValueType GetValueType() const = 0;
388
389   //------------------------------------------------------------------
390   // Subclasses can implement the functions below.
391   //------------------------------------------------------------------
392   virtual ConstString GetTypeName();
393
394   virtual ConstString GetDisplayTypeName();
395
396   virtual ConstString GetQualifiedTypeName();
397
398   virtual lldb::LanguageType GetObjectRuntimeLanguage();
399
400   virtual uint32_t
401   GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr);
402
403   virtual bool IsPointerType();
404
405   virtual bool IsArrayType();
406
407   virtual bool IsScalarType();
408
409   virtual bool IsPointerOrReferenceType();
410
411   virtual bool IsPossibleDynamicType();
412
413   bool IsNilReference();
414
415   bool IsUninitializedReference();
416
417   virtual bool IsBaseClass() { return false; }
418
419   bool IsBaseClass(uint32_t &depth);
420
421   virtual bool IsDereferenceOfParent() { return false; }
422
423   bool IsIntegerType(bool &is_signed);
424
425   virtual bool GetBaseClassPath(Stream &s);
426
427   virtual void GetExpressionPath(
428       Stream &s, bool qualify_cxx_base_classes,
429       GetExpressionPathFormat = eGetExpressionPathFormatDereferencePointers);
430
431   lldb::ValueObjectSP GetValueForExpressionPath(
432       llvm::StringRef expression,
433       ExpressionPathScanEndReason *reason_to_stop = nullptr,
434       ExpressionPathEndResultType *final_value_type = nullptr,
435       const GetValueForExpressionPathOptions &options =
436           GetValueForExpressionPathOptions::DefaultOptions(),
437       ExpressionPathAftermath *final_task_on_target = nullptr);
438
439   virtual bool IsInScope() { return true; }
440
441   virtual lldb::offset_t GetByteOffset() { return 0; }
442
443   virtual uint32_t GetBitfieldBitSize() { return 0; }
444
445   virtual uint32_t GetBitfieldBitOffset() { return 0; }
446
447   bool IsBitfield() {
448     return (GetBitfieldBitSize() != 0) || (GetBitfieldBitOffset() != 0);
449   }
450
451   virtual bool IsArrayItemForPointer() { return m_is_array_item_for_pointer; }
452
453   virtual const char *GetValueAsCString();
454
455   virtual bool GetValueAsCString(const lldb_private::TypeFormatImpl &format,
456                                  std::string &destination);
457
458   bool GetValueAsCString(lldb::Format format, std::string &destination);
459
460   virtual uint64_t GetValueAsUnsigned(uint64_t fail_value,
461                                       bool *success = nullptr);
462
463   virtual int64_t GetValueAsSigned(int64_t fail_value, bool *success = nullptr);
464
465   virtual bool SetValueFromCString(const char *value_str, Error &error);
466
467   // Return the module associated with this value object in case the
468   // value is from an executable file and might have its data in
469   // sections of the file. This can be used for variables.
470   virtual lldb::ModuleSP GetModule();
471
472   ValueObject *GetRoot();
473
474   // Given a ValueObject, loop over itself and its parent, and its parent's
475   // parent, ..
476   // until either the given callback returns false, or you end up at a null
477   // pointer
478   ValueObject *FollowParentChain(std::function<bool(ValueObject *)>);
479
480   virtual bool GetDeclaration(Declaration &decl);
481
482   //------------------------------------------------------------------
483   // The functions below should NOT be modified by subclasses
484   //------------------------------------------------------------------
485   const Error &GetError();
486
487   const ConstString &GetName() const;
488
489   virtual lldb::ValueObjectSP GetChildAtIndex(size_t idx, bool can_create);
490
491   // this will always create the children if necessary
492   lldb::ValueObjectSP
493   GetChildAtIndexPath(const std::initializer_list<size_t> &idxs,
494                       size_t *index_of_error = nullptr);
495
496   lldb::ValueObjectSP GetChildAtIndexPath(const std::vector<size_t> &idxs,
497                                           size_t *index_of_error = nullptr);
498
499   lldb::ValueObjectSP GetChildAtIndexPath(
500       const std::initializer_list<std::pair<size_t, bool>> &idxs,
501       size_t *index_of_error = nullptr);
502
503   lldb::ValueObjectSP
504   GetChildAtIndexPath(const std::vector<std::pair<size_t, bool>> &idxs,
505                       size_t *index_of_error = nullptr);
506
507   // this will always create the children if necessary
508   lldb::ValueObjectSP
509   GetChildAtNamePath(const std::initializer_list<ConstString> &names,
510                      ConstString *name_of_error = nullptr);
511
512   lldb::ValueObjectSP GetChildAtNamePath(const std::vector<ConstString> &names,
513                                          ConstString *name_of_error = nullptr);
514
515   lldb::ValueObjectSP GetChildAtNamePath(
516       const std::initializer_list<std::pair<ConstString, bool>> &names,
517       ConstString *name_of_error = nullptr);
518
519   lldb::ValueObjectSP
520   GetChildAtNamePath(const std::vector<std::pair<ConstString, bool>> &names,
521                      ConstString *name_of_error = nullptr);
522
523   virtual lldb::ValueObjectSP GetChildMemberWithName(const ConstString &name,
524                                                      bool can_create);
525
526   virtual size_t GetIndexOfChildWithName(const ConstString &name);
527
528   size_t GetNumChildren(uint32_t max = UINT32_MAX);
529
530   const Value &GetValue() const;
531
532   Value &GetValue();
533
534   virtual bool ResolveValue(Scalar &scalar);
535
536   // return 'false' whenever you set the error, otherwise
537   // callers may assume true means everything is OK - this will
538   // break breakpoint conditions among potentially a few others
539   virtual bool IsLogicalTrue(Error &error);
540
541   virtual const char *GetLocationAsCString();
542
543   const char *
544   GetSummaryAsCString(lldb::LanguageType lang = lldb::eLanguageTypeUnknown);
545
546   bool
547   GetSummaryAsCString(TypeSummaryImpl *summary_ptr, std::string &destination,
548                       lldb::LanguageType lang = lldb::eLanguageTypeUnknown);
549
550   bool GetSummaryAsCString(std::string &destination,
551                            const TypeSummaryOptions &options);
552
553   bool GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
554                            std::string &destination,
555                            const TypeSummaryOptions &options);
556
557   std::pair<TypeValidatorResult, std::string> GetValidationStatus();
558
559   const char *GetObjectDescription();
560
561   bool HasSpecialPrintableRepresentation(
562       ValueObjectRepresentationStyle val_obj_display,
563       lldb::Format custom_format);
564
565   enum class PrintableRepresentationSpecialCases : bool {
566     eDisable = false,
567     eAllow = true
568   };
569
570   bool
571   DumpPrintableRepresentation(Stream &s,
572                               ValueObjectRepresentationStyle val_obj_display =
573                                   eValueObjectRepresentationStyleSummary,
574                               lldb::Format custom_format = lldb::eFormatInvalid,
575                               PrintableRepresentationSpecialCases special =
576                                   PrintableRepresentationSpecialCases::eAllow,
577                               bool do_dump_error = true);
578   bool GetValueIsValid() const;
579
580   // If you call this on a newly created ValueObject, it will always return
581   // false.
582   bool GetValueDidChange();
583
584   bool UpdateValueIfNeeded(bool update_format = true);
585
586   bool UpdateFormatsIfNeeded();
587
588   lldb::ValueObjectSP GetSP() { return m_manager->GetSharedPointer(this); }
589
590   // Change the name of the current ValueObject. Should *not* be used from a
591   // synthetic child provider as it would change the name of the non synthetic
592   // child as well.
593   void SetName(const ConstString &name);
594
595   virtual lldb::addr_t GetAddressOf(bool scalar_is_load_address = true,
596                                     AddressType *address_type = nullptr);
597
598   lldb::addr_t GetPointerValue(AddressType *address_type = nullptr);
599
600   lldb::ValueObjectSP GetSyntheticChild(const ConstString &key) const;
601
602   lldb::ValueObjectSP GetSyntheticArrayMember(size_t index, bool can_create);
603
604   lldb::ValueObjectSP GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
605                                                 bool can_create);
606
607   lldb::ValueObjectSP GetSyntheticExpressionPathChild(const char *expression,
608                                                       bool can_create);
609
610   virtual lldb::ValueObjectSP
611   GetSyntheticChildAtOffset(uint32_t offset, const CompilerType &type,
612                             bool can_create,
613                             ConstString name_const_str = ConstString());
614
615   virtual lldb::ValueObjectSP
616   GetSyntheticBase(uint32_t offset, const CompilerType &type, bool can_create,
617                    ConstString name_const_str = ConstString());
618
619   virtual lldb::ValueObjectSP GetDynamicValue(lldb::DynamicValueType valueType);
620
621   lldb::DynamicValueType GetDynamicValueType();
622
623   virtual lldb::ValueObjectSP GetStaticValue();
624
625   virtual lldb::ValueObjectSP GetNonSyntheticValue();
626
627   lldb::ValueObjectSP GetSyntheticValue(bool use_synthetic = true);
628
629   virtual bool HasSyntheticValue();
630
631   virtual bool IsSynthetic() { return false; }
632
633   lldb::ValueObjectSP
634   GetQualifiedRepresentationIfAvailable(lldb::DynamicValueType dynValue,
635                                         bool synthValue);
636
637   virtual lldb::ValueObjectSP CreateConstantValue(const ConstString &name);
638
639   virtual lldb::ValueObjectSP Dereference(Error &error);
640
641   // Creates a copy of the ValueObject with a new name and setting the current
642   // ValueObject as its parent. It should be used when we want to change the
643   // name of a ValueObject without modifying the actual ValueObject itself
644   // (e.g. sythetic child provider).
645   virtual lldb::ValueObjectSP Clone(const ConstString &new_name);
646
647   virtual lldb::ValueObjectSP AddressOf(Error &error);
648
649   virtual lldb::addr_t GetLiveAddress() { return LLDB_INVALID_ADDRESS; }
650
651   virtual void SetLiveAddress(lldb::addr_t addr = LLDB_INVALID_ADDRESS,
652                               AddressType address_type = eAddressTypeLoad) {}
653
654   // Find the address of the C++ vtable pointer
655   virtual lldb::addr_t GetCPPVTableAddress(AddressType &address_type);
656
657   virtual lldb::ValueObjectSP Cast(const CompilerType &compiler_type);
658
659   virtual lldb::ValueObjectSP CastPointerType(const char *name,
660                                               CompilerType &ast_type);
661
662   virtual lldb::ValueObjectSP CastPointerType(const char *name,
663                                               lldb::TypeSP &type_sp);
664
665   // The backing bits of this value object were updated, clear any
666   // descriptive string, so we know we have to refetch them
667   virtual void ValueUpdated() {
668     ClearUserVisibleData(eClearUserVisibleDataItemsValue |
669                          eClearUserVisibleDataItemsSummary |
670                          eClearUserVisibleDataItemsDescription);
671   }
672
673   virtual bool IsDynamic() { return false; }
674
675   virtual bool DoesProvideSyntheticValue() { return false; }
676
677   virtual bool IsSyntheticChildrenGenerated();
678
679   virtual void SetSyntheticChildrenGenerated(bool b);
680
681   virtual SymbolContextScope *GetSymbolContextScope();
682
683   void Dump(Stream &s);
684
685   void Dump(Stream &s, const DumpValueObjectOptions &options);
686
687   static lldb::ValueObjectSP
688   CreateValueObjectFromExpression(llvm::StringRef name,
689                                   llvm::StringRef expression,
690                                   const ExecutionContext &exe_ctx);
691
692   static lldb::ValueObjectSP
693   CreateValueObjectFromExpression(llvm::StringRef name,
694                                   llvm::StringRef expression,
695                                   const ExecutionContext &exe_ctx,
696                                   const EvaluateExpressionOptions &options);
697
698   static lldb::ValueObjectSP
699   CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address,
700                                const ExecutionContext &exe_ctx,
701                                CompilerType type);
702
703   static lldb::ValueObjectSP
704   CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data,
705                             const ExecutionContext &exe_ctx, CompilerType type);
706
707   void LogValueObject(Log *log);
708
709   void LogValueObject(Log *log, const DumpValueObjectOptions &options);
710
711   lldb::ValueObjectSP Persist();
712
713   // returns true if this is a char* or a char[]
714   // if it is a char* and check_pointer is true,
715   // it also checks that the pointer is valid
716   bool IsCStringContainer(bool check_pointer = false);
717
718   std::pair<size_t, bool>
719   ReadPointedString(lldb::DataBufferSP &buffer_sp, Error &error,
720                     uint32_t max_length = 0, bool honor_array = true,
721                     lldb::Format item_format = lldb::eFormatCharArray);
722
723   virtual size_t GetPointeeData(DataExtractor &data, uint32_t item_idx = 0,
724                                 uint32_t item_count = 1);
725
726   virtual uint64_t GetData(DataExtractor &data, Error &error);
727
728   virtual bool SetData(DataExtractor &data, Error &error);
729
730   virtual bool GetIsConstant() const { return m_update_point.IsConstant(); }
731
732   bool NeedsUpdating() {
733     const bool accept_invalid_exe_ctx =
734         (CanUpdateWithInvalidExecutionContext() == eLazyBoolYes);
735     return m_update_point.NeedsUpdating(accept_invalid_exe_ctx);
736   }
737
738   void SetIsConstant() { m_update_point.SetIsConstant(); }
739
740   lldb::Format GetFormat() const;
741
742   virtual void SetFormat(lldb::Format format) {
743     if (format != m_format)
744       ClearUserVisibleData(eClearUserVisibleDataItemsValue);
745     m_format = format;
746   }
747
748   virtual lldb::LanguageType GetPreferredDisplayLanguage();
749
750   void SetPreferredDisplayLanguage(lldb::LanguageType);
751
752   lldb::TypeSummaryImplSP GetSummaryFormat() {
753     UpdateFormatsIfNeeded();
754     return m_type_summary_sp;
755   }
756
757   void SetSummaryFormat(lldb::TypeSummaryImplSP format) {
758     m_type_summary_sp = format;
759     ClearUserVisibleData(eClearUserVisibleDataItemsSummary);
760   }
761
762   lldb::TypeValidatorImplSP GetValidator() {
763     UpdateFormatsIfNeeded();
764     return m_type_validator_sp;
765   }
766
767   void SetValidator(lldb::TypeValidatorImplSP format) {
768     m_type_validator_sp = format;
769     ClearUserVisibleData(eClearUserVisibleDataItemsValidator);
770   }
771
772   void SetValueFormat(lldb::TypeFormatImplSP format) {
773     m_type_format_sp = format;
774     ClearUserVisibleData(eClearUserVisibleDataItemsValue);
775   }
776
777   lldb::TypeFormatImplSP GetValueFormat() {
778     UpdateFormatsIfNeeded();
779     return m_type_format_sp;
780   }
781
782   void SetSyntheticChildren(const lldb::SyntheticChildrenSP &synth_sp) {
783     if (synth_sp.get() == m_synthetic_children_sp.get())
784       return;
785     ClearUserVisibleData(eClearUserVisibleDataItemsSyntheticChildren);
786     m_synthetic_children_sp = synth_sp;
787   }
788
789   lldb::SyntheticChildrenSP GetSyntheticChildren() {
790     UpdateFormatsIfNeeded();
791     return m_synthetic_children_sp;
792   }
793
794   // Use GetParent for display purposes, but if you want to tell the parent to
795   // update itself
796   // then use m_parent.  The ValueObjectDynamicValue's parent is not the correct
797   // parent for
798   // displaying, they are really siblings, so for display it needs to route
799   // through to its grandparent.
800   virtual ValueObject *GetParent() { return m_parent; }
801
802   virtual const ValueObject *GetParent() const { return m_parent; }
803
804   ValueObject *GetNonBaseClassParent();
805
806   void SetAddressTypeOfChildren(AddressType at) {
807     m_address_type_of_ptr_or_ref_children = at;
808   }
809
810   AddressType GetAddressTypeOfChildren();
811
812   void SetHasCompleteType() { m_did_calculate_complete_objc_class_type = true; }
813
814   //------------------------------------------------------------------
815   /// Find out if a ValueObject might have children.
816   ///
817   /// This call is much more efficient than CalculateNumChildren() as
818   /// it doesn't need to complete the underlying type. This is designed
819   /// to be used in a UI environment in order to detect if the
820   /// disclosure triangle should be displayed or not.
821   ///
822   /// This function returns true for class, union, structure,
823   /// pointers, references, arrays and more. Again, it does so without
824   /// doing any expensive type completion.
825   ///
826   /// @return
827   ///     Returns \b true if the ValueObject might have children, or \b
828   ///     false otherwise.
829   //------------------------------------------------------------------
830   virtual bool MightHaveChildren();
831
832   virtual lldb::VariableSP GetVariable() { return nullptr; }
833
834   virtual bool IsRuntimeSupportValue();
835
836   virtual uint64_t GetLanguageFlags();
837
838   virtual void SetLanguageFlags(uint64_t flags);
839
840 protected:
841   typedef ClusterManager<ValueObject> ValueObjectManager;
842
843   class ChildrenManager {
844   public:
845     ChildrenManager() : m_mutex(), m_children(), m_children_count(0) {}
846
847     bool HasChildAtIndex(size_t idx) {
848       std::lock_guard<std::recursive_mutex> guard(m_mutex);
849       return (m_children.find(idx) != m_children.end());
850     }
851
852     ValueObject *GetChildAtIndex(size_t idx) {
853       std::lock_guard<std::recursive_mutex> guard(m_mutex);
854       const auto iter = m_children.find(idx);
855       return ((iter == m_children.end()) ? nullptr : iter->second);
856     }
857
858     void SetChildAtIndex(size_t idx, ValueObject *valobj) {
859       // we do not need to be mutex-protected to make a pair
860       ChildrenPair pair(idx, valobj);
861       std::lock_guard<std::recursive_mutex> guard(m_mutex);
862       m_children.insert(pair);
863     }
864
865     void SetChildrenCount(size_t count) { Clear(count); }
866
867     size_t GetChildrenCount() { return m_children_count; }
868
869     void Clear(size_t new_count = 0) {
870       std::lock_guard<std::recursive_mutex> guard(m_mutex);
871       m_children_count = new_count;
872       m_children.clear();
873     }
874
875   private:
876     typedef std::map<size_t, ValueObject *> ChildrenMap;
877     typedef ChildrenMap::iterator ChildrenIterator;
878     typedef ChildrenMap::value_type ChildrenPair;
879     std::recursive_mutex m_mutex;
880     ChildrenMap m_children;
881     size_t m_children_count;
882   };
883
884   //------------------------------------------------------------------
885   // Classes that inherit from ValueObject can see and modify these
886   //------------------------------------------------------------------
887   ValueObject
888       *m_parent; // The parent value object, or nullptr if this has no parent
889   ValueObject *m_root; // The root of the hierarchy for this ValueObject (or
890                        // nullptr if never calculated)
891   EvaluationPoint m_update_point; // Stores both the stop id and the full
892                                   // context at which this value was last
893   // updated.  When we are asked to update the value object, we check whether
894   // the context & stop id are the same before updating.
895   ConstString m_name; // The name of this object
896   DataExtractor
897       m_data; // A data extractor that can be used to extract the value.
898   Value m_value;
899   Error m_error; // An error object that can describe any errors that occur when
900                  // updating values.
901   std::string m_value_str; // Cached value string that will get cleared if/when
902                            // the value is updated.
903   std::string m_old_value_str; // Cached old value string from the last time the
904                                // value was gotten
905   std::string m_location_str;  // Cached location string that will get cleared
906                                // if/when the value is updated.
907   std::string m_summary_str;   // Cached summary string that will get cleared
908                                // if/when the value is updated.
909   std::string m_object_desc_str; // Cached result of the "object printer".  This
910                                  // differs from the summary
911   // in that the summary is consed up by us, the object_desc_string is builtin.
912
913   llvm::Optional<std::pair<TypeValidatorResult, std::string>>
914       m_validation_result;
915
916   CompilerType m_override_type; // If the type of the value object should be
917                                 // overridden, the type to impose.
918
919   ValueObjectManager *m_manager; // This object is managed by the root object
920                                  // (any ValueObject that gets created
921   // without a parent.)  The manager gets passed through all the generations of
922   // dependent objects, and will keep the whole cluster of objects alive as long
923   // as a shared pointer to any of them has been handed out.  Shared pointers to
924   // value objects must always be made with the GetSP method.
925
926   ChildrenManager m_children;
927   std::map<ConstString, ValueObject *> m_synthetic_children;
928
929   ValueObject *m_dynamic_value;
930   ValueObject *m_synthetic_value;
931   ValueObject *m_deref_valobj;
932
933   lldb::ValueObjectSP m_addr_of_valobj_sp; // We have to hold onto a shared
934                                            // pointer to this one because it is
935                                            // created
936   // as an independent ValueObjectConstResult, which isn't managed by us.
937
938   lldb::Format m_format;
939   lldb::Format m_last_format;
940   uint32_t m_last_format_mgr_revision;
941   lldb::TypeSummaryImplSP m_type_summary_sp;
942   lldb::TypeFormatImplSP m_type_format_sp;
943   lldb::SyntheticChildrenSP m_synthetic_children_sp;
944   lldb::TypeValidatorImplSP m_type_validator_sp;
945   ProcessModID m_user_id_of_forced_summary;
946   AddressType m_address_type_of_ptr_or_ref_children;
947
948   llvm::SmallVector<uint8_t, 16> m_value_checksum;
949
950   lldb::LanguageType m_preferred_display_language;
951
952   uint64_t m_language_flags;
953
954   bool m_value_is_valid : 1, m_value_did_change : 1, m_children_count_valid : 1,
955       m_old_value_valid : 1, m_is_deref_of_parent : 1,
956       m_is_array_item_for_pointer : 1, m_is_bitfield_for_scalar : 1,
957       m_is_child_at_offset : 1, m_is_getting_summary : 1,
958       m_did_calculate_complete_objc_class_type : 1,
959       m_is_synthetic_children_generated : 1;
960
961   friend class ValueObjectChild;
962   friend class ClangExpressionDeclMap; // For GetValue
963   friend class ExpressionVariable;     // For SetName
964   friend class Target;                 // For SetName
965   friend class ValueObjectConstResultImpl;
966   friend class ValueObjectSynthetic; // For ClearUserVisibleData
967
968   //------------------------------------------------------------------
969   // Constructors and Destructors
970   //------------------------------------------------------------------
971
972   // Use the no-argument constructor to make a constant variable object (with no
973   // ExecutionContextScope.)
974
975   ValueObject();
976
977   // Use this constructor to create a "root variable object".  The ValueObject
978   // will be locked to this context
979   // through-out its lifespan.
980
981   ValueObject(ExecutionContextScope *exe_scope,
982               AddressType child_ptr_or_ref_addr_type = eAddressTypeLoad);
983
984   // Use this constructor to create a ValueObject owned by another ValueObject.
985   // It will inherit the ExecutionContext
986   // of its parent.
987
988   ValueObject(ValueObject &parent);
989
990   ValueObjectManager *GetManager() { return m_manager; }
991
992   virtual bool UpdateValue() = 0;
993
994   virtual LazyBool CanUpdateWithInvalidExecutionContext() {
995     return eLazyBoolCalculate;
996   }
997
998   virtual void CalculateDynamicValue(lldb::DynamicValueType use_dynamic);
999
1000   virtual lldb::DynamicValueType GetDynamicValueTypeImpl() {
1001     return lldb::eNoDynamicValues;
1002   }
1003
1004   virtual bool HasDynamicValueTypeInfo() { return false; }
1005
1006   virtual void CalculateSyntheticValue(bool use_synthetic = true);
1007
1008   // Should only be called by ValueObject::GetChildAtIndex()
1009   // Returns a ValueObject managed by this ValueObject's manager.
1010   virtual ValueObject *CreateChildAtIndex(size_t idx,
1011                                           bool synthetic_array_member,
1012                                           int32_t synthetic_index);
1013
1014   // Should only be called by ValueObject::GetNumChildren()
1015   virtual size_t CalculateNumChildren(uint32_t max = UINT32_MAX) = 0;
1016
1017   void SetNumChildren(size_t num_children);
1018
1019   void SetValueDidChange(bool value_changed);
1020
1021   void SetValueIsValid(bool valid);
1022
1023   void ClearUserVisibleData(
1024       uint32_t items = ValueObject::eClearUserVisibleDataItemsAllStrings);
1025
1026   void AddSyntheticChild(const ConstString &key, ValueObject *valobj);
1027
1028   DataExtractor &GetDataExtractor();
1029
1030   void ClearDynamicTypeInformation();
1031
1032   //------------------------------------------------------------------
1033   // Subclasses must implement the functions below.
1034   //------------------------------------------------------------------
1035
1036   virtual CompilerType GetCompilerTypeImpl() = 0;
1037
1038   const char *GetLocationAsCStringImpl(const Value &value,
1039                                        const DataExtractor &data);
1040
1041   bool IsChecksumEmpty();
1042
1043   void SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType);
1044
1045 private:
1046   virtual CompilerType MaybeCalculateCompleteType();
1047
1048   lldb::ValueObjectSP GetValueForExpressionPath_Impl(
1049       llvm::StringRef expression_cstr,
1050       ExpressionPathScanEndReason *reason_to_stop,
1051       ExpressionPathEndResultType *final_value_type,
1052       const GetValueForExpressionPathOptions &options,
1053       ExpressionPathAftermath *final_task_on_target);
1054
1055   DISALLOW_COPY_AND_ASSIGN(ValueObject);
1056 };
1057
1058 //------------------------------------------------------------------------------
1059 // A value object manager class that is seeded with the static variable value
1060 // and it vends the user facing value object. If the type is dynamic it can
1061 // vend the dynamic type. If this user type also has a synthetic type associated
1062 // with it, it will vend the synthetic type. The class watches the process' stop
1063 // ID and will update the user type when needed.
1064 //------------------------------------------------------------------------------
1065 class ValueObjectManager {
1066   // The root value object is the static typed variable object.
1067   lldb::ValueObjectSP m_root_valobj_sp;
1068   // The user value object is the value object the user wants to see.
1069   lldb::ValueObjectSP m_user_valobj_sp;
1070   lldb::DynamicValueType m_use_dynamic;
1071   uint32_t m_stop_id; // The stop ID that m_user_valobj_sp is valid for.
1072   bool m_use_synthetic;
1073
1074 public:
1075   ValueObjectManager() {}
1076   
1077   ValueObjectManager(lldb::ValueObjectSP in_valobj_sp,
1078                      lldb::DynamicValueType use_dynamic, bool use_synthetic);
1079   
1080   bool IsValid() const;
1081   
1082   lldb::ValueObjectSP GetRootSP() const { return m_root_valobj_sp; }
1083   
1084   // Gets the correct value object from the root object for a given process
1085   // stop ID. If dynamic values are enabled, or if synthetic children are
1086   // enabled, the value object that the user wants to see might change while
1087   // debugging.
1088   lldb::ValueObjectSP GetSP();
1089   
1090   void SetUseDynamic(lldb::DynamicValueType use_dynamic);
1091   void SetUseSynthetic(bool use_synthetic);
1092   lldb::DynamicValueType GetUseDynamic() const { return m_use_dynamic; }
1093   bool GetUseSynthetic() const { return m_use_synthetic; }
1094   lldb::TargetSP GetTargetSP() const;
1095   lldb::ProcessSP GetProcessSP() const;
1096   lldb::ThreadSP GetThreadSP() const;
1097   lldb::StackFrameSP GetFrameSP() const;
1098 };
1099
1100 } // namespace lldb_private
1101
1102 #endif // liblldb_ValueObject_h_