1 //===-- ScriptedProcess.h ------------------------------------- -*- C++ -*-===//
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 #ifndef LLDB_SOURCE_PLUGINS_SCRIPTED_PROCESS_H
10 #define LLDB_SOURCE_PLUGINS_SCRIPTED_PROCESS_H
11 
12 #include "lldb/Target/Process.h"
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/Utility/Status.h"
15 
16 #include "ScriptedThread.h"
17 
18 #include <mutex>
19 
20 namespace lldb_private {
21 
22 class ScriptedProcess : public Process {
23 protected:
24   class ScriptedProcessInfo {
25   public:
26     ScriptedProcessInfo(const ProcessLaunchInfo &launch_info) {
27       m_class_name = launch_info.GetScriptedProcessClassName();
28       m_args_sp = launch_info.GetScriptedProcessDictionarySP();
29     }
30 
31     std::string GetClassName() const { return m_class_name; }
32     StructuredData::DictionarySP GetArgsSP() const { return m_args_sp; }
33 
34   private:
35     std::string m_class_name;
36     StructuredData::DictionarySP m_args_sp;
37   };
38 
39 public:
40   static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,
41                                         lldb::ListenerSP listener_sp,
42                                         const FileSpec *crash_file_path,
43                                         bool can_connect);
44 
45   static void Initialize();
46 
47   static void Terminate();
48 
49   static llvm::StringRef GetPluginNameStatic() { return "ScriptedProcess"; }
50 
51   static llvm::StringRef GetPluginDescriptionStatic();
52 
53   ScriptedProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
54                   const ScriptedProcess::ScriptedProcessInfo &launch_info,
55                   Status &error);
56 
57   ~ScriptedProcess() override;
58 
59   bool CanDebug(lldb::TargetSP target_sp,
60                 bool plugin_specified_by_name) override;
61 
62   DynamicLoader *GetDynamicLoader() override { return nullptr; }
63 
64   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
65 
66   SystemRuntime *GetSystemRuntime() override { return nullptr; }
67 
68   Status DoLoadCore() override;
69 
70   Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override;
71 
72   void DidLaunch() override;
73 
74   Status DoResume() override;
75 
76   Status DoDestroy() override;
77 
78   void RefreshStateAfterStop() override;
79 
80   bool IsAlive() override;
81 
82   size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
83                       Status &error) override;
84 
85   ArchSpec GetArchitecture();
86 
87   Status
88   GetMemoryRegions(lldb_private::MemoryRegionInfos &region_list) override;
89 
90   bool GetProcessInfo(ProcessInstanceInfo &info) override;
91 
92   lldb_private::StructuredData::ObjectSP
93   GetLoadedDynamicLibrariesInfos() override;
94 
95 protected:
96   Status DoStop();
97 
98   void Clear();
99 
100   bool DoUpdateThreadList(ThreadList &old_thread_list,
101                           ThreadList &new_thread_list) override;
102 
103   Status DoGetMemoryRegionInfo(lldb::addr_t load_addr,
104                                MemoryRegionInfo &range_info) override;
105 
106 private:
107   friend class ScriptedThread;
108 
109   void CheckInterpreterAndScriptObject() const;
110   ScriptedProcessInterface &GetInterface() const;
111   static bool IsScriptLanguageSupported(lldb::ScriptLanguage language);
112 
113   // Member variables.
114   const ScriptedProcessInfo m_scripted_process_info;
115   lldb_private::ScriptInterpreter *m_interpreter = nullptr;
116   lldb_private::StructuredData::ObjectSP m_script_object_sp = nullptr;
117   //@}
118 };
119 
120 } // namespace lldb_private
121 
122 #endif // LLDB_SOURCE_PLUGINS_SCRIPTED_PROCESS_H
123