2021-03-23 16:22:18 +00:00
|
|
|
//===-- ScriptedProcessPythonInterface.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"
|
2023-03-07 05:36:27 +04:00
|
|
|
#if LLDB_ENABLE_PYTHON
|
|
|
|
|
// LLDB Python header must be included first
|
2023-10-23 09:51:01 -07:00
|
|
|
#include "../lldb-python.h"
|
2023-03-07 05:36:27 +04:00
|
|
|
#endif
|
2023-03-03 15:17:59 -08:00
|
|
|
#include "lldb/Target/Process.h"
|
2021-09-03 17:35:02 +00:00
|
|
|
#include "lldb/Utility/Log.h"
|
2022-11-18 13:53:57 -08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2021-03-23 16:22:18 +00:00
|
|
|
#include "lldb/lldb-enumerations.h"
|
|
|
|
|
|
|
|
|
|
#if LLDB_ENABLE_PYTHON
|
|
|
|
|
|
2023-10-23 09:51:01 -07:00
|
|
|
#include "../SWIGPythonBridge.h"
|
|
|
|
|
#include "../ScriptInterpreterPythonImpl.h"
|
2021-03-23 16:22:18 +00:00
|
|
|
#include "ScriptedProcessPythonInterface.h"
|
2021-10-06 00:09:20 +00:00
|
|
|
#include "ScriptedThreadPythonInterface.h"
|
2023-01-07 13:43:00 -08:00
|
|
|
#include <optional>
|
2021-03-23 16:22:18 +00:00
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
using namespace lldb_private::python;
|
|
|
|
|
using Locker = ScriptInterpreterPythonImpl::Locker;
|
|
|
|
|
|
2021-09-03 17:35:02 +00:00
|
|
|
ScriptedProcessPythonInterface::ScriptedProcessPythonInterface(
|
|
|
|
|
ScriptInterpreterPythonImpl &interpreter)
|
|
|
|
|
: ScriptedProcessInterface(), ScriptedPythonInterface(interpreter) {}
|
|
|
|
|
|
2023-10-25 10:05:54 -07:00
|
|
|
llvm::Expected<StructuredData::GenericSP>
|
|
|
|
|
ScriptedProcessPythonInterface::CreatePluginObject(
|
2021-09-03 17:35:02 +00:00
|
|
|
llvm::StringRef class_name, ExecutionContext &exe_ctx,
|
2022-01-18 12:45:57 +01:00
|
|
|
StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
|
2023-10-25 10:05:54 -07:00
|
|
|
ExecutionContextRefSP exe_ctx_ref_sp =
|
2023-01-11 23:04:24 -08:00
|
|
|
std::make_shared<ExecutionContextRef>(exe_ctx);
|
2023-10-25 10:05:54 -07:00
|
|
|
StructuredDataImpl sd_impl(args_sp);
|
|
|
|
|
return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,
|
|
|
|
|
exe_ctx_ref_sp, sd_impl);
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 16:53:39 -08:00
|
|
|
StructuredData::DictionarySP ScriptedProcessPythonInterface::GetCapabilities() {
|
|
|
|
|
Status error;
|
|
|
|
|
StructuredData::DictionarySP dict =
|
|
|
|
|
Dispatch<StructuredData::DictionarySP>("get_capabilities", error);
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
|
|
|
|
|
error))
|
2023-01-30 16:53:39 -08:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 15:17:59 -08:00
|
|
|
Status
|
|
|
|
|
ScriptedProcessPythonInterface::Attach(const ProcessAttachInfo &attach_info) {
|
|
|
|
|
lldb::ProcessAttachInfoSP attach_info_sp =
|
|
|
|
|
std::make_shared<ProcessAttachInfo>(attach_info);
|
|
|
|
|
return GetStatusFromMethod("attach", attach_info_sp);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 16:22:18 +00:00
|
|
|
Status ScriptedProcessPythonInterface::Launch() {
|
2021-07-20 13:00:54 +00:00
|
|
|
return GetStatusFromMethod("launch");
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Status ScriptedProcessPythonInterface::Resume() {
|
2023-04-14 14:54:01 -07:00
|
|
|
// When calling ScriptedProcess.Resume from lldb we should always stop.
|
|
|
|
|
return GetStatusFromMethod("resume", /*should_stop=*/true);
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-07 14:18:35 -08:00
|
|
|
std::optional<MemoryRegionInfo>
|
2021-03-23 16:22:18 +00:00
|
|
|
ScriptedProcessPythonInterface::GetMemoryRegionContainingAddress(
|
[lldb/Plugins] Add memory region support in ScriptedProcess
This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.
In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
such as the memory region name, address range, permissions ...
This is used when reading memory at some address to compute the offset
in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
to simplify the access to a specific memory region.
With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.
To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.
Differential Revision: https://reviews.llvm.org/D108953
Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 12:25:04 +00:00
|
|
|
lldb::addr_t address, Status &error) {
|
2023-01-07 14:18:35 -08:00
|
|
|
auto mem_region = Dispatch<std::optional<MemoryRegionInfo>>(
|
[lldb/Plugins] Add memory region support in ScriptedProcess
This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.
In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
such as the memory region name, address range, permissions ...
This is used when reading memory at some address to compute the offset
in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
to simplify the access to a specific memory region.
With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.
To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.
Differential Revision: https://reviews.llvm.org/D108953
Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 12:25:04 +00:00
|
|
|
"get_memory_region_containing_address", error, address);
|
|
|
|
|
|
|
|
|
|
if (error.Fail()) {
|
2021-10-08 18:48:02 +00:00
|
|
|
return ErrorWithMessage<MemoryRegionInfo>(LLVM_PRETTY_FUNCTION,
|
[lldb/Plugins] Add memory region support in ScriptedProcess
This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.
In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
such as the memory region name, address range, permissions ...
This is used when reading memory at some address to compute the offset
in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
to simplify the access to a specific memory region.
With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.
To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.
Differential Revision: https://reviews.llvm.org/D108953
Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 12:25:04 +00:00
|
|
|
error.AsCString(), error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mem_region;
|
2022-01-18 12:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StructuredData::DictionarySP ScriptedProcessPythonInterface::GetThreadsInfo() {
|
|
|
|
|
Status error;
|
|
|
|
|
StructuredData::DictionarySP dict =
|
|
|
|
|
Dispatch<StructuredData::DictionarySP>("get_threads_info", error);
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
|
|
|
|
|
error))
|
2022-01-18 12:44:48 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return dict;
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 15:03:15 -07:00
|
|
|
bool ScriptedProcessPythonInterface::CreateBreakpoint(lldb::addr_t addr,
|
|
|
|
|
Status &error) {
|
|
|
|
|
Status py_error;
|
|
|
|
|
StructuredData::ObjectSP obj =
|
|
|
|
|
Dispatch("create_breakpoint", py_error, addr, error);
|
|
|
|
|
|
|
|
|
|
// If there was an error on the python call, surface it to the user.
|
|
|
|
|
if (py_error.Fail())
|
|
|
|
|
error = py_error;
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
|
|
|
|
|
error))
|
2023-04-25 15:03:15 -07:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return obj->GetBooleanValue();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 17:35:02 +00:00
|
|
|
lldb::DataExtractorSP ScriptedProcessPythonInterface::ReadMemoryAtAddress(
|
|
|
|
|
lldb::addr_t address, size_t size, Status &error) {
|
2022-11-18 13:53:57 -08:00
|
|
|
Status py_error;
|
|
|
|
|
lldb::DataExtractorSP data_sp = Dispatch<lldb::DataExtractorSP>(
|
|
|
|
|
"read_memory_at_address", py_error, address, size, error);
|
|
|
|
|
|
|
|
|
|
// If there was an error on the python call, surface it to the user.
|
|
|
|
|
if (py_error.Fail())
|
|
|
|
|
error = py_error;
|
|
|
|
|
|
|
|
|
|
return data_sp;
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 10:26:38 +00:00
|
|
|
lldb::offset_t ScriptedProcessPythonInterface::WriteMemoryAtAddress(
|
2023-03-03 19:30:56 -08:00
|
|
|
lldb::addr_t addr, lldb::DataExtractorSP data_sp, Status &error) {
|
|
|
|
|
Status py_error;
|
|
|
|
|
StructuredData::ObjectSP obj =
|
|
|
|
|
Dispatch("write_memory_at_address", py_error, addr, data_sp, error);
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
|
|
|
|
|
error))
|
2023-03-03 19:30:56 -08:00
|
|
|
return LLDB_INVALID_OFFSET;
|
|
|
|
|
|
|
|
|
|
// If there was an error on the python call, surface it to the user.
|
|
|
|
|
if (py_error.Fail())
|
|
|
|
|
error = py_error;
|
|
|
|
|
|
2023-05-22 13:52:09 -07:00
|
|
|
return obj->GetUnsignedIntegerValue(LLDB_INVALID_OFFSET);
|
2023-03-03 19:30:56 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 11:22:32 -08:00
|
|
|
StructuredData::ArraySP ScriptedProcessPythonInterface::GetLoadedImages() {
|
|
|
|
|
Status error;
|
|
|
|
|
StructuredData::ArraySP array =
|
|
|
|
|
Dispatch<StructuredData::ArraySP>("get_loaded_images", error);
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, array,
|
|
|
|
|
error))
|
2023-01-30 16:53:39 -08:00
|
|
|
return {};
|
2022-03-04 11:22:32 -08:00
|
|
|
|
|
|
|
|
return array;
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lldb::pid_t ScriptedProcessPythonInterface::GetProcessID() {
|
2021-09-03 17:35:02 +00:00
|
|
|
Status error;
|
|
|
|
|
StructuredData::ObjectSP obj = Dispatch("get_process_id", error);
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
|
|
|
|
|
error))
|
2021-09-03 17:35:02 +00:00
|
|
|
return LLDB_INVALID_PROCESS_ID;
|
|
|
|
|
|
2023-05-22 13:52:09 -07:00
|
|
|
return obj->GetUnsignedIntegerValue(LLDB_INVALID_PROCESS_ID);
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ScriptedProcessPythonInterface::IsAlive() {
|
2021-09-03 17:35:02 +00:00
|
|
|
Status error;
|
|
|
|
|
StructuredData::ObjectSP obj = Dispatch("is_alive", error);
|
2021-07-22 20:47:25 +00:00
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
|
|
|
|
|
error))
|
2021-10-06 00:09:20 +00:00
|
|
|
return {};
|
2021-07-22 20:47:25 +00:00
|
|
|
|
2021-09-03 17:35:02 +00:00
|
|
|
return obj->GetBooleanValue();
|
2021-03-23 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-07 14:18:35 -08:00
|
|
|
std::optional<std::string>
|
2021-10-06 00:09:20 +00:00
|
|
|
ScriptedProcessPythonInterface::GetScriptedThreadPluginName() {
|
|
|
|
|
Status error;
|
|
|
|
|
StructuredData::ObjectSP obj = Dispatch("get_scripted_thread_plugin", error);
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
|
|
|
|
|
error))
|
2021-10-06 00:09:20 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return obj->GetStringValue().str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lldb::ScriptedThreadInterfaceSP
|
2022-01-18 12:45:27 +01:00
|
|
|
ScriptedProcessPythonInterface::CreateScriptedThreadInterface() {
|
2023-10-23 09:51:01 -07:00
|
|
|
return m_interpreter.CreateScriptedThreadInterface();
|
2021-10-06 00:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 14:27:28 -07:00
|
|
|
StructuredData::DictionarySP ScriptedProcessPythonInterface::GetMetadata() {
|
|
|
|
|
Status error;
|
|
|
|
|
StructuredData::DictionarySP dict =
|
|
|
|
|
Dispatch<StructuredData::DictionarySP>("get_process_metadata", error);
|
|
|
|
|
|
2024-05-23 01:25:48 -07:00
|
|
|
if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
|
|
|
|
|
error))
|
2022-11-03 14:27:28 -07:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 16:22:18 +00:00
|
|
|
#endif
|