LLDB mainline
SBBreakpointName.cpp
Go to the documentation of this file.
1//===-- SBBreakpointName.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
10#include "Utils.h"
11#include "lldb/API/SBDebugger.h"
12#include "lldb/API/SBError.h"
13#include "lldb/API/SBStream.h"
16#include "lldb/API/SBTarget.h"
18
21#include "lldb/Core/Debugger.h"
25#include "lldb/Target/Target.h"
27#include "lldb/Utility/Stream.h"
29
31
32using namespace lldb;
33using namespace lldb_private;
34
35namespace lldb
36{
38public:
39 SBBreakpointNameImpl(TargetSP target_sp, const char *name) {
40 if (!name || name[0] == '\0')
41 return;
42 m_name.assign(name);
43
44 if (!target_sp)
45 return;
46
47 m_target_wp = target_sp;
48 }
49
50 SBBreakpointNameImpl(SBTarget &sb_target, const char *name);
51 bool operator==(const SBBreakpointNameImpl &rhs);
52 bool operator!=(const SBBreakpointNameImpl &rhs);
53
54 // For now we take a simple approach and only keep the name, and relook up
55 // the location when we need it.
56
58 return m_target_wp.lock();
59 }
60
61 const char *GetName() const {
62 return m_name.c_str();
63 }
64
65 bool IsValid() const {
66 return !m_name.empty() && m_target_wp.lock();
67 }
68
70
71private:
73 std::string m_name;
74};
75
77 const char *name) {
78 if (!name || name[0] == '\0')
79 return;
80 m_name.assign(name);
81
82 if (!sb_target.IsValid())
83 return;
84
85 TargetSP target_sp = sb_target.GetSP();
86 if (!target_sp)
87 return;
88
89 m_target_wp = target_sp;
90}
91
93 return m_name == rhs.m_name && m_target_wp.lock() == rhs.m_target_wp.lock();
94}
95
97 return m_name != rhs.m_name || m_target_wp.lock() != rhs.m_target_wp.lock();
98}
99
101 if (!IsValid())
102 return nullptr;
103 TargetSP target_sp = GetTarget();
104 if (!target_sp)
105 return nullptr;
107 return target_sp->FindBreakpointName(m_name, true, error);
108}
109
110} // namespace lldb
111
113
114SBBreakpointName::SBBreakpointName(SBTarget &sb_target, const char *name) {
115 LLDB_INSTRUMENT_VA(this, sb_target, name);
116
117 m_impl_up = std::make_unique<SBBreakpointNameImpl>(sb_target, name);
118 // Call FindBreakpointName here to make sure the name is valid, reset if not:
120 if (!bp_name)
121 m_impl_up.reset();
122}
123
125 LLDB_INSTRUMENT_VA(this, sb_bkpt, name);
126
127 if (!sb_bkpt.IsValid()) {
128 m_impl_up.reset();
129 return;
130 }
131 BreakpointSP bkpt_sp = sb_bkpt.GetSP();
132 Target &target = bkpt_sp->GetTarget();
133
134 m_impl_up =
135 std::make_unique<SBBreakpointNameImpl>(target.shared_from_this(), name);
136
137 // Call FindBreakpointName here to make sure the name is valid, reset if not:
139 if (!bp_name) {
140 m_impl_up.reset();
141 return;
142 }
143
144 // Now copy over the breakpoint's options:
145 target.ConfigureBreakpointName(*bp_name, bkpt_sp->GetOptions(),
147}
148
150 LLDB_INSTRUMENT_VA(this, rhs);
151
152 if (!rhs.m_impl_up)
153 return;
154 else
155 m_impl_up = std::make_unique<SBBreakpointNameImpl>(
156 rhs.m_impl_up->GetTarget(), rhs.m_impl_up->GetName());
157}
158
160
162operator=(const SBBreakpointName &rhs) {
163 LLDB_INSTRUMENT_VA(this, rhs);
164
165 if (!rhs.m_impl_up) {
166 m_impl_up.reset();
167 return *this;
168 }
169
170 m_impl_up = std::make_unique<SBBreakpointNameImpl>(rhs.m_impl_up->GetTarget(),
171 rhs.m_impl_up->GetName());
172 return *this;
173}
174
176 LLDB_INSTRUMENT_VA(this, rhs);
177
178 if (!m_impl_up || !rhs.m_impl_up)
179 return m_impl_up == rhs.m_impl_up;
180 return *m_impl_up == *rhs.m_impl_up;
181}
182
184 LLDB_INSTRUMENT_VA(this, rhs);
185
186 if (!m_impl_up || !rhs.m_impl_up)
187 return m_impl_up != rhs.m_impl_up;
188 return *m_impl_up != *rhs.m_impl_up;
189}
190
192 LLDB_INSTRUMENT_VA(this);
193 return this->operator bool();
194}
195SBBreakpointName::operator bool() const {
196 LLDB_INSTRUMENT_VA(this);
197
198 if (!m_impl_up)
199 return false;
200 return m_impl_up->IsValid();
201}
202
203const char *SBBreakpointName::GetName() const {
204 LLDB_INSTRUMENT_VA(this);
205
206 if (!m_impl_up)
207 return "<Invalid Breakpoint Name Object>";
208 TargetSP target_sp = m_impl_up->GetTarget();
209 StringPoolRef pool = target_sp ? GetDebuggerStringPool(*target_sp)
211 return pool.Intern(m_impl_up->GetName());
212}
213
215 LLDB_INSTRUMENT_VA(this, enable);
216
218 if (!bp_name)
219 return;
220
221 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
222 std::lock_guard<TargetAPIMutex> guard(api_lock);
223
224 bp_name->GetOptions().SetEnabled(enable);
225 UpdateName(*bp_name);
226}
227
229 if (!IsValid())
230 return;
231
232 TargetSP target_sp = m_impl_up->GetTarget();
233 if (!target_sp)
234 return;
235 target_sp->ApplyNameToBreakpoints(bp_name);
236
237}
238
240 LLDB_INSTRUMENT_VA(this);
241
243 if (!bp_name)
244 return false;
245
246 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
247 std::lock_guard<TargetAPIMutex> guard(api_lock);
248
249 return bp_name->GetOptions().IsEnabled();
250}
251
252void SBBreakpointName::SetOneShot(bool one_shot) {
253 LLDB_INSTRUMENT_VA(this, one_shot);
254
256 if (!bp_name)
257 return;
258
259 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
260 std::lock_guard<TargetAPIMutex> guard(api_lock);
261
262 bp_name->GetOptions().SetOneShot(one_shot);
263 UpdateName(*bp_name);
264}
265
267 LLDB_INSTRUMENT_VA(this);
268
269 const BreakpointName *bp_name = GetBreakpointName();
270 if (!bp_name)
271 return false;
272
273 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
274 std::lock_guard<TargetAPIMutex> guard(api_lock);
275
276 return bp_name->GetOptions().IsOneShot();
277}
278
280 LLDB_INSTRUMENT_VA(this, count);
281
283 if (!bp_name)
284 return;
285
286 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
287 std::lock_guard<TargetAPIMutex> guard(api_lock);
288
289 bp_name->GetOptions().SetIgnoreCount(count);
290 UpdateName(*bp_name);
291}
292
294 LLDB_INSTRUMENT_VA(this);
295
297 if (!bp_name)
298 return false;
299
300 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
301 std::lock_guard<TargetAPIMutex> guard(api_lock);
302
303 return bp_name->GetOptions().GetIgnoreCount();
304}
305
306void SBBreakpointName::SetCondition(const char *condition) {
307 LLDB_INSTRUMENT_VA(this, condition);
308
310 if (!bp_name)
311 return;
312
313 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
314 std::lock_guard<TargetAPIMutex> guard(api_lock);
315
316 bp_name->GetOptions().SetCondition(StopCondition(condition));
317 UpdateName(*bp_name);
318}
319
321 LLDB_INSTRUMENT_VA(this);
322
324 if (!bp_name)
325 return nullptr;
326
327 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
328 std::lock_guard<TargetAPIMutex> guard(api_lock);
329
330 return GetDebuggerStringPool(*m_impl_up->GetTarget())
331 .Intern(bp_name->GetOptions().GetCondition().GetText());
332}
333
334void SBBreakpointName::SetAutoContinue(bool auto_continue) {
335 LLDB_INSTRUMENT_VA(this, auto_continue);
336
338 if (!bp_name)
339 return;
340
341 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
342 std::lock_guard<TargetAPIMutex> guard(api_lock);
343
344 bp_name->GetOptions().SetAutoContinue(auto_continue);
345 UpdateName(*bp_name);
346}
347
349 LLDB_INSTRUMENT_VA(this);
350
352 if (!bp_name)
353 return false;
354
355 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
356 std::lock_guard<TargetAPIMutex> guard(api_lock);
357
358 return bp_name->GetOptions().IsAutoContinue();
359}
360
362 LLDB_INSTRUMENT_VA(this, tid);
363
365 if (!bp_name)
366 return;
367
368 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
369 std::lock_guard<TargetAPIMutex> guard(api_lock);
370
371 bp_name->GetOptions().SetThreadID(tid);
372 UpdateName(*bp_name);
373}
374
376 LLDB_INSTRUMENT_VA(this);
377
379 if (!bp_name)
381
382 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
383 std::lock_guard<TargetAPIMutex> guard(api_lock);
384
385 return bp_name->GetOptions().GetThreadSpec()->GetTID();
386}
387
389 LLDB_INSTRUMENT_VA(this, index);
390
392 if (!bp_name)
393 return;
394
395 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
396 std::lock_guard<TargetAPIMutex> guard(api_lock);
397
398 bp_name->GetOptions().GetThreadSpec()->SetIndex(index);
399 UpdateName(*bp_name);
400}
401
403 LLDB_INSTRUMENT_VA(this);
404
406 if (!bp_name)
408
409 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
410 std::lock_guard<TargetAPIMutex> guard(api_lock);
411
412 return bp_name->GetOptions().GetThreadSpec()->GetIndex();
413}
414
415void SBBreakpointName::SetThreadName(const char *thread_name) {
416 LLDB_INSTRUMENT_VA(this, thread_name);
417
419 if (!bp_name)
420 return;
421
422 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
423 std::lock_guard<TargetAPIMutex> guard(api_lock);
424
425 bp_name->GetOptions().GetThreadSpec()->SetName(thread_name);
426 UpdateName(*bp_name);
427}
428
430 LLDB_INSTRUMENT_VA(this);
431
433 if (!bp_name)
434 return nullptr;
435
436 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
437 std::lock_guard<TargetAPIMutex> guard(api_lock);
438
439 return GetDebuggerStringPool(*m_impl_up->GetTarget())
440 .Intern(bp_name->GetOptions().GetThreadSpec()->GetName());
441}
442
443void SBBreakpointName::SetQueueName(const char *queue_name) {
444 LLDB_INSTRUMENT_VA(this, queue_name);
445
447 if (!bp_name)
448 return;
449
450 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
451 std::lock_guard<TargetAPIMutex> guard(api_lock);
452
453 bp_name->GetOptions().GetThreadSpec()->SetQueueName(queue_name);
454 UpdateName(*bp_name);
455}
456
458 LLDB_INSTRUMENT_VA(this);
459
461 if (!bp_name)
462 return nullptr;
463
464 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
465 std::lock_guard<TargetAPIMutex> guard(api_lock);
466
467 return GetDebuggerStringPool(*m_impl_up->GetTarget())
469}
470
472 LLDB_INSTRUMENT_VA(this, commands);
473
475 if (!bp_name)
476 return;
477 if (commands.GetSize() == 0)
478 return;
479
480 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
481 std::lock_guard<TargetAPIMutex> guard(api_lock);
482 std::unique_ptr<BreakpointOptions::CommandData> cmd_data_up(
484
485 bp_name->GetOptions().SetCommandDataCallback(cmd_data_up);
486 UpdateName(*bp_name);
487}
488
490 LLDB_INSTRUMENT_VA(this, commands);
491
493 if (!bp_name)
494 return false;
495
496 StringList command_list;
497 bool has_commands =
498 bp_name->GetOptions().GetCommandLineCallbacks(command_list);
499 if (has_commands)
500 commands.AppendList(command_list);
501 return has_commands;
502}
503
505 LLDB_INSTRUMENT_VA(this);
506
508 if (!bp_name)
509 return "";
510
511 return GetDebuggerStringPool(*m_impl_up->GetTarget())
512 .Intern(bp_name->GetHelp());
513}
514
515void SBBreakpointName::SetHelpString(const char *help_string) {
516 LLDB_INSTRUMENT_VA(this, help_string);
517
519 if (!bp_name)
520 return;
521
522 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
523 std::lock_guard<TargetAPIMutex> guard(api_lock);
524 bp_name->SetHelp(help_string);
525}
526
528 LLDB_INSTRUMENT_VA(this, s);
529
531 if (!bp_name)
532 {
533 s.Printf("No value");
534 return false;
535 }
536
537 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
538 std::lock_guard<TargetAPIMutex> guard(api_lock);
540 return true;
541}
542
544 void *baton) {
545 LLDB_INSTRUMENT_VA(this, callback, baton);
546
548 if (!bp_name)
549 return;
550 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
551 std::lock_guard<TargetAPIMutex> guard(api_lock);
552
553 BatonSP baton_sp(new SBBreakpointCallbackBaton(callback, baton));
555 ::PrivateBreakpointHitCallback,
556 baton_sp,
557 false);
558 UpdateName(*bp_name);
559}
560
562 const char *callback_function_name) {
563 LLDB_INSTRUMENT_VA(this, callback_function_name);
564 SBStructuredData empty_args;
565 SetScriptCallbackFunction(callback_function_name, empty_args);
566}
567
569 const char *callback_function_name,
570 SBStructuredData &extra_args) {
571 LLDB_INSTRUMENT_VA(this, callback_function_name, extra_args);
572 SBError sb_error;
574 if (!bp_name) {
575 sb_error = Status::FromErrorString("unrecognized breakpoint name");
576 return sb_error;
577 }
578
579 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
580 std::lock_guard<TargetAPIMutex> guard(api_lock);
581
582 BreakpointOptions &bp_options = bp_name->GetOptions();
583 llvm::Error error = m_impl_up->GetTarget()
584 ->GetDebugger()
585 .GetScriptInterpreter()
586 ->SetBreakpointCommandCallbackFunction(
587 bp_options, callback_function_name,
588 extra_args.m_impl_up->GetObjectSP());
589 sb_error.SetError(Status::FromError(std::move(error)));
590 UpdateName(*bp_name);
591 return sb_error;
592}
593
595SBBreakpointName::SetScriptCallbackBody(const char *callback_body_text) {
596 LLDB_INSTRUMENT_VA(this, callback_body_text);
597
598 SBError sb_error;
600 if (!bp_name)
601 return sb_error;
602
603 TargetAPIMutex api_lock = m_impl_up->GetTarget()->GetAPIMutex();
604 std::lock_guard<TargetAPIMutex> guard(api_lock);
605
606 BreakpointOptions &bp_options = bp_name->GetOptions();
607 llvm::Error error =
608 m_impl_up->GetTarget()
609 ->GetDebugger()
610 .GetScriptInterpreter()
611 ->SetBreakpointCommandCallback(bp_options, callback_body_text,
612 /*is_callback=*/false);
613 sb_error.SetError(Status::FromError(std::move(error)));
614 if (!sb_error.Fail())
615 UpdateName(*bp_name);
616
617 return sb_error;
618}
619
621 LLDB_INSTRUMENT_VA(this);
622
624 if (!bp_name)
625 return false;
626 return bp_name->GetPermissions().GetAllowList();
627}
628
630 LLDB_INSTRUMENT_VA(this, value);
631
633 if (!bp_name)
634 return;
635 bp_name->GetPermissions().SetAllowList(value);
636}
637
639 LLDB_INSTRUMENT_VA(this);
640
642 if (!bp_name)
643 return false;
644 return bp_name->GetPermissions().GetAllowDelete();
645}
646
648 LLDB_INSTRUMENT_VA(this, value);
649
651 if (!bp_name)
652 return;
653 bp_name->GetPermissions().SetAllowDelete(value);
654}
655
657 LLDB_INSTRUMENT_VA(this);
658
660 if (!bp_name)
661 return false;
662 return bp_name->GetPermissions().GetAllowDisable();
663}
664
666 LLDB_INSTRUMENT_VA(this, value);
667
669 if (!bp_name)
670 return;
671 bp_name->GetPermissions().SetAllowDisable(value);
672}
673
675{
676 if (!IsValid())
677 return nullptr;
678 return m_impl_up->GetBreakpointName();
679}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
bool operator==(const SBBreakpointNameImpl &rhs)
bool operator!=(const SBBreakpointNameImpl &rhs)
lldb_private::BreakpointName * GetBreakpointName() const
SBBreakpointNameImpl(TargetSP target_sp, const char *name)
const char * GetName() const
void SetCommandLineCommands(lldb::SBStringList &commands)
const char * GetName() const
void SetQueueName(const char *queue_name)
const char * GetQueueName() const
void SetThreadIndex(uint32_t index)
void SetAutoContinue(bool auto_continue)
bool operator!=(const lldb::SBBreakpointName &rhs)
void SetCondition(const char *condition)
bool GetDescription(lldb::SBStream &description)
const char * GetThreadName() const
void SetThreadName(const char *thread_name)
void SetAllowDisable(bool value)
void SetEnabled(bool enable)
void SetIgnoreCount(uint32_t count)
bool operator==(const lldb::SBBreakpointName &rhs)
std::unique_ptr< SBBreakpointNameImpl > m_impl_up
void SetAllowDelete(bool value)
void SetCallback(SBBreakpointHitCallback callback, void *baton)
SBError SetScriptCallbackBody(const char *script_body_text)
void SetAllowList(bool value)
lldb_private::BreakpointName * GetBreakpointName() const
void SetOneShot(bool one_shot)
uint32_t GetIgnoreCount() const
uint32_t GetThreadIndex() const
void SetHelpString(const char *help_string)
const char * GetHelpString() const
void SetThreadID(lldb::tid_t sb_thread_id)
void UpdateName(lldb_private::BreakpointName &bp_name)
void SetScriptCallbackFunction(const char *callback_function_name)
const lldb::SBBreakpointName & operator=(const lldb::SBBreakpointName &rhs)
bool GetCommandLineCommands(lldb::SBStringList &commands)
lldb::BreakpointSP GetSP() const
void SetError(uint32_t err, lldb::ErrorType type)
Definition SBError.cpp:124
bool Fail() const
Definition SBError.cpp:70
lldb_private::Stream * get()
Definition SBStream.cpp:177
uint32_t GetSize() const
void AppendList(const char **strv, int strc)
StructuredDataImplUP m_impl_up
bool IsValid() const
Definition SBTarget.cpp:171
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:600
bool GetDescription(Stream *s, lldb::DescriptionLevel level)
void SetHelp(const char *description)
BreakpointOptions & GetOptions()
"lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a breakpoint or breakpoint lo...
void SetIgnoreCount(uint32_t n)
Set the breakpoint to ignore the next count breakpoint hits.
void SetCondition(StopCondition condition)
Set the breakpoint stop condition.
void SetEnabled(bool enabled)
If enable is true, enable the breakpoint, if false disable it.
const StopCondition & GetCondition() const
Return the breakpoint condition.
bool GetCommandLineCallbacks(StringList &command_list)
Returns the command line commands for the callback on this breakpoint.
bool IsOneShot() const
Check the One-shot state.
bool IsEnabled() const
Check the Enable/Disable state.
uint32_t GetIgnoreCount() const
Return the current Ignore Count.
bool IsAutoContinue() const
Check the auto-continue state.
ThreadSpec * GetThreadSpec()
Returns a pointer to the ThreadSpec for this option, creating it.
void SetOneShot(bool one_shot)
If enable is true, enable the breakpoint, if false disable it.
void SetAutoContinue(bool auto_continue)
Set the auto-continue state.
void SetThreadID(lldb::tid_t thread_id)
void SetCommandDataCallback(std::unique_ptr< CommandData > &cmd_data)
Set a callback based on BreakpointOptions::CommandData.
void SetCallback(BreakpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous=false)
Adds a callback to the breakpoint option set.
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
llvm::StringRef GetText() const
A handle to a StringPool that does not own it.
Definition StringPool.h:72
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
A Lockable handle over a Target's API mutex, returned by Target::GetAPIMutex() and backing the public...
void ConfigureBreakpointName(BreakpointName &bp_name, const BreakpointOptions &options, const BreakpointName::Permissions &permissions)
Definition Target.cpp:947
void SetIndex(uint32_t index)
Definition ThreadSpec.h:47
void SetName(llvm::StringRef name)
Definition ThreadSpec.h:51
uint32_t GetIndex() const
Definition ThreadSpec.h:57
const char * GetName() const
void SetQueueName(llvm::StringRef queue_name)
Definition ThreadSpec.h:53
const char * GetQueueName() const
lldb::tid_t GetTID() const
Definition ThreadSpec.h:59
#define LLDB_INVALID_THREAD_ID
A class that represents a running process on the host machine.
StringPoolRef GetDebuggerStringPool(const Target &target)
Strings interned here live as long as target's debugger.
Definition Utils.h:21
@ eScriptLanguageNone
@ eDescriptionLevelFull
class LLDB_API SBBreakpointNameImpl
Definition SBDefines.h:55
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::Baton > BatonSP
bool(* SBBreakpointHitCallback)(void *baton, lldb::SBProcess &process, lldb::SBThread &thread, lldb::SBBreakpointLocation &location)
Definition SBDefines.h:145
std::weak_ptr< lldb_private::Target > TargetWP
std::shared_ptr< lldb_private::Target > TargetSP
uint64_t tid_t
Definition lldb-types.h:85