1 //===-- BreakpointLocationList.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
9 #include "lldb/Breakpoint/BreakpointLocationList.h"
10
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Target/SectionLoadList.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/ArchSpec.h"
18 #include "lldb/Utility/LLDBLog.h"
19 #include "lldb/Utility/Log.h"
20
21 using namespace lldb;
22 using namespace lldb_private;
23
BreakpointLocationList(Breakpoint & owner)24 BreakpointLocationList::BreakpointLocationList(Breakpoint &owner)
25 : m_owner(owner), m_next_id(0), m_new_location_recorder(nullptr) {}
26
27 BreakpointLocationList::~BreakpointLocationList() = default;
28
29 BreakpointLocationSP
Create(const Address & addr,bool resolve_indirect_symbols)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 resolve_indirect_symbols));
38 m_locations.push_back(bp_loc_sp);
39 m_address_to_location[addr] = bp_loc_sp;
40 return bp_loc_sp;
41 }
42
ShouldStop(StoppointCallbackContext * context,lldb::break_id_t break_id)43 bool BreakpointLocationList::ShouldStop(StoppointCallbackContext *context,
44 lldb::break_id_t break_id) {
45 BreakpointLocationSP bp = FindByID(break_id);
46 if (bp) {
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);
51 }
52 // We should stop here since this BreakpointLocation isn't valid anymore or
53 // it doesn't exist.
54 return true;
55 }
56
FindIDByAddress(const Address & addr)57 lldb::break_id_t BreakpointLocationList::FindIDByAddress(const Address &addr) {
58 BreakpointLocationSP bp_loc_sp = FindByAddress(addr);
59 if (bp_loc_sp) {
60 return bp_loc_sp->GetID();
61 }
62 return LLDB_INVALID_BREAK_ID;
63 }
64
Compare(BreakpointLocationSP lhs,lldb::break_id_t val)65 static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val) {
66 return lhs->GetID() < val;
67 }
68
69 BreakpointLocationSP
FindByID(lldb::break_id_t break_id) const70 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 llvm::lower_bound(m_locations, break_id, Compare);
75 if (pos != end && (*pos)->GetID() == break_id)
76 return *(pos);
77 return BreakpointLocationSP();
78 }
79
FindInModule(Module * module,BreakpointLocationCollection & bp_loc_list)80 size_t BreakpointLocationList::FindInModule(
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
FindByAddress(const Address & addr) const97 BreakpointLocationList::FindByAddress(const Address &addr) const {
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.
107 m_owner.GetTarget().ResolveLoadAddress(addr.GetOffset(), so_addr);
108 if (!so_addr.IsValid()) {
109 // The address didn't resolve, so just set to passed in addr.
110 so_addr = addr;
111 }
112 }
113
114 addr_map::const_iterator pos = m_address_to_location.find(so_addr);
115 if (pos != m_address_to_location.end())
116 bp_loc_sp = pos->second;
117 }
118
119 return bp_loc_sp;
120 }
121
Dump(Stream * s) const122 void BreakpointLocationList::Dump(Stream *s) const {
123 s->Printf("%p: ", static_cast<const void *>(this));
124 // s->Indent();
125 std::lock_guard<std::recursive_mutex> guard(m_mutex);
126 s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",
127 (uint64_t)m_locations.size());
128 s->IndentMore();
129 collection::const_iterator pos, end = m_locations.end();
130 for (pos = m_locations.begin(); pos != end; ++pos)
131 (*pos)->Dump(s);
132 s->IndentLess();
133 }
134
GetByIndex(size_t i)135 BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) {
136 std::lock_guard<std::recursive_mutex> guard(m_mutex);
137 BreakpointLocationSP bp_loc_sp;
138 if (i < m_locations.size())
139 bp_loc_sp = m_locations[i];
140
141 return bp_loc_sp;
142 }
143
GetByIndex(size_t i) const144 const BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) const {
145 std::lock_guard<std::recursive_mutex> guard(m_mutex);
146 BreakpointLocationSP bp_loc_sp;
147 if (i < m_locations.size())
148 bp_loc_sp = m_locations[i];
149
150 return bp_loc_sp;
151 }
152
ClearAllBreakpointSites()153 void BreakpointLocationList::ClearAllBreakpointSites() {
154 std::lock_guard<std::recursive_mutex> guard(m_mutex);
155 collection::iterator pos, end = m_locations.end();
156 Log *log = GetLog(LLDBLog::Breakpoints);
157
158 for (pos = m_locations.begin(); pos != end; ++pos) {
159 if (llvm::Error error = (*pos)->ClearBreakpointSite())
160 LLDB_LOG_ERROR(log, std::move(error), "{0}");
161 }
162 }
163
ResolveAllBreakpointSites()164 void BreakpointLocationList::ResolveAllBreakpointSites() {
165 std::lock_guard<std::recursive_mutex> guard(m_mutex);
166 collection::iterator pos, end = m_locations.end();
167 Log *log = GetLog(LLDBLog::Breakpoints);
168
169 for (pos = m_locations.begin(); pos != end; ++pos) {
170 if ((*pos)->IsEnabled()) {
171 if (llvm::Error error = (*pos)->ResolveBreakpointSite())
172 LLDB_LOG_ERROR(log, std::move(error), "{0}");
173 }
174 }
175 }
176
GetHitCount() const177 uint32_t BreakpointLocationList::GetHitCount() const {
178 uint32_t hit_count = 0;
179 std::lock_guard<std::recursive_mutex> guard(m_mutex);
180 collection::const_iterator pos, end = m_locations.end();
181 for (pos = m_locations.begin(); pos != end; ++pos)
182 hit_count += (*pos)->GetHitCount();
183 return hit_count;
184 }
185
ResetHitCount()186 void BreakpointLocationList::ResetHitCount() {
187 std::lock_guard<std::recursive_mutex> guard(m_mutex);
188 for (auto &loc : m_locations)
189 loc->ResetHitCount();
190 }
191
GetNumResolvedLocations() const192 size_t BreakpointLocationList::GetNumResolvedLocations() const {
193 std::lock_guard<std::recursive_mutex> guard(m_mutex);
194 size_t resolve_count = 0;
195 collection::const_iterator pos, end = m_locations.end();
196 for (pos = m_locations.begin(); pos != end; ++pos) {
197 if ((*pos)->IsResolved())
198 ++resolve_count;
199 }
200 return resolve_count;
201 }
202
GetDescription(Stream * s,lldb::DescriptionLevel level)203 void BreakpointLocationList::GetDescription(Stream *s,
204 lldb::DescriptionLevel level) {
205 std::lock_guard<std::recursive_mutex> guard(m_mutex);
206 collection::iterator pos, end = m_locations.end();
207
208 for (pos = m_locations.begin(); pos != end; ++pos) {
209 s->Printf(" ");
210 (*pos)->GetDescription(s, level);
211 }
212 }
213
AddLocation(const Address & addr,bool resolve_indirect_symbols,bool * new_location)214 BreakpointLocationSP BreakpointLocationList::AddLocation(
215 const Address &addr, bool resolve_indirect_symbols, bool *new_location) {
216 std::lock_guard<std::recursive_mutex> guard(m_mutex);
217
218 if (new_location)
219 *new_location = false;
220 BreakpointLocationSP bp_loc_sp(FindByAddress(addr));
221 if (!bp_loc_sp) {
222 bp_loc_sp = Create(addr, resolve_indirect_symbols);
223 if (bp_loc_sp) {
224 if (llvm::Error error = bp_loc_sp->ResolveBreakpointSite())
225 LLDB_LOG_ERROR(GetLog(LLDBLog::Breakpoints), std::move(error), "{0}");
226
227 if (new_location)
228 *new_location = true;
229 if (m_new_location_recorder) {
230 m_new_location_recorder->Add(bp_loc_sp);
231 }
232 }
233 }
234 return bp_loc_sp;
235 }
236
SwapLocation(BreakpointLocationSP to_location_sp,BreakpointLocationSP from_location_sp)237 void BreakpointLocationList::SwapLocation(
238 BreakpointLocationSP to_location_sp,
239 BreakpointLocationSP from_location_sp) {
240 if (!from_location_sp || !to_location_sp)
241 return;
242
243 m_address_to_location.erase(to_location_sp->GetAddress());
244 to_location_sp->SwapLocation(from_location_sp);
245 RemoveLocation(from_location_sp);
246 m_address_to_location[to_location_sp->GetAddress()] = to_location_sp;
247 llvm::consumeError(to_location_sp->ResolveBreakpointSite());
248 }
249
RemoveLocation(const lldb::BreakpointLocationSP & bp_loc_sp)250 bool BreakpointLocationList::RemoveLocation(
251 const lldb::BreakpointLocationSP &bp_loc_sp) {
252 if (bp_loc_sp) {
253 std::lock_guard<std::recursive_mutex> guard(m_mutex);
254
255 m_address_to_location.erase(bp_loc_sp->GetAddress());
256
257 size_t num_locations = m_locations.size();
258 for (size_t idx = 0; idx < num_locations; idx++) {
259 if (m_locations[idx].get() == bp_loc_sp.get()) {
260 RemoveLocationByIndex(idx);
261 return true;
262 }
263 }
264 }
265 return false;
266 }
267
RemoveLocationByIndex(size_t idx)268 void BreakpointLocationList::RemoveLocationByIndex(size_t idx) {
269 assert (idx < m_locations.size());
270 m_address_to_location.erase(m_locations[idx]->GetAddress());
271 m_locations.erase(m_locations.begin() + idx);
272 }
273
RemoveInvalidLocations(const ArchSpec & arch)274 void BreakpointLocationList::RemoveInvalidLocations(const ArchSpec &arch) {
275 std::lock_guard<std::recursive_mutex> guard(m_mutex);
276 size_t idx = 0;
277 // Don't cache m_location.size() as it will change since we might remove
278 // locations from our vector...
279 while (idx < m_locations.size()) {
280 BreakpointLocation *bp_loc = m_locations[idx].get();
281 if (bp_loc->GetAddress().SectionWasDeleted()) {
282 // Section was deleted which means this breakpoint comes from a module
283 // that is no longer valid, so we should remove it.
284 RemoveLocationByIndex(idx);
285 continue;
286 }
287 if (arch.IsValid()) {
288 ModuleSP module_sp(bp_loc->GetAddress().GetModule());
289 if (module_sp) {
290 if (!arch.IsCompatibleMatch(module_sp->GetArchitecture())) {
291 // The breakpoint was in a module whose architecture is no longer
292 // compatible with "arch", so we need to remove it
293 RemoveLocationByIndex(idx);
294 continue;
295 }
296 }
297 }
298 // Only increment the index if we didn't remove the locations at index
299 // "idx"
300 ++idx;
301 }
302 }
303
StartRecordingNewLocations(BreakpointLocationCollection & new_locations)304 void BreakpointLocationList::StartRecordingNewLocations(
305 BreakpointLocationCollection &new_locations) {
306 std::lock_guard<std::recursive_mutex> guard(m_mutex);
307 assert(m_new_location_recorder == nullptr);
308 m_new_location_recorder = &new_locations;
309 }
310
StopRecordingNewLocations()311 void BreakpointLocationList::StopRecordingNewLocations() {
312 std::lock_guard<std::recursive_mutex> guard(m_mutex);
313 m_new_location_recorder = nullptr;
314 }
315
Compact()316 void BreakpointLocationList::Compact() {
317 lldb::break_id_t highest_id = 0;
318
319 for (BreakpointLocationSP loc_sp : m_locations) {
320 lldb::break_id_t cur_id = loc_sp->GetID();
321 if (cur_id > highest_id)
322 highest_id = cur_id;
323 }
324 m_next_id = highest_id;
325 }
326