1dda28197Spatrick //===-- BreakpointResolverScripted.cpp ------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Breakpoint/BreakpointResolverScripted.h"
10061da546Spatrick
11061da546Spatrick
12061da546Spatrick #include "lldb/Breakpoint/BreakpointLocation.h"
13061da546Spatrick #include "lldb/Core/Debugger.h"
14061da546Spatrick #include "lldb/Core/Module.h"
15061da546Spatrick #include "lldb/Core/Section.h"
16061da546Spatrick #include "lldb/Core/StructuredDataImpl.h"
17061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
18061da546Spatrick #include "lldb/Interpreter/ScriptInterpreter.h"
19061da546Spatrick #include "lldb/Target/Process.h"
20061da546Spatrick #include "lldb/Target/Target.h"
21061da546Spatrick #include "lldb/Utility/Log.h"
22061da546Spatrick #include "lldb/Utility/StreamString.h"
23061da546Spatrick
24061da546Spatrick using namespace lldb;
25061da546Spatrick using namespace lldb_private;
26061da546Spatrick
27061da546Spatrick // BreakpointResolverScripted:
BreakpointResolverScripted(const BreakpointSP & bkpt,const llvm::StringRef class_name,lldb::SearchDepth depth,const StructuredDataImpl & args_data)28061da546Spatrick BreakpointResolverScripted::BreakpointResolverScripted(
29dda28197Spatrick const BreakpointSP &bkpt, const llvm::StringRef class_name,
30*f6aab3d8Srobert lldb::SearchDepth depth, const StructuredDataImpl &args_data)
31061da546Spatrick : BreakpointResolver(bkpt, BreakpointResolver::PythonResolver),
32*f6aab3d8Srobert m_class_name(std::string(class_name)), m_depth(depth), m_args(args_data) {
33dda28197Spatrick CreateImplementationIfNeeded(bkpt);
34061da546Spatrick }
35061da546Spatrick
CreateImplementationIfNeeded(BreakpointSP breakpoint_sp)36dda28197Spatrick void BreakpointResolverScripted::CreateImplementationIfNeeded(
37dda28197Spatrick BreakpointSP breakpoint_sp) {
38061da546Spatrick if (m_implementation_sp)
39061da546Spatrick return;
40061da546Spatrick
41061da546Spatrick if (m_class_name.empty())
42061da546Spatrick return;
43061da546Spatrick
44dda28197Spatrick if (!breakpoint_sp)
45dda28197Spatrick return;
46dda28197Spatrick
47dda28197Spatrick TargetSP target_sp = breakpoint_sp->GetTargetSP();
48061da546Spatrick ScriptInterpreter *script_interp = target_sp->GetDebugger()
49061da546Spatrick .GetScriptInterpreter();
50061da546Spatrick if (!script_interp)
51061da546Spatrick return;
52dda28197Spatrick
53061da546Spatrick m_implementation_sp = script_interp->CreateScriptedBreakpointResolver(
54*f6aab3d8Srobert m_class_name.c_str(), m_args, breakpoint_sp);
55061da546Spatrick }
56061da546Spatrick
NotifyBreakpointSet()57061da546Spatrick void BreakpointResolverScripted::NotifyBreakpointSet() {
58dda28197Spatrick CreateImplementationIfNeeded(GetBreakpoint());
59061da546Spatrick }
60061da546Spatrick
61061da546Spatrick BreakpointResolver *
CreateFromStructuredData(const BreakpointSP & bkpt,const StructuredData::Dictionary & options_dict,Status & error)62061da546Spatrick BreakpointResolverScripted::CreateFromStructuredData(
63dda28197Spatrick const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
64061da546Spatrick Status &error) {
65061da546Spatrick llvm::StringRef class_name;
66061da546Spatrick bool success;
67061da546Spatrick
68061da546Spatrick success = options_dict.GetValueForKeyAsString(
69061da546Spatrick GetKey(OptionNames::PythonClassName), class_name);
70061da546Spatrick if (!success) {
71061da546Spatrick error.SetErrorString("BRFL::CFSD: Couldn't find class name entry.");
72061da546Spatrick return nullptr;
73061da546Spatrick }
74061da546Spatrick // The Python function will actually provide the search depth, this is a
75061da546Spatrick // placeholder.
76061da546Spatrick lldb::SearchDepth depth = lldb::eSearchDepthTarget;
77061da546Spatrick
78*f6aab3d8Srobert StructuredDataImpl args_data_impl;
79061da546Spatrick StructuredData::Dictionary *args_dict = nullptr;
80*f6aab3d8Srobert if (options_dict.GetValueForKeyAsDictionary(GetKey(OptionNames::ScriptArgs),
81*f6aab3d8Srobert args_dict))
82*f6aab3d8Srobert args_data_impl.SetObjectSP(args_dict->shared_from_this());
83061da546Spatrick return new BreakpointResolverScripted(bkpt, class_name, depth,
84061da546Spatrick args_data_impl);
85061da546Spatrick }
86061da546Spatrick
87061da546Spatrick StructuredData::ObjectSP
SerializeToStructuredData()88061da546Spatrick BreakpointResolverScripted::SerializeToStructuredData() {
89061da546Spatrick StructuredData::DictionarySP options_dict_sp(
90061da546Spatrick new StructuredData::Dictionary());
91061da546Spatrick
92061da546Spatrick options_dict_sp->AddStringItem(GetKey(OptionNames::PythonClassName),
93061da546Spatrick m_class_name);
94*f6aab3d8Srobert if (m_args.IsValid())
95061da546Spatrick options_dict_sp->AddItem(GetKey(OptionNames::ScriptArgs),
96*f6aab3d8Srobert m_args.GetObjectSP());
97061da546Spatrick
98061da546Spatrick return WrapOptionsDict(options_dict_sp);
99061da546Spatrick }
100061da546Spatrick
GetScriptInterpreter()101061da546Spatrick ScriptInterpreter *BreakpointResolverScripted::GetScriptInterpreter() {
102dda28197Spatrick return GetBreakpoint()->GetTarget().GetDebugger().GetScriptInterpreter();
103061da546Spatrick }
104061da546Spatrick
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)105061da546Spatrick Searcher::CallbackReturn BreakpointResolverScripted::SearchCallback(
106061da546Spatrick SearchFilter &filter, SymbolContext &context, Address *addr) {
107061da546Spatrick bool should_continue = true;
108061da546Spatrick if (!m_implementation_sp)
109061da546Spatrick return Searcher::eCallbackReturnStop;
110061da546Spatrick
111061da546Spatrick ScriptInterpreter *interp = GetScriptInterpreter();
112061da546Spatrick should_continue = interp->ScriptedBreakpointResolverSearchCallback(
113061da546Spatrick m_implementation_sp,
114061da546Spatrick &context);
115061da546Spatrick if (should_continue)
116061da546Spatrick return Searcher::eCallbackReturnContinue;
117dda28197Spatrick
118061da546Spatrick return Searcher::eCallbackReturnStop;
119061da546Spatrick }
120061da546Spatrick
121061da546Spatrick lldb::SearchDepth
GetDepth()122061da546Spatrick BreakpointResolverScripted::GetDepth() {
123061da546Spatrick lldb::SearchDepth depth = lldb::eSearchDepthModule;
124061da546Spatrick if (m_implementation_sp) {
125061da546Spatrick ScriptInterpreter *interp = GetScriptInterpreter();
126061da546Spatrick depth = interp->ScriptedBreakpointResolverSearchDepth(
127061da546Spatrick m_implementation_sp);
128061da546Spatrick }
129061da546Spatrick return depth;
130061da546Spatrick }
131061da546Spatrick
GetDescription(Stream * s)132061da546Spatrick void BreakpointResolverScripted::GetDescription(Stream *s) {
133061da546Spatrick StructuredData::GenericSP generic_sp;
134061da546Spatrick std::string short_help;
135061da546Spatrick
136061da546Spatrick if (m_implementation_sp) {
137061da546Spatrick ScriptInterpreter *interp = GetScriptInterpreter();
138061da546Spatrick interp->GetShortHelpForCommandObject(m_implementation_sp,
139061da546Spatrick short_help);
140061da546Spatrick }
141061da546Spatrick if (!short_help.empty())
142061da546Spatrick s->PutCString(short_help.c_str());
143061da546Spatrick else
144061da546Spatrick s->Printf("python class = %s", m_class_name.c_str());
145061da546Spatrick }
146061da546Spatrick
Dump(Stream * s) const147061da546Spatrick void BreakpointResolverScripted::Dump(Stream *s) const {}
148061da546Spatrick
149061da546Spatrick lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)150dda28197Spatrick BreakpointResolverScripted::CopyForBreakpoint(BreakpointSP &breakpoint) {
151*f6aab3d8Srobert return std::make_shared<BreakpointResolverScripted>(breakpoint, m_class_name,
152*f6aab3d8Srobert m_depth, m_args);
153061da546Spatrick }
154