LLDB  mainline
BreakpointSiteList.cpp
Go to the documentation of this file.
1 //===-- BreakpointSiteList.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 
10 
11 #include "lldb/Utility/Stream.h"
12 #include <algorithm>
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 BreakpointSiteList::BreakpointSiteList() : m_mutex(), m_bp_site_list() {}
18 
20 
21 // Add breakpoint site to the list. However, if the element already exists in
22 // the list, then we don't add it, and return LLDB_INVALID_BREAK_ID.
23 
24 lldb::break_id_t BreakpointSiteList::Add(const BreakpointSiteSP &bp) {
25  lldb::addr_t bp_site_load_addr = bp->GetLoadAddress();
26  std::lock_guard<std::recursive_mutex> guard(m_mutex);
27  collection::iterator iter = m_bp_site_list.find(bp_site_load_addr);
28 
29  if (iter == m_bp_site_list.end()) {
30  m_bp_site_list.insert(iter, collection::value_type(bp_site_load_addr, bp));
31  return bp->GetID();
32  } else {
33  return LLDB_INVALID_BREAK_ID;
34  }
35 }
36 
38  lldb::break_id_t site_id) {
39  BreakpointSiteSP site_sp(FindByID(site_id));
40  if (site_sp) {
41  // Let the BreakpointSite decide if it should stop here (could not have
42  // reached it's target hit count yet, or it could have a callback that
43  // decided it shouldn't stop (shared library loads/unloads).
44  return site_sp->ShouldStop(context);
45  }
46  // We should stop here since this BreakpointSite isn't valid anymore or it
47  // doesn't exist.
48  return true;
49 }
51  BreakpointSiteSP bp = FindByAddress(addr);
52  if (bp) {
53  // DBLogIf(PD_LOG_BREAKPOINTS, "BreakpointSiteList::%s ( addr = 0x%8.8"
54  // PRIx64 " ) => %u", __FUNCTION__, (uint64_t)addr, bp->GetID());
55  return bp.get()->GetID();
56  }
57  // DBLogIf(PD_LOG_BREAKPOINTS, "BreakpointSiteList::%s ( addr = 0x%8.8"
58  // PRIx64
59  // " ) => NONE", __FUNCTION__, (uint64_t)addr);
60  return LLDB_INVALID_BREAK_ID;
61 }
62 
64  std::lock_guard<std::recursive_mutex> guard(m_mutex);
65  collection::iterator pos = GetIDIterator(break_id); // Predicate
66  if (pos != m_bp_site_list.end()) {
67  m_bp_site_list.erase(pos);
68  return true;
69  }
70  return false;
71 }
72 
74  std::lock_guard<std::recursive_mutex> guard(m_mutex);
75  collection::iterator pos = m_bp_site_list.find(address);
76  if (pos != m_bp_site_list.end()) {
77  m_bp_site_list.erase(pos);
78  return true;
79  }
80  return false;
81 }
82 
84 public:
85  BreakpointSiteIDMatches(lldb::break_id_t break_id) : m_break_id(break_id) {}
86 
87  bool operator()(std::pair<lldb::addr_t, BreakpointSiteSP> val_pair) const {
88  return m_break_id == val_pair.second->GetID();
89  }
90 
91 private:
93 };
94 
95 BreakpointSiteList::collection::iterator
97  std::lock_guard<std::recursive_mutex> guard(m_mutex);
98  return std::find_if(m_bp_site_list.begin(),
99  m_bp_site_list.end(), // Search full range
100  BreakpointSiteIDMatches(break_id)); // Predicate
101 }
102 
103 BreakpointSiteList::collection::const_iterator
105  std::lock_guard<std::recursive_mutex> guard(m_mutex);
106  return std::find_if(m_bp_site_list.begin(),
107  m_bp_site_list.end(), // Search full range
108  BreakpointSiteIDMatches(break_id)); // Predicate
109 }
110 
111 BreakpointSiteSP BreakpointSiteList::FindByID(lldb::break_id_t break_id) {
112  std::lock_guard<std::recursive_mutex> guard(m_mutex);
113  BreakpointSiteSP stop_sp;
114  collection::iterator pos = GetIDIterator(break_id);
115  if (pos != m_bp_site_list.end())
116  stop_sp = pos->second;
117 
118  return stop_sp;
119 }
120 
121 const BreakpointSiteSP
123  std::lock_guard<std::recursive_mutex> guard(m_mutex);
124  BreakpointSiteSP stop_sp;
125  collection::const_iterator pos = GetIDConstIterator(break_id);
126  if (pos != m_bp_site_list.end())
127  stop_sp = pos->second;
128 
129  return stop_sp;
130 }
131 
133  BreakpointSiteSP found_sp;
134  std::lock_guard<std::recursive_mutex> guard(m_mutex);
135  collection::iterator iter = m_bp_site_list.find(addr);
136  if (iter != m_bp_site_list.end())
137  found_sp = iter->second;
138  return found_sp;
139 }
140 
142  lldb::break_id_t bp_site_id, lldb::break_id_t bp_id) {
143  std::lock_guard<std::recursive_mutex> guard(m_mutex);
144  collection::const_iterator pos = GetIDConstIterator(bp_site_id);
145  if (pos != m_bp_site_list.end())
146  return pos->second->IsBreakpointAtThisSite(bp_id);
147 
148  return false;
149 }
150 
152  s->Printf("%p: ", static_cast<const void *>(this));
153  // s->Indent();
154  s->Printf("BreakpointSiteList with %u BreakpointSites:\n",
155  (uint32_t)m_bp_site_list.size());
156  s->IndentMore();
157  collection::const_iterator pos;
158  collection::const_iterator end = m_bp_site_list.end();
159  for (pos = m_bp_site_list.begin(); pos != end; ++pos)
160  pos->second->Dump(s);
161  s->IndentLess();
162 }
163 
165  std::function<void(BreakpointSite *)> const &callback) {
166  std::lock_guard<std::recursive_mutex> guard(m_mutex);
167  for (auto pair : m_bp_site_list)
168  callback(pair.second.get());
169 }
170 
172  lldb::addr_t upper_bound,
173  BreakpointSiteList &bp_site_list) const {
174  if (lower_bound > upper_bound)
175  return false;
176 
177  std::lock_guard<std::recursive_mutex> guard(m_mutex);
178  collection::const_iterator lower, upper, pos;
179  lower = m_bp_site_list.lower_bound(lower_bound);
180  if (lower == m_bp_site_list.end() || (*lower).first >= upper_bound)
181  return false;
182 
183  // This is one tricky bit. The breakpoint might overlap the bottom end of
184  // the range. So we grab the breakpoint prior to the lower bound, and check
185  // that that + its byte size isn't in our range.
186  if (lower != m_bp_site_list.begin()) {
187  collection::const_iterator prev_pos = lower;
188  prev_pos--;
189  const BreakpointSiteSP &prev_bp = (*prev_pos).second;
190  if (prev_bp->GetLoadAddress() + prev_bp->GetByteSize() > lower_bound)
191  bp_site_list.Add(prev_bp);
192  }
193 
194  upper = m_bp_site_list.upper_bound(upper_bound);
195 
196  for (pos = lower; pos != upper; pos++) {
197  bp_site_list.Add((*pos).second);
198  }
199  return true;
200 }
lldb_private::Stream::IndentLess
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:171
BreakpointSiteIDMatches
Definition: BreakpointSiteList.cpp:83
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::BreakpointSiteList::FindIDByAddress
lldb::break_id_t FindIDByAddress(lldb::addr_t addr)
Returns the breakpoint site id to the breakpoint site at address addr.
Definition: BreakpointSiteList.cpp:50
lldb_private::BreakpointSiteList::BreakpointSiteContainsBreakpoint
bool BreakpointSiteContainsBreakpoint(lldb::break_id_t bp_site_id, lldb::break_id_t bp_id)
Returns whether the breakpoint site bp_site_id has bp_id.
Definition: BreakpointSiteList.cpp:141
BreakpointSiteList.h
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
BreakpointSiteIDMatches::operator()
bool operator()(std::pair< lldb::addr_t, BreakpointSiteSP > val_pair) const
Definition: BreakpointSiteList.cpp:87
lldb_private::BreakpointSiteList::FindInRange
bool FindInRange(lldb::addr_t lower_bound, lldb::addr_t upper_bound, BreakpointSiteList &bp_site_list) const
Definition: BreakpointSiteList.cpp:171
lldb_private::BreakpointSiteList::m_bp_site_list
collection m_bp_site_list
Definition: BreakpointSiteList.h:168
lldb_private::BreakpointSiteList::~BreakpointSiteList
~BreakpointSiteList()
Destructor, currently does nothing.
lldb_private::BreakpointSiteList::Remove
bool Remove(lldb::break_id_t breakID)
Removes the breakpoint site given by breakID from this list.
Definition: BreakpointSiteList.cpp:63
lldb_private::BreakpointSiteList::FindByAddress
lldb::BreakpointSiteSP FindByAddress(lldb::addr_t addr)
Returns a shared pointer to the breakpoint site at address addr.
Definition: BreakpointSiteList.cpp:132
lldb_private::BreakpointSite
Definition: BreakpointSite.h:35
BreakpointSiteIDMatches::BreakpointSiteIDMatches
BreakpointSiteIDMatches(lldb::break_id_t break_id)
Definition: BreakpointSiteList.cpp:85
lldb::break_id_t
int32_t break_id_t
Definition: lldb-types.h:88
lldb_private::BreakpointSiteList::FindByID
lldb::BreakpointSiteSP FindByID(lldb::break_id_t breakID)
Returns a shared pointer to the breakpoint site with id breakID.
Definition: BreakpointSiteList.cpp:111
lldb_private::BreakpointSiteList::RemoveByAddress
bool RemoveByAddress(lldb::addr_t addr)
Removes the breakpoint site at address addr from this list.
Definition: BreakpointSiteList.cpp:73
lldb_private::BreakpointSiteList::ShouldStop
bool ShouldStop(StoppointCallbackContext *context, lldb::break_id_t breakID)
Enquires of the breakpoint site on in this list with ID breakID whether we should stop for the breakp...
Definition: BreakpointSiteList.cpp:37
LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:49
lldb_private::BreakpointSiteList::m_mutex
std::recursive_mutex m_mutex
Definition: BreakpointSiteList.h:167
uint32_t
lldb_private::Stream::IndentMore
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:168
lldb_private::BreakpointSiteList::Dump
void Dump(Stream *s) const
Standard Dump routine, doesn't do anything at present.
Definition: BreakpointSiteList.cpp:151
lldb_private::BreakpointSiteList::GetIDIterator
collection::iterator GetIDIterator(lldb::break_id_t breakID)
Definition: BreakpointSiteList.cpp:96
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::BreakpointSiteList::Add
lldb::break_id_t Add(const lldb::BreakpointSiteSP &bp_site_sp)
Add a BreakpointSite to the list.
Definition: BreakpointSiteList.cpp:24
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
BreakpointSiteIDMatches::m_break_id
const lldb::break_id_t m_break_id
Definition: BreakpointSiteList.cpp:92
lldb_private::BreakpointSiteList::ForEach
void ForEach(std::function< void(BreakpointSite *)> const &callback)
Definition: BreakpointSiteList.cpp:164
lldb_private::BreakpointSiteList::GetIDConstIterator
collection::const_iterator GetIDConstIterator(lldb::break_id_t breakID) const
Definition: BreakpointSiteList.cpp:104
Stream.h
lldb
Definition: SBAddress.h:15
lldb_private::BreakpointSiteList
Definition: BreakpointSiteList.h:23