Path: blob/main/contrib/llvm-project/lldb/source/Breakpoint/BreakpointLocationCollection.cpp
39587 views
//===-- BreakpointLocationCollection.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/BreakpointLocationCollection.h"9#include "lldb/Breakpoint/Breakpoint.h"10#include "lldb/Breakpoint/BreakpointLocation.h"11#include "lldb/Core/ModuleList.h"12#include "lldb/Target/Thread.h"13#include "lldb/Target/ThreadSpec.h"1415using namespace lldb;16using namespace lldb_private;1718// BreakpointLocationCollection constructor19BreakpointLocationCollection::BreakpointLocationCollection() = default;2021// Destructor22BreakpointLocationCollection::~BreakpointLocationCollection() = default;2324void BreakpointLocationCollection::Add(const BreakpointLocationSP &bp_loc) {25std::lock_guard<std::mutex> guard(m_collection_mutex);26BreakpointLocationSP old_bp_loc =27FindByIDPair(bp_loc->GetBreakpoint().GetID(), bp_loc->GetID());28if (!old_bp_loc.get())29m_break_loc_collection.push_back(bp_loc);30}3132bool BreakpointLocationCollection::Remove(lldb::break_id_t bp_id,33lldb::break_id_t bp_loc_id) {34std::lock_guard<std::mutex> guard(m_collection_mutex);35collection::iterator pos = GetIDPairIterator(bp_id, bp_loc_id); // Predicate36if (pos != m_break_loc_collection.end()) {37m_break_loc_collection.erase(pos);38return true;39}40return false;41}4243class BreakpointIDPairMatches {44public:45BreakpointIDPairMatches(lldb::break_id_t break_id,46lldb::break_id_t break_loc_id)47: m_break_id(break_id), m_break_loc_id(break_loc_id) {}4849bool operator()(const BreakpointLocationSP &bp_loc) const {50return m_break_id == bp_loc->GetBreakpoint().GetID() &&51m_break_loc_id == bp_loc->GetID();52}5354private:55const lldb::break_id_t m_break_id;56const lldb::break_id_t m_break_loc_id;57};5859BreakpointLocationCollection::collection::iterator60BreakpointLocationCollection::GetIDPairIterator(lldb::break_id_t break_id,61lldb::break_id_t break_loc_id) {62return std::find_if(63m_break_loc_collection.begin(),64m_break_loc_collection.end(), // Search full range65BreakpointIDPairMatches(break_id, break_loc_id)); // Predicate66}6768BreakpointLocationCollection::collection::const_iterator69BreakpointLocationCollection::GetIDPairConstIterator(70lldb::break_id_t break_id, lldb::break_id_t break_loc_id) const {71return std::find_if(72m_break_loc_collection.begin(),73m_break_loc_collection.end(), // Search full range74BreakpointIDPairMatches(break_id, break_loc_id)); // Predicate75}7677BreakpointLocationSP78BreakpointLocationCollection::FindByIDPair(lldb::break_id_t break_id,79lldb::break_id_t break_loc_id) {80BreakpointLocationSP stop_sp;81collection::iterator pos = GetIDPairIterator(break_id, break_loc_id);82if (pos != m_break_loc_collection.end())83stop_sp = *pos;8485return stop_sp;86}8788const BreakpointLocationSP BreakpointLocationCollection::FindByIDPair(89lldb::break_id_t break_id, lldb::break_id_t break_loc_id) const {90BreakpointLocationSP stop_sp;91collection::const_iterator pos =92GetIDPairConstIterator(break_id, break_loc_id);93if (pos != m_break_loc_collection.end())94stop_sp = *pos;9596return stop_sp;97}9899BreakpointLocationSP BreakpointLocationCollection::GetByIndex(size_t i) {100std::lock_guard<std::mutex> guard(m_collection_mutex);101BreakpointLocationSP stop_sp;102if (i < m_break_loc_collection.size())103stop_sp = m_break_loc_collection[i];104105return stop_sp;106}107108const BreakpointLocationSP109BreakpointLocationCollection::GetByIndex(size_t i) const {110std::lock_guard<std::mutex> guard(m_collection_mutex);111BreakpointLocationSP stop_sp;112if (i < m_break_loc_collection.size())113stop_sp = m_break_loc_collection[i];114115return stop_sp;116}117118bool BreakpointLocationCollection::ShouldStop(119StoppointCallbackContext *context) {120bool shouldStop = false;121size_t i = 0;122size_t prev_size = GetSize();123while (i < prev_size) {124// ShouldStop can remove the breakpoint from the list, or even delete125// it, so we should126BreakpointLocationSP cur_loc_sp = GetByIndex(i);127BreakpointSP keep_bkpt_alive_sp = cur_loc_sp->GetBreakpoint().shared_from_this();128if (cur_loc_sp->ShouldStop(context))129shouldStop = true;130131if (prev_size == GetSize())132i++;133prev_size = GetSize();134}135return shouldStop;136}137138bool BreakpointLocationCollection::ValidForThisThread(Thread &thread) {139std::lock_guard<std::mutex> guard(m_collection_mutex);140collection::iterator pos, begin = m_break_loc_collection.begin(),141end = m_break_loc_collection.end();142143for (pos = begin; pos != end; ++pos) {144if ((*pos)->ValidForThisThread(thread))145return true;146}147return false;148}149150bool BreakpointLocationCollection::IsInternal() const {151std::lock_guard<std::mutex> guard(m_collection_mutex);152collection::const_iterator pos, begin = m_break_loc_collection.begin(),153end = m_break_loc_collection.end();154155bool is_internal = true;156157for (pos = begin; pos != end; ++pos) {158if (!(*pos)->GetBreakpoint().IsInternal()) {159is_internal = false;160break;161}162}163return is_internal;164}165166void BreakpointLocationCollection::GetDescription(167Stream *s, lldb::DescriptionLevel level) {168std::lock_guard<std::mutex> guard(m_collection_mutex);169collection::iterator pos, begin = m_break_loc_collection.begin(),170end = m_break_loc_collection.end();171172for (pos = begin; pos != end; ++pos) {173if (pos != begin)174s->PutChar(' ');175(*pos)->GetDescription(s, level);176}177}178179BreakpointLocationCollection &BreakpointLocationCollection::operator=(180const BreakpointLocationCollection &rhs) {181if (this != &rhs) {182std::lock(m_collection_mutex, rhs.m_collection_mutex);183std::lock_guard<std::mutex> lhs_guard(m_collection_mutex, std::adopt_lock);184std::lock_guard<std::mutex> rhs_guard(rhs.m_collection_mutex, std::adopt_lock);185m_break_loc_collection = rhs.m_break_loc_collection;186}187return *this;188}189190191