31#include "llvm/Support/Error.h"
41 return "Scripted Process plug-in.";
49 llvm::ArrayRef<lldb::ScriptLanguage> supported_languages =
52 return llvm::is_contained(supported_languages, language);
65 if (!scripted_metadata)
69 auto process_sp = std::shared_ptr<ScriptedProcess>(
72 if (
error.Fail() || !process_sp || !process_sp->m_interface_up) {
78 llvm::formatv(
"failed to create ScriptedProcess: {0}",
81 target_sp->GetDebugger().GetID());
91 bool plugin_specified_by_name) {
103 "ScriptedProcess::%s () - ERROR: %s", __FUNCTION__,
"Invalid target");
108 target_sp->GetDebugger().GetScriptInterpreter();
112 "ScriptedProcess::%s () - ERROR: %s", __FUNCTION__,
113 "Debugger has no Script Interpreter");
121 "ScriptedProcess::%s () - ERROR: %s", __FUNCTION__,
122 "Script interpreter couldn't create Scripted Process Interface");
133 std::string error_msg = llvm::toString(obj_or_err.takeError());
135 "failed to create script object: {0}", error_msg);
141 if (!object_sp || !object_sp->IsValid()) {
143 "ScriptedProcess::%s () - ERROR: %s", __FUNCTION__,
144 "Failed to create valid script object");
206 !metadata_sp->GetValueForKeyAsDictionary(
"addressable_bits",
bits))
209 uint64_t lowmem_bits = 0;
210 uint64_t highmem_bits = 0;
211 bits->GetValueForKeyAsInteger(
"lowmem", lowmem_bits);
212 if (!
bits->GetValueForKeyAsInteger(
"highmem", highmem_bits))
213 highmem_bits = lowmem_bits;
232 "{0} does not support reverse execution of processes",
GetPluginName());
239 "failed to attach to scripted process: {0}",
error.AsCString());
279 if (!data_extractor_sp || !data_extractor_sp->HasData() ||
error.Fail()) {
282 "Failed to read memory from scripted process at 0x{0:x-}: {1}", addr,
288 offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData(
289 0, data_extractor_sp->GetByteSize(), buf, size,
GetByteOrder());
293 LLVM_PRETTY_FUNCTION,
"Failed to copy read memory to buffer.",
error);
306 if (!data_extractor_sp || !data_extractor_sp->HasData())
315 "failed to write memory to scripted process at 0x{0:x-}: {1}",
316 vm_addr,
error.AsCString());
319 LLVM_PRETTY_FUNCTION,
"Failed to copy write buffer to memory.",
error);
325 return bytes_written;
329 assert(bp_site !=
nullptr);
337 "Scripted Processes don't support hardware breakpoints");
353 if (
auto region_or_err =
355 region = *region_or_err;
364 while (
auto region_or_err =
371 address += range.GetRangeBase() + range.GetByteSize();
372 region_list.push_back(mem_region);
393 LLVM_PRETTY_FUNCTION,
394 "Couldn't fetch thread list from Scripted Process.",
error);
404 std::map<size_t, StructuredData::ObjectSP> sorted_threads;
405 auto sort_keys = [&sorted_threads,
410 llvm::StringRef key = item->GetStringValue();
414 if (!llvm::to_integer(key, idx))
417 sorted_threads[idx] = thread_info_sp->GetValueForKey(key);
421 size_t thread_count = thread_info_sp->GetSize();
423 if (!keys->ForEach(sort_keys) || sorted_threads.size() != thread_count)
426 LLVM_PRETTY_FUNCTION,
"Couldn't sort thread list.",
error);
428 auto create_scripted_thread =
429 [
this, &
error, &new_thread_list](
430 const std::pair<size_t, StructuredData::ObjectSP> pair) ->
bool {
431 size_t idx = pair.first;
436 LLVM_PRETTY_FUNCTION,
"Invalid thread info object",
error);
438 auto thread_or_error =
441 if (!thread_or_error) {
443 llvm::formatv(
"failed to create scripted thread ({0}): {1}", idx,
444 llvm::toString(thread_or_error.takeError()))
450 ThreadSP thread_sp = thread_or_error.get();
451 lldbassert(thread_sp &&
"Couldn't initialize scripted thread.");
456 LLVM_PRETTY_FUNCTION,
457 llvm::Twine(
"Invalid Register Context for thread " + llvm::Twine(idx))
466 llvm::for_each(sorted_threads, create_scripted_thread);
468 return new_thread_list.
GetSize(
false) > 0;
483 const bool add_exe_file_as_first_arg =
false;
485 add_exe_file_as_first_arg);
494 auto error_with_message = [&
error](llvm::StringRef message) {
496 message.data(),
error);
501 if (!loaded_images_sp || !loaded_images_sp->GetSize())
503 LLVM_PRETTY_FUNCTION,
"No loaded images.",
error);
508 auto reload_image = [&target, &module_list, &error_with_message](
513 return error_with_message(
"Couldn't cast image object into dictionary.");
517 bool has_path = dict->
HasKey(
"path");
518 bool has_uuid = dict->
HasKey(
"uuid");
519 if (!has_path && !has_uuid)
520 return error_with_message(
"Dictionary should have key 'path' or 'uuid'");
521 if (!dict->
HasKey(
"load_addr"))
522 return error_with_message(
"Dictionary is missing key 'load_addr'");
524 llvm::StringRef path =
"";
530 llvm::StringRef uuid =
"";
541 return error_with_message(
542 "Couldn't get valid load address or slide offset.");
552 bool is_placeholder_module =
false;
558 "ScriptedProcess::%s unable to locate the matching "
559 "object file path %s, creating a placeholder module at 0x%" PRIx64,
560 __FUNCTION__, path.str().c_str(), load_addr);
565 is_placeholder_module =
true;
568 bool changed =
false;
569 module_sp->SetLoadAddress(target, load_addr,
false ,
572 if (!changed && !module_sp->GetObjectFile())
573 return error_with_message(
"Couldn't set the load address for module.");
576 module_sp->SetFileSpecAndObjectName(objfile,
579 if (is_placeholder_module) {
587 size_t loaded_images_size = loaded_images_sp->GetSize();
588 bool print_error =
true;
589 for (
size_t idx = 0; idx < loaded_images_size; idx++) {
590 const auto &loaded_image = loaded_images_sp->GetItemAtIndex(idx);
591 if (!reload_image(loaded_image.get()) && print_error) {
594 LLVM_PRETTY_FUNCTION,
"Couldn't reload all images.",
error);
600 return loaded_images_sp;
607 if (!metadata_sp || !metadata_sp->GetSize())
609 LLVM_PRETTY_FUNCTION,
"No metadata.",
error);
617 if (
const char *queue_name = thread_sp->GetQueueName()) {
618 QueueSP queue_sp = std::make_shared<Queue>(
619 m_process->shared_from_this(), thread_sp->GetQueueID(), queue_name);
633 if (object_instance_sp &&
635 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[]
A class which holds the metadata from a remote stub/corefile note about how many bits are used for ad...
void SetAddressableBits(uint32_t addressing_bits)
When a single value is available for the number of bits.
An architecture specification class.
Class that manages the actual breakpoint that will be inserted into the running program.
A uniqued constant string class.
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetPath(llvm::StringRef p)
Temporary helper for FileSystem change.
llvm::StringRef GetFilename() const
Filename string const get accessor.
size_t MemorySize() const
Get the memory cost of this object.
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 lldb::ModuleSP CreateModuleFromObjectFile(Args &&...args)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
An address in a process, qualified by an address space.
lldb::addr_t GetValue() const
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
void SetArchitecture(const ArchSpec &arch)
void SetProcessID(lldb::pid_t pid)
lldb::pid_t GetID() const
Returns the pid of the process or LLDB_INVALID_PROCESS_ID if there is no known pid.
void SetAddressableBitMasks(AddressableBits bit_masks)
Process(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp)
Construct with a shared pointer to a target, and the Process listener.
QueueList m_queue_list
The list of libdispatch queues at a given stop point.
bool IsBreakpointSitePhysicallyEnabled(const BreakpointSite &site)
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)
virtual void Finalize(bool destructing)
This object is about to be destroyed, do any necessary cleanup.
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.
virtual lldb::ScriptedProcessInterfaceUP CreateScriptedProcessInterface()
static Ret ErrorWithMessage(llvm::StringRef caller_name, llvm::StringRef user_msg, Status &error, LLDBLog log_category=LLDBLog::Process)
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 bool CreateBreakpoint(lldb::addr_t addr, Status &error)
virtual StructuredData::DictionarySP GetMetadata()
virtual StructuredData::ArraySP GetLoadedImages()
virtual lldb::offset_t WriteMemoryAtAddress(lldb::addr_t addr, lldb::DataExtractorSP data_sp, Status &error)
virtual llvm::Expected< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, ExecutionContext &exe_ctx, StructuredData::Generic *script_obj=nullptr)=0
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.
const ScriptedMetadata m_scripted_metadata
size_t DoReadMemory(const ProcessAddress &addr, void *buf, size_t size, Status &error) override
Actually do the reading of memory from a process.
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
lldb_private::StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos(lldb::BinaryInformationLevel info_level) override
Retrieve a StructuredData dictionary about all of the binaries loaded in the process at this time.
void DidResume() override
Called after resuming a process.
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
Status DoResume(lldb::RunDirection direction) override
Resumes all of a process's threads as configured using the Thread run control functions.
ScriptedProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const ScriptedMetadata &scripted_metadata, Status &error)
lldb::ScriptedProcessInterfaceUP m_interface_up
llvm::StringRef GetPluginName() override
Status EnableBreakpointSite(BreakpointSite *bp_site) override
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.
void DidLaunchOrAttach()
Pick up the process state that has to be in place before the first stop is reported,...
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)
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
virtual lldb::addr_t GetLoadAddress() const
bool HardwareRequired() const
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)
This call may preload module symbols, and may do so in parallel depending on the following target set...
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 ModuleList & GetImages() const
Get accessor for the images for this process.
const ArchSpec & GetArchitecture() const
void AddThread(const lldb::ThreadSP &thread_sp)
uint32_t GetSize(bool can_update=true)
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.
static uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit)
ScriptLanguage
Script interpreter types.
std::shared_ptr< lldb_private::Queue > QueueSP
RunDirection
Execution directions.
std::shared_ptr< lldb_private::Thread > ThreadSP
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateRunning
Process or thread is running and can't be examined.
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Listener > ListenerSP
BinaryInformationLevel
When the Process plugin can retrieve information about all binaries loaded in the target process,...
std::shared_ptr< lldb_private::Target > TargetSP
@ eStructuredDataTypeGeneric
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP