1 //===-- BreakpointResolverScripted.cpp ------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Breakpoint/BreakpointResolverScripted.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Core/Debugger.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Core/StructuredDataImpl.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/ScriptInterpreter.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/StreamString.h"
25 using namespace lldb_private
;
27 // BreakpointResolverScripted:
28 BreakpointResolverScripted::BreakpointResolverScripted(
29 const BreakpointSP
&bkpt
, const llvm::StringRef class_name
,
30 lldb::SearchDepth depth
, const StructuredDataImpl
&args_data
)
31 : BreakpointResolver(bkpt
, BreakpointResolver::PythonResolver
),
32 m_class_name(std::string(class_name
)), m_depth(depth
), m_args(args_data
) {
33 CreateImplementationIfNeeded(bkpt
);
36 void BreakpointResolverScripted::CreateImplementationIfNeeded(
37 BreakpointSP breakpoint_sp
) {
38 if (m_implementation_sp
)
41 if (m_class_name
.empty())
47 TargetSP target_sp
= breakpoint_sp
->GetTargetSP();
48 ScriptInterpreter
*script_interp
= target_sp
->GetDebugger()
49 .GetScriptInterpreter();
53 m_implementation_sp
= script_interp
->CreateScriptedBreakpointResolver(
54 m_class_name
.c_str(), m_args
, breakpoint_sp
);
57 void BreakpointResolverScripted::NotifyBreakpointSet() {
58 CreateImplementationIfNeeded(GetBreakpoint());
61 BreakpointResolverSP
BreakpointResolverScripted::CreateFromStructuredData(
62 const StructuredData::Dictionary
&options_dict
, Status
&error
) {
63 llvm::StringRef class_name
;
66 success
= options_dict
.GetValueForKeyAsString(
67 GetKey(OptionNames::PythonClassName
), class_name
);
70 Status::FromErrorString("BRFL::CFSD: Couldn't find class name entry.");
73 // The Python function will actually provide the search depth, this is a
75 lldb::SearchDepth depth
= lldb::eSearchDepthTarget
;
77 StructuredDataImpl args_data_impl
;
78 StructuredData::Dictionary
*args_dict
= nullptr;
79 if (options_dict
.GetValueForKeyAsDictionary(GetKey(OptionNames::ScriptArgs
),
81 args_data_impl
.SetObjectSP(args_dict
->shared_from_this());
82 return std::make_shared
<BreakpointResolverScripted
>(nullptr, class_name
,
83 depth
, args_data_impl
);
86 StructuredData::ObjectSP
87 BreakpointResolverScripted::SerializeToStructuredData() {
88 StructuredData::DictionarySP
options_dict_sp(
89 new StructuredData::Dictionary());
91 options_dict_sp
->AddStringItem(GetKey(OptionNames::PythonClassName
),
94 options_dict_sp
->AddItem(GetKey(OptionNames::ScriptArgs
),
95 m_args
.GetObjectSP());
97 return WrapOptionsDict(options_dict_sp
);
100 ScriptInterpreter
*BreakpointResolverScripted::GetScriptInterpreter() {
101 return GetBreakpoint()->GetTarget().GetDebugger().GetScriptInterpreter();
104 Searcher::CallbackReturn
BreakpointResolverScripted::SearchCallback(
105 SearchFilter
&filter
, SymbolContext
&context
, Address
*addr
) {
106 bool should_continue
= true;
107 if (!m_implementation_sp
)
108 return Searcher::eCallbackReturnStop
;
110 ScriptInterpreter
*interp
= GetScriptInterpreter();
111 should_continue
= interp
->ScriptedBreakpointResolverSearchCallback(
115 return Searcher::eCallbackReturnContinue
;
117 return Searcher::eCallbackReturnStop
;
121 BreakpointResolverScripted::GetDepth() {
122 lldb::SearchDepth depth
= lldb::eSearchDepthModule
;
123 if (m_implementation_sp
) {
124 ScriptInterpreter
*interp
= GetScriptInterpreter();
125 depth
= interp
->ScriptedBreakpointResolverSearchDepth(
126 m_implementation_sp
);
131 void BreakpointResolverScripted::GetDescription(Stream
*s
) {
132 StructuredData::GenericSP generic_sp
;
133 std::string short_help
;
135 if (m_implementation_sp
) {
136 ScriptInterpreter
*interp
= GetScriptInterpreter();
137 interp
->GetShortHelpForCommandObject(m_implementation_sp
,
140 if (!short_help
.empty())
141 s
->PutCString(short_help
.c_str());
143 s
->Printf("python class = %s", m_class_name
.c_str());
146 void BreakpointResolverScripted::Dump(Stream
*s
) const {}
148 lldb::BreakpointResolverSP
149 BreakpointResolverScripted::CopyForBreakpoint(BreakpointSP
&breakpoint
) {
150 return std::make_shared
<BreakpointResolverScripted
>(breakpoint
, m_class_name
,