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