1 //===-- BreakpointLocationList.cpp ------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Breakpoint/BreakpointLocationList.h"
11
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"
19
20 using namespace lldb;
21 using namespace lldb_private;
22
BreakpointLocationList(Breakpoint & owner)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) {}
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 m_owner.IsHardware(), 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 std::lower_bound(m_locations.begin(), end, break_id, Compare);
75 if (pos != end && (*pos)->GetID() == break_id)
76 return *(pos);
77 else
78 return BreakpointLocationSP();
79 }
80
FindInModule(Module * module,BreakpointLocationCollection & bp_loc_list)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();
86
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);
92 }
93 }
94 return bp_loc_list.GetSize() - orig_size;
95 }
96
97 const BreakpointLocationSP
FindByAddress(const Address & addr) const98 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()) {
102 Address so_addr;
103
104 if (addr.IsSectionOffset()) {
105 so_addr = addr;
106 } else {
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.
112 so_addr = addr;
113 }
114 }
115
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;
119 }
120
121 return bp_loc_sp;
122 }
123
Dump(Stream * s) const124 void BreakpointLocationList::Dump(Stream *s) const {
125 s->Printf("%p: ", static_cast<const void *>(this));
126 // s->Indent();
127 std::lock_guard<std::recursive_mutex> guard(m_mutex);
128 s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",
129 (uint64_t)m_locations.size());
130 s->IndentMore();
131 collection::const_iterator pos, end = m_locations.end();
132 for (pos = m_locations.begin(); pos != end; ++pos)
133 (*pos).get()->Dump(s);
134 s->IndentLess();
135 }
136
GetByIndex(size_t i)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];
142
143 return bp_loc_sp;
144 }
145
GetByIndex(size_t i) const146 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];
151
152 return bp_loc_sp;
153 }
154
ClearAllBreakpointSites()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();
160 }
161
ResolveAllBreakpointSites()162 void BreakpointLocationList::ResolveAllBreakpointSites() {
163 std::lock_guard<std::recursive_mutex> guard(m_mutex);
164 collection::iterator pos, end = m_locations.end();
165
166 for (pos = m_locations.begin(); pos != end; ++pos) {
167 if ((*pos)->IsEnabled())
168 (*pos)->ResolveBreakpointSite();
169 }
170 }
171
GetHitCount() const172 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();
178 return hit_count;
179 }
180
GetNumResolvedLocations() const181 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())
187 ++resolve_count;
188 }
189 return resolve_count;
190 }
191
GetDescription(Stream * s,lldb::DescriptionLevel level)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();
196
197 for (pos = m_locations.begin(); pos != end; ++pos) {
198 s->Printf(" ");
199 (*pos)->GetDescription(s, level);
200 }
201 }
202
AddLocation(const Address & addr,bool resolve_indirect_symbols,bool * new_location)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);
206
207 if (new_location)
208 *new_location = false;
209 BreakpointLocationSP bp_loc_sp(FindByAddress(addr));
210 if (!bp_loc_sp) {
211 bp_loc_sp = Create(addr, resolve_indirect_symbols);
212 if (bp_loc_sp) {
213 bp_loc_sp->ResolveBreakpointSite();
214
215 if (new_location)
216 *new_location = true;
217 if (m_new_location_recorder) {
218 m_new_location_recorder->Add(bp_loc_sp);
219 }
220 }
221 }
222 return bp_loc_sp;
223 }
224
SwapLocation(BreakpointLocationSP to_location_sp,BreakpointLocationSP from_location_sp)225 void BreakpointLocationList::SwapLocation(
226 BreakpointLocationSP to_location_sp,
227 BreakpointLocationSP from_location_sp) {
228 if (!from_location_sp || !to_location_sp)
229 return;
230
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();
236 }
237
RemoveLocation(const lldb::BreakpointLocationSP & bp_loc_sp)238 bool BreakpointLocationList::RemoveLocation(
239 const lldb::BreakpointLocationSP &bp_loc_sp) {
240 if (bp_loc_sp) {
241 std::lock_guard<std::recursive_mutex> guard(m_mutex);
242
243 m_address_to_location.erase(bp_loc_sp->GetAddress());
244
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);
249 return true;
250 }
251 }
252 }
253 return false;
254 }
255
RemoveLocationByIndex(size_t 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);
260 }
261
RemoveInvalidLocations(const ArchSpec & arch)262 void BreakpointLocationList::RemoveInvalidLocations(const ArchSpec &arch) {
263 std::lock_guard<std::recursive_mutex> guard(m_mutex);
264 size_t idx = 0;
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);
273 continue;
274 }
275 if (arch.IsValid()) {
276 ModuleSP module_sp(bp_loc->GetAddress().GetModule());
277 if (module_sp) {
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);
282 continue;
283 }
284 }
285 }
286 // Only increment the index if we didn't remove the locations at index
287 // "idx"
288 ++idx;
289 }
290 }
291
StartRecordingNewLocations(BreakpointLocationCollection & new_locations)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;
297 }
298
StopRecordingNewLocations()299 void BreakpointLocationList::StopRecordingNewLocations() {
300 std::lock_guard<std::recursive_mutex> guard(m_mutex);
301 m_new_location_recorder = nullptr;
302 }
303
Compact()304 void BreakpointLocationList::Compact() {
305 lldb::break_id_t highest_id = 0;
306
307 for (BreakpointLocationSP loc_sp : m_locations) {
308 lldb::break_id_t cur_id = loc_sp->GetID();
309 if (cur_id > highest_id)
310 highest_id = cur_id;
311 }
312 m_next_id = highest_id;
313 }
314