Path: blob/main/contrib/llvm-project/lldb/source/API/SBLaunchInfo.cpp
39587 views
//===-- SBLaunchInfo.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/SBLaunchInfo.h"9#include "lldb/Utility/Instrumentation.h"1011#include "lldb/API/SBEnvironment.h"12#include "lldb/API/SBError.h"13#include "lldb/API/SBFileSpec.h"14#include "lldb/API/SBListener.h"15#include "lldb/API/SBStream.h"16#include "lldb/API/SBStructuredData.h"17#include "lldb/Core/StructuredDataImpl.h"18#include "lldb/Host/ProcessLaunchInfo.h"19#include "lldb/Utility/Listener.h"20#include "lldb/Utility/ScriptedMetadata.h"2122using namespace lldb;23using namespace lldb_private;2425class lldb_private::SBLaunchInfoImpl : public ProcessLaunchInfo {26public:27SBLaunchInfoImpl() : m_envp(GetEnvironment().getEnvp()) {}2829const char *const *GetEnvp() const { return m_envp; }30void RegenerateEnvp() { m_envp = GetEnvironment().getEnvp(); }3132SBLaunchInfoImpl &operator=(const ProcessLaunchInfo &rhs) {33ProcessLaunchInfo::operator=(rhs);34RegenerateEnvp();35return *this;36}3738private:39Environment::Envp m_envp;40};4142SBLaunchInfo::SBLaunchInfo(const char **argv)43: m_opaque_sp(new SBLaunchInfoImpl()) {44LLDB_INSTRUMENT_VA(this, argv);4546m_opaque_sp->GetFlags().Reset(eLaunchFlagDebug | eLaunchFlagDisableASLR);47if (argv && argv[0])48m_opaque_sp->GetArguments().SetArguments(argv);49}5051SBLaunchInfo::SBLaunchInfo(const SBLaunchInfo &rhs) {52LLDB_INSTRUMENT_VA(this, rhs);5354m_opaque_sp = rhs.m_opaque_sp;55}5657SBLaunchInfo &SBLaunchInfo::operator=(const SBLaunchInfo &rhs) {58LLDB_INSTRUMENT_VA(this, rhs);5960m_opaque_sp = rhs.m_opaque_sp;61return *this;62}6364SBLaunchInfo::~SBLaunchInfo() = default;6566const lldb_private::ProcessLaunchInfo &SBLaunchInfo::ref() const {67return *m_opaque_sp;68}6970void SBLaunchInfo::set_ref(const ProcessLaunchInfo &info) {71*m_opaque_sp = info;72}7374lldb::pid_t SBLaunchInfo::GetProcessID() {75LLDB_INSTRUMENT_VA(this);7677return m_opaque_sp->GetProcessID();78}7980uint32_t SBLaunchInfo::GetUserID() {81LLDB_INSTRUMENT_VA(this);8283return m_opaque_sp->GetUserID();84}8586uint32_t SBLaunchInfo::GetGroupID() {87LLDB_INSTRUMENT_VA(this);8889return m_opaque_sp->GetGroupID();90}9192bool SBLaunchInfo::UserIDIsValid() {93LLDB_INSTRUMENT_VA(this);9495return m_opaque_sp->UserIDIsValid();96}9798bool SBLaunchInfo::GroupIDIsValid() {99LLDB_INSTRUMENT_VA(this);100101return m_opaque_sp->GroupIDIsValid();102}103104void SBLaunchInfo::SetUserID(uint32_t uid) {105LLDB_INSTRUMENT_VA(this, uid);106107m_opaque_sp->SetUserID(uid);108}109110void SBLaunchInfo::SetGroupID(uint32_t gid) {111LLDB_INSTRUMENT_VA(this, gid);112113m_opaque_sp->SetGroupID(gid);114}115116SBFileSpec SBLaunchInfo::GetExecutableFile() {117LLDB_INSTRUMENT_VA(this);118119return SBFileSpec(m_opaque_sp->GetExecutableFile());120}121122void SBLaunchInfo::SetExecutableFile(SBFileSpec exe_file,123bool add_as_first_arg) {124LLDB_INSTRUMENT_VA(this, exe_file, add_as_first_arg);125126m_opaque_sp->SetExecutableFile(exe_file.ref(), add_as_first_arg);127}128129SBListener SBLaunchInfo::GetListener() {130LLDB_INSTRUMENT_VA(this);131132return SBListener(m_opaque_sp->GetListener());133}134135void SBLaunchInfo::SetListener(SBListener &listener) {136LLDB_INSTRUMENT_VA(this, listener);137138m_opaque_sp->SetListener(listener.GetSP());139}140141uint32_t SBLaunchInfo::GetNumArguments() {142LLDB_INSTRUMENT_VA(this);143144return m_opaque_sp->GetArguments().GetArgumentCount();145}146147const char *SBLaunchInfo::GetArgumentAtIndex(uint32_t idx) {148LLDB_INSTRUMENT_VA(this, idx);149150return ConstString(m_opaque_sp->GetArguments().GetArgumentAtIndex(idx))151.GetCString();152}153154void SBLaunchInfo::SetArguments(const char **argv, bool append) {155LLDB_INSTRUMENT_VA(this, argv, append);156157if (append) {158if (argv)159m_opaque_sp->GetArguments().AppendArguments(argv);160} else {161if (argv)162m_opaque_sp->GetArguments().SetArguments(argv);163else164m_opaque_sp->GetArguments().Clear();165}166}167168uint32_t SBLaunchInfo::GetNumEnvironmentEntries() {169LLDB_INSTRUMENT_VA(this);170171return m_opaque_sp->GetEnvironment().size();172}173174const char *SBLaunchInfo::GetEnvironmentEntryAtIndex(uint32_t idx) {175LLDB_INSTRUMENT_VA(this, idx);176177if (idx > GetNumEnvironmentEntries())178return nullptr;179return ConstString(m_opaque_sp->GetEnvp()[idx]).GetCString();180}181182void SBLaunchInfo::SetEnvironmentEntries(const char **envp, bool append) {183LLDB_INSTRUMENT_VA(this, envp, append);184SetEnvironment(SBEnvironment(Environment(envp)), append);185}186187void SBLaunchInfo::SetEnvironment(const SBEnvironment &env, bool append) {188LLDB_INSTRUMENT_VA(this, env, append);189Environment &refEnv = env.ref();190if (append) {191for (auto &KV : refEnv)192m_opaque_sp->GetEnvironment().insert_or_assign(KV.first(), KV.second);193} else194m_opaque_sp->GetEnvironment() = refEnv;195m_opaque_sp->RegenerateEnvp();196}197198SBEnvironment SBLaunchInfo::GetEnvironment() {199LLDB_INSTRUMENT_VA(this);200return SBEnvironment(Environment(m_opaque_sp->GetEnvironment()));201}202203void SBLaunchInfo::Clear() {204LLDB_INSTRUMENT_VA(this);205206m_opaque_sp->Clear();207}208209const char *SBLaunchInfo::GetWorkingDirectory() const {210LLDB_INSTRUMENT_VA(this);211212return m_opaque_sp->GetWorkingDirectory().GetPathAsConstString().AsCString();213}214215void SBLaunchInfo::SetWorkingDirectory(const char *working_dir) {216LLDB_INSTRUMENT_VA(this, working_dir);217218m_opaque_sp->SetWorkingDirectory(FileSpec(working_dir));219}220221uint32_t SBLaunchInfo::GetLaunchFlags() {222LLDB_INSTRUMENT_VA(this);223224return m_opaque_sp->GetFlags().Get();225}226227void SBLaunchInfo::SetLaunchFlags(uint32_t flags) {228LLDB_INSTRUMENT_VA(this, flags);229230m_opaque_sp->GetFlags().Reset(flags);231}232233const char *SBLaunchInfo::GetProcessPluginName() {234LLDB_INSTRUMENT_VA(this);235236return ConstString(m_opaque_sp->GetProcessPluginName()).GetCString();237}238239void SBLaunchInfo::SetProcessPluginName(const char *plugin_name) {240LLDB_INSTRUMENT_VA(this, plugin_name);241242return m_opaque_sp->SetProcessPluginName(plugin_name);243}244245const char *SBLaunchInfo::GetShell() {246LLDB_INSTRUMENT_VA(this);247248// Constify this string so that it is saved in the string pool. Otherwise it249// would be freed when this function goes out of scope.250ConstString shell(m_opaque_sp->GetShell().GetPath().c_str());251return shell.AsCString();252}253254void SBLaunchInfo::SetShell(const char *path) {255LLDB_INSTRUMENT_VA(this, path);256257m_opaque_sp->SetShell(FileSpec(path));258}259260bool SBLaunchInfo::GetShellExpandArguments() {261LLDB_INSTRUMENT_VA(this);262263return m_opaque_sp->GetShellExpandArguments();264}265266void SBLaunchInfo::SetShellExpandArguments(bool expand) {267LLDB_INSTRUMENT_VA(this, expand);268269m_opaque_sp->SetShellExpandArguments(expand);270}271272uint32_t SBLaunchInfo::GetResumeCount() {273LLDB_INSTRUMENT_VA(this);274275return m_opaque_sp->GetResumeCount();276}277278void SBLaunchInfo::SetResumeCount(uint32_t c) {279LLDB_INSTRUMENT_VA(this, c);280281m_opaque_sp->SetResumeCount(c);282}283284bool SBLaunchInfo::AddCloseFileAction(int fd) {285LLDB_INSTRUMENT_VA(this, fd);286287return m_opaque_sp->AppendCloseFileAction(fd);288}289290bool SBLaunchInfo::AddDuplicateFileAction(int fd, int dup_fd) {291LLDB_INSTRUMENT_VA(this, fd, dup_fd);292293return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);294}295296bool SBLaunchInfo::AddOpenFileAction(int fd, const char *path, bool read,297bool write) {298LLDB_INSTRUMENT_VA(this, fd, path, read, write);299300return m_opaque_sp->AppendOpenFileAction(fd, FileSpec(path), read, write);301}302303bool SBLaunchInfo::AddSuppressFileAction(int fd, bool read, bool write) {304LLDB_INSTRUMENT_VA(this, fd, read, write);305306return m_opaque_sp->AppendSuppressFileAction(fd, read, write);307}308309void SBLaunchInfo::SetLaunchEventData(const char *data) {310LLDB_INSTRUMENT_VA(this, data);311312m_opaque_sp->SetLaunchEventData(data);313}314315const char *SBLaunchInfo::GetLaunchEventData() const {316LLDB_INSTRUMENT_VA(this);317318return ConstString(m_opaque_sp->GetLaunchEventData()).GetCString();319}320321void SBLaunchInfo::SetDetachOnError(bool enable) {322LLDB_INSTRUMENT_VA(this, enable);323324m_opaque_sp->SetDetachOnError(enable);325}326327bool SBLaunchInfo::GetDetachOnError() const {328LLDB_INSTRUMENT_VA(this);329330return m_opaque_sp->GetDetachOnError();331}332333const char *SBLaunchInfo::GetScriptedProcessClassName() const {334LLDB_INSTRUMENT_VA(this);335336ScriptedMetadataSP metadata_sp = m_opaque_sp->GetScriptedMetadata();337338if (!metadata_sp || !*metadata_sp)339return nullptr;340341// Constify this string so that it is saved in the string pool. Otherwise it342// would be freed when this function goes out of scope.343ConstString class_name(metadata_sp->GetClassName().data());344return class_name.AsCString();345}346347void SBLaunchInfo::SetScriptedProcessClassName(const char *class_name) {348LLDB_INSTRUMENT_VA(this, class_name);349ScriptedMetadataSP metadata_sp = m_opaque_sp->GetScriptedMetadata();350StructuredData::DictionarySP dict_sp =351metadata_sp ? metadata_sp->GetArgsSP() : nullptr;352metadata_sp = std::make_shared<ScriptedMetadata>(class_name, dict_sp);353m_opaque_sp->SetScriptedMetadata(metadata_sp);354}355356lldb::SBStructuredData SBLaunchInfo::GetScriptedProcessDictionary() const {357LLDB_INSTRUMENT_VA(this);358359ScriptedMetadataSP metadata_sp = m_opaque_sp->GetScriptedMetadata();360361SBStructuredData data;362if (!metadata_sp)363return data;364365lldb_private::StructuredData::DictionarySP dict_sp = metadata_sp->GetArgsSP();366data.m_impl_up->SetObjectSP(dict_sp);367368return data;369}370371void SBLaunchInfo::SetScriptedProcessDictionary(lldb::SBStructuredData dict) {372LLDB_INSTRUMENT_VA(this, dict);373if (!dict.IsValid() || !dict.m_impl_up)374return;375376StructuredData::ObjectSP obj_sp = dict.m_impl_up->GetObjectSP();377378if (!obj_sp)379return;380381StructuredData::DictionarySP dict_sp =382std::make_shared<StructuredData::Dictionary>(obj_sp);383if (!dict_sp || dict_sp->GetType() == lldb::eStructuredDataTypeInvalid)384return;385386ScriptedMetadataSP metadata_sp = m_opaque_sp->GetScriptedMetadata();387llvm::StringRef class_name = metadata_sp ? metadata_sp->GetClassName() : "";388metadata_sp = std::make_shared<ScriptedMetadata>(class_name, dict_sp);389m_opaque_sp->SetScriptedMetadata(metadata_sp);390}391392SBListener SBLaunchInfo::GetShadowListener() {393LLDB_INSTRUMENT_VA(this);394395lldb::ListenerSP shadow_sp = m_opaque_sp->GetShadowListener();396if (!shadow_sp)397return SBListener();398return SBListener(shadow_sp);399}400401void SBLaunchInfo::SetShadowListener(SBListener &listener) {402LLDB_INSTRUMENT_VA(this, listener);403404m_opaque_sp->SetShadowListener(listener.GetSP());405}406407408