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 //===----------------------------------------------------------------------===//
13 // Other libraries and framework includes
15 #include "lldb/Breakpoint/WatchpointList.h"
16 #include "lldb/Breakpoint/Watchpoint.h"
19 using namespace lldb_private;
21 WatchpointList::WatchpointList() :
23 m_mutex (Mutex::eMutexTypeRecursive),
28 WatchpointList::~WatchpointList()
32 // Add a watchpoint to the list.
34 WatchpointList::Add (const WatchpointSP &wp_sp, bool notify)
36 Mutex::Locker locker (m_mutex);
37 wp_sp->SetID(++m_next_wp_id);
38 m_watchpoints.push_back(wp_sp);
41 if (wp_sp->GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
42 wp_sp->GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged,
43 new Watchpoint::WatchpointEventData (eWatchpointEventTypeAdded, wp_sp));
45 return wp_sp->GetID();
49 WatchpointList::Dump (Stream *s) const
51 DumpWithLevel(s, lldb::eDescriptionLevelBrief);
55 WatchpointList::DumpWithLevel (Stream *s, lldb::DescriptionLevel description_level) const
57 Mutex::Locker locker (m_mutex);
58 s->Printf("%p: ", this);
60 s->Printf("WatchpointList with %" PRIu64 " Watchpoints:\n",
61 (uint64_t)m_watchpoints.size());
63 wp_collection::const_iterator pos, end = m_watchpoints.end();
64 for (pos = m_watchpoints.begin(); pos != end; ++pos)
65 (*pos)->DumpWithLevel(s, description_level);
70 WatchpointList::FindByAddress (lldb::addr_t addr) const
73 Mutex::Locker locker (m_mutex);
74 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)->GetLoadAddress() == addr) {
88 WatchpointList::FindBySpec (std::string spec) const
91 Mutex::Locker locker (m_mutex);
92 if (!m_watchpoints.empty())
94 wp_collection::const_iterator pos, end = m_watchpoints.end();
95 for (pos = m_watchpoints.begin(); pos != end; ++pos)
96 if ((*pos)->GetWatchSpec() == spec) {
105 class WatchpointIDMatches
108 WatchpointIDMatches (lldb::watch_id_t watch_id) :
113 bool operator() (const WatchpointSP &wp) const
115 return m_watch_id == wp->GetID();
119 const lldb::watch_id_t m_watch_id;
122 WatchpointList::wp_collection::iterator
123 WatchpointList::GetIDIterator (lldb::watch_id_t watch_id)
125 return std::find_if(m_watchpoints.begin(), m_watchpoints.end(), // Search full range
126 WatchpointIDMatches(watch_id)); // Predicate
129 WatchpointList::wp_collection::const_iterator
130 WatchpointList::GetIDConstIterator (lldb::watch_id_t watch_id) const
132 return std::find_if(m_watchpoints.begin(), m_watchpoints.end(), // Search full range
133 WatchpointIDMatches(watch_id)); // Predicate
137 WatchpointList::FindByID (lldb::watch_id_t watch_id) const
140 Mutex::Locker locker (m_mutex);
141 wp_collection::const_iterator pos = GetIDConstIterator(watch_id);
142 if (pos != m_watchpoints.end())
149 WatchpointList::FindIDByAddress (lldb::addr_t addr)
151 WatchpointSP wp_sp = FindByAddress (addr);
154 return wp_sp->GetID();
156 return LLDB_INVALID_WATCH_ID;
160 WatchpointList::FindIDBySpec (std::string spec)
162 WatchpointSP wp_sp = FindBySpec (spec);
165 return wp_sp->GetID();
167 return LLDB_INVALID_WATCH_ID;
171 WatchpointList::GetByIndex (uint32_t i)
173 Mutex::Locker locker (m_mutex);
175 if (i < m_watchpoints.size())
177 wp_collection::const_iterator pos = m_watchpoints.begin();
178 std::advance(pos, i);
185 WatchpointList::GetByIndex (uint32_t i) const
187 Mutex::Locker locker (m_mutex);
189 if (i < m_watchpoints.size())
191 wp_collection::const_iterator pos = m_watchpoints.begin();
192 std::advance(pos, i);
198 std::vector<lldb::watch_id_t>
199 WatchpointList::GetWatchpointIDs() const
201 std::vector<lldb::watch_id_t> IDs;
202 wp_collection::const_iterator pos, end = m_watchpoints.end();
203 for (pos = m_watchpoints.begin(); pos != end; ++pos)
204 IDs.push_back((*pos)->GetID());
209 WatchpointList::Remove (lldb::watch_id_t watch_id, bool notify)
211 Mutex::Locker locker (m_mutex);
212 wp_collection::iterator pos = GetIDIterator(watch_id);
213 if (pos != m_watchpoints.end())
215 WatchpointSP wp_sp = *pos;
218 if (wp_sp->GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
219 wp_sp->GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged,
220 new Watchpoint::WatchpointEventData (eWatchpointEventTypeRemoved, wp_sp));
222 m_watchpoints.erase(pos);
229 WatchpointList::GetHitCount () const
231 uint32_t hit_count = 0;
232 Mutex::Locker locker (m_mutex);
233 wp_collection::const_iterator pos, end = m_watchpoints.end();
234 for (pos = m_watchpoints.begin(); pos != end; ++pos)
235 hit_count += (*pos)->GetHitCount();
240 WatchpointList::ShouldStop (StoppointCallbackContext *context, lldb::watch_id_t watch_id)
243 WatchpointSP wp_sp = FindByID (watch_id);
246 // Let the Watchpoint decide if it should stop here (could not have
247 // reached it's target hit count yet, or it could have a callback
248 // that decided it shouldn't stop.
249 return wp_sp->ShouldStop (context);
251 // We should stop here since this Watchpoint isn't valid anymore or it
257 WatchpointList::GetDescription (Stream *s, lldb::DescriptionLevel level)
259 Mutex::Locker locker (m_mutex);
260 wp_collection::iterator pos, end = m_watchpoints.end();
262 for (pos = m_watchpoints.begin(); pos != end; ++pos)
270 WatchpointList::SetEnabledAll (bool enabled)
272 Mutex::Locker locker(m_mutex);
274 wp_collection::iterator pos, end = m_watchpoints.end();
275 for (pos = m_watchpoints.begin(); pos != end; ++pos)
276 (*pos)->SetEnabled (enabled);
280 WatchpointList::RemoveAll (bool notify)
282 Mutex::Locker locker(m_mutex);
287 wp_collection::iterator pos, end = m_watchpoints.end();
288 for (pos = m_watchpoints.begin(); pos != end; ++pos)
290 if ((*pos)->GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
292 (*pos)->GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged,
293 new Watchpoint::WatchpointEventData (eWatchpointEventTypeRemoved,
299 m_watchpoints.clear();
303 WatchpointList::GetListMutex (Mutex::Locker &locker)
305 return locker.Lock (m_mutex);