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 <inttypes.h>
10 
11 #include "lldb/Breakpoint/BreakpointSite.h"
12 
13 #include "lldb/Breakpoint/Breakpoint.h"
14 #include "lldb/Breakpoint/BreakpointLocation.h"
15 #include "lldb/Breakpoint/BreakpointSiteList.h"
16 #include "lldb/Utility/Stream.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 BreakpointSite::BreakpointSite(BreakpointSiteList *list,
22                                const BreakpointLocationSP &owner,
23                                lldb::addr_t addr, bool use_hardware)
24     : StoppointLocation(GetNextID(), addr, 0, use_hardware),
25       m_type(eSoftware), // Process subclasses need to set this correctly using
26                          // SetType()
27       m_saved_opcode(), m_trap_opcode(),
28       m_enabled(false), // Need to create it disabled, so the first enable turns
29                         // it on.
30       m_owners(), m_owners_mutex() {
31   m_owners.Add(owner);
32 }
33 
34 BreakpointSite::~BreakpointSite() {
35   BreakpointLocationSP bp_loc_sp;
36   const size_t owner_count = m_owners.GetSize();
37   for (size_t i = 0; i < owner_count; i++) {
38     m_owners.GetByIndex(i)->ClearBreakpointSite();
39   }
40 }
41 
42 break_id_t BreakpointSite::GetNextID() {
43   static break_id_t g_next_id = 0;
44   return ++g_next_id;
45 }
46 
47 // RETURNS - true if we should stop at this breakpoint, false if we
48 // should continue.
49 
50 bool BreakpointSite::ShouldStop(StoppointCallbackContext *context) {
51   IncrementHitCount();
52   // ShouldStop can do a lot of work, and might even come come back and hit
53   // this breakpoint site again.  So don't hold the m_owners_mutex the whole
54   // while.  Instead make a local copy of the collection and call ShouldStop on
55   // the copy.
56   BreakpointLocationCollection owners_copy;
57   {
58     std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
59     owners_copy = m_owners;
60   }
61   return owners_copy.ShouldStop(context);
62 }
63 
64 bool BreakpointSite::IsBreakpointAtThisSite(lldb::break_id_t bp_id) {
65   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
66   const size_t owner_count = m_owners.GetSize();
67   for (size_t i = 0; i < owner_count; i++) {
68     if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
69       return true;
70   }
71   return false;
72 }
73 
74 void BreakpointSite::Dump(Stream *s) const {
75   if (s == nullptr)
76     return;
77 
78   s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64
79             "  type = %s breakpoint  hw_index = %i  hit_count = %-4u",
80             GetID(), (uint64_t)m_addr, IsHardware() ? "hardware" : "software",
81             GetHardwareIndex(), GetHitCount());
82 }
83 
84 void BreakpointSite::GetDescription(Stream *s, lldb::DescriptionLevel level) {
85   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
86   if (level != lldb::eDescriptionLevelBrief)
87     s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(),
88               GetLoadAddress());
89   m_owners.GetDescription(s, level);
90 }
91 
92 bool BreakpointSite::IsInternal() const { return m_owners.IsInternal(); }
93 
94 uint8_t *BreakpointSite::GetTrapOpcodeBytes() { return &m_trap_opcode[0]; }
95 
96 const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const {
97   return &m_trap_opcode[0];
98 }
99 
100 size_t BreakpointSite::GetTrapOpcodeMaxByteSize() const {
101   return sizeof(m_trap_opcode);
102 }
103 
104 bool BreakpointSite::SetTrapOpcode(const uint8_t *trap_opcode,
105                                    uint32_t trap_opcode_size) {
106   if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode)) {
107     m_byte_size = trap_opcode_size;
108     ::memcpy(m_trap_opcode, trap_opcode, trap_opcode_size);
109     return true;
110   }
111   m_byte_size = 0;
112   return false;
113 }
114 
115 uint8_t *BreakpointSite::GetSavedOpcodeBytes() { return &m_saved_opcode[0]; }
116 
117 const uint8_t *BreakpointSite::GetSavedOpcodeBytes() const {
118   return &m_saved_opcode[0];
119 }
120 
121 bool BreakpointSite::IsEnabled() const { return m_enabled; }
122 
123 void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; }
124 
125 void BreakpointSite::AddOwner(const BreakpointLocationSP &owner) {
126   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
127   m_owners.Add(owner);
128 }
129 
130 size_t BreakpointSite::RemoveOwner(lldb::break_id_t break_id,
131                                    lldb::break_id_t break_loc_id) {
132   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
133   m_owners.Remove(break_id, break_loc_id);
134   return m_owners.GetSize();
135 }
136 
137 size_t BreakpointSite::GetNumberOfOwners() {
138   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
139   return m_owners.GetSize();
140 }
141 
142 BreakpointLocationSP BreakpointSite::GetOwnerAtIndex(size_t index) {
143   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
144   return m_owners.GetByIndex(index);
145 }
146 
147 bool BreakpointSite::ValidForThisThread(Thread *thread) {
148   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
149   return m_owners.ValidForThisThread(thread);
150 }
151 
152 void BreakpointSite::BumpHitCounts() {
153   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
154   for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
155     loc_sp->BumpHitCount();
156   }
157 }
158 
159 void BreakpointSite::SetHardwareIndex(uint32_t index) {
160   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
161   for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
162     loc_sp->SetHardwareIndex(index);
163   }
164 }
165 
166 bool BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size,
167                                      lldb::addr_t *intersect_addr,
168                                      size_t *intersect_size,
169                                      size_t *opcode_offset) const {
170   // We only use software traps for software breakpoints
171   if (!IsHardware()) {
172     if (m_byte_size > 0) {
173       const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
174       const lldb::addr_t end_addr = addr + size;
175       // Is the breakpoint end address before the passed in start address?
176       if (bp_end_addr <= addr)
177         return false;
178       // Is the breakpoint start address after passed in end address?
179       if (end_addr <= m_addr)
180         return false;
181       if (intersect_addr || intersect_size || opcode_offset) {
182         if (m_addr < addr) {
183           if (intersect_addr)
184             *intersect_addr = addr;
185           if (intersect_size)
186             *intersect_size =
187                 std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
188           if (opcode_offset)
189             *opcode_offset = addr - m_addr;
190         } else {
191           if (intersect_addr)
192             *intersect_addr = m_addr;
193           if (intersect_size)
194             *intersect_size =
195                 std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
196           if (opcode_offset)
197             *opcode_offset = 0;
198         }
199       }
200       return true;
201     }
202   }
203   return false;
204 }
205 
206 size_t
207 BreakpointSite::CopyOwnersList(BreakpointLocationCollection &out_collection) {
208   std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
209   for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
210     out_collection.Add(loc_sp);
211   }
212   return out_collection.GetSize();
213 }
214