186 lines · cpp
1//===-- BreakpointResolverScripted.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/Breakpoint/BreakpointResolverScripted.h"10 11 12#include "lldb/Breakpoint/BreakpointLocation.h"13#include "lldb/Core/Debugger.h"14#include "lldb/Core/Module.h"15#include "lldb/Core/Section.h"16#include "lldb/Core/StructuredDataImpl.h"17#include "lldb/Interpreter/CommandInterpreter.h"18#include "lldb/Interpreter/ScriptInterpreter.h"19#include "lldb/Target/Process.h"20#include "lldb/Target/Target.h"21#include "lldb/Utility/Log.h"22#include "lldb/Utility/StreamString.h"23 24using namespace lldb;25using namespace lldb_private;26 27// BreakpointResolverScripted:28BreakpointResolverScripted::BreakpointResolverScripted(29 const BreakpointSP &bkpt, const llvm::StringRef class_name,30 lldb::SearchDepth depth, const StructuredDataImpl &args_data)31 : BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),32 m_class_name(std::string(class_name)), m_depth(depth), m_args(args_data) {33 CreateImplementationIfNeeded(bkpt);34}35 36void BreakpointResolverScripted::CreateImplementationIfNeeded(37 BreakpointSP breakpoint_sp) {38 if (m_interface_sp)39 return;40 41 if (m_class_name.empty())42 return;43 44 if (!breakpoint_sp)45 return;46 47 TargetSP target_sp = breakpoint_sp->GetTargetSP();48 ScriptInterpreter *script_interp = target_sp->GetDebugger()49 .GetScriptInterpreter();50 if (!script_interp)51 return;52 53 if (!m_interface_sp)54 m_interface_sp = script_interp->CreateScriptedBreakpointInterface();55 56 if (!m_interface_sp) {57 m_error = Status::FromErrorStringWithFormat(58 "BreakpointResolverScripted::%s () - ERROR: %s", __FUNCTION__,59 "Script interpreter couldn't create Scripted Breakpoint Interface");60 return;61 }62 63 auto obj_or_err =64 m_interface_sp->CreatePluginObject(m_class_name, breakpoint_sp, m_args);65 if (!obj_or_err) {66 m_interface_sp.reset();67 m_error = Status::FromError(obj_or_err.takeError());68 return;69 }70 71 StructuredData::ObjectSP object_sp = *obj_or_err;72 if (!object_sp || !object_sp->IsValid()) {73 m_error = Status::FromErrorStringWithFormat(74 "ScriptedBreakpoint::%s () - ERROR: %s", __FUNCTION__,75 "Failed to create valid script object");76 }77}78 79void BreakpointResolverScripted::NotifyBreakpointSet() {80 CreateImplementationIfNeeded(GetBreakpoint());81}82 83BreakpointResolverSP BreakpointResolverScripted::CreateFromStructuredData(84 const StructuredData::Dictionary &options_dict, Status &error) {85 llvm::StringRef class_name;86 bool success;87 88 success = options_dict.GetValueForKeyAsString(89 GetKey(OptionNames::PythonClassName), class_name);90 if (!success) {91 error =92 Status::FromErrorString("BRFL::CFSD: Couldn't find class name entry.");93 return nullptr;94 }95 // The Python function will actually provide the search depth, this is a96 // placeholder.97 lldb::SearchDepth depth = lldb::eSearchDepthTarget;98 99 StructuredDataImpl args_data_impl;100 StructuredData::Dictionary *args_dict = nullptr;101 if (options_dict.GetValueForKeyAsDictionary(GetKey(OptionNames::ScriptArgs),102 args_dict))103 args_data_impl.SetObjectSP(args_dict->shared_from_this());104 return std::make_shared<BreakpointResolverScripted>(nullptr, class_name,105 depth, args_data_impl);106}107 108StructuredData::ObjectSP109BreakpointResolverScripted::SerializeToStructuredData() {110 StructuredData::DictionarySP options_dict_sp(111 new StructuredData::Dictionary());112 113 options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),114 m_class_name);115 if (m_args.IsValid())116 options_dict_sp->AddItem(GetKey(OptionNames::ScriptArgs),117 m_args.GetObjectSP());118 119 return WrapOptionsDict(options_dict_sp);120}121 122ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {123 return GetBreakpoint()->GetTarget().GetDebugger().GetScriptInterpreter();124}125 126Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(127 SearchFilter &filter, SymbolContext &context, Address *addr) {128 bool should_continue = true;129 if (!m_interface_sp)130 return Searcher::eCallbackReturnStop;131 132 should_continue = m_interface_sp->ResolverCallback(context);133 if (should_continue)134 return Searcher::eCallbackReturnContinue;135 136 return Searcher::eCallbackReturnStop;137}138 139lldb::SearchDepth140BreakpointResolverScripted::GetDepth() {141 lldb::SearchDepth depth = lldb::eSearchDepthModule;142 if (m_interface_sp)143 depth = m_interface_sp->GetDepth();144 145 return depth;146}147 148void BreakpointResolverScripted::GetDescription(Stream *s) {149 StructuredData::GenericSP generic_sp;150 std::optional<std::string> short_help;151 152 CreateImplementationIfNeeded(GetBreakpoint());153 154 if (m_interface_sp) {155 short_help = m_interface_sp->GetShortHelp();156 }157 if (short_help && !short_help->empty())158 s->PutCString(short_help->c_str());159 else160 s->Printf("python class = %s", m_class_name.c_str());161}162 163std::optional<std::string> BreakpointResolverScripted::GetLocationDescription(164 lldb::BreakpointLocationSP bp_loc_sp, lldb::DescriptionLevel level) {165 CreateImplementationIfNeeded(GetBreakpoint());166 if (m_interface_sp)167 return m_interface_sp->GetLocationDescription(bp_loc_sp, level);168 return {};169}170 171lldb::BreakpointLocationSP172BreakpointResolverScripted::WasHit(lldb::StackFrameSP frame_sp,173 lldb::BreakpointLocationSP bp_loc_sp) {174 if (m_interface_sp)175 return m_interface_sp->WasHit(frame_sp, bp_loc_sp);176 return {};177}178 179void BreakpointResolverScripted::Dump(Stream *s) const {}180 181lldb::BreakpointResolverSP182BreakpointResolverScripted::CopyForBreakpoint(BreakpointSP &breakpoint) {183 return std::make_shared<BreakpointResolverScripted>(breakpoint, m_class_name,184 m_depth, m_args);185}186