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