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