blob: fa5d8c1f9f814b3faed5fe827e6137e0ac46a346 [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
10#include "lldb/Breakpoint/BreakpointSite.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Greg Clayton4e78f602010-11-18 18:52:36 +000016#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Breakpoint/BreakpointLocation.h"
18#include "lldb/Breakpoint/BreakpointSiteList.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
23BreakpointSite::BreakpointSite
24(
25 BreakpointSiteList *list,
Greg Claytone1cd1be2012-01-29 20:56:30 +000026 const BreakpointLocationSP& owner,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027 lldb::addr_t addr,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028 bool use_hardware
29) :
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.
35 m_owners()
36{
37 m_owners.Add(owner);
38}
39
40BreakpointSite::~BreakpointSite()
41{
42 BreakpointLocationSP bp_loc_sp;
Greg Claytonc982c762010-07-09 20:39:50 +000043 const size_t owner_count = m_owners.GetSize();
44 for (size_t i = 0; i < owner_count; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045 {
46 m_owners.GetByIndex(i)->ClearBreakpointSite();
47 }
48}
49
50break_id_t
51BreakpointSite::GetNextID()
52{
53 static break_id_t g_next_id = 0;
54 return ++g_next_id;
55}
56
57// RETURNS - true if we should stop at this breakpoint, false if we
58// should continue.
59
60bool
61BreakpointSite::ShouldStop (StoppointCallbackContext *context)
62{
Johnny Chenfab7a912012-01-23 23:03:59 +000063 IncrementHitCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064 return m_owners.ShouldStop (context);
65}
66
67bool
68BreakpointSite::IsBreakpointAtThisSite (lldb::break_id_t bp_id)
69{
Greg Claytonc982c762010-07-09 20:39:50 +000070 const size_t owner_count = m_owners.GetSize();
71 for (size_t i = 0; i < owner_count; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 {
73 if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
74 return true;
75 }
76 return false;
77}
78
79void
80BreakpointSite::Dump(Stream *s) const
81{
82 if (s == NULL)
83 return;
84
Daniel Malead01b2952012-11-29 21:49:15 +000085 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 +000086 GetID(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 (uint64_t)m_addr,
88 IsHardware() ? "hardware" : "software",
89 GetHardwareIndex(),
90 GetHitCount());
91}
92
93void
94BreakpointSite::GetDescription (Stream *s, lldb::DescriptionLevel level)
95{
96 if (level != lldb::eDescriptionLevelBrief)
Daniel Malead01b2952012-11-29 21:49:15 +000097 s->Printf ("breakpoint site: %d at 0x%8.8" PRIx64, GetID(), GetLoadAddress());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 m_owners.GetDescription (s, level);
99}
100
Jim Ingham29950772013-01-26 02:19:28 +0000101bool
102BreakpointSite::IsInternal() const
103{
104 return m_owners.IsInternal();
105}
106
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107uint8_t *
108BreakpointSite::GetTrapOpcodeBytes()
109{
110 return &m_trap_opcode[0];
111}
112
113const uint8_t *
114BreakpointSite::GetTrapOpcodeBytes() const
115{
116 return &m_trap_opcode[0];
117}
118
119size_t
120BreakpointSite::GetTrapOpcodeMaxByteSize() const
121{
122 return sizeof(m_trap_opcode);
123}
124
125bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000126BreakpointSite::SetTrapOpcode (const uint8_t *trap_opcode, uint32_t trap_opcode_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127{
128 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode))
129 {
130 m_byte_size = trap_opcode_size;
131 ::memcpy (m_trap_opcode, trap_opcode, trap_opcode_size);
132 return true;
133 }
134 m_byte_size = 0;
135 return false;
136}
137
138uint8_t *
139BreakpointSite::GetSavedOpcodeBytes()
140{
141 return &m_saved_opcode[0];
142}
143
144const uint8_t *
145BreakpointSite::GetSavedOpcodeBytes() const
146{
147 return &m_saved_opcode[0];
148}
149
150bool
151BreakpointSite::IsEnabled () const
152{
153 return m_enabled;
154}
155
156void
157BreakpointSite::SetEnabled (bool enabled)
158{
159 m_enabled = enabled;
160}
161
162void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000163BreakpointSite::AddOwner (const BreakpointLocationSP &owner)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164{
165 m_owners.Add(owner);
166}
167
Greg Claytonc7bece562013-01-25 18:06:21 +0000168size_t
Greg Claytonc982c762010-07-09 20:39:50 +0000169BreakpointSite::RemoveOwner (lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170{
171 m_owners.Remove(break_id, break_loc_id);
172 return m_owners.GetSize();
173}
174
Greg Claytonc7bece562013-01-25 18:06:21 +0000175size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176BreakpointSite::GetNumberOfOwners ()
177{
178 return m_owners.GetSize();
179}
180
181BreakpointLocationSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000182BreakpointSite::GetOwnerAtIndex (size_t index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183{
184 return m_owners.GetByIndex (index);
185}
186
187bool
Jim Ingham1b54c882010-06-16 02:00:15 +0000188BreakpointSite::ValidForThisThread (Thread *thread)
189{
190 return m_owners.ValidForThisThread(thread);
191}
192
193bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size, lldb::addr_t *intersect_addr, size_t *intersect_size, size_t *opcode_offset) const
195{
196 // We only use software traps for software breakpoints
197 if (!IsHardware())
198 {
199 if (m_byte_size > 0)
200 {
201 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
202 const lldb::addr_t end_addr = addr + size;
203 // Is the breakpoint end address before the passed in start address?
204 if (bp_end_addr <= addr)
205 return false;
206 // Is the breakpoint start address after passed in end address?
207 if (end_addr <= m_addr)
208 return false;
209 if (intersect_addr || intersect_size || opcode_offset)
210 {
211 if (m_addr < addr)
212 {
213 if (intersect_addr)
214 *intersect_addr = addr;
215 if (intersect_size)
216 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
217 if (opcode_offset)
218 *opcode_offset = addr - m_addr;
219 }
220 else
221 {
222 if (intersect_addr)
223 *intersect_addr = m_addr;
224 if (intersect_size)
225 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
226 if (opcode_offset)
227 *opcode_offset = 0;
228 }
229 }
230 return true;
231 }
232 }
233 return false;
234}