1 //===-- Breakpoint.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 // Other libraries and framework includes
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/ModuleList.h"
24 #include "lldb/Core/SearchFilter.h"
25 #include "lldb/Core/Section.h"
26 #include "lldb/Core/Stream.h"
27 #include "lldb/Core/StreamString.h"
28 #include "lldb/Symbol/SymbolContext.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/ThreadSpec.h"
31 #include "lldb/lldb-private-log.h"
32 #include "llvm/Support/Casting.h"
35 using namespace lldb_private;
39 Breakpoint::GetEventIdentifier ()
41 static ConstString g_identifier("event-identifier.breakpoint.changed");
45 //----------------------------------------------------------------------
46 // Breakpoint constructor
47 //----------------------------------------------------------------------
48 Breakpoint::Breakpoint(Target &target, SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp) :
49 m_being_created(true),
51 m_filter_sp (filter_sp),
52 m_resolver_sp (resolver_sp),
56 m_being_created = false;
59 //----------------------------------------------------------------------
61 //----------------------------------------------------------------------
62 Breakpoint::~Breakpoint()
67 Breakpoint::IsInternal () const
69 return LLDB_BREAK_ID_IS_INTERNAL(m_bid);
75 Breakpoint::GetTarget ()
81 Breakpoint::GetTarget () const
87 Breakpoint::AddLocation (const Address &addr, bool *new_location)
89 return m_locations.AddLocation (addr, new_location);
93 Breakpoint::FindLocationByAddress (const Address &addr)
95 return m_locations.FindByAddress(addr);
99 Breakpoint::FindLocationIDByAddress (const Address &addr)
101 return m_locations.FindIDByAddress(addr);
105 Breakpoint::FindLocationByID (break_id_t bp_loc_id)
107 return m_locations.FindByID(bp_loc_id);
111 Breakpoint::GetLocationAtIndex (size_t index)
113 return m_locations.GetByIndex(index);
116 // For each of the overall options we need to decide how they propagate to
117 // the location options. This will determine the precedence of options on
118 // the breakpoint vs. its locations.
120 // Disable at the breakpoint level should override the location settings.
121 // That way you can conveniently turn off a whole breakpoint without messing
122 // up the individual settings.
125 Breakpoint::SetEnabled (bool enable)
127 if (enable == m_options.IsEnabled())
130 m_options.SetEnabled(enable);
132 m_locations.ResolveAllBreakpointSites();
134 m_locations.ClearAllBreakpointSites();
136 SendBreakpointChangedEvent (enable ? eBreakpointEventTypeEnabled : eBreakpointEventTypeDisabled);
141 Breakpoint::IsEnabled ()
143 return m_options.IsEnabled();
147 Breakpoint::SetIgnoreCount (uint32_t n)
149 if (m_options.GetIgnoreCount() == n)
152 m_options.SetIgnoreCount(n);
153 SendBreakpointChangedEvent (eBreakpointEventTypeIgnoreChanged);
157 Breakpoint::DecrementIgnoreCount ()
159 uint32_t ignore = m_options.GetIgnoreCount();
161 m_options.SetIgnoreCount(ignore - 1);
165 Breakpoint::GetIgnoreCount () const
167 return m_options.GetIgnoreCount();
171 Breakpoint::IgnoreCountShouldStop ()
173 uint32_t ignore = GetIgnoreCount();
176 // When we get here we know the location that caused the stop doesn't have an ignore count,
177 // since by contract we call it first... So we don't have to find & decrement it, we only have
178 // to decrement our own ignore count.
179 DecrementIgnoreCount();
187 Breakpoint::GetHitCount () const
189 return m_locations.GetHitCount();
193 Breakpoint::IsOneShot () const
195 return m_options.IsOneShot();
199 Breakpoint::SetOneShot (bool one_shot)
201 m_options.SetOneShot (one_shot);
205 Breakpoint::SetThreadID (lldb::tid_t thread_id)
207 if (m_options.GetThreadSpec()->GetTID() == thread_id)
210 m_options.GetThreadSpec()->SetTID(thread_id);
211 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
215 Breakpoint::GetThreadID () const
217 if (m_options.GetThreadSpecNoCreate() == NULL)
218 return LLDB_INVALID_THREAD_ID;
220 return m_options.GetThreadSpecNoCreate()->GetTID();
224 Breakpoint::SetThreadIndex (uint32_t index)
226 if (m_options.GetThreadSpec()->GetIndex() == index)
229 m_options.GetThreadSpec()->SetIndex(index);
230 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
234 Breakpoint::GetThreadIndex() const
236 if (m_options.GetThreadSpecNoCreate() == NULL)
239 return m_options.GetThreadSpecNoCreate()->GetIndex();
243 Breakpoint::SetThreadName (const char *thread_name)
245 if (m_options.GetThreadSpec()->GetName() != NULL
246 && ::strcmp (m_options.GetThreadSpec()->GetName(), thread_name) == 0)
249 m_options.GetThreadSpec()->SetName (thread_name);
250 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
254 Breakpoint::GetThreadName () const
256 if (m_options.GetThreadSpecNoCreate() == NULL)
259 return m_options.GetThreadSpecNoCreate()->GetName();
263 Breakpoint::SetQueueName (const char *queue_name)
265 if (m_options.GetThreadSpec()->GetQueueName() != NULL
266 && ::strcmp (m_options.GetThreadSpec()->GetQueueName(), queue_name) == 0)
269 m_options.GetThreadSpec()->SetQueueName (queue_name);
270 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
274 Breakpoint::GetQueueName () const
276 if (m_options.GetThreadSpecNoCreate() == NULL)
279 return m_options.GetThreadSpecNoCreate()->GetQueueName();
283 Breakpoint::SetCondition (const char *condition)
285 m_options.SetCondition (condition);
286 SendBreakpointChangedEvent (eBreakpointEventTypeConditionChanged);
290 Breakpoint::GetConditionText () const
292 return m_options.GetConditionText();
295 // This function is used when "baton" doesn't need to be freed
297 Breakpoint::SetCallback (BreakpointHitCallback callback, void *baton, bool is_synchronous)
299 // The default "Baton" class will keep a copy of "baton" and won't free
300 // or delete it when it goes goes out of scope.
301 m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
303 SendBreakpointChangedEvent (eBreakpointEventTypeCommandChanged);
306 // This function is used when a baton needs to be freed and therefore is
307 // contained in a "Baton" subclass.
309 Breakpoint::SetCallback (BreakpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
311 m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
315 Breakpoint::ClearCallback ()
317 m_options.ClearCallback ();
321 Breakpoint::InvokeCallback (StoppointCallbackContext *context, break_id_t bp_loc_id)
323 return m_options.InvokeCallback (context, GetID(), bp_loc_id);
327 Breakpoint::GetOptions ()
333 Breakpoint::ResolveBreakpoint ()
336 m_resolver_sp->ResolveBreakpoint(*m_filter_sp);
340 Breakpoint::ResolveBreakpointInModules (ModuleList &module_list)
343 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
347 Breakpoint::ClearAllBreakpointSites ()
349 m_locations.ClearAllBreakpointSites();
352 //----------------------------------------------------------------------
353 // ModulesChanged: Pass in a list of new modules, and
354 //----------------------------------------------------------------------
357 Breakpoint::ModulesChanged (ModuleList &module_list, bool load, bool delete_locations)
359 Mutex::Locker modules_mutex(module_list.GetMutex());
362 // The logic for handling new modules is:
363 // 1) If the filter rejects this module, then skip it.
364 // 2) Run through the current location list and if there are any locations
365 // for that module, we mark the module as "seen" and we don't try to re-resolve
366 // breakpoint locations for that module.
367 // However, we do add breakpoint sites to these locations if needed.
368 // 3) If we don't see this module in our breakpoint location list, call ResolveInModules.
370 ModuleList new_modules; // We'll stuff the "unseen" modules in this list, and then resolve
371 // them after the locations pass. Have to do it this way because
372 // resolving breakpoints will add new locations potentially.
374 const size_t num_locs = m_locations.GetSize();
375 size_t num_modules = module_list.GetSize();
376 for (size_t i = 0; i < num_modules; i++)
379 ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (i));
380 if (!m_filter_sp->ModulePasses (module_sp))
383 for (size_t loc_idx = 0; loc_idx < num_locs; loc_idx++)
385 BreakpointLocationSP break_loc = m_locations.GetByIndex(loc_idx);
386 if (!break_loc->IsEnabled())
388 SectionSP section_sp (break_loc->GetAddress().GetSection());
389 if (!section_sp || section_sp->GetModule() == module_sp)
394 if (!break_loc->ResolveBreakpointSite())
396 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
398 log->Printf ("Warning: could not set breakpoint site for breakpoint location %d of breakpoint %d.\n",
399 break_loc->GetID(), GetID());
405 new_modules.AppendIfNeeded (module_sp);
409 if (new_modules.GetSize() > 0)
411 // If this is not an internal breakpoint, set up to record the new locations, then dispatch
412 // an event with the new locations.
415 BreakpointEventData *new_locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsAdded,
418 m_locations.StartRecordingNewLocations(new_locations_event->GetBreakpointLocationCollection());
420 ResolveBreakpointInModules(new_modules);
422 m_locations.StopRecordingNewLocations();
423 if (new_locations_event->GetBreakpointLocationCollection().GetSize() != 0)
425 SendBreakpointChangedEvent (new_locations_event);
428 delete new_locations_event;
431 ResolveBreakpointInModules(new_modules);
437 // Go through the currently set locations and if any have breakpoints in
438 // the module list, then remove their breakpoint sites, and their locations if asked to.
440 BreakpointEventData *removed_locations_event;
442 removed_locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsRemoved,
445 removed_locations_event = NULL;
447 size_t num_modules = module_list.GetSize();
448 for (size_t i = 0; i < num_modules; i++)
450 ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (i));
451 if (m_filter_sp->ModulePasses (module_sp))
454 size_t num_locations = m_locations.GetSize();
455 BreakpointLocationCollection locations_to_remove;
456 for (loc_idx = 0; loc_idx < num_locations; loc_idx++)
458 BreakpointLocationSP break_loc_sp (m_locations.GetByIndex(loc_idx));
459 SectionSP section_sp (break_loc_sp->GetAddress().GetSection());
460 if (section_sp && section_sp->GetModule() == module_sp)
462 // Remove this breakpoint since the shared library is
463 // unloaded, but keep the breakpoint location around
464 // so we always get complete hit count and breakpoint
466 break_loc_sp->ClearBreakpointSite();
467 if (removed_locations_event)
469 removed_locations_event->GetBreakpointLocationCollection().Add(break_loc_sp);
471 if (delete_locations)
472 locations_to_remove.Add (break_loc_sp);
477 if (delete_locations)
479 size_t num_locations_to_remove = locations_to_remove.GetSize();
480 for (loc_idx = 0; loc_idx < num_locations_to_remove; loc_idx++)
481 m_locations.RemoveLocation (locations_to_remove.GetByIndex(loc_idx));
485 SendBreakpointChangedEvent (removed_locations_event);
490 Breakpoint::ModuleReplaced (ModuleSP old_module_sp, ModuleSP new_module_sp)
492 ModuleList temp_list;
493 temp_list.Append (new_module_sp);
494 ModulesChanged (temp_list, true);
496 // TO DO: For now I'm just adding locations for the new module and removing the
497 // breakpoint locations that were in the old module.
498 // We should really go find the ones that are in the new module & if we can determine that they are "equivalent"
499 // carry over the options from the old location to the new.
502 temp_list.Append (old_module_sp);
503 ModulesChanged (temp_list, false, true);
507 Breakpoint::Dump (Stream *)
512 Breakpoint::GetNumResolvedLocations() const
514 // Return the number of breakpoints that are actually resolved and set
515 // down in the inferior process.
516 return m_locations.GetNumResolvedLocations();
520 Breakpoint::GetNumLocations() const
522 return m_locations.GetSize();
526 Breakpoint::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_locations)
530 if (!m_kind_description.empty())
532 if (eDescriptionLevelBrief)
534 s->PutCString (GetBreakpointKind());
538 s->Printf("Kind: %s\n", GetBreakpointKind ());
541 const size_t num_locations = GetNumLocations ();
542 const size_t num_resolved_locations = GetNumResolvedLocations ();
544 // They just made the breakpoint, they don't need to be told HOW they made it...
545 // Also, we'll print the breakpoint number differently depending on whether there is 1 or more locations.
546 if (level != eDescriptionLevelInitial)
548 s->Printf("%i: ", GetID());
549 GetResolverDescription (s);
550 GetFilterDescription (s);
555 case lldb::eDescriptionLevelBrief:
556 case lldb::eDescriptionLevelFull:
557 if (num_locations > 0)
559 s->Printf(", locations = %" PRIu64, (uint64_t)num_locations);
560 if (num_resolved_locations > 0)
561 s->Printf(", resolved = %" PRIu64, (uint64_t)num_resolved_locations);
565 // Don't print the pending notification for exception resolvers since we don't generally
566 // know how to set them until the target is run.
567 if (m_resolver_sp->getResolverID() != BreakpointResolver::ExceptionResolver)
568 s->Printf(", locations = 0 (pending)");
571 GetOptions()->GetDescription(s, level);
573 if (level == lldb::eDescriptionLevelFull)
580 case lldb::eDescriptionLevelInitial:
581 s->Printf ("Breakpoint %i: ", GetID());
582 if (num_locations == 0)
584 s->Printf ("no locations (pending).");
586 else if (num_locations == 1)
588 // If there is one location only, we'll just print that location information. But don't do this if
589 // show locations is true, then that will be handled below.
590 if (show_locations == false)
592 GetLocationAtIndex(0)->GetDescription(s, level);
596 s->Printf ("%zd locations.", num_locations);
601 s->Printf ("%zd locations.", num_locations);
605 case lldb::eDescriptionLevelVerbose:
606 // Verbose mode does a debug dump of the breakpoint
610 GetOptions()->GetDescription(s, level);
617 // The brief description is just the location name (1.2 or whatever). That's pointless to
618 // show in the breakpoint's description, so suppress it.
619 if (show_locations && level != lldb::eDescriptionLevelBrief)
622 for (size_t i = 0; i < num_locations; ++i)
624 BreakpointLocation *loc = GetLocationAtIndex(i).get();
625 loc->GetDescription(s, level);
633 Breakpoint::GetResolverDescription (Stream *s)
636 m_resolver_sp->GetDescription (s);
641 Breakpoint::GetMatchingFileLine (const ConstString &filename, uint32_t line_number, BreakpointLocationCollection &loc_coll)
643 // TODO: To be correct, this method needs to fill the breakpoint location collection
644 // with the location IDs which match the filename and line_number.
649 BreakpointResolverFileLine *resolverFileLine = dyn_cast<BreakpointResolverFileLine>(m_resolver_sp.get());
650 if (resolverFileLine &&
651 resolverFileLine->m_file_spec.GetFilename() == filename &&
652 resolverFileLine->m_line_number == line_number)
661 Breakpoint::GetFilterDescription (Stream *s)
663 m_filter_sp->GetDescription (s);
667 Breakpoint::SendBreakpointChangedEvent (lldb::BreakpointEventType eventKind)
671 && GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
673 BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind, shared_from_this());
675 GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
680 Breakpoint::SendBreakpointChangedEvent (BreakpointEventData *data)
688 && GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
689 GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
694 Breakpoint::BreakpointEventData::BreakpointEventData (BreakpointEventType sub_type,
695 const BreakpointSP &new_breakpoint_sp) :
697 m_breakpoint_event (sub_type),
698 m_new_breakpoint_sp (new_breakpoint_sp)
702 Breakpoint::BreakpointEventData::~BreakpointEventData ()
707 Breakpoint::BreakpointEventData::GetFlavorString ()
709 static ConstString g_flavor ("Breakpoint::BreakpointEventData");
714 Breakpoint::BreakpointEventData::GetFlavor () const
716 return BreakpointEventData::GetFlavorString ();
721 Breakpoint::BreakpointEventData::GetBreakpoint ()
723 return m_new_breakpoint_sp;
727 Breakpoint::BreakpointEventData::GetBreakpointEventType () const
729 return m_breakpoint_event;
733 Breakpoint::BreakpointEventData::Dump (Stream *s) const
737 const Breakpoint::BreakpointEventData *
738 Breakpoint::BreakpointEventData::GetEventDataFromEvent (const Event *event)
742 const EventData *event_data = event->GetData();
743 if (event_data && event_data->GetFlavor() == BreakpointEventData::GetFlavorString())
744 return static_cast <const BreakpointEventData *> (event->GetData());
750 Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (const EventSP &event_sp)
752 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
755 return eBreakpointEventTypeInvalidType;
757 return data->GetBreakpointEventType();
761 Breakpoint::BreakpointEventData::GetBreakpointFromEvent (const EventSP &event_sp)
765 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
767 bp_sp = data->m_new_breakpoint_sp;
773 Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent (const EventSP &event_sp)
775 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
777 return data->m_locations.GetSize();
782 lldb::BreakpointLocationSP
783 Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent (const lldb::EventSP &event_sp, uint32_t bp_loc_idx)
785 lldb::BreakpointLocationSP bp_loc_sp;
787 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
790 bp_loc_sp = data->m_locations.GetByIndex(bp_loc_idx);