blob: 082a9379a90de0cbe828b2f45c08f958b251d277 [file] [log] [blame]
Chris Lattner24943d22010-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
16#include "lldb/Breakpoint/BreakpointLocation.h"
17#include "lldb/Breakpoint/BreakpointSiteList.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22BreakpointSite::BreakpointSite
23(
24 BreakpointSiteList *list,
25 BreakpointLocationSP& owner,
26 lldb::addr_t addr,
27 lldb::tid_t tid,
28 bool use_hardware
29) :
30 StoppointLocation(GetNextID(), addr, tid, use_hardware),
31 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 Clayton54e7afa2010-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 Lattner24943d22010-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{
63 m_hit_count++;
64 return m_owners.ShouldStop (context);
65}
66
67bool
68BreakpointSite::IsBreakpointAtThisSite (lldb::break_id_t bp_id)
69{
Greg Clayton54e7afa2010-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 Lattner24943d22010-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
Jim Ingham3c7b5b92010-06-16 02:00:15 +000085 s->Printf("BreakpointSite %u: addr = 0x%8.8llx type = %s breakpoint hw_index = %i hit_count = %-4u",
Chris Lattner24943d22010-06-08 16:52:24 +000086 GetID(),
Chris Lattner24943d22010-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)
97 s->Printf ("breakpoint site: %d ", GetID());
98 m_owners.GetDescription (s, level);
99}
100
101uint8_t *
102BreakpointSite::GetTrapOpcodeBytes()
103{
104 return &m_trap_opcode[0];
105}
106
107const uint8_t *
108BreakpointSite::GetTrapOpcodeBytes() const
109{
110 return &m_trap_opcode[0];
111}
112
113size_t
114BreakpointSite::GetTrapOpcodeMaxByteSize() const
115{
116 return sizeof(m_trap_opcode);
117}
118
119bool
120BreakpointSite::SetTrapOpcode (const uint8_t *trap_opcode, size_t trap_opcode_size)
121{
122 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode))
123 {
124 m_byte_size = trap_opcode_size;
125 ::memcpy (m_trap_opcode, trap_opcode, trap_opcode_size);
126 return true;
127 }
128 m_byte_size = 0;
129 return false;
130}
131
132uint8_t *
133BreakpointSite::GetSavedOpcodeBytes()
134{
135 return &m_saved_opcode[0];
136}
137
138const uint8_t *
139BreakpointSite::GetSavedOpcodeBytes() const
140{
141 return &m_saved_opcode[0];
142}
143
144bool
145BreakpointSite::IsEnabled () const
146{
147 return m_enabled;
148}
149
150void
151BreakpointSite::SetEnabled (bool enabled)
152{
153 m_enabled = enabled;
154}
155
156void
157BreakpointSite::AddOwner (BreakpointLocationSP &owner)
158{
159 m_owners.Add(owner);
160}
161
162uint32_t
Greg Clayton54e7afa2010-07-09 20:39:50 +0000163BreakpointSite::RemoveOwner (lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000164{
165 m_owners.Remove(break_id, break_loc_id);
166 return m_owners.GetSize();
167}
168
169uint32_t
170BreakpointSite::GetNumberOfOwners ()
171{
172 return m_owners.GetSize();
173}
174
175BreakpointLocationSP
176BreakpointSite::GetOwnerAtIndex (uint32_t index)
177{
178 return m_owners.GetByIndex (index);
179}
180
181bool
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000182BreakpointSite::ValidForThisThread (Thread *thread)
183{
184 return m_owners.ValidForThisThread(thread);
185}
186
187bool
Chris Lattner24943d22010-06-08 16:52:24 +0000188BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size, lldb::addr_t *intersect_addr, size_t *intersect_size, size_t *opcode_offset) const
189{
190 // We only use software traps for software breakpoints
191 if (!IsHardware())
192 {
193 if (m_byte_size > 0)
194 {
195 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
196 const lldb::addr_t end_addr = addr + size;
197 // Is the breakpoint end address before the passed in start address?
198 if (bp_end_addr <= addr)
199 return false;
200 // Is the breakpoint start address after passed in end address?
201 if (end_addr <= m_addr)
202 return false;
203 if (intersect_addr || intersect_size || opcode_offset)
204 {
205 if (m_addr < addr)
206 {
207 if (intersect_addr)
208 *intersect_addr = addr;
209 if (intersect_size)
210 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
211 if (opcode_offset)
212 *opcode_offset = addr - m_addr;
213 }
214 else
215 {
216 if (intersect_addr)
217 *intersect_addr = m_addr;
218 if (intersect_size)
219 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
220 if (opcode_offset)
221 *opcode_offset = 0;
222 }
223 }
224 return true;
225 }
226 }
227 return false;
228}