180814287SRaphael Isemann //===-- BreakpointLocationList.cpp ----------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
930fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocationList.h"
10b35db639SGreg Clayton 
11c87e752bSMichael Sartain #include "lldb/Breakpoint/Breakpoint.h"
12b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h"
13b35db639SGreg Clayton #include "lldb/Core/Module.h"
141f746071SGreg Clayton #include "lldb/Core/Section.h"
15d5944cd1SGreg Clayton #include "lldb/Target/SectionLoadList.h"
16c87e752bSMichael Sartain #include "lldb/Target/Target.h"
175f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
1830fdc8d8SChris Lattner 
1930fdc8d8SChris Lattner using namespace lldb;
2030fdc8d8SChris Lattner using namespace lldb_private;
2130fdc8d8SChris Lattner 
BreakpointLocationList(Breakpoint & owner)2216ff8604SSaleem Abdulrasool BreakpointLocationList::BreakpointLocationList(Breakpoint &owner)
23*ee4b6cf5SKazu Hirata     : m_owner(owner), m_next_id(0), m_new_location_recorder(nullptr) {}
2430fdc8d8SChris Lattner 
2516fd7511SEugene Zelenko BreakpointLocationList::~BreakpointLocationList() = default;
2630fdc8d8SChris Lattner 
27c0d34465SGreg Clayton BreakpointLocationSP
Create(const Address & addr,bool resolve_indirect_symbols)28b9c1b51eSKate Stone BreakpointLocationList::Create(const Address &addr,
29b9c1b51eSKate Stone                                bool resolve_indirect_symbols) {
3016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
31c0d34465SGreg Clayton   // The location ID is just the size of the location list + 1
32e72dfb32SGreg Clayton   lldb::break_id_t bp_loc_id = ++m_next_id;
33b9c1b51eSKate Stone   BreakpointLocationSP bp_loc_sp(
34b9c1b51eSKate Stone       new BreakpointLocation(bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID,
35b9c1b51eSKate Stone                              m_owner.IsHardware(), resolve_indirect_symbols));
3630fdc8d8SChris Lattner   m_locations.push_back(bp_loc_sp);
37c0d34465SGreg Clayton   m_address_to_location[addr] = bp_loc_sp;
38c0d34465SGreg Clayton   return bp_loc_sp;
3930fdc8d8SChris Lattner }
4030fdc8d8SChris Lattner 
ShouldStop(StoppointCallbackContext * context,lldb::break_id_t break_id)41b9c1b51eSKate Stone bool BreakpointLocationList::ShouldStop(StoppointCallbackContext *context,
42b9c1b51eSKate Stone                                         lldb::break_id_t break_id) {
4330fdc8d8SChris Lattner   BreakpointLocationSP bp = FindByID(break_id);
44b9c1b51eSKate Stone   if (bp) {
4530fdc8d8SChris Lattner     // Let the BreakpointLocation decide if it should stop here (could not have
4605097246SAdrian Prantl     // reached it's target hit count yet, or it could have a callback that
4705097246SAdrian Prantl     // decided it shouldn't stop (shared library loads/unloads).
4830fdc8d8SChris Lattner     return bp->ShouldStop(context);
4930fdc8d8SChris Lattner   }
5005097246SAdrian Prantl   // We should stop here since this BreakpointLocation isn't valid anymore or
5105097246SAdrian Prantl   // it doesn't exist.
5230fdc8d8SChris Lattner   return true;
5330fdc8d8SChris Lattner }
5430fdc8d8SChris Lattner 
FindIDByAddress(const Address & addr)55b9c1b51eSKate Stone lldb::break_id_t BreakpointLocationList::FindIDByAddress(const Address &addr) {
5630fdc8d8SChris Lattner   BreakpointLocationSP bp_loc_sp = FindByAddress(addr);
57b9c1b51eSKate Stone   if (bp_loc_sp) {
5830fdc8d8SChris Lattner     return bp_loc_sp->GetID();
5930fdc8d8SChris Lattner   }
6030fdc8d8SChris Lattner   return LLDB_INVALID_BREAK_ID;
6130fdc8d8SChris Lattner }
6230fdc8d8SChris Lattner 
Compare(BreakpointLocationSP lhs,lldb::break_id_t val)63b9c1b51eSKate Stone static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val) {
64177553e4SJim Ingham   return lhs->GetID() < val;
65177553e4SJim Ingham }
66177553e4SJim Ingham 
6730fdc8d8SChris Lattner BreakpointLocationSP
FindByID(lldb::break_id_t break_id) const68b9c1b51eSKate Stone BreakpointLocationList::FindByID(lldb::break_id_t break_id) const {
6916ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
70c6b26f05SGreg Clayton   collection::const_iterator end = m_locations.end();
71b9c1b51eSKate Stone   collection::const_iterator pos =
72b9c1b51eSKate Stone       std::lower_bound(m_locations.begin(), end, break_id, Compare);
73c6b26f05SGreg Clayton   if (pos != end && (*pos)->GetID() == break_id)
74c6b26f05SGreg Clayton     return *(pos);
75177553e4SJim Ingham   else
76c6b26f05SGreg Clayton     return BreakpointLocationSP();
7730fdc8d8SChris Lattner }
7830fdc8d8SChris Lattner 
FindInModule(Module * module,BreakpointLocationCollection & bp_loc_list)79b9c1b51eSKate Stone size_t BreakpointLocationList::FindInModule(
80b9c1b51eSKate Stone     Module *module, BreakpointLocationCollection &bp_loc_list) {
8116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
8230fdc8d8SChris Lattner   const size_t orig_size = bp_loc_list.GetSize();
8330fdc8d8SChris Lattner   collection::iterator pos, end = m_locations.end();
8430fdc8d8SChris Lattner 
85b9c1b51eSKate Stone   for (pos = m_locations.begin(); pos != end; ++pos) {
8630fdc8d8SChris Lattner     BreakpointLocationSP break_loc = (*pos);
87e72dfb32SGreg Clayton     SectionSP section_sp(break_loc->GetAddress().GetSection());
88b9c1b51eSKate Stone     if (section_sp && section_sp->GetModule().get() == module) {
8930fdc8d8SChris Lattner       bp_loc_list.Add(break_loc);
9030fdc8d8SChris Lattner     }
9130fdc8d8SChris Lattner   }
9230fdc8d8SChris Lattner   return bp_loc_list.GetSize() - orig_size;
9330fdc8d8SChris Lattner }
9430fdc8d8SChris Lattner 
9530fdc8d8SChris Lattner const BreakpointLocationSP
FindByAddress(const Address & addr) const96b9c1b51eSKate Stone BreakpointLocationList::FindByAddress(const Address &addr) const {
9716ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
98c0d34465SGreg Clayton   BreakpointLocationSP bp_loc_sp;
99b9c1b51eSKate Stone   if (!m_locations.empty()) {
100c87e752bSMichael Sartain     Address so_addr;
101c87e752bSMichael Sartain 
102b9c1b51eSKate Stone     if (addr.IsSectionOffset()) {
103c87e752bSMichael Sartain       so_addr = addr;
104b9c1b51eSKate Stone     } else {
105c87e752bSMichael Sartain       // Try and resolve as a load address if possible.
106b9c1b51eSKate Stone       m_owner.GetTarget().GetSectionLoadList().ResolveLoadAddress(
107b9c1b51eSKate Stone           addr.GetOffset(), so_addr);
108b9c1b51eSKate Stone       if (!so_addr.IsValid()) {
109c87e752bSMichael Sartain         // The address didn't resolve, so just set to passed in addr.
110c87e752bSMichael Sartain         so_addr = addr;
111c87e752bSMichael Sartain       }
112c87e752bSMichael Sartain     }
113c87e752bSMichael Sartain 
114c87e752bSMichael Sartain     addr_map::const_iterator pos = m_address_to_location.find(so_addr);
11530fdc8d8SChris Lattner     if (pos != m_address_to_location.end())
116c0d34465SGreg Clayton       bp_loc_sp = pos->second;
11730fdc8d8SChris Lattner   }
11830fdc8d8SChris Lattner 
119c0d34465SGreg Clayton   return bp_loc_sp;
12030fdc8d8SChris Lattner }
12130fdc8d8SChris Lattner 
Dump(Stream * s) const122b9c1b51eSKate Stone void BreakpointLocationList::Dump(Stream *s) const {
123324a1036SSaleem Abdulrasool   s->Printf("%p: ", static_cast<const void *>(this));
124ded470d3SGreg Clayton   // s->Indent();
12516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
126b9c1b51eSKate Stone   s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",
127b9c1b51eSKate Stone             (uint64_t)m_locations.size());
12830fdc8d8SChris Lattner   s->IndentMore();
12930fdc8d8SChris Lattner   collection::const_iterator pos, end = m_locations.end();
13030fdc8d8SChris Lattner   for (pos = m_locations.begin(); pos != end; ++pos)
13170355aceSJonas Devlieghere     (*pos)->Dump(s);
13230fdc8d8SChris Lattner   s->IndentLess();
13330fdc8d8SChris Lattner }
13430fdc8d8SChris Lattner 
GetByIndex(size_t i)135b9c1b51eSKate Stone BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) {
13616ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
137c0d34465SGreg Clayton   BreakpointLocationSP bp_loc_sp;
138c982c768SGreg Clayton   if (i < m_locations.size())
139c0d34465SGreg Clayton     bp_loc_sp = m_locations[i];
14030fdc8d8SChris Lattner 
141c0d34465SGreg Clayton   return bp_loc_sp;
14230fdc8d8SChris Lattner }
14330fdc8d8SChris Lattner 
GetByIndex(size_t i) const144b9c1b51eSKate Stone const BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) const {
14516ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
146c0d34465SGreg Clayton   BreakpointLocationSP bp_loc_sp;
147c982c768SGreg Clayton   if (i < m_locations.size())
148c0d34465SGreg Clayton     bp_loc_sp = m_locations[i];
14930fdc8d8SChris Lattner 
150c0d34465SGreg Clayton   return bp_loc_sp;
15130fdc8d8SChris Lattner }
15230fdc8d8SChris Lattner 
ClearAllBreakpointSites()153b9c1b51eSKate Stone void BreakpointLocationList::ClearAllBreakpointSites() {
15416ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
15530fdc8d8SChris Lattner   collection::iterator pos, end = m_locations.end();
15630fdc8d8SChris Lattner   for (pos = m_locations.begin(); pos != end; ++pos)
15730fdc8d8SChris Lattner     (*pos)->ClearBreakpointSite();
15830fdc8d8SChris Lattner }
15930fdc8d8SChris Lattner 
ResolveAllBreakpointSites()160b9c1b51eSKate Stone void BreakpointLocationList::ResolveAllBreakpointSites() {
16116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
16230fdc8d8SChris Lattner   collection::iterator pos, end = m_locations.end();
16330fdc8d8SChris Lattner 
164b9c1b51eSKate Stone   for (pos = m_locations.begin(); pos != end; ++pos) {
165d4ce0a15SJim Ingham     if ((*pos)->IsEnabled())
16630fdc8d8SChris Lattner       (*pos)->ResolveBreakpointSite();
16730fdc8d8SChris Lattner   }
168d4ce0a15SJim Ingham }
16930fdc8d8SChris Lattner 
GetHitCount() const170b9c1b51eSKate Stone uint32_t BreakpointLocationList::GetHitCount() const {
1719fed0d85SGreg Clayton   uint32_t hit_count = 0;
17216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1739fed0d85SGreg Clayton   collection::const_iterator pos, end = m_locations.end();
1749fed0d85SGreg Clayton   for (pos = m_locations.begin(); pos != end; ++pos)
1759fed0d85SGreg Clayton     hit_count += (*pos)->GetHitCount();
1769fed0d85SGreg Clayton   return hit_count;
1779fed0d85SGreg Clayton }
1789fed0d85SGreg Clayton 
GetNumResolvedLocations() const179b9c1b51eSKate Stone size_t BreakpointLocationList::GetNumResolvedLocations() const {
18016ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
18130fdc8d8SChris Lattner   size_t resolve_count = 0;
18230fdc8d8SChris Lattner   collection::const_iterator pos, end = m_locations.end();
183b9c1b51eSKate Stone   for (pos = m_locations.begin(); pos != end; ++pos) {
18430fdc8d8SChris Lattner     if ((*pos)->IsResolved())
18530fdc8d8SChris Lattner       ++resolve_count;
18630fdc8d8SChris Lattner   }
18730fdc8d8SChris Lattner   return resolve_count;
18830fdc8d8SChris Lattner }
18930fdc8d8SChris Lattner 
GetDescription(Stream * s,lldb::DescriptionLevel level)190b9c1b51eSKate Stone void BreakpointLocationList::GetDescription(Stream *s,
191b9c1b51eSKate Stone                                             lldb::DescriptionLevel level) {
19216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
19330fdc8d8SChris Lattner   collection::iterator pos, end = m_locations.end();
19430fdc8d8SChris Lattner 
195b9c1b51eSKate Stone   for (pos = m_locations.begin(); pos != end; ++pos) {
19630fdc8d8SChris Lattner     s->Printf(" ");
19730fdc8d8SChris Lattner     (*pos)->GetDescription(s, level);
19830fdc8d8SChris Lattner   }
19930fdc8d8SChris Lattner }
20030fdc8d8SChris Lattner 
AddLocation(const Address & addr,bool resolve_indirect_symbols,bool * new_location)201b9c1b51eSKate Stone BreakpointLocationSP BreakpointLocationList::AddLocation(
202b9c1b51eSKate Stone     const Address &addr, bool resolve_indirect_symbols, bool *new_location) {
20316ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
204e6bc6cb9SJim Ingham 
205e6bc6cb9SJim Ingham   if (new_location)
206e6bc6cb9SJim Ingham     *new_location = false;
207e6bc6cb9SJim Ingham   BreakpointLocationSP bp_loc_sp(FindByAddress(addr));
208b9c1b51eSKate Stone   if (!bp_loc_sp) {
2091460e4bfSJim Ingham     bp_loc_sp = Create(addr, resolve_indirect_symbols);
210b9c1b51eSKate Stone     if (bp_loc_sp) {
211e6bc6cb9SJim Ingham       bp_loc_sp->ResolveBreakpointSite();
212e6bc6cb9SJim Ingham 
213e6bc6cb9SJim Ingham       if (new_location)
214e6bc6cb9SJim Ingham         *new_location = true;
215b9c1b51eSKate Stone       if (m_new_location_recorder) {
216e6bc6cb9SJim Ingham         m_new_location_recorder->Add(bp_loc_sp);
217e6bc6cb9SJim Ingham       }
218e6bc6cb9SJim Ingham     }
219e6bc6cb9SJim Ingham   }
220e6bc6cb9SJim Ingham   return bp_loc_sp;
221e6bc6cb9SJim Ingham }
222e6bc6cb9SJim Ingham 
SwapLocation(BreakpointLocationSP to_location_sp,BreakpointLocationSP from_location_sp)223b9c1b51eSKate Stone void BreakpointLocationList::SwapLocation(
224b9c1b51eSKate Stone     BreakpointLocationSP to_location_sp,
225b9c1b51eSKate Stone     BreakpointLocationSP from_location_sp) {
22677fd738fSJim Ingham   if (!from_location_sp || !to_location_sp)
22777fd738fSJim Ingham     return;
22877fd738fSJim Ingham 
22977fd738fSJim Ingham   m_address_to_location.erase(to_location_sp->GetAddress());
23077fd738fSJim Ingham   to_location_sp->SwapLocation(from_location_sp);
23177fd738fSJim Ingham   RemoveLocation(from_location_sp);
23277fd738fSJim Ingham   m_address_to_location[to_location_sp->GetAddress()] = to_location_sp;
23377fd738fSJim Ingham   to_location_sp->ResolveBreakpointSite();
23477fd738fSJim Ingham }
23577fd738fSJim Ingham 
RemoveLocation(const lldb::BreakpointLocationSP & bp_loc_sp)236b9c1b51eSKate Stone bool BreakpointLocationList::RemoveLocation(
237b9c1b51eSKate Stone     const lldb::BreakpointLocationSP &bp_loc_sp) {
238b9c1b51eSKate Stone   if (bp_loc_sp) {
23916ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(m_mutex);
240e72dfb32SGreg Clayton 
241e72dfb32SGreg Clayton     m_address_to_location.erase(bp_loc_sp->GetAddress());
242e72dfb32SGreg Clayton 
2435ec7cd72SJim Ingham     size_t num_locations = m_locations.size();
2445ec7cd72SJim Ingham     for (size_t idx = 0; idx < num_locations; idx++) {
2455ec7cd72SJim Ingham       if (m_locations[idx].get() == bp_loc_sp.get()) {
2465ec7cd72SJim Ingham         RemoveLocationByIndex(idx);
247e72dfb32SGreg Clayton         return true;
248e72dfb32SGreg Clayton       }
249e72dfb32SGreg Clayton     }
250e72dfb32SGreg Clayton   }
251e72dfb32SGreg Clayton   return false;
252e72dfb32SGreg Clayton }
253e72dfb32SGreg Clayton 
RemoveLocationByIndex(size_t idx)2545ec7cd72SJim Ingham void BreakpointLocationList::RemoveLocationByIndex(size_t idx) {
2555ec7cd72SJim Ingham   assert (idx < m_locations.size());
2565ec7cd72SJim Ingham   m_address_to_location.erase(m_locations[idx]->GetAddress());
2575ec7cd72SJim Ingham   m_locations.erase(m_locations.begin() + idx);
2585ec7cd72SJim Ingham }
2595ec7cd72SJim Ingham 
RemoveInvalidLocations(const ArchSpec & arch)260b9c1b51eSKate Stone void BreakpointLocationList::RemoveInvalidLocations(const ArchSpec &arch) {
26116ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
262b35db639SGreg Clayton   size_t idx = 0;
26305097246SAdrian Prantl   // Don't cache m_location.size() as it will change since we might remove
26405097246SAdrian Prantl   // locations from our vector...
265b9c1b51eSKate Stone   while (idx < m_locations.size()) {
266b35db639SGreg Clayton     BreakpointLocation *bp_loc = m_locations[idx].get();
267b9c1b51eSKate Stone     if (bp_loc->GetAddress().SectionWasDeleted()) {
268b35db639SGreg Clayton       // Section was deleted which means this breakpoint comes from a module
269b35db639SGreg Clayton       // that is no longer valid, so we should remove it.
2705ec7cd72SJim Ingham       RemoveLocationByIndex(idx);
271b35db639SGreg Clayton       continue;
272b35db639SGreg Clayton     }
273b9c1b51eSKate Stone     if (arch.IsValid()) {
274b35db639SGreg Clayton       ModuleSP module_sp(bp_loc->GetAddress().GetModule());
275b9c1b51eSKate Stone       if (module_sp) {
276b9c1b51eSKate Stone         if (!arch.IsCompatibleMatch(module_sp->GetArchitecture())) {
277b35db639SGreg Clayton           // The breakpoint was in a module whose architecture is no longer
278b35db639SGreg Clayton           // compatible with "arch", so we need to remove it
2795ec7cd72SJim Ingham           RemoveLocationByIndex(idx);
280b35db639SGreg Clayton           continue;
281b35db639SGreg Clayton         }
282b35db639SGreg Clayton       }
283b35db639SGreg Clayton     }
28405097246SAdrian Prantl     // Only increment the index if we didn't remove the locations at index
28505097246SAdrian Prantl     // "idx"
286b35db639SGreg Clayton     ++idx;
287b35db639SGreg Clayton   }
288b35db639SGreg Clayton }
289e6bc6cb9SJim Ingham 
StartRecordingNewLocations(BreakpointLocationCollection & new_locations)290b9c1b51eSKate Stone void BreakpointLocationList::StartRecordingNewLocations(
291b9c1b51eSKate Stone     BreakpointLocationCollection &new_locations) {
29216ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
29316fd7511SEugene Zelenko   assert(m_new_location_recorder == nullptr);
294e6bc6cb9SJim Ingham   m_new_location_recorder = &new_locations;
295e6bc6cb9SJim Ingham }
296e6bc6cb9SJim Ingham 
StopRecordingNewLocations()297b9c1b51eSKate Stone void BreakpointLocationList::StopRecordingNewLocations() {
29816ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_mutex);
29916fd7511SEugene Zelenko   m_new_location_recorder = nullptr;
300e6bc6cb9SJim Ingham }
301e6bc6cb9SJim Ingham 
Compact()302b9c1b51eSKate Stone void BreakpointLocationList::Compact() {
30377fd738fSJim Ingham   lldb::break_id_t highest_id = 0;
30477fd738fSJim Ingham 
305b9c1b51eSKate Stone   for (BreakpointLocationSP loc_sp : m_locations) {
30677fd738fSJim Ingham     lldb::break_id_t cur_id = loc_sp->GetID();
30777fd738fSJim Ingham     if (cur_id > highest_id)
30877fd738fSJim Ingham       highest_id = cur_id;
30977fd738fSJim Ingham   }
31077fd738fSJim Ingham   m_next_id = highest_id;
31177fd738fSJim Ingham }
312