xref: /freebsd/contrib/llvm-project/lldb/source/API/SBLaunchInfo.cpp (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 //===-- SBLaunchInfo.cpp --------------------------------------------------===//
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 #include "SBReproducerPrivate.h"
11 
12 #include "lldb/API/SBEnvironment.h"
13 #include "lldb/API/SBFileSpec.h"
14 #include "lldb/API/SBListener.h"
15 #include "lldb/Host/ProcessLaunchInfo.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 class lldb_private::SBLaunchInfoImpl : public ProcessLaunchInfo {
21 public:
22   SBLaunchInfoImpl()
23       : ProcessLaunchInfo(), m_envp(GetEnvironment().getEnvp()) {}
24 
25   const char *const *GetEnvp() const { return m_envp; }
26   void RegenerateEnvp() { m_envp = GetEnvironment().getEnvp(); }
27 
28   SBLaunchInfoImpl &operator=(const ProcessLaunchInfo &rhs) {
29     ProcessLaunchInfo::operator=(rhs);
30     RegenerateEnvp();
31     return *this;
32   }
33 
34 private:
35   Environment::Envp m_envp;
36 };
37 
38 SBLaunchInfo::SBLaunchInfo(const char **argv)
39     : m_opaque_sp(new SBLaunchInfoImpl()) {
40   LLDB_RECORD_CONSTRUCTOR(SBLaunchInfo, (const char **), argv);
41 
42   m_opaque_sp->GetFlags().Reset(eLaunchFlagDebug | eLaunchFlagDisableASLR);
43   if (argv && argv[0])
44     m_opaque_sp->GetArguments().SetArguments(argv);
45 }
46 
47 SBLaunchInfo::SBLaunchInfo(const SBLaunchInfo &rhs) {
48   LLDB_RECORD_CONSTRUCTOR(SBLaunchInfo, (const lldb::SBLaunchInfo &), rhs);
49 
50   m_opaque_sp = rhs.m_opaque_sp;
51 }
52 
53 SBLaunchInfo &SBLaunchInfo::operator=(const SBLaunchInfo &rhs) {
54   LLDB_RECORD_METHOD(SBLaunchInfo &,
55                      SBLaunchInfo, operator=,(const lldb::SBLaunchInfo &), rhs);
56 
57   m_opaque_sp = rhs.m_opaque_sp;
58   return LLDB_RECORD_RESULT(*this);
59 }
60 
61 SBLaunchInfo::~SBLaunchInfo() = default;
62 
63 const lldb_private::ProcessLaunchInfo &SBLaunchInfo::ref() const {
64   return *m_opaque_sp;
65 }
66 
67 void SBLaunchInfo::set_ref(const ProcessLaunchInfo &info) {
68   *m_opaque_sp = info;
69 }
70 
71 lldb::pid_t SBLaunchInfo::GetProcessID() {
72   LLDB_RECORD_METHOD_NO_ARGS(lldb::pid_t, SBLaunchInfo, GetProcessID);
73 
74   return m_opaque_sp->GetProcessID();
75 }
76 
77 uint32_t SBLaunchInfo::GetUserID() {
78   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBLaunchInfo, GetUserID);
79 
80   return m_opaque_sp->GetUserID();
81 }
82 
83 uint32_t SBLaunchInfo::GetGroupID() {
84   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBLaunchInfo, GetGroupID);
85 
86   return m_opaque_sp->GetGroupID();
87 }
88 
89 bool SBLaunchInfo::UserIDIsValid() {
90   LLDB_RECORD_METHOD_NO_ARGS(bool, SBLaunchInfo, UserIDIsValid);
91 
92   return m_opaque_sp->UserIDIsValid();
93 }
94 
95 bool SBLaunchInfo::GroupIDIsValid() {
96   LLDB_RECORD_METHOD_NO_ARGS(bool, SBLaunchInfo, GroupIDIsValid);
97 
98   return m_opaque_sp->GroupIDIsValid();
99 }
100 
101 void SBLaunchInfo::SetUserID(uint32_t uid) {
102   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetUserID, (uint32_t), uid);
103 
104   m_opaque_sp->SetUserID(uid);
105 }
106 
107 void SBLaunchInfo::SetGroupID(uint32_t gid) {
108   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetGroupID, (uint32_t), gid);
109 
110   m_opaque_sp->SetGroupID(gid);
111 }
112 
113 SBFileSpec SBLaunchInfo::GetExecutableFile() {
114   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFileSpec, SBLaunchInfo, GetExecutableFile);
115 
116   return LLDB_RECORD_RESULT(SBFileSpec(m_opaque_sp->GetExecutableFile()));
117 }
118 
119 void SBLaunchInfo::SetExecutableFile(SBFileSpec exe_file,
120                                      bool add_as_first_arg) {
121   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetExecutableFile,
122                      (lldb::SBFileSpec, bool), exe_file, add_as_first_arg);
123 
124   m_opaque_sp->SetExecutableFile(exe_file.ref(), add_as_first_arg);
125 }
126 
127 SBListener SBLaunchInfo::GetListener() {
128   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBListener, SBLaunchInfo, GetListener);
129 
130   return LLDB_RECORD_RESULT(SBListener(m_opaque_sp->GetListener()));
131 }
132 
133 void SBLaunchInfo::SetListener(SBListener &listener) {
134   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetListener, (lldb::SBListener &),
135                      listener);
136 
137   m_opaque_sp->SetListener(listener.GetSP());
138 }
139 
140 uint32_t SBLaunchInfo::GetNumArguments() {
141   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBLaunchInfo, GetNumArguments);
142 
143   return m_opaque_sp->GetArguments().GetArgumentCount();
144 }
145 
146 const char *SBLaunchInfo::GetArgumentAtIndex(uint32_t idx) {
147   LLDB_RECORD_METHOD(const char *, SBLaunchInfo, GetArgumentAtIndex, (uint32_t),
148                      idx);
149 
150   return m_opaque_sp->GetArguments().GetArgumentAtIndex(idx);
151 }
152 
153 void SBLaunchInfo::SetArguments(const char **argv, bool append) {
154   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetArguments, (const char **, bool),
155                      argv, append);
156 
157   if (append) {
158     if (argv)
159       m_opaque_sp->GetArguments().AppendArguments(argv);
160   } else {
161     if (argv)
162       m_opaque_sp->GetArguments().SetArguments(argv);
163     else
164       m_opaque_sp->GetArguments().Clear();
165   }
166 }
167 
168 uint32_t SBLaunchInfo::GetNumEnvironmentEntries() {
169   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBLaunchInfo, GetNumEnvironmentEntries);
170 
171   return m_opaque_sp->GetEnvironment().size();
172 }
173 
174 const char *SBLaunchInfo::GetEnvironmentEntryAtIndex(uint32_t idx) {
175   LLDB_RECORD_METHOD(const char *, SBLaunchInfo, GetEnvironmentEntryAtIndex,
176                      (uint32_t), idx);
177 
178   if (idx > GetNumEnvironmentEntries())
179     return nullptr;
180   return m_opaque_sp->GetEnvp()[idx];
181 }
182 
183 void SBLaunchInfo::SetEnvironmentEntries(const char **envp, bool append) {
184   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetEnvironmentEntries,
185                      (const char **, bool), envp, append);
186   SetEnvironment(SBEnvironment(Environment(envp)), append);
187 }
188 
189 void SBLaunchInfo::SetEnvironment(const SBEnvironment &env, bool append) {
190   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetEnvironment,
191                      (const lldb::SBEnvironment &, bool), env, append);
192   Environment &refEnv = env.ref();
193   if (append)
194     m_opaque_sp->GetEnvironment().insert(refEnv.begin(), refEnv.end());
195   else
196     m_opaque_sp->GetEnvironment() = refEnv;
197   m_opaque_sp->RegenerateEnvp();
198 }
199 
200 SBEnvironment SBLaunchInfo::GetEnvironment() {
201   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBLaunchInfo, GetEnvironment);
202   return LLDB_RECORD_RESULT(
203       SBEnvironment(Environment(m_opaque_sp->GetEnvironment())));
204 }
205 
206 void SBLaunchInfo::Clear() {
207   LLDB_RECORD_METHOD_NO_ARGS(void, SBLaunchInfo, Clear);
208 
209   m_opaque_sp->Clear();
210 }
211 
212 const char *SBLaunchInfo::GetWorkingDirectory() const {
213   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBLaunchInfo,
214                                    GetWorkingDirectory);
215 
216   return m_opaque_sp->GetWorkingDirectory().GetCString();
217 }
218 
219 void SBLaunchInfo::SetWorkingDirectory(const char *working_dir) {
220   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetWorkingDirectory, (const char *),
221                      working_dir);
222 
223   m_opaque_sp->SetWorkingDirectory(FileSpec(working_dir));
224 }
225 
226 uint32_t SBLaunchInfo::GetLaunchFlags() {
227   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBLaunchInfo, GetLaunchFlags);
228 
229   return m_opaque_sp->GetFlags().Get();
230 }
231 
232 void SBLaunchInfo::SetLaunchFlags(uint32_t flags) {
233   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetLaunchFlags, (uint32_t), flags);
234 
235   m_opaque_sp->GetFlags().Reset(flags);
236 }
237 
238 const char *SBLaunchInfo::GetProcessPluginName() {
239   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBLaunchInfo, GetProcessPluginName);
240 
241   return m_opaque_sp->GetProcessPluginName();
242 }
243 
244 void SBLaunchInfo::SetProcessPluginName(const char *plugin_name) {
245   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetProcessPluginName, (const char *),
246                      plugin_name);
247 
248   return m_opaque_sp->SetProcessPluginName(plugin_name);
249 }
250 
251 const char *SBLaunchInfo::GetShell() {
252   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBLaunchInfo, GetShell);
253 
254   // Constify this string so that it is saved in the string pool.  Otherwise it
255   // would be freed when this function goes out of scope.
256   ConstString shell(m_opaque_sp->GetShell().GetPath().c_str());
257   return shell.AsCString();
258 }
259 
260 void SBLaunchInfo::SetShell(const char *path) {
261   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetShell, (const char *), path);
262 
263   m_opaque_sp->SetShell(FileSpec(path));
264 }
265 
266 bool SBLaunchInfo::GetShellExpandArguments() {
267   LLDB_RECORD_METHOD_NO_ARGS(bool, SBLaunchInfo, GetShellExpandArguments);
268 
269   return m_opaque_sp->GetShellExpandArguments();
270 }
271 
272 void SBLaunchInfo::SetShellExpandArguments(bool expand) {
273   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetShellExpandArguments, (bool),
274                      expand);
275 
276   m_opaque_sp->SetShellExpandArguments(expand);
277 }
278 
279 uint32_t SBLaunchInfo::GetResumeCount() {
280   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBLaunchInfo, GetResumeCount);
281 
282   return m_opaque_sp->GetResumeCount();
283 }
284 
285 void SBLaunchInfo::SetResumeCount(uint32_t c) {
286   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetResumeCount, (uint32_t), c);
287 
288   m_opaque_sp->SetResumeCount(c);
289 }
290 
291 bool SBLaunchInfo::AddCloseFileAction(int fd) {
292   LLDB_RECORD_METHOD(bool, SBLaunchInfo, AddCloseFileAction, (int), fd);
293 
294   return m_opaque_sp->AppendCloseFileAction(fd);
295 }
296 
297 bool SBLaunchInfo::AddDuplicateFileAction(int fd, int dup_fd) {
298   LLDB_RECORD_METHOD(bool, SBLaunchInfo, AddDuplicateFileAction, (int, int), fd,
299                      dup_fd);
300 
301   return m_opaque_sp->AppendDuplicateFileAction(fd, dup_fd);
302 }
303 
304 bool SBLaunchInfo::AddOpenFileAction(int fd, const char *path, bool read,
305                                      bool write) {
306   LLDB_RECORD_METHOD(bool, SBLaunchInfo, AddOpenFileAction,
307                      (int, const char *, bool, bool), fd, path, read, write);
308 
309   return m_opaque_sp->AppendOpenFileAction(fd, FileSpec(path), read, write);
310 }
311 
312 bool SBLaunchInfo::AddSuppressFileAction(int fd, bool read, bool write) {
313   LLDB_RECORD_METHOD(bool, SBLaunchInfo, AddSuppressFileAction,
314                      (int, bool, bool), fd, read, write);
315 
316   return m_opaque_sp->AppendSuppressFileAction(fd, read, write);
317 }
318 
319 void SBLaunchInfo::SetLaunchEventData(const char *data) {
320   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetLaunchEventData, (const char *),
321                      data);
322 
323   m_opaque_sp->SetLaunchEventData(data);
324 }
325 
326 const char *SBLaunchInfo::GetLaunchEventData() const {
327   LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBLaunchInfo,
328                                    GetLaunchEventData);
329 
330   return m_opaque_sp->GetLaunchEventData();
331 }
332 
333 void SBLaunchInfo::SetDetachOnError(bool enable) {
334   LLDB_RECORD_METHOD(void, SBLaunchInfo, SetDetachOnError, (bool), enable);
335 
336   m_opaque_sp->SetDetachOnError(enable);
337 }
338 
339 bool SBLaunchInfo::GetDetachOnError() const {
340   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBLaunchInfo, GetDetachOnError);
341 
342   return m_opaque_sp->GetDetachOnError();
343 }
344 
345 namespace lldb_private {
346 namespace repro {
347 
348 template <>
349 void RegisterMethods<SBLaunchInfo>(Registry &R) {
350   LLDB_REGISTER_CONSTRUCTOR(SBLaunchInfo, (const char **));
351   LLDB_REGISTER_CONSTRUCTOR(SBLaunchInfo, (const lldb::SBLaunchInfo &));
352   LLDB_REGISTER_METHOD(SBLaunchInfo &,
353                        SBLaunchInfo, operator=,(const lldb::SBLaunchInfo &));
354   LLDB_REGISTER_METHOD(lldb::pid_t, SBLaunchInfo, GetProcessID, ());
355   LLDB_REGISTER_METHOD(uint32_t, SBLaunchInfo, GetUserID, ());
356   LLDB_REGISTER_METHOD(uint32_t, SBLaunchInfo, GetGroupID, ());
357   LLDB_REGISTER_METHOD(bool, SBLaunchInfo, UserIDIsValid, ());
358   LLDB_REGISTER_METHOD(bool, SBLaunchInfo, GroupIDIsValid, ());
359   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetUserID, (uint32_t));
360   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetGroupID, (uint32_t));
361   LLDB_REGISTER_METHOD(lldb::SBFileSpec, SBLaunchInfo, GetExecutableFile, ());
362   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetExecutableFile,
363                        (lldb::SBFileSpec, bool));
364   LLDB_REGISTER_METHOD(lldb::SBListener, SBLaunchInfo, GetListener, ());
365   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetListener, (lldb::SBListener &));
366   LLDB_REGISTER_METHOD(uint32_t, SBLaunchInfo, GetNumArguments, ());
367   LLDB_REGISTER_METHOD(const char *, SBLaunchInfo, GetArgumentAtIndex,
368                        (uint32_t));
369   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetArguments,
370                        (const char **, bool));
371   LLDB_REGISTER_METHOD(uint32_t, SBLaunchInfo, GetNumEnvironmentEntries, ());
372   LLDB_REGISTER_METHOD(const char *, SBLaunchInfo, GetEnvironmentEntryAtIndex,
373                        (uint32_t));
374   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetEnvironmentEntries,
375                        (const char **, bool));
376   LLDB_REGISTER_METHOD(void, SBLaunchInfo, Clear, ());
377   LLDB_REGISTER_METHOD_CONST(const char *, SBLaunchInfo, GetWorkingDirectory,
378                              ());
379   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetWorkingDirectory,
380                        (const char *));
381   LLDB_REGISTER_METHOD(uint32_t, SBLaunchInfo, GetLaunchFlags, ());
382   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetLaunchFlags, (uint32_t));
383   LLDB_REGISTER_METHOD(const char *, SBLaunchInfo, GetProcessPluginName, ());
384   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetProcessPluginName,
385                        (const char *));
386   LLDB_REGISTER_METHOD(const char *, SBLaunchInfo, GetShell, ());
387   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetShell, (const char *));
388   LLDB_REGISTER_METHOD(bool, SBLaunchInfo, GetShellExpandArguments, ());
389   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetShellExpandArguments, (bool));
390   LLDB_REGISTER_METHOD(uint32_t, SBLaunchInfo, GetResumeCount, ());
391   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetResumeCount, (uint32_t));
392   LLDB_REGISTER_METHOD(bool, SBLaunchInfo, AddCloseFileAction, (int));
393   LLDB_REGISTER_METHOD(bool, SBLaunchInfo, AddDuplicateFileAction,
394                        (int, int));
395   LLDB_REGISTER_METHOD(bool, SBLaunchInfo, AddOpenFileAction,
396                        (int, const char *, bool, bool));
397   LLDB_REGISTER_METHOD(bool, SBLaunchInfo, AddSuppressFileAction,
398                        (int, bool, bool));
399   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetLaunchEventData,
400                        (const char *));
401   LLDB_REGISTER_METHOD_CONST(const char *, SBLaunchInfo, GetLaunchEventData,
402                              ());
403   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetDetachOnError, (bool));
404   LLDB_REGISTER_METHOD_CONST(bool, SBLaunchInfo, GetDetachOnError, ());
405   LLDB_REGISTER_METHOD(void, SBLaunchInfo, SetEnvironment,
406                        (const lldb::SBEnvironment &, bool));
407   LLDB_REGISTER_METHOD(lldb::SBEnvironment, SBLaunchInfo, GetEnvironment, ());
408 }
409 
410 }
411 }
412