LLDB  mainline
ScriptedProcess.cpp
Go to the documentation of this file.
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"
14 
15 #include "lldb/Host/OptionParser.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 
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 
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()) {
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  ExecutionContext exe_ctx(target_sp, /*get_process=*/false);
113 
115  m_scripted_process_info.GetClassName().c_str(), exe_ctx,
117 
118  if (!object_sp || !object_sp->IsValid()) {
119  error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",
120  __FUNCTION__,
121  "Failed to create valid script object");
122  return;
123  }
124 
125  m_script_object_sp = object_sp;
126 }
127 
129  Clear();
130  // We need to call finalize on the process before destroying ourselves to
131  // make sure all of the broadcaster cleanup goes as planned. If we destruct
132  // this class, then Process::~Process() might have problems trying to fully
133  // destroy the broadcaster.
134  Finalize();
135 }
136 
138  static llvm::once_flag g_once_flag;
139 
140  llvm::call_once(g_once_flag, []() {
143  });
144 }
145 
148 }
149 
151 
154 
155  return DoLaunch(nullptr, launch_info);
156 }
157 
159  ProcessLaunchInfo &launch_info) {
161 
162  /* FIXME: This doesn't reflect how lldb actually launches a process.
163  In reality, it attaches to debugserver, then resume the process. */
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 
176 
178  GetThreadList();
179 
180  return {};
181 }
182 
186 }
187 
190 
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 
206  error = GetInterface().Resume();
207  }
208 
209  return error;
210 }
211 
214 
216 
217  if (GetInterface().ShouldStop()) {
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 
228 
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 
262  return GetTarget().GetArchitecture();
263 }
264 
266  MemoryRegionInfo &region) {
267  // TODO: Implement
268  return Status();
269 }
270 
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 
288 
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 
299  info.Clear();
300  info.SetProcessID(GetID());
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 
313 }
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:108
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb::ScriptLanguage
ScriptLanguage
Script interpreter types.
Definition: lldb-enumerations.h:216
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::Process::m_thread_list
ThreadList m_thread_list
The threads for this process as the user will see them.
Definition: Process.h:2899
lldb_private::ProcessInfo::SetExecutableFile
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:62
lldb_private::ProcessInfo::SetArchitecture
void SetArchitecture(const ArchSpec &arch)
Definition: ProcessInfo.h:65
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:116
lldb_private::StructuredData::GenericSP
std::shared_ptr< Generic > GenericSP
Definition: StructuredData.h:68
lldb_private::ScriptedProcess::GetInterface
ScriptedProcessInterface & GetInterface() const
Definition: ScriptedProcess.cpp:311
lldb_private::ScriptedProcess::CreateInstance
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec *crash_file_path, bool can_connect)
Definition: ScriptedProcess.cpp:60
lldb_private::ScriptedProcess::DoDestroy
Status DoDestroy() override
Definition: ScriptedProcess.cpp:227
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::Process
Definition: Process.h:342
lldb_private::ScriptedProcess::DoLoadCore
Status DoLoadCore() override
Definition: ScriptedProcess.cpp:152
lldb_private::Target::GetExecutableModule
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1348
Module.h
lldb_private::Process::GetThreadList
ThreadList & GetThreadList()
Definition: Process.h:2116
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1214
lldb_private::StateAsCString
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
lldb_private::ScriptedProcess::ScriptedProcessInfo::GetDictionarySP
StructuredData::DictionarySP GetDictionarySP() const
Definition: ScriptedProcess.h:30
lldb_private::Module
Definition: Module.h:84
OptionArgParser.h
lldb_private::ScriptedProcessInterface::CreatePluginObject
StructuredData::GenericSP CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx, StructuredData::DictionarySP args_sp) override
Definition: ScriptedProcessInterface.h:24
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::MemoryRegionInfo
Definition: MemoryRegionInfo.h:21
lldb_private::ProcessInfo::SetProcessID
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:69
lldb_private::ScriptedProcess::DoUpdateThreadList
bool DoUpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) override
Update the thread list following process plug-in's specific logic.
Definition: ScriptedProcess.cpp:289
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Process::SetPrivateState
void SetPrivateState(lldb::StateType state)
Definition: Process.cpp:1406
lldb_private::ScriptedProcess::GetProcessInfo
bool GetProcessInfo(ProcessInstanceInfo &info) override
Definition: ScriptedProcess.cpp:298
lldb_private::ScriptedProcess::DidLaunch
void DidLaunch() override
Called after launching a process.
Definition: ScriptedProcess.cpp:183
lldb_private::ScriptInterpreter::GetScriptedProcessInterface
ScriptedProcessInterface & GetScriptedProcessInterface()
Definition: ScriptInterpreter.h:558
ThreadLauncher.h
Debugger.h
lldb_private::ScriptedProcess::Clear
void Clear()
Definition: ScriptedProcess.cpp:287
lldb_private::ScriptedProcess::DoReadMemory
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Status &error) override
Actually do the reading of memory from a process.
Definition: ScriptedProcess.cpp:235
lldb_private::PluginManager::RegisterPlugin
static bool RegisterPlugin(ConstString name, const char *description, ABICreateInstance create_callback)
Definition: PluginManager.cpp:288
lldb::eStateStopped
@ eStateStopped
Process or thread is stopped and can be examined.
Definition: lldb-enumerations.h:84
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::ThreadList::Clear
void Clear()
Definition: ThreadList.cpp:443
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Process::m_pid
lldb::pid_t m_pid
Definition: Process.h:2869
lldb_private::ScriptedProcessInterface::GetProcessID
virtual lldb::pid_t GetProcessID()
Definition: ScriptedProcessInterface.h:57
lldb_private::PluginManager::UnregisterPlugin
static bool UnregisterPlugin(ABICreateInstance create_callback)
Definition: PluginManager.cpp:293
lldb_private::ScriptedProcess::GetPluginName
ConstString GetPluginName() override
Definition: ScriptedProcess.cpp:150
LLDB_INVALID_OFFSET
#define LLDB_INVALID_OFFSET
Definition: lldb-defines.h:97
lldb_private::ProcessLaunchInfo
Definition: ProcessLaunchInfo.h:31
Log.h
MemoryRegionInfo.h
ScriptedProcess.h
lldb_private::ScriptedProcessInterface::ReadMemoryAtAddress
virtual lldb::DataExtractorSP ReadMemoryAtAddress(lldb::addr_t address, size_t size, Status &error)
Definition: ScriptedProcessInterface.h:51
lldb_private::MemoryRegionInfos
Definition: MemoryRegionInfo.h:164
lldb_private::ScriptedProcess::GetArchitecture
ArchSpec GetArchitecture()
Definition: ScriptedProcess.cpp:261
lldb_private::Process::Finalize
virtual void Finalize()
This object is about to be destroyed, do any necessary cleanup.
Definition: Process.cpp:511
lldb_private::ThreadList
Definition: ThreadList.h:26
lldb_private::ConstString
Definition: ConstString.h:40
lldb::eScriptLanguagePython
@ eScriptLanguagePython
Definition: lldb-enumerations.h:218
lldb_private::ScriptedProcessInterface::Launch
virtual Status Launch()
Definition: ScriptedProcessInterface.h:29
lldb_private::Process::UpdateThreadListIfNeeded
void UpdateThreadListIfNeeded()
Definition: Process.cpp:1139
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::ScriptedProcess::m_script_object_sp
lldb_private::StructuredData::ObjectSP m_script_object_sp
Definition: ScriptedProcess.h:111
lldb_private::ScriptedProcessInterface::Resume
virtual Status Resume()
Definition: ScriptedProcessInterface.h:31
lldb_private::ScriptedProcess::Initialize
static void Initialize()
Definition: ScriptedProcess.cpp:137
lldb_private::ScriptedProcess::Terminate
static void Terminate()
Definition: ScriptedProcess.cpp:146
lldb_private::ScriptedProcess::ScriptedProcessInfo::GetClassName
std::string GetClassName() const
Definition: ScriptedProcess.h:29
lldb_private::TargetProperties::GetProcessLaunchInfo
const ProcessLaunchInfo & GetProcessLaunchInfo() const
Definition: Target.cpp:4301
lldb_private::ScriptedProcessInterface::IsAlive
virtual bool IsAlive()
Definition: ScriptedProcessInterface.h:59
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
lldb_private::ScriptedProcess::m_scripted_process_info
const ScriptedProcessInfo m_scripted_process_info
Definition: ScriptedProcess.h:109
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:970
lldb_private::Status
Definition: Status.h:44
message
message(FATAL_ERROR "invalid libipt include path provided") endif() include_directories($
Definition: Plugins/Trace/intel-pt/CMakeLists.txt:6
lldb_private::ScriptedProcessInterface::Stop
virtual Status Stop()
Definition: ScriptedProcessInterface.h:35
lldb_private::ScriptedProcessInterface
Definition: ScriptedProcessInterface.h:21
lldb_private::ScriptedProcess::~ScriptedProcess
~ScriptedProcess() override
Definition: ScriptedProcess.cpp:128
lldb_private::ScriptedProcess
Definition: ScriptedProcess.h:20
lldb_private::ThreadList::GetSize
uint32_t GetSize(bool can_update=true)
Definition: ThreadList.cpp:82
lldb_private::ScriptedProcess::IsAlive
bool IsAlive() override
Check if a process is still alive.
Definition: ScriptedProcess.cpp:229
lldb_private::ScriptedProcess::DoLaunch
Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override
Launch a new process.
Definition: ScriptedProcess.cpp:158
PluginManager.h
lldb_private::ScriptedProcess::GetMemoryRegionInfo
Status GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info) override
Locate the memory region that contains load_addr.
Definition: ScriptedProcess.cpp:265
lldb_private::Process::GetID
lldb::pid_t GetID() const
Returns the pid of the process or LLDB_INVALID_PROCESS_ID if there is no known pid.
Definition: Process.h:542
lldb_private::ScriptedProcess::DoResume
Status DoResume() override
Resumes all of a process's threads as configured using the Thread run control functions.
Definition: ScriptedProcess.cpp:188
g_supported_script_languages
static constexpr lldb::ScriptLanguage g_supported_script_languages[]
Definition: ScriptedProcess.cpp:44
OptionParser.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ScriptedProcess::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: ScriptedProcess.cpp:40
CommandInterpreter.h
Logging.h
State.h
OptionGroupBoolean.h
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::Process::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3336
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Log
Definition: Log.h:49
lldb_private::ScriptedProcess::GetMemoryRegions
Status GetMemoryRegions(lldb_private::MemoryRegionInfos &region_list) override
Obtain all the mapped memory regions within this process.
Definition: ScriptedProcess.cpp:271
lldb_private::ScriptedProcess::DoStop
Status DoStop()
Definition: ScriptedProcess.cpp:212
ScriptInterpreter.h
lldb_private::ScriptedProcess::ScriptedProcessInfo
Definition: ScriptedProcess.h:22
lldb_private::ScriptedProcess::CheckInterpreterAndScriptObject
void CheckInterpreterAndScriptObject() const
Definition: ScriptedProcess.cpp:55
lldb
Definition: SBAddress.h:15
LIBLLDB_LOG_PROCESS
#define LIBLLDB_LOG_PROCESS
Definition: Logging.h:15
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:85
RegisterContext.h
lldb_private::ScriptedProcess::m_interpreter
lldb_private::ScriptInterpreter * m_interpreter
Definition: ScriptedProcess.h:110
lldb_private::ScriptedProcess::GetPluginNameStatic
static ConstString GetPluginNameStatic()
Definition: ScriptedProcess.cpp:35