Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
| 11 | // C Includes |
| 12 | // C++ Includes |
| 13 | // Other libraries and framework includes |
| 14 | // Project includes |
| 15 | #include "lldb/Breakpoint/BreakpointLocationList.h" |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame^] | 16 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Michael Sartain | c87e752 | 2013-07-16 21:22:53 +0000 | [diff] [blame] | 18 | #include "lldb/Breakpoint/Breakpoint.h" |
Greg Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame^] | 19 | #include "lldb/Core/ArchSpec.h" |
| 20 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Section.h" |
Michael Sartain | c87e752 | 2013-07-16 21:22:53 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 27 | BreakpointLocationList::BreakpointLocationList(Breakpoint &owner) : |
Bill Wendling | c9851e2 | 2012-04-03 04:14:58 +0000 | [diff] [blame] | 28 | m_owner (owner), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | m_locations(), |
| 30 | m_address_to_location (), |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 31 | m_mutex (Mutex::eMutexTypeRecursive), |
Bill Wendling | c9851e2 | 2012-04-03 04:14:58 +0000 | [diff] [blame] | 32 | m_next_id (0), |
| 33 | m_new_location_recorder (NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | BreakpointLocationList::~BreakpointLocationList() |
| 38 | { |
| 39 | } |
| 40 | |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 41 | BreakpointLocationSP |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 42 | BreakpointLocationList::Create (const Address &addr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 44 | Mutex::Locker locker (m_mutex); |
| 45 | // The location ID is just the size of the location list + 1 |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 46 | lldb::break_id_t bp_loc_id = ++m_next_id; |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 47 | BreakpointLocationSP bp_loc_sp (new BreakpointLocation (bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID, m_owner.IsHardware())); |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 48 | m_locations.push_back (bp_loc_sp); |
| 49 | m_address_to_location[addr] = bp_loc_sp; |
| 50 | return bp_loc_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 54 | BreakpointLocationList::ShouldStop (StoppointCallbackContext *context, lldb::break_id_t break_id) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | { |
| 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 Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 69 | lldb::break_id_t |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 70 | BreakpointLocationList::FindIDByAddress (const Address &addr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | { |
| 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | BreakpointLocationSP |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 81 | BreakpointLocationList::FindByID (lldb::break_id_t break_id) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | { |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 83 | BreakpointLocationSP bp_loc_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | Mutex::Locker locker (m_mutex); |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 85 | // 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | size_t |
| 96 | BreakpointLocationList::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | BreakpointLocationSP break_loc = (*pos); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 106 | SectionSP section_sp (break_loc->GetAddress().GetSection()); |
| 107 | if (section_sp && section_sp->GetModule().get() == module) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | { |
Johnny Chen | 0861be0 | 2011-08-11 21:43:13 +0000 | [diff] [blame] | 109 | bp_loc_list.Add (break_loc); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | return bp_loc_list.GetSize() - orig_size; |
| 113 | } |
| 114 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | const BreakpointLocationSP |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 116 | BreakpointLocationList::FindByAddress (const Address &addr) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | { |
| 118 | Mutex::Locker locker (m_mutex); |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 119 | BreakpointLocationSP bp_loc_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | if (!m_locations.empty()) |
| 121 | { |
Michael Sartain | c87e752 | 2013-07-16 21:22:53 +0000 | [diff] [blame] | 122 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | if (pos != m_address_to_location.end()) |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 141 | bp_loc_sp = pos->second; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 144 | return bp_loc_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | BreakpointLocationList::Dump (Stream *s) const |
| 149 | { |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 150 | s->Printf("%p: ", this); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 151 | //s->Indent(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | Mutex::Locker locker (m_mutex); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 153 | s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n", (uint64_t)m_locations.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | 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 | |
| 162 | BreakpointLocationSP |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 163 | BreakpointLocationList::GetByIndex (size_t i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | { |
| 165 | Mutex::Locker locker (m_mutex); |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 166 | BreakpointLocationSP bp_loc_sp; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 167 | if (i < m_locations.size()) |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 168 | bp_loc_sp = m_locations[i]; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 170 | return bp_loc_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | const BreakpointLocationSP |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 174 | BreakpointLocationList::GetByIndex (size_t i) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | { |
| 176 | Mutex::Locker locker (m_mutex); |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 177 | BreakpointLocationSP bp_loc_sp; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 178 | if (i < m_locations.size()) |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 179 | bp_loc_sp = m_locations[i]; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | |
Greg Clayton | c0d3446 | 2011-02-05 00:38:04 +0000 | [diff] [blame] | 181 | return bp_loc_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void |
| 185 | BreakpointLocationList::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 | |
| 193 | void |
| 194 | BreakpointLocationList::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 Ingham | d4ce0a1 | 2010-10-20 03:36:33 +0000 | [diff] [blame] | 200 | { |
| 201 | if ((*pos)->IsEnabled()) |
| 202 | (*pos)->ResolveBreakpointSite(); |
| 203 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Greg Clayton | 9fed0d8 | 2010-07-23 23:33:17 +0000 | [diff] [blame] | 206 | uint32_t |
| 207 | BreakpointLocationList::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | size_t |
| 218 | BreakpointLocationList::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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | void |
| 232 | BreakpointLocationList::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 Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 244 | BreakpointLocationSP |
| 245 | BreakpointLocationList::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 Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 270 | bool |
| 271 | BreakpointLocationList::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 Clayton | b35db63 | 2013-11-09 00:03:31 +0000 | [diff] [blame^] | 292 | void |
| 293 | BreakpointLocationList::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 Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 327 | |
| 328 | void |
| 329 | BreakpointLocationList::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 | |
| 336 | void |
| 337 | BreakpointLocationList::StopRecordingNewLocations () |
| 338 | { |
| 339 | Mutex::Locker locker (m_mutex); |
| 340 | m_new_location_recorder = NULL; |
| 341 | } |
| 342 | |