blob: d9397fcb41256a5d44349dcdd55afef1f12aabb4 [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;
43 for (int i = 0; i < m_owners.GetSize(); i++)
44 {
45 m_owners.GetByIndex(i)->ClearBreakpointSite();
46 }
47}
48
49break_id_t
50BreakpointSite::GetNextID()
51{
52 static break_id_t g_next_id = 0;
53 return ++g_next_id;
54}
55
56// RETURNS - true if we should stop at this breakpoint, false if we
57// should continue.
58
59bool
60BreakpointSite::ShouldStop (StoppointCallbackContext *context)
61{
62 m_hit_count++;
63 return m_owners.ShouldStop (context);
64}
65
66bool
67BreakpointSite::IsBreakpointAtThisSite (lldb::break_id_t bp_id)
68{
69 for (int i = 0; i < m_owners.GetSize(); i++)
70 {
71 if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
72 return true;
73 }
74 return false;
75}
76
77void
78BreakpointSite::Dump(Stream *s) const
79{
80 if (s == NULL)
81 return;
82
Jim Ingham3c7b5b92010-06-16 02:00:15 +000083 s->Printf("BreakpointSite %u: addr = 0x%8.8llx type = %s breakpoint hw_index = %i hit_count = %-4u",
Chris Lattner24943d22010-06-08 16:52:24 +000084 GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +000085 (uint64_t)m_addr,
86 IsHardware() ? "hardware" : "software",
87 GetHardwareIndex(),
88 GetHitCount());
89}
90
91void
92BreakpointSite::GetDescription (Stream *s, lldb::DescriptionLevel level)
93{
94 if (level != lldb::eDescriptionLevelBrief)
95 s->Printf ("breakpoint site: %d ", GetID());
96 m_owners.GetDescription (s, level);
97}
98
99uint8_t *
100BreakpointSite::GetTrapOpcodeBytes()
101{
102 return &m_trap_opcode[0];
103}
104
105const uint8_t *
106BreakpointSite::GetTrapOpcodeBytes() const
107{
108 return &m_trap_opcode[0];
109}
110
111size_t
112BreakpointSite::GetTrapOpcodeMaxByteSize() const
113{
114 return sizeof(m_trap_opcode);
115}
116
117bool
118BreakpointSite::SetTrapOpcode (const uint8_t *trap_opcode, size_t trap_opcode_size)
119{
120 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode))
121 {
122 m_byte_size = trap_opcode_size;
123 ::memcpy (m_trap_opcode, trap_opcode, trap_opcode_size);
124 return true;
125 }
126 m_byte_size = 0;
127 return false;
128}
129
130uint8_t *
131BreakpointSite::GetSavedOpcodeBytes()
132{
133 return &m_saved_opcode[0];
134}
135
136const uint8_t *
137BreakpointSite::GetSavedOpcodeBytes() const
138{
139 return &m_saved_opcode[0];
140}
141
142bool
143BreakpointSite::IsEnabled () const
144{
145 return m_enabled;
146}
147
148void
149BreakpointSite::SetEnabled (bool enabled)
150{
151 m_enabled = enabled;
152}
153
154void
155BreakpointSite::AddOwner (BreakpointLocationSP &owner)
156{
157 m_owners.Add(owner);
158}
159
160uint32_t
161BreakpointSite::RemoveOwner (lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
162{
163 m_owners.Remove(break_id, break_loc_id);
164 return m_owners.GetSize();
165}
166
167uint32_t
168BreakpointSite::GetNumberOfOwners ()
169{
170 return m_owners.GetSize();
171}
172
173BreakpointLocationSP
174BreakpointSite::GetOwnerAtIndex (uint32_t index)
175{
176 return m_owners.GetByIndex (index);
177}
178
179bool
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000180BreakpointSite::ValidForThisThread (Thread *thread)
181{
182 return m_owners.ValidForThisThread(thread);
183}
184
185bool
Chris Lattner24943d22010-06-08 16:52:24 +0000186BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size, lldb::addr_t *intersect_addr, size_t *intersect_size, size_t *opcode_offset) const
187{
188 // We only use software traps for software breakpoints
189 if (!IsHardware())
190 {
191 if (m_byte_size > 0)
192 {
193 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
194 const lldb::addr_t end_addr = addr + size;
195 // Is the breakpoint end address before the passed in start address?
196 if (bp_end_addr <= addr)
197 return false;
198 // Is the breakpoint start address after passed in end address?
199 if (end_addr <= m_addr)
200 return false;
201 if (intersect_addr || intersect_size || opcode_offset)
202 {
203 if (m_addr < addr)
204 {
205 if (intersect_addr)
206 *intersect_addr = addr;
207 if (intersect_size)
208 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
209 if (opcode_offset)
210 *opcode_offset = addr - m_addr;
211 }
212 else
213 {
214 if (intersect_addr)
215 *intersect_addr = m_addr;
216 if (intersect_size)
217 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
218 if (opcode_offset)
219 *opcode_offset = 0;
220 }
221 }
222 return true;
223 }
224 }
225 return false;
226}