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 LLDB_BREAKPOINT_WATCHPOINT_H 10 #define LLDB_BREAKPOINT_WATCHPOINT_H 11 12 #include <memory> 13 #include <string> 14 15 #include "lldb/Breakpoint/StoppointSite.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 22 namespace lldb_private { 23 24 class Watchpoint : public std::enable_shared_from_this<Watchpoint>, 25 public StoppointSite { 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 34 static llvm::StringRef GetFlavorString(); 35 36 llvm::StringRef 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 WatchpointEventData(const WatchpointEventData &) = delete; 58 const WatchpointEventData &operator=(const WatchpointEventData &) = delete; 59 }; 60 61 Watchpoint(Target &target, lldb::addr_t addr, uint32_t size, 62 const CompilerType *type, bool hardware = true); 63 64 ~Watchpoint() override; 65 66 bool IsEnabled() const; 67 68 // This doesn't really enable/disable the watchpoint. It is currently just 69 // for use in the Process plugin's {Enable,Disable}Watchpoint, which should 70 // be used instead. 71 void SetEnabled(bool enabled, bool notify = true); 72 73 bool IsHardware() const override; 74 75 bool ShouldStop(StoppointCallbackContext *context) override; 76 77 bool WatchpointRead() const; 78 bool WatchpointWrite() const; 79 uint32_t GetIgnoreCount() const; 80 void SetIgnoreCount(uint32_t n); 81 void SetWatchpointType(uint32_t type, bool notify = true); 82 void SetDeclInfo(const std::string &str); 83 std::string GetWatchSpec(); 84 void SetWatchSpec(const std::string &str); 85 86 // Snapshot management interface. 87 bool IsWatchVariable() const; 88 void SetWatchVariable(bool val); 89 bool CaptureWatchedValue(const ExecutionContext &exe_ctx); 90 91 /// \struct WatchpointVariableContext 92 /// \brief Represents the context of a watchpoint variable. 93 /// 94 /// This struct encapsulates the information related to a watchpoint variable, 95 /// including the watch ID and the execution context in which it is being 96 /// used. This struct is passed as a Baton to the \b 97 /// VariableWatchpointDisabler breakpoint callback. 98 struct WatchpointVariableContext { 99 /// \brief Constructor for WatchpointVariableContext. 100 /// \param watch_id The ID of the watchpoint. 101 /// \param exe_ctx The execution context associated with the watchpoint. 102 WatchpointVariableContext(lldb::watch_id_t watch_id, 103 ExecutionContext exe_ctx) 104 : watch_id(watch_id), exe_ctx(exe_ctx) {} 105 106 lldb::watch_id_t watch_id; ///< The ID of the watchpoint. 107 ExecutionContext 108 exe_ctx; ///< The execution context associated with the watchpoint. 109 }; 110 111 class WatchpointVariableBaton : public TypedBaton<WatchpointVariableContext> { 112 public: 113 WatchpointVariableBaton(std::unique_ptr<WatchpointVariableContext> Data) 114 : TypedBaton(std::move(Data)) {} 115 }; 116 117 bool SetupVariableWatchpointDisabler(lldb::StackFrameSP frame_sp) const; 118 119 /// Callback routine to disable the watchpoint set on a local variable when 120 /// it goes out of scope. 121 static bool VariableWatchpointDisabler( 122 void *baton, lldb_private::StoppointCallbackContext *context, 123 lldb::user_id_t break_id, lldb::user_id_t break_loc_id); 124 125 void GetDescription(Stream *s, lldb::DescriptionLevel level); 126 void Dump(Stream *s) const override; 127 void DumpSnapshots(Stream *s, const char *prefix = nullptr) const; 128 void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const; 129 Target &GetTarget() { return m_target; } 130 const Status &GetError() { return m_error; } 131 132 /// Returns the WatchpointOptions structure set for this watchpoint. 133 /// 134 /// \return 135 /// A pointer to this watchpoint's WatchpointOptions. 136 WatchpointOptions *GetOptions() { return &m_options; } 137 138 /// Set the callback action invoked when the watchpoint is hit. 139 /// 140 /// \param[in] callback 141 /// The method that will get called when the watchpoint is hit. 142 /// \param[in] callback_baton 143 /// A void * pointer that will get passed back to the callback function. 144 /// \param[in] is_synchronous 145 /// If \b true the callback will be run on the private event thread 146 /// before the stop event gets reported. If false, the callback will get 147 /// handled on the public event thread after the stop has been posted. 148 void SetCallback(WatchpointHitCallback callback, void *callback_baton, 149 bool is_synchronous = false); 150 151 void SetCallback(WatchpointHitCallback callback, 152 const lldb::BatonSP &callback_baton_sp, 153 bool is_synchronous = false); 154 155 void ClearCallback(); 156 157 /// Invoke the callback action when the watchpoint is hit. 158 /// 159 /// \param[in] context 160 /// Described the watchpoint event. 161 /// 162 /// \return 163 /// \b true if the target should stop at this watchpoint and \b false not. 164 bool InvokeCallback(StoppointCallbackContext *context); 165 166 // Condition 167 /// Set the watchpoint's condition. 168 /// 169 /// \param[in] condition 170 /// The condition expression to evaluate when the watchpoint is hit. 171 /// Pass in nullptr to clear the condition. 172 void SetCondition(const char *condition); 173 174 /// Return a pointer to the text of the condition expression. 175 /// 176 /// \return 177 /// A pointer to the condition expression text, or nullptr if no 178 // condition has been set. 179 const char *GetConditionText() const; 180 181 void TurnOnEphemeralMode(); 182 183 void TurnOffEphemeralMode(); 184 185 bool IsDisabledDuringEphemeralMode(); 186 187 const CompilerType &GetCompilerType() { return m_type; } 188 189 private: 190 friend class Target; 191 friend class WatchpointList; 192 friend class StopInfoWatchpoint; // This needs to call UndoHitCount() 193 194 void ResetHistoricValues() { 195 m_old_value_sp.reset(); 196 m_new_value_sp.reset(); 197 } 198 199 void UndoHitCount() { m_hit_counter.Decrement(); } 200 201 Target &m_target; 202 bool m_enabled; // Is this watchpoint enabled 203 bool m_is_hardware; // Is this a hardware watchpoint 204 bool m_is_watch_variable; // True if set via 'watchpoint set variable'. 205 bool m_is_ephemeral; // True if the watchpoint is in the ephemeral mode, 206 // meaning that it is 207 // undergoing a pair of temporary disable/enable actions to avoid recursively 208 // triggering further watchpoint events. 209 uint32_t m_disabled_count; // Keep track of the count that the watchpoint is 210 // disabled while in ephemeral mode. 211 // At the end of the ephemeral mode when the watchpoint is to be enabled 212 // again, we check the count, if it is more than 1, it means the user- 213 // supplied actions actually want the watchpoint to be disabled! 214 uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from 215 m_watch_write : 1; // 1 if we stop when the watched data is written to 216 uint32_t m_ignore_count; // Number of times to ignore this watchpoint 217 std::string m_decl_str; // Declaration information, if any. 218 std::string m_watch_spec_str; // Spec for the watchpoint. 219 lldb::ValueObjectSP m_old_value_sp; 220 lldb::ValueObjectSP m_new_value_sp; 221 CompilerType m_type; 222 Status m_error; // An error object describing errors associated with this 223 // watchpoint. 224 WatchpointOptions 225 m_options; // Settable watchpoint options, which is a delegate to handle 226 // the callback machinery. 227 bool m_being_created; 228 229 std::unique_ptr<UserExpression> m_condition_up; // The condition to test. 230 231 void SetID(lldb::watch_id_t id) { m_id = id; } 232 233 void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind); 234 235 void SendWatchpointChangedEvent(WatchpointEventData *data); 236 237 Watchpoint(const Watchpoint &) = delete; 238 const Watchpoint &operator=(const Watchpoint &) = delete; 239 }; 240 241 } // namespace lldb_private 242 243 #endif // LLDB_BREAKPOINT_WATCHPOINT_H 244