1 //===-- BreakpointList.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/BreakpointList.h"
11
12 #include "lldb/Target/Target.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
NotifyChange(const BreakpointSP & bp,BreakpointEventType event)17 static void NotifyChange(const BreakpointSP &bp, BreakpointEventType event) {
18 Target &target = bp->GetTarget();
19 if (target.EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
20 target.BroadcastEvent(Target::eBroadcastBitBreakpointChanged,
21 new Breakpoint::BreakpointEventData(event, bp));
22 }
23
BreakpointList(bool is_internal)24 BreakpointList::BreakpointList(bool is_internal)
25 : m_mutex(), m_breakpoints(), m_next_break_id(0),
26 m_is_internal(is_internal) {}
27
~BreakpointList()28 BreakpointList::~BreakpointList() {}
29
Add(BreakpointSP & bp_sp,bool notify)30 break_id_t BreakpointList::Add(BreakpointSP &bp_sp, bool notify) {
31 std::lock_guard<std::recursive_mutex> guard(m_mutex);
32
33 // Internal breakpoint IDs are negative, normal ones are positive
34 bp_sp->SetID(m_is_internal ? --m_next_break_id : ++m_next_break_id);
35
36 m_breakpoints.push_back(bp_sp);
37
38 if (notify)
39 NotifyChange(bp_sp, eBreakpointEventTypeAdded);
40
41 return bp_sp->GetID();
42 }
43
Remove(break_id_t break_id,bool notify)44 bool BreakpointList::Remove(break_id_t break_id, bool notify) {
45 std::lock_guard<std::recursive_mutex> guard(m_mutex);
46
47 auto it = std::find_if(
48 m_breakpoints.begin(), m_breakpoints.end(),
49 [&](const BreakpointSP &bp) { return bp->GetID() == break_id; });
50
51 if (it == m_breakpoints.end())
52 return false;
53
54 if (notify)
55 NotifyChange(*it, eBreakpointEventTypeRemoved);
56
57 m_breakpoints.erase(it);
58
59 return true;
60 }
61
RemoveInvalidLocations(const ArchSpec & arch)62 void BreakpointList::RemoveInvalidLocations(const ArchSpec &arch) {
63 std::lock_guard<std::recursive_mutex> guard(m_mutex);
64 for (const auto &bp_sp : m_breakpoints)
65 bp_sp->RemoveInvalidLocations(arch);
66 }
67
SetEnabledAll(bool enabled)68 void BreakpointList::SetEnabledAll(bool enabled) {
69 std::lock_guard<std::recursive_mutex> guard(m_mutex);
70 for (const auto &bp_sp : m_breakpoints)
71 bp_sp->SetEnabled(enabled);
72 }
73
SetEnabledAllowed(bool enabled)74 void BreakpointList::SetEnabledAllowed(bool enabled) {
75 std::lock_guard<std::recursive_mutex> guard(m_mutex);
76 for (const auto &bp_sp : m_breakpoints)
77 if (bp_sp->AllowDisable())
78 bp_sp->SetEnabled(enabled);
79 }
80
RemoveAll(bool notify)81 void BreakpointList::RemoveAll(bool notify) {
82 std::lock_guard<std::recursive_mutex> guard(m_mutex);
83 ClearAllBreakpointSites();
84
85 if (notify) {
86 for (const auto &bp_sp : m_breakpoints)
87 NotifyChange(bp_sp, eBreakpointEventTypeRemoved);
88 }
89
90 m_breakpoints.clear();
91 }
92
RemoveAllowed(bool notify)93 void BreakpointList::RemoveAllowed(bool notify) {
94 std::lock_guard<std::recursive_mutex> guard(m_mutex);
95
96 for (const auto &bp_sp : m_breakpoints) {
97 if (bp_sp->AllowDelete())
98 bp_sp->ClearAllBreakpointSites();
99 if (notify)
100 NotifyChange(bp_sp, eBreakpointEventTypeRemoved);
101 }
102
103 m_breakpoints.erase(
104 std::remove_if(m_breakpoints.begin(), m_breakpoints.end(),
105 [&](const BreakpointSP &bp) { return bp->AllowDelete(); }),
106 m_breakpoints.end());
107 }
108
109 BreakpointList::bp_collection::iterator
GetBreakpointIDIterator(break_id_t break_id)110 BreakpointList::GetBreakpointIDIterator(break_id_t break_id) {
111 return std::find_if(
112 m_breakpoints.begin(), m_breakpoints.end(),
113 [&](const BreakpointSP &bp) { return bp->GetID() == break_id; });
114 }
115
116 BreakpointList::bp_collection::const_iterator
GetBreakpointIDConstIterator(break_id_t break_id) const117 BreakpointList::GetBreakpointIDConstIterator(break_id_t break_id) const {
118 return std::find_if(
119 m_breakpoints.begin(), m_breakpoints.end(),
120 [&](const BreakpointSP &bp) { return bp->GetID() == break_id; });
121 }
122
FindBreakpointByID(break_id_t break_id) const123 BreakpointSP BreakpointList::FindBreakpointByID(break_id_t break_id) const {
124 std::lock_guard<std::recursive_mutex> guard(m_mutex);
125
126 auto it = GetBreakpointIDConstIterator(break_id);
127 if (it != m_breakpoints.end())
128 return *it;
129 return {};
130 }
131
FindBreakpointsByName(const char * name,BreakpointList & matching_bps)132 bool BreakpointList::FindBreakpointsByName(const char *name,
133 BreakpointList &matching_bps) {
134 Status error;
135 if (!name)
136 return false;
137
138 if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(name), error))
139 return false;
140
141 for (BreakpointSP bkpt_sp : Breakpoints()) {
142 if (bkpt_sp->MatchesName(name)) {
143 matching_bps.Add(bkpt_sp, false);
144 }
145 }
146
147 return true;
148 }
149
Dump(Stream * s) const150 void BreakpointList::Dump(Stream *s) const {
151 std::lock_guard<std::recursive_mutex> guard(m_mutex);
152 s->Printf("%p: ", static_cast<const void *>(this));
153 s->Indent();
154 s->Printf("BreakpointList with %u Breakpoints:\n",
155 (uint32_t)m_breakpoints.size());
156 s->IndentMore();
157 for (const auto &bp_sp : m_breakpoints)
158 bp_sp->Dump(s);
159 s->IndentLess();
160 }
161
GetBreakpointAtIndex(size_t i) const162 BreakpointSP BreakpointList::GetBreakpointAtIndex(size_t i) const {
163 std::lock_guard<std::recursive_mutex> guard(m_mutex);
164 if (i < m_breakpoints.size())
165 return m_breakpoints[i];
166 return {};
167 }
168
UpdateBreakpoints(ModuleList & module_list,bool added,bool delete_locations)169 void BreakpointList::UpdateBreakpoints(ModuleList &module_list, bool added,
170 bool delete_locations) {
171 std::lock_guard<std::recursive_mutex> guard(m_mutex);
172 for (const auto &bp_sp : m_breakpoints)
173 bp_sp->ModulesChanged(module_list, added, delete_locations);
174 }
175
UpdateBreakpointsWhenModuleIsReplaced(ModuleSP old_module_sp,ModuleSP new_module_sp)176 void BreakpointList::UpdateBreakpointsWhenModuleIsReplaced(
177 ModuleSP old_module_sp, ModuleSP new_module_sp) {
178 std::lock_guard<std::recursive_mutex> guard(m_mutex);
179 for (const auto &bp_sp : m_breakpoints)
180 bp_sp->ModuleReplaced(old_module_sp, new_module_sp);
181 }
182
ClearAllBreakpointSites()183 void BreakpointList::ClearAllBreakpointSites() {
184 std::lock_guard<std::recursive_mutex> guard(m_mutex);
185 for (const auto &bp_sp : m_breakpoints)
186 bp_sp->ClearAllBreakpointSites();
187 }
188
GetListMutex(std::unique_lock<std::recursive_mutex> & lock)189 void BreakpointList::GetListMutex(
190 std::unique_lock<std::recursive_mutex> &lock) {
191 lock = std::unique_lock<std::recursive_mutex>(m_mutex);
192 }
193