Path: blob/main/contrib/llvm-project/lldb/source/API/SBBreakpointLocation.cpp
39587 views
//===-- SBBreakpointLocation.cpp ------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBBreakpointLocation.h"9#include "lldb/API/SBAddress.h"10#include "lldb/API/SBDebugger.h"11#include "lldb/API/SBDefines.h"12#include "lldb/API/SBStream.h"13#include "lldb/API/SBStringList.h"14#include "lldb/API/SBStructuredData.h"15#include "lldb/Utility/Instrumentation.h"1617#include "lldb/Breakpoint/Breakpoint.h"18#include "lldb/Breakpoint/BreakpointLocation.h"19#include "lldb/Core/Debugger.h"20#include "lldb/Core/StructuredDataImpl.h"21#include "lldb/Interpreter/CommandInterpreter.h"22#include "lldb/Interpreter/ScriptInterpreter.h"23#include "lldb/Target/Target.h"24#include "lldb/Target/ThreadSpec.h"25#include "lldb/Utility/Stream.h"26#include "lldb/lldb-defines.h"27#include "lldb/lldb-types.h"2829#include "SBBreakpointOptionCommon.h"3031using namespace lldb;32using namespace lldb_private;3334SBBreakpointLocation::SBBreakpointLocation() { LLDB_INSTRUMENT_VA(this); }3536SBBreakpointLocation::SBBreakpointLocation(37const lldb::BreakpointLocationSP &break_loc_sp)38: m_opaque_wp(break_loc_sp) {39LLDB_INSTRUMENT_VA(this, break_loc_sp);40}4142SBBreakpointLocation::SBBreakpointLocation(const SBBreakpointLocation &rhs)43: m_opaque_wp(rhs.m_opaque_wp) {44LLDB_INSTRUMENT_VA(this, rhs);45}4647const SBBreakpointLocation &SBBreakpointLocation::48operator=(const SBBreakpointLocation &rhs) {49LLDB_INSTRUMENT_VA(this, rhs);5051m_opaque_wp = rhs.m_opaque_wp;52return *this;53}5455SBBreakpointLocation::~SBBreakpointLocation() = default;5657BreakpointLocationSP SBBreakpointLocation::GetSP() const {58return m_opaque_wp.lock();59}6061bool SBBreakpointLocation::IsValid() const {62LLDB_INSTRUMENT_VA(this);63return this->operator bool();64}65SBBreakpointLocation::operator bool() const {66LLDB_INSTRUMENT_VA(this);6768return bool(GetSP());69}7071SBAddress SBBreakpointLocation::GetAddress() {72LLDB_INSTRUMENT_VA(this);7374BreakpointLocationSP loc_sp = GetSP();75if (loc_sp) {76return SBAddress(loc_sp->GetAddress());77}7879return SBAddress();80}8182addr_t SBBreakpointLocation::GetLoadAddress() {83LLDB_INSTRUMENT_VA(this);8485addr_t ret_addr = LLDB_INVALID_ADDRESS;86BreakpointLocationSP loc_sp = GetSP();8788if (loc_sp) {89std::lock_guard<std::recursive_mutex> guard(90loc_sp->GetTarget().GetAPIMutex());91ret_addr = loc_sp->GetLoadAddress();92}9394return ret_addr;95}9697void SBBreakpointLocation::SetEnabled(bool enabled) {98LLDB_INSTRUMENT_VA(this, enabled);99100BreakpointLocationSP loc_sp = GetSP();101if (loc_sp) {102std::lock_guard<std::recursive_mutex> guard(103loc_sp->GetTarget().GetAPIMutex());104loc_sp->SetEnabled(enabled);105}106}107108bool SBBreakpointLocation::IsEnabled() {109LLDB_INSTRUMENT_VA(this);110111BreakpointLocationSP loc_sp = GetSP();112if (loc_sp) {113std::lock_guard<std::recursive_mutex> guard(114loc_sp->GetTarget().GetAPIMutex());115return loc_sp->IsEnabled();116} else117return false;118}119120uint32_t SBBreakpointLocation::GetHitCount() {121LLDB_INSTRUMENT_VA(this);122123BreakpointLocationSP loc_sp = GetSP();124if (loc_sp) {125std::lock_guard<std::recursive_mutex> guard(126loc_sp->GetTarget().GetAPIMutex());127return loc_sp->GetHitCount();128} else129return 0;130}131132uint32_t SBBreakpointLocation::GetIgnoreCount() {133LLDB_INSTRUMENT_VA(this);134135BreakpointLocationSP loc_sp = GetSP();136if (loc_sp) {137std::lock_guard<std::recursive_mutex> guard(138loc_sp->GetTarget().GetAPIMutex());139return loc_sp->GetIgnoreCount();140} else141return 0;142}143144void SBBreakpointLocation::SetIgnoreCount(uint32_t n) {145LLDB_INSTRUMENT_VA(this, n);146147BreakpointLocationSP loc_sp = GetSP();148if (loc_sp) {149std::lock_guard<std::recursive_mutex> guard(150loc_sp->GetTarget().GetAPIMutex());151loc_sp->SetIgnoreCount(n);152}153}154155void SBBreakpointLocation::SetCondition(const char *condition) {156LLDB_INSTRUMENT_VA(this, condition);157158BreakpointLocationSP loc_sp = GetSP();159if (loc_sp) {160std::lock_guard<std::recursive_mutex> guard(161loc_sp->GetTarget().GetAPIMutex());162loc_sp->SetCondition(condition);163}164}165166const char *SBBreakpointLocation::GetCondition() {167LLDB_INSTRUMENT_VA(this);168169BreakpointLocationSP loc_sp = GetSP();170if (!loc_sp)171return nullptr;172173std::lock_guard<std::recursive_mutex> guard(174loc_sp->GetTarget().GetAPIMutex());175return ConstString(loc_sp->GetConditionText()).GetCString();176}177178void SBBreakpointLocation::SetAutoContinue(bool auto_continue) {179LLDB_INSTRUMENT_VA(this, auto_continue);180181BreakpointLocationSP loc_sp = GetSP();182if (loc_sp) {183std::lock_guard<std::recursive_mutex> guard(184loc_sp->GetTarget().GetAPIMutex());185loc_sp->SetAutoContinue(auto_continue);186}187}188189bool SBBreakpointLocation::GetAutoContinue() {190LLDB_INSTRUMENT_VA(this);191192BreakpointLocationSP loc_sp = GetSP();193if (loc_sp) {194std::lock_guard<std::recursive_mutex> guard(195loc_sp->GetTarget().GetAPIMutex());196return loc_sp->IsAutoContinue();197}198return false;199}200201void SBBreakpointLocation::SetCallback(SBBreakpointHitCallback callback,202void *baton) {203LLDB_INSTRUMENT_VA(this, callback, baton);204205BreakpointLocationSP loc_sp = GetSP();206207if (loc_sp) {208std::lock_guard<std::recursive_mutex> guard(209loc_sp->GetTarget().GetAPIMutex());210BatonSP baton_sp(new SBBreakpointCallbackBaton(callback, baton));211loc_sp->SetCallback(SBBreakpointCallbackBaton::PrivateBreakpointHitCallback,212baton_sp, false);213}214}215216void SBBreakpointLocation::SetScriptCallbackFunction(217const char *callback_function_name) {218LLDB_INSTRUMENT_VA(this, callback_function_name);219}220221SBError SBBreakpointLocation::SetScriptCallbackFunction(222const char *callback_function_name,223SBStructuredData &extra_args) {224LLDB_INSTRUMENT_VA(this, callback_function_name, extra_args);225SBError sb_error;226BreakpointLocationSP loc_sp = GetSP();227228if (loc_sp) {229Status error;230std::lock_guard<std::recursive_mutex> guard(231loc_sp->GetTarget().GetAPIMutex());232BreakpointOptions &bp_options = loc_sp->GetLocationOptions();233error = loc_sp->GetBreakpoint()234.GetTarget()235.GetDebugger()236.GetScriptInterpreter()237->SetBreakpointCommandCallbackFunction(bp_options,238callback_function_name,239extra_args.m_impl_up240->GetObjectSP());241sb_error.SetError(error);242} else243sb_error.SetErrorString("invalid breakpoint");244245return sb_error;246}247248SBError249SBBreakpointLocation::SetScriptCallbackBody(const char *callback_body_text) {250LLDB_INSTRUMENT_VA(this, callback_body_text);251252BreakpointLocationSP loc_sp = GetSP();253254SBError sb_error;255if (loc_sp) {256std::lock_guard<std::recursive_mutex> guard(257loc_sp->GetTarget().GetAPIMutex());258BreakpointOptions &bp_options = loc_sp->GetLocationOptions();259Status error =260loc_sp->GetBreakpoint()261.GetTarget()262.GetDebugger()263.GetScriptInterpreter()264->SetBreakpointCommandCallback(bp_options, callback_body_text,265/*is_callback=*/false);266sb_error.SetError(error);267} else268sb_error.SetErrorString("invalid breakpoint");269270return sb_error;271}272273void SBBreakpointLocation::SetCommandLineCommands(SBStringList &commands) {274LLDB_INSTRUMENT_VA(this, commands);275276BreakpointLocationSP loc_sp = GetSP();277if (!loc_sp)278return;279if (commands.GetSize() == 0)280return;281282std::lock_guard<std::recursive_mutex> guard(283loc_sp->GetTarget().GetAPIMutex());284std::unique_ptr<BreakpointOptions::CommandData> cmd_data_up(285new BreakpointOptions::CommandData(*commands, eScriptLanguageNone));286287loc_sp->GetLocationOptions().SetCommandDataCallback(cmd_data_up);288}289290bool SBBreakpointLocation::GetCommandLineCommands(SBStringList &commands) {291LLDB_INSTRUMENT_VA(this, commands);292293BreakpointLocationSP loc_sp = GetSP();294if (!loc_sp)295return false;296StringList command_list;297bool has_commands =298loc_sp->GetLocationOptions().GetCommandLineCallbacks(command_list);299if (has_commands)300commands.AppendList(command_list);301return has_commands;302}303304void SBBreakpointLocation::SetThreadID(tid_t thread_id) {305LLDB_INSTRUMENT_VA(this, thread_id);306307BreakpointLocationSP loc_sp = GetSP();308if (loc_sp) {309std::lock_guard<std::recursive_mutex> guard(310loc_sp->GetTarget().GetAPIMutex());311loc_sp->SetThreadID(thread_id);312}313}314315tid_t SBBreakpointLocation::GetThreadID() {316LLDB_INSTRUMENT_VA(this);317318tid_t tid = LLDB_INVALID_THREAD_ID;319BreakpointLocationSP loc_sp = GetSP();320if (loc_sp) {321std::lock_guard<std::recursive_mutex> guard(322loc_sp->GetTarget().GetAPIMutex());323return loc_sp->GetThreadID();324}325return tid;326}327328void SBBreakpointLocation::SetThreadIndex(uint32_t index) {329LLDB_INSTRUMENT_VA(this, index);330331BreakpointLocationSP loc_sp = GetSP();332if (loc_sp) {333std::lock_guard<std::recursive_mutex> guard(334loc_sp->GetTarget().GetAPIMutex());335loc_sp->SetThreadIndex(index);336}337}338339uint32_t SBBreakpointLocation::GetThreadIndex() const {340LLDB_INSTRUMENT_VA(this);341342uint32_t thread_idx = UINT32_MAX;343BreakpointLocationSP loc_sp = GetSP();344if (loc_sp) {345std::lock_guard<std::recursive_mutex> guard(346loc_sp->GetTarget().GetAPIMutex());347return loc_sp->GetThreadIndex();348}349return thread_idx;350}351352void SBBreakpointLocation::SetThreadName(const char *thread_name) {353LLDB_INSTRUMENT_VA(this, thread_name);354355BreakpointLocationSP loc_sp = GetSP();356if (loc_sp) {357std::lock_guard<std::recursive_mutex> guard(358loc_sp->GetTarget().GetAPIMutex());359loc_sp->SetThreadName(thread_name);360}361}362363const char *SBBreakpointLocation::GetThreadName() const {364LLDB_INSTRUMENT_VA(this);365366BreakpointLocationSP loc_sp = GetSP();367if (!loc_sp)368return nullptr;369370std::lock_guard<std::recursive_mutex> guard(371loc_sp->GetTarget().GetAPIMutex());372return ConstString(loc_sp->GetThreadName()).GetCString();373}374375void SBBreakpointLocation::SetQueueName(const char *queue_name) {376LLDB_INSTRUMENT_VA(this, queue_name);377378BreakpointLocationSP loc_sp = GetSP();379if (loc_sp) {380std::lock_guard<std::recursive_mutex> guard(381loc_sp->GetTarget().GetAPIMutex());382loc_sp->SetQueueName(queue_name);383}384}385386const char *SBBreakpointLocation::GetQueueName() const {387LLDB_INSTRUMENT_VA(this);388389BreakpointLocationSP loc_sp = GetSP();390if (!loc_sp)391return nullptr;392393std::lock_guard<std::recursive_mutex> guard(394loc_sp->GetTarget().GetAPIMutex());395return ConstString(loc_sp->GetQueueName()).GetCString();396}397398bool SBBreakpointLocation::IsResolved() {399LLDB_INSTRUMENT_VA(this);400401BreakpointLocationSP loc_sp = GetSP();402if (loc_sp) {403std::lock_guard<std::recursive_mutex> guard(404loc_sp->GetTarget().GetAPIMutex());405return loc_sp->IsResolved();406}407return false;408}409410void SBBreakpointLocation::SetLocation(411const lldb::BreakpointLocationSP &break_loc_sp) {412// Uninstall the callbacks?413m_opaque_wp = break_loc_sp;414}415416bool SBBreakpointLocation::GetDescription(SBStream &description,417DescriptionLevel level) {418LLDB_INSTRUMENT_VA(this, description, level);419420Stream &strm = description.ref();421BreakpointLocationSP loc_sp = GetSP();422423if (loc_sp) {424std::lock_guard<std::recursive_mutex> guard(425loc_sp->GetTarget().GetAPIMutex());426loc_sp->GetDescription(&strm, level);427strm.EOL();428} else429strm.PutCString("No value");430431return true;432}433434break_id_t SBBreakpointLocation::GetID() {435LLDB_INSTRUMENT_VA(this);436437BreakpointLocationSP loc_sp = GetSP();438if (loc_sp) {439std::lock_guard<std::recursive_mutex> guard(440loc_sp->GetTarget().GetAPIMutex());441return loc_sp->GetID();442} else443return LLDB_INVALID_BREAK_ID;444}445446SBBreakpoint SBBreakpointLocation::GetBreakpoint() {447LLDB_INSTRUMENT_VA(this);448449BreakpointLocationSP loc_sp = GetSP();450451SBBreakpoint sb_bp;452if (loc_sp) {453std::lock_guard<std::recursive_mutex> guard(454loc_sp->GetTarget().GetAPIMutex());455sb_bp = loc_sp->GetBreakpoint().shared_from_this();456}457458return sb_bp;459}460461462