1 //===-- BreakpointList.cpp ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Breakpoint/BreakpointList.h"
11 #include "lldb/Target/Target.h"
13 #include "llvm/Support/Errc.h"
16 using namespace lldb_private
;
18 static void NotifyChange(const BreakpointSP
&bp
, BreakpointEventType event
) {
19 Target
&target
= bp
->GetTarget();
20 if (target
.EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged
))
21 target
.BroadcastEvent(Target::eBroadcastBitBreakpointChanged
,
22 new Breakpoint::BreakpointEventData(event
, bp
));
25 BreakpointList::BreakpointList(bool is_internal
)
26 : m_next_break_id(0), m_is_internal(is_internal
) {}
28 BreakpointList::~BreakpointList() = default;
30 break_id_t
BreakpointList::Add(BreakpointSP
&bp_sp
, bool notify
) {
31 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
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
);
36 m_breakpoints
.push_back(bp_sp
);
39 NotifyChange(bp_sp
, eBreakpointEventTypeAdded
);
41 return bp_sp
->GetID();
44 bool BreakpointList::Remove(break_id_t break_id
, bool notify
) {
45 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
47 auto it
= std::find_if(
48 m_breakpoints
.begin(), m_breakpoints
.end(),
49 [&](const BreakpointSP
&bp
) { return bp
->GetID() == break_id
; });
51 if (it
== m_breakpoints
.end())
55 NotifyChange(*it
, eBreakpointEventTypeRemoved
);
57 m_breakpoints
.erase(it
);
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
);
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
);
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
);
81 void BreakpointList::RemoveAll(bool notify
) {
82 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
83 ClearAllBreakpointSites();
86 for (const auto &bp_sp
: m_breakpoints
)
87 NotifyChange(bp_sp
, eBreakpointEventTypeRemoved
);
90 m_breakpoints
.clear();
93 void BreakpointList::RemoveAllowed(bool notify
) {
94 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
96 for (const auto &bp_sp
: m_breakpoints
) {
97 if (bp_sp
->AllowDelete())
98 bp_sp
->ClearAllBreakpointSites();
100 NotifyChange(bp_sp
, eBreakpointEventTypeRemoved
);
103 llvm::erase_if(m_breakpoints
,
104 [&](const BreakpointSP
&bp
) { return bp
->AllowDelete(); });
107 BreakpointList::bp_collection::iterator
108 BreakpointList::GetBreakpointIDIterator(break_id_t break_id
) {
110 m_breakpoints
.begin(), m_breakpoints
.end(),
111 [&](const BreakpointSP
&bp
) { return bp
->GetID() == break_id
; });
114 BreakpointList::bp_collection::const_iterator
115 BreakpointList::GetBreakpointIDConstIterator(break_id_t break_id
) const {
117 m_breakpoints
.begin(), m_breakpoints
.end(),
118 [&](const BreakpointSP
&bp
) { return bp
->GetID() == break_id
; });
121 BreakpointSP
BreakpointList::FindBreakpointByID(break_id_t break_id
) const {
122 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
124 auto it
= GetBreakpointIDConstIterator(break_id
);
125 if (it
!= m_breakpoints
.end())
130 llvm::Expected
<std::vector
<lldb::BreakpointSP
>>
131 BreakpointList::FindBreakpointsByName(const char *name
) {
133 return llvm::createStringError(llvm::errc::invalid_argument
,
134 "FindBreakpointsByName requires a name");
137 if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(name
), error
))
138 return error
.ToError();
140 std::vector
<lldb::BreakpointSP
> matching_bps
;
141 for (BreakpointSP bkpt_sp
: Breakpoints()) {
142 if (bkpt_sp
->MatchesName(name
)) {
143 matching_bps
.push_back(bkpt_sp
);
150 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));
154 s
->Printf("BreakpointList with %u Breakpoints:\n",
155 (uint32_t)m_breakpoints
.size());
157 for (const auto &bp_sp
: m_breakpoints
)
162 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
];
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
);
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
);
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();
189 void BreakpointList::ResetHitCounts() {
190 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
191 for (const auto &bp_sp
: m_breakpoints
)
192 bp_sp
->ResetHitCount();
195 void BreakpointList::GetListMutex(
196 std::unique_lock
<std::recursive_mutex
> &lock
) {
197 lock
= std::unique_lock
<std::recursive_mutex
>(m_mutex
);