1 //===-- WatchpointList.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_WATCHPOINTLIST_H 10 #define LLDB_BREAKPOINT_WATCHPOINTLIST_H 11 12 #include <list> 13 #include <mutex> 14 #include <vector> 15 16 #include "lldb/Core/Address.h" 17 #include "lldb/Utility/Iterable.h" 18 #include "lldb/lldb-private.h" 19 20 namespace lldb_private { 21 22 /// \class WatchpointList WatchpointList.h "lldb/Breakpoint/WatchpointList.h" 23 /// This class is used by Watchpoint to manage a list of watchpoints, 24 // each watchpoint in the list has a unique ID, and is unique by Address as 25 // well. 26 27 class WatchpointList { 28 // Only Target can make the watchpoint list, or add elements to it. This is 29 // not just some random collection of watchpoints. Rather, the act of adding 30 // the watchpoint to this list sets its ID. 31 friend class Watchpoint; 32 friend class Target; 33 34 public: 35 /// Default constructor makes an empty list. 36 WatchpointList(); 37 38 /// Destructor, currently does nothing. 39 ~WatchpointList(); 40 41 typedef std::list<lldb::WatchpointSP> wp_collection; 42 typedef LockingAdaptedIterable<std::recursive_mutex, wp_collection> 43 WatchpointIterable; 44 45 /// Add a Watchpoint to the list. 46 /// 47 /// \param[in] wp_sp 48 /// A shared pointer to a watchpoint being added to the list. 49 /// 50 /// \return 51 /// The ID of the Watchpoint in the list. 52 lldb::watch_id_t Add(const lldb::WatchpointSP &wp_sp, bool notify); 53 54 /// Standard "Dump" method. 55 void Dump(Stream *s) const; 56 57 /// Dump with lldb::DescriptionLevel. 58 void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const; 59 60 /// Returns a shared pointer to the watchpoint at address \a addr - const 61 /// version. 62 /// 63 /// \param[in] addr 64 /// The address to look for. 65 /// 66 /// \result 67 /// A shared pointer to the watchpoint. May contain a NULL 68 /// pointer if the watchpoint doesn't exist. 69 const lldb::WatchpointSP FindByAddress(lldb::addr_t addr) const; 70 71 /// Returns a shared pointer to the watchpoint with watchpoint spec \a spec 72 /// - const version. 73 /// 74 /// \param[in] spec 75 /// The watchpoint spec to look for. 76 /// 77 /// \result 78 /// A shared pointer to the watchpoint. May contain a NULL 79 /// pointer if the watchpoint doesn't exist. 80 const lldb::WatchpointSP FindBySpec(std::string spec) const; 81 82 /// Returns a shared pointer to the watchpoint with id \a watchID, const 83 /// version. 84 /// 85 /// \param[in] watchID 86 /// The watchpoint location ID to seek for. 87 /// 88 /// \result 89 /// A shared pointer to the watchpoint. May contain a NULL 90 /// pointer if the watchpoint doesn't exist. 91 lldb::WatchpointSP FindByID(lldb::watch_id_t watchID) const; 92 93 /// Returns the watchpoint id to the watchpoint at address \a addr. 94 /// 95 /// \param[in] addr 96 /// The address to match. 97 /// 98 /// \result 99 /// The ID of the watchpoint, or LLDB_INVALID_WATCH_ID. 100 lldb::watch_id_t FindIDByAddress(lldb::addr_t addr); 101 102 /// Returns the watchpoint id to the watchpoint with watchpoint spec \a 103 /// spec. 104 /// 105 /// \param[in] spec 106 /// The watchpoint spec to match. 107 /// 108 /// \result 109 /// The ID of the watchpoint, or LLDB_INVALID_WATCH_ID. 110 lldb::watch_id_t FindIDBySpec(std::string spec); 111 112 /// Returns a shared pointer to the watchpoint with index \a i. 113 /// 114 /// \param[in] i 115 /// The watchpoint index to seek for. 116 /// 117 /// \result 118 /// A shared pointer to the watchpoint. May contain a NULL pointer if 119 /// the watchpoint doesn't exist. 120 lldb::WatchpointSP GetByIndex(uint32_t i); 121 122 /// Returns a shared pointer to the watchpoint with index \a i, const 123 /// version. 124 /// 125 /// \param[in] i 126 /// The watchpoint index to seek for. 127 /// 128 /// \result 129 /// A shared pointer to the watchpoint. May contain a NULL pointer if 130 /// the watchpoint location doesn't exist. 131 const lldb::WatchpointSP GetByIndex(uint32_t i) const; 132 133 /// Removes the watchpoint given by \b watchID from this list. 134 /// 135 /// \param[in] watchID 136 /// The watchpoint ID to remove. 137 /// 138 /// \result 139 /// \b true if the watchpoint \a watchID was in the list. 140 bool Remove(lldb::watch_id_t watchID, bool notify); 141 142 /// Returns the number hit count of all watchpoints in this list. 143 /// 144 /// \result 145 /// Hit count of all watchpoints in this list. 146 uint32_t GetHitCount() const; 147 148 /// Enquires of the watchpoint in this list with ID \a watchID whether we 149 /// should stop. 150 /// 151 /// \param[in] context 152 /// This contains the information about this stop. 153 /// 154 /// \param[in] watchID 155 /// This watch ID that we hit. 156 /// 157 /// \return 158 /// \b true if we should stop, \b false otherwise. 159 bool ShouldStop(StoppointCallbackContext *context, lldb::watch_id_t watchID); 160 161 /// Returns the number of elements in this watchpoint list. 162 /// 163 /// \result 164 /// The number of elements. GetSize()165 size_t GetSize() const { 166 std::lock_guard<std::recursive_mutex> guard(m_mutex); 167 return m_watchpoints.size(); 168 } 169 170 /// Print a description of the watchpoints in this list to the stream \a s. 171 /// 172 /// \param[in] s 173 /// The stream to which to print the description. 174 /// 175 /// \param[in] level 176 /// The description level that indicates the detail level to 177 /// provide. 178 /// 179 /// \see lldb::DescriptionLevel 180 void GetDescription(Stream *s, lldb::DescriptionLevel level); 181 182 void SetEnabledAll(bool enabled); 183 184 void RemoveAll(bool notify); 185 186 /// Sets the passed in Locker to hold the Watchpoint List mutex. 187 /// 188 /// \param[in] lock 189 /// The locker object that is set. 190 void GetListMutex(std::unique_lock<std::recursive_mutex> &lock); 191 Watchpoints()192 WatchpointIterable Watchpoints() const { 193 return WatchpointIterable(m_watchpoints, m_mutex); 194 } 195 196 protected: 197 typedef std::vector<lldb::watch_id_t> id_vector; 198 199 id_vector GetWatchpointIDs() const; 200 201 wp_collection::iterator GetIDIterator(lldb::watch_id_t watchID); 202 203 wp_collection::const_iterator 204 GetIDConstIterator(lldb::watch_id_t watchID) const; 205 206 wp_collection m_watchpoints; 207 mutable std::recursive_mutex m_mutex; 208 209 lldb::watch_id_t m_next_wp_id = 0; 210 }; 211 212 } // namespace lldb_private 213 214 #endif // LLDB_BREAKPOINT_WATCHPOINTLIST_H 215