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