Watchpoint.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //===-- Watchpoint.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_WATCHPOINT_H
  9. #define LLDB_BREAKPOINT_WATCHPOINT_H
  10. #include <memory>
  11. #include <string>
  12. #include "lldb/Breakpoint/StoppointSite.h"
  13. #include "lldb/Breakpoint/WatchpointOptions.h"
  14. #include "lldb/Symbol/CompilerType.h"
  15. #include "lldb/Target/Target.h"
  16. #include "lldb/Utility/UserID.h"
  17. #include "lldb/lldb-private.h"
  18. namespace lldb_private {
  19. class Watchpoint : public std::enable_shared_from_this<Watchpoint>,
  20. public StoppointSite {
  21. public:
  22. class WatchpointEventData : public EventData {
  23. public:
  24. WatchpointEventData(lldb::WatchpointEventType sub_type,
  25. const lldb::WatchpointSP &new_watchpoint_sp);
  26. ~WatchpointEventData() override;
  27. static ConstString GetFlavorString();
  28. ConstString GetFlavor() const override;
  29. lldb::WatchpointEventType GetWatchpointEventType() const;
  30. lldb::WatchpointSP &GetWatchpoint();
  31. void Dump(Stream *s) const override;
  32. static lldb::WatchpointEventType
  33. GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp);
  34. static lldb::WatchpointSP
  35. GetWatchpointFromEvent(const lldb::EventSP &event_sp);
  36. static const WatchpointEventData *
  37. GetEventDataFromEvent(const Event *event_sp);
  38. private:
  39. lldb::WatchpointEventType m_watchpoint_event;
  40. lldb::WatchpointSP m_new_watchpoint_sp;
  41. WatchpointEventData(const WatchpointEventData &) = delete;
  42. const WatchpointEventData &operator=(const WatchpointEventData &) = delete;
  43. };
  44. Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
  45. const CompilerType *type, bool hardware = true);
  46. ~Watchpoint() override;
  47. void IncrementFalseAlarmsAndReviseHitCount();
  48. bool IsEnabled() const;
  49. // This doesn't really enable/disable the watchpoint. It is currently just
  50. // for use in the Process plugin's {Enable,Disable}Watchpoint, which should
  51. // be used instead.
  52. void SetEnabled(bool enabled, bool notify = true);
  53. bool IsHardware() const override;
  54. bool ShouldStop(StoppointCallbackContext *context) override;
  55. bool WatchpointRead() const;
  56. bool WatchpointWrite() const;
  57. uint32_t GetIgnoreCount() const;
  58. void SetIgnoreCount(uint32_t n);
  59. void SetWatchpointType(uint32_t type, bool notify = true);
  60. void SetDeclInfo(const std::string &str);
  61. std::string GetWatchSpec();
  62. void SetWatchSpec(const std::string &str);
  63. // Snapshot management interface.
  64. bool IsWatchVariable() const;
  65. void SetWatchVariable(bool val);
  66. bool CaptureWatchedValue(const ExecutionContext &exe_ctx);
  67. void GetDescription(Stream *s, lldb::DescriptionLevel level);
  68. void Dump(Stream *s) const override;
  69. void DumpSnapshots(Stream *s, const char *prefix = nullptr) const;
  70. void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const;
  71. Target &GetTarget() { return m_target; }
  72. const Status &GetError() { return m_error; }
  73. /// Returns the WatchpointOptions structure set for this watchpoint.
  74. ///
  75. /// \return
  76. /// A pointer to this watchpoint's WatchpointOptions.
  77. WatchpointOptions *GetOptions() { return &m_options; }
  78. /// Set the callback action invoked when the watchpoint is hit.
  79. ///
  80. /// \param[in] callback
  81. /// The method that will get called when the watchpoint is hit.
  82. /// \param[in] callback_baton
  83. /// A void * pointer that will get passed back to the callback function.
  84. /// \param[in] is_synchronous
  85. /// If \b true the callback will be run on the private event thread
  86. /// before the stop event gets reported. If false, the callback will get
  87. /// handled on the public event thread after the stop has been posted.
  88. void SetCallback(WatchpointHitCallback callback, void *callback_baton,
  89. bool is_synchronous = false);
  90. void SetCallback(WatchpointHitCallback callback,
  91. const lldb::BatonSP &callback_baton_sp,
  92. bool is_synchronous = false);
  93. void ClearCallback();
  94. /// Invoke the callback action when the watchpoint is hit.
  95. ///
  96. /// \param[in] context
  97. /// Described the watchpoint event.
  98. ///
  99. /// \return
  100. /// \b true if the target should stop at this watchpoint and \b false not.
  101. bool InvokeCallback(StoppointCallbackContext *context);
  102. // Condition
  103. /// Set the watchpoint's condition.
  104. ///
  105. /// \param[in] condition
  106. /// The condition expression to evaluate when the watchpoint is hit.
  107. /// Pass in nullptr to clear the condition.
  108. void SetCondition(const char *condition);
  109. /// Return a pointer to the text of the condition expression.
  110. ///
  111. /// \return
  112. /// A pointer to the condition expression text, or nullptr if no
  113. // condition has been set.
  114. const char *GetConditionText() const;
  115. void TurnOnEphemeralMode();
  116. void TurnOffEphemeralMode();
  117. bool IsDisabledDuringEphemeralMode();
  118. const CompilerType &GetCompilerType() { return m_type; }
  119. private:
  120. friend class Target;
  121. friend class WatchpointList;
  122. void ResetHistoricValues() {
  123. m_old_value_sp.reset();
  124. m_new_value_sp.reset();
  125. }
  126. Target &m_target;
  127. bool m_enabled; // Is this watchpoint enabled
  128. bool m_is_hardware; // Is this a hardware watchpoint
  129. bool m_is_watch_variable; // True if set via 'watchpoint set variable'.
  130. bool m_is_ephemeral; // True if the watchpoint is in the ephemeral mode,
  131. // meaning that it is
  132. // undergoing a pair of temporary disable/enable actions to avoid recursively
  133. // triggering further watchpoint events.
  134. uint32_t m_disabled_count; // Keep track of the count that the watchpoint is
  135. // disabled while in ephemeral mode.
  136. // At the end of the ephemeral mode when the watchpoint is to be enabled
  137. // again, we check the count, if it is more than 1, it means the user-
  138. // supplied actions actually want the watchpoint to be disabled!
  139. uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
  140. m_watch_write : 1, // 1 if we stop when the watched data is written to
  141. m_watch_was_read : 1, // Set to 1 when watchpoint is hit for a read access
  142. m_watch_was_written : 1; // Set to 1 when watchpoint is hit for a write
  143. // access
  144. uint32_t m_ignore_count; // Number of times to ignore this watchpoint
  145. uint32_t m_false_alarms; // Number of false alarms.
  146. std::string m_decl_str; // Declaration information, if any.
  147. std::string m_watch_spec_str; // Spec for the watchpoint.
  148. lldb::ValueObjectSP m_old_value_sp;
  149. lldb::ValueObjectSP m_new_value_sp;
  150. CompilerType m_type;
  151. Status m_error; // An error object describing errors associated with this
  152. // watchpoint.
  153. WatchpointOptions
  154. m_options; // Settable watchpoint options, which is a delegate to handle
  155. // the callback machinery.
  156. bool m_being_created;
  157. std::unique_ptr<UserExpression> m_condition_up; // The condition to test.
  158. void SetID(lldb::watch_id_t id) { m_id = id; }
  159. void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind);
  160. void SendWatchpointChangedEvent(WatchpointEventData *data);
  161. Watchpoint(const Watchpoint &) = delete;
  162. const Watchpoint &operator=(const Watchpoint &) = delete;
  163. };
  164. } // namespace lldb_private
  165. #endif // LLDB_BREAKPOINT_WATCHPOINT_H