1 //===-- SBLaunchInfo.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBLaunchInfo.h"
10 
11 #include "lldb/API/SBFileSpec.h"
12 #include "lldb/API/SBListener.h"
13 #include "lldb/Host/ProcessLaunchInfo.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 class lldb_private::SBLaunchInfoImpl : public ProcessLaunchInfo {
19 public:
20   SBLaunchInfoImpl()
21       : ProcessLaunchInfo(), m_envp(GetEnvironment().getEnvp()) {}
22 
23   const char *const *GetEnvp() const { return m_envp; }
24   void RegenerateEnvp() { m_envp = GetEnvironment().getEnvp(); }
25 
26   SBLaunchInfoImpl &operator=(const ProcessLaunchInfo &rhs) {
27     ProcessLaunchInfo::operator=(rhs);
28     RegenerateEnvp();
29     return *this;
30   }
31 
32 private:
33   Environment::Envp m_envp;
34 };
35 
36 SBLaunchInfo::SBLaunchInfo(const char **argv)
37     : m_opaque_sp(new SBLaunchInfoImpl()) {
38   m_opaque_sp->GetFlags().Reset(eLaunchFlagDebug | eLaunchFlagDisableASLR);
39   if (argv && argv[0])
40     m_opaque_sp->GetArguments().SetArguments(argv);
41 }
42 
43 SBLaunchInfo::~SBLaunchInfo() {}
44 
45 const lldb_private::ProcessLaunchInfo &SBLaunchInfo::ref() const {
46   return *m_opaque_sp;
47 }
48 
49 void SBLaunchInfo::set_ref(const ProcessLaunchInfo &info) {
50   *m_opaque_sp = info;
51 }
52 
53 lldb::pid_t SBLaunchInfo::GetProcessID() { return m_opaque_sp->GetProcessID(); }
54 
55 uint32_t SBLaunchInfo::GetUserID() { return m_opaque_sp->GetUserID(); }
56 
57 uint32_t SBLaunchInfo::GetGroupID() { return m_opaque_sp->GetGroupID(); }
58 
59 bool SBLaunchInfo::UserIDIsValid() { return m_opaque_sp->UserIDIsValid(); }
60 
61 bool SBLaunchInfo::GroupIDIsValid() { return m_opaque_sp->GroupIDIsValid(); }
62 
63 void SBLaunchInfo::SetUserID(uint32_t uid) { m_opaque_sp->SetUserID(uid); }
64 
65 void SBLaunchInfo::SetGroupID(uint32_t gid) { m_opaque_sp->SetGroupID(gid); }
66 
67 SBFileSpec SBLaunchInfo::GetExecutableFile() {
68   return SBFileSpec(m_opaque_sp->GetExecutableFile());
69 }
70 
71 void SBLaunchInfo::SetExecutableFile(SBFileSpec exe_file,
72                                      bool add_as_first_arg) {
73   m_opaque_sp->SetExecutableFile(exe_file.ref(), add_as_first_arg);
74 }
75 
76 SBListener SBLaunchInfo::GetListener() {
77   return SBListener(m_opaque_sp->GetListener());
78 }
79 
80 void SBLaunchInfo::SetListener(SBListener &listener) {
81   m_opaque_sp->SetListener(listener.GetSP());
82 }
83 
84 uint32_t SBLaunchInfo::GetNumArguments() {
85   return m_opaque_sp->GetArguments().GetArgumentCount();
86 }
87 
88 const char *SBLaunchInfo::GetArgumentAtIndex(uint32_t idx) {
89   return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
90 }
91 
92 void SBLaunchInfo::SetArguments(const char **argv, bool append) {
93   if (append) {
94     if (argv)
95       m_opaque_sp->GetArguments().AppendArguments(argv);
96   } else {
97     if (argv)
98       m_opaque_sp->GetArguments().SetArguments(argv);
99     else
100       m_opaque_sp->GetArguments().Clear();
101   }
102 }
103 
104 uint32_t SBLaunchInfo::GetNumEnvironmentEntries() {
105   return m_opaque_sp->GetEnvironment().size();
106 }
107 
108 const char *SBLaunchInfo::GetEnvironmentEntryAtIndex(uint32_t idx) {
109   if (idx > GetNumEnvironmentEntries())
110     return nullptr;
111   return m_opaque_sp->GetEnvp()[idx];
112 }
113 
114 void SBLaunchInfo::SetEnvironmentEntries(const char **envp, bool append) {
115   Environment env(envp);
116   if (append)
117     m_opaque_sp->GetEnvironment().insert(env.begin(), env.end());
118   else
119     m_opaque_sp->GetEnvironment() = env;
120   m_opaque_sp->RegenerateEnvp();
121 }
122 
123 void SBLaunchInfo::Clear() { m_opaque_sp->Clear(); }
124 
125 const char *SBLaunchInfo::GetWorkingDirectory() const {
126   return m_opaque_sp->GetWorkingDirectory().GetCString();
127 }
128 
129 void SBLaunchInfo::SetWorkingDirectory(const char *working_dir) {
130   m_opaque_sp->SetWorkingDirectory(FileSpec(working_dir));
131 }
132 
133 uint32_t SBLaunchInfo::GetLaunchFlags() {
134   return m_opaque_sp->GetFlags().Get();
135 }
136 
137 void SBLaunchInfo::SetLaunchFlags(uint32_t flags) {
138   m_opaque_sp->GetFlags().Reset(flags);
139 }
140 
141 const char *SBLaunchInfo::GetProcessPluginName() {
142   return m_opaque_sp->GetProcessPluginName();
143 }
144 
145 void SBLaunchInfo::SetProcessPluginName(const char *plugin_name) {
146   return m_opaque_sp->SetProcessPluginName(plugin_name);
147 }
148 
149 const char *SBLaunchInfo::GetShell() {
150   // Constify this string so that it is saved in the string pool.  Otherwise it
151   // would be freed when this function goes out of scope.
152   ConstString shell(m_opaque_sp->GetShell().GetPath().c_str());
153   return shell.AsCString();
154 }
155 
156 void SBLaunchInfo::SetShell(const char *path) {
157   m_opaque_sp->SetShell(FileSpec(path));
158 }
159 
160 bool SBLaunchInfo::GetShellExpandArguments() {
161   return m_opaque_sp->GetShellExpandArguments();
162 }
163 
164 void SBLaunchInfo::SetShellExpandArguments(bool expand) {
165   m_opaque_sp->SetShellExpandArguments(expand);
166 }
167 
168 uint32_t SBLaunchInfo::GetResumeCount() {
169   return m_opaque_sp->GetResumeCount();
170 }
171 
172 void SBLaunchInfo::SetResumeCount(uint32_t c) {
173   m_opaque_sp->SetResumeCount(c);
174 }
175 
176 bool SBLaunchInfo::AddCloseFileAction(int fd) {
177   return m_opaque_sp->AppendCloseFileAction(fd);
178 }
179 
180 bool SBLaunchInfo::AddDuplicateFileAction(int fd, int dup_fd) {
181   return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
182 }
183 
184 bool SBLaunchInfo::AddOpenFileAction(int fd, const char *path, bool read,
185                                      bool write) {
186   return m_opaque_sp->AppendOpenFileAction(fd, FileSpec(path), read, write);
187 }
188 
189 bool SBLaunchInfo::AddSuppressFileAction(int fd, bool read, bool write) {
190   return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
191 }
192 
193 void SBLaunchInfo::SetLaunchEventData(const char *data) {
194   m_opaque_sp->SetLaunchEventData(data);
195 }
196 
197 const char *SBLaunchInfo::GetLaunchEventData() const {
198   return m_opaque_sp->GetLaunchEventData();
199 }
200 
201 void SBLaunchInfo::SetDetachOnError(bool enable) {
202   m_opaque_sp->SetDetachOnError(enable);
203 }
204 
205 bool SBLaunchInfo::GetDetachOnError() const {
206   return m_opaque_sp->GetDetachOnError();
207 }
208