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