15ffd83dbSDimitry Andric //===-- SBProcess.cpp -----------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lldb/API/SBProcess.h" 1004eeddc0SDimitry Andric #include "lldb/Utility/Instrumentation.h" 110b57cec5SDimitry Andric 12fe6060f1SDimitry Andric #include <cinttypes> 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "lldb/lldb-defines.h" 150b57cec5SDimitry Andric #include "lldb/lldb-types.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "lldb/Core/Debugger.h" 180b57cec5SDimitry Andric #include "lldb/Core/Module.h" 190b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h" 200b57cec5SDimitry Andric #include "lldb/Core/StreamFile.h" 215ffd83dbSDimitry Andric #include "lldb/Core/StructuredDataImpl.h" 220b57cec5SDimitry Andric #include "lldb/Target/MemoryRegionInfo.h" 230b57cec5SDimitry Andric #include "lldb/Target/Process.h" 240b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h" 250b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h" 260b57cec5SDimitry Andric #include "lldb/Target/Target.h" 270b57cec5SDimitry Andric #include "lldb/Target/Thread.h" 280b57cec5SDimitry Andric #include "lldb/Utility/Args.h" 290b57cec5SDimitry Andric #include "lldb/Utility/ProcessInfo.h" 300b57cec5SDimitry Andric #include "lldb/Utility/State.h" 310b57cec5SDimitry Andric #include "lldb/Utility/Stream.h" 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric #include "lldb/API/SBBroadcaster.h" 340b57cec5SDimitry Andric #include "lldb/API/SBCommandReturnObject.h" 350b57cec5SDimitry Andric #include "lldb/API/SBDebugger.h" 360b57cec5SDimitry Andric #include "lldb/API/SBEvent.h" 379dba64beSDimitry Andric #include "lldb/API/SBFile.h" 380b57cec5SDimitry Andric #include "lldb/API/SBFileSpec.h" 390b57cec5SDimitry Andric #include "lldb/API/SBMemoryRegionInfo.h" 400b57cec5SDimitry Andric #include "lldb/API/SBMemoryRegionInfoList.h" 41*fe013be4SDimitry Andric #include "lldb/API/SBScriptObject.h" 420b57cec5SDimitry Andric #include "lldb/API/SBStream.h" 430b57cec5SDimitry Andric #include "lldb/API/SBStringList.h" 440b57cec5SDimitry Andric #include "lldb/API/SBStructuredData.h" 450b57cec5SDimitry Andric #include "lldb/API/SBThread.h" 460b57cec5SDimitry Andric #include "lldb/API/SBThreadCollection.h" 470b57cec5SDimitry Andric #include "lldb/API/SBTrace.h" 480b57cec5SDimitry Andric #include "lldb/API/SBUnixSignals.h" 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric using namespace lldb; 510b57cec5SDimitry Andric using namespace lldb_private; 520b57cec5SDimitry Andric 5304eeddc0SDimitry Andric SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // SBProcess constructor 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) { 5804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs); 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric SBProcess::SBProcess(const lldb::ProcessSP &process_sp) 620b57cec5SDimitry Andric : m_opaque_wp(process_sp) { 6304eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, process_sp); 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric const SBProcess &SBProcess::operator=(const SBProcess &rhs) { 6704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric if (this != &rhs) 700b57cec5SDimitry Andric m_opaque_wp = rhs.m_opaque_wp; 7104eeddc0SDimitry Andric return *this; 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // Destructor 755ffd83dbSDimitry Andric SBProcess::~SBProcess() = default; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric const char *SBProcess::GetBroadcasterClassName() { 7804eeddc0SDimitry Andric LLDB_INSTRUMENT(); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric return Process::GetStaticBroadcasterClass().AsCString(); 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric const char *SBProcess::GetPluginName() { 8404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 870b57cec5SDimitry Andric if (process_sp) { 88349cc55cSDimitry Andric return ConstString(process_sp->GetPluginName()).GetCString(); 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric return "<Unknown>"; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric const char *SBProcess::GetShortPluginName() { 9404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 970b57cec5SDimitry Andric if (process_sp) { 98349cc55cSDimitry Andric return ConstString(process_sp->GetPluginName()).GetCString(); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric return "<Unknown>"; 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric void SBProcess::Clear() { 10804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric m_opaque_wp.reset(); 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric bool SBProcess::IsValid() const { 11404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 1150b57cec5SDimitry Andric return this->operator bool(); 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric SBProcess::operator bool() const { 11804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric ProcessSP process_sp(m_opaque_wp.lock()); 1210b57cec5SDimitry Andric return ((bool)process_sp && process_sp->IsValid()); 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric bool SBProcess::RemoteLaunch(char const **argv, char const **envp, 1250b57cec5SDimitry Andric const char *stdin_path, const char *stdout_path, 1260b57cec5SDimitry Andric const char *stderr_path, 1270b57cec5SDimitry Andric const char *working_directory, 1280b57cec5SDimitry Andric uint32_t launch_flags, bool stop_at_entry, 1290b57cec5SDimitry Andric lldb::SBError &error) { 13004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, argv, envp, stdin_path, stdout_path, stderr_path, 1310b57cec5SDimitry Andric working_directory, launch_flags, stop_at_entry, error); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 1340b57cec5SDimitry Andric if (process_sp) { 1350b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 1360b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 1370b57cec5SDimitry Andric if (process_sp->GetState() == eStateConnected) { 1380b57cec5SDimitry Andric if (stop_at_entry) 1390b57cec5SDimitry Andric launch_flags |= eLaunchFlagStopAtEntry; 1400b57cec5SDimitry Andric ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path), 1410b57cec5SDimitry Andric FileSpec(stderr_path), 1420b57cec5SDimitry Andric FileSpec(working_directory), launch_flags); 1430b57cec5SDimitry Andric Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer(); 1440b57cec5SDimitry Andric if (exe_module) 1450b57cec5SDimitry Andric launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true); 1460b57cec5SDimitry Andric if (argv) 1470b57cec5SDimitry Andric launch_info.GetArguments().AppendArguments(argv); 1480b57cec5SDimitry Andric if (envp) 1490b57cec5SDimitry Andric launch_info.GetEnvironment() = Environment(envp); 1500b57cec5SDimitry Andric error.SetError(process_sp->Launch(launch_info)); 1510b57cec5SDimitry Andric } else { 1520b57cec5SDimitry Andric error.SetErrorString("must be in eStateConnected to call RemoteLaunch"); 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric } else { 1550b57cec5SDimitry Andric error.SetErrorString("unable to attach pid"); 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric return error.Success(); 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid, 1620b57cec5SDimitry Andric lldb::SBError &error) { 16304eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, pid, error); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 1660b57cec5SDimitry Andric if (process_sp) { 1670b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 1680b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 1690b57cec5SDimitry Andric if (process_sp->GetState() == eStateConnected) { 1700b57cec5SDimitry Andric ProcessAttachInfo attach_info; 1710b57cec5SDimitry Andric attach_info.SetProcessID(pid); 1720b57cec5SDimitry Andric error.SetError(process_sp->Attach(attach_info)); 1730b57cec5SDimitry Andric } else { 1740b57cec5SDimitry Andric error.SetErrorString( 1750b57cec5SDimitry Andric "must be in eStateConnected to call RemoteAttachToProcessWithID"); 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric } else { 1780b57cec5SDimitry Andric error.SetErrorString("unable to attach pid"); 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric return error.Success(); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric uint32_t SBProcess::GetNumThreads() { 18504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric uint32_t num_threads = 0; 1880b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 1890b57cec5SDimitry Andric if (process_sp) { 1900b57cec5SDimitry Andric Process::StopLocker stop_locker; 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 1930b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 1940b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 1950b57cec5SDimitry Andric num_threads = process_sp->GetThreadList().GetSize(can_update); 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric return num_threads; 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric SBThread SBProcess::GetSelectedThread() const { 20204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric SBThread sb_thread; 2050b57cec5SDimitry Andric ThreadSP thread_sp; 2060b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 2070b57cec5SDimitry Andric if (process_sp) { 2080b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 2090b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 2100b57cec5SDimitry Andric thread_sp = process_sp->GetThreadList().GetSelectedThread(); 2110b57cec5SDimitry Andric sb_thread.SetThread(thread_sp); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 21404eeddc0SDimitry Andric return sb_thread; 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid, 2180b57cec5SDimitry Andric lldb::addr_t context) { 21904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, tid, context); 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric SBThread sb_thread; 2220b57cec5SDimitry Andric ThreadSP thread_sp; 2230b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 2240b57cec5SDimitry Andric if (process_sp) { 2250b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 2260b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 2270b57cec5SDimitry Andric thread_sp = process_sp->CreateOSPluginThread(tid, context); 2280b57cec5SDimitry Andric sb_thread.SetThread(thread_sp); 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 23104eeddc0SDimitry Andric return sb_thread; 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric SBTarget SBProcess::GetTarget() const { 23504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric SBTarget sb_target; 2380b57cec5SDimitry Andric TargetSP target_sp; 2390b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 2400b57cec5SDimitry Andric if (process_sp) { 2410b57cec5SDimitry Andric target_sp = process_sp->GetTarget().shared_from_this(); 2420b57cec5SDimitry Andric sb_target.SetSP(target_sp); 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric 24504eeddc0SDimitry Andric return sb_target; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric size_t SBProcess::PutSTDIN(const char *src, size_t src_len) { 24904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, src, src_len); 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric size_t ret_val = 0; 2520b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 2530b57cec5SDimitry Andric if (process_sp) { 2540b57cec5SDimitry Andric Status error; 2550b57cec5SDimitry Andric ret_val = process_sp->PutSTDIN(src, src_len, error); 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric return ret_val; 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const { 26204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, dst, dst_len); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric size_t bytes_read = 0; 2650b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 2660b57cec5SDimitry Andric if (process_sp) { 2670b57cec5SDimitry Andric Status error; 2680b57cec5SDimitry Andric bytes_read = process_sp->GetSTDOUT(dst, dst_len, error); 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric return bytes_read; 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const { 27504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, dst, dst_len); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric size_t bytes_read = 0; 2780b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 2790b57cec5SDimitry Andric if (process_sp) { 2800b57cec5SDimitry Andric Status error; 2810b57cec5SDimitry Andric bytes_read = process_sp->GetSTDERR(dst, dst_len, error); 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric return bytes_read; 2850b57cec5SDimitry Andric } 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const { 28804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, dst, dst_len); 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric size_t bytes_read = 0; 2910b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 2920b57cec5SDimitry Andric if (process_sp) { 2930b57cec5SDimitry Andric Status error; 2940b57cec5SDimitry Andric bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error); 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric return bytes_read; 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 3009dba64beSDimitry Andric void SBProcess::ReportEventState(const SBEvent &event, SBFile out) const { 30104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, event, out); 3029dba64beSDimitry Andric 3039dba64beSDimitry Andric return ReportEventState(event, out.m_opaque_sp); 3049dba64beSDimitry Andric } 3059dba64beSDimitry Andric 3060b57cec5SDimitry Andric void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const { 30704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, event, out); 3089dba64beSDimitry Andric FileSP outfile = std::make_shared<NativeFile>(out, false); 3099dba64beSDimitry Andric return ReportEventState(event, outfile); 3109dba64beSDimitry Andric } 3110b57cec5SDimitry Andric 3129dba64beSDimitry Andric void SBProcess::ReportEventState(const SBEvent &event, FileSP out) const { 3139dba64beSDimitry Andric 31404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, event, out); 3159dba64beSDimitry Andric 3169dba64beSDimitry Andric if (!out || !out->IsValid()) 3170b57cec5SDimitry Andric return; 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 3200b57cec5SDimitry Andric if (process_sp) { 3219dba64beSDimitry Andric StreamFile stream(out); 3220b57cec5SDimitry Andric const StateType event_state = SBProcess::GetStateFromEvent(event); 3239dba64beSDimitry Andric stream.Printf("Process %" PRIu64 " %s\n", 3240b57cec5SDimitry Andric process_sp->GetID(), SBDebugger::StateAsCString(event_state)); 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric void SBProcess::AppendEventStateReport(const SBEvent &event, 3290b57cec5SDimitry Andric SBCommandReturnObject &result) { 33004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, event, result); 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 3330b57cec5SDimitry Andric if (process_sp) { 3340b57cec5SDimitry Andric const StateType event_state = SBProcess::GetStateFromEvent(event); 3350b57cec5SDimitry Andric char message[1024]; 3360b57cec5SDimitry Andric ::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n", 3370b57cec5SDimitry Andric process_sp->GetID(), SBDebugger::StateAsCString(event_state)); 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric result.AppendMessage(message); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric bool SBProcess::SetSelectedThread(const SBThread &thread) { 34404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, thread); 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 3470b57cec5SDimitry Andric if (process_sp) { 3480b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 3490b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 3500b57cec5SDimitry Andric return process_sp->GetThreadList().SetSelectedThreadByID( 3510b57cec5SDimitry Andric thread.GetThreadID()); 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric return false; 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) { 35704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, tid); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric bool ret_val = false; 3600b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 3610b57cec5SDimitry Andric if (process_sp) { 3620b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 3630b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 3640b57cec5SDimitry Andric ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid); 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric return ret_val; 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) { 37104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, index_id); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric bool ret_val = false; 3740b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 3750b57cec5SDimitry Andric if (process_sp) { 3760b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 3770b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 3780b57cec5SDimitry Andric ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric return ret_val; 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric SBThread SBProcess::GetThreadAtIndex(size_t index) { 38604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, index); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric SBThread sb_thread; 3890b57cec5SDimitry Andric ThreadSP thread_sp; 3900b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 3910b57cec5SDimitry Andric if (process_sp) { 3920b57cec5SDimitry Andric Process::StopLocker stop_locker; 3930b57cec5SDimitry Andric const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 3940b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 3950b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 3960b57cec5SDimitry Andric thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update); 3970b57cec5SDimitry Andric sb_thread.SetThread(thread_sp); 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric 40004eeddc0SDimitry Andric return sb_thread; 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric uint32_t SBProcess::GetNumQueues() { 40404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric uint32_t num_queues = 0; 4070b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 4080b57cec5SDimitry Andric if (process_sp) { 4090b57cec5SDimitry Andric Process::StopLocker stop_locker; 4100b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 4110b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 4120b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 4130b57cec5SDimitry Andric num_queues = process_sp->GetQueueList().GetSize(); 4140b57cec5SDimitry Andric } 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric return num_queues; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric SBQueue SBProcess::GetQueueAtIndex(size_t index) { 42104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, index); 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric SBQueue sb_queue; 4240b57cec5SDimitry Andric QueueSP queue_sp; 4250b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 4260b57cec5SDimitry Andric if (process_sp) { 4270b57cec5SDimitry Andric Process::StopLocker stop_locker; 4280b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 4290b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 4300b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 4310b57cec5SDimitry Andric queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index); 4320b57cec5SDimitry Andric sb_queue.SetQueue(queue_sp); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric } 4350b57cec5SDimitry Andric 43604eeddc0SDimitry Andric return sb_queue; 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric uint32_t SBProcess::GetStopID(bool include_expression_stops) { 44004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, include_expression_stops); 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 4430b57cec5SDimitry Andric if (process_sp) { 4440b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 4450b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 4460b57cec5SDimitry Andric if (include_expression_stops) 4470b57cec5SDimitry Andric return process_sp->GetStopID(); 4480b57cec5SDimitry Andric else 4490b57cec5SDimitry Andric return process_sp->GetLastNaturalStopID(); 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric return 0; 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) { 45504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, stop_id); 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric SBEvent sb_event; 4580b57cec5SDimitry Andric EventSP event_sp; 4590b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 4600b57cec5SDimitry Andric if (process_sp) { 4610b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 4620b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 4630b57cec5SDimitry Andric event_sp = process_sp->GetStopEventForStopID(stop_id); 4640b57cec5SDimitry Andric sb_event.reset(event_sp); 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric 46704eeddc0SDimitry Andric return sb_event; 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric 470*fe013be4SDimitry Andric void SBProcess::ForceScriptedState(StateType new_state) { 471*fe013be4SDimitry Andric LLDB_INSTRUMENT_VA(this, new_state); 472*fe013be4SDimitry Andric 473*fe013be4SDimitry Andric if (ProcessSP process_sp = GetSP()) { 474*fe013be4SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 475*fe013be4SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 476*fe013be4SDimitry Andric process_sp->ForceScriptedState(new_state); 477*fe013be4SDimitry Andric } 478*fe013be4SDimitry Andric } 479*fe013be4SDimitry Andric 4800b57cec5SDimitry Andric StateType SBProcess::GetState() { 48104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric StateType ret_val = eStateInvalid; 4840b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 4850b57cec5SDimitry Andric if (process_sp) { 4860b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 4870b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 4880b57cec5SDimitry Andric ret_val = process_sp->GetState(); 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric return ret_val; 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric int SBProcess::GetExitStatus() { 49504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric int exit_status = 0; 4980b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 4990b57cec5SDimitry Andric if (process_sp) { 5000b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 5010b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 5020b57cec5SDimitry Andric exit_status = process_sp->GetExitStatus(); 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric return exit_status; 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric const char *SBProcess::GetExitDescription() { 50904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 512*fe013be4SDimitry Andric if (!process_sp) 513*fe013be4SDimitry Andric return nullptr; 514*fe013be4SDimitry Andric 5150b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 5160b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 517*fe013be4SDimitry Andric return ConstString(process_sp->GetExitDescription()).GetCString(); 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric lldb::pid_t SBProcess::GetProcessID() { 52104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID; 5240b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 5250b57cec5SDimitry Andric if (process_sp) 5260b57cec5SDimitry Andric ret_val = process_sp->GetID(); 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric return ret_val; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric uint32_t SBProcess::GetUniqueID() { 53204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric uint32_t ret_val = 0; 5350b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 5360b57cec5SDimitry Andric if (process_sp) 5370b57cec5SDimitry Andric ret_val = process_sp->GetUniqueID(); 5380b57cec5SDimitry Andric return ret_val; 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric ByteOrder SBProcess::GetByteOrder() const { 54204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric ByteOrder byteOrder = eByteOrderInvalid; 5450b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 5460b57cec5SDimitry Andric if (process_sp) 5470b57cec5SDimitry Andric byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder(); 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric return byteOrder; 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric uint32_t SBProcess::GetAddressByteSize() const { 55404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric uint32_t size = 0; 5570b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 5580b57cec5SDimitry Andric if (process_sp) 5590b57cec5SDimitry Andric size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize(); 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric return size; 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric SBError SBProcess::Continue() { 56604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric SBError sb_error; 5690b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric if (process_sp) { 5720b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 5730b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 5740b57cec5SDimitry Andric 5750b57cec5SDimitry Andric if (process_sp->GetTarget().GetDebugger().GetAsyncExecution()) 5760b57cec5SDimitry Andric sb_error.ref() = process_sp->Resume(); 5770b57cec5SDimitry Andric else 5780b57cec5SDimitry Andric sb_error.ref() = process_sp->ResumeSynchronous(nullptr); 5790b57cec5SDimitry Andric } else 5800b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 5810b57cec5SDimitry Andric 58204eeddc0SDimitry Andric return sb_error; 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric SBError SBProcess::Destroy() { 58604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric SBError sb_error; 5890b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 5900b57cec5SDimitry Andric if (process_sp) { 5910b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 5920b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 5930b57cec5SDimitry Andric sb_error.SetError(process_sp->Destroy(false)); 5940b57cec5SDimitry Andric } else 5950b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 5960b57cec5SDimitry Andric 59704eeddc0SDimitry Andric return sb_error; 5980b57cec5SDimitry Andric } 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric SBError SBProcess::Stop() { 60104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric SBError sb_error; 6040b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 6050b57cec5SDimitry Andric if (process_sp) { 6060b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 6070b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 6080b57cec5SDimitry Andric sb_error.SetError(process_sp->Halt()); 6090b57cec5SDimitry Andric } else 6100b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 6110b57cec5SDimitry Andric 61204eeddc0SDimitry Andric return sb_error; 6130b57cec5SDimitry Andric } 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric SBError SBProcess::Kill() { 61604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric SBError sb_error; 6190b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 6200b57cec5SDimitry Andric if (process_sp) { 6210b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 6220b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 6230b57cec5SDimitry Andric sb_error.SetError(process_sp->Destroy(true)); 6240b57cec5SDimitry Andric } else 6250b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 6260b57cec5SDimitry Andric 62704eeddc0SDimitry Andric return sb_error; 6280b57cec5SDimitry Andric } 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric SBError SBProcess::Detach() { 63104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric // FIXME: This should come from a process default. 6340b57cec5SDimitry Andric bool keep_stopped = false; 63504eeddc0SDimitry Andric return Detach(keep_stopped); 6360b57cec5SDimitry Andric } 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric SBError SBProcess::Detach(bool keep_stopped) { 63904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, keep_stopped); 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric SBError sb_error; 6420b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 6430b57cec5SDimitry Andric if (process_sp) { 6440b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 6450b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 6460b57cec5SDimitry Andric sb_error.SetError(process_sp->Detach(keep_stopped)); 6470b57cec5SDimitry Andric } else 6480b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 6490b57cec5SDimitry Andric 65004eeddc0SDimitry Andric return sb_error; 6510b57cec5SDimitry Andric } 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric SBError SBProcess::Signal(int signo) { 65404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, signo); 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric SBError sb_error; 6570b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 6580b57cec5SDimitry Andric if (process_sp) { 6590b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 6600b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 6610b57cec5SDimitry Andric sb_error.SetError(process_sp->Signal(signo)); 6620b57cec5SDimitry Andric } else 6630b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 6640b57cec5SDimitry Andric 66504eeddc0SDimitry Andric return sb_error; 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric SBUnixSignals SBProcess::GetUnixSignals() { 66904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric if (auto process_sp = GetSP()) 67204eeddc0SDimitry Andric return SBUnixSignals{process_sp}; 6730b57cec5SDimitry Andric 67404eeddc0SDimitry Andric return SBUnixSignals{}; 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric void SBProcess::SendAsyncInterrupt() { 67804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 6810b57cec5SDimitry Andric if (process_sp) { 6820b57cec5SDimitry Andric process_sp->SendAsyncInterrupt(); 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric SBThread SBProcess::GetThreadByID(tid_t tid) { 68704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, tid); 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric SBThread sb_thread; 6900b57cec5SDimitry Andric ThreadSP thread_sp; 6910b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 6920b57cec5SDimitry Andric if (process_sp) { 6930b57cec5SDimitry Andric Process::StopLocker stop_locker; 6940b57cec5SDimitry Andric const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 6950b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 6960b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 6970b57cec5SDimitry Andric thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update); 6980b57cec5SDimitry Andric sb_thread.SetThread(thread_sp); 6990b57cec5SDimitry Andric } 7000b57cec5SDimitry Andric 70104eeddc0SDimitry Andric return sb_thread; 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) { 70504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, index_id); 7060b57cec5SDimitry Andric 7070b57cec5SDimitry Andric SBThread sb_thread; 7080b57cec5SDimitry Andric ThreadSP thread_sp; 7090b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 7100b57cec5SDimitry Andric if (process_sp) { 7110b57cec5SDimitry Andric Process::StopLocker stop_locker; 7120b57cec5SDimitry Andric const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock()); 7130b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 7140b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 7150b57cec5SDimitry Andric thread_sp = 7160b57cec5SDimitry Andric process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update); 7170b57cec5SDimitry Andric sb_thread.SetThread(thread_sp); 7180b57cec5SDimitry Andric } 7190b57cec5SDimitry Andric 72004eeddc0SDimitry Andric return sb_thread; 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric StateType SBProcess::GetStateFromEvent(const SBEvent &event) { 72404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get()); 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric return ret_val; 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric bool SBProcess::GetRestartedFromEvent(const SBEvent &event) { 73204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get()); 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric return ret_val; 7370b57cec5SDimitry Andric } 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) { 74004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric return Process::ProcessEventData::GetNumRestartedReasons(event.get()); 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric const char * 7460b57cec5SDimitry Andric SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event, 7470b57cec5SDimitry Andric size_t idx) { 74804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event, idx); 7490b57cec5SDimitry Andric 750*fe013be4SDimitry Andric return ConstString(Process::ProcessEventData::GetRestartedReasonAtIndex( 751*fe013be4SDimitry Andric event.get(), idx)) 752*fe013be4SDimitry Andric .GetCString(); 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric 7550b57cec5SDimitry Andric SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) { 75604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7570b57cec5SDimitry Andric 7580b57cec5SDimitry Andric ProcessSP process_sp = 7590b57cec5SDimitry Andric Process::ProcessEventData::GetProcessFromEvent(event.get()); 7600b57cec5SDimitry Andric if (!process_sp) { 7610b57cec5SDimitry Andric // StructuredData events also know the process they come from. Try that. 7620b57cec5SDimitry Andric process_sp = EventDataStructuredData::GetProcessFromEvent(event.get()); 7630b57cec5SDimitry Andric } 7640b57cec5SDimitry Andric 76504eeddc0SDimitry Andric return SBProcess(process_sp); 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) { 76904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric return Process::ProcessEventData::GetInterruptedFromEvent(event.get()); 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric lldb::SBStructuredData 7750b57cec5SDimitry Andric SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) { 77604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7770b57cec5SDimitry Andric 77804eeddc0SDimitry Andric return SBStructuredData(event.GetSP()); 7790b57cec5SDimitry Andric } 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric bool SBProcess::EventIsProcessEvent(const SBEvent &event) { 78204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7830b57cec5SDimitry Andric 784*fe013be4SDimitry Andric return Process::ProcessEventData::GetEventDataFromEvent(event.get()) != 785*fe013be4SDimitry Andric nullptr; 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) { 78904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(event); 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric EventSP event_sp = event.GetSP(); 7920b57cec5SDimitry Andric EventData *event_data = event_sp ? event_sp->GetData() : nullptr; 7930b57cec5SDimitry Andric return event_data && (event_data->GetFlavor() == 7940b57cec5SDimitry Andric EventDataStructuredData::GetFlavorString()); 7950b57cec5SDimitry Andric } 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric SBBroadcaster SBProcess::GetBroadcaster() const { 79804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 8010b57cec5SDimitry Andric 8020b57cec5SDimitry Andric SBBroadcaster broadcaster(process_sp.get(), false); 8030b57cec5SDimitry Andric 80404eeddc0SDimitry Andric return broadcaster; 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric const char *SBProcess::GetBroadcasterClass() { 80804eeddc0SDimitry Andric LLDB_INSTRUMENT(); 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric return Process::GetStaticBroadcasterClass().AsCString(); 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len, 8140b57cec5SDimitry Andric SBError &sb_error) { 81504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, addr, dst, dst_len, sb_error); 8160b57cec5SDimitry Andric 817*fe013be4SDimitry Andric if (!dst) { 818*fe013be4SDimitry Andric sb_error.SetErrorStringWithFormat( 819*fe013be4SDimitry Andric "no buffer provided to read %zu bytes into", dst_len); 820*fe013be4SDimitry Andric return 0; 821*fe013be4SDimitry Andric } 8220b57cec5SDimitry Andric 823*fe013be4SDimitry Andric size_t bytes_read = 0; 8240b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric if (process_sp) { 8280b57cec5SDimitry Andric Process::StopLocker stop_locker; 8290b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 8300b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 8310b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 8320b57cec5SDimitry Andric bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref()); 8330b57cec5SDimitry Andric } else { 8340b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 8350b57cec5SDimitry Andric } 8360b57cec5SDimitry Andric } else { 8370b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric return bytes_read; 8410b57cec5SDimitry Andric } 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size, 8440b57cec5SDimitry Andric lldb::SBError &sb_error) { 84504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, addr, buf, size, sb_error); 8460b57cec5SDimitry Andric 8470b57cec5SDimitry Andric size_t bytes_read = 0; 8480b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 8490b57cec5SDimitry Andric if (process_sp) { 8500b57cec5SDimitry Andric Process::StopLocker stop_locker; 8510b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 8520b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 8530b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 8540b57cec5SDimitry Andric bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size, 8550b57cec5SDimitry Andric sb_error.ref()); 8560b57cec5SDimitry Andric } else { 8570b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 8580b57cec5SDimitry Andric } 8590b57cec5SDimitry Andric } else { 8600b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 8610b57cec5SDimitry Andric } 8620b57cec5SDimitry Andric return bytes_read; 8630b57cec5SDimitry Andric } 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size, 8660b57cec5SDimitry Andric lldb::SBError &sb_error) { 86704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, addr, byte_size, sb_error); 8680b57cec5SDimitry Andric 8690b57cec5SDimitry Andric uint64_t value = 0; 8700b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 8710b57cec5SDimitry Andric if (process_sp) { 8720b57cec5SDimitry Andric Process::StopLocker stop_locker; 8730b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 8740b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 8750b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 8760b57cec5SDimitry Andric value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0, 8770b57cec5SDimitry Andric sb_error.ref()); 8780b57cec5SDimitry Andric } else { 8790b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric } else { 8820b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 8830b57cec5SDimitry Andric } 8840b57cec5SDimitry Andric return value; 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr, 8880b57cec5SDimitry Andric lldb::SBError &sb_error) { 88904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, addr, sb_error); 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric lldb::addr_t ptr = LLDB_INVALID_ADDRESS; 8920b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 8930b57cec5SDimitry Andric if (process_sp) { 8940b57cec5SDimitry Andric Process::StopLocker stop_locker; 8950b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 8960b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 8970b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 8980b57cec5SDimitry Andric ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref()); 8990b57cec5SDimitry Andric } else { 9000b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 9010b57cec5SDimitry Andric } 9020b57cec5SDimitry Andric } else { 9030b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric return ptr; 9060b57cec5SDimitry Andric } 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len, 9090b57cec5SDimitry Andric SBError &sb_error) { 91004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, addr, src, src_len, sb_error); 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric size_t bytes_written = 0; 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 9150b57cec5SDimitry Andric 9160b57cec5SDimitry Andric if (process_sp) { 9170b57cec5SDimitry Andric Process::StopLocker stop_locker; 9180b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 9190b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 9200b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 9210b57cec5SDimitry Andric bytes_written = 9220b57cec5SDimitry Andric process_sp->WriteMemory(addr, src, src_len, sb_error.ref()); 9230b57cec5SDimitry Andric } else { 9240b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 9250b57cec5SDimitry Andric } 9260b57cec5SDimitry Andric } 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric return bytes_written; 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric bool SBProcess::GetDescription(SBStream &description) { 93204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, description); 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric Stream &strm = description.ref(); 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 9370b57cec5SDimitry Andric if (process_sp) { 9380b57cec5SDimitry Andric char path[PATH_MAX]; 9390b57cec5SDimitry Andric GetTarget().GetExecutable().GetPath(path, sizeof(path)); 9400b57cec5SDimitry Andric Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer(); 9410b57cec5SDimitry Andric const char *exe_name = nullptr; 9420b57cec5SDimitry Andric if (exe_module) 9430b57cec5SDimitry Andric exe_name = exe_module->GetFileSpec().GetFilename().AsCString(); 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s", 9460b57cec5SDimitry Andric process_sp->GetID(), lldb_private::StateAsCString(GetState()), 9470b57cec5SDimitry Andric GetNumThreads(), exe_name ? ", executable = " : "", 9480b57cec5SDimitry Andric exe_name ? exe_name : ""); 9490b57cec5SDimitry Andric } else 9500b57cec5SDimitry Andric strm.PutCString("No value"); 9510b57cec5SDimitry Andric 9520b57cec5SDimitry Andric return true; 9530b57cec5SDimitry Andric } 9540b57cec5SDimitry Andric 9555ffd83dbSDimitry Andric SBStructuredData SBProcess::GetExtendedCrashInformation() { 95604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 9575ffd83dbSDimitry Andric SBStructuredData data; 9585ffd83dbSDimitry Andric ProcessSP process_sp(GetSP()); 9595ffd83dbSDimitry Andric if (!process_sp) 96004eeddc0SDimitry Andric return data; 9615ffd83dbSDimitry Andric 9625ffd83dbSDimitry Andric PlatformSP platform_sp = process_sp->GetTarget().GetPlatform(); 9635ffd83dbSDimitry Andric 9645ffd83dbSDimitry Andric if (!platform_sp) 96504eeddc0SDimitry Andric return data; 9665ffd83dbSDimitry Andric 9675ffd83dbSDimitry Andric auto expected_data = 9685ffd83dbSDimitry Andric platform_sp->FetchExtendedCrashInformation(*process_sp.get()); 9695ffd83dbSDimitry Andric 9705ffd83dbSDimitry Andric if (!expected_data) 97104eeddc0SDimitry Andric return data; 9725ffd83dbSDimitry Andric 9735ffd83dbSDimitry Andric StructuredData::ObjectSP fetched_data = *expected_data; 9745ffd83dbSDimitry Andric data.m_impl_up->SetObjectSP(fetched_data); 97504eeddc0SDimitry Andric return data; 9765ffd83dbSDimitry Andric } 9775ffd83dbSDimitry Andric 9780b57cec5SDimitry Andric uint32_t 9790b57cec5SDimitry Andric SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const { 98004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, sb_error); 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric uint32_t num = 0; 9830b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 9840b57cec5SDimitry Andric if (process_sp) { 9850b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 9860b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 987*fe013be4SDimitry Andric std::optional<uint32_t> actual_num = process_sp->GetWatchpointSlotCount(); 988*fe013be4SDimitry Andric if (actual_num) { 989*fe013be4SDimitry Andric num = *actual_num; 990*fe013be4SDimitry Andric } else { 991*fe013be4SDimitry Andric sb_error.SetErrorString("Unable to determine number of watchpoints"); 992*fe013be4SDimitry Andric } 9930b57cec5SDimitry Andric } else { 9940b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 9950b57cec5SDimitry Andric } 9960b57cec5SDimitry Andric return num; 9970b57cec5SDimitry Andric } 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec, 10000b57cec5SDimitry Andric lldb::SBError &sb_error) { 100104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, sb_remote_image_spec, sb_error); 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error); 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec, 10070b57cec5SDimitry Andric const lldb::SBFileSpec &sb_remote_image_spec, 10080b57cec5SDimitry Andric lldb::SBError &sb_error) { 100904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, sb_local_image_spec, sb_remote_image_spec, sb_error); 10100b57cec5SDimitry Andric 10110b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 10120b57cec5SDimitry Andric if (process_sp) { 10130b57cec5SDimitry Andric Process::StopLocker stop_locker; 10140b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 10150b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 10160b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 10170b57cec5SDimitry Andric PlatformSP platform_sp = process_sp->GetTarget().GetPlatform(); 10180b57cec5SDimitry Andric return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec, 10190b57cec5SDimitry Andric *sb_remote_image_spec, sb_error.ref()); 10200b57cec5SDimitry Andric } else { 10210b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 10220b57cec5SDimitry Andric } 10230b57cec5SDimitry Andric } else { 10240b57cec5SDimitry Andric sb_error.SetErrorString("process is invalid"); 10250b57cec5SDimitry Andric } 10260b57cec5SDimitry Andric return LLDB_INVALID_IMAGE_TOKEN; 10270b57cec5SDimitry Andric } 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec &image_spec, 10300b57cec5SDimitry Andric SBStringList &paths, 10310b57cec5SDimitry Andric lldb::SBFileSpec &loaded_path, 10320b57cec5SDimitry Andric lldb::SBError &error) { 103304eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, image_spec, paths, loaded_path, error); 10340b57cec5SDimitry Andric 10350b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 10360b57cec5SDimitry Andric if (process_sp) { 10370b57cec5SDimitry Andric Process::StopLocker stop_locker; 10380b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 10390b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 10400b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 10410b57cec5SDimitry Andric PlatformSP platform_sp = process_sp->GetTarget().GetPlatform(); 10420b57cec5SDimitry Andric size_t num_paths = paths.GetSize(); 10430b57cec5SDimitry Andric std::vector<std::string> paths_vec; 10440b57cec5SDimitry Andric paths_vec.reserve(num_paths); 10450b57cec5SDimitry Andric for (size_t i = 0; i < num_paths; i++) 10460b57cec5SDimitry Andric paths_vec.push_back(paths.GetStringAtIndex(i)); 10470b57cec5SDimitry Andric FileSpec loaded_spec; 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric uint32_t token = platform_sp->LoadImageUsingPaths( 10500b57cec5SDimitry Andric process_sp.get(), *image_spec, paths_vec, error.ref(), &loaded_spec); 10510b57cec5SDimitry Andric if (token != LLDB_INVALID_IMAGE_TOKEN) 10520b57cec5SDimitry Andric loaded_path = loaded_spec; 10530b57cec5SDimitry Andric return token; 10540b57cec5SDimitry Andric } else { 10550b57cec5SDimitry Andric error.SetErrorString("process is running"); 10560b57cec5SDimitry Andric } 10570b57cec5SDimitry Andric } else { 10580b57cec5SDimitry Andric error.SetErrorString("process is invalid"); 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric return LLDB_INVALID_IMAGE_TOKEN; 10620b57cec5SDimitry Andric } 10630b57cec5SDimitry Andric 10640b57cec5SDimitry Andric lldb::SBError SBProcess::UnloadImage(uint32_t image_token) { 106504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, image_token); 10660b57cec5SDimitry Andric 10670b57cec5SDimitry Andric lldb::SBError sb_error; 10680b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 10690b57cec5SDimitry Andric if (process_sp) { 10700b57cec5SDimitry Andric Process::StopLocker stop_locker; 10710b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 10720b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 10730b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 10740b57cec5SDimitry Andric PlatformSP platform_sp = process_sp->GetTarget().GetPlatform(); 10750b57cec5SDimitry Andric sb_error.SetError( 10760b57cec5SDimitry Andric platform_sp->UnloadImage(process_sp.get(), image_token)); 10770b57cec5SDimitry Andric } else { 10780b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 10790b57cec5SDimitry Andric } 10800b57cec5SDimitry Andric } else 10810b57cec5SDimitry Andric sb_error.SetErrorString("invalid process"); 108204eeddc0SDimitry Andric return sb_error; 10830b57cec5SDimitry Andric } 10840b57cec5SDimitry Andric 10850b57cec5SDimitry Andric lldb::SBError SBProcess::SendEventData(const char *event_data) { 108604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, event_data); 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric lldb::SBError sb_error; 10890b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 10900b57cec5SDimitry Andric if (process_sp) { 10910b57cec5SDimitry Andric Process::StopLocker stop_locker; 10920b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 10930b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 10940b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 10950b57cec5SDimitry Andric sb_error.SetError(process_sp->SendEventData(event_data)); 10960b57cec5SDimitry Andric } else { 10970b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 10980b57cec5SDimitry Andric } 10990b57cec5SDimitry Andric } else 11000b57cec5SDimitry Andric sb_error.SetErrorString("invalid process"); 110104eeddc0SDimitry Andric return sb_error; 11020b57cec5SDimitry Andric } 11030b57cec5SDimitry Andric 11040b57cec5SDimitry Andric uint32_t SBProcess::GetNumExtendedBacktraceTypes() { 110504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 11080b57cec5SDimitry Andric if (process_sp && process_sp->GetSystemRuntime()) { 11090b57cec5SDimitry Andric SystemRuntime *runtime = process_sp->GetSystemRuntime(); 11100b57cec5SDimitry Andric return runtime->GetExtendedBacktraceTypes().size(); 11110b57cec5SDimitry Andric } 11120b57cec5SDimitry Andric return 0; 11130b57cec5SDimitry Andric } 11140b57cec5SDimitry Andric 11150b57cec5SDimitry Andric const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) { 111604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, idx); 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 11190b57cec5SDimitry Andric if (process_sp && process_sp->GetSystemRuntime()) { 11200b57cec5SDimitry Andric SystemRuntime *runtime = process_sp->GetSystemRuntime(); 11210b57cec5SDimitry Andric const std::vector<ConstString> &names = 11220b57cec5SDimitry Andric runtime->GetExtendedBacktraceTypes(); 11230b57cec5SDimitry Andric if (idx < names.size()) { 11240b57cec5SDimitry Andric return names[idx].AsCString(); 11250b57cec5SDimitry Andric } 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric return nullptr; 11280b57cec5SDimitry Andric } 11290b57cec5SDimitry Andric 11300b57cec5SDimitry Andric SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) { 113104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, addr); 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 11340b57cec5SDimitry Andric SBThreadCollection threads; 11350b57cec5SDimitry Andric if (process_sp) { 11360b57cec5SDimitry Andric threads = SBThreadCollection(process_sp->GetHistoryThreads(addr)); 11370b57cec5SDimitry Andric } 113804eeddc0SDimitry Andric return threads; 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric bool SBProcess::IsInstrumentationRuntimePresent( 11420b57cec5SDimitry Andric InstrumentationRuntimeType type) { 114304eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, type); 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 11460b57cec5SDimitry Andric if (!process_sp) 11470b57cec5SDimitry Andric return false; 11480b57cec5SDimitry Andric 11499dba64beSDimitry Andric std::lock_guard<std::recursive_mutex> guard( 11509dba64beSDimitry Andric process_sp->GetTarget().GetAPIMutex()); 11519dba64beSDimitry Andric 11520b57cec5SDimitry Andric InstrumentationRuntimeSP runtime_sp = 11530b57cec5SDimitry Andric process_sp->GetInstrumentationRuntime(type); 11540b57cec5SDimitry Andric 11550b57cec5SDimitry Andric if (!runtime_sp.get()) 11560b57cec5SDimitry Andric return false; 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andric return runtime_sp->IsActive(); 11590b57cec5SDimitry Andric } 11600b57cec5SDimitry Andric 11610b57cec5SDimitry Andric lldb::SBError SBProcess::SaveCore(const char *file_name) { 116204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, file_name); 116381ad6265SDimitry Andric return SaveCore(file_name, "", SaveCoreStyle::eSaveCoreFull); 116481ad6265SDimitry Andric } 116581ad6265SDimitry Andric 116681ad6265SDimitry Andric lldb::SBError SBProcess::SaveCore(const char *file_name, 116781ad6265SDimitry Andric const char *flavor, 116881ad6265SDimitry Andric SaveCoreStyle core_style) { 116981ad6265SDimitry Andric LLDB_INSTRUMENT_VA(this, file_name, flavor, core_style); 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric lldb::SBError error; 11720b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 11730b57cec5SDimitry Andric if (!process_sp) { 11740b57cec5SDimitry Andric error.SetErrorString("SBProcess is invalid"); 117504eeddc0SDimitry Andric return error; 11760b57cec5SDimitry Andric } 11770b57cec5SDimitry Andric 11780b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 11790b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric if (process_sp->GetState() != eStateStopped) { 11820b57cec5SDimitry Andric error.SetErrorString("the process is not stopped"); 118304eeddc0SDimitry Andric return error; 11840b57cec5SDimitry Andric } 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric FileSpec core_file(file_name); 1187*fe013be4SDimitry Andric FileSystem::Instance().Resolve(core_file); 118881ad6265SDimitry Andric error.ref() = PluginManager::SaveCore(process_sp, core_file, core_style, 118981ad6265SDimitry Andric flavor); 119081ad6265SDimitry Andric 119104eeddc0SDimitry Andric return error; 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric lldb::SBError 11950b57cec5SDimitry Andric SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr, 11960b57cec5SDimitry Andric SBMemoryRegionInfo &sb_region_info) { 119704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, load_addr, sb_region_info); 11980b57cec5SDimitry Andric 11990b57cec5SDimitry Andric lldb::SBError sb_error; 12000b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 12010b57cec5SDimitry Andric if (process_sp) { 12020b57cec5SDimitry Andric Process::StopLocker stop_locker; 12030b57cec5SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 12040b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 12050b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric sb_error.ref() = 12080b57cec5SDimitry Andric process_sp->GetMemoryRegionInfo(load_addr, sb_region_info.ref()); 12090b57cec5SDimitry Andric } else { 12100b57cec5SDimitry Andric sb_error.SetErrorString("process is running"); 12110b57cec5SDimitry Andric } 12120b57cec5SDimitry Andric } else { 12130b57cec5SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 12140b57cec5SDimitry Andric } 121504eeddc0SDimitry Andric return sb_error; 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() { 121904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric lldb::SBMemoryRegionInfoList sb_region_list; 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 12240b57cec5SDimitry Andric Process::StopLocker stop_locker; 12250b57cec5SDimitry Andric if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) { 12260b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 12270b57cec5SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric process_sp->GetMemoryRegions(sb_region_list.ref()); 12300b57cec5SDimitry Andric } 12310b57cec5SDimitry Andric 123204eeddc0SDimitry Andric return sb_region_list; 12330b57cec5SDimitry Andric } 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric lldb::SBProcessInfo SBProcess::GetProcessInfo() { 123604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric lldb::SBProcessInfo sb_proc_info; 12390b57cec5SDimitry Andric ProcessSP process_sp(GetSP()); 12400b57cec5SDimitry Andric ProcessInstanceInfo proc_info; 12410b57cec5SDimitry Andric if (process_sp && process_sp->GetProcessInfo(proc_info)) { 12420b57cec5SDimitry Andric sb_proc_info.SetProcessInfo(proc_info); 12430b57cec5SDimitry Andric } 124404eeddc0SDimitry Andric return sb_proc_info; 12450b57cec5SDimitry Andric } 12460b57cec5SDimitry Andric 1247fe6060f1SDimitry Andric lldb::addr_t SBProcess::AllocateMemory(size_t size, uint32_t permissions, 1248fe6060f1SDimitry Andric lldb::SBError &sb_error) { 124904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, size, permissions, sb_error); 1250fe6060f1SDimitry Andric 1251fe6060f1SDimitry Andric lldb::addr_t addr = LLDB_INVALID_ADDRESS; 1252fe6060f1SDimitry Andric ProcessSP process_sp(GetSP()); 1253fe6060f1SDimitry Andric if (process_sp) { 1254fe6060f1SDimitry Andric Process::StopLocker stop_locker; 1255fe6060f1SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 1256fe6060f1SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 1257fe6060f1SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 1258fe6060f1SDimitry Andric addr = process_sp->AllocateMemory(size, permissions, sb_error.ref()); 1259fe6060f1SDimitry Andric } else { 1260fe6060f1SDimitry Andric sb_error.SetErrorString("process is running"); 1261fe6060f1SDimitry Andric } 1262fe6060f1SDimitry Andric } else { 1263fe6060f1SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 1264fe6060f1SDimitry Andric } 1265fe6060f1SDimitry Andric return addr; 1266fe6060f1SDimitry Andric } 1267fe6060f1SDimitry Andric 1268fe6060f1SDimitry Andric lldb::SBError SBProcess::DeallocateMemory(lldb::addr_t ptr) { 126904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, ptr); 1270fe6060f1SDimitry Andric 1271fe6060f1SDimitry Andric lldb::SBError sb_error; 1272fe6060f1SDimitry Andric ProcessSP process_sp(GetSP()); 1273fe6060f1SDimitry Andric if (process_sp) { 1274fe6060f1SDimitry Andric Process::StopLocker stop_locker; 1275fe6060f1SDimitry Andric if (stop_locker.TryLock(&process_sp->GetRunLock())) { 1276fe6060f1SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 1277fe6060f1SDimitry Andric process_sp->GetTarget().GetAPIMutex()); 1278fe6060f1SDimitry Andric Status error = process_sp->DeallocateMemory(ptr); 1279fe6060f1SDimitry Andric sb_error.SetError(error); 1280fe6060f1SDimitry Andric } else { 1281fe6060f1SDimitry Andric sb_error.SetErrorString("process is running"); 1282fe6060f1SDimitry Andric } 1283fe6060f1SDimitry Andric } else { 1284fe6060f1SDimitry Andric sb_error.SetErrorString("SBProcess is invalid"); 1285fe6060f1SDimitry Andric } 1286fe6060f1SDimitry Andric return sb_error; 1287fe6060f1SDimitry Andric } 1288*fe013be4SDimitry Andric 1289*fe013be4SDimitry Andric lldb::SBScriptObject SBProcess::GetScriptedImplementation() { 1290*fe013be4SDimitry Andric LLDB_INSTRUMENT_VA(this); 1291*fe013be4SDimitry Andric ProcessSP process_sp(GetSP()); 1292*fe013be4SDimitry Andric return lldb::SBScriptObject((process_sp) ? process_sp->GetImplementation() 1293*fe013be4SDimitry Andric : nullptr, 1294*fe013be4SDimitry Andric eScriptLanguageDefault); 1295*fe013be4SDimitry Andric } 1296