xref: /llvm-project/lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp (revision 680ca7f21a7716698227966a9e70e7efb75cff7e)
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/LLDBLog.h"
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(GetLog(LLDBLog::Process), "%s", error.AsCString());
71     return nullptr;
72   }
73 
74   return process_sp;
75 }
76 
77 bool ScriptedProcess::CanDebug(lldb::TargetSP target_sp,
78                                bool plugin_specified_by_name) {
79   return true;
80 }
81 
82 ScriptedProcess::ScriptedProcess(
83     lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
84     const ScriptedProcess::ScriptedProcessInfo &scripted_process_info,
85     Status &error)
86     : Process(target_sp, listener_sp),
87       m_scripted_process_info(scripted_process_info) {
88 
89   if (!target_sp) {
90     error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
91                                    __FUNCTION__, "Invalid target");
92     return;
93   }
94 
95   m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
96 
97   if (!m_interpreter) {
98     error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
99                                    __FUNCTION__,
100                                    "Debugger has no Script Interpreter");
101     return;
102   }
103 
104   ExecutionContext exe_ctx(target_sp, /*get_process=*/false);
105 
106   StructuredData::GenericSP object_sp = GetInterface().CreatePluginObject(
107       m_scripted_process_info.GetClassName().c_str(), exe_ctx,
108       m_scripted_process_info.GetArgsSP());
109 
110   if (!object_sp || !object_sp->IsValid()) {
111     error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
112                                    __FUNCTION__,
113                                    "Failed to create valid script object");
114     return;
115   }
116 
117   m_script_object_sp = object_sp;
118 }
119 
120 ScriptedProcess::~ScriptedProcess() {
121   Clear();
122   // We need to call finalize on the process before destroying ourselves to
123   // make sure all of the broadcaster cleanup goes as planned. If we destruct
124   // this class, then Process::~Process() might have problems trying to fully
125   // destroy the broadcaster.
126   Finalize();
127 }
128 
129 void ScriptedProcess::Initialize() {
130   static llvm::once_flag g_once_flag;
131 
132   llvm::call_once(g_once_flag, []() {
133     PluginManager::RegisterPlugin(GetPluginNameStatic(),
134                                   GetPluginDescriptionStatic(), CreateInstance);
135   });
136 }
137 
138 void ScriptedProcess::Terminate() {
139   PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance);
140 }
141 
142 Status ScriptedProcess::DoLoadCore() {
143   ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo();
144 
145   return DoLaunch(nullptr, launch_info);
146 }
147 
148 Status ScriptedProcess::DoLaunch(Module *exe_module,
149                                  ProcessLaunchInfo &launch_info) {
150   CheckInterpreterAndScriptObject();
151 
152   /* FIXME: This doesn't reflect how lldb actually launches a process.
153            In reality, it attaches to debugserver, then resume the process. */
154   Status error = GetInterface().Launch();
155   SetPrivateState(eStateRunning);
156 
157   if (error.Fail())
158     return error;
159 
160   // TODO: Fetch next state from stopped event queue then send stop event
161   //  const StateType state = SetThreadStopInfo(response);
162   //  if (state != eStateInvalid) {
163   //    SetPrivateState(state);
164 
165   SetPrivateState(eStateStopped);
166 
167   return {};
168 }
169 
170 void ScriptedProcess::DidLaunch() {
171   CheckInterpreterAndScriptObject();
172   m_pid = GetInterface().GetProcessID();
173   GetLoadedDynamicLibrariesInfos();
174 }
175 
176 Status ScriptedProcess::DoResume() {
177   CheckInterpreterAndScriptObject();
178 
179   Log *log = GetLog(LLDBLog::Process);
180   // FIXME: Fetch data from thread.
181   const StateType thread_resume_state = eStateRunning;
182   LLDB_LOGF(log, "ScriptedProcess::%s thread_resume_state = %s", __FUNCTION__,
183             StateAsCString(thread_resume_state));
184 
185   bool resume = (thread_resume_state == eStateRunning);
186   assert(thread_resume_state == eStateRunning && "invalid thread resume state");
187 
188   Status error;
189   if (resume) {
190     LLDB_LOGF(log, "ScriptedProcess::%s sending resume", __FUNCTION__);
191 
192     SetPrivateState(eStateRunning);
193     SetPrivateState(eStateStopped);
194     error = GetInterface().Resume();
195   }
196 
197   return error;
198 }
199 
200 Status ScriptedProcess::DoStop() {
201   CheckInterpreterAndScriptObject();
202 
203   Log *log = GetLog(LLDBLog::Process);
204 
205   if (GetInterface().ShouldStop()) {
206     SetPrivateState(eStateStopped);
207     LLDB_LOGF(log, "ScriptedProcess::%s Immediate stop", __FUNCTION__);
208     return {};
209   }
210 
211   LLDB_LOGF(log, "ScriptedProcess::%s Delayed stop", __FUNCTION__);
212   return GetInterface().Stop();
213 }
214 
215 Status ScriptedProcess::DoDestroy() { return Status(); }
216 
217 bool ScriptedProcess::IsAlive() {
218   if (m_interpreter && m_script_object_sp)
219     return GetInterface().IsAlive();
220   return false;
221 }
222 
223 size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
224                                      Status &error) {
225   if (!m_interpreter)
226     return ScriptedInterface::ErrorWithMessage<size_t>(
227         LLVM_PRETTY_FUNCTION, "No interpreter.", error);
228 
229   lldb::DataExtractorSP data_extractor_sp =
230       GetInterface().ReadMemoryAtAddress(addr, size, error);
231 
232   if (!data_extractor_sp || !data_extractor_sp->GetByteSize() || error.Fail())
233     return 0;
234 
235   offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData(
236       0, data_extractor_sp->GetByteSize(), buf, size, GetByteOrder());
237 
238   if (!bytes_copied || bytes_copied == LLDB_INVALID_OFFSET)
239     return ScriptedInterface::ErrorWithMessage<size_t>(
240         LLVM_PRETTY_FUNCTION, "Failed to copy read memory to buffer.", error);
241 
242   return size;
243 }
244 
245 ArchSpec ScriptedProcess::GetArchitecture() {
246   return GetTarget().GetArchitecture();
247 }
248 
249 Status ScriptedProcess::DoGetMemoryRegionInfo(lldb::addr_t load_addr,
250                                               MemoryRegionInfo &region) {
251   CheckInterpreterAndScriptObject();
252 
253   Status error;
254   if (auto region_or_err =
255           GetInterface().GetMemoryRegionContainingAddress(load_addr, error))
256     region = *region_or_err;
257 
258   return error;
259 }
260 
261 Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos &region_list) {
262   CheckInterpreterAndScriptObject();
263 
264   Status error;
265   lldb::addr_t address = 0;
266 
267   while (auto region_or_err =
268              GetInterface().GetMemoryRegionContainingAddress(address, error)) {
269     if (error.Fail())
270       break;
271 
272     MemoryRegionInfo &mem_region = *region_or_err;
273     auto range = mem_region.GetRange();
274     address += range.GetRangeBase() + range.GetByteSize();
275     region_list.push_back(mem_region);
276   }
277 
278   return error;
279 }
280 
281 void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); }
282 
283 bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list,
284                                          ThreadList &new_thread_list) {
285   // TODO: Implement
286   // This is supposed to get the current set of threads, if any of them are in
287   // old_thread_list then they get copied to new_thread_list, and then any
288   // actually new threads will get added to new_thread_list.
289 
290   CheckInterpreterAndScriptObject();
291   m_thread_plans.ClearThreadCache();
292 
293   Status error;
294   ScriptLanguage language = m_interpreter->GetLanguage();
295 
296   if (language != eScriptLanguagePython)
297     return ScriptedInterface::ErrorWithMessage<bool>(
298         LLVM_PRETTY_FUNCTION,
299         llvm::Twine("ScriptInterpreter language (" +
300                     llvm::Twine(m_interpreter->LanguageToString(language)) +
301                     llvm::Twine(") not supported."))
302             .str(),
303         error);
304 
305   StructuredData::DictionarySP thread_info_sp = GetInterface().GetThreadsInfo();
306 
307   // FIXME: Need to sort the dictionary otherwise the thread ids won't match the
308   // thread indices.
309 
310   if (!thread_info_sp)
311     return ScriptedInterface::ErrorWithMessage<bool>(
312         LLVM_PRETTY_FUNCTION,
313         "Couldn't fetch thread list from Scripted Process.", error);
314 
315   auto create_scripted_thread =
316       [this, &old_thread_list, &error,
317        &new_thread_list](ConstString key, StructuredData::Object *val) -> bool {
318     if (!val)
319       return ScriptedInterface::ErrorWithMessage<bool>(
320           LLVM_PRETTY_FUNCTION, "Invalid thread info object", error);
321 
322     lldb::tid_t tid = LLDB_INVALID_THREAD_ID;
323     if (!llvm::to_integer(key.AsCString(), tid))
324       return ScriptedInterface::ErrorWithMessage<bool>(
325           LLVM_PRETTY_FUNCTION, "Invalid thread id", error);
326 
327     if (ThreadSP thread_sp =
328             old_thread_list.FindThreadByID(tid, false /*=can_update*/)) {
329       // If the thread was already in the old_thread_list,
330       // just add it back to the new_thread_list.
331       new_thread_list.AddThread(thread_sp);
332       return true;
333     }
334 
335     auto thread_or_error = ScriptedThread::Create(*this, val->GetAsGeneric());
336 
337     if (!thread_or_error)
338       return ScriptedInterface::ErrorWithMessage<bool>(
339           LLVM_PRETTY_FUNCTION, toString(thread_or_error.takeError()), error);
340 
341     ThreadSP thread_sp = thread_or_error.get();
342     lldbassert(thread_sp && "Couldn't initialize scripted thread.");
343 
344     RegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext();
345     if (!reg_ctx_sp)
346       return ScriptedInterface::ErrorWithMessage<bool>(
347           LLVM_PRETTY_FUNCTION,
348           llvm::Twine("Invalid Register Context for thread " +
349                       llvm::Twine(key.AsCString()))
350               .str(),
351           error);
352 
353     new_thread_list.AddThread(thread_sp);
354 
355     return true;
356   };
357 
358   thread_info_sp->ForEach(create_scripted_thread);
359 
360   return new_thread_list.GetSize(false) > 0;
361 }
362 
363 void ScriptedProcess::RefreshStateAfterStop() {
364   // Let all threads recover from stopping and do any clean up based on the
365   // previous thread state (if any).
366   m_thread_list.RefreshStateAfterStop();
367 }
368 
369 bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
370   info.Clear();
371   info.SetProcessID(GetID());
372   info.SetArchitecture(GetArchitecture());
373   lldb::ModuleSP module_sp = GetTarget().GetExecutableModule();
374   if (module_sp) {
375     const bool add_exe_file_as_first_arg = false;
376     info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
377                            add_exe_file_as_first_arg);
378   }
379   return true;
380 }
381 
382 lldb_private::StructuredData::ObjectSP
383 ScriptedProcess::GetLoadedDynamicLibrariesInfos() {
384   CheckInterpreterAndScriptObject();
385 
386   Status error;
387   auto error_with_message = [&error](llvm::StringRef message) {
388     return ScriptedInterface::ErrorWithMessage<bool>(LLVM_PRETTY_FUNCTION,
389                                                      message.data(), error);
390   };
391 
392   StructuredData::ArraySP loaded_images_sp = GetInterface().GetLoadedImages();
393 
394   if (!loaded_images_sp || !loaded_images_sp->GetSize())
395     return GetInterface().ErrorWithMessage<StructuredData::ObjectSP>(
396         LLVM_PRETTY_FUNCTION, "No loaded images.", error);
397 
398   ModuleList module_list;
399   Target &target = GetTarget();
400 
401   auto reload_image = [&target, &module_list, &error_with_message](
402                           StructuredData::Object *obj) -> bool {
403     StructuredData::Dictionary *dict = obj->GetAsDictionary();
404 
405     if (!dict)
406       return error_with_message("Couldn't cast image object into dictionary.");
407 
408     ModuleSpec module_spec;
409     llvm::StringRef value;
410 
411     bool has_path = dict->HasKey("path");
412     bool has_uuid = dict->HasKey("uuid");
413     if (!has_path && !has_uuid)
414       return error_with_message("Dictionary should have key 'path' or 'uuid'");
415     if (!dict->HasKey("load_addr"))
416       return error_with_message("Dictionary is missing key 'load_addr'");
417 
418     if (has_path) {
419       dict->GetValueForKeyAsString("path", value);
420       module_spec.GetFileSpec().SetPath(value);
421     }
422 
423     if (has_uuid) {
424       dict->GetValueForKeyAsString("uuid", value);
425       module_spec.GetUUID().SetFromStringRef(value);
426     }
427     module_spec.GetArchitecture() = target.GetArchitecture();
428 
429     ModuleSP module_sp =
430         target.GetOrCreateModule(module_spec, true /* notify */);
431 
432     if (!module_sp)
433       return error_with_message("Couldn't create or get module.");
434 
435     lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
436     lldb::addr_t slide = LLDB_INVALID_OFFSET;
437     dict->GetValueForKeyAsInteger("load_addr", load_addr);
438     dict->GetValueForKeyAsInteger("slide", slide);
439     if (load_addr == LLDB_INVALID_ADDRESS)
440       return error_with_message(
441           "Couldn't get valid load address or slide offset.");
442 
443     if (slide != LLDB_INVALID_OFFSET)
444       load_addr += slide;
445 
446     bool changed = false;
447     module_sp->SetLoadAddress(target, load_addr, false /*=value_is_offset*/,
448                               changed);
449 
450     if (!changed && !module_sp->GetObjectFile())
451       return error_with_message("Couldn't set the load address for module.");
452 
453     dict->GetValueForKeyAsString("path", value);
454     FileSpec objfile(value);
455     module_sp->SetFileSpecAndObjectName(objfile, objfile.GetFilename());
456 
457     return module_list.AppendIfNeeded(module_sp);
458   };
459 
460   if (!loaded_images_sp->ForEach(reload_image))
461     return GetInterface().ErrorWithMessage<StructuredData::ObjectSP>(
462         LLVM_PRETTY_FUNCTION, "Couldn't reload all images.", error);
463 
464   target.ModulesDidLoad(module_list);
465 
466   return loaded_images_sp;
467 }
468 
469 ScriptedProcessInterface &ScriptedProcess::GetInterface() const {
470   return m_interpreter->GetScriptedProcessInterface();
471 }
472