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