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