Skip to content

Commit 4679676

Browse files
committed
[lldb/Plugins] Add ScriptedProcess Process Plugin
This patch introduces Scripted Processes to lldb. The goal, here, is to be able to attach in the debugger to fake processes that are backed by script files (in Python, Lua, Swift, etc ...) and inspect them statically. Scripted Processes can be used in cooperative multithreading environments like the XNU Kernel or other real-time operating systems, but it can also help us improve the debugger testing infrastructure by writting synthetic tests that simulates hard-to-reproduce process/thread states. Although ScriptedProcess is not feature-complete at the moment, it has basic execution capabilities and will improve in the following patches. rdar://65508855 Differential Revision: https://reviews.llvm.org/D95713 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 2cff3de commit 4679676

File tree

8 files changed

+430
-4
lines changed

8 files changed

+430
-4
lines changed

lldb/bindings/python/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ function(finish_swig_python swig_target lldb_python_bindings_dir lldb_python_tar
111111
FILES
112112
"${LLDB_SOURCE_DIR}/examples/python/scripted_process/scripted_process.py")
113113

114+
create_python_package(
115+
${swig_target}
116+
${lldb_python_target_dir}
117+
"plugins"
118+
FILES
119+
"${LLDB_SOURCE_DIR}/examples/python/scripted_process/scripted_process.py")
120+
114121
if(APPLE)
115122
create_python_package(
116123
${swig_target}

lldb/include/lldb/Target/Process.h

-2
Original file line numberDiff line numberDiff line change
@@ -2561,8 +2561,6 @@ void PruneThreadPlans();
25612561
virtual size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
25622562
Status &error) = 0;
25632563

2564-
void SetState(lldb::EventSP &event_sp);
2565-
25662564
lldb::StateType GetPrivateState();
25672565

25682566
/// The "private" side of resuming a process. This doesn't alter the state

lldb/source/Plugins/Process/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
1212
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
1313
add_subdirectory(MacOSX-Kernel)
1414
endif()
15+
add_subdirectory(scripted)
1516
add_subdirectory(gdb-remote)
1617
add_subdirectory(Utility)
1718
add_subdirectory(elf-core)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_lldb_library(lldbPluginScriptedProcess PLUGIN
2+
ScriptedProcess.cpp
3+
4+
LINK_LIBS
5+
lldbCore
6+
lldbTarget
7+
lldbUtility
8+
lldbPluginProcessUtility
9+
LINK_COMPONENTS
10+
BinaryFormat
11+
Object
12+
Support
13+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
//===-- ScriptedProcess.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 "ScriptedProcess.h"
10+
11+
#include "lldb/Core/Debugger.h"
12+
#include "lldb/Core/Module.h"
13+
#include "lldb/Core/PluginManager.h"
14+
15+
#include "lldb/Host/OptionParser.h"
16+
17+
#include "lldb/Interpreter/OptionArgParser.h"
18+
#include "lldb/Interpreter/OptionGroupBoolean.h"
19+
#include "lldb/Interpreter/ScriptInterpreter.h"
20+
#include "lldb/Target/MemoryRegionInfo.h"
21+
22+
LLDB_PLUGIN_DEFINE(ScriptedProcess)
23+
24+
using namespace lldb;
25+
using namespace lldb_private;
26+
27+
ConstString ScriptedProcess::GetPluginNameStatic() {
28+
static ConstString g_name("ScriptedProcess");
29+
return g_name;
30+
}
31+
32+
const char *ScriptedProcess::GetPluginDescriptionStatic() {
33+
return "Scripted Process plug-in.";
34+
}
35+
36+
lldb::ProcessSP ScriptedProcess::CreateInstance(lldb::TargetSP target_sp,
37+
lldb::ListenerSP listener_sp,
38+
const FileSpec *file,
39+
bool can_connect) {
40+
ScriptedProcess::LaunchInfo launch_info(target_sp->GetProcessLaunchInfo());
41+
42+
auto process_sp =
43+
std::make_shared<ScriptedProcess>(target_sp, listener_sp, launch_info);
44+
45+
if (!process_sp || !process_sp->m_script_object_sp ||
46+
!process_sp->m_script_object_sp->IsValid())
47+
return nullptr;
48+
49+
return process_sp;
50+
}
51+
52+
bool ScriptedProcess::CanDebug(lldb::TargetSP target_sp,
53+
bool plugin_specified_by_name) {
54+
return true;
55+
}
56+
57+
ScriptedProcess::ScriptedProcess(lldb::TargetSP target_sp,
58+
lldb::ListenerSP listener_sp,
59+
const ScriptedProcess::LaunchInfo &launch_info)
60+
: Process(target_sp, listener_sp), m_launch_info(launch_info),
61+
m_interpreter(nullptr), m_script_object_sp(nullptr) {
62+
if (!target_sp)
63+
return;
64+
65+
m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
66+
67+
if (!m_interpreter)
68+
return;
69+
70+
StructuredData::ObjectSP object_sp = GetInterface().CreatePluginObject(
71+
m_launch_info.GetClassName().c_str(), target_sp,
72+
m_launch_info.GetDictionarySP());
73+
74+
if (object_sp && object_sp->IsValid())
75+
m_script_object_sp = object_sp;
76+
}
77+
78+
ScriptedProcess::~ScriptedProcess() {
79+
Clear();
80+
// We need to call finalize on the process before destroying ourselves to
81+
// make sure all of the broadcaster cleanup goes as planned. If we destruct
82+
// this class, then Process::~Process() might have problems trying to fully
83+
// destroy the broadcaster.
84+
Finalize();
85+
}
86+
87+
void ScriptedProcess::Initialize() {
88+
static llvm::once_flag g_once_flag;
89+
90+
llvm::call_once(g_once_flag, []() {
91+
PluginManager::RegisterPlugin(GetPluginNameStatic(),
92+
GetPluginDescriptionStatic(), CreateInstance);
93+
});
94+
}
95+
96+
void ScriptedProcess::Terminate() {
97+
PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance);
98+
}
99+
100+
ConstString ScriptedProcess::GetPluginName() { return GetPluginNameStatic(); }
101+
102+
uint32_t ScriptedProcess::GetPluginVersion() { return 1; }
103+
104+
Status ScriptedProcess::DoLoadCore() {
105+
ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo();
106+
107+
return DoLaunch(nullptr, launch_info);
108+
}
109+
110+
Status ScriptedProcess::DoLaunch(Module *exe_module,
111+
ProcessLaunchInfo &launch_info) {
112+
if (!m_interpreter)
113+
return Status("No interpreter.");
114+
115+
if (!m_script_object_sp)
116+
return Status("No python object.");
117+
118+
Status status = GetInterface().Launch();
119+
120+
if (status.Success()) {
121+
SetPrivateState(eStateRunning);
122+
SetPrivateState(eStateStopped);
123+
}
124+
125+
return status;
126+
};
127+
128+
void ScriptedProcess::DidLaunch() {
129+
if (m_interpreter)
130+
m_pid = GetInterface().GetProcessID();
131+
}
132+
133+
Status ScriptedProcess::DoResume() {
134+
if (!m_interpreter)
135+
return Status("No interpreter.");
136+
137+
if (!m_script_object_sp)
138+
return Status("No python object.");
139+
140+
Status status = GetInterface().Resume();
141+
142+
if (status.Success()) {
143+
SetPrivateState(eStateRunning);
144+
SetPrivateState(eStateStopped);
145+
}
146+
147+
return status;
148+
}
149+
150+
Status ScriptedProcess::DoDestroy() { return Status(); }
151+
152+
bool ScriptedProcess::IsAlive() {
153+
if (!m_interpreter)
154+
return false;
155+
156+
return GetInterface().IsAlive();
157+
}
158+
159+
size_t ScriptedProcess::ReadMemory(lldb::addr_t addr, void *buf, size_t size,
160+
Status &error) {
161+
return DoReadMemory(addr, buf, size, error);
162+
}
163+
164+
size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
165+
Status &error) {
166+
167+
auto error_with_message = [&error](llvm::StringRef message) {
168+
error.SetErrorString(message);
169+
return LLDB_INVALID_ADDRESS;
170+
};
171+
172+
if (!m_interpreter)
173+
return error_with_message("No interpreter.");
174+
175+
lldb::DataExtractorSP data_extractor_sp =
176+
GetInterface().ReadMemoryAtAddress(addr, size, error);
177+
178+
if (!data_extractor_sp || error.Fail())
179+
return LLDB_INVALID_ADDRESS;
180+
181+
if (data_extractor_sp->GetByteSize() != size)
182+
return error_with_message("Failed to read requested memory size.");
183+
184+
if (data_extractor_sp->CopyData(0, size, buf) <= size)
185+
return error_with_message("Failed to copy read memory to buffer.");
186+
187+
return size;
188+
}
189+
190+
ArchSpec ScriptedProcess::GetArchitecture() {
191+
return GetTarget().GetArchitecture();
192+
}
193+
194+
Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
195+
MemoryRegionInfo &region) {
196+
return Status();
197+
}
198+
199+
Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos &region_list) {
200+
Status error;
201+
202+
if (!m_interpreter) {
203+
error.SetErrorString("No interpreter.");
204+
return error;
205+
}
206+
207+
lldb::addr_t address = 0;
208+
lldb::MemoryRegionInfoSP mem_region_sp = nullptr;
209+
210+
while ((mem_region_sp =
211+
GetInterface().GetMemoryRegionContainingAddress(address))) {
212+
auto range = mem_region_sp->GetRange();
213+
address += range.GetRangeBase() + range.GetByteSize();
214+
region_list.push_back(*mem_region_sp.get());
215+
}
216+
217+
return error;
218+
}
219+
220+
void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); }
221+
222+
bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
223+
ThreadList &new_thread_list) {
224+
return new_thread_list.GetSize(false) > 0;
225+
}
226+
227+
bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
228+
info.Clear();
229+
info.SetProcessID(GetID());
230+
info.SetArchitecture(GetArchitecture());
231+
lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
232+
if (module_sp) {
233+
const bool add_exe_file_as_first_arg = false;
234+
info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
235+
add_exe_file_as_first_arg);
236+
}
237+
return true;
238+
}
239+
240+
ScriptedProcessInterface &ScriptedProcess::GetInterface() const {
241+
return m_interpreter->GetScriptedProcessInterface();
242+
}

0 commit comments

Comments
 (0)