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