670 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/API/SBBreakpointName.h"10#include "lldb/API/SBDebugger.h"11#include "lldb/API/SBError.h"12#include "lldb/API/SBStream.h"13#include "lldb/API/SBStringList.h"14#include "lldb/API/SBStructuredData.h"15#include "lldb/API/SBTarget.h"16#include "lldb/Utility/Instrumentation.h"17 18#include "lldb/Breakpoint/BreakpointName.h"19#include "lldb/Breakpoint/StoppointCallbackContext.h"20#include "lldb/Core/Debugger.h"21#include "lldb/Core/StructuredDataImpl.h"22#include "lldb/Interpreter/CommandInterpreter.h"23#include "lldb/Interpreter/ScriptInterpreter.h"24#include "lldb/Target/Target.h"25#include "lldb/Target/ThreadSpec.h"26#include "lldb/Utility/Stream.h"27 28#include "SBBreakpointOptionCommon.h"29 30using namespace lldb;31using namespace lldb_private;32 33namespace lldb34{35class SBBreakpointNameImpl {36public:37 SBBreakpointNameImpl(TargetSP target_sp, const char *name) {38 if (!name || name[0] == '\0')39 return;40 m_name.assign(name);41 42 if (!target_sp)43 return;44 45 m_target_wp = target_sp;46 }47 48 SBBreakpointNameImpl(SBTarget &sb_target, const char *name);49 bool operator==(const SBBreakpointNameImpl &rhs);50 bool operator!=(const SBBreakpointNameImpl &rhs);51 52 // For now we take a simple approach and only keep the name, and relook up53 // the location when we need it.54 55 TargetSP GetTarget() const {56 return m_target_wp.lock();57 }58 59 const char *GetName() const {60 return m_name.c_str();61 }62 63 bool IsValid() const {64 return !m_name.empty() && m_target_wp.lock();65 }66 67 lldb_private::BreakpointName *GetBreakpointName() const;68 69private:70 TargetWP m_target_wp;71 std::string m_name;72};73 74SBBreakpointNameImpl::SBBreakpointNameImpl(SBTarget &sb_target,75 const char *name) {76 if (!name || name[0] == '\0')77 return;78 m_name.assign(name);79 80 if (!sb_target.IsValid())81 return;82 83 TargetSP target_sp = sb_target.GetSP();84 if (!target_sp)85 return;86 87 m_target_wp = target_sp;88}89 90bool SBBreakpointNameImpl::operator==(const SBBreakpointNameImpl &rhs) {91 return m_name == rhs.m_name && m_target_wp.lock() == rhs.m_target_wp.lock();92}93 94bool SBBreakpointNameImpl::operator!=(const SBBreakpointNameImpl &rhs) {95 return m_name != rhs.m_name || m_target_wp.lock() != rhs.m_target_wp.lock();96}97 98lldb_private::BreakpointName *SBBreakpointNameImpl::GetBreakpointName() const {99 if (!IsValid())100 return nullptr;101 TargetSP target_sp = GetTarget();102 if (!target_sp)103 return nullptr;104 Status error;105 return target_sp->FindBreakpointName(ConstString(m_name), true, error);106}107 108} // namespace lldb109 110SBBreakpointName::SBBreakpointName() { LLDB_INSTRUMENT_VA(this); }111 112SBBreakpointName::SBBreakpointName(SBTarget &sb_target, const char *name) {113 LLDB_INSTRUMENT_VA(this, sb_target, name);114 115 m_impl_up = std::make_unique<SBBreakpointNameImpl>(sb_target, name);116 // Call FindBreakpointName here to make sure the name is valid, reset if not:117 BreakpointName *bp_name = GetBreakpointName();118 if (!bp_name)119 m_impl_up.reset();120}121 122SBBreakpointName::SBBreakpointName(SBBreakpoint &sb_bkpt, const char *name) {123 LLDB_INSTRUMENT_VA(this, sb_bkpt, name);124 125 if (!sb_bkpt.IsValid()) {126 m_impl_up.reset();127 return;128 }129 BreakpointSP bkpt_sp = sb_bkpt.GetSP();130 Target &target = bkpt_sp->GetTarget();131 132 m_impl_up =133 std::make_unique<SBBreakpointNameImpl>(target.shared_from_this(), name);134 135 // Call FindBreakpointName here to make sure the name is valid, reset if not:136 BreakpointName *bp_name = GetBreakpointName();137 if (!bp_name) {138 m_impl_up.reset();139 return;140 }141 142 // Now copy over the breakpoint's options:143 target.ConfigureBreakpointName(*bp_name, bkpt_sp->GetOptions(),144 BreakpointName::Permissions());145}146 147SBBreakpointName::SBBreakpointName(const SBBreakpointName &rhs) {148 LLDB_INSTRUMENT_VA(this, rhs);149 150 if (!rhs.m_impl_up)151 return;152 else153 m_impl_up = std::make_unique<SBBreakpointNameImpl>(154 rhs.m_impl_up->GetTarget(), rhs.m_impl_up->GetName());155}156 157SBBreakpointName::~SBBreakpointName() = default;158 159const SBBreakpointName &SBBreakpointName::160operator=(const SBBreakpointName &rhs) {161 LLDB_INSTRUMENT_VA(this, rhs);162 163 if (!rhs.m_impl_up) {164 m_impl_up.reset();165 return *this;166 }167 168 m_impl_up = std::make_unique<SBBreakpointNameImpl>(rhs.m_impl_up->GetTarget(),169 rhs.m_impl_up->GetName());170 return *this;171}172 173bool SBBreakpointName::operator==(const lldb::SBBreakpointName &rhs) {174 LLDB_INSTRUMENT_VA(this, rhs);175 176 return *m_impl_up == *rhs.m_impl_up;177}178 179bool SBBreakpointName::operator!=(const lldb::SBBreakpointName &rhs) {180 LLDB_INSTRUMENT_VA(this, rhs);181 182 return *m_impl_up != *rhs.m_impl_up;183}184 185bool SBBreakpointName::IsValid() const {186 LLDB_INSTRUMENT_VA(this);187 return this->operator bool();188}189SBBreakpointName::operator bool() const {190 LLDB_INSTRUMENT_VA(this);191 192 if (!m_impl_up)193 return false;194 return m_impl_up->IsValid();195}196 197const char *SBBreakpointName::GetName() const {198 LLDB_INSTRUMENT_VA(this);199 200 if (!m_impl_up)201 return "<Invalid Breakpoint Name Object>";202 return ConstString(m_impl_up->GetName()).GetCString();203}204 205void SBBreakpointName::SetEnabled(bool enable) {206 LLDB_INSTRUMENT_VA(this, enable);207 208 BreakpointName *bp_name = GetBreakpointName();209 if (!bp_name)210 return;211 212 std::lock_guard<std::recursive_mutex> guard(213 m_impl_up->GetTarget()->GetAPIMutex());214 215 bp_name->GetOptions().SetEnabled(enable);216}217 218void SBBreakpointName::UpdateName(BreakpointName &bp_name) {219 if (!IsValid())220 return;221 222 TargetSP target_sp = m_impl_up->GetTarget();223 if (!target_sp)224 return;225 target_sp->ApplyNameToBreakpoints(bp_name);226 227}228 229bool SBBreakpointName::IsEnabled() {230 LLDB_INSTRUMENT_VA(this);231 232 BreakpointName *bp_name = GetBreakpointName();233 if (!bp_name)234 return false;235 236 std::lock_guard<std::recursive_mutex> guard(237 m_impl_up->GetTarget()->GetAPIMutex());238 239 return bp_name->GetOptions().IsEnabled();240}241 242void SBBreakpointName::SetOneShot(bool one_shot) {243 LLDB_INSTRUMENT_VA(this, one_shot);244 245 BreakpointName *bp_name = GetBreakpointName();246 if (!bp_name)247 return;248 249 std::lock_guard<std::recursive_mutex> guard(250 m_impl_up->GetTarget()->GetAPIMutex());251 252 bp_name->GetOptions().SetOneShot(one_shot);253 UpdateName(*bp_name);254}255 256bool SBBreakpointName::IsOneShot() const {257 LLDB_INSTRUMENT_VA(this);258 259 const BreakpointName *bp_name = GetBreakpointName();260 if (!bp_name)261 return false;262 263 std::lock_guard<std::recursive_mutex> guard(264 m_impl_up->GetTarget()->GetAPIMutex());265 266 return bp_name->GetOptions().IsOneShot();267}268 269void SBBreakpointName::SetIgnoreCount(uint32_t count) {270 LLDB_INSTRUMENT_VA(this, count);271 272 BreakpointName *bp_name = GetBreakpointName();273 if (!bp_name)274 return;275 276 std::lock_guard<std::recursive_mutex> guard(277 m_impl_up->GetTarget()->GetAPIMutex());278 279 bp_name->GetOptions().SetIgnoreCount(count);280 UpdateName(*bp_name);281}282 283uint32_t SBBreakpointName::GetIgnoreCount() const {284 LLDB_INSTRUMENT_VA(this);285 286 BreakpointName *bp_name = GetBreakpointName();287 if (!bp_name)288 return false;289 290 std::lock_guard<std::recursive_mutex> guard(291 m_impl_up->GetTarget()->GetAPIMutex());292 293 return bp_name->GetOptions().GetIgnoreCount();294}295 296void SBBreakpointName::SetCondition(const char *condition) {297 LLDB_INSTRUMENT_VA(this, condition);298 299 BreakpointName *bp_name = GetBreakpointName();300 if (!bp_name)301 return;302 303 std::lock_guard<std::recursive_mutex> guard(304 m_impl_up->GetTarget()->GetAPIMutex());305 306 bp_name->GetOptions().SetCondition(StopCondition(condition));307 UpdateName(*bp_name);308}309 310const char *SBBreakpointName::GetCondition() {311 LLDB_INSTRUMENT_VA(this);312 313 BreakpointName *bp_name = GetBreakpointName();314 if (!bp_name)315 return nullptr;316 317 std::lock_guard<std::recursive_mutex> guard(318 m_impl_up->GetTarget()->GetAPIMutex());319 320 return ConstString(bp_name->GetOptions().GetCondition().GetText())321 .GetCString();322}323 324void SBBreakpointName::SetAutoContinue(bool auto_continue) {325 LLDB_INSTRUMENT_VA(this, auto_continue);326 327 BreakpointName *bp_name = GetBreakpointName();328 if (!bp_name)329 return;330 331 std::lock_guard<std::recursive_mutex> guard(332 m_impl_up->GetTarget()->GetAPIMutex());333 334 bp_name->GetOptions().SetAutoContinue(auto_continue);335 UpdateName(*bp_name);336}337 338bool SBBreakpointName::GetAutoContinue() {339 LLDB_INSTRUMENT_VA(this);340 341 BreakpointName *bp_name = GetBreakpointName();342 if (!bp_name)343 return false;344 345 std::lock_guard<std::recursive_mutex> guard(346 m_impl_up->GetTarget()->GetAPIMutex());347 348 return bp_name->GetOptions().IsAutoContinue();349}350 351void SBBreakpointName::SetThreadID(lldb::tid_t tid) {352 LLDB_INSTRUMENT_VA(this, tid);353 354 BreakpointName *bp_name = GetBreakpointName();355 if (!bp_name)356 return;357 358 std::lock_guard<std::recursive_mutex> guard(359 m_impl_up->GetTarget()->GetAPIMutex());360 361 bp_name->GetOptions().SetThreadID(tid);362 UpdateName(*bp_name);363}364 365lldb::tid_t SBBreakpointName::GetThreadID() {366 LLDB_INSTRUMENT_VA(this);367 368 BreakpointName *bp_name = GetBreakpointName();369 if (!bp_name)370 return LLDB_INVALID_THREAD_ID;371 372 std::lock_guard<std::recursive_mutex> guard(373 m_impl_up->GetTarget()->GetAPIMutex());374 375 return bp_name->GetOptions().GetThreadSpec()->GetTID();376}377 378void SBBreakpointName::SetThreadIndex(uint32_t index) {379 LLDB_INSTRUMENT_VA(this, index);380 381 BreakpointName *bp_name = GetBreakpointName();382 if (!bp_name)383 return;384 385 std::lock_guard<std::recursive_mutex> guard(386 m_impl_up->GetTarget()->GetAPIMutex());387 388 bp_name->GetOptions().GetThreadSpec()->SetIndex(index);389 UpdateName(*bp_name);390}391 392uint32_t SBBreakpointName::GetThreadIndex() const {393 LLDB_INSTRUMENT_VA(this);394 395 BreakpointName *bp_name = GetBreakpointName();396 if (!bp_name)397 return LLDB_INVALID_THREAD_ID;398 399 std::lock_guard<std::recursive_mutex> guard(400 m_impl_up->GetTarget()->GetAPIMutex());401 402 return bp_name->GetOptions().GetThreadSpec()->GetIndex();403}404 405void SBBreakpointName::SetThreadName(const char *thread_name) {406 LLDB_INSTRUMENT_VA(this, thread_name);407 408 BreakpointName *bp_name = GetBreakpointName();409 if (!bp_name)410 return;411 412 std::lock_guard<std::recursive_mutex> guard(413 m_impl_up->GetTarget()->GetAPIMutex());414 415 bp_name->GetOptions().GetThreadSpec()->SetName(thread_name);416 UpdateName(*bp_name);417}418 419const char *SBBreakpointName::GetThreadName() const {420 LLDB_INSTRUMENT_VA(this);421 422 BreakpointName *bp_name = GetBreakpointName();423 if (!bp_name)424 return nullptr;425 426 std::lock_guard<std::recursive_mutex> guard(427 m_impl_up->GetTarget()->GetAPIMutex());428 429 return ConstString(bp_name->GetOptions().GetThreadSpec()->GetName())430 .GetCString();431}432 433void SBBreakpointName::SetQueueName(const char *queue_name) {434 LLDB_INSTRUMENT_VA(this, queue_name);435 436 BreakpointName *bp_name = GetBreakpointName();437 if (!bp_name)438 return;439 440 std::lock_guard<std::recursive_mutex> guard(441 m_impl_up->GetTarget()->GetAPIMutex());442 443 bp_name->GetOptions().GetThreadSpec()->SetQueueName(queue_name);444 UpdateName(*bp_name);445}446 447const char *SBBreakpointName::GetQueueName() const {448 LLDB_INSTRUMENT_VA(this);449 450 BreakpointName *bp_name = GetBreakpointName();451 if (!bp_name)452 return nullptr;453 454 std::lock_guard<std::recursive_mutex> guard(455 m_impl_up->GetTarget()->GetAPIMutex());456 457 return ConstString(bp_name->GetOptions().GetThreadSpec()->GetQueueName())458 .GetCString();459}460 461void SBBreakpointName::SetCommandLineCommands(SBStringList &commands) {462 LLDB_INSTRUMENT_VA(this, commands);463 464 BreakpointName *bp_name = GetBreakpointName();465 if (!bp_name)466 return;467 if (commands.GetSize() == 0)468 return;469 470 471 std::lock_guard<std::recursive_mutex> guard(472 m_impl_up->GetTarget()->GetAPIMutex());473 std::unique_ptr<BreakpointOptions::CommandData> cmd_data_up(474 new BreakpointOptions::CommandData(*commands, eScriptLanguageNone));475 476 bp_name->GetOptions().SetCommandDataCallback(cmd_data_up);477 UpdateName(*bp_name);478}479 480bool SBBreakpointName::GetCommandLineCommands(SBStringList &commands) {481 LLDB_INSTRUMENT_VA(this, commands);482 483 BreakpointName *bp_name = GetBreakpointName();484 if (!bp_name)485 return false;486 487 StringList command_list;488 bool has_commands =489 bp_name->GetOptions().GetCommandLineCallbacks(command_list);490 if (has_commands)491 commands.AppendList(command_list);492 return has_commands;493}494 495const char *SBBreakpointName::GetHelpString() const {496 LLDB_INSTRUMENT_VA(this);497 498 BreakpointName *bp_name = GetBreakpointName();499 if (!bp_name)500 return "";501 502 return ConstString(bp_name->GetHelp()).GetCString();503}504 505void SBBreakpointName::SetHelpString(const char *help_string) {506 LLDB_INSTRUMENT_VA(this, help_string);507 508 BreakpointName *bp_name = GetBreakpointName();509 if (!bp_name)510 return;511 512 513 std::lock_guard<std::recursive_mutex> guard(514 m_impl_up->GetTarget()->GetAPIMutex());515 bp_name->SetHelp(help_string);516}517 518bool SBBreakpointName::GetDescription(SBStream &s) {519 LLDB_INSTRUMENT_VA(this, s);520 521 BreakpointName *bp_name = GetBreakpointName();522 if (!bp_name)523 {524 s.Printf("No value");525 return false;526 }527 528 std::lock_guard<std::recursive_mutex> guard(529 m_impl_up->GetTarget()->GetAPIMutex());530 bp_name->GetDescription(s.get(), eDescriptionLevelFull);531 return true;532}533 534void SBBreakpointName::SetCallback(SBBreakpointHitCallback callback,535 void *baton) {536 LLDB_INSTRUMENT_VA(this, callback, baton);537 538 BreakpointName *bp_name = GetBreakpointName();539 if (!bp_name)540 return;541 std::lock_guard<std::recursive_mutex> guard(542 m_impl_up->GetTarget()->GetAPIMutex());543 544 BatonSP baton_sp(new SBBreakpointCallbackBaton(callback, baton));545 bp_name->GetOptions().SetCallback(SBBreakpointCallbackBaton546 ::PrivateBreakpointHitCallback,547 baton_sp,548 false);549 UpdateName(*bp_name);550}551 552void SBBreakpointName::SetScriptCallbackFunction(553 const char *callback_function_name) {554 LLDB_INSTRUMENT_VA(this, callback_function_name);555 SBStructuredData empty_args;556 SetScriptCallbackFunction(callback_function_name, empty_args);557}558 559SBError SBBreakpointName::SetScriptCallbackFunction(560 const char *callback_function_name, 561 SBStructuredData &extra_args) {562 LLDB_INSTRUMENT_VA(this, callback_function_name, extra_args);563 SBError sb_error;564 BreakpointName *bp_name = GetBreakpointName();565 if (!bp_name) {566 sb_error = Status::FromErrorString("unrecognized breakpoint name");567 return sb_error;568 }569 570 std::lock_guard<std::recursive_mutex> guard(571 m_impl_up->GetTarget()->GetAPIMutex());572 573 BreakpointOptions &bp_options = bp_name->GetOptions();574 Status error = m_impl_up->GetTarget()575 ->GetDebugger()576 .GetScriptInterpreter()577 ->SetBreakpointCommandCallbackFunction(578 bp_options, callback_function_name,579 extra_args.m_impl_up->GetObjectSP());580 sb_error.SetError(std::move(error));581 UpdateName(*bp_name);582 return sb_error;583}584 585SBError586SBBreakpointName::SetScriptCallbackBody(const char *callback_body_text) {587 LLDB_INSTRUMENT_VA(this, callback_body_text);588 589 SBError sb_error;590 BreakpointName *bp_name = GetBreakpointName();591 if (!bp_name)592 return sb_error;593 594 std::lock_guard<std::recursive_mutex> guard(595 m_impl_up->GetTarget()->GetAPIMutex());596 597 BreakpointOptions &bp_options = bp_name->GetOptions();598 Status error = m_impl_up->GetTarget()599 ->GetDebugger()600 .GetScriptInterpreter()601 ->SetBreakpointCommandCallback(602 bp_options, callback_body_text, /*is_callback=*/false);603 sb_error.SetError(std::move(error));604 if (!sb_error.Fail())605 UpdateName(*bp_name);606 607 return sb_error;608}609 610bool SBBreakpointName::GetAllowList() const {611 LLDB_INSTRUMENT_VA(this);612 613 BreakpointName *bp_name = GetBreakpointName();614 if (!bp_name)615 return false;616 return bp_name->GetPermissions().GetAllowList();617}618 619void SBBreakpointName::SetAllowList(bool value) {620 LLDB_INSTRUMENT_VA(this, value);621 622 BreakpointName *bp_name = GetBreakpointName();623 if (!bp_name)624 return;625 bp_name->GetPermissions().SetAllowList(value);626}627 628bool SBBreakpointName::GetAllowDelete() {629 LLDB_INSTRUMENT_VA(this);630 631 BreakpointName *bp_name = GetBreakpointName();632 if (!bp_name)633 return false;634 return bp_name->GetPermissions().GetAllowDelete();635}636 637void SBBreakpointName::SetAllowDelete(bool value) {638 LLDB_INSTRUMENT_VA(this, value);639 640 BreakpointName *bp_name = GetBreakpointName();641 if (!bp_name)642 return;643 bp_name->GetPermissions().SetAllowDelete(value);644}645 646bool SBBreakpointName::GetAllowDisable() {647 LLDB_INSTRUMENT_VA(this);648 649 BreakpointName *bp_name = GetBreakpointName();650 if (!bp_name)651 return false;652 return bp_name->GetPermissions().GetAllowDisable();653}654 655void SBBreakpointName::SetAllowDisable(bool value) {656 LLDB_INSTRUMENT_VA(this, value);657 658 BreakpointName *bp_name = GetBreakpointName();659 if (!bp_name)660 return;661 bp_name->GetPermissions().SetAllowDisable(value);662}663 664lldb_private::BreakpointName *SBBreakpointName::GetBreakpointName() const665{666 if (!IsValid())667 return nullptr;668 return m_impl_up->GetBreakpointName();669}670