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