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