LLDB mainline
BreakpointSite.cpp
Go to the documentation of this file.
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
12
15#include "lldb/Target/Thread.h"
16#include "lldb/Utility/Stream.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
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()
27 m_enabled(false) // Need to create it disabled, so the first enable turns
28 // it on.
29{
30 m_constituents.Add(constituent);
31}
32
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
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
50 BreakpointLocationCollection &stopping_bp_locs) {
51 m_hit_counter.Increment();
52 // ShouldStop can do a lot of work, and might even come back and hit
53 // this breakpoint site again. So don't hold the m_constituents_mutex the
54 // whole while. Instead make a local copy of the collection and call
55 // 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
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
75 if (s == nullptr)
76 return;
77
78 s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64
79 " type = %s breakpoint hit_count = %-4u",
80 GetID(), (uint64_t)m_addr, IsHardware() ? "hardware" : "software",
81 GetHitCount());
82}
83
85 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
87 s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(),
89 m_constituents.GetDescription(s, level);
90}
91
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 else
103 result = loc_frame_index;
104 }
105 }
106 return result;
107}
108
109bool BreakpointSite::IsInternal() const { return m_constituents.IsInternal(); }
110
112
114 return &m_trap_opcode[0];
115}
116
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
133
135 return &m_saved_opcode[0];
136}
137
138void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; }
139
141 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
142 m_constituents.Add(constituent);
143}
144
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
153 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
154 return m_constituents.GetSize();
155}
156
158 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
159 return m_constituents.GetByIndex(index);
160}
161
163 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
164 if (ThreadSP backed_thread = thread.GetBackedThread())
165 return m_constituents.ValidForThisThread(*backed_thread);
166 return m_constituents.ValidForThisThread(thread);
167}
168
170 if (ThreadSP backed_thread = thread.GetBackedThread())
171 return ContainsUserBreakpointForThread(*backed_thread);
172
173 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
174 for (const BreakpointLocationSP &bp_loc :
175 m_constituents.BreakpointLocations()) {
176 const Breakpoint &bp = bp_loc->GetBreakpoint();
177 if (bp.IsInternal())
178 continue;
179 if (bp_loc->ValidForThisThread(thread))
180 return true;
181 }
182 return false;
183}
184
186 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
187 for (BreakpointLocationSP loc_sp : m_constituents.BreakpointLocations()) {
188 loc_sp->BumpHitCount();
189 }
190}
191
193 lldb::addr_t *intersect_addr,
194 size_t *intersect_size,
195 size_t *opcode_offset) const {
196 // The function should be called only for software breakpoints.
198
199 if (m_byte_size == 0)
200 return false;
201
202 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
203 const lldb::addr_t end_addr = addr + size;
204 // Is the breakpoint end address before the passed in start address?
205 if (bp_end_addr <= addr)
206 return false;
207
208 // Is the breakpoint start address after passed in end address?
209 if (end_addr <= m_addr)
210 return false;
211
212 if (intersect_addr || intersect_size || opcode_offset) {
213 if (m_addr < addr) {
214 if (intersect_addr)
215 *intersect_addr = addr;
216 if (intersect_size)
217 *intersect_size =
218 std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
219 if (opcode_offset)
220 *opcode_offset = addr - m_addr;
221 } else {
222 if (intersect_addr)
223 *intersect_addr = m_addr;
224 if (intersect_size)
225 *intersect_size =
226 std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
227 if (opcode_offset)
228 *opcode_offset = 0;
229 }
230 }
231 return true;
232}
233
235 BreakpointLocationCollection &out_collection) {
236 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
237 for (BreakpointLocationSP loc_sp : m_constituents.BreakpointLocations()) {
238 out_collection.Add(loc_sp);
239 }
240 return out_collection.GetSize();
241}
#define lldbassert(x)
Definition LLDBAssert.h:16
bool ShouldStop(StoppointCallbackContext *context, BreakpointLocationCollection &stopped_bp_locs)
Enquires of all the breakpoint locations in this list whether we should stop at a hit at breakID.
void Add(const lldb::BreakpointLocationSP &bp_loc_sp)
Add the breakpoint bp_loc_sp to the list.
size_t GetSize() const
Returns the number of elements in this breakpoint location list.
BreakpointLocationCollection m_constituents
This has the BreakpointLocations that share this breakpoint site.
bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size)
Sets the trap opcode.
static lldb::break_id_t GetNextID()
void Dump(Stream *s) const override
Standard Dump method.
size_t GetTrapOpcodeMaxByteSize() const
Get the size of the trap opcode for this address.
size_t GetNumberOfConstituents()
This method returns the number of breakpoint locations currently located at this breakpoint site.
BreakpointSite::Type m_type
The type of this breakpoint site.
BreakpointSite::Type GetType() const
std::recursive_mutex m_constituents_mutex
This mutex protects the constituents collection.
bool IntersectsRange(lldb::addr_t addr, size_t size, lldb::addr_t *intersect_addr, size_t *intersect_size, size_t *opcode_offset) const
Says whether addr and size size intersects with the address intersect_addr.
bool ShouldStop(StoppointCallbackContext *context, BreakpointLocationCollection &stopping_bp_loc) override
Enquires of the breakpoint locations that produced this breakpoint site whether we should stop at thi...
bool IsBreakpointAtThisSite(lldb::break_id_t bp_id)
Tell whether a breakpoint has a location at this site.
void AddConstituent(const lldb::BreakpointLocationSP &constituent)
The "Constituents" are the breakpoint locations that share this breakpoint site.
uint8_t * GetTrapOpcodeBytes()
Returns the Opcode Bytes for this breakpoint.
bool IsInternal() const
Tell whether ALL the breakpoints in the location collection are internal.
bool ValidForThisThread(Thread &thread)
Check whether the constituents of this breakpoint site have any thread specifiers,...
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Print a description of this breakpoint site to the stream s.
BreakpointSite(const lldb::BreakpointLocationSP &constituent, lldb::addr_t m_addr, bool use_hardware)
uint8_t m_trap_opcode[8]
The opcode that was used to create the breakpoint if it is a software breakpoint site.
size_t RemoveConstituent(lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
The method removes the constituent at break_loc_id from this breakpoint list.
uint8_t * GetSavedOpcodeBytes()
Gets the original instruction bytes that were overwritten by the trap.
bool ContainsUserBreakpointForThread(Thread &thread)
Returns true if at least one constituent is both public and valid for thread.
lldb::BreakpointLocationSP GetConstituentAtIndex(size_t idx)
This method returns the breakpoint location at index index located at this breakpoint site.
bool IsHardware() const override
bool m_enabled
Boolean indicating if this breakpoint site enabled or not.
std::optional< uint32_t > GetSuggestedStackFrameIndex()
uint8_t m_saved_opcode[8]
The saved opcode bytes if this breakpoint site uses trap opcodes.
size_t CopyConstituentsList(BreakpointLocationCollection &out_collection)
This method copies the breakpoint site's constituents into a new collection.
void SetEnabled(bool enabled)
Sets whether the current breakpoint site is enabled or not.
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition Breakpoint.h:81
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
lldb::addr_t m_addr
The load address of this stop point.
StoppointSite(lldb::break_id_t bid, lldb::addr_t m_addr, bool hardware)
lldb::break_id_t GetID() const
uint32_t GetHitCount() const
StoppointHitCounter m_hit_counter
Number of times this breakpoint/watchpoint has been hit.
virtual lldb::addr_t GetLoadAddress() const
uint32_t m_byte_size
The size in bytes of stoppoint, e.g.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
std::shared_ptr< lldb_private::Thread > ThreadSP
int32_t break_id_t
Definition lldb-types.h:87
uint64_t addr_t
Definition lldb-types.h:80