180814287SRaphael Isemann //===-- SBPlatform.cpp ----------------------------------------------------===//
2fbb76349SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fbb76349SGreg Clayton //
7fbb76349SGreg Clayton //===----------------------------------------------------------------------===//
8fbb76349SGreg Clayton 
9fbb76349SGreg Clayton #include "lldb/API/SBPlatform.h"
10ca69be21SWalter Erquinigo #include "lldb/API/SBEnvironment.h"
11fbb76349SGreg Clayton #include "lldb/API/SBError.h"
12fbb76349SGreg Clayton #include "lldb/API/SBFileSpec.h"
131ef7b2c8SOleksiy Vyalov #include "lldb/API/SBLaunchInfo.h"
14ca69be21SWalter Erquinigo #include "lldb/API/SBPlatform.h"
1598d0a4b3SChaoren Lin #include "lldb/API/SBUnixSignals.h"
16fbb76349SGreg Clayton #include "lldb/Host/File.h"
17fbb76349SGreg Clayton #include "lldb/Target/Platform.h"
18b9c1b51eSKate Stone #include "lldb/Target/Target.h"
195f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
20145d95c9SPavel Labath #include "lldb/Utility/Args.h"
211755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
2297206d57SZachary Turner #include "lldb/Utility/Status.h"
23fbb76349SGreg Clayton 
247d86ee5aSZachary Turner #include "llvm/Support/FileSystem.h"
257d86ee5aSZachary Turner 
261ef7b2c8SOleksiy Vyalov #include <functional>
271ef7b2c8SOleksiy Vyalov 
28fbb76349SGreg Clayton using namespace lldb;
29fbb76349SGreg Clayton using namespace lldb_private;
30fbb76349SGreg Clayton 
31fbb76349SGreg Clayton // PlatformConnectOptions
32fbb76349SGreg Clayton struct PlatformConnectOptions {
33a3436f73SKazu Hirata   PlatformConnectOptions(const char *url = nullptr) {
34fbb76349SGreg Clayton     if (url && url[0])
35fbb76349SGreg Clayton       m_url = url;
36fbb76349SGreg Clayton   }
37fbb76349SGreg Clayton 
38866b7a65SJonas Devlieghere   ~PlatformConnectOptions() = default;
39fbb76349SGreg Clayton 
40fbb76349SGreg Clayton   std::string m_url;
41fbb76349SGreg Clayton   std::string m_rsync_options;
42fbb76349SGreg Clayton   std::string m_rsync_remote_path_prefix;
439494c510SJonas Devlieghere   bool m_rsync_enabled = false;
449494c510SJonas Devlieghere   bool m_rsync_omit_hostname_from_remote_path = false;
45fbb76349SGreg Clayton   ConstString m_local_cache_directory;
46fbb76349SGreg Clayton };
47fbb76349SGreg Clayton 
48fbb76349SGreg Clayton // PlatformShellCommand
49fbb76349SGreg Clayton struct PlatformShellCommand {
50addb5148SMed Ismail Bennani   PlatformShellCommand(llvm::StringRef shell_interpreter,
51addb5148SMed Ismail Bennani                        llvm::StringRef shell_command)
52a3436f73SKazu Hirata       : m_status(0), m_signo(0) {
53addb5148SMed Ismail Bennani     if (!shell_interpreter.empty())
54addb5148SMed Ismail Bennani       m_shell = shell_interpreter.str();
55addb5148SMed Ismail Bennani 
56addb5148SMed Ismail Bennani     if (!m_shell.empty() && !shell_command.empty())
57addb5148SMed Ismail Bennani       m_command = shell_command.str();
58addb5148SMed Ismail Bennani   }
59addb5148SMed Ismail Bennani 
60a3436f73SKazu Hirata   PlatformShellCommand(llvm::StringRef shell_command = llvm::StringRef()) {
61addb5148SMed Ismail Bennani     if (!shell_command.empty())
62addb5148SMed Ismail Bennani       m_command = shell_command.str();
63fbb76349SGreg Clayton   }
64fbb76349SGreg Clayton 
65866b7a65SJonas Devlieghere   ~PlatformShellCommand() = default;
66fbb76349SGreg Clayton 
67addb5148SMed Ismail Bennani   std::string m_shell;
68fbb76349SGreg Clayton   std::string m_command;
69fbb76349SGreg Clayton   std::string m_working_dir;
70fbb76349SGreg Clayton   std::string m_output;
719494c510SJonas Devlieghere   int m_status = 0;
729494c510SJonas Devlieghere   int m_signo = 0;
7319dd1a0eSPavel Labath   Timeout<std::ratio<1>> m_timeout = llvm::None;
74fbb76349SGreg Clayton };
75fbb76349SGreg Clayton // SBPlatformConnectOptions
76b9c1b51eSKate Stone SBPlatformConnectOptions::SBPlatformConnectOptions(const char *url)
77baf5664fSJonas Devlieghere     : m_opaque_ptr(new PlatformConnectOptions(url)) {
781755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, url);
79baf5664fSJonas Devlieghere }
80fbb76349SGreg Clayton 
81b9c1b51eSKate Stone SBPlatformConnectOptions::SBPlatformConnectOptions(
82b9c1b51eSKate Stone     const SBPlatformConnectOptions &rhs)
83b9c1b51eSKate Stone     : m_opaque_ptr(new PlatformConnectOptions()) {
841755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
85baf5664fSJonas Devlieghere 
86fbb76349SGreg Clayton   *m_opaque_ptr = *rhs.m_opaque_ptr;
87fbb76349SGreg Clayton }
88fbb76349SGreg Clayton 
89b9c1b51eSKate Stone SBPlatformConnectOptions::~SBPlatformConnectOptions() { delete m_opaque_ptr; }
90fbb76349SGreg Clayton 
91bc0a35f3SJonas Devlieghere SBPlatformConnectOptions &
92bc0a35f3SJonas Devlieghere SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs) {
931755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
94baf5664fSJonas Devlieghere 
95fbb76349SGreg Clayton   *m_opaque_ptr = *rhs.m_opaque_ptr;
96d232abc3SJonas Devlieghere   return *this;
97fbb76349SGreg Clayton }
98fbb76349SGreg Clayton 
99b9c1b51eSKate Stone const char *SBPlatformConnectOptions::GetURL() {
1001755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
101baf5664fSJonas Devlieghere 
102fbb76349SGreg Clayton   if (m_opaque_ptr->m_url.empty())
103248a1305SKonrad Kleine     return nullptr;
104fbb76349SGreg Clayton   return m_opaque_ptr->m_url.c_str();
105fbb76349SGreg Clayton }
106fbb76349SGreg Clayton 
107b9c1b51eSKate Stone void SBPlatformConnectOptions::SetURL(const char *url) {
1081755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, url);
109baf5664fSJonas Devlieghere 
110fbb76349SGreg Clayton   if (url && url[0])
111fbb76349SGreg Clayton     m_opaque_ptr->m_url = url;
112fbb76349SGreg Clayton   else
113fbb76349SGreg Clayton     m_opaque_ptr->m_url.clear();
114fbb76349SGreg Clayton }
115fbb76349SGreg Clayton 
116b9c1b51eSKate Stone bool SBPlatformConnectOptions::GetRsyncEnabled() {
1171755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
118baf5664fSJonas Devlieghere 
119fbb76349SGreg Clayton   return m_opaque_ptr->m_rsync_enabled;
120fbb76349SGreg Clayton }
121fbb76349SGreg Clayton 
122b9c1b51eSKate Stone void SBPlatformConnectOptions::EnableRsync(
123b9c1b51eSKate Stone     const char *options, const char *remote_path_prefix,
124b9c1b51eSKate Stone     bool omit_hostname_from_remote_path) {
1251755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, options, remote_path_prefix,
1261755f5b1SJonas Devlieghere                      omit_hostname_from_remote_path);
127baf5664fSJonas Devlieghere 
128fbb76349SGreg Clayton   m_opaque_ptr->m_rsync_enabled = true;
129b9c1b51eSKate Stone   m_opaque_ptr->m_rsync_omit_hostname_from_remote_path =
130b9c1b51eSKate Stone       omit_hostname_from_remote_path;
131fbb76349SGreg Clayton   if (remote_path_prefix && remote_path_prefix[0])
132fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix;
133fbb76349SGreg Clayton   else
134fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_remote_path_prefix.clear();
135fbb76349SGreg Clayton 
136fbb76349SGreg Clayton   if (options && options[0])
137fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_options = options;
138fbb76349SGreg Clayton   else
139fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_options.clear();
140fbb76349SGreg Clayton }
141fbb76349SGreg Clayton 
142b9c1b51eSKate Stone void SBPlatformConnectOptions::DisableRsync() {
1431755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
144baf5664fSJonas Devlieghere 
145fbb76349SGreg Clayton   m_opaque_ptr->m_rsync_enabled = false;
146fbb76349SGreg Clayton }
147fbb76349SGreg Clayton 
148b9c1b51eSKate Stone const char *SBPlatformConnectOptions::GetLocalCacheDirectory() {
1491755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
150baf5664fSJonas Devlieghere 
151fbb76349SGreg Clayton   return m_opaque_ptr->m_local_cache_directory.GetCString();
152fbb76349SGreg Clayton }
153fbb76349SGreg Clayton 
154b9c1b51eSKate Stone void SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path) {
1551755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, path);
156baf5664fSJonas Devlieghere 
157fbb76349SGreg Clayton   if (path && path[0])
158fbb76349SGreg Clayton     m_opaque_ptr->m_local_cache_directory.SetCString(path);
159fbb76349SGreg Clayton   else
160fbb76349SGreg Clayton     m_opaque_ptr->m_local_cache_directory = ConstString();
161fbb76349SGreg Clayton }
162fbb76349SGreg Clayton 
163fbb76349SGreg Clayton // SBPlatformShellCommand
164addb5148SMed Ismail Bennani SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_interpreter,
165addb5148SMed Ismail Bennani                                                const char *shell_command)
166addb5148SMed Ismail Bennani     : m_opaque_ptr(new PlatformShellCommand(shell_interpreter, shell_command)) {
1671755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, shell_interpreter, shell_command);
168addb5148SMed Ismail Bennani }
169addb5148SMed Ismail Bennani 
170b9c1b51eSKate Stone SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_command)
171baf5664fSJonas Devlieghere     : m_opaque_ptr(new PlatformShellCommand(shell_command)) {
1721755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, shell_command);
173baf5664fSJonas Devlieghere }
174fbb76349SGreg Clayton 
175b9c1b51eSKate Stone SBPlatformShellCommand::SBPlatformShellCommand(
176b9c1b51eSKate Stone     const SBPlatformShellCommand &rhs)
177b9c1b51eSKate Stone     : m_opaque_ptr(new PlatformShellCommand()) {
1781755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
179baf5664fSJonas Devlieghere 
180fbb76349SGreg Clayton   *m_opaque_ptr = *rhs.m_opaque_ptr;
181fbb76349SGreg Clayton }
182fbb76349SGreg Clayton 
183bc0a35f3SJonas Devlieghere SBPlatformShellCommand &
184bc0a35f3SJonas Devlieghere SBPlatformShellCommand::operator=(const SBPlatformShellCommand &rhs) {
185ede5cd9aSJonas Devlieghere 
1861755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
187ede5cd9aSJonas Devlieghere 
188ede5cd9aSJonas Devlieghere   *m_opaque_ptr = *rhs.m_opaque_ptr;
189d232abc3SJonas Devlieghere   return *this;
190ede5cd9aSJonas Devlieghere }
191ede5cd9aSJonas Devlieghere 
192b9c1b51eSKate Stone SBPlatformShellCommand::~SBPlatformShellCommand() { delete m_opaque_ptr; }
193fbb76349SGreg Clayton 
194b9c1b51eSKate Stone void SBPlatformShellCommand::Clear() {
1951755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
196baf5664fSJonas Devlieghere 
1973a29f8b9SPavel Labath   m_opaque_ptr->m_output = std::string();
198fbb76349SGreg Clayton   m_opaque_ptr->m_status = 0;
199fbb76349SGreg Clayton   m_opaque_ptr->m_signo = 0;
200fbb76349SGreg Clayton }
201fbb76349SGreg Clayton 
202addb5148SMed Ismail Bennani const char *SBPlatformShellCommand::GetShell() {
2031755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
204addb5148SMed Ismail Bennani 
205addb5148SMed Ismail Bennani   if (m_opaque_ptr->m_shell.empty())
206addb5148SMed Ismail Bennani     return nullptr;
207addb5148SMed Ismail Bennani   return m_opaque_ptr->m_shell.c_str();
208addb5148SMed Ismail Bennani }
209addb5148SMed Ismail Bennani 
210addb5148SMed Ismail Bennani void SBPlatformShellCommand::SetShell(const char *shell_interpreter) {
2111755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, shell_interpreter);
212addb5148SMed Ismail Bennani 
213addb5148SMed Ismail Bennani   if (shell_interpreter && shell_interpreter[0])
214addb5148SMed Ismail Bennani     m_opaque_ptr->m_shell = shell_interpreter;
215addb5148SMed Ismail Bennani   else
216addb5148SMed Ismail Bennani     m_opaque_ptr->m_shell.clear();
217addb5148SMed Ismail Bennani }
218addb5148SMed Ismail Bennani 
219b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetCommand() {
2201755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
221baf5664fSJonas Devlieghere 
222fbb76349SGreg Clayton   if (m_opaque_ptr->m_command.empty())
223248a1305SKonrad Kleine     return nullptr;
224fbb76349SGreg Clayton   return m_opaque_ptr->m_command.c_str();
225fbb76349SGreg Clayton }
226fbb76349SGreg Clayton 
227b9c1b51eSKate Stone void SBPlatformShellCommand::SetCommand(const char *shell_command) {
2281755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, shell_command);
229baf5664fSJonas Devlieghere 
230fbb76349SGreg Clayton   if (shell_command && shell_command[0])
231fbb76349SGreg Clayton     m_opaque_ptr->m_command = shell_command;
232fbb76349SGreg Clayton   else
233fbb76349SGreg Clayton     m_opaque_ptr->m_command.clear();
234fbb76349SGreg Clayton }
235fbb76349SGreg Clayton 
236b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetWorkingDirectory() {
2371755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
238baf5664fSJonas Devlieghere 
239fbb76349SGreg Clayton   if (m_opaque_ptr->m_working_dir.empty())
240248a1305SKonrad Kleine     return nullptr;
241fbb76349SGreg Clayton   return m_opaque_ptr->m_working_dir.c_str();
242fbb76349SGreg Clayton }
243fbb76349SGreg Clayton 
244b9c1b51eSKate Stone void SBPlatformShellCommand::SetWorkingDirectory(const char *path) {
2451755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, path);
246baf5664fSJonas Devlieghere 
247fbb76349SGreg Clayton   if (path && path[0])
248fbb76349SGreg Clayton     m_opaque_ptr->m_working_dir = path;
249fbb76349SGreg Clayton   else
250fbb76349SGreg Clayton     m_opaque_ptr->m_working_dir.clear();
251fbb76349SGreg Clayton }
252fbb76349SGreg Clayton 
253b9c1b51eSKate Stone uint32_t SBPlatformShellCommand::GetTimeoutSeconds() {
2541755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
255baf5664fSJonas Devlieghere 
25619dd1a0eSPavel Labath   if (m_opaque_ptr->m_timeout)
25719dd1a0eSPavel Labath     return m_opaque_ptr->m_timeout->count();
25819dd1a0eSPavel Labath   return UINT32_MAX;
259fbb76349SGreg Clayton }
260fbb76349SGreg Clayton 
261b9c1b51eSKate Stone void SBPlatformShellCommand::SetTimeoutSeconds(uint32_t sec) {
2621755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, sec);
263baf5664fSJonas Devlieghere 
26419dd1a0eSPavel Labath   if (sec == UINT32_MAX)
26519dd1a0eSPavel Labath     m_opaque_ptr->m_timeout = llvm::None;
26619dd1a0eSPavel Labath   else
26719dd1a0eSPavel Labath     m_opaque_ptr->m_timeout = std::chrono::seconds(sec);
268fbb76349SGreg Clayton }
269fbb76349SGreg Clayton 
270baf5664fSJonas Devlieghere int SBPlatformShellCommand::GetSignal() {
2711755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
272fbb76349SGreg Clayton 
273baf5664fSJonas Devlieghere   return m_opaque_ptr->m_signo;
274baf5664fSJonas Devlieghere }
275baf5664fSJonas Devlieghere 
276baf5664fSJonas Devlieghere int SBPlatformShellCommand::GetStatus() {
2771755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
278baf5664fSJonas Devlieghere 
279baf5664fSJonas Devlieghere   return m_opaque_ptr->m_status;
280baf5664fSJonas Devlieghere }
281fbb76349SGreg Clayton 
282b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetOutput() {
2831755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
284baf5664fSJonas Devlieghere 
285fbb76349SGreg Clayton   if (m_opaque_ptr->m_output.empty())
286248a1305SKonrad Kleine     return nullptr;
287fbb76349SGreg Clayton   return m_opaque_ptr->m_output.c_str();
288fbb76349SGreg Clayton }
289fbb76349SGreg Clayton 
290fbb76349SGreg Clayton // SBPlatform
2911755f5b1SJonas Devlieghere SBPlatform::SBPlatform() { LLDB_INSTRUMENT_VA(this); }
292fbb76349SGreg Clayton 
293a3436f73SKazu Hirata SBPlatform::SBPlatform(const char *platform_name) {
2941755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, platform_name);
295baf5664fSJonas Devlieghere 
29697206d57SZachary Turner   Status error;
297615eb7e6SGreg Clayton   if (platform_name && platform_name[0])
298615eb7e6SGreg Clayton     m_opaque_sp = Platform::Create(ConstString(platform_name), error);
299fbb76349SGreg Clayton }
300fbb76349SGreg Clayton 
30166dc4672SJonas Devlieghere SBPlatform::SBPlatform(const SBPlatform &rhs) {
3021755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
30366dc4672SJonas Devlieghere 
30466dc4672SJonas Devlieghere   m_opaque_sp = rhs.m_opaque_sp;
30566dc4672SJonas Devlieghere }
30666dc4672SJonas Devlieghere 
307ede5cd9aSJonas Devlieghere SBPlatform &SBPlatform::operator=(const SBPlatform &rhs) {
3081755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
30966dc4672SJonas Devlieghere 
31066dc4672SJonas Devlieghere   m_opaque_sp = rhs.m_opaque_sp;
311d232abc3SJonas Devlieghere   return *this;
31266dc4672SJonas Devlieghere }
31366dc4672SJonas Devlieghere 
314866b7a65SJonas Devlieghere SBPlatform::~SBPlatform() = default;
315fbb76349SGreg Clayton 
316a31130f6STatyana Krasnukha SBPlatform SBPlatform::GetHostPlatform() {
3171755f5b1SJonas Devlieghere   LLDB_INSTRUMENT();
3185c2bf577SJonas Devlieghere 
319a31130f6STatyana Krasnukha   SBPlatform host_platform;
320a31130f6STatyana Krasnukha   host_platform.m_opaque_sp = Platform::GetHostPlatform();
321d232abc3SJonas Devlieghere   return host_platform;
322a31130f6STatyana Krasnukha }
323a31130f6STatyana Krasnukha 
324baf5664fSJonas Devlieghere bool SBPlatform::IsValid() const {
3251755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
3267f5237bcSPavel Labath   return this->operator bool();
3277f5237bcSPavel Labath }
3287f5237bcSPavel Labath SBPlatform::operator bool() const {
3291755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
330fbb76349SGreg Clayton 
331248a1305SKonrad Kleine   return m_opaque_sp.get() != nullptr;
332baf5664fSJonas Devlieghere }
333baf5664fSJonas Devlieghere 
334baf5664fSJonas Devlieghere void SBPlatform::Clear() {
3351755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
336baf5664fSJonas Devlieghere 
337baf5664fSJonas Devlieghere   m_opaque_sp.reset();
338baf5664fSJonas Devlieghere }
339fbb76349SGreg Clayton 
340b9c1b51eSKate Stone const char *SBPlatform::GetName() {
3411755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
342baf5664fSJonas Devlieghere 
343fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
344fbb76349SGreg Clayton   if (platform_sp)
345*d2edca62SPavel Labath     return ConstString(platform_sp->GetName()).AsCString();
346248a1305SKonrad Kleine   return nullptr;
347fbb76349SGreg Clayton }
348fbb76349SGreg Clayton 
349b9c1b51eSKate Stone lldb::PlatformSP SBPlatform::GetSP() const { return m_opaque_sp; }
350fbb76349SGreg Clayton 
351b9c1b51eSKate Stone void SBPlatform::SetSP(const lldb::PlatformSP &platform_sp) {
352fbb76349SGreg Clayton   m_opaque_sp = platform_sp;
353fbb76349SGreg Clayton }
354fbb76349SGreg Clayton 
355b9c1b51eSKate Stone const char *SBPlatform::GetWorkingDirectory() {
3561755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
357baf5664fSJonas Devlieghere 
358fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
359fbb76349SGreg Clayton   if (platform_sp)
360fbb76349SGreg Clayton     return platform_sp->GetWorkingDirectory().GetCString();
361248a1305SKonrad Kleine   return nullptr;
362fbb76349SGreg Clayton }
363fbb76349SGreg Clayton 
364b9c1b51eSKate Stone bool SBPlatform::SetWorkingDirectory(const char *path) {
3651755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, path);
366baf5664fSJonas Devlieghere 
367fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
368b9c1b51eSKate Stone   if (platform_sp) {
369fbb76349SGreg Clayton     if (path)
3708f3be7a3SJonas Devlieghere       platform_sp->SetWorkingDirectory(FileSpec(path));
371fbb76349SGreg Clayton     else
3728f3be7a3SJonas Devlieghere       platform_sp->SetWorkingDirectory(FileSpec());
373fbb76349SGreg Clayton     return true;
374fbb76349SGreg Clayton   }
375fbb76349SGreg Clayton   return false;
376fbb76349SGreg Clayton }
377fbb76349SGreg Clayton 
378b9c1b51eSKate Stone SBError SBPlatform::ConnectRemote(SBPlatformConnectOptions &connect_options) {
3791755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, connect_options);
380baf5664fSJonas Devlieghere 
381fbb76349SGreg Clayton   SBError sb_error;
382fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
383b9c1b51eSKate Stone   if (platform_sp && connect_options.GetURL()) {
384fbb76349SGreg Clayton     Args args;
385bbea3610SRaphael Isemann     args.AppendArgument(connect_options.GetURL());
386fbb76349SGreg Clayton     sb_error.ref() = platform_sp->ConnectRemote(args);
387b9c1b51eSKate Stone   } else {
388fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
389fbb76349SGreg Clayton   }
390d232abc3SJonas Devlieghere   return sb_error;
391fbb76349SGreg Clayton }
392fbb76349SGreg Clayton 
393b9c1b51eSKate Stone void SBPlatform::DisconnectRemote() {
3941755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
395baf5664fSJonas Devlieghere 
396fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
397fbb76349SGreg Clayton   if (platform_sp)
398fbb76349SGreg Clayton     platform_sp->DisconnectRemote();
399fbb76349SGreg Clayton }
400fbb76349SGreg Clayton 
401b9c1b51eSKate Stone bool SBPlatform::IsConnected() {
4021755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
403baf5664fSJonas Devlieghere 
404fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
405fbb76349SGreg Clayton   if (platform_sp)
406ca38766cSJim Ingham     return platform_sp->IsConnected();
407fbb76349SGreg Clayton   return false;
408fbb76349SGreg Clayton }
409fbb76349SGreg Clayton 
410b9c1b51eSKate Stone const char *SBPlatform::GetTriple() {
4111755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
412baf5664fSJonas Devlieghere 
413fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
414b9c1b51eSKate Stone   if (platform_sp) {
415ccd28a14STamas Berghammer     ArchSpec arch(platform_sp->GetSystemArchitecture());
416b9c1b51eSKate Stone     if (arch.IsValid()) {
417b9c1b51eSKate Stone       // Const-ify the string so we don't need to worry about the lifetime of
418b9c1b51eSKate Stone       // the string
419fbb76349SGreg Clayton       return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
420fbb76349SGreg Clayton     }
421fbb76349SGreg Clayton   }
422248a1305SKonrad Kleine   return nullptr;
423fbb76349SGreg Clayton }
424fbb76349SGreg Clayton 
425b9c1b51eSKate Stone const char *SBPlatform::GetOSBuild() {
4261755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
427baf5664fSJonas Devlieghere 
428fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
429b9c1b51eSKate Stone   if (platform_sp) {
43040e4ac3eSPavel Labath     std::string s = platform_sp->GetOSBuildString().getValueOr("");
431b9c1b51eSKate Stone     if (!s.empty()) {
432b9c1b51eSKate Stone       // Const-ify the string so we don't need to worry about the lifetime of
433b9c1b51eSKate Stone       // the string
43440e4ac3eSPavel Labath       return ConstString(s).GetCString();
435fbb76349SGreg Clayton     }
436fbb76349SGreg Clayton   }
437248a1305SKonrad Kleine   return nullptr;
438fbb76349SGreg Clayton }
439fbb76349SGreg Clayton 
440b9c1b51eSKate Stone const char *SBPlatform::GetOSDescription() {
4411755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
442baf5664fSJonas Devlieghere 
443fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
444b9c1b51eSKate Stone   if (platform_sp) {
445f5158ca4SPavel Labath     std::string s = platform_sp->GetOSKernelDescription().getValueOr("");
446b9c1b51eSKate Stone     if (!s.empty()) {
447b9c1b51eSKate Stone       // Const-ify the string so we don't need to worry about the lifetime of
448b9c1b51eSKate Stone       // the string
449fbb76349SGreg Clayton       return ConstString(s.c_str()).GetCString();
450fbb76349SGreg Clayton     }
451fbb76349SGreg Clayton   }
452248a1305SKonrad Kleine   return nullptr;
453fbb76349SGreg Clayton }
454fbb76349SGreg Clayton 
455b9c1b51eSKate Stone const char *SBPlatform::GetHostname() {
4561755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
457baf5664fSJonas Devlieghere 
458fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
459fbb76349SGreg Clayton   if (platform_sp)
460fbb76349SGreg Clayton     return platform_sp->GetHostname();
461248a1305SKonrad Kleine   return nullptr;
462fbb76349SGreg Clayton }
463fbb76349SGreg Clayton 
464b9c1b51eSKate Stone uint32_t SBPlatform::GetOSMajorVersion() {
4651755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
466baf5664fSJonas Devlieghere 
4672272c481SPavel Labath   llvm::VersionTuple version;
4682272c481SPavel Labath   if (PlatformSP platform_sp = GetSP())
4692272c481SPavel Labath     version = platform_sp->GetOSVersion();
4702272c481SPavel Labath   return version.empty() ? UINT32_MAX : version.getMajor();
471fbb76349SGreg Clayton }
472fbb76349SGreg Clayton 
473b9c1b51eSKate Stone uint32_t SBPlatform::GetOSMinorVersion() {
4741755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
475baf5664fSJonas Devlieghere 
4762272c481SPavel Labath   llvm::VersionTuple version;
4772272c481SPavel Labath   if (PlatformSP platform_sp = GetSP())
4782272c481SPavel Labath     version = platform_sp->GetOSVersion();
4792272c481SPavel Labath   return version.getMinor().getValueOr(UINT32_MAX);
480fbb76349SGreg Clayton }
481fbb76349SGreg Clayton 
482b9c1b51eSKate Stone uint32_t SBPlatform::GetOSUpdateVersion() {
4831755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
484baf5664fSJonas Devlieghere 
4852272c481SPavel Labath   llvm::VersionTuple version;
4862272c481SPavel Labath   if (PlatformSP platform_sp = GetSP())
4872272c481SPavel Labath     version = platform_sp->GetOSVersion();
4882272c481SPavel Labath   return version.getSubminor().getValueOr(UINT32_MAX);
489fbb76349SGreg Clayton }
490fbb76349SGreg Clayton 
49190342453SPavel Labath void SBPlatform::SetSDKRoot(const char *sysroot) {
4921755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, sysroot);
49390342453SPavel Labath   if (PlatformSP platform_sp = GetSP())
49490342453SPavel Labath     platform_sp->SetSDKRootDirectory(ConstString(sysroot));
49590342453SPavel Labath }
49690342453SPavel Labath 
497b9c1b51eSKate Stone SBError SBPlatform::Get(SBFileSpec &src, SBFileSpec &dst) {
4981755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, src, dst);
499baf5664fSJonas Devlieghere 
500fbb76349SGreg Clayton   SBError sb_error;
501fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
502b9c1b51eSKate Stone   if (platform_sp) {
503fbb76349SGreg Clayton     sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
504b9c1b51eSKate Stone   } else {
505fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
506fbb76349SGreg Clayton   }
507d232abc3SJonas Devlieghere   return sb_error;
508fbb76349SGreg Clayton }
509fbb76349SGreg Clayton 
510b9c1b51eSKate Stone SBError SBPlatform::Put(SBFileSpec &src, SBFileSpec &dst) {
5111755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, src, dst);
512d232abc3SJonas Devlieghere   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
513b9c1b51eSKate Stone     if (src.Exists()) {
514d232abc3SJonas Devlieghere       uint32_t permissions = FileSystem::Instance().GetPermissions(src.ref());
515b9c1b51eSKate Stone       if (permissions == 0) {
5163a58d898SJonas Devlieghere         if (FileSystem::Instance().IsDirectory(src.ref()))
517fbb76349SGreg Clayton           permissions = eFilePermissionsDirectoryDefault;
518fbb76349SGreg Clayton         else
519fbb76349SGreg Clayton           permissions = eFilePermissionsFileDefault;
520fbb76349SGreg Clayton       }
521fbb76349SGreg Clayton 
5221ef7b2c8SOleksiy Vyalov       return platform_sp->PutFile(src.ref(), dst.ref(), permissions);
523fbb76349SGreg Clayton     }
5241ef7b2c8SOleksiy Vyalov 
52597206d57SZachary Turner     Status error;
526b9c1b51eSKate Stone     error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
527b9c1b51eSKate Stone                                    src.ref().GetPath().c_str());
5281ef7b2c8SOleksiy Vyalov     return error;
529d232abc3SJonas Devlieghere   });
530fbb76349SGreg Clayton }
531fbb76349SGreg Clayton 
532b9c1b51eSKate Stone SBError SBPlatform::Install(SBFileSpec &src, SBFileSpec &dst) {
5331755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, src, dst);
534d232abc3SJonas Devlieghere   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
535fbb76349SGreg Clayton     if (src.Exists())
5361ef7b2c8SOleksiy Vyalov       return platform_sp->Install(src.ref(), dst.ref());
5371ef7b2c8SOleksiy Vyalov 
53897206d57SZachary Turner     Status error;
539b9c1b51eSKate Stone     error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
540b9c1b51eSKate Stone                                    src.ref().GetPath().c_str());
5411ef7b2c8SOleksiy Vyalov     return error;
542d232abc3SJonas Devlieghere   });
543fbb76349SGreg Clayton }
544fbb76349SGreg Clayton 
545b9c1b51eSKate Stone SBError SBPlatform::Run(SBPlatformShellCommand &shell_command) {
5461755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, shell_command);
547d232abc3SJonas Devlieghere   return ExecuteConnected(
548d232abc3SJonas Devlieghere       [&](const lldb::PlatformSP &platform_sp) {
549fbb76349SGreg Clayton         const char *command = shell_command.GetCommand();
5501ef7b2c8SOleksiy Vyalov         if (!command)
55197206d57SZachary Turner           return Status("invalid shell command (empty)");
5521ef7b2c8SOleksiy Vyalov 
553fbb76349SGreg Clayton         const char *working_dir = shell_command.GetWorkingDirectory();
554248a1305SKonrad Kleine         if (working_dir == nullptr) {
555fbb76349SGreg Clayton           working_dir = platform_sp->GetWorkingDirectory().GetCString();
556fbb76349SGreg Clayton           if (working_dir)
557fbb76349SGreg Clayton             shell_command.SetWorkingDirectory(working_dir);
558fbb76349SGreg Clayton         }
559bc0a35f3SJonas Devlieghere         return platform_sp->RunShellCommand(
560bc0a35f3SJonas Devlieghere             shell_command.m_opaque_ptr->m_shell, command, FileSpec(working_dir),
561fbb76349SGreg Clayton             &shell_command.m_opaque_ptr->m_status,
562fbb76349SGreg Clayton             &shell_command.m_opaque_ptr->m_signo,
563fbb76349SGreg Clayton             &shell_command.m_opaque_ptr->m_output,
56419dd1a0eSPavel Labath             shell_command.m_opaque_ptr->m_timeout);
565d232abc3SJonas Devlieghere       });
566fbb76349SGreg Clayton }
5671ef7b2c8SOleksiy Vyalov 
568b9c1b51eSKate Stone SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
5691755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, launch_info);
570d232abc3SJonas Devlieghere   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
57162930e57SPavel Labath     ProcessLaunchInfo info = launch_info.ref();
57262930e57SPavel Labath     Status error = platform_sp->LaunchProcess(info);
57362930e57SPavel Labath     launch_info.set_ref(info);
57462930e57SPavel Labath     return error;
575d232abc3SJonas Devlieghere   });
5761ef7b2c8SOleksiy Vyalov }
5771ef7b2c8SOleksiy Vyalov 
578b9c1b51eSKate Stone SBError SBPlatform::Kill(const lldb::pid_t pid) {
5791755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, pid);
580d232abc3SJonas Devlieghere   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
5811ef7b2c8SOleksiy Vyalov     return platform_sp->KillProcess(pid);
582d232abc3SJonas Devlieghere   });
5831ef7b2c8SOleksiy Vyalov }
5841ef7b2c8SOleksiy Vyalov 
585b9c1b51eSKate Stone SBError SBPlatform::ExecuteConnected(
58697206d57SZachary Turner     const std::function<Status(const lldb::PlatformSP &)> &func) {
5871ef7b2c8SOleksiy Vyalov   SBError sb_error;
5881ef7b2c8SOleksiy Vyalov   const auto platform_sp(GetSP());
589b9c1b51eSKate Stone   if (platform_sp) {
5901ef7b2c8SOleksiy Vyalov     if (platform_sp->IsConnected())
5911ef7b2c8SOleksiy Vyalov       sb_error.ref() = func(platform_sp);
5921ef7b2c8SOleksiy Vyalov     else
593fbb76349SGreg Clayton       sb_error.SetErrorString("not connected");
594b9c1b51eSKate Stone   } else
595fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
5961ef7b2c8SOleksiy Vyalov 
597fbb76349SGreg Clayton   return sb_error;
598fbb76349SGreg Clayton }
599fbb76349SGreg Clayton 
600b9c1b51eSKate Stone SBError SBPlatform::MakeDirectory(const char *path, uint32_t file_permissions) {
6011755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, path, file_permissions);
602baf5664fSJonas Devlieghere 
603fbb76349SGreg Clayton   SBError sb_error;
604fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
605b9c1b51eSKate Stone   if (platform_sp) {
606b9c1b51eSKate Stone     sb_error.ref() =
6078f3be7a3SJonas Devlieghere         platform_sp->MakeDirectory(FileSpec(path), file_permissions);
608b9c1b51eSKate Stone   } else {
609fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
610fbb76349SGreg Clayton   }
611d232abc3SJonas Devlieghere   return sb_error;
612fbb76349SGreg Clayton }
613fbb76349SGreg Clayton 
614b9c1b51eSKate Stone uint32_t SBPlatform::GetFilePermissions(const char *path) {
6151755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, path);
616baf5664fSJonas Devlieghere 
617fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
618b9c1b51eSKate Stone   if (platform_sp) {
619fbb76349SGreg Clayton     uint32_t file_permissions = 0;
6208f3be7a3SJonas Devlieghere     platform_sp->GetFilePermissions(FileSpec(path), file_permissions);
621fbb76349SGreg Clayton     return file_permissions;
622fbb76349SGreg Clayton   }
623fbb76349SGreg Clayton   return 0;
624fbb76349SGreg Clayton }
625fbb76349SGreg Clayton 
626b9c1b51eSKate Stone SBError SBPlatform::SetFilePermissions(const char *path,
627b9c1b51eSKate Stone                                        uint32_t file_permissions) {
6281755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, path, file_permissions);
629baf5664fSJonas Devlieghere 
630fbb76349SGreg Clayton   SBError sb_error;
631fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
632b9c1b51eSKate Stone   if (platform_sp) {
6338f3be7a3SJonas Devlieghere     sb_error.ref() =
6348f3be7a3SJonas Devlieghere         platform_sp->SetFilePermissions(FileSpec(path), file_permissions);
635b9c1b51eSKate Stone   } else {
636fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
637fbb76349SGreg Clayton   }
638d232abc3SJonas Devlieghere   return sb_error;
639fbb76349SGreg Clayton }
640fbb76349SGreg Clayton 
641b9c1b51eSKate Stone SBUnixSignals SBPlatform::GetUnixSignals() const {
6421755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
64398d0a4b3SChaoren Lin 
644baf5664fSJonas Devlieghere   if (auto platform_sp = GetSP())
645d232abc3SJonas Devlieghere     return SBUnixSignals{platform_sp};
646baf5664fSJonas Devlieghere 
647d232abc3SJonas Devlieghere   return SBUnixSignals();
64898d0a4b3SChaoren Lin }
649ae211eceSMichal Gorny 
650ca69be21SWalter Erquinigo SBEnvironment SBPlatform::GetEnvironment() {
6511755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
652ca69be21SWalter Erquinigo   PlatformSP platform_sp(GetSP());
653ca69be21SWalter Erquinigo 
654ca69be21SWalter Erquinigo   if (platform_sp) {
655d232abc3SJonas Devlieghere     return SBEnvironment(platform_sp->GetEnvironment());
656ca69be21SWalter Erquinigo   }
657ca69be21SWalter Erquinigo 
658d232abc3SJonas Devlieghere   return SBEnvironment();
659ca69be21SWalter Erquinigo }
660