Watchpoint.h revision 360784
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
15#include "lldb/Breakpoint/StoppointLocation.h"
16#include "lldb/Breakpoint/WatchpointOptions.h"
17#include "lldb/Symbol/CompilerType.h"
18#include "lldb/Target/Target.h"
19#include "lldb/Utility/UserID.h"
20#include "lldb/lldb-private.h"
21
22namespace lldb_private {
23
24class Watchpoint : public std::enable_shared_from_this<Watchpoint>,
25                   public StoppointLocation {
26public:
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
34    static ConstString GetFlavorString();
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
65  void IncrementFalseAlarmsAndReviseHitCount();
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  void SetEnabled(bool enabled, bool notify = true);
73
74  bool IsHardware() const override;
75
76  bool ShouldStop(StoppointCallbackContext *context) override;
77
78  bool WatchpointRead() const;
79  bool WatchpointWrite() const;
80  uint32_t GetIgnoreCount() const;
81  void SetIgnoreCount(uint32_t n);
82  void SetWatchpointType(uint32_t type, bool notify = true);
83  void SetDeclInfo(const std::string &str);
84  std::string GetWatchSpec();
85  void SetWatchSpec(const std::string &str);
86
87  // Snapshot management interface.
88  bool IsWatchVariable() const;
89  void SetWatchVariable(bool val);
90  bool CaptureWatchedValue(const ExecutionContext &exe_ctx);
91
92  void GetDescription(Stream *s, lldb::DescriptionLevel level);
93  void Dump(Stream *s) const override;
94  void DumpSnapshots(Stream *s, const char *prefix = nullptr) const;
95  void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const;
96  Target &GetTarget() { return m_target; }
97  const Status &GetError() { return m_error; }
98
99  /// Returns the WatchpointOptions structure set for this watchpoint.
100  ///
101  /// \return
102  ///     A pointer to this watchpoint's WatchpointOptions.
103  WatchpointOptions *GetOptions() { return &m_options; }
104
105  /// Set the callback action invoked when the watchpoint is hit.
106  ///
107  /// \param[in] callback
108  ///    The method that will get called when the watchpoint is hit.
109  /// \param[in] callback_baton
110  ///    A void * pointer that will get passed back to the callback function.
111  /// \param[in] is_synchronous
112  ///    If \b true the callback will be run on the private event thread
113  ///    before the stop event gets reported.  If false, the callback will get
114  ///    handled on the public event thread after the stop has been posted.
115  void SetCallback(WatchpointHitCallback callback, void *callback_baton,
116                   bool is_synchronous = false);
117
118  void SetCallback(WatchpointHitCallback callback,
119                   const lldb::BatonSP &callback_baton_sp,
120                   bool is_synchronous = false);
121
122  void ClearCallback();
123
124  /// Invoke the callback action when the watchpoint is hit.
125  ///
126  /// \param[in] context
127  ///     Described the watchpoint event.
128  ///
129  /// \return
130  ///     \b true if the target should stop at this watchpoint and \b false not.
131  bool InvokeCallback(StoppointCallbackContext *context);
132
133  // Condition
134  /// Set the watchpoint's condition.
135  ///
136  /// \param[in] condition
137  ///    The condition expression to evaluate when the watchpoint is hit.
138  ///    Pass in nullptr to clear the condition.
139  void SetCondition(const char *condition);
140
141  /// Return a pointer to the text of the condition expression.
142  ///
143  /// \return
144  ///    A pointer to the condition expression text, or nullptr if no
145  //     condition has been set.
146  const char *GetConditionText() const;
147
148  void TurnOnEphemeralMode();
149
150  void TurnOffEphemeralMode();
151
152  bool IsDisabledDuringEphemeralMode();
153
154  const CompilerType &GetCompilerType() { return m_type; }
155
156private:
157  friend class Target;
158  friend class WatchpointList;
159
160  void ResetHitCount() { m_hit_count = 0; }
161
162  void ResetHistoricValues() {
163    m_old_value_sp.reset(nullptr);
164    m_new_value_sp.reset(nullptr);
165  }
166
167  Target &m_target;
168  bool m_enabled;           // Is this watchpoint enabled
169  bool m_is_hardware;       // Is this a hardware watchpoint
170  bool m_is_watch_variable; // True if set via 'watchpoint set variable'.
171  bool m_is_ephemeral;      // True if the watchpoint is in the ephemeral mode,
172                            // meaning that it is
173  // undergoing a pair of temporary disable/enable actions to avoid recursively
174  // triggering further watchpoint events.
175  uint32_t m_disabled_count; // Keep track of the count that the watchpoint is
176                             // disabled while in ephemeral mode.
177  // At the end of the ephemeral mode when the watchpoint is to be enabled
178  // again, we check the count, if it is more than 1, it means the user-
179  // supplied actions actually want the watchpoint to be disabled!
180  uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
181      m_watch_write : 1,     // 1 if we stop when the watched data is written to
182      m_watch_was_read : 1, // Set to 1 when watchpoint is hit for a read access
183      m_watch_was_written : 1;  // Set to 1 when watchpoint is hit for a write
184                                // access
185  uint32_t m_ignore_count;      // Number of times to ignore this watchpoint
186  uint32_t m_false_alarms;      // Number of false alarms.
187  std::string m_decl_str;       // Declaration information, if any.
188  std::string m_watch_spec_str; // Spec for the watchpoint.
189  lldb::ValueObjectSP m_old_value_sp;
190  lldb::ValueObjectSP m_new_value_sp;
191  CompilerType m_type;
192  Status m_error; // An error object describing errors associated with this
193                  // watchpoint.
194  WatchpointOptions
195      m_options; // Settable watchpoint options, which is a delegate to handle
196                 // the callback machinery.
197  bool m_being_created;
198
199  std::unique_ptr<UserExpression> m_condition_up; // The condition to test.
200
201  void SetID(lldb::watch_id_t id) { m_loc_id = id; }
202
203  void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind);
204
205  void SendWatchpointChangedEvent(WatchpointEventData *data);
206
207  DISALLOW_COPY_AND_ASSIGN(Watchpoint);
208};
209
210} // namespace lldb_private
211
212#endif // liblldb_Watchpoint_h_
213