1 //===-- Watchpoint.h --------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_Watchpoint_h_ 11 #define liblldb_Watchpoint_h_ 12 13 // C Includes 14 15 // C++ Includes 16 #include <list> 17 #include <string> 18 19 // Other libraries and framework includes 20 21 // Project includes 22 #include "lldb/lldb-private.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Core/UserID.h" 25 #include "lldb/Breakpoint/WatchpointOptions.h" 26 #include "lldb/Breakpoint/StoppointLocation.h" 27 28 namespace lldb_private { 29 30 class Watchpoint : 31 public std::enable_shared_from_this<Watchpoint>, 32 public StoppointLocation 33 { 34 public: 35 36 class WatchpointEventData : 37 public EventData 38 { 39 public: 40 41 static const ConstString & 42 GetFlavorString (); 43 44 virtual const ConstString & 45 GetFlavor () const; 46 47 WatchpointEventData (lldb::WatchpointEventType sub_type, 48 const lldb::WatchpointSP &new_watchpoint_sp); 49 50 virtual 51 ~WatchpointEventData(); 52 53 lldb::WatchpointEventType 54 GetWatchpointEventType () const; 55 56 lldb::WatchpointSP & 57 GetWatchpoint (); 58 59 virtual void 60 Dump (Stream *s) const; 61 62 static lldb::WatchpointEventType 63 GetWatchpointEventTypeFromEvent (const lldb::EventSP &event_sp); 64 65 static lldb::WatchpointSP 66 GetWatchpointFromEvent (const lldb::EventSP &event_sp); 67 68 static const WatchpointEventData * 69 GetEventDataFromEvent (const Event *event_sp); 70 71 private: 72 73 lldb::WatchpointEventType m_watchpoint_event; 74 lldb::WatchpointSP m_new_watchpoint_sp; 75 76 DISALLOW_COPY_AND_ASSIGN (WatchpointEventData); 77 }; 78 79 Watchpoint (Target& target, lldb::addr_t addr, uint32_t size, const ClangASTType *type, bool hardware = true); 80 ~Watchpoint (); 81 82 void 83 IncrementFalseAlarmsAndReviseHitCount(); 84 85 bool 86 IsEnabled () const; 87 88 void 89 SetEnabled (bool enabled, bool notify = true); 90 91 virtual bool 92 IsHardware () const; 93 94 virtual bool 95 ShouldStop (StoppointCallbackContext *context); 96 97 bool WatchpointRead () const; 98 bool WatchpointWrite () const; 99 uint32_t GetIgnoreCount () const; 100 void SetIgnoreCount (uint32_t n); 101 void SetWatchpointType (uint32_t type, bool notify = true); 102 void SetDeclInfo (const std::string &str); 103 std::string GetWatchSpec(); 104 void SetWatchSpec (const std::string &str); 105 106 // Snapshot management interface. 107 bool IsWatchVariable() const; 108 void SetWatchVariable(bool val); 109 bool CaptureWatchedValue (const ExecutionContext &exe_ctx); 110 111 void GetDescription (Stream *s, lldb::DescriptionLevel level); 112 void Dump (Stream *s) const; 113 void DumpSnapshots (Stream *s, const char * prefix = NULL) const; 114 void DumpWithLevel (Stream *s, lldb::DescriptionLevel description_level) const; GetTarget()115 Target &GetTarget() { return m_target; } GetError()116 const Error &GetError() { return m_error; } 117 118 //------------------------------------------------------------------ 119 /// Returns the WatchpointOptions structure set for this watchpoint. 120 /// 121 /// @return 122 /// A pointer to this watchpoint's WatchpointOptions. 123 //------------------------------------------------------------------ 124 WatchpointOptions * GetOptions()125 GetOptions () { return &m_options; } 126 127 //------------------------------------------------------------------ 128 /// Set the callback action invoked when the watchpoint is hit. 129 /// 130 /// @param[in] callback 131 /// The method that will get called when the watchpoint is hit. 132 /// @param[in] callback_baton 133 /// A void * pointer that will get passed back to the callback function. 134 /// @param[in] is_synchronous 135 /// If \b true the callback will be run on the private event thread 136 /// before the stop event gets reported. If false, the callback will get 137 /// handled on the public event thead after the stop has been posted. 138 /// 139 /// @return 140 /// \b true if the process should stop when you hit the watchpoint. 141 /// \b false if it should continue. 142 //------------------------------------------------------------------ 143 void 144 SetCallback (WatchpointHitCallback callback, 145 void *callback_baton, 146 bool is_synchronous = false); 147 148 void 149 SetCallback (WatchpointHitCallback callback, 150 const lldb::BatonSP &callback_baton_sp, 151 bool is_synchronous = false); 152 153 void ClearCallback(); 154 155 //------------------------------------------------------------------ 156 /// Invoke the callback action when the watchpoint is hit. 157 /// 158 /// @param[in] context 159 /// Described the watchpoint event. 160 /// 161 /// @return 162 /// \b true if the target should stop at this watchpoint and \b false not. 163 //------------------------------------------------------------------ 164 bool 165 InvokeCallback (StoppointCallbackContext *context); 166 167 //------------------------------------------------------------------ 168 // Condition 169 //------------------------------------------------------------------ 170 //------------------------------------------------------------------ 171 /// Set the watchpoint's condition. 172 /// 173 /// @param[in] condition 174 /// The condition expression to evaluate when the watchpoint is hit. 175 /// Pass in NULL to clear the condition. 176 //------------------------------------------------------------------ 177 void SetCondition (const char *condition); 178 179 //------------------------------------------------------------------ 180 /// Return a pointer to the text of the condition expression. 181 /// 182 /// @return 183 /// A pointer to the condition expression text, or NULL if no 184 // condition has been set. 185 //------------------------------------------------------------------ 186 const char *GetConditionText () const; 187 188 void 189 TurnOnEphemeralMode(); 190 191 void 192 TurnOffEphemeralMode(); 193 194 bool 195 IsDisabledDuringEphemeralMode(); 196 197 const ClangASTType & GetClangASTType()198 GetClangASTType() 199 { 200 return m_type; 201 } 202 203 204 private: 205 friend class Target; 206 friend class WatchpointList; 207 ResetHitCount()208 void ResetHitCount() { m_hit_count = 0; } 209 210 Target &m_target; 211 bool m_enabled; // Is this watchpoint enabled 212 bool m_is_hardware; // Is this a hardware watchpoint 213 bool m_is_watch_variable; // True if set via 'watchpoint set variable'. 214 bool m_is_ephemeral; // True if the watchpoint is in the ephemeral mode, meaning that it is 215 // undergoing a pair of temporary disable/enable actions to avoid recursively 216 // triggering further watchpoint events. 217 uint32_t m_disabled_count; // Keep track of the count that the watchpoint is disabled while in ephemeral mode. 218 // At the end of the ephemeral mode when the watchpoint is to be enabled agian, 219 // we check the count, if it is more than 1, it means the user-supplied actions 220 // actually want the watchpoint to be disabled! 221 uint32_t m_watch_read:1, // 1 if we stop when the watched data is read from 222 m_watch_write:1, // 1 if we stop when the watched data is written to 223 m_watch_was_read:1, // Set to 1 when watchpoint is hit for a read access 224 m_watch_was_written:1; // Set to 1 when watchpoint is hit for a write access 225 uint32_t m_ignore_count; // Number of times to ignore this watchpoint 226 uint32_t m_false_alarms; // Number of false alarms. 227 std::string m_decl_str; // Declaration information, if any. 228 std::string m_watch_spec_str; // Spec for the watchpoint. 229 lldb::ValueObjectSP m_old_value_sp; 230 lldb::ValueObjectSP m_new_value_sp; 231 ClangASTType m_type; 232 Error m_error; // An error object describing errors associated with this watchpoint. 233 WatchpointOptions m_options; // Settable watchpoint options, which is a delegate to handle 234 // the callback machinery. 235 bool m_being_created; 236 237 std::unique_ptr<ClangUserExpression> m_condition_ap; // The condition to test. 238 SetID(lldb::watch_id_t id)239 void SetID(lldb::watch_id_t id) { m_loc_id = id; } 240 241 void 242 SendWatchpointChangedEvent (lldb::WatchpointEventType eventKind); 243 244 void 245 SendWatchpointChangedEvent (WatchpointEventData *data); 246 247 DISALLOW_COPY_AND_ASSIGN (Watchpoint); 248 }; 249 250 } // namespace lldb_private 251 252 #endif // liblldb_Watchpoint_h_ 253