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