blob: d2aaea098cdb9bc138f7dd7af93639cd7f63bb91 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- BreakpointSite.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
Zachary Turnerfc858812015-03-04 17:43:00 +000012#include <inttypes.h>
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014// Other libraries and framework includes
15// Project includes
Eugene Zelenko16fd7512015-10-30 18:50:12 +000016#include "lldb/Breakpoint/BreakpointSite.h"
17
Greg Clayton4e78f602010-11-18 18:52:36 +000018#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Breakpoint/BreakpointLocation.h"
20#include "lldb/Breakpoint/BreakpointSiteList.h"
Zachary Turnerfc858812015-03-04 17:43:00 +000021#include "lldb/Core/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
Eugene Zelenko16fd7512015-10-30 18:50:12 +000026BreakpointSite::BreakpointSite(BreakpointSiteList *list,
27 const BreakpointLocationSP& owner,
28 lldb::addr_t addr,
29 bool use_hardware) :
Greg Claytonc7bece562013-01-25 18:06:21 +000030 StoppointLocation(GetNextID(), addr, 0, use_hardware),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031 m_type (eSoftware), // Process subclasses need to set this correctly using SetType()
32 m_saved_opcode(),
33 m_trap_opcode(),
34 m_enabled(false), // Need to create it disabled, so the first enable turns it on.
Jim Ingham5799e292014-06-18 01:04:40 +000035 m_owners(),
36 m_owners_mutex(Mutex::eMutexTypeRecursive)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037{
38 m_owners.Add(owner);
39}
40
41BreakpointSite::~BreakpointSite()
42{
43 BreakpointLocationSP bp_loc_sp;
Greg Claytonc982c762010-07-09 20:39:50 +000044 const size_t owner_count = m_owners.GetSize();
45 for (size_t i = 0; i < owner_count; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 {
47 m_owners.GetByIndex(i)->ClearBreakpointSite();
48 }
49}
50
51break_id_t
52BreakpointSite::GetNextID()
53{
54 static break_id_t g_next_id = 0;
55 return ++g_next_id;
56}
57
58// RETURNS - true if we should stop at this breakpoint, false if we
59// should continue.
60
61bool
62BreakpointSite::ShouldStop (StoppointCallbackContext *context)
63{
Jim Ingham2dfa00f2014-06-18 23:40:13 +000064 Mutex::Locker locker(m_owners_mutex);
Johnny Chenfab7a912012-01-23 23:03:59 +000065 IncrementHitCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 return m_owners.ShouldStop (context);
67}
68
69bool
70BreakpointSite::IsBreakpointAtThisSite (lldb::break_id_t bp_id)
71{
Jim Ingham2dfa00f2014-06-18 23:40:13 +000072 Mutex::Locker locker(m_owners_mutex);
Greg Claytonc982c762010-07-09 20:39:50 +000073 const size_t owner_count = m_owners.GetSize();
74 for (size_t i = 0; i < owner_count; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 {
76 if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
77 return true;
78 }
79 return false;
80}
81
82void
83BreakpointSite::Dump(Stream *s) const
84{
Eugene Zelenko16fd7512015-10-30 18:50:12 +000085 if (s == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 return;
87
Daniel Malead01b2952012-11-29 21:49:15 +000088 s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64 " type = %s breakpoint hw_index = %i hit_count = %-4u",
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 GetID(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 (uint64_t)m_addr,
91 IsHardware() ? "hardware" : "software",
92 GetHardwareIndex(),
93 GetHitCount());
94}
95
96void
97BreakpointSite::GetDescription (Stream *s, lldb::DescriptionLevel level)
98{
Jim Ingham2dfa00f2014-06-18 23:40:13 +000099 Mutex::Locker locker(m_owners_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 if (level != lldb::eDescriptionLevelBrief)
Daniel Malead01b2952012-11-29 21:49:15 +0000101 s->Printf ("breakpoint site: %d at 0x%8.8" PRIx64, GetID(), GetLoadAddress());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 m_owners.GetDescription (s, level);
103}
104
Jim Ingham29950772013-01-26 02:19:28 +0000105bool
106BreakpointSite::IsInternal() const
107{
108 return m_owners.IsInternal();
109}
110
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111uint8_t *
112BreakpointSite::GetTrapOpcodeBytes()
113{
114 return &m_trap_opcode[0];
115}
116
117const uint8_t *
118BreakpointSite::GetTrapOpcodeBytes() const
119{
120 return &m_trap_opcode[0];
121}
122
123size_t
124BreakpointSite::GetTrapOpcodeMaxByteSize() const
125{
126 return sizeof(m_trap_opcode);
127}
128
129bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000130BreakpointSite::SetTrapOpcode (const uint8_t *trap_opcode, uint32_t trap_opcode_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131{
132 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode))
133 {
134 m_byte_size = trap_opcode_size;
135 ::memcpy (m_trap_opcode, trap_opcode, trap_opcode_size);
136 return true;
137 }
138 m_byte_size = 0;
139 return false;
140}
141
142uint8_t *
143BreakpointSite::GetSavedOpcodeBytes()
144{
145 return &m_saved_opcode[0];
146}
147
148const uint8_t *
149BreakpointSite::GetSavedOpcodeBytes() const
150{
151 return &m_saved_opcode[0];
152}
153
154bool
155BreakpointSite::IsEnabled () const
156{
157 return m_enabled;
158}
159
160void
161BreakpointSite::SetEnabled (bool enabled)
162{
163 m_enabled = enabled;
164}
165
166void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000167BreakpointSite::AddOwner (const BreakpointLocationSP &owner)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168{
Jim Ingham2dfa00f2014-06-18 23:40:13 +0000169 Mutex::Locker locker(m_owners_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 m_owners.Add(owner);
171}
172
Greg Claytonc7bece562013-01-25 18:06:21 +0000173size_t
Greg Claytonc982c762010-07-09 20:39:50 +0000174BreakpointSite::RemoveOwner (lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175{
Jim Ingham2dfa00f2014-06-18 23:40:13 +0000176 Mutex::Locker locker(m_owners_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 m_owners.Remove(break_id, break_loc_id);
178 return m_owners.GetSize();
179}
180
Greg Claytonc7bece562013-01-25 18:06:21 +0000181size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182BreakpointSite::GetNumberOfOwners ()
183{
Jim Ingham2dfa00f2014-06-18 23:40:13 +0000184 Mutex::Locker locker(m_owners_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 return m_owners.GetSize();
186}
187
188BreakpointLocationSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000189BreakpointSite::GetOwnerAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190{
Jim Ingham2dfa00f2014-06-18 23:40:13 +0000191 Mutex::Locker locker(m_owners_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 return m_owners.GetByIndex (index);
193}
194
195bool
Jim Ingham1b54c882010-06-16 02:00:15 +0000196BreakpointSite::ValidForThisThread (Thread *thread)
197{
Jim Ingham2dfa00f2014-06-18 23:40:13 +0000198 Mutex::Locker locker(m_owners_mutex);
Jim Ingham1b54c882010-06-16 02:00:15 +0000199 return m_owners.ValidForThisThread(thread);
200}
201
Jim Inghama672ece2014-10-22 01:54:17 +0000202void
203BreakpointSite::BumpHitCounts()
204{
Jim Inghamff244c12015-07-29 00:40:36 +0000205 Mutex::Locker locker(m_owners_mutex);
Jim Inghama672ece2014-10-22 01:54:17 +0000206 for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations())
207 {
208 loc_sp->BumpHitCount();
209 }
210}
211
Jim Ingham1b54c882010-06-16 02:00:15 +0000212bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size, lldb::addr_t *intersect_addr, size_t *intersect_size, size_t *opcode_offset) const
214{
215 // We only use software traps for software breakpoints
216 if (!IsHardware())
217 {
218 if (m_byte_size > 0)
219 {
220 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
221 const lldb::addr_t end_addr = addr + size;
222 // Is the breakpoint end address before the passed in start address?
223 if (bp_end_addr <= addr)
224 return false;
225 // Is the breakpoint start address after passed in end address?
226 if (end_addr <= m_addr)
227 return false;
228 if (intersect_addr || intersect_size || opcode_offset)
229 {
230 if (m_addr < addr)
231 {
232 if (intersect_addr)
233 *intersect_addr = addr;
234 if (intersect_size)
235 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
236 if (opcode_offset)
237 *opcode_offset = addr - m_addr;
238 }
239 else
240 {
241 if (intersect_addr)
242 *intersect_addr = m_addr;
243 if (intersect_size)
244 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
245 if (opcode_offset)
246 *opcode_offset = 0;
247 }
248 }
249 return true;
250 }
251 }
252 return false;
253}
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000254
255size_t
256BreakpointSite::CopyOwnersList (BreakpointLocationCollection &out_collection)
257{
258 Mutex::Locker locker(m_owners_mutex);
259 for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations())
260 {
261 out_collection.Add(loc_sp);
262 }
263 return out_collection.GetSize();
264}