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 //===----------------------------------------------------------------------===//
10 #include "lldb/Breakpoint/BreakpointLocationList.h"
12 #include "lldb/Breakpoint/Breakpoint.h"
13 #include "lldb/Breakpoint/BreakpointLocation.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Target/SectionLoadList.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Utility/ArchSpec.h"
21 using namespace lldb_private;
23 BreakpointLocationList::BreakpointLocationList(Breakpoint &owner)
24 : m_owner(owner), m_locations(), m_address_to_location(), m_mutex(),
25 m_next_id(0), m_new_location_recorder(nullptr) {}
27 BreakpointLocationList::~BreakpointLocationList() = default;
30 BreakpointLocationList::Create(const Address &addr,
31 bool resolve_indirect_symbols) {
32 std::lock_guard<std::recursive_mutex> guard(m_mutex);
33 // The location ID is just the size of the location list + 1
34 lldb::break_id_t bp_loc_id = ++m_next_id;
35 BreakpointLocationSP bp_loc_sp(
36 new BreakpointLocation(bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID,
37 m_owner.IsHardware(), resolve_indirect_symbols));
38 m_locations.push_back(bp_loc_sp);
39 m_address_to_location[addr] = bp_loc_sp;
43 bool BreakpointLocationList::ShouldStop(StoppointCallbackContext *context,
44 lldb::break_id_t break_id) {
45 BreakpointLocationSP bp = FindByID(break_id);
47 // Let the BreakpointLocation decide if it should stop here (could not have
48 // reached it's target hit count yet, or it could have a callback that
49 // decided it shouldn't stop (shared library loads/unloads).
50 return bp->ShouldStop(context);
52 // We should stop here since this BreakpointLocation isn't valid anymore or
57 lldb::break_id_t BreakpointLocationList::FindIDByAddress(const Address &addr) {
58 BreakpointLocationSP bp_loc_sp = FindByAddress(addr);
60 return bp_loc_sp->GetID();
62 return LLDB_INVALID_BREAK_ID;
65 static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val) {
66 return lhs->GetID() < val;
70 BreakpointLocationList::FindByID(lldb::break_id_t break_id) const {
71 std::lock_guard<std::recursive_mutex> guard(m_mutex);
72 collection::const_iterator end = m_locations.end();
73 collection::const_iterator pos =
74 std::lower_bound(m_locations.begin(), end, break_id, Compare);
75 if (pos != end && (*pos)->GetID() == break_id)
78 return BreakpointLocationSP();
81 size_t BreakpointLocationList::FindInModule(
82 Module *module, BreakpointLocationCollection &bp_loc_list) {
83 std::lock_guard<std::recursive_mutex> guard(m_mutex);
84 const size_t orig_size = bp_loc_list.GetSize();
85 collection::iterator pos, end = m_locations.end();
87 for (pos = m_locations.begin(); pos != end; ++pos) {
88 BreakpointLocationSP break_loc = (*pos);
89 SectionSP section_sp(break_loc->GetAddress().GetSection());
90 if (section_sp && section_sp->GetModule().get() == module) {
91 bp_loc_list.Add(break_loc);
94 return bp_loc_list.GetSize() - orig_size;
97 const BreakpointLocationSP
98 BreakpointLocationList::FindByAddress(const Address &addr) const {
99 std::lock_guard<std::recursive_mutex> guard(m_mutex);
100 BreakpointLocationSP bp_loc_sp;
101 if (!m_locations.empty()) {
104 if (addr.IsSectionOffset()) {
107 // Try and resolve as a load address if possible.
108 m_owner.GetTarget().GetSectionLoadList().ResolveLoadAddress(
109 addr.GetOffset(), so_addr);
110 if (!so_addr.IsValid()) {
111 // The address didn't resolve, so just set to passed in addr.
116 addr_map::const_iterator pos = m_address_to_location.find(so_addr);
117 if (pos != m_address_to_location.end())
118 bp_loc_sp = pos->second;
124 void BreakpointLocationList::Dump(Stream *s) const {
125 s->Printf("%p: ", static_cast<const void *>(this));
127 std::lock_guard<std::recursive_mutex> guard(m_mutex);
128 s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",
129 (uint64_t)m_locations.size());
131 collection::const_iterator pos, end = m_locations.end();
132 for (pos = m_locations.begin(); pos != end; ++pos)
133 (*pos).get()->Dump(s);
137 BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) {
138 std::lock_guard<std::recursive_mutex> guard(m_mutex);
139 BreakpointLocationSP bp_loc_sp;
140 if (i < m_locations.size())
141 bp_loc_sp = m_locations[i];
146 const BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) const {
147 std::lock_guard<std::recursive_mutex> guard(m_mutex);
148 BreakpointLocationSP bp_loc_sp;
149 if (i < m_locations.size())
150 bp_loc_sp = m_locations[i];
155 void BreakpointLocationList::ClearAllBreakpointSites() {
156 std::lock_guard<std::recursive_mutex> guard(m_mutex);
157 collection::iterator pos, end = m_locations.end();
158 for (pos = m_locations.begin(); pos != end; ++pos)
159 (*pos)->ClearBreakpointSite();
162 void BreakpointLocationList::ResolveAllBreakpointSites() {
163 std::lock_guard<std::recursive_mutex> guard(m_mutex);
164 collection::iterator pos, end = m_locations.end();
166 for (pos = m_locations.begin(); pos != end; ++pos) {
167 if ((*pos)->IsEnabled())
168 (*pos)->ResolveBreakpointSite();
172 uint32_t BreakpointLocationList::GetHitCount() const {
173 uint32_t hit_count = 0;
174 std::lock_guard<std::recursive_mutex> guard(m_mutex);
175 collection::const_iterator pos, end = m_locations.end();
176 for (pos = m_locations.begin(); pos != end; ++pos)
177 hit_count += (*pos)->GetHitCount();
181 size_t BreakpointLocationList::GetNumResolvedLocations() const {
182 std::lock_guard<std::recursive_mutex> guard(m_mutex);
183 size_t resolve_count = 0;
184 collection::const_iterator pos, end = m_locations.end();
185 for (pos = m_locations.begin(); pos != end; ++pos) {
186 if ((*pos)->IsResolved())
189 return resolve_count;
192 void BreakpointLocationList::GetDescription(Stream *s,
193 lldb::DescriptionLevel level) {
194 std::lock_guard<std::recursive_mutex> guard(m_mutex);
195 collection::iterator pos, end = m_locations.end();
197 for (pos = m_locations.begin(); pos != end; ++pos) {
199 (*pos)->GetDescription(s, level);
203 BreakpointLocationSP BreakpointLocationList::AddLocation(
204 const Address &addr, bool resolve_indirect_symbols, bool *new_location) {
205 std::lock_guard<std::recursive_mutex> guard(m_mutex);
208 *new_location = false;
209 BreakpointLocationSP bp_loc_sp(FindByAddress(addr));
211 bp_loc_sp = Create(addr, resolve_indirect_symbols);
213 bp_loc_sp->ResolveBreakpointSite();
216 *new_location = true;
217 if (m_new_location_recorder) {
218 m_new_location_recorder->Add(bp_loc_sp);
225 void BreakpointLocationList::SwapLocation(
226 BreakpointLocationSP to_location_sp,
227 BreakpointLocationSP from_location_sp) {
228 if (!from_location_sp || !to_location_sp)
231 m_address_to_location.erase(to_location_sp->GetAddress());
232 to_location_sp->SwapLocation(from_location_sp);
233 RemoveLocation(from_location_sp);
234 m_address_to_location[to_location_sp->GetAddress()] = to_location_sp;
235 to_location_sp->ResolveBreakpointSite();
238 bool BreakpointLocationList::RemoveLocation(
239 const lldb::BreakpointLocationSP &bp_loc_sp) {
241 std::lock_guard<std::recursive_mutex> guard(m_mutex);
243 m_address_to_location.erase(bp_loc_sp->GetAddress());
245 size_t num_locations = m_locations.size();
246 for (size_t idx = 0; idx < num_locations; idx++) {
247 if (m_locations[idx].get() == bp_loc_sp.get()) {
248 RemoveLocationByIndex(idx);
256 void BreakpointLocationList::RemoveLocationByIndex(size_t idx) {
257 assert (idx < m_locations.size());
258 m_address_to_location.erase(m_locations[idx]->GetAddress());
259 m_locations.erase(m_locations.begin() + idx);
262 void BreakpointLocationList::RemoveInvalidLocations(const ArchSpec &arch) {
263 std::lock_guard<std::recursive_mutex> guard(m_mutex);
265 // Don't cache m_location.size() as it will change since we might remove
266 // locations from our vector...
267 while (idx < m_locations.size()) {
268 BreakpointLocation *bp_loc = m_locations[idx].get();
269 if (bp_loc->GetAddress().SectionWasDeleted()) {
270 // Section was deleted which means this breakpoint comes from a module
271 // that is no longer valid, so we should remove it.
272 RemoveLocationByIndex(idx);
275 if (arch.IsValid()) {
276 ModuleSP module_sp(bp_loc->GetAddress().GetModule());
278 if (!arch.IsCompatibleMatch(module_sp->GetArchitecture())) {
279 // The breakpoint was in a module whose architecture is no longer
280 // compatible with "arch", so we need to remove it
281 RemoveLocationByIndex(idx);
286 // Only increment the index if we didn't remove the locations at index
292 void BreakpointLocationList::StartRecordingNewLocations(
293 BreakpointLocationCollection &new_locations) {
294 std::lock_guard<std::recursive_mutex> guard(m_mutex);
295 assert(m_new_location_recorder == nullptr);
296 m_new_location_recorder = &new_locations;
299 void BreakpointLocationList::StopRecordingNewLocations() {
300 std::lock_guard<std::recursive_mutex> guard(m_mutex);
301 m_new_location_recorder = nullptr;
304 void BreakpointLocationList::Compact() {
305 lldb::break_id_t highest_id = 0;
307 for (BreakpointLocationSP loc_sp : m_locations) {
308 lldb::break_id_t cur_id = loc_sp->GetID();
309 if (cur_id > highest_id)
312 m_next_id = highest_id;