1 //===-- SBAttachInfo.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/SBAttachInfo.h"
11 #include "lldb/API/SBFileSpec.h"
12 #include "lldb/API/SBListener.h"
13 #include "lldb/API/SBStructuredData.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Utility/Instrumentation.h"
16 #include "lldb/Utility/ScriptedMetadata.h"
19 using namespace lldb_private
;
21 SBAttachInfo::SBAttachInfo() : m_opaque_sp(new ProcessAttachInfo()) {
22 LLDB_INSTRUMENT_VA(this);
25 SBAttachInfo::SBAttachInfo(lldb::pid_t pid
)
26 : m_opaque_sp(new ProcessAttachInfo()) {
27 LLDB_INSTRUMENT_VA(this, pid
);
29 m_opaque_sp
->SetProcessID(pid
);
32 SBAttachInfo::SBAttachInfo(const char *path
, bool wait_for
)
33 : m_opaque_sp(new ProcessAttachInfo()) {
34 LLDB_INSTRUMENT_VA(this, path
, wait_for
);
37 m_opaque_sp
->GetExecutableFile().SetFile(path
, FileSpec::Style::native
);
38 m_opaque_sp
->SetWaitForLaunch(wait_for
);
41 SBAttachInfo::SBAttachInfo(const char *path
, bool wait_for
, bool async
)
42 : m_opaque_sp(new ProcessAttachInfo()) {
43 LLDB_INSTRUMENT_VA(this, path
, wait_for
, async
);
46 m_opaque_sp
->GetExecutableFile().SetFile(path
, FileSpec::Style::native
);
47 m_opaque_sp
->SetWaitForLaunch(wait_for
);
48 m_opaque_sp
->SetAsync(async
);
51 SBAttachInfo::SBAttachInfo(const SBAttachInfo
&rhs
)
52 : m_opaque_sp(new ProcessAttachInfo()) {
53 LLDB_INSTRUMENT_VA(this, rhs
);
55 m_opaque_sp
= clone(rhs
.m_opaque_sp
);
58 SBAttachInfo::~SBAttachInfo() = default;
60 lldb_private::ProcessAttachInfo
&SBAttachInfo::ref() { return *m_opaque_sp
; }
62 SBAttachInfo
&SBAttachInfo::operator=(const SBAttachInfo
&rhs
) {
63 LLDB_INSTRUMENT_VA(this, rhs
);
66 m_opaque_sp
= clone(rhs
.m_opaque_sp
);
70 lldb::pid_t
SBAttachInfo::GetProcessID() {
71 LLDB_INSTRUMENT_VA(this);
73 return m_opaque_sp
->GetProcessID();
76 void SBAttachInfo::SetProcessID(lldb::pid_t pid
) {
77 LLDB_INSTRUMENT_VA(this, pid
);
79 m_opaque_sp
->SetProcessID(pid
);
82 uint32_t SBAttachInfo::GetResumeCount() {
83 LLDB_INSTRUMENT_VA(this);
85 return m_opaque_sp
->GetResumeCount();
88 void SBAttachInfo::SetResumeCount(uint32_t c
) {
89 LLDB_INSTRUMENT_VA(this, c
);
91 m_opaque_sp
->SetResumeCount(c
);
94 const char *SBAttachInfo::GetProcessPluginName() {
95 LLDB_INSTRUMENT_VA(this);
97 return ConstString(m_opaque_sp
->GetProcessPluginName()).GetCString();
100 void SBAttachInfo::SetProcessPluginName(const char *plugin_name
) {
101 LLDB_INSTRUMENT_VA(this, plugin_name
);
103 return m_opaque_sp
->SetProcessPluginName(plugin_name
);
106 void SBAttachInfo::SetExecutable(const char *path
) {
107 LLDB_INSTRUMENT_VA(this, path
);
110 m_opaque_sp
->GetExecutableFile().SetFile(path
, FileSpec::Style::native
);
112 m_opaque_sp
->GetExecutableFile().Clear();
115 void SBAttachInfo::SetExecutable(SBFileSpec exe_file
) {
116 LLDB_INSTRUMENT_VA(this, exe_file
);
118 if (exe_file
.IsValid())
119 m_opaque_sp
->GetExecutableFile() = exe_file
.ref();
121 m_opaque_sp
->GetExecutableFile().Clear();
124 bool SBAttachInfo::GetWaitForLaunch() {
125 LLDB_INSTRUMENT_VA(this);
127 return m_opaque_sp
->GetWaitForLaunch();
130 void SBAttachInfo::SetWaitForLaunch(bool b
) {
131 LLDB_INSTRUMENT_VA(this, b
);
133 m_opaque_sp
->SetWaitForLaunch(b
);
136 void SBAttachInfo::SetWaitForLaunch(bool b
, bool async
) {
137 LLDB_INSTRUMENT_VA(this, b
, async
);
139 m_opaque_sp
->SetWaitForLaunch(b
);
140 m_opaque_sp
->SetAsync(async
);
143 bool SBAttachInfo::GetIgnoreExisting() {
144 LLDB_INSTRUMENT_VA(this);
146 return m_opaque_sp
->GetIgnoreExisting();
149 void SBAttachInfo::SetIgnoreExisting(bool b
) {
150 LLDB_INSTRUMENT_VA(this, b
);
152 m_opaque_sp
->SetIgnoreExisting(b
);
155 uint32_t SBAttachInfo::GetUserID() {
156 LLDB_INSTRUMENT_VA(this);
158 return m_opaque_sp
->GetUserID();
161 uint32_t SBAttachInfo::GetGroupID() {
162 LLDB_INSTRUMENT_VA(this);
164 return m_opaque_sp
->GetGroupID();
167 bool SBAttachInfo::UserIDIsValid() {
168 LLDB_INSTRUMENT_VA(this);
170 return m_opaque_sp
->UserIDIsValid();
173 bool SBAttachInfo::GroupIDIsValid() {
174 LLDB_INSTRUMENT_VA(this);
176 return m_opaque_sp
->GroupIDIsValid();
179 void SBAttachInfo::SetUserID(uint32_t uid
) {
180 LLDB_INSTRUMENT_VA(this, uid
);
182 m_opaque_sp
->SetUserID(uid
);
185 void SBAttachInfo::SetGroupID(uint32_t gid
) {
186 LLDB_INSTRUMENT_VA(this, gid
);
188 m_opaque_sp
->SetGroupID(gid
);
191 uint32_t SBAttachInfo::GetEffectiveUserID() {
192 LLDB_INSTRUMENT_VA(this);
194 return m_opaque_sp
->GetEffectiveUserID();
197 uint32_t SBAttachInfo::GetEffectiveGroupID() {
198 LLDB_INSTRUMENT_VA(this);
200 return m_opaque_sp
->GetEffectiveGroupID();
203 bool SBAttachInfo::EffectiveUserIDIsValid() {
204 LLDB_INSTRUMENT_VA(this);
206 return m_opaque_sp
->EffectiveUserIDIsValid();
209 bool SBAttachInfo::EffectiveGroupIDIsValid() {
210 LLDB_INSTRUMENT_VA(this);
212 return m_opaque_sp
->EffectiveGroupIDIsValid();
215 void SBAttachInfo::SetEffectiveUserID(uint32_t uid
) {
216 LLDB_INSTRUMENT_VA(this, uid
);
218 m_opaque_sp
->SetEffectiveUserID(uid
);
221 void SBAttachInfo::SetEffectiveGroupID(uint32_t gid
) {
222 LLDB_INSTRUMENT_VA(this, gid
);
224 m_opaque_sp
->SetEffectiveGroupID(gid
);
227 lldb::pid_t
SBAttachInfo::GetParentProcessID() {
228 LLDB_INSTRUMENT_VA(this);
230 return m_opaque_sp
->GetParentProcessID();
233 void SBAttachInfo::SetParentProcessID(lldb::pid_t pid
) {
234 LLDB_INSTRUMENT_VA(this, pid
);
236 m_opaque_sp
->SetParentProcessID(pid
);
239 bool SBAttachInfo::ParentProcessIDIsValid() {
240 LLDB_INSTRUMENT_VA(this);
242 return m_opaque_sp
->ParentProcessIDIsValid();
245 SBListener
SBAttachInfo::GetListener() {
246 LLDB_INSTRUMENT_VA(this);
248 return SBListener(m_opaque_sp
->GetListener());
251 void SBAttachInfo::SetListener(SBListener
&listener
) {
252 LLDB_INSTRUMENT_VA(this, listener
);
254 m_opaque_sp
->SetListener(listener
.GetSP());
257 SBListener
SBAttachInfo::GetShadowListener() {
258 LLDB_INSTRUMENT_VA(this);
260 lldb::ListenerSP shadow_sp
= m_opaque_sp
->GetShadowListener();
263 return SBListener(shadow_sp
);
266 void SBAttachInfo::SetShadowListener(SBListener
&listener
) {
267 LLDB_INSTRUMENT_VA(this, listener
);
269 ListenerSP listener_sp
= listener
.GetSP();
270 if (listener_sp
&& listener
.IsValid())
271 listener_sp
->SetShadow(true);
273 listener_sp
= nullptr;
275 m_opaque_sp
->SetShadowListener(listener_sp
);
278 const char *SBAttachInfo::GetScriptedProcessClassName() const {
279 LLDB_INSTRUMENT_VA(this);
281 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
283 if (!metadata_sp
|| !*metadata_sp
)
286 // Constify this string so that it is saved in the string pool. Otherwise it
287 // would be freed when this function goes out of scope.
288 ConstString
class_name(metadata_sp
->GetClassName().data());
289 return class_name
.AsCString();
292 void SBAttachInfo::SetScriptedProcessClassName(const char *class_name
) {
293 LLDB_INSTRUMENT_VA(this, class_name
);
295 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
298 metadata_sp
= std::make_shared
<ScriptedMetadata
>(class_name
, nullptr);
300 metadata_sp
= std::make_shared
<ScriptedMetadata
>(class_name
,
301 metadata_sp
->GetArgsSP());
303 m_opaque_sp
->SetScriptedMetadata(metadata_sp
);
306 lldb::SBStructuredData
SBAttachInfo::GetScriptedProcessDictionary() const {
307 LLDB_INSTRUMENT_VA(this);
309 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
311 SBStructuredData data
;
315 lldb_private::StructuredData::DictionarySP dict_sp
= metadata_sp
->GetArgsSP();
316 data
.m_impl_up
->SetObjectSP(dict_sp
);
321 void SBAttachInfo::SetScriptedProcessDictionary(lldb::SBStructuredData dict
) {
322 LLDB_INSTRUMENT_VA(this, dict
);
324 if (!dict
.IsValid() || !dict
.m_impl_up
)
327 StructuredData::ObjectSP obj_sp
= dict
.m_impl_up
->GetObjectSP();
332 StructuredData::DictionarySP dict_sp
=
333 std::make_shared
<StructuredData::Dictionary
>(obj_sp
);
334 if (!dict_sp
|| dict_sp
->GetType() == lldb::eStructuredDataTypeInvalid
)
337 ScriptedMetadataSP metadata_sp
= m_opaque_sp
->GetScriptedMetadata();
340 metadata_sp
= std::make_shared
<ScriptedMetadata
>("", dict_sp
);
342 metadata_sp
= std::make_shared
<ScriptedMetadata
>(
343 metadata_sp
->GetClassName(), dict_sp
);
345 m_opaque_sp
->SetScriptedMetadata(metadata_sp
);