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   StructuredData::ObjectSP object_sp = GetInterface().CreatePluginObject(
113       m_scripted_process_info.GetClassName().c_str(), target_sp,
114       m_scripted_process_info.GetDictionarySP());
115 
116   if (!object_sp || !object_sp->IsValid()) {
117     error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
118                                    __FUNCTION__,
119                                    "Failed to create valid script object");
120     return;
121   }
122 
123   m_script_object_sp = object_sp;
124 }
125 
126 ScriptedProcess::~ScriptedProcess() {
127   Clear();
128   // We need to call finalize on the process before destroying ourselves to
129   // make sure all of the broadcaster cleanup goes as planned. If we destruct
130   // this class, then Process::~Process() might have problems trying to fully
131   // destroy the broadcaster.
132   Finalize();
133 }
134 
135 void ScriptedProcess::Initialize() {
136   static llvm::once_flag g_once_flag;
137 
138   llvm::call_once(g_once_flag, []() {
139     PluginManager::RegisterPlugin(GetPluginNameStatic(),
140                                   GetPluginDescriptionStatic(), CreateInstance);
141   });
142 }
143 
144 void ScriptedProcess::Terminate() {
145   PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance);
146 }
147 
148 ConstString ScriptedProcess::GetPluginName() { return GetPluginNameStatic(); }
149 
150 uint32_t ScriptedProcess::GetPluginVersion() { return 1; }
151 
152 Status ScriptedProcess::DoLoadCore() {
153   ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo();
154 
155   return DoLaunch(nullptr, launch_info);
156 }
157 
158 Status ScriptedProcess::DoLaunch(Module *exe_module,
159                                  ProcessLaunchInfo &launch_info) {
160   CheckInterpreterAndScriptObject();
161 
162   /* FIXME: This doesn't reflect how lldb actually launches a process.
163            In reality, it attaches to debugserver, then resume the process. */
164   Status error = GetInterface().Launch();
165   SetPrivateState(eStateRunning);
166 
167   if (error.Fail())
168     return error;
169 
170   // TODO: Fetch next state from stopped event queue then send stop event
171   //  const StateType state = SetThreadStopInfo(response);
172   //  if (state != eStateInvalid) {
173   //    SetPrivateState(state);
174 
175   SetPrivateState(eStateStopped);
176 
177   UpdateThreadListIfNeeded();
178   GetThreadList();
179 
180   return {};
181 }
182 
183 void ScriptedProcess::DidLaunch() {
184   CheckInterpreterAndScriptObject();
185   m_pid = GetInterface().GetProcessID();
186 }
187 
188 Status ScriptedProcess::DoResume() {
189   CheckInterpreterAndScriptObject();
190 
191   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
192   // FIXME: Fetch data from thread.
193   const StateType thread_resume_state = eStateRunning;
194   LLDB_LOGF(log, "ScriptedProcess::%s thread_resume_state = %s", __FUNCTION__,
195             StateAsCString(thread_resume_state));
196 
197   bool resume = (thread_resume_state == eStateRunning);
198   assert(thread_resume_state == eStateRunning && "invalid thread resume state");
199 
200   Status error;
201   if (resume) {
202     LLDB_LOGF(log, "ScriptedProcess::%s sending resume", __FUNCTION__);
203 
204     SetPrivateState(eStateRunning);
205     SetPrivateState(eStateStopped);
206     error = GetInterface().Resume();
207   }
208 
209   return error;
210 }
211 
212 Status ScriptedProcess::DoStop() {
213   CheckInterpreterAndScriptObject();
214 
215   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
216 
217   if (GetInterface().ShouldStop()) {
218     SetPrivateState(eStateStopped);
219     LLDB_LOGF(log, "ScriptedProcess::%s Immediate stop", __FUNCTION__);
220     return {};
221   }
222 
223   LLDB_LOGF(log, "ScriptedProcess::%s Delayed stop", __FUNCTION__);
224   return GetInterface().Stop();
225 }
226 
227 Status ScriptedProcess::DoDestroy() { return Status(); }
228 
229 bool ScriptedProcess::IsAlive() {
230   if (m_interpreter && m_script_object_sp)
231     return GetInterface().IsAlive();
232   return false;
233 }
234 
235 size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
236                                      Status &error) {
237 
238   auto error_with_message = [&error](llvm::StringRef message) {
239     error.SetErrorString(message);
240     return 0;
241   };
242 
243   if (!m_interpreter)
244     return error_with_message("No interpreter.");
245 
246   lldb::DataExtractorSP data_extractor_sp =
247       GetInterface().ReadMemoryAtAddress(addr, size, error);
248 
249   if (!data_extractor_sp || error.Fail())
250     return 0;
251 
252   offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData(
253       0, data_extractor_sp->GetByteSize(), buf, size, GetByteOrder());
254 
255   if (!bytes_copied || bytes_copied == LLDB_INVALID_OFFSET)
256     return error_with_message("Failed to copy read memory to buffer.");
257 
258   return size;
259 }
260 
261 ArchSpec ScriptedProcess::GetArchitecture() {
262   return GetTarget().GetArchitecture();
263 }
264 
265 Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
266                                             MemoryRegionInfo &region) {
267   // TODO: Implement
268   return Status();
269 }
270 
271 Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos &region_list) {
272   CheckInterpreterAndScriptObject();
273 
274   lldb::addr_t address = 0;
275   lldb::MemoryRegionInfoSP mem_region_sp = nullptr;
276 
277   while ((mem_region_sp =
278               GetInterface().GetMemoryRegionContainingAddress(address))) {
279     auto range = mem_region_sp->GetRange();
280     address += range.GetRangeBase() + range.GetByteSize();
281     region_list.push_back(*mem_region_sp.get());
282   }
283 
284   return {};
285 }
286 
287 void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); }
288 
289 bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
290                                          ThreadList &new_thread_list) {
291   // TODO: Implement
292   // This is supposed to get the current set of threads, if any of them are in
293   // old_thread_list then they get copied to new_thread_list, and then any
294   // actually new threads will get added to new_thread_list.
295   return new_thread_list.GetSize(false) > 0;
296 }
297 
298 bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
299   info.Clear();
300   info.SetProcessID(GetID());
301   info.SetArchitecture(GetArchitecture());
302   lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
303   if (module_sp) {
304     const bool add_exe_file_as_first_arg = false;
305     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
306                            add_exe_file_as_first_arg);
307   }
308   return true;
309 }
310 
311 ScriptedProcessInterface &ScriptedProcess::GetInterface() const {
312   return m_interpreter->GetScriptedProcessInterface();
313 }
314