Path: blob/main/contrib/llvm-project/lldb/source/API/SBProcess.cpp
39587 views
//===-- SBProcess.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBProcess.h"9#include "lldb/Utility/Instrumentation.h"1011#include <cinttypes>1213#include "lldb/lldb-defines.h"14#include "lldb/lldb-types.h"1516#include "lldb/Core/AddressRangeListImpl.h"17#include "lldb/Core/Debugger.h"18#include "lldb/Core/Module.h"19#include "lldb/Core/PluginManager.h"20#include "lldb/Core/StructuredDataImpl.h"21#include "lldb/Host/StreamFile.h"22#include "lldb/Target/MemoryRegionInfo.h"23#include "lldb/Target/Process.h"24#include "lldb/Target/RegisterContext.h"25#include "lldb/Target/SystemRuntime.h"26#include "lldb/Target/Target.h"27#include "lldb/Target/Thread.h"28#include "lldb/Utility/Args.h"29#include "lldb/Utility/LLDBLog.h"30#include "lldb/Utility/ProcessInfo.h"31#include "lldb/Utility/State.h"32#include "lldb/Utility/Stream.h"3334#include "lldb/API/SBBroadcaster.h"35#include "lldb/API/SBCommandReturnObject.h"36#include "lldb/API/SBDebugger.h"37#include "lldb/API/SBEvent.h"38#include "lldb/API/SBFile.h"39#include "lldb/API/SBFileSpec.h"40#include "lldb/API/SBMemoryRegionInfo.h"41#include "lldb/API/SBMemoryRegionInfoList.h"42#include "lldb/API/SBSaveCoreOptions.h"43#include "lldb/API/SBScriptObject.h"44#include "lldb/API/SBStream.h"45#include "lldb/API/SBStringList.h"46#include "lldb/API/SBStructuredData.h"47#include "lldb/API/SBThread.h"48#include "lldb/API/SBThreadCollection.h"49#include "lldb/API/SBTrace.h"50#include "lldb/API/SBUnixSignals.h"5152using namespace lldb;53using namespace lldb_private;5455SBProcess::SBProcess() { LLDB_INSTRUMENT_VA(this); }5657// SBProcess constructor5859SBProcess::SBProcess(const SBProcess &rhs) : m_opaque_wp(rhs.m_opaque_wp) {60LLDB_INSTRUMENT_VA(this, rhs);61}6263SBProcess::SBProcess(const lldb::ProcessSP &process_sp)64: m_opaque_wp(process_sp) {65LLDB_INSTRUMENT_VA(this, process_sp);66}6768const SBProcess &SBProcess::operator=(const SBProcess &rhs) {69LLDB_INSTRUMENT_VA(this, rhs);7071if (this != &rhs)72m_opaque_wp = rhs.m_opaque_wp;73return *this;74}7576// Destructor77SBProcess::~SBProcess() = default;7879const char *SBProcess::GetBroadcasterClassName() {80LLDB_INSTRUMENT();8182return ConstString(Process::GetStaticBroadcasterClass()).AsCString();83}8485const char *SBProcess::GetPluginName() {86LLDB_INSTRUMENT_VA(this);8788ProcessSP process_sp(GetSP());89if (process_sp) {90return ConstString(process_sp->GetPluginName()).GetCString();91}92return "<Unknown>";93}9495const char *SBProcess::GetShortPluginName() {96LLDB_INSTRUMENT_VA(this);9798ProcessSP process_sp(GetSP());99if (process_sp) {100return ConstString(process_sp->GetPluginName()).GetCString();101}102return "<Unknown>";103}104105lldb::ProcessSP SBProcess::GetSP() const { return m_opaque_wp.lock(); }106107void SBProcess::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }108109void SBProcess::Clear() {110LLDB_INSTRUMENT_VA(this);111112m_opaque_wp.reset();113}114115bool SBProcess::IsValid() const {116LLDB_INSTRUMENT_VA(this);117return this->operator bool();118}119SBProcess::operator bool() const {120LLDB_INSTRUMENT_VA(this);121122ProcessSP process_sp(m_opaque_wp.lock());123return ((bool)process_sp && process_sp->IsValid());124}125126bool SBProcess::RemoteLaunch(char const **argv, char const **envp,127const char *stdin_path, const char *stdout_path,128const char *stderr_path,129const char *working_directory,130uint32_t launch_flags, bool stop_at_entry,131lldb::SBError &error) {132LLDB_INSTRUMENT_VA(this, argv, envp, stdin_path, stdout_path, stderr_path,133working_directory, launch_flags, stop_at_entry, error);134135ProcessSP process_sp(GetSP());136if (process_sp) {137std::lock_guard<std::recursive_mutex> guard(138process_sp->GetTarget().GetAPIMutex());139if (process_sp->GetState() == eStateConnected) {140if (stop_at_entry)141launch_flags |= eLaunchFlagStopAtEntry;142ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path),143FileSpec(stderr_path),144FileSpec(working_directory), launch_flags);145Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();146if (exe_module)147launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);148if (argv)149launch_info.GetArguments().AppendArguments(argv);150if (envp)151launch_info.GetEnvironment() = Environment(envp);152error.SetError(process_sp->Launch(launch_info));153} else {154error.SetErrorString("must be in eStateConnected to call RemoteLaunch");155}156} else {157error.SetErrorString("unable to attach pid");158}159160return error.Success();161}162163bool SBProcess::RemoteAttachToProcessWithID(lldb::pid_t pid,164lldb::SBError &error) {165LLDB_INSTRUMENT_VA(this, pid, error);166167ProcessSP process_sp(GetSP());168if (process_sp) {169std::lock_guard<std::recursive_mutex> guard(170process_sp->GetTarget().GetAPIMutex());171if (process_sp->GetState() == eStateConnected) {172ProcessAttachInfo attach_info;173attach_info.SetProcessID(pid);174error.SetError(process_sp->Attach(attach_info));175} else {176error.SetErrorString(177"must be in eStateConnected to call RemoteAttachToProcessWithID");178}179} else {180error.SetErrorString("unable to attach pid");181}182183return error.Success();184}185186uint32_t SBProcess::GetNumThreads() {187LLDB_INSTRUMENT_VA(this);188189uint32_t num_threads = 0;190ProcessSP process_sp(GetSP());191if (process_sp) {192Process::StopLocker stop_locker;193194const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());195std::lock_guard<std::recursive_mutex> guard(196process_sp->GetTarget().GetAPIMutex());197num_threads = process_sp->GetThreadList().GetSize(can_update);198}199200return num_threads;201}202203SBThread SBProcess::GetSelectedThread() const {204LLDB_INSTRUMENT_VA(this);205206SBThread sb_thread;207ThreadSP thread_sp;208ProcessSP process_sp(GetSP());209if (process_sp) {210std::lock_guard<std::recursive_mutex> guard(211process_sp->GetTarget().GetAPIMutex());212thread_sp = process_sp->GetThreadList().GetSelectedThread();213sb_thread.SetThread(thread_sp);214}215216return sb_thread;217}218219SBThread SBProcess::CreateOSPluginThread(lldb::tid_t tid,220lldb::addr_t context) {221LLDB_INSTRUMENT_VA(this, tid, context);222223SBThread sb_thread;224ThreadSP thread_sp;225ProcessSP process_sp(GetSP());226if (process_sp) {227std::lock_guard<std::recursive_mutex> guard(228process_sp->GetTarget().GetAPIMutex());229thread_sp = process_sp->CreateOSPluginThread(tid, context);230sb_thread.SetThread(thread_sp);231}232233return sb_thread;234}235236SBTarget SBProcess::GetTarget() const {237LLDB_INSTRUMENT_VA(this);238239SBTarget sb_target;240TargetSP target_sp;241ProcessSP process_sp(GetSP());242if (process_sp) {243target_sp = process_sp->GetTarget().shared_from_this();244sb_target.SetSP(target_sp);245}246247return sb_target;248}249250size_t SBProcess::PutSTDIN(const char *src, size_t src_len) {251LLDB_INSTRUMENT_VA(this, src, src_len);252253size_t ret_val = 0;254ProcessSP process_sp(GetSP());255if (process_sp) {256Status error;257ret_val = process_sp->PutSTDIN(src, src_len, error);258}259260return ret_val;261}262263size_t SBProcess::GetSTDOUT(char *dst, size_t dst_len) const {264LLDB_INSTRUMENT_VA(this, dst, dst_len);265266size_t bytes_read = 0;267ProcessSP process_sp(GetSP());268if (process_sp) {269Status error;270bytes_read = process_sp->GetSTDOUT(dst, dst_len, error);271}272273return bytes_read;274}275276size_t SBProcess::GetSTDERR(char *dst, size_t dst_len) const {277LLDB_INSTRUMENT_VA(this, dst, dst_len);278279size_t bytes_read = 0;280ProcessSP process_sp(GetSP());281if (process_sp) {282Status error;283bytes_read = process_sp->GetSTDERR(dst, dst_len, error);284}285286return bytes_read;287}288289size_t SBProcess::GetAsyncProfileData(char *dst, size_t dst_len) const {290LLDB_INSTRUMENT_VA(this, dst, dst_len);291292size_t bytes_read = 0;293ProcessSP process_sp(GetSP());294if (process_sp) {295Status error;296bytes_read = process_sp->GetAsyncProfileData(dst, dst_len, error);297}298299return bytes_read;300}301302void SBProcess::ReportEventState(const SBEvent &event, SBFile out) const {303LLDB_INSTRUMENT_VA(this, event, out);304305return ReportEventState(event, out.m_opaque_sp);306}307308void SBProcess::ReportEventState(const SBEvent &event, FILE *out) const {309LLDB_INSTRUMENT_VA(this, event, out);310FileSP outfile = std::make_shared<NativeFile>(out, false);311return ReportEventState(event, outfile);312}313314void SBProcess::ReportEventState(const SBEvent &event, FileSP out) const {315316LLDB_INSTRUMENT_VA(this, event, out);317318if (!out || !out->IsValid())319return;320321ProcessSP process_sp(GetSP());322if (process_sp) {323StreamFile stream(out);324const StateType event_state = SBProcess::GetStateFromEvent(event);325stream.Printf("Process %" PRIu64 " %s\n", process_sp->GetID(),326SBDebugger::StateAsCString(event_state));327}328}329330void SBProcess::AppendEventStateReport(const SBEvent &event,331SBCommandReturnObject &result) {332LLDB_INSTRUMENT_VA(this, event, result);333334ProcessSP process_sp(GetSP());335if (process_sp) {336const StateType event_state = SBProcess::GetStateFromEvent(event);337char message[1024];338::snprintf(message, sizeof(message), "Process %" PRIu64 " %s\n",339process_sp->GetID(), SBDebugger::StateAsCString(event_state));340341result.AppendMessage(message);342}343}344345bool SBProcess::SetSelectedThread(const SBThread &thread) {346LLDB_INSTRUMENT_VA(this, thread);347348ProcessSP process_sp(GetSP());349if (process_sp) {350std::lock_guard<std::recursive_mutex> guard(351process_sp->GetTarget().GetAPIMutex());352return process_sp->GetThreadList().SetSelectedThreadByID(353thread.GetThreadID());354}355return false;356}357358bool SBProcess::SetSelectedThreadByID(lldb::tid_t tid) {359LLDB_INSTRUMENT_VA(this, tid);360361bool ret_val = false;362ProcessSP process_sp(GetSP());363if (process_sp) {364std::lock_guard<std::recursive_mutex> guard(365process_sp->GetTarget().GetAPIMutex());366ret_val = process_sp->GetThreadList().SetSelectedThreadByID(tid);367}368369return ret_val;370}371372bool SBProcess::SetSelectedThreadByIndexID(uint32_t index_id) {373LLDB_INSTRUMENT_VA(this, index_id);374375bool ret_val = false;376ProcessSP process_sp(GetSP());377if (process_sp) {378std::lock_guard<std::recursive_mutex> guard(379process_sp->GetTarget().GetAPIMutex());380ret_val = process_sp->GetThreadList().SetSelectedThreadByIndexID(index_id);381}382383return ret_val;384}385386SBThread SBProcess::GetThreadAtIndex(size_t index) {387LLDB_INSTRUMENT_VA(this, index);388389SBThread sb_thread;390ThreadSP thread_sp;391ProcessSP process_sp(GetSP());392if (process_sp) {393Process::StopLocker stop_locker;394const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());395std::lock_guard<std::recursive_mutex> guard(396process_sp->GetTarget().GetAPIMutex());397thread_sp = process_sp->GetThreadList().GetThreadAtIndex(index, can_update);398sb_thread.SetThread(thread_sp);399}400401return sb_thread;402}403404uint32_t SBProcess::GetNumQueues() {405LLDB_INSTRUMENT_VA(this);406407uint32_t num_queues = 0;408ProcessSP process_sp(GetSP());409if (process_sp) {410Process::StopLocker stop_locker;411if (stop_locker.TryLock(&process_sp->GetRunLock())) {412std::lock_guard<std::recursive_mutex> guard(413process_sp->GetTarget().GetAPIMutex());414num_queues = process_sp->GetQueueList().GetSize();415}416}417418return num_queues;419}420421SBQueue SBProcess::GetQueueAtIndex(size_t index) {422LLDB_INSTRUMENT_VA(this, index);423424SBQueue sb_queue;425QueueSP queue_sp;426ProcessSP process_sp(GetSP());427if (process_sp) {428Process::StopLocker stop_locker;429if (stop_locker.TryLock(&process_sp->GetRunLock())) {430std::lock_guard<std::recursive_mutex> guard(431process_sp->GetTarget().GetAPIMutex());432queue_sp = process_sp->GetQueueList().GetQueueAtIndex(index);433sb_queue.SetQueue(queue_sp);434}435}436437return sb_queue;438}439440uint32_t SBProcess::GetStopID(bool include_expression_stops) {441LLDB_INSTRUMENT_VA(this, include_expression_stops);442443ProcessSP process_sp(GetSP());444if (process_sp) {445std::lock_guard<std::recursive_mutex> guard(446process_sp->GetTarget().GetAPIMutex());447if (include_expression_stops)448return process_sp->GetStopID();449else450return process_sp->GetLastNaturalStopID();451}452return 0;453}454455SBEvent SBProcess::GetStopEventForStopID(uint32_t stop_id) {456LLDB_INSTRUMENT_VA(this, stop_id);457458SBEvent sb_event;459EventSP event_sp;460ProcessSP process_sp(GetSP());461if (process_sp) {462std::lock_guard<std::recursive_mutex> guard(463process_sp->GetTarget().GetAPIMutex());464event_sp = process_sp->GetStopEventForStopID(stop_id);465sb_event.reset(event_sp);466}467468return sb_event;469}470471void SBProcess::ForceScriptedState(StateType new_state) {472LLDB_INSTRUMENT_VA(this, new_state);473474if (ProcessSP process_sp = GetSP()) {475std::lock_guard<std::recursive_mutex> guard(476process_sp->GetTarget().GetAPIMutex());477process_sp->ForceScriptedState(new_state);478}479}480481StateType SBProcess::GetState() {482LLDB_INSTRUMENT_VA(this);483484StateType ret_val = eStateInvalid;485ProcessSP process_sp(GetSP());486if (process_sp) {487std::lock_guard<std::recursive_mutex> guard(488process_sp->GetTarget().GetAPIMutex());489ret_val = process_sp->GetState();490}491492return ret_val;493}494495int SBProcess::GetExitStatus() {496LLDB_INSTRUMENT_VA(this);497498int exit_status = 0;499ProcessSP process_sp(GetSP());500if (process_sp) {501std::lock_guard<std::recursive_mutex> guard(502process_sp->GetTarget().GetAPIMutex());503exit_status = process_sp->GetExitStatus();504}505506return exit_status;507}508509const char *SBProcess::GetExitDescription() {510LLDB_INSTRUMENT_VA(this);511512ProcessSP process_sp(GetSP());513if (!process_sp)514return nullptr;515516std::lock_guard<std::recursive_mutex> guard(517process_sp->GetTarget().GetAPIMutex());518return ConstString(process_sp->GetExitDescription()).GetCString();519}520521lldb::pid_t SBProcess::GetProcessID() {522LLDB_INSTRUMENT_VA(this);523524lldb::pid_t ret_val = LLDB_INVALID_PROCESS_ID;525ProcessSP process_sp(GetSP());526if (process_sp)527ret_val = process_sp->GetID();528529return ret_val;530}531532uint32_t SBProcess::GetUniqueID() {533LLDB_INSTRUMENT_VA(this);534535uint32_t ret_val = 0;536ProcessSP process_sp(GetSP());537if (process_sp)538ret_val = process_sp->GetUniqueID();539return ret_val;540}541542ByteOrder SBProcess::GetByteOrder() const {543LLDB_INSTRUMENT_VA(this);544545ByteOrder byteOrder = eByteOrderInvalid;546ProcessSP process_sp(GetSP());547if (process_sp)548byteOrder = process_sp->GetTarget().GetArchitecture().GetByteOrder();549550return byteOrder;551}552553uint32_t SBProcess::GetAddressByteSize() const {554LLDB_INSTRUMENT_VA(this);555556uint32_t size = 0;557ProcessSP process_sp(GetSP());558if (process_sp)559size = process_sp->GetTarget().GetArchitecture().GetAddressByteSize();560561return size;562}563564SBError SBProcess::Continue() {565LLDB_INSTRUMENT_VA(this);566567SBError sb_error;568ProcessSP process_sp(GetSP());569570if (process_sp) {571std::lock_guard<std::recursive_mutex> guard(572process_sp->GetTarget().GetAPIMutex());573574if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())575sb_error.ref() = process_sp->Resume();576else577sb_error.ref() = process_sp->ResumeSynchronous(nullptr);578} else579sb_error.SetErrorString("SBProcess is invalid");580581return sb_error;582}583584SBError SBProcess::Destroy() {585LLDB_INSTRUMENT_VA(this);586587SBError sb_error;588ProcessSP process_sp(GetSP());589if (process_sp) {590std::lock_guard<std::recursive_mutex> guard(591process_sp->GetTarget().GetAPIMutex());592sb_error.SetError(process_sp->Destroy(false));593} else594sb_error.SetErrorString("SBProcess is invalid");595596return sb_error;597}598599SBError SBProcess::Stop() {600LLDB_INSTRUMENT_VA(this);601602SBError sb_error;603ProcessSP process_sp(GetSP());604if (process_sp) {605std::lock_guard<std::recursive_mutex> guard(606process_sp->GetTarget().GetAPIMutex());607sb_error.SetError(process_sp->Halt());608} else609sb_error.SetErrorString("SBProcess is invalid");610611return sb_error;612}613614SBError SBProcess::Kill() {615LLDB_INSTRUMENT_VA(this);616617SBError sb_error;618ProcessSP process_sp(GetSP());619if (process_sp) {620std::lock_guard<std::recursive_mutex> guard(621process_sp->GetTarget().GetAPIMutex());622sb_error.SetError(process_sp->Destroy(true));623} else624sb_error.SetErrorString("SBProcess is invalid");625626return sb_error;627}628629SBError SBProcess::Detach() {630LLDB_INSTRUMENT_VA(this);631632// FIXME: This should come from a process default.633bool keep_stopped = false;634return Detach(keep_stopped);635}636637SBError SBProcess::Detach(bool keep_stopped) {638LLDB_INSTRUMENT_VA(this, keep_stopped);639640SBError sb_error;641ProcessSP process_sp(GetSP());642if (process_sp) {643std::lock_guard<std::recursive_mutex> guard(644process_sp->GetTarget().GetAPIMutex());645sb_error.SetError(process_sp->Detach(keep_stopped));646} else647sb_error.SetErrorString("SBProcess is invalid");648649return sb_error;650}651652SBError SBProcess::Signal(int signo) {653LLDB_INSTRUMENT_VA(this, signo);654655SBError sb_error;656ProcessSP process_sp(GetSP());657if (process_sp) {658std::lock_guard<std::recursive_mutex> guard(659process_sp->GetTarget().GetAPIMutex());660sb_error.SetError(process_sp->Signal(signo));661} else662sb_error.SetErrorString("SBProcess is invalid");663664return sb_error;665}666667SBUnixSignals SBProcess::GetUnixSignals() {668LLDB_INSTRUMENT_VA(this);669670if (auto process_sp = GetSP())671return SBUnixSignals{process_sp};672673return SBUnixSignals{};674}675676void SBProcess::SendAsyncInterrupt() {677LLDB_INSTRUMENT_VA(this);678679ProcessSP process_sp(GetSP());680if (process_sp) {681process_sp->SendAsyncInterrupt();682}683}684685SBThread SBProcess::GetThreadByID(tid_t tid) {686LLDB_INSTRUMENT_VA(this, tid);687688SBThread sb_thread;689ThreadSP thread_sp;690ProcessSP process_sp(GetSP());691if (process_sp) {692Process::StopLocker stop_locker;693const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());694std::lock_guard<std::recursive_mutex> guard(695process_sp->GetTarget().GetAPIMutex());696thread_sp = process_sp->GetThreadList().FindThreadByID(tid, can_update);697sb_thread.SetThread(thread_sp);698}699700return sb_thread;701}702703SBThread SBProcess::GetThreadByIndexID(uint32_t index_id) {704LLDB_INSTRUMENT_VA(this, index_id);705706SBThread sb_thread;707ThreadSP thread_sp;708ProcessSP process_sp(GetSP());709if (process_sp) {710Process::StopLocker stop_locker;711const bool can_update = stop_locker.TryLock(&process_sp->GetRunLock());712std::lock_guard<std::recursive_mutex> guard(713process_sp->GetTarget().GetAPIMutex());714thread_sp =715process_sp->GetThreadList().FindThreadByIndexID(index_id, can_update);716sb_thread.SetThread(thread_sp);717}718719return sb_thread;720}721722StateType SBProcess::GetStateFromEvent(const SBEvent &event) {723LLDB_INSTRUMENT_VA(event);724725StateType ret_val = Process::ProcessEventData::GetStateFromEvent(event.get());726727return ret_val;728}729730bool SBProcess::GetRestartedFromEvent(const SBEvent &event) {731LLDB_INSTRUMENT_VA(event);732733bool ret_val = Process::ProcessEventData::GetRestartedFromEvent(event.get());734735return ret_val;736}737738size_t SBProcess::GetNumRestartedReasonsFromEvent(const lldb::SBEvent &event) {739LLDB_INSTRUMENT_VA(event);740741return Process::ProcessEventData::GetNumRestartedReasons(event.get());742}743744const char *745SBProcess::GetRestartedReasonAtIndexFromEvent(const lldb::SBEvent &event,746size_t idx) {747LLDB_INSTRUMENT_VA(event, idx);748749return ConstString(Process::ProcessEventData::GetRestartedReasonAtIndex(750event.get(), idx))751.GetCString();752}753754SBProcess SBProcess::GetProcessFromEvent(const SBEvent &event) {755LLDB_INSTRUMENT_VA(event);756757ProcessSP process_sp =758Process::ProcessEventData::GetProcessFromEvent(event.get());759if (!process_sp) {760// StructuredData events also know the process they come from. Try that.761process_sp = EventDataStructuredData::GetProcessFromEvent(event.get());762}763764return SBProcess(process_sp);765}766767bool SBProcess::GetInterruptedFromEvent(const SBEvent &event) {768LLDB_INSTRUMENT_VA(event);769770return Process::ProcessEventData::GetInterruptedFromEvent(event.get());771}772773lldb::SBStructuredData774SBProcess::GetStructuredDataFromEvent(const lldb::SBEvent &event) {775LLDB_INSTRUMENT_VA(event);776777return SBStructuredData(event.GetSP());778}779780bool SBProcess::EventIsProcessEvent(const SBEvent &event) {781LLDB_INSTRUMENT_VA(event);782783return Process::ProcessEventData::GetEventDataFromEvent(event.get()) !=784nullptr;785}786787bool SBProcess::EventIsStructuredDataEvent(const lldb::SBEvent &event) {788LLDB_INSTRUMENT_VA(event);789790EventSP event_sp = event.GetSP();791EventData *event_data = event_sp ? event_sp->GetData() : nullptr;792return event_data && (event_data->GetFlavor() ==793EventDataStructuredData::GetFlavorString());794}795796SBBroadcaster SBProcess::GetBroadcaster() const {797LLDB_INSTRUMENT_VA(this);798799ProcessSP process_sp(GetSP());800801SBBroadcaster broadcaster(process_sp.get(), false);802803return broadcaster;804}805806const char *SBProcess::GetBroadcasterClass() {807LLDB_INSTRUMENT();808809return ConstString(Process::GetStaticBroadcasterClass()).AsCString();810}811812lldb::SBAddressRangeList SBProcess::FindRangesInMemory(813const void *buf, uint64_t size, const SBAddressRangeList &ranges,814uint32_t alignment, uint32_t max_matches, SBError &error) {815LLDB_INSTRUMENT_VA(this, buf, size, ranges, alignment, max_matches, error);816817lldb::SBAddressRangeList matches;818819ProcessSP process_sp(GetSP());820if (!process_sp) {821error.SetErrorString("SBProcess is invalid");822return matches;823}824Process::StopLocker stop_locker;825if (!stop_locker.TryLock(&process_sp->GetRunLock())) {826error.SetErrorString("process is running");827return matches;828}829std::lock_guard<std::recursive_mutex> guard(830process_sp->GetTarget().GetAPIMutex());831matches.m_opaque_up->ref() = process_sp->FindRangesInMemory(832reinterpret_cast<const uint8_t *>(buf), size, ranges.ref().ref(),833alignment, max_matches, error.ref());834return matches;835}836837lldb::addr_t SBProcess::FindInMemory(const void *buf, uint64_t size,838const SBAddressRange &range,839uint32_t alignment, SBError &error) {840LLDB_INSTRUMENT_VA(this, buf, size, range, alignment, error);841842ProcessSP process_sp(GetSP());843844if (!process_sp) {845error.SetErrorString("SBProcess is invalid");846return LLDB_INVALID_ADDRESS;847}848849Process::StopLocker stop_locker;850if (!stop_locker.TryLock(&process_sp->GetRunLock())) {851error.SetErrorString("process is running");852return LLDB_INVALID_ADDRESS;853}854855std::lock_guard<std::recursive_mutex> guard(856process_sp->GetTarget().GetAPIMutex());857return process_sp->FindInMemory(reinterpret_cast<const uint8_t *>(buf), size,858range.ref(), alignment, error.ref());859}860861size_t SBProcess::ReadMemory(addr_t addr, void *dst, size_t dst_len,862SBError &sb_error) {863LLDB_INSTRUMENT_VA(this, addr, dst, dst_len, sb_error);864865if (!dst) {866sb_error.SetErrorStringWithFormat(867"no buffer provided to read %zu bytes into", dst_len);868return 0;869}870871size_t bytes_read = 0;872ProcessSP process_sp(GetSP());873874875if (process_sp) {876Process::StopLocker stop_locker;877if (stop_locker.TryLock(&process_sp->GetRunLock())) {878std::lock_guard<std::recursive_mutex> guard(879process_sp->GetTarget().GetAPIMutex());880bytes_read = process_sp->ReadMemory(addr, dst, dst_len, sb_error.ref());881} else {882sb_error.SetErrorString("process is running");883}884} else {885sb_error.SetErrorString("SBProcess is invalid");886}887888return bytes_read;889}890891size_t SBProcess::ReadCStringFromMemory(addr_t addr, void *buf, size_t size,892lldb::SBError &sb_error) {893LLDB_INSTRUMENT_VA(this, addr, buf, size, sb_error);894895size_t bytes_read = 0;896ProcessSP process_sp(GetSP());897if (process_sp) {898Process::StopLocker stop_locker;899if (stop_locker.TryLock(&process_sp->GetRunLock())) {900std::lock_guard<std::recursive_mutex> guard(901process_sp->GetTarget().GetAPIMutex());902bytes_read = process_sp->ReadCStringFromMemory(addr, (char *)buf, size,903sb_error.ref());904} else {905sb_error.SetErrorString("process is running");906}907} else {908sb_error.SetErrorString("SBProcess is invalid");909}910return bytes_read;911}912913uint64_t SBProcess::ReadUnsignedFromMemory(addr_t addr, uint32_t byte_size,914lldb::SBError &sb_error) {915LLDB_INSTRUMENT_VA(this, addr, byte_size, sb_error);916917uint64_t value = 0;918ProcessSP process_sp(GetSP());919if (process_sp) {920Process::StopLocker stop_locker;921if (stop_locker.TryLock(&process_sp->GetRunLock())) {922std::lock_guard<std::recursive_mutex> guard(923process_sp->GetTarget().GetAPIMutex());924value = process_sp->ReadUnsignedIntegerFromMemory(addr, byte_size, 0,925sb_error.ref());926} else {927sb_error.SetErrorString("process is running");928}929} else {930sb_error.SetErrorString("SBProcess is invalid");931}932return value;933}934935lldb::addr_t SBProcess::ReadPointerFromMemory(addr_t addr,936lldb::SBError &sb_error) {937LLDB_INSTRUMENT_VA(this, addr, sb_error);938939lldb::addr_t ptr = LLDB_INVALID_ADDRESS;940ProcessSP process_sp(GetSP());941if (process_sp) {942Process::StopLocker stop_locker;943if (stop_locker.TryLock(&process_sp->GetRunLock())) {944std::lock_guard<std::recursive_mutex> guard(945process_sp->GetTarget().GetAPIMutex());946ptr = process_sp->ReadPointerFromMemory(addr, sb_error.ref());947} else {948sb_error.SetErrorString("process is running");949}950} else {951sb_error.SetErrorString("SBProcess is invalid");952}953return ptr;954}955956size_t SBProcess::WriteMemory(addr_t addr, const void *src, size_t src_len,957SBError &sb_error) {958LLDB_INSTRUMENT_VA(this, addr, src, src_len, sb_error);959960size_t bytes_written = 0;961962ProcessSP process_sp(GetSP());963964if (process_sp) {965Process::StopLocker stop_locker;966if (stop_locker.TryLock(&process_sp->GetRunLock())) {967std::lock_guard<std::recursive_mutex> guard(968process_sp->GetTarget().GetAPIMutex());969bytes_written =970process_sp->WriteMemory(addr, src, src_len, sb_error.ref());971} else {972sb_error.SetErrorString("process is running");973}974}975976return bytes_written;977}978979void SBProcess::GetStatus(SBStream &status) {980LLDB_INSTRUMENT_VA(this, status);981982ProcessSP process_sp(GetSP());983if (process_sp)984process_sp->GetStatus(status.ref());985}986987bool SBProcess::GetDescription(SBStream &description) {988LLDB_INSTRUMENT_VA(this, description);989990Stream &strm = description.ref();991992ProcessSP process_sp(GetSP());993if (process_sp) {994char path[PATH_MAX];995GetTarget().GetExecutable().GetPath(path, sizeof(path));996Module *exe_module = process_sp->GetTarget().GetExecutableModulePointer();997const char *exe_name = nullptr;998if (exe_module)999exe_name = exe_module->GetFileSpec().GetFilename().AsCString();10001001strm.Printf("SBProcess: pid = %" PRIu64 ", state = %s, threads = %d%s%s",1002process_sp->GetID(), lldb_private::StateAsCString(GetState()),1003GetNumThreads(), exe_name ? ", executable = " : "",1004exe_name ? exe_name : "");1005} else1006strm.PutCString("No value");10071008return true;1009}10101011SBStructuredData SBProcess::GetExtendedCrashInformation() {1012LLDB_INSTRUMENT_VA(this);1013SBStructuredData data;1014ProcessSP process_sp(GetSP());1015if (!process_sp)1016return data;10171018PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();10191020if (!platform_sp)1021return data;10221023auto expected_data =1024platform_sp->FetchExtendedCrashInformation(*process_sp.get());10251026if (!expected_data)1027return data;10281029StructuredData::ObjectSP fetched_data = *expected_data;1030data.m_impl_up->SetObjectSP(fetched_data);1031return data;1032}10331034uint32_t1035SBProcess::GetNumSupportedHardwareWatchpoints(lldb::SBError &sb_error) const {1036LLDB_INSTRUMENT_VA(this, sb_error);10371038uint32_t num = 0;1039ProcessSP process_sp(GetSP());1040if (process_sp) {1041std::lock_guard<std::recursive_mutex> guard(1042process_sp->GetTarget().GetAPIMutex());1043std::optional<uint32_t> actual_num = process_sp->GetWatchpointSlotCount();1044if (actual_num) {1045num = *actual_num;1046} else {1047sb_error.SetErrorString("Unable to determine number of watchpoints");1048}1049} else {1050sb_error.SetErrorString("SBProcess is invalid");1051}1052return num;1053}10541055uint32_t SBProcess::LoadImage(lldb::SBFileSpec &sb_remote_image_spec,1056lldb::SBError &sb_error) {1057LLDB_INSTRUMENT_VA(this, sb_remote_image_spec, sb_error);10581059return LoadImage(SBFileSpec(), sb_remote_image_spec, sb_error);1060}10611062uint32_t SBProcess::LoadImage(const lldb::SBFileSpec &sb_local_image_spec,1063const lldb::SBFileSpec &sb_remote_image_spec,1064lldb::SBError &sb_error) {1065LLDB_INSTRUMENT_VA(this, sb_local_image_spec, sb_remote_image_spec, sb_error);10661067ProcessSP process_sp(GetSP());1068if (process_sp) {1069Process::StopLocker stop_locker;1070if (stop_locker.TryLock(&process_sp->GetRunLock())) {1071std::lock_guard<std::recursive_mutex> guard(1072process_sp->GetTarget().GetAPIMutex());1073PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();1074return platform_sp->LoadImage(process_sp.get(), *sb_local_image_spec,1075*sb_remote_image_spec, sb_error.ref());1076} else {1077sb_error.SetErrorString("process is running");1078}1079} else {1080sb_error.SetErrorString("process is invalid");1081}1082return LLDB_INVALID_IMAGE_TOKEN;1083}10841085uint32_t SBProcess::LoadImageUsingPaths(const lldb::SBFileSpec &image_spec,1086SBStringList &paths,1087lldb::SBFileSpec &loaded_path,1088lldb::SBError &error) {1089LLDB_INSTRUMENT_VA(this, image_spec, paths, loaded_path, error);10901091ProcessSP process_sp(GetSP());1092if (process_sp) {1093Process::StopLocker stop_locker;1094if (stop_locker.TryLock(&process_sp->GetRunLock())) {1095std::lock_guard<std::recursive_mutex> guard(1096process_sp->GetTarget().GetAPIMutex());1097PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();1098size_t num_paths = paths.GetSize();1099std::vector<std::string> paths_vec;1100paths_vec.reserve(num_paths);1101for (size_t i = 0; i < num_paths; i++)1102paths_vec.push_back(paths.GetStringAtIndex(i));1103FileSpec loaded_spec;11041105uint32_t token = platform_sp->LoadImageUsingPaths(1106process_sp.get(), *image_spec, paths_vec, error.ref(), &loaded_spec);1107if (token != LLDB_INVALID_IMAGE_TOKEN)1108loaded_path = loaded_spec;1109return token;1110} else {1111error.SetErrorString("process is running");1112}1113} else {1114error.SetErrorString("process is invalid");1115}11161117return LLDB_INVALID_IMAGE_TOKEN;1118}11191120lldb::SBError SBProcess::UnloadImage(uint32_t image_token) {1121LLDB_INSTRUMENT_VA(this, image_token);11221123lldb::SBError sb_error;1124ProcessSP process_sp(GetSP());1125if (process_sp) {1126Process::StopLocker stop_locker;1127if (stop_locker.TryLock(&process_sp->GetRunLock())) {1128std::lock_guard<std::recursive_mutex> guard(1129process_sp->GetTarget().GetAPIMutex());1130PlatformSP platform_sp = process_sp->GetTarget().GetPlatform();1131sb_error.SetError(1132platform_sp->UnloadImage(process_sp.get(), image_token));1133} else {1134sb_error.SetErrorString("process is running");1135}1136} else1137sb_error.SetErrorString("invalid process");1138return sb_error;1139}11401141lldb::SBError SBProcess::SendEventData(const char *event_data) {1142LLDB_INSTRUMENT_VA(this, event_data);11431144lldb::SBError sb_error;1145ProcessSP process_sp(GetSP());1146if (process_sp) {1147Process::StopLocker stop_locker;1148if (stop_locker.TryLock(&process_sp->GetRunLock())) {1149std::lock_guard<std::recursive_mutex> guard(1150process_sp->GetTarget().GetAPIMutex());1151sb_error.SetError(process_sp->SendEventData(event_data));1152} else {1153sb_error.SetErrorString("process is running");1154}1155} else1156sb_error.SetErrorString("invalid process");1157return sb_error;1158}11591160uint32_t SBProcess::GetNumExtendedBacktraceTypes() {1161LLDB_INSTRUMENT_VA(this);11621163ProcessSP process_sp(GetSP());1164if (process_sp && process_sp->GetSystemRuntime()) {1165SystemRuntime *runtime = process_sp->GetSystemRuntime();1166return runtime->GetExtendedBacktraceTypes().size();1167}1168return 0;1169}11701171const char *SBProcess::GetExtendedBacktraceTypeAtIndex(uint32_t idx) {1172LLDB_INSTRUMENT_VA(this, idx);11731174ProcessSP process_sp(GetSP());1175if (process_sp && process_sp->GetSystemRuntime()) {1176SystemRuntime *runtime = process_sp->GetSystemRuntime();1177const std::vector<ConstString> &names =1178runtime->GetExtendedBacktraceTypes();1179if (idx < names.size()) {1180return names[idx].AsCString();1181}1182}1183return nullptr;1184}11851186SBThreadCollection SBProcess::GetHistoryThreads(addr_t addr) {1187LLDB_INSTRUMENT_VA(this, addr);11881189ProcessSP process_sp(GetSP());1190SBThreadCollection threads;1191if (process_sp) {1192threads = SBThreadCollection(process_sp->GetHistoryThreads(addr));1193}1194return threads;1195}11961197bool SBProcess::IsInstrumentationRuntimePresent(1198InstrumentationRuntimeType type) {1199LLDB_INSTRUMENT_VA(this, type);12001201ProcessSP process_sp(GetSP());1202if (!process_sp)1203return false;12041205std::lock_guard<std::recursive_mutex> guard(1206process_sp->GetTarget().GetAPIMutex());12071208InstrumentationRuntimeSP runtime_sp =1209process_sp->GetInstrumentationRuntime(type);12101211if (!runtime_sp.get())1212return false;12131214return runtime_sp->IsActive();1215}12161217lldb::SBError SBProcess::SaveCore(const char *file_name) {1218LLDB_INSTRUMENT_VA(this, file_name);1219SBSaveCoreOptions options;1220options.SetOutputFile(SBFileSpec(file_name));1221options.SetStyle(SaveCoreStyle::eSaveCoreFull);1222return SaveCore(options);1223}12241225lldb::SBError SBProcess::SaveCore(const char *file_name,1226const char *flavor,1227SaveCoreStyle core_style) {1228LLDB_INSTRUMENT_VA(this, file_name, flavor, core_style);1229SBSaveCoreOptions options;1230options.SetOutputFile(SBFileSpec(file_name));1231options.SetStyle(core_style);1232SBError error = options.SetPluginName(flavor);1233if (error.Fail())1234return error;1235return SaveCore(options);1236}12371238lldb::SBError SBProcess::SaveCore(SBSaveCoreOptions &options) {12391240LLDB_INSTRUMENT_VA(this, options);12411242lldb::SBError error;1243ProcessSP process_sp(GetSP());1244if (!process_sp) {1245error.SetErrorString("SBProcess is invalid");1246return error;1247}12481249std::lock_guard<std::recursive_mutex> guard(1250process_sp->GetTarget().GetAPIMutex());12511252if (process_sp->GetState() != eStateStopped) {1253error.SetErrorString("the process is not stopped");1254return error;1255}12561257error.ref() = PluginManager::SaveCore(process_sp, options.ref());12581259return error;1260}12611262lldb::SBError1263SBProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,1264SBMemoryRegionInfo &sb_region_info) {1265LLDB_INSTRUMENT_VA(this, load_addr, sb_region_info);12661267lldb::SBError sb_error;1268ProcessSP process_sp(GetSP());1269if (process_sp) {1270Process::StopLocker stop_locker;1271if (stop_locker.TryLock(&process_sp->GetRunLock())) {1272std::lock_guard<std::recursive_mutex> guard(1273process_sp->GetTarget().GetAPIMutex());12741275sb_error.ref() =1276process_sp->GetMemoryRegionInfo(load_addr, sb_region_info.ref());1277} else {1278sb_error.SetErrorString("process is running");1279}1280} else {1281sb_error.SetErrorString("SBProcess is invalid");1282}1283return sb_error;1284}12851286lldb::SBMemoryRegionInfoList SBProcess::GetMemoryRegions() {1287LLDB_INSTRUMENT_VA(this);12881289lldb::SBMemoryRegionInfoList sb_region_list;12901291ProcessSP process_sp(GetSP());1292Process::StopLocker stop_locker;1293if (process_sp && stop_locker.TryLock(&process_sp->GetRunLock())) {1294std::lock_guard<std::recursive_mutex> guard(1295process_sp->GetTarget().GetAPIMutex());12961297process_sp->GetMemoryRegions(sb_region_list.ref());1298}12991300return sb_region_list;1301}13021303lldb::SBProcessInfo SBProcess::GetProcessInfo() {1304LLDB_INSTRUMENT_VA(this);13051306lldb::SBProcessInfo sb_proc_info;1307ProcessSP process_sp(GetSP());1308ProcessInstanceInfo proc_info;1309if (process_sp && process_sp->GetProcessInfo(proc_info)) {1310sb_proc_info.SetProcessInfo(proc_info);1311}1312return sb_proc_info;1313}13141315lldb::SBFileSpec SBProcess::GetCoreFile() {1316LLDB_INSTRUMENT_VA(this);13171318ProcessSP process_sp(GetSP());1319FileSpec core_file;1320if (process_sp) {1321core_file = process_sp->GetCoreFile();1322}1323return SBFileSpec(core_file);1324}13251326addr_t SBProcess::GetAddressMask(AddressMaskType type,1327AddressMaskRange addr_range) {1328LLDB_INSTRUMENT_VA(this, type, addr_range);13291330if (ProcessSP process_sp = GetSP()) {1331switch (type) {1332case eAddressMaskTypeCode:1333if (addr_range == eAddressMaskRangeHigh)1334return process_sp->GetHighmemCodeAddressMask();1335else1336return process_sp->GetCodeAddressMask();1337case eAddressMaskTypeData:1338if (addr_range == eAddressMaskRangeHigh)1339return process_sp->GetHighmemDataAddressMask();1340else1341return process_sp->GetDataAddressMask();1342case eAddressMaskTypeAny:1343if (addr_range == eAddressMaskRangeHigh)1344return process_sp->GetHighmemDataAddressMask();1345else1346return process_sp->GetDataAddressMask();1347}1348}1349return LLDB_INVALID_ADDRESS_MASK;1350}13511352void SBProcess::SetAddressMask(AddressMaskType type, addr_t mask,1353AddressMaskRange addr_range) {1354LLDB_INSTRUMENT_VA(this, type, mask, addr_range);13551356if (ProcessSP process_sp = GetSP()) {1357switch (type) {1358case eAddressMaskTypeCode:1359if (addr_range == eAddressMaskRangeAll) {1360process_sp->SetCodeAddressMask(mask);1361process_sp->SetHighmemCodeAddressMask(mask);1362} else if (addr_range == eAddressMaskRangeHigh) {1363process_sp->SetHighmemCodeAddressMask(mask);1364} else {1365process_sp->SetCodeAddressMask(mask);1366}1367break;1368case eAddressMaskTypeData:1369if (addr_range == eAddressMaskRangeAll) {1370process_sp->SetDataAddressMask(mask);1371process_sp->SetHighmemDataAddressMask(mask);1372} else if (addr_range == eAddressMaskRangeHigh) {1373process_sp->SetHighmemDataAddressMask(mask);1374} else {1375process_sp->SetDataAddressMask(mask);1376}1377break;1378case eAddressMaskTypeAll:1379if (addr_range == eAddressMaskRangeAll) {1380process_sp->SetCodeAddressMask(mask);1381process_sp->SetDataAddressMask(mask);1382process_sp->SetHighmemCodeAddressMask(mask);1383process_sp->SetHighmemDataAddressMask(mask);1384} else if (addr_range == eAddressMaskRangeHigh) {1385process_sp->SetHighmemCodeAddressMask(mask);1386process_sp->SetHighmemDataAddressMask(mask);1387} else {1388process_sp->SetCodeAddressMask(mask);1389process_sp->SetDataAddressMask(mask);1390}1391break;1392}1393}1394}13951396void SBProcess::SetAddressableBits(AddressMaskType type, uint32_t num_bits,1397AddressMaskRange addr_range) {1398LLDB_INSTRUMENT_VA(this, type, num_bits, addr_range);13991400SetAddressMask(type, AddressableBits::AddressableBitToMask(num_bits),1401addr_range);1402}14031404addr_t SBProcess::FixAddress(addr_t addr, AddressMaskType type) {1405LLDB_INSTRUMENT_VA(this, addr, type);14061407if (ProcessSP process_sp = GetSP()) {1408if (type == eAddressMaskTypeAny)1409return process_sp->FixAnyAddress(addr);1410else if (type == eAddressMaskTypeData)1411return process_sp->FixDataAddress(addr);1412else if (type == eAddressMaskTypeCode)1413return process_sp->FixCodeAddress(addr);1414}1415return addr;1416}14171418lldb::addr_t SBProcess::AllocateMemory(size_t size, uint32_t permissions,1419lldb::SBError &sb_error) {1420LLDB_INSTRUMENT_VA(this, size, permissions, sb_error);14211422lldb::addr_t addr = LLDB_INVALID_ADDRESS;1423ProcessSP process_sp(GetSP());1424if (process_sp) {1425Process::StopLocker stop_locker;1426if (stop_locker.TryLock(&process_sp->GetRunLock())) {1427std::lock_guard<std::recursive_mutex> guard(1428process_sp->GetTarget().GetAPIMutex());1429addr = process_sp->AllocateMemory(size, permissions, sb_error.ref());1430} else {1431sb_error.SetErrorString("process is running");1432}1433} else {1434sb_error.SetErrorString("SBProcess is invalid");1435}1436return addr;1437}14381439lldb::SBError SBProcess::DeallocateMemory(lldb::addr_t ptr) {1440LLDB_INSTRUMENT_VA(this, ptr);14411442lldb::SBError sb_error;1443ProcessSP process_sp(GetSP());1444if (process_sp) {1445Process::StopLocker stop_locker;1446if (stop_locker.TryLock(&process_sp->GetRunLock())) {1447std::lock_guard<std::recursive_mutex> guard(1448process_sp->GetTarget().GetAPIMutex());1449Status error = process_sp->DeallocateMemory(ptr);1450sb_error.SetError(error);1451} else {1452sb_error.SetErrorString("process is running");1453}1454} else {1455sb_error.SetErrorString("SBProcess is invalid");1456}1457return sb_error;1458}14591460lldb::SBScriptObject SBProcess::GetScriptedImplementation() {1461LLDB_INSTRUMENT_VA(this);1462ProcessSP process_sp(GetSP());1463return lldb::SBScriptObject((process_sp) ? process_sp->GetImplementation()1464: nullptr,1465eScriptLanguageDefault);1466}146714681469