LLDB  mainline
WatchpointList.cpp
Go to the documentation of this file.
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 
11 
12 using namespace lldb;
13 using namespace lldb_private;
14 
15 WatchpointList::WatchpointList() : m_watchpoints(), m_mutex() {}
16 
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(wp_sp);
24  if (notify) {
25  if (wp_sp->GetTarget().EventTypeHasListeners(
27  wp_sp->GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged,
29  eWatchpointEventTypeAdded, wp_sp));
30  }
31  return wp_sp->GetID();
32 }
33 
34 void WatchpointList::Dump(Stream *s) const {
36 }
37 
39  Stream *s, lldb::DescriptionLevel description_level) const {
40  std::lock_guard<std::recursive_mutex> guard(m_mutex);
41  s->Printf("%p: ", static_cast<const void *>(this));
42  // s->Indent();
43  s->Printf("WatchpointList with %" PRIu64 " Watchpoints:\n",
44  (uint64_t)m_watchpoints.size());
45  s->IndentMore();
46  wp_collection::const_iterator pos, end = m_watchpoints.end();
47  for (pos = m_watchpoints.begin(); pos != end; ++pos)
48  (*pos)->DumpWithLevel(s, description_level);
49  s->IndentLess();
50 }
51 
52 const WatchpointSP WatchpointList::FindByAddress(lldb::addr_t addr) const {
53  WatchpointSP wp_sp;
54  std::lock_guard<std::recursive_mutex> guard(m_mutex);
55  if (!m_watchpoints.empty()) {
56  wp_collection::const_iterator pos, end = m_watchpoints.end();
57  for (pos = m_watchpoints.begin(); pos != end; ++pos) {
58  lldb::addr_t wp_addr = (*pos)->GetLoadAddress();
59  uint32_t wp_bytesize = (*pos)->GetByteSize();
60  if ((wp_addr <= addr) && ((wp_addr + wp_bytesize) > addr)) {
61  wp_sp = *pos;
62  break;
63  }
64  }
65  }
66 
67  return wp_sp;
68 }
69 
70 const WatchpointSP WatchpointList::FindBySpec(std::string spec) const {
71  WatchpointSP wp_sp;
72  std::lock_guard<std::recursive_mutex> guard(m_mutex);
73  if (!m_watchpoints.empty()) {
74  wp_collection::const_iterator pos, end = m_watchpoints.end();
75  for (pos = m_watchpoints.begin(); pos != end; ++pos)
76  if ((*pos)->GetWatchSpec() == spec) {
77  wp_sp = *pos;
78  break;
79  }
80  }
81 
82  return wp_sp;
83 }
84 
86 public:
87  WatchpointIDMatches(lldb::watch_id_t watch_id) : m_watch_id(watch_id) {}
88 
89  bool operator()(const WatchpointSP &wp) const {
90  return m_watch_id == wp->GetID();
91  }
92 
93 private:
95 };
96 
97 WatchpointList::wp_collection::iterator
99  return std::find_if(m_watchpoints.begin(),
100  m_watchpoints.end(), // Search full range
101  WatchpointIDMatches(watch_id)); // Predicate
102 }
103 
104 WatchpointList::wp_collection::const_iterator
106  return std::find_if(m_watchpoints.begin(),
107  m_watchpoints.end(), // Search full range
108  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 
122  WatchpointSP wp_sp = FindByAddress(addr);
123  if (wp_sp) {
124  return wp_sp->GetID();
125  }
126  return LLDB_INVALID_WATCH_ID;
127 }
128 
130  WatchpointSP wp_sp = FindBySpec(spec);
131  if (wp_sp) {
132  return wp_sp->GetID();
133  }
134  return LLDB_INVALID_WATCH_ID;
135 }
136 
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(pos, 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(pos, 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((*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(
175  wp_sp->GetTarget().BroadcastEvent(
177  new Watchpoint::WatchpointEventData(eWatchpointEventTypeRemoved,
178  wp_sp));
179  }
180  m_watchpoints.erase(pos);
181  return true;
182  }
183  return false;
184 }
185 
187  uint32_t hit_count = 0;
188  std::lock_guard<std::recursive_mutex> guard(m_mutex);
189  wp_collection::const_iterator pos, end = m_watchpoints.end();
190  for (pos = m_watchpoints.begin(); pos != end; ++pos)
191  hit_count += (*pos)->GetHitCount();
192  return hit_count;
193 }
194 
196  lldb::watch_id_t watch_id) {
197 
198  WatchpointSP wp_sp = FindByID(watch_id);
199  if (wp_sp) {
200  // Let the Watchpoint decide if it should stop here (could not have reached
201  // it's target hit count yet, or it could have a callback that decided it
202  // shouldn't stop.
203  return wp_sp->ShouldStop(context);
204  }
205  // We should stop here since this Watchpoint isn't valid anymore or it
206  // doesn't exist.
207  return true;
208 }
209 
211  std::lock_guard<std::recursive_mutex> guard(m_mutex);
212  wp_collection::iterator pos, end = m_watchpoints.end();
213 
214  for (pos = m_watchpoints.begin(); pos != end; ++pos) {
215  s->Printf(" ");
216  (*pos)->Dump(s);
217  }
218 }
219 
220 void WatchpointList::SetEnabledAll(bool enabled) {
221  std::lock_guard<std::recursive_mutex> guard(m_mutex);
222 
223  wp_collection::iterator pos, end = m_watchpoints.end();
224  for (pos = m_watchpoints.begin(); pos != end; ++pos)
225  (*pos)->SetEnabled(enabled);
226 }
227 
228 void WatchpointList::RemoveAll(bool notify) {
229  std::lock_guard<std::recursive_mutex> guard(m_mutex);
230  if (notify) {
231 
232  {
233  wp_collection::iterator pos, end = m_watchpoints.end();
234  for (pos = m_watchpoints.begin(); pos != end; ++pos) {
235  if ((*pos)->GetTarget().EventTypeHasListeners(
237  (*pos)->GetTarget().BroadcastEvent(
239  new Watchpoint::WatchpointEventData(eWatchpointEventTypeRemoved,
240  *pos));
241  }
242  }
243  }
244  }
245  m_watchpoints.clear();
246 }
247 
249  std::unique_lock<std::recursive_mutex> &lock) {
250  lock = std::unique_lock<std::recursive_mutex>(m_mutex);
251 }
LLDB_INVALID_WATCH_ID
#define LLDB_INVALID_WATCH_ID
Definition: lldb-defines.h:55
lldb_private::Stream::IndentLess
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:171
lldb_private::WatchpointList::~WatchpointList
~WatchpointList()
Destructor, currently does nothing.
lldb_private::StoppointCallbackContext
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
Definition: StoppointCallbackContext.h:26
lldb_private::WatchpointList::m_next_wp_id
lldb::watch_id_t m_next_wp_id
Definition: WatchpointList.h:210
lldb_private::WatchpointList::m_watchpoints
wp_collection m_watchpoints
Definition: WatchpointList.h:207
lldb_private::WatchpointList::Remove
bool Remove(lldb::watch_id_t watchID, bool notify)
Removes the watchpoint given by watchID from this list.
Definition: WatchpointList.cpp:167
lldb_private::WatchpointList::FindByID
lldb::WatchpointSP FindByID(lldb::watch_id_t watchID) const
Returns a shared pointer to the watchpoint with id watchID, const version.
Definition: WatchpointList.cpp:111
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::WatchpointList::FindIDBySpec
lldb::watch_id_t FindIDBySpec(std::string spec)
Returns the watchpoint id to the watchpoint with watchpoint spec spec.
Definition: WatchpointList.cpp:129
lldb_private::WatchpointList::Add
lldb::watch_id_t Add(const lldb::WatchpointSP &wp_sp, bool notify)
Add a Watchpoint to the list.
Definition: WatchpointList.cpp:20
lldb_private::WatchpointList::DumpWithLevel
void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const
Dump with lldb::DescriptionLevel.
Definition: WatchpointList.cpp:38
Watchpoint.h
lldb_private::WatchpointList::RemoveAll
void RemoveAll(bool notify)
Definition: WatchpointList.cpp:228
lldb_private::WatchpointList::ShouldStop
bool ShouldStop(StoppointCallbackContext *context, lldb::watch_id_t watchID)
Enquires of the watchpoint in this list with ID watchID whether we should stop.
Definition: WatchpointList.cpp:195
lldb_private::WatchpointList::GetIDIterator
wp_collection::iterator GetIDIterator(lldb::watch_id_t watchID)
Definition: WatchpointList.cpp:98
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb::watch_id_t
int32_t watch_id_t
Definition: lldb-types.h:89
lldb_private::WatchpointList::Dump
void Dump(Stream *s) const
Standard "Dump" method.
Definition: WatchpointList.cpp:34
lldb_private::WatchpointList::GetIDConstIterator
wp_collection::const_iterator GetIDConstIterator(lldb::watch_id_t watchID) const
Definition: WatchpointList.cpp:105
uint32_t
lldb_private::Stream::IndentMore
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:168
WatchpointIDMatches::WatchpointIDMatches
WatchpointIDMatches(lldb::watch_id_t watch_id)
Definition: WatchpointList.cpp:87
lldb_private::Target::eBroadcastBitBreakpointChanged
@ eBroadcastBitBreakpointChanged
Definition: Target.h:462
lldb_private::WatchpointList::GetHitCount
uint32_t GetHitCount() const
Returns the number hit count of all watchpoints in this list.
Definition: WatchpointList.cpp:186
lldb_private::WatchpointList::FindIDByAddress
lldb::watch_id_t FindIDByAddress(lldb::addr_t addr)
Returns the watchpoint id to the watchpoint at address addr.
Definition: WatchpointList.cpp:121
lldb_private::WatchpointList::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Print a description of the watchpoints in this list to the stream s.
Definition: WatchpointList.cpp:210
lldb_private::WatchpointList::GetWatchpointIDs
id_vector GetWatchpointIDs() const
Definition: WatchpointList.cpp:159
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
WatchpointIDMatches
Definition: WatchpointList.cpp:85
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Target::eBroadcastBitWatchpointChanged
@ eBroadcastBitWatchpointChanged
Definition: Target.h:465
lldb_private::WatchpointList::SetEnabledAll
void SetEnabledAll(bool enabled)
Definition: WatchpointList.cpp:220
lldb_private::WatchpointList::FindBySpec
const lldb::WatchpointSP FindBySpec(std::string spec) const
Returns a shared pointer to the watchpoint with watchpoint spec spec.
Definition: WatchpointList.cpp:70
WatchpointList.h
WatchpointIDMatches::operator()
bool operator()(const WatchpointSP &wp) const
Definition: WatchpointList.cpp:89
lldb_private::WatchpointList::m_mutex
std::recursive_mutex m_mutex
Definition: WatchpointList.h:208
lldb
Definition: SBAddress.h:15
lldb_private::WatchpointList::GetByIndex
lldb::WatchpointSP GetByIndex(uint32_t i)
Returns a shared pointer to the watchpoint with index i.
Definition: WatchpointList.cpp:137
lldb_private::WatchpointList::GetListMutex
void GetListMutex(std::unique_lock< std::recursive_mutex > &lock)
Sets the passed in Locker to hold the Watchpoint List mutex.
Definition: WatchpointList.cpp:248
lldb::eDescriptionLevelBrief
@ eDescriptionLevelBrief
Definition: lldb-enumerations.h:208
lldb_private::WatchpointList::FindByAddress
const lldb::WatchpointSP FindByAddress(lldb::addr_t addr) const
Returns a shared pointer to the watchpoint at address addr - const version.
Definition: WatchpointList.cpp:52
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207
WatchpointIDMatches::m_watch_id
const lldb::watch_id_t m_watch_id
Definition: WatchpointList.cpp:94
lldb_private::Watchpoint::WatchpointEventData
Definition: Watchpoint.h:27