LLDB  mainline
BreakpointList.cpp
Go to the documentation of this file.
1 //===-- BreakpointList.cpp ------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "lldb/Target/Target.h"
12 
13 #include "llvm/Support/Errc.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
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));
23 }
24 
25 BreakpointList::BreakpointList(bool is_internal)
26  : m_next_break_id(0), m_is_internal(is_internal) {}
27 
29 
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 
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 
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 
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 
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 
81 void BreakpointList::RemoveAll(bool notify) {
82  std::lock_guard<std::recursive_mutex> guard(m_mutex);
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 
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  llvm::erase_if(m_breakpoints,
104  [&](const BreakpointSP &bp) { return bp->AllowDelete(); });
105 }
106 
107 BreakpointList::bp_collection::iterator
109  return std::find_if(
110  m_breakpoints.begin(), m_breakpoints.end(),
111  [&](const BreakpointSP &bp) { return bp->GetID() == break_id; });
112 }
113 
114 BreakpointList::bp_collection::const_iterator
116  return std::find_if(
117  m_breakpoints.begin(), m_breakpoints.end(),
118  [&](const BreakpointSP &bp) { return bp->GetID() == break_id; });
119 }
120 
121 BreakpointSP BreakpointList::FindBreakpointByID(break_id_t break_id) const {
122  std::lock_guard<std::recursive_mutex> guard(m_mutex);
123 
124  auto it = GetBreakpointIDConstIterator(break_id);
125  if (it != m_breakpoints.end())
126  return *it;
127  return {};
128 }
129 
130 llvm::Expected<std::vector<lldb::BreakpointSP>>
132  if (!name)
133  return llvm::createStringError(llvm::errc::invalid_argument,
134  "FindBreakpointsByName requires a name");
135 
136  Status error;
137  if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(name), error))
138  return error.ToError();
139 
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);
144  }
145  }
146 
147  return matching_bps;
148 }
149 
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));
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 
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];
166  return {};
167 }
168 
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 
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 
184  std::lock_guard<std::recursive_mutex> guard(m_mutex);
185  for (const auto &bp_sp : m_breakpoints)
186  bp_sp->ClearAllBreakpointSites();
187 }
188 
190  std::unique_lock<std::recursive_mutex> &lock) {
191  lock = std::unique_lock<std::recursive_mutex>(m_mutex);
192 }
lldb_private::Stream::IndentLess
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:171
lldb_private::BreakpointList::RemoveAllowed
void RemoveAllowed(bool notify)
Removes all the breakpoints from this list - first checking the ePermDelete on the breakpoints.
Definition: BreakpointList.cpp:93
lldb_private::BreakpointList::Breakpoints
BreakpointIterable Breakpoints()
Definition: BreakpointList.h:166
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::BreakpointList::m_mutex
std::recursive_mutex m_mutex
Definition: BreakpointList.h:157
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Breakpoint::BreakpointEventData
Definition: Breakpoint.h:102
lldb_private::Target
Definition: Target.h:467
lldb_private::BreakpointList::m_breakpoints
bp_collection m_breakpoints
Definition: BreakpointList.h:158
Target.h
lldb_private::BreakpointList::UpdateBreakpoints
void UpdateBreakpoints(ModuleList &module_list, bool load, bool delete_locations)
Tell all the breakpoints to update themselves due to a change in the modules in module_list.
Definition: BreakpointList.cpp:169
BreakpointList.h
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Stream::Indent
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:130
lldb_private::ModuleList
Definition: ModuleList.h:81
lldb_private::BreakpointID::StringIsBreakpointName
static bool StringIsBreakpointName(llvm::StringRef str, Status &error)
Takes an input string and checks to see whether it is a breakpoint name.
Definition: BreakpointID.cpp:90
lldb::break_id_t
int32_t break_id_t
Definition: lldb-types.h:88
lldb_private::BreakpointList::Add
lldb::break_id_t Add(lldb::BreakpointSP &bp_sp, bool notify)
Add the breakpoint bp_sp to the list.
Definition: BreakpointList.cpp:30
NotifyChange
static void NotifyChange(const BreakpointSP &bp, BreakpointEventType event)
Definition: BreakpointList.cpp:18
lldb_private::Broadcaster::EventTypeHasListeners
bool EventTypeHasListeners(uint32_t event_type)
Definition: Broadcaster.h:345
lldb_private::BreakpointList::SetEnabledAllowed
void SetEnabledAllowed(bool enabled)
Definition: BreakpointList.cpp:74
lldb_private::BreakpointList::GetListMutex
void GetListMutex(std::unique_lock< std::recursive_mutex > &lock)
Sets the passed in Locker to hold the Breakpoint List mutex.
Definition: BreakpointList.cpp:189
lldb_private::Broadcaster::BroadcastEvent
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
Definition: Broadcaster.h:262
lldb_private::Status
Definition: Status.h:44
lldb_private::BreakpointList::GetBreakpointAtIndex
lldb::BreakpointSP GetBreakpointAtIndex(size_t i) const
Returns a shared pointer to the breakpoint with index i.
Definition: BreakpointList.cpp:162
lldb_private::BreakpointList::Dump
void Dump(Stream *s) const
Standard "Dump" method. At present it does nothing.
Definition: BreakpointList.cpp:150
uint32_t
lldb_private::Stream::IndentMore
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:168
lldb_private::BreakpointList::GetBreakpointIDIterator
bp_collection::iterator GetBreakpointIDIterator(lldb::break_id_t breakID)
Definition: BreakpointList.cpp:108
lldb_private::BreakpointList::RemoveInvalidLocations
void RemoveInvalidLocations(const ArchSpec &arch)
Removes all invalid breakpoint locations.
Definition: BreakpointList.cpp:62
lldb_private::BreakpointList::SetEnabledAll
void SetEnabledAll(bool enabled)
Definition: BreakpointList.cpp:68
lldb_private::BreakpointList::GetBreakpointIDConstIterator
bp_collection::const_iterator GetBreakpointIDConstIterator(lldb::break_id_t breakID) const
Definition: BreakpointList.cpp:115
lldb_private::BreakpointList::ClearAllBreakpointSites
void ClearAllBreakpointSites()
Definition: BreakpointList.cpp:183
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::BreakpointList::m_is_internal
bool m_is_internal
Definition: BreakpointList.h:160
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::BreakpointList::UpdateBreakpointsWhenModuleIsReplaced
void UpdateBreakpointsWhenModuleIsReplaced(lldb::ModuleSP old_module_sp, lldb::ModuleSP new_module_sp)
Definition: BreakpointList.cpp:176
lldb_private::BreakpointList::RemoveAll
void RemoveAll(bool notify)
Removes all the breakpoints from this list.
Definition: BreakpointList.cpp:81
lldb_private::BreakpointList::FindBreakpointsByName
llvm::Expected< std::vector< lldb::BreakpointSP > > FindBreakpointsByName(const char *name)
Find all the breakpoints with a given name.
Definition: BreakpointList.cpp:131
lldb_private::BreakpointList::Remove
bool Remove(lldb::break_id_t breakID, bool notify)
Removes the breakpoint given by breakID from this list.
Definition: BreakpointList.cpp:44
lldb_private::BreakpointList::FindBreakpointByID
lldb::BreakpointSP FindBreakpointByID(lldb::break_id_t breakID) const
Returns a shared pointer to the breakpoint with id breakID.
Definition: BreakpointList.cpp:121
lldb_private::BreakpointList::m_next_break_id
lldb::break_id_t m_next_break_id
Definition: BreakpointList.h:159
lldb
Definition: SBAddress.h:15
lldb_private::BreakpointList::~BreakpointList
~BreakpointList()