Bug Summary

File:tools/lldb/source/Breakpoint/Breakpoint.cpp
Warning:line 212, column 7
Value stored to 'success' is never read

Annotated Source Code

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
36using namespace lldb;
37using namespace lldb_private;
38using namespace llvm;
39
40const ConstString &Breakpoint::GetEventIdentifier() {
41 static ConstString g_identifier("event-identifier.breakpoint.changed");
42 return g_identifier;
43}
44
45const char *Breakpoint::g_option_names[static_cast<uint32_t>(
46 Breakpoint::OptionNames::LastOptionName)]{"Names", "Hardware"};
47
48//----------------------------------------------------------------------
49// Breakpoint constructor
50//----------------------------------------------------------------------
51Breakpoint::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()), m_locations(*this),
57 m_resolve_indirect_symbols(resolve_indirect_symbols), m_hit_count(0) {
58 m_being_created = false;
59}
60
61Breakpoint::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//----------------------------------------------------------------------
76Breakpoint::~Breakpoint() = default;
77
78//----------------------------------------------------------------------
79// Serialization
80//----------------------------------------------------------------------
81StructuredData::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
129lldb::BreakpointSP Breakpoint::CreateFromStructuredData(
130 Target &target, StructuredData::ObjectSP &object_data, Error &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 Error 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 std::string name;
211 Error error;
212 success = names_array->GetItemAtIndexAsString(i, name);
Value stored to 'success' is never read
213 result_sp->AddName(name.c_str(), error);
214 }
215 }
216
217 return result_sp;
218}
219
220bool 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 std::string 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
255const lldb::TargetSP Breakpoint::GetTargetSP() {
256 return m_target.shared_from_this();
257}
258
259bool Breakpoint::IsInternal() const { return LLDB_BREAK_ID_IS_INTERNAL(m_bid)((m_bid) < 0); }
260
261BreakpointLocationSP Breakpoint::AddLocation(const Address &addr,
262 bool *new_location) {
263 return m_locations.AddLocation(addr, m_resolve_indirect_symbols,
264 new_location);
265}
266
267BreakpointLocationSP Breakpoint::FindLocationByAddress(const Address &addr) {
268 return m_locations.FindByAddress(addr);
269}
270
271break_id_t Breakpoint::FindLocationIDByAddress(const Address &addr) {
272 return m_locations.FindIDByAddress(addr);
273}
274
275BreakpointLocationSP Breakpoint::FindLocationByID(break_id_t bp_loc_id) {
276 return m_locations.FindByID(bp_loc_id);
277}
278
279BreakpointLocationSP Breakpoint::GetLocationAtIndex(size_t index) {
280 return m_locations.GetByIndex(index);
281}
282
283void 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
295void 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
309bool Breakpoint::IsEnabled() { return m_options_up->IsEnabled(); }
310
311void 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
319void Breakpoint::DecrementIgnoreCount() {
320 uint32_t ignore = m_options_up->GetIgnoreCount();
321 if (ignore != 0)
322 m_options_up->SetIgnoreCount(ignore - 1);
323}
324
325uint32_t Breakpoint::GetIgnoreCount() const {
326 return m_options_up->GetIgnoreCount();
327}
328
329bool 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
343uint32_t Breakpoint::GetHitCount() const { return m_hit_count; }
344
345bool Breakpoint::IsOneShot() const { return m_options_up->IsOneShot(); }
346
347void Breakpoint::SetOneShot(bool one_shot) {
348 m_options_up->SetOneShot(one_shot);
349}
350
351void Breakpoint::SetThreadID(lldb::tid_t thread_id) {
352 if (m_options_up->GetThreadSpec()->GetTID() == thread_id)
353 return;
354
355 m_options_up->GetThreadSpec()->SetTID(thread_id);
356 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged);
357}
358
359lldb::tid_t Breakpoint::GetThreadID() const {
360 if (m_options_up->GetThreadSpecNoCreate() == nullptr)
361 return LLDB_INVALID_THREAD_ID0;
362 else
363 return m_options_up->GetThreadSpecNoCreate()->GetTID();
364}
365
366void Breakpoint::SetThreadIndex(uint32_t index) {
367 if (m_options_up->GetThreadSpec()->GetIndex() == index)
368 return;
369
370 m_options_up->GetThreadSpec()->SetIndex(index);
371 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged);
372}
373
374uint32_t Breakpoint::GetThreadIndex() const {
375 if (m_options_up->GetThreadSpecNoCreate() == nullptr)
376 return 0;
377 else
378 return m_options_up->GetThreadSpecNoCreate()->GetIndex();
379}
380
381void Breakpoint::SetThreadName(const char *thread_name) {
382 if (m_options_up->GetThreadSpec()->GetName() != nullptr &&
383 ::strcmp(m_options_up->GetThreadSpec()->GetName(), thread_name) == 0)
384 return;
385
386 m_options_up->GetThreadSpec()->SetName(thread_name);
387 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged);
388}
389
390const char *Breakpoint::GetThreadName() const {
391 if (m_options_up->GetThreadSpecNoCreate() == nullptr)
392 return nullptr;
393 else
394 return m_options_up->GetThreadSpecNoCreate()->GetName();
395}
396
397void Breakpoint::SetQueueName(const char *queue_name) {
398 if (m_options_up->GetThreadSpec()->GetQueueName() != nullptr &&
399 ::strcmp(m_options_up->GetThreadSpec()->GetQueueName(), queue_name) == 0)
400 return;
401
402 m_options_up->GetThreadSpec()->SetQueueName(queue_name);
403 SendBreakpointChangedEvent(eBreakpointEventTypeThreadChanged);
404}
405
406const char *Breakpoint::GetQueueName() const {
407 if (m_options_up->GetThreadSpecNoCreate() == nullptr)
408 return nullptr;
409 else
410 return m_options_up->GetThreadSpecNoCreate()->GetQueueName();
411}
412
413void Breakpoint::SetCondition(const char *condition) {
414 m_options_up->SetCondition(condition);
415 SendBreakpointChangedEvent(eBreakpointEventTypeConditionChanged);
416}
417
418const char *Breakpoint::GetConditionText() const {
419 return m_options_up->GetConditionText();
420}
421
422// This function is used when "baton" doesn't need to be freed
423void Breakpoint::SetCallback(BreakpointHitCallback callback, void *baton,
424 bool is_synchronous) {
425 // The default "Baton" class will keep a copy of "baton" and won't free
426 // or delete it when it goes goes out of scope.
427 m_options_up->SetCallback(callback, std::make_shared<UntypedBaton>(baton),
428 is_synchronous);
429
430 SendBreakpointChangedEvent(eBreakpointEventTypeCommandChanged);
431}
432
433// This function is used when a baton needs to be freed and therefore is
434// contained in a "Baton" subclass.
435void Breakpoint::SetCallback(BreakpointHitCallback callback,
436 const BatonSP &callback_baton_sp,
437 bool is_synchronous) {
438 m_options_up->SetCallback(callback, callback_baton_sp, is_synchronous);
439}
440
441void Breakpoint::ClearCallback() { m_options_up->ClearCallback(); }
442
443bool Breakpoint::InvokeCallback(StoppointCallbackContext *context,
444 break_id_t bp_loc_id) {
445 return m_options_up->InvokeCallback(context, GetID(), bp_loc_id);
446}
447
448BreakpointOptions *Breakpoint::GetOptions() { return m_options_up.get(); }
449
450void Breakpoint::ResolveBreakpoint() {
451 if (m_resolver_sp)
452 m_resolver_sp->ResolveBreakpoint(*m_filter_sp);
453}
454
455void Breakpoint::ResolveBreakpointInModules(
456 ModuleList &module_list, BreakpointLocationCollection &new_locations) {
457 m_locations.StartRecordingNewLocations(new_locations);
458
459 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
460
461 m_locations.StopRecordingNewLocations();
462}
463
464void Breakpoint::ResolveBreakpointInModules(ModuleList &module_list,
465 bool send_event) {
466 if (m_resolver_sp) {
467 // If this is not an internal breakpoint, set up to record the new
468 // locations, then dispatch
469 // an event with the new locations.
470 if (!IsInternal() && send_event) {
471 BreakpointEventData *new_locations_event = new BreakpointEventData(
472 eBreakpointEventTypeLocationsAdded, shared_from_this());
473
474 ResolveBreakpointInModules(
475 module_list, new_locations_event->GetBreakpointLocationCollection());
476
477 if (new_locations_event->GetBreakpointLocationCollection().GetSize() !=
478 0) {
479 SendBreakpointChangedEvent(new_locations_event);
480 } else
481 delete new_locations_event;
482 } else {
483 m_resolver_sp->ResolveBreakpointInModules(*m_filter_sp, module_list);
484 }
485 }
486}
487
488void Breakpoint::ClearAllBreakpointSites() {
489 m_locations.ClearAllBreakpointSites();
490}
491
492//----------------------------------------------------------------------
493// ModulesChanged: Pass in a list of new modules, and
494//----------------------------------------------------------------------
495
496void Breakpoint::ModulesChanged(ModuleList &module_list, bool load,
497 bool delete_locations) {
498 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS(1u << 5)));
499 if (log)
500 log->Printf("Breakpoint::ModulesChanged: num_modules: %zu load: %i "
501 "delete_locations: %i\n",
502 module_list.GetSize(), load, delete_locations);
503
504 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
505 if (load) {
506 // The logic for handling new modules is:
507 // 1) If the filter rejects this module, then skip it.
508 // 2) Run through the current location list and if there are any locations
509 // for that module, we mark the module as "seen" and we don't try to
510 // re-resolve
511 // breakpoint locations for that module.
512 // However, we do add breakpoint sites to these locations if needed.
513 // 3) If we don't see this module in our breakpoint location list, call
514 // ResolveInModules.
515
516 ModuleList new_modules; // We'll stuff the "unseen" modules in this list,
517 // and then resolve
518 // them after the locations pass. Have to do it this way because
519 // resolving breakpoints will add new locations potentially.
520
521 for (ModuleSP module_sp : module_list.ModulesNoLocking()) {
522 bool seen = false;
523 if (!m_filter_sp->ModulePasses(module_sp))
524 continue;
525
526 for (BreakpointLocationSP break_loc_sp :
527 m_locations.BreakpointLocations()) {
528 if (!break_loc_sp->IsEnabled())
529 continue;
530 SectionSP section_sp(break_loc_sp->GetAddress().GetSection());
531 if (!section_sp || section_sp->GetModule() == module_sp) {
532 if (!seen)
533 seen = true;
534
535 if (!break_loc_sp->ResolveBreakpointSite()) {
536 if (log)
537 log->Printf("Warning: could not set breakpoint site for "
538 "breakpoint location %d of breakpoint %d.\n",
539 break_loc_sp->GetID(), GetID());
540 }
541 }
542 }
543
544 if (!seen)
545 new_modules.AppendIfNeeded(module_sp);
546 }
547
548 if (new_modules.GetSize() > 0) {
549 ResolveBreakpointInModules(new_modules);
550 }
551 } else {
552 // Go through the currently set locations and if any have breakpoints in
553 // the module list, then remove their breakpoint sites, and their locations
554 // if asked to.
555
556 BreakpointEventData *removed_locations_event;
557 if (!IsInternal())
558 removed_locations_event = new BreakpointEventData(
559 eBreakpointEventTypeLocationsRemoved, shared_from_this());
560 else
561 removed_locations_event = nullptr;
562
563 size_t num_modules = module_list.GetSize();
564 for (size_t i = 0; i < num_modules; i++) {
565 ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(i));
566 if (m_filter_sp->ModulePasses(module_sp)) {
567 size_t loc_idx = 0;
568 size_t num_locations = m_locations.GetSize();
569 BreakpointLocationCollection locations_to_remove;
570 for (loc_idx = 0; loc_idx < num_locations; loc_idx++) {
571 BreakpointLocationSP break_loc_sp(m_locations.GetByIndex(loc_idx));
572 SectionSP section_sp(break_loc_sp->GetAddress().GetSection());
573 if (section_sp && section_sp->GetModule() == module_sp) {
574 // Remove this breakpoint since the shared library is
575 // unloaded, but keep the breakpoint location around
576 // so we always get complete hit count and breakpoint
577 // lifetime info
578 break_loc_sp->ClearBreakpointSite();
579 if (removed_locations_event) {
580 removed_locations_event->GetBreakpointLocationCollection().Add(
581 break_loc_sp);
582 }
583 if (delete_locations)
584 locations_to_remove.Add(break_loc_sp);
585 }
586 }
587
588 if (delete_locations) {
589 size_t num_locations_to_remove = locations_to_remove.GetSize();
590 for (loc_idx = 0; loc_idx < num_locations_to_remove; loc_idx++)
591 m_locations.RemoveLocation(locations_to_remove.GetByIndex(loc_idx));
592 }
593 }
594 }
595 SendBreakpointChangedEvent(removed_locations_event);
596 }
597}
598
599namespace {
600static bool SymbolContextsMightBeEquivalent(SymbolContext &old_sc,
601 SymbolContext &new_sc) {
602 bool equivalent_scs = false;
603
604 if (old_sc.module_sp.get() == new_sc.module_sp.get()) {
605 // If these come from the same module, we can directly compare the pointers:
606 if (old_sc.comp_unit && new_sc.comp_unit &&
607 (old_sc.comp_unit == new_sc.comp_unit)) {
608 if (old_sc.function && new_sc.function &&
609 (old_sc.function == new_sc.function)) {
610 equivalent_scs = true;
611 }
612 } else if (old_sc.symbol && new_sc.symbol &&
613 (old_sc.symbol == new_sc.symbol)) {
614 equivalent_scs = true;
615 }
616 } else {
617 // Otherwise we will compare by name...
618 if (old_sc.comp_unit && new_sc.comp_unit) {
619 if (FileSpec::Equal(*old_sc.comp_unit, *new_sc.comp_unit, true)) {
620 // Now check the functions:
621 if (old_sc.function && new_sc.function &&
622 (old_sc.function->GetName() == new_sc.function->GetName())) {
623 equivalent_scs = true;
624 }
625 }
626 } else if (old_sc.symbol && new_sc.symbol) {
627 if (Mangled::Compare(old_sc.symbol->GetMangled(),
628 new_sc.symbol->GetMangled()) == 0) {
629 equivalent_scs = true;
630 }
631 }
632 }
633 return equivalent_scs;
634}
635} // anonymous namespace
636
637void Breakpoint::ModuleReplaced(ModuleSP old_module_sp,
638 ModuleSP new_module_sp) {
639 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS(1u << 5)));
640 if (log)
641 log->Printf("Breakpoint::ModulesReplaced for %s\n",
642 old_module_sp->GetSpecificationDescription().c_str());
643 // First find all the locations that are in the old module
644
645 BreakpointLocationCollection old_break_locs;
646 for (BreakpointLocationSP break_loc_sp : m_locations.BreakpointLocations()) {
647 SectionSP section_sp = break_loc_sp->GetAddress().GetSection();
648 if (section_sp && section_sp->GetModule() == old_module_sp) {
649 old_break_locs.Add(break_loc_sp);
650 }
651 }
652
653 size_t num_old_locations = old_break_locs.GetSize();
654
655 if (num_old_locations == 0) {
656 // There were no locations in the old module, so we just need to check if
657 // there were any in the new module.
658 ModuleList temp_list;
659 temp_list.Append(new_module_sp);
660 ResolveBreakpointInModules(temp_list);
661 } else {
662 // First search the new module for locations.
663 // Then compare this with the old list, copy over locations that "look the
664 // same"
665 // Then delete the old locations.
666 // Finally remember to post the creation event.
667 //
668 // Two locations are the same if they have the same comp unit & function (by
669 // name) and there are the same number
670 // of locations in the old function as in the new one.
671
672 ModuleList temp_list;
673 temp_list.Append(new_module_sp);
674 BreakpointLocationCollection new_break_locs;
675 ResolveBreakpointInModules(temp_list, new_break_locs);
676 BreakpointLocationCollection locations_to_remove;
677 BreakpointLocationCollection locations_to_announce;
678
679 size_t num_new_locations = new_break_locs.GetSize();
680
681 if (num_new_locations > 0) {
682 // Break out the case of one location -> one location since that's the
683 // most common one, and there's no need
684 // to build up the structures needed for the merge in that case.
685 if (num_new_locations == 1 && num_old_locations == 1) {
686 bool equivalent_locations = false;
687 SymbolContext old_sc, new_sc;
688 // The only way the old and new location can be equivalent is if they
689 // have the same amount of information:
690 BreakpointLocationSP old_loc_sp = old_break_locs.GetByIndex(0);
691 BreakpointLocationSP new_loc_sp = new_break_locs.GetByIndex(0);
692
693 if (old_loc_sp->GetAddress().CalculateSymbolContext(&old_sc) ==
694 new_loc_sp->GetAddress().CalculateSymbolContext(&new_sc)) {
695 equivalent_locations =
696 SymbolContextsMightBeEquivalent(old_sc, new_sc);
697 }
698
699 if (equivalent_locations) {
700 m_locations.SwapLocation(old_loc_sp, new_loc_sp);
701 } else {
702 locations_to_remove.Add(old_loc_sp);
703 locations_to_announce.Add(new_loc_sp);
704 }
705 } else {
706 // We don't want to have to keep computing the SymbolContexts for these
707 // addresses over and over,
708 // so lets get them up front:
709
710 typedef std::map<lldb::break_id_t, SymbolContext> IDToSCMap;
711 IDToSCMap old_sc_map;
712 for (size_t idx = 0; idx < num_old_locations; idx++) {
713 SymbolContext sc;
714 BreakpointLocationSP bp_loc_sp = old_break_locs.GetByIndex(idx);
715 lldb::break_id_t loc_id = bp_loc_sp->GetID();
716 bp_loc_sp->GetAddress().CalculateSymbolContext(&old_sc_map[loc_id]);
717 }
718
719 std::map<lldb::break_id_t, SymbolContext> new_sc_map;
720 for (size_t idx = 0; idx < num_new_locations; idx++) {
721 SymbolContext sc;
722 BreakpointLocationSP bp_loc_sp = new_break_locs.GetByIndex(idx);
723 lldb::break_id_t loc_id = bp_loc_sp->GetID();
724 bp_loc_sp->GetAddress().CalculateSymbolContext(&new_sc_map[loc_id]);
725 }
726 // Take an element from the old Symbol Contexts
727 while (old_sc_map.size() > 0) {
728 lldb::break_id_t old_id = old_sc_map.begin()->first;
729 SymbolContext &old_sc = old_sc_map.begin()->second;
730
731 // Count the number of entries equivalent to this SC for the old list:
732 std::vector<lldb::break_id_t> old_id_vec;
733 old_id_vec.push_back(old_id);
734
735 IDToSCMap::iterator tmp_iter;
736 for (tmp_iter = ++old_sc_map.begin(); tmp_iter != old_sc_map.end();
737 tmp_iter++) {
738 if (SymbolContextsMightBeEquivalent(old_sc, tmp_iter->second))
739 old_id_vec.push_back(tmp_iter->first);
740 }
741
742 // Now find all the equivalent locations in the new list.
743 std::vector<lldb::break_id_t> new_id_vec;
744 for (tmp_iter = new_sc_map.begin(); tmp_iter != new_sc_map.end();
745 tmp_iter++) {
746 if (SymbolContextsMightBeEquivalent(old_sc, tmp_iter->second))
747 new_id_vec.push_back(tmp_iter->first);
748 }
749
750 // Alright, if we have the same number of potentially equivalent
751 // locations in the old
752 // and new modules, we'll just map them one to one in ascending ID
753 // order (assuming the
754 // resolver's order would match the equivalent ones.
755 // Otherwise, we'll dump all the old ones, and just take the new ones,
756 // erasing the elements
757 // from both maps as we go.
758
759 if (old_id_vec.size() == new_id_vec.size()) {
760 sort(old_id_vec.begin(), old_id_vec.end());
761 sort(new_id_vec.begin(), new_id_vec.end());
762 size_t num_elements = old_id_vec.size();
763 for (size_t idx = 0; idx < num_elements; idx++) {
764 BreakpointLocationSP old_loc_sp =
765 old_break_locs.FindByIDPair(GetID(), old_id_vec[idx]);
766 BreakpointLocationSP new_loc_sp =
767 new_break_locs.FindByIDPair(GetID(), new_id_vec[idx]);
768 m_locations.SwapLocation(old_loc_sp, new_loc_sp);
769 old_sc_map.erase(old_id_vec[idx]);
770 new_sc_map.erase(new_id_vec[idx]);
771 }
772 } else {
773 for (lldb::break_id_t old_id : old_id_vec) {
774 locations_to_remove.Add(
775 old_break_locs.FindByIDPair(GetID(), old_id));
776 old_sc_map.erase(old_id);
777 }
778 for (lldb::break_id_t new_id : new_id_vec) {
779 locations_to_announce.Add(
780 new_break_locs.FindByIDPair(GetID(), new_id));
781 new_sc_map.erase(new_id);
782 }
783 }
784 }
785 }
786 }
787
788 // Now remove the remaining old locations, and cons up a removed locations
789 // event.
790 // Note, we don't put the new locations that were swapped with an old
791 // location on the locations_to_remove
792 // list, so we don't need to worry about telling the world about removing a
793 // location we didn't tell them
794 // about adding.
795
796 BreakpointEventData *locations_event;
797 if (!IsInternal())
798 locations_event = new BreakpointEventData(
799 eBreakpointEventTypeLocationsRemoved, shared_from_this());
800 else
801 locations_event = nullptr;
802
803 for (BreakpointLocationSP loc_sp :
804 locations_to_remove.BreakpointLocations()) {
805 m_locations.RemoveLocation(loc_sp);
806 if (locations_event)
807 locations_event->GetBreakpointLocationCollection().Add(loc_sp);
808 }
809 SendBreakpointChangedEvent(locations_event);
810
811 // And announce the new ones.
812
813 if (!IsInternal()) {
814 locations_event = new BreakpointEventData(
815 eBreakpointEventTypeLocationsAdded, shared_from_this());
816 for (BreakpointLocationSP loc_sp :
817 locations_to_announce.BreakpointLocations())
818 locations_event->GetBreakpointLocationCollection().Add(loc_sp);
819
820 SendBreakpointChangedEvent(locations_event);
821 }
822 m_locations.Compact();
823 }
824}
825
826void Breakpoint::Dump(Stream *) {}
827
828size_t Breakpoint::GetNumResolvedLocations() const {
829 // Return the number of breakpoints that are actually resolved and set
830 // down in the inferior process.
831 return m_locations.GetNumResolvedLocations();
832}
833
834size_t Breakpoint::GetNumLocations() const { return m_locations.GetSize(); }
835
836bool Breakpoint::AddName(const char *new_name, Error &error) {
837 if (!new_name)
838 return false;
839 if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(new_name), error)) {
840 error.SetErrorStringWithFormat("input name \"%s\" not a breakpoint name.",
841 new_name);
842 return false;
843 }
844 if (!error.Success())
845 return false;
846
847 m_name_list.insert(new_name);
848 return true;
849}
850
851void Breakpoint::GetDescription(Stream *s, lldb::DescriptionLevel level,
852 bool show_locations) {
853 assert(s != nullptr)((s != nullptr) ? static_cast<void> (0) : __assert_fail
("s != nullptr", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301235/tools/lldb/source/Breakpoint/Breakpoint.cpp"
, 853, __PRETTY_FUNCTION__))
;
854
855 if (!m_kind_description.empty()) {
856 if (level == eDescriptionLevelBrief) {
857 s->PutCString(GetBreakpointKind());
858 return;
859 } else
860 s->Printf("Kind: %s\n", GetBreakpointKind());
861 }
862
863 const size_t num_locations = GetNumLocations();
864 const size_t num_resolved_locations = GetNumResolvedLocations();
865
866 // They just made the breakpoint, they don't need to be told HOW they made
867 // it...
868 // Also, we'll print the breakpoint number differently depending on whether
869 // there is 1 or more locations.
870 if (level != eDescriptionLevelInitial) {
871 s->Printf("%i: ", GetID());
872 GetResolverDescription(s);
873 GetFilterDescription(s);
874 }
875
876 switch (level) {
877 case lldb::eDescriptionLevelBrief:
878 case lldb::eDescriptionLevelFull:
879 if (num_locations > 0) {
880 s->Printf(", locations = %" PRIu64"l" "u", (uint64_t)num_locations);
881 if (num_resolved_locations > 0)
882 s->Printf(", resolved = %" PRIu64"l" "u" ", hit count = %d",
883 (uint64_t)num_resolved_locations, GetHitCount());
884 } else {
885 // Don't print the pending notification for exception resolvers since we
886 // don't generally
887 // know how to set them until the target is run.
888 if (m_resolver_sp->getResolverID() !=
889 BreakpointResolver::ExceptionResolver)
890 s->Printf(", locations = 0 (pending)");
891 }
892
893 GetOptions()->GetDescription(s, level);
894
895 if (m_precondition_sp)
896 m_precondition_sp->GetDescription(*s, level);
897
898 if (level == lldb::eDescriptionLevelFull) {
899 if (!m_name_list.empty()) {
900 s->EOL();
901 s->Indent();
902 s->Printf("Names:");
903 s->EOL();
904 s->IndentMore();
905 for (std::string name : m_name_list) {
906 s->Indent();
907 s->Printf("%s\n", name.c_str());
908 }
909 s->IndentLess();
910 }
911 s->IndentLess();
912 s->EOL();
913 }
914 break;
915
916 case lldb::eDescriptionLevelInitial:
917 s->Printf("Breakpoint %i: ", GetID());
918 if (num_locations == 0) {
919 s->Printf("no locations (pending).");
920 } else if (num_locations == 1 && !show_locations) {
921 // There is only one location, so we'll just print that location
922 // information.
923 GetLocationAtIndex(0)->GetDescription(s, level);
924 } else {
925 s->Printf("%" PRIu64"l" "u" " locations.", static_cast<uint64_t>(num_locations));
926 }
927 s->EOL();
928 break;
929
930 case lldb::eDescriptionLevelVerbose:
931 // Verbose mode does a debug dump of the breakpoint
932 Dump(s);
933 s->EOL();
934 // s->Indent();
935 GetOptions()->GetDescription(s, level);
936 break;
937
938 default:
939 break;
940 }
941
942 // The brief description is just the location name (1.2 or whatever). That's
943 // pointless to
944 // show in the breakpoint's description, so suppress it.
945 if (show_locations && level != lldb::eDescriptionLevelBrief) {
946 s->IndentMore();
947 for (size_t i = 0; i < num_locations; ++i) {
948 BreakpointLocation *loc = GetLocationAtIndex(i).get();
949 loc->GetDescription(s, level);
950 s->EOL();
951 }
952 s->IndentLess();
953 }
954}
955
956void Breakpoint::GetResolverDescription(Stream *s) {
957 if (m_resolver_sp)
958 m_resolver_sp->GetDescription(s);
959}
960
961bool Breakpoint::GetMatchingFileLine(const ConstString &filename,
962 uint32_t line_number,
963 BreakpointLocationCollection &loc_coll) {
964 // TODO: To be correct, this method needs to fill the breakpoint location
965 // collection
966 // with the location IDs which match the filename and line_number.
967 //
968
969 if (m_resolver_sp) {
970 BreakpointResolverFileLine *resolverFileLine =
971 dyn_cast<BreakpointResolverFileLine>(m_resolver_sp.get());
972 if (resolverFileLine &&
973 resolverFileLine->m_file_spec.GetFilename() == filename &&
974 resolverFileLine->m_line_number == line_number) {
975 return true;
976 }
977 }
978 return false;
979}
980
981void Breakpoint::GetFilterDescription(Stream *s) {
982 m_filter_sp->GetDescription(s);
983}
984
985bool Breakpoint::EvaluatePrecondition(StoppointCallbackContext &context) {
986 if (!m_precondition_sp)
987 return true;
988
989 return m_precondition_sp->EvaluatePrecondition(context);
990}
991
992bool Breakpoint::BreakpointPrecondition::EvaluatePrecondition(
993 StoppointCallbackContext &context) {
994 return true;
995}
996
997void Breakpoint::BreakpointPrecondition::GetDescription(
998 Stream &stream, lldb::DescriptionLevel level) {}
999
1000Error Breakpoint::BreakpointPrecondition::ConfigurePrecondition(Args &options) {
1001 Error error;
1002 error.SetErrorString("Base breakpoint precondition has no options.");
1003 return error;
1004}
1005
1006void Breakpoint::SendBreakpointChangedEvent(
1007 lldb::BreakpointEventType eventKind) {
1008 if (!m_being_created && !IsInternal() &&
1009 GetTarget().EventTypeHasListeners(
1010 Target::eBroadcastBitBreakpointChanged)) {
1011 BreakpointEventData *data =
1012 new Breakpoint::BreakpointEventData(eventKind, shared_from_this());
1013
1014 GetTarget().BroadcastEvent(Target::eBroadcastBitBreakpointChanged, data);
1015 }
1016}
1017
1018void Breakpoint::SendBreakpointChangedEvent(BreakpointEventData *data) {
1019 if (data == nullptr)
1020 return;
1021
1022 if (!m_being_created && !IsInternal() &&
1023 GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
1024 GetTarget().BroadcastEvent(Target::eBroadcastBitBreakpointChanged, data);
1025 else
1026 delete data;
1027}
1028
1029Breakpoint::BreakpointEventData::BreakpointEventData(
1030 BreakpointEventType sub_type, const BreakpointSP &new_breakpoint_sp)
1031 : EventData(), m_breakpoint_event(sub_type),
1032 m_new_breakpoint_sp(new_breakpoint_sp) {}
1033
1034Breakpoint::BreakpointEventData::~BreakpointEventData() = default;
1035
1036const ConstString &Breakpoint::BreakpointEventData::GetFlavorString() {
1037 static ConstString g_flavor("Breakpoint::BreakpointEventData");
1038 return g_flavor;
1039}
1040
1041const ConstString &Breakpoint::BreakpointEventData::GetFlavor() const {
1042 return BreakpointEventData::GetFlavorString();
1043}
1044
1045BreakpointSP &Breakpoint::BreakpointEventData::GetBreakpoint() {
1046 return m_new_breakpoint_sp;
1047}
1048
1049BreakpointEventType
1050Breakpoint::BreakpointEventData::GetBreakpointEventType() const {
1051 return m_breakpoint_event;
1052}
1053
1054void Breakpoint::BreakpointEventData::Dump(Stream *s) const {}
1055
1056const Breakpoint::BreakpointEventData *
1057Breakpoint::BreakpointEventData::GetEventDataFromEvent(const Event *event) {
1058 if (event) {
1059 const EventData *event_data = event->GetData();
1060 if (event_data &&
1061 event_data->GetFlavor() == BreakpointEventData::GetFlavorString())
1062 return static_cast<const BreakpointEventData *>(event->GetData());
1063 }
1064 return nullptr;
1065}
1066
1067BreakpointEventType
1068Breakpoint::BreakpointEventData::GetBreakpointEventTypeFromEvent(
1069 const EventSP &event_sp) {
1070 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get());
1071
1072 if (data == nullptr)
1073 return eBreakpointEventTypeInvalidType;
1074 else
1075 return data->GetBreakpointEventType();
1076}
1077
1078BreakpointSP Breakpoint::BreakpointEventData::GetBreakpointFromEvent(
1079 const EventSP &event_sp) {
1080 BreakpointSP bp_sp;
1081
1082 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get());
1083 if (data)
1084 bp_sp = data->m_new_breakpoint_sp;
1085
1086 return bp_sp;
1087}
1088
1089size_t Breakpoint::BreakpointEventData::GetNumBreakpointLocationsFromEvent(
1090 const EventSP &event_sp) {
1091 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get());
1092 if (data)
1093 return data->m_locations.GetSize();
1094
1095 return 0;
1096}
1097
1098lldb::BreakpointLocationSP
1099Breakpoint::BreakpointEventData::GetBreakpointLocationAtIndexFromEvent(
1100 const lldb::EventSP &event_sp, uint32_t bp_loc_idx) {
1101 lldb::BreakpointLocationSP bp_loc_sp;
1102
1103 const BreakpointEventData *data = GetEventDataFromEvent(event_sp.get());
1104 if (data) {
1105 bp_loc_sp = data->m_locations.GetByIndex(bp_loc_idx);
1106 }
1107
1108 return bp_loc_sp;
1109}