1 //===-- Watchpoint.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Watchpoint_h_
11 #define liblldb_Watchpoint_h_
18 // Other libraries and framework includes
20 #include "lldb/Breakpoint/StoppointLocation.h"
21 #include "lldb/Breakpoint/WatchpointOptions.h"
22 #include "lldb/Symbol/CompilerType.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/UserID.h"
25 #include "lldb/lldb-private.h"
27 namespace lldb_private {
29 class Watchpoint : public std::enable_shared_from_this<Watchpoint>,
30 public StoppointLocation {
32 class WatchpointEventData : public EventData {
34 WatchpointEventData(lldb::WatchpointEventType sub_type,
35 const lldb::WatchpointSP &new_watchpoint_sp);
37 ~WatchpointEventData() override;
39 static const ConstString &GetFlavorString();
41 const ConstString &GetFlavor() const override;
43 lldb::WatchpointEventType GetWatchpointEventType() const;
45 lldb::WatchpointSP &GetWatchpoint();
47 void Dump(Stream *s) const override;
49 static lldb::WatchpointEventType
50 GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp);
52 static lldb::WatchpointSP
53 GetWatchpointFromEvent(const lldb::EventSP &event_sp);
55 static const WatchpointEventData *
56 GetEventDataFromEvent(const Event *event_sp);
59 lldb::WatchpointEventType m_watchpoint_event;
60 lldb::WatchpointSP m_new_watchpoint_sp;
62 DISALLOW_COPY_AND_ASSIGN(WatchpointEventData);
65 Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
66 const CompilerType *type, bool hardware = true);
68 ~Watchpoint() override;
70 void IncrementFalseAlarmsAndReviseHitCount();
72 bool IsEnabled() const;
74 // This doesn't really enable/disable the watchpoint.
75 // It is currently just for use in the Process plugin's
76 // {Enable,Disable}Watchpoint, which should be used instead.
78 void SetEnabled(bool enabled, bool notify = true);
80 bool IsHardware() const override;
82 bool ShouldStop(StoppointCallbackContext *context) override;
84 bool WatchpointRead() const;
85 bool WatchpointWrite() const;
86 uint32_t GetIgnoreCount() const;
87 void SetIgnoreCount(uint32_t n);
88 void SetWatchpointType(uint32_t type, bool notify = true);
89 void SetDeclInfo(const std::string &str);
90 std::string GetWatchSpec();
91 void SetWatchSpec(const std::string &str);
93 // Snapshot management interface.
94 bool IsWatchVariable() const;
95 void SetWatchVariable(bool val);
96 bool CaptureWatchedValue(const ExecutionContext &exe_ctx);
98 void GetDescription(Stream *s, lldb::DescriptionLevel level);
99 void Dump(Stream *s) const override;
100 void DumpSnapshots(Stream *s, const char *prefix = nullptr) const;
101 void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const;
102 Target &GetTarget() { return m_target; }
103 const Status &GetError() { return m_error; }
105 //------------------------------------------------------------------
106 /// Returns the WatchpointOptions structure set for this watchpoint.
109 /// A pointer to this watchpoint's WatchpointOptions.
110 //------------------------------------------------------------------
111 WatchpointOptions *GetOptions() { return &m_options; }
113 //------------------------------------------------------------------
114 /// Set the callback action invoked when the watchpoint is hit.
116 /// @param[in] callback
117 /// The method that will get called when the watchpoint is hit.
118 /// @param[in] callback_baton
119 /// A void * pointer that will get passed back to the callback function.
120 /// @param[in] is_synchronous
121 /// If \b true the callback will be run on the private event thread
122 /// before the stop event gets reported. If false, the callback will get
123 /// handled on the public event thread after the stop has been posted.
126 /// \b true if the process should stop when you hit the watchpoint.
127 /// \b false if it should continue.
128 //------------------------------------------------------------------
129 void SetCallback(WatchpointHitCallback callback, void *callback_baton,
130 bool is_synchronous = false);
132 void SetCallback(WatchpointHitCallback callback,
133 const lldb::BatonSP &callback_baton_sp,
134 bool is_synchronous = false);
136 void ClearCallback();
138 //------------------------------------------------------------------
139 /// Invoke the callback action when the watchpoint is hit.
141 /// @param[in] context
142 /// Described the watchpoint event.
145 /// \b true if the target should stop at this watchpoint and \b false not.
146 //------------------------------------------------------------------
147 bool InvokeCallback(StoppointCallbackContext *context);
149 //------------------------------------------------------------------
151 //------------------------------------------------------------------
152 //------------------------------------------------------------------
153 /// Set the watchpoint's condition.
155 /// @param[in] condition
156 /// The condition expression to evaluate when the watchpoint is hit.
157 /// Pass in nullptr to clear the condition.
158 //------------------------------------------------------------------
159 void SetCondition(const char *condition);
161 //------------------------------------------------------------------
162 /// Return a pointer to the text of the condition expression.
165 /// A pointer to the condition expression text, or nullptr if no
166 // condition has been set.
167 //------------------------------------------------------------------
168 const char *GetConditionText() const;
170 void TurnOnEphemeralMode();
172 void TurnOffEphemeralMode();
174 bool IsDisabledDuringEphemeralMode();
176 const CompilerType &GetCompilerType() { return m_type; }
180 friend class WatchpointList;
182 void ResetHitCount() { m_hit_count = 0; }
184 void ResetHistoricValues() {
185 m_old_value_sp.reset(nullptr);
186 m_new_value_sp.reset(nullptr);
190 bool m_enabled; // Is this watchpoint enabled
191 bool m_is_hardware; // Is this a hardware watchpoint
192 bool m_is_watch_variable; // True if set via 'watchpoint set variable'.
193 bool m_is_ephemeral; // True if the watchpoint is in the ephemeral mode,
194 // meaning that it is
195 // undergoing a pair of temporary disable/enable actions to avoid recursively
196 // triggering further watchpoint events.
197 uint32_t m_disabled_count; // Keep track of the count that the watchpoint is
198 // disabled while in ephemeral mode.
199 // At the end of the ephemeral mode when the watchpoint is to be enabled
201 // we check the count, if it is more than 1, it means the user-supplied
203 // actually want the watchpoint to be disabled!
204 uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
205 m_watch_write : 1, // 1 if we stop when the watched data is written to
206 m_watch_was_read : 1, // Set to 1 when watchpoint is hit for a read access
207 m_watch_was_written : 1; // Set to 1 when watchpoint is hit for a write
209 uint32_t m_ignore_count; // Number of times to ignore this watchpoint
210 uint32_t m_false_alarms; // Number of false alarms.
211 std::string m_decl_str; // Declaration information, if any.
212 std::string m_watch_spec_str; // Spec for the watchpoint.
213 lldb::ValueObjectSP m_old_value_sp;
214 lldb::ValueObjectSP m_new_value_sp;
216 Status m_error; // An error object describing errors associated with this
219 m_options; // Settable watchpoint options, which is a delegate to handle
220 // the callback machinery.
221 bool m_being_created;
223 std::unique_ptr<UserExpression> m_condition_ap; // The condition to test.
225 void SetID(lldb::watch_id_t id) { m_loc_id = id; }
227 void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind);
229 void SendWatchpointChangedEvent(WatchpointEventData *data);
231 DISALLOW_COPY_AND_ASSIGN(Watchpoint);
234 } // namespace lldb_private
236 #endif // liblldb_Watchpoint_h_