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

source code of lldb/source/Breakpoint/BreakpointSite.cpp