Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lldb/bindings/python/python-wrapper.swig
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,18 @@ void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBEvent(PyObject * data
return sb_ptr;
}

void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBModule(PyObject * data) {
lldb::SBModule *sb_ptr = nullptr;

int valid_cast =
SWIG_ConvertPtr(data, (void **)&sb_ptr, SWIGTYPE_p_lldb__SBModule, 0);

if (valid_cast == -1)
return NULL;

return sb_ptr;
}

void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBStream(PyObject * data) {
lldb::SBStream *sb_ptr = nullptr;

Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class LLDB_API SBModule {
friend class SBType;

friend class lldb_private::python::SWIGBridge;
friend class lldb_private::ScriptInterpreter;

explicit SBModule(const lldb::ModuleSP &module_sp);

Expand Down
30 changes: 30 additions & 0 deletions lldb/include/lldb/Interpreter/Interfaces/JITLoaderInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- JITLoaderInterface.h ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_INTERPRETER_INTERFACES_JITLOADERINTERFACE_H
#define LLDB_INTERPRETER_INTERFACES_JITLOADERINTERFACE_H

#include "ScriptedThreadInterface.h"
#include "lldb/lldb-private.h"

namespace lldb_private {
class JITLoaderInterface : virtual public ScriptedInterface {
public:

virtual llvm::Expected<StructuredData::GenericSP>
CreatePluginObject(llvm::StringRef class_name,
lldb_private::ExecutionContext &exe_ctx) = 0;

virtual void DidAttach() {};
virtual void DidLaunch() {};
virtual void ModulesDidLoad(lldb_private::ModuleList &module_list) {};

};
} // namespace lldb_private

#endif // LLDB_INTERPRETER_INTERFACES_JITLOADERINTERFACE_H
8 changes: 8 additions & 0 deletions lldb/include/lldb/Interpreter/ScriptInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "lldb/API/SBEvent.h"
#include "lldb/API/SBExecutionContext.h"
#include "lldb/API/SBLaunchInfo.h"
#include "lldb/API/SBModule.h"
#include "lldb/API/SBMemoryRegionInfo.h"
#include "lldb/API/SBStream.h"
#include "lldb/Breakpoint/BreakpointOptions.h"
Expand All @@ -24,6 +25,7 @@
#include "lldb/Core/ThreadedCommunication.h"
#include "lldb/Host/PseudoTerminal.h"
#include "lldb/Host/StreamFile.h"
#include "lldb/Interpreter/Interfaces/JITLoaderInterface.h"
#include "lldb/Interpreter/Interfaces/OperatingSystemInterface.h"
#include "lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h"
#include "lldb/Interpreter/Interfaces/ScriptedProcessInterface.h"
Expand Down Expand Up @@ -560,6 +562,10 @@ class ScriptInterpreter : public PluginInterface {
return {};
}

virtual lldb::JITLoaderInterfaceSP CreateJITLoaderInterface() {
return {};
}

virtual lldb::ScriptedPlatformInterfaceUP GetScriptedPlatformInterface() {
return {};
}
Expand Down Expand Up @@ -597,6 +603,8 @@ class ScriptInterpreter : public PluginInterface {
lldb::ExecutionContextRefSP GetOpaqueTypeFromSBExecutionContext(
const lldb::SBExecutionContext &exe_ctx) const;

lldb::ModuleSP GetOpaqueTypeFromSBModule(const lldb::SBModule &module) const;

protected:
Debugger &m_debugger;
lldb::ScriptLanguage m_script_lang;
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/Target/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ProcessProperties : public Properties {
Args GetExtraStartupCommands() const;
void SetExtraStartupCommands(const Args &args);
FileSpec GetPythonOSPluginPath() const;
FileSpec GetPythonJITLoaderPath() const;
uint32_t GetVirtualAddressableBits() const;
void SetVirtualAddressableBits(uint32_t bits);
uint32_t GetHighmemVirtualAddressableBits() const;
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/lldb-forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Instruction;
class InstructionList;
class InstrumentationRuntime;
class JITLoader;
class JITLoaderInterface;
class JITLoaderList;
class Language;
class LanguageCategory;
Expand Down Expand Up @@ -364,6 +365,7 @@ typedef std::shared_ptr<lldb_private::IOHandler> IOHandlerSP;
typedef std::shared_ptr<lldb_private::IOObject> IOObjectSP;
typedef std::shared_ptr<lldb_private::IRExecutionUnit> IRExecutionUnitSP;
typedef std::shared_ptr<lldb_private::JITLoader> JITLoaderSP;
typedef std::shared_ptr<lldb_private::JITLoaderInterface> JITLoaderInterfaceSP;
typedef std::unique_ptr<lldb_private::JITLoaderList> JITLoaderListUP;
typedef std::shared_ptr<lldb_private::LanguageRuntime> LanguageRuntimeSP;
typedef std::unique_ptr<lldb_private::SystemRuntime> SystemRuntimeUP;
Expand Down
6 changes: 6 additions & 0 deletions lldb/source/Interpreter/ScriptInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ ScriptInterpreter::GetOpaqueTypeFromSBExecutionContext(
return exe_ctx.m_exe_ctx_sp;
}

lldb::ModuleSP
ScriptInterpreter::GetOpaqueTypeFromSBModule(
const lldb::SBModule &module) const {
return module.m_opaque_sp;
}

lldb::ScriptLanguage
ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Plugins/JITLoader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(GDB)
add_subdirectory(Python)
11 changes: 11 additions & 0 deletions lldb/source/Plugins/JITLoader/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_lldb_library(lldbPluginJITLoaderPython PLUGIN
JITLoaderPython.cpp

LINK_LIBS
lldbCore
lldbInterpreter
lldbSymbol
lldbTarget
lldbValueObject
lldbPluginProcessUtility
)
146 changes: 146 additions & 0 deletions lldb/source/Plugins/JITLoader/Python/JITLoaderPython.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//===-- JITLoaderPython.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/Config.h"

#if LLDB_ENABLE_PYTHON

#include "JITLoaderPython.h"

#include "Plugins/Process/Utility/RegisterContextDummy.h"
#include "Plugins/Process/Utility/RegisterContextMemory.h"
#include "Plugins/Process/Utility/ThreadMemory.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/ScriptInterpreter.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadList.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/ValueObject/ValueObjectVariable.h"

#include <memory>

using namespace lldb;
using namespace lldb_private;

LLDB_PLUGIN_DEFINE(JITLoaderPython)

void JITLoaderPython::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance,
nullptr);
}

void JITLoaderPython::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}

JITLoaderSP JITLoaderPython::CreateInstance(Process *process, bool force) {
// Python JITLoader plug-ins must be requested by name, so force must
// be true
FileSpec python_os_plugin_spec(process->GetPythonJITLoaderPath());
if (python_os_plugin_spec &&
FileSystem::Instance().Exists(python_os_plugin_spec)) {
std::unique_ptr<JITLoaderPython> os_up(
new JITLoaderPython(process, python_os_plugin_spec));
if (os_up.get() && os_up->IsValid())
return os_up;
}
return nullptr;
}

llvm::StringRef JITLoaderPython::GetPluginDescriptionStatic() {
return "JIT loader plug-in that implements a JIT loader using a python "
"class that implements the necessary JITLoader functionality.";
}

JITLoaderPython::JITLoaderPython(lldb_private::Process *process,
const FileSpec &python_module_path)
: JITLoader(process), m_interpreter(nullptr), m_script_object_sp() {
if (!process)
return;
TargetSP target_sp = process->CalculateTarget();
if (!target_sp)
return;
m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
if (!m_interpreter)
return;

std::string os_plugin_class_name(
python_module_path.GetFilename().AsCString(""));
if (os_plugin_class_name.empty())
return;

LoadScriptOptions options;
char python_module_path_cstr[PATH_MAX];
python_module_path.GetPath(python_module_path_cstr,
sizeof(python_module_path_cstr));
Status error;
if (!m_interpreter->LoadScriptingModule(python_module_path_cstr, options,
error))
return;

// Strip the ".py" extension if there is one
size_t py_extension_pos = os_plugin_class_name.rfind(".py");
if (py_extension_pos != std::string::npos)
os_plugin_class_name.erase(py_extension_pos);
// Add ".JITLoaderPlugin" to the module name to get a string like
// "modulename.JITLoaderPlugin"
os_plugin_class_name += ".JITLoaderPlugin";

JITLoaderInterfaceSP interface_sp =
m_interpreter->CreateJITLoaderInterface();
if (!interface_sp)
return;

ExecutionContext exe_ctx(process);
auto obj_or_err = interface_sp->CreatePluginObject(
os_plugin_class_name, exe_ctx);

if (!obj_or_err) {
llvm::consumeError(obj_or_err.takeError());
return;
}

StructuredData::GenericSP owned_script_object_sp = *obj_or_err;
if (!owned_script_object_sp->IsValid())
return;

m_script_object_sp = owned_script_object_sp;
m_interface_sp = interface_sp;
}

JITLoaderPython::~JITLoaderPython() = default;

void JITLoaderPython::DidAttach() {
if (m_interface_sp)
m_interface_sp->DidAttach();
}

void JITLoaderPython::DidLaunch() {
if (m_interface_sp)
m_interface_sp->DidLaunch();
}

void JITLoaderPython::ModulesDidLoad(lldb_private::ModuleList &module_list) {
if (m_interface_sp)
m_interface_sp->ModulesDidLoad(module_list);
}

#endif // #if LLDB_ENABLE_PYTHON
62 changes: 62 additions & 0 deletions lldb/source/Plugins/JITLoader/Python/JITLoaderPython.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//===-- JITLoaderPython.h ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_JITLoaderPython_h_
#define liblldb_JITLoaderPython_h_

#include "lldb/Host/Config.h"

#if LLDB_ENABLE_PYTHON

#include "lldb/Target/JITLoader.h"
#include "lldb/Utility/StructuredData.h"

namespace lldb_private {
class ScriptInterpreter;
}

class JITLoaderPython : public lldb_private::JITLoader {
public:
JITLoaderPython(lldb_private::Process *process,
const lldb_private::FileSpec &python_module_path);

~JITLoaderPython() override;

// Static Functions
static lldb::JITLoaderSP CreateInstance(lldb_private::Process *process,
bool force);

static void Initialize();

static void Terminate();

static llvm::StringRef GetPluginNameStatic() { return "python"; }

static llvm::StringRef GetPluginDescriptionStatic();

// lldb_private::PluginInterface Methods
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }

// lldb_private::JITLoader Methods
void DidAttach() override;
void DidLaunch() override;
void ModulesDidLoad(lldb_private::ModuleList &module_list) override;

protected:
bool IsValid() const {
return m_script_object_sp && m_script_object_sp->IsValid();
}

lldb_private::ScriptInterpreter *m_interpreter = nullptr;
lldb::JITLoaderInterfaceSP m_interface_sp;
lldb_private::StructuredData::GenericSP m_script_object_sp;
};

#endif // LLDB_ENABLE_PYTHON

#endif // liblldb_JITLoaderPython_h_
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (LLDB_ENABLE_LIBEDIT)
endif()

add_lldb_library(lldbPluginScriptInterpreterPythonInterfaces PLUGIN
JITLoaderPythonInterface.cpp
OperatingSystemPythonInterface.cpp
ScriptInterpreterPythonInterfaces.cpp
ScriptedPlatformPythonInterface.cpp
Expand All @@ -40,5 +41,3 @@ add_lldb_library(lldbPluginScriptInterpreterPythonInterfaces PLUGIN
LINK_COMPONENTS
Support
)


Loading
Loading