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