Path: blob/main/contrib/llvm-project/lldb/source/Breakpoint/BreakpointResolverScripted.cpp
39587 views
//===-- BreakpointResolverScripted.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/Breakpoint/BreakpointResolverScripted.h"91011#include "lldb/Breakpoint/BreakpointLocation.h"12#include "lldb/Core/Debugger.h"13#include "lldb/Core/Module.h"14#include "lldb/Core/Section.h"15#include "lldb/Core/StructuredDataImpl.h"16#include "lldb/Interpreter/CommandInterpreter.h"17#include "lldb/Interpreter/ScriptInterpreter.h"18#include "lldb/Target/Process.h"19#include "lldb/Target/Target.h"20#include "lldb/Utility/Log.h"21#include "lldb/Utility/StreamString.h"2223using namespace lldb;24using namespace lldb_private;2526// BreakpointResolverScripted:27BreakpointResolverScripted::BreakpointResolverScripted(28const BreakpointSP &bkpt, const llvm::StringRef class_name,29lldb::SearchDepth depth, const StructuredDataImpl &args_data)30: BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),31m_class_name(std::string(class_name)), m_depth(depth), m_args(args_data) {32CreateImplementationIfNeeded(bkpt);33}3435void BreakpointResolverScripted::CreateImplementationIfNeeded(36BreakpointSP breakpoint_sp) {37if (m_implementation_sp)38return;3940if (m_class_name.empty())41return;4243if (!breakpoint_sp)44return;4546TargetSP target_sp = breakpoint_sp->GetTargetSP();47ScriptInterpreter *script_interp = target_sp->GetDebugger()48.GetScriptInterpreter();49if (!script_interp)50return;5152m_implementation_sp = script_interp->CreateScriptedBreakpointResolver(53m_class_name.c_str(), m_args, breakpoint_sp);54}5556void BreakpointResolverScripted::NotifyBreakpointSet() {57CreateImplementationIfNeeded(GetBreakpoint());58}5960BreakpointResolverSP BreakpointResolverScripted::CreateFromStructuredData(61const StructuredData::Dictionary &options_dict, Status &error) {62llvm::StringRef class_name;63bool success;6465success = options_dict.GetValueForKeyAsString(66GetKey(OptionNames::PythonClassName), class_name);67if (!success) {68error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");69return nullptr;70}71// The Python function will actually provide the search depth, this is a72// placeholder.73lldb::SearchDepth depth = lldb::eSearchDepthTarget;7475StructuredDataImpl args_data_impl;76StructuredData::Dictionary *args_dict = nullptr;77if (options_dict.GetValueForKeyAsDictionary(GetKey(OptionNames::ScriptArgs),78args_dict))79args_data_impl.SetObjectSP(args_dict->shared_from_this());80return std::make_shared<BreakpointResolverScripted>(nullptr, class_name,81depth, args_data_impl);82}8384StructuredData::ObjectSP85BreakpointResolverScripted::SerializeToStructuredData() {86StructuredData::DictionarySP options_dict_sp(87new StructuredData::Dictionary());8889options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),90m_class_name);91if (m_args.IsValid())92options_dict_sp->AddItem(GetKey(OptionNames::ScriptArgs),93m_args.GetObjectSP());9495return WrapOptionsDict(options_dict_sp);96}9798ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {99return GetBreakpoint()->GetTarget().GetDebugger().GetScriptInterpreter();100}101102Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(103SearchFilter &filter, SymbolContext &context, Address *addr) {104bool should_continue = true;105if (!m_implementation_sp)106return Searcher::eCallbackReturnStop;107108ScriptInterpreter *interp = GetScriptInterpreter();109should_continue = interp->ScriptedBreakpointResolverSearchCallback(110m_implementation_sp,111&context);112if (should_continue)113return Searcher::eCallbackReturnContinue;114115return Searcher::eCallbackReturnStop;116}117118lldb::SearchDepth119BreakpointResolverScripted::GetDepth() {120lldb::SearchDepth depth = lldb::eSearchDepthModule;121if (m_implementation_sp) {122ScriptInterpreter *interp = GetScriptInterpreter();123depth = interp->ScriptedBreakpointResolverSearchDepth(124m_implementation_sp);125}126return depth;127}128129void BreakpointResolverScripted::GetDescription(Stream *s) {130StructuredData::GenericSP generic_sp;131std::string short_help;132133if (m_implementation_sp) {134ScriptInterpreter *interp = GetScriptInterpreter();135interp->GetShortHelpForCommandObject(m_implementation_sp,136short_help);137}138if (!short_help.empty())139s->PutCString(short_help.c_str());140else141s->Printf("python class = %s", m_class_name.c_str());142}143144void BreakpointResolverScripted::Dump(Stream *s) const {}145146lldb::BreakpointResolverSP147BreakpointResolverScripted::CopyForBreakpoint(BreakpointSP &breakpoint) {148return std::make_shared<BreakpointResolverScripted>(breakpoint, m_class_name,149m_depth, m_args);150}151152153