Path: blob/main/contrib/llvm-project/lldb/source/Breakpoint/BreakpointList.cpp
39587 views
//===-- BreakpointList.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Breakpoint/BreakpointList.h"910#include "lldb/Target/Target.h"1112#include "llvm/Support/Errc.h"1314using namespace lldb;15using namespace lldb_private;1617static void NotifyChange(const BreakpointSP &bp, BreakpointEventType event) {18Target &target = bp->GetTarget();19if (target.EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged)) {20auto event_data_sp =21std::make_shared<Breakpoint::BreakpointEventData>(event, bp);22target.BroadcastEvent(Target::eBroadcastBitBreakpointChanged,23event_data_sp);24}25}2627BreakpointList::BreakpointList(bool is_internal)28: m_next_break_id(0), m_is_internal(is_internal) {}2930BreakpointList::~BreakpointList() = default;3132break_id_t BreakpointList::Add(BreakpointSP &bp_sp, bool notify) {33std::lock_guard<std::recursive_mutex> guard(m_mutex);3435// Internal breakpoint IDs are negative, normal ones are positive36bp_sp->SetID(m_is_internal ? --m_next_break_id : ++m_next_break_id);3738m_breakpoints.push_back(bp_sp);3940if (notify)41NotifyChange(bp_sp, eBreakpointEventTypeAdded);4243return bp_sp->GetID();44}4546bool BreakpointList::Remove(break_id_t break_id, bool notify) {47std::lock_guard<std::recursive_mutex> guard(m_mutex);4849auto it = std::find_if(50m_breakpoints.begin(), m_breakpoints.end(),51[&](const BreakpointSP &bp) { return bp->GetID() == break_id; });5253if (it == m_breakpoints.end())54return false;5556if (notify)57NotifyChange(*it, eBreakpointEventTypeRemoved);5859m_breakpoints.erase(it);6061return true;62}6364void BreakpointList::RemoveInvalidLocations(const ArchSpec &arch) {65std::lock_guard<std::recursive_mutex> guard(m_mutex);66for (const auto &bp_sp : m_breakpoints)67bp_sp->RemoveInvalidLocations(arch);68}6970void BreakpointList::SetEnabledAll(bool enabled) {71std::lock_guard<std::recursive_mutex> guard(m_mutex);72for (const auto &bp_sp : m_breakpoints)73bp_sp->SetEnabled(enabled);74}7576void BreakpointList::SetEnabledAllowed(bool enabled) {77std::lock_guard<std::recursive_mutex> guard(m_mutex);78for (const auto &bp_sp : m_breakpoints)79if (bp_sp->AllowDisable())80bp_sp->SetEnabled(enabled);81}8283void BreakpointList::RemoveAll(bool notify) {84std::lock_guard<std::recursive_mutex> guard(m_mutex);85ClearAllBreakpointSites();8687if (notify) {88for (const auto &bp_sp : m_breakpoints)89NotifyChange(bp_sp, eBreakpointEventTypeRemoved);90}9192m_breakpoints.clear();93}9495void BreakpointList::RemoveAllowed(bool notify) {96std::lock_guard<std::recursive_mutex> guard(m_mutex);9798for (const auto &bp_sp : m_breakpoints) {99if (bp_sp->AllowDelete())100bp_sp->ClearAllBreakpointSites();101if (notify)102NotifyChange(bp_sp, eBreakpointEventTypeRemoved);103}104105llvm::erase_if(m_breakpoints,106[&](const BreakpointSP &bp) { return bp->AllowDelete(); });107}108109BreakpointList::bp_collection::iterator110BreakpointList::GetBreakpointIDIterator(break_id_t break_id) {111return std::find_if(112m_breakpoints.begin(), m_breakpoints.end(),113[&](const BreakpointSP &bp) { return bp->GetID() == break_id; });114}115116BreakpointList::bp_collection::const_iterator117BreakpointList::GetBreakpointIDConstIterator(break_id_t break_id) const {118return std::find_if(119m_breakpoints.begin(), m_breakpoints.end(),120[&](const BreakpointSP &bp) { return bp->GetID() == break_id; });121}122123BreakpointSP BreakpointList::FindBreakpointByID(break_id_t break_id) const {124std::lock_guard<std::recursive_mutex> guard(m_mutex);125126auto it = GetBreakpointIDConstIterator(break_id);127if (it != m_breakpoints.end())128return *it;129return {};130}131132llvm::Expected<std::vector<lldb::BreakpointSP>>133BreakpointList::FindBreakpointsByName(const char *name) {134if (!name)135return llvm::createStringError(llvm::errc::invalid_argument,136"FindBreakpointsByName requires a name");137138Status error;139if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(name), error))140return error.ToError();141142std::vector<lldb::BreakpointSP> matching_bps;143for (BreakpointSP bkpt_sp : Breakpoints()) {144if (bkpt_sp->MatchesName(name)) {145matching_bps.push_back(bkpt_sp);146}147}148149return matching_bps;150}151152void BreakpointList::Dump(Stream *s) const {153std::lock_guard<std::recursive_mutex> guard(m_mutex);154s->Printf("%p: ", static_cast<const void *>(this));155s->Indent();156s->Printf("BreakpointList with %u Breakpoints:\n",157(uint32_t)m_breakpoints.size());158s->IndentMore();159for (const auto &bp_sp : m_breakpoints)160bp_sp->Dump(s);161s->IndentLess();162}163164BreakpointSP BreakpointList::GetBreakpointAtIndex(size_t i) const {165std::lock_guard<std::recursive_mutex> guard(m_mutex);166if (i < m_breakpoints.size())167return m_breakpoints[i];168return {};169}170171void BreakpointList::UpdateBreakpoints(ModuleList &module_list, bool added,172bool delete_locations) {173std::lock_guard<std::recursive_mutex> guard(m_mutex);174for (const auto &bp_sp : m_breakpoints)175bp_sp->ModulesChanged(module_list, added, delete_locations);176}177178void BreakpointList::UpdateBreakpointsWhenModuleIsReplaced(179ModuleSP old_module_sp, ModuleSP new_module_sp) {180std::lock_guard<std::recursive_mutex> guard(m_mutex);181for (const auto &bp_sp : m_breakpoints)182bp_sp->ModuleReplaced(old_module_sp, new_module_sp);183}184185void BreakpointList::ClearAllBreakpointSites() {186std::lock_guard<std::recursive_mutex> guard(m_mutex);187for (const auto &bp_sp : m_breakpoints)188bp_sp->ClearAllBreakpointSites();189}190191void BreakpointList::ResetHitCounts() {192std::lock_guard<std::recursive_mutex> guard(m_mutex);193for (const auto &bp_sp : m_breakpoints)194bp_sp->ResetHitCount();195}196197void BreakpointList::GetListMutex(198std::unique_lock<std::recursive_mutex> &lock) {199lock = std::unique_lock<std::recursive_mutex>(m_mutex);200}201202203