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