blob: cd0920d07c7523ab550a0d9b94e1d6f405aa4d25 [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
83 s->Printf("BreakpointSite %u: tid = %4.4x addr = 0x%8.8llx type = %s breakpoint hw_index = %i hit_count = %-4u",
84 GetID(),
85 m_tid,
86 (uint64_t)m_addr,
87 IsHardware() ? "hardware" : "software",
88 GetHardwareIndex(),
89 GetHitCount());
90}
91
92void
93BreakpointSite::GetDescription (Stream *s, lldb::DescriptionLevel level)
94{
95 if (level != lldb::eDescriptionLevelBrief)
96 s->Printf ("breakpoint site: %d ", GetID());
97 m_owners.GetDescription (s, level);
98}
99
100uint8_t *
101BreakpointSite::GetTrapOpcodeBytes()
102{
103 return &m_trap_opcode[0];
104}
105
106const uint8_t *
107BreakpointSite::GetTrapOpcodeBytes() const
108{
109 return &m_trap_opcode[0];
110}
111
112size_t
113BreakpointSite::GetTrapOpcodeMaxByteSize() const
114{
115 return sizeof(m_trap_opcode);
116}
117
118bool
119BreakpointSite::SetTrapOpcode (const uint8_t *trap_opcode, size_t trap_opcode_size)
120{
121 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode))
122 {
123 m_byte_size = trap_opcode_size;
124 ::memcpy (m_trap_opcode, trap_opcode, trap_opcode_size);
125 return true;
126 }
127 m_byte_size = 0;
128 return false;
129}
130
131uint8_t *
132BreakpointSite::GetSavedOpcodeBytes()
133{
134 return &m_saved_opcode[0];
135}
136
137const uint8_t *
138BreakpointSite::GetSavedOpcodeBytes() const
139{
140 return &m_saved_opcode[0];
141}
142
143bool
144BreakpointSite::IsEnabled () const
145{
146 return m_enabled;
147}
148
149void
150BreakpointSite::SetEnabled (bool enabled)
151{
152 m_enabled = enabled;
153}
154
155void
156BreakpointSite::AddOwner (BreakpointLocationSP &owner)
157{
158 m_owners.Add(owner);
159}
160
161uint32_t
162BreakpointSite::RemoveOwner (lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
163{
164 m_owners.Remove(break_id, break_loc_id);
165 return m_owners.GetSize();
166}
167
168uint32_t
169BreakpointSite::GetNumberOfOwners ()
170{
171 return m_owners.GetSize();
172}
173
174BreakpointLocationSP
175BreakpointSite::GetOwnerAtIndex (uint32_t index)
176{
177 return m_owners.GetByIndex (index);
178}
179
180bool
181BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size, lldb::addr_t *intersect_addr, size_t *intersect_size, size_t *opcode_offset) const
182{
183 // We only use software traps for software breakpoints
184 if (!IsHardware())
185 {
186 if (m_byte_size > 0)
187 {
188 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
189 const lldb::addr_t end_addr = addr + size;
190 // Is the breakpoint end address before the passed in start address?
191 if (bp_end_addr <= addr)
192 return false;
193 // Is the breakpoint start address after passed in end address?
194 if (end_addr <= m_addr)
195 return false;
196 if (intersect_addr || intersect_size || opcode_offset)
197 {
198 if (m_addr < addr)
199 {
200 if (intersect_addr)
201 *intersect_addr = addr;
202 if (intersect_size)
203 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
204 if (opcode_offset)
205 *opcode_offset = addr - m_addr;
206 }
207 else
208 {
209 if (intersect_addr)
210 *intersect_addr = m_addr;
211 if (intersect_size)
212 *intersect_size = std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
213 if (opcode_offset)
214 *opcode_offset = 0;
215 }
216 }
217 return true;
218 }
219 }
220 return false;
221}