1 //===-- SBValue.h -----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_API_SBVALUE_H
10 #define LLDB_API_SBVALUE_H
11 
12 #include "lldb/API/SBData.h"
13 #include "lldb/API/SBDefines.h"
14 #include "lldb/API/SBType.h"
15 
16 class ValueImpl;
17 class ValueLocker;
18 
19 namespace lldb_private {
20 namespace python {
21 class SWIGBridge;
22 }
23 } // namespace lldb_private
24 
25 namespace lldb {
26 
27 class LLDB_API SBValue {
28 public:
29   SBValue();
30 
31   SBValue(const lldb::SBValue &rhs);
32 
33   lldb::SBValue &operator=(const lldb::SBValue &rhs);
34 
35   ~SBValue();
36 
37   explicit operator bool() const;
38 
39   bool IsValid();
40 
41   void Clear();
42 
43   SBError GetError();
44 
45   lldb::user_id_t GetID();
46 
47   const char *GetName();
48 
49   const char *GetTypeName();
50 
51   const char *GetDisplayTypeName();
52 
53   size_t GetByteSize();
54 
55   bool IsInScope();
56 
57   lldb::Format GetFormat();
58 
59   void SetFormat(lldb::Format format);
60 
61   const char *GetValue();
62 
63   int64_t GetValueAsSigned(lldb::SBError &error, int64_t fail_value = 0);
64 
65   uint64_t GetValueAsUnsigned(lldb::SBError &error, uint64_t fail_value = 0);
66 
67   int64_t GetValueAsSigned(int64_t fail_value = 0);
68 
69   uint64_t GetValueAsUnsigned(uint64_t fail_value = 0);
70 
71   lldb::addr_t GetValueAsAddress();
72 
73   ValueType GetValueType();
74 
75   // If you call this on a newly created ValueObject, it will always return
76   // false.
77   bool GetValueDidChange();
78 
79   const char *GetSummary();
80 
81   const char *GetSummary(lldb::SBStream &stream,
82                          lldb::SBTypeSummaryOptions &options);
83 
84   const char *GetObjectDescription();
85 
86   lldb::SBValue GetDynamicValue(lldb::DynamicValueType use_dynamic);
87 
88   lldb::SBValue GetStaticValue();
89 
90   lldb::SBValue GetNonSyntheticValue();
91 
92   lldb::DynamicValueType GetPreferDynamicValue();
93 
94   void SetPreferDynamicValue(lldb::DynamicValueType use_dynamic);
95 
96   bool GetPreferSyntheticValue();
97 
98   void SetPreferSyntheticValue(bool use_synthetic);
99 
100   bool IsDynamic();
101 
102   bool IsSynthetic();
103 
104   bool IsSyntheticChildrenGenerated();
105 
106   void SetSyntheticChildrenGenerated(bool);
107 
108   const char *GetLocation();
109 
110   LLDB_DEPRECATED_FIXME("Use the variant that takes an SBError &",
111                         "SetValueFromCString(const char *, SBError &)")
112   bool SetValueFromCString(const char *value_str);
113 
114   bool SetValueFromCString(const char *value_str, lldb::SBError &error);
115 
116   lldb::SBTypeFormat GetTypeFormat();
117 
118   lldb::SBTypeSummary GetTypeSummary();
119 
120   lldb::SBTypeFilter GetTypeFilter();
121 
122   lldb::SBTypeSynthetic GetTypeSynthetic();
123 
124   lldb::SBValue GetChildAtIndex(uint32_t idx);
125 
126   lldb::SBValue CreateChildAtOffset(const char *name, uint32_t offset,
127                                     lldb::SBType type);
128 
129   LLDB_DEPRECATED("Use the expression evaluator to perform type casting")
130   lldb::SBValue Cast(lldb::SBType type);
131 
132   lldb::SBValue CreateValueFromExpression(const char *name,
133                                           const char *expression);
134 
135   lldb::SBValue CreateValueFromExpression(const char *name,
136                                           const char *expression,
137                                           SBExpressionOptions &options);
138 
139   lldb::SBValue CreateValueFromAddress(const char *name, lldb::addr_t address,
140                                        lldb::SBType type);
141 
142   // this has no address! GetAddress() and GetLoadAddress() as well as
143   // AddressOf() on the return of this call all return invalid
144   lldb::SBValue CreateValueFromData(const char *name, lldb::SBData data,
145                                     lldb::SBType type);
146 
147   /// Get a child value by index from a value.
148   ///
149   /// Structs, unions, classes, arrays and pointers have child
150   /// values that can be access by index.
151   ///
152   /// Structs and unions access child members using a zero based index
153   /// for each child member. For
154   ///
155   /// Classes reserve the first indexes for base classes that have
156   /// members (empty base classes are omitted), and all members of the
157   /// current class will then follow the base classes.
158   ///
159   /// Pointers differ depending on what they point to. If the pointer
160   /// points to a simple type, the child at index zero
161   /// is the only child value available, unless \a synthetic_allowed
162   /// is \b true, in which case the pointer will be used as an array
163   /// and can create 'synthetic' child values using positive or
164   /// negative indexes. If the pointer points to an aggregate type
165   /// (an array, class, union, struct), then the pointee is
166   /// transparently skipped and any children are going to be the indexes
167   /// of the child values within the aggregate type. For example if
168   /// we have a 'Point' type and we have a SBValue that contains a
169   /// pointer to a 'Point' type, then the child at index zero will be
170   /// the 'x' member, and the child at index 1 will be the 'y' member
171   /// (the child at index zero won't be a 'Point' instance).
172   ///
173   /// If you actually need an SBValue that represents the type pointed
174   /// to by a SBValue for which GetType().IsPointeeType() returns true,
175   /// regardless of the pointee type, you can do that with SBValue::Dereference.
176   ///
177   /// Arrays have a preset number of children that can be accessed by
178   /// index and will returns invalid child values for indexes that are
179   /// out of bounds unless the \a synthetic_allowed is \b true. In this
180   /// case the array can create 'synthetic' child values for indexes
181   /// that aren't in the array bounds using positive or negative
182   /// indexes.
183   ///
184   /// \param[in] idx
185   ///     The index of the child value to get
186   ///
187   /// \param[in] use_dynamic
188   ///     An enumeration that specifies whether to get dynamic values,
189   ///     and also if the target can be run to figure out the dynamic
190   ///     type of the child value.
191   ///
192   /// \param[in] can_create_synthetic
193   ///     If \b true, then allow child values to be created by index
194   ///     for pointers and arrays for indexes that normally wouldn't
195   ///     be allowed.
196   ///
197   /// \return
198   ///     A new SBValue object that represents the child member value.
199   lldb::SBValue GetChildAtIndex(uint32_t idx,
200                                 lldb::DynamicValueType use_dynamic,
201                                 bool can_create_synthetic);
202 
203   // Matches children of this object only and will match base classes and
204   // member names if this is a clang typed object.
205   uint32_t GetIndexOfChildWithName(const char *name);
206 
207   // Matches child members of this object and child members of any base
208   // classes.
209   lldb::SBValue GetChildMemberWithName(const char *name);
210 
211   // Matches child members of this object and child members of any base
212   // classes.
213   lldb::SBValue GetChildMemberWithName(const char *name,
214                                        lldb::DynamicValueType use_dynamic);
215 
216   // Expands nested expressions like .a->b[0].c[1]->d
217   lldb::SBValue GetValueForExpressionPath(const char *expr_path);
218 
219   lldb::SBValue AddressOf();
220 
221   lldb::addr_t GetLoadAddress();
222 
223   lldb::SBAddress GetAddress();
224 
225   /// Get an SBData wrapping what this SBValue points to.
226   ///
227   /// This method will dereference the current SBValue, if its
228   /// data type is a T* or T[], and extract item_count elements
229   /// of type T from it, copying their contents in an SBData.
230   ///
231   /// \param[in] item_idx
232   ///     The index of the first item to retrieve. For an array
233   ///     this is equivalent to array[item_idx], for a pointer
234   ///     to *(pointer + item_idx). In either case, the measurement
235   ///     unit for item_idx is the sizeof(T) rather than the byte
236   ///
237   /// \param[in] item_count
238   ///     How many items should be copied into the output. By default
239   ///     only one item is copied, but more can be asked for.
240   ///
241   /// \return
242   ///     An SBData with the contents of the copied items, on success.
243   ///     An empty SBData otherwise.
244   lldb::SBData GetPointeeData(uint32_t item_idx = 0, uint32_t item_count = 1);
245 
246   /// Get an SBData wrapping the contents of this SBValue.
247   ///
248   /// This method will read the contents of this object in memory
249   /// and copy them into an SBData for future use.
250   ///
251   /// \return
252   ///     An SBData with the contents of this SBValue, on success.
253   ///     An empty SBData otherwise.
254   lldb::SBData GetData();
255 
256   bool SetData(lldb::SBData &data, lldb::SBError &error);
257 
258   /// Creates a copy of the SBValue with a new name and setting the current
259   /// SBValue as its parent. It should be used when we want to change the
260   /// name of a SBValue without modifying the actual SBValue itself
261   /// (e.g. sythetic child provider).
262   lldb::SBValue Clone(const char *new_name);
263 
264   lldb::SBDeclaration GetDeclaration();
265 
266   /// Find out if a SBValue might have children.
267   ///
268   /// This call is much more efficient than GetNumChildren() as it
269   /// doesn't need to complete the underlying type. This is designed
270   /// to be used in a UI environment in order to detect if the
271   /// disclosure triangle should be displayed or not.
272   ///
273   /// This function returns true for class, union, structure,
274   /// pointers, references, arrays and more. Again, it does so without
275   /// doing any expensive type completion.
276   ///
277   /// \return
278   ///     Returns \b true if the SBValue might have children, or \b
279   ///     false otherwise.
280   bool MightHaveChildren();
281 
282   bool IsRuntimeSupportValue();
283 
284   uint32_t GetNumChildren();
285 
286   uint32_t GetNumChildren(uint32_t max);
287 
288   LLDB_DEPRECATED("SBValue::GetOpaqueType() is deprecated.")
289   void *GetOpaqueType();
290 
291   lldb::SBTarget GetTarget();
292 
293   lldb::SBProcess GetProcess();
294 
295   lldb::SBThread GetThread();
296 
297   lldb::SBFrame GetFrame();
298 
299   lldb::SBValue Dereference();
300 
301   LLDB_DEPRECATED("Use GetType().IsPointerType() instead")
302   bool TypeIsPointerType();
303 
304   lldb::SBType GetType();
305 
306   lldb::SBValue Persist();
307 
308   bool GetDescription(lldb::SBStream &description);
309 
310   bool GetExpressionPath(lldb::SBStream &description);
311 
312   bool GetExpressionPath(lldb::SBStream &description,
313                          bool qualify_cxx_base_classes);
314 
315   lldb::SBValue EvaluateExpression(const char *expr) const;
316   lldb::SBValue EvaluateExpression(const char *expr,
317                                    const SBExpressionOptions &options) const;
318   lldb::SBValue EvaluateExpression(const char *expr,
319                                    const SBExpressionOptions &options,
320                                    const char *name) const;
321 
322   /// Watch this value if it resides in memory.
323   ///
324   /// Sets a watchpoint on the value.
325   ///
326   /// \param[in] resolve_location
327   ///     Resolve the location of this value once and watch its address.
328   ///     This value must currently be set to \b true as watching all
329   ///     locations of a variable or a variable path is not yet supported,
330   ///     though we plan to support it in the future.
331   ///
332   /// \param[in] read
333   ///     Stop when this value is accessed.
334   ///
335   /// \param[in] write
336   ///     Stop when this value is modified
337   ///
338   /// \param[out] error
339   ///     An error object. Contains the reason if there is some failure.
340   ///
341   /// \return
342   ///     An SBWatchpoint object. This object might not be valid upon
343   ///     return due to a value not being contained in memory, too
344   ///     large, or watchpoint resources are not available or all in
345   ///     use.
346   lldb::SBWatchpoint Watch(bool resolve_location, bool read, bool write,
347                            SBError &error);
348 
349   // Backward compatibility fix in the interim.
350   lldb::SBWatchpoint Watch(bool resolve_location, bool read, bool write);
351 
352   /// Watch this value that this value points to in memory
353   ///
354   /// Sets a watchpoint on the value.
355   ///
356   /// \param[in] resolve_location
357   ///     Resolve the location of this value once and watch its address.
358   ///     This value must currently be set to \b true as watching all
359   ///     locations of a variable or a variable path is not yet supported,
360   ///     though we plan to support it in the future.
361   ///
362   /// \param[in] read
363   ///     Stop when this value is accessed.
364   ///
365   /// \param[in] write
366   ///     Stop when this value is modified
367   ///
368   /// \param[out] error
369   ///     An error object. Contains the reason if there is some failure.
370   ///
371   /// \return
372   ///     An SBWatchpoint object. This object might not be valid upon
373   ///     return due to a value not being contained in memory, too
374   ///     large, or watchpoint resources are not available or all in
375   ///     use.
376   lldb::SBWatchpoint WatchPointee(bool resolve_location, bool read, bool write,
377                                   SBError &error);
378 
379   /// If this value represents a C++ class that has a vtable, return an value
380   /// that represents the virtual function table.
381   ///
382   /// SBValue::GetError() will be in the success state if this value represents
383   /// a C++ class with a vtable, or an appropriate error describing that the
384   /// object isn't a C++ class with a vtable or not a C++ class.
385   ///
386   /// SBValue::GetName() will be the demangled symbol name for the virtual
387   /// function table like "vtable for <classname>".
388   ///
389   /// SBValue::GetValue() will be the address of the first vtable entry if the
390   /// current SBValue is a class with a vtable, or nothing the current SBValue
391   /// is not a C++ class or not a C++ class that has a vtable.
392   ///
393   /// SBValue::GetValueAtUnsigned(...) will return the address of the first
394   /// vtable entry.
395   ///
396   /// SBValue::GetLoadAddress() will return the address of the vtable pointer
397   /// found in the parent SBValue.
398   ///
399   /// SBValue::GetNumChildren() will return the number of virtual function
400   /// pointers in the vtable, or zero on error.
401   ///
402   /// SBValue::GetChildAtIndex(...) will return each virtual function pointer
403   /// as a SBValue object.
404   ///
405   /// The child SBValue objects will have the following values:
406   ///
407   /// SBValue::GetError() will indicate success if the vtable entry was
408   /// successfully read from memory, or an error if not.
409   ///
410   /// SBValue::GetName() will be the vtable function index in the form "[%u]"
411   /// where %u is the index.
412   ///
413   /// SBValue::GetValue() will be the virtual function pointer value as a
414   /// string.
415   ///
416   /// SBValue::GetValueAtUnsigned(...) will return the virtual function
417   /// pointer value.
418   ///
419   /// SBValue::GetLoadAddress() will return the address of the virtual function
420   /// pointer.
421   ///
422   /// SBValue::GetNumChildren() returns 0
423   lldb::SBValue GetVTable();
424 
425 protected:
426   friend class SBBlock;
427   friend class SBFrame;
428   friend class SBModule;
429   friend class SBTarget;
430   friend class SBThread;
431   friend class SBTypeStaticField;
432   friend class SBTypeSummary;
433   friend class SBValueList;
434 
435   friend class lldb_private::python::SWIGBridge;
436 
437   SBValue(const lldb::ValueObjectSP &value_sp);
438 
439   /// Same as the protected version of GetSP that takes a locker, except that we
440   /// make the
441   /// locker locally in the function.  Since the Target API mutex is recursive,
442   /// and the
443   /// StopLocker is a read lock, you can call this function even if you are
444   /// already
445   /// holding the two above-mentioned locks.
446   ///
447   /// \return
448   ///     A ValueObjectSP of the best kind (static, dynamic or synthetic) we
449   ///     can cons up, in accordance with the SBValue's settings.
450   lldb::ValueObjectSP GetSP() const;
451 
452   /// Get the appropriate ValueObjectSP from this SBValue, consulting the
453   /// use_dynamic and use_synthetic options passed in to SetSP when the
454   /// SBValue's contents were set.  Since this often requires examining memory,
455   /// and maybe even running code, it needs to acquire the Target API and
456   /// Process StopLock.
457   /// Those are held in an opaque class ValueLocker which is currently local to
458   /// SBValue.cpp.
459   /// So you don't have to get these yourself just default construct a
460   /// ValueLocker, and pass it into this.
461   /// If we need to make a ValueLocker and use it in some other .cpp file, we'll
462   /// have to move it to
463   /// ValueObject.h/cpp or somewhere else convenient.  We haven't needed to so
464   /// far.
465   ///
466   /// \param[in] value_locker
467   ///     An object that will hold the Target API, and Process RunLocks, and
468   ///     auto-destroy them when it goes out of scope.  Currently this is only
469   ///     useful in
470   ///     SBValue.cpp.
471   ///
472   /// \return
473   ///     A ValueObjectSP of the best kind (static, dynamic or synthetic) we
474   ///     can cons up, in accordance with the SBValue's settings.
475   lldb::ValueObjectSP GetSP(ValueLocker &value_locker) const;
476 
477   // these calls do the right thing WRT adjusting their settings according to
478   // the target's preferences
479   void SetSP(const lldb::ValueObjectSP &sp);
480 
481   void SetSP(const lldb::ValueObjectSP &sp, bool use_synthetic);
482 
483   void SetSP(const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic);
484 
485   void SetSP(const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic,
486              bool use_synthetic);
487 
488   void SetSP(const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic,
489              bool use_synthetic, const char *name);
490 
491 private:
492   typedef std::shared_ptr<ValueImpl> ValueImplSP;
493   ValueImplSP m_opaque_sp;
494 
495   void SetSP(ValueImplSP impl_sp);
496 };
497 
498 } // namespace lldb
499 
500 #endif // LLDB_API_SBVALUE_H
501