SBValue.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #ifndef LLDB_API_SBVALUE_H
  9. #define LLDB_API_SBVALUE_H
  10. #include "lldb/API/SBData.h"
  11. #include "lldb/API/SBDefines.h"
  12. #include "lldb/API/SBType.h"
  13. class ValueImpl;
  14. class ValueLocker;
  15. namespace lldb {
  16. class LLDB_API SBValue {
  17. public:
  18. SBValue();
  19. SBValue(const lldb::SBValue &rhs);
  20. lldb::SBValue &operator=(const lldb::SBValue &rhs);
  21. ~SBValue();
  22. explicit operator bool() const;
  23. bool IsValid();
  24. void Clear();
  25. SBError GetError();
  26. lldb::user_id_t GetID();
  27. const char *GetName();
  28. const char *GetTypeName();
  29. const char *GetDisplayTypeName();
  30. size_t GetByteSize();
  31. bool IsInScope();
  32. lldb::Format GetFormat();
  33. void SetFormat(lldb::Format format);
  34. const char *GetValue();
  35. int64_t GetValueAsSigned(lldb::SBError &error, int64_t fail_value = 0);
  36. uint64_t GetValueAsUnsigned(lldb::SBError &error, uint64_t fail_value = 0);
  37. int64_t GetValueAsSigned(int64_t fail_value = 0);
  38. uint64_t GetValueAsUnsigned(uint64_t fail_value = 0);
  39. ValueType GetValueType();
  40. // If you call this on a newly created ValueObject, it will always return
  41. // false.
  42. bool GetValueDidChange();
  43. const char *GetSummary();
  44. const char *GetSummary(lldb::SBStream &stream,
  45. lldb::SBTypeSummaryOptions &options);
  46. const char *GetObjectDescription();
  47. lldb::SBValue GetDynamicValue(lldb::DynamicValueType use_dynamic);
  48. lldb::SBValue GetStaticValue();
  49. lldb::SBValue GetNonSyntheticValue();
  50. lldb::DynamicValueType GetPreferDynamicValue();
  51. void SetPreferDynamicValue(lldb::DynamicValueType use_dynamic);
  52. bool GetPreferSyntheticValue();
  53. void SetPreferSyntheticValue(bool use_synthetic);
  54. bool IsDynamic();
  55. bool IsSynthetic();
  56. bool IsSyntheticChildrenGenerated();
  57. void SetSyntheticChildrenGenerated(bool);
  58. const char *GetLocation();
  59. // Deprecated - use the one that takes SBError&
  60. bool SetValueFromCString(const char *value_str);
  61. bool SetValueFromCString(const char *value_str, lldb::SBError &error);
  62. lldb::SBTypeFormat GetTypeFormat();
  63. lldb::SBTypeSummary GetTypeSummary();
  64. lldb::SBTypeFilter GetTypeFilter();
  65. lldb::SBTypeSynthetic GetTypeSynthetic();
  66. lldb::SBValue GetChildAtIndex(uint32_t idx);
  67. lldb::SBValue CreateChildAtOffset(const char *name, uint32_t offset,
  68. lldb::SBType type);
  69. // Deprecated - use the expression evaluator to perform type casting
  70. lldb::SBValue Cast(lldb::SBType type);
  71. lldb::SBValue CreateValueFromExpression(const char *name,
  72. const char *expression);
  73. lldb::SBValue CreateValueFromExpression(const char *name,
  74. const char *expression,
  75. SBExpressionOptions &options);
  76. lldb::SBValue CreateValueFromAddress(const char *name, lldb::addr_t address,
  77. lldb::SBType type);
  78. // this has no address! GetAddress() and GetLoadAddress() as well as
  79. // AddressOf() on the return of this call all return invalid
  80. lldb::SBValue CreateValueFromData(const char *name, lldb::SBData data,
  81. lldb::SBType type);
  82. /// Get a child value by index from a value.
  83. ///
  84. /// Structs, unions, classes, arrays and pointers have child
  85. /// values that can be access by index.
  86. ///
  87. /// Structs and unions access child members using a zero based index
  88. /// for each child member. For
  89. ///
  90. /// Classes reserve the first indexes for base classes that have
  91. /// members (empty base classes are omitted), and all members of the
  92. /// current class will then follow the base classes.
  93. ///
  94. /// Pointers differ depending on what they point to. If the pointer
  95. /// points to a simple type, the child at index zero
  96. /// is the only child value available, unless \a synthetic_allowed
  97. /// is \b true, in which case the pointer will be used as an array
  98. /// and can create 'synthetic' child values using positive or
  99. /// negative indexes. If the pointer points to an aggregate type
  100. /// (an array, class, union, struct), then the pointee is
  101. /// transparently skipped and any children are going to be the indexes
  102. /// of the child values within the aggregate type. For example if
  103. /// we have a 'Point' type and we have a SBValue that contains a
  104. /// pointer to a 'Point' type, then the child at index zero will be
  105. /// the 'x' member, and the child at index 1 will be the 'y' member
  106. /// (the child at index zero won't be a 'Point' instance).
  107. ///
  108. /// If you actually need an SBValue that represents the type pointed
  109. /// to by a SBValue for which GetType().IsPointeeType() returns true,
  110. /// regardless of the pointee type, you can do that with SBValue::Dereference.
  111. ///
  112. /// Arrays have a preset number of children that can be accessed by
  113. /// index and will returns invalid child values for indexes that are
  114. /// out of bounds unless the \a synthetic_allowed is \b true. In this
  115. /// case the array can create 'synthetic' child values for indexes
  116. /// that aren't in the array bounds using positive or negative
  117. /// indexes.
  118. ///
  119. /// \param[in] idx
  120. /// The index of the child value to get
  121. ///
  122. /// \param[in] use_dynamic
  123. /// An enumeration that specifies whether to get dynamic values,
  124. /// and also if the target can be run to figure out the dynamic
  125. /// type of the child value.
  126. ///
  127. /// \param[in] can_create_synthetic
  128. /// If \b true, then allow child values to be created by index
  129. /// for pointers and arrays for indexes that normally wouldn't
  130. /// be allowed.
  131. ///
  132. /// \return
  133. /// A new SBValue object that represents the child member value.
  134. lldb::SBValue GetChildAtIndex(uint32_t idx,
  135. lldb::DynamicValueType use_dynamic,
  136. bool can_create_synthetic);
  137. // Matches children of this object only and will match base classes and
  138. // member names if this is a clang typed object.
  139. uint32_t GetIndexOfChildWithName(const char *name);
  140. // Matches child members of this object and child members of any base
  141. // classes.
  142. lldb::SBValue GetChildMemberWithName(const char *name);
  143. // Matches child members of this object and child members of any base
  144. // classes.
  145. lldb::SBValue GetChildMemberWithName(const char *name,
  146. lldb::DynamicValueType use_dynamic);
  147. // Expands nested expressions like .a->b[0].c[1]->d
  148. lldb::SBValue GetValueForExpressionPath(const char *expr_path);
  149. lldb::SBValue AddressOf();
  150. lldb::addr_t GetLoadAddress();
  151. lldb::SBAddress GetAddress();
  152. /// Get an SBData wrapping what this SBValue points to.
  153. ///
  154. /// This method will dereference the current SBValue, if its
  155. /// data type is a T* or T[], and extract item_count elements
  156. /// of type T from it, copying their contents in an SBData.
  157. ///
  158. /// \param[in] item_idx
  159. /// The index of the first item to retrieve. For an array
  160. /// this is equivalent to array[item_idx], for a pointer
  161. /// to *(pointer + item_idx). In either case, the measurement
  162. /// unit for item_idx is the sizeof(T) rather than the byte
  163. ///
  164. /// \param[in] item_count
  165. /// How many items should be copied into the output. By default
  166. /// only one item is copied, but more can be asked for.
  167. ///
  168. /// \return
  169. /// An SBData with the contents of the copied items, on success.
  170. /// An empty SBData otherwise.
  171. lldb::SBData GetPointeeData(uint32_t item_idx = 0, uint32_t item_count = 1);
  172. /// Get an SBData wrapping the contents of this SBValue.
  173. ///
  174. /// This method will read the contents of this object in memory
  175. /// and copy them into an SBData for future use.
  176. ///
  177. /// \return
  178. /// An SBData with the contents of this SBValue, on success.
  179. /// An empty SBData otherwise.
  180. lldb::SBData GetData();
  181. bool SetData(lldb::SBData &data, lldb::SBError &error);
  182. lldb::SBDeclaration GetDeclaration();
  183. /// Find out if a SBValue might have children.
  184. ///
  185. /// This call is much more efficient than GetNumChildren() as it
  186. /// doesn't need to complete the underlying type. This is designed
  187. /// to be used in a UI environment in order to detect if the
  188. /// disclosure triangle should be displayed or not.
  189. ///
  190. /// This function returns true for class, union, structure,
  191. /// pointers, references, arrays and more. Again, it does so without
  192. /// doing any expensive type completion.
  193. ///
  194. /// \return
  195. /// Returns \b true if the SBValue might have children, or \b
  196. /// false otherwise.
  197. bool MightHaveChildren();
  198. bool IsRuntimeSupportValue();
  199. uint32_t GetNumChildren();
  200. uint32_t GetNumChildren(uint32_t max);
  201. void *GetOpaqueType();
  202. lldb::SBTarget GetTarget();
  203. lldb::SBProcess GetProcess();
  204. lldb::SBThread GetThread();
  205. lldb::SBFrame GetFrame();
  206. lldb::SBValue Dereference();
  207. // Deprecated - please use GetType().IsPointerType() instead.
  208. bool TypeIsPointerType();
  209. lldb::SBType GetType();
  210. lldb::SBValue Persist();
  211. bool GetDescription(lldb::SBStream &description);
  212. bool GetExpressionPath(lldb::SBStream &description);
  213. bool GetExpressionPath(lldb::SBStream &description,
  214. bool qualify_cxx_base_classes);
  215. lldb::SBValue EvaluateExpression(const char *expr) const;
  216. lldb::SBValue EvaluateExpression(const char *expr,
  217. const SBExpressionOptions &options) const;
  218. lldb::SBValue EvaluateExpression(const char *expr,
  219. const SBExpressionOptions &options,
  220. const char *name) const;
  221. SBValue(const lldb::ValueObjectSP &value_sp);
  222. /// Watch this value if it resides in memory.
  223. ///
  224. /// Sets a watchpoint on the value.
  225. ///
  226. /// \param[in] resolve_location
  227. /// Resolve the location of this value once and watch its address.
  228. /// This value must currently be set to \b true as watching all
  229. /// locations of a variable or a variable path is not yet supported,
  230. /// though we plan to support it in the future.
  231. ///
  232. /// \param[in] read
  233. /// Stop when this value is accessed.
  234. ///
  235. /// \param[in] write
  236. /// Stop when this value is modified
  237. ///
  238. /// \param[out] error
  239. /// An error object. Contains the reason if there is some failure.
  240. ///
  241. /// \return
  242. /// An SBWatchpoint object. This object might not be valid upon
  243. /// return due to a value not being contained in memory, too
  244. /// large, or watchpoint resources are not available or all in
  245. /// use.
  246. lldb::SBWatchpoint Watch(bool resolve_location, bool read, bool write,
  247. SBError &error);
  248. // Backward compatibility fix in the interim.
  249. lldb::SBWatchpoint Watch(bool resolve_location, bool read, bool write);
  250. /// Watch this value that this value points to in memory
  251. ///
  252. /// Sets a watchpoint on the value.
  253. ///
  254. /// \param[in] resolve_location
  255. /// Resolve the location of this value once and watch its address.
  256. /// This value must currently be set to \b true as watching all
  257. /// locations of a variable or a variable path is not yet supported,
  258. /// though we plan to support it in the future.
  259. ///
  260. /// \param[in] read
  261. /// Stop when this value is accessed.
  262. ///
  263. /// \param[in] write
  264. /// Stop when this value is modified
  265. ///
  266. /// \param[out] error
  267. /// An error object. Contains the reason if there is some failure.
  268. ///
  269. /// \return
  270. /// An SBWatchpoint object. This object might not be valid upon
  271. /// return due to a value not being contained in memory, too
  272. /// large, or watchpoint resources are not available or all in
  273. /// use.
  274. lldb::SBWatchpoint WatchPointee(bool resolve_location, bool read, bool write,
  275. SBError &error);
  276. /// Same as the protected version of GetSP that takes a locker, except that we
  277. /// make the
  278. /// locker locally in the function. Since the Target API mutex is recursive,
  279. /// and the
  280. /// StopLocker is a read lock, you can call this function even if you are
  281. /// already
  282. /// holding the two above-mentioned locks.
  283. ///
  284. /// \return
  285. /// A ValueObjectSP of the best kind (static, dynamic or synthetic) we
  286. /// can cons up, in accordance with the SBValue's settings.
  287. lldb::ValueObjectSP GetSP() const;
  288. protected:
  289. friend class SBBlock;
  290. friend class SBFrame;
  291. friend class SBTarget;
  292. friend class SBThread;
  293. friend class SBValueList;
  294. /// Get the appropriate ValueObjectSP from this SBValue, consulting the
  295. /// use_dynamic and use_synthetic options passed in to SetSP when the
  296. /// SBValue's contents were set. Since this often requires examining memory,
  297. /// and maybe even running code, it needs to acquire the Target API and
  298. /// Process StopLock.
  299. /// Those are held in an opaque class ValueLocker which is currently local to
  300. /// SBValue.cpp.
  301. /// So you don't have to get these yourself just default construct a
  302. /// ValueLocker, and pass it into this.
  303. /// If we need to make a ValueLocker and use it in some other .cpp file, we'll
  304. /// have to move it to
  305. /// ValueObject.h/cpp or somewhere else convenient. We haven't needed to so
  306. /// far.
  307. ///
  308. /// \param[in] value_locker
  309. /// An object that will hold the Target API, and Process RunLocks, and
  310. /// auto-destroy them when it goes out of scope. Currently this is only
  311. /// useful in
  312. /// SBValue.cpp.
  313. ///
  314. /// \return
  315. /// A ValueObjectSP of the best kind (static, dynamic or synthetic) we
  316. /// can cons up, in accordance with the SBValue's settings.
  317. lldb::ValueObjectSP GetSP(ValueLocker &value_locker) const;
  318. // these calls do the right thing WRT adjusting their settings according to
  319. // the target's preferences
  320. void SetSP(const lldb::ValueObjectSP &sp);
  321. void SetSP(const lldb::ValueObjectSP &sp, bool use_synthetic);
  322. void SetSP(const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic);
  323. void SetSP(const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic,
  324. bool use_synthetic);
  325. void SetSP(const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic,
  326. bool use_synthetic, const char *name);
  327. private:
  328. typedef std::shared_ptr<ValueImpl> ValueImplSP;
  329. ValueImplSP m_opaque_sp;
  330. void SetSP(ValueImplSP impl_sp);
  331. };
  332. } // namespace lldb
  333. #endif // LLDB_API_SBVALUE_H