LLDB  mainline
Watchpoint.h
Go to the documentation of this file.
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 
9 #ifndef liblldb_Watchpoint_h_
10 #define liblldb_Watchpoint_h_
11 
12 #include <memory>
13 #include <string>
14 
18 #include "lldb/Target/Target.h"
19 #include "lldb/Utility/UserID.h"
20 #include "lldb/lldb-private.h"
21 
22 namespace lldb_private {
23 
24 class Watchpoint : public std::enable_shared_from_this<Watchpoint>,
25  public StoppointLocation {
26 public:
27  class WatchpointEventData : public EventData {
28  public:
29  WatchpointEventData(lldb::WatchpointEventType sub_type,
30  const lldb::WatchpointSP &new_watchpoint_sp);
31 
32  ~WatchpointEventData() override;
33 
35 
36  ConstString GetFlavor() const override;
37 
38  lldb::WatchpointEventType GetWatchpointEventType() const;
39 
40  lldb::WatchpointSP &GetWatchpoint();
41 
42  void Dump(Stream *s) const override;
43 
44  static lldb::WatchpointEventType
45  GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp);
46 
47  static lldb::WatchpointSP
48  GetWatchpointFromEvent(const lldb::EventSP &event_sp);
49 
50  static const WatchpointEventData *
51  GetEventDataFromEvent(const Event *event_sp);
52 
53  private:
54  lldb::WatchpointEventType m_watchpoint_event;
55  lldb::WatchpointSP m_new_watchpoint_sp;
56 
57  DISALLOW_COPY_AND_ASSIGN(WatchpointEventData);
58  };
59 
60  Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
61  const CompilerType *type, bool hardware = true);
62 
63  ~Watchpoint() override;
64 
66 
67  bool IsEnabled() const;
68 
69  // This doesn't really enable/disable the watchpoint. It is currently just
70  // for use in the Process plugin's {Enable,Disable}Watchpoint, which should
71  // be used instead.
72 
73  void SetEnabled(bool enabled, bool notify = true);
74 
75  bool IsHardware() const override;
76 
77  bool ShouldStop(StoppointCallbackContext *context) override;
78 
79  bool WatchpointRead() const;
80  bool WatchpointWrite() const;
81  uint32_t GetIgnoreCount() const;
82  void SetIgnoreCount(uint32_t n);
83  void SetWatchpointType(uint32_t type, bool notify = true);
84  void SetDeclInfo(const std::string &str);
85  std::string GetWatchSpec();
86  void SetWatchSpec(const std::string &str);
87 
88  // Snapshot management interface.
89  bool IsWatchVariable() const;
90  void SetWatchVariable(bool val);
91  bool CaptureWatchedValue(const ExecutionContext &exe_ctx);
92 
94  void Dump(Stream *s) const override;
95  void DumpSnapshots(Stream *s, const char *prefix = nullptr) const;
96  void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const;
97  Target &GetTarget() { return m_target; }
98  const Status &GetError() { return m_error; }
99 
100  /// Returns the WatchpointOptions structure set for this watchpoint.
101  ///
102  /// \return
103  /// A pointer to this watchpoint's WatchpointOptions.
104  WatchpointOptions *GetOptions() { return &m_options; }
105 
106  /// Set the callback action invoked when the watchpoint is hit.
107  ///
108  /// \param[in] callback
109  /// The method that will get called when the watchpoint is hit.
110  /// \param[in] callback_baton
111  /// A void * pointer that will get passed back to the callback function.
112  /// \param[in] is_synchronous
113  /// If \b true the callback will be run on the private event thread
114  /// before the stop event gets reported. If false, the callback will get
115  /// handled on the public event thread after the stop has been posted.
116  ///
117  /// \return
118  /// \b true if the process should stop when you hit the watchpoint.
119  /// \b false if it should continue.
120  void SetCallback(WatchpointHitCallback callback, void *callback_baton,
121  bool is_synchronous = false);
122 
123  void SetCallback(WatchpointHitCallback callback,
124  const lldb::BatonSP &callback_baton_sp,
125  bool is_synchronous = false);
126 
127  void ClearCallback();
128 
129  /// Invoke the callback action when the watchpoint is hit.
130  ///
131  /// \param[in] context
132  /// Described the watchpoint event.
133  ///
134  /// \return
135  /// \b true if the target should stop at this watchpoint and \b false not.
137 
138  // Condition
139  /// Set the watchpoint's condition.
140  ///
141  /// \param[in] condition
142  /// The condition expression to evaluate when the watchpoint is hit.
143  /// Pass in nullptr to clear the condition.
144  void SetCondition(const char *condition);
145 
146  /// Return a pointer to the text of the condition expression.
147  ///
148  /// \return
149  /// A pointer to the condition expression text, or nullptr if no
150  // condition has been set.
151  const char *GetConditionText() const;
152 
153  void TurnOnEphemeralMode();
154 
155  void TurnOffEphemeralMode();
156 
158 
159  const CompilerType &GetCompilerType() { return m_type; }
160 
161 private:
162  friend class Target;
163  friend class WatchpointList;
164 
165  void ResetHitCount() { m_hit_count = 0; }
166 
167  void ResetHistoricValues() {
168  m_old_value_sp.reset(nullptr);
169  m_new_value_sp.reset(nullptr);
170  }
171 
172  Target &m_target;
173  bool m_enabled; // Is this watchpoint enabled
174  bool m_is_hardware; // Is this a hardware watchpoint
175  bool m_is_watch_variable; // True if set via 'watchpoint set variable'.
176  bool m_is_ephemeral; // True if the watchpoint is in the ephemeral mode,
177  // meaning that it is
178  // undergoing a pair of temporary disable/enable actions to avoid recursively
179  // triggering further watchpoint events.
180  uint32_t m_disabled_count; // Keep track of the count that the watchpoint is
181  // disabled while in ephemeral mode.
182  // At the end of the ephemeral mode when the watchpoint is to be enabled
183  // again, we check the count, if it is more than 1, it means the user-
184  // supplied actions actually want the watchpoint to be disabled!
185  uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
186  m_watch_write : 1, // 1 if we stop when the watched data is written to
187  m_watch_was_read : 1, // Set to 1 when watchpoint is hit for a read access
188  m_watch_was_written : 1; // Set to 1 when watchpoint is hit for a write
189  // access
190  uint32_t m_ignore_count; // Number of times to ignore this watchpoint
191  uint32_t m_false_alarms; // Number of false alarms.
192  std::string m_decl_str; // Declaration information, if any.
193  std::string m_watch_spec_str; // Spec for the watchpoint.
194  lldb::ValueObjectSP m_old_value_sp;
195  lldb::ValueObjectSP m_new_value_sp;
196  CompilerType m_type;
197  Status m_error; // An error object describing errors associated with this
198  // watchpoint.
200  m_options; // Settable watchpoint options, which is a delegate to handle
201  // the callback machinery.
202  bool m_being_created;
203 
204  std::unique_ptr<UserExpression> m_condition_up; // The condition to test.
205 
206  void SetID(lldb::watch_id_t id) { m_loc_id = id; }
207 
208  void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind);
209 
210  void SendWatchpointChangedEvent(WatchpointEventData *data);
211 
212  DISALLOW_COPY_AND_ASSIGN(Watchpoint);
213 };
214 
215 } // namespace lldb_private
216 
217 #endif // liblldb_Watchpoint_h_
void SetCondition(const char *condition)
Set the watchpoint&#39;s condition.
Definition: Watchpoint.cpp:273
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
ConstString GetFlavor() const override
Definition: Watchpoint.cpp:333
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
This class is used by Watchpoint to manage a list of watchpoints,.
bool InvokeCallback(StoppointCallbackContext *context)
Invoke the callback action when the watchpoint is hit.
Definition: Watchpoint.cpp:269
void SetCallback(WatchpointHitCallback callback, void *callback_baton, bool is_synchronous=false)
Set the callback action invoked when the watchpoint is hit.
Definition: Watchpoint.cpp:55
void SetDeclInfo(const std::string &str)
Definition: Watchpoint.cpp:79
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
int32_t watch_id_t
Definition: lldb-types.h:89
void DumpSnapshots(Stream *s, const char *prefix=nullptr) const
Definition: Watchpoint.cpp:146
void IncrementFalseAlarmsAndReviseHitCount()
Definition: Watchpoint.cpp:114
WatchpointEventData(lldb::WatchpointEventType sub_type, const lldb::WatchpointSP &new_watchpoint_sp)
Definition: Watchpoint.cpp:321
void SetEnabled(bool enabled, bool notify=true)
Definition: Watchpoint.cpp:228
Watchpoint(Target &target, lldb::addr_t addr, uint32_t size, const CompilerType *type, bool hardware=true)
Definition: Watchpoint.cpp:25
WatchpointOptions * GetOptions()
Returns the WatchpointOptions structure set for this watchpoint.
Definition: Watchpoint.h:104
void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const
Definition: Watchpoint.cpp:175
bool IsHardware() const override
Definition: Watchpoint.cpp:89
void SetIgnoreCount(uint32_t n)
Definition: Watchpoint.cpp:262
bool IsWatchVariable() const
Definition: Watchpoint.cpp:91
void SetWatchVariable(bool val)
Definition: Watchpoint.cpp:93
void SetWatchSpec(const std::string &str)
Definition: Watchpoint.cpp:83
const CompilerType & GetCompilerType()
Definition: Watchpoint.h:159
void SetWatchpointType(uint32_t type, bool notify=true)
Definition: Watchpoint.cpp:246
void Dump(Stream *s) const override
Definition: Watchpoint.cpp:346
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
bool WatchpointWrite() const
Definition: Watchpoint.cpp:258
uint64_t addr_t
Definition: lldb-types.h:83
std::string GetWatchSpec()
Definition: Watchpoint.cpp:81
A uniqued constant string class.
Definition: ConstString.h:38
const Status & GetError()
Definition: Watchpoint.h:98
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:360
static lldb::WatchpointSP GetWatchpointFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:370
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Definition: Watchpoint.cpp:136
const char * GetConditionText() const
Return a pointer to the text of the condition expression.
Definition: Watchpoint.cpp:292
bool WatchpointRead() const
Definition: Watchpoint.cpp:256
bool ShouldStop(StoppointCallbackContext *context) override
Definition: Watchpoint.cpp:130
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
bool CaptureWatchedValue(const ExecutionContext &exe_ctx)
Definition: Watchpoint.cpp:95
lldb::WatchpointEventType GetWatchpointEventType() const
Definition: Watchpoint.cpp:342
uint32_t GetIgnoreCount() const
Definition: Watchpoint.cpp:260
static const WatchpointEventData * GetEventDataFromEvent(const Event *event_sp)
Definition: Watchpoint.cpp:349
An error handling class.
Definition: Status.h:44