| 1 | //===-- WatchpointList.cpp ------------------------------------------------===// |
| 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 | #include "lldb/Breakpoint/WatchpointList.h" |
| 10 | #include "lldb/Breakpoint/Watchpoint.h" |
| 11 | |
| 12 | using namespace lldb; |
| 13 | using namespace lldb_private; |
| 14 | |
| 15 | WatchpointList::WatchpointList() = default; |
| 16 | |
| 17 | WatchpointList::~WatchpointList() = default; |
| 18 | |
| 19 | // Add a watchpoint to the list. |
| 20 | lldb::watch_id_t WatchpointList::Add(const WatchpointSP &wp_sp, bool notify) { |
| 21 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 22 | wp_sp->SetID(++m_next_wp_id); |
| 23 | m_watchpoints.push_back(x: wp_sp); |
| 24 | if (notify) { |
| 25 | if (wp_sp->GetTarget().EventTypeHasListeners( |
| 26 | event_type: Target::eBroadcastBitWatchpointChanged)) { |
| 27 | auto data_sp = std::make_shared<Watchpoint::WatchpointEventData>( |
| 28 | args: eWatchpointEventTypeAdded, args: wp_sp); |
| 29 | wp_sp->GetTarget().BroadcastEvent(event_type: Target::eBroadcastBitWatchpointChanged, |
| 30 | event_data_sp: data_sp); |
| 31 | } |
| 32 | } |
| 33 | return wp_sp->GetID(); |
| 34 | } |
| 35 | |
| 36 | void WatchpointList::Dump(Stream *s) const { |
| 37 | DumpWithLevel(s, description_level: lldb::eDescriptionLevelBrief); |
| 38 | } |
| 39 | |
| 40 | void WatchpointList::DumpWithLevel( |
| 41 | Stream *s, lldb::DescriptionLevel description_level) const { |
| 42 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 43 | s->Printf(format: "%p: " , static_cast<const void *>(this)); |
| 44 | // s->Indent(); |
| 45 | s->Printf(format: "WatchpointList with %" PRIu64 " Watchpoints:\n" , |
| 46 | (uint64_t)m_watchpoints.size()); |
| 47 | s->IndentMore(); |
| 48 | wp_collection::const_iterator pos, end = m_watchpoints.end(); |
| 49 | for (pos = m_watchpoints.begin(); pos != end; ++pos) |
| 50 | (*pos)->DumpWithLevel(s, description_level); |
| 51 | s->IndentLess(); |
| 52 | } |
| 53 | |
| 54 | const WatchpointSP WatchpointList::FindByAddress(lldb::addr_t addr) const { |
| 55 | WatchpointSP wp_sp; |
| 56 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 57 | if (!m_watchpoints.empty()) { |
| 58 | wp_collection::const_iterator pos, end = m_watchpoints.end(); |
| 59 | for (pos = m_watchpoints.begin(); pos != end; ++pos) { |
| 60 | lldb::addr_t wp_addr = (*pos)->GetLoadAddress(); |
| 61 | uint32_t wp_bytesize = (*pos)->GetByteSize(); |
| 62 | if ((wp_addr <= addr) && ((wp_addr + wp_bytesize) > addr)) { |
| 63 | wp_sp = *pos; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return wp_sp; |
| 70 | } |
| 71 | |
| 72 | const WatchpointSP WatchpointList::FindBySpec(std::string spec) const { |
| 73 | WatchpointSP wp_sp; |
| 74 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 75 | if (!m_watchpoints.empty()) { |
| 76 | wp_collection::const_iterator pos, end = m_watchpoints.end(); |
| 77 | for (pos = m_watchpoints.begin(); pos != end; ++pos) |
| 78 | if ((*pos)->GetWatchSpec() == spec) { |
| 79 | wp_sp = *pos; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return wp_sp; |
| 85 | } |
| 86 | |
| 87 | class WatchpointIDMatches { |
| 88 | public: |
| 89 | WatchpointIDMatches(lldb::watch_id_t watch_id) : m_watch_id(watch_id) {} |
| 90 | |
| 91 | bool operator()(const WatchpointSP &wp) const { |
| 92 | return m_watch_id == wp->GetID(); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | const lldb::watch_id_t m_watch_id; |
| 97 | }; |
| 98 | |
| 99 | WatchpointList::wp_collection::iterator |
| 100 | WatchpointList::GetIDIterator(lldb::watch_id_t watch_id) { |
| 101 | return llvm::find_if(Range&: m_watchpoints, // Search full range |
| 102 | P: WatchpointIDMatches(watch_id)); // Predicate |
| 103 | } |
| 104 | |
| 105 | WatchpointList::wp_collection::const_iterator |
| 106 | WatchpointList::GetIDConstIterator(lldb::watch_id_t watch_id) const { |
| 107 | return llvm::find_if(Range: m_watchpoints, // Search full range |
| 108 | P: WatchpointIDMatches(watch_id)); // Predicate |
| 109 | } |
| 110 | |
| 111 | WatchpointSP WatchpointList::FindByID(lldb::watch_id_t watch_id) const { |
| 112 | WatchpointSP wp_sp; |
| 113 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 114 | wp_collection::const_iterator pos = GetIDConstIterator(watch_id); |
| 115 | if (pos != m_watchpoints.end()) |
| 116 | wp_sp = *pos; |
| 117 | |
| 118 | return wp_sp; |
| 119 | } |
| 120 | |
| 121 | lldb::watch_id_t WatchpointList::FindIDByAddress(lldb::addr_t addr) { |
| 122 | WatchpointSP wp_sp = FindByAddress(addr); |
| 123 | if (wp_sp) { |
| 124 | return wp_sp->GetID(); |
| 125 | } |
| 126 | return LLDB_INVALID_WATCH_ID; |
| 127 | } |
| 128 | |
| 129 | lldb::watch_id_t WatchpointList::FindIDBySpec(std::string spec) { |
| 130 | WatchpointSP wp_sp = FindBySpec(spec); |
| 131 | if (wp_sp) { |
| 132 | return wp_sp->GetID(); |
| 133 | } |
| 134 | return LLDB_INVALID_WATCH_ID; |
| 135 | } |
| 136 | |
| 137 | WatchpointSP WatchpointList::GetByIndex(uint32_t i) { |
| 138 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 139 | WatchpointSP wp_sp; |
| 140 | if (i < m_watchpoints.size()) { |
| 141 | wp_collection::const_iterator pos = m_watchpoints.begin(); |
| 142 | std::advance(i&: pos, n: i); |
| 143 | wp_sp = *pos; |
| 144 | } |
| 145 | return wp_sp; |
| 146 | } |
| 147 | |
| 148 | const WatchpointSP WatchpointList::GetByIndex(uint32_t i) const { |
| 149 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 150 | WatchpointSP wp_sp; |
| 151 | if (i < m_watchpoints.size()) { |
| 152 | wp_collection::const_iterator pos = m_watchpoints.begin(); |
| 153 | std::advance(i&: pos, n: i); |
| 154 | wp_sp = *pos; |
| 155 | } |
| 156 | return wp_sp; |
| 157 | } |
| 158 | |
| 159 | std::vector<lldb::watch_id_t> WatchpointList::GetWatchpointIDs() const { |
| 160 | std::vector<lldb::watch_id_t> IDs; |
| 161 | wp_collection::const_iterator pos, end = m_watchpoints.end(); |
| 162 | for (pos = m_watchpoints.begin(); pos != end; ++pos) |
| 163 | IDs.push_back(x: (*pos)->GetID()); |
| 164 | return IDs; |
| 165 | } |
| 166 | |
| 167 | bool WatchpointList::Remove(lldb::watch_id_t watch_id, bool notify) { |
| 168 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 169 | wp_collection::iterator pos = GetIDIterator(watch_id); |
| 170 | if (pos != m_watchpoints.end()) { |
| 171 | WatchpointSP wp_sp = *pos; |
| 172 | if (notify) { |
| 173 | if (wp_sp->GetTarget().EventTypeHasListeners( |
| 174 | event_type: Target::eBroadcastBitWatchpointChanged)) { |
| 175 | auto data_sp = std::make_shared<Watchpoint::WatchpointEventData>( |
| 176 | args: eWatchpointEventTypeRemoved, args&: wp_sp); |
| 177 | wp_sp->GetTarget().BroadcastEvent( |
| 178 | event_type: Target::eBroadcastBitWatchpointChanged, event_data_sp: data_sp); |
| 179 | } |
| 180 | } |
| 181 | m_watchpoints.erase(position: pos); |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | uint32_t WatchpointList::GetHitCount() const { |
| 188 | uint32_t hit_count = 0; |
| 189 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 190 | wp_collection::const_iterator pos, end = m_watchpoints.end(); |
| 191 | for (pos = m_watchpoints.begin(); pos != end; ++pos) |
| 192 | hit_count += (*pos)->GetHitCount(); |
| 193 | return hit_count; |
| 194 | } |
| 195 | |
| 196 | bool WatchpointList::ShouldStop(StoppointCallbackContext *context, |
| 197 | lldb::watch_id_t watch_id) { |
| 198 | |
| 199 | WatchpointSP wp_sp = FindByID(watch_id); |
| 200 | if (wp_sp) { |
| 201 | // Let the Watchpoint decide if it should stop here (could not have reached |
| 202 | // it's target hit count yet, or it could have a callback that decided it |
| 203 | // shouldn't stop. |
| 204 | return wp_sp->ShouldStop(context); |
| 205 | } |
| 206 | // We should stop here since this Watchpoint isn't valid anymore or it |
| 207 | // doesn't exist. |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | void WatchpointList::GetDescription(Stream *s, lldb::DescriptionLevel level) { |
| 212 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 213 | wp_collection::iterator pos, end = m_watchpoints.end(); |
| 214 | |
| 215 | for (pos = m_watchpoints.begin(); pos != end; ++pos) { |
| 216 | s->Printf(format: " " ); |
| 217 | (*pos)->Dump(s); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void WatchpointList::SetEnabledAll(bool enabled) { |
| 222 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 223 | |
| 224 | wp_collection::iterator pos, end = m_watchpoints.end(); |
| 225 | for (pos = m_watchpoints.begin(); pos != end; ++pos) |
| 226 | (*pos)->SetEnabled(enabled); |
| 227 | } |
| 228 | |
| 229 | void WatchpointList::RemoveAll(bool notify) { |
| 230 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 231 | if (notify) { |
| 232 | |
| 233 | { |
| 234 | wp_collection::iterator pos, end = m_watchpoints.end(); |
| 235 | for (pos = m_watchpoints.begin(); pos != end; ++pos) { |
| 236 | if ((*pos)->GetTarget().EventTypeHasListeners( |
| 237 | event_type: Target::eBroadcastBitWatchpointChanged)) { |
| 238 | auto data_sp = std::make_shared<Watchpoint::WatchpointEventData>( |
| 239 | args: eWatchpointEventTypeRemoved, args&: *pos); |
| 240 | (*pos)->GetTarget().BroadcastEvent( |
| 241 | event_type: Target::eBroadcastBitWatchpointChanged, event_data_sp: data_sp); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | m_watchpoints.clear(); |
| 247 | } |
| 248 | |
| 249 | void WatchpointList::GetListMutex( |
| 250 | std::unique_lock<std::recursive_mutex> &lock) { |
| 251 | lock = std::unique_lock<std::recursive_mutex>(m_mutex); |
| 252 | } |
| 253 | |