36 return "Scripted Process plug-in.";
40 ScriptLanguage::eScriptLanguagePython,
44 llvm::ArrayRef<lldb::ScriptLanguage> supported_languages =
47 return llvm::is_contained(supported_languages, language);
51 lldb::ListenerSP listener_sp,
61 auto process_sp = std::shared_ptr<ScriptedProcess>(
64 if (
error.Fail() || !process_sp || !process_sp->m_interface_up) {
73 bool plugin_specified_by_name) {
78 lldb::ListenerSP listener_sp,
81 :
Process(target_sp, listener_sp), m_scripted_metadata(scripted_metadata) {
84 error.SetErrorStringWithFormat(
"ScriptedProcess::%s () - ERROR: %s",
85 __FUNCTION__,
"Invalid target");
90 target_sp->GetDebugger().GetScriptInterpreter();
93 error.SetErrorStringWithFormat(
"ScriptedProcess::%s () - ERROR: %s",
95 "Debugger has no Script Interpreter");
102 error.SetErrorStringWithFormat(
103 "ScriptedProcess::%s () - ERROR: %s", __FUNCTION__,
104 "Script interpreter couldn't create Scripted Process Interface");
115 if (!object_sp || !object_sp->IsValid()) {
116 error.SetErrorStringWithFormat(
"ScriptedProcess::%s () - ERROR: %s",
118 "Failed to create valid script object");
133 static llvm::once_flag g_once_flag;
135 llvm::call_once(g_once_flag, []() {
148 return DoLaunch(
nullptr, launch_info);
180 LLDB_LOGF(log,
"ScriptedProcess::%s thread_resume_state = %s", __FUNCTION__,
184 assert(thread_resume_state ==
eStateRunning &&
"invalid thread resume state");
188 LLDB_LOGF(log,
"ScriptedProcess::%s sending resume", __FUNCTION__);
231 LLDB_LOGF(log,
"ScriptedProcess::%s Immediate stop", __FUNCTION__);
235 LLDB_LOGF(log,
"ScriptedProcess::%s Delayed stop", __FUNCTION__);
245 lldb::DataExtractorSP data_extractor_sp =
248 if (!data_extractor_sp || !data_extractor_sp->GetByteSize() ||
error.Fail())
251 offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData(
252 0, data_extractor_sp->GetByteSize(), buf, size,
GetByteOrder());
255 return ScriptedInterface::ErrorWithMessage<size_t>(
256 LLVM_PRETTY_FUNCTION,
"Failed to copy read memory to buffer.",
error);
266 lldb::DataExtractorSP data_extractor_sp = std::make_shared<DataExtractor>(
269 if (!data_extractor_sp || !data_extractor_sp->GetByteSize())
276 return ScriptedInterface::ErrorWithMessage<size_t>(
277 LLVM_PRETTY_FUNCTION,
"Failed to copy write buffer to memory.",
error);
282 return bytes_written;
292 if (
auto region_or_err =
294 region = *region_or_err;
303 while (
auto region_or_err =
311 region_list.push_back(mem_region);
331 return ScriptedInterface::ErrorWithMessage<bool>(
332 LLVM_PRETTY_FUNCTION,
333 "Couldn't fetch thread list from Scripted Process.",
error);
343 std::map<size_t, StructuredData::ObjectSP> sorted_threads;
344 auto sort_keys = [&sorted_threads,
349 llvm::StringRef key = item->GetStringValue();
353 if (!llvm::to_integer(key, idx))
356 sorted_threads[idx] = thread_info_sp->GetValueForKey(key);
360 size_t thread_count = thread_info_sp->GetSize();
362 if (!keys->ForEach(sort_keys) || sorted_threads.size() != thread_count)
364 return ScriptedInterface::ErrorWithMessage<bool>(
365 LLVM_PRETTY_FUNCTION,
"Couldn't sort thread list.",
error);
367 auto create_scripted_thread =
368 [
this, &
error, &new_thread_list](
369 const std::pair<size_t, StructuredData::ObjectSP> pair) ->
bool {
370 size_t idx = pair.first;
374 return ScriptedInterface::ErrorWithMessage<bool>(
375 LLVM_PRETTY_FUNCTION,
"Invalid thread info object",
error);
377 auto thread_or_error =
380 if (!thread_or_error)
381 return ScriptedInterface::ErrorWithMessage<bool>(
382 LLVM_PRETTY_FUNCTION,
toString(thread_or_error.takeError()),
error);
384 ThreadSP thread_sp = thread_or_error.get();
385 lldbassert(thread_sp &&
"Couldn't initialize scripted thread.");
387 RegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext();
389 return ScriptedInterface::ErrorWithMessage<bool>(
390 LLVM_PRETTY_FUNCTION,
391 llvm::Twine(
"Invalid Register Context for thread " + llvm::Twine(idx))
400 llvm::for_each(sorted_threads, create_scripted_thread);
402 return new_thread_list.
GetSize(
false) > 0;
417 const bool add_exe_file_as_first_arg =
false;
419 add_exe_file_as_first_arg);
427 auto error_with_message = [&
error](llvm::StringRef message) {
428 return ScriptedInterface::ErrorWithMessage<bool>(LLVM_PRETTY_FUNCTION,
429 message.data(),
error);
434 if (!loaded_images_sp || !loaded_images_sp->GetSize())
435 return ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
436 LLVM_PRETTY_FUNCTION,
"No loaded images.",
error);
441 auto reload_image = [&target, &module_list, &error_with_message](
446 return error_with_message(
"Couldn't cast image object into dictionary.");
449 llvm::StringRef value;
451 bool has_path = dict->
HasKey(
"path");
452 bool has_uuid = dict->
HasKey(
"uuid");
453 if (!has_path && !has_uuid)
454 return error_with_message(
"Dictionary should have key 'path' or 'uuid'");
455 if (!dict->
HasKey(
"load_addr"))
456 return error_with_message(
"Dictionary is missing key 'load_addr'");
473 return error_with_message(
"Couldn't create or get module.");
480 return error_with_message(
481 "Couldn't get valid load address or slide offset.");
486 bool changed =
false;
487 module_sp->SetLoadAddress(target, load_addr,
false ,
490 if (!changed && !module_sp->GetObjectFile())
491 return error_with_message(
"Couldn't set the load address for module.");
495 module_sp->SetFileSpecAndObjectName(objfile, objfile.
GetFilename());
500 if (!loaded_images_sp->ForEach(reload_image))
501 return ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
502 LLVM_PRETTY_FUNCTION,
"Couldn't reload all images.",
error);
506 return loaded_images_sp;
513 if (!metadata_sp || !metadata_sp->GetSize())
514 return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
515 LLVM_PRETTY_FUNCTION,
"No metadata.",
error);
522 for (ThreadSP thread_sp :
Threads()) {
523 if (
const char *queue_name = thread_sp->GetQueueName()) {
524 QueueSP queue_sp = std::make_shared<Queue>(
525 m_process->shared_from_this(), thread_sp->GetQueueID(), queue_name);
539 if (object_instance_sp &&
541 return object_instance_sp->GetAsGeneric()->GetValue();
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
static constexpr lldb::ScriptLanguage g_supported_script_languages[]
An architecture specification class.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const ConstString & GetFilename() const
Filename string const get accessor.
void SetPath(llvm::StringRef p)
Temporary helper for FileSystem change.
A collection class for Module objects.
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
ArchSpec & GetArchitecture()
A class that describes an executable image and its associated object and symbol files.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
void SetArchitecture(const ArchSpec &arch)
void SetProcessID(lldb::pid_t pid)
A plug-in interface definition class for debugging a process.
virtual void Finalize()
This object is about to be destroyed, do any necessary cleanup.
lldb::pid_t GetID() const
Returns the pid of the process or LLDB_INVALID_PROCESS_ID if there is no known pid.
QueueList m_queue_list
The list of libdispatch queues at a given stop point.
lldb::ByteOrder GetByteOrder() const
ThreadList::ThreadIterable Threads()
ThreadPlanStackMap m_thread_plans
This is the list of thread plans for threads in m_thread_list, as well as threads we knew existed,...
uint32_t GetAddressByteSize() const
void SetPrivateState(lldb::StateType state)
ThreadList m_thread_list
The threads for this process as the user will see them.
Target & GetTarget()
Get the target object pointer for this module.
void AddQueue(lldb::QueueSP queue)
Add a Queue to the QueueList.
virtual lldb::ScriptedProcessInterfaceUP CreateScriptedProcessInterface()
StructuredData::GenericSP GetScriptObjectInstance()
virtual lldb::DataExtractorSP ReadMemoryAtAddress(lldb::addr_t address, size_t size, Status &error)
virtual StructuredData::DictionarySP GetThreadsInfo()
virtual Status Attach(const ProcessAttachInfo &attach_info)
virtual lldb::pid_t GetProcessID()
virtual StructuredData::DictionarySP GetMetadata()
virtual StructuredData::ArraySP GetLoadedImages()
virtual lldb::offset_t WriteMemoryAtAddress(lldb::addr_t addr, lldb::DataExtractorSP data_sp, Status &error)
StructuredData::GenericSP CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx, StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj=nullptr) override
Status DoResume() override
Resumes all of a process's threads as configured using the Thread run control functions.
Status DoAttachToProcessWithID(lldb::pid_t pid, const ProcessAttachInfo &attach_info) override
Attach to an existing process using a process ID.
void * GetImplementation() override
bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override
Check if a plug-in instance can debug the file in module.
bool DoUpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) override
Update the thread list following process plug-in's specific logic.
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Status &error) override
Actually do the reading of memory from a process.
lldb_private::StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos() override
const ScriptedMetadata m_scripted_metadata
void DidAttach(ArchSpec &process_arch) override
Called after attaching a process.
Status DoDestroy() override
static llvm::StringRef GetPluginNameStatic()
Status GetMemoryRegions(lldb_private::MemoryRegionInfos ®ion_list) override
Obtain all the mapped memory regions within this process.
static bool IsScriptLanguageSupported(lldb::ScriptLanguage language)
ArchSpec GetArchitecture()
bool IsAlive() override
Check if a process is still alive.
Status DoAttach(const ProcessAttachInfo &attach_info)
Status DoLoadCore() override
bool GetProcessInfo(ProcessInstanceInfo &info) override
Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override
Launch a new process.
size_t DoWriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, Status &error) override
Actually do the writing of memory to a process.
void CheckScriptedInterface() const
ScriptedProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const ScriptedMetadata &scripted_metadata, Status &error)
lldb::ScriptedProcessInterfaceUP m_interface_up
lldb_private::StructuredData::DictionarySP GetMetadata() override
Fetch process defined metadata.
Status DoAttachToProcessWithName(const char *process_name, const ProcessAttachInfo &attach_info) override
Attach to an existing process using a partial process name.
void RefreshStateAfterStop() override
Currently called as part of ShouldStop.
~ScriptedProcess() override
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec *crash_file_path, bool can_connect)
static llvm::StringRef GetPluginDescriptionStatic()
void UpdateQueueListIfNeeded() override
ScriptedProcessInterface & GetInterface() const
void DidLaunch() override
Called after launching a process.
Status DoGetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info) override
DoGetMemoryRegionInfo is called by GetMemoryRegionInfo after it has removed non address bits from loa...
static llvm::Expected< std::shared_ptr< ScriptedThread > > Create(ScriptedProcess &process, StructuredData::Generic *script_object=nullptr)
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
bool HasKey(llvm::StringRef key) const
Dictionary * GetAsDictionary()
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
const ProcessLaunchInfo & GetProcessLaunchInfo() const
void ModulesDidLoad(ModuleList &module_list)
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
const ArchSpec & GetArchitecture() const
void AddThread(const lldb::ThreadSP &thread_sp)
uint32_t GetSize(bool can_update=true)
void RefreshStateAfterStop()
void ClearThreadCache()
Clear the Thread* cache that each ThreadPlan contains.
bool SetFromStringRef(llvm::StringRef str)
#define LLDB_INVALID_ADDRESS
#define LLDB_INVALID_OFFSET
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
const char * toString(AppleArm64ExceptionClass EC)
ScriptLanguage
Script interpreter types.
StateType
Process and Thread States.
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateRunning
Process or thread is running and can't be examined.
@ eStructuredDataTypeGeneric
BaseType GetRangeBase() const