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