15ffd83dbSDimitry Andric //===-- BreakpointResolverScripted.cpp ------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverScripted.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
130b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
140b57cec5SDimitry Andric #include "lldb/Core/Module.h"
150b57cec5SDimitry Andric #include "lldb/Core/Section.h"
160b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
170b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
180b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
190b57cec5SDimitry Andric #include "lldb/Target/Process.h"
200b57cec5SDimitry Andric #include "lldb/Target/Target.h"
210b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
220b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace lldb;
250b57cec5SDimitry Andric using namespace lldb_private;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric // BreakpointResolverScripted:
BreakpointResolverScripted(const BreakpointSP & bkpt,const llvm::StringRef class_name,lldb::SearchDepth depth,const StructuredDataImpl & args_data)280b57cec5SDimitry Andric BreakpointResolverScripted::BreakpointResolverScripted(
295ffd83dbSDimitry Andric     const BreakpointSP &bkpt, const llvm::StringRef class_name,
300eae32dcSDimitry Andric     lldb::SearchDepth depth, const StructuredDataImpl &args_data)
310b57cec5SDimitry Andric     : BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),
320eae32dcSDimitry Andric       m_class_name(std::string(class_name)), m_depth(depth), m_args(args_data) {
335ffd83dbSDimitry Andric   CreateImplementationIfNeeded(bkpt);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
CreateImplementationIfNeeded(BreakpointSP breakpoint_sp)365ffd83dbSDimitry Andric void BreakpointResolverScripted::CreateImplementationIfNeeded(
375ffd83dbSDimitry Andric     BreakpointSP breakpoint_sp) {
380b57cec5SDimitry Andric   if (m_implementation_sp)
390b57cec5SDimitry Andric     return;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   if (m_class_name.empty())
420b57cec5SDimitry Andric     return;
430b57cec5SDimitry Andric 
445ffd83dbSDimitry Andric   if (!breakpoint_sp)
455ffd83dbSDimitry Andric     return;
465ffd83dbSDimitry Andric 
475ffd83dbSDimitry Andric   TargetSP target_sp = breakpoint_sp->GetTargetSP();
480b57cec5SDimitry Andric   ScriptInterpreter *script_interp = target_sp->GetDebugger()
490b57cec5SDimitry Andric                                               .GetScriptInterpreter();
500b57cec5SDimitry Andric   if (!script_interp)
510b57cec5SDimitry Andric     return;
525ffd83dbSDimitry Andric 
530b57cec5SDimitry Andric   m_implementation_sp = script_interp->CreateScriptedBreakpointResolver(
540eae32dcSDimitry Andric       m_class_name.c_str(), m_args, breakpoint_sp);
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
NotifyBreakpointSet()570b57cec5SDimitry Andric void BreakpointResolverScripted::NotifyBreakpointSet() {
585ffd83dbSDimitry Andric   CreateImplementationIfNeeded(GetBreakpoint());
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
CreateFromStructuredData(const StructuredData::Dictionary & options_dict,Status & error)615f757f3fSDimitry Andric BreakpointResolverSP BreakpointResolverScripted::CreateFromStructuredData(
625f757f3fSDimitry Andric     const StructuredData::Dictionary &options_dict, Status &error) {
630b57cec5SDimitry Andric   llvm::StringRef class_name;
640b57cec5SDimitry Andric   bool success;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   success = options_dict.GetValueForKeyAsString(
670b57cec5SDimitry Andric       GetKey(OptionNames::PythonClassName), class_name);
680b57cec5SDimitry Andric   if (!success) {
690b57cec5SDimitry Andric     error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
700b57cec5SDimitry Andric     return nullptr;
710b57cec5SDimitry Andric   }
729dba64beSDimitry Andric   // The Python function will actually provide the search depth, this is a
739dba64beSDimitry Andric   // placeholder.
749dba64beSDimitry Andric   lldb::SearchDepth depth = lldb::eSearchDepthTarget;
750b57cec5SDimitry Andric 
760eae32dcSDimitry Andric   StructuredDataImpl args_data_impl;
779dba64beSDimitry Andric   StructuredData::Dictionary *args_dict = nullptr;
780eae32dcSDimitry Andric   if (options_dict.GetValueForKeyAsDictionary(GetKey(OptionNames::ScriptArgs),
790eae32dcSDimitry Andric                                               args_dict))
800eae32dcSDimitry Andric     args_data_impl.SetObjectSP(args_dict->shared_from_this());
815f757f3fSDimitry Andric   return std::make_shared<BreakpointResolverScripted>(nullptr, class_name,
825f757f3fSDimitry Andric                                                       depth, args_data_impl);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()860b57cec5SDimitry Andric BreakpointResolverScripted::SerializeToStructuredData() {
870b57cec5SDimitry Andric   StructuredData::DictionarySP options_dict_sp(
880b57cec5SDimitry Andric       new StructuredData::Dictionary());
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),
910b57cec5SDimitry Andric                                    m_class_name);
920eae32dcSDimitry Andric   if (m_args.IsValid())
939dba64beSDimitry Andric     options_dict_sp->AddItem(GetKey(OptionNames::ScriptArgs),
940eae32dcSDimitry Andric                              m_args.GetObjectSP());
959dba64beSDimitry Andric 
960b57cec5SDimitry Andric   return WrapOptionsDict(options_dict_sp);
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric 
GetScriptInterpreter()990b57cec5SDimitry Andric ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {
1005ffd83dbSDimitry Andric   return GetBreakpoint()->GetTarget().GetDebugger().GetScriptInterpreter();
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)1039dba64beSDimitry Andric Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(
1049dba64beSDimitry Andric     SearchFilter &filter, SymbolContext &context, Address *addr) {
1050b57cec5SDimitry Andric   bool should_continue = true;
1060b57cec5SDimitry Andric   if (!m_implementation_sp)
1070b57cec5SDimitry Andric     return Searcher::eCallbackReturnStop;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   ScriptInterpreter *interp = GetScriptInterpreter();
1100b57cec5SDimitry Andric   should_continue = interp->ScriptedBreakpointResolverSearchCallback(
1110b57cec5SDimitry Andric       m_implementation_sp,
1120b57cec5SDimitry Andric       &context);
1130b57cec5SDimitry Andric   if (should_continue)
1140b57cec5SDimitry Andric     return Searcher::eCallbackReturnContinue;
1155ffd83dbSDimitry Andric 
1160b57cec5SDimitry Andric   return Searcher::eCallbackReturnStop;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric lldb::SearchDepth
GetDepth()1200b57cec5SDimitry Andric BreakpointResolverScripted::GetDepth() {
1210b57cec5SDimitry Andric   lldb::SearchDepth depth = lldb::eSearchDepthModule;
1220b57cec5SDimitry Andric   if (m_implementation_sp) {
1230b57cec5SDimitry Andric     ScriptInterpreter *interp = GetScriptInterpreter();
1240b57cec5SDimitry Andric     depth = interp->ScriptedBreakpointResolverSearchDepth(
1250b57cec5SDimitry Andric         m_implementation_sp);
1260b57cec5SDimitry Andric   }
1270b57cec5SDimitry Andric   return depth;
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
GetDescription(Stream * s)1300b57cec5SDimitry Andric void BreakpointResolverScripted::GetDescription(Stream *s) {
1310b57cec5SDimitry Andric   StructuredData::GenericSP generic_sp;
1320b57cec5SDimitry Andric   std::string short_help;
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   if (m_implementation_sp) {
1350b57cec5SDimitry Andric     ScriptInterpreter *interp = GetScriptInterpreter();
1360b57cec5SDimitry Andric     interp->GetShortHelpForCommandObject(m_implementation_sp,
1370b57cec5SDimitry Andric                                          short_help);
1380b57cec5SDimitry Andric   }
1390b57cec5SDimitry Andric   if (!short_help.empty())
1400b57cec5SDimitry Andric     s->PutCString(short_help.c_str());
1410b57cec5SDimitry Andric   else
1420b57cec5SDimitry Andric     s->Printf("python class = %s", m_class_name.c_str());
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
Dump(Stream * s) const1450b57cec5SDimitry Andric void BreakpointResolverScripted::Dump(Stream *s) const {}
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)1485ffd83dbSDimitry Andric BreakpointResolverScripted::CopyForBreakpoint(BreakpointSP &breakpoint) {
1490eae32dcSDimitry Andric   return std::make_shared<BreakpointResolverScripted>(breakpoint, m_class_name,
1500eae32dcSDimitry Andric                                                       m_depth, m_args);
1510b57cec5SDimitry Andric }
152