1 //===-- WatchpointList.cpp --------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Breakpoint/WatchpointList.h"
11 #include "lldb/Breakpoint/Watchpoint.h"
12
13 using namespace lldb;
14 using namespace lldb_private;
15
WatchpointList()16 WatchpointList::WatchpointList()
17 : m_watchpoints(), m_mutex(), m_next_wp_id(0) {}
18
~WatchpointList()19 WatchpointList::~WatchpointList() {}
20
21 // Add a watchpoint to the list.
Add(const WatchpointSP & wp_sp,bool notify)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);
26 if (notify) {
27 if (wp_sp->GetTarget().EventTypeHasListeners(
28 Target::eBroadcastBitWatchpointChanged))
29 wp_sp->GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged,
30 new Watchpoint::WatchpointEventData(
31 eWatchpointEventTypeAdded, wp_sp));
32 }
33 return wp_sp->GetID();
34 }
35
Dump(Stream * s) const36 void WatchpointList::Dump(Stream *s) const {
37 DumpWithLevel(s, lldb::eDescriptionLevelBrief);
38 }
39
DumpWithLevel(Stream * s,lldb::DescriptionLevel description_level) const40 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));
44 // s->Indent();
45 s->Printf("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
FindByAddress(lldb::addr_t addr) const54 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
FindBySpec(std::string spec) const72 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:
WatchpointIDMatches(lldb::watch_id_t watch_id)89 WatchpointIDMatches(lldb::watch_id_t watch_id) : m_watch_id(watch_id) {}
90
operator ()(const WatchpointSP & wp) const91 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
GetIDIterator(lldb::watch_id_t watch_id)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
104 }
105
106 WatchpointList::wp_collection::const_iterator
GetIDConstIterator(lldb::watch_id_t watch_id) const107 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
111 }
112
FindByID(lldb::watch_id_t watch_id) const113 WatchpointSP WatchpointList::FindByID(lldb::watch_id_t watch_id) const {
114 WatchpointSP wp_sp;
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())
118 wp_sp = *pos;
119
120 return wp_sp;
121 }
122
FindIDByAddress(lldb::addr_t addr)123 lldb::watch_id_t WatchpointList::FindIDByAddress(lldb::addr_t addr) {
124 WatchpointSP wp_sp = FindByAddress(addr);
125 if (wp_sp) {
126 return wp_sp->GetID();
127 }
128 return LLDB_INVALID_WATCH_ID;
129 }
130
FindIDBySpec(std::string spec)131 lldb::watch_id_t WatchpointList::FindIDBySpec(std::string spec) {
132 WatchpointSP wp_sp = FindBySpec(spec);
133 if (wp_sp) {
134 return wp_sp->GetID();
135 }
136 return LLDB_INVALID_WATCH_ID;
137 }
138
GetByIndex(uint32_t i)139 WatchpointSP WatchpointList::GetByIndex(uint32_t i) {
140 std::lock_guard<std::recursive_mutex> guard(m_mutex);
141 WatchpointSP wp_sp;
142 if (i < m_watchpoints.size()) {
143 wp_collection::const_iterator pos = m_watchpoints.begin();
144 std::advance(pos, i);
145 wp_sp = *pos;
146 }
147 return wp_sp;
148 }
149
GetByIndex(uint32_t i) const150 const WatchpointSP WatchpointList::GetByIndex(uint32_t i) const {
151 std::lock_guard<std::recursive_mutex> guard(m_mutex);
152 WatchpointSP wp_sp;
153 if (i < m_watchpoints.size()) {
154 wp_collection::const_iterator pos = m_watchpoints.begin();
155 std::advance(pos, i);
156 wp_sp = *pos;
157 }
158 return wp_sp;
159 }
160
GetWatchpointIDs() const161 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());
166 return IDs;
167 }
168
Remove(lldb::watch_id_t watch_id,bool notify)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;
174 if (notify) {
175 if (wp_sp->GetTarget().EventTypeHasListeners(
176 Target::eBroadcastBitWatchpointChanged))
177 wp_sp->GetTarget().BroadcastEvent(
178 Target::eBroadcastBitWatchpointChanged,
179 new Watchpoint::WatchpointEventData(eWatchpointEventTypeRemoved,
180 wp_sp));
181 }
182 m_watchpoints.erase(pos);
183 return true;
184 }
185 return false;
186 }
187
GetHitCount() const188 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();
194 return hit_count;
195 }
196
ShouldStop(StoppointCallbackContext * context,lldb::watch_id_t watch_id)197 bool WatchpointList::ShouldStop(StoppointCallbackContext *context,
198 lldb::watch_id_t watch_id) {
199
200 WatchpointSP wp_sp = FindByID(watch_id);
201 if (wp_sp) {
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
204 // shouldn't stop.
205 return wp_sp->ShouldStop(context);
206 }
207 // We should stop here since this Watchpoint isn't valid anymore or it
208 // doesn't exist.
209 return true;
210 }
211
GetDescription(Stream * s,lldb::DescriptionLevel level)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();
215
216 for (pos = m_watchpoints.begin(); pos != end; ++pos) {
217 s->Printf(" ");
218 (*pos)->Dump(s);
219 }
220 }
221
SetEnabledAll(bool enabled)222 void WatchpointList::SetEnabledAll(bool enabled) {
223 std::lock_guard<std::recursive_mutex> guard(m_mutex);
224
225 wp_collection::iterator pos, end = m_watchpoints.end();
226 for (pos = m_watchpoints.begin(); pos != end; ++pos)
227 (*pos)->SetEnabled(enabled);
228 }
229
RemoveAll(bool notify)230 void WatchpointList::RemoveAll(bool notify) {
231 std::lock_guard<std::recursive_mutex> guard(m_mutex);
232 if (notify) {
233
234 {
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,
242 *pos));
243 }
244 }
245 }
246 }
247 m_watchpoints.clear();
248 }
249
GetListMutex(std::unique_lock<std::recursive_mutex> & lock)250 void WatchpointList::GetListMutex(
251 std::unique_lock<std::recursive_mutex> &lock) {
252 lock = std::unique_lock<std::recursive_mutex>(m_mutex);
253 }
254