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