BreakpointOptions.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //===-- BreakpointOptions.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_BREAKPOINT_BREAKPOINTOPTIONS_H
  9. #define LLDB_BREAKPOINT_BREAKPOINTOPTIONS_H
  10. #include <memory>
  11. #include <string>
  12. #include "lldb/Utility/Baton.h"
  13. #include "lldb/Utility/Flags.h"
  14. #include "lldb/Utility/StringList.h"
  15. #include "lldb/Utility/StructuredData.h"
  16. #include "lldb/lldb-private.h"
  17. namespace lldb_private {
  18. /// \class BreakpointOptions BreakpointOptions.h
  19. /// "lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a
  20. /// breakpoint or breakpoint location.
  21. class BreakpointOptions {
  22. friend class BreakpointLocation;
  23. friend class BreakpointName;
  24. friend class lldb_private::BreakpointOptionGroup;
  25. friend class Breakpoint;
  26. public:
  27. enum OptionKind {
  28. eCallback = 1 << 0,
  29. eEnabled = 1 << 1,
  30. eOneShot = 1 << 2,
  31. eIgnoreCount = 1 << 3,
  32. eThreadSpec = 1 << 4,
  33. eCondition = 1 << 5,
  34. eAutoContinue = 1 << 6,
  35. eAllOptions = (eCallback | eEnabled | eOneShot | eIgnoreCount | eThreadSpec
  36. | eCondition | eAutoContinue)
  37. };
  38. struct CommandData {
  39. CommandData()
  40. : user_source(), script_source(),
  41. interpreter(lldb::eScriptLanguageNone), stop_on_error(true) {}
  42. CommandData(const StringList &user_source, lldb::ScriptLanguage interp)
  43. : user_source(user_source), script_source(), interpreter(interp),
  44. stop_on_error(true) {}
  45. virtual ~CommandData() = default;
  46. static const char *GetSerializationKey() { return "BKPTCMDData"; }
  47. StructuredData::ObjectSP SerializeToStructuredData();
  48. static std::unique_ptr<CommandData>
  49. CreateFromStructuredData(const StructuredData::Dictionary &options_dict,
  50. Status &error);
  51. StringList user_source;
  52. std::string script_source;
  53. enum lldb::ScriptLanguage
  54. interpreter; // eScriptLanguageNone means command interpreter.
  55. bool stop_on_error;
  56. private:
  57. enum class OptionNames : uint32_t {
  58. UserSource = 0,
  59. Interpreter,
  60. StopOnError,
  61. LastOptionName
  62. };
  63. static const char
  64. *g_option_names[static_cast<uint32_t>(OptionNames::LastOptionName)];
  65. static const char *GetKey(OptionNames enum_value) {
  66. return g_option_names[static_cast<uint32_t>(enum_value)];
  67. }
  68. };
  69. class CommandBaton : public TypedBaton<CommandData> {
  70. public:
  71. explicit CommandBaton(std::unique_ptr<CommandData> Data)
  72. : TypedBaton(std::move(Data)) {}
  73. void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level,
  74. unsigned indentation) const override;
  75. };
  76. typedef std::shared_ptr<CommandBaton> CommandBatonSP;
  77. // Constructors and Destructors
  78. /// This constructor allows you to specify all the breakpoint options except
  79. /// the callback. That one is more complicated, and better to do by hand.
  80. ///
  81. /// \param[in] condition
  82. /// The expression which if it evaluates to \b true if we are to stop
  83. ///
  84. /// \param[in] enabled
  85. /// Is this breakpoint enabled.
  86. ///
  87. /// \param[in] ignore
  88. /// How many breakpoint hits we should ignore before stopping.
  89. ///
  90. /// \param[in] one_shot
  91. /// Should this breakpoint delete itself after being hit once.
  92. ///
  93. /// \param[in] auto_continue
  94. /// Should this breakpoint auto-continue after running its commands.
  95. ///
  96. BreakpointOptions(const char *condition, bool enabled = true,
  97. int32_t ignore = 0, bool one_shot = false,
  98. bool auto_continue = false);
  99. /// Breakpoints make options with all flags set. Locations and Names make
  100. /// options with no flags set.
  101. BreakpointOptions(bool all_flags_set);
  102. BreakpointOptions(const BreakpointOptions &rhs);
  103. virtual ~BreakpointOptions();
  104. static std::unique_ptr<BreakpointOptions>
  105. CreateFromStructuredData(Target &target,
  106. const StructuredData::Dictionary &data_dict,
  107. Status &error);
  108. virtual StructuredData::ObjectSP SerializeToStructuredData();
  109. static const char *GetSerializationKey() { return "BKPTOptions"; }
  110. // Operators
  111. const BreakpointOptions &operator=(const BreakpointOptions &rhs);
  112. /// Copy over only the options set in the incoming BreakpointOptions.
  113. void CopyOverSetOptions(const BreakpointOptions &rhs);
  114. // Callbacks
  115. //
  116. // Breakpoint callbacks come in two forms, synchronous and asynchronous.
  117. // Synchronous callbacks will get run before any of the thread plans are
  118. // consulted, and if they return false the target will continue "under the
  119. // radar" of the thread plans. There are a couple of restrictions to
  120. // synchronous callbacks:
  121. // 1) They should NOT resume the target themselves.
  122. // Just return false if you want the target to restart.
  123. // 2) Breakpoints with synchronous callbacks can't have conditions
  124. // (or rather, they can have them, but they won't do anything.
  125. // Ditto with ignore counts, etc... You are supposed to control that all
  126. // through the callback.
  127. // Asynchronous callbacks get run as part of the "ShouldStop" logic in the
  128. // thread plan. The logic there is:
  129. // a) If the breakpoint is thread specific and not for this thread, continue
  130. // w/o running the callback.
  131. // NB. This is actually enforced underneath the breakpoint system, the
  132. // Process plugin is expected to
  133. // call BreakpointSite::IsValidForThread, and set the thread's StopInfo
  134. // to "no reason". That way,
  135. // thread displays won't show stops for breakpoints not for that
  136. // thread...
  137. // b) If the ignore count says we shouldn't stop, then ditto.
  138. // c) If the condition says we shouldn't stop, then ditto.
  139. // d) Otherwise, the callback will get run, and if it returns true we will
  140. // stop, and if false we won't.
  141. // The asynchronous callback can run the target itself, but at present that
  142. // should be the last action the callback does. We will relax this condition
  143. // at some point, but it will take a bit of plumbing to get that to work.
  144. //
  145. /// Adds a callback to the breakpoint option set.
  146. ///
  147. /// \param[in] callback
  148. /// The function to be called when the breakpoint gets hit.
  149. ///
  150. /// \param[in] baton_sp
  151. /// A baton which will get passed back to the callback when it is invoked.
  152. ///
  153. /// \param[in] synchronous
  154. /// Whether this is a synchronous or asynchronous callback. See discussion
  155. /// above.
  156. void SetCallback(BreakpointHitCallback callback,
  157. const lldb::BatonSP &baton_sp, bool synchronous = false);
  158. void SetCallback(BreakpointHitCallback callback,
  159. const BreakpointOptions::CommandBatonSP &command_baton_sp,
  160. bool synchronous = false);
  161. /// Returns the command line commands for the callback on this breakpoint.
  162. ///
  163. /// \param[out] command_list
  164. /// The commands will be appended to this list.
  165. ///
  166. /// \return
  167. /// \btrue if the command callback is a command-line callback,
  168. /// \bfalse otherwise.
  169. bool GetCommandLineCallbacks(StringList &command_list);
  170. /// Remove the callback from this option set.
  171. void ClearCallback();
  172. // The rest of these functions are meant to be used only within the
  173. // breakpoint handling mechanism.
  174. /// Use this function to invoke the callback for a specific stop.
  175. ///
  176. /// \param[in] context
  177. /// The context in which the callback is to be invoked. This includes the
  178. /// stop event, the
  179. /// execution context of the stop (since you might hit the same breakpoint
  180. /// on multiple threads) and
  181. /// whether we are currently executing synchronous or asynchronous
  182. /// callbacks.
  183. ///
  184. /// \param[in] break_id
  185. /// The breakpoint ID that owns this option set.
  186. ///
  187. /// \param[in] break_loc_id
  188. /// The breakpoint location ID that owns this option set.
  189. ///
  190. /// \return
  191. /// The callback return value.
  192. bool InvokeCallback(StoppointCallbackContext *context,
  193. lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
  194. /// Used in InvokeCallback to tell whether it is the right time to run this
  195. /// kind of callback.
  196. ///
  197. /// \return
  198. /// The synchronicity of our callback.
  199. bool IsCallbackSynchronous() const { return m_callback_is_synchronous; }
  200. /// Fetch the baton from the callback.
  201. ///
  202. /// \return
  203. /// The baton.
  204. Baton *GetBaton();
  205. /// Fetch a const version of the baton from the callback.
  206. ///
  207. /// \return
  208. /// The baton.
  209. const Baton *GetBaton() const;
  210. // Condition
  211. /// Set the breakpoint option's condition.
  212. ///
  213. /// \param[in] condition
  214. /// The condition expression to evaluate when the breakpoint is hit.
  215. void SetCondition(const char *condition);
  216. /// Return a pointer to the text of the condition expression.
  217. ///
  218. /// \return
  219. /// A pointer to the condition expression text, or nullptr if no
  220. // condition has been set.
  221. const char *GetConditionText(size_t *hash = nullptr) const;
  222. // Enabled/Ignore Count
  223. /// Check the Enable/Disable state.
  224. /// \return
  225. /// \b true if the breakpoint is enabled, \b false if disabled.
  226. bool IsEnabled() const { return m_enabled; }
  227. /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
  228. void SetEnabled(bool enabled) {
  229. m_enabled = enabled;
  230. m_set_flags.Set(eEnabled);
  231. }
  232. /// Check the auto-continue state.
  233. /// \return
  234. /// \b true if the breakpoint is set to auto-continue, \b false otherwise.
  235. bool IsAutoContinue() const { return m_auto_continue; }
  236. /// Set the auto-continue state.
  237. void SetAutoContinue(bool auto_continue) {
  238. m_auto_continue = auto_continue;
  239. m_set_flags.Set(eAutoContinue);
  240. }
  241. /// Check the One-shot state.
  242. /// \return
  243. /// \b true if the breakpoint is one-shot, \b false otherwise.
  244. bool IsOneShot() const { return m_one_shot; }
  245. /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
  246. void SetOneShot(bool one_shot) {
  247. m_one_shot = one_shot;
  248. m_set_flags.Set(eOneShot);
  249. }
  250. /// Set the breakpoint to ignore the next \a count breakpoint hits.
  251. /// \param[in] n
  252. /// The number of breakpoint hits to ignore.
  253. void SetIgnoreCount(uint32_t n) {
  254. m_ignore_count = n;
  255. m_set_flags.Set(eIgnoreCount);
  256. }
  257. /// Return the current Ignore Count.
  258. /// \return
  259. /// The number of breakpoint hits to be ignored.
  260. uint32_t GetIgnoreCount() const { return m_ignore_count; }
  261. /// Return the current thread spec for this option. This will return nullptr
  262. /// if the no thread specifications have been set for this Option yet.
  263. /// \return
  264. /// The thread specification pointer for this option, or nullptr if none
  265. /// has
  266. /// been set yet.
  267. const ThreadSpec *GetThreadSpecNoCreate() const;
  268. /// Returns a pointer to the ThreadSpec for this option, creating it. if it
  269. /// hasn't been created already. This API is used for setting the
  270. /// ThreadSpec items for this option.
  271. ThreadSpec *GetThreadSpec();
  272. void SetThreadID(lldb::tid_t thread_id);
  273. void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
  274. /// Check if the breakpoint option has a callback set.
  275. ///
  276. /// \return
  277. /// If the breakpoint option has a callback, \b true otherwise \b false.
  278. bool HasCallback() const;
  279. /// This is the default empty callback.
  280. static bool NullCallback(void *baton, StoppointCallbackContext *context,
  281. lldb::user_id_t break_id,
  282. lldb::user_id_t break_loc_id);
  283. /// Set a callback based on BreakpointOptions::CommandData. \param[in]
  284. /// cmd_data
  285. /// A UP holding the new'ed CommandData object.
  286. /// The breakpoint will take ownership of pointer held by this object.
  287. void SetCommandDataCallback(std::unique_ptr<CommandData> &cmd_data);
  288. void Clear();
  289. bool AnySet() const {
  290. return m_set_flags.AnySet(eAllOptions);
  291. }
  292. protected:
  293. // Classes that inherit from BreakpointOptions can see and modify these
  294. bool IsOptionSet(OptionKind kind)
  295. {
  296. return m_set_flags.Test(kind);
  297. }
  298. enum class OptionNames {
  299. ConditionText = 0,
  300. IgnoreCount,
  301. EnabledState,
  302. OneShotState,
  303. AutoContinue,
  304. LastOptionName
  305. };
  306. static const char *g_option_names[(size_t)OptionNames::LastOptionName];
  307. static const char *GetKey(OptionNames enum_value) {
  308. return g_option_names[(size_t)enum_value];
  309. }
  310. static bool BreakpointOptionsCallbackFunction(
  311. void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
  312. lldb::user_id_t break_loc_id);
  313. void SetThreadSpec(std::unique_ptr<ThreadSpec> &thread_spec_up);
  314. private:
  315. /// For BreakpointOptions only
  316. /// This is the callback function pointer
  317. BreakpointHitCallback m_callback;
  318. /// This is the client data for the callback
  319. lldb::BatonSP m_callback_baton_sp;
  320. bool m_baton_is_command_baton;
  321. bool m_callback_is_synchronous;
  322. bool m_enabled;
  323. /// If set, the breakpoint delete itself after being hit once.
  324. bool m_one_shot;
  325. /// Number of times to ignore this breakpoint.
  326. uint32_t m_ignore_count;
  327. /// Thread for which this breakpoint will stop.
  328. std::unique_ptr<ThreadSpec> m_thread_spec_up;
  329. /// The condition to test.
  330. std::string m_condition_text;
  331. /// Its hash, so that locations know when the condition is updated.
  332. size_t m_condition_text_hash;
  333. /// If set, inject breakpoint condition into process.
  334. bool m_inject_condition;
  335. /// If set, auto-continue from breakpoint.
  336. bool m_auto_continue;
  337. /// Which options are set at this level.
  338. /// Drawn from BreakpointOptions::SetOptionsFlags.
  339. Flags m_set_flags;
  340. };
  341. } // namespace lldb_private
  342. #endif // LLDB_BREAKPOINT_BREAKPOINTOPTIONS_H