1 //===-- WatchpointList.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Breakpoint/WatchpointList.h"
11 #include "lldb/Breakpoint/Watchpoint.h"
14 using namespace lldb_private;
16 WatchpointList::WatchpointList()
17 : m_watchpoints(), m_mutex(), m_next_wp_id(0) {}
19 WatchpointList::~WatchpointList() {}
21 // Add a watchpoint to the list.
22 lldb::watch_id_t WatchpointList::Add(const WatchpointSP &wp_sp, bool notify) {
23 std::lock_guard<std::recursive_mutex> guard(m_mutex);
24 wp_sp->SetID(++m_next_wp_id);
25 m_watchpoints.push_back(wp_sp);
27 if (wp_sp->GetTarget().EventTypeHasListeners(
28 Target::eBroadcastBitWatchpointChanged))
29 wp_sp->GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged,
30 new Watchpoint::WatchpointEventData(
31 eWatchpointEventTypeAdded, wp_sp));
33 return wp_sp->GetID();
36 void WatchpointList::Dump(Stream *s) const {
37 DumpWithLevel(s, lldb::eDescriptionLevelBrief);
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("%p: ", static_cast<const void *>(this));
45 s->Printf("WatchpointList with %" PRIu64 " Watchpoints:\n",
46 (uint64_t)m_watchpoints.size());
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);
54 const WatchpointSP WatchpointList::FindByAddress(lldb::addr_t addr) const {
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)) {
72 const WatchpointSP WatchpointList::FindBySpec(std::string spec) const {
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) {
87 class WatchpointIDMatches {
89 WatchpointIDMatches(lldb::watch_id_t watch_id) : m_watch_id(watch_id) {}
91 bool operator()(const WatchpointSP &wp) const {
92 return m_watch_id == wp->GetID();
96 const lldb::watch_id_t m_watch_id;
99 WatchpointList::wp_collection::iterator
100 WatchpointList::GetIDIterator(lldb::watch_id_t watch_id) {
101 return std::find_if(m_watchpoints.begin(),
102 m_watchpoints.end(), // Search full range
103 WatchpointIDMatches(watch_id)); // Predicate
106 WatchpointList::wp_collection::const_iterator
107 WatchpointList::GetIDConstIterator(lldb::watch_id_t watch_id) const {
108 return std::find_if(m_watchpoints.begin(),
109 m_watchpoints.end(), // Search full range
110 WatchpointIDMatches(watch_id)); // Predicate
113 WatchpointSP WatchpointList::FindByID(lldb::watch_id_t watch_id) const {
115 std::lock_guard<std::recursive_mutex> guard(m_mutex);
116 wp_collection::const_iterator pos = GetIDConstIterator(watch_id);
117 if (pos != m_watchpoints.end())
123 lldb::watch_id_t WatchpointList::FindIDByAddress(lldb::addr_t addr) {
124 WatchpointSP wp_sp = FindByAddress(addr);
126 return wp_sp->GetID();
128 return LLDB_INVALID_WATCH_ID;
131 lldb::watch_id_t WatchpointList::FindIDBySpec(std::string spec) {
132 WatchpointSP wp_sp = FindBySpec(spec);
134 return wp_sp->GetID();
136 return LLDB_INVALID_WATCH_ID;
139 WatchpointSP WatchpointList::GetByIndex(uint32_t i) {
140 std::lock_guard<std::recursive_mutex> guard(m_mutex);
142 if (i < m_watchpoints.size()) {
143 wp_collection::const_iterator pos = m_watchpoints.begin();
144 std::advance(pos, i);
150 const WatchpointSP WatchpointList::GetByIndex(uint32_t i) const {
151 std::lock_guard<std::recursive_mutex> guard(m_mutex);
153 if (i < m_watchpoints.size()) {
154 wp_collection::const_iterator pos = m_watchpoints.begin();
155 std::advance(pos, i);
161 std::vector<lldb::watch_id_t> WatchpointList::GetWatchpointIDs() const {
162 std::vector<lldb::watch_id_t> IDs;
163 wp_collection::const_iterator pos, end = m_watchpoints.end();
164 for (pos = m_watchpoints.begin(); pos != end; ++pos)
165 IDs.push_back((*pos)->GetID());
169 bool WatchpointList::Remove(lldb::watch_id_t watch_id, bool notify) {
170 std::lock_guard<std::recursive_mutex> guard(m_mutex);
171 wp_collection::iterator pos = GetIDIterator(watch_id);
172 if (pos != m_watchpoints.end()) {
173 WatchpointSP wp_sp = *pos;
175 if (wp_sp->GetTarget().EventTypeHasListeners(
176 Target::eBroadcastBitWatchpointChanged))
177 wp_sp->GetTarget().BroadcastEvent(
178 Target::eBroadcastBitWatchpointChanged,
179 new Watchpoint::WatchpointEventData(eWatchpointEventTypeRemoved,
182 m_watchpoints.erase(pos);
188 uint32_t WatchpointList::GetHitCount() const {
189 uint32_t hit_count = 0;
190 std::lock_guard<std::recursive_mutex> guard(m_mutex);
191 wp_collection::const_iterator pos, end = m_watchpoints.end();
192 for (pos = m_watchpoints.begin(); pos != end; ++pos)
193 hit_count += (*pos)->GetHitCount();
197 bool WatchpointList::ShouldStop(StoppointCallbackContext *context,
198 lldb::watch_id_t watch_id) {
200 WatchpointSP wp_sp = FindByID(watch_id);
202 // Let the Watchpoint decide if it should stop here (could not have reached
203 // it's target hit count yet, or it could have a callback that decided it
205 return wp_sp->ShouldStop(context);
207 // We should stop here since this Watchpoint isn't valid anymore or it
212 void WatchpointList::GetDescription(Stream *s, lldb::DescriptionLevel level) {
213 std::lock_guard<std::recursive_mutex> guard(m_mutex);
214 wp_collection::iterator pos, end = m_watchpoints.end();
216 for (pos = m_watchpoints.begin(); pos != end; ++pos) {
222 void WatchpointList::SetEnabledAll(bool enabled) {
223 std::lock_guard<std::recursive_mutex> guard(m_mutex);
225 wp_collection::iterator pos, end = m_watchpoints.end();
226 for (pos = m_watchpoints.begin(); pos != end; ++pos)
227 (*pos)->SetEnabled(enabled);
230 void WatchpointList::RemoveAll(bool notify) {
231 std::lock_guard<std::recursive_mutex> guard(m_mutex);
235 wp_collection::iterator pos, end = m_watchpoints.end();
236 for (pos = m_watchpoints.begin(); pos != end; ++pos) {
237 if ((*pos)->GetTarget().EventTypeHasListeners(
238 Target::eBroadcastBitBreakpointChanged)) {
239 (*pos)->GetTarget().BroadcastEvent(
240 Target::eBroadcastBitWatchpointChanged,
241 new Watchpoint::WatchpointEventData(eWatchpointEventTypeRemoved,
247 m_watchpoints.clear();
250 void WatchpointList::GetListMutex(
251 std::unique_lock<std::recursive_mutex> &lock) {
252 lock = std::unique_lock<std::recursive_mutex>(m_mutex);