Path: blob/main/contrib/llvm-project/lldb/source/Breakpoint/BreakpointLocationList.cpp
39587 views
//===-- BreakpointLocationList.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/BreakpointLocationList.h"910#include "lldb/Breakpoint/Breakpoint.h"11#include "lldb/Breakpoint/BreakpointLocation.h"12#include "lldb/Core/Module.h"13#include "lldb/Core/Section.h"14#include "lldb/Target/SectionLoadList.h"15#include "lldb/Target/Target.h"16#include "lldb/Utility/ArchSpec.h"1718using namespace lldb;19using namespace lldb_private;2021BreakpointLocationList::BreakpointLocationList(Breakpoint &owner)22: m_owner(owner), m_next_id(0), m_new_location_recorder(nullptr) {}2324BreakpointLocationList::~BreakpointLocationList() = default;2526BreakpointLocationSP27BreakpointLocationList::Create(const Address &addr,28bool resolve_indirect_symbols) {29std::lock_guard<std::recursive_mutex> guard(m_mutex);30// The location ID is just the size of the location list + 131lldb::break_id_t bp_loc_id = ++m_next_id;32BreakpointLocationSP bp_loc_sp(33new BreakpointLocation(bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID,34m_owner.IsHardware(), resolve_indirect_symbols));35m_locations.push_back(bp_loc_sp);36m_address_to_location[addr] = bp_loc_sp;37return bp_loc_sp;38}3940bool BreakpointLocationList::ShouldStop(StoppointCallbackContext *context,41lldb::break_id_t break_id) {42BreakpointLocationSP bp = FindByID(break_id);43if (bp) {44// Let the BreakpointLocation decide if it should stop here (could not have45// reached it's target hit count yet, or it could have a callback that46// decided it shouldn't stop (shared library loads/unloads).47return bp->ShouldStop(context);48}49// We should stop here since this BreakpointLocation isn't valid anymore or50// it doesn't exist.51return true;52}5354lldb::break_id_t BreakpointLocationList::FindIDByAddress(const Address &addr) {55BreakpointLocationSP bp_loc_sp = FindByAddress(addr);56if (bp_loc_sp) {57return bp_loc_sp->GetID();58}59return LLDB_INVALID_BREAK_ID;60}6162static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val) {63return lhs->GetID() < val;64}6566BreakpointLocationSP67BreakpointLocationList::FindByID(lldb::break_id_t break_id) const {68std::lock_guard<std::recursive_mutex> guard(m_mutex);69collection::const_iterator end = m_locations.end();70collection::const_iterator pos =71llvm::lower_bound(m_locations, break_id, Compare);72if (pos != end && (*pos)->GetID() == break_id)73return *(pos);74else75return BreakpointLocationSP();76}7778size_t BreakpointLocationList::FindInModule(79Module *module, BreakpointLocationCollection &bp_loc_list) {80std::lock_guard<std::recursive_mutex> guard(m_mutex);81const size_t orig_size = bp_loc_list.GetSize();82collection::iterator pos, end = m_locations.end();8384for (pos = m_locations.begin(); pos != end; ++pos) {85BreakpointLocationSP break_loc = (*pos);86SectionSP section_sp(break_loc->GetAddress().GetSection());87if (section_sp && section_sp->GetModule().get() == module) {88bp_loc_list.Add(break_loc);89}90}91return bp_loc_list.GetSize() - orig_size;92}9394const BreakpointLocationSP95BreakpointLocationList::FindByAddress(const Address &addr) const {96std::lock_guard<std::recursive_mutex> guard(m_mutex);97BreakpointLocationSP bp_loc_sp;98if (!m_locations.empty()) {99Address so_addr;100101if (addr.IsSectionOffset()) {102so_addr = addr;103} else {104// Try and resolve as a load address if possible.105m_owner.GetTarget().GetSectionLoadList().ResolveLoadAddress(106addr.GetOffset(), so_addr);107if (!so_addr.IsValid()) {108// The address didn't resolve, so just set to passed in addr.109so_addr = addr;110}111}112113addr_map::const_iterator pos = m_address_to_location.find(so_addr);114if (pos != m_address_to_location.end())115bp_loc_sp = pos->second;116}117118return bp_loc_sp;119}120121void BreakpointLocationList::Dump(Stream *s) const {122s->Printf("%p: ", static_cast<const void *>(this));123// s->Indent();124std::lock_guard<std::recursive_mutex> guard(m_mutex);125s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",126(uint64_t)m_locations.size());127s->IndentMore();128collection::const_iterator pos, end = m_locations.end();129for (pos = m_locations.begin(); pos != end; ++pos)130(*pos)->Dump(s);131s->IndentLess();132}133134BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) {135std::lock_guard<std::recursive_mutex> guard(m_mutex);136BreakpointLocationSP bp_loc_sp;137if (i < m_locations.size())138bp_loc_sp = m_locations[i];139140return bp_loc_sp;141}142143const BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) const {144std::lock_guard<std::recursive_mutex> guard(m_mutex);145BreakpointLocationSP bp_loc_sp;146if (i < m_locations.size())147bp_loc_sp = m_locations[i];148149return bp_loc_sp;150}151152void BreakpointLocationList::ClearAllBreakpointSites() {153std::lock_guard<std::recursive_mutex> guard(m_mutex);154collection::iterator pos, end = m_locations.end();155for (pos = m_locations.begin(); pos != end; ++pos)156(*pos)->ClearBreakpointSite();157}158159void BreakpointLocationList::ResolveAllBreakpointSites() {160std::lock_guard<std::recursive_mutex> guard(m_mutex);161collection::iterator pos, end = m_locations.end();162163for (pos = m_locations.begin(); pos != end; ++pos) {164if ((*pos)->IsEnabled())165(*pos)->ResolveBreakpointSite();166}167}168169uint32_t BreakpointLocationList::GetHitCount() const {170uint32_t hit_count = 0;171std::lock_guard<std::recursive_mutex> guard(m_mutex);172collection::const_iterator pos, end = m_locations.end();173for (pos = m_locations.begin(); pos != end; ++pos)174hit_count += (*pos)->GetHitCount();175return hit_count;176}177178void BreakpointLocationList::ResetHitCount() {179std::lock_guard<std::recursive_mutex> guard(m_mutex);180for (auto &loc : m_locations)181loc->ResetHitCount();182}183184size_t BreakpointLocationList::GetNumResolvedLocations() const {185std::lock_guard<std::recursive_mutex> guard(m_mutex);186size_t resolve_count = 0;187collection::const_iterator pos, end = m_locations.end();188for (pos = m_locations.begin(); pos != end; ++pos) {189if ((*pos)->IsResolved())190++resolve_count;191}192return resolve_count;193}194195void BreakpointLocationList::GetDescription(Stream *s,196lldb::DescriptionLevel level) {197std::lock_guard<std::recursive_mutex> guard(m_mutex);198collection::iterator pos, end = m_locations.end();199200for (pos = m_locations.begin(); pos != end; ++pos) {201s->Printf(" ");202(*pos)->GetDescription(s, level);203}204}205206BreakpointLocationSP BreakpointLocationList::AddLocation(207const Address &addr, bool resolve_indirect_symbols, bool *new_location) {208std::lock_guard<std::recursive_mutex> guard(m_mutex);209210if (new_location)211*new_location = false;212BreakpointLocationSP bp_loc_sp(FindByAddress(addr));213if (!bp_loc_sp) {214bp_loc_sp = Create(addr, resolve_indirect_symbols);215if (bp_loc_sp) {216bp_loc_sp->ResolveBreakpointSite();217218if (new_location)219*new_location = true;220if (m_new_location_recorder) {221m_new_location_recorder->Add(bp_loc_sp);222}223}224}225return bp_loc_sp;226}227228void BreakpointLocationList::SwapLocation(229BreakpointLocationSP to_location_sp,230BreakpointLocationSP from_location_sp) {231if (!from_location_sp || !to_location_sp)232return;233234m_address_to_location.erase(to_location_sp->GetAddress());235to_location_sp->SwapLocation(from_location_sp);236RemoveLocation(from_location_sp);237m_address_to_location[to_location_sp->GetAddress()] = to_location_sp;238to_location_sp->ResolveBreakpointSite();239}240241bool BreakpointLocationList::RemoveLocation(242const lldb::BreakpointLocationSP &bp_loc_sp) {243if (bp_loc_sp) {244std::lock_guard<std::recursive_mutex> guard(m_mutex);245246m_address_to_location.erase(bp_loc_sp->GetAddress());247248size_t num_locations = m_locations.size();249for (size_t idx = 0; idx < num_locations; idx++) {250if (m_locations[idx].get() == bp_loc_sp.get()) {251RemoveLocationByIndex(idx);252return true;253}254}255}256return false;257}258259void BreakpointLocationList::RemoveLocationByIndex(size_t idx) {260assert (idx < m_locations.size());261m_address_to_location.erase(m_locations[idx]->GetAddress());262m_locations.erase(m_locations.begin() + idx);263}264265void BreakpointLocationList::RemoveInvalidLocations(const ArchSpec &arch) {266std::lock_guard<std::recursive_mutex> guard(m_mutex);267size_t idx = 0;268// Don't cache m_location.size() as it will change since we might remove269// locations from our vector...270while (idx < m_locations.size()) {271BreakpointLocation *bp_loc = m_locations[idx].get();272if (bp_loc->GetAddress().SectionWasDeleted()) {273// Section was deleted which means this breakpoint comes from a module274// that is no longer valid, so we should remove it.275RemoveLocationByIndex(idx);276continue;277}278if (arch.IsValid()) {279ModuleSP module_sp(bp_loc->GetAddress().GetModule());280if (module_sp) {281if (!arch.IsCompatibleMatch(module_sp->GetArchitecture())) {282// The breakpoint was in a module whose architecture is no longer283// compatible with "arch", so we need to remove it284RemoveLocationByIndex(idx);285continue;286}287}288}289// Only increment the index if we didn't remove the locations at index290// "idx"291++idx;292}293}294295void BreakpointLocationList::StartRecordingNewLocations(296BreakpointLocationCollection &new_locations) {297std::lock_guard<std::recursive_mutex> guard(m_mutex);298assert(m_new_location_recorder == nullptr);299m_new_location_recorder = &new_locations;300}301302void BreakpointLocationList::StopRecordingNewLocations() {303std::lock_guard<std::recursive_mutex> guard(m_mutex);304m_new_location_recorder = nullptr;305}306307void BreakpointLocationList::Compact() {308lldb::break_id_t highest_id = 0;309310for (BreakpointLocationSP loc_sp : m_locations) {311lldb::break_id_t cur_id = loc_sp->GetID();312if (cur_id > highest_id)313highest_id = cur_id;314}315m_next_id = highest_id;316}317318319