1*b5893f02SDimitry Andric //===-- BreakpointResolverScripted.cpp ---------------------------*- C++ -*-===//
2*b5893f02SDimitry Andric //
3*b5893f02SDimitry Andric //                     The LLVM Compiler Infrastructure
4*b5893f02SDimitry Andric //
5*b5893f02SDimitry Andric // This file is distributed under the University of Illinois Open Source
6*b5893f02SDimitry Andric // License. See LICENSE.TXT for details.
7*b5893f02SDimitry Andric //
8*b5893f02SDimitry Andric //===----------------------------------------------------------------------===//
9*b5893f02SDimitry Andric 
10*b5893f02SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverScripted.h"
11*b5893f02SDimitry Andric 
12*b5893f02SDimitry Andric 
13*b5893f02SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
14*b5893f02SDimitry Andric #include "lldb/Core/Debugger.h"
15*b5893f02SDimitry Andric #include "lldb/Core/Module.h"
16*b5893f02SDimitry Andric #include "lldb/Core/Section.h"
17*b5893f02SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
18*b5893f02SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
19*b5893f02SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
20*b5893f02SDimitry Andric #include "lldb/Target/Process.h"
21*b5893f02SDimitry Andric #include "lldb/Target/Target.h"
22*b5893f02SDimitry Andric #include "lldb/Utility/Log.h"
23*b5893f02SDimitry Andric #include "lldb/Utility/StreamString.h"
24*b5893f02SDimitry Andric 
25*b5893f02SDimitry Andric using namespace lldb;
26*b5893f02SDimitry Andric using namespace lldb_private;
27*b5893f02SDimitry Andric 
28*b5893f02SDimitry Andric //----------------------------------------------------------------------
29*b5893f02SDimitry Andric // BreakpointResolverScripted:
30*b5893f02SDimitry Andric //----------------------------------------------------------------------
BreakpointResolverScripted(Breakpoint * bkpt,const llvm::StringRef class_name,lldb::SearchDepth depth,StructuredDataImpl * args_data,ScriptInterpreter & script_interp)31*b5893f02SDimitry Andric BreakpointResolverScripted::BreakpointResolverScripted(
32*b5893f02SDimitry Andric     Breakpoint *bkpt,
33*b5893f02SDimitry Andric     const llvm::StringRef class_name,
34*b5893f02SDimitry Andric     lldb::SearchDepth depth,
35*b5893f02SDimitry Andric     StructuredDataImpl *args_data,
36*b5893f02SDimitry Andric     ScriptInterpreter &script_interp)
37*b5893f02SDimitry Andric     : BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),
38*b5893f02SDimitry Andric       m_class_name(class_name), m_depth(depth), m_args_ptr(args_data) {
39*b5893f02SDimitry Andric   CreateImplementationIfNeeded();
40*b5893f02SDimitry Andric }
41*b5893f02SDimitry Andric 
CreateImplementationIfNeeded()42*b5893f02SDimitry Andric void BreakpointResolverScripted::CreateImplementationIfNeeded() {
43*b5893f02SDimitry Andric   if (m_implementation_sp)
44*b5893f02SDimitry Andric     return;
45*b5893f02SDimitry Andric 
46*b5893f02SDimitry Andric   if (m_class_name.empty())
47*b5893f02SDimitry Andric     return;
48*b5893f02SDimitry Andric 
49*b5893f02SDimitry Andric   if (m_breakpoint) {
50*b5893f02SDimitry Andric     TargetSP target_sp = m_breakpoint->GetTargetSP();
51*b5893f02SDimitry Andric     ScriptInterpreter *script_interp = target_sp->GetDebugger()
52*b5893f02SDimitry Andric                                                 .GetCommandInterpreter()
53*b5893f02SDimitry Andric                                                 .GetScriptInterpreter();
54*b5893f02SDimitry Andric     if (!script_interp)
55*b5893f02SDimitry Andric       return;
56*b5893f02SDimitry Andric     lldb::BreakpointSP bkpt_sp(m_breakpoint->shared_from_this());
57*b5893f02SDimitry Andric     m_implementation_sp = script_interp->CreateScriptedBreakpointResolver(
58*b5893f02SDimitry Andric         m_class_name.c_str(), m_args_ptr, bkpt_sp);
59*b5893f02SDimitry Andric   }
60*b5893f02SDimitry Andric }
61*b5893f02SDimitry Andric 
NotifyBreakpointSet()62*b5893f02SDimitry Andric void BreakpointResolverScripted::NotifyBreakpointSet() {
63*b5893f02SDimitry Andric   CreateImplementationIfNeeded();
64*b5893f02SDimitry Andric }
65*b5893f02SDimitry Andric 
~BreakpointResolverScripted()66*b5893f02SDimitry Andric BreakpointResolverScripted::~BreakpointResolverScripted() {}
67*b5893f02SDimitry Andric 
68*b5893f02SDimitry Andric BreakpointResolver *
CreateFromStructuredData(Breakpoint * bkpt,const StructuredData::Dictionary & options_dict,Status & error)69*b5893f02SDimitry Andric BreakpointResolverScripted::CreateFromStructuredData(
70*b5893f02SDimitry Andric     Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
71*b5893f02SDimitry Andric     Status &error) {
72*b5893f02SDimitry Andric   llvm::StringRef class_name;
73*b5893f02SDimitry Andric   bool success;
74*b5893f02SDimitry Andric 
75*b5893f02SDimitry Andric   if (!bkpt)
76*b5893f02SDimitry Andric     return nullptr;
77*b5893f02SDimitry Andric 
78*b5893f02SDimitry Andric   success = options_dict.GetValueForKeyAsString(
79*b5893f02SDimitry Andric       GetKey(OptionNames::PythonClassName), class_name);
80*b5893f02SDimitry Andric   if (!success) {
81*b5893f02SDimitry Andric     error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
82*b5893f02SDimitry Andric     return nullptr;
83*b5893f02SDimitry Andric   }
84*b5893f02SDimitry Andric   lldb::SearchDepth depth;
85*b5893f02SDimitry Andric   int depth_as_int;
86*b5893f02SDimitry Andric   success = options_dict.GetValueForKeyAsInteger(
87*b5893f02SDimitry Andric       GetKey(OptionNames::SearchDepth), depth_as_int);
88*b5893f02SDimitry Andric   if (!success) {
89*b5893f02SDimitry Andric     error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
90*b5893f02SDimitry Andric     return nullptr;
91*b5893f02SDimitry Andric   }
92*b5893f02SDimitry Andric   if (depth_as_int >= (int) OptionNames::LastOptionName) {
93*b5893f02SDimitry Andric     error.SetErrorString("BRFL::CFSD: Invalid value for search depth.");
94*b5893f02SDimitry Andric     return nullptr;
95*b5893f02SDimitry Andric   }
96*b5893f02SDimitry Andric   depth = (lldb::SearchDepth) depth_as_int;
97*b5893f02SDimitry Andric 
98*b5893f02SDimitry Andric   StructuredDataImpl *args_data_impl = new StructuredDataImpl();
99*b5893f02SDimitry Andric   StructuredData::Dictionary *args_dict = new StructuredData::Dictionary();
100*b5893f02SDimitry Andric   success = options_dict.GetValueForKeyAsDictionary(
101*b5893f02SDimitry Andric     GetKey(OptionNames::ScriptArgs), args_dict);
102*b5893f02SDimitry Andric   if (success) {
103*b5893f02SDimitry Andric     // FIXME: The resolver needs a copy of the ARGS dict that it can own,
104*b5893f02SDimitry Andric     // so I need to make a copy constructor for the Dictionary so I can pass
105*b5893f02SDimitry Andric     // that to it here.  For now the args are empty.
106*b5893f02SDimitry Andric     //StructuredData::Dictionary *dict_copy = new StructuredData::Dictionary(args_dict);
107*b5893f02SDimitry Andric 
108*b5893f02SDimitry Andric   }
109*b5893f02SDimitry Andric   ScriptInterpreter *script_interp = bkpt->GetTarget()
110*b5893f02SDimitry Andric                                          .GetDebugger()
111*b5893f02SDimitry Andric                                          .GetCommandInterpreter()
112*b5893f02SDimitry Andric                                          .GetScriptInterpreter();
113*b5893f02SDimitry Andric   return new BreakpointResolverScripted(bkpt, class_name, depth, args_data_impl,
114*b5893f02SDimitry Andric                                       *script_interp);
115*b5893f02SDimitry Andric }
116*b5893f02SDimitry Andric 
117*b5893f02SDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()118*b5893f02SDimitry Andric BreakpointResolverScripted::SerializeToStructuredData() {
119*b5893f02SDimitry Andric   StructuredData::DictionarySP options_dict_sp(
120*b5893f02SDimitry Andric       new StructuredData::Dictionary());
121*b5893f02SDimitry Andric 
122*b5893f02SDimitry Andric   options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),
123*b5893f02SDimitry Andric                                    m_class_name);
124*b5893f02SDimitry Andric   return WrapOptionsDict(options_dict_sp);
125*b5893f02SDimitry Andric }
126*b5893f02SDimitry Andric 
GetScriptInterpreter()127*b5893f02SDimitry Andric ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {
128*b5893f02SDimitry Andric     return m_breakpoint->GetTarget().GetDebugger().GetCommandInterpreter()
129*b5893f02SDimitry Andric         .GetScriptInterpreter();
130*b5893f02SDimitry Andric }
131*b5893f02SDimitry Andric 
132*b5893f02SDimitry Andric Searcher::CallbackReturn
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr,bool containing)133*b5893f02SDimitry Andric BreakpointResolverScripted::SearchCallback(SearchFilter &filter,
134*b5893f02SDimitry Andric                                           SymbolContext &context, Address *addr,
135*b5893f02SDimitry Andric                                           bool containing) {
136*b5893f02SDimitry Andric   assert(m_breakpoint != NULL);
137*b5893f02SDimitry Andric   bool should_continue = true;
138*b5893f02SDimitry Andric   if (!m_implementation_sp)
139*b5893f02SDimitry Andric     return Searcher::eCallbackReturnStop;
140*b5893f02SDimitry Andric 
141*b5893f02SDimitry Andric   ScriptInterpreter *interp = GetScriptInterpreter();
142*b5893f02SDimitry Andric   should_continue = interp->ScriptedBreakpointResolverSearchCallback(
143*b5893f02SDimitry Andric       m_implementation_sp,
144*b5893f02SDimitry Andric       &context);
145*b5893f02SDimitry Andric   if (should_continue)
146*b5893f02SDimitry Andric     return Searcher::eCallbackReturnContinue;
147*b5893f02SDimitry Andric   else
148*b5893f02SDimitry Andric     return Searcher::eCallbackReturnStop;
149*b5893f02SDimitry Andric }
150*b5893f02SDimitry Andric 
151*b5893f02SDimitry Andric lldb::SearchDepth
GetDepth()152*b5893f02SDimitry Andric BreakpointResolverScripted::GetDepth() {
153*b5893f02SDimitry Andric   assert(m_breakpoint != NULL);
154*b5893f02SDimitry Andric   lldb::SearchDepth depth = lldb::eSearchDepthModule;
155*b5893f02SDimitry Andric   if (m_implementation_sp) {
156*b5893f02SDimitry Andric     ScriptInterpreter *interp = GetScriptInterpreter();
157*b5893f02SDimitry Andric     depth = interp->ScriptedBreakpointResolverSearchDepth(
158*b5893f02SDimitry Andric         m_implementation_sp);
159*b5893f02SDimitry Andric   }
160*b5893f02SDimitry Andric   return depth;
161*b5893f02SDimitry Andric }
162*b5893f02SDimitry Andric 
GetDescription(Stream * s)163*b5893f02SDimitry Andric void BreakpointResolverScripted::GetDescription(Stream *s) {
164*b5893f02SDimitry Andric   StructuredData::GenericSP generic_sp;
165*b5893f02SDimitry Andric   std::string short_help;
166*b5893f02SDimitry Andric 
167*b5893f02SDimitry Andric   if (m_implementation_sp) {
168*b5893f02SDimitry Andric     ScriptInterpreter *interp = GetScriptInterpreter();
169*b5893f02SDimitry Andric     interp->GetShortHelpForCommandObject(m_implementation_sp,
170*b5893f02SDimitry Andric                                          short_help);
171*b5893f02SDimitry Andric   }
172*b5893f02SDimitry Andric   if (!short_help.empty())
173*b5893f02SDimitry Andric     s->PutCString(short_help.c_str());
174*b5893f02SDimitry Andric   else
175*b5893f02SDimitry Andric     s->Printf("python class = %s", m_class_name.c_str());
176*b5893f02SDimitry Andric }
177*b5893f02SDimitry Andric 
Dump(Stream * s) const178*b5893f02SDimitry Andric void BreakpointResolverScripted::Dump(Stream *s) const {}
179*b5893f02SDimitry Andric 
180*b5893f02SDimitry Andric lldb::BreakpointResolverSP
CopyForBreakpoint(Breakpoint & breakpoint)181*b5893f02SDimitry Andric BreakpointResolverScripted::CopyForBreakpoint(Breakpoint &breakpoint) {
182*b5893f02SDimitry Andric   ScriptInterpreter *script_interp = GetScriptInterpreter();
183*b5893f02SDimitry Andric   // FIXME: Have to make a copy of the arguments from the m_args_ptr and then
184*b5893f02SDimitry Andric   // pass that to the new resolver.
185*b5893f02SDimitry Andric   lldb::BreakpointResolverSP ret_sp(
186*b5893f02SDimitry Andric       new BreakpointResolverScripted(&breakpoint, m_class_name,
187*b5893f02SDimitry Andric                                    m_depth, nullptr, *script_interp));
188*b5893f02SDimitry Andric   return ret_sp;
189*b5893f02SDimitry Andric }
190