brintos

brintos / llvm-project-archived public Read only

0
0
Text · 10.7 KiB · ea431aa Raw
327 lines · cpp
1//===-- BreakpointLocationList.cpp ----------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Breakpoint/BreakpointLocationList.h"10 11#include "lldb/Breakpoint/Breakpoint.h"12#include "lldb/Breakpoint/BreakpointLocation.h"13#include "lldb/Core/Module.h"14#include "lldb/Core/Section.h"15#include "lldb/Target/SectionLoadList.h"16#include "lldb/Target/Target.h"17#include "lldb/Utility/ArchSpec.h"18#include "lldb/Utility/LLDBLog.h"19#include "lldb/Utility/Log.h"20 21using namespace lldb;22using namespace lldb_private;23 24BreakpointLocationList::BreakpointLocationList(Breakpoint &owner)25    : m_owner(owner), m_next_id(0), m_new_location_recorder(nullptr) {}26 27BreakpointLocationList::~BreakpointLocationList() = default;28 29BreakpointLocationSP30BreakpointLocationList::Create(const Address &addr,31                               bool resolve_indirect_symbols) {32  std::lock_guard<std::recursive_mutex> guard(m_mutex);33  // The location ID is just the size of the location list + 134  lldb::break_id_t bp_loc_id = ++m_next_id;35  BreakpointLocationSP bp_loc_sp(36      new BreakpointLocation(bp_loc_id, m_owner, addr, LLDB_INVALID_THREAD_ID,37                             resolve_indirect_symbols));38  m_locations.push_back(bp_loc_sp);39  m_address_to_location[addr] = bp_loc_sp;40  return bp_loc_sp;41}42 43bool BreakpointLocationList::ShouldStop(StoppointCallbackContext *context,44                                        lldb::break_id_t break_id,45                                        lldb::BreakpointLocationSP &bp_loc_sp) {46  BreakpointLocationSP bp = FindByID(break_id);47  if (bp) {48    // Let the BreakpointLocation decide if it should stop here (could not have49    // reached it's target hit count yet, or it could have a callback that50    // decided it shouldn't stop (shared library loads/unloads).51    return bp->ShouldStop(context, bp_loc_sp);52  }53  // We should stop here since this BreakpointLocation isn't valid anymore or54  // it doesn't exist.55  return true;56}57 58lldb::break_id_t BreakpointLocationList::FindIDByAddress(const Address &addr) {59  BreakpointLocationSP bp_loc_sp = FindByAddress(addr);60  if (bp_loc_sp) {61    return bp_loc_sp->GetID();62  }63  return LLDB_INVALID_BREAK_ID;64}65 66static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val) {67  return lhs->GetID() < val;68}69 70BreakpointLocationSP71BreakpointLocationList::FindByID(lldb::break_id_t break_id) const {72  std::lock_guard<std::recursive_mutex> guard(m_mutex);73  collection::const_iterator end = m_locations.end();74  collection::const_iterator pos =75      llvm::lower_bound(m_locations, break_id, Compare);76  if (pos != end && (*pos)->GetID() == break_id)77    return *(pos);78  return BreakpointLocationSP();79}80 81size_t BreakpointLocationList::FindInModule(82    Module *module, BreakpointLocationCollection &bp_loc_list) {83  std::lock_guard<std::recursive_mutex> guard(m_mutex);84  const size_t orig_size = bp_loc_list.GetSize();85  collection::iterator pos, end = m_locations.end();86 87  for (pos = m_locations.begin(); pos != end; ++pos) {88    BreakpointLocationSP break_loc = (*pos);89    SectionSP section_sp(break_loc->GetAddress().GetSection());90    if (section_sp && section_sp->GetModule().get() == module) {91      bp_loc_list.Add(break_loc);92    }93  }94  return bp_loc_list.GetSize() - orig_size;95}96 97const BreakpointLocationSP98BreakpointLocationList::FindByAddress(const Address &addr) const {99  std::lock_guard<std::recursive_mutex> guard(m_mutex);100  BreakpointLocationSP bp_loc_sp;101  if (!m_locations.empty()) {102    Address so_addr;103 104    if (addr.IsSectionOffset()) {105      so_addr = addr;106    } else {107      // Try and resolve as a load address if possible.108      m_owner.GetTarget().ResolveLoadAddress(addr.GetOffset(), so_addr);109      if (!so_addr.IsValid()) {110        // The address didn't resolve, so just set to passed in addr.111        so_addr = addr;112      }113    }114 115    addr_map::const_iterator pos = m_address_to_location.find(so_addr);116    if (pos != m_address_to_location.end())117      bp_loc_sp = pos->second;118  }119 120  return bp_loc_sp;121}122 123void BreakpointLocationList::Dump(Stream *s) const {124  s->Printf("%p: ", static_cast<const void *>(this));125  // s->Indent();126  std::lock_guard<std::recursive_mutex> guard(m_mutex);127  s->Printf("BreakpointLocationList with %" PRIu64 " BreakpointLocations:\n",128            (uint64_t)m_locations.size());129  s->IndentMore();130  collection::const_iterator pos, end = m_locations.end();131  for (pos = m_locations.begin(); pos != end; ++pos)132    (*pos)->Dump(s);133  s->IndentLess();134}135 136BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) {137  std::lock_guard<std::recursive_mutex> guard(m_mutex);138  BreakpointLocationSP bp_loc_sp;139  if (i < m_locations.size())140    bp_loc_sp = m_locations[i];141 142  return bp_loc_sp;143}144 145const BreakpointLocationSP BreakpointLocationList::GetByIndex(size_t i) const {146  std::lock_guard<std::recursive_mutex> guard(m_mutex);147  BreakpointLocationSP bp_loc_sp;148  if (i < m_locations.size())149    bp_loc_sp = m_locations[i];150 151  return bp_loc_sp;152}153 154void BreakpointLocationList::ClearAllBreakpointSites() {155  std::lock_guard<std::recursive_mutex> guard(m_mutex);156  collection::iterator pos, end = m_locations.end();157  Log *log = GetLog(LLDBLog::Breakpoints);158 159  for (pos = m_locations.begin(); pos != end; ++pos) {160    if (llvm::Error error = (*pos)->ClearBreakpointSite())161      LLDB_LOG_ERROR(log, std::move(error), "{0}");162  }163}164 165void BreakpointLocationList::ResolveAllBreakpointSites() {166  std::lock_guard<std::recursive_mutex> guard(m_mutex);167  collection::iterator pos, end = m_locations.end();168  Log *log = GetLog(LLDBLog::Breakpoints);169 170  for (pos = m_locations.begin(); pos != end; ++pos) {171    if ((*pos)->IsEnabled()) {172      if (llvm::Error error = (*pos)->ResolveBreakpointSite())173        LLDB_LOG_ERROR(log, std::move(error), "{0}");174    }175  }176}177 178uint32_t BreakpointLocationList::GetHitCount() const {179  uint32_t hit_count = 0;180  std::lock_guard<std::recursive_mutex> guard(m_mutex);181  collection::const_iterator pos, end = m_locations.end();182  for (pos = m_locations.begin(); pos != end; ++pos)183    hit_count += (*pos)->GetHitCount();184  return hit_count;185}186 187void BreakpointLocationList::ResetHitCount() {188  std::lock_guard<std::recursive_mutex> guard(m_mutex);189  for (auto &loc : m_locations)190    loc->ResetHitCount();191}192 193size_t BreakpointLocationList::GetNumResolvedLocations() const {194  std::lock_guard<std::recursive_mutex> guard(m_mutex);195  size_t resolve_count = 0;196  collection::const_iterator pos, end = m_locations.end();197  for (pos = m_locations.begin(); pos != end; ++pos) {198    if ((*pos)->IsResolved())199      ++resolve_count;200  }201  return resolve_count;202}203 204void BreakpointLocationList::GetDescription(Stream *s,205                                            lldb::DescriptionLevel level) {206  std::lock_guard<std::recursive_mutex> guard(m_mutex);207  collection::iterator pos, end = m_locations.end();208 209  for (pos = m_locations.begin(); pos != end; ++pos) {210    s->Printf(" ");211    (*pos)->GetDescription(s, level);212  }213}214 215BreakpointLocationSP BreakpointLocationList::AddLocation(216    const Address &addr, bool resolve_indirect_symbols, bool *new_location) {217  std::lock_guard<std::recursive_mutex> guard(m_mutex);218 219  if (new_location)220    *new_location = false;221  BreakpointLocationSP bp_loc_sp(FindByAddress(addr));222  if (!bp_loc_sp) {223    bp_loc_sp = Create(addr, resolve_indirect_symbols);224    if (bp_loc_sp) {225      if (llvm::Error error = bp_loc_sp->ResolveBreakpointSite())226        LLDB_LOG_ERROR(GetLog(LLDBLog::Breakpoints), std::move(error), "{0}");227 228      if (new_location)229        *new_location = true;230      if (m_new_location_recorder) {231        m_new_location_recorder->Add(bp_loc_sp);232      }233    }234  }235  return bp_loc_sp;236}237 238void BreakpointLocationList::SwapLocation(239    BreakpointLocationSP to_location_sp,240    BreakpointLocationSP from_location_sp) {241  if (!from_location_sp || !to_location_sp)242    return;243 244  m_address_to_location.erase(to_location_sp->GetAddress());245  to_location_sp->SwapLocation(from_location_sp);246  RemoveLocation(from_location_sp);247  m_address_to_location[to_location_sp->GetAddress()] = to_location_sp;248  llvm::consumeError(to_location_sp->ResolveBreakpointSite());249}250 251bool BreakpointLocationList::RemoveLocation(252    const lldb::BreakpointLocationSP &bp_loc_sp) {253  if (bp_loc_sp) {254    std::lock_guard<std::recursive_mutex> guard(m_mutex);255 256    m_address_to_location.erase(bp_loc_sp->GetAddress());257 258    size_t num_locations = m_locations.size();259    for (size_t idx = 0; idx < num_locations; idx++) {260      if (m_locations[idx].get() == bp_loc_sp.get()) {261        RemoveLocationByIndex(idx);262        return true;263      }264    }265  }266  return false;267}268 269void BreakpointLocationList::RemoveLocationByIndex(size_t idx) {270  assert (idx < m_locations.size());271  m_address_to_location.erase(m_locations[idx]->GetAddress());272  m_locations.erase(m_locations.begin() + idx);273}274 275void BreakpointLocationList::RemoveInvalidLocations(const ArchSpec &arch) {276  std::lock_guard<std::recursive_mutex> guard(m_mutex);277  size_t idx = 0;278  // Don't cache m_location.size() as it will change since we might remove279  // locations from our vector...280  while (idx < m_locations.size()) {281    BreakpointLocation *bp_loc = m_locations[idx].get();282    if (bp_loc->GetAddress().SectionWasDeleted()) {283      // Section was deleted which means this breakpoint comes from a module284      // that is no longer valid, so we should remove it.285      RemoveLocationByIndex(idx);286      continue;287    }288    if (arch.IsValid()) {289      ModuleSP module_sp(bp_loc->GetAddress().GetModule());290      if (module_sp) {291        if (!arch.IsCompatibleMatch(module_sp->GetArchitecture())) {292          // The breakpoint was in a module whose architecture is no longer293          // compatible with "arch", so we need to remove it294          RemoveLocationByIndex(idx);295          continue;296        }297      }298    }299    // Only increment the index if we didn't remove the locations at index300    // "idx"301    ++idx;302  }303}304 305void BreakpointLocationList::StartRecordingNewLocations(306    BreakpointLocationCollection &new_locations) {307  std::lock_guard<std::recursive_mutex> guard(m_mutex);308  assert(m_new_location_recorder == nullptr);309  m_new_location_recorder = &new_locations;310}311 312void BreakpointLocationList::StopRecordingNewLocations() {313  std::lock_guard<std::recursive_mutex> guard(m_mutex);314  m_new_location_recorder = nullptr;315}316 317void BreakpointLocationList::Compact() {318  lldb::break_id_t highest_id = 0;319 320  for (BreakpointLocationSP loc_sp : m_locations) {321    lldb::break_id_t cur_id = loc_sp->GetID();322    if (cur_id > highest_id)323      highest_id = cur_id;324  }325  m_next_id = highest_id;326}327