blob: a5c5136eb7a6704ff70816c4e219da20d189926d [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
Zachary Turnerfc858812015-03-04 17:43:00 +000012#include <inttypes.h>
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014// Other libraries and framework includes
15// Project includes
Eugene Zelenko16fd7512015-10-30 18:50:12 +000016#include "lldb/Breakpoint/BreakpointSite.h"
17
Greg Clayton4e78f602010-11-18 18:52:36 +000018#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Breakpoint/BreakpointLocation.h"
20#include "lldb/Breakpoint/BreakpointSiteList.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000021#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026BreakpointSite::BreakpointSite(BreakpointSiteList *list,
27 const BreakpointLocationSP &owner,
28 lldb::addr_t addr, bool use_hardware)
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000029 : StoppointLocation(GetNextID(), addr, 0, use_hardware),
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 m_type(eSoftware), // Process subclasses need to set this correctly using
31 // SetType()
32 m_saved_opcode(), m_trap_opcode(),
33 m_enabled(false), // Need to create it disabled, so the first enable turns
34 // it on.
35 m_owners(), m_owners_mutex() {
36 m_owners.Add(owner);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039BreakpointSite::~BreakpointSite() {
40 BreakpointLocationSP bp_loc_sp;
41 const size_t owner_count = m_owners.GetSize();
42 for (size_t i = 0; i < owner_count; i++) {
43 m_owners.GetByIndex(i)->ClearBreakpointSite();
44 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047break_id_t BreakpointSite::GetNextID() {
48 static break_id_t g_next_id = 0;
49 return ++g_next_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050}
51
52// RETURNS - true if we should stop at this breakpoint, false if we
53// should continue.
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055bool BreakpointSite::ShouldStop(StoppointCallbackContext *context) {
56 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
57 IncrementHitCount();
58 return m_owners.ShouldStop(context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061bool BreakpointSite::IsBreakpointAtThisSite(lldb::break_id_t bp_id) {
62 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
63 const size_t owner_count = m_owners.GetSize();
64 for (size_t i = 0; i < owner_count; i++) {
65 if (m_owners.GetByIndex(i)->GetBreakpoint().GetID() == bp_id)
66 return true;
67 }
68 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069}
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071void BreakpointSite::Dump(Stream *s) const {
72 if (s == nullptr)
73 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 s->Printf("BreakpointSite %u: addr = 0x%8.8" PRIx64
76 " type = %s breakpoint hw_index = %i hit_count = %-4u",
77 GetID(), (uint64_t)m_addr, IsHardware() ? "hardware" : "software",
78 GetHardwareIndex(), GetHitCount());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081void BreakpointSite::GetDescription(Stream *s, lldb::DescriptionLevel level) {
82 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
83 if (level != lldb::eDescriptionLevelBrief)
84 s->Printf("breakpoint site: %d at 0x%8.8" PRIx64, GetID(),
85 GetLoadAddress());
86 m_owners.GetDescription(s, level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087}
88
Kate Stoneb9c1b512016-09-06 20:57:50 +000089bool BreakpointSite::IsInternal() const { return m_owners.IsInternal(); }
90
91uint8_t *BreakpointSite::GetTrapOpcodeBytes() { return &m_trap_opcode[0]; }
92
93const uint8_t *BreakpointSite::GetTrapOpcodeBytes() const {
94 return &m_trap_opcode[0];
Jim Ingham29950772013-01-26 02:19:28 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097size_t BreakpointSite::GetTrapOpcodeMaxByteSize() const {
98 return sizeof(m_trap_opcode);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101bool BreakpointSite::SetTrapOpcode(const uint8_t *trap_opcode,
102 uint32_t trap_opcode_size) {
103 if (trap_opcode_size > 0 && trap_opcode_size <= sizeof(m_trap_opcode)) {
104 m_byte_size = trap_opcode_size;
105 ::memcpy(m_trap_opcode, trap_opcode, trap_opcode_size);
106 return true;
107 }
108 m_byte_size = 0;
109 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112uint8_t *BreakpointSite::GetSavedOpcodeBytes() { return &m_saved_opcode[0]; }
113
114const uint8_t *BreakpointSite::GetSavedOpcodeBytes() const {
115 return &m_saved_opcode[0];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118bool BreakpointSite::IsEnabled() const { return m_enabled; }
119
120void BreakpointSite::SetEnabled(bool enabled) { m_enabled = enabled; }
121
122void BreakpointSite::AddOwner(const BreakpointLocationSP &owner) {
123 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
124 m_owners.Add(owner);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127size_t BreakpointSite::RemoveOwner(lldb::break_id_t break_id,
128 lldb::break_id_t break_loc_id) {
129 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
130 m_owners.Remove(break_id, break_loc_id);
131 return m_owners.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134size_t BreakpointSite::GetNumberOfOwners() {
135 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
136 return m_owners.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137}
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139BreakpointLocationSP BreakpointSite::GetOwnerAtIndex(size_t index) {
140 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
141 return m_owners.GetByIndex(index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142}
143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144bool BreakpointSite::ValidForThisThread(Thread *thread) {
145 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
146 return m_owners.ValidForThisThread(thread);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149void BreakpointSite::BumpHitCounts() {
150 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
151 for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
152 loc_sp->BumpHitCount();
153 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156bool BreakpointSite::IntersectsRange(lldb::addr_t addr, size_t size,
157 lldb::addr_t *intersect_addr,
158 size_t *intersect_size,
159 size_t *opcode_offset) const {
160 // We only use software traps for software breakpoints
161 if (!IsHardware()) {
162 if (m_byte_size > 0) {
163 const lldb::addr_t bp_end_addr = m_addr + m_byte_size;
164 const lldb::addr_t end_addr = addr + size;
165 // Is the breakpoint end address before the passed in start address?
166 if (bp_end_addr <= addr)
167 return false;
168 // Is the breakpoint start address after passed in end address?
169 if (end_addr <= m_addr)
170 return false;
171 if (intersect_addr || intersect_size || opcode_offset) {
172 if (m_addr < addr) {
173 if (intersect_addr)
174 *intersect_addr = addr;
175 if (intersect_size)
176 *intersect_size =
177 std::min<lldb::addr_t>(bp_end_addr, end_addr) - addr;
178 if (opcode_offset)
179 *opcode_offset = addr - m_addr;
180 } else {
181 if (intersect_addr)
182 *intersect_addr = m_addr;
183 if (intersect_size)
184 *intersect_size =
185 std::min<lldb::addr_t>(bp_end_addr, end_addr) - m_addr;
186 if (opcode_offset)
187 *opcode_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 }
190 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 }
193 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194}
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000195
196size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197BreakpointSite::CopyOwnersList(BreakpointLocationCollection &out_collection) {
198 std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
199 for (BreakpointLocationSP loc_sp : m_owners.BreakpointLocations()) {
200 out_collection.Add(loc_sp);
201 }
202 return out_collection.GetSize();
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000203}