228 lines · cpp
1//===-- BreakpointSite.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 <cinttypes>10 11#include "lldb/Breakpoint/BreakpointSite.h"12 13#include "lldb/Breakpoint/Breakpoint.h"14#include "lldb/Breakpoint/BreakpointLocation.h"15#include "lldb/Target/Thread.h"16#include "lldb/Utility/Stream.h"17 18using namespace lldb;19using namespace lldb_private;20 21BreakpointSite::BreakpointSite(const BreakpointLocationSP &constituent,22 lldb::addr_t addr, bool use_hardware)23 : StoppointSite(GetNextID(), addr, 0, use_hardware),24 m_type(eSoftware), // Process subclasses need to set this correctly using25 // SetType()26 m_saved_opcode(), m_trap_opcode(),27 m_enabled(false) // Need to create it disabled, so the first enable turns28 // it on.29{30 m_constituents.Add(constituent);31}32 33BreakpointSite::~BreakpointSite() {34 BreakpointLocationSP bp_loc_sp;35 const size_t constituent_count = m_constituents.GetSize();36 for (size_t i = 0; i < constituent_count; i++)37 llvm::consumeError(m_constituents.GetByIndex(i)->ClearBreakpointSite());38}39 40break_id_t BreakpointSite::GetNextID() {41 static break_id_t g_next_id = 0;42 return ++g_next_id;43}44 45// RETURNS - true if we should stop at this breakpoint, false if we46// should continue.47 48bool BreakpointSite::ShouldStop(49 StoppointCallbackContext *context,50 BreakpointLocationCollection &stopping_bp_locs) {51 m_hit_counter.Increment();52 // ShouldStop can do a lot of work, and might even come back and hit53 // this breakpoint site again. So don't hold the m_constituents_mutex the54 // whole while. Instead make a local copy of the collection and call55 // ShouldStop on the copy.56 BreakpointLocationCollection constituents_copy;57 {58 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);59 constituents_copy = m_constituents;60 }61 return constituents_copy.ShouldStop(context, stopping_bp_locs);62}63 64bool BreakpointSite::IsBreakpointAtThisSite(lldb::break_id_t bp_id) {65 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);66 const size_t constituent_count = m_constituents.GetSize();67 for (size_t i = 0; i < constituent_count; i++) {68 if (m_constituents.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)69 return true;70 }71 return false;72}73 74void BreakpointSite::Dump(Stream *s) const {75 if (s == nullptr)76 return;77 78 s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx6479 " type = %s breakpoint hit_count = %-4u",80 GetID(), (uint64_t)m_addr, IsHardware() ? "hardware" : "software",81 GetHitCount());82}83 84void BreakpointSite::GetDescription(Stream *s, lldb::DescriptionLevel level) {85 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);86 if (level != lldb::eDescriptionLevelBrief)87 s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(),88 GetLoadAddress());89 m_constituents.GetDescription(s, level);90}91 92std::optional<uint32_t> BreakpointSite::GetSuggestedStackFrameIndex() {93 94 std::optional<uint32_t> result;95 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);96 for (BreakpointLocationSP loc_sp : m_constituents.BreakpointLocations()) {97 std::optional<uint32_t> loc_frame_index =98 loc_sp->GetSuggestedStackFrameIndex();99 if (loc_frame_index) {100 if (result)101 result = std::max(*loc_frame_index, *result);102 else103 result = loc_frame_index;104 }105 }106 return result;107}108 109bool BreakpointSite::IsInternal() const { return m_constituents.IsInternal(); }110 111uint8_t *BreakpointSite::GetTrapOpcodeBytes() { return &m_trap_opcode[0]; }112 113const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const {114 return &m_trap_opcode[0];115}116 117size_t BreakpointSite::GetTrapOpcodeMaxByteSize() const {118 return sizeof(m_trap_opcode);119}120 121bool BreakpointSite::SetTrapOpcode(const uint8_t *trap_opcode,122 uint32_t trap_opcode_size) {123 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode)) {124 m_byte_size = trap_opcode_size;125 ::memcpy(m_trap_opcode, trap_opcode, trap_opcode_size);126 return true;127 }128 m_byte_size = 0;129 return false;130}131 132uint8_t *BreakpointSite::GetSavedOpcodeBytes() { return &m_saved_opcode[0]; }133 134const uint8_t *BreakpointSite::GetSavedOpcodeBytes() const {135 return &m_saved_opcode[0];136}137 138bool BreakpointSite::IsEnabled() const { return m_enabled; }139 140void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; }141 142void BreakpointSite::AddConstituent(const BreakpointLocationSP &constituent) {143 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);144 m_constituents.Add(constituent);145}146 147size_t BreakpointSite::RemoveConstituent(lldb::break_id_t break_id,148 lldb::break_id_t break_loc_id) {149 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);150 m_constituents.Remove(break_id, break_loc_id);151 return m_constituents.GetSize();152}153 154size_t BreakpointSite::GetNumberOfConstituents() {155 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);156 return m_constituents.GetSize();157}158 159BreakpointLocationSP BreakpointSite::GetConstituentAtIndex(size_t index) {160 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);161 return m_constituents.GetByIndex(index);162}163 164bool BreakpointSite::ValidForThisThread(Thread &thread) {165 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);166 if (ThreadSP backed_thread = thread.GetBackedThread())167 return m_constituents.ValidForThisThread(*backed_thread);168 return m_constituents.ValidForThisThread(thread);169}170 171void BreakpointSite::BumpHitCounts() {172 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);173 for (BreakpointLocationSP loc_sp : m_constituents.BreakpointLocations()) {174 loc_sp->BumpHitCount();175 }176}177 178bool BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size,179 lldb::addr_t *intersect_addr,180 size_t *intersect_size,181 size_t *opcode_offset) const {182 // The function should be called only for software breakpoints.183 lldbassert(GetType() == Type::eSoftware);184 185 if (m_byte_size == 0)186 return false;187 188 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;189 const lldb::addr_t end_addr = addr + size;190 // Is the breakpoint end address before the passed in start address?191 if (bp_end_addr <= addr)192 return false;193 194 // Is the breakpoint start address after passed in end address?195 if (end_addr <= m_addr)196 return false;197 198 if (intersect_addr || intersect_size || opcode_offset) {199 if (m_addr < addr) {200 if (intersect_addr)201 *intersect_addr = addr;202 if (intersect_size)203 *intersect_size =204 std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;205 if (opcode_offset)206 *opcode_offset = addr - m_addr;207 } else {208 if (intersect_addr)209 *intersect_addr = m_addr;210 if (intersect_size)211 *intersect_size =212 std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;213 if (opcode_offset)214 *opcode_offset = 0;215 }216 }217 return true;218}219 220size_t BreakpointSite::CopyConstituentsList(221 BreakpointLocationCollection &out_collection) {222 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);223 for (BreakpointLocationSP loc_sp : m_constituents.BreakpointLocations()) {224 out_collection.Add(loc_sp);225 }226 return out_collection.GetSize();227}228