blob: 5f94a190d107c237627c4e91e8fff69594f648f0 [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
Greg Clayton640dc6b2010-11-18 18:52:36 +000016#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner24943d22010-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 Clayton13d24fb2012-01-29 20:56:30 +000026 const BreakpointLocationSP& owner,
Chris Lattner24943d22010-06-08 16:52:24 +000027 lldb::addr_t addr,
28 lldb::tid_t tid,
29 bool use_hardware
30) :
31 StoppointLocation(GetNextID(), addr, tid, use_hardware),
32 m_type (eSoftware), // Process subclasses need to set this correctly using SetType()
33 m_saved_opcode(),
34 m_trap_opcode(),
35 m_enabled(false), // Need to create it disabled, so the first enable turns it on.
36 m_owners()
37{
38 m_owners.Add(owner);
39}
40
41BreakpointSite::~BreakpointSite()
42{
43 BreakpointLocationSP bp_loc_sp;
Greg Clayton54e7afa2010-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 Lattner24943d22010-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{
Johnny Chen51b7c5f2012-01-23 23:03:59 +000064 IncrementHitCount();
Chris Lattner24943d22010-06-08 16:52:24 +000065 return m_owners.ShouldStop (context);
66}
67
68bool
69BreakpointSite::IsBreakpointAtThisSite (lldb::break_id_t bp_id)
70{
Greg Clayton54e7afa2010-07-09 20:39:50 +000071 const size_t owner_count = m_owners.GetSize();
72 for (size_t i = 0; i < owner_count; i++)
Chris Lattner24943d22010-06-08 16:52:24 +000073 {
74 if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
75 return true;
76 }
77 return false;
78}
79
80void
81BreakpointSite::Dump(Stream *s) const
82{
83 if (s == NULL)
84 return;
85
Daniel Malea5f35a4b2012-11-29 21:49:15 +000086 s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64 " type = %s breakpoint hw_index = %i hit_count = %-4u",
Chris Lattner24943d22010-06-08 16:52:24 +000087 GetID(),
Chris Lattner24943d22010-06-08 16:52:24 +000088 (uint64_t)m_addr,
89 IsHardware() ? "hardware" : "software",
90 GetHardwareIndex(),
91 GetHitCount());
92}
93
94void
95BreakpointSite::GetDescription (Stream *s, lldb::DescriptionLevel level)
96{
97 if (level != lldb::eDescriptionLevelBrief)
Daniel Malea5f35a4b2012-11-29 21:49:15 +000098 s->Printf ("breakpoint site: %d at 0x%8.8" PRIx64, GetID(), GetLoadAddress());
Chris Lattner24943d22010-06-08 16:52:24 +000099 m_owners.GetDescription (s, level);
100}
101
102uint8_t *
103BreakpointSite::GetTrapOpcodeBytes()
104{
105 return &m_trap_opcode[0];
106}
107
108const uint8_t *
109BreakpointSite::GetTrapOpcodeBytes() const
110{
111 return &m_trap_opcode[0];
112}
113
114size_t
115BreakpointSite::GetTrapOpcodeMaxByteSize() const
116{
117 return sizeof(m_trap_opcode);
118}
119
120bool
121BreakpointSite::SetTrapOpcode (const uint8_t *trap_opcode, size_t trap_opcode_size)
122{
123 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode))
124 {
125 m_byte_size = trap_opcode_size;
126 ::memcpy (m_trap_opcode, trap_opcode, trap_opcode_size);
127 return true;
128 }
129 m_byte_size = 0;
130 return false;
131}
132
133uint8_t *
134BreakpointSite::GetSavedOpcodeBytes()
135{
136 return &m_saved_opcode[0];
137}
138
139const uint8_t *
140BreakpointSite::GetSavedOpcodeBytes() const
141{
142 return &m_saved_opcode[0];
143}
144
145bool
146BreakpointSite::IsEnabled () const
147{
148 return m_enabled;
149}
150
151void
152BreakpointSite::SetEnabled (bool enabled)
153{
154 m_enabled = enabled;
155}
156
157void
Greg Clayton13d24fb2012-01-29 20:56:30 +0000158BreakpointSite::AddOwner (const BreakpointLocationSP &owner)
Chris Lattner24943d22010-06-08 16:52:24 +0000159{
160 m_owners.Add(owner);
161}
162
163uint32_t
Greg Clayton54e7afa2010-07-09 20:39:50 +0000164BreakpointSite::RemoveOwner (lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
Chris Lattner24943d22010-06-08 16:52:24 +0000165{
166 m_owners.Remove(break_id, break_loc_id);
167 return m_owners.GetSize();
168}
169
170uint32_t
171BreakpointSite::GetNumberOfOwners ()
172{
173 return m_owners.GetSize();
174}
175
176BreakpointLocationSP
177BreakpointSite::GetOwnerAtIndex (uint32_t index)
178{
179 return m_owners.GetByIndex (index);
180}
181
182bool
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000183BreakpointSite::ValidForThisThread (Thread *thread)
184{
185 return m_owners.ValidForThisThread(thread);
186}
187
188bool
Chris Lattner24943d22010-06-08 16:52:24 +0000189BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size, lldb::addr_t *intersect_addr, size_t *intersect_size, size_t *opcode_offset) const
190{
191 // We only use software traps for software breakpoints
192 if (!IsHardware())
193 {
194 if (m_byte_size > 0)
195 {
196 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
197 const lldb::addr_t end_addr = addr + size;
198 // Is the breakpoint end address before the passed in start address?
199 if (bp_end_addr <= addr)
200 return false;
201 // Is the breakpoint start address after passed in end address?
202 if (end_addr <= m_addr)
203 return false;
204 if (intersect_addr || intersect_size || opcode_offset)
205 {
206 if (m_addr < addr)
207 {
208 if (intersect_addr)
209 *intersect_addr = addr;
210 if (intersect_size)
211 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
212 if (opcode_offset)
213 *opcode_offset = addr - m_addr;
214 }
215 else
216 {
217 if (intersect_addr)
218 *intersect_addr = m_addr;
219 if (intersect_size)
220 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
221 if (opcode_offset)
222 *opcode_offset = 0;
223 }
224 }
225 return true;
226 }
227 }
228 return false;
229}