1 //===-- BreakpointLocationList.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Breakpoint/BreakpointLocationList.h"
16 #include "lldb/Breakpoint/Breakpoint.h"
17 #include "lldb/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Core/ArchSpec.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Target/SectionLoadList.h"
22 #include "lldb/Target/Target.h"
25 using namespace lldb_private;
27 BreakpointLocationList::BreakpointLocationList(Breakpoint &owner)
28 : m_owner(owner), m_locations(), m_address_to_location(), m_mutex(),
29 m_next_id(0), m_new_location_recorder(nullptr) {}
31 BreakpointLocationList::~BreakpointLocationList() = default;
34 BreakpointLocationList::Create(const Address &addr,
35 bool resolve_indirect_symbols) {
36 std::lock_guard<std::recursive_mutex> guard(m_mutex);
37 // The location ID is just the size of the location list + 1
38 lldb::break_id_t bp_loc_id = ++m_next_id;
39 BreakpointLocationSP bp_loc_sp(
40 new BreakpointLocation(bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID,
41 m_owner.IsHardware(), resolve_indirect_symbols));
42 m_locations.push_back(bp_loc_sp);
43 m_address_to_location[addr] = bp_loc_sp;
47 bool BreakpointLocationList::ShouldStop(StoppointCallbackContext *context,
48 lldb::break_id_t break_id) {
49 BreakpointLocationSP bp = FindByID(break_id);
51 // Let the BreakpointLocation decide if it should stop here (could not have
52 // reached it's target hit count yet, or it could have a callback
53 // that decided it shouldn't stop (shared library loads/unloads).
54 return bp->ShouldStop(context);
56 // We should stop here since this BreakpointLocation isn't valid anymore or it
61 lldb::break_id_t BreakpointLocationList::FindIDByAddress(const Address &addr) {
62 BreakpointLocationSP bp_loc_sp = FindByAddress(addr);
64 return bp_loc_sp->GetID();
66 return LLDB_INVALID_BREAK_ID;
69 static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val) {
70 return lhs->GetID() < val;
74 BreakpointLocationList::FindByID(lldb::break_id_t break_id) const {
75 std::lock_guard<std::recursive_mutex> guard(m_mutex);
76 collection::const_iterator end = m_locations.end();
77 collection::const_iterator pos =
78 std::lower_bound(m_locations.begin(), end, break_id, Compare);
79 if (pos != end && (*pos)->GetID() == break_id)
82 return BreakpointLocationSP();
85 size_t BreakpointLocationList::FindInModule(
86 Module *module, BreakpointLocationCollection &bp_loc_list) {
87 std::lock_guard<std::recursive_mutex> guard(m_mutex);
88 const size_t orig_size = bp_loc_list.GetSize();
89 collection::iterator pos, end = m_locations.end();
91 for (pos = m_locations.begin(); pos != end; ++pos) {
92 BreakpointLocationSP break_loc = (*pos);
93 SectionSP section_sp(break_loc->GetAddress().GetSection());
94 if (section_sp && section_sp->GetModule().get() == module) {
95 bp_loc_list.Add(break_loc);
98 return bp_loc_list.GetSize() - orig_size;
101 const BreakpointLocationSP
102 BreakpointLocationList::FindByAddress(const Address &addr) const {
103 std::lock_guard<std::recursive_mutex> guard(m_mutex);
104 BreakpointLocationSP bp_loc_sp;
105 if (!m_locations.empty()) {
108 if (addr.IsSectionOffset()) {
111 // Try and resolve as a load address if possible.
112 m_owner.GetTarget().GetSectionLoadList().ResolveLoadAddress(
113 addr.GetOffset(), so_addr);
114 if (!so_addr.IsValid()) {
115 // The address didn't resolve, so just set to passed in addr.
120 addr_map::const_iterator pos = m_address_to_location.find(so_addr);
121 if (pos != m_address_to_location.end())
122 bp_loc_sp = pos->second;
128 void BreakpointLocationList::Dump(Stream *s) const {
129 s->Printf("%p: ", static_cast<const void *>(this));
131 std::lock_guard<std::recursive_mutex> guard(m_mutex);
132 s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",
133 (uint64_t)m_locations.size());
135 collection::const_iterator pos, end = m_locations.end();
136 for (pos = m_locations.begin(); pos != end; ++pos)
137 (*pos).get()->Dump(s);
141 BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) {
142 std::lock_guard<std::recursive_mutex> guard(m_mutex);
143 BreakpointLocationSP bp_loc_sp;
144 if (i < m_locations.size())
145 bp_loc_sp = m_locations[i];
150 const BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) const {
151 std::lock_guard<std::recursive_mutex> guard(m_mutex);
152 BreakpointLocationSP bp_loc_sp;
153 if (i < m_locations.size())
154 bp_loc_sp = m_locations[i];
159 void BreakpointLocationList::ClearAllBreakpointSites() {
160 std::lock_guard<std::recursive_mutex> guard(m_mutex);
161 collection::iterator pos, end = m_locations.end();
162 for (pos = m_locations.begin(); pos != end; ++pos)
163 (*pos)->ClearBreakpointSite();
166 void BreakpointLocationList::ResolveAllBreakpointSites() {
167 std::lock_guard<std::recursive_mutex> guard(m_mutex);
168 collection::iterator pos, end = m_locations.end();
170 for (pos = m_locations.begin(); pos != end; ++pos) {
171 if ((*pos)->IsEnabled())
172 (*pos)->ResolveBreakpointSite();
176 uint32_t BreakpointLocationList::GetHitCount() const {
177 uint32_t hit_count = 0;
178 std::lock_guard<std::recursive_mutex> guard(m_mutex);
179 collection::const_iterator pos, end = m_locations.end();
180 for (pos = m_locations.begin(); pos != end; ++pos)
181 hit_count += (*pos)->GetHitCount();
185 size_t BreakpointLocationList::GetNumResolvedLocations() const {
186 std::lock_guard<std::recursive_mutex> guard(m_mutex);
187 size_t resolve_count = 0;
188 collection::const_iterator pos, end = m_locations.end();
189 for (pos = m_locations.begin(); pos != end; ++pos) {
190 if ((*pos)->IsResolved())
193 return resolve_count;
196 void BreakpointLocationList::GetDescription(Stream *s,
197 lldb::DescriptionLevel level) {
198 std::lock_guard<std::recursive_mutex> guard(m_mutex);
199 collection::iterator pos, end = m_locations.end();
201 for (pos = m_locations.begin(); pos != end; ++pos) {
203 (*pos)->GetDescription(s, level);
207 BreakpointLocationSP BreakpointLocationList::AddLocation(
208 const Address &addr, bool resolve_indirect_symbols, bool *new_location) {
209 std::lock_guard<std::recursive_mutex> guard(m_mutex);
212 *new_location = false;
213 BreakpointLocationSP bp_loc_sp(FindByAddress(addr));
215 bp_loc_sp = Create(addr, resolve_indirect_symbols);
217 bp_loc_sp->ResolveBreakpointSite();
220 *new_location = true;
221 if (m_new_location_recorder) {
222 m_new_location_recorder->Add(bp_loc_sp);
229 void BreakpointLocationList::SwapLocation(
230 BreakpointLocationSP to_location_sp,
231 BreakpointLocationSP from_location_sp) {
232 if (!from_location_sp || !to_location_sp)
235 m_address_to_location.erase(to_location_sp->GetAddress());
236 to_location_sp->SwapLocation(from_location_sp);
237 RemoveLocation(from_location_sp);
238 m_address_to_location[to_location_sp->GetAddress()] = to_location_sp;
239 to_location_sp->ResolveBreakpointSite();
242 bool BreakpointLocationList::RemoveLocation(
243 const lldb::BreakpointLocationSP &bp_loc_sp) {
245 std::lock_guard<std::recursive_mutex> guard(m_mutex);
247 m_address_to_location.erase(bp_loc_sp->GetAddress());
249 collection::iterator pos, end = m_locations.end();
250 for (pos = m_locations.begin(); pos != end; ++pos) {
251 if ((*pos).get() == bp_loc_sp.get()) {
252 m_locations.erase(pos);
260 void BreakpointLocationList::RemoveInvalidLocations(const ArchSpec &arch) {
261 std::lock_guard<std::recursive_mutex> guard(m_mutex);
263 // Don't cache m_location.size() as it will change since we might
264 // remove locations from our vector...
265 while (idx < m_locations.size()) {
266 BreakpointLocation *bp_loc = m_locations[idx].get();
267 if (bp_loc->GetAddress().SectionWasDeleted()) {
268 // Section was deleted which means this breakpoint comes from a module
269 // that is no longer valid, so we should remove it.
270 m_locations.erase(m_locations.begin() + idx);
273 if (arch.IsValid()) {
274 ModuleSP module_sp(bp_loc->GetAddress().GetModule());
276 if (!arch.IsCompatibleMatch(module_sp->GetArchitecture())) {
277 // The breakpoint was in a module whose architecture is no longer
278 // compatible with "arch", so we need to remove it
279 m_locations.erase(m_locations.begin() + idx);
284 // Only increment the index if we didn't remove the locations at index "idx"
289 void BreakpointLocationList::StartRecordingNewLocations(
290 BreakpointLocationCollection &new_locations) {
291 std::lock_guard<std::recursive_mutex> guard(m_mutex);
292 assert(m_new_location_recorder == nullptr);
293 m_new_location_recorder = &new_locations;
296 void BreakpointLocationList::StopRecordingNewLocations() {
297 std::lock_guard<std::recursive_mutex> guard(m_mutex);
298 m_new_location_recorder = nullptr;
301 void BreakpointLocationList::Compact() {
302 lldb::break_id_t highest_id = 0;
304 for (BreakpointLocationSP loc_sp : m_locations) {
305 lldb::break_id_t cur_id = loc_sp->GetID();
306 if (cur_id > highest_id)
309 m_next_id = highest_id;