blob: 6536002bda67ff64ea7273e2c36ae15c19bd0910 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- BreakpointLocationCollection.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
12// Other libraries and framework includes
13// Project includes
14#include "lldb/Breakpoint/BreakpointLocationCollection.h"
Greg Clayton4e78f602010-11-18 18:52:36 +000015#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Breakpoint/BreakpointLocation.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Core/ModuleList.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000018#include "lldb/Target/Thread.h"
19#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24//----------------------------------------------------------------------
25// BreakpointLocationCollection constructor
26//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000027BreakpointLocationCollection::BreakpointLocationCollection()
28 : m_break_loc_collection(), m_collection_mutex() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30//----------------------------------------------------------------------
31// Destructor
32//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000033BreakpointLocationCollection::~BreakpointLocationCollection() {}
34
35void BreakpointLocationCollection::Add(const BreakpointLocationSP &bp_loc) {
36 std::lock_guard<std::mutex> guard(m_collection_mutex);
37 BreakpointLocationSP old_bp_loc =
38 FindByIDPair(bp_loc->GetBreakpoint().GetID(), bp_loc->GetID());
39 if (!old_bp_loc.get())
40 m_break_loc_collection.push_back(bp_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041}
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043bool BreakpointLocationCollection::Remove(lldb::break_id_t bp_id,
44 lldb::break_id_t bp_loc_id) {
45 std::lock_guard<std::mutex> guard(m_collection_mutex);
46 collection::iterator pos = GetIDPairIterator(bp_id, bp_loc_id); // Predicate
47 if (pos != m_break_loc_collection.end()) {
48 m_break_loc_collection.erase(pos);
49 return true;
50 }
51 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054class BreakpointIDPairMatches {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 BreakpointIDPairMatches(lldb::break_id_t break_id,
57 lldb::break_id_t break_loc_id)
58 : m_break_id(break_id), m_break_loc_id(break_loc_id) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 bool operator()(const BreakpointLocationSP &bp_loc) const {
61 return m_break_id == bp_loc->GetBreakpoint().GetID() &&
62 m_break_loc_id == bp_loc->GetID();
63 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
65private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 const lldb::break_id_t m_break_id;
67 const lldb::break_id_t m_break_loc_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068};
69
70BreakpointLocationCollection::collection::iterator
Kate Stoneb9c1b512016-09-06 20:57:50 +000071BreakpointLocationCollection::GetIDPairIterator(lldb::break_id_t break_id,
72 lldb::break_id_t break_loc_id) {
73 return std::find_if(
74 m_break_loc_collection.begin(),
75 m_break_loc_collection.end(), // Search full range
76 BreakpointIDPairMatches(break_id, break_loc_id)); // Predicate
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
79BreakpointLocationCollection::collection::const_iterator
Kate Stoneb9c1b512016-09-06 20:57:50 +000080BreakpointLocationCollection::GetIDPairConstIterator(
81 lldb::break_id_t break_id, lldb::break_id_t break_loc_id) const {
82 return std::find_if(
83 m_break_loc_collection.begin(),
84 m_break_loc_collection.end(), // Search full range
85 BreakpointIDPairMatches(break_id, break_loc_id)); // Predicate
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086}
87
88BreakpointLocationSP
Kate Stoneb9c1b512016-09-06 20:57:50 +000089BreakpointLocationCollection::FindByIDPair(lldb::break_id_t break_id,
90 lldb::break_id_t break_loc_id) {
91 BreakpointLocationSP stop_sp;
92 collection::iterator pos = GetIDPairIterator(break_id, break_loc_id);
93 if (pos != m_break_loc_collection.end())
94 stop_sp = *pos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 return stop_sp;
97}
98
99const BreakpointLocationSP BreakpointLocationCollection::FindByIDPair(
100 lldb::break_id_t break_id, lldb::break_id_t break_loc_id) const {
101 BreakpointLocationSP stop_sp;
102 collection::const_iterator pos =
103 GetIDPairConstIterator(break_id, break_loc_id);
104 if (pos != m_break_loc_collection.end())
105 stop_sp = *pos;
106
107 return stop_sp;
108}
109
110BreakpointLocationSP BreakpointLocationCollection::GetByIndex(size_t i) {
111 std::lock_guard<std::mutex> guard(m_collection_mutex);
112 BreakpointLocationSP stop_sp;
113 if (i < m_break_loc_collection.size())
114 stop_sp = m_break_loc_collection[i];
115
116 return stop_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117}
118
119const BreakpointLocationSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120BreakpointLocationCollection::GetByIndex(size_t i) const {
121 std::lock_guard<std::mutex> guard(m_collection_mutex);
122 BreakpointLocationSP stop_sp;
123 if (i < m_break_loc_collection.size())
124 stop_sp = m_break_loc_collection[i];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 return stop_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129bool BreakpointLocationCollection::ShouldStop(
130 StoppointCallbackContext *context) {
131 bool shouldStop = false;
132 size_t i = 0;
133 size_t prev_size = GetSize();
134 while (i < prev_size) {
135 // ShouldStop can remove the breakpoint from the list
136 if (GetByIndex(i)->ShouldStop(context))
137 shouldStop = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 if (prev_size == GetSize())
140 i++;
141 prev_size = GetSize();
142 }
143 return shouldStop;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146bool BreakpointLocationCollection::ValidForThisThread(Thread *thread) {
147 std::lock_guard<std::mutex> guard(m_collection_mutex);
148 collection::iterator pos, begin = m_break_loc_collection.begin(),
149 end = m_break_loc_collection.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 for (pos = begin; pos != end; ++pos) {
152 if ((*pos)->ValidForThisThread(thread))
153 return true;
154 }
155 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158bool BreakpointLocationCollection::IsInternal() const {
159 std::lock_guard<std::mutex> guard(m_collection_mutex);
160 collection::const_iterator pos, begin = m_break_loc_collection.begin(),
161 end = m_break_loc_collection.end();
Tamas Berghammer729dcfb2015-05-27 09:46:47 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 bool is_internal = true;
164
165 for (pos = begin; pos != end; ++pos) {
166 if (!(*pos)->GetBreakpoint().IsInternal()) {
167 is_internal = false;
168 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 }
171 return is_internal;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172}
173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174void BreakpointLocationCollection::GetDescription(
175 Stream *s, lldb::DescriptionLevel level) {
176 std::lock_guard<std::mutex> guard(m_collection_mutex);
177 collection::iterator pos, begin = m_break_loc_collection.begin(),
178 end = m_break_loc_collection.end();
Jim Ingham1b54c882010-06-16 02:00:15 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 for (pos = begin; pos != end; ++pos) {
181 if (pos != begin)
182 s->PutChar(' ');
183 (*pos)->GetDescription(s, level);
184 }
Jim Ingham1b54c882010-06-16 02:00:15 +0000185}