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
9#include "lldb/lldb-enumerations.h"
10#include "lldb/Breakpoint/BreakpointIDList.h"
11
12#include "lldb/Breakpoint/Breakpoint.h"
13#include "lldb/Breakpoint/BreakpointLocation.h"
14#include "lldb/Target/Target.h"
15#include "lldb/Utility/Args.h"
16#include "lldb/Utility/StreamString.h"
17
18#include "llvm/ADT/STLExtras.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
23// class BreakpointIDList
24
25BreakpointIDList::BreakpointIDList() : m_breakpoint_ids() {}
26
27BreakpointIDList::~BreakpointIDList() = default;
28
29size_t BreakpointIDList::GetSize() const { return m_breakpoint_ids.size(); }
30
31BreakpointID BreakpointIDList::GetBreakpointIDAtIndex(size_t index) const {
32 return ((index < m_breakpoint_ids.size()) ? m_breakpoint_ids[index]
33 : BreakpointID());
34}
35
36bool BreakpointIDList::RemoveBreakpointIDAtIndex(size_t index) {
37 if (index >= m_breakpoint_ids.size())
38 return false;
39
40 m_breakpoint_ids.erase(position: m_breakpoint_ids.begin() + index);
41 return true;
42}
43
44void BreakpointIDList::Clear() { m_breakpoint_ids.clear(); }
45
46bool BreakpointIDList::AddBreakpointID(BreakpointID bp_id) {
47 m_breakpoint_ids.push_back(x: bp_id);
48
49 return true; // We don't do any verification in this function, so always
50 // return true.
51}
52
53bool BreakpointIDList::Contains(BreakpointID bp_id) const {
54 return llvm::is_contained(Range: m_breakpoint_ids, Element: bp_id);
55}
56
57// This function takes OLD_ARGS, which is usually the result of breaking the
58// command string arguments into
59// an array of space-separated strings, and searches through the arguments for
60// any breakpoint ID range specifiers.
61// Any string in the array that is not part of an ID range specifier is copied
62// directly into NEW_ARGS. If any
63// ID range specifiers are found, the range is interpreted and a list of
64// canonical breakpoint IDs corresponding to
65// all the current breakpoints and locations in the range are added to
66// NEW_ARGS. When this function is done,
67// NEW_ARGS should be a copy of OLD_ARGS, with and ID range specifiers replaced
68// by the members of the range.
69
70llvm::Error BreakpointIDList::FindAndReplaceIDRanges(
71 Args &old_args, Target *target, bool allow_locations,
72 BreakpointName::Permissions ::PermissionKinds purpose, Args &new_args) {
73 llvm::StringRef range_from;
74 llvm::StringRef range_to;
75 llvm::StringRef current_arg;
76 std::set<std::string> names_found;
77
78 for (size_t i = 0; i < old_args.size(); ++i) {
79 bool is_range = false;
80
81 current_arg = old_args[i].ref();
82 if (!allow_locations && current_arg.contains(C: '.')) {
83 new_args.Clear();
84 return llvm::createStringError(
85 EC: llvm::inconvertibleErrorCode(),
86 Fmt: "Breakpoint locations not allowed, saw location: %s.",
87 Vals: current_arg.str().c_str());
88 }
89
90 Status error;
91
92 std::tie(args&: range_from, args&: range_to) =
93 BreakpointIDList::SplitIDRangeExpression(in_string: current_arg);
94 if (!range_from.empty() && !range_to.empty()) {
95 is_range = true;
96 } else if (BreakpointID::StringIsBreakpointName(str: current_arg, error)) {
97 if (!error.Success()) {
98 new_args.Clear();
99 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
100 Msg: error.AsCString());
101 } else
102 names_found.insert(x: std::string(current_arg));
103 } else if ((i + 2 < old_args.size()) &&
104 BreakpointID::IsRangeIdentifier(str: old_args[i + 1].ref()) &&
105 BreakpointID::IsValidIDExpression(str: current_arg) &&
106 BreakpointID::IsValidIDExpression(str: old_args[i + 2].ref())) {
107 range_from = current_arg;
108 range_to = old_args[i + 2].ref();
109 is_range = true;
110 i = i + 2;
111 } else {
112 // See if user has specified id.*
113 llvm::StringRef tmp_str = old_args[i].ref();
114 size_t pos = tmp_str.find(C: '.');
115 if (pos != llvm::StringRef::npos) {
116 llvm::StringRef bp_id_str = tmp_str.substr(Start: 0, N: pos);
117 if (BreakpointID::IsValidIDExpression(str: bp_id_str) &&
118 tmp_str[pos + 1] == '*' && tmp_str.size() == (pos + 2)) {
119
120 BreakpointSP breakpoint_sp;
121 auto bp_id = BreakpointID::ParseCanonicalReference(input: bp_id_str);
122 if (bp_id)
123 breakpoint_sp = target->GetBreakpointByID(break_id: bp_id->GetBreakpointID());
124 if (!breakpoint_sp) {
125 new_args.Clear();
126 return llvm::createStringError(
127 EC: llvm::inconvertibleErrorCode(),
128 Fmt: "'%d' is not a valid breakpoint ID.\n",
129 Vals: bp_id->GetBreakpointID());
130 }
131 const size_t num_locations = breakpoint_sp->GetNumLocations();
132 for (size_t j = 0; j < num_locations; ++j) {
133 BreakpointLocation *bp_loc =
134 breakpoint_sp->GetLocationAtIndex(index: j).get();
135 StreamString canonical_id_str;
136 BreakpointID::GetCanonicalReference(
137 s: &canonical_id_str, break_id: bp_id->GetBreakpointID(), break_loc_id: bp_loc->GetID());
138 new_args.AppendArgument(arg_str: canonical_id_str.GetString());
139 }
140 }
141 }
142 }
143
144 if (!is_range) {
145 new_args.AppendArgument(arg_str: current_arg);
146 continue;
147 }
148
149 auto start_bp = BreakpointID::ParseCanonicalReference(input: range_from);
150 auto end_bp = BreakpointID::ParseCanonicalReference(input: range_to);
151
152 if (!start_bp ||
153 !target->GetBreakpointByID(break_id: start_bp->GetBreakpointID())) {
154 new_args.Clear();
155 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
156 Fmt: "'%s' is not a valid breakpoint ID.\n",
157 Vals: range_from.str().c_str());
158 }
159
160 if (!end_bp ||
161 !target->GetBreakpointByID(break_id: end_bp->GetBreakpointID())) {
162 new_args.Clear();
163 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
164 Fmt: "'%s' is not a valid breakpoint ID.\n",
165 Vals: range_to.str().c_str());
166 }
167 break_id_t start_bp_id = start_bp->GetBreakpointID();
168 break_id_t start_loc_id = start_bp->GetLocationID();
169 break_id_t end_bp_id = end_bp->GetBreakpointID();
170 break_id_t end_loc_id = end_bp->GetLocationID();
171 if (((start_loc_id == LLDB_INVALID_BREAK_ID) &&
172 (end_loc_id != LLDB_INVALID_BREAK_ID)) ||
173 ((start_loc_id != LLDB_INVALID_BREAK_ID) &&
174 (end_loc_id == LLDB_INVALID_BREAK_ID))) {
175 new_args.Clear();
176 return llvm::createStringError(EC: llvm::inconvertibleErrorCode(),
177 Msg: "Invalid breakpoint id range: Either "
178 "both ends of range must specify"
179 " a breakpoint location, or neither can "
180 "specify a breakpoint location.");
181 }
182
183 // We have valid range starting & ending breakpoint IDs. Go through all
184 // the breakpoints in the target and find all the breakpoints that fit into
185 // this range, and add them to new_args.
186
187 // Next check to see if we have location id's. If so, make sure the
188 // start_bp_id and end_bp_id are for the same breakpoint; otherwise we have
189 // an illegal range: breakpoint id ranges that specify bp locations are NOT
190 // allowed to cross major bp id numbers.
191
192 if ((start_loc_id != LLDB_INVALID_BREAK_ID) ||
193 (end_loc_id != LLDB_INVALID_BREAK_ID)) {
194 if (start_bp_id != end_bp_id) {
195 new_args.Clear();
196 return llvm::createStringError(
197 EC: llvm::inconvertibleErrorCode(),
198 Fmt: "Invalid range: Ranges that specify particular breakpoint "
199 "locations"
200 " must be within the same major breakpoint; you specified two"
201 " different major breakpoints, %d and %d.\n",
202 Vals: start_bp_id, Vals: end_bp_id);
203 }
204 }
205
206 const BreakpointList &breakpoints = target->GetBreakpointList();
207 const size_t num_breakpoints = breakpoints.GetSize();
208 for (size_t j = 0; j < num_breakpoints; ++j) {
209 Breakpoint *breakpoint = breakpoints.GetBreakpointAtIndex(i: j).get();
210 break_id_t cur_bp_id = breakpoint->GetID();
211
212 if ((cur_bp_id < start_bp_id) || (cur_bp_id > end_bp_id))
213 continue;
214
215 const size_t num_locations = breakpoint->GetNumLocations();
216
217 if ((cur_bp_id == start_bp_id) &&
218 (start_loc_id != LLDB_INVALID_BREAK_ID)) {
219 for (size_t k = 0; k < num_locations; ++k) {
220 BreakpointLocation *bp_loc = breakpoint->GetLocationAtIndex(index: k).get();
221 if ((bp_loc->GetID() >= start_loc_id) &&
222 (bp_loc->GetID() <= end_loc_id)) {
223 StreamString canonical_id_str;
224 BreakpointID::GetCanonicalReference(s: &canonical_id_str, break_id: cur_bp_id,
225 break_loc_id: bp_loc->GetID());
226 new_args.AppendArgument(arg_str: canonical_id_str.GetString());
227 }
228 }
229 } else if ((cur_bp_id == end_bp_id) &&
230 (end_loc_id != LLDB_INVALID_BREAK_ID)) {
231 for (size_t k = 0; k < num_locations; ++k) {
232 BreakpointLocation *bp_loc = breakpoint->GetLocationAtIndex(index: k).get();
233 if (bp_loc->GetID() <= end_loc_id) {
234 StreamString canonical_id_str;
235 BreakpointID::GetCanonicalReference(s: &canonical_id_str, break_id: cur_bp_id,
236 break_loc_id: bp_loc->GetID());
237 new_args.AppendArgument(arg_str: canonical_id_str.GetString());
238 }
239 }
240 } else {
241 StreamString canonical_id_str;
242 BreakpointID::GetCanonicalReference(s: &canonical_id_str, break_id: cur_bp_id,
243 LLDB_INVALID_BREAK_ID);
244 new_args.AppendArgument(arg_str: canonical_id_str.GetString());
245 }
246 }
247 }
248
249 // Okay, now see if we found any names, and if we did, add them:
250 if (target && !names_found.empty()) {
251 Status error;
252 // Remove any names that aren't visible for this purpose:
253 auto iter = names_found.begin();
254 while (iter != names_found.end()) {
255 BreakpointName *bp_name = target->FindBreakpointName(name: ConstString(*iter),
256 can_create: true,
257 error);
258 if (bp_name && !bp_name->GetPermission(permission: purpose))
259 iter = names_found.erase(position: iter);
260 else
261 iter++;
262 }
263
264 if (!names_found.empty()) {
265 for (BreakpointSP bkpt_sp : target->GetBreakpointList().Breakpoints()) {
266 for (std::string name : names_found) {
267 if (bkpt_sp->MatchesName(name: name.c_str())) {
268 StreamString canonical_id_str;
269 BreakpointID::GetCanonicalReference(
270 s: &canonical_id_str, break_id: bkpt_sp->GetID(), LLDB_INVALID_BREAK_ID);
271 new_args.AppendArgument(arg_str: canonical_id_str.GetString());
272 }
273 }
274 }
275 }
276 }
277 return llvm::Error::success();
278}
279
280std::pair<llvm::StringRef, llvm::StringRef>
281BreakpointIDList::SplitIDRangeExpression(llvm::StringRef in_string) {
282 for (auto specifier_str : BreakpointID::GetRangeSpecifiers()) {
283 size_t idx = in_string.find(Str: specifier_str);
284 if (idx == llvm::StringRef::npos)
285 continue;
286 llvm::StringRef right1 = in_string.drop_front(N: idx);
287
288 llvm::StringRef from = in_string.take_front(N: idx);
289 llvm::StringRef to = right1.drop_front(N: specifier_str.size());
290
291 if (BreakpointID::IsValidIDExpression(str: from) &&
292 BreakpointID::IsValidIDExpression(str: to)) {
293 return std::make_pair(x&: from, y&: to);
294 }
295 }
296
297 return std::pair<llvm::StringRef, llvm::StringRef>();
298}
299

source code of lldb/source/Breakpoint/BreakpointIDList.cpp