1cfca06d7SDimitry Andric //===-- BreakpointResolverScripted.cpp ------------------------------------===//
294994d37SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
694994d37SDimitry Andric //
794994d37SDimitry Andric //===----------------------------------------------------------------------===//
894994d37SDimitry Andric
994994d37SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverScripted.h"
1094994d37SDimitry Andric
1194994d37SDimitry Andric
1294994d37SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
1394994d37SDimitry Andric #include "lldb/Core/Debugger.h"
1494994d37SDimitry Andric #include "lldb/Core/Module.h"
1594994d37SDimitry Andric #include "lldb/Core/Section.h"
1694994d37SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
1794994d37SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
1894994d37SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
1994994d37SDimitry Andric #include "lldb/Target/Process.h"
2094994d37SDimitry Andric #include "lldb/Target/Target.h"
2194994d37SDimitry Andric #include "lldb/Utility/Log.h"
2294994d37SDimitry Andric #include "lldb/Utility/StreamString.h"
2394994d37SDimitry Andric
2494994d37SDimitry Andric using namespace lldb;
2594994d37SDimitry Andric using namespace lldb_private;
2694994d37SDimitry Andric
2794994d37SDimitry Andric // BreakpointResolverScripted:
BreakpointResolverScripted(const BreakpointSP & bkpt,const llvm::StringRef class_name,lldb::SearchDepth depth,const StructuredDataImpl & args_data)2894994d37SDimitry Andric BreakpointResolverScripted::BreakpointResolverScripted(
29cfca06d7SDimitry Andric const BreakpointSP &bkpt, const llvm::StringRef class_name,
3077fc4c14SDimitry Andric lldb::SearchDepth depth, const StructuredDataImpl &args_data)
3194994d37SDimitry Andric : BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),
3277fc4c14SDimitry Andric m_class_name(std::string(class_name)), m_depth(depth), m_args(args_data) {
33cfca06d7SDimitry Andric CreateImplementationIfNeeded(bkpt);
3494994d37SDimitry Andric }
3594994d37SDimitry Andric
CreateImplementationIfNeeded(BreakpointSP breakpoint_sp)36cfca06d7SDimitry Andric void BreakpointResolverScripted::CreateImplementationIfNeeded(
37cfca06d7SDimitry Andric BreakpointSP breakpoint_sp) {
3894994d37SDimitry Andric if (m_implementation_sp)
3994994d37SDimitry Andric return;
4094994d37SDimitry Andric
4194994d37SDimitry Andric if (m_class_name.empty())
4294994d37SDimitry Andric return;
4394994d37SDimitry Andric
44cfca06d7SDimitry Andric if (!breakpoint_sp)
45cfca06d7SDimitry Andric return;
46cfca06d7SDimitry Andric
47cfca06d7SDimitry Andric TargetSP target_sp = breakpoint_sp->GetTargetSP();
4894994d37SDimitry Andric ScriptInterpreter *script_interp = target_sp->GetDebugger()
4994994d37SDimitry Andric .GetScriptInterpreter();
5094994d37SDimitry Andric if (!script_interp)
5194994d37SDimitry Andric return;
52cfca06d7SDimitry Andric
5394994d37SDimitry Andric m_implementation_sp = script_interp->CreateScriptedBreakpointResolver(
5477fc4c14SDimitry Andric m_class_name.c_str(), m_args, breakpoint_sp);
5594994d37SDimitry Andric }
5694994d37SDimitry Andric
NotifyBreakpointSet()5794994d37SDimitry Andric void BreakpointResolverScripted::NotifyBreakpointSet() {
58cfca06d7SDimitry Andric CreateImplementationIfNeeded(GetBreakpoint());
5994994d37SDimitry Andric }
6094994d37SDimitry Andric
CreateFromStructuredData(const StructuredData::Dictionary & options_dict,Status & error)61b1c73532SDimitry Andric BreakpointResolverSP BreakpointResolverScripted::CreateFromStructuredData(
62312c0ed1SDimitry Andric const StructuredData::Dictionary &options_dict, Status &error) {
6394994d37SDimitry Andric llvm::StringRef class_name;
6494994d37SDimitry Andric bool success;
6594994d37SDimitry Andric
6694994d37SDimitry Andric success = options_dict.GetValueForKeyAsString(
6794994d37SDimitry Andric GetKey(OptionNames::PythonClassName), class_name);
6894994d37SDimitry Andric if (!success) {
6994994d37SDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
7094994d37SDimitry Andric return nullptr;
7194994d37SDimitry Andric }
72ead24645SDimitry Andric // The Python function will actually provide the search depth, this is a
73ead24645SDimitry Andric // placeholder.
74ead24645SDimitry Andric lldb::SearchDepth depth = lldb::eSearchDepthTarget;
7594994d37SDimitry Andric
7677fc4c14SDimitry Andric StructuredDataImpl args_data_impl;
77ead24645SDimitry Andric StructuredData::Dictionary *args_dict = nullptr;
7877fc4c14SDimitry Andric if (options_dict.GetValueForKeyAsDictionary(GetKey(OptionNames::ScriptArgs),
7977fc4c14SDimitry Andric args_dict))
8077fc4c14SDimitry Andric args_data_impl.SetObjectSP(args_dict->shared_from_this());
81312c0ed1SDimitry Andric return std::make_shared<BreakpointResolverScripted>(nullptr, class_name,
82312c0ed1SDimitry Andric depth, args_data_impl);
8394994d37SDimitry Andric }
8494994d37SDimitry Andric
8594994d37SDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()8694994d37SDimitry Andric BreakpointResolverScripted::SerializeToStructuredData() {
8794994d37SDimitry Andric StructuredData::DictionarySP options_dict_sp(
8894994d37SDimitry Andric new StructuredData::Dictionary());
8994994d37SDimitry Andric
9094994d37SDimitry Andric options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),
9194994d37SDimitry Andric m_class_name);
9277fc4c14SDimitry Andric if (m_args.IsValid())
93ead24645SDimitry Andric options_dict_sp->AddItem(GetKey(OptionNames::ScriptArgs),
9477fc4c14SDimitry Andric m_args.GetObjectSP());
95ead24645SDimitry Andric
9694994d37SDimitry Andric return WrapOptionsDict(options_dict_sp);
9794994d37SDimitry Andric }
9894994d37SDimitry Andric
GetScriptInterpreter()9994994d37SDimitry Andric ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {
100cfca06d7SDimitry Andric return GetBreakpoint()->GetTarget().GetDebugger().GetScriptInterpreter();
10194994d37SDimitry Andric }
10294994d37SDimitry Andric
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)103ead24645SDimitry Andric Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(
104ead24645SDimitry Andric SearchFilter &filter, SymbolContext &context, Address *addr) {
10594994d37SDimitry Andric bool should_continue = true;
10694994d37SDimitry Andric if (!m_implementation_sp)
10794994d37SDimitry Andric return Searcher::eCallbackReturnStop;
10894994d37SDimitry Andric
10994994d37SDimitry Andric ScriptInterpreter *interp = GetScriptInterpreter();
11094994d37SDimitry Andric should_continue = interp->ScriptedBreakpointResolverSearchCallback(
11194994d37SDimitry Andric m_implementation_sp,
11294994d37SDimitry Andric &context);
11394994d37SDimitry Andric if (should_continue)
11494994d37SDimitry Andric return Searcher::eCallbackReturnContinue;
115cfca06d7SDimitry Andric
11694994d37SDimitry Andric return Searcher::eCallbackReturnStop;
11794994d37SDimitry Andric }
11894994d37SDimitry Andric
11994994d37SDimitry Andric lldb::SearchDepth
GetDepth()12094994d37SDimitry Andric BreakpointResolverScripted::GetDepth() {
12194994d37SDimitry Andric lldb::SearchDepth depth = lldb::eSearchDepthModule;
12294994d37SDimitry Andric if (m_implementation_sp) {
12394994d37SDimitry Andric ScriptInterpreter *interp = GetScriptInterpreter();
12494994d37SDimitry Andric depth = interp->ScriptedBreakpointResolverSearchDepth(
12594994d37SDimitry Andric m_implementation_sp);
12694994d37SDimitry Andric }
12794994d37SDimitry Andric return depth;
12894994d37SDimitry Andric }
12994994d37SDimitry Andric
GetDescription(Stream * s)13094994d37SDimitry Andric void BreakpointResolverScripted::GetDescription(Stream *s) {
13194994d37SDimitry Andric StructuredData::GenericSP generic_sp;
13294994d37SDimitry Andric std::string short_help;
13394994d37SDimitry Andric
13494994d37SDimitry Andric if (m_implementation_sp) {
13594994d37SDimitry Andric ScriptInterpreter *interp = GetScriptInterpreter();
13694994d37SDimitry Andric interp->GetShortHelpForCommandObject(m_implementation_sp,
13794994d37SDimitry Andric short_help);
13894994d37SDimitry Andric }
13994994d37SDimitry Andric if (!short_help.empty())
14094994d37SDimitry Andric s->PutCString(short_help.c_str());
14194994d37SDimitry Andric else
14294994d37SDimitry Andric s->Printf("python class = %s", m_class_name.c_str());
14394994d37SDimitry Andric }
14494994d37SDimitry Andric
Dump(Stream * s) const14594994d37SDimitry Andric void BreakpointResolverScripted::Dump(Stream *s) const {}
14694994d37SDimitry Andric
14794994d37SDimitry Andric lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)148cfca06d7SDimitry Andric BreakpointResolverScripted::CopyForBreakpoint(BreakpointSP &breakpoint) {
14977fc4c14SDimitry Andric return std::make_shared<BreakpointResolverScripted>(breakpoint, m_class_name,
15077fc4c14SDimitry Andric m_depth, m_args);
15194994d37SDimitry Andric }
152