LLDB  mainline
BreakpointLocationList.cpp
Go to the documentation of this file.
1 //===-- BreakpointLocationList.cpp ------------------------------*- C++ -*-===//
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 
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Section.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/ArchSpec.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 BreakpointLocationList::BreakpointLocationList(Breakpoint &owner)
23  : m_owner(owner), m_locations(), m_address_to_location(), m_mutex(),
24  m_next_id(0), m_new_location_recorder(nullptr) {}
25 
27 
28 BreakpointLocationSP
30  bool resolve_indirect_symbols) {
31  std::lock_guard<std::recursive_mutex> guard(m_mutex);
32  // The location ID is just the size of the location list + 1
33  lldb::break_id_t bp_loc_id = ++m_next_id;
34  BreakpointLocationSP bp_loc_sp(
36  m_owner.IsHardware(), resolve_indirect_symbols));
37  m_locations.push_back(bp_loc_sp);
38  m_address_to_location[addr] = bp_loc_sp;
39  return bp_loc_sp;
40 }
41 
43  lldb::break_id_t break_id) {
44  BreakpointLocationSP bp = FindByID(break_id);
45  if (bp) {
46  // Let the BreakpointLocation decide if it should stop here (could not have
47  // reached it's target hit count yet, or it could have a callback that
48  // decided it shouldn't stop (shared library loads/unloads).
49  return bp->ShouldStop(context);
50  }
51  // We should stop here since this BreakpointLocation isn't valid anymore or
52  // it doesn't exist.
53  return true;
54 }
55 
57  BreakpointLocationSP bp_loc_sp = FindByAddress(addr);
58  if (bp_loc_sp) {
59  return bp_loc_sp->GetID();
60  }
61  return LLDB_INVALID_BREAK_ID;
62 }
63 
64 static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val) {
65  return lhs->GetID() < val;
66 }
67 
68 BreakpointLocationSP
70  std::lock_guard<std::recursive_mutex> guard(m_mutex);
71  collection::const_iterator end = m_locations.end();
72  collection::const_iterator pos =
73  std::lower_bound(m_locations.begin(), end, break_id, Compare);
74  if (pos != end && (*pos)->GetID() == break_id)
75  return *(pos);
76  else
77  return BreakpointLocationSP();
78 }
79 
81  Module *module, BreakpointLocationCollection &bp_loc_list) {
82  std::lock_guard<std::recursive_mutex> guard(m_mutex);
83  const size_t orig_size = bp_loc_list.GetSize();
84  collection::iterator pos, end = m_locations.end();
85 
86  for (pos = m_locations.begin(); pos != end; ++pos) {
87  BreakpointLocationSP break_loc = (*pos);
88  SectionSP section_sp(break_loc->GetAddress().GetSection());
89  if (section_sp && section_sp->GetModule().get() == module) {
90  bp_loc_list.Add(break_loc);
91  }
92  }
93  return bp_loc_list.GetSize() - orig_size;
94 }
95 
96 const BreakpointLocationSP
98  std::lock_guard<std::recursive_mutex> guard(m_mutex);
99  BreakpointLocationSP bp_loc_sp;
100  if (!m_locations.empty()) {
101  Address so_addr;
102 
103  if (addr.IsSectionOffset()) {
104  so_addr = addr;
105  } else {
106  // Try and resolve as a load address if possible.
108  addr.GetOffset(), so_addr);
109  if (!so_addr.IsValid()) {
110  // The address didn't resolve, so just set to passed in addr.
111  so_addr = addr;
112  }
113  }
114 
115  addr_map::const_iterator pos = m_address_to_location.find(so_addr);
116  if (pos != m_address_to_location.end())
117  bp_loc_sp = pos->second;
118  }
119 
120  return bp_loc_sp;
121 }
122 
124  s->Printf("%p: ", static_cast<const void *>(this));
125  // s->Indent();
126  std::lock_guard<std::recursive_mutex> guard(m_mutex);
127  s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",
128  (uint64_t)m_locations.size());
129  s->IndentMore();
130  collection::const_iterator pos, end = m_locations.end();
131  for (pos = m_locations.begin(); pos != end; ++pos)
132  (*pos)->Dump(s);
133  s->IndentLess();
134 }
135 
136 BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) {
137  std::lock_guard<std::recursive_mutex> guard(m_mutex);
138  BreakpointLocationSP bp_loc_sp;
139  if (i < m_locations.size())
140  bp_loc_sp = m_locations[i];
141 
142  return bp_loc_sp;
143 }
144 
145 const BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) const {
146  std::lock_guard<std::recursive_mutex> guard(m_mutex);
147  BreakpointLocationSP bp_loc_sp;
148  if (i < m_locations.size())
149  bp_loc_sp = m_locations[i];
150 
151  return bp_loc_sp;
152 }
153 
155  std::lock_guard<std::recursive_mutex> guard(m_mutex);
156  collection::iterator pos, end = m_locations.end();
157  for (pos = m_locations.begin(); pos != end; ++pos)
158  (*pos)->ClearBreakpointSite();
159 }
160 
162  std::lock_guard<std::recursive_mutex> guard(m_mutex);
163  collection::iterator pos, end = m_locations.end();
164 
165  for (pos = m_locations.begin(); pos != end; ++pos) {
166  if ((*pos)->IsEnabled())
167  (*pos)->ResolveBreakpointSite();
168  }
169 }
170 
172  uint32_t hit_count = 0;
173  std::lock_guard<std::recursive_mutex> guard(m_mutex);
174  collection::const_iterator pos, end = m_locations.end();
175  for (pos = m_locations.begin(); pos != end; ++pos)
176  hit_count += (*pos)->GetHitCount();
177  return hit_count;
178 }
179 
181  std::lock_guard<std::recursive_mutex> guard(m_mutex);
182  size_t resolve_count = 0;
183  collection::const_iterator pos, end = m_locations.end();
184  for (pos = m_locations.begin(); pos != end; ++pos) {
185  if ((*pos)->IsResolved())
186  ++resolve_count;
187  }
188  return resolve_count;
189 }
190 
192  lldb::DescriptionLevel level) {
193  std::lock_guard<std::recursive_mutex> guard(m_mutex);
194  collection::iterator pos, end = m_locations.end();
195 
196  for (pos = m_locations.begin(); pos != end; ++pos) {
197  s->Printf(" ");
198  (*pos)->GetDescription(s, level);
199  }
200 }
201 
203  const Address &addr, bool resolve_indirect_symbols, bool *new_location) {
204  std::lock_guard<std::recursive_mutex> guard(m_mutex);
205 
206  if (new_location)
207  *new_location = false;
208  BreakpointLocationSP bp_loc_sp(FindByAddress(addr));
209  if (!bp_loc_sp) {
210  bp_loc_sp = Create(addr, resolve_indirect_symbols);
211  if (bp_loc_sp) {
212  bp_loc_sp->ResolveBreakpointSite();
213 
214  if (new_location)
215  *new_location = true;
217  m_new_location_recorder->Add(bp_loc_sp);
218  }
219  }
220  }
221  return bp_loc_sp;
222 }
223 
225  BreakpointLocationSP to_location_sp,
226  BreakpointLocationSP from_location_sp) {
227  if (!from_location_sp || !to_location_sp)
228  return;
229 
230  m_address_to_location.erase(to_location_sp->GetAddress());
231  to_location_sp->SwapLocation(from_location_sp);
232  RemoveLocation(from_location_sp);
233  m_address_to_location[to_location_sp->GetAddress()] = to_location_sp;
234  to_location_sp->ResolveBreakpointSite();
235 }
236 
238  const lldb::BreakpointLocationSP &bp_loc_sp) {
239  if (bp_loc_sp) {
240  std::lock_guard<std::recursive_mutex> guard(m_mutex);
241 
242  m_address_to_location.erase(bp_loc_sp->GetAddress());
243 
244  size_t num_locations = m_locations.size();
245  for (size_t idx = 0; idx < num_locations; idx++) {
246  if (m_locations[idx].get() == bp_loc_sp.get()) {
248  return true;
249  }
250  }
251  }
252  return false;
253 }
254 
256  assert (idx < m_locations.size());
257  m_address_to_location.erase(m_locations[idx]->GetAddress());
258  m_locations.erase(m_locations.begin() + idx);
259 }
260 
262  std::lock_guard<std::recursive_mutex> guard(m_mutex);
263  size_t idx = 0;
264  // Don't cache m_location.size() as it will change since we might remove
265  // locations from our vector...
266  while (idx < m_locations.size()) {
267  BreakpointLocation *bp_loc = m_locations[idx].get();
268  if (bp_loc->GetAddress().SectionWasDeleted()) {
269  // Section was deleted which means this breakpoint comes from a module
270  // that is no longer valid, so we should remove it.
272  continue;
273  }
274  if (arch.IsValid()) {
275  ModuleSP module_sp(bp_loc->GetAddress().GetModule());
276  if (module_sp) {
277  if (!arch.IsCompatibleMatch(module_sp->GetArchitecture())) {
278  // The breakpoint was in a module whose architecture is no longer
279  // compatible with "arch", so we need to remove it
281  continue;
282  }
283  }
284  }
285  // Only increment the index if we didn't remove the locations at index
286  // "idx"
287  ++idx;
288  }
289 }
290 
292  BreakpointLocationCollection &new_locations) {
293  std::lock_guard<std::recursive_mutex> guard(m_mutex);
294  assert(m_new_location_recorder == nullptr);
295  m_new_location_recorder = &new_locations;
296 }
297 
299  std::lock_guard<std::recursive_mutex> guard(m_mutex);
300  m_new_location_recorder = nullptr;
301 }
302 
304  lldb::break_id_t highest_id = 0;
305 
306  for (BreakpointLocationSP loc_sp : m_locations) {
307  lldb::break_id_t cur_id = loc_sp->GetID();
308  if (cur_id > highest_id)
309  highest_id = cur_id;
310  }
311  m_next_id = highest_id;
312 }
void Add(const lldb::BreakpointLocationSP &bp_loc_sp)
Add the breakpoint bp_loc_sp to the list.
void RemoveInvalidLocations(const ArchSpec &arch)
bool IsSectionOffset() const
Check if an address is section offset.
Definition: Address.h:330
General Outline: A breakpoint location is defined by the breakpoint that produces it...
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
int32_t break_id_t
Definition: lldb-types.h:88
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:78
size_t GetNumResolvedLocations() const
Returns the number of breakpoint locations in this list with resolved breakpoints.
bool RemoveLocation(const lldb::BreakpointLocationSP &bp_loc_sp)
An architecture specification class.
Definition: ArchSpec.h:32
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:467
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:329
bool SectionWasDeleted() const
Definition: Address.cpp:762
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:317
void ResolveAllBreakpointSites()
Tells all the breakpoint locations in this list to attempt to resolve any possible breakpoint sites...
lldb::break_id_t FindIDByAddress(const Address &addr)
Returns the breakpoint location id to the breakpoint location at address addr.
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1012
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Print a description of the breakpoint locations in this list to the stream s.
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:109
void StartRecordingNewLocations(BreakpointLocationCollection &new_locations)
size_t FindInModule(Module *module, BreakpointLocationCollection &bp_loc_list)
Returns a breakpoint location list of the breakpoint locations in the module module.
void SwapLocation(lldb::BreakpointLocationSP to_location_sp, lldb::BreakpointLocationSP from_location_sp)
void Dump(Stream *s) const
Standard "Dump" method. At present it does nothing.
lldb::BreakpointLocationSP GetByIndex(size_t i)
Returns a shared pointer to the breakpoint location with index i.
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, bool allow_section_end=false) const
size_t GetSize() const
Returns the number of elements in this breakpoint location list.
void IndentLess(int amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:221
BreakpointLocationCollection * m_new_location_recorder
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
bool IsHardware() const
Definition: Breakpoint.h:524
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:343
A section + offset based address class.
Definition: Address.h:80
Address & GetAddress()
Gets the Address for this breakpoint location.
bool ShouldStop(StoppointCallbackContext *context, lldb::break_id_t breakID)
Enquires of the breakpoint location in this list with ID breakID whether we should stop...
lldb::BreakpointLocationSP Create(const Address &addr, bool resolve_indirect_symbols)
Add the breakpoint bp_loc_sp to the list.
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:93
const lldb::BreakpointLocationSP FindByAddress(const Address &addr) const
Returns a shared pointer to the breakpoint location at address addr - const version.
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:264
lldb::BreakpointLocationSP AddLocation(const Address &addr, bool resolve_indirect_symbols, bool *new_location=nullptr)
static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val)
Definition: SBAddress.h:15
void ClearAllBreakpointSites()
Removes all the locations in this list from their breakpoint site owners list.
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:49
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
void IndentMore(int amount=2)
Increment the current indentation level.
Definition: Stream.cpp:218
lldb::BreakpointLocationSP FindByID(lldb::break_id_t breakID) const
Returns a shared pointer to the breakpoint location with id breakID, const version.
uint32_t GetHitCount() const
Returns the number hit count of all locations in this list.
bool IsCompatibleMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring a compatible cpu type match between them...
Definition: ArchSpec.cpp:981