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 
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  : StoppointSite(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 
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 
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 
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 
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",
82 }
83 
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 
93 
95 
96 const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const {
97  return &m_trap_opcode[0];
98 }
99 
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 
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 
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 
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 
148  std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
149  return m_owners.ValidForThisThread(thread);
150 }
151 
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 
160  lldb::addr_t *intersect_addr,
161  size_t *intersect_size,
162  size_t *opcode_offset) const {
163  // The function should be called only for software breakpoints.
164  lldbassert(GetType() == Type::eSoftware);
165 
166  if (m_byte_size == 0)
167  return false;
168 
169  const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
170  const lldb::addr_t end_addr = addr + size;
171  // Is the breakpoint end address before the passed in start address?
172  if (bp_end_addr <= addr)
173  return false;
174 
175  // Is the breakpoint start address after passed in end address?
176  if (end_addr <= m_addr)
177  return false;
178 
179  if (intersect_addr || intersect_size || opcode_offset) {
180  if (m_addr < addr) {
181  if (intersect_addr)
182  *intersect_addr = addr;
183  if (intersect_size)
184  *intersect_size =
185  std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
186  if (opcode_offset)
187  *opcode_offset = addr - m_addr;
188  } else {
189  if (intersect_addr)
190  *intersect_addr = m_addr;
191  if (intersect_size)
192  *intersect_size =
193  std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
194  if (opcode_offset)
195  *opcode_offset = 0;
196  }
197  }
198  return true;
199 }
200 
201 size_t
203  std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
204  for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
205  out_collection.Add(loc_sp);
206  }
207  return out_collection.GetSize();
208 }
lldb_private::BreakpointLocationCollection::GetByIndex
lldb::BreakpointLocationSP GetByIndex(size_t i)
Returns a shared pointer to the breakpoint location with index i.
Definition: BreakpointLocationCollection.cpp:101
lldb_private::BreakpointSite::IsBreakpointAtThisSite
bool IsBreakpointAtThisSite(lldb::break_id_t bp_id)
Tell whether a breakpoint has a location at this site.
Definition: BreakpointSite.cpp:64
lldb_private::StoppointSite::GetLoadAddress
virtual lldb::addr_t GetLoadAddress() const
Definition: StoppointSite.h:27
lldb_private::BreakpointSite::GetSavedOpcodeBytes
uint8_t * GetSavedOpcodeBytes()
Gets the original instruction bytes that were overwritten by the trap.
Definition: BreakpointSite.cpp:115
lldb_private::BreakpointSite::IsInternal
bool IsInternal() const
Tell whether ALL the breakpoints in the location collection are internal.
Definition: BreakpointSite.cpp:92
lldb_private::BreakpointLocationCollection::IsInternal
bool IsInternal() const
Tell whether ALL the breakpoints in the location collection are internal.
Definition: BreakpointLocationCollection.cpp:149
lldb_private::StoppointCallbackContext
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
Definition: StoppointCallbackContext.h:26
lldb_private::BreakpointSite::CopyOwnersList
size_t CopyOwnersList(BreakpointLocationCollection &out_collection)
This method copies the breakpoint site's owners into a new collection.
Definition: BreakpointSite.cpp:202
lldb_private::BreakpointSite::BumpHitCounts
void BumpHitCounts()
Definition: BreakpointSite.cpp:152
lldb_private::BreakpointSite::AddOwner
void AddOwner(const lldb::BreakpointLocationSP &owner)
The "Owners" are the breakpoint locations that share this breakpoint site.
Definition: BreakpointSite.cpp:125
lldb_private::BreakpointSite::GetOwnerAtIndex
lldb::BreakpointLocationSP GetOwnerAtIndex(size_t idx)
This method returns the breakpoint location at index index located at this breakpoint site.
Definition: BreakpointSite.cpp:142
BreakpointLocation.h
lldb_private::BreakpointSite::GetType
BreakpointSite::Type GetType() const
Definition: BreakpointSite.h:192
lldb_private::BreakpointLocationCollection::BreakpointLocations
BreakpointLocationCollectionIterable BreakpointLocations()
Definition: BreakpointLocationCollection.h:171
BreakpointSiteList.h
lldb_private::BreakpointSite::m_enabled
bool m_enabled
Boolean indicating if this breakpoint site enabled or not.
Definition: BreakpointSite.h:216
lldb_private::BreakpointLocationCollection::ValidForThisThread
bool ValidForThisThread(Thread &thread)
Check whether this collection of breakpoint locations have any thread specifiers, and if yes,...
Definition: BreakpointLocationCollection.cpp:137
lldb_private::Stream
Definition: Stream.h:28
lldb_private::StoppointSite::GetID
lldb::break_id_t GetID() const
Definition: StoppointSite.h:49
lldb_private::StoppointSite::GetHitCount
uint32_t GetHitCount() const
Definition: StoppointSite.h:33
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::BreakpointSite::ValidForThisThread
bool ValidForThisThread(Thread &thread)
Check whether the owners of this breakpoint site have any thread specifiers, and if yes,...
Definition: BreakpointSite.cpp:147
lldb_private::BreakpointSite::m_trap_opcode
uint8_t m_trap_opcode[8]
The opcode that was used to create the breakpoint if it is a software breakpoint site.
Definition: BreakpointSite.h:213
lldb_private::StoppointHitCounter::Increment
void Increment(uint32_t difference=1)
Definition: StoppointHitCounter.h:24
lldb_private::BreakpointSite::GetTrapOpcodeMaxByteSize
size_t GetTrapOpcodeMaxByteSize() const
Get the size of the trap opcode for this address.
Definition: BreakpointSite.cpp:100
lldb_private::BreakpointSite::m_owners_mutex
std::recursive_mutex m_owners_mutex
This mutex protects the owners collection.
Definition: BreakpointSite.h:223
lldb_private::BreakpointLocationCollection::ShouldStop
bool ShouldStop(StoppointCallbackContext *context)
Enquires of all the breakpoint locations in this list whether we should stop at a hit at breakID.
Definition: BreakpointLocationCollection.cpp:120
lldb_private::Thread
Definition: Thread.h:60
lldb_private::BreakpointSite::SetEnabled
void SetEnabled(bool enabled)
Sets whether the current breakpoint site is enabled or not.
Definition: BreakpointSite.cpp:123
lldb::break_id_t
int32_t break_id_t
Definition: lldb-types.h:88
lldb_private::BreakpointSite::IsHardware
bool IsHardware() const override
Definition: BreakpointSite.h:186
lldb_private::BreakpointLocationCollection::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Print a description of the breakpoint locations in this list to the stream s.
Definition: BreakpointLocationCollection.cpp:165
lldb_private::BreakpointSite::m_owners
BreakpointLocationCollection m_owners
This has the BreakpointLocations that share this breakpoint site.
Definition: BreakpointSite.h:220
lldb_private::StoppointSite::m_byte_size
uint32_t m_byte_size
The size in bytes of stoppoint, e.g.
Definition: StoppointSite.h:68
lldb_private::BreakpointSite::~BreakpointSite
~BreakpointSite() override
Definition: BreakpointSite.cpp:34
lldb_private::BreakpointLocationCollection
Definition: BreakpointLocationCollection.h:20
lldb_private::BreakpointSite::IntersectsRange
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.
Definition: BreakpointSite.cpp:159
lldb_private::StoppointSite::GetHardwareIndex
uint32_t GetHardwareIndex() const
Definition: StoppointSite.h:41
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
lldb_private::BreakpointSite::m_saved_opcode
uint8_t m_saved_opcode[8]
The saved opcode bytes if this breakpoint site uses trap opcodes.
Definition: BreakpointSite.h:211
lldb_private::BreakpointSite::GetTrapOpcodeBytes
uint8_t * GetTrapOpcodeBytes()
Returns the Opcode Bytes for this breakpoint.
Definition: BreakpointSite.cpp:94
uint32_t
lldb_private::BreakpointSite::RemoveOwner
size_t RemoveOwner(lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
The method removes the owner at break_loc_id from this breakpoint list.
Definition: BreakpointSite.cpp:130
lldb_private::BreakpointSite::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Print a description of this breakpoint site to the stream s.
Definition: BreakpointSite.cpp:84
lldb_private::BreakpointSite::GetNextID
static lldb::break_id_t GetNextID()
Definition: BreakpointSite.cpp:42
lldb_private::BreakpointSite::ShouldStop
bool ShouldStop(StoppointCallbackContext *context) override
Enquires of the breakpoint locations that produced this breakpoint site whether we should stop at thi...
Definition: BreakpointSite.cpp:50
lldb_private::BreakpointSite::IsEnabled
bool IsEnabled() const
Tells whether the current breakpoint site is enabled or not.
Definition: BreakpointSite.cpp:121
lldb_private::StoppointSite::m_hit_counter
StoppointHitCounter m_hit_counter
Number of times this breakpoint/watchpoint has been hit.
Definition: StoppointSite.h:71
lldb_private::BreakpointLocationCollection::Add
void Add(const lldb::BreakpointLocationSP &bp_loc_sp)
Add the breakpoint bp_loc_sp to the list.
Definition: BreakpointLocationCollection.cpp:26
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::StoppointSite::m_addr
lldb::addr_t m_addr
The load address of this stop point.
Definition: StoppointSite.h:56
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::BreakpointLocationCollection::GetSize
size_t GetSize() const
Returns the number of elements in this breakpoint location list.
Definition: BreakpointLocationCollection.h:104
Breakpoint.h
lldb_private::BreakpointSite::SetTrapOpcode
bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size)
Sets the trap opcode.
Definition: BreakpointSite.cpp:104
lldb_private::BreakpointSite::GetNumberOfOwners
size_t GetNumberOfOwners()
This method returns the number of breakpoint locations currently located at this breakpoint site.
Definition: BreakpointSite.cpp:137
BreakpointSite.h
Stream.h
lldb_private::BreakpointLocationCollection::Remove
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.
Definition: BreakpointLocationCollection.cpp:34
lldb_private::BreakpointSite::Dump
void Dump(Stream *s) const override
Standard Dump method.
Definition: BreakpointSite.cpp:74
lldb
Definition: SBAddress.h:15
lldb_private::BreakpointSiteList
Definition: BreakpointSiteList.h:23
lldb::eDescriptionLevelBrief
@ eDescriptionLevelBrief
Definition: lldb-enumerations.h:208
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207
lldb_private::StoppointSite
Definition: StoppointSite.h:18