blob: 8be3d9b67e2e18a6f3507cf282ad2b48dd5babd4 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
11// C Includes
12// C++ Includes
13// Other libraries and framework includes
14// Project includes
15#include "lldb/Breakpoint/BreakpointLocationList.h"
Greg Claytonb35db632013-11-09 00:03:31 +000016
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Breakpoint/BreakpointLocation.h"
Michael Sartainc87e7522013-07-16 21:22:53 +000018#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonb35db632013-11-09 00:03:31 +000019#include "lldb/Core/ArchSpec.h"
20#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Section.h"
Michael Sartainc87e7522013-07-16 21:22:53 +000022#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
Jim Inghame6bc6cb2012-02-08 05:23:15 +000027BreakpointLocationList::BreakpointLocationList(Breakpoint &owner) :
Bill Wendlingc9851e22012-04-03 04:14:58 +000028 m_owner (owner),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029 m_locations(),
30 m_address_to_location (),
Jim Inghame6bc6cb2012-02-08 05:23:15 +000031 m_mutex (Mutex::eMutexTypeRecursive),
Bill Wendlingc9851e22012-04-03 04:14:58 +000032 m_next_id (0),
33 m_new_location_recorder (NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034{
35}
36
37BreakpointLocationList::~BreakpointLocationList()
38{
39}
40
Greg Claytonc0d34462011-02-05 00:38:04 +000041BreakpointLocationSP
Jim Inghame6bc6cb2012-02-08 05:23:15 +000042BreakpointLocationList::Create (const Address &addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043{
Greg Claytonc0d34462011-02-05 00:38:04 +000044 Mutex::Locker locker (m_mutex);
45 // The location ID is just the size of the location list + 1
Greg Claytone72dfb32012-02-24 01:59:29 +000046 lldb::break_id_t bp_loc_id = ++m_next_id;
Greg Claytoneb023e72013-10-11 19:48:25 +000047 BreakpointLocationSP bp_loc_sp (new BreakpointLocation (bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID, m_owner.IsHardware()));
Greg Claytonc0d34462011-02-05 00:38:04 +000048 m_locations.push_back (bp_loc_sp);
49 m_address_to_location[addr] = bp_loc_sp;
50 return bp_loc_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
53bool
Greg Claytonc982c762010-07-09 20:39:50 +000054BreakpointLocationList::ShouldStop (StoppointCallbackContext *context, lldb::break_id_t break_id)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055{
56 BreakpointLocationSP bp = FindByID (break_id);
57 if (bp)
58 {
59 // Let the BreakpointLocation decide if it should stop here (could not have
60 // reached it's target hit count yet, or it could have a callback
61 // that decided it shouldn't stop (shared library loads/unloads).
62 return bp->ShouldStop (context);
63 }
64 // We should stop here since this BreakpointLocation isn't valid anymore or it
65 // doesn't exist.
66 return true;
67}
68
Greg Claytonc982c762010-07-09 20:39:50 +000069lldb::break_id_t
Greg Claytonc0d34462011-02-05 00:38:04 +000070BreakpointLocationList::FindIDByAddress (const Address &addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071{
72 BreakpointLocationSP bp_loc_sp = FindByAddress (addr);
73 if (bp_loc_sp)
74 {
75 return bp_loc_sp->GetID();
76 }
77 return LLDB_INVALID_BREAK_ID;
78}
79
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080BreakpointLocationSP
Greg Claytonc982c762010-07-09 20:39:50 +000081BreakpointLocationList::FindByID (lldb::break_id_t break_id) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082{
Greg Claytonc0d34462011-02-05 00:38:04 +000083 BreakpointLocationSP bp_loc_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 Mutex::Locker locker (m_mutex);
Greg Claytonc0d34462011-02-05 00:38:04 +000085 // We never remove a breakpoint locations, so the ID can be translated into
86 // the location index by subtracting 1
87 uint32_t idx = break_id - 1;
88 if (idx <= m_locations.size())
89 {
90 bp_loc_sp = m_locations[idx];
91 }
92 return bp_loc_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093}
94
95size_t
96BreakpointLocationList::FindInModule (Module *module,
97 BreakpointLocationCollection& bp_loc_list)
98{
99 Mutex::Locker locker (m_mutex);
100 const size_t orig_size = bp_loc_list.GetSize();
101 collection::iterator pos, end = m_locations.end();
102
103 for (pos = m_locations.begin(); pos != end; ++pos)
104 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 BreakpointLocationSP break_loc = (*pos);
Greg Claytone72dfb32012-02-24 01:59:29 +0000106 SectionSP section_sp (break_loc->GetAddress().GetSection());
107 if (section_sp && section_sp->GetModule().get() == module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 {
Johnny Chen0861be02011-08-11 21:43:13 +0000109 bp_loc_list.Add (break_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 }
111 }
112 return bp_loc_list.GetSize() - orig_size;
113}
114
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115const BreakpointLocationSP
Greg Claytonc0d34462011-02-05 00:38:04 +0000116BreakpointLocationList::FindByAddress (const Address &addr) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117{
118 Mutex::Locker locker (m_mutex);
Greg Claytonc0d34462011-02-05 00:38:04 +0000119 BreakpointLocationSP bp_loc_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 if (!m_locations.empty())
121 {
Michael Sartainc87e7522013-07-16 21:22:53 +0000122 Address so_addr;
123
124 if (addr.IsSectionOffset())
125 {
126 so_addr = addr;
127 }
128 else
129 {
130 // Try and resolve as a load address if possible.
131 m_owner.GetTarget().GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), so_addr);
132 if (!so_addr.IsValid())
133 {
134 // The address didn't resolve, so just set to passed in addr.
135 so_addr = addr;
136 }
137 }
138
139 addr_map::const_iterator pos = m_address_to_location.find (so_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 if (pos != m_address_to_location.end())
Greg Claytonc0d34462011-02-05 00:38:04 +0000141 bp_loc_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 }
143
Greg Claytonc0d34462011-02-05 00:38:04 +0000144 return bp_loc_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145}
146
147void
148BreakpointLocationList::Dump (Stream *s) const
149{
Jason Molendafd54b362011-09-20 21:44:10 +0000150 s->Printf("%p: ", this);
Greg Claytonded470d2011-03-19 01:12:21 +0000151 //s->Indent();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 Mutex::Locker locker (m_mutex);
Daniel Malead01b2952012-11-29 21:49:15 +0000153 s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n", (uint64_t)m_locations.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 s->IndentMore();
155 collection::const_iterator pos, end = m_locations.end();
156 for (pos = m_locations.begin(); pos != end; ++pos)
157 (*pos).get()->Dump(s);
158 s->IndentLess();
159}
160
161
162BreakpointLocationSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000163BreakpointLocationList::GetByIndex (size_t i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164{
165 Mutex::Locker locker (m_mutex);
Greg Claytonc0d34462011-02-05 00:38:04 +0000166 BreakpointLocationSP bp_loc_sp;
Greg Claytonc982c762010-07-09 20:39:50 +0000167 if (i < m_locations.size())
Greg Claytonc0d34462011-02-05 00:38:04 +0000168 bp_loc_sp = m_locations[i];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169
Greg Claytonc0d34462011-02-05 00:38:04 +0000170 return bp_loc_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171}
172
173const BreakpointLocationSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000174BreakpointLocationList::GetByIndex (size_t i) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175{
176 Mutex::Locker locker (m_mutex);
Greg Claytonc0d34462011-02-05 00:38:04 +0000177 BreakpointLocationSP bp_loc_sp;
Greg Claytonc982c762010-07-09 20:39:50 +0000178 if (i < m_locations.size())
Greg Claytonc0d34462011-02-05 00:38:04 +0000179 bp_loc_sp = m_locations[i];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180
Greg Claytonc0d34462011-02-05 00:38:04 +0000181 return bp_loc_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182}
183
184void
185BreakpointLocationList::ClearAllBreakpointSites ()
186{
187 Mutex::Locker locker (m_mutex);
188 collection::iterator pos, end = m_locations.end();
189 for (pos = m_locations.begin(); pos != end; ++pos)
190 (*pos)->ClearBreakpointSite();
191}
192
193void
194BreakpointLocationList::ResolveAllBreakpointSites ()
195{
196 Mutex::Locker locker (m_mutex);
197 collection::iterator pos, end = m_locations.end();
198
199 for (pos = m_locations.begin(); pos != end; ++pos)
Jim Inghamd4ce0a12010-10-20 03:36:33 +0000200 {
201 if ((*pos)->IsEnabled())
202 (*pos)->ResolveBreakpointSite();
203 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204}
205
Greg Clayton9fed0d82010-07-23 23:33:17 +0000206uint32_t
207BreakpointLocationList::GetHitCount () const
208{
209 uint32_t hit_count = 0;
210 Mutex::Locker locker (m_mutex);
211 collection::const_iterator pos, end = m_locations.end();
212 for (pos = m_locations.begin(); pos != end; ++pos)
213 hit_count += (*pos)->GetHitCount();
214 return hit_count;
215}
216
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217size_t
218BreakpointLocationList::GetNumResolvedLocations() const
219{
220 Mutex::Locker locker (m_mutex);
221 size_t resolve_count = 0;
222 collection::const_iterator pos, end = m_locations.end();
223 for (pos = m_locations.begin(); pos != end; ++pos)
224 {
225 if ((*pos)->IsResolved())
226 ++resolve_count;
227 }
228 return resolve_count;
229}
230
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231void
232BreakpointLocationList::GetDescription (Stream *s, lldb::DescriptionLevel level)
233{
234 Mutex::Locker locker (m_mutex);
235 collection::iterator pos, end = m_locations.end();
236
237 for (pos = m_locations.begin(); pos != end; ++pos)
238 {
239 s->Printf(" ");
240 (*pos)->GetDescription(s, level);
241 }
242}
243
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000244BreakpointLocationSP
245BreakpointLocationList::AddLocation (const Address &addr, bool *new_location)
246{
247 Mutex::Locker locker (m_mutex);
248
249 if (new_location)
250 *new_location = false;
251 BreakpointLocationSP bp_loc_sp (FindByAddress(addr));
252 if (!bp_loc_sp)
253 {
254 bp_loc_sp = Create (addr);
255 if (bp_loc_sp)
256 {
257 bp_loc_sp->ResolveBreakpointSite();
258
259 if (new_location)
260 *new_location = true;
261 if(m_new_location_recorder)
262 {
263 m_new_location_recorder->Add(bp_loc_sp);
264 }
265 }
266 }
267 return bp_loc_sp;
268}
269
Greg Claytone72dfb32012-02-24 01:59:29 +0000270bool
271BreakpointLocationList::RemoveLocation (const lldb::BreakpointLocationSP &bp_loc_sp)
272{
273 if (bp_loc_sp)
274 {
275 Mutex::Locker locker (m_mutex);
276
277 m_address_to_location.erase (bp_loc_sp->GetAddress());
278
279 collection::iterator pos, end = m_locations.end();
280 for (pos = m_locations.begin(); pos != end; ++pos)
281 {
282 if ((*pos).get() == bp_loc_sp.get())
283 {
284 m_locations.erase (pos);
285 return true;
286 }
287 }
288 }
289 return false;
290}
291
Greg Claytonb35db632013-11-09 00:03:31 +0000292void
293BreakpointLocationList::RemoveInvalidLocations (const ArchSpec &arch)
294{
295 Mutex::Locker locker (m_mutex);
296 size_t idx = 0;
297 // Don't cache m_location.size() as it will change since we might
298 // remove locations from our vector...
299 while (idx < m_locations.size())
300 {
301 BreakpointLocation *bp_loc = m_locations[idx].get();
302 if (bp_loc->GetAddress().SectionWasDeleted())
303 {
304 // Section was deleted which means this breakpoint comes from a module
305 // that is no longer valid, so we should remove it.
306 m_locations.erase(m_locations.begin() + idx);
307 continue;
308 }
309 if (arch.IsValid())
310 {
311 ModuleSP module_sp (bp_loc->GetAddress().GetModule());
312 if (module_sp)
313 {
314 if (!arch.IsCompatibleMatch(module_sp->GetArchitecture()))
315 {
316 // The breakpoint was in a module whose architecture is no longer
317 // compatible with "arch", so we need to remove it
318 m_locations.erase(m_locations.begin() + idx);
319 continue;
320 }
321 }
322 }
323 // Only increment the index if we didn't remove the locations at index "idx"
324 ++idx;
325 }
326}
Jim Inghame6bc6cb2012-02-08 05:23:15 +0000327
328void
329BreakpointLocationList::StartRecordingNewLocations (BreakpointLocationCollection &new_locations)
330{
331 Mutex::Locker locker (m_mutex);
332 assert (m_new_location_recorder == NULL);
333 m_new_location_recorder = &new_locations;
334}
335
336void
337BreakpointLocationList::StopRecordingNewLocations ()
338{
339 Mutex::Locker locker (m_mutex);
340 m_new_location_recorder = NULL;
341}
342