1 //===-- Breakpoint.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 
16 #include "lldb/Core/Address.h"
17 #include "lldb/Breakpoint/Breakpoint.h"
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
20 #include "lldb/Breakpoint/BreakpointResolver.h"
21 #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/ModuleList.h"
25 #include "lldb/Core/SearchFilter.h"
26 #include "lldb/Core/Section.h"
27 #include "lldb/Core/Stream.h"
28 #include "lldb/Core/StreamString.h"
29 #include "lldb/Symbol/CompileUnit.h"
30 #include "lldb/Symbol/Function.h"
31 #include "lldb/Symbol/SymbolContext.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/ThreadSpec.h"
34 #include "lldb/lldb-private-log.h"
35 #include "llvm/Support/Casting.h"
36 
37 using namespace lldb;
38 using namespace lldb_private;
39 using namespace llvm;
40 
41 const ConstString &
42 Breakpoint::GetEventIdentifier ()
43 {
44     static ConstString g_identifier("event-identifier.breakpoint.changed");
45     return g_identifier;
46 }
47 
48 //----------------------------------------------------------------------
49 // Breakpoint constructor
50 //----------------------------------------------------------------------
51 Breakpoint::Breakpoint(Target &target,
52                        SearchFilterSP &filter_sp,
53                        BreakpointResolverSP &resolver_sp,
54                        bool hardware,
55                        bool resolve_indirect_symbols) :
56     m_being_created(true),
57     m_hardware(hardware),
58     m_target (target),
59     m_filter_sp (filter_sp),
60     m_resolver_sp (resolver_sp),
61     m_options (),
62     m_locations (*this),
63     m_resolve_indirect_symbols(resolve_indirect_symbols)
64 {
65     m_being_created = false;
66 }
67 
68 //----------------------------------------------------------------------
69 // Destructor
70 //----------------------------------------------------------------------
71 Breakpoint::~Breakpoint()
72 {
73 }
74 
75 bool
76 Breakpoint::IsInternal () const
77 {
78     return LLDB_BREAK_ID_IS_INTERNAL(m_bid);
79 }
80 
81 
82 
83 Target&
84 Breakpoint::GetTarget ()
85 {
86     return m_target;
87 }
88 
89 const Target&
90 Breakpoint::GetTarget () const
91 {
92     return m_target;
93 }
94 
95 BreakpointLocationSP
96 Breakpoint::AddLocation (const Address &addr, bool *new_location)
97 {
98     return m_locations.AddLocation (addr, m_resolve_indirect_symbols, new_location);
99 }
100 
101 BreakpointLocationSP
102 Breakpoint::FindLocationByAddress (const Address &addr)
103 {
104     return m_locations.FindByAddress(addr);
105 }
106 
107 break_id_t
108 Breakpoint::FindLocationIDByAddress (const Address &addr)
109 {
110     return m_locations.FindIDByAddress(addr);
111 }
112 
113 BreakpointLocationSP
114 Breakpoint::FindLocationByID (break_id_t bp_loc_id)
115 {
116     return m_locations.FindByID(bp_loc_id);
117 }
118 
119 BreakpointLocationSP
120 Breakpoint::GetLocationAtIndex (size_t index)
121 {
122     return m_locations.GetByIndex(index);
123 }
124 
125 void
126 Breakpoint::RemoveInvalidLocations (const ArchSpec &arch)
127 {
128     m_locations.RemoveInvalidLocations(arch);
129 }
130 
131 // For each of the overall options we need to decide how they propagate to
132 // the location options.  This will determine the precedence of options on
133 // the breakpoint vs. its locations.
134 
135 // Disable at the breakpoint level should override the location settings.
136 // That way you can conveniently turn off a whole breakpoint without messing
137 // up the individual settings.
138 
139 void
140 Breakpoint::SetEnabled (bool enable)
141 {
142     if (enable == m_options.IsEnabled())
143         return;
144 
145     m_options.SetEnabled(enable);
146     if (enable)
147         m_locations.ResolveAllBreakpointSites();
148     else
149         m_locations.ClearAllBreakpointSites();
150 
151     SendBreakpointChangedEvent (enable ? eBreakpointEventTypeEnabled : eBreakpointEventTypeDisabled);
152 
153 }
154 
155 bool
156 Breakpoint::IsEnabled ()
157 {
158     return m_options.IsEnabled();
159 }
160 
161 void
162 Breakpoint::SetIgnoreCount (uint32_t n)
163 {
164     if (m_options.GetIgnoreCount() == n)
165         return;
166 
167     m_options.SetIgnoreCount(n);
168     SendBreakpointChangedEvent (eBreakpointEventTypeIgnoreChanged);
169 }
170 
171 void
172 Breakpoint::DecrementIgnoreCount ()
173 {
174     uint32_t ignore = m_options.GetIgnoreCount();
175     if (ignore != 0)
176         m_options.SetIgnoreCount(ignore - 1);
177 }
178 
179 uint32_t
180 Breakpoint::GetIgnoreCount () const
181 {
182     return m_options.GetIgnoreCount();
183 }
184 
185 bool
186 Breakpoint::IgnoreCountShouldStop ()
187 {
188     uint32_t ignore = GetIgnoreCount();
189     if (ignore != 0)
190     {
191         // When we get here we know the location that caused the stop doesn't have an ignore count,
192         // since by contract we call it first...  So we don't have to find & decrement it, we only have
193         // to decrement our own ignore count.
194         DecrementIgnoreCount();
195         return false;
196     }
197     else
198         return true;
199 }
200 
201 uint32_t
202 Breakpoint::GetHitCount () const
203 {
204     return m_locations.GetHitCount();
205 }
206 
207 bool
208 Breakpoint::IsOneShot () const
209 {
210     return m_options.IsOneShot();
211 }
212 
213 void
214 Breakpoint::SetOneShot (bool one_shot)
215 {
216     m_options.SetOneShot (one_shot);
217 }
218 
219 void
220 Breakpoint::SetThreadID (lldb::tid_t thread_id)
221 {
222     if (m_options.GetThreadSpec()->GetTID() == thread_id)
223         return;
224 
225     m_options.GetThreadSpec()->SetTID(thread_id);
226     SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
227 }
228 
229 lldb::tid_t
230 Breakpoint::GetThreadID () const
231 {
232     if (m_options.GetThreadSpecNoCreate() == NULL)
233         return LLDB_INVALID_THREAD_ID;
234     else
235         return m_options.GetThreadSpecNoCreate()->GetTID();
236 }
237 
238 void
239 Breakpoint::SetThreadIndex (uint32_t index)
240 {
241     if (m_options.GetThreadSpec()->GetIndex() == index)
242         return;
243 
244     m_options.GetThreadSpec()->SetIndex(index);
245     SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
246 }
247 
248 uint32_t
249 Breakpoint::GetThreadIndex() const
250 {
251     if (m_options.GetThreadSpecNoCreate() == NULL)
252         return 0;
253     else
254         return m_options.GetThreadSpecNoCreate()->GetIndex();
255 }
256 
257 void
258 Breakpoint::SetThreadName (const char *thread_name)
259 {
260     if (m_options.GetThreadSpec()->GetName() != NULL
261         && ::strcmp (m_options.GetThreadSpec()->GetName(), thread_name) == 0)
262         return;
263 
264     m_options.GetThreadSpec()->SetName (thread_name);
265     SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
266 }
267 
268 const char *
269 Breakpoint::GetThreadName () const
270 {
271     if (m_options.GetThreadSpecNoCreate() == NULL)
272         return NULL;
273     else
274         return m_options.GetThreadSpecNoCreate()->GetName();
275 }
276 
277 void
278 Breakpoint::SetQueueName (const char *queue_name)
279 {
280     if (m_options.GetThreadSpec()->GetQueueName() != NULL
281         && ::strcmp (m_options.GetThreadSpec()->GetQueueName(), queue_name) == 0)
282         return;
283 
284     m_options.GetThreadSpec()->SetQueueName (queue_name);
285     SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
286 }
287 
288 const char *
289 Breakpoint::GetQueueName () const
290 {
291     if (m_options.GetThreadSpecNoCreate() == NULL)
292         return NULL;
293     else
294         return m_options.GetThreadSpecNoCreate()->GetQueueName();
295 }
296 
297 void
298 Breakpoint::SetCondition (const char *condition)
299 {
300     m_options.SetCondition (condition);
301     SendBreakpointChangedEvent (eBreakpointEventTypeConditionChanged);
302 }
303 
304 const char *
305 Breakpoint::GetConditionText () const
306 {
307     return m_options.GetConditionText();
308 }
309 
310 // This function is used when "baton" doesn't need to be freed
311 void
312 Breakpoint::SetCallback (BreakpointHitCallback callback, void *baton, bool is_synchronous)
313 {
314     // The default "Baton" class will keep a copy of "baton" and won't free
315     // or delete it when it goes goes out of scope.
316     m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
317 
318     SendBreakpointChangedEvent (eBreakpointEventTypeCommandChanged);
319 }
320 
321 // This function is used when a baton needs to be freed and therefore is
322 // contained in a "Baton" subclass.
323 void
324 Breakpoint::SetCallback (BreakpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
325 {
326     m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
327 }
328 
329 void
330 Breakpoint::ClearCallback ()
331 {
332     m_options.ClearCallback ();
333 }
334 
335 bool
336 Breakpoint::InvokeCallback (StoppointCallbackContext *context, break_id_t bp_loc_id)
337 {
338     return m_options.InvokeCallback (context, GetID(), bp_loc_id);
339 }
340 
341 BreakpointOptions *
342 Breakpoint::GetOptions ()
343 {
344     return &m_options;
345 }
346 
347 void
348 Breakpoint::ResolveBreakpoint ()
349 {
350     if (m_resolver_sp)
351         m_resolver_sp->ResolveBreakpoint(*m_filter_sp);
352 }
353 
354 void
355 Breakpoint::ResolveBreakpointInModules (ModuleList &module_list, BreakpointLocationCollection &new_locations)
356 {
357     m_locations.StartRecordingNewLocations(new_locations);
358 
359     m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
360 
361     m_locations.StopRecordingNewLocations();
362 }
363 
364 void
365 Breakpoint::ResolveBreakpointInModules (ModuleList &module_list, bool send_event)
366 {
367     if (m_resolver_sp)
368     {
369         // If this is not an internal breakpoint, set up to record the new locations, then dispatch
370         // an event with the new locations.
371         if (!IsInternal() && send_event)
372         {
373             BreakpointEventData *new_locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsAdded,
374                                                                                 shared_from_this());
375 
376             ResolveBreakpointInModules (module_list, new_locations_event->GetBreakpointLocationCollection());
377 
378             if (new_locations_event->GetBreakpointLocationCollection().GetSize() != 0)
379             {
380                 SendBreakpointChangedEvent (new_locations_event);
381             }
382             else
383                 delete new_locations_event;
384         }
385         else
386         {
387             m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
388         }
389     }
390 }
391 
392 void
393 Breakpoint::ClearAllBreakpointSites ()
394 {
395     m_locations.ClearAllBreakpointSites();
396 }
397 
398 //----------------------------------------------------------------------
399 // ModulesChanged: Pass in a list of new modules, and
400 //----------------------------------------------------------------------
401 
402 void
403 Breakpoint::ModulesChanged (ModuleList &module_list, bool load, bool delete_locations)
404 {
405     Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
406     if (log)
407         log->Printf ("Breakpoint::ModulesChanged: num_modules: %zu load: %i delete_locations: %i\n",
408                      module_list.GetSize(), load, delete_locations);
409 
410     Mutex::Locker modules_mutex(module_list.GetMutex());
411     if (load)
412     {
413         // The logic for handling new modules is:
414         // 1) If the filter rejects this module, then skip it.
415         // 2) Run through the current location list and if there are any locations
416         //    for that module, we mark the module as "seen" and we don't try to re-resolve
417         //    breakpoint locations for that module.
418         //    However, we do add breakpoint sites to these locations if needed.
419         // 3) If we don't see this module in our breakpoint location list, call ResolveInModules.
420 
421         ModuleList new_modules;  // We'll stuff the "unseen" modules in this list, and then resolve
422                                  // them after the locations pass.  Have to do it this way because
423                                  // resolving breakpoints will add new locations potentially.
424 
425         for (ModuleSP module_sp : module_list.ModulesNoLocking())
426         {
427             bool seen = false;
428             if (!m_filter_sp->ModulePasses (module_sp))
429                 continue;
430 
431             for (BreakpointLocationSP break_loc_sp : m_locations.BreakpointLocations())
432             {
433                 if (!break_loc_sp->IsEnabled())
434                     continue;
435                 SectionSP section_sp (break_loc_sp->GetAddress().GetSection());
436                 if (!section_sp || section_sp->GetModule() == module_sp)
437                 {
438                     if (!seen)
439                         seen = true;
440 
441                     if (!break_loc_sp->ResolveBreakpointSite())
442                     {
443                         if (log)
444                             log->Printf ("Warning: could not set breakpoint site for breakpoint location %d of breakpoint %d.\n",
445                                          break_loc_sp->GetID(), GetID());
446                     }
447                 }
448             }
449 
450             if (!seen)
451                 new_modules.AppendIfNeeded (module_sp);
452 
453         }
454 
455         if (new_modules.GetSize() > 0)
456         {
457             ResolveBreakpointInModules(new_modules);
458         }
459     }
460     else
461     {
462         // Go through the currently set locations and if any have breakpoints in
463         // the module list, then remove their breakpoint sites, and their locations if asked to.
464 
465         BreakpointEventData *removed_locations_event;
466         if (!IsInternal())
467             removed_locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsRemoved,
468                                                                shared_from_this());
469         else
470             removed_locations_event = NULL;
471 
472         size_t num_modules = module_list.GetSize();
473         for (size_t i = 0; i < num_modules; i++)
474         {
475             ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (i));
476             if (m_filter_sp->ModulePasses (module_sp))
477             {
478                 size_t loc_idx = 0;
479                 size_t num_locations = m_locations.GetSize();
480                 BreakpointLocationCollection locations_to_remove;
481                 for (loc_idx = 0; loc_idx < num_locations; loc_idx++)
482                 {
483                     BreakpointLocationSP break_loc_sp (m_locations.GetByIndex(loc_idx));
484                     SectionSP section_sp (break_loc_sp->GetAddress().GetSection());
485                     if (section_sp && section_sp->GetModule() == module_sp)
486                     {
487                         // Remove this breakpoint since the shared library is
488                         // unloaded, but keep the breakpoint location around
489                         // so we always get complete hit count and breakpoint
490                         // lifetime info
491                         break_loc_sp->ClearBreakpointSite();
492                         if (removed_locations_event)
493                         {
494                             removed_locations_event->GetBreakpointLocationCollection().Add(break_loc_sp);
495                         }
496                         if (delete_locations)
497                             locations_to_remove.Add (break_loc_sp);
498 
499                     }
500                 }
501 
502                 if (delete_locations)
503                 {
504                     size_t num_locations_to_remove = locations_to_remove.GetSize();
505                     for (loc_idx = 0; loc_idx < num_locations_to_remove; loc_idx++)
506                         m_locations.RemoveLocation  (locations_to_remove.GetByIndex(loc_idx));
507                 }
508             }
509         }
510         SendBreakpointChangedEvent (removed_locations_event);
511     }
512 }
513 
514 namespace
515 {
516 static bool
517 SymbolContextsMightBeEquivalent(SymbolContext &old_sc, SymbolContext &new_sc)
518 {
519     bool equivalent_scs = false;
520 
521     if (old_sc.module_sp.get() == new_sc.module_sp.get())
522     {
523         // If these come from the same module, we can directly compare the pointers:
524         if (old_sc.comp_unit && new_sc.comp_unit
525             && (old_sc.comp_unit == new_sc.comp_unit))
526         {
527             if (old_sc.function && new_sc.function
528                 && (old_sc.function == new_sc.function))
529             {
530                 equivalent_scs = true;
531             }
532         }
533         else if (old_sc.symbol && new_sc.symbol
534                 && (old_sc.symbol == new_sc.symbol))
535         {
536             equivalent_scs = true;
537         }
538     }
539     else
540     {
541         // Otherwise we will compare by name...
542         if (old_sc.comp_unit && new_sc.comp_unit)
543         {
544             if (FileSpec::Equal(*old_sc.comp_unit, *new_sc.comp_unit, true))
545             {
546                 // Now check the functions:
547                 if (old_sc.function && new_sc.function
548                     && (old_sc.function->GetName() == new_sc.function->GetName()))
549                 {
550                     equivalent_scs = true;
551                 }
552             }
553         }
554         else if (old_sc.symbol && new_sc.symbol)
555         {
556             if (Mangled::Compare(old_sc.symbol->GetMangled(), new_sc.symbol->GetMangled()) == 0)
557             {
558                 equivalent_scs = true;
559             }
560         }
561     }
562     return equivalent_scs;
563 }
564 }
565 
566 void
567 Breakpoint::ModuleReplaced (ModuleSP old_module_sp, ModuleSP new_module_sp)
568 {
569     Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
570     if (log)
571         log->Printf ("Breakpoint::ModulesReplaced for %s\n",
572                      old_module_sp->GetSpecificationDescription().c_str());
573     // First find all the locations that are in the old module
574 
575     BreakpointLocationCollection old_break_locs;
576     for (BreakpointLocationSP break_loc_sp : m_locations.BreakpointLocations())
577     {
578         SectionSP section_sp = break_loc_sp->GetAddress().GetSection();
579         if (section_sp && section_sp->GetModule() == old_module_sp)
580         {
581             old_break_locs.Add(break_loc_sp);
582         }
583     }
584 
585     size_t num_old_locations = old_break_locs.GetSize();
586 
587     if (num_old_locations == 0)
588     {
589         // There were no locations in the old module, so we just need to check if there were any in the new module.
590         ModuleList temp_list;
591         temp_list.Append (new_module_sp);
592         ResolveBreakpointInModules(temp_list);
593     }
594     else
595     {
596         // First search the new module for locations.
597         // Then compare this with the old list, copy over locations that "look the same"
598         // Then delete the old locations.
599         // Finally remember to post the creation event.
600         //
601         // Two locations are the same if they have the same comp unit & function (by name) and there are the same number
602         // of locations in the old function as in the new one.
603 
604         ModuleList temp_list;
605         temp_list.Append (new_module_sp);
606         BreakpointLocationCollection new_break_locs;
607         ResolveBreakpointInModules(temp_list, new_break_locs);
608         BreakpointLocationCollection locations_to_remove;
609         BreakpointLocationCollection locations_to_announce;
610 
611         size_t num_new_locations = new_break_locs.GetSize();
612 
613         if (num_new_locations > 0)
614         {
615             // Break out the case of one location -> one location since that's the most common one, and there's no need
616             // to build up the structures needed for the merge in that case.
617             if (num_new_locations == 1 && num_old_locations == 1)
618             {
619                 bool equivalent_locations = false;
620                 SymbolContext old_sc, new_sc;
621                 // The only way the old and new location can be equivalent is if they have the same amount of information:
622                 BreakpointLocationSP old_loc_sp = old_break_locs.GetByIndex(0);
623                 BreakpointLocationSP new_loc_sp = new_break_locs.GetByIndex(0);
624 
625                 if (old_loc_sp->GetAddress().CalculateSymbolContext(&old_sc)
626                     == new_loc_sp->GetAddress().CalculateSymbolContext(&new_sc))
627                 {
628                     equivalent_locations = SymbolContextsMightBeEquivalent(old_sc, new_sc);
629                 }
630 
631                 if (equivalent_locations)
632                 {
633                     m_locations.SwapLocation (old_loc_sp, new_loc_sp);
634                 }
635                 else
636                 {
637                     locations_to_remove.Add(old_loc_sp);
638                     locations_to_announce.Add(new_loc_sp);
639                 }
640             }
641             else
642             {
643                 //We don't want to have to keep computing the SymbolContexts for these addresses over and over,
644                 // so lets get them up front:
645 
646                 typedef std::map<lldb::break_id_t, SymbolContext> IDToSCMap;
647                 IDToSCMap old_sc_map;
648                 for (size_t idx = 0; idx < num_old_locations; idx++)
649                 {
650                     SymbolContext sc;
651                     BreakpointLocationSP bp_loc_sp = old_break_locs.GetByIndex(idx);
652                     lldb::break_id_t loc_id = bp_loc_sp->GetID();
653                     bp_loc_sp->GetAddress().CalculateSymbolContext(&old_sc_map[loc_id]);
654                 }
655 
656                 std::map<lldb::break_id_t, SymbolContext> new_sc_map;
657                 for (size_t idx = 0; idx < num_new_locations; idx++)
658                 {
659                     SymbolContext sc;
660                     BreakpointLocationSP bp_loc_sp = new_break_locs.GetByIndex(idx);
661                     lldb::break_id_t loc_id = bp_loc_sp->GetID();
662                     bp_loc_sp->GetAddress().CalculateSymbolContext(&new_sc_map[loc_id]);
663                 }
664                 // Take an element from the old Symbol Contexts
665                 while (old_sc_map.size() > 0)
666                 {
667                     lldb::break_id_t old_id = old_sc_map.begin()->first;
668                     SymbolContext &old_sc = old_sc_map.begin()->second;
669 
670                     // Count the number of entries equivalent to this SC for the old list:
671                     std::vector<lldb::break_id_t> old_id_vec;
672                     old_id_vec.push_back(old_id);
673 
674                     IDToSCMap::iterator tmp_iter;
675                     for (tmp_iter = ++old_sc_map.begin(); tmp_iter != old_sc_map.end(); tmp_iter++)
676                     {
677                         if (SymbolContextsMightBeEquivalent (old_sc, tmp_iter->second))
678                             old_id_vec.push_back (tmp_iter->first);
679                     }
680 
681                     // Now find all the equivalent locations in the new list.
682                     std::vector<lldb::break_id_t> new_id_vec;
683                     for (tmp_iter = new_sc_map.begin(); tmp_iter != new_sc_map.end(); tmp_iter++)
684                     {
685                         if (SymbolContextsMightBeEquivalent (old_sc, tmp_iter->second))
686                             new_id_vec.push_back(tmp_iter->first);
687                     }
688 
689                     // Alright, if we have the same number of potentially equivalent locations in the old
690                     // and new modules, we'll just map them one to one in ascending ID order (assuming the
691                     // resolver's order would match the equivalent ones.
692                     // Otherwise, we'll dump all the old ones, and just take the new ones, erasing the elements
693                     // from both maps as we go.
694 
695                     if (old_id_vec.size() == new_id_vec.size())
696                     {
697                         sort(old_id_vec.begin(), old_id_vec.end());
698                         sort(new_id_vec.begin(), new_id_vec.end());
699                         size_t num_elements = old_id_vec.size();
700                         for (size_t idx = 0; idx < num_elements; idx++)
701                         {
702                             BreakpointLocationSP old_loc_sp = old_break_locs.FindByIDPair(GetID(), old_id_vec[idx]);
703                             BreakpointLocationSP new_loc_sp = new_break_locs.FindByIDPair(GetID(), new_id_vec[idx]);
704                             m_locations.SwapLocation(old_loc_sp, new_loc_sp);
705                             old_sc_map.erase(old_id_vec[idx]);
706                             new_sc_map.erase(new_id_vec[idx]);
707                         }
708                     }
709                     else
710                     {
711                         for (lldb::break_id_t old_id : old_id_vec)
712                         {
713                             locations_to_remove.Add(old_break_locs.FindByIDPair(GetID(), old_id));
714                             old_sc_map.erase(old_id);
715                         }
716                         for (lldb::break_id_t new_id : new_id_vec)
717                         {
718                             locations_to_announce.Add(new_break_locs.FindByIDPair(GetID(), new_id));
719                             new_sc_map.erase(new_id);
720                         }
721                     }
722                 }
723             }
724         }
725 
726         // Now remove the remaining old locations, and cons up a removed locations event.
727         // Note, we don't put the new locations that were swapped with an old location on the locations_to_remove
728         // list, so we don't need to worry about telling the world about removing a location we didn't tell them
729         // about adding.
730 
731         BreakpointEventData *locations_event;
732         if (!IsInternal())
733             locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsRemoved,
734                                                                shared_from_this());
735         else
736             locations_event = NULL;
737 
738         for (BreakpointLocationSP loc_sp : locations_to_remove.BreakpointLocations())
739         {
740             m_locations.RemoveLocation(loc_sp);
741             if (locations_event)
742                 locations_event->GetBreakpointLocationCollection().Add(loc_sp);
743         }
744         SendBreakpointChangedEvent (locations_event);
745 
746         // And announce the new ones.
747 
748         if (!IsInternal())
749         {
750             locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsAdded,
751                                                                shared_from_this());
752             for (BreakpointLocationSP loc_sp : locations_to_announce.BreakpointLocations())
753                     locations_event->GetBreakpointLocationCollection().Add(loc_sp);
754 
755             SendBreakpointChangedEvent (locations_event);
756         }
757         m_locations.Compact();
758     }
759 }
760 
761 void
762 Breakpoint::Dump (Stream *)
763 {
764 }
765 
766 size_t
767 Breakpoint::GetNumResolvedLocations() const
768 {
769     // Return the number of breakpoints that are actually resolved and set
770     // down in the inferior process.
771     return m_locations.GetNumResolvedLocations();
772 }
773 
774 size_t
775 Breakpoint::GetNumLocations() const
776 {
777     return m_locations.GetSize();
778 }
779 
780 void
781 Breakpoint::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_locations)
782 {
783     assert (s != NULL);
784 
785     if (!m_kind_description.empty())
786     {
787         if (level == eDescriptionLevelBrief)
788         {
789             s->PutCString (GetBreakpointKind());
790             return;
791         }
792         else
793             s->Printf("Kind: %s\n", GetBreakpointKind ());
794     }
795 
796     const size_t num_locations = GetNumLocations ();
797     const size_t num_resolved_locations = GetNumResolvedLocations ();
798 
799     // They just made the breakpoint, they don't need to be told HOW they made it...
800     // Also, we'll print the breakpoint number differently depending on whether there is 1 or more locations.
801     if (level != eDescriptionLevelInitial)
802     {
803         s->Printf("%i: ", GetID());
804         GetResolverDescription (s);
805         GetFilterDescription (s);
806     }
807 
808     switch (level)
809     {
810     case lldb::eDescriptionLevelBrief:
811     case lldb::eDescriptionLevelFull:
812         if (num_locations > 0)
813         {
814             s->Printf(", locations = %" PRIu64, (uint64_t)num_locations);
815             if (num_resolved_locations > 0)
816                 s->Printf(", resolved = %" PRIu64 ", hit count = %d", (uint64_t)num_resolved_locations, GetHitCount());
817         }
818         else
819         {
820             // Don't print the pending notification for exception resolvers since we don't generally
821             // know how to set them until the target is run.
822             if (m_resolver_sp->getResolverID() != BreakpointResolver::ExceptionResolver)
823                 s->Printf(", locations = 0 (pending)");
824         }
825 
826         GetOptions()->GetDescription(s, level);
827 
828         if (level == lldb::eDescriptionLevelFull)
829         {
830             s->IndentLess();
831             s->EOL();
832         }
833         break;
834 
835     case lldb::eDescriptionLevelInitial:
836         s->Printf ("Breakpoint %i: ", GetID());
837         if (num_locations == 0)
838         {
839             s->Printf ("no locations (pending).");
840         }
841         else if (num_locations == 1)
842         {
843             // If there is one location only, we'll just print that location information.  But don't do this if
844             // show locations is true, then that will be handled below.
845             if (show_locations == false)
846             {
847                 GetLocationAtIndex(0)->GetDescription(s, level);
848             }
849             else
850             {
851                 s->Printf ("%zd locations.", num_locations);
852             }
853         }
854         else
855         {
856             s->Printf ("%zd locations.", num_locations);
857         }
858         s->EOL();
859         break;
860     case lldb::eDescriptionLevelVerbose:
861         // Verbose mode does a debug dump of the breakpoint
862         Dump (s);
863         s->EOL ();
864             //s->Indent();
865         GetOptions()->GetDescription(s, level);
866         break;
867 
868     default:
869         break;
870     }
871 
872     // The brief description is just the location name (1.2 or whatever).  That's pointless to
873     // show in the breakpoint's description, so suppress it.
874     if (show_locations && level != lldb::eDescriptionLevelBrief)
875     {
876         s->IndentMore();
877         for (size_t i = 0; i < num_locations; ++i)
878         {
879             BreakpointLocation *loc = GetLocationAtIndex(i).get();
880             loc->GetDescription(s, level);
881             s->EOL();
882         }
883         s->IndentLess();
884     }
885 }
886 
887 void
888 Breakpoint::GetResolverDescription (Stream *s)
889 {
890     if (m_resolver_sp)
891         m_resolver_sp->GetDescription (s);
892 }
893 
894 
895 bool
896 Breakpoint::GetMatchingFileLine (const ConstString &filename, uint32_t line_number, BreakpointLocationCollection &loc_coll)
897 {
898     // TODO: To be correct, this method needs to fill the breakpoint location collection
899     //       with the location IDs which match the filename and line_number.
900     //
901 
902     if (m_resolver_sp)
903     {
904         BreakpointResolverFileLine *resolverFileLine = dyn_cast<BreakpointResolverFileLine>(m_resolver_sp.get());
905         if (resolverFileLine &&
906             resolverFileLine->m_file_spec.GetFilename() == filename &&
907             resolverFileLine->m_line_number == line_number)
908         {
909             return true;
910         }
911     }
912     return false;
913 }
914 
915 void
916 Breakpoint::GetFilterDescription (Stream *s)
917 {
918     m_filter_sp->GetDescription (s);
919 }
920 
921 void
922 Breakpoint::SendBreakpointChangedEvent (lldb::BreakpointEventType eventKind)
923 {
924     if (!m_being_created
925         && !IsInternal()
926         && GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
927     {
928         BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind, shared_from_this());
929 
930         GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
931     }
932 }
933 
934 void
935 Breakpoint::SendBreakpointChangedEvent (BreakpointEventData *data)
936 {
937 
938     if (data == NULL)
939         return;
940 
941     if (!m_being_created
942         && !IsInternal()
943         && GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
944         GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
945     else
946         delete data;
947 }
948 
949 Breakpoint::BreakpointEventData::BreakpointEventData (BreakpointEventType sub_type,
950                                                       const BreakpointSP &new_breakpoint_sp) :
951     EventData (),
952     m_breakpoint_event (sub_type),
953     m_new_breakpoint_sp (new_breakpoint_sp)
954 {
955 }
956 
957 Breakpoint::BreakpointEventData::~BreakpointEventData ()
958 {
959 }
960 
961 const ConstString &
962 Breakpoint::BreakpointEventData::GetFlavorString ()
963 {
964     static ConstString g_flavor ("Breakpoint::BreakpointEventData");
965     return g_flavor;
966 }
967 
968 const ConstString &
969 Breakpoint::BreakpointEventData::GetFlavor () const
970 {
971     return BreakpointEventData::GetFlavorString ();
972 }
973 
974 
975 BreakpointSP &
976 Breakpoint::BreakpointEventData::GetBreakpoint ()
977 {
978     return m_new_breakpoint_sp;
979 }
980 
981 BreakpointEventType
982 Breakpoint::BreakpointEventData::GetBreakpointEventType () const
983 {
984     return m_breakpoint_event;
985 }
986 
987 void
988 Breakpoint::BreakpointEventData::Dump (Stream *s) const
989 {
990 }
991 
992 const Breakpoint::BreakpointEventData *
993 Breakpoint::BreakpointEventData::GetEventDataFromEvent (const Event *event)
994 {
995     if (event)
996     {
997         const EventData *event_data = event->GetData();
998         if (event_data && event_data->GetFlavor() == BreakpointEventData::GetFlavorString())
999             return static_cast <const BreakpointEventData *> (event->GetData());
1000     }
1001     return NULL;
1002 }
1003 
1004 BreakpointEventType
1005 Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (const EventSP &event_sp)
1006 {
1007     const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
1008 
1009     if (data == NULL)
1010         return eBreakpointEventTypeInvalidType;
1011     else
1012         return data->GetBreakpointEventType();
1013 }
1014 
1015 BreakpointSP
1016 Breakpoint::BreakpointEventData::GetBreakpointFromEvent (const EventSP &event_sp)
1017 {
1018     BreakpointSP bp_sp;
1019 
1020     const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
1021     if (data)
1022         bp_sp = data->m_new_breakpoint_sp;
1023 
1024     return bp_sp;
1025 }
1026 
1027 size_t
1028 Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent (const EventSP &event_sp)
1029 {
1030     const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
1031     if (data)
1032         return data->m_locations.GetSize();
1033 
1034     return 0;
1035 }
1036 
1037 lldb::BreakpointLocationSP
1038 Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent (const lldb::EventSP &event_sp, uint32_t bp_loc_idx)
1039 {
1040     lldb::BreakpointLocationSP bp_loc_sp;
1041 
1042     const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
1043     if (data)
1044     {
1045         bp_loc_sp = data->m_locations.GetByIndex(bp_loc_idx);
1046     }
1047 
1048     return bp_loc_sp;
1049 }
1050