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"
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Breakpoint/Breakpoint.h"
18 #include "lldb/Core/Section.h"
19 #include "lldb/Target/Target.h"
22 using namespace lldb_private;
24 BreakpointLocationList::BreakpointLocationList(Breakpoint &owner) :
27 m_address_to_location (),
28 m_mutex (Mutex::eMutexTypeRecursive),
30 m_new_location_recorder (NULL)
34 BreakpointLocationList::~BreakpointLocationList()
39 BreakpointLocationList::Create (const Address &addr)
41 Mutex::Locker locker (m_mutex);
42 // The location ID is just the size of the location list + 1
43 lldb::break_id_t bp_loc_id = ++m_next_id;
44 BreakpointLocationSP bp_loc_sp (new BreakpointLocation (bp_loc_id, m_owner, addr));
45 m_locations.push_back (bp_loc_sp);
46 m_address_to_location[addr] = bp_loc_sp;
51 BreakpointLocationList::ShouldStop (StoppointCallbackContext *context, lldb::break_id_t break_id)
53 BreakpointLocationSP bp = FindByID (break_id);
56 // Let the BreakpointLocation decide if it should stop here (could not have
57 // reached it's target hit count yet, or it could have a callback
58 // that decided it shouldn't stop (shared library loads/unloads).
59 return bp->ShouldStop (context);
61 // We should stop here since this BreakpointLocation isn't valid anymore or it
67 BreakpointLocationList::FindIDByAddress (const Address &addr)
69 BreakpointLocationSP bp_loc_sp = FindByAddress (addr);
72 return bp_loc_sp->GetID();
74 return LLDB_INVALID_BREAK_ID;
78 BreakpointLocationList::FindByID (lldb::break_id_t break_id) const
80 BreakpointLocationSP bp_loc_sp;
81 Mutex::Locker locker (m_mutex);
82 // We never remove a breakpoint locations, so the ID can be translated into
83 // the location index by subtracting 1
84 uint32_t idx = break_id - 1;
85 if (idx <= m_locations.size())
87 bp_loc_sp = m_locations[idx];
93 BreakpointLocationList::FindInModule (Module *module,
94 BreakpointLocationCollection& bp_loc_list)
96 Mutex::Locker locker (m_mutex);
97 const size_t orig_size = bp_loc_list.GetSize();
98 collection::iterator pos, end = m_locations.end();
100 for (pos = m_locations.begin(); pos != end; ++pos)
102 BreakpointLocationSP break_loc = (*pos);
103 SectionSP section_sp (break_loc->GetAddress().GetSection());
104 if (section_sp && section_sp->GetModule().get() == module)
106 bp_loc_list.Add (break_loc);
109 return bp_loc_list.GetSize() - orig_size;
112 const BreakpointLocationSP
113 BreakpointLocationList::FindByAddress (const Address &addr) const
115 Mutex::Locker locker (m_mutex);
116 BreakpointLocationSP bp_loc_sp;
117 if (!m_locations.empty())
121 if (addr.IsSectionOffset())
127 // Try and resolve as a load address if possible.
128 m_owner.GetTarget().GetSectionLoadList().ResolveLoadAddress (addr.GetOffset(), so_addr);
129 if (!so_addr.IsValid())
131 // The address didn't resolve, so just set to passed in addr.
136 addr_map::const_iterator pos = m_address_to_location.find (so_addr);
137 if (pos != m_address_to_location.end())
138 bp_loc_sp = pos->second;
145 BreakpointLocationList::Dump (Stream *s) const
147 s->Printf("%p: ", this);
149 Mutex::Locker locker (m_mutex);
150 s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n", (uint64_t)m_locations.size());
152 collection::const_iterator pos, end = m_locations.end();
153 for (pos = m_locations.begin(); pos != end; ++pos)
154 (*pos).get()->Dump(s);
160 BreakpointLocationList::GetByIndex (size_t i)
162 Mutex::Locker locker (m_mutex);
163 BreakpointLocationSP bp_loc_sp;
164 if (i < m_locations.size())
165 bp_loc_sp = m_locations[i];
170 const BreakpointLocationSP
171 BreakpointLocationList::GetByIndex (size_t i) const
173 Mutex::Locker locker (m_mutex);
174 BreakpointLocationSP bp_loc_sp;
175 if (i < m_locations.size())
176 bp_loc_sp = m_locations[i];
182 BreakpointLocationList::ClearAllBreakpointSites ()
184 Mutex::Locker locker (m_mutex);
185 collection::iterator pos, end = m_locations.end();
186 for (pos = m_locations.begin(); pos != end; ++pos)
187 (*pos)->ClearBreakpointSite();
191 BreakpointLocationList::ResolveAllBreakpointSites ()
193 Mutex::Locker locker (m_mutex);
194 collection::iterator pos, end = m_locations.end();
196 for (pos = m_locations.begin(); pos != end; ++pos)
198 if ((*pos)->IsEnabled())
199 (*pos)->ResolveBreakpointSite();
204 BreakpointLocationList::GetHitCount () const
206 uint32_t hit_count = 0;
207 Mutex::Locker locker (m_mutex);
208 collection::const_iterator pos, end = m_locations.end();
209 for (pos = m_locations.begin(); pos != end; ++pos)
210 hit_count += (*pos)->GetHitCount();
215 BreakpointLocationList::GetNumResolvedLocations() const
217 Mutex::Locker locker (m_mutex);
218 size_t resolve_count = 0;
219 collection::const_iterator pos, end = m_locations.end();
220 for (pos = m_locations.begin(); pos != end; ++pos)
222 if ((*pos)->IsResolved())
225 return resolve_count;
229 BreakpointLocationList::GetDescription (Stream *s, lldb::DescriptionLevel level)
231 Mutex::Locker locker (m_mutex);
232 collection::iterator pos, end = m_locations.end();
234 for (pos = m_locations.begin(); pos != end; ++pos)
237 (*pos)->GetDescription(s, level);
242 BreakpointLocationList::AddLocation (const Address &addr, bool *new_location)
244 Mutex::Locker locker (m_mutex);
247 *new_location = false;
248 BreakpointLocationSP bp_loc_sp (FindByAddress(addr));
251 bp_loc_sp = Create (addr);
254 bp_loc_sp->ResolveBreakpointSite();
257 *new_location = true;
258 if(m_new_location_recorder)
260 m_new_location_recorder->Add(bp_loc_sp);
268 BreakpointLocationList::RemoveLocation (const lldb::BreakpointLocationSP &bp_loc_sp)
272 Mutex::Locker locker (m_mutex);
274 m_address_to_location.erase (bp_loc_sp->GetAddress());
276 collection::iterator pos, end = m_locations.end();
277 for (pos = m_locations.begin(); pos != end; ++pos)
279 if ((*pos).get() == bp_loc_sp.get())
281 m_locations.erase (pos);
292 BreakpointLocationList::StartRecordingNewLocations (BreakpointLocationCollection &new_locations)
294 Mutex::Locker locker (m_mutex);
295 assert (m_new_location_recorder == NULL);
296 m_new_location_recorder = &new_locations;
300 BreakpointLocationList::StopRecordingNewLocations ()
302 Mutex::Locker locker (m_mutex);
303 m_new_location_recorder = NULL;