1 //===-- Breakpoint.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 #include "llvm/Support/Casting.h" 14 15 // Project includes 16 #include "lldb/Breakpoint/Breakpoint.h" 17 #include "lldb/Breakpoint/BreakpointLocation.h" 18 #include "lldb/Breakpoint/BreakpointLocationCollection.h" 19 #include "lldb/Breakpoint/BreakpointResolver.h" 20 #include "lldb/Breakpoint/BreakpointResolverFileLine.h" 21 #include "lldb/Core/Address.h" 22 #include "lldb/Core/Module.h" 23 #include "lldb/Core/ModuleList.h" 24 #include "lldb/Core/SearchFilter.h" 25 #include "lldb/Core/Section.h" 26 #include "lldb/Symbol/CompileUnit.h" 27 #include "lldb/Symbol/Function.h" 28 #include "lldb/Symbol/Symbol.h" 29 #include "lldb/Symbol/SymbolContext.h" 30 #include "lldb/Target/Target.h" 31 #include "lldb/Target/ThreadSpec.h" 32 #include "lldb/Utility/Log.h" 33 #include "lldb/Utility/Stream.h" 34 #include "lldb/Utility/StreamString.h" 35 36 using namespace lldb; 37 using namespace lldb_private; 38 using namespace llvm; 39 40 const ConstString &Breakpoint::GetEventIdentifier() { 41 static ConstString g_identifier("event-identifier.breakpoint.changed"); 42 return g_identifier; 43 } 44 45 const char *Breakpoint::g_option_names[static_cast<uint32_t>( 46 Breakpoint::OptionNames::LastOptionName)]{"Names", "Hardware"}; 47 48 //---------------------------------------------------------------------- 49 // Breakpoint constructor 50 //---------------------------------------------------------------------- 51 Breakpoint::Breakpoint(Target &target, SearchFilterSP &filter_sp, 52 BreakpointResolverSP &resolver_sp, bool hardware, 53 bool resolve_indirect_symbols) 54 : m_being_created(true), m_hardware(hardware), m_target(target), 55 m_filter_sp(filter_sp), m_resolver_sp(resolver_sp), 56 m_options_up(new BreakpointOptions(true)), m_locations(*this), 57 m_resolve_indirect_symbols(resolve_indirect_symbols), m_hit_count(0) { 58 m_being_created = false; 59 } 60 61 Breakpoint::Breakpoint(Target &new_target, Breakpoint &source_bp) 62 : m_being_created(true), m_hardware(source_bp.m_hardware), 63 m_target(new_target), m_name_list(source_bp.m_name_list), 64 m_options_up(new BreakpointOptions(*source_bp.m_options_up.get())), 65 m_locations(*this), 66 m_resolve_indirect_symbols(source_bp.m_resolve_indirect_symbols), 67 m_hit_count(0) { 68 // Now go through and copy the filter & resolver: 69 m_resolver_sp = source_bp.m_resolver_sp->CopyForBreakpoint(*this); 70 m_filter_sp = source_bp.m_filter_sp->CopyForBreakpoint(*this); 71 } 72 73 //---------------------------------------------------------------------- 74 // Destructor 75 //---------------------------------------------------------------------- 76 Breakpoint::~Breakpoint() = default; 77 78 //---------------------------------------------------------------------- 79 // Serialization 80 //---------------------------------------------------------------------- 81 StructuredData::ObjectSP Breakpoint::SerializeToStructuredData() { 82 // Serialize the resolver: 83 StructuredData::DictionarySP breakpoint_dict_sp( 84 new StructuredData::Dictionary()); 85 StructuredData::DictionarySP breakpoint_contents_sp( 86 new StructuredData::Dictionary()); 87 88 if (!m_name_list.empty()) { 89 StructuredData::ArraySP names_array_sp(new StructuredData::Array()); 90 for (auto name : m_name_list) { 91 names_array_sp->AddItem( 92 StructuredData::StringSP(new StructuredData::String(name))); 93 } 94 breakpoint_contents_sp->AddItem(Breakpoint::GetKey(OptionNames::Names), 95 names_array_sp); 96 } 97 98 breakpoint_contents_sp->AddBooleanItem( 99 Breakpoint::GetKey(OptionNames::Hardware), m_hardware); 100 101 StructuredData::ObjectSP resolver_dict_sp( 102 m_resolver_sp->SerializeToStructuredData()); 103 if (!resolver_dict_sp) 104 return StructuredData::ObjectSP(); 105 106 breakpoint_contents_sp->AddItem(BreakpointResolver::GetSerializationKey(), 107 resolver_dict_sp); 108 109 StructuredData::ObjectSP filter_dict_sp( 110 m_filter_sp->SerializeToStructuredData()); 111 if (!filter_dict_sp) 112 return StructuredData::ObjectSP(); 113 114 breakpoint_contents_sp->AddItem(SearchFilter::GetSerializationKey(), 115 filter_dict_sp); 116 117 StructuredData::ObjectSP options_dict_sp( 118 m_options_up->SerializeToStructuredData()); 119 if (!options_dict_sp) 120 return StructuredData::ObjectSP(); 121 122 breakpoint_contents_sp->AddItem(BreakpointOptions::GetSerializationKey(), 123 options_dict_sp); 124 125 breakpoint_dict_sp->AddItem(GetSerializationKey(), breakpoint_contents_sp); 126 return breakpoint_dict_sp; 127 } 128 129 lldb::BreakpointSP Breakpoint::CreateFromStructuredData( 130 Target &target, StructuredData::ObjectSP &object_data, Status &error) { 131 BreakpointSP result_sp; 132 133 StructuredData::Dictionary *breakpoint_dict = object_data->GetAsDictionary(); 134 135 if (!breakpoint_dict || !breakpoint_dict->IsValid()) { 136 error.SetErrorString("Can't deserialize from an invalid data object."); 137 return result_sp; 138 } 139 140 StructuredData::Dictionary *resolver_dict; 141 bool success = breakpoint_dict->GetValueForKeyAsDictionary( 142 BreakpointResolver::GetSerializationKey(), resolver_dict); 143 if (!success) { 144 error.SetErrorStringWithFormat( 145 "Breakpoint data missing toplevel resolver key"); 146 return result_sp; 147 } 148 149 Status create_error; 150 BreakpointResolverSP resolver_sp = 151 BreakpointResolver::CreateFromStructuredData(*resolver_dict, 152 create_error); 153 if (create_error.Fail()) { 154 error.SetErrorStringWithFormat( 155 "Error creating breakpoint resolver from data: %s.", 156 create_error.AsCString()); 157 return result_sp; 158 } 159 160 StructuredData::Dictionary *filter_dict; 161 success = breakpoint_dict->GetValueForKeyAsDictionary( 162 SearchFilter::GetSerializationKey(), filter_dict); 163 SearchFilterSP filter_sp; 164 if (!success) 165 filter_sp.reset( 166 new SearchFilterForUnconstrainedSearches(target.shared_from_this())); 167 else { 168 filter_sp = SearchFilter::CreateFromStructuredData(target, *filter_dict, 169 create_error); 170 if (create_error.Fail()) { 171 error.SetErrorStringWithFormat( 172 "Error creating breakpoint filter from data: %s.", 173 create_error.AsCString()); 174 return result_sp; 175 } 176 } 177 178 std::unique_ptr<BreakpointOptions> options_up; 179 StructuredData::Dictionary *options_dict; 180 success = breakpoint_dict->GetValueForKeyAsDictionary( 181 BreakpointOptions::GetSerializationKey(), options_dict); 182 if (success) { 183 options_up = BreakpointOptions::CreateFromStructuredData( 184 target, *options_dict, create_error); 185 if (create_error.Fail()) { 186 error.SetErrorStringWithFormat( 187 "Error creating breakpoint options from data: %s.", 188 create_error.AsCString()); 189 return result_sp; 190 } 191 } 192 193 bool hardware = false; 194 success = breakpoint_dict->GetValueForKeyAsBoolean( 195 Breakpoint::GetKey(OptionNames::Hardware), hardware); 196 197 result_sp = 198 target.CreateBreakpoint(filter_sp, resolver_sp, false, hardware, true); 199 200 if (result_sp && options_up) { 201 result_sp->m_options_up = std::move(options_up); 202 } 203 204 StructuredData::Array *names_array; 205 success = breakpoint_dict->GetValueForKeyAsArray( 206 Breakpoint::GetKey(OptionNames::Names), names_array); 207 if (success && names_array) { 208 size_t num_names = names_array->GetSize(); 209 for (size_t i = 0; i < num_names; i++) { 210 llvm::StringRef name; 211 Status error; 212 success = names_array->GetItemAtIndexAsString(i, name); 213 result_sp->AddName(name, error); 214 } 215 } 216 217 return result_sp; 218 } 219 220 bool Breakpoint::SerializedBreakpointMatchesNames( 221 StructuredData::ObjectSP &bkpt_object_sp, std::vector<std::string> &names) { 222 if (!bkpt_object_sp) 223 return false; 224 225 StructuredData::Dictionary *bkpt_dict = bkpt_object_sp->GetAsDictionary(); 226 if (!bkpt_dict) 227 return false; 228 229 if (names.empty()) 230 return true; 231 232 StructuredData::Array *names_array; 233 234 bool success = 235 bkpt_dict->GetValueForKeyAsArray(GetKey(OptionNames::Names), names_array); 236 // If there are no names, it can't match these names; 237 if (!success) 238 return false; 239 240 size_t num_names = names_array->GetSize(); 241 std::vector<std::string>::iterator begin = names.begin(); 242 std::vector<std::string>::iterator end = names.end(); 243 244 for (size_t i = 0; i < num_names; i++) { 245 llvm::StringRef name; 246 if (names_array->GetItemAtIndexAsString(i, name)) { 247 if (std::find(begin, end, name) != end) { 248 return true; 249 } 250 } 251 } 252 return false; 253 } 254 255 const lldb::TargetSP Breakpoint::GetTargetSP() { 256 return m_target.shared_from_this(); 257 } 258 259 bool Breakpoint::IsInternal() const { return LLDB_BREAK_ID_IS_INTERNAL(m_bid); } 260 261 BreakpointLocationSP Breakpoint::AddLocation(const Address &addr, 262 bool *new_location) { 263 return m_locations.AddLocation(addr, m_resolve_indirect_symbols, 264 new_location); 265 } 266 267 BreakpointLocationSP Breakpoint::FindLocationByAddress(const Address &addr) { 268 return m_locations.FindByAddress(addr); 269 } 270 271 break_id_t Breakpoint::FindLocationIDByAddress(const Address &addr) { 272 return m_locations.FindIDByAddress(addr); 273 } 274 275 BreakpointLocationSP Breakpoint::FindLocationByID(break_id_t bp_loc_id) { 276 return m_locations.FindByID(bp_loc_id); 277 } 278 279 BreakpointLocationSP Breakpoint::GetLocationAtIndex(size_t index) { 280 return m_locations.GetByIndex(index); 281 } 282 283 void Breakpoint::RemoveInvalidLocations(const ArchSpec &arch) { 284 m_locations.RemoveInvalidLocations(arch); 285 } 286 287 // For each of the overall options we need to decide how they propagate to 288 // the location options. This will determine the precedence of options on 289 // the breakpoint vs. its locations. 290 291 // Disable at the breakpoint level should override the location settings. 292 // That way you can conveniently turn off a whole breakpoint without messing 293 // up the individual settings. 294 295 void Breakpoint::SetEnabled(bool enable) { 296 if (enable == m_options_up->IsEnabled()) 297 return; 298 299 m_options_up->SetEnabled(enable); 300 if (enable) 301 m_locations.ResolveAllBreakpointSites(); 302 else 303 m_locations.ClearAllBreakpointSites(); 304 305 SendBreakpointChangedEvent(enable ? eBreakpointEventTypeEnabled 306 : eBreakpointEventTypeDisabled); 307 } 308 309 bool Breakpoint::IsEnabled() { return m_options_up->IsEnabled(); } 310 311 void Breakpoint::SetIgnoreCount(uint32_t n) { 312 if (m_options_up->GetIgnoreCount() == n) 313 return; 314 315 m_options_up->SetIgnoreCount(n); 316 SendBreakpointChangedEvent(eBreakpointEventTypeIgnoreChanged); 317 } 318 319 void Breakpoint::DecrementIgnoreCount() { 320 uint32_t ignore = m_options_up->GetIgnoreCount(); 321 if (ignore != 0) 322 m_options_up->SetIgnoreCount(ignore - 1); 323 } 324 325 uint32_t Breakpoint::GetIgnoreCount() const { 326 return m_options_up->GetIgnoreCount(); 327 } 328 329 bool Breakpoint::IgnoreCountShouldStop() { 330 uint32_t ignore = GetIgnoreCount(); 331 if (ignore != 0) { 332 // When we get here we know the location that caused the stop doesn't have 333 // an ignore count, 334 // since by contract we call it first... So we don't have to find & 335 // decrement it, we only have 336 // to decrement our own ignore count. 337 DecrementIgnoreCount(); 338 return false; 339 } else 340 return true; 341 } 342 343 uint32_t Breakpoint::GetHitCount() const { return m_hit_count; } 344 345 bool Breakpoint::IsOneShot() const { return m_options_up->IsOneShot(); } 346 347 void Breakpoint::SetOneShot(bool one_shot) { 348 m_options_up->SetOneShot(one_shot); 349 } 350 351 bool Breakpoint::IsAutoContinue() const { 352 return m_options_up->IsAutoContinue(); 353 } 354 355 void Breakpoint::SetAutoContinue(bool auto_continue) { 356 m_options_up->SetAutoContinue(auto_continue); 357 } 358 359 void Breakpoint::SetThreadID(lldb::tid_t thread_id) { 360 if (m_options_up->GetThreadSpec()->GetTID() == thread_id) 361 return; 362 363 m_options_up->GetThreadSpec()->SetTID(thread_id); 364 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged); 365 } 366 367 lldb::tid_t Breakpoint::GetThreadID() const { 368 if (m_options_up->GetThreadSpecNoCreate() == nullptr) 369 return LLDB_INVALID_THREAD_ID; 370 else 371 return m_options_up->GetThreadSpecNoCreate()->GetTID(); 372 } 373 374 void Breakpoint::SetThreadIndex(uint32_t index) { 375 if (m_options_up->GetThreadSpec()->GetIndex() == index) 376 return; 377 378 m_options_up->GetThreadSpec()->SetIndex(index); 379 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged); 380 } 381 382 uint32_t Breakpoint::GetThreadIndex() const { 383 if (m_options_up->GetThreadSpecNoCreate() == nullptr) 384 return 0; 385 else 386 return m_options_up->GetThreadSpecNoCreate()->GetIndex(); 387 } 388 389 void Breakpoint::SetThreadName(const char *thread_name) { 390 if (m_options_up->GetThreadSpec()->GetName() != nullptr && 391 ::strcmp(m_options_up->GetThreadSpec()->GetName(), thread_name) == 0) 392 return; 393 394 m_options_up->GetThreadSpec()->SetName(thread_name); 395 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged); 396 } 397 398 const char *Breakpoint::GetThreadName() const { 399 if (m_options_up->GetThreadSpecNoCreate() == nullptr) 400 return nullptr; 401 else 402 return m_options_up->GetThreadSpecNoCreate()->GetName(); 403 } 404 405 void Breakpoint::SetQueueName(const char *queue_name) { 406 if (m_options_up->GetThreadSpec()->GetQueueName() != nullptr && 407 ::strcmp(m_options_up->GetThreadSpec()->GetQueueName(), queue_name) == 0) 408 return; 409 410 m_options_up->GetThreadSpec()->SetQueueName(queue_name); 411 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged); 412 } 413 414 const char *Breakpoint::GetQueueName() const { 415 if (m_options_up->GetThreadSpecNoCreate() == nullptr) 416 return nullptr; 417 else 418 return m_options_up->GetThreadSpecNoCreate()->GetQueueName(); 419 } 420 421 void Breakpoint::SetCondition(const char *condition) { 422 m_options_up->SetCondition(condition); 423 SendBreakpointChangedEvent(eBreakpointEventTypeConditionChanged); 424 } 425 426 const char *Breakpoint::GetConditionText() const { 427 return m_options_up->GetConditionText(); 428 } 429 430 // This function is used when "baton" doesn't need to be freed 431 void Breakpoint::SetCallback(BreakpointHitCallback callback, void *baton, 432 bool is_synchronous) { 433 // The default "Baton" class will keep a copy of "baton" and won't free 434 // or delete it when it goes goes out of scope. 435 m_options_up->SetCallback(callback, std::make_shared<UntypedBaton>(baton), 436 is_synchronous); 437 438 SendBreakpointChangedEvent(eBreakpointEventTypeCommandChanged); 439 } 440 441 // This function is used when a baton needs to be freed and therefore is 442 // contained in a "Baton" subclass. 443 void Breakpoint::SetCallback(BreakpointHitCallback callback, 444 const BatonSP &callback_baton_sp, 445 bool is_synchronous) { 446 m_options_up->SetCallback(callback, callback_baton_sp, is_synchronous); 447 } 448 449 void Breakpoint::ClearCallback() { m_options_up->ClearCallback(); } 450 451 bool Breakpoint::InvokeCallback(StoppointCallbackContext *context, 452 break_id_t bp_loc_id) { 453 return m_options_up->InvokeCallback(context, GetID(), bp_loc_id); 454 } 455 456 BreakpointOptions *Breakpoint::GetOptions() { return m_options_up.get(); } 457 458 void Breakpoint::ResolveBreakpoint() { 459 if (m_resolver_sp) 460 m_resolver_sp->ResolveBreakpoint(*m_filter_sp); 461 } 462 463 void Breakpoint::ResolveBreakpointInModules( 464 ModuleList &module_list, BreakpointLocationCollection &new_locations) { 465 m_locations.StartRecordingNewLocations(new_locations); 466 467 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list); 468 469 m_locations.StopRecordingNewLocations(); 470 } 471 472 void Breakpoint::ResolveBreakpointInModules(ModuleList &module_list, 473 bool send_event) { 474 if (m_resolver_sp) { 475 // If this is not an internal breakpoint, set up to record the new 476 // locations, then dispatch 477 // an event with the new locations. 478 if (!IsInternal() && send_event) { 479 BreakpointEventData *new_locations_event = new BreakpointEventData( 480 eBreakpointEventTypeLocationsAdded, shared_from_this()); 481 482 ResolveBreakpointInModules( 483 module_list, new_locations_event->GetBreakpointLocationCollection()); 484 485 if (new_locations_event->GetBreakpointLocationCollection().GetSize() != 486 0) { 487 SendBreakpointChangedEvent(new_locations_event); 488 } else 489 delete new_locations_event; 490 } else { 491 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list); 492 } 493 } 494 } 495 496 void Breakpoint::ClearAllBreakpointSites() { 497 m_locations.ClearAllBreakpointSites(); 498 } 499 500 //---------------------------------------------------------------------- 501 // ModulesChanged: Pass in a list of new modules, and 502 //---------------------------------------------------------------------- 503 504 void Breakpoint::ModulesChanged(ModuleList &module_list, bool load, 505 bool delete_locations) { 506 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 507 if (log) 508 log->Printf("Breakpoint::ModulesChanged: num_modules: %zu load: %i " 509 "delete_locations: %i\n", 510 module_list.GetSize(), load, delete_locations); 511 512 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex()); 513 if (load) { 514 // The logic for handling new modules is: 515 // 1) If the filter rejects this module, then skip it. 516 // 2) Run through the current location list and if there are any locations 517 // for that module, we mark the module as "seen" and we don't try to 518 // re-resolve 519 // breakpoint locations for that module. 520 // However, we do add breakpoint sites to these locations if needed. 521 // 3) If we don't see this module in our breakpoint location list, call 522 // ResolveInModules. 523 524 ModuleList new_modules; // We'll stuff the "unseen" modules in this list, 525 // and then resolve 526 // them after the locations pass. Have to do it this way because 527 // resolving breakpoints will add new locations potentially. 528 529 for (ModuleSP module_sp : module_list.ModulesNoLocking()) { 530 bool seen = false; 531 if (!m_filter_sp->ModulePasses(module_sp)) 532 continue; 533 534 for (BreakpointLocationSP break_loc_sp : 535 m_locations.BreakpointLocations()) { 536 if (!break_loc_sp->IsEnabled()) 537 continue; 538 SectionSP section_sp(break_loc_sp->GetAddress().GetSection()); 539 if (!section_sp || section_sp->GetModule() == module_sp) { 540 if (!seen) 541 seen = true; 542 543 if (!break_loc_sp->ResolveBreakpointSite()) { 544 if (log) 545 log->Printf("Warning: could not set breakpoint site for " 546 "breakpoint location %d of breakpoint %d.\n", 547 break_loc_sp->GetID(), GetID()); 548 } 549 } 550 } 551 552 if (!seen) 553 new_modules.AppendIfNeeded(module_sp); 554 } 555 556 if (new_modules.GetSize() > 0) { 557 ResolveBreakpointInModules(new_modules); 558 } 559 } else { 560 // Go through the currently set locations and if any have breakpoints in 561 // the module list, then remove their breakpoint sites, and their locations 562 // if asked to. 563 564 BreakpointEventData *removed_locations_event; 565 if (!IsInternal()) 566 removed_locations_event = new BreakpointEventData( 567 eBreakpointEventTypeLocationsRemoved, shared_from_this()); 568 else 569 removed_locations_event = nullptr; 570 571 size_t num_modules = module_list.GetSize(); 572 for (size_t i = 0; i < num_modules; i++) { 573 ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(i)); 574 if (m_filter_sp->ModulePasses(module_sp)) { 575 size_t loc_idx = 0; 576 size_t num_locations = m_locations.GetSize(); 577 BreakpointLocationCollection locations_to_remove; 578 for (loc_idx = 0; loc_idx < num_locations; loc_idx++) { 579 BreakpointLocationSP break_loc_sp(m_locations.GetByIndex(loc_idx)); 580 SectionSP section_sp(break_loc_sp->GetAddress().GetSection()); 581 if (section_sp && section_sp->GetModule() == module_sp) { 582 // Remove this breakpoint since the shared library is 583 // unloaded, but keep the breakpoint location around 584 // so we always get complete hit count and breakpoint 585 // lifetime info 586 break_loc_sp->ClearBreakpointSite(); 587 if (removed_locations_event) { 588 removed_locations_event->GetBreakpointLocationCollection().Add( 589 break_loc_sp); 590 } 591 if (delete_locations) 592 locations_to_remove.Add(break_loc_sp); 593 } 594 } 595 596 if (delete_locations) { 597 size_t num_locations_to_remove = locations_to_remove.GetSize(); 598 for (loc_idx = 0; loc_idx < num_locations_to_remove; loc_idx++) 599 m_locations.RemoveLocation(locations_to_remove.GetByIndex(loc_idx)); 600 } 601 } 602 } 603 SendBreakpointChangedEvent(removed_locations_event); 604 } 605 } 606 607 namespace { 608 static bool SymbolContextsMightBeEquivalent(SymbolContext &old_sc, 609 SymbolContext &new_sc) { 610 bool equivalent_scs = false; 611 612 if (old_sc.module_sp.get() == new_sc.module_sp.get()) { 613 // If these come from the same module, we can directly compare the pointers: 614 if (old_sc.comp_unit && new_sc.comp_unit && 615 (old_sc.comp_unit == new_sc.comp_unit)) { 616 if (old_sc.function && new_sc.function && 617 (old_sc.function == new_sc.function)) { 618 equivalent_scs = true; 619 } 620 } else if (old_sc.symbol && new_sc.symbol && 621 (old_sc.symbol == new_sc.symbol)) { 622 equivalent_scs = true; 623 } 624 } else { 625 // Otherwise we will compare by name... 626 if (old_sc.comp_unit && new_sc.comp_unit) { 627 if (FileSpec::Equal(*old_sc.comp_unit, *new_sc.comp_unit, true)) { 628 // Now check the functions: 629 if (old_sc.function && new_sc.function && 630 (old_sc.function->GetName() == new_sc.function->GetName())) { 631 equivalent_scs = true; 632 } 633 } 634 } else if (old_sc.symbol && new_sc.symbol) { 635 if (Mangled::Compare(old_sc.symbol->GetMangled(), 636 new_sc.symbol->GetMangled()) == 0) { 637 equivalent_scs = true; 638 } 639 } 640 } 641 return equivalent_scs; 642 } 643 } // anonymous namespace 644 645 void Breakpoint::ModuleReplaced(ModuleSP old_module_sp, 646 ModuleSP new_module_sp) { 647 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 648 if (log) 649 log->Printf("Breakpoint::ModulesReplaced for %s\n", 650 old_module_sp->GetSpecificationDescription().c_str()); 651 // First find all the locations that are in the old module 652 653 BreakpointLocationCollection old_break_locs; 654 for (BreakpointLocationSP break_loc_sp : m_locations.BreakpointLocations()) { 655 SectionSP section_sp = break_loc_sp->GetAddress().GetSection(); 656 if (section_sp && section_sp->GetModule() == old_module_sp) { 657 old_break_locs.Add(break_loc_sp); 658 } 659 } 660 661 size_t num_old_locations = old_break_locs.GetSize(); 662 663 if (num_old_locations == 0) { 664 // There were no locations in the old module, so we just need to check if 665 // there were any in the new module. 666 ModuleList temp_list; 667 temp_list.Append(new_module_sp); 668 ResolveBreakpointInModules(temp_list); 669 } else { 670 // First search the new module for locations. 671 // Then compare this with the old list, copy over locations that "look the 672 // same" 673 // Then delete the old locations. 674 // Finally remember to post the creation event. 675 // 676 // Two locations are the same if they have the same comp unit & function (by 677 // name) and there are the same number 678 // of locations in the old function as in the new one. 679 680 ModuleList temp_list; 681 temp_list.Append(new_module_sp); 682 BreakpointLocationCollection new_break_locs; 683 ResolveBreakpointInModules(temp_list, new_break_locs); 684 BreakpointLocationCollection locations_to_remove; 685 BreakpointLocationCollection locations_to_announce; 686 687 size_t num_new_locations = new_break_locs.GetSize(); 688 689 if (num_new_locations > 0) { 690 // Break out the case of one location -> one location since that's the 691 // most common one, and there's no need 692 // to build up the structures needed for the merge in that case. 693 if (num_new_locations == 1 && num_old_locations == 1) { 694 bool equivalent_locations = false; 695 SymbolContext old_sc, new_sc; 696 // The only way the old and new location can be equivalent is if they 697 // have the same amount of information: 698 BreakpointLocationSP old_loc_sp = old_break_locs.GetByIndex(0); 699 BreakpointLocationSP new_loc_sp = new_break_locs.GetByIndex(0); 700 701 if (old_loc_sp->GetAddress().CalculateSymbolContext(&old_sc) == 702 new_loc_sp->GetAddress().CalculateSymbolContext(&new_sc)) { 703 equivalent_locations = 704 SymbolContextsMightBeEquivalent(old_sc, new_sc); 705 } 706 707 if (equivalent_locations) { 708 m_locations.SwapLocation(old_loc_sp, new_loc_sp); 709 } else { 710 locations_to_remove.Add(old_loc_sp); 711 locations_to_announce.Add(new_loc_sp); 712 } 713 } else { 714 // We don't want to have to keep computing the SymbolContexts for these 715 // addresses over and over, 716 // so lets get them up front: 717 718 typedef std::map<lldb::break_id_t, SymbolContext> IDToSCMap; 719 IDToSCMap old_sc_map; 720 for (size_t idx = 0; idx < num_old_locations; idx++) { 721 SymbolContext sc; 722 BreakpointLocationSP bp_loc_sp = old_break_locs.GetByIndex(idx); 723 lldb::break_id_t loc_id = bp_loc_sp->GetID(); 724 bp_loc_sp->GetAddress().CalculateSymbolContext(&old_sc_map[loc_id]); 725 } 726 727 std::map<lldb::break_id_t, SymbolContext> new_sc_map; 728 for (size_t idx = 0; idx < num_new_locations; idx++) { 729 SymbolContext sc; 730 BreakpointLocationSP bp_loc_sp = new_break_locs.GetByIndex(idx); 731 lldb::break_id_t loc_id = bp_loc_sp->GetID(); 732 bp_loc_sp->GetAddress().CalculateSymbolContext(&new_sc_map[loc_id]); 733 } 734 // Take an element from the old Symbol Contexts 735 while (old_sc_map.size() > 0) { 736 lldb::break_id_t old_id = old_sc_map.begin()->first; 737 SymbolContext &old_sc = old_sc_map.begin()->second; 738 739 // Count the number of entries equivalent to this SC for the old list: 740 std::vector<lldb::break_id_t> old_id_vec; 741 old_id_vec.push_back(old_id); 742 743 IDToSCMap::iterator tmp_iter; 744 for (tmp_iter = ++old_sc_map.begin(); tmp_iter != old_sc_map.end(); 745 tmp_iter++) { 746 if (SymbolContextsMightBeEquivalent(old_sc, tmp_iter->second)) 747 old_id_vec.push_back(tmp_iter->first); 748 } 749 750 // Now find all the equivalent locations in the new list. 751 std::vector<lldb::break_id_t> new_id_vec; 752 for (tmp_iter = new_sc_map.begin(); tmp_iter != new_sc_map.end(); 753 tmp_iter++) { 754 if (SymbolContextsMightBeEquivalent(old_sc, tmp_iter->second)) 755 new_id_vec.push_back(tmp_iter->first); 756 } 757 758 // Alright, if we have the same number of potentially equivalent 759 // locations in the old 760 // and new modules, we'll just map them one to one in ascending ID 761 // order (assuming the 762 // resolver's order would match the equivalent ones. 763 // Otherwise, we'll dump all the old ones, and just take the new ones, 764 // erasing the elements 765 // from both maps as we go. 766 767 if (old_id_vec.size() == new_id_vec.size()) { 768 sort(old_id_vec.begin(), old_id_vec.end()); 769 sort(new_id_vec.begin(), new_id_vec.end()); 770 size_t num_elements = old_id_vec.size(); 771 for (size_t idx = 0; idx < num_elements; idx++) { 772 BreakpointLocationSP old_loc_sp = 773 old_break_locs.FindByIDPair(GetID(), old_id_vec[idx]); 774 BreakpointLocationSP new_loc_sp = 775 new_break_locs.FindByIDPair(GetID(), new_id_vec[idx]); 776 m_locations.SwapLocation(old_loc_sp, new_loc_sp); 777 old_sc_map.erase(old_id_vec[idx]); 778 new_sc_map.erase(new_id_vec[idx]); 779 } 780 } else { 781 for (lldb::break_id_t old_id : old_id_vec) { 782 locations_to_remove.Add( 783 old_break_locs.FindByIDPair(GetID(), old_id)); 784 old_sc_map.erase(old_id); 785 } 786 for (lldb::break_id_t new_id : new_id_vec) { 787 locations_to_announce.Add( 788 new_break_locs.FindByIDPair(GetID(), new_id)); 789 new_sc_map.erase(new_id); 790 } 791 } 792 } 793 } 794 } 795 796 // Now remove the remaining old locations, and cons up a removed locations 797 // event. 798 // Note, we don't put the new locations that were swapped with an old 799 // location on the locations_to_remove 800 // list, so we don't need to worry about telling the world about removing a 801 // location we didn't tell them 802 // about adding. 803 804 BreakpointEventData *locations_event; 805 if (!IsInternal()) 806 locations_event = new BreakpointEventData( 807 eBreakpointEventTypeLocationsRemoved, shared_from_this()); 808 else 809 locations_event = nullptr; 810 811 for (BreakpointLocationSP loc_sp : 812 locations_to_remove.BreakpointLocations()) { 813 m_locations.RemoveLocation(loc_sp); 814 if (locations_event) 815 locations_event->GetBreakpointLocationCollection().Add(loc_sp); 816 } 817 SendBreakpointChangedEvent(locations_event); 818 819 // And announce the new ones. 820 821 if (!IsInternal()) { 822 locations_event = new BreakpointEventData( 823 eBreakpointEventTypeLocationsAdded, shared_from_this()); 824 for (BreakpointLocationSP loc_sp : 825 locations_to_announce.BreakpointLocations()) 826 locations_event->GetBreakpointLocationCollection().Add(loc_sp); 827 828 SendBreakpointChangedEvent(locations_event); 829 } 830 m_locations.Compact(); 831 } 832 } 833 834 void Breakpoint::Dump(Stream *) {} 835 836 size_t Breakpoint::GetNumResolvedLocations() const { 837 // Return the number of breakpoints that are actually resolved and set 838 // down in the inferior process. 839 return m_locations.GetNumResolvedLocations(); 840 } 841 842 size_t Breakpoint::GetNumLocations() const { return m_locations.GetSize(); } 843 844 bool Breakpoint::AddName(llvm::StringRef new_name, Status &error) { 845 if (new_name.empty()) 846 return false; 847 if (!BreakpointID::StringIsBreakpointName(new_name, error)) { 848 error.SetErrorStringWithFormatv("input name \"{0}\" not a breakpoint name.", 849 new_name); 850 return false; 851 } 852 if (!error.Success()) 853 return false; 854 855 m_name_list.insert(new_name); 856 return true; 857 } 858 859 void Breakpoint::GetDescription(Stream *s, lldb::DescriptionLevel level, 860 bool show_locations) { 861 assert(s != nullptr); 862 863 if (!m_kind_description.empty()) { 864 if (level == eDescriptionLevelBrief) { 865 s->PutCString(GetBreakpointKind()); 866 return; 867 } else 868 s->Printf("Kind: %s\n", GetBreakpointKind()); 869 } 870 871 const size_t num_locations = GetNumLocations(); 872 const size_t num_resolved_locations = GetNumResolvedLocations(); 873 874 // They just made the breakpoint, they don't need to be told HOW they made 875 // it... 876 // Also, we'll print the breakpoint number differently depending on whether 877 // there is 1 or more locations. 878 if (level != eDescriptionLevelInitial) { 879 s->Printf("%i: ", GetID()); 880 GetResolverDescription(s); 881 GetFilterDescription(s); 882 } 883 884 switch (level) { 885 case lldb::eDescriptionLevelBrief: 886 case lldb::eDescriptionLevelFull: 887 if (num_locations > 0) { 888 s->Printf(", locations = %" PRIu64, (uint64_t)num_locations); 889 if (num_resolved_locations > 0) 890 s->Printf(", resolved = %" PRIu64 ", hit count = %d", 891 (uint64_t)num_resolved_locations, GetHitCount()); 892 } else { 893 // Don't print the pending notification for exception resolvers since we 894 // don't generally 895 // know how to set them until the target is run. 896 if (m_resolver_sp->getResolverID() != 897 BreakpointResolver::ExceptionResolver) 898 s->Printf(", locations = 0 (pending)"); 899 } 900 901 GetOptions()->GetDescription(s, level); 902 903 if (m_precondition_sp) 904 m_precondition_sp->GetDescription(*s, level); 905 906 if (level == lldb::eDescriptionLevelFull) { 907 if (!m_name_list.empty()) { 908 s->EOL(); 909 s->Indent(); 910 s->Printf("Names:"); 911 s->EOL(); 912 s->IndentMore(); 913 for (std::string name : m_name_list) { 914 s->Indent(); 915 s->Printf("%s\n", name.c_str()); 916 } 917 s->IndentLess(); 918 } 919 s->IndentLess(); 920 s->EOL(); 921 } 922 break; 923 924 case lldb::eDescriptionLevelInitial: 925 s->Printf("Breakpoint %i: ", GetID()); 926 if (num_locations == 0) { 927 s->Printf("no locations (pending)."); 928 } else if (num_locations == 1 && !show_locations) { 929 // There is only one location, so we'll just print that location 930 // information. 931 GetLocationAtIndex(0)->GetDescription(s, level); 932 } else { 933 s->Printf("%" PRIu64 " locations.", static_cast<uint64_t>(num_locations)); 934 } 935 s->EOL(); 936 break; 937 938 case lldb::eDescriptionLevelVerbose: 939 // Verbose mode does a debug dump of the breakpoint 940 Dump(s); 941 s->EOL(); 942 // s->Indent(); 943 GetOptions()->GetDescription(s, level); 944 break; 945 946 default: 947 break; 948 } 949 950 // The brief description is just the location name (1.2 or whatever). That's 951 // pointless to 952 // show in the breakpoint's description, so suppress it. 953 if (show_locations && level != lldb::eDescriptionLevelBrief) { 954 s->IndentMore(); 955 for (size_t i = 0; i < num_locations; ++i) { 956 BreakpointLocation *loc = GetLocationAtIndex(i).get(); 957 loc->GetDescription(s, level); 958 s->EOL(); 959 } 960 s->IndentLess(); 961 } 962 } 963 964 void Breakpoint::GetResolverDescription(Stream *s) { 965 if (m_resolver_sp) 966 m_resolver_sp->GetDescription(s); 967 } 968 969 bool Breakpoint::GetMatchingFileLine(const ConstString &filename, 970 uint32_t line_number, 971 BreakpointLocationCollection &loc_coll) { 972 // TODO: To be correct, this method needs to fill the breakpoint location 973 // collection 974 // with the location IDs which match the filename and line_number. 975 // 976 977 if (m_resolver_sp) { 978 BreakpointResolverFileLine *resolverFileLine = 979 dyn_cast<BreakpointResolverFileLine>(m_resolver_sp.get()); 980 if (resolverFileLine && 981 resolverFileLine->m_file_spec.GetFilename() == filename && 982 resolverFileLine->m_line_number == line_number) { 983 return true; 984 } 985 } 986 return false; 987 } 988 989 void Breakpoint::GetFilterDescription(Stream *s) { 990 m_filter_sp->GetDescription(s); 991 } 992 993 bool Breakpoint::EvaluatePrecondition(StoppointCallbackContext &context) { 994 if (!m_precondition_sp) 995 return true; 996 997 return m_precondition_sp->EvaluatePrecondition(context); 998 } 999 1000 bool Breakpoint::BreakpointPrecondition::EvaluatePrecondition( 1001 StoppointCallbackContext &context) { 1002 return true; 1003 } 1004 1005 void Breakpoint::BreakpointPrecondition::GetDescription( 1006 Stream &stream, lldb::DescriptionLevel level) {} 1007 1008 Status 1009 Breakpoint::BreakpointPrecondition::ConfigurePrecondition(Args &options) { 1010 Status error; 1011 error.SetErrorString("Base breakpoint precondition has no options."); 1012 return error; 1013 } 1014 1015 void Breakpoint::SendBreakpointChangedEvent( 1016 lldb::BreakpointEventType eventKind) { 1017 if (!m_being_created && !IsInternal() && 1018 GetTarget().EventTypeHasListeners( 1019 Target::eBroadcastBitBreakpointChanged)) { 1020 BreakpointEventData *data = 1021 new Breakpoint::BreakpointEventData(eventKind, shared_from_this()); 1022 1023 GetTarget().BroadcastEvent(Target::eBroadcastBitBreakpointChanged, data); 1024 } 1025 } 1026 1027 void Breakpoint::SendBreakpointChangedEvent(BreakpointEventData *data) { 1028 if (data == nullptr) 1029 return; 1030 1031 if (!m_being_created && !IsInternal() && 1032 GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged)) 1033 GetTarget().BroadcastEvent(Target::eBroadcastBitBreakpointChanged, data); 1034 else 1035 delete data; 1036 } 1037 1038 Breakpoint::BreakpointEventData::BreakpointEventData( 1039 BreakpointEventType sub_type, const BreakpointSP &new_breakpoint_sp) 1040 : EventData(), m_breakpoint_event(sub_type), 1041 m_new_breakpoint_sp(new_breakpoint_sp) {} 1042 1043 Breakpoint::BreakpointEventData::~BreakpointEventData() = default; 1044 1045 const ConstString &Breakpoint::BreakpointEventData::GetFlavorString() { 1046 static ConstString g_flavor("Breakpoint::BreakpointEventData"); 1047 return g_flavor; 1048 } 1049 1050 const ConstString &Breakpoint::BreakpointEventData::GetFlavor() const { 1051 return BreakpointEventData::GetFlavorString(); 1052 } 1053 1054 BreakpointSP &Breakpoint::BreakpointEventData::GetBreakpoint() { 1055 return m_new_breakpoint_sp; 1056 } 1057 1058 BreakpointEventType 1059 Breakpoint::BreakpointEventData::GetBreakpointEventType() const { 1060 return m_breakpoint_event; 1061 } 1062 1063 void Breakpoint::BreakpointEventData::Dump(Stream *s) const {} 1064 1065 const Breakpoint::BreakpointEventData * 1066 Breakpoint::BreakpointEventData::GetEventDataFromEvent(const Event *event) { 1067 if (event) { 1068 const EventData *event_data = event->GetData(); 1069 if (event_data && 1070 event_data->GetFlavor() == BreakpointEventData::GetFlavorString()) 1071 return static_cast<const BreakpointEventData *>(event->GetData()); 1072 } 1073 return nullptr; 1074 } 1075 1076 BreakpointEventType 1077 Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent( 1078 const EventSP &event_sp) { 1079 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get()); 1080 1081 if (data == nullptr) 1082 return eBreakpointEventTypeInvalidType; 1083 else 1084 return data->GetBreakpointEventType(); 1085 } 1086 1087 BreakpointSP Breakpoint::BreakpointEventData::GetBreakpointFromEvent( 1088 const EventSP &event_sp) { 1089 BreakpointSP bp_sp; 1090 1091 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get()); 1092 if (data) 1093 bp_sp = data->m_new_breakpoint_sp; 1094 1095 return bp_sp; 1096 } 1097 1098 size_t Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent( 1099 const EventSP &event_sp) { 1100 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get()); 1101 if (data) 1102 return data->m_locations.GetSize(); 1103 1104 return 0; 1105 } 1106 1107 lldb::BreakpointLocationSP 1108 Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent( 1109 const lldb::EventSP &event_sp, uint32_t bp_loc_idx) { 1110 lldb::BreakpointLocationSP bp_loc_sp; 1111 1112 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get()); 1113 if (data) { 1114 bp_loc_sp = data->m_locations.GetByIndex(bp_loc_idx); 1115 } 1116 1117 return bp_loc_sp; 1118 } 1119