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