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 #include "lldb/Utility/State.h" 24fe6060f1SDimitry Andric 25fe6060f1SDimitry Andric #include <mutex> 26fe6060f1SDimitry Andric 27fe6060f1SDimitry Andric LLDB_PLUGIN_DEFINE(ScriptedProcess) 28fe6060f1SDimitry Andric 29fe6060f1SDimitry Andric using namespace lldb; 30fe6060f1SDimitry Andric using namespace lldb_private; 31fe6060f1SDimitry Andric 32349cc55cSDimitry Andric llvm::StringRef ScriptedProcess::GetPluginDescriptionStatic() { 33fe6060f1SDimitry Andric return "Scripted Process plug-in."; 34fe6060f1SDimitry Andric } 35fe6060f1SDimitry Andric 36fe6060f1SDimitry Andric static constexpr lldb::ScriptLanguage g_supported_script_languages[] = { 37fe6060f1SDimitry Andric ScriptLanguage::eScriptLanguagePython, 38fe6060f1SDimitry Andric }; 39fe6060f1SDimitry Andric 40fe6060f1SDimitry Andric bool ScriptedProcess::IsScriptLanguageSupported(lldb::ScriptLanguage language) { 41fe6060f1SDimitry Andric llvm::ArrayRef<lldb::ScriptLanguage> supported_languages = 42fe6060f1SDimitry Andric llvm::makeArrayRef(g_supported_script_languages); 43fe6060f1SDimitry Andric 44fe6060f1SDimitry Andric return llvm::is_contained(supported_languages, language); 45fe6060f1SDimitry Andric } 46fe6060f1SDimitry Andric 47fe6060f1SDimitry Andric void ScriptedProcess::CheckInterpreterAndScriptObject() const { 48fe6060f1SDimitry Andric lldbassert(m_interpreter && "Invalid Script Interpreter."); 49fe6060f1SDimitry Andric lldbassert(m_script_object_sp && "Invalid Script Object."); 50fe6060f1SDimitry Andric } 51fe6060f1SDimitry Andric 52fe6060f1SDimitry Andric lldb::ProcessSP ScriptedProcess::CreateInstance(lldb::TargetSP target_sp, 53fe6060f1SDimitry Andric lldb::ListenerSP listener_sp, 54fe6060f1SDimitry Andric const FileSpec *file, 55fe6060f1SDimitry Andric bool can_connect) { 56fe6060f1SDimitry Andric if (!target_sp || 57fe6060f1SDimitry Andric !IsScriptLanguageSupported(target_sp->GetDebugger().GetScriptLanguage())) 58fe6060f1SDimitry Andric return nullptr; 59fe6060f1SDimitry Andric 60fe6060f1SDimitry Andric Status error; 61fe6060f1SDimitry Andric ScriptedProcess::ScriptedProcessInfo scripted_process_info( 62fe6060f1SDimitry Andric target_sp->GetProcessLaunchInfo()); 63fe6060f1SDimitry Andric 64fe6060f1SDimitry Andric auto process_sp = std::make_shared<ScriptedProcess>( 65fe6060f1SDimitry Andric target_sp, listener_sp, scripted_process_info, error); 66fe6060f1SDimitry Andric 67fe6060f1SDimitry Andric if (error.Fail() || !process_sp || !process_sp->m_script_object_sp || 68fe6060f1SDimitry Andric !process_sp->m_script_object_sp->IsValid()) { 69fe6060f1SDimitry Andric LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS), "%s", 70fe6060f1SDimitry Andric error.AsCString()); 71fe6060f1SDimitry Andric return nullptr; 72fe6060f1SDimitry Andric } 73fe6060f1SDimitry Andric 74fe6060f1SDimitry Andric return process_sp; 75fe6060f1SDimitry Andric } 76fe6060f1SDimitry Andric 77fe6060f1SDimitry Andric bool ScriptedProcess::CanDebug(lldb::TargetSP target_sp, 78fe6060f1SDimitry Andric bool plugin_specified_by_name) { 79fe6060f1SDimitry Andric return true; 80fe6060f1SDimitry Andric } 81fe6060f1SDimitry Andric 82fe6060f1SDimitry Andric ScriptedProcess::ScriptedProcess( 83fe6060f1SDimitry Andric lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, 84fe6060f1SDimitry Andric const ScriptedProcess::ScriptedProcessInfo &scripted_process_info, 85fe6060f1SDimitry Andric Status &error) 86fe6060f1SDimitry Andric : Process(target_sp, listener_sp), 87fe6060f1SDimitry Andric m_scripted_process_info(scripted_process_info) { 88fe6060f1SDimitry Andric 89fe6060f1SDimitry Andric if (!target_sp) { 90fe6060f1SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s", 91fe6060f1SDimitry Andric __FUNCTION__, "Invalid target"); 92fe6060f1SDimitry Andric return; 93fe6060f1SDimitry Andric } 94fe6060f1SDimitry Andric 95fe6060f1SDimitry Andric m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); 96fe6060f1SDimitry Andric 97fe6060f1SDimitry Andric if (!m_interpreter) { 98fe6060f1SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s", 99fe6060f1SDimitry Andric __FUNCTION__, 100fe6060f1SDimitry Andric "Debugger has no Script Interpreter"); 101fe6060f1SDimitry Andric return; 102fe6060f1SDimitry Andric } 103fe6060f1SDimitry Andric 104349cc55cSDimitry Andric ExecutionContext exe_ctx(target_sp, /*get_process=*/false); 105349cc55cSDimitry Andric 106349cc55cSDimitry Andric StructuredData::GenericSP object_sp = GetInterface().CreatePluginObject( 107349cc55cSDimitry Andric m_scripted_process_info.GetClassName().c_str(), exe_ctx, 108349cc55cSDimitry Andric m_scripted_process_info.GetArgsSP()); 109fe6060f1SDimitry Andric 110fe6060f1SDimitry Andric if (!object_sp || !object_sp->IsValid()) { 111fe6060f1SDimitry Andric error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s", 112fe6060f1SDimitry Andric __FUNCTION__, 113fe6060f1SDimitry Andric "Failed to create valid script object"); 114fe6060f1SDimitry Andric return; 115fe6060f1SDimitry Andric } 116fe6060f1SDimitry Andric 117fe6060f1SDimitry Andric m_script_object_sp = object_sp; 118fe6060f1SDimitry Andric } 119fe6060f1SDimitry Andric 120fe6060f1SDimitry Andric ScriptedProcess::~ScriptedProcess() { 121fe6060f1SDimitry Andric Clear(); 122fe6060f1SDimitry Andric // We need to call finalize on the process before destroying ourselves to 123fe6060f1SDimitry Andric // make sure all of the broadcaster cleanup goes as planned. If we destruct 124fe6060f1SDimitry Andric // this class, then Process::~Process() might have problems trying to fully 125fe6060f1SDimitry Andric // destroy the broadcaster. 126fe6060f1SDimitry Andric Finalize(); 127fe6060f1SDimitry Andric } 128fe6060f1SDimitry Andric 129fe6060f1SDimitry Andric void ScriptedProcess::Initialize() { 130fe6060f1SDimitry Andric static llvm::once_flag g_once_flag; 131fe6060f1SDimitry Andric 132fe6060f1SDimitry Andric llvm::call_once(g_once_flag, []() { 133fe6060f1SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(), 134fe6060f1SDimitry Andric GetPluginDescriptionStatic(), CreateInstance); 135fe6060f1SDimitry Andric }); 136fe6060f1SDimitry Andric } 137fe6060f1SDimitry Andric 138fe6060f1SDimitry Andric void ScriptedProcess::Terminate() { 139fe6060f1SDimitry Andric PluginManager::UnregisterPlugin(ScriptedProcess::CreateInstance); 140fe6060f1SDimitry Andric } 141fe6060f1SDimitry Andric 142fe6060f1SDimitry Andric Status ScriptedProcess::DoLoadCore() { 143fe6060f1SDimitry Andric ProcessLaunchInfo launch_info = GetTarget().GetProcessLaunchInfo(); 144fe6060f1SDimitry Andric 145fe6060f1SDimitry Andric return DoLaunch(nullptr, launch_info); 146fe6060f1SDimitry Andric } 147fe6060f1SDimitry Andric 148fe6060f1SDimitry Andric Status ScriptedProcess::DoLaunch(Module *exe_module, 149fe6060f1SDimitry Andric ProcessLaunchInfo &launch_info) { 150fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 151fe6060f1SDimitry Andric 152fe6060f1SDimitry Andric /* FIXME: This doesn't reflect how lldb actually launches a process. 153fe6060f1SDimitry Andric In reality, it attaches to debugserver, then resume the process. */ 154fe6060f1SDimitry Andric Status error = GetInterface().Launch(); 155fe6060f1SDimitry Andric SetPrivateState(eStateRunning); 156fe6060f1SDimitry Andric 157fe6060f1SDimitry Andric if (error.Fail()) 158fe6060f1SDimitry Andric return error; 159fe6060f1SDimitry Andric 160fe6060f1SDimitry Andric // TODO: Fetch next state from stopped event queue then send stop event 161fe6060f1SDimitry Andric // const StateType state = SetThreadStopInfo(response); 162fe6060f1SDimitry Andric // if (state != eStateInvalid) { 163fe6060f1SDimitry Andric // SetPrivateState(state); 164fe6060f1SDimitry Andric 165fe6060f1SDimitry Andric SetPrivateState(eStateStopped); 166fe6060f1SDimitry Andric 167fe6060f1SDimitry Andric UpdateThreadListIfNeeded(); 168fe6060f1SDimitry Andric GetThreadList(); 169fe6060f1SDimitry Andric 170fe6060f1SDimitry Andric return {}; 171fe6060f1SDimitry Andric } 172fe6060f1SDimitry Andric 173fe6060f1SDimitry Andric void ScriptedProcess::DidLaunch() { 174fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 175fe6060f1SDimitry Andric m_pid = GetInterface().GetProcessID(); 176fe6060f1SDimitry Andric } 177fe6060f1SDimitry Andric 178fe6060f1SDimitry Andric Status ScriptedProcess::DoResume() { 179fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 180fe6060f1SDimitry Andric 181fe6060f1SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 182fe6060f1SDimitry Andric // FIXME: Fetch data from thread. 183fe6060f1SDimitry Andric const StateType thread_resume_state = eStateRunning; 184fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s thread_resume_state = %s", __FUNCTION__, 185fe6060f1SDimitry Andric StateAsCString(thread_resume_state)); 186fe6060f1SDimitry Andric 187fe6060f1SDimitry Andric bool resume = (thread_resume_state == eStateRunning); 188fe6060f1SDimitry Andric assert(thread_resume_state == eStateRunning && "invalid thread resume state"); 189fe6060f1SDimitry Andric 190fe6060f1SDimitry Andric Status error; 191fe6060f1SDimitry Andric if (resume) { 192fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s sending resume", __FUNCTION__); 193fe6060f1SDimitry Andric 194fe6060f1SDimitry Andric SetPrivateState(eStateRunning); 195fe6060f1SDimitry Andric SetPrivateState(eStateStopped); 196fe6060f1SDimitry Andric error = GetInterface().Resume(); 197fe6060f1SDimitry Andric } 198fe6060f1SDimitry Andric 199fe6060f1SDimitry Andric return error; 200fe6060f1SDimitry Andric } 201fe6060f1SDimitry Andric 202fe6060f1SDimitry Andric Status ScriptedProcess::DoStop() { 203fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 204fe6060f1SDimitry Andric 205fe6060f1SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 206fe6060f1SDimitry Andric 207fe6060f1SDimitry Andric if (GetInterface().ShouldStop()) { 208fe6060f1SDimitry Andric SetPrivateState(eStateStopped); 209fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s Immediate stop", __FUNCTION__); 210fe6060f1SDimitry Andric return {}; 211fe6060f1SDimitry Andric } 212fe6060f1SDimitry Andric 213fe6060f1SDimitry Andric LLDB_LOGF(log, "ScriptedProcess::%s Delayed stop", __FUNCTION__); 214fe6060f1SDimitry Andric return GetInterface().Stop(); 215fe6060f1SDimitry Andric } 216fe6060f1SDimitry Andric 217fe6060f1SDimitry Andric Status ScriptedProcess::DoDestroy() { return Status(); } 218fe6060f1SDimitry Andric 219fe6060f1SDimitry Andric bool ScriptedProcess::IsAlive() { 220fe6060f1SDimitry Andric if (m_interpreter && m_script_object_sp) 221fe6060f1SDimitry Andric return GetInterface().IsAlive(); 222fe6060f1SDimitry Andric return false; 223fe6060f1SDimitry Andric } 224fe6060f1SDimitry Andric 225fe6060f1SDimitry Andric size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, 226fe6060f1SDimitry Andric Status &error) { 227fe6060f1SDimitry Andric if (!m_interpreter) 228349cc55cSDimitry Andric return GetInterface().ErrorWithMessage<size_t>(LLVM_PRETTY_FUNCTION, 229349cc55cSDimitry Andric "No interpreter.", error); 230fe6060f1SDimitry Andric 231fe6060f1SDimitry Andric lldb::DataExtractorSP data_extractor_sp = 232fe6060f1SDimitry Andric GetInterface().ReadMemoryAtAddress(addr, size, error); 233fe6060f1SDimitry Andric 234349cc55cSDimitry Andric if (!data_extractor_sp || !data_extractor_sp->GetByteSize() || error.Fail()) 235fe6060f1SDimitry Andric return 0; 236fe6060f1SDimitry Andric 237fe6060f1SDimitry Andric offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData( 238fe6060f1SDimitry Andric 0, data_extractor_sp->GetByteSize(), buf, size, GetByteOrder()); 239fe6060f1SDimitry Andric 240fe6060f1SDimitry Andric if (!bytes_copied || bytes_copied == LLDB_INVALID_OFFSET) 241349cc55cSDimitry Andric return GetInterface().ErrorWithMessage<size_t>( 242349cc55cSDimitry Andric LLVM_PRETTY_FUNCTION, "Failed to copy read memory to buffer.", error); 243fe6060f1SDimitry Andric 244fe6060f1SDimitry Andric return size; 245fe6060f1SDimitry Andric } 246fe6060f1SDimitry Andric 247fe6060f1SDimitry Andric ArchSpec ScriptedProcess::GetArchitecture() { 248fe6060f1SDimitry Andric return GetTarget().GetArchitecture(); 249fe6060f1SDimitry Andric } 250fe6060f1SDimitry Andric 2514824e7fdSDimitry Andric Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr, 252fe6060f1SDimitry Andric MemoryRegionInfo ®ion) { 253349cc55cSDimitry Andric CheckInterpreterAndScriptObject(); 254349cc55cSDimitry Andric 255349cc55cSDimitry Andric Status error; 256349cc55cSDimitry Andric if (auto region_or_err = 257349cc55cSDimitry Andric GetInterface().GetMemoryRegionContainingAddress(load_addr, error)) 258349cc55cSDimitry Andric region = *region_or_err; 259349cc55cSDimitry Andric 260349cc55cSDimitry Andric return error; 261fe6060f1SDimitry Andric } 262fe6060f1SDimitry Andric 263fe6060f1SDimitry Andric Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos ®ion_list) { 264fe6060f1SDimitry Andric CheckInterpreterAndScriptObject(); 265fe6060f1SDimitry Andric 266349cc55cSDimitry Andric Status error; 267fe6060f1SDimitry Andric lldb::addr_t address = 0; 268fe6060f1SDimitry Andric 269349cc55cSDimitry Andric while (auto region_or_err = 270349cc55cSDimitry Andric GetInterface().GetMemoryRegionContainingAddress(address, error)) { 271349cc55cSDimitry Andric if (error.Fail()) 272349cc55cSDimitry Andric break; 273349cc55cSDimitry Andric 274349cc55cSDimitry Andric MemoryRegionInfo &mem_region = *region_or_err; 275349cc55cSDimitry Andric auto range = mem_region.GetRange(); 276fe6060f1SDimitry Andric address += range.GetRangeBase() + range.GetByteSize(); 277349cc55cSDimitry Andric region_list.push_back(mem_region); 278fe6060f1SDimitry Andric } 279fe6060f1SDimitry Andric 280349cc55cSDimitry Andric return error; 281fe6060f1SDimitry Andric } 282fe6060f1SDimitry Andric 283fe6060f1SDimitry Andric void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); } 284fe6060f1SDimitry Andric 285fe6060f1SDimitry Andric bool ScriptedProcess::DoUpdateThreadList(ThreadList &old_thread_list, 286fe6060f1SDimitry Andric ThreadList &new_thread_list) { 287fe6060f1SDimitry Andric // TODO: Implement 288fe6060f1SDimitry Andric // This is supposed to get the current set of threads, if any of them are in 289fe6060f1SDimitry Andric // old_thread_list then they get copied to new_thread_list, and then any 290fe6060f1SDimitry Andric // actually new threads will get added to new_thread_list. 291349cc55cSDimitry Andric 292349cc55cSDimitry Andric CheckInterpreterAndScriptObject(); 293349cc55cSDimitry Andric m_thread_plans.ClearThreadCache(); 294349cc55cSDimitry Andric 295349cc55cSDimitry Andric Status error; 296349cc55cSDimitry Andric ScriptLanguage language = m_interpreter->GetLanguage(); 297349cc55cSDimitry Andric 298349cc55cSDimitry Andric if (language != eScriptLanguagePython) 299349cc55cSDimitry Andric return GetInterface().ErrorWithMessage<bool>( 300349cc55cSDimitry Andric LLVM_PRETTY_FUNCTION, 301349cc55cSDimitry Andric llvm::Twine("ScriptInterpreter language (" + 302349cc55cSDimitry Andric llvm::Twine(m_interpreter->LanguageToString(language)) + 303349cc55cSDimitry Andric llvm::Twine(") not supported.")) 304349cc55cSDimitry Andric .str(), 305349cc55cSDimitry Andric error); 306349cc55cSDimitry Andric 307349cc55cSDimitry Andric lldb::ThreadSP thread_sp; 308349cc55cSDimitry Andric thread_sp = std::make_shared<ScriptedThread>(*this, error); 309349cc55cSDimitry Andric 310349cc55cSDimitry Andric if (!thread_sp || error.Fail()) 311349cc55cSDimitry Andric return GetInterface().ErrorWithMessage<bool>(LLVM_PRETTY_FUNCTION, 312349cc55cSDimitry Andric error.AsCString(), error); 313349cc55cSDimitry Andric 314*0eae32dcSDimitry Andric RegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext(); 315*0eae32dcSDimitry Andric if (!reg_ctx_sp) 316*0eae32dcSDimitry Andric return GetInterface().ErrorWithMessage<bool>( 317*0eae32dcSDimitry Andric LLVM_PRETTY_FUNCTION, "Invalid Register Context", error); 318*0eae32dcSDimitry Andric 319349cc55cSDimitry Andric new_thread_list.AddThread(thread_sp); 320349cc55cSDimitry Andric 321fe6060f1SDimitry Andric return new_thread_list.GetSize(false) > 0; 322fe6060f1SDimitry Andric } 323fe6060f1SDimitry Andric 324349cc55cSDimitry Andric void ScriptedProcess::RefreshStateAfterStop() { 325349cc55cSDimitry Andric // Let all threads recover from stopping and do any clean up based on the 326349cc55cSDimitry Andric // previous thread state (if any). 327349cc55cSDimitry Andric m_thread_list.RefreshStateAfterStop(); 328349cc55cSDimitry Andric } 329349cc55cSDimitry Andric 330fe6060f1SDimitry Andric bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) { 331fe6060f1SDimitry Andric info.Clear(); 332fe6060f1SDimitry Andric info.SetProcessID(GetID()); 333fe6060f1SDimitry Andric info.SetArchitecture(GetArchitecture()); 334fe6060f1SDimitry Andric lldb::ModuleSP module_sp = GetTarget().GetExecutableModule(); 335fe6060f1SDimitry Andric if (module_sp) { 336fe6060f1SDimitry Andric const bool add_exe_file_as_first_arg = false; 337fe6060f1SDimitry Andric info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(), 338fe6060f1SDimitry Andric add_exe_file_as_first_arg); 339fe6060f1SDimitry Andric } 340fe6060f1SDimitry Andric return true; 341fe6060f1SDimitry Andric } 342fe6060f1SDimitry Andric 343fe6060f1SDimitry Andric ScriptedProcessInterface &ScriptedProcess::GetInterface() const { 344fe6060f1SDimitry Andric return m_interpreter->GetScriptedProcessInterface(); 345fe6060f1SDimitry Andric } 346