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