1e19adf54SJim Ingham //===-- BreakpointResolverScripted.cpp ---------------------------*- C++ -*-===//
2e19adf54SJim Ingham //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e19adf54SJim Ingham //
7e19adf54SJim Ingham //===----------------------------------------------------------------------===//
8e19adf54SJim Ingham 
9e19adf54SJim Ingham #include "lldb/Breakpoint/BreakpointResolverScripted.h"
10e19adf54SJim Ingham 
11e19adf54SJim Ingham 
12e19adf54SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
13e19adf54SJim Ingham #include "lldb/Core/Debugger.h"
14e19adf54SJim Ingham #include "lldb/Core/Module.h"
15e19adf54SJim Ingham #include "lldb/Core/Section.h"
16e19adf54SJim Ingham #include "lldb/Core/StructuredDataImpl.h"
17e19adf54SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
18e19adf54SJim Ingham #include "lldb/Interpreter/ScriptInterpreter.h"
19e19adf54SJim Ingham #include "lldb/Target/Process.h"
20e19adf54SJim Ingham #include "lldb/Target/Target.h"
21e19adf54SJim Ingham #include "lldb/Utility/Log.h"
22e19adf54SJim Ingham #include "lldb/Utility/StreamString.h"
23e19adf54SJim Ingham 
24e19adf54SJim Ingham using namespace lldb;
25e19adf54SJim Ingham using namespace lldb_private;
26e19adf54SJim Ingham 
27e19adf54SJim Ingham // BreakpointResolverScripted:
28e19adf54SJim Ingham BreakpointResolverScripted::BreakpointResolverScripted(
29e19adf54SJim Ingham     Breakpoint *bkpt,
30e19adf54SJim Ingham     const llvm::StringRef class_name,
31e19adf54SJim Ingham     lldb::SearchDepth depth,
32*47b33dccSJim Ingham     StructuredDataImpl *args_data)
33e19adf54SJim Ingham     : BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),
34e19adf54SJim Ingham       m_class_name(class_name), m_depth(depth), m_args_ptr(args_data) {
35e19adf54SJim Ingham   CreateImplementationIfNeeded();
36e19adf54SJim Ingham }
37e19adf54SJim Ingham 
38e19adf54SJim Ingham void BreakpointResolverScripted::CreateImplementationIfNeeded() {
39e19adf54SJim Ingham   if (m_implementation_sp)
40e19adf54SJim Ingham     return;
41e19adf54SJim Ingham 
42e19adf54SJim Ingham   if (m_class_name.empty())
43e19adf54SJim Ingham     return;
44e19adf54SJim Ingham 
45e19adf54SJim Ingham   if (m_breakpoint) {
46e19adf54SJim Ingham     TargetSP target_sp = m_breakpoint->GetTargetSP();
47e19adf54SJim Ingham     ScriptInterpreter *script_interp = target_sp->GetDebugger()
48e19adf54SJim Ingham                                                 .GetScriptInterpreter();
49e19adf54SJim Ingham     if (!script_interp)
50e19adf54SJim Ingham       return;
51e19adf54SJim Ingham     lldb::BreakpointSP bkpt_sp(m_breakpoint->shared_from_this());
52e19adf54SJim Ingham     m_implementation_sp = script_interp->CreateScriptedBreakpointResolver(
53e19adf54SJim Ingham         m_class_name.c_str(), m_args_ptr, bkpt_sp);
54e19adf54SJim Ingham   }
55e19adf54SJim Ingham }
56e19adf54SJim Ingham 
57e19adf54SJim Ingham void BreakpointResolverScripted::NotifyBreakpointSet() {
58e19adf54SJim Ingham   CreateImplementationIfNeeded();
59e19adf54SJim Ingham }
60e19adf54SJim Ingham 
61e19adf54SJim Ingham BreakpointResolverScripted::~BreakpointResolverScripted() {}
62e19adf54SJim Ingham 
63e19adf54SJim Ingham BreakpointResolver *
64e19adf54SJim Ingham BreakpointResolverScripted::CreateFromStructuredData(
65e19adf54SJim Ingham     Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
66e19adf54SJim Ingham     Status &error) {
67e19adf54SJim Ingham   llvm::StringRef class_name;
68e19adf54SJim Ingham   bool success;
69e19adf54SJim Ingham 
70e19adf54SJim Ingham   success = options_dict.GetValueForKeyAsString(
71e19adf54SJim Ingham       GetKey(OptionNames::PythonClassName), class_name);
72e19adf54SJim Ingham   if (!success) {
73e19adf54SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
74e19adf54SJim Ingham     return nullptr;
75e19adf54SJim Ingham   }
76*47b33dccSJim Ingham   // The Python function will actually provide the search depth, this is a
77*47b33dccSJim Ingham   // placeholder.
78*47b33dccSJim Ingham   lldb::SearchDepth depth = lldb::eSearchDepthTarget;
79e19adf54SJim Ingham 
80e19adf54SJim Ingham   StructuredDataImpl *args_data_impl = new StructuredDataImpl();
816d7fb299SKonrad Kleine   StructuredData::Dictionary *args_dict = nullptr;
82e19adf54SJim Ingham   success = options_dict.GetValueForKeyAsDictionary(
83e19adf54SJim Ingham     GetKey(OptionNames::ScriptArgs), args_dict);
84e19adf54SJim Ingham   if (success) {
85*47b33dccSJim Ingham       args_data_impl->SetObjectSP(args_dict->shared_from_this());
86e19adf54SJim Ingham   }
87*47b33dccSJim Ingham   return new BreakpointResolverScripted(bkpt, class_name, depth,
88*47b33dccSJim Ingham                                         args_data_impl);
89e19adf54SJim Ingham }
90e19adf54SJim Ingham 
91e19adf54SJim Ingham StructuredData::ObjectSP
92e19adf54SJim Ingham BreakpointResolverScripted::SerializeToStructuredData() {
93e19adf54SJim Ingham   StructuredData::DictionarySP options_dict_sp(
94e19adf54SJim Ingham       new StructuredData::Dictionary());
95e19adf54SJim Ingham 
96e19adf54SJim Ingham   options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),
97e19adf54SJim Ingham                                    m_class_name);
98*47b33dccSJim Ingham   if (m_args_ptr->IsValid())
99*47b33dccSJim Ingham       options_dict_sp->AddItem(GetKey(OptionNames::ScriptArgs),
100*47b33dccSJim Ingham           m_args_ptr->GetObjectSP());
101*47b33dccSJim Ingham 
102e19adf54SJim Ingham   return WrapOptionsDict(options_dict_sp);
103e19adf54SJim Ingham }
104e19adf54SJim Ingham 
105e19adf54SJim Ingham ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {
1062b29b432SJonas Devlieghere   return m_breakpoint->GetTarget().GetDebugger().GetScriptInterpreter();
107e19adf54SJim Ingham }
108e19adf54SJim Ingham 
10995e264fcSRaphael Isemann Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(
11095e264fcSRaphael Isemann     SearchFilter &filter, SymbolContext &context, Address *addr) {
111248a1305SKonrad Kleine   assert(m_breakpoint != nullptr);
112e19adf54SJim Ingham   bool should_continue = true;
113e19adf54SJim Ingham   if (!m_implementation_sp)
114e19adf54SJim Ingham     return Searcher::eCallbackReturnStop;
115e19adf54SJim Ingham 
116e19adf54SJim Ingham   ScriptInterpreter *interp = GetScriptInterpreter();
117e19adf54SJim Ingham   should_continue = interp->ScriptedBreakpointResolverSearchCallback(
118e19adf54SJim Ingham       m_implementation_sp,
119e19adf54SJim Ingham       &context);
120e19adf54SJim Ingham   if (should_continue)
121e19adf54SJim Ingham     return Searcher::eCallbackReturnContinue;
122e19adf54SJim Ingham   else
123e19adf54SJim Ingham     return Searcher::eCallbackReturnStop;
124e19adf54SJim Ingham }
125e19adf54SJim Ingham 
126e19adf54SJim Ingham lldb::SearchDepth
127e19adf54SJim Ingham BreakpointResolverScripted::GetDepth() {
128248a1305SKonrad Kleine   assert(m_breakpoint != nullptr);
129e19adf54SJim Ingham   lldb::SearchDepth depth = lldb::eSearchDepthModule;
130e19adf54SJim Ingham   if (m_implementation_sp) {
131e19adf54SJim Ingham     ScriptInterpreter *interp = GetScriptInterpreter();
132e19adf54SJim Ingham     depth = interp->ScriptedBreakpointResolverSearchDepth(
133e19adf54SJim Ingham         m_implementation_sp);
134e19adf54SJim Ingham   }
135e19adf54SJim Ingham   return depth;
136e19adf54SJim Ingham }
137e19adf54SJim Ingham 
138e19adf54SJim Ingham void BreakpointResolverScripted::GetDescription(Stream *s) {
139e19adf54SJim Ingham   StructuredData::GenericSP generic_sp;
140e19adf54SJim Ingham   std::string short_help;
141e19adf54SJim Ingham 
142e19adf54SJim Ingham   if (m_implementation_sp) {
143e19adf54SJim Ingham     ScriptInterpreter *interp = GetScriptInterpreter();
144e19adf54SJim Ingham     interp->GetShortHelpForCommandObject(m_implementation_sp,
145e19adf54SJim Ingham                                          short_help);
146e19adf54SJim Ingham   }
147e19adf54SJim Ingham   if (!short_help.empty())
148e19adf54SJim Ingham     s->PutCString(short_help.c_str());
149e19adf54SJim Ingham   else
150e19adf54SJim Ingham     s->Printf("python class = %s", m_class_name.c_str());
151e19adf54SJim Ingham }
152e19adf54SJim Ingham 
153e19adf54SJim Ingham void BreakpointResolverScripted::Dump(Stream *s) const {}
154e19adf54SJim Ingham 
155e19adf54SJim Ingham lldb::BreakpointResolverSP
156e19adf54SJim Ingham BreakpointResolverScripted::CopyForBreakpoint(Breakpoint &breakpoint) {
157e19adf54SJim Ingham   // FIXME: Have to make a copy of the arguments from the m_args_ptr and then
158e19adf54SJim Ingham   // pass that to the new resolver.
159e19adf54SJim Ingham   lldb::BreakpointResolverSP ret_sp(
160*47b33dccSJim Ingham       new BreakpointResolverScripted(&breakpoint, m_class_name, m_depth,
161*47b33dccSJim Ingham                                      nullptr));
162e19adf54SJim Ingham   return ret_sp;
163e19adf54SJim Ingham }
164