xref: /freebsd/contrib/llvm-project/lldb/source/Breakpoint/BreakpointSite.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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/Breakpoint/BreakpointSiteList.h"
160b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace lldb;
190b57cec5SDimitry Andric using namespace lldb_private;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric BreakpointSite::BreakpointSite(BreakpointSiteList *list,
220b57cec5SDimitry Andric                                const BreakpointLocationSP &owner,
230b57cec5SDimitry Andric                                lldb::addr_t addr, bool use_hardware)
24e8d8bef9SDimitry Andric     : StoppointSite(GetNextID(), addr, 0, use_hardware),
250b57cec5SDimitry Andric       m_type(eSoftware), // Process subclasses need to set this correctly using
260b57cec5SDimitry Andric                          // SetType()
270b57cec5SDimitry Andric       m_saved_opcode(), m_trap_opcode(),
28*81ad6265SDimitry Andric       m_enabled(false) // Need to create it disabled, so the first enable turns
290b57cec5SDimitry Andric                        // it on.
30*81ad6265SDimitry Andric {
310b57cec5SDimitry Andric   m_owners.Add(owner);
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric BreakpointSite::~BreakpointSite() {
350b57cec5SDimitry Andric   BreakpointLocationSP bp_loc_sp;
360b57cec5SDimitry Andric   const size_t owner_count = m_owners.GetSize();
370b57cec5SDimitry Andric   for (size_t i = 0; i < owner_count; i++) {
380b57cec5SDimitry Andric     m_owners.GetByIndex(i)->ClearBreakpointSite();
390b57cec5SDimitry Andric   }
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric break_id_t BreakpointSite::GetNextID() {
430b57cec5SDimitry Andric   static break_id_t g_next_id = 0;
440b57cec5SDimitry Andric   return ++g_next_id;
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric // RETURNS - true if we should stop at this breakpoint, false if we
480b57cec5SDimitry Andric // should continue.
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric bool BreakpointSite::ShouldStop(StoppointCallbackContext *context) {
51e8d8bef9SDimitry Andric   m_hit_counter.Increment();
520b57cec5SDimitry Andric   // ShouldStop can do a lot of work, and might even come come back and hit
530b57cec5SDimitry Andric   // this breakpoint site again.  So don't hold the m_owners_mutex the whole
540b57cec5SDimitry Andric   // while.  Instead make a local copy of the collection and call ShouldStop on
550b57cec5SDimitry Andric   // the copy.
560b57cec5SDimitry Andric   BreakpointLocationCollection owners_copy;
570b57cec5SDimitry Andric   {
580b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
590b57cec5SDimitry Andric     owners_copy = m_owners;
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric   return owners_copy.ShouldStop(context);
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric bool BreakpointSite::IsBreakpointAtThisSite(lldb::break_id_t bp_id) {
650b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
660b57cec5SDimitry Andric   const size_t owner_count = m_owners.GetSize();
670b57cec5SDimitry Andric   for (size_t i = 0; i < owner_count; i++) {
680b57cec5SDimitry Andric     if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
690b57cec5SDimitry Andric       return true;
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric   return false;
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric void BreakpointSite::Dump(Stream *s) const {
750b57cec5SDimitry Andric   if (s == nullptr)
760b57cec5SDimitry Andric     return;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64
790b57cec5SDimitry Andric             "  type = %s breakpoint  hw_index = %i  hit_count = %-4u",
800b57cec5SDimitry Andric             GetID(), (uint64_t)m_addr, IsHardware() ? "hardware" : "software",
810b57cec5SDimitry Andric             GetHardwareIndex(), GetHitCount());
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric void BreakpointSite::GetDescription(Stream *s, lldb::DescriptionLevel level) {
850b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
860b57cec5SDimitry Andric   if (level != lldb::eDescriptionLevelBrief)
870b57cec5SDimitry Andric     s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(),
880b57cec5SDimitry Andric               GetLoadAddress());
890b57cec5SDimitry Andric   m_owners.GetDescription(s, level);
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric bool BreakpointSite::IsInternal() const { return m_owners.IsInternal(); }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric uint8_t *BreakpointSite::GetTrapOpcodeBytes() { return &m_trap_opcode[0]; }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const {
970b57cec5SDimitry Andric   return &m_trap_opcode[0];
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric size_t BreakpointSite::GetTrapOpcodeMaxByteSize() const {
1010b57cec5SDimitry Andric   return sizeof(m_trap_opcode);
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric bool BreakpointSite::SetTrapOpcode(const uint8_t *trap_opcode,
1050b57cec5SDimitry Andric                                    uint32_t trap_opcode_size) {
1060b57cec5SDimitry Andric   if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode)) {
1070b57cec5SDimitry Andric     m_byte_size = trap_opcode_size;
1080b57cec5SDimitry Andric     ::memcpy(m_trap_opcode, trap_opcode, trap_opcode_size);
1090b57cec5SDimitry Andric     return true;
1100b57cec5SDimitry Andric   }
1110b57cec5SDimitry Andric   m_byte_size = 0;
1120b57cec5SDimitry Andric   return false;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric uint8_t *BreakpointSite::GetSavedOpcodeBytes() { return &m_saved_opcode[0]; }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric const uint8_t *BreakpointSite::GetSavedOpcodeBytes() const {
1180b57cec5SDimitry Andric   return &m_saved_opcode[0];
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric bool BreakpointSite::IsEnabled() const { return m_enabled; }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric void BreakpointSite::AddOwner(const BreakpointLocationSP &owner) {
1260b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
1270b57cec5SDimitry Andric   m_owners.Add(owner);
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric size_t BreakpointSite::RemoveOwner(lldb::break_id_t break_id,
1310b57cec5SDimitry Andric                                    lldb::break_id_t break_loc_id) {
1320b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
1330b57cec5SDimitry Andric   m_owners.Remove(break_id, break_loc_id);
1340b57cec5SDimitry Andric   return m_owners.GetSize();
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric size_t BreakpointSite::GetNumberOfOwners() {
1380b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
1390b57cec5SDimitry Andric   return m_owners.GetSize();
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric BreakpointLocationSP BreakpointSite::GetOwnerAtIndex(size_t index) {
1430b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
1440b57cec5SDimitry Andric   return m_owners.GetByIndex(index);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
147fe6060f1SDimitry Andric bool BreakpointSite::ValidForThisThread(Thread &thread) {
1480b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
1490b57cec5SDimitry Andric   return m_owners.ValidForThisThread(thread);
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric void BreakpointSite::BumpHitCounts() {
1530b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
1540b57cec5SDimitry Andric   for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
1550b57cec5SDimitry Andric     loc_sp->BumpHitCount();
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric bool BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size,
1600b57cec5SDimitry Andric                                      lldb::addr_t *intersect_addr,
1610b57cec5SDimitry Andric                                      size_t *intersect_size,
1620b57cec5SDimitry Andric                                      size_t *opcode_offset) const {
163e8d8bef9SDimitry Andric   // The function should be called only for software breakpoints.
164e8d8bef9SDimitry Andric   lldbassert(GetType() == Type::eSoftware);
165e8d8bef9SDimitry Andric 
166e8d8bef9SDimitry Andric   if (m_byte_size == 0)
167e8d8bef9SDimitry Andric     return false;
168e8d8bef9SDimitry Andric 
1690b57cec5SDimitry Andric   const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
1700b57cec5SDimitry Andric   const lldb::addr_t end_addr = addr + size;
1710b57cec5SDimitry Andric   // Is the breakpoint end address before the passed in start address?
1720b57cec5SDimitry Andric   if (bp_end_addr <= addr)
1730b57cec5SDimitry Andric     return false;
174e8d8bef9SDimitry Andric 
1750b57cec5SDimitry Andric   // Is the breakpoint start address after passed in end address?
1760b57cec5SDimitry Andric   if (end_addr <= m_addr)
1770b57cec5SDimitry Andric     return false;
178e8d8bef9SDimitry Andric 
1790b57cec5SDimitry Andric   if (intersect_addr || intersect_size || opcode_offset) {
1800b57cec5SDimitry Andric     if (m_addr < addr) {
1810b57cec5SDimitry Andric       if (intersect_addr)
1820b57cec5SDimitry Andric         *intersect_addr = addr;
1830b57cec5SDimitry Andric       if (intersect_size)
1840b57cec5SDimitry Andric         *intersect_size =
1850b57cec5SDimitry Andric             std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
1860b57cec5SDimitry Andric       if (opcode_offset)
1870b57cec5SDimitry Andric         *opcode_offset = addr - m_addr;
1880b57cec5SDimitry Andric     } else {
1890b57cec5SDimitry Andric       if (intersect_addr)
1900b57cec5SDimitry Andric         *intersect_addr = m_addr;
1910b57cec5SDimitry Andric       if (intersect_size)
1920b57cec5SDimitry Andric         *intersect_size =
1930b57cec5SDimitry Andric             std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
1940b57cec5SDimitry Andric       if (opcode_offset)
1950b57cec5SDimitry Andric         *opcode_offset = 0;
1960b57cec5SDimitry Andric     }
1970b57cec5SDimitry Andric   }
1980b57cec5SDimitry Andric   return true;
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric size_t
2020b57cec5SDimitry Andric BreakpointSite::CopyOwnersList(BreakpointLocationCollection &out_collection) {
2030b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
2040b57cec5SDimitry Andric   for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
2050b57cec5SDimitry Andric     out_collection.Add(loc_sp);
2060b57cec5SDimitry Andric   }
2070b57cec5SDimitry Andric   return out_collection.GetSize();
2080b57cec5SDimitry Andric }
209