1 //===-- SBBreakpointName.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBBreakpointName.h"
11 #include "lldb/API/SBDebugger.h"
12 #include "lldb/API/SBError.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBStringList.h"
15 #include "lldb/API/SBTarget.h"
17 #include "lldb/Breakpoint/BreakpointName.h"
18 #include "lldb/Breakpoint/StoppointCallbackContext.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Interpreter/CommandInterpreter.h"
21 #include "lldb/Interpreter/ScriptInterpreter.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/ThreadSpec.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/Stream.h"
27 #include "SBBreakpointOptionCommon.h"
30 using namespace lldb_private;
34 class SBBreakpointNameImpl {
36 SBBreakpointNameImpl(TargetSP target_sp, const char *name) {
37 if (!name || name[0] == '\0')
44 m_target_wp = target_sp;
47 SBBreakpointNameImpl(SBTarget &sb_target, const char *name);
48 bool operator==(const SBBreakpointNameImpl &rhs);
49 bool operator!=(const SBBreakpointNameImpl &rhs);
51 // For now we take a simple approach and only keep the name, and relook up
52 // the location when we need it.
54 TargetSP GetTarget() const {
55 return m_target_wp.lock();
58 const char *GetName() const {
59 return m_name.c_str();
62 bool IsValid() const {
63 return !m_name.empty() && m_target_wp.lock();
66 lldb_private::BreakpointName *GetBreakpointName() const;
73 SBBreakpointNameImpl::SBBreakpointNameImpl(SBTarget &sb_target,
75 if (!name || name[0] == '\0')
79 if (!sb_target.IsValid())
82 TargetSP target_sp = sb_target.GetSP();
86 m_target_wp = target_sp;
89 bool SBBreakpointNameImpl::operator==(const SBBreakpointNameImpl &rhs) {
90 return m_name == rhs.m_name && m_target_wp.lock() == rhs.m_target_wp.lock();
93 bool SBBreakpointNameImpl::operator!=(const SBBreakpointNameImpl &rhs) {
94 return m_name != rhs.m_name || m_target_wp.lock() != rhs.m_target_wp.lock();
97 lldb_private::BreakpointName *SBBreakpointNameImpl::GetBreakpointName() const {
100 TargetSP target_sp = GetTarget();
104 return target_sp->FindBreakpointName(ConstString(m_name), true, error);
109 SBBreakpointName::SBBreakpointName() {}
111 SBBreakpointName::SBBreakpointName(SBTarget &sb_target, const char *name)
113 m_impl_up.reset(new SBBreakpointNameImpl(sb_target, name));
114 // Call FindBreakpointName here to make sure the name is valid, reset if not:
115 BreakpointName *bp_name = GetBreakpointName();
120 SBBreakpointName::SBBreakpointName(SBBreakpoint &sb_bkpt, const char *name)
122 if (!sb_bkpt.IsValid()) {
126 BreakpointSP bkpt_sp = sb_bkpt.GetSP();
127 Target &target = bkpt_sp->GetTarget();
129 m_impl_up.reset(new SBBreakpointNameImpl(target.shared_from_this(), name));
131 // Call FindBreakpointName here to make sure the name is valid, reset if not:
132 BreakpointName *bp_name = GetBreakpointName();
138 // Now copy over the breakpoint's options:
139 target.ConfigureBreakpointName(*bp_name, *bkpt_sp->GetOptions(),
140 BreakpointName::Permissions());
143 SBBreakpointName::SBBreakpointName(const SBBreakpointName &rhs)
148 m_impl_up.reset(new SBBreakpointNameImpl(rhs.m_impl_up->GetTarget(),
149 rhs.m_impl_up->GetName()));
152 SBBreakpointName::~SBBreakpointName() = default;
154 const SBBreakpointName &SBBreakpointName::operator=(const SBBreakpointName &rhs)
156 if (!rhs.m_impl_up) {
161 m_impl_up.reset(new SBBreakpointNameImpl(rhs.m_impl_up->GetTarget(),
162 rhs.m_impl_up->GetName()));
166 bool SBBreakpointName::operator==(const lldb::SBBreakpointName &rhs) {
167 return *m_impl_up == *rhs.m_impl_up;
170 bool SBBreakpointName::operator!=(const lldb::SBBreakpointName &rhs) {
171 return *m_impl_up != *rhs.m_impl_up;
174 bool SBBreakpointName::IsValid() const {
177 return m_impl_up->IsValid();
180 const char *SBBreakpointName::GetName() const {
182 return "<Invalid Breakpoint Name Object>";
183 return m_impl_up->GetName();
186 void SBBreakpointName::SetEnabled(bool enable) {
187 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
189 BreakpointName *bp_name = GetBreakpointName();
193 LLDB_LOG(log, "Name: {0} enabled: {1}\n", bp_name->GetName(), enable);
194 std::lock_guard<std::recursive_mutex> guard(
195 m_impl_up->GetTarget()->GetAPIMutex());
197 bp_name->GetOptions().SetEnabled(enable);
200 void SBBreakpointName::UpdateName(BreakpointName &bp_name) {
204 TargetSP target_sp = m_impl_up->GetTarget();
207 target_sp->ApplyNameToBreakpoints(bp_name);
211 bool SBBreakpointName::IsEnabled() {
212 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
214 BreakpointName *bp_name = GetBreakpointName();
218 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
219 std::lock_guard<std::recursive_mutex> guard(
220 m_impl_up->GetTarget()->GetAPIMutex());
222 return bp_name->GetOptions().IsEnabled();
225 void SBBreakpointName::SetOneShot(bool one_shot) {
226 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
228 BreakpointName *bp_name = GetBreakpointName();
232 LLDB_LOG(log, "Name: {0} one_shot: {1}\n", bp_name->GetName(), one_shot);
233 std::lock_guard<std::recursive_mutex> guard(
234 m_impl_up->GetTarget()->GetAPIMutex());
236 bp_name->GetOptions().SetOneShot(one_shot);
237 UpdateName(*bp_name);
240 bool SBBreakpointName::IsOneShot() const {
241 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
243 const BreakpointName *bp_name = GetBreakpointName();
247 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
248 std::lock_guard<std::recursive_mutex> guard(
249 m_impl_up->GetTarget()->GetAPIMutex());
251 return bp_name->GetOptions().IsOneShot();
254 void SBBreakpointName::SetIgnoreCount(uint32_t count) {
255 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
257 BreakpointName *bp_name = GetBreakpointName();
261 LLDB_LOG(log, "Name: {0} one_shot: {1}\n", bp_name->GetName(), count);
262 std::lock_guard<std::recursive_mutex> guard(
263 m_impl_up->GetTarget()->GetAPIMutex());
265 bp_name->GetOptions().SetIgnoreCount(count);
266 UpdateName(*bp_name);
269 uint32_t SBBreakpointName::GetIgnoreCount() const {
270 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
272 BreakpointName *bp_name = GetBreakpointName();
276 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
277 std::lock_guard<std::recursive_mutex> guard(
278 m_impl_up->GetTarget()->GetAPIMutex());
280 return bp_name->GetOptions().GetIgnoreCount();
283 void SBBreakpointName::SetCondition(const char *condition) {
284 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
286 BreakpointName *bp_name = GetBreakpointName();
290 LLDB_LOG(log, "Name: {0} one_shot: {1}\n", bp_name->GetName(),
291 condition ? condition : "<NULL>");
293 std::lock_guard<std::recursive_mutex> guard(
294 m_impl_up->GetTarget()->GetAPIMutex());
296 bp_name->GetOptions().SetCondition(condition);
297 UpdateName(*bp_name);
300 const char *SBBreakpointName::GetCondition() {
301 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
303 BreakpointName *bp_name = GetBreakpointName();
307 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
308 std::lock_guard<std::recursive_mutex> guard(
309 m_impl_up->GetTarget()->GetAPIMutex());
311 return bp_name->GetOptions().GetConditionText();
314 void SBBreakpointName::SetAutoContinue(bool auto_continue) {
315 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
317 BreakpointName *bp_name = GetBreakpointName();
321 LLDB_LOG(log, "Name: {0} auto-continue: {1}\n", bp_name->GetName(), auto_continue);
323 std::lock_guard<std::recursive_mutex> guard(
324 m_impl_up->GetTarget()->GetAPIMutex());
326 bp_name->GetOptions().SetAutoContinue(auto_continue);
327 UpdateName(*bp_name);
330 bool SBBreakpointName::GetAutoContinue() {
331 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
333 BreakpointName *bp_name = GetBreakpointName();
337 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
338 std::lock_guard<std::recursive_mutex> guard(
339 m_impl_up->GetTarget()->GetAPIMutex());
341 return bp_name->GetOptions().IsAutoContinue();
344 void SBBreakpointName::SetThreadID(tid_t tid) {
345 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
347 BreakpointName *bp_name = GetBreakpointName();
351 LLDB_LOG(log, "Name: {0} tid: {1:x}\n", bp_name->GetName(), tid);
353 std::lock_guard<std::recursive_mutex> guard(
354 m_impl_up->GetTarget()->GetAPIMutex());
356 bp_name->GetOptions().SetThreadID(tid);
357 UpdateName(*bp_name);
360 tid_t SBBreakpointName::GetThreadID() {
361 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
363 BreakpointName *bp_name = GetBreakpointName();
365 return LLDB_INVALID_THREAD_ID;
367 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
368 std::lock_guard<std::recursive_mutex> guard(
369 m_impl_up->GetTarget()->GetAPIMutex());
371 return bp_name->GetOptions().GetThreadSpec()->GetTID();
374 void SBBreakpointName::SetThreadIndex(uint32_t index) {
375 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
377 BreakpointName *bp_name = GetBreakpointName();
381 LLDB_LOG(log, "Name: {0} thread index: {1}\n", bp_name->GetName(), index);
383 std::lock_guard<std::recursive_mutex> guard(
384 m_impl_up->GetTarget()->GetAPIMutex());
386 bp_name->GetOptions().GetThreadSpec()->SetIndex(index);
387 UpdateName(*bp_name);
390 uint32_t SBBreakpointName::GetThreadIndex() const {
391 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
393 BreakpointName *bp_name = GetBreakpointName();
395 return LLDB_INVALID_THREAD_ID;
397 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
398 std::lock_guard<std::recursive_mutex> guard(
399 m_impl_up->GetTarget()->GetAPIMutex());
401 return bp_name->GetOptions().GetThreadSpec()->GetIndex();
404 void SBBreakpointName::SetThreadName(const char *thread_name) {
405 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
407 BreakpointName *bp_name = GetBreakpointName();
411 LLDB_LOG(log, "Name: {0} thread name: {1}\n", bp_name->GetName(), thread_name);
413 std::lock_guard<std::recursive_mutex> guard(
414 m_impl_up->GetTarget()->GetAPIMutex());
416 bp_name->GetOptions().GetThreadSpec()->SetName(thread_name);
417 UpdateName(*bp_name);
420 const char *SBBreakpointName::GetThreadName() const {
421 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
423 BreakpointName *bp_name = GetBreakpointName();
427 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
428 std::lock_guard<std::recursive_mutex> guard(
429 m_impl_up->GetTarget()->GetAPIMutex());
431 return bp_name->GetOptions().GetThreadSpec()->GetName();
434 void SBBreakpointName::SetQueueName(const char *queue_name) {
435 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
437 BreakpointName *bp_name = GetBreakpointName();
441 LLDB_LOG(log, "Name: {0} queue name: {1}\n", bp_name->GetName(), queue_name);
443 std::lock_guard<std::recursive_mutex> guard(
444 m_impl_up->GetTarget()->GetAPIMutex());
446 bp_name->GetOptions().GetThreadSpec()->SetQueueName(queue_name);
447 UpdateName(*bp_name);
450 const char *SBBreakpointName::GetQueueName() const {
451 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
453 BreakpointName *bp_name = GetBreakpointName();
457 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
458 std::lock_guard<std::recursive_mutex> guard(
459 m_impl_up->GetTarget()->GetAPIMutex());
461 return bp_name->GetOptions().GetThreadSpec()->GetQueueName();
464 void SBBreakpointName::SetCommandLineCommands(SBStringList &commands) {
465 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
466 BreakpointName *bp_name = GetBreakpointName();
469 if (commands.GetSize() == 0)
472 LLDB_LOG(log, "Name: {0} commands\n", bp_name->GetName());
474 std::lock_guard<std::recursive_mutex> guard(
475 m_impl_up->GetTarget()->GetAPIMutex());
476 std::unique_ptr<BreakpointOptions::CommandData> cmd_data_up(
477 new BreakpointOptions::CommandData(*commands, eScriptLanguageNone));
479 bp_name->GetOptions().SetCommandDataCallback(cmd_data_up);
480 UpdateName(*bp_name);
483 bool SBBreakpointName::GetCommandLineCommands(SBStringList &commands) {
484 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
486 BreakpointName *bp_name = GetBreakpointName();
490 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
491 StringList command_list;
493 bp_name->GetOptions().GetCommandLineCallbacks(command_list);
495 commands.AppendList(command_list);
499 const char *SBBreakpointName::GetHelpString() const {
500 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
502 BreakpointName *bp_name = GetBreakpointName();
506 LLDB_LOG(log, "Help: {0}\n", bp_name->GetHelp());
507 return bp_name->GetHelp();
510 void SBBreakpointName::SetHelpString(const char *help_string) {
511 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
512 BreakpointName *bp_name = GetBreakpointName();
516 LLDB_LOG(log, "Name: {0} help: {1}\n", bp_name->GetName(), help_string);
518 std::lock_guard<std::recursive_mutex> guard(
519 m_impl_up->GetTarget()->GetAPIMutex());
520 bp_name->SetHelp(help_string);
523 bool SBBreakpointName::GetDescription(SBStream &s) {
524 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
526 BreakpointName *bp_name = GetBreakpointName();
529 s.Printf("No value");
533 LLDB_LOG(log, "Name: {0}\n", bp_name->GetName());
534 std::lock_guard<std::recursive_mutex> guard(
535 m_impl_up->GetTarget()->GetAPIMutex());
536 bp_name->GetDescription(s.get(), eDescriptionLevelFull);
540 void SBBreakpointName::SetCallback(SBBreakpointHitCallback callback,
542 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
543 BreakpointName *bp_name = GetBreakpointName();
546 LLDB_LOG(log, "callback = {1}, baton = {2}", callback, baton);
547 std::lock_guard<std::recursive_mutex> guard(
548 m_impl_up->GetTarget()->GetAPIMutex());
550 BatonSP baton_sp(new SBBreakpointCallbackBaton(callback, baton));
551 bp_name->GetOptions().SetCallback(SBBreakpointCallbackBaton
552 ::PrivateBreakpointHitCallback,
555 UpdateName(*bp_name);
558 void SBBreakpointName::SetScriptCallbackFunction(
559 const char *callback_function_name) {
560 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
562 BreakpointName *bp_name = GetBreakpointName();
566 LLDB_LOG(log, "Name: {0} callback: {1}\n", bp_name->GetName(),
567 callback_function_name);
569 std::lock_guard<std::recursive_mutex> guard(
570 m_impl_up->GetTarget()->GetAPIMutex());
572 BreakpointOptions &bp_options = bp_name->GetOptions();
573 m_impl_up->GetTarget()
575 .GetCommandInterpreter()
576 .GetScriptInterpreter()
577 ->SetBreakpointCommandCallbackFunction(&bp_options,
578 callback_function_name);
579 UpdateName(*bp_name);
582 SBError SBBreakpointName::SetScriptCallbackBody(const char *callback_body_text)
584 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
586 BreakpointName *bp_name = GetBreakpointName();
590 LLDB_LOG(log, "Name: {0} callback: {1}\n", bp_name->GetName(),
593 std::lock_guard<std::recursive_mutex> guard(
594 m_impl_up->GetTarget()->GetAPIMutex());
596 BreakpointOptions &bp_options = bp_name->GetOptions();
598 m_impl_up->GetTarget()
600 .GetCommandInterpreter()
601 .GetScriptInterpreter()
602 ->SetBreakpointCommandCallback(&bp_options, callback_body_text);
603 sb_error.SetError(error);
604 if (!sb_error.Fail())
605 UpdateName(*bp_name);
610 bool SBBreakpointName::GetAllowList() const
612 BreakpointName *bp_name = GetBreakpointName();
615 return bp_name->GetPermissions().GetAllowList();
618 void SBBreakpointName::SetAllowList(bool value)
620 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
622 BreakpointName *bp_name = GetBreakpointName();
626 log->Printf("Setting allow list to %u for %s.", value,
627 bp_name->GetName().AsCString());
628 bp_name->GetPermissions().SetAllowList(value);
631 bool SBBreakpointName::GetAllowDelete()
633 BreakpointName *bp_name = GetBreakpointName();
636 return bp_name->GetPermissions().GetAllowDelete();
639 void SBBreakpointName::SetAllowDelete(bool value)
641 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
643 BreakpointName *bp_name = GetBreakpointName();
647 log->Printf("Setting allow delete to %u for %s.", value,
648 bp_name->GetName().AsCString());
649 bp_name->GetPermissions().SetAllowDelete(value);
652 bool SBBreakpointName::GetAllowDisable()
654 BreakpointName *bp_name = GetBreakpointName();
657 return bp_name->GetPermissions().GetAllowDisable();
660 void SBBreakpointName::SetAllowDisable(bool value)
662 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
664 BreakpointName *bp_name = GetBreakpointName();
668 log->Printf("Setting allow disable to %u for %s.", value,
669 bp_name->GetName().AsCString());
670 bp_name->GetPermissions().SetAllowDisable(value);
673 lldb_private::BreakpointName *SBBreakpointName::GetBreakpointName() const
677 return m_impl_up->GetBreakpointName();