1fe6060f1SDimitry Andric //===-- ScriptedProcess.cpp -----------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #include "ScriptedProcess.h" 10fe6060f1SDimitry Andric 11fe6060f1SDimitry Andric #include "lldb/Core/Debugger.h" 12fe6060f1SDimitry Andric #include "lldb/Core/Module.h" 13fe6060f1SDimitry Andric #include "lldb/Core/PluginManager.h" 14fe6060f1SDimitry Andric 15fe6060f1SDimitry Andric #include "lldb/Host/OptionParser.h" 16fe6060f1SDimitry Andric #include "lldb/Host/ThreadLauncher.h" 17fe6060f1SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 18fe6060f1SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h" 19fe6060f1SDimitry Andric #include "lldb/Interpreter/OptionGroupBoolean.h" 20fe6060f1SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h" 21fe6060f1SDimitry Andric #include "lldb/Target/MemoryRegionInfo.h" 22fe6060f1SDimitry Andric #include "lldb/Target/RegisterContext.h" 23fe6060f1SDimitry Andric 24fe6060f1SDimitry Andric #include "lldb/Utility/Log.h" 25fe6060f1SDimitry Andric #include "lldb/Utility/Logging.h" 26fe6060f1SDimitry Andric #include "lldb/Utility/State.h" 27fe6060f1SDimitry Andric 28fe6060f1SDimitry Andric #include <mutex> 29fe6060f1SDimitry Andric 30fe6060f1SDimitry Andric LLDB_PLUGIN_DEFINE(ScriptedProcess) 31fe6060f1SDimitry Andric 32fe6060f1SDimitry Andric using namespace lldb; 33fe6060f1SDimitry Andric using namespace lldb_private; 34fe6060f1SDimitry Andric 35fe6060f1SDimitry Andric ConstString ScriptedProcess::GetPluginNameStatic() { 36fe6060f1SDimitry Andric static ConstString g_name("ScriptedProcess"); 37fe6060f1SDimitry Andric return g_name; 38fe6060f1SDimitry Andric } 39fe6060f1SDimitry Andric 40fe6060f1SDimitry Andric const char *ScriptedProcess::GetPluginDescriptionStatic() { 41fe6060f1SDimitry Andric return "Scripted Process plug-in."; 42fe6060f1SDimitry Andric } 43fe6060f1SDimitry Andric 44fe6060f1SDimitry Andric static constexpr lldb::ScriptLanguage g_supported_script_languages[] = { 45fe6060f1SDimitry Andric ScriptLanguage::eScriptLanguagePython, 46fe6060f1SDimitry Andric }; 47fe6060f1SDimitry Andric 48fe6060f1SDimitry Andric bool ScriptedProcess::IsScriptLanguageSupported(lldb::ScriptLanguage language) { 49fe6060f1SDimitry Andric llvm::ArrayRef<lldb::ScriptLanguage> supported_languages = 50fe6060f1SDimitry Andric llvm::makeArrayRef(g_supported_script_languages); 51fe6060f1SDimitry Andric 52fe6060f1SDimitry Andric return llvm::is_contained(supported_languages, language); 53fe6060f1SDimitry Andric } 54fe6060f1SDimitry Andric 55fe6060f1SDimitry Andric void ScriptedProcess::CheckInterpreterAndScriptObject() const { 56fe6060f1SDimitry Andric lldbassert(m_interpreter && "Invalid Script Interpreter."); 57fe6060f1SDimitry Andric lldbassert(m_script_object_sp && "Invalid Script Object."); 58fe6060f1SDimitry Andric } 59fe6060f1SDimitry Andric 60fe6060f1SDimitry Andric lldb::ProcessSP ScriptedProcess::CreateInstance(lldb::TargetSP target_sp, 61fe6060f1SDimitry Andric lldb::ListenerSP listener_sp, 62fe6060f1SDimitry Andric const FileSpec *file, 63fe6060f1SDimitry Andric bool can_connect) { 64fe6060f1SDimitry Andric if (!target_sp || 65fe6060f1SDimitry Andric !IsScriptLanguageSupported(target_sp->GetDebugger().GetScriptLanguage())) 66fe6060f1SDimitry Andric return nullptr; 67fe6060f1SDimitry Andric 68fe6060f1SDimitry Andric Status error; 69fe6060f1SDimitry Andric ScriptedProcess::ScriptedProcessInfo scripted_process_info( 70fe6060f1SDimitry Andric target_sp->GetProcessLaunchInfo()); 71fe6060f1SDimitry Andric 72fe6060f1SDimitry Andric auto process_sp = std::make_shared<ScriptedProcess>( 73fe6060f1SDimitry Andric target_sp, listener_sp, scripted_process_info, error); 74fe6060f1SDimitry Andric 75fe6060f1SDimitry Andric if (error.Fail() || !process_sp || !process_sp->m_script_object_sp || 76fe6060f1SDimitry Andric !process_sp->m_script_object_sp->IsValid()) { 77fe6060f1SDimitry Andric LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS), "%s", 78fe6060f1SDimitry Andric error.AsCString()); 79fe6060f1SDimitry Andric return nullptr; 80fe6060f1SDimitry Andric } 81fe6060f1SDimitry Andric 82fe6060f1SDimitry Andric return process_sp; 83fe6060f1SDimitry Andric } 84fe6060f1SDimitry Andric 85fe6060f1SDimitry Andric bool ScriptedProcess::CanDebug(lldb::TargetSP target_sp, 86fe6060f1SDimitry Andric bool plugin_specified_by_name) { 87fe6060f1SDimitry Andric return true; 88fe6060f1SDimitry Andric } 89fe6060f1SDimitry Andric 90fe6060f1SDimitry Andric ScriptedProcess::ScriptedProcess( 91fe6060f1SDimitry Andric lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, 92fe6060f1SDimitry Andric const ScriptedProcess::ScriptedProcessInfo &scripted_process_info, 93fe6060f1SDimitry Andric Status &error) 94fe6060f1SDimitry Andric : Process(target_sp, listener_sp), 95fe6060f1SDimitry Andric m_scripted_process_info(scripted_process_info) { 96fe6060f1SDimitry Andric 97fe6060f1SDimitry Andric if (!target_sp) { 98fe6060f1SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s", 99fe6060f1SDimitry Andric __FUNCTION__, "Invalid target"); 100fe6060f1SDimitry Andric return; 101fe6060f1SDimitry Andric } 102fe6060f1SDimitry Andric 103fe6060f1SDimitry Andric m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); 104fe6060f1SDimitry Andric 105fe6060f1SDimitry Andric if (!m_interpreter) { 106fe6060f1SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s", 107fe6060f1SDimitry Andric __FUNCTION__, 108fe6060f1SDimitry Andric "Debugger has no Script Interpreter"); 109fe6060f1SDimitry Andric return; 110fe6060f1SDimitry Andric } 111fe6060f1SDimitry Andric 112fe6060f1SDimitry Andric StructuredData::ObjectSP object_sp = GetInterface().CreatePluginObject( 113fe6060f1SDimitry Andric m_scripted_process_info.GetClassName().c_str(), target_sp, 114fe6060f1SDimitry Andric m_scripted_process_info.GetDictionarySP()); 115fe6060f1SDimitry Andric 116fe6060f1SDimitry Andric if (!object_sp || !object_sp->IsValid()) { 117fe6060f1SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s", 118fe6060f1SDimitry Andric __FUNCTION__, 119fe6060f1SDimitry Andric "Failed to create valid script object"); 120fe6060f1SDimitry Andric return; 121fe6060f1SDimitry Andric } 122fe6060f1SDimitry Andric 123fe6060f1SDimitry Andric m_script_object_sp = object_sp; 124fe6060f1SDimitry Andric } 125fe6060f1SDimitry Andric 126fe6060f1SDimitry Andric ScriptedProcess::~ScriptedProcess() { 127fe6060f1SDimitry Andric Clear(); 128fe6060f1SDimitry Andric // We need to call finalize on the process before destroying ourselves to 129fe6060f1SDimitry Andric // make sure all of the broadcaster cleanup goes as planned. If we destruct 130fe6060f1SDimitry Andric // this class, then Process::~Process() might have problems trying to fully 131fe6060f1SDimitry Andric // destroy the broadcaster. 132fe6060f1SDimitry Andric Finalize(); 133fe6060f1SDimitry Andric } 134fe6060f1SDimitry Andric 135fe6060f1SDimitry Andric void ScriptedProcess::Initialize() { 136fe6060f1SDimitry Andric static llvm::once_flag g_once_flag; 137fe6060f1SDimitry Andric 138fe6060f1SDimitry Andric llvm::call_once(g_once_flag, []() { 139fe6060f1SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(), 140fe6060f1SDimitry Andric GetPluginDescriptionStatic(), CreateInstance); 141fe6060f1SDimitry Andric }); 142fe6060f1SDimitry Andric } 143fe6060f1SDimitry Andric 144fe6060f1SDimitry Andric void ScriptedProcess::Terminate() { 145fe6060f1SDimitry Andric PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance); 146fe6060f1SDimitry Andric } 147fe6060f1SDimitry Andric 148fe6060f1SDimitry Andric ConstString ScriptedProcess::GetPluginName() { return GetPluginNameStatic(); } 149fe6060f1SDimitry Andric 150fe6060f1SDimitry Andric uint32_t ScriptedProcess::GetPluginVersion() { return 1; } 151fe6060f1SDimitry Andric 152fe6060f1SDimitry Andric Status ScriptedProcess::DoLoadCore() { 153fe6060f1SDimitry Andric ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo(); 154fe6060f1SDimitry Andric 155fe6060f1SDimitry Andric return DoLaunch(nullptr, launch_info); 156fe6060f1SDimitry Andric } 157fe6060f1SDimitry Andric 158fe6060f1SDimitry Andric Status ScriptedProcess::DoLaunch(Module *exe_module, 159fe6060f1SDimitry Andric ProcessLaunchInfo &launch_info) { 160fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 161fe6060f1SDimitry Andric 162fe6060f1SDimitry Andric /* FIXME: This doesn't reflect how lldb actually launches a process. 163fe6060f1SDimitry Andric In reality, it attaches to debugserver, then resume the process. */ 164fe6060f1SDimitry Andric Status error = GetInterface().Launch(); 165fe6060f1SDimitry Andric SetPrivateState(eStateRunning); 166fe6060f1SDimitry Andric 167fe6060f1SDimitry Andric if (error.Fail()) 168fe6060f1SDimitry Andric return error; 169fe6060f1SDimitry Andric 170fe6060f1SDimitry Andric // TODO: Fetch next state from stopped event queue then send stop event 171fe6060f1SDimitry Andric // const StateType state = SetThreadStopInfo(response); 172fe6060f1SDimitry Andric // if (state != eStateInvalid) { 173fe6060f1SDimitry Andric // SetPrivateState(state); 174fe6060f1SDimitry Andric 175fe6060f1SDimitry Andric SetPrivateState(eStateStopped); 176fe6060f1SDimitry Andric 177fe6060f1SDimitry Andric UpdateThreadListIfNeeded(); 178fe6060f1SDimitry Andric GetThreadList(); 179fe6060f1SDimitry Andric 180fe6060f1SDimitry Andric return {}; 181fe6060f1SDimitry Andric } 182fe6060f1SDimitry Andric 183fe6060f1SDimitry Andric void ScriptedProcess::DidLaunch() { 184fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 185fe6060f1SDimitry Andric m_pid = GetInterface().GetProcessID(); 186fe6060f1SDimitry Andric } 187fe6060f1SDimitry Andric 188fe6060f1SDimitry Andric Status ScriptedProcess::DoResume() { 189fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 190fe6060f1SDimitry Andric 191fe6060f1SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 192fe6060f1SDimitry Andric // FIXME: Fetch data from thread. 193fe6060f1SDimitry Andric const StateType thread_resume_state = eStateRunning; 194fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s thread_resume_state = %s", __FUNCTION__, 195fe6060f1SDimitry Andric StateAsCString(thread_resume_state)); 196fe6060f1SDimitry Andric 197fe6060f1SDimitry Andric bool resume = (thread_resume_state == eStateRunning); 198fe6060f1SDimitry Andric assert(thread_resume_state == eStateRunning && "invalid thread resume state"); 199fe6060f1SDimitry Andric 200fe6060f1SDimitry Andric Status error; 201fe6060f1SDimitry Andric if (resume) { 202fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s sending resume", __FUNCTION__); 203fe6060f1SDimitry Andric 204fe6060f1SDimitry Andric SetPrivateState(eStateRunning); 205fe6060f1SDimitry Andric SetPrivateState(eStateStopped); 206fe6060f1SDimitry Andric error = GetInterface().Resume(); 207fe6060f1SDimitry Andric } 208fe6060f1SDimitry Andric 209fe6060f1SDimitry Andric return error; 210fe6060f1SDimitry Andric } 211fe6060f1SDimitry Andric 212fe6060f1SDimitry Andric Status ScriptedProcess::DoStop() { 213fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 214fe6060f1SDimitry Andric 215fe6060f1SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 216fe6060f1SDimitry Andric 217fe6060f1SDimitry Andric if (GetInterface().ShouldStop()) { 218fe6060f1SDimitry Andric SetPrivateState(eStateStopped); 219fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s Immediate stop", __FUNCTION__); 220fe6060f1SDimitry Andric return {}; 221fe6060f1SDimitry Andric } 222fe6060f1SDimitry Andric 223fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s Delayed stop", __FUNCTION__); 224fe6060f1SDimitry Andric return GetInterface().Stop(); 225fe6060f1SDimitry Andric } 226fe6060f1SDimitry Andric 227fe6060f1SDimitry Andric Status ScriptedProcess::DoDestroy() { return Status(); } 228fe6060f1SDimitry Andric 229fe6060f1SDimitry Andric bool ScriptedProcess::IsAlive() { 230fe6060f1SDimitry Andric if (m_interpreter && m_script_object_sp) 231fe6060f1SDimitry Andric return GetInterface().IsAlive(); 232fe6060f1SDimitry Andric return false; 233fe6060f1SDimitry Andric } 234fe6060f1SDimitry Andric 235fe6060f1SDimitry Andric size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, 236fe6060f1SDimitry Andric Status &error) { 237fe6060f1SDimitry Andric 238fe6060f1SDimitry Andric auto error_with_message = [&error](llvm::StringRef message) { 239fe6060f1SDimitry Andric error.SetErrorString(message); 240fe6060f1SDimitry Andric return 0; 241fe6060f1SDimitry Andric }; 242fe6060f1SDimitry Andric 243fe6060f1SDimitry Andric if (!m_interpreter) 244fe6060f1SDimitry Andric return error_with_message("No interpreter."); 245fe6060f1SDimitry Andric 246fe6060f1SDimitry Andric lldb::DataExtractorSP data_extractor_sp = 247fe6060f1SDimitry Andric GetInterface().ReadMemoryAtAddress(addr, size, error); 248fe6060f1SDimitry Andric 249fe6060f1SDimitry Andric if (!data_extractor_sp || error.Fail()) 250fe6060f1SDimitry Andric return 0; 251fe6060f1SDimitry Andric 252fe6060f1SDimitry Andric offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData( 253fe6060f1SDimitry Andric 0, data_extractor_sp->GetByteSize(), buf, size, GetByteOrder()); 254fe6060f1SDimitry Andric 255fe6060f1SDimitry Andric if (!bytes_copied || bytes_copied == LLDB_INVALID_OFFSET) 256fe6060f1SDimitry Andric return error_with_message("Failed to copy read memory to buffer."); 257fe6060f1SDimitry Andric 258fe6060f1SDimitry Andric return size; 259fe6060f1SDimitry Andric } 260fe6060f1SDimitry Andric 261fe6060f1SDimitry Andric ArchSpec ScriptedProcess::GetArchitecture() { 262fe6060f1SDimitry Andric return GetTarget().GetArchitecture(); 263fe6060f1SDimitry Andric } 264fe6060f1SDimitry Andric 265fe6060f1SDimitry Andric Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr, 266fe6060f1SDimitry Andric MemoryRegionInfo ®ion) { 267fe6060f1SDimitry Andric // TODO: Implement 268fe6060f1SDimitry Andric return Status(); 269fe6060f1SDimitry Andric } 270fe6060f1SDimitry Andric 271fe6060f1SDimitry Andric Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos ®ion_list) { 272fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 273fe6060f1SDimitry Andric 274fe6060f1SDimitry Andric lldb::addr_t address = 0; 275fe6060f1SDimitry Andric lldb::MemoryRegionInfoSP mem_region_sp = nullptr; 276fe6060f1SDimitry Andric 277fe6060f1SDimitry Andric while ((mem_region_sp = 278fe6060f1SDimitry Andric GetInterface().GetMemoryRegionContainingAddress(address))) { 279fe6060f1SDimitry Andric auto range = mem_region_sp->GetRange(); 280fe6060f1SDimitry Andric address += range.GetRangeBase() + range.GetByteSize(); 281fe6060f1SDimitry Andric region_list.push_back(*mem_region_sp.get()); 282fe6060f1SDimitry Andric } 283fe6060f1SDimitry Andric 284fe6060f1SDimitry Andric return {}; 285fe6060f1SDimitry Andric } 286fe6060f1SDimitry Andric 287fe6060f1SDimitry Andric void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); } 288fe6060f1SDimitry Andric 289fe6060f1SDimitry Andric bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list, 290fe6060f1SDimitry Andric ThreadList &new_thread_list) { 291fe6060f1SDimitry Andric // TODO: Implement 292fe6060f1SDimitry Andric // This is supposed to get the current set of threads, if any of them are in 293fe6060f1SDimitry Andric // old_thread_list then they get copied to new_thread_list, and then any 294fe6060f1SDimitry Andric // actually new threads will get added to new_thread_list. 295fe6060f1SDimitry Andric return new_thread_list.GetSize(false) > 0; 296fe6060f1SDimitry Andric } 297fe6060f1SDimitry Andric 298fe6060f1SDimitry Andric bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) { 299fe6060f1SDimitry Andric info.Clear(); 300fe6060f1SDimitry Andric info.SetProcessID(GetID()); 301fe6060f1SDimitry Andric info.SetArchitecture(GetArchitecture()); 302fe6060f1SDimitry Andric lldb::ModuleSP module_sp = GetTarget().GetExecutableModule(); 303fe6060f1SDimitry Andric if (module_sp) { 304fe6060f1SDimitry Andric const bool add_exe_file_as_first_arg = false; 305fe6060f1SDimitry Andric info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(), 306fe6060f1SDimitry Andric add_exe_file_as_first_arg); 307fe6060f1SDimitry Andric } 308fe6060f1SDimitry Andric return true; 309fe6060f1SDimitry Andric } 310fe6060f1SDimitry Andric 311fe6060f1SDimitry Andric ScriptedProcessInterface &ScriptedProcess::GetInterface() const { 312fe6060f1SDimitry Andric return m_interpreter->GetScriptedProcessInterface(); 313fe6060f1SDimitry Andric } 314