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/Utility/Stream.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
21 lldb::addr_t addr, bool use_hardware)
22 : StoppointSite(GetNextID(), addr, 0, use_hardware),
23 m_type(eSoftware), // Process subclasses need to set this correctly using
24 // SetType()
25 m_saved_opcode(), m_trap_opcode(),
26 m_enabled(false) // Need to create it disabled, so the first enable turns
27 // it on.
28{
29 m_constituents.Add(constituent);
30}
31
33 BreakpointLocationSP bp_loc_sp;
34 const size_t constituent_count = m_constituents.GetSize();
35 for (size_t i = 0; i < constituent_count; i++) {
36 m_constituents.GetByIndex(i)->ClearBreakpointSite();
37 }
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 // 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
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
73 if (s == nullptr)
74 return;
75
76 s->Printf("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
83 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
85 s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(),
88}
89
91
93
94const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const {
95 return &m_trap_opcode[0];
96}
97
99 return sizeof(m_trap_opcode);
100}
101
102bool BreakpointSite::SetTrapOpcode(const uint8_t *trap_opcode,
103 uint32_t trap_opcode_size) {
104 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode)) {
105 m_byte_size = trap_opcode_size;
106 ::memcpy(m_trap_opcode, trap_opcode, trap_opcode_size);
107 return true;
108 }
109 m_byte_size = 0;
110 return false;
111}
112
114
116 return &m_saved_opcode[0];
117}
118
119bool BreakpointSite::IsEnabled() const { return m_enabled; }
120
121void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; }
122
124 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
125 m_constituents.Add(constituent);
126}
127
129 lldb::break_id_t break_loc_id) {
130 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
131 m_constituents.Remove(break_id, break_loc_id);
132 return m_constituents.GetSize();
133}
134
136 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
137 return m_constituents.GetSize();
138}
139
141 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
142 return m_constituents.GetByIndex(index);
143}
144
146 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
147 return m_constituents.ValidForThisThread(thread);
148}
149
151 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
153 loc_sp->BumpHitCount();
154 }
155}
156
158 lldb::addr_t *intersect_addr,
159 size_t *intersect_size,
160 size_t *opcode_offset) const {
161 // The function should be called only for software breakpoints.
163
164 if (m_byte_size == 0)
165 return false;
166
167 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
168 const lldb::addr_t end_addr = addr + size;
169 // Is the breakpoint end address before the passed in start address?
170 if (bp_end_addr <= addr)
171 return false;
172
173 // Is the breakpoint start address after passed in end address?
174 if (end_addr <= m_addr)
175 return false;
176
177 if (intersect_addr || intersect_size || opcode_offset) {
178 if (m_addr < addr) {
179 if (intersect_addr)
180 *intersect_addr = addr;
181 if (intersect_size)
182 *intersect_size =
183 std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
184 if (opcode_offset)
185 *opcode_offset = addr - m_addr;
186 } else {
187 if (intersect_addr)
188 *intersect_addr = m_addr;
189 if (intersect_size)
190 *intersect_size =
191 std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
192 if (opcode_offset)
193 *opcode_offset = 0;
194 }
195 }
196 return true;
197}
198
200 BreakpointLocationCollection &out_collection) {
201 std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
203 out_collection.Add(loc_sp);
204 }
205 return out_collection.GetSize();
206}
#define lldbassert(x)
Definition: LLDBAssert.h:15
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Print a description of the breakpoint locations in this list to the stream s.
bool IsInternal() const
Tell whether ALL the breakpoints in the location collection are internal.
bool ValidForThisThread(Thread &thread)
Check whether this collection of breakpoint locations have any thread specifiers, and if yes,...
bool ShouldStop(StoppointCallbackContext *context)
Enquires of all the breakpoint locations in this list whether we should stop at a hit at breakID.
BreakpointLocationCollectionIterable BreakpointLocations()
lldb::BreakpointLocationSP GetByIndex(size_t i)
Returns a shared pointer to the breakpoint location with index i.
void Add(const lldb::BreakpointLocationSP &bp_loc_sp)
Add the breakpoint bp_loc_sp to the list.
bool Remove(lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
Removes the breakpoint location given by breakID from this 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.
bool ShouldStop(StoppointCallbackContext *context) override
Enquires of the breakpoint locations that produced this breakpoint site whether we should stop at thi...
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 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 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.
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.
uint8_t m_saved_opcode[8]
The saved opcode bytes if this breakpoint site uses trap opcodes.
bool IsEnabled() const
Tells whether the current breakpoint site is enabled or not.
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: When we hit a breakpoint we need to package up whatever information is needed to eva...
void Increment(uint32_t difference=1)
lldb::addr_t m_addr
The load address of this stop point.
Definition: StoppointSite.h:52
lldb::break_id_t GetID() const
Definition: StoppointSite.h:45
uint32_t GetHitCount() const
Definition: StoppointSite.h:33
StoppointHitCounter m_hit_counter
Number of times this breakpoint/watchpoint has been hit.
Definition: StoppointSite.h:64
virtual lldb::addr_t GetLoadAddress() const
Definition: StoppointSite.h:27
uint32_t m_byte_size
The size in bytes of stoppoint, e.g.
Definition: StoppointSite.h:61
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.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:322
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
int32_t break_id_t
Definition: lldb-types.h:86
uint64_t addr_t
Definition: lldb-types.h:80