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,
49 SearchFilterSP &filter_sp,
50 BreakpointResolverSP &resolver_sp,
52 bool resolve_indirect_symbols) :
53 m_being_created(true),
56 m_filter_sp (filter_sp),
57 m_resolver_sp (resolver_sp),
60 m_resolve_indirect_symbols(resolve_indirect_symbols)
62 m_being_created = false;
65 //----------------------------------------------------------------------
67 //----------------------------------------------------------------------
68 Breakpoint::~Breakpoint()
73 Breakpoint::IsInternal () const
75 return LLDB_BREAK_ID_IS_INTERNAL(m_bid);
81 Breakpoint::GetTarget ()
87 Breakpoint::GetTarget () const
93 Breakpoint::AddLocation (const Address &addr, bool *new_location)
95 return m_locations.AddLocation (addr, m_resolve_indirect_symbols, new_location);
99 Breakpoint::FindLocationByAddress (const Address &addr)
101 return m_locations.FindByAddress(addr);
105 Breakpoint::FindLocationIDByAddress (const Address &addr)
107 return m_locations.FindIDByAddress(addr);
111 Breakpoint::FindLocationByID (break_id_t bp_loc_id)
113 return m_locations.FindByID(bp_loc_id);
117 Breakpoint::GetLocationAtIndex (size_t index)
119 return m_locations.GetByIndex(index);
123 Breakpoint::RemoveInvalidLocations (const ArchSpec &arch)
125 m_locations.RemoveInvalidLocations(arch);
128 // For each of the overall options we need to decide how they propagate to
129 // the location options. This will determine the precedence of options on
130 // the breakpoint vs. its locations.
132 // Disable at the breakpoint level should override the location settings.
133 // That way you can conveniently turn off a whole breakpoint without messing
134 // up the individual settings.
137 Breakpoint::SetEnabled (bool enable)
139 if (enable == m_options.IsEnabled())
142 m_options.SetEnabled(enable);
144 m_locations.ResolveAllBreakpointSites();
146 m_locations.ClearAllBreakpointSites();
148 SendBreakpointChangedEvent (enable ? eBreakpointEventTypeEnabled : eBreakpointEventTypeDisabled);
153 Breakpoint::IsEnabled ()
155 return m_options.IsEnabled();
159 Breakpoint::SetIgnoreCount (uint32_t n)
161 if (m_options.GetIgnoreCount() == n)
164 m_options.SetIgnoreCount(n);
165 SendBreakpointChangedEvent (eBreakpointEventTypeIgnoreChanged);
169 Breakpoint::DecrementIgnoreCount ()
171 uint32_t ignore = m_options.GetIgnoreCount();
173 m_options.SetIgnoreCount(ignore - 1);
177 Breakpoint::GetIgnoreCount () const
179 return m_options.GetIgnoreCount();
183 Breakpoint::IgnoreCountShouldStop ()
185 uint32_t ignore = GetIgnoreCount();
188 // When we get here we know the location that caused the stop doesn't have an ignore count,
189 // since by contract we call it first... So we don't have to find & decrement it, we only have
190 // to decrement our own ignore count.
191 DecrementIgnoreCount();
199 Breakpoint::GetHitCount () const
201 return m_locations.GetHitCount();
205 Breakpoint::IsOneShot () const
207 return m_options.IsOneShot();
211 Breakpoint::SetOneShot (bool one_shot)
213 m_options.SetOneShot (one_shot);
217 Breakpoint::SetThreadID (lldb::tid_t thread_id)
219 if (m_options.GetThreadSpec()->GetTID() == thread_id)
222 m_options.GetThreadSpec()->SetTID(thread_id);
223 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
227 Breakpoint::GetThreadID () const
229 if (m_options.GetThreadSpecNoCreate() == NULL)
230 return LLDB_INVALID_THREAD_ID;
232 return m_options.GetThreadSpecNoCreate()->GetTID();
236 Breakpoint::SetThreadIndex (uint32_t index)
238 if (m_options.GetThreadSpec()->GetIndex() == index)
241 m_options.GetThreadSpec()->SetIndex(index);
242 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
246 Breakpoint::GetThreadIndex() const
248 if (m_options.GetThreadSpecNoCreate() == NULL)
251 return m_options.GetThreadSpecNoCreate()->GetIndex();
255 Breakpoint::SetThreadName (const char *thread_name)
257 if (m_options.GetThreadSpec()->GetName() != NULL
258 && ::strcmp (m_options.GetThreadSpec()->GetName(), thread_name) == 0)
261 m_options.GetThreadSpec()->SetName (thread_name);
262 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
266 Breakpoint::GetThreadName () const
268 if (m_options.GetThreadSpecNoCreate() == NULL)
271 return m_options.GetThreadSpecNoCreate()->GetName();
275 Breakpoint::SetQueueName (const char *queue_name)
277 if (m_options.GetThreadSpec()->GetQueueName() != NULL
278 && ::strcmp (m_options.GetThreadSpec()->GetQueueName(), queue_name) == 0)
281 m_options.GetThreadSpec()->SetQueueName (queue_name);
282 SendBreakpointChangedEvent (eBreakpointEventTypeThreadChanged);
286 Breakpoint::GetQueueName () const
288 if (m_options.GetThreadSpecNoCreate() == NULL)
291 return m_options.GetThreadSpecNoCreate()->GetQueueName();
295 Breakpoint::SetCondition (const char *condition)
297 m_options.SetCondition (condition);
298 SendBreakpointChangedEvent (eBreakpointEventTypeConditionChanged);
302 Breakpoint::GetConditionText () const
304 return m_options.GetConditionText();
307 // This function is used when "baton" doesn't need to be freed
309 Breakpoint::SetCallback (BreakpointHitCallback callback, void *baton, bool is_synchronous)
311 // The default "Baton" class will keep a copy of "baton" and won't free
312 // or delete it when it goes goes out of scope.
313 m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
315 SendBreakpointChangedEvent (eBreakpointEventTypeCommandChanged);
318 // This function is used when a baton needs to be freed and therefore is
319 // contained in a "Baton" subclass.
321 Breakpoint::SetCallback (BreakpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
323 m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
327 Breakpoint::ClearCallback ()
329 m_options.ClearCallback ();
333 Breakpoint::InvokeCallback (StoppointCallbackContext *context, break_id_t bp_loc_id)
335 return m_options.InvokeCallback (context, GetID(), bp_loc_id);
339 Breakpoint::GetOptions ()
345 Breakpoint::ResolveBreakpoint ()
348 m_resolver_sp->ResolveBreakpoint(*m_filter_sp);
352 Breakpoint::ResolveBreakpointInModules (ModuleList &module_list)
355 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
359 Breakpoint::ClearAllBreakpointSites ()
361 m_locations.ClearAllBreakpointSites();
364 //----------------------------------------------------------------------
365 // ModulesChanged: Pass in a list of new modules, and
366 //----------------------------------------------------------------------
369 Breakpoint::ModulesChanged (ModuleList &module_list, bool load, bool delete_locations)
371 Mutex::Locker modules_mutex(module_list.GetMutex());
374 // The logic for handling new modules is:
375 // 1) If the filter rejects this module, then skip it.
376 // 2) Run through the current location list and if there are any locations
377 // for that module, we mark the module as "seen" and we don't try to re-resolve
378 // breakpoint locations for that module.
379 // However, we do add breakpoint sites to these locations if needed.
380 // 3) If we don't see this module in our breakpoint location list, call ResolveInModules.
382 ModuleList new_modules; // We'll stuff the "unseen" modules in this list, and then resolve
383 // them after the locations pass. Have to do it this way because
384 // resolving breakpoints will add new locations potentially.
386 const size_t num_locs = m_locations.GetSize();
387 size_t num_modules = module_list.GetSize();
388 for (size_t i = 0; i < num_modules; i++)
391 ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (i));
392 if (!m_filter_sp->ModulePasses (module_sp))
395 for (size_t loc_idx = 0; loc_idx < num_locs; loc_idx++)
397 BreakpointLocationSP break_loc = m_locations.GetByIndex(loc_idx);
398 if (!break_loc->IsEnabled())
400 SectionSP section_sp (break_loc->GetAddress().GetSection());
401 if (!section_sp || section_sp->GetModule() == module_sp)
406 if (!break_loc->ResolveBreakpointSite())
408 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
410 log->Printf ("Warning: could not set breakpoint site for breakpoint location %d of breakpoint %d.\n",
411 break_loc->GetID(), GetID());
417 new_modules.AppendIfNeeded (module_sp);
421 if (new_modules.GetSize() > 0)
423 // If this is not an internal breakpoint, set up to record the new locations, then dispatch
424 // an event with the new locations.
427 BreakpointEventData *new_locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsAdded,
430 m_locations.StartRecordingNewLocations(new_locations_event->GetBreakpointLocationCollection());
432 ResolveBreakpointInModules(new_modules);
434 m_locations.StopRecordingNewLocations();
435 if (new_locations_event->GetBreakpointLocationCollection().GetSize() != 0)
437 SendBreakpointChangedEvent (new_locations_event);
440 delete new_locations_event;
443 ResolveBreakpointInModules(new_modules);
449 // Go through the currently set locations and if any have breakpoints in
450 // the module list, then remove their breakpoint sites, and their locations if asked to.
452 BreakpointEventData *removed_locations_event;
454 removed_locations_event = new BreakpointEventData (eBreakpointEventTypeLocationsRemoved,
457 removed_locations_event = NULL;
459 size_t num_modules = module_list.GetSize();
460 for (size_t i = 0; i < num_modules; i++)
462 ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (i));
463 if (m_filter_sp->ModulePasses (module_sp))
466 size_t num_locations = m_locations.GetSize();
467 BreakpointLocationCollection locations_to_remove;
468 for (loc_idx = 0; loc_idx < num_locations; loc_idx++)
470 BreakpointLocationSP break_loc_sp (m_locations.GetByIndex(loc_idx));
471 SectionSP section_sp (break_loc_sp->GetAddress().GetSection());
472 if (section_sp && section_sp->GetModule() == module_sp)
474 // Remove this breakpoint since the shared library is
475 // unloaded, but keep the breakpoint location around
476 // so we always get complete hit count and breakpoint
478 break_loc_sp->ClearBreakpointSite();
479 if (removed_locations_event)
481 removed_locations_event->GetBreakpointLocationCollection().Add(break_loc_sp);
483 if (delete_locations)
484 locations_to_remove.Add (break_loc_sp);
489 if (delete_locations)
491 size_t num_locations_to_remove = locations_to_remove.GetSize();
492 for (loc_idx = 0; loc_idx < num_locations_to_remove; loc_idx++)
493 m_locations.RemoveLocation (locations_to_remove.GetByIndex(loc_idx));
497 SendBreakpointChangedEvent (removed_locations_event);
502 Breakpoint::ModuleReplaced (ModuleSP old_module_sp, ModuleSP new_module_sp)
504 ModuleList temp_list;
505 temp_list.Append (new_module_sp);
506 ModulesChanged (temp_list, true);
508 // TO DO: For now I'm just adding locations for the new module and removing the
509 // breakpoint locations that were in the old module.
510 // We should really go find the ones that are in the new module & if we can determine that they are "equivalent"
511 // carry over the options from the old location to the new.
514 temp_list.Append (old_module_sp);
515 ModulesChanged (temp_list, false, true);
519 Breakpoint::Dump (Stream *)
524 Breakpoint::GetNumResolvedLocations() const
526 // Return the number of breakpoints that are actually resolved and set
527 // down in the inferior process.
528 return m_locations.GetNumResolvedLocations();
532 Breakpoint::GetNumLocations() const
534 return m_locations.GetSize();
538 Breakpoint::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_locations)
542 if (!m_kind_description.empty())
544 if (eDescriptionLevelBrief)
546 s->PutCString (GetBreakpointKind());
550 s->Printf("Kind: %s\n", GetBreakpointKind ());
553 const size_t num_locations = GetNumLocations ();
554 const size_t num_resolved_locations = GetNumResolvedLocations ();
556 // They just made the breakpoint, they don't need to be told HOW they made it...
557 // Also, we'll print the breakpoint number differently depending on whether there is 1 or more locations.
558 if (level != eDescriptionLevelInitial)
560 s->Printf("%i: ", GetID());
561 GetResolverDescription (s);
562 GetFilterDescription (s);
567 case lldb::eDescriptionLevelBrief:
568 case lldb::eDescriptionLevelFull:
569 if (num_locations > 0)
571 s->Printf(", locations = %" PRIu64, (uint64_t)num_locations);
572 if (num_resolved_locations > 0)
573 s->Printf(", resolved = %" PRIu64 ", hit count = %d", (uint64_t)num_resolved_locations, GetHitCount());
577 // Don't print the pending notification for exception resolvers since we don't generally
578 // know how to set them until the target is run.
579 if (m_resolver_sp->getResolverID() != BreakpointResolver::ExceptionResolver)
580 s->Printf(", locations = 0 (pending)");
583 GetOptions()->GetDescription(s, level);
585 if (level == lldb::eDescriptionLevelFull)
592 case lldb::eDescriptionLevelInitial:
593 s->Printf ("Breakpoint %i: ", GetID());
594 if (num_locations == 0)
596 s->Printf ("no locations (pending).");
598 else if (num_locations == 1)
600 // If there is one location only, we'll just print that location information. But don't do this if
601 // show locations is true, then that will be handled below.
602 if (show_locations == false)
604 GetLocationAtIndex(0)->GetDescription(s, level);
608 s->Printf ("%zd locations.", num_locations);
613 s->Printf ("%zd locations.", num_locations);
617 case lldb::eDescriptionLevelVerbose:
618 // Verbose mode does a debug dump of the breakpoint
622 GetOptions()->GetDescription(s, level);
629 // The brief description is just the location name (1.2 or whatever). That's pointless to
630 // show in the breakpoint's description, so suppress it.
631 if (show_locations && level != lldb::eDescriptionLevelBrief)
634 for (size_t i = 0; i < num_locations; ++i)
636 BreakpointLocation *loc = GetLocationAtIndex(i).get();
637 loc->GetDescription(s, level);
645 Breakpoint::GetResolverDescription (Stream *s)
648 m_resolver_sp->GetDescription (s);
653 Breakpoint::GetMatchingFileLine (const ConstString &filename, uint32_t line_number, BreakpointLocationCollection &loc_coll)
655 // TODO: To be correct, this method needs to fill the breakpoint location collection
656 // with the location IDs which match the filename and line_number.
661 BreakpointResolverFileLine *resolverFileLine = dyn_cast<BreakpointResolverFileLine>(m_resolver_sp.get());
662 if (resolverFileLine &&
663 resolverFileLine->m_file_spec.GetFilename() == filename &&
664 resolverFileLine->m_line_number == line_number)
673 Breakpoint::GetFilterDescription (Stream *s)
675 m_filter_sp->GetDescription (s);
679 Breakpoint::SendBreakpointChangedEvent (lldb::BreakpointEventType eventKind)
683 && GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
685 BreakpointEventData *data = new Breakpoint::BreakpointEventData (eventKind, shared_from_this());
687 GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
692 Breakpoint::SendBreakpointChangedEvent (BreakpointEventData *data)
700 && GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
701 GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged, data);
706 Breakpoint::BreakpointEventData::BreakpointEventData (BreakpointEventType sub_type,
707 const BreakpointSP &new_breakpoint_sp) :
709 m_breakpoint_event (sub_type),
710 m_new_breakpoint_sp (new_breakpoint_sp)
714 Breakpoint::BreakpointEventData::~BreakpointEventData ()
719 Breakpoint::BreakpointEventData::GetFlavorString ()
721 static ConstString g_flavor ("Breakpoint::BreakpointEventData");
726 Breakpoint::BreakpointEventData::GetFlavor () const
728 return BreakpointEventData::GetFlavorString ();
733 Breakpoint::BreakpointEventData::GetBreakpoint ()
735 return m_new_breakpoint_sp;
739 Breakpoint::BreakpointEventData::GetBreakpointEventType () const
741 return m_breakpoint_event;
745 Breakpoint::BreakpointEventData::Dump (Stream *s) const
749 const Breakpoint::BreakpointEventData *
750 Breakpoint::BreakpointEventData::GetEventDataFromEvent (const Event *event)
754 const EventData *event_data = event->GetData();
755 if (event_data && event_data->GetFlavor() == BreakpointEventData::GetFlavorString())
756 return static_cast <const BreakpointEventData *> (event->GetData());
762 Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent (const EventSP &event_sp)
764 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
767 return eBreakpointEventTypeInvalidType;
769 return data->GetBreakpointEventType();
773 Breakpoint::BreakpointEventData::GetBreakpointFromEvent (const EventSP &event_sp)
777 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
779 bp_sp = data->m_new_breakpoint_sp;
785 Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent (const EventSP &event_sp)
787 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
789 return data->m_locations.GetSize();
794 lldb::BreakpointLocationSP
795 Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent (const lldb::EventSP &event_sp, uint32_t bp_loc_idx)
797 lldb::BreakpointLocationSP bp_loc_sp;
799 const BreakpointEventData *data = GetEventDataFromEvent (event_sp.get());
802 bp_loc_sp = data->m_locations.GetByIndex(bp_loc_idx);