180814287SRaphael Isemann //===-- WatchpointList.cpp ------------------------------------------------===//
201a67860SJohnny Chen //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601a67860SJohnny Chen //
701a67860SJohnny Chen //===----------------------------------------------------------------------===//
801a67860SJohnny Chen 
901a67860SJohnny Chen #include "lldb/Breakpoint/WatchpointList.h"
1001a67860SJohnny Chen #include "lldb/Breakpoint/Watchpoint.h"
1101a67860SJohnny Chen 
1201a67860SJohnny Chen using namespace lldb;
1301a67860SJohnny Chen using namespace lldb_private;
1401a67860SJohnny Chen 
15*ee4b6cf5SKazu Hirata WatchpointList::WatchpointList() = default;
1601a67860SJohnny Chen 
17fd2433e1SJonas Devlieghere WatchpointList::~WatchpointList() = default;
1801a67860SJohnny Chen 
19a4d6bc9fSJohnny Chen // Add a watchpoint to the list.
Add(const WatchpointSP & wp_sp,bool notify)20b9c1b51eSKate Stone lldb::watch_id_t WatchpointList::Add(const WatchpointSP &wp_sp, bool notify) {
21bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
22a4d6bc9fSJohnny Chen   wp_sp->SetID(++m_next_wp_id);
23a4d6bc9fSJohnny Chen   m_watchpoints.push_back(wp_sp);
24b9c1b51eSKate Stone   if (notify) {
25b9c1b51eSKate Stone     if (wp_sp->GetTarget().EventTypeHasListeners(
26b9c1b51eSKate Stone             Target::eBroadcastBitWatchpointChanged))
271b5792e5SJim Ingham       wp_sp->GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged,
28b9c1b51eSKate Stone                                         new Watchpoint::WatchpointEventData(
29b9c1b51eSKate Stone                                             eWatchpointEventTypeAdded, wp_sp));
301b5792e5SJim Ingham   }
3101a67860SJohnny Chen   return wp_sp->GetID();
3201a67860SJohnny Chen }
3301a67860SJohnny Chen 
Dump(Stream * s) const34b9c1b51eSKate Stone void WatchpointList::Dump(Stream *s) const {
3501a67860SJohnny Chen   DumpWithLevel(s, lldb::eDescriptionLevelBrief);
3601a67860SJohnny Chen }
3701a67860SJohnny Chen 
DumpWithLevel(Stream * s,lldb::DescriptionLevel description_level) const38b9c1b51eSKate Stone void WatchpointList::DumpWithLevel(
39b9c1b51eSKate Stone     Stream *s, lldb::DescriptionLevel description_level) const {
40bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
41324a1036SSaleem Abdulrasool   s->Printf("%p: ", static_cast<const void *>(this));
4201a67860SJohnny Chen   // s->Indent();
43d01b2953SDaniel Malea   s->Printf("WatchpointList with %" PRIu64 " Watchpoints:\n",
4443e0af06SGreg Clayton             (uint64_t)m_watchpoints.size());
4501a67860SJohnny Chen   s->IndentMore();
46a4d6bc9fSJohnny Chen   wp_collection::const_iterator pos, end = m_watchpoints.end();
47a4d6bc9fSJohnny Chen   for (pos = m_watchpoints.begin(); pos != end; ++pos)
48a4d6bc9fSJohnny Chen     (*pos)->DumpWithLevel(s, description_level);
4901a67860SJohnny Chen   s->IndentLess();
5001a67860SJohnny Chen }
5101a67860SJohnny Chen 
FindByAddress(lldb::addr_t addr) const52b9c1b51eSKate Stone const WatchpointSP WatchpointList::FindByAddress(lldb::addr_t addr) const {
5301a67860SJohnny Chen   WatchpointSP wp_sp;
54bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
55b9c1b51eSKate Stone   if (!m_watchpoints.empty()) {
56a4d6bc9fSJohnny Chen     wp_collection::const_iterator pos, end = m_watchpoints.end();
57b9c1b51eSKate Stone     for (pos = m_watchpoints.begin(); pos != end; ++pos) {
5865a47b31STed Woodward       lldb::addr_t wp_addr = (*pos)->GetLoadAddress();
5965a47b31STed Woodward       uint32_t wp_bytesize = (*pos)->GetByteSize();
60b9c1b51eSKate Stone       if ((wp_addr <= addr) && ((wp_addr + wp_bytesize) > addr)) {
61a4d6bc9fSJohnny Chen         wp_sp = *pos;
62a4d6bc9fSJohnny Chen         break;
63a4d6bc9fSJohnny Chen       }
64a4d6bc9fSJohnny Chen     }
6565a47b31STed Woodward   }
66a4d6bc9fSJohnny Chen 
67a4d6bc9fSJohnny Chen   return wp_sp;
68a4d6bc9fSJohnny Chen }
69a4d6bc9fSJohnny Chen 
FindBySpec(std::string spec) const70b9c1b51eSKate Stone const WatchpointSP WatchpointList::FindBySpec(std::string spec) const {
71a4d6bc9fSJohnny Chen   WatchpointSP wp_sp;
72bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
73b9c1b51eSKate Stone   if (!m_watchpoints.empty()) {
74a4d6bc9fSJohnny Chen     wp_collection::const_iterator pos, end = m_watchpoints.end();
75a4d6bc9fSJohnny Chen     for (pos = m_watchpoints.begin(); pos != end; ++pos)
76a4d6bc9fSJohnny Chen       if ((*pos)->GetWatchSpec() == spec) {
77a4d6bc9fSJohnny Chen         wp_sp = *pos;
78a4d6bc9fSJohnny Chen         break;
79a4d6bc9fSJohnny Chen       }
8001a67860SJohnny Chen   }
8101a67860SJohnny Chen 
8201a67860SJohnny Chen   return wp_sp;
8301a67860SJohnny Chen }
8401a67860SJohnny Chen 
85b9c1b51eSKate Stone class WatchpointIDMatches {
8601a67860SJohnny Chen public:
WatchpointIDMatches(lldb::watch_id_t watch_id)87b9c1b51eSKate Stone   WatchpointIDMatches(lldb::watch_id_t watch_id) : m_watch_id(watch_id) {}
8801a67860SJohnny Chen 
operator ()(const WatchpointSP & wp) const89b9c1b51eSKate Stone   bool operator()(const WatchpointSP &wp) const {
90a4d6bc9fSJohnny Chen     return m_watch_id == wp->GetID();
9101a67860SJohnny Chen   }
9201a67860SJohnny Chen 
9301a67860SJohnny Chen private:
9401a67860SJohnny Chen   const lldb::watch_id_t m_watch_id;
9501a67860SJohnny Chen };
9601a67860SJohnny Chen 
97a4d6bc9fSJohnny Chen WatchpointList::wp_collection::iterator
GetIDIterator(lldb::watch_id_t watch_id)98b9c1b51eSKate Stone WatchpointList::GetIDIterator(lldb::watch_id_t watch_id) {
99b9c1b51eSKate Stone   return std::find_if(m_watchpoints.begin(),
100b9c1b51eSKate Stone                       m_watchpoints.end(),            // Search full range
10101a67860SJohnny Chen                       WatchpointIDMatches(watch_id)); // Predicate
10201a67860SJohnny Chen }
10301a67860SJohnny Chen 
104a4d6bc9fSJohnny Chen WatchpointList::wp_collection::const_iterator
GetIDConstIterator(lldb::watch_id_t watch_id) const105b9c1b51eSKate Stone WatchpointList::GetIDConstIterator(lldb::watch_id_t watch_id) const {
106b9c1b51eSKate Stone   return std::find_if(m_watchpoints.begin(),
107b9c1b51eSKate Stone                       m_watchpoints.end(),            // Search full range
10801a67860SJohnny Chen                       WatchpointIDMatches(watch_id)); // Predicate
10901a67860SJohnny Chen }
11001a67860SJohnny Chen 
FindByID(lldb::watch_id_t watch_id) const111b9c1b51eSKate Stone WatchpointSP WatchpointList::FindByID(lldb::watch_id_t watch_id) const {
11201a67860SJohnny Chen   WatchpointSP wp_sp;
113bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
114a4d6bc9fSJohnny Chen   wp_collection::const_iterator pos = GetIDConstIterator(watch_id);
115a4d6bc9fSJohnny Chen   if (pos != m_watchpoints.end())
116a4d6bc9fSJohnny Chen     wp_sp = *pos;
11701a67860SJohnny Chen 
11801a67860SJohnny Chen   return wp_sp;
11901a67860SJohnny Chen }
12001a67860SJohnny Chen 
FindIDByAddress(lldb::addr_t addr)121b9c1b51eSKate Stone lldb::watch_id_t WatchpointList::FindIDByAddress(lldb::addr_t addr) {
12201a67860SJohnny Chen   WatchpointSP wp_sp = FindByAddress(addr);
123b9c1b51eSKate Stone   if (wp_sp) {
12401a67860SJohnny Chen     return wp_sp->GetID();
12501a67860SJohnny Chen   }
12601a67860SJohnny Chen   return LLDB_INVALID_WATCH_ID;
12701a67860SJohnny Chen }
12801a67860SJohnny Chen 
FindIDBySpec(std::string spec)129b9c1b51eSKate Stone lldb::watch_id_t WatchpointList::FindIDBySpec(std::string spec) {
130a4d6bc9fSJohnny Chen   WatchpointSP wp_sp = FindBySpec(spec);
131b9c1b51eSKate Stone   if (wp_sp) {
132a4d6bc9fSJohnny Chen     return wp_sp->GetID();
133a4d6bc9fSJohnny Chen   }
134a4d6bc9fSJohnny Chen   return LLDB_INVALID_WATCH_ID;
135a4d6bc9fSJohnny Chen }
136a4d6bc9fSJohnny Chen 
GetByIndex(uint32_t i)137b9c1b51eSKate Stone WatchpointSP WatchpointList::GetByIndex(uint32_t i) {
138bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
13901a67860SJohnny Chen   WatchpointSP wp_sp;
140b9c1b51eSKate Stone   if (i < m_watchpoints.size()) {
141a4d6bc9fSJohnny Chen     wp_collection::const_iterator pos = m_watchpoints.begin();
14201a67860SJohnny Chen     std::advance(pos, i);
143a4d6bc9fSJohnny Chen     wp_sp = *pos;
14401a67860SJohnny Chen   }
14501a67860SJohnny Chen   return wp_sp;
14601a67860SJohnny Chen }
14701a67860SJohnny Chen 
GetByIndex(uint32_t i) const148b9c1b51eSKate Stone const WatchpointSP WatchpointList::GetByIndex(uint32_t i) const {
149bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
15001a67860SJohnny Chen   WatchpointSP wp_sp;
151b9c1b51eSKate Stone   if (i < m_watchpoints.size()) {
152a4d6bc9fSJohnny Chen     wp_collection::const_iterator pos = m_watchpoints.begin();
15301a67860SJohnny Chen     std::advance(pos, i);
154a4d6bc9fSJohnny Chen     wp_sp = *pos;
15501a67860SJohnny Chen   }
15601a67860SJohnny Chen   return wp_sp;
15701a67860SJohnny Chen }
15801a67860SJohnny Chen 
GetWatchpointIDs() const159b9c1b51eSKate Stone std::vector<lldb::watch_id_t> WatchpointList::GetWatchpointIDs() const {
160a4d6bc9fSJohnny Chen   std::vector<lldb::watch_id_t> IDs;
161a4d6bc9fSJohnny Chen   wp_collection::const_iterator pos, end = m_watchpoints.end();
162a4d6bc9fSJohnny Chen   for (pos = m_watchpoints.begin(); pos != end; ++pos)
163a4d6bc9fSJohnny Chen     IDs.push_back((*pos)->GetID());
164a4d6bc9fSJohnny Chen   return IDs;
165a4d6bc9fSJohnny Chen }
166a4d6bc9fSJohnny Chen 
Remove(lldb::watch_id_t watch_id,bool notify)167b9c1b51eSKate Stone bool WatchpointList::Remove(lldb::watch_id_t watch_id, bool notify) {
168bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
169a4d6bc9fSJohnny Chen   wp_collection::iterator pos = GetIDIterator(watch_id);
170b9c1b51eSKate Stone   if (pos != m_watchpoints.end()) {
1711b5792e5SJim Ingham     WatchpointSP wp_sp = *pos;
172b9c1b51eSKate Stone     if (notify) {
173b9c1b51eSKate Stone       if (wp_sp->GetTarget().EventTypeHasListeners(
174b9c1b51eSKate Stone               Target::eBroadcastBitWatchpointChanged))
175b9c1b51eSKate Stone         wp_sp->GetTarget().BroadcastEvent(
176b9c1b51eSKate Stone             Target::eBroadcastBitWatchpointChanged,
177b9c1b51eSKate Stone             new Watchpoint::WatchpointEventData(eWatchpointEventTypeRemoved,
178b9c1b51eSKate Stone                                                 wp_sp));
1791b5792e5SJim Ingham     }
180a4d6bc9fSJohnny Chen     m_watchpoints.erase(pos);
18101a67860SJohnny Chen     return true;
18201a67860SJohnny Chen   }
18301a67860SJohnny Chen   return false;
18401a67860SJohnny Chen }
18501a67860SJohnny Chen 
GetHitCount() const186b9c1b51eSKate Stone uint32_t WatchpointList::GetHitCount() const {
18701a67860SJohnny Chen   uint32_t hit_count = 0;
188bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
189a4d6bc9fSJohnny Chen   wp_collection::const_iterator pos, end = m_watchpoints.end();
190a4d6bc9fSJohnny Chen   for (pos = m_watchpoints.begin(); pos != end; ++pos)
191a4d6bc9fSJohnny Chen     hit_count += (*pos)->GetHitCount();
19201a67860SJohnny Chen   return hit_count;
19301a67860SJohnny Chen }
19401a67860SJohnny Chen 
ShouldStop(StoppointCallbackContext * context,lldb::watch_id_t watch_id)195b9c1b51eSKate Stone bool WatchpointList::ShouldStop(StoppointCallbackContext *context,
196b9c1b51eSKate Stone                                 lldb::watch_id_t watch_id) {
19701a67860SJohnny Chen 
19801a67860SJohnny Chen   WatchpointSP wp_sp = FindByID(watch_id);
199b9c1b51eSKate Stone   if (wp_sp) {
20005097246SAdrian Prantl     // Let the Watchpoint decide if it should stop here (could not have reached
20105097246SAdrian Prantl     // it's target hit count yet, or it could have a callback that decided it
20205097246SAdrian Prantl     // shouldn't stop.
20301a67860SJohnny Chen     return wp_sp->ShouldStop(context);
20401a67860SJohnny Chen   }
20501a67860SJohnny Chen   // We should stop here since this Watchpoint isn't valid anymore or it
20601a67860SJohnny Chen   // doesn't exist.
20701a67860SJohnny Chen   return true;
20801a67860SJohnny Chen }
20901a67860SJohnny Chen 
GetDescription(Stream * s,lldb::DescriptionLevel level)210b9c1b51eSKate Stone void WatchpointList::GetDescription(Stream *s, lldb::DescriptionLevel level) {
211bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
212a4d6bc9fSJohnny Chen   wp_collection::iterator pos, end = m_watchpoints.end();
21301a67860SJohnny Chen 
214b9c1b51eSKate Stone   for (pos = m_watchpoints.begin(); pos != end; ++pos) {
21501a67860SJohnny Chen     s->Printf(" ");
216a4d6bc9fSJohnny Chen     (*pos)->Dump(s);
21701a67860SJohnny Chen   }
21801a67860SJohnny Chen }
21901a67860SJohnny Chen 
SetEnabledAll(bool enabled)220b9c1b51eSKate Stone void WatchpointList::SetEnabledAll(bool enabled) {
221bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
22201a67860SJohnny Chen 
223a4d6bc9fSJohnny Chen   wp_collection::iterator pos, end = m_watchpoints.end();
224a4d6bc9fSJohnny Chen   for (pos = m_watchpoints.begin(); pos != end; ++pos)
225a4d6bc9fSJohnny Chen     (*pos)->SetEnabled(enabled);
22601a67860SJohnny Chen }
22701a67860SJohnny Chen 
RemoveAll(bool notify)228b9c1b51eSKate Stone void WatchpointList::RemoveAll(bool notify) {
229bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
230b9c1b51eSKate Stone   if (notify) {
2311b5792e5SJim Ingham 
2321b5792e5SJim Ingham     {
2331b5792e5SJim Ingham       wp_collection::iterator pos, end = m_watchpoints.end();
234b9c1b51eSKate Stone       for (pos = m_watchpoints.begin(); pos != end; ++pos) {
235b9c1b51eSKate Stone         if ((*pos)->GetTarget().EventTypeHasListeners(
236b9c1b51eSKate Stone                 Target::eBroadcastBitBreakpointChanged)) {
237b9c1b51eSKate Stone           (*pos)->GetTarget().BroadcastEvent(
238b9c1b51eSKate Stone               Target::eBroadcastBitWatchpointChanged,
2391b5792e5SJim Ingham               new Watchpoint::WatchpointEventData(eWatchpointEventTypeRemoved,
2401b5792e5SJim Ingham                                                   *pos));
2411b5792e5SJim Ingham         }
2421b5792e5SJim Ingham       }
2431b5792e5SJim Ingham     }
2441b5792e5SJim Ingham   }
245a4d6bc9fSJohnny Chen   m_watchpoints.clear();
24601a67860SJohnny Chen }
24701a67860SJohnny Chen 
GetListMutex(std::unique_lock<std::recursive_mutex> & lock)248b9c1b51eSKate Stone void WatchpointList::GetListMutex(
249b9c1b51eSKate Stone     std::unique_lock<std::recursive_mutex> &lock) {
250bb19a13cSSaleem Abdulrasool   lock = std::unique_lock<std::recursive_mutex>(m_mutex);
25101a67860SJohnny Chen }
252