LLDB mainline
BreakpointIDList.cpp
Go to the documentation of this file.
1//===-- BreakpointIDList.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
11
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
18#include "lldb/Utility/Args.h"
20#include "lldb/lldb-forward.h"
21
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/StringRef.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
28// class BreakpointIDList
29
31
33
34size_t BreakpointIDList::GetSize() const { return m_breakpoint_ids.size(); }
35
37 return ((index < m_breakpoint_ids.size()) ? m_breakpoint_ids[index]
38 : BreakpointID());
39}
40
42 if (index >= m_breakpoint_ids.size())
43 return false;
44
45 m_breakpoint_ids.erase(m_breakpoint_ids.begin() + index);
46 return true;
47}
48
50
52 m_breakpoint_ids.push_back(bp_id);
53
54 return true; // We don't do any verification in this function, so always
55 // return true.
56}
57
59 return llvm::is_contained(m_breakpoint_ids, bp_id);
60}
61
62static std::string LocationIDForStop(StopInfoSP stop_info_sp, uint32_t idx) {
63 assert(stop_info_sp->GetStopReason() == lldb::eStopReasonBreakpoint &&
64 "expected breakpoint stop");
65 break_id_t bp_id = stop_info_sp->GetStopReasonDataAtIndex(idx);
66 break_id_t loc_id = stop_info_sp->GetStopReasonDataAtIndex(idx + 1);
67 StreamString stream;
68 BreakpointID::GetCanonicalReference(&stream, bp_id, loc_id);
69 return stream.GetString().str();
70}
71
72// This function takes OLD_ARGS, which is usually the result of breaking the
73// command string arguments into
74// an array of space-separated strings, and searches through the arguments for
75// any breakpoint ID range specifiers.
76// Any string in the array that is not part of an ID range specifier is copied
77// directly into NEW_ARGS. If any
78// ID range specifiers are found, the range is interpreted and a list of
79// canonical breakpoint IDs corresponding to
80// all the current breakpoints and locations in the range are added to
81// NEW_ARGS. When this function is done,
82// NEW_ARGS should be a copy of OLD_ARGS, with and ID range specifiers replaced
83// by the members of the range.
84
86 Args &old_args, ExecutionContext &exe_ctx, bool allow_locations,
87 BreakpointName::Permissions ::PermissionKinds purpose, Args &new_args) {
88 Target *target = exe_ctx.GetTargetPtr();
89 llvm::StringRef range_from;
90 llvm::StringRef range_to;
91 llvm::StringRef current_arg;
92 std::set<std::string> names_found;
93
94 for (size_t i = 0; i < old_args.size(); ++i) {
95 bool is_range = false;
96
97 current_arg = old_args[i].ref();
98
99 if (allow_locations && current_arg == ".") {
100 Thread *thread = exe_ctx.GetThreadPtr();
101 if (!thread) {
102 new_args.Clear();
103 return llvm::createStringError("no current thread");
104 }
105 StopInfoSP stop_info_sp = thread->GetStopInfo();
106 if (!stop_info_sp ||
107 stop_info_sp->GetStopReason() != eStopReasonBreakpoint) {
108 new_args.Clear();
109 return llvm::createStringError(
110 "current thread is not stopped at a breakpoint");
111 }
112
113 uint32_t data_count = stop_info_sp->GetStopReasonDataCount();
114 for (uint32_t j = 0; j < data_count; j += 2) {
115 std::string location_id = LocationIDForStop(stop_info_sp, j);
116 new_args.AppendArgument(location_id);
117 }
118
119 continue;
120 }
121
122 if (!allow_locations && current_arg.contains('.')) {
123 new_args.Clear();
124 return llvm::createStringError(
125 llvm::inconvertibleErrorCode(),
126 "Breakpoint locations not allowed, saw location: %s.",
127 current_arg.str().c_str());
128 }
129
131
132 std::tie(range_from, range_to) =
134 if (!range_from.empty() && !range_to.empty()) {
135 is_range = true;
136 } else if (BreakpointID::StringIsBreakpointName(current_arg, error)) {
137 if (!error.Success()) {
138 new_args.Clear();
139 return llvm::createStringError(llvm::inconvertibleErrorCode(),
140 error.AsCString());
141 }
142 names_found.insert(std::string(current_arg));
143 } else if ((i + 2 < old_args.size()) &&
144 BreakpointID::IsRangeIdentifier(old_args[i + 1].ref()) &&
146 BreakpointID::IsValidIDExpression(old_args[i + 2].ref())) {
147 range_from = current_arg;
148 range_to = old_args[i + 2].ref();
149 is_range = true;
150 i = i + 2;
151 } else {
152 // See if user has specified id.*
153 llvm::StringRef tmp_str = old_args[i].ref();
154 auto [prefix, suffix] = tmp_str.split('.');
155 if (suffix == "*" && BreakpointID::IsValidIDExpression(prefix)) {
156
157 BreakpointSP breakpoint_sp;
158 auto bp_id = BreakpointID::ParseCanonicalReference(prefix);
159 if (bp_id)
160 breakpoint_sp = target->GetBreakpointByID(bp_id->GetBreakpointID());
161 if (!breakpoint_sp) {
162 new_args.Clear();
163 return llvm::createStringError(llvm::inconvertibleErrorCode(),
164 "'%d' is not a valid breakpoint ID.\n",
165 bp_id->GetBreakpointID());
166 }
167 const size_t num_locations = breakpoint_sp->GetNumLocations();
168 for (size_t j = 0; j < num_locations; ++j) {
169 BreakpointLocation *bp_loc =
170 breakpoint_sp->GetLocationAtIndex(j).get();
171 StreamString canonical_id_str;
173 &canonical_id_str, bp_id->GetBreakpointID(), bp_loc->GetID());
174 new_args.AppendArgument(canonical_id_str.GetString());
175 }
176 }
177 }
178
179 if (!is_range) {
180 new_args.AppendArgument(current_arg);
181 continue;
182 }
183
184 auto start_bp = BreakpointID::ParseCanonicalReference(range_from);
185 auto end_bp = BreakpointID::ParseCanonicalReference(range_to);
186
187 if (!start_bp ||
188 !target->GetBreakpointByID(start_bp->GetBreakpointID())) {
189 new_args.Clear();
190 return llvm::createStringError(llvm::inconvertibleErrorCode(),
191 "'%s' is not a valid breakpoint ID.\n",
192 range_from.str().c_str());
193 }
194
195 if (!end_bp ||
196 !target->GetBreakpointByID(end_bp->GetBreakpointID())) {
197 new_args.Clear();
198 return llvm::createStringError(llvm::inconvertibleErrorCode(),
199 "'%s' is not a valid breakpoint ID.\n",
200 range_to.str().c_str());
201 }
202 break_id_t start_bp_id = start_bp->GetBreakpointID();
203 break_id_t start_loc_id = start_bp->GetLocationID();
204 break_id_t end_bp_id = end_bp->GetBreakpointID();
205 break_id_t end_loc_id = end_bp->GetLocationID();
206 if (((start_loc_id == LLDB_INVALID_BREAK_ID) &&
207 (end_loc_id != LLDB_INVALID_BREAK_ID)) ||
208 ((start_loc_id != LLDB_INVALID_BREAK_ID) &&
209 (end_loc_id == LLDB_INVALID_BREAK_ID))) {
210 new_args.Clear();
211 return llvm::createStringError(llvm::inconvertibleErrorCode(),
212 "Invalid breakpoint id range: Either "
213 "both ends of range must specify"
214 " a breakpoint location, or neither can "
215 "specify a breakpoint location.");
216 }
217
218 // We have valid range starting & ending breakpoint IDs. Go through all
219 // the breakpoints in the target and find all the breakpoints that fit into
220 // this range, and add them to new_args.
221
222 // Next check to see if we have location id's. If so, make sure the
223 // start_bp_id and end_bp_id are for the same breakpoint; otherwise we have
224 // an illegal range: breakpoint id ranges that specify bp locations are NOT
225 // allowed to cross major bp id numbers.
226
227 if ((start_loc_id != LLDB_INVALID_BREAK_ID) ||
228 (end_loc_id != LLDB_INVALID_BREAK_ID)) {
229 if (start_bp_id != end_bp_id) {
230 new_args.Clear();
231 return llvm::createStringError(
232 llvm::inconvertibleErrorCode(),
233 "Invalid range: Ranges that specify particular breakpoint "
234 "locations"
235 " must be within the same major breakpoint; you specified two"
236 " different major breakpoints, %d and %d.\n",
237 start_bp_id, end_bp_id);
238 }
239 }
240
241 const BreakpointList &breakpoints = target->GetBreakpointList();
242 const size_t num_breakpoints = breakpoints.GetSize();
243 for (size_t j = 0; j < num_breakpoints; ++j) {
244 Breakpoint *breakpoint = breakpoints.GetBreakpointAtIndex(j).get();
245 break_id_t cur_bp_id = breakpoint->GetID();
246
247 if ((cur_bp_id < start_bp_id) || (cur_bp_id > end_bp_id))
248 continue;
249
250 const size_t num_locations = breakpoint->GetNumLocations();
251
252 if ((cur_bp_id == start_bp_id) &&
253 (start_loc_id != LLDB_INVALID_BREAK_ID)) {
254 for (size_t k = 0; k < num_locations; ++k) {
255 BreakpointLocation *bp_loc = breakpoint->GetLocationAtIndex(k).get();
256 if ((bp_loc->GetID() >= start_loc_id) &&
257 (bp_loc->GetID() <= end_loc_id)) {
258 StreamString canonical_id_str;
259 BreakpointID::GetCanonicalReference(&canonical_id_str, cur_bp_id,
260 bp_loc->GetID());
261 new_args.AppendArgument(canonical_id_str.GetString());
262 }
263 }
264 } else if ((cur_bp_id == end_bp_id) &&
265 (end_loc_id != LLDB_INVALID_BREAK_ID)) {
266 for (size_t k = 0; k < num_locations; ++k) {
267 BreakpointLocation *bp_loc = breakpoint->GetLocationAtIndex(k).get();
268 if (bp_loc->GetID() <= end_loc_id) {
269 StreamString canonical_id_str;
270 BreakpointID::GetCanonicalReference(&canonical_id_str, cur_bp_id,
271 bp_loc->GetID());
272 new_args.AppendArgument(canonical_id_str.GetString());
273 }
274 }
275 } else {
276 StreamString canonical_id_str;
277 BreakpointID::GetCanonicalReference(&canonical_id_str, cur_bp_id,
279 new_args.AppendArgument(canonical_id_str.GetString());
280 }
281 }
282 }
283
284 // Okay, now see if we found any names, and if we did, add them:
285 if (target && !names_found.empty()) {
287 // Remove any names that aren't visible for this purpose:
288 auto iter = names_found.begin();
289 while (iter != names_found.end()) {
290 BreakpointName *bp_name = target->FindBreakpointName(ConstString(*iter),
291 true,
292 error);
293 if (bp_name && !bp_name->GetPermission(purpose))
294 iter = names_found.erase(iter);
295 else
296 iter++;
297 }
298
299 if (!names_found.empty()) {
300 for (BreakpointSP bkpt_sp : target->GetBreakpointList().Breakpoints()) {
301 for (const std::string &name : names_found) {
302 if (bkpt_sp->MatchesName(name.c_str())) {
303 StreamString canonical_id_str;
305 &canonical_id_str, bkpt_sp->GetID(), LLDB_INVALID_BREAK_ID);
306 new_args.AppendArgument(canonical_id_str.GetString());
307 }
308 }
309 }
310 }
311 }
312 return llvm::Error::success();
313}
314
315std::pair<llvm::StringRef, llvm::StringRef>
317 for (auto specifier_str : BreakpointID::GetRangeSpecifiers()) {
318 size_t idx = in_string.find(specifier_str);
319 if (idx == llvm::StringRef::npos)
320 continue;
321 llvm::StringRef right1 = in_string.drop_front(idx);
322
323 llvm::StringRef from = in_string.take_front(idx);
324 llvm::StringRef to = right1.drop_front(specifier_str.size());
325
328 return std::make_pair(from, to);
329 }
330 }
331
332 return std::pair<llvm::StringRef, llvm::StringRef>();
333}
static std::string LocationIDForStop(StopInfoSP stop_info_sp, uint32_t idx)
static llvm::raw_ostream & error(Stream &strm)
A command line argument class.
Definition Args.h:33
void AppendArgument(llvm::StringRef arg_str, char quote_char='\0')
Appends a new argument to the end of the list argument list.
Definition Args.cpp:332
size_t size() const
Definition Args.h:139
void Clear()
Clear the arguments.
Definition Args.cpp:388
bool Contains(BreakpointID bp_id) const
bool AddBreakpointID(BreakpointID bp_id)
bool RemoveBreakpointIDAtIndex(size_t index)
static std::pair< llvm::StringRef, llvm::StringRef > SplitIDRangeExpression(llvm::StringRef in_string)
static llvm::Error FindAndReplaceIDRanges(Args &old_args, ExecutionContext &exe_ctx, bool allow_locations, BreakpointName::Permissions ::PermissionKinds purpose, Args &new_args)
BreakpointID GetBreakpointIDAtIndex(size_t index) const
static std::optional< BreakpointID > ParseCanonicalReference(llvm::StringRef input)
Takes an input string containing the description of a breakpoint or breakpoint and location and retur...
static void GetCanonicalReference(Stream *s, lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
Takes a breakpoint ID and the breakpoint location id and returns a string containing the canonical de...
static bool IsValidIDExpression(llvm::StringRef str)
static bool IsRangeIdentifier(llvm::StringRef str)
static llvm::ArrayRef< llvm::StringRef > GetRangeSpecifiers()
static bool StringIsBreakpointName(llvm::StringRef str, Status &error)
Takes an input string and checks to see whether it is a breakpoint name.
General Outline: Allows adding and removing breakpoints and find by ID and index.
BreakpointIterable Breakpoints()
size_t GetSize() const
Returns the number of elements in this breakpoint list.
lldb::BreakpointSP GetBreakpointAtIndex(size_t i) const
Returns a shared pointer to the breakpoint with index i.
General Outline: A breakpoint location is defined by the breakpoint that produces it,...
lldb::break_id_t GetID() const
Returns the breakpoint location ID.
bool GetPermission(Permissions::PermissionKinds permission) const
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition Breakpoint.h:81
lldb::BreakpointLocationSP GetLocationAtIndex(size_t index, bool use_facade=true)
Get breakpoint locations by index.
size_t GetNumLocations(bool use_facade=true) const
Return the number of breakpoint locations.
A uniqued constant string class.
Definition ConstString.h:40
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
An error handling class.
Definition Status.h:118
lldb::break_id_t GetID() const
Definition Stoppoint.cpp:22
llvm::StringRef GetString() const
lldb::BreakpointSP GetBreakpointByID(lldb::break_id_t break_id)
Definition Target.cpp:436
BreakpointList & GetBreakpointList(bool internal=false)
Definition Target.cpp:422
BreakpointName * FindBreakpointName(ConstString name, bool can_create, Status &error)
Definition Target.cpp:884
#define LLDB_INVALID_BREAK_ID
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
int32_t break_id_t
Definition lldb-types.h:87
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
@ eStopReasonBreakpoint