1fbb76349SGreg Clayton //===-- SBPlatform.cpp ------------------------------------------*- C++ -*-===//
2fbb76349SGreg Clayton //
3fbb76349SGreg Clayton //                     The LLVM Compiler Infrastructure
4fbb76349SGreg Clayton //
5fbb76349SGreg Clayton // This file is distributed under the University of Illinois Open Source
6fbb76349SGreg Clayton // License. See LICENSE.TXT for details.
7fbb76349SGreg Clayton //
8fbb76349SGreg Clayton //===----------------------------------------------------------------------===//
9fbb76349SGreg Clayton 
10fbb76349SGreg Clayton #include "lldb/API/SBPlatform.h"
11fbb76349SGreg Clayton #include "lldb/API/SBError.h"
12fbb76349SGreg Clayton #include "lldb/API/SBFileSpec.h"
131ef7b2c8SOleksiy Vyalov #include "lldb/API/SBLaunchInfo.h"
1498d0a4b3SChaoren Lin #include "lldb/API/SBUnixSignals.h"
15fbb76349SGreg Clayton #include "lldb/Core/ArchSpec.h"
16fbb76349SGreg Clayton #include "lldb/Core/Error.h"
17fbb76349SGreg Clayton #include "lldb/Host/File.h"
18fbb76349SGreg Clayton #include "lldb/Interpreter/Args.h"
19fbb76349SGreg Clayton #include "lldb/Target/Platform.h"
20b9c1b51eSKate Stone #include "lldb/Target/Target.h"
21fbb76349SGreg Clayton 
221ef7b2c8SOleksiy Vyalov #include <functional>
231ef7b2c8SOleksiy Vyalov 
24fbb76349SGreg Clayton using namespace lldb;
25fbb76349SGreg Clayton using namespace lldb_private;
26fbb76349SGreg Clayton 
27fbb76349SGreg Clayton //----------------------------------------------------------------------
28fbb76349SGreg Clayton // PlatformConnectOptions
29fbb76349SGreg Clayton //----------------------------------------------------------------------
30fbb76349SGreg Clayton struct PlatformConnectOptions {
31b9c1b51eSKate Stone   PlatformConnectOptions(const char *url = NULL)
32b9c1b51eSKate Stone       : m_url(), m_rsync_options(), m_rsync_remote_path_prefix(),
33b9c1b51eSKate Stone         m_rsync_enabled(false), m_rsync_omit_hostname_from_remote_path(false),
34b9c1b51eSKate Stone         m_local_cache_directory() {
35fbb76349SGreg Clayton     if (url && url[0])
36fbb76349SGreg Clayton       m_url = url;
37fbb76349SGreg Clayton   }
38fbb76349SGreg Clayton 
39b9c1b51eSKate Stone   ~PlatformConnectOptions() {}
40fbb76349SGreg Clayton 
41fbb76349SGreg Clayton   std::string m_url;
42fbb76349SGreg Clayton   std::string m_rsync_options;
43fbb76349SGreg Clayton   std::string m_rsync_remote_path_prefix;
44fbb76349SGreg Clayton   bool m_rsync_enabled;
45fbb76349SGreg Clayton   bool m_rsync_omit_hostname_from_remote_path;
46fbb76349SGreg Clayton   ConstString m_local_cache_directory;
47fbb76349SGreg Clayton };
48fbb76349SGreg Clayton 
49fbb76349SGreg Clayton //----------------------------------------------------------------------
50fbb76349SGreg Clayton // PlatformShellCommand
51fbb76349SGreg Clayton //----------------------------------------------------------------------
52fbb76349SGreg Clayton struct PlatformShellCommand {
53b9c1b51eSKate Stone   PlatformShellCommand(const char *shell_command = NULL)
54b9c1b51eSKate Stone       : m_command(), m_working_dir(), m_status(0), m_signo(0),
55b9c1b51eSKate Stone         m_timeout_sec(UINT32_MAX) {
56fbb76349SGreg Clayton     if (shell_command && shell_command[0])
57fbb76349SGreg Clayton       m_command = shell_command;
58fbb76349SGreg Clayton   }
59fbb76349SGreg Clayton 
60b9c1b51eSKate Stone   ~PlatformShellCommand() {}
61fbb76349SGreg Clayton 
62fbb76349SGreg Clayton   std::string m_command;
63fbb76349SGreg Clayton   std::string m_working_dir;
64fbb76349SGreg Clayton   std::string m_output;
65fbb76349SGreg Clayton   int m_status;
66fbb76349SGreg Clayton   int m_signo;
67fbb76349SGreg Clayton   uint32_t m_timeout_sec;
68fbb76349SGreg Clayton };
69fbb76349SGreg Clayton //----------------------------------------------------------------------
70fbb76349SGreg Clayton // SBPlatformConnectOptions
71fbb76349SGreg Clayton //----------------------------------------------------------------------
72b9c1b51eSKate Stone SBPlatformConnectOptions::SBPlatformConnectOptions(const char *url)
73b9c1b51eSKate Stone     : m_opaque_ptr(new PlatformConnectOptions(url)) {}
74fbb76349SGreg Clayton 
75b9c1b51eSKate Stone SBPlatformConnectOptions::SBPlatformConnectOptions(
76b9c1b51eSKate Stone     const SBPlatformConnectOptions &rhs)
77b9c1b51eSKate Stone     : m_opaque_ptr(new PlatformConnectOptions()) {
78fbb76349SGreg Clayton   *m_opaque_ptr = *rhs.m_opaque_ptr;
79fbb76349SGreg Clayton }
80fbb76349SGreg Clayton 
81b9c1b51eSKate Stone SBPlatformConnectOptions::~SBPlatformConnectOptions() { delete m_opaque_ptr; }
82fbb76349SGreg Clayton 
83b9c1b51eSKate Stone void SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs) {
84fbb76349SGreg Clayton   *m_opaque_ptr = *rhs.m_opaque_ptr;
85fbb76349SGreg Clayton }
86fbb76349SGreg Clayton 
87b9c1b51eSKate Stone const char *SBPlatformConnectOptions::GetURL() {
88fbb76349SGreg Clayton   if (m_opaque_ptr->m_url.empty())
89fbb76349SGreg Clayton     return NULL;
90fbb76349SGreg Clayton   return m_opaque_ptr->m_url.c_str();
91fbb76349SGreg Clayton }
92fbb76349SGreg Clayton 
93b9c1b51eSKate Stone void SBPlatformConnectOptions::SetURL(const char *url) {
94fbb76349SGreg Clayton   if (url && url[0])
95fbb76349SGreg Clayton     m_opaque_ptr->m_url = url;
96fbb76349SGreg Clayton   else
97fbb76349SGreg Clayton     m_opaque_ptr->m_url.clear();
98fbb76349SGreg Clayton }
99fbb76349SGreg Clayton 
100b9c1b51eSKate Stone bool SBPlatformConnectOptions::GetRsyncEnabled() {
101fbb76349SGreg Clayton   return m_opaque_ptr->m_rsync_enabled;
102fbb76349SGreg Clayton }
103fbb76349SGreg Clayton 
104b9c1b51eSKate Stone void SBPlatformConnectOptions::EnableRsync(
105b9c1b51eSKate Stone     const char *options, const char *remote_path_prefix,
106b9c1b51eSKate Stone     bool omit_hostname_from_remote_path) {
107fbb76349SGreg Clayton   m_opaque_ptr->m_rsync_enabled = true;
108b9c1b51eSKate Stone   m_opaque_ptr->m_rsync_omit_hostname_from_remote_path =
109b9c1b51eSKate Stone       omit_hostname_from_remote_path;
110fbb76349SGreg Clayton   if (remote_path_prefix && remote_path_prefix[0])
111fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix;
112fbb76349SGreg Clayton   else
113fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_remote_path_prefix.clear();
114fbb76349SGreg Clayton 
115fbb76349SGreg Clayton   if (options && options[0])
116fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_options = options;
117fbb76349SGreg Clayton   else
118fbb76349SGreg Clayton     m_opaque_ptr->m_rsync_options.clear();
119fbb76349SGreg Clayton }
120fbb76349SGreg Clayton 
121b9c1b51eSKate Stone void SBPlatformConnectOptions::DisableRsync() {
122fbb76349SGreg Clayton   m_opaque_ptr->m_rsync_enabled = false;
123fbb76349SGreg Clayton }
124fbb76349SGreg Clayton 
125b9c1b51eSKate Stone const char *SBPlatformConnectOptions::GetLocalCacheDirectory() {
126fbb76349SGreg Clayton   return m_opaque_ptr->m_local_cache_directory.GetCString();
127fbb76349SGreg Clayton }
128fbb76349SGreg Clayton 
129b9c1b51eSKate Stone void SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path) {
130fbb76349SGreg Clayton   if (path && path[0])
131fbb76349SGreg Clayton     m_opaque_ptr->m_local_cache_directory.SetCString(path);
132fbb76349SGreg Clayton   else
133fbb76349SGreg Clayton     m_opaque_ptr->m_local_cache_directory = ConstString();
134fbb76349SGreg Clayton }
135fbb76349SGreg Clayton 
136fbb76349SGreg Clayton //----------------------------------------------------------------------
137fbb76349SGreg Clayton // SBPlatformShellCommand
138fbb76349SGreg Clayton //----------------------------------------------------------------------
139b9c1b51eSKate Stone SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_command)
140b9c1b51eSKate Stone     : m_opaque_ptr(new PlatformShellCommand(shell_command)) {}
141fbb76349SGreg Clayton 
142b9c1b51eSKate Stone SBPlatformShellCommand::SBPlatformShellCommand(
143b9c1b51eSKate Stone     const SBPlatformShellCommand &rhs)
144b9c1b51eSKate Stone     : m_opaque_ptr(new PlatformShellCommand()) {
145fbb76349SGreg Clayton   *m_opaque_ptr = *rhs.m_opaque_ptr;
146fbb76349SGreg Clayton }
147fbb76349SGreg Clayton 
148b9c1b51eSKate Stone SBPlatformShellCommand::~SBPlatformShellCommand() { delete m_opaque_ptr; }
149fbb76349SGreg Clayton 
150b9c1b51eSKate Stone void SBPlatformShellCommand::Clear() {
1513a29f8b9SPavel Labath   m_opaque_ptr->m_output = std::string();
152fbb76349SGreg Clayton   m_opaque_ptr->m_status = 0;
153fbb76349SGreg Clayton   m_opaque_ptr->m_signo = 0;
154fbb76349SGreg Clayton }
155fbb76349SGreg Clayton 
156b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetCommand() {
157fbb76349SGreg Clayton   if (m_opaque_ptr->m_command.empty())
158fbb76349SGreg Clayton     return NULL;
159fbb76349SGreg Clayton   return m_opaque_ptr->m_command.c_str();
160fbb76349SGreg Clayton }
161fbb76349SGreg Clayton 
162b9c1b51eSKate Stone void SBPlatformShellCommand::SetCommand(const char *shell_command) {
163fbb76349SGreg Clayton   if (shell_command && shell_command[0])
164fbb76349SGreg Clayton     m_opaque_ptr->m_command = shell_command;
165fbb76349SGreg Clayton   else
166fbb76349SGreg Clayton     m_opaque_ptr->m_command.clear();
167fbb76349SGreg Clayton }
168fbb76349SGreg Clayton 
169b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetWorkingDirectory() {
170fbb76349SGreg Clayton   if (m_opaque_ptr->m_working_dir.empty())
171fbb76349SGreg Clayton     return NULL;
172fbb76349SGreg Clayton   return m_opaque_ptr->m_working_dir.c_str();
173fbb76349SGreg Clayton }
174fbb76349SGreg Clayton 
175b9c1b51eSKate Stone void SBPlatformShellCommand::SetWorkingDirectory(const char *path) {
176fbb76349SGreg Clayton   if (path && path[0])
177fbb76349SGreg Clayton     m_opaque_ptr->m_working_dir = path;
178fbb76349SGreg Clayton   else
179fbb76349SGreg Clayton     m_opaque_ptr->m_working_dir.clear();
180fbb76349SGreg Clayton }
181fbb76349SGreg Clayton 
182b9c1b51eSKate Stone uint32_t SBPlatformShellCommand::GetTimeoutSeconds() {
183fbb76349SGreg Clayton   return m_opaque_ptr->m_timeout_sec;
184fbb76349SGreg Clayton }
185fbb76349SGreg Clayton 
186b9c1b51eSKate Stone void SBPlatformShellCommand::SetTimeoutSeconds(uint32_t sec) {
187fbb76349SGreg Clayton   m_opaque_ptr->m_timeout_sec = sec;
188fbb76349SGreg Clayton }
189fbb76349SGreg Clayton 
190b9c1b51eSKate Stone int SBPlatformShellCommand::GetSignal() { return m_opaque_ptr->m_signo; }
191fbb76349SGreg Clayton 
192b9c1b51eSKate Stone int SBPlatformShellCommand::GetStatus() { return m_opaque_ptr->m_status; }
193fbb76349SGreg Clayton 
194b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetOutput() {
195fbb76349SGreg Clayton   if (m_opaque_ptr->m_output.empty())
196fbb76349SGreg Clayton     return NULL;
197fbb76349SGreg Clayton   return m_opaque_ptr->m_output.c_str();
198fbb76349SGreg Clayton }
199fbb76349SGreg Clayton 
200fbb76349SGreg Clayton //----------------------------------------------------------------------
201fbb76349SGreg Clayton // SBPlatform
202fbb76349SGreg Clayton //----------------------------------------------------------------------
203b9c1b51eSKate Stone SBPlatform::SBPlatform() : m_opaque_sp() {}
204fbb76349SGreg Clayton 
205b9c1b51eSKate Stone SBPlatform::SBPlatform(const char *platform_name) : m_opaque_sp() {
206fbb76349SGreg Clayton   Error error;
207615eb7e6SGreg Clayton   if (platform_name && platform_name[0])
208615eb7e6SGreg Clayton     m_opaque_sp = Platform::Create(ConstString(platform_name), error);
209fbb76349SGreg Clayton }
210fbb76349SGreg Clayton 
211b9c1b51eSKate Stone SBPlatform::~SBPlatform() {}
212fbb76349SGreg Clayton 
213b9c1b51eSKate Stone bool SBPlatform::IsValid() const { return m_opaque_sp.get() != NULL; }
214fbb76349SGreg Clayton 
215b9c1b51eSKate Stone void SBPlatform::Clear() { m_opaque_sp.reset(); }
216fbb76349SGreg Clayton 
217b9c1b51eSKate Stone const char *SBPlatform::GetName() {
218fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
219fbb76349SGreg Clayton   if (platform_sp)
220fbb76349SGreg Clayton     return platform_sp->GetName().GetCString();
221fbb76349SGreg Clayton   return NULL;
222fbb76349SGreg Clayton }
223fbb76349SGreg Clayton 
224b9c1b51eSKate Stone lldb::PlatformSP SBPlatform::GetSP() const { return m_opaque_sp; }
225fbb76349SGreg Clayton 
226b9c1b51eSKate Stone void SBPlatform::SetSP(const lldb::PlatformSP &platform_sp) {
227fbb76349SGreg Clayton   m_opaque_sp = platform_sp;
228fbb76349SGreg Clayton }
229fbb76349SGreg Clayton 
230b9c1b51eSKate Stone const char *SBPlatform::GetWorkingDirectory() {
231fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
232fbb76349SGreg Clayton   if (platform_sp)
233fbb76349SGreg Clayton     return platform_sp->GetWorkingDirectory().GetCString();
234fbb76349SGreg Clayton   return NULL;
235fbb76349SGreg Clayton }
236fbb76349SGreg Clayton 
237b9c1b51eSKate Stone bool SBPlatform::SetWorkingDirectory(const char *path) {
238fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
239b9c1b51eSKate Stone   if (platform_sp) {
240fbb76349SGreg Clayton     if (path)
241d3173f34SChaoren Lin       platform_sp->SetWorkingDirectory(FileSpec{path, false});
242fbb76349SGreg Clayton     else
243d3173f34SChaoren Lin       platform_sp->SetWorkingDirectory(FileSpec{});
244fbb76349SGreg Clayton     return true;
245fbb76349SGreg Clayton   }
246fbb76349SGreg Clayton   return false;
247fbb76349SGreg Clayton }
248fbb76349SGreg Clayton 
249b9c1b51eSKate Stone SBError SBPlatform::ConnectRemote(SBPlatformConnectOptions &connect_options) {
250fbb76349SGreg Clayton   SBError sb_error;
251fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
252b9c1b51eSKate Stone   if (platform_sp && connect_options.GetURL()) {
253fbb76349SGreg Clayton     Args args;
254*ecbb0bb1SZachary Turner     args.AppendArgument(
255*ecbb0bb1SZachary Turner         llvm::StringRef::withNullAsEmpty(connect_options.GetURL()));
256fbb76349SGreg Clayton     sb_error.ref() = platform_sp->ConnectRemote(args);
257b9c1b51eSKate Stone   } else {
258fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
259fbb76349SGreg Clayton   }
260fbb76349SGreg Clayton   return sb_error;
261fbb76349SGreg Clayton }
262fbb76349SGreg Clayton 
263b9c1b51eSKate Stone void SBPlatform::DisconnectRemote() {
264fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
265fbb76349SGreg Clayton   if (platform_sp)
266fbb76349SGreg Clayton     platform_sp->DisconnectRemote();
267fbb76349SGreg Clayton }
268fbb76349SGreg Clayton 
269b9c1b51eSKate Stone bool SBPlatform::IsConnected() {
270fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
271fbb76349SGreg Clayton   if (platform_sp)
272fbb76349SGreg Clayton     platform_sp->IsConnected();
273fbb76349SGreg Clayton   return false;
274fbb76349SGreg Clayton }
275fbb76349SGreg Clayton 
276b9c1b51eSKate Stone const char *SBPlatform::GetTriple() {
277fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
278b9c1b51eSKate Stone   if (platform_sp) {
279ccd28a14STamas Berghammer     ArchSpec arch(platform_sp->GetSystemArchitecture());
280b9c1b51eSKate Stone     if (arch.IsValid()) {
281b9c1b51eSKate Stone       // Const-ify the string so we don't need to worry about the lifetime of
282b9c1b51eSKate Stone       // the string
283fbb76349SGreg Clayton       return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
284fbb76349SGreg Clayton     }
285fbb76349SGreg Clayton   }
286fbb76349SGreg Clayton   return NULL;
287fbb76349SGreg Clayton }
288fbb76349SGreg Clayton 
289b9c1b51eSKate Stone const char *SBPlatform::GetOSBuild() {
290fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
291b9c1b51eSKate Stone   if (platform_sp) {
292fbb76349SGreg Clayton     std::string s;
293b9c1b51eSKate Stone     if (platform_sp->GetOSBuildString(s)) {
294b9c1b51eSKate Stone       if (!s.empty()) {
295b9c1b51eSKate Stone         // Const-ify the string so we don't need to worry about the lifetime of
296b9c1b51eSKate Stone         // the string
297fbb76349SGreg Clayton         return ConstString(s.c_str()).GetCString();
298fbb76349SGreg Clayton       }
299fbb76349SGreg Clayton     }
300fbb76349SGreg Clayton   }
301fbb76349SGreg Clayton   return NULL;
302fbb76349SGreg Clayton }
303fbb76349SGreg Clayton 
304b9c1b51eSKate Stone const char *SBPlatform::GetOSDescription() {
305fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
306b9c1b51eSKate Stone   if (platform_sp) {
307fbb76349SGreg Clayton     std::string s;
308b9c1b51eSKate Stone     if (platform_sp->GetOSKernelDescription(s)) {
309b9c1b51eSKate Stone       if (!s.empty()) {
310b9c1b51eSKate Stone         // Const-ify the string so we don't need to worry about the lifetime of
311b9c1b51eSKate Stone         // the string
312fbb76349SGreg Clayton         return ConstString(s.c_str()).GetCString();
313fbb76349SGreg Clayton       }
314fbb76349SGreg Clayton     }
315fbb76349SGreg Clayton   }
316fbb76349SGreg Clayton   return NULL;
317fbb76349SGreg Clayton }
318fbb76349SGreg Clayton 
319b9c1b51eSKate Stone const char *SBPlatform::GetHostname() {
320fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
321fbb76349SGreg Clayton   if (platform_sp)
322fbb76349SGreg Clayton     return platform_sp->GetHostname();
323fbb76349SGreg Clayton   return NULL;
324fbb76349SGreg Clayton }
325fbb76349SGreg Clayton 
326b9c1b51eSKate Stone uint32_t SBPlatform::GetOSMajorVersion() {
327fbb76349SGreg Clayton   uint32_t major, minor, update;
328fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
329fbb76349SGreg Clayton   if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
330fbb76349SGreg Clayton     return major;
331fbb76349SGreg Clayton   return UINT32_MAX;
332fbb76349SGreg Clayton }
333fbb76349SGreg Clayton 
334b9c1b51eSKate Stone uint32_t SBPlatform::GetOSMinorVersion() {
335fbb76349SGreg Clayton   uint32_t major, minor, update;
336fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
337fbb76349SGreg Clayton   if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
338fbb76349SGreg Clayton     return minor;
339fbb76349SGreg Clayton   return UINT32_MAX;
340fbb76349SGreg Clayton }
341fbb76349SGreg Clayton 
342b9c1b51eSKate Stone uint32_t SBPlatform::GetOSUpdateVersion() {
343fbb76349SGreg Clayton   uint32_t major, minor, update;
344fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
345fbb76349SGreg Clayton   if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
346fbb76349SGreg Clayton     return update;
347fbb76349SGreg Clayton   return UINT32_MAX;
348fbb76349SGreg Clayton }
349fbb76349SGreg Clayton 
350b9c1b51eSKate Stone SBError SBPlatform::Get(SBFileSpec &src, SBFileSpec &dst) {
351fbb76349SGreg Clayton   SBError sb_error;
352fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
353b9c1b51eSKate Stone   if (platform_sp) {
354fbb76349SGreg Clayton     sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
355b9c1b51eSKate Stone   } else {
356fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
357fbb76349SGreg Clayton   }
358fbb76349SGreg Clayton   return sb_error;
359fbb76349SGreg Clayton }
360fbb76349SGreg Clayton 
361b9c1b51eSKate Stone SBError SBPlatform::Put(SBFileSpec &src, SBFileSpec &dst) {
362b9c1b51eSKate Stone   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
363b9c1b51eSKate Stone     if (src.Exists()) {
364fbb76349SGreg Clayton       uint32_t permissions = src.ref().GetPermissions();
365b9c1b51eSKate Stone       if (permissions == 0) {
366fbb76349SGreg Clayton         if (src.ref().GetFileType() == FileSpec::eFileTypeDirectory)
367fbb76349SGreg Clayton           permissions = eFilePermissionsDirectoryDefault;
368fbb76349SGreg Clayton         else
369fbb76349SGreg Clayton           permissions = eFilePermissionsFileDefault;
370fbb76349SGreg Clayton       }
371fbb76349SGreg Clayton 
3721ef7b2c8SOleksiy Vyalov       return platform_sp->PutFile(src.ref(), dst.ref(), permissions);
373fbb76349SGreg Clayton     }
3741ef7b2c8SOleksiy Vyalov 
3751ef7b2c8SOleksiy Vyalov     Error error;
376b9c1b51eSKate Stone     error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
377b9c1b51eSKate Stone                                    src.ref().GetPath().c_str());
3781ef7b2c8SOleksiy Vyalov     return error;
3791ef7b2c8SOleksiy Vyalov   });
380fbb76349SGreg Clayton }
381fbb76349SGreg Clayton 
382b9c1b51eSKate Stone SBError SBPlatform::Install(SBFileSpec &src, SBFileSpec &dst) {
383b9c1b51eSKate Stone   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
384fbb76349SGreg Clayton     if (src.Exists())
3851ef7b2c8SOleksiy Vyalov       return platform_sp->Install(src.ref(), dst.ref());
3861ef7b2c8SOleksiy Vyalov 
3871ef7b2c8SOleksiy Vyalov     Error error;
388b9c1b51eSKate Stone     error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'",
389b9c1b51eSKate Stone                                    src.ref().GetPath().c_str());
3901ef7b2c8SOleksiy Vyalov     return error;
3911ef7b2c8SOleksiy Vyalov   });
392fbb76349SGreg Clayton }
393fbb76349SGreg Clayton 
394b9c1b51eSKate Stone SBError SBPlatform::Run(SBPlatformShellCommand &shell_command) {
395b9c1b51eSKate Stone   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
396fbb76349SGreg Clayton     const char *command = shell_command.GetCommand();
3971ef7b2c8SOleksiy Vyalov     if (!command)
3981ef7b2c8SOleksiy Vyalov       return Error("invalid shell command (empty)");
3991ef7b2c8SOleksiy Vyalov 
400fbb76349SGreg Clayton     const char *working_dir = shell_command.GetWorkingDirectory();
401b9c1b51eSKate Stone     if (working_dir == NULL) {
402fbb76349SGreg Clayton       working_dir = platform_sp->GetWorkingDirectory().GetCString();
403fbb76349SGreg Clayton       if (working_dir)
404fbb76349SGreg Clayton         shell_command.SetWorkingDirectory(working_dir);
405fbb76349SGreg Clayton     }
406b9c1b51eSKate Stone     return platform_sp->RunShellCommand(
407b9c1b51eSKate Stone         command, FileSpec{working_dir, false},
408fbb76349SGreg Clayton         &shell_command.m_opaque_ptr->m_status,
409fbb76349SGreg Clayton         &shell_command.m_opaque_ptr->m_signo,
410fbb76349SGreg Clayton         &shell_command.m_opaque_ptr->m_output,
411fbb76349SGreg Clayton         shell_command.m_opaque_ptr->m_timeout_sec);
4121ef7b2c8SOleksiy Vyalov   });
413fbb76349SGreg Clayton }
4141ef7b2c8SOleksiy Vyalov 
415b9c1b51eSKate Stone SBError SBPlatform::Launch(SBLaunchInfo &launch_info) {
416b9c1b51eSKate Stone   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
4171ef7b2c8SOleksiy Vyalov     return platform_sp->LaunchProcess(launch_info.ref());
4181ef7b2c8SOleksiy Vyalov   });
4191ef7b2c8SOleksiy Vyalov }
4201ef7b2c8SOleksiy Vyalov 
421b9c1b51eSKate Stone SBError SBPlatform::Kill(const lldb::pid_t pid) {
422b9c1b51eSKate Stone   return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) {
4231ef7b2c8SOleksiy Vyalov     return platform_sp->KillProcess(pid);
4241ef7b2c8SOleksiy Vyalov   });
4251ef7b2c8SOleksiy Vyalov }
4261ef7b2c8SOleksiy Vyalov 
427b9c1b51eSKate Stone SBError SBPlatform::ExecuteConnected(
428b9c1b51eSKate Stone     const std::function<Error(const lldb::PlatformSP &)> &func) {
4291ef7b2c8SOleksiy Vyalov   SBError sb_error;
4301ef7b2c8SOleksiy Vyalov   const auto platform_sp(GetSP());
431b9c1b51eSKate Stone   if (platform_sp) {
4321ef7b2c8SOleksiy Vyalov     if (platform_sp->IsConnected())
4331ef7b2c8SOleksiy Vyalov       sb_error.ref() = func(platform_sp);
4341ef7b2c8SOleksiy Vyalov     else
435fbb76349SGreg Clayton       sb_error.SetErrorString("not connected");
436b9c1b51eSKate Stone   } else
437fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
4381ef7b2c8SOleksiy Vyalov 
439fbb76349SGreg Clayton   return sb_error;
440fbb76349SGreg Clayton }
441fbb76349SGreg Clayton 
442b9c1b51eSKate Stone SBError SBPlatform::MakeDirectory(const char *path, uint32_t file_permissions) {
443fbb76349SGreg Clayton   SBError sb_error;
444fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
445b9c1b51eSKate Stone   if (platform_sp) {
446b9c1b51eSKate Stone     sb_error.ref() =
447b9c1b51eSKate Stone         platform_sp->MakeDirectory(FileSpec{path, false}, file_permissions);
448b9c1b51eSKate Stone   } else {
449fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
450fbb76349SGreg Clayton   }
451fbb76349SGreg Clayton   return sb_error;
452fbb76349SGreg Clayton }
453fbb76349SGreg Clayton 
454b9c1b51eSKate Stone uint32_t SBPlatform::GetFilePermissions(const char *path) {
455fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
456b9c1b51eSKate Stone   if (platform_sp) {
457fbb76349SGreg Clayton     uint32_t file_permissions = 0;
458d3173f34SChaoren Lin     platform_sp->GetFilePermissions(FileSpec{path, false}, file_permissions);
459fbb76349SGreg Clayton     return file_permissions;
460fbb76349SGreg Clayton   }
461fbb76349SGreg Clayton   return 0;
462fbb76349SGreg Clayton }
463fbb76349SGreg Clayton 
464b9c1b51eSKate Stone SBError SBPlatform::SetFilePermissions(const char *path,
465b9c1b51eSKate Stone                                        uint32_t file_permissions) {
466fbb76349SGreg Clayton   SBError sb_error;
467fbb76349SGreg Clayton   PlatformSP platform_sp(GetSP());
468b9c1b51eSKate Stone   if (platform_sp) {
469b9c1b51eSKate Stone     sb_error.ref() = platform_sp->SetFilePermissions(FileSpec{path, false},
470b9c1b51eSKate Stone                                                      file_permissions);
471b9c1b51eSKate Stone   } else {
472fbb76349SGreg Clayton     sb_error.SetErrorString("invalid platform");
473fbb76349SGreg Clayton   }
474fbb76349SGreg Clayton   return sb_error;
475fbb76349SGreg Clayton }
476fbb76349SGreg Clayton 
477b9c1b51eSKate Stone SBUnixSignals SBPlatform::GetUnixSignals() const {
47898d0a4b3SChaoren Lin   if (auto platform_sp = GetSP())
47998d0a4b3SChaoren Lin     return SBUnixSignals{platform_sp};
48098d0a4b3SChaoren Lin 
48198d0a4b3SChaoren Lin   return {};
48298d0a4b3SChaoren Lin }
483