1*5f7ddb14SDimitry Andric //===-- ScriptedProcess.cpp -----------------------------------------------===//
2*5f7ddb14SDimitry Andric //
3*5f7ddb14SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*5f7ddb14SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*5f7ddb14SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*5f7ddb14SDimitry Andric //
7*5f7ddb14SDimitry Andric //===----------------------------------------------------------------------===//
8*5f7ddb14SDimitry Andric
9*5f7ddb14SDimitry Andric #include "ScriptedProcess.h"
10*5f7ddb14SDimitry Andric
11*5f7ddb14SDimitry Andric #include "lldb/Core/Debugger.h"
12*5f7ddb14SDimitry Andric #include "lldb/Core/Module.h"
13*5f7ddb14SDimitry Andric #include "lldb/Core/PluginManager.h"
14*5f7ddb14SDimitry Andric
15*5f7ddb14SDimitry Andric #include "lldb/Host/OptionParser.h"
16*5f7ddb14SDimitry Andric #include "lldb/Host/ThreadLauncher.h"
17*5f7ddb14SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
18*5f7ddb14SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
19*5f7ddb14SDimitry Andric #include "lldb/Interpreter/OptionGroupBoolean.h"
20*5f7ddb14SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
21*5f7ddb14SDimitry Andric #include "lldb/Target/MemoryRegionInfo.h"
22*5f7ddb14SDimitry Andric #include "lldb/Target/RegisterContext.h"
23*5f7ddb14SDimitry Andric
24*5f7ddb14SDimitry Andric #include "lldb/Utility/Log.h"
25*5f7ddb14SDimitry Andric #include "lldb/Utility/Logging.h"
26*5f7ddb14SDimitry Andric #include "lldb/Utility/State.h"
27*5f7ddb14SDimitry Andric
28*5f7ddb14SDimitry Andric #include <mutex>
29*5f7ddb14SDimitry Andric
30*5f7ddb14SDimitry Andric LLDB_PLUGIN_DEFINE(ScriptedProcess)
31*5f7ddb14SDimitry Andric
32*5f7ddb14SDimitry Andric using namespace lldb;
33*5f7ddb14SDimitry Andric using namespace lldb_private;
34*5f7ddb14SDimitry Andric
GetPluginNameStatic()35*5f7ddb14SDimitry Andric ConstString ScriptedProcess::GetPluginNameStatic() {
36*5f7ddb14SDimitry Andric static ConstString g_name("ScriptedProcess");
37*5f7ddb14SDimitry Andric return g_name;
38*5f7ddb14SDimitry Andric }
39*5f7ddb14SDimitry Andric
GetPluginDescriptionStatic()40*5f7ddb14SDimitry Andric const char *ScriptedProcess::GetPluginDescriptionStatic() {
41*5f7ddb14SDimitry Andric return "Scripted Process plug-in.";
42*5f7ddb14SDimitry Andric }
43*5f7ddb14SDimitry Andric
44*5f7ddb14SDimitry Andric static constexpr lldb::ScriptLanguage g_supported_script_languages[] = {
45*5f7ddb14SDimitry Andric ScriptLanguage::eScriptLanguagePython,
46*5f7ddb14SDimitry Andric };
47*5f7ddb14SDimitry Andric
IsScriptLanguageSupported(lldb::ScriptLanguage language)48*5f7ddb14SDimitry Andric bool ScriptedProcess::IsScriptLanguageSupported(lldb::ScriptLanguage language) {
49*5f7ddb14SDimitry Andric llvm::ArrayRef<lldb::ScriptLanguage> supported_languages =
50*5f7ddb14SDimitry Andric llvm::makeArrayRef(g_supported_script_languages);
51*5f7ddb14SDimitry Andric
52*5f7ddb14SDimitry Andric return llvm::is_contained(supported_languages, language);
53*5f7ddb14SDimitry Andric }
54*5f7ddb14SDimitry Andric
CheckInterpreterAndScriptObject() const55*5f7ddb14SDimitry Andric void ScriptedProcess::CheckInterpreterAndScriptObject() const {
56*5f7ddb14SDimitry Andric lldbassert(m_interpreter && "Invalid Script Interpreter.");
57*5f7ddb14SDimitry Andric lldbassert(m_script_object_sp && "Invalid Script Object.");
58*5f7ddb14SDimitry Andric }
59*5f7ddb14SDimitry Andric
CreateInstance(lldb::TargetSP target_sp,lldb::ListenerSP listener_sp,const FileSpec * file,bool can_connect)60*5f7ddb14SDimitry Andric lldb::ProcessSP ScriptedProcess::CreateInstance(lldb::TargetSP target_sp,
61*5f7ddb14SDimitry Andric lldb::ListenerSP listener_sp,
62*5f7ddb14SDimitry Andric const FileSpec *file,
63*5f7ddb14SDimitry Andric bool can_connect) {
64*5f7ddb14SDimitry Andric if (!target_sp ||
65*5f7ddb14SDimitry Andric !IsScriptLanguageSupported(target_sp->GetDebugger().GetScriptLanguage()))
66*5f7ddb14SDimitry Andric return nullptr;
67*5f7ddb14SDimitry Andric
68*5f7ddb14SDimitry Andric Status error;
69*5f7ddb14SDimitry Andric ScriptedProcess::ScriptedProcessInfo scripted_process_info(
70*5f7ddb14SDimitry Andric target_sp->GetProcessLaunchInfo());
71*5f7ddb14SDimitry Andric
72*5f7ddb14SDimitry Andric auto process_sp = std::make_shared<ScriptedProcess>(
73*5f7ddb14SDimitry Andric target_sp, listener_sp, scripted_process_info, error);
74*5f7ddb14SDimitry Andric
75*5f7ddb14SDimitry Andric if (error.Fail() || !process_sp || !process_sp->m_script_object_sp ||
76*5f7ddb14SDimitry Andric !process_sp->m_script_object_sp->IsValid()) {
77*5f7ddb14SDimitry Andric LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS), "%s",
78*5f7ddb14SDimitry Andric error.AsCString());
79*5f7ddb14SDimitry Andric return nullptr;
80*5f7ddb14SDimitry Andric }
81*5f7ddb14SDimitry Andric
82*5f7ddb14SDimitry Andric return process_sp;
83*5f7ddb14SDimitry Andric }
84*5f7ddb14SDimitry Andric
CanDebug(lldb::TargetSP target_sp,bool plugin_specified_by_name)85*5f7ddb14SDimitry Andric bool ScriptedProcess::CanDebug(lldb::TargetSP target_sp,
86*5f7ddb14SDimitry Andric bool plugin_specified_by_name) {
87*5f7ddb14SDimitry Andric return true;
88*5f7ddb14SDimitry Andric }
89*5f7ddb14SDimitry Andric
ScriptedProcess(lldb::TargetSP target_sp,lldb::ListenerSP listener_sp,const ScriptedProcess::ScriptedProcessInfo & scripted_process_info,Status & error)90*5f7ddb14SDimitry Andric ScriptedProcess::ScriptedProcess(
91*5f7ddb14SDimitry Andric lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
92*5f7ddb14SDimitry Andric const ScriptedProcess::ScriptedProcessInfo &scripted_process_info,
93*5f7ddb14SDimitry Andric Status &error)
94*5f7ddb14SDimitry Andric : Process(target_sp, listener_sp),
95*5f7ddb14SDimitry Andric m_scripted_process_info(scripted_process_info) {
96*5f7ddb14SDimitry Andric
97*5f7ddb14SDimitry Andric if (!target_sp) {
98*5f7ddb14SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
99*5f7ddb14SDimitry Andric __FUNCTION__, "Invalid target");
100*5f7ddb14SDimitry Andric return;
101*5f7ddb14SDimitry Andric }
102*5f7ddb14SDimitry Andric
103*5f7ddb14SDimitry Andric m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
104*5f7ddb14SDimitry Andric
105*5f7ddb14SDimitry Andric if (!m_interpreter) {
106*5f7ddb14SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
107*5f7ddb14SDimitry Andric __FUNCTION__,
108*5f7ddb14SDimitry Andric "Debugger has no Script Interpreter");
109*5f7ddb14SDimitry Andric return;
110*5f7ddb14SDimitry Andric }
111*5f7ddb14SDimitry Andric
112*5f7ddb14SDimitry Andric StructuredData::ObjectSP object_sp = GetInterface().CreatePluginObject(
113*5f7ddb14SDimitry Andric m_scripted_process_info.GetClassName().c_str(), target_sp,
114*5f7ddb14SDimitry Andric m_scripted_process_info.GetDictionarySP());
115*5f7ddb14SDimitry Andric
116*5f7ddb14SDimitry Andric if (!object_sp || !object_sp->IsValid()) {
117*5f7ddb14SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
118*5f7ddb14SDimitry Andric __FUNCTION__,
119*5f7ddb14SDimitry Andric "Failed to create valid script object");
120*5f7ddb14SDimitry Andric return;
121*5f7ddb14SDimitry Andric }
122*5f7ddb14SDimitry Andric
123*5f7ddb14SDimitry Andric m_script_object_sp = object_sp;
124*5f7ddb14SDimitry Andric }
125*5f7ddb14SDimitry Andric
~ScriptedProcess()126*5f7ddb14SDimitry Andric ScriptedProcess::~ScriptedProcess() {
127*5f7ddb14SDimitry Andric Clear();
128*5f7ddb14SDimitry Andric // We need to call finalize on the process before destroying ourselves to
129*5f7ddb14SDimitry Andric // make sure all of the broadcaster cleanup goes as planned. If we destruct
130*5f7ddb14SDimitry Andric // this class, then Process::~Process() might have problems trying to fully
131*5f7ddb14SDimitry Andric // destroy the broadcaster.
132*5f7ddb14SDimitry Andric Finalize();
133*5f7ddb14SDimitry Andric }
134*5f7ddb14SDimitry Andric
Initialize()135*5f7ddb14SDimitry Andric void ScriptedProcess::Initialize() {
136*5f7ddb14SDimitry Andric static llvm::once_flag g_once_flag;
137*5f7ddb14SDimitry Andric
138*5f7ddb14SDimitry Andric llvm::call_once(g_once_flag, []() {
139*5f7ddb14SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
140*5f7ddb14SDimitry Andric GetPluginDescriptionStatic(), CreateInstance);
141*5f7ddb14SDimitry Andric });
142*5f7ddb14SDimitry Andric }
143*5f7ddb14SDimitry Andric
Terminate()144*5f7ddb14SDimitry Andric void ScriptedProcess::Terminate() {
145*5f7ddb14SDimitry Andric PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance);
146*5f7ddb14SDimitry Andric }
147*5f7ddb14SDimitry Andric
GetPluginName()148*5f7ddb14SDimitry Andric ConstString ScriptedProcess::GetPluginName() { return GetPluginNameStatic(); }
149*5f7ddb14SDimitry Andric
GetPluginVersion()150*5f7ddb14SDimitry Andric uint32_t ScriptedProcess::GetPluginVersion() { return 1; }
151*5f7ddb14SDimitry Andric
DoLoadCore()152*5f7ddb14SDimitry Andric Status ScriptedProcess::DoLoadCore() {
153*5f7ddb14SDimitry Andric ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo();
154*5f7ddb14SDimitry Andric
155*5f7ddb14SDimitry Andric return DoLaunch(nullptr, launch_info);
156*5f7ddb14SDimitry Andric }
157*5f7ddb14SDimitry Andric
DoLaunch(Module * exe_module,ProcessLaunchInfo & launch_info)158*5f7ddb14SDimitry Andric Status ScriptedProcess::DoLaunch(Module *exe_module,
159*5f7ddb14SDimitry Andric ProcessLaunchInfo &launch_info) {
160*5f7ddb14SDimitry Andric CheckInterpreterAndScriptObject();
161*5f7ddb14SDimitry Andric
162*5f7ddb14SDimitry Andric /* FIXME: This doesn't reflect how lldb actually launches a process.
163*5f7ddb14SDimitry Andric In reality, it attaches to debugserver, then resume the process. */
164*5f7ddb14SDimitry Andric Status error = GetInterface().Launch();
165*5f7ddb14SDimitry Andric SetPrivateState(eStateRunning);
166*5f7ddb14SDimitry Andric
167*5f7ddb14SDimitry Andric if (error.Fail())
168*5f7ddb14SDimitry Andric return error;
169*5f7ddb14SDimitry Andric
170*5f7ddb14SDimitry Andric // TODO: Fetch next state from stopped event queue then send stop event
171*5f7ddb14SDimitry Andric // const StateType state = SetThreadStopInfo(response);
172*5f7ddb14SDimitry Andric // if (state != eStateInvalid) {
173*5f7ddb14SDimitry Andric // SetPrivateState(state);
174*5f7ddb14SDimitry Andric
175*5f7ddb14SDimitry Andric SetPrivateState(eStateStopped);
176*5f7ddb14SDimitry Andric
177*5f7ddb14SDimitry Andric UpdateThreadListIfNeeded();
178*5f7ddb14SDimitry Andric GetThreadList();
179*5f7ddb14SDimitry Andric
180*5f7ddb14SDimitry Andric return {};
181*5f7ddb14SDimitry Andric }
182*5f7ddb14SDimitry Andric
DidLaunch()183*5f7ddb14SDimitry Andric void ScriptedProcess::DidLaunch() {
184*5f7ddb14SDimitry Andric CheckInterpreterAndScriptObject();
185*5f7ddb14SDimitry Andric m_pid = GetInterface().GetProcessID();
186*5f7ddb14SDimitry Andric }
187*5f7ddb14SDimitry Andric
DoResume()188*5f7ddb14SDimitry Andric Status ScriptedProcess::DoResume() {
189*5f7ddb14SDimitry Andric CheckInterpreterAndScriptObject();
190*5f7ddb14SDimitry Andric
191*5f7ddb14SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
192*5f7ddb14SDimitry Andric // FIXME: Fetch data from thread.
193*5f7ddb14SDimitry Andric const StateType thread_resume_state = eStateRunning;
194*5f7ddb14SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s thread_resume_state = %s", __FUNCTION__,
195*5f7ddb14SDimitry Andric StateAsCString(thread_resume_state));
196*5f7ddb14SDimitry Andric
197*5f7ddb14SDimitry Andric bool resume = (thread_resume_state == eStateRunning);
198*5f7ddb14SDimitry Andric assert(thread_resume_state == eStateRunning && "invalid thread resume state");
199*5f7ddb14SDimitry Andric
200*5f7ddb14SDimitry Andric Status error;
201*5f7ddb14SDimitry Andric if (resume) {
202*5f7ddb14SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s sending resume", __FUNCTION__);
203*5f7ddb14SDimitry Andric
204*5f7ddb14SDimitry Andric SetPrivateState(eStateRunning);
205*5f7ddb14SDimitry Andric SetPrivateState(eStateStopped);
206*5f7ddb14SDimitry Andric error = GetInterface().Resume();
207*5f7ddb14SDimitry Andric }
208*5f7ddb14SDimitry Andric
209*5f7ddb14SDimitry Andric return error;
210*5f7ddb14SDimitry Andric }
211*5f7ddb14SDimitry Andric
DoStop()212*5f7ddb14SDimitry Andric Status ScriptedProcess::DoStop() {
213*5f7ddb14SDimitry Andric CheckInterpreterAndScriptObject();
214*5f7ddb14SDimitry Andric
215*5f7ddb14SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
216*5f7ddb14SDimitry Andric
217*5f7ddb14SDimitry Andric if (GetInterface().ShouldStop()) {
218*5f7ddb14SDimitry Andric SetPrivateState(eStateStopped);
219*5f7ddb14SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s Immediate stop", __FUNCTION__);
220*5f7ddb14SDimitry Andric return {};
221*5f7ddb14SDimitry Andric }
222*5f7ddb14SDimitry Andric
223*5f7ddb14SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s Delayed stop", __FUNCTION__);
224*5f7ddb14SDimitry Andric return GetInterface().Stop();
225*5f7ddb14SDimitry Andric }
226*5f7ddb14SDimitry Andric
DoDestroy()227*5f7ddb14SDimitry Andric Status ScriptedProcess::DoDestroy() { return Status(); }
228*5f7ddb14SDimitry Andric
IsAlive()229*5f7ddb14SDimitry Andric bool ScriptedProcess::IsAlive() {
230*5f7ddb14SDimitry Andric if (m_interpreter && m_script_object_sp)
231*5f7ddb14SDimitry Andric return GetInterface().IsAlive();
232*5f7ddb14SDimitry Andric return false;
233*5f7ddb14SDimitry Andric }
234*5f7ddb14SDimitry Andric
DoReadMemory(lldb::addr_t addr,void * buf,size_t size,Status & error)235*5f7ddb14SDimitry Andric size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
236*5f7ddb14SDimitry Andric Status &error) {
237*5f7ddb14SDimitry Andric
238*5f7ddb14SDimitry Andric auto error_with_message = [&error](llvm::StringRef message) {
239*5f7ddb14SDimitry Andric error.SetErrorString(message);
240*5f7ddb14SDimitry Andric return 0;
241*5f7ddb14SDimitry Andric };
242*5f7ddb14SDimitry Andric
243*5f7ddb14SDimitry Andric if (!m_interpreter)
244*5f7ddb14SDimitry Andric return error_with_message("No interpreter.");
245*5f7ddb14SDimitry Andric
246*5f7ddb14SDimitry Andric lldb::DataExtractorSP data_extractor_sp =
247*5f7ddb14SDimitry Andric GetInterface().ReadMemoryAtAddress(addr, size, error);
248*5f7ddb14SDimitry Andric
249*5f7ddb14SDimitry Andric if (!data_extractor_sp || error.Fail())
250*5f7ddb14SDimitry Andric return 0;
251*5f7ddb14SDimitry Andric
252*5f7ddb14SDimitry Andric offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData(
253*5f7ddb14SDimitry Andric 0, data_extractor_sp->GetByteSize(), buf, size, GetByteOrder());
254*5f7ddb14SDimitry Andric
255*5f7ddb14SDimitry Andric if (!bytes_copied || bytes_copied == LLDB_INVALID_OFFSET)
256*5f7ddb14SDimitry Andric return error_with_message("Failed to copy read memory to buffer.");
257*5f7ddb14SDimitry Andric
258*5f7ddb14SDimitry Andric return size;
259*5f7ddb14SDimitry Andric }
260*5f7ddb14SDimitry Andric
GetArchitecture()261*5f7ddb14SDimitry Andric ArchSpec ScriptedProcess::GetArchitecture() {
262*5f7ddb14SDimitry Andric return GetTarget().GetArchitecture();
263*5f7ddb14SDimitry Andric }
264*5f7ddb14SDimitry Andric
GetMemoryRegionInfo(lldb::addr_t load_addr,MemoryRegionInfo & region)265*5f7ddb14SDimitry Andric Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
266*5f7ddb14SDimitry Andric MemoryRegionInfo ®ion) {
267*5f7ddb14SDimitry Andric // TODO: Implement
268*5f7ddb14SDimitry Andric return Status();
269*5f7ddb14SDimitry Andric }
270*5f7ddb14SDimitry Andric
GetMemoryRegions(MemoryRegionInfos & region_list)271*5f7ddb14SDimitry Andric Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos ®ion_list) {
272*5f7ddb14SDimitry Andric CheckInterpreterAndScriptObject();
273*5f7ddb14SDimitry Andric
274*5f7ddb14SDimitry Andric lldb::addr_t address = 0;
275*5f7ddb14SDimitry Andric lldb::MemoryRegionInfoSP mem_region_sp = nullptr;
276*5f7ddb14SDimitry Andric
277*5f7ddb14SDimitry Andric while ((mem_region_sp =
278*5f7ddb14SDimitry Andric GetInterface().GetMemoryRegionContainingAddress(address))) {
279*5f7ddb14SDimitry Andric auto range = mem_region_sp->GetRange();
280*5f7ddb14SDimitry Andric address += range.GetRangeBase() + range.GetByteSize();
281*5f7ddb14SDimitry Andric region_list.push_back(*mem_region_sp.get());
282*5f7ddb14SDimitry Andric }
283*5f7ddb14SDimitry Andric
284*5f7ddb14SDimitry Andric return {};
285*5f7ddb14SDimitry Andric }
286*5f7ddb14SDimitry Andric
Clear()287*5f7ddb14SDimitry Andric void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); }
288*5f7ddb14SDimitry Andric
DoUpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)289*5f7ddb14SDimitry Andric bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
290*5f7ddb14SDimitry Andric ThreadList &new_thread_list) {
291*5f7ddb14SDimitry Andric // TODO: Implement
292*5f7ddb14SDimitry Andric // This is supposed to get the current set of threads, if any of them are in
293*5f7ddb14SDimitry Andric // old_thread_list then they get copied to new_thread_list, and then any
294*5f7ddb14SDimitry Andric // actually new threads will get added to new_thread_list.
295*5f7ddb14SDimitry Andric return new_thread_list.GetSize(false) > 0;
296*5f7ddb14SDimitry Andric }
297*5f7ddb14SDimitry Andric
GetProcessInfo(ProcessInstanceInfo & info)298*5f7ddb14SDimitry Andric bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
299*5f7ddb14SDimitry Andric info.Clear();
300*5f7ddb14SDimitry Andric info.SetProcessID(GetID());
301*5f7ddb14SDimitry Andric info.SetArchitecture(GetArchitecture());
302*5f7ddb14SDimitry Andric lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
303*5f7ddb14SDimitry Andric if (module_sp) {
304*5f7ddb14SDimitry Andric const bool add_exe_file_as_first_arg = false;
305*5f7ddb14SDimitry Andric info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
306*5f7ddb14SDimitry Andric add_exe_file_as_first_arg);
307*5f7ddb14SDimitry Andric }
308*5f7ddb14SDimitry Andric return true;
309*5f7ddb14SDimitry Andric }
310*5f7ddb14SDimitry Andric
GetInterface() const311*5f7ddb14SDimitry Andric ScriptedProcessInterface &ScriptedProcess::GetInterface() const {
312*5f7ddb14SDimitry Andric return m_interpreter->GetScriptedProcessInterface();
313*5f7ddb14SDimitry Andric }
314