Skip to content

Commit a90b085

Browse files
committed
[lldb/Interpreter] Introduce ScriptedPlatform{,Python}Interface
This patch introduces both the ScriptedPlatformInterface and the ScriptedPlatformPythonInterface. As the name suggests, these calls will be used to call into the Scripted Platform python implementation from the C++ Scripted Platform plugin instance. Differential Revision: https://reviews.llvm.org/D139251 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 96141b0 commit a90b085

File tree

8 files changed

+231
-3
lines changed

8 files changed

+231
-3
lines changed

lldb/include/lldb/Interpreter/ScriptInterpreter.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/Core/StreamFile.h"
2222
#include "lldb/Core/ThreadedCommunication.h"
2323
#include "lldb/Host/PseudoTerminal.h"
24+
#include "lldb/Interpreter/ScriptedPlatformInterface.h"
2425
#include "lldb/Interpreter/ScriptedProcessInterface.h"
2526
#include "lldb/Utility/Broadcaster.h"
2627
#include "lldb/Utility/Status.h"
@@ -146,7 +147,12 @@ class ScriptInterpreter : public PluginInterface {
146147
eScriptReturnTypeOpaqueObject
147148
};
148149

149-
ScriptInterpreter(Debugger &debugger, lldb::ScriptLanguage script_lang);
150+
ScriptInterpreter(
151+
Debugger &debugger, lldb::ScriptLanguage script_lang,
152+
lldb::ScriptedProcessInterfaceUP scripted_process_interface_up =
153+
std::make_unique<ScriptedProcessInterface>(),
154+
lldb::ScriptedPlatformInterfaceUP scripted_platform_interface_up =
155+
std::make_unique<ScriptedPlatformInterface>());
150156

151157
virtual StructuredData::DictionarySP GetInterpreterInfo();
152158

@@ -574,6 +580,10 @@ class ScriptInterpreter : public PluginInterface {
574580
return std::make_unique<ScriptedProcessInterface>();
575581
}
576582

583+
ScriptedPlatformInterface &GetScriptedPlatformInterface() {
584+
return *m_scripted_platform_interface_up;
585+
}
586+
577587
lldb::DataExtractorSP
578588
GetDataExtractorFromSBData(const lldb::SBData &data) const;
579589

@@ -594,6 +604,8 @@ class ScriptInterpreter : public PluginInterface {
594604
protected:
595605
Debugger &m_debugger;
596606
lldb::ScriptLanguage m_script_lang;
607+
lldb::ScriptedProcessInterfaceUP m_scripted_process_interface_up;
608+
lldb::ScriptedPlatformInterfaceUP m_scripted_platform_interface_up;
597609
};
598610

599611
} // namespace lldb_private
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- ScriptedPlatformInterface.h -----------------------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_INTERPRETER_SCRIPTEDPLATFORMINTERFACE_H
10+
#define LLDB_INTERPRETER_SCRIPTEDPLATFORMINTERFACE_H
11+
12+
#include "lldb/Core/StructuredDataImpl.h"
13+
#include "lldb/Interpreter/ScriptedInterface.h"
14+
15+
#include "lldb/lldb-private.h"
16+
17+
#include <string>
18+
19+
namespace lldb_private {
20+
class ScriptedPlatformInterface : virtual public ScriptedInterface {
21+
public:
22+
StructuredData::GenericSP
23+
CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx,
24+
StructuredData::DictionarySP args_sp,
25+
StructuredData::Generic *script_obj = nullptr) override {
26+
return {};
27+
}
28+
29+
virtual StructuredData::DictionarySP ListProcesses() { return {}; }
30+
31+
virtual StructuredData::DictionarySP GetProcessInfo(lldb::pid_t) {
32+
return {};
33+
}
34+
35+
virtual Status AttachToProcess(lldb::ProcessAttachInfoSP attach_info) {
36+
return Status("ScriptedPlatformInterface cannot attach to a process");
37+
}
38+
39+
virtual Status LaunchProcess(lldb::ProcessLaunchInfoSP launch_info) {
40+
return Status("ScriptedPlatformInterface cannot launch process");
41+
}
42+
43+
virtual Status KillProcess(lldb::pid_t pid) {
44+
return Status("ScriptedPlatformInterface cannot kill process");
45+
}
46+
};
47+
} // namespace lldb_private
48+
49+
#endif // LLDB_INTERPRETER_SCRIPTEDPLATFORMINTERFACE_H

lldb/include/lldb/lldb-forward.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class PersistentExpressionState;
161161
class Platform;
162162
class Process;
163163
class ProcessAttachInfo;
164+
class ProcessLaunchInfo;
164165
class ProcessInfo;
165166
class ProcessInstanceInfo;
166167
class ProcessInstanceInfoMatch;
@@ -181,6 +182,7 @@ class Scalar;
181182
class ScriptInterpreter;
182183
class ScriptInterpreterLocker;
183184
class ScriptedMetadata;
185+
class ScriptedPlatformInterface;
184186
class ScriptedProcessInterface;
185187
class ScriptedThreadInterface;
186188
class ScriptedSyntheticChildren;
@@ -380,6 +382,8 @@ typedef std::shared_ptr<lldb_private::ScriptSummaryFormat>
380382
ScriptSummaryFormatSP;
381383
typedef std::shared_ptr<lldb_private::ScriptInterpreter> ScriptInterpreterSP;
382384
typedef std::shared_ptr<lldb_private::ScriptedMetadata> ScriptedMetadataSP;
385+
typedef std::unique_ptr<lldb_private::ScriptedPlatformInterface>
386+
ScriptedPlatformInterfaceUP;
383387
typedef std::unique_ptr<lldb_private::ScriptedProcessInterface>
384388
ScriptedProcessInterfaceUP;
385389
typedef std::shared_ptr<lldb_private::ScriptedThreadInterface>

lldb/source/Interpreter/ScriptInterpreter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ using namespace lldb;
2727
using namespace lldb_private;
2828

2929
ScriptInterpreter::ScriptInterpreter(
30-
Debugger &debugger, lldb::ScriptLanguage script_lang)
31-
: m_debugger(debugger), m_script_lang(script_lang) {}
30+
Debugger &debugger, lldb::ScriptLanguage script_lang,
31+
lldb::ScriptedProcessInterfaceUP scripted_process_interface_up,
32+
lldb::ScriptedPlatformInterfaceUP scripted_platform_interface_up)
33+
: m_debugger(debugger), m_script_lang(script_lang),
34+
m_scripted_process_interface_up(std::move(scripted_process_interface_up)),
35+
m_scripted_platform_interface_up(
36+
std::move(scripted_platform_interface_up)) {}
3237

3338
void ScriptInterpreter::CollectDataForBreakpointCommandCallback(
3439
std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,

lldb/source/Plugins/ScriptInterpreter/Python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_lldb_library(lldbPluginScriptInterpreterPython PLUGIN
2020
ScriptedPythonInterface.cpp
2121
ScriptedProcessPythonInterface.cpp
2222
ScriptedThreadPythonInterface.cpp
23+
ScriptedPlatformPythonInterface.cpp
2324

2425
LINK_LIBS
2526
lldbBreakpoint

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "PythonReadline.h"
1919
#include "SWIGPythonBridge.h"
2020
#include "ScriptInterpreterPythonImpl.h"
21+
#include "ScriptedPlatformPythonInterface.h"
2122
#include "ScriptedProcessPythonInterface.h"
2223

2324
#include "lldb/API/SBError.h"
@@ -410,6 +411,10 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
410411
m_active_io_handler(eIOHandlerNone), m_session_is_active(false),
411412
m_pty_secondary_is_open(false), m_valid_session(true), m_lock_count(0),
412413
m_command_thread_state(nullptr) {
414+
m_scripted_process_interface_up =
415+
std::make_unique<ScriptedProcessPythonInterface>(*this);
416+
m_scripted_platform_interface_up =
417+
std::make_unique<ScriptedPlatformPythonInterface>(*this);
413418

414419
m_dictionary_name.append("_dict");
415420
StreamString run_string;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===-- ScriptedPlatformPythonInterface.cpp -------------------------------===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/Host/Config.h"
10+
#include "lldb/Utility/Log.h"
11+
#include "lldb/Utility/Status.h"
12+
#include "lldb/lldb-enumerations.h"
13+
14+
#if LLDB_ENABLE_PYTHON
15+
16+
// LLDB Python header must be included first
17+
#include "lldb-python.h"
18+
19+
#include "SWIGPythonBridge.h"
20+
#include "ScriptInterpreterPythonImpl.h"
21+
#include "ScriptedPlatformPythonInterface.h"
22+
23+
using namespace lldb;
24+
using namespace lldb_private;
25+
using namespace lldb_private::python;
26+
using Locker = ScriptInterpreterPythonImpl::Locker;
27+
28+
ScriptedPlatformPythonInterface::ScriptedPlatformPythonInterface(
29+
ScriptInterpreterPythonImpl &interpreter)
30+
: ScriptedPlatformInterface(), ScriptedPythonInterface(interpreter) {}
31+
32+
StructuredData::GenericSP ScriptedPlatformPythonInterface::CreatePluginObject(
33+
llvm::StringRef class_name, ExecutionContext &exe_ctx,
34+
StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
35+
if (class_name.empty())
36+
return {};
37+
38+
StructuredDataImpl args_impl(args_sp);
39+
std::string error_string;
40+
41+
Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
42+
Locker::FreeLock);
43+
44+
lldb::ExecutionContextRefSP exe_ctx_ref_sp =
45+
std::make_shared<ExecutionContextRef>(exe_ctx);
46+
47+
PythonObject ret_val = LLDBSwigPythonCreateScriptedObject(
48+
class_name.str().c_str(), m_interpreter.GetDictionaryName(),
49+
exe_ctx_ref_sp, args_impl, error_string);
50+
51+
m_object_instance_sp =
52+
StructuredData::GenericSP(new StructuredPythonObject(std::move(ret_val)));
53+
54+
return m_object_instance_sp;
55+
}
56+
57+
StructuredData::DictionarySP ScriptedPlatformPythonInterface::ListProcesses() {
58+
Status error;
59+
StructuredData::DictionarySP dict_sp =
60+
Dispatch<StructuredData::DictionarySP>("list_processes", error);
61+
62+
if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
63+
return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
64+
LLVM_PRETTY_FUNCTION,
65+
llvm::Twine("Null or invalid object (" +
66+
llvm::Twine(error.AsCString()) + llvm::Twine(")."))
67+
.str(),
68+
error);
69+
}
70+
71+
return dict_sp;
72+
}
73+
74+
StructuredData::DictionarySP
75+
ScriptedPlatformPythonInterface::GetProcessInfo(lldb::pid_t pid) {
76+
Status error;
77+
StructuredData::DictionarySP dict_sp =
78+
Dispatch<StructuredData::DictionarySP>("get_process_info", error, pid);
79+
80+
if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
81+
return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
82+
LLVM_PRETTY_FUNCTION,
83+
llvm::Twine("Null or invalid object (" +
84+
llvm::Twine(error.AsCString()) + llvm::Twine(")."))
85+
.str(),
86+
error);
87+
}
88+
89+
return dict_sp;
90+
}
91+
92+
Status ScriptedPlatformPythonInterface::AttachToProcess(
93+
ProcessAttachInfoSP attach_info) {
94+
// FIXME: Pass `attach_info` to method call
95+
return GetStatusFromMethod("attach_to_process");
96+
}
97+
98+
Status ScriptedPlatformPythonInterface::LaunchProcess(
99+
ProcessLaunchInfoSP launch_info) {
100+
// FIXME: Pass `launch_info` to method call
101+
return GetStatusFromMethod("launch_process");
102+
}
103+
104+
Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) {
105+
return GetStatusFromMethod("kill_process", pid);
106+
}
107+
108+
#endif // LLDB_ENABLE_PYTHON
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- ScriptedPlatformPythonInterface.h -----------------------*- C++ -*-===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTEDPLATFORMPYTHONINTERFACE_H
10+
#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTEDPLATFORMPYTHONINTERFACE_H
11+
12+
#include "lldb/Host/Config.h"
13+
14+
#if LLDB_ENABLE_PYTHON
15+
16+
#include "ScriptedPythonInterface.h"
17+
#include "lldb/Interpreter/ScriptedPlatformInterface.h"
18+
19+
namespace lldb_private {
20+
class ScriptedPlatformPythonInterface : public ScriptedPlatformInterface,
21+
public ScriptedPythonInterface {
22+
public:
23+
ScriptedPlatformPythonInterface(ScriptInterpreterPythonImpl &interpreter);
24+
25+
StructuredData::GenericSP
26+
CreatePluginObject(const llvm::StringRef class_name,
27+
ExecutionContext &exe_ctx,
28+
StructuredData::DictionarySP args_sp,
29+
StructuredData::Generic *script_obj = nullptr) override;
30+
31+
StructuredData::DictionarySP ListProcesses() override;
32+
33+
StructuredData::DictionarySP GetProcessInfo(lldb::pid_t) override;
34+
35+
Status AttachToProcess(lldb::ProcessAttachInfoSP attach_info) override;
36+
37+
Status LaunchProcess(lldb::ProcessLaunchInfoSP launch_info) override;
38+
39+
Status KillProcess(lldb::pid_t pid) override;
40+
};
41+
} // namespace lldb_private
42+
43+
#endif // LLDB_ENABLE_PYTHON
44+
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTEDPLATFORMPYTHONINTERFACE_H

0 commit comments

Comments
 (0)