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 //===----------------------------------------------------------------------===//
13 // Other libraries and framework includes
15 #include "lldb/Breakpoint/BreakpointLocationList.h"
17 #include "lldb/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Breakpoint/Breakpoint.h"
19 #include "lldb/Core/ArchSpec.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/Section.h"
22 #include "lldb/Target/SectionLoadList.h"
23 #include "lldb/Target/Target.h"
27 using namespace lldb_private;
29 BreakpointLocationList::BreakpointLocationList(Breakpoint &owner) :
32 m_address_to_location (),
33 m_mutex (Mutex::eMutexTypeRecursive),
35 m_new_location_recorder (NULL)
39 BreakpointLocationList::~BreakpointLocationList()
44 BreakpointLocationList::Create (const Address &addr, bool resolve_indirect_symbols)
46 Mutex::Locker locker (m_mutex);
47 // The location ID is just the size of the location list + 1
48 lldb::break_id_t bp_loc_id = ++m_next_id;
49 BreakpointLocationSP bp_loc_sp (new BreakpointLocation (bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID, m_owner.IsHardware(), resolve_indirect_symbols));
50 m_locations.push_back (bp_loc_sp);
51 m_address_to_location[addr] = bp_loc_sp;
56 BreakpointLocationList::ShouldStop (StoppointCallbackContext *context, lldb::break_id_t break_id)
58 BreakpointLocationSP bp = FindByID (break_id);
61 // Let the BreakpointLocation decide if it should stop here (could not have
62 // reached it's target hit count yet, or it could have a callback
63 // that decided it shouldn't stop (shared library loads/unloads).
64 return bp->ShouldStop (context);
66 // We should stop here since this BreakpointLocation isn't valid anymore or it
72 BreakpointLocationList::FindIDByAddress (const Address &addr)
74 BreakpointLocationSP bp_loc_sp = FindByAddress (addr);
77 return bp_loc_sp->GetID();
79 return LLDB_INVALID_BREAK_ID;
83 Compare (BreakpointLocationSP lhs, lldb::break_id_t val)
85 return lhs->GetID() < val;
89 BreakpointLocationList::FindByID (lldb::break_id_t break_id) const
91 Mutex::Locker locker (m_mutex);
92 collection::const_iterator end = m_locations.end();
93 collection::const_iterator pos = std::lower_bound(m_locations.begin(), end, break_id, Compare);
94 if (pos != end && (*pos)->GetID() == break_id)
97 return BreakpointLocationSP();
101 BreakpointLocationList::FindInModule (Module *module,
102 BreakpointLocationCollection& bp_loc_list)
104 Mutex::Locker locker (m_mutex);
105 const size_t orig_size = bp_loc_list.GetSize();
106 collection::iterator pos, end = m_locations.end();
108 for (pos = m_locations.begin(); pos != end; ++pos)
110 BreakpointLocationSP break_loc = (*pos);
111 SectionSP section_sp (break_loc->GetAddress().GetSection());
112 if (section_sp && section_sp->GetModule().get() == module)
114 bp_loc_list.Add (break_loc);
117 return bp_loc_list.GetSize() - orig_size;
120 const BreakpointLocationSP
121 BreakpointLocationList::FindByAddress (const Address &addr) const
123 Mutex::Locker locker (m_mutex);
124 BreakpointLocationSP bp_loc_sp;
125 if (!m_locations.empty())
129 if (addr.IsSectionOffset())
135 // Try and resolve as a load address if possible.
136 m_owner.GetTarget().GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), so_addr);
137 if (!so_addr.IsValid())
139 // The address didn't resolve, so just set to passed in addr.
144 addr_map::const_iterator pos = m_address_to_location.find (so_addr);
145 if (pos != m_address_to_location.end())
146 bp_loc_sp = pos->second;
153 BreakpointLocationList::Dump (Stream *s) const
155 s->Printf("%p: ", this);
157 Mutex::Locker locker (m_mutex);
158 s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n", (uint64_t)m_locations.size());
160 collection::const_iterator pos, end = m_locations.end();
161 for (pos = m_locations.begin(); pos != end; ++pos)
162 (*pos).get()->Dump(s);
168 BreakpointLocationList::GetByIndex (size_t i)
170 Mutex::Locker locker (m_mutex);
171 BreakpointLocationSP bp_loc_sp;
172 if (i < m_locations.size())
173 bp_loc_sp = m_locations[i];
178 const BreakpointLocationSP
179 BreakpointLocationList::GetByIndex (size_t i) const
181 Mutex::Locker locker (m_mutex);
182 BreakpointLocationSP bp_loc_sp;
183 if (i < m_locations.size())
184 bp_loc_sp = m_locations[i];
190 BreakpointLocationList::ClearAllBreakpointSites ()
192 Mutex::Locker locker (m_mutex);
193 collection::iterator pos, end = m_locations.end();
194 for (pos = m_locations.begin(); pos != end; ++pos)
195 (*pos)->ClearBreakpointSite();
199 BreakpointLocationList::ResolveAllBreakpointSites ()
201 Mutex::Locker locker (m_mutex);
202 collection::iterator pos, end = m_locations.end();
204 for (pos = m_locations.begin(); pos != end; ++pos)
206 if ((*pos)->IsEnabled())
207 (*pos)->ResolveBreakpointSite();
212 BreakpointLocationList::GetHitCount () const
214 uint32_t hit_count = 0;
215 Mutex::Locker locker (m_mutex);
216 collection::const_iterator pos, end = m_locations.end();
217 for (pos = m_locations.begin(); pos != end; ++pos)
218 hit_count += (*pos)->GetHitCount();
223 BreakpointLocationList::GetNumResolvedLocations() const
225 Mutex::Locker locker (m_mutex);
226 size_t resolve_count = 0;
227 collection::const_iterator pos, end = m_locations.end();
228 for (pos = m_locations.begin(); pos != end; ++pos)
230 if ((*pos)->IsResolved())
233 return resolve_count;
237 BreakpointLocationList::GetDescription (Stream *s, lldb::DescriptionLevel level)
239 Mutex::Locker locker (m_mutex);
240 collection::iterator pos, end = m_locations.end();
242 for (pos = m_locations.begin(); pos != end; ++pos)
245 (*pos)->GetDescription(s, level);
250 BreakpointLocationList::AddLocation (const Address &addr, bool resolve_indirect_symbols, bool *new_location)
252 Mutex::Locker locker (m_mutex);
255 *new_location = false;
256 BreakpointLocationSP bp_loc_sp (FindByAddress(addr));
259 bp_loc_sp = Create (addr, resolve_indirect_symbols);
262 bp_loc_sp->ResolveBreakpointSite();
265 *new_location = true;
266 if(m_new_location_recorder)
268 m_new_location_recorder->Add(bp_loc_sp);
276 BreakpointLocationList::RemoveLocation (const lldb::BreakpointLocationSP &bp_loc_sp)
280 Mutex::Locker locker (m_mutex);
282 m_address_to_location.erase (bp_loc_sp->GetAddress());
284 collection::iterator pos, end = m_locations.end();
285 for (pos = m_locations.begin(); pos != end; ++pos)
287 if ((*pos).get() == bp_loc_sp.get())
289 m_locations.erase (pos);
298 BreakpointLocationList::RemoveInvalidLocations (const ArchSpec &arch)
300 Mutex::Locker locker (m_mutex);
302 // Don't cache m_location.size() as it will change since we might
303 // remove locations from our vector...
304 while (idx < m_locations.size())
306 BreakpointLocation *bp_loc = m_locations[idx].get();
307 if (bp_loc->GetAddress().SectionWasDeleted())
309 // Section was deleted which means this breakpoint comes from a module
310 // that is no longer valid, so we should remove it.
311 m_locations.erase(m_locations.begin() + idx);
316 ModuleSP module_sp (bp_loc->GetAddress().GetModule());
319 if (!arch.IsCompatibleMatch(module_sp->GetArchitecture()))
321 // The breakpoint was in a module whose architecture is no longer
322 // compatible with "arch", so we need to remove it
323 m_locations.erase(m_locations.begin() + idx);
328 // Only increment the index if we didn't remove the locations at index "idx"
334 BreakpointLocationList::StartRecordingNewLocations (BreakpointLocationCollection &new_locations)
336 Mutex::Locker locker (m_mutex);
337 assert (m_new_location_recorder == NULL);
338 m_new_location_recorder = &new_locations;
342 BreakpointLocationList::StopRecordingNewLocations ()
344 Mutex::Locker locker (m_mutex);
345 m_new_location_recorder = NULL;