15ffd83dbSDimitry Andric //===-- BreakpointSite.cpp ------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 9fe6060f1SDimitry Andric #include <cinttypes> 100b57cec5SDimitry Andric 110b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointSite.h" 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h" 140b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h" 150b57cec5SDimitry Andric #include "lldb/Utility/Stream.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric using namespace lldb; 180b57cec5SDimitry Andric using namespace lldb_private; 190b57cec5SDimitry Andric 20*5f757f3fSDimitry Andric BreakpointSite::BreakpointSite(const BreakpointLocationSP &constituent, 210b57cec5SDimitry Andric lldb::addr_t addr, bool use_hardware) 22e8d8bef9SDimitry Andric : StoppointSite(GetNextID(), addr, 0, use_hardware), 230b57cec5SDimitry Andric m_type(eSoftware), // Process subclasses need to set this correctly using 240b57cec5SDimitry Andric // SetType() 250b57cec5SDimitry Andric m_saved_opcode(), m_trap_opcode(), 2681ad6265SDimitry Andric m_enabled(false) // Need to create it disabled, so the first enable turns 270b57cec5SDimitry Andric // it on. 2881ad6265SDimitry Andric { 29*5f757f3fSDimitry Andric m_constituents.Add(constituent); 300b57cec5SDimitry Andric } 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric BreakpointSite::~BreakpointSite() { 330b57cec5SDimitry Andric BreakpointLocationSP bp_loc_sp; 34*5f757f3fSDimitry Andric const size_t constituent_count = m_constituents.GetSize(); 35*5f757f3fSDimitry Andric for (size_t i = 0; i < constituent_count; i++) { 36*5f757f3fSDimitry Andric m_constituents.GetByIndex(i)->ClearBreakpointSite(); 370b57cec5SDimitry Andric } 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric break_id_t BreakpointSite::GetNextID() { 410b57cec5SDimitry Andric static break_id_t g_next_id = 0; 420b57cec5SDimitry Andric return ++g_next_id; 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric // RETURNS - true if we should stop at this breakpoint, false if we 460b57cec5SDimitry Andric // should continue. 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric bool BreakpointSite::ShouldStop(StoppointCallbackContext *context) { 49e8d8bef9SDimitry Andric m_hit_counter.Increment(); 50*5f757f3fSDimitry Andric // ShouldStop can do a lot of work, and might even come back and hit 51*5f757f3fSDimitry Andric // this breakpoint site again. So don't hold the m_constituents_mutex the 52*5f757f3fSDimitry Andric // whole while. Instead make a local copy of the collection and call 53*5f757f3fSDimitry Andric // ShouldStop on the copy. 54*5f757f3fSDimitry Andric BreakpointLocationCollection constituents_copy; 550b57cec5SDimitry Andric { 56*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 57*5f757f3fSDimitry Andric constituents_copy = m_constituents; 580b57cec5SDimitry Andric } 59*5f757f3fSDimitry Andric return constituents_copy.ShouldStop(context); 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric bool BreakpointSite::IsBreakpointAtThisSite(lldb::break_id_t bp_id) { 63*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 64*5f757f3fSDimitry Andric const size_t constituent_count = m_constituents.GetSize(); 65*5f757f3fSDimitry Andric for (size_t i = 0; i < constituent_count; i++) { 66*5f757f3fSDimitry Andric if (m_constituents.GetByIndex(i)->GetBreakpoint().GetID() == bp_id) 670b57cec5SDimitry Andric return true; 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric return false; 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric void BreakpointSite::Dump(Stream *s) const { 730b57cec5SDimitry Andric if (s == nullptr) 740b57cec5SDimitry Andric return; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64 77*5f757f3fSDimitry Andric " type = %s breakpoint hit_count = %-4u", 780b57cec5SDimitry Andric GetID(), (uint64_t)m_addr, IsHardware() ? "hardware" : "software", 79*5f757f3fSDimitry Andric GetHitCount()); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric void BreakpointSite::GetDescription(Stream *s, lldb::DescriptionLevel level) { 83*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 840b57cec5SDimitry Andric if (level != lldb::eDescriptionLevelBrief) 850b57cec5SDimitry Andric s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(), 860b57cec5SDimitry Andric GetLoadAddress()); 87*5f757f3fSDimitry Andric m_constituents.GetDescription(s, level); 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric 90*5f757f3fSDimitry Andric bool BreakpointSite::IsInternal() const { return m_constituents.IsInternal(); } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric uint8_t *BreakpointSite::GetTrapOpcodeBytes() { return &m_trap_opcode[0]; } 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const { 950b57cec5SDimitry Andric return &m_trap_opcode[0]; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric size_t BreakpointSite::GetTrapOpcodeMaxByteSize() const { 990b57cec5SDimitry Andric return sizeof(m_trap_opcode); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric bool BreakpointSite::SetTrapOpcode(const uint8_t *trap_opcode, 1030b57cec5SDimitry Andric uint32_t trap_opcode_size) { 1040b57cec5SDimitry Andric if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode)) { 1050b57cec5SDimitry Andric m_byte_size = trap_opcode_size; 1060b57cec5SDimitry Andric ::memcpy(m_trap_opcode, trap_opcode, trap_opcode_size); 1070b57cec5SDimitry Andric return true; 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric m_byte_size = 0; 1100b57cec5SDimitry Andric return false; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric uint8_t *BreakpointSite::GetSavedOpcodeBytes() { return &m_saved_opcode[0]; } 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric const uint8_t *BreakpointSite::GetSavedOpcodeBytes() const { 1160b57cec5SDimitry Andric return &m_saved_opcode[0]; 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric bool BreakpointSite::IsEnabled() const { return m_enabled; } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; } 1220b57cec5SDimitry Andric 123*5f757f3fSDimitry Andric void BreakpointSite::AddConstituent(const BreakpointLocationSP &constituent) { 124*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 125*5f757f3fSDimitry Andric m_constituents.Add(constituent); 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 128*5f757f3fSDimitry Andric size_t BreakpointSite::RemoveConstituent(lldb::break_id_t break_id, 1290b57cec5SDimitry Andric lldb::break_id_t break_loc_id) { 130*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 131*5f757f3fSDimitry Andric m_constituents.Remove(break_id, break_loc_id); 132*5f757f3fSDimitry Andric return m_constituents.GetSize(); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 135*5f757f3fSDimitry Andric size_t BreakpointSite::GetNumberOfConstituents() { 136*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 137*5f757f3fSDimitry Andric return m_constituents.GetSize(); 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 140*5f757f3fSDimitry Andric BreakpointLocationSP BreakpointSite::GetConstituentAtIndex(size_t index) { 141*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 142*5f757f3fSDimitry Andric return m_constituents.GetByIndex(index); 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 145fe6060f1SDimitry Andric bool BreakpointSite::ValidForThisThread(Thread &thread) { 146*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 147*5f757f3fSDimitry Andric return m_constituents.ValidForThisThread(thread); 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric void BreakpointSite::BumpHitCounts() { 151*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 152*5f757f3fSDimitry Andric for (BreakpointLocationSP loc_sp : m_constituents.BreakpointLocations()) { 1530b57cec5SDimitry Andric loc_sp->BumpHitCount(); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric bool BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size, 1580b57cec5SDimitry Andric lldb::addr_t *intersect_addr, 1590b57cec5SDimitry Andric size_t *intersect_size, 1600b57cec5SDimitry Andric size_t *opcode_offset) const { 161e8d8bef9SDimitry Andric // The function should be called only for software breakpoints. 162e8d8bef9SDimitry Andric lldbassert(GetType() == Type::eSoftware); 163e8d8bef9SDimitry Andric 164e8d8bef9SDimitry Andric if (m_byte_size == 0) 165e8d8bef9SDimitry Andric return false; 166e8d8bef9SDimitry Andric 1670b57cec5SDimitry Andric const lldb::addr_t bp_end_addr = m_addr + m_byte_size; 1680b57cec5SDimitry Andric const lldb::addr_t end_addr = addr + size; 1690b57cec5SDimitry Andric // Is the breakpoint end address before the passed in start address? 1700b57cec5SDimitry Andric if (bp_end_addr <= addr) 1710b57cec5SDimitry Andric return false; 172e8d8bef9SDimitry Andric 1730b57cec5SDimitry Andric // Is the breakpoint start address after passed in end address? 1740b57cec5SDimitry Andric if (end_addr <= m_addr) 1750b57cec5SDimitry Andric return false; 176e8d8bef9SDimitry Andric 1770b57cec5SDimitry Andric if (intersect_addr || intersect_size || opcode_offset) { 1780b57cec5SDimitry Andric if (m_addr < addr) { 1790b57cec5SDimitry Andric if (intersect_addr) 1800b57cec5SDimitry Andric *intersect_addr = addr; 1810b57cec5SDimitry Andric if (intersect_size) 1820b57cec5SDimitry Andric *intersect_size = 1830b57cec5SDimitry Andric std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr; 1840b57cec5SDimitry Andric if (opcode_offset) 1850b57cec5SDimitry Andric *opcode_offset = addr - m_addr; 1860b57cec5SDimitry Andric } else { 1870b57cec5SDimitry Andric if (intersect_addr) 1880b57cec5SDimitry Andric *intersect_addr = m_addr; 1890b57cec5SDimitry Andric if (intersect_size) 1900b57cec5SDimitry Andric *intersect_size = 1910b57cec5SDimitry Andric std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr; 1920b57cec5SDimitry Andric if (opcode_offset) 1930b57cec5SDimitry Andric *opcode_offset = 0; 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric return true; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 199*5f757f3fSDimitry Andric size_t BreakpointSite::CopyConstituentsList( 200*5f757f3fSDimitry Andric BreakpointLocationCollection &out_collection) { 201*5f757f3fSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex); 202*5f757f3fSDimitry Andric for (BreakpointLocationSP loc_sp : m_constituents.BreakpointLocations()) { 2030b57cec5SDimitry Andric out_collection.Add(loc_sp); 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric return out_collection.GetSize(); 2060b57cec5SDimitry Andric } 207