1 //===-- SBLaunchInfo.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBLaunchInfo.h"
10 #include "lldb/Utility/Instrumentation.h"
12 #include "lldb/API/SBEnvironment.h"
13 #include "lldb/API/SBError.h"
14 #include "lldb/API/SBFileSpec.h"
15 #include "lldb/API/SBListener.h"
16 #include "lldb/API/SBStream.h"
17 #include "lldb/API/SBStructuredData.h"
18 #include "lldb/Core/StructuredDataImpl.h"
19 #include "lldb/Host/ProcessLaunchInfo.h"
20 #include "lldb/Utility/Listener.h"
21 #include "lldb/Utility/ScriptedMetadata.h"
24 using namespace lldb_private
;
26 class lldb_private::SBLaunchInfoImpl
: public ProcessLaunchInfo
{
28 SBLaunchInfoImpl() : m_envp(GetEnvironment().getEnvp()) {}
30 const char *const *GetEnvp() const { return m_envp
; }
31 void RegenerateEnvp() { m_envp
= GetEnvironment().getEnvp(); }
33 SBLaunchInfoImpl
&operator=(const ProcessLaunchInfo
&rhs
) {
34 ProcessLaunchInfo::operator=(rhs
);
40 Environment::Envp m_envp
;
43 SBLaunchInfo::SBLaunchInfo(const char **argv
)
44 : m_opaque_sp(new SBLaunchInfoImpl()) {
45 LLDB_INSTRUMENT_VA(this, argv
);
47 m_opaque_sp
->GetFlags().Reset(eLaunchFlagDebug
| eLaunchFlagDisableASLR
);
49 m_opaque_sp
->GetArguments().SetArguments(argv
);
52 SBLaunchInfo::SBLaunchInfo(const SBLaunchInfo
&rhs
) {
53 LLDB_INSTRUMENT_VA(this, rhs
);
55 m_opaque_sp
= rhs
.m_opaque_sp
;
58 SBLaunchInfo
&SBLaunchInfo::operator=(const SBLaunchInfo
&rhs
) {
59 LLDB_INSTRUMENT_VA(this, rhs
);
61 m_opaque_sp
= rhs
.m_opaque_sp
;
65 SBLaunchInfo::~SBLaunchInfo() = default;
67 const lldb_private::ProcessLaunchInfo
&SBLaunchInfo::ref() const {
71 void SBLaunchInfo::set_ref(const ProcessLaunchInfo
&info
) {
75 lldb::pid_t
SBLaunchInfo::GetProcessID() {
76 LLDB_INSTRUMENT_VA(this);
78 return m_opaque_sp
->GetProcessID();
81 uint32_t SBLaunchInfo::GetUserID() {
82 LLDB_INSTRUMENT_VA(this);
84 return m_opaque_sp
->GetUserID();
87 uint32_t SBLaunchInfo::GetGroupID() {
88 LLDB_INSTRUMENT_VA(this);
90 return m_opaque_sp
->GetGroupID();
93 bool SBLaunchInfo::UserIDIsValid() {
94 LLDB_INSTRUMENT_VA(this);
96 return m_opaque_sp
->UserIDIsValid();
99 bool SBLaunchInfo::GroupIDIsValid() {
100 LLDB_INSTRUMENT_VA(this);
102 return m_opaque_sp
->GroupIDIsValid();
105 void SBLaunchInfo::SetUserID(uint32_t uid
) {
106 LLDB_INSTRUMENT_VA(this, uid
);
108 m_opaque_sp
->SetUserID(uid
);
111 void SBLaunchInfo::SetGroupID(uint32_t gid
) {
112 LLDB_INSTRUMENT_VA(this, gid
);
114 m_opaque_sp
->SetGroupID(gid
);
117 SBFileSpec
SBLaunchInfo::GetExecutableFile() {
118 LLDB_INSTRUMENT_VA(this);
120 return SBFileSpec(m_opaque_sp
->GetExecutableFile());
123 void SBLaunchInfo::SetExecutableFile(SBFileSpec exe_file
,
124 bool add_as_first_arg
) {
125 LLDB_INSTRUMENT_VA(this, exe_file
, add_as_first_arg
);
127 m_opaque_sp
->SetExecutableFile(exe_file
.ref(), add_as_first_arg
);
130 SBListener
SBLaunchInfo::GetListener() {
131 LLDB_INSTRUMENT_VA(this);
133 return SBListener(m_opaque_sp
->GetListener());
136 void SBLaunchInfo::SetListener(SBListener
&listener
) {
137 LLDB_INSTRUMENT_VA(this, listener
);
139 m_opaque_sp
->SetListener(listener
.GetSP());
142 uint32_t SBLaunchInfo::GetNumArguments() {
143 LLDB_INSTRUMENT_VA(this);
145 return m_opaque_sp
->GetArguments().GetArgumentCount();
148 const char *SBLaunchInfo::GetArgumentAtIndex(uint32_t idx
) {
149 LLDB_INSTRUMENT_VA(this, idx
);
151 return ConstString(m_opaque_sp
->GetArguments().GetArgumentAtIndex(idx
))
155 void SBLaunchInfo::SetArguments(const char **argv
, bool append
) {
156 LLDB_INSTRUMENT_VA(this, argv
, append
);
160 m_opaque_sp
->GetArguments().AppendArguments(argv
);
163 m_opaque_sp
->GetArguments().SetArguments(argv
);
165 m_opaque_sp
->GetArguments().Clear();
169 uint32_t SBLaunchInfo::GetNumEnvironmentEntries() {
170 LLDB_INSTRUMENT_VA(this);
172 return m_opaque_sp
->GetEnvironment().size();
175 const char *SBLaunchInfo::GetEnvironmentEntryAtIndex(uint32_t idx
) {
176 LLDB_INSTRUMENT_VA(this, idx
);
178 if (idx
> GetNumEnvironmentEntries())
180 return ConstString(m_opaque_sp
->GetEnvp()[idx
]).GetCString();
183 void SBLaunchInfo::SetEnvironmentEntries(const char **envp
, bool append
) {
184 LLDB_INSTRUMENT_VA(this, envp
, append
);
185 SetEnvironment(SBEnvironment(Environment(envp
)), append
);
188 void SBLaunchInfo::SetEnvironment(const SBEnvironment
&env
, bool append
) {
189 LLDB_INSTRUMENT_VA(this, env
, append
);
190 Environment
&refEnv
= env
.ref();
192 for (auto &KV
: refEnv
)
193 m_opaque_sp
->GetEnvironment().insert_or_assign(KV
.first(), KV
.second
);
195 m_opaque_sp
->GetEnvironment() = refEnv
;
196 m_opaque_sp
->RegenerateEnvp();
199 SBEnvironment
SBLaunchInfo::GetEnvironment() {
200 LLDB_INSTRUMENT_VA(this);
201 return SBEnvironment(Environment(m_opaque_sp
->GetEnvironment()));
204 void SBLaunchInfo::Clear() {
205 LLDB_INSTRUMENT_VA(this);
207 m_opaque_sp
->Clear();
210 const char *SBLaunchInfo::GetWorkingDirectory() const {
211 LLDB_INSTRUMENT_VA(this);
213 return m_opaque_sp
->GetWorkingDirectory().GetPathAsConstString().AsCString();
216 void SBLaunchInfo::SetWorkingDirectory(const char *working_dir
) {
217 LLDB_INSTRUMENT_VA(this, working_dir
);
219 m_opaque_sp
->SetWorkingDirectory(FileSpec(working_dir
));
222 uint32_t SBLaunchInfo::GetLaunchFlags() {
223 LLDB_INSTRUMENT_VA(this);
225 return m_opaque_sp
->GetFlags().Get();
228 void SBLaunchInfo::SetLaunchFlags(uint32_t flags
) {
229 LLDB_INSTRUMENT_VA(this, flags
);
231 m_opaque_sp
->GetFlags().Reset(flags
);
234 const char *SBLaunchInfo::GetProcessPluginName() {
235 LLDB_INSTRUMENT_VA(this);
237 return ConstString(m_opaque_sp
->GetProcessPluginName()).GetCString();
240 void SBLaunchInfo::SetProcessPluginName(const char *plugin_name
) {
241 LLDB_INSTRUMENT_VA(this, plugin_name
);
243 return m_opaque_sp
->SetProcessPluginName(plugin_name
);
246 const char *SBLaunchInfo::GetShell() {
247 LLDB_INSTRUMENT_VA(this);
249 // Constify this string so that it is saved in the string pool. Otherwise it
250 // would be freed when this function goes out of scope.
251 ConstString
shell(m_opaque_sp
->GetShell().GetPath().c_str());
252 return shell
.AsCString();
255 void SBLaunchInfo::SetShell(const char *path
) {
256 LLDB_INSTRUMENT_VA(this, path
);
258 m_opaque_sp
->SetShell(FileSpec(path
));
261 bool SBLaunchInfo::GetShellExpandArguments() {
262 LLDB_INSTRUMENT_VA(this);
264 return m_opaque_sp
->GetShellExpandArguments();
267 void SBLaunchInfo::SetShellExpandArguments(bool expand
) {
268 LLDB_INSTRUMENT_VA(this, expand
);
270 m_opaque_sp
->SetShellExpandArguments(expand
);
273 uint32_t SBLaunchInfo::GetResumeCount() {
274 LLDB_INSTRUMENT_VA(this);
276 return m_opaque_sp
->GetResumeCount();
279 void SBLaunchInfo::SetResumeCount(uint32_t c
) {
280 LLDB_INSTRUMENT_VA(this, c
);
282 m_opaque_sp
->SetResumeCount(c
);
285 bool SBLaunchInfo::AddCloseFileAction(int fd
) {
286 LLDB_INSTRUMENT_VA(this, fd
);
288 return m_opaque_sp
->AppendCloseFileAction(fd
);
291 bool SBLaunchInfo::AddDuplicateFileAction(int fd
, int dup_fd
) {
292 LLDB_INSTRUMENT_VA(this, fd
, dup_fd
);
294 return m_opaque_sp
->AppendDuplicateFileAction(fd
, dup_fd
);
297 bool SBLaunchInfo::AddOpenFileAction(int fd
, const char *path
, bool read
,
299 LLDB_INSTRUMENT_VA(this, fd
, path
, read
, write
);
301 return m_opaque_sp
->AppendOpenFileAction(fd
, FileSpec(path
), read
, write
);
304 bool SBLaunchInfo::AddSuppressFileAction(int fd
, bool read
, bool write
) {
305 LLDB_INSTRUMENT_VA(this, fd
, read
, write
);
307 return m_opaque_sp
->AppendSuppressFileAction(fd
, read
, write
);
310 void SBLaunchInfo::SetLaunchEventData(const char *data
) {
311 LLDB_INSTRUMENT_VA(this, data
);
313 m_opaque_sp
->SetLaunchEventData(data
);
316 const char *SBLaunchInfo::GetLaunchEventData() const {
317 LLDB_INSTRUMENT_VA(this);
319 return ConstString(m_opaque_sp
->GetLaunchEventData()).GetCString();
322 void SBLaunchInfo::SetDetachOnError(bool enable
) {
323 LLDB_INSTRUMENT_VA(this, enable
);
325 m_opaque_sp
->SetDetachOnError(enable
);
328 bool SBLaunchInfo::GetDetachOnError() const {
329 LLDB_INSTRUMENT_VA(this);
331 return m_opaque_sp
->GetDetachOnError();
334 const char *SBLaunchInfo::GetScriptedProcessClassName() const {
335 LLDB_INSTRUMENT_VA(this);
337 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
339 if (!metadata_sp
|| !*metadata_sp
)
342 // Constify this string so that it is saved in the string pool. Otherwise it
343 // would be freed when this function goes out of scope.
344 ConstString
class_name(metadata_sp
->GetClassName().data());
345 return class_name
.AsCString();
348 void SBLaunchInfo::SetScriptedProcessClassName(const char *class_name
) {
349 LLDB_INSTRUMENT_VA(this, class_name
);
350 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
351 StructuredData::DictionarySP dict_sp
=
352 metadata_sp
? metadata_sp
->GetArgsSP() : nullptr;
353 metadata_sp
= std::make_shared
<ScriptedMetadata
>(class_name
, dict_sp
);
354 m_opaque_sp
->SetScriptedMetadata(metadata_sp
);
357 lldb::SBStructuredData
SBLaunchInfo::GetScriptedProcessDictionary() const {
358 LLDB_INSTRUMENT_VA(this);
360 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
362 SBStructuredData data
;
366 lldb_private::StructuredData::DictionarySP dict_sp
= metadata_sp
->GetArgsSP();
367 data
.m_impl_up
->SetObjectSP(dict_sp
);
372 void SBLaunchInfo::SetScriptedProcessDictionary(lldb::SBStructuredData dict
) {
373 LLDB_INSTRUMENT_VA(this, dict
);
374 if (!dict
.IsValid() || !dict
.m_impl_up
)
377 StructuredData::ObjectSP obj_sp
= dict
.m_impl_up
->GetObjectSP();
382 StructuredData::DictionarySP dict_sp
=
383 std::make_shared
<StructuredData::Dictionary
>(obj_sp
);
384 if (!dict_sp
|| dict_sp
->GetType() == lldb::eStructuredDataTypeInvalid
)
387 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
388 llvm::StringRef class_name
= metadata_sp
? metadata_sp
->GetClassName() : "";
389 metadata_sp
= std::make_shared
<ScriptedMetadata
>(class_name
, dict_sp
);
390 m_opaque_sp
->SetScriptedMetadata(metadata_sp
);
393 SBListener
SBLaunchInfo::GetShadowListener() {
394 LLDB_INSTRUMENT_VA(this);
396 lldb::ListenerSP shadow_sp
= m_opaque_sp
->GetShadowListener();
399 return SBListener(shadow_sp
);
402 void SBLaunchInfo::SetShadowListener(SBListener
&listener
) {
403 LLDB_INSTRUMENT_VA(this, listener
);
405 m_opaque_sp
->SetShadowListener(listener
.GetSP());