|
| 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" |
| 13 | +#include "lldb/Core/PluginManager.h" |
| 14 | + |
| 15 | +#include "lldb/Host/OptionParser.h" |
| 16 | + |
| 17 | +#include "lldb/Interpreter/OptionArgParser.h" |
| 18 | +#include "lldb/Interpreter/OptionGroupBoolean.h" |
| 19 | +#include "lldb/Interpreter/ScriptInterpreter.h" |
| 20 | +#include "lldb/Target/MemoryRegionInfo.h" |
| 21 | + |
| 22 | +LLDB_PLUGIN_DEFINE(ScriptedProcess) |
| 23 | + |
| 24 | +using namespace lldb; |
| 25 | +using namespace lldb_private; |
| 26 | + |
| 27 | +ConstString ScriptedProcess::GetPluginNameStatic() { |
| 28 | + static ConstString g_name("ScriptedProcess"); |
| 29 | + return g_name; |
| 30 | +} |
| 31 | + |
| 32 | +const char *ScriptedProcess::GetPluginDescriptionStatic() { |
| 33 | + return "Scripted Process plug-in."; |
| 34 | +} |
| 35 | + |
| 36 | +lldb::ProcessSP ScriptedProcess::CreateInstance(lldb::TargetSP target_sp, |
| 37 | + lldb::ListenerSP listener_sp, |
| 38 | + const FileSpec *file, |
| 39 | + bool can_connect) { |
| 40 | + ScriptedProcess::LaunchInfo launch_info(target_sp->GetProcessLaunchInfo()); |
| 41 | + |
| 42 | + auto process_sp = |
| 43 | + std::make_shared<ScriptedProcess>(target_sp, listener_sp, launch_info); |
| 44 | + |
| 45 | + if (!process_sp || !process_sp->m_script_object_sp || |
| 46 | + !process_sp->m_script_object_sp->IsValid()) |
| 47 | + return nullptr; |
| 48 | + |
| 49 | + return process_sp; |
| 50 | +} |
| 51 | + |
| 52 | +bool ScriptedProcess::CanDebug(lldb::TargetSP target_sp, |
| 53 | + bool plugin_specified_by_name) { |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +ScriptedProcess::ScriptedProcess(lldb::TargetSP target_sp, |
| 58 | + lldb::ListenerSP listener_sp, |
| 59 | + const ScriptedProcess::LaunchInfo &launch_info) |
| 60 | + : Process(target_sp, listener_sp), m_launch_info(launch_info), |
| 61 | + m_interpreter(nullptr), m_script_object_sp(nullptr) { |
| 62 | + if (!target_sp) |
| 63 | + return; |
| 64 | + |
| 65 | + m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); |
| 66 | + |
| 67 | + if (!m_interpreter) |
| 68 | + return; |
| 69 | + |
| 70 | + StructuredData::ObjectSP object_sp = GetInterface().CreatePluginObject( |
| 71 | + m_launch_info.GetClassName().c_str(), target_sp, |
| 72 | + m_launch_info.GetDictionarySP()); |
| 73 | + |
| 74 | + if (object_sp && object_sp->IsValid()) |
| 75 | + m_script_object_sp = object_sp; |
| 76 | +} |
| 77 | + |
| 78 | +ScriptedProcess::~ScriptedProcess() { |
| 79 | + Clear(); |
| 80 | + // We need to call finalize on the process before destroying ourselves to |
| 81 | + // make sure all of the broadcaster cleanup goes as planned. If we destruct |
| 82 | + // this class, then Process::~Process() might have problems trying to fully |
| 83 | + // destroy the broadcaster. |
| 84 | + Finalize(); |
| 85 | +} |
| 86 | + |
| 87 | +void ScriptedProcess::Initialize() { |
| 88 | + static llvm::once_flag g_once_flag; |
| 89 | + |
| 90 | + llvm::call_once(g_once_flag, []() { |
| 91 | + PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 92 | + GetPluginDescriptionStatic(), CreateInstance); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +void ScriptedProcess::Terminate() { |
| 97 | + PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance); |
| 98 | +} |
| 99 | + |
| 100 | +ConstString ScriptedProcess::GetPluginName() { return GetPluginNameStatic(); } |
| 101 | + |
| 102 | +uint32_t ScriptedProcess::GetPluginVersion() { return 1; } |
| 103 | + |
| 104 | +Status ScriptedProcess::DoLoadCore() { |
| 105 | + ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo(); |
| 106 | + |
| 107 | + return DoLaunch(nullptr, launch_info); |
| 108 | +} |
| 109 | + |
| 110 | +Status ScriptedProcess::DoLaunch(Module *exe_module, |
| 111 | + ProcessLaunchInfo &launch_info) { |
| 112 | + if (!m_interpreter) |
| 113 | + return Status("No interpreter."); |
| 114 | + |
| 115 | + if (!m_script_object_sp) |
| 116 | + return Status("No python object."); |
| 117 | + |
| 118 | + Status status = GetInterface().Launch(); |
| 119 | + |
| 120 | + if (status.Success()) { |
| 121 | + SetPrivateState(eStateRunning); |
| 122 | + SetPrivateState(eStateStopped); |
| 123 | + } |
| 124 | + |
| 125 | + return status; |
| 126 | +}; |
| 127 | + |
| 128 | +void ScriptedProcess::DidLaunch() { |
| 129 | + if (m_interpreter) |
| 130 | + m_pid = GetInterface().GetProcessID(); |
| 131 | +} |
| 132 | + |
| 133 | +Status ScriptedProcess::DoResume() { |
| 134 | + if (!m_interpreter) |
| 135 | + return Status("No interpreter."); |
| 136 | + |
| 137 | + if (!m_script_object_sp) |
| 138 | + return Status("No python object."); |
| 139 | + |
| 140 | + Status status = GetInterface().Resume(); |
| 141 | + |
| 142 | + if (status.Success()) { |
| 143 | + SetPrivateState(eStateRunning); |
| 144 | + SetPrivateState(eStateStopped); |
| 145 | + } |
| 146 | + |
| 147 | + return status; |
| 148 | +} |
| 149 | + |
| 150 | +Status ScriptedProcess::DoDestroy() { return Status(); } |
| 151 | + |
| 152 | +bool ScriptedProcess::IsAlive() { |
| 153 | + if (!m_interpreter) |
| 154 | + return false; |
| 155 | + |
| 156 | + return GetInterface().IsAlive(); |
| 157 | +} |
| 158 | + |
| 159 | +size_t ScriptedProcess::ReadMemory(lldb::addr_t addr, void *buf, size_t size, |
| 160 | + Status &error) { |
| 161 | + return DoReadMemory(addr, buf, size, error); |
| 162 | +} |
| 163 | + |
| 164 | +size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, |
| 165 | + Status &error) { |
| 166 | + |
| 167 | + auto error_with_message = [&error](llvm::StringRef message) { |
| 168 | + error.SetErrorString(message); |
| 169 | + return LLDB_INVALID_ADDRESS; |
| 170 | + }; |
| 171 | + |
| 172 | + if (!m_interpreter) |
| 173 | + return error_with_message("No interpreter."); |
| 174 | + |
| 175 | + lldb::DataExtractorSP data_extractor_sp = |
| 176 | + GetInterface().ReadMemoryAtAddress(addr, size, error); |
| 177 | + |
| 178 | + if (!data_extractor_sp || error.Fail()) |
| 179 | + return LLDB_INVALID_ADDRESS; |
| 180 | + |
| 181 | + if (data_extractor_sp->GetByteSize() != size) |
| 182 | + return error_with_message("Failed to read requested memory size."); |
| 183 | + |
| 184 | + if (data_extractor_sp->CopyData(0, size, buf) <= size) |
| 185 | + return error_with_message("Failed to copy read memory to buffer."); |
| 186 | + |
| 187 | + return size; |
| 188 | +} |
| 189 | + |
| 190 | +ArchSpec ScriptedProcess::GetArchitecture() { |
| 191 | + return GetTarget().GetArchitecture(); |
| 192 | +} |
| 193 | + |
| 194 | +Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr, |
| 195 | + MemoryRegionInfo ®ion) { |
| 196 | + return Status(); |
| 197 | +} |
| 198 | + |
| 199 | +Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos ®ion_list) { |
| 200 | + Status error; |
| 201 | + |
| 202 | + if (!m_interpreter) { |
| 203 | + error.SetErrorString("No interpreter."); |
| 204 | + return error; |
| 205 | + } |
| 206 | + |
| 207 | + lldb::addr_t address = 0; |
| 208 | + lldb::MemoryRegionInfoSP mem_region_sp = nullptr; |
| 209 | + |
| 210 | + while ((mem_region_sp = |
| 211 | + GetInterface().GetMemoryRegionContainingAddress(address))) { |
| 212 | + auto range = mem_region_sp->GetRange(); |
| 213 | + address += range.GetRangeBase() + range.GetByteSize(); |
| 214 | + region_list.push_back(*mem_region_sp.get()); |
| 215 | + } |
| 216 | + |
| 217 | + return error; |
| 218 | +} |
| 219 | + |
| 220 | +void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); } |
| 221 | + |
| 222 | +bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list, |
| 223 | + ThreadList &new_thread_list) { |
| 224 | + return new_thread_list.GetSize(false) > 0; |
| 225 | +} |
| 226 | + |
| 227 | +bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) { |
| 228 | + info.Clear(); |
| 229 | + info.SetProcessID(GetID()); |
| 230 | + info.SetArchitecture(GetArchitecture()); |
| 231 | + lldb::ModuleSP module_sp = GetTarget().GetExecutableModule(); |
| 232 | + if (module_sp) { |
| 233 | + const bool add_exe_file_as_first_arg = false; |
| 234 | + info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(), |
| 235 | + add_exe_file_as_first_arg); |
| 236 | + } |
| 237 | + return true; |
| 238 | +} |
| 239 | + |
| 240 | +ScriptedProcessInterface &ScriptedProcess::GetInterface() const { |
| 241 | + return m_interpreter->GetScriptedProcessInterface(); |
| 242 | +} |
0 commit comments