1 //===-- BreakpointSite.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/Breakpoint/BreakpointSite.h"
14 #include "lldb/Breakpoint/Breakpoint.h"
15 #include "lldb/Breakpoint/BreakpointLocation.h"
16 #include "lldb/Breakpoint/BreakpointSiteList.h"
17 #include "lldb/Utility/Stream.h"
20 using namespace lldb_private;
22 BreakpointSite::BreakpointSite(BreakpointSiteList *list,
23 const BreakpointLocationSP &owner,
24 lldb::addr_t addr, bool use_hardware)
25 : StoppointLocation(GetNextID(), addr, 0, use_hardware),
26 m_type(eSoftware), // Process subclasses need to set this correctly using
28 m_saved_opcode(), m_trap_opcode(),
29 m_enabled(false), // Need to create it disabled, so the first enable turns
31 m_owners(), m_owners_mutex() {
35 BreakpointSite::~BreakpointSite() {
36 BreakpointLocationSP bp_loc_sp;
37 const size_t owner_count = m_owners.GetSize();
38 for (size_t i = 0; i < owner_count; i++) {
39 m_owners.GetByIndex(i)->ClearBreakpointSite();
43 break_id_t BreakpointSite::GetNextID() {
44 static break_id_t g_next_id = 0;
48 // RETURNS - true if we should stop at this breakpoint, false if we
51 bool BreakpointSite::ShouldStop(StoppointCallbackContext *context) {
52 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
54 return m_owners.ShouldStop(context);
57 bool BreakpointSite::IsBreakpointAtThisSite(lldb::break_id_t bp_id) {
58 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
59 const size_t owner_count = m_owners.GetSize();
60 for (size_t i = 0; i < owner_count; i++) {
61 if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
67 void BreakpointSite::Dump(Stream *s) const {
71 s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64
72 " type = %s breakpoint hw_index = %i hit_count = %-4u",
73 GetID(), (uint64_t)m_addr, IsHardware() ? "hardware" : "software",
74 GetHardwareIndex(), GetHitCount());
77 void BreakpointSite::GetDescription(Stream *s, lldb::DescriptionLevel level) {
78 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
79 if (level != lldb::eDescriptionLevelBrief)
80 s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(),
82 m_owners.GetDescription(s, level);
85 bool BreakpointSite::IsInternal() const { return m_owners.IsInternal(); }
87 uint8_t *BreakpointSite::GetTrapOpcodeBytes() { return &m_trap_opcode[0]; }
89 const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const {
90 return &m_trap_opcode[0];
93 size_t BreakpointSite::GetTrapOpcodeMaxByteSize() const {
94 return sizeof(m_trap_opcode);
97 bool BreakpointSite::SetTrapOpcode(const uint8_t *trap_opcode,
98 uint32_t trap_opcode_size) {
99 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode)) {
100 m_byte_size = trap_opcode_size;
101 ::memcpy(m_trap_opcode, trap_opcode, trap_opcode_size);
108 uint8_t *BreakpointSite::GetSavedOpcodeBytes() { return &m_saved_opcode[0]; }
110 const uint8_t *BreakpointSite::GetSavedOpcodeBytes() const {
111 return &m_saved_opcode[0];
114 bool BreakpointSite::IsEnabled() const { return m_enabled; }
116 void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; }
118 void BreakpointSite::AddOwner(const BreakpointLocationSP &owner) {
119 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
123 size_t BreakpointSite::RemoveOwner(lldb::break_id_t break_id,
124 lldb::break_id_t break_loc_id) {
125 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
126 m_owners.Remove(break_id, break_loc_id);
127 return m_owners.GetSize();
130 size_t BreakpointSite::GetNumberOfOwners() {
131 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
132 return m_owners.GetSize();
135 BreakpointLocationSP BreakpointSite::GetOwnerAtIndex(size_t index) {
136 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
137 return m_owners.GetByIndex(index);
140 bool BreakpointSite::ValidForThisThread(Thread *thread) {
141 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
142 return m_owners.ValidForThisThread(thread);
145 void BreakpointSite::BumpHitCounts() {
146 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
147 for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
148 loc_sp->BumpHitCount();
152 bool BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size,
153 lldb::addr_t *intersect_addr,
154 size_t *intersect_size,
155 size_t *opcode_offset) const {
156 // We only use software traps for software breakpoints
158 if (m_byte_size > 0) {
159 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
160 const lldb::addr_t end_addr = addr + size;
161 // Is the breakpoint end address before the passed in start address?
162 if (bp_end_addr <= addr)
164 // Is the breakpoint start address after passed in end address?
165 if (end_addr <= m_addr)
167 if (intersect_addr || intersect_size || opcode_offset) {
170 *intersect_addr = addr;
173 std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
175 *opcode_offset = addr - m_addr;
178 *intersect_addr = m_addr;
181 std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
193 BreakpointSite::CopyOwnersList(BreakpointLocationCollection &out_collection) {
194 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
195 for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
196 out_collection.Add(loc_sp);
198 return out_collection.GetSize();