1 //===-- SBPlatform.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBPlatform.h"
11 #include "lldb/API/SBError.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBLaunchInfo.h"
14 #include "lldb/Core/ArchSpec.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Host/File.h"
17 #include "lldb/Interpreter/Args.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/Platform.h"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------
27 // PlatformConnectOptions
28 //----------------------------------------------------------------------
29 struct PlatformConnectOptions {
30 PlatformConnectOptions(const char *url = NULL) :
33 m_rsync_remote_path_prefix(),
34 m_rsync_enabled(false),
35 m_rsync_omit_hostname_from_remote_path(false),
36 m_local_cache_directory ()
42 ~PlatformConnectOptions()
47 std::string m_rsync_options;
48 std::string m_rsync_remote_path_prefix;
50 bool m_rsync_omit_hostname_from_remote_path;
51 ConstString m_local_cache_directory;
54 //----------------------------------------------------------------------
55 // PlatformShellCommand
56 //----------------------------------------------------------------------
57 struct PlatformShellCommand {
58 PlatformShellCommand(const char *shell_command = NULL) :
63 m_timeout_sec(UINT32_MAX)
65 if (shell_command && shell_command[0])
66 m_command = shell_command;
69 ~PlatformShellCommand()
73 std::string m_command;
74 std::string m_working_dir;
78 uint32_t m_timeout_sec;
80 //----------------------------------------------------------------------
81 // SBPlatformConnectOptions
82 //----------------------------------------------------------------------
83 SBPlatformConnectOptions::SBPlatformConnectOptions (const char *url) :
84 m_opaque_ptr(new PlatformConnectOptions(url))
89 SBPlatformConnectOptions::SBPlatformConnectOptions(const SBPlatformConnectOptions &rhs) :
90 m_opaque_ptr(new PlatformConnectOptions())
92 *m_opaque_ptr = *rhs.m_opaque_ptr;
95 SBPlatformConnectOptions::~SBPlatformConnectOptions ()
101 SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs)
103 *m_opaque_ptr = *rhs.m_opaque_ptr;
107 SBPlatformConnectOptions::GetURL()
109 if (m_opaque_ptr->m_url.empty())
111 return m_opaque_ptr->m_url.c_str();
115 SBPlatformConnectOptions::SetURL(const char *url)
118 m_opaque_ptr->m_url = url;
120 m_opaque_ptr->m_url.clear();
124 SBPlatformConnectOptions::GetRsyncEnabled()
126 return m_opaque_ptr->m_rsync_enabled;
130 SBPlatformConnectOptions::EnableRsync (const char *options,
131 const char *remote_path_prefix,
132 bool omit_hostname_from_remote_path)
134 m_opaque_ptr->m_rsync_enabled = true;
135 m_opaque_ptr->m_rsync_omit_hostname_from_remote_path = omit_hostname_from_remote_path;
136 if (remote_path_prefix && remote_path_prefix[0])
137 m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix;
139 m_opaque_ptr->m_rsync_remote_path_prefix.clear();
141 if (options && options[0])
142 m_opaque_ptr->m_rsync_options = options;
144 m_opaque_ptr->m_rsync_options.clear();
149 SBPlatformConnectOptions::DisableRsync ()
151 m_opaque_ptr->m_rsync_enabled = false;
155 SBPlatformConnectOptions::GetLocalCacheDirectory()
157 return m_opaque_ptr->m_local_cache_directory.GetCString();
161 SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path)
164 m_opaque_ptr->m_local_cache_directory.SetCString(path);
166 m_opaque_ptr->m_local_cache_directory = ConstString();
169 //----------------------------------------------------------------------
170 // SBPlatformShellCommand
171 //----------------------------------------------------------------------
172 SBPlatformShellCommand::SBPlatformShellCommand (const char *shell_command) :
173 m_opaque_ptr(new PlatformShellCommand(shell_command))
177 SBPlatformShellCommand::SBPlatformShellCommand (const SBPlatformShellCommand &rhs) :
178 m_opaque_ptr(new PlatformShellCommand())
180 *m_opaque_ptr = *rhs.m_opaque_ptr;
183 SBPlatformShellCommand::~SBPlatformShellCommand()
189 SBPlatformShellCommand::Clear()
191 m_opaque_ptr->m_output = std::move(std::string());
192 m_opaque_ptr->m_status = 0;
193 m_opaque_ptr->m_signo = 0;
197 SBPlatformShellCommand::GetCommand()
199 if (m_opaque_ptr->m_command.empty())
201 return m_opaque_ptr->m_command.c_str();
205 SBPlatformShellCommand::SetCommand(const char *shell_command)
207 if (shell_command && shell_command[0])
208 m_opaque_ptr->m_command = shell_command;
210 m_opaque_ptr->m_command.clear();
214 SBPlatformShellCommand::GetWorkingDirectory ()
216 if (m_opaque_ptr->m_working_dir.empty())
218 return m_opaque_ptr->m_working_dir.c_str();
222 SBPlatformShellCommand::SetWorkingDirectory (const char *path)
225 m_opaque_ptr->m_working_dir = path;
227 m_opaque_ptr->m_working_dir.clear();
231 SBPlatformShellCommand::GetTimeoutSeconds ()
233 return m_opaque_ptr->m_timeout_sec;
237 SBPlatformShellCommand::SetTimeoutSeconds (uint32_t sec)
239 m_opaque_ptr->m_timeout_sec = sec;
243 SBPlatformShellCommand::GetSignal ()
245 return m_opaque_ptr->m_signo;
249 SBPlatformShellCommand::GetStatus ()
251 return m_opaque_ptr->m_status;
255 SBPlatformShellCommand::GetOutput ()
257 if (m_opaque_ptr->m_output.empty())
259 return m_opaque_ptr->m_output.c_str();
262 //----------------------------------------------------------------------
264 //----------------------------------------------------------------------
265 SBPlatform::SBPlatform () :
271 SBPlatform::SBPlatform (const char *platform_name) :
275 if (platform_name && platform_name[0])
276 m_opaque_sp = Platform::Create (ConstString(platform_name), error);
279 SBPlatform::~SBPlatform()
284 SBPlatform::IsValid () const
286 return m_opaque_sp.get() != NULL;
296 SBPlatform::GetName ()
298 PlatformSP platform_sp(GetSP());
300 return platform_sp->GetName().GetCString();
305 SBPlatform::GetSP () const
311 SBPlatform::SetSP (const lldb::PlatformSP& platform_sp)
313 m_opaque_sp = platform_sp;
317 SBPlatform::GetWorkingDirectory()
319 PlatformSP platform_sp(GetSP());
321 return platform_sp->GetWorkingDirectory().GetCString();
326 SBPlatform::SetWorkingDirectory(const char *path)
328 PlatformSP platform_sp(GetSP());
332 platform_sp->SetWorkingDirectory(FileSpec{path, false});
334 platform_sp->SetWorkingDirectory(FileSpec{});
341 SBPlatform::ConnectRemote (SBPlatformConnectOptions &connect_options)
344 PlatformSP platform_sp(GetSP());
345 if (platform_sp && connect_options.GetURL())
348 args.AppendArgument(connect_options.GetURL());
349 sb_error.ref() = platform_sp->ConnectRemote(args);
353 sb_error.SetErrorString("invalid platform");
359 SBPlatform::DisconnectRemote ()
361 PlatformSP platform_sp(GetSP());
363 platform_sp->DisconnectRemote();
367 SBPlatform::IsConnected()
369 PlatformSP platform_sp(GetSP());
371 platform_sp->IsConnected();
376 SBPlatform::GetTriple()
378 PlatformSP platform_sp(GetSP());
381 ArchSpec arch(platform_sp->GetSystemArchitecture());
384 // Const-ify the string so we don't need to worry about the lifetime of the string
385 return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
392 SBPlatform::GetOSBuild()
394 PlatformSP platform_sp(GetSP());
398 if (platform_sp->GetOSBuildString(s))
402 // Const-ify the string so we don't need to worry about the lifetime of the string
403 return ConstString(s.c_str()).GetCString();
411 SBPlatform::GetOSDescription()
413 PlatformSP platform_sp(GetSP());
417 if (platform_sp->GetOSKernelDescription(s))
421 // Const-ify the string so we don't need to worry about the lifetime of the string
422 return ConstString(s.c_str()).GetCString();
430 SBPlatform::GetHostname ()
432 PlatformSP platform_sp(GetSP());
434 return platform_sp->GetHostname();
439 SBPlatform::GetOSMajorVersion ()
441 uint32_t major, minor, update;
442 PlatformSP platform_sp(GetSP());
443 if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
450 SBPlatform::GetOSMinorVersion ()
452 uint32_t major, minor, update;
453 PlatformSP platform_sp(GetSP());
454 if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
460 SBPlatform::GetOSUpdateVersion ()
462 uint32_t major, minor, update;
463 PlatformSP platform_sp(GetSP());
464 if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
470 SBPlatform::Get (SBFileSpec &src,
474 PlatformSP platform_sp(GetSP());
477 sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
481 sb_error.SetErrorString("invalid platform");
487 SBPlatform::Put (SBFileSpec &src,
490 return ExecuteConnected(
491 [&](const lldb::PlatformSP& platform_sp)
495 uint32_t permissions = src.ref().GetPermissions();
496 if (permissions == 0)
498 if (src.ref().GetFileType() == FileSpec::eFileTypeDirectory)
499 permissions = eFilePermissionsDirectoryDefault;
501 permissions = eFilePermissionsFileDefault;
504 return platform_sp->PutFile(src.ref(), dst.ref(), permissions);
508 error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", src.ref().GetPath().c_str());
514 SBPlatform::Install (SBFileSpec &src,
517 return ExecuteConnected(
518 [&](const lldb::PlatformSP& platform_sp)
521 return platform_sp->Install(src.ref(), dst.ref());
524 error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", src.ref().GetPath().c_str());
531 SBPlatform::Run (SBPlatformShellCommand &shell_command)
533 return ExecuteConnected(
534 [&](const lldb::PlatformSP& platform_sp)
536 const char *command = shell_command.GetCommand();
538 return Error("invalid shell command (empty)");
540 const char *working_dir = shell_command.GetWorkingDirectory();
541 if (working_dir == NULL)
543 working_dir = platform_sp->GetWorkingDirectory().GetCString();
545 shell_command.SetWorkingDirectory(working_dir);
547 return platform_sp->RunShellCommand(command,
548 FileSpec{working_dir, false},
549 &shell_command.m_opaque_ptr->m_status,
550 &shell_command.m_opaque_ptr->m_signo,
551 &shell_command.m_opaque_ptr->m_output,
552 shell_command.m_opaque_ptr->m_timeout_sec);
557 SBPlatform::Launch (SBLaunchInfo &launch_info)
559 return ExecuteConnected(
560 [&](const lldb::PlatformSP& platform_sp)
562 return platform_sp->LaunchProcess(launch_info.ref());
567 SBPlatform::Kill (const lldb::pid_t pid)
569 return ExecuteConnected(
570 [&](const lldb::PlatformSP& platform_sp)
572 return platform_sp->KillProcess(pid);
577 SBPlatform::ExecuteConnected (const std::function<Error(const lldb::PlatformSP&)>& func)
580 const auto platform_sp(GetSP());
583 if (platform_sp->IsConnected())
584 sb_error.ref() = func(platform_sp);
586 sb_error.SetErrorString("not connected");
589 sb_error.SetErrorString("invalid platform");
595 SBPlatform::MakeDirectory (const char *path, uint32_t file_permissions)
598 PlatformSP platform_sp(GetSP());
601 sb_error.ref() = platform_sp->MakeDirectory(FileSpec{path, false}, file_permissions);
605 sb_error.SetErrorString("invalid platform");
611 SBPlatform::GetFilePermissions (const char *path)
613 PlatformSP platform_sp(GetSP());
616 uint32_t file_permissions = 0;
617 platform_sp->GetFilePermissions(FileSpec{path, false}, file_permissions);
618 return file_permissions;
625 SBPlatform::SetFilePermissions (const char *path, uint32_t file_permissions)
628 PlatformSP platform_sp(GetSP());
631 sb_error.ref() = platform_sp->SetFilePermissions(FileSpec{path, false}, file_permissions);
635 sb_error.SetErrorString("invalid platform");