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" 10d51402acSJonas Devlieghere #include "lldb/Utility/ReproducerInstrumentation.h" 11ca69be21SWalter Erquinigo #include "lldb/API/SBEnvironment.h" 12fbb76349SGreg Clayton #include "lldb/API/SBError.h" 13fbb76349SGreg Clayton #include "lldb/API/SBFileSpec.h" 141ef7b2c8SOleksiy Vyalov #include "lldb/API/SBLaunchInfo.h" 15ca69be21SWalter Erquinigo #include "lldb/API/SBPlatform.h" 1698d0a4b3SChaoren Lin #include "lldb/API/SBUnixSignals.h" 17fbb76349SGreg Clayton #include "lldb/Host/File.h" 18fbb76349SGreg Clayton #include "lldb/Target/Platform.h" 19b9c1b51eSKate Stone #include "lldb/Target/Target.h" 205f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h" 21145d95c9SPavel Labath #include "lldb/Utility/Args.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)) { 78baf5664fSJonas Devlieghere LLDB_RECORD_CONSTRUCTOR(SBPlatformConnectOptions, (const char *), url); 79baf5664fSJonas Devlieghere } 80fbb76349SGreg Clayton 81b9c1b51eSKate Stone SBPlatformConnectOptions::SBPlatformConnectOptions( 82b9c1b51eSKate Stone const SBPlatformConnectOptions &rhs) 83b9c1b51eSKate Stone : m_opaque_ptr(new PlatformConnectOptions()) { 84baf5664fSJonas Devlieghere LLDB_RECORD_CONSTRUCTOR(SBPlatformConnectOptions, 85baf5664fSJonas Devlieghere (const lldb::SBPlatformConnectOptions &), rhs); 86baf5664fSJonas Devlieghere 87fbb76349SGreg Clayton *m_opaque_ptr = *rhs.m_opaque_ptr; 88fbb76349SGreg Clayton } 89fbb76349SGreg Clayton 90b9c1b51eSKate Stone SBPlatformConnectOptions::~SBPlatformConnectOptions() { delete m_opaque_ptr; } 91fbb76349SGreg Clayton 92bc0a35f3SJonas Devlieghere SBPlatformConnectOptions & 93bc0a35f3SJonas Devlieghere SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs) { 94baf5664fSJonas Devlieghere LLDB_RECORD_METHOD( 95ede5cd9aSJonas Devlieghere SBPlatformConnectOptions &, 96baf5664fSJonas Devlieghere SBPlatformConnectOptions, operator=,( 97baf5664fSJonas Devlieghere const lldb::SBPlatformConnectOptions &), 98baf5664fSJonas Devlieghere rhs); 99baf5664fSJonas Devlieghere 100fbb76349SGreg Clayton *m_opaque_ptr = *rhs.m_opaque_ptr; 101*d232abc3SJonas Devlieghere return *this; 102fbb76349SGreg Clayton } 103fbb76349SGreg Clayton 104b9c1b51eSKate Stone const char *SBPlatformConnectOptions::GetURL() { 105baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformConnectOptions, GetURL); 106baf5664fSJonas Devlieghere 107fbb76349SGreg Clayton if (m_opaque_ptr->m_url.empty()) 108248a1305SKonrad Kleine return nullptr; 109fbb76349SGreg Clayton return m_opaque_ptr->m_url.c_str(); 110fbb76349SGreg Clayton } 111fbb76349SGreg Clayton 112b9c1b51eSKate Stone void SBPlatformConnectOptions::SetURL(const char *url) { 113baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, SetURL, (const char *), 114baf5664fSJonas Devlieghere url); 115baf5664fSJonas Devlieghere 116fbb76349SGreg Clayton if (url && url[0]) 117fbb76349SGreg Clayton m_opaque_ptr->m_url = url; 118fbb76349SGreg Clayton else 119fbb76349SGreg Clayton m_opaque_ptr->m_url.clear(); 120fbb76349SGreg Clayton } 121fbb76349SGreg Clayton 122b9c1b51eSKate Stone bool SBPlatformConnectOptions::GetRsyncEnabled() { 123baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(bool, SBPlatformConnectOptions, GetRsyncEnabled); 124baf5664fSJonas Devlieghere 125fbb76349SGreg Clayton return m_opaque_ptr->m_rsync_enabled; 126fbb76349SGreg Clayton } 127fbb76349SGreg Clayton 128b9c1b51eSKate Stone void SBPlatformConnectOptions::EnableRsync( 129b9c1b51eSKate Stone const char *options, const char *remote_path_prefix, 130b9c1b51eSKate Stone bool omit_hostname_from_remote_path) { 131baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, EnableRsync, 132baf5664fSJonas Devlieghere (const char *, const char *, bool), options, 133baf5664fSJonas Devlieghere remote_path_prefix, omit_hostname_from_remote_path); 134baf5664fSJonas Devlieghere 135fbb76349SGreg Clayton m_opaque_ptr->m_rsync_enabled = true; 136b9c1b51eSKate Stone m_opaque_ptr->m_rsync_omit_hostname_from_remote_path = 137b9c1b51eSKate Stone omit_hostname_from_remote_path; 138fbb76349SGreg Clayton if (remote_path_prefix && remote_path_prefix[0]) 139fbb76349SGreg Clayton m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix; 140fbb76349SGreg Clayton else 141fbb76349SGreg Clayton m_opaque_ptr->m_rsync_remote_path_prefix.clear(); 142fbb76349SGreg Clayton 143fbb76349SGreg Clayton if (options && options[0]) 144fbb76349SGreg Clayton m_opaque_ptr->m_rsync_options = options; 145fbb76349SGreg Clayton else 146fbb76349SGreg Clayton m_opaque_ptr->m_rsync_options.clear(); 147fbb76349SGreg Clayton } 148fbb76349SGreg Clayton 149b9c1b51eSKate Stone void SBPlatformConnectOptions::DisableRsync() { 150baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatformConnectOptions, DisableRsync); 151baf5664fSJonas Devlieghere 152fbb76349SGreg Clayton m_opaque_ptr->m_rsync_enabled = false; 153fbb76349SGreg Clayton } 154fbb76349SGreg Clayton 155b9c1b51eSKate Stone const char *SBPlatformConnectOptions::GetLocalCacheDirectory() { 156baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformConnectOptions, 157baf5664fSJonas Devlieghere GetLocalCacheDirectory); 158baf5664fSJonas Devlieghere 159fbb76349SGreg Clayton return m_opaque_ptr->m_local_cache_directory.GetCString(); 160fbb76349SGreg Clayton } 161fbb76349SGreg Clayton 162b9c1b51eSKate Stone void SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path) { 163baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBPlatformConnectOptions, SetLocalCacheDirectory, 164baf5664fSJonas Devlieghere (const char *), path); 165baf5664fSJonas Devlieghere 166fbb76349SGreg Clayton if (path && path[0]) 167fbb76349SGreg Clayton m_opaque_ptr->m_local_cache_directory.SetCString(path); 168fbb76349SGreg Clayton else 169fbb76349SGreg Clayton m_opaque_ptr->m_local_cache_directory = ConstString(); 170fbb76349SGreg Clayton } 171fbb76349SGreg Clayton 172fbb76349SGreg Clayton // SBPlatformShellCommand 173addb5148SMed Ismail Bennani SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_interpreter, 174addb5148SMed Ismail Bennani const char *shell_command) 175addb5148SMed Ismail Bennani : m_opaque_ptr(new PlatformShellCommand(shell_interpreter, shell_command)) { 176addb5148SMed Ismail Bennani LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand, (const char *, const char *), 177addb5148SMed Ismail Bennani shell_interpreter, shell_command); 178addb5148SMed Ismail Bennani } 179addb5148SMed Ismail Bennani 180b9c1b51eSKate Stone SBPlatformShellCommand::SBPlatformShellCommand(const char *shell_command) 181baf5664fSJonas Devlieghere : m_opaque_ptr(new PlatformShellCommand(shell_command)) { 182baf5664fSJonas Devlieghere LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand, (const char *), 183baf5664fSJonas Devlieghere shell_command); 184baf5664fSJonas Devlieghere } 185fbb76349SGreg Clayton 186b9c1b51eSKate Stone SBPlatformShellCommand::SBPlatformShellCommand( 187b9c1b51eSKate Stone const SBPlatformShellCommand &rhs) 188b9c1b51eSKate Stone : m_opaque_ptr(new PlatformShellCommand()) { 189baf5664fSJonas Devlieghere LLDB_RECORD_CONSTRUCTOR(SBPlatformShellCommand, 190baf5664fSJonas Devlieghere (const lldb::SBPlatformShellCommand &), rhs); 191baf5664fSJonas Devlieghere 192fbb76349SGreg Clayton *m_opaque_ptr = *rhs.m_opaque_ptr; 193fbb76349SGreg Clayton } 194fbb76349SGreg Clayton 195bc0a35f3SJonas Devlieghere SBPlatformShellCommand & 196bc0a35f3SJonas Devlieghere SBPlatformShellCommand::operator=(const SBPlatformShellCommand &rhs) { 197ede5cd9aSJonas Devlieghere 198ede5cd9aSJonas Devlieghere LLDB_RECORD_METHOD( 199ede5cd9aSJonas Devlieghere SBPlatformShellCommand &, 200ede5cd9aSJonas Devlieghere SBPlatformShellCommand, operator=,(const lldb::SBPlatformShellCommand &), 201ede5cd9aSJonas Devlieghere rhs); 202ede5cd9aSJonas Devlieghere 203ede5cd9aSJonas Devlieghere *m_opaque_ptr = *rhs.m_opaque_ptr; 204*d232abc3SJonas Devlieghere return *this; 205ede5cd9aSJonas Devlieghere } 206ede5cd9aSJonas Devlieghere 207b9c1b51eSKate Stone SBPlatformShellCommand::~SBPlatformShellCommand() { delete m_opaque_ptr; } 208fbb76349SGreg Clayton 209b9c1b51eSKate Stone void SBPlatformShellCommand::Clear() { 210baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatformShellCommand, Clear); 211baf5664fSJonas Devlieghere 2123a29f8b9SPavel Labath m_opaque_ptr->m_output = std::string(); 213fbb76349SGreg Clayton m_opaque_ptr->m_status = 0; 214fbb76349SGreg Clayton m_opaque_ptr->m_signo = 0; 215fbb76349SGreg Clayton } 216fbb76349SGreg Clayton 217addb5148SMed Ismail Bennani const char *SBPlatformShellCommand::GetShell() { 218addb5148SMed Ismail Bennani LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetShell); 219addb5148SMed Ismail Bennani 220addb5148SMed Ismail Bennani if (m_opaque_ptr->m_shell.empty()) 221addb5148SMed Ismail Bennani return nullptr; 222addb5148SMed Ismail Bennani return m_opaque_ptr->m_shell.c_str(); 223addb5148SMed Ismail Bennani } 224addb5148SMed Ismail Bennani 225addb5148SMed Ismail Bennani void SBPlatformShellCommand::SetShell(const char *shell_interpreter) { 226addb5148SMed Ismail Bennani LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetShell, (const char *), 227addb5148SMed Ismail Bennani shell_interpreter); 228addb5148SMed Ismail Bennani 229addb5148SMed Ismail Bennani if (shell_interpreter && shell_interpreter[0]) 230addb5148SMed Ismail Bennani m_opaque_ptr->m_shell = shell_interpreter; 231addb5148SMed Ismail Bennani else 232addb5148SMed Ismail Bennani m_opaque_ptr->m_shell.clear(); 233addb5148SMed Ismail Bennani } 234addb5148SMed Ismail Bennani 235b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetCommand() { 236baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetCommand); 237baf5664fSJonas Devlieghere 238fbb76349SGreg Clayton if (m_opaque_ptr->m_command.empty()) 239248a1305SKonrad Kleine return nullptr; 240fbb76349SGreg Clayton return m_opaque_ptr->m_command.c_str(); 241fbb76349SGreg Clayton } 242fbb76349SGreg Clayton 243b9c1b51eSKate Stone void SBPlatformShellCommand::SetCommand(const char *shell_command) { 244baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetCommand, (const char *), 245baf5664fSJonas Devlieghere shell_command); 246baf5664fSJonas Devlieghere 247fbb76349SGreg Clayton if (shell_command && shell_command[0]) 248fbb76349SGreg Clayton m_opaque_ptr->m_command = shell_command; 249fbb76349SGreg Clayton else 250fbb76349SGreg Clayton m_opaque_ptr->m_command.clear(); 251fbb76349SGreg Clayton } 252fbb76349SGreg Clayton 253b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetWorkingDirectory() { 254baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, 255baf5664fSJonas Devlieghere GetWorkingDirectory); 256baf5664fSJonas Devlieghere 257fbb76349SGreg Clayton if (m_opaque_ptr->m_working_dir.empty()) 258248a1305SKonrad Kleine return nullptr; 259fbb76349SGreg Clayton return m_opaque_ptr->m_working_dir.c_str(); 260fbb76349SGreg Clayton } 261fbb76349SGreg Clayton 262b9c1b51eSKate Stone void SBPlatformShellCommand::SetWorkingDirectory(const char *path) { 263baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetWorkingDirectory, 264baf5664fSJonas Devlieghere (const char *), path); 265baf5664fSJonas Devlieghere 266fbb76349SGreg Clayton if (path && path[0]) 267fbb76349SGreg Clayton m_opaque_ptr->m_working_dir = path; 268fbb76349SGreg Clayton else 269fbb76349SGreg Clayton m_opaque_ptr->m_working_dir.clear(); 270fbb76349SGreg Clayton } 271fbb76349SGreg Clayton 272b9c1b51eSKate Stone uint32_t SBPlatformShellCommand::GetTimeoutSeconds() { 273baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatformShellCommand, 274baf5664fSJonas Devlieghere GetTimeoutSeconds); 275baf5664fSJonas Devlieghere 27619dd1a0eSPavel Labath if (m_opaque_ptr->m_timeout) 27719dd1a0eSPavel Labath return m_opaque_ptr->m_timeout->count(); 27819dd1a0eSPavel Labath return UINT32_MAX; 279fbb76349SGreg Clayton } 280fbb76349SGreg Clayton 281b9c1b51eSKate Stone void SBPlatformShellCommand::SetTimeoutSeconds(uint32_t sec) { 282baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(void, SBPlatformShellCommand, SetTimeoutSeconds, 283baf5664fSJonas Devlieghere (uint32_t), sec); 284baf5664fSJonas Devlieghere 28519dd1a0eSPavel Labath if (sec == UINT32_MAX) 28619dd1a0eSPavel Labath m_opaque_ptr->m_timeout = llvm::None; 28719dd1a0eSPavel Labath else 28819dd1a0eSPavel Labath m_opaque_ptr->m_timeout = std::chrono::seconds(sec); 289fbb76349SGreg Clayton } 290fbb76349SGreg Clayton 291baf5664fSJonas Devlieghere int SBPlatformShellCommand::GetSignal() { 292baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(int, SBPlatformShellCommand, GetSignal); 293fbb76349SGreg Clayton 294baf5664fSJonas Devlieghere return m_opaque_ptr->m_signo; 295baf5664fSJonas Devlieghere } 296baf5664fSJonas Devlieghere 297baf5664fSJonas Devlieghere int SBPlatformShellCommand::GetStatus() { 298baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(int, SBPlatformShellCommand, GetStatus); 299baf5664fSJonas Devlieghere 300baf5664fSJonas Devlieghere return m_opaque_ptr->m_status; 301baf5664fSJonas Devlieghere } 302fbb76349SGreg Clayton 303b9c1b51eSKate Stone const char *SBPlatformShellCommand::GetOutput() { 304baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatformShellCommand, GetOutput); 305baf5664fSJonas Devlieghere 306fbb76349SGreg Clayton if (m_opaque_ptr->m_output.empty()) 307248a1305SKonrad Kleine return nullptr; 308fbb76349SGreg Clayton return m_opaque_ptr->m_output.c_str(); 309fbb76349SGreg Clayton } 310fbb76349SGreg Clayton 311fbb76349SGreg Clayton // SBPlatform 312a3436f73SKazu Hirata SBPlatform::SBPlatform() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBPlatform); } 313fbb76349SGreg Clayton 314a3436f73SKazu Hirata SBPlatform::SBPlatform(const char *platform_name) { 315baf5664fSJonas Devlieghere LLDB_RECORD_CONSTRUCTOR(SBPlatform, (const char *), platform_name); 316baf5664fSJonas Devlieghere 31797206d57SZachary Turner Status error; 318615eb7e6SGreg Clayton if (platform_name && platform_name[0]) 319615eb7e6SGreg Clayton m_opaque_sp = Platform::Create(ConstString(platform_name), error); 320fbb76349SGreg Clayton } 321fbb76349SGreg Clayton 32266dc4672SJonas Devlieghere SBPlatform::SBPlatform(const SBPlatform &rhs) { 32366dc4672SJonas Devlieghere LLDB_RECORD_CONSTRUCTOR(SBPlatform, (const lldb::SBPlatform &), rhs); 32466dc4672SJonas Devlieghere 32566dc4672SJonas Devlieghere m_opaque_sp = rhs.m_opaque_sp; 32666dc4672SJonas Devlieghere } 32766dc4672SJonas Devlieghere 328ede5cd9aSJonas Devlieghere SBPlatform &SBPlatform::operator=(const SBPlatform &rhs) { 329ede5cd9aSJonas Devlieghere LLDB_RECORD_METHOD(SBPlatform &, 330ede5cd9aSJonas Devlieghere SBPlatform, operator=,(const lldb::SBPlatform &), rhs); 33166dc4672SJonas Devlieghere 33266dc4672SJonas Devlieghere m_opaque_sp = rhs.m_opaque_sp; 333*d232abc3SJonas Devlieghere return *this; 33466dc4672SJonas Devlieghere } 33566dc4672SJonas Devlieghere 336866b7a65SJonas Devlieghere SBPlatform::~SBPlatform() = default; 337fbb76349SGreg Clayton 338a31130f6STatyana Krasnukha SBPlatform SBPlatform::GetHostPlatform() { 3395c2bf577SJonas Devlieghere LLDB_RECORD_STATIC_METHOD_NO_ARGS(lldb::SBPlatform, SBPlatform, 3405c2bf577SJonas Devlieghere GetHostPlatform); 3415c2bf577SJonas Devlieghere 342a31130f6STatyana Krasnukha SBPlatform host_platform; 343a31130f6STatyana Krasnukha host_platform.m_opaque_sp = Platform::GetHostPlatform(); 344*d232abc3SJonas Devlieghere return host_platform; 345a31130f6STatyana Krasnukha } 346a31130f6STatyana Krasnukha 347baf5664fSJonas Devlieghere bool SBPlatform::IsValid() const { 348baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBPlatform, IsValid); 3497f5237bcSPavel Labath return this->operator bool(); 3507f5237bcSPavel Labath } 3517f5237bcSPavel Labath SBPlatform::operator bool() const { 3527f5237bcSPavel Labath LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBPlatform, operator bool); 353fbb76349SGreg Clayton 354248a1305SKonrad Kleine return m_opaque_sp.get() != nullptr; 355baf5664fSJonas Devlieghere } 356baf5664fSJonas Devlieghere 357baf5664fSJonas Devlieghere void SBPlatform::Clear() { 358baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatform, Clear); 359baf5664fSJonas Devlieghere 360baf5664fSJonas Devlieghere m_opaque_sp.reset(); 361baf5664fSJonas Devlieghere } 362fbb76349SGreg Clayton 363b9c1b51eSKate Stone const char *SBPlatform::GetName() { 364baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetName); 365baf5664fSJonas Devlieghere 366fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 367fbb76349SGreg Clayton if (platform_sp) 368fbb76349SGreg Clayton return platform_sp->GetName().GetCString(); 369248a1305SKonrad Kleine return nullptr; 370fbb76349SGreg Clayton } 371fbb76349SGreg Clayton 372b9c1b51eSKate Stone lldb::PlatformSP SBPlatform::GetSP() const { return m_opaque_sp; } 373fbb76349SGreg Clayton 374b9c1b51eSKate Stone void SBPlatform::SetSP(const lldb::PlatformSP &platform_sp) { 375fbb76349SGreg Clayton m_opaque_sp = platform_sp; 376fbb76349SGreg Clayton } 377fbb76349SGreg Clayton 378b9c1b51eSKate Stone const char *SBPlatform::GetWorkingDirectory() { 379baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetWorkingDirectory); 380baf5664fSJonas Devlieghere 381fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 382fbb76349SGreg Clayton if (platform_sp) 383fbb76349SGreg Clayton return platform_sp->GetWorkingDirectory().GetCString(); 384248a1305SKonrad Kleine return nullptr; 385fbb76349SGreg Clayton } 386fbb76349SGreg Clayton 387b9c1b51eSKate Stone bool SBPlatform::SetWorkingDirectory(const char *path) { 388baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(bool, SBPlatform, SetWorkingDirectory, (const char *), 389baf5664fSJonas Devlieghere path); 390baf5664fSJonas Devlieghere 391fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 392b9c1b51eSKate Stone if (platform_sp) { 393fbb76349SGreg Clayton if (path) 3948f3be7a3SJonas Devlieghere platform_sp->SetWorkingDirectory(FileSpec(path)); 395fbb76349SGreg Clayton else 3968f3be7a3SJonas Devlieghere platform_sp->SetWorkingDirectory(FileSpec()); 397fbb76349SGreg Clayton return true; 398fbb76349SGreg Clayton } 399fbb76349SGreg Clayton return false; 400fbb76349SGreg Clayton } 401fbb76349SGreg Clayton 402b9c1b51eSKate Stone SBError SBPlatform::ConnectRemote(SBPlatformConnectOptions &connect_options) { 403baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, ConnectRemote, 404baf5664fSJonas Devlieghere (lldb::SBPlatformConnectOptions &), connect_options); 405baf5664fSJonas Devlieghere 406fbb76349SGreg Clayton SBError sb_error; 407fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 408b9c1b51eSKate Stone if (platform_sp && connect_options.GetURL()) { 409fbb76349SGreg Clayton Args args; 410bbea3610SRaphael Isemann args.AppendArgument(connect_options.GetURL()); 411fbb76349SGreg Clayton sb_error.ref() = platform_sp->ConnectRemote(args); 412b9c1b51eSKate Stone } else { 413fbb76349SGreg Clayton sb_error.SetErrorString("invalid platform"); 414fbb76349SGreg Clayton } 415*d232abc3SJonas Devlieghere return sb_error; 416fbb76349SGreg Clayton } 417fbb76349SGreg Clayton 418b9c1b51eSKate Stone void SBPlatform::DisconnectRemote() { 419baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(void, SBPlatform, DisconnectRemote); 420baf5664fSJonas Devlieghere 421fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 422fbb76349SGreg Clayton if (platform_sp) 423fbb76349SGreg Clayton platform_sp->DisconnectRemote(); 424fbb76349SGreg Clayton } 425fbb76349SGreg Clayton 426b9c1b51eSKate Stone bool SBPlatform::IsConnected() { 427baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(bool, SBPlatform, IsConnected); 428baf5664fSJonas Devlieghere 429fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 430fbb76349SGreg Clayton if (platform_sp) 431ca38766cSJim Ingham return platform_sp->IsConnected(); 432fbb76349SGreg Clayton return false; 433fbb76349SGreg Clayton } 434fbb76349SGreg Clayton 435b9c1b51eSKate Stone const char *SBPlatform::GetTriple() { 436baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetTriple); 437baf5664fSJonas Devlieghere 438fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 439b9c1b51eSKate Stone if (platform_sp) { 440ccd28a14STamas Berghammer ArchSpec arch(platform_sp->GetSystemArchitecture()); 441b9c1b51eSKate Stone if (arch.IsValid()) { 442b9c1b51eSKate Stone // Const-ify the string so we don't need to worry about the lifetime of 443b9c1b51eSKate Stone // the string 444fbb76349SGreg Clayton return ConstString(arch.GetTriple().getTriple().c_str()).GetCString(); 445fbb76349SGreg Clayton } 446fbb76349SGreg Clayton } 447248a1305SKonrad Kleine return nullptr; 448fbb76349SGreg Clayton } 449fbb76349SGreg Clayton 450b9c1b51eSKate Stone const char *SBPlatform::GetOSBuild() { 451baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSBuild); 452baf5664fSJonas Devlieghere 453fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 454b9c1b51eSKate Stone if (platform_sp) { 45540e4ac3eSPavel Labath std::string s = platform_sp->GetOSBuildString().getValueOr(""); 456b9c1b51eSKate Stone if (!s.empty()) { 457b9c1b51eSKate Stone // Const-ify the string so we don't need to worry about the lifetime of 458b9c1b51eSKate Stone // the string 45940e4ac3eSPavel Labath return ConstString(s).GetCString(); 460fbb76349SGreg Clayton } 461fbb76349SGreg Clayton } 462248a1305SKonrad Kleine return nullptr; 463fbb76349SGreg Clayton } 464fbb76349SGreg Clayton 465b9c1b51eSKate Stone const char *SBPlatform::GetOSDescription() { 466baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetOSDescription); 467baf5664fSJonas Devlieghere 468fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 469b9c1b51eSKate Stone if (platform_sp) { 470f5158ca4SPavel Labath std::string s = platform_sp->GetOSKernelDescription().getValueOr(""); 471b9c1b51eSKate Stone if (!s.empty()) { 472b9c1b51eSKate Stone // Const-ify the string so we don't need to worry about the lifetime of 473b9c1b51eSKate Stone // the string 474fbb76349SGreg Clayton return ConstString(s.c_str()).GetCString(); 475fbb76349SGreg Clayton } 476fbb76349SGreg Clayton } 477248a1305SKonrad Kleine return nullptr; 478fbb76349SGreg Clayton } 479fbb76349SGreg Clayton 480b9c1b51eSKate Stone const char *SBPlatform::GetHostname() { 481baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(const char *, SBPlatform, GetHostname); 482baf5664fSJonas Devlieghere 483fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 484fbb76349SGreg Clayton if (platform_sp) 485fbb76349SGreg Clayton return platform_sp->GetHostname(); 486248a1305SKonrad Kleine return nullptr; 487fbb76349SGreg Clayton } 488fbb76349SGreg Clayton 489b9c1b51eSKate Stone uint32_t SBPlatform::GetOSMajorVersion() { 490baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMajorVersion); 491baf5664fSJonas Devlieghere 4922272c481SPavel Labath llvm::VersionTuple version; 4932272c481SPavel Labath if (PlatformSP platform_sp = GetSP()) 4942272c481SPavel Labath version = platform_sp->GetOSVersion(); 4952272c481SPavel Labath return version.empty() ? UINT32_MAX : version.getMajor(); 496fbb76349SGreg Clayton } 497fbb76349SGreg Clayton 498b9c1b51eSKate Stone uint32_t SBPlatform::GetOSMinorVersion() { 499baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSMinorVersion); 500baf5664fSJonas Devlieghere 5012272c481SPavel Labath llvm::VersionTuple version; 5022272c481SPavel Labath if (PlatformSP platform_sp = GetSP()) 5032272c481SPavel Labath version = platform_sp->GetOSVersion(); 5042272c481SPavel Labath return version.getMinor().getValueOr(UINT32_MAX); 505fbb76349SGreg Clayton } 506fbb76349SGreg Clayton 507b9c1b51eSKate Stone uint32_t SBPlatform::GetOSUpdateVersion() { 508baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBPlatform, GetOSUpdateVersion); 509baf5664fSJonas Devlieghere 5102272c481SPavel Labath llvm::VersionTuple version; 5112272c481SPavel Labath if (PlatformSP platform_sp = GetSP()) 5122272c481SPavel Labath version = platform_sp->GetOSVersion(); 5132272c481SPavel Labath return version.getSubminor().getValueOr(UINT32_MAX); 514fbb76349SGreg Clayton } 515fbb76349SGreg Clayton 516b9c1b51eSKate Stone SBError SBPlatform::Get(SBFileSpec &src, SBFileSpec &dst) { 517baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Get, 518baf5664fSJonas Devlieghere (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst); 519baf5664fSJonas Devlieghere 520fbb76349SGreg Clayton SBError sb_error; 521fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 522b9c1b51eSKate Stone if (platform_sp) { 523fbb76349SGreg Clayton sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref()); 524b9c1b51eSKate Stone } else { 525fbb76349SGreg Clayton sb_error.SetErrorString("invalid platform"); 526fbb76349SGreg Clayton } 527*d232abc3SJonas Devlieghere return sb_error; 528fbb76349SGreg Clayton } 529fbb76349SGreg Clayton 530b9c1b51eSKate Stone SBError SBPlatform::Put(SBFileSpec &src, SBFileSpec &dst) { 531baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Put, 532baf5664fSJonas Devlieghere (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst); 533*d232abc3SJonas Devlieghere return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) { 534b9c1b51eSKate Stone if (src.Exists()) { 535*d232abc3SJonas Devlieghere uint32_t permissions = FileSystem::Instance().GetPermissions(src.ref()); 536b9c1b51eSKate Stone if (permissions == 0) { 5373a58d898SJonas Devlieghere if (FileSystem::Instance().IsDirectory(src.ref())) 538fbb76349SGreg Clayton permissions = eFilePermissionsDirectoryDefault; 539fbb76349SGreg Clayton else 540fbb76349SGreg Clayton permissions = eFilePermissionsFileDefault; 541fbb76349SGreg Clayton } 542fbb76349SGreg Clayton 5431ef7b2c8SOleksiy Vyalov return platform_sp->PutFile(src.ref(), dst.ref(), permissions); 544fbb76349SGreg Clayton } 5451ef7b2c8SOleksiy Vyalov 54697206d57SZachary Turner Status error; 547b9c1b51eSKate Stone error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", 548b9c1b51eSKate Stone src.ref().GetPath().c_str()); 5491ef7b2c8SOleksiy Vyalov return error; 550*d232abc3SJonas Devlieghere }); 551fbb76349SGreg Clayton } 552fbb76349SGreg Clayton 553b9c1b51eSKate Stone SBError SBPlatform::Install(SBFileSpec &src, SBFileSpec &dst) { 554baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Install, 555baf5664fSJonas Devlieghere (lldb::SBFileSpec &, lldb::SBFileSpec &), src, dst); 556*d232abc3SJonas Devlieghere return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) { 557fbb76349SGreg Clayton if (src.Exists()) 5581ef7b2c8SOleksiy Vyalov return platform_sp->Install(src.ref(), dst.ref()); 5591ef7b2c8SOleksiy Vyalov 56097206d57SZachary Turner Status error; 561b9c1b51eSKate Stone error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", 562b9c1b51eSKate Stone src.ref().GetPath().c_str()); 5631ef7b2c8SOleksiy Vyalov return error; 564*d232abc3SJonas Devlieghere }); 565fbb76349SGreg Clayton } 566fbb76349SGreg Clayton 567b9c1b51eSKate Stone SBError SBPlatform::Run(SBPlatformShellCommand &shell_command) { 568baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Run, 569baf5664fSJonas Devlieghere (lldb::SBPlatformShellCommand &), shell_command); 570*d232abc3SJonas Devlieghere return ExecuteConnected( 571*d232abc3SJonas Devlieghere [&](const lldb::PlatformSP &platform_sp) { 572fbb76349SGreg Clayton const char *command = shell_command.GetCommand(); 5731ef7b2c8SOleksiy Vyalov if (!command) 57497206d57SZachary Turner return Status("invalid shell command (empty)"); 5751ef7b2c8SOleksiy Vyalov 576fbb76349SGreg Clayton const char *working_dir = shell_command.GetWorkingDirectory(); 577248a1305SKonrad Kleine if (working_dir == nullptr) { 578fbb76349SGreg Clayton working_dir = platform_sp->GetWorkingDirectory().GetCString(); 579fbb76349SGreg Clayton if (working_dir) 580fbb76349SGreg Clayton shell_command.SetWorkingDirectory(working_dir); 581fbb76349SGreg Clayton } 582bc0a35f3SJonas Devlieghere return platform_sp->RunShellCommand( 583bc0a35f3SJonas Devlieghere shell_command.m_opaque_ptr->m_shell, command, FileSpec(working_dir), 584fbb76349SGreg Clayton &shell_command.m_opaque_ptr->m_status, 585fbb76349SGreg Clayton &shell_command.m_opaque_ptr->m_signo, 586fbb76349SGreg Clayton &shell_command.m_opaque_ptr->m_output, 58719dd1a0eSPavel Labath shell_command.m_opaque_ptr->m_timeout); 588*d232abc3SJonas Devlieghere }); 589fbb76349SGreg Clayton } 5901ef7b2c8SOleksiy Vyalov 591b9c1b51eSKate Stone SBError SBPlatform::Launch(SBLaunchInfo &launch_info) { 592baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Launch, (lldb::SBLaunchInfo &), 593baf5664fSJonas Devlieghere launch_info); 594*d232abc3SJonas Devlieghere return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) { 59562930e57SPavel Labath ProcessLaunchInfo info = launch_info.ref(); 59662930e57SPavel Labath Status error = platform_sp->LaunchProcess(info); 59762930e57SPavel Labath launch_info.set_ref(info); 59862930e57SPavel Labath return error; 599*d232abc3SJonas Devlieghere }); 6001ef7b2c8SOleksiy Vyalov } 6011ef7b2c8SOleksiy Vyalov 602b9c1b51eSKate Stone SBError SBPlatform::Kill(const lldb::pid_t pid) { 603baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, Kill, (const lldb::pid_t), pid); 604*d232abc3SJonas Devlieghere return ExecuteConnected([&](const lldb::PlatformSP &platform_sp) { 6051ef7b2c8SOleksiy Vyalov return platform_sp->KillProcess(pid); 606*d232abc3SJonas Devlieghere }); 6071ef7b2c8SOleksiy Vyalov } 6081ef7b2c8SOleksiy Vyalov 609b9c1b51eSKate Stone SBError SBPlatform::ExecuteConnected( 61097206d57SZachary Turner const std::function<Status(const lldb::PlatformSP &)> &func) { 6111ef7b2c8SOleksiy Vyalov SBError sb_error; 6121ef7b2c8SOleksiy Vyalov const auto platform_sp(GetSP()); 613b9c1b51eSKate Stone if (platform_sp) { 6141ef7b2c8SOleksiy Vyalov if (platform_sp->IsConnected()) 6151ef7b2c8SOleksiy Vyalov sb_error.ref() = func(platform_sp); 6161ef7b2c8SOleksiy Vyalov else 617fbb76349SGreg Clayton sb_error.SetErrorString("not connected"); 618b9c1b51eSKate Stone } else 619fbb76349SGreg Clayton sb_error.SetErrorString("invalid platform"); 6201ef7b2c8SOleksiy Vyalov 621fbb76349SGreg Clayton return sb_error; 622fbb76349SGreg Clayton } 623fbb76349SGreg Clayton 624b9c1b51eSKate Stone SBError SBPlatform::MakeDirectory(const char *path, uint32_t file_permissions) { 625baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, MakeDirectory, 626baf5664fSJonas Devlieghere (const char *, uint32_t), path, file_permissions); 627baf5664fSJonas Devlieghere 628fbb76349SGreg Clayton SBError sb_error; 629fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 630b9c1b51eSKate Stone if (platform_sp) { 631b9c1b51eSKate Stone sb_error.ref() = 6328f3be7a3SJonas Devlieghere platform_sp->MakeDirectory(FileSpec(path), file_permissions); 633b9c1b51eSKate Stone } else { 634fbb76349SGreg Clayton sb_error.SetErrorString("invalid platform"); 635fbb76349SGreg Clayton } 636*d232abc3SJonas Devlieghere return sb_error; 637fbb76349SGreg Clayton } 638fbb76349SGreg Clayton 639b9c1b51eSKate Stone uint32_t SBPlatform::GetFilePermissions(const char *path) { 640baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(uint32_t, SBPlatform, GetFilePermissions, (const char *), 641baf5664fSJonas Devlieghere path); 642baf5664fSJonas Devlieghere 643fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 644b9c1b51eSKate Stone if (platform_sp) { 645fbb76349SGreg Clayton uint32_t file_permissions = 0; 6468f3be7a3SJonas Devlieghere platform_sp->GetFilePermissions(FileSpec(path), file_permissions); 647fbb76349SGreg Clayton return file_permissions; 648fbb76349SGreg Clayton } 649fbb76349SGreg Clayton return 0; 650fbb76349SGreg Clayton } 651fbb76349SGreg Clayton 652b9c1b51eSKate Stone SBError SBPlatform::SetFilePermissions(const char *path, 653b9c1b51eSKate Stone uint32_t file_permissions) { 654baf5664fSJonas Devlieghere LLDB_RECORD_METHOD(lldb::SBError, SBPlatform, SetFilePermissions, 655baf5664fSJonas Devlieghere (const char *, uint32_t), path, file_permissions); 656baf5664fSJonas Devlieghere 657fbb76349SGreg Clayton SBError sb_error; 658fbb76349SGreg Clayton PlatformSP platform_sp(GetSP()); 659b9c1b51eSKate Stone if (platform_sp) { 6608f3be7a3SJonas Devlieghere sb_error.ref() = 6618f3be7a3SJonas Devlieghere platform_sp->SetFilePermissions(FileSpec(path), file_permissions); 662b9c1b51eSKate Stone } else { 663fbb76349SGreg Clayton sb_error.SetErrorString("invalid platform"); 664fbb76349SGreg Clayton } 665*d232abc3SJonas Devlieghere return sb_error; 666fbb76349SGreg Clayton } 667fbb76349SGreg Clayton 668b9c1b51eSKate Stone SBUnixSignals SBPlatform::GetUnixSignals() const { 669baf5664fSJonas Devlieghere LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBUnixSignals, SBPlatform, 670baf5664fSJonas Devlieghere GetUnixSignals); 67198d0a4b3SChaoren Lin 672baf5664fSJonas Devlieghere if (auto platform_sp = GetSP()) 673*d232abc3SJonas Devlieghere return SBUnixSignals{platform_sp}; 674baf5664fSJonas Devlieghere 675*d232abc3SJonas Devlieghere return SBUnixSignals(); 67698d0a4b3SChaoren Lin } 677ae211eceSMichal Gorny 678ca69be21SWalter Erquinigo SBEnvironment SBPlatform::GetEnvironment() { 679ca69be21SWalter Erquinigo LLDB_RECORD_METHOD_NO_ARGS(lldb::SBEnvironment, SBPlatform, GetEnvironment); 680ca69be21SWalter Erquinigo PlatformSP platform_sp(GetSP()); 681ca69be21SWalter Erquinigo 682ca69be21SWalter Erquinigo if (platform_sp) { 683*d232abc3SJonas Devlieghere return SBEnvironment(platform_sp->GetEnvironment()); 684ca69be21SWalter Erquinigo } 685ca69be21SWalter Erquinigo 686*d232abc3SJonas Devlieghere return SBEnvironment(); 687ca69be21SWalter Erquinigo } 688