15ffd83dbSDimitry Andric //===-- Platform.cpp ------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include <algorithm>
100b57cec5SDimitry Andric #include <csignal>
110b57cec5SDimitry Andric #include <fstream>
120b57cec5SDimitry Andric #include <memory>
13bdd1243dSDimitry Andric #include <optional>
140b57cec5SDimitry Andric #include <vector>
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
170b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
180b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
190b57cec5SDimitry Andric #include "lldb/Core/Module.h"
200b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
210b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
2281ad6265SDimitry Andric #include "lldb/Host/FileCache.h"
230b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
240b57cec5SDimitry Andric #include "lldb/Host/Host.h"
250b57cec5SDimitry Andric #include "lldb/Host/HostInfo.h"
260b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
275ffd83dbSDimitry Andric #include "lldb/Interpreter/OptionValueFileSpec.h"
280b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValueProperties.h"
290b57cec5SDimitry Andric #include "lldb/Interpreter/Property.h"
300b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
310b57cec5SDimitry Andric #include "lldb/Target/ModuleCache.h"
320b57cec5SDimitry Andric #include "lldb/Target/Platform.h"
330b57cec5SDimitry Andric #include "lldb/Target/Process.h"
340b57cec5SDimitry Andric #include "lldb/Target/Target.h"
350b57cec5SDimitry Andric #include "lldb/Target/UnixSignals.h"
360b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
370b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
3881ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
390b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
400b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
410b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h"
4281ad6265SDimitry Andric #include "llvm/ADT/STLExtras.h"
430b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
445ffd83dbSDimitry Andric #include "llvm/Support/Path.h"
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric // Define these constants from POSIX mman.h rather than include the file so
470b57cec5SDimitry Andric // that they will be correct even when compiled on Linux.
480b57cec5SDimitry Andric #define MAP_PRIVATE 2
490b57cec5SDimitry Andric #define MAP_ANON 0x1000
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric using namespace lldb;
520b57cec5SDimitry Andric using namespace lldb_private;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // Use a singleton function for g_local_platform_sp to avoid init constructors
550b57cec5SDimitry Andric // since LLDB is often part of a shared library
GetHostPlatformSP()560b57cec5SDimitry Andric static PlatformSP &GetHostPlatformSP() {
570b57cec5SDimitry Andric   static PlatformSP g_platform_sp;
580b57cec5SDimitry Andric   return g_platform_sp;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
GetHostPlatformName()610b57cec5SDimitry Andric const char *Platform::GetHostPlatformName() { return "host"; }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric namespace {
640b57cec5SDimitry Andric 
659dba64beSDimitry Andric #define LLDB_PROPERTIES_platform
669dba64beSDimitry Andric #include "TargetProperties.inc"
670b57cec5SDimitry Andric 
689dba64beSDimitry Andric enum {
699dba64beSDimitry Andric #define LLDB_PROPERTIES_platform
709dba64beSDimitry Andric #include "TargetPropertiesEnum.inc"
719dba64beSDimitry Andric };
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric } // namespace
740b57cec5SDimitry Andric 
GetSettingName()755f757f3fSDimitry Andric llvm::StringRef PlatformProperties::GetSettingName() {
765f757f3fSDimitry Andric   static constexpr llvm::StringLiteral g_setting_name("platform");
770b57cec5SDimitry Andric   return g_setting_name;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric 
PlatformProperties()800b57cec5SDimitry Andric PlatformProperties::PlatformProperties() {
810b57cec5SDimitry Andric   m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
829dba64beSDimitry Andric   m_collection_sp->Initialize(g_platform_properties);
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   auto module_cache_dir = GetModuleCacheDirectory();
850b57cec5SDimitry Andric   if (module_cache_dir)
860b57cec5SDimitry Andric     return;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   llvm::SmallString<64> user_home_dir;
89e8d8bef9SDimitry Andric   if (!FileSystem::Instance().GetHomeDirectory(user_home_dir))
900b57cec5SDimitry Andric     return;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   module_cache_dir = FileSpec(user_home_dir.c_str());
930b57cec5SDimitry Andric   module_cache_dir.AppendPathComponent(".lldb");
940b57cec5SDimitry Andric   module_cache_dir.AppendPathComponent("module_cache");
955ffd83dbSDimitry Andric   SetDefaultModuleCacheDirectory(module_cache_dir);
960b57cec5SDimitry Andric   SetModuleCacheDirectory(module_cache_dir);
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric 
GetUseModuleCache() const990b57cec5SDimitry Andric bool PlatformProperties::GetUseModuleCache() const {
1000b57cec5SDimitry Andric   const auto idx = ePropertyUseModuleCache;
10106c3fb27SDimitry Andric   return GetPropertyAtIndexAs<bool>(
10206c3fb27SDimitry Andric       idx, g_platform_properties[idx].default_uint_value != 0);
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
SetUseModuleCache(bool use_module_cache)1050b57cec5SDimitry Andric bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
10606c3fb27SDimitry Andric   return SetPropertyAtIndex(ePropertyUseModuleCache, use_module_cache);
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
GetModuleCacheDirectory() const1090b57cec5SDimitry Andric FileSpec PlatformProperties::GetModuleCacheDirectory() const {
11006c3fb27SDimitry Andric   return GetPropertyAtIndexAs<FileSpec>(ePropertyModuleCacheDirectory, {});
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
SetModuleCacheDirectory(const FileSpec & dir_spec)1130b57cec5SDimitry Andric bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
11406c3fb27SDimitry Andric   return m_collection_sp->SetPropertyAtIndex(ePropertyModuleCacheDirectory,
11506c3fb27SDimitry Andric                                              dir_spec);
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
SetDefaultModuleCacheDirectory(const FileSpec & dir_spec)1185ffd83dbSDimitry Andric void PlatformProperties::SetDefaultModuleCacheDirectory(
1195ffd83dbSDimitry Andric     const FileSpec &dir_spec) {
1205ffd83dbSDimitry Andric   auto f_spec_opt = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(
12106c3fb27SDimitry Andric       ePropertyModuleCacheDirectory);
1225ffd83dbSDimitry Andric   assert(f_spec_opt);
1235ffd83dbSDimitry Andric   f_spec_opt->SetDefaultValue(dir_spec);
1245ffd83dbSDimitry Andric }
1255ffd83dbSDimitry Andric 
1260b57cec5SDimitry Andric /// Get the native host platform plug-in.
1270b57cec5SDimitry Andric ///
1280b57cec5SDimitry Andric /// There should only be one of these for each host that LLDB runs
1290b57cec5SDimitry Andric /// upon that should be statically compiled in and registered using
1300b57cec5SDimitry Andric /// preprocessor macros or other similar build mechanisms.
1310b57cec5SDimitry Andric ///
1320b57cec5SDimitry Andric /// This platform will be used as the default platform when launching
1330b57cec5SDimitry Andric /// or attaching to processes unless another platform is specified.
GetHostPlatform()1340b57cec5SDimitry Andric PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
1350b57cec5SDimitry Andric 
Initialize()13681ad6265SDimitry Andric void Platform::Initialize() {}
1370b57cec5SDimitry Andric 
Terminate()13881ad6265SDimitry Andric void Platform::Terminate() {}
1390b57cec5SDimitry Andric 
GetGlobalPlatformProperties()140349cc55cSDimitry Andric PlatformProperties &Platform::GetGlobalPlatformProperties() {
141349cc55cSDimitry Andric   static PlatformProperties g_settings;
142349cc55cSDimitry Andric   return g_settings;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
SetHostPlatform(const lldb::PlatformSP & platform_sp)1450b57cec5SDimitry Andric void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
1460b57cec5SDimitry Andric   // The native platform should use its static void Platform::Initialize()
1470b57cec5SDimitry Andric   // function to register itself as the native platform.
1480b57cec5SDimitry Andric   GetHostPlatformSP() = platform_sp;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
GetFileWithUUID(const FileSpec & platform_file,const UUID * uuid_ptr,FileSpec & local_file)1510b57cec5SDimitry Andric Status Platform::GetFileWithUUID(const FileSpec &platform_file,
1520b57cec5SDimitry Andric                                  const UUID *uuid_ptr, FileSpec &local_file) {
1530b57cec5SDimitry Andric   // Default to the local case
1540b57cec5SDimitry Andric   local_file = platform_file;
1550b57cec5SDimitry Andric   return Status();
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric FileSpecList
LocateExecutableScriptingResources(Target * target,Module & module,Stream & feedback_stream)1590b57cec5SDimitry Andric Platform::LocateExecutableScriptingResources(Target *target, Module &module,
16006c3fb27SDimitry Andric                                              Stream &feedback_stream) {
1610b57cec5SDimitry Andric   return FileSpecList();
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric // PlatformSP
1650b57cec5SDimitry Andric // Platform::FindPlugin (Process *process, ConstString plugin_name)
1660b57cec5SDimitry Andric //{
1670b57cec5SDimitry Andric //    PlatformCreateInstance create_callback = nullptr;
1680b57cec5SDimitry Andric //    if (plugin_name)
1690b57cec5SDimitry Andric //    {
1700b57cec5SDimitry Andric //        create_callback  =
1710b57cec5SDimitry Andric //        PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
1720b57cec5SDimitry Andric //        if (create_callback)
1730b57cec5SDimitry Andric //        {
1740b57cec5SDimitry Andric //            ArchSpec arch;
1750b57cec5SDimitry Andric //            if (process)
1760b57cec5SDimitry Andric //            {
1770b57cec5SDimitry Andric //                arch = process->GetTarget().GetArchitecture();
1780b57cec5SDimitry Andric //            }
1790b57cec5SDimitry Andric //            PlatformSP platform_sp(create_callback(process, &arch));
1800b57cec5SDimitry Andric //            if (platform_sp)
1810b57cec5SDimitry Andric //                return platform_sp;
1820b57cec5SDimitry Andric //        }
1830b57cec5SDimitry Andric //    }
1840b57cec5SDimitry Andric //    else
1850b57cec5SDimitry Andric //    {
1860b57cec5SDimitry Andric //        for (uint32_t idx = 0; (create_callback =
1870b57cec5SDimitry Andric //        PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
1880b57cec5SDimitry Andric //        ++idx)
1890b57cec5SDimitry Andric //        {
1900b57cec5SDimitry Andric //            PlatformSP platform_sp(create_callback(process, nullptr));
1910b57cec5SDimitry Andric //            if (platform_sp)
1920b57cec5SDimitry Andric //                return platform_sp;
1930b57cec5SDimitry Andric //        }
1940b57cec5SDimitry Andric //    }
1950b57cec5SDimitry Andric //    return PlatformSP();
1960b57cec5SDimitry Andric //}
1970b57cec5SDimitry Andric 
GetSharedModule(const ModuleSpec & module_spec,Process * process,ModuleSP & module_sp,const FileSpecList * module_search_paths_ptr,llvm::SmallVectorImpl<lldb::ModuleSP> * old_modules,bool * did_create_ptr)198eaeb601bSDimitry Andric Status Platform::GetSharedModule(
199eaeb601bSDimitry Andric     const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
2000b57cec5SDimitry Andric     const FileSpecList *module_search_paths_ptr,
201eaeb601bSDimitry Andric     llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
2020b57cec5SDimitry Andric   if (IsHost())
203eaeb601bSDimitry Andric     return ModuleList::GetSharedModule(module_spec, module_sp,
204eaeb601bSDimitry Andric                                        module_search_paths_ptr, old_modules,
2050b57cec5SDimitry Andric                                        did_create_ptr, false);
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   // Module resolver lambda.
2080b57cec5SDimitry Andric   auto resolver = [&](const ModuleSpec &spec) {
2090b57cec5SDimitry Andric     Status error(eErrorTypeGeneric);
2100b57cec5SDimitry Andric     ModuleSpec resolved_spec;
2110b57cec5SDimitry Andric     // Check if we have sysroot set.
21206c3fb27SDimitry Andric     if (!m_sdk_sysroot.empty()) {
2130b57cec5SDimitry Andric       // Prepend sysroot to module spec.
2140b57cec5SDimitry Andric       resolved_spec = spec;
21506c3fb27SDimitry Andric       resolved_spec.GetFileSpec().PrependPathComponent(m_sdk_sysroot);
2160b57cec5SDimitry Andric       // Try to get shared module with resolved spec.
217eaeb601bSDimitry Andric       error = ModuleList::GetSharedModule(resolved_spec, module_sp,
218eaeb601bSDimitry Andric                                           module_search_paths_ptr, old_modules,
2190b57cec5SDimitry Andric                                           did_create_ptr, false);
2200b57cec5SDimitry Andric     }
2210b57cec5SDimitry Andric     // If we don't have sysroot or it didn't work then
2220b57cec5SDimitry Andric     // try original module spec.
2230b57cec5SDimitry Andric     if (!error.Success()) {
2240b57cec5SDimitry Andric       resolved_spec = spec;
225eaeb601bSDimitry Andric       error = ModuleList::GetSharedModule(resolved_spec, module_sp,
226eaeb601bSDimitry Andric                                           module_search_paths_ptr, old_modules,
2270b57cec5SDimitry Andric                                           did_create_ptr, false);
2280b57cec5SDimitry Andric     }
2290b57cec5SDimitry Andric     if (error.Success() && module_sp)
2300b57cec5SDimitry Andric       module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
2310b57cec5SDimitry Andric     return error;
2320b57cec5SDimitry Andric   };
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
2350b57cec5SDimitry Andric                                did_create_ptr);
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
GetModuleSpec(const FileSpec & module_file_spec,const ArchSpec & arch,ModuleSpec & module_spec)2380b57cec5SDimitry Andric bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
2390b57cec5SDimitry Andric                              const ArchSpec &arch, ModuleSpec &module_spec) {
2400b57cec5SDimitry Andric   ModuleSpecList module_specs;
2410b57cec5SDimitry Andric   if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
2420b57cec5SDimitry Andric                                           module_specs) == 0)
2430b57cec5SDimitry Andric     return false;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   ModuleSpec matched_module_spec;
2460b57cec5SDimitry Andric   return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
2470b57cec5SDimitry Andric                                              module_spec);
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric 
Create(llvm::StringRef name)25081ad6265SDimitry Andric PlatformSP Platform::Create(llvm::StringRef name) {
25181ad6265SDimitry Andric   lldb::PlatformSP platform_sp;
25281ad6265SDimitry Andric   if (name == GetHostPlatformName())
2530b57cec5SDimitry Andric     return GetHostPlatform();
2540b57cec5SDimitry Andric 
25581ad6265SDimitry Andric   if (PlatformCreateInstance create_callback =
25681ad6265SDimitry Andric           PluginManager::GetPlatformCreateCallbackForPluginName(name))
25781ad6265SDimitry Andric     return create_callback(true, nullptr);
25881ad6265SDimitry Andric   return nullptr;
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
GetAugmentedArchSpec(Platform * platform,llvm::StringRef triple)2610b57cec5SDimitry Andric ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
2620b57cec5SDimitry Andric   if (platform)
2630b57cec5SDimitry Andric     return platform->GetAugmentedArchSpec(triple);
2640b57cec5SDimitry Andric   return HostInfo::GetAugmentedArchSpec(triple);
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric /// Default Constructor
Platform(bool is_host)2680b57cec5SDimitry Andric Platform::Platform(bool is_host)
2690b57cec5SDimitry Andric     : m_is_host(is_host), m_os_version_set_while_connected(false),
27081ad6265SDimitry Andric       m_system_arch_set_while_connected(false), m_max_uid_name_len(0),
27181ad6265SDimitry Andric       m_max_gid_name_len(0), m_supports_rsync(false), m_rsync_opts(),
27281ad6265SDimitry Andric       m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
2730b57cec5SDimitry Andric       m_ignores_remote_hostname(false), m_trap_handlers(),
2740b57cec5SDimitry Andric       m_calculated_trap_handlers(false),
2759dba64beSDimitry Andric       m_module_cache(std::make_unique<ModuleCache>()) {
27681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
2779dba64beSDimitry Andric   LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this));
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric 
280349cc55cSDimitry Andric Platform::~Platform() = default;
2810b57cec5SDimitry Andric 
GetStatus(Stream & strm)2820b57cec5SDimitry Andric void Platform::GetStatus(Stream &strm) {
283349cc55cSDimitry Andric   strm.Format("  Platform: {0}\n", GetPluginName());
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   ArchSpec arch(GetSystemArchitecture());
2860b57cec5SDimitry Andric   if (arch.IsValid()) {
2870b57cec5SDimitry Andric     if (!arch.GetTriple().str().empty()) {
2880b57cec5SDimitry Andric       strm.Printf("    Triple: ");
289480093f4SDimitry Andric       arch.DumpTriple(strm.AsRawOstream());
2900b57cec5SDimitry Andric       strm.EOL();
2910b57cec5SDimitry Andric     }
2920b57cec5SDimitry Andric   }
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   llvm::VersionTuple os_version = GetOSVersion();
2950b57cec5SDimitry Andric   if (!os_version.empty()) {
2960b57cec5SDimitry Andric     strm.Format("OS Version: {0}", os_version.getAsString());
2970b57cec5SDimitry Andric 
298bdd1243dSDimitry Andric     if (std::optional<std::string> s = GetOSBuildString())
299349cc55cSDimitry Andric       strm.Format(" ({0})", *s);
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric     strm.EOL();
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   if (IsHost()) {
3050b57cec5SDimitry Andric     strm.Printf("  Hostname: %s\n", GetHostname());
3060b57cec5SDimitry Andric   } else {
3070b57cec5SDimitry Andric     const bool is_connected = IsConnected();
3080b57cec5SDimitry Andric     if (is_connected)
3090b57cec5SDimitry Andric       strm.Printf("  Hostname: %s\n", GetHostname());
3100b57cec5SDimitry Andric     strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
3110b57cec5SDimitry Andric   }
3120b57cec5SDimitry Andric 
31306c3fb27SDimitry Andric   if (const std::string &sdk_root = GetSDKRootDirectory(); !sdk_root.empty())
31406c3fb27SDimitry Andric     strm.Format("   Sysroot: {0}\n", sdk_root);
31506c3fb27SDimitry Andric 
3160b57cec5SDimitry Andric   if (GetWorkingDirectory()) {
317bdd1243dSDimitry Andric     strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetPath().c_str());
3180b57cec5SDimitry Andric   }
3190b57cec5SDimitry Andric   if (!IsConnected())
3200b57cec5SDimitry Andric     return;
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   std::string specific_info(GetPlatformSpecificConnectionInformation());
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   if (!specific_info.empty())
3250b57cec5SDimitry Andric     strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
3265ffd83dbSDimitry Andric 
327bdd1243dSDimitry Andric   if (std::optional<std::string> s = GetOSKernelDescription())
328349cc55cSDimitry Andric     strm.Format("    Kernel: {0}\n", *s);
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
GetOSVersion(Process * process)3310b57cec5SDimitry Andric llvm::VersionTuple Platform::GetOSVersion(Process *process) {
3320b57cec5SDimitry Andric   std::lock_guard<std::mutex> guard(m_mutex);
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   if (IsHost()) {
3350b57cec5SDimitry Andric     if (m_os_version.empty()) {
3360b57cec5SDimitry Andric       // We have a local host platform
3370b57cec5SDimitry Andric       m_os_version = HostInfo::GetOSVersion();
3380b57cec5SDimitry Andric       m_os_version_set_while_connected = !m_os_version.empty();
3390b57cec5SDimitry Andric     }
3400b57cec5SDimitry Andric   } else {
3410b57cec5SDimitry Andric     // We have a remote platform. We can only fetch the remote
3420b57cec5SDimitry Andric     // OS version if we are connected, and we don't want to do it
3430b57cec5SDimitry Andric     // more than once.
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric     const bool is_connected = IsConnected();
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     bool fetch = false;
3480b57cec5SDimitry Andric     if (!m_os_version.empty()) {
3490b57cec5SDimitry Andric       // We have valid OS version info, check to make sure it wasn't manually
3500b57cec5SDimitry Andric       // set prior to connecting. If it was manually set prior to connecting,
3510b57cec5SDimitry Andric       // then lets fetch the actual OS version info if we are now connected.
3520b57cec5SDimitry Andric       if (is_connected && !m_os_version_set_while_connected)
3530b57cec5SDimitry Andric         fetch = true;
3540b57cec5SDimitry Andric     } else {
3550b57cec5SDimitry Andric       // We don't have valid OS version info, fetch it if we are connected
3560b57cec5SDimitry Andric       fetch = is_connected;
3570b57cec5SDimitry Andric     }
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric     if (fetch)
3600b57cec5SDimitry Andric       m_os_version_set_while_connected = GetRemoteOSVersion();
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   if (!m_os_version.empty())
3640b57cec5SDimitry Andric     return m_os_version;
3650b57cec5SDimitry Andric   if (process) {
3660b57cec5SDimitry Andric     // Check with the process in case it can answer the question if a process
3670b57cec5SDimitry Andric     // was provided
3680b57cec5SDimitry Andric     return process->GetHostOSVersion();
3690b57cec5SDimitry Andric   }
3700b57cec5SDimitry Andric   return llvm::VersionTuple();
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
GetOSBuildString()373bdd1243dSDimitry Andric std::optional<std::string> Platform::GetOSBuildString() {
3740b57cec5SDimitry Andric   if (IsHost())
375349cc55cSDimitry Andric     return HostInfo::GetOSBuildString();
376349cc55cSDimitry Andric   return GetRemoteOSBuildString();
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric 
GetOSKernelDescription()379bdd1243dSDimitry Andric std::optional<std::string> Platform::GetOSKernelDescription() {
3800b57cec5SDimitry Andric   if (IsHost())
381349cc55cSDimitry Andric     return HostInfo::GetOSKernelDescription();
382349cc55cSDimitry Andric   return GetRemoteOSKernelDescription();
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
AddClangModuleCompilationOptions(Target * target,std::vector<std::string> & options)3850b57cec5SDimitry Andric void Platform::AddClangModuleCompilationOptions(
3860b57cec5SDimitry Andric     Target *target, std::vector<std::string> &options) {
3870b57cec5SDimitry Andric   std::vector<std::string> default_compilation_options = {
3880b57cec5SDimitry Andric       "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   options.insert(options.end(), default_compilation_options.begin(),
3910b57cec5SDimitry Andric                  default_compilation_options.end());
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
GetWorkingDirectory()3940b57cec5SDimitry Andric FileSpec Platform::GetWorkingDirectory() {
3950b57cec5SDimitry Andric   if (IsHost()) {
3960b57cec5SDimitry Andric     llvm::SmallString<64> cwd;
3970b57cec5SDimitry Andric     if (llvm::sys::fs::current_path(cwd))
3980b57cec5SDimitry Andric       return {};
3990b57cec5SDimitry Andric     else {
4000b57cec5SDimitry Andric       FileSpec file_spec(cwd);
4010b57cec5SDimitry Andric       FileSystem::Instance().Resolve(file_spec);
4020b57cec5SDimitry Andric       return file_spec;
4030b57cec5SDimitry Andric     }
4040b57cec5SDimitry Andric   } else {
4050b57cec5SDimitry Andric     if (!m_working_dir)
4060b57cec5SDimitry Andric       m_working_dir = GetRemoteWorkingDirectory();
4070b57cec5SDimitry Andric     return m_working_dir;
4080b57cec5SDimitry Andric   }
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric struct RecurseCopyBaton {
4120b57cec5SDimitry Andric   const FileSpec &dst;
4130b57cec5SDimitry Andric   Platform *platform_ptr;
4140b57cec5SDimitry Andric   Status error;
4150b57cec5SDimitry Andric };
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric static FileSystem::EnumerateDirectoryResult
RecurseCopy_Callback(void * baton,llvm::sys::fs::file_type ft,llvm::StringRef path)4180b57cec5SDimitry Andric RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
4190b57cec5SDimitry Andric                      llvm::StringRef path) {
4200b57cec5SDimitry Andric   RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
4210b57cec5SDimitry Andric   FileSpec src(path);
4220b57cec5SDimitry Andric   namespace fs = llvm::sys::fs;
4230b57cec5SDimitry Andric   switch (ft) {
4240b57cec5SDimitry Andric   case fs::file_type::fifo_file:
4250b57cec5SDimitry Andric   case fs::file_type::socket_file:
4260b57cec5SDimitry Andric     // we have no way to copy pipes and sockets - ignore them and continue
4270b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
4280b57cec5SDimitry Andric     break;
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   case fs::file_type::directory_file: {
4310b57cec5SDimitry Andric     // make the new directory and get in there
4320b57cec5SDimitry Andric     FileSpec dst_dir = rc_baton->dst;
4330b57cec5SDimitry Andric     if (!dst_dir.GetFilename())
43406c3fb27SDimitry Andric       dst_dir.SetFilename(src.GetFilename());
4350b57cec5SDimitry Andric     Status error = rc_baton->platform_ptr->MakeDirectory(
4360b57cec5SDimitry Andric         dst_dir, lldb::eFilePermissionsDirectoryDefault);
4370b57cec5SDimitry Andric     if (error.Fail()) {
4380b57cec5SDimitry Andric       rc_baton->error.SetErrorStringWithFormat(
439bdd1243dSDimitry Andric           "unable to setup directory %s on remote end",
440bdd1243dSDimitry Andric           dst_dir.GetPath().c_str());
4410b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4420b57cec5SDimitry Andric     }
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric     // now recurse
4450b57cec5SDimitry Andric     std::string src_dir_path(src.GetPath());
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric     // Make a filespec that only fills in the directory of a FileSpec so when
4480b57cec5SDimitry Andric     // we enumerate we can quickly fill in the filename for dst copies
4490b57cec5SDimitry Andric     FileSpec recurse_dst;
450bdd1243dSDimitry Andric     recurse_dst.SetDirectory(dst_dir.GetPathAsConstString());
4510b57cec5SDimitry Andric     RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
4520b57cec5SDimitry Andric                                   Status()};
4530b57cec5SDimitry Andric     FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
4540b57cec5SDimitry Andric                                               RecurseCopy_Callback, &rc_baton2);
4550b57cec5SDimitry Andric     if (rc_baton2.error.Fail()) {
4560b57cec5SDimitry Andric       rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
4570b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4580b57cec5SDimitry Andric     }
4590b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
4600b57cec5SDimitry Andric   } break;
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric   case fs::file_type::symlink_file: {
4630b57cec5SDimitry Andric     // copy the file and keep going
4640b57cec5SDimitry Andric     FileSpec dst_file = rc_baton->dst;
4650b57cec5SDimitry Andric     if (!dst_file.GetFilename())
466bdd1243dSDimitry Andric       dst_file.SetFilename(src.GetFilename());
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric     FileSpec src_resolved;
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric     rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric     if (rc_baton->error.Fail())
4730b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric     rc_baton->error =
4760b57cec5SDimitry Andric         rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric     if (rc_baton->error.Fail())
4790b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
4820b57cec5SDimitry Andric   } break;
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric   case fs::file_type::regular_file: {
4850b57cec5SDimitry Andric     // copy the file and keep going
4860b57cec5SDimitry Andric     FileSpec dst_file = rc_baton->dst;
4870b57cec5SDimitry Andric     if (!dst_file.GetFilename())
488bdd1243dSDimitry Andric       dst_file.SetFilename(src.GetFilename());
4890b57cec5SDimitry Andric     Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
4900b57cec5SDimitry Andric     if (err.Fail()) {
4910b57cec5SDimitry Andric       rc_baton->error.SetErrorString(err.AsCString());
4920b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4930b57cec5SDimitry Andric     }
4940b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
4950b57cec5SDimitry Andric   } break;
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   default:
4980b57cec5SDimitry Andric     rc_baton->error.SetErrorStringWithFormat(
4990b57cec5SDimitry Andric         "invalid file detected during copy: %s", src.GetPath().c_str());
5000b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
5010b57cec5SDimitry Andric     break;
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric   llvm_unreachable("Unhandled file_type!");
5040b57cec5SDimitry Andric }
5050b57cec5SDimitry Andric 
Install(const FileSpec & src,const FileSpec & dst)5060b57cec5SDimitry Andric Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
5070b57cec5SDimitry Andric   Status error;
5080b57cec5SDimitry Andric 
50981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
5109dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')",
5119dba64beSDimitry Andric             src.GetPath().c_str(), dst.GetPath().c_str());
5120b57cec5SDimitry Andric   FileSpec fixed_dst(dst);
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric   if (!fixed_dst.GetFilename())
515bdd1243dSDimitry Andric     fixed_dst.SetFilename(src.GetFilename());
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   FileSpec working_dir = GetWorkingDirectory();
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   if (dst) {
5200b57cec5SDimitry Andric     if (dst.GetDirectory()) {
5210b57cec5SDimitry Andric       const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
5220b57cec5SDimitry Andric       if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
523bdd1243dSDimitry Andric         fixed_dst.SetDirectory(dst.GetDirectory());
5240b57cec5SDimitry Andric       }
5250b57cec5SDimitry Andric       // If the fixed destination file doesn't have a directory yet, then we
5260b57cec5SDimitry Andric       // must have a relative path. We will resolve this relative path against
5270b57cec5SDimitry Andric       // the platform's working directory
5280b57cec5SDimitry Andric       if (!fixed_dst.GetDirectory()) {
5290b57cec5SDimitry Andric         FileSpec relative_spec;
5300b57cec5SDimitry Andric         std::string path;
5310b57cec5SDimitry Andric         if (working_dir) {
5320b57cec5SDimitry Andric           relative_spec = working_dir;
5330b57cec5SDimitry Andric           relative_spec.AppendPathComponent(dst.GetPath());
534bdd1243dSDimitry Andric           fixed_dst.SetDirectory(relative_spec.GetDirectory());
5350b57cec5SDimitry Andric         } else {
5360b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
5370b57cec5SDimitry Andric               "platform working directory must be valid for relative path '%s'",
5380b57cec5SDimitry Andric               dst.GetPath().c_str());
5390b57cec5SDimitry Andric           return error;
5400b57cec5SDimitry Andric         }
5410b57cec5SDimitry Andric       }
5420b57cec5SDimitry Andric     } else {
5430b57cec5SDimitry Andric       if (working_dir) {
544bdd1243dSDimitry Andric         fixed_dst.SetDirectory(working_dir.GetPathAsConstString());
5450b57cec5SDimitry Andric       } else {
5460b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
5470b57cec5SDimitry Andric             "platform working directory must be valid for relative path '%s'",
5480b57cec5SDimitry Andric             dst.GetPath().c_str());
5490b57cec5SDimitry Andric         return error;
5500b57cec5SDimitry Andric       }
5510b57cec5SDimitry Andric     }
5520b57cec5SDimitry Andric   } else {
5530b57cec5SDimitry Andric     if (working_dir) {
554bdd1243dSDimitry Andric       fixed_dst.SetDirectory(working_dir.GetPathAsConstString());
5550b57cec5SDimitry Andric     } else {
5560b57cec5SDimitry Andric       error.SetErrorStringWithFormat("platform working directory must be valid "
5570b57cec5SDimitry Andric                                      "when destination directory is empty");
5580b57cec5SDimitry Andric       return error;
5590b57cec5SDimitry Andric     }
5600b57cec5SDimitry Andric   }
5610b57cec5SDimitry Andric 
5629dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
5630b57cec5SDimitry Andric             src.GetPath().c_str(), dst.GetPath().c_str(),
5640b57cec5SDimitry Andric             fixed_dst.GetPath().c_str());
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric   if (GetSupportsRSync()) {
5670b57cec5SDimitry Andric     error = PutFile(src, dst);
5680b57cec5SDimitry Andric   } else {
5690b57cec5SDimitry Andric     namespace fs = llvm::sys::fs;
5700b57cec5SDimitry Andric     switch (fs::get_file_type(src.GetPath(), false)) {
5710b57cec5SDimitry Andric     case fs::file_type::directory_file: {
5720b57cec5SDimitry Andric       llvm::sys::fs::remove(fixed_dst.GetPath());
5730b57cec5SDimitry Andric       uint32_t permissions = FileSystem::Instance().GetPermissions(src);
5740b57cec5SDimitry Andric       if (permissions == 0)
5750b57cec5SDimitry Andric         permissions = eFilePermissionsDirectoryDefault;
5760b57cec5SDimitry Andric       error = MakeDirectory(fixed_dst, permissions);
5770b57cec5SDimitry Andric       if (error.Success()) {
5780b57cec5SDimitry Andric         // Make a filespec that only fills in the directory of a FileSpec so
5790b57cec5SDimitry Andric         // when we enumerate we can quickly fill in the filename for dst copies
5800b57cec5SDimitry Andric         FileSpec recurse_dst;
581bdd1243dSDimitry Andric         recurse_dst.SetDirectory(fixed_dst.GetPathAsConstString());
5820b57cec5SDimitry Andric         std::string src_dir_path(src.GetPath());
5830b57cec5SDimitry Andric         RecurseCopyBaton baton = {recurse_dst, this, Status()};
5840b57cec5SDimitry Andric         FileSystem::Instance().EnumerateDirectory(
5850b57cec5SDimitry Andric             src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
5860b57cec5SDimitry Andric         return baton.error;
5870b57cec5SDimitry Andric       }
5880b57cec5SDimitry Andric     } break;
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric     case fs::file_type::regular_file:
5910b57cec5SDimitry Andric       llvm::sys::fs::remove(fixed_dst.GetPath());
5920b57cec5SDimitry Andric       error = PutFile(src, fixed_dst);
5930b57cec5SDimitry Andric       break;
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric     case fs::file_type::symlink_file: {
5960b57cec5SDimitry Andric       llvm::sys::fs::remove(fixed_dst.GetPath());
5970b57cec5SDimitry Andric       FileSpec src_resolved;
5980b57cec5SDimitry Andric       error = FileSystem::Instance().Readlink(src, src_resolved);
5990b57cec5SDimitry Andric       if (error.Success())
6000b57cec5SDimitry Andric         error = CreateSymlink(dst, src_resolved);
6010b57cec5SDimitry Andric     } break;
6020b57cec5SDimitry Andric     case fs::file_type::fifo_file:
6030b57cec5SDimitry Andric       error.SetErrorString("platform install doesn't handle pipes");
6040b57cec5SDimitry Andric       break;
6050b57cec5SDimitry Andric     case fs::file_type::socket_file:
6060b57cec5SDimitry Andric       error.SetErrorString("platform install doesn't handle sockets");
6070b57cec5SDimitry Andric       break;
6080b57cec5SDimitry Andric     default:
6090b57cec5SDimitry Andric       error.SetErrorString(
6100b57cec5SDimitry Andric           "platform install doesn't handle non file or directory items");
6110b57cec5SDimitry Andric       break;
6120b57cec5SDimitry Andric     }
6130b57cec5SDimitry Andric   }
6140b57cec5SDimitry Andric   return error;
6150b57cec5SDimitry Andric }
6160b57cec5SDimitry Andric 
SetWorkingDirectory(const FileSpec & file_spec)6170b57cec5SDimitry Andric bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
6180b57cec5SDimitry Andric   if (IsHost()) {
61981ad6265SDimitry Andric     Log *log = GetLog(LLDBLog::Platform);
6200b57cec5SDimitry Andric     LLDB_LOG(log, "{0}", file_spec);
6210b57cec5SDimitry Andric     if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
6220b57cec5SDimitry Andric       LLDB_LOG(log, "error: {0}", ec.message());
6230b57cec5SDimitry Andric       return false;
6240b57cec5SDimitry Andric     }
6250b57cec5SDimitry Andric     return true;
6260b57cec5SDimitry Andric   } else {
6270b57cec5SDimitry Andric     m_working_dir.Clear();
6280b57cec5SDimitry Andric     return SetRemoteWorkingDirectory(file_spec);
6290b57cec5SDimitry Andric   }
6300b57cec5SDimitry Andric }
6310b57cec5SDimitry Andric 
MakeDirectory(const FileSpec & file_spec,uint32_t permissions)6320b57cec5SDimitry Andric Status Platform::MakeDirectory(const FileSpec &file_spec,
6330b57cec5SDimitry Andric                                uint32_t permissions) {
6340b57cec5SDimitry Andric   if (IsHost())
6350b57cec5SDimitry Andric     return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
6360b57cec5SDimitry Andric   else {
6370b57cec5SDimitry Andric     Status error;
638349cc55cSDimitry Andric     error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
639349cc55cSDimitry Andric                                     GetPluginName(), LLVM_PRETTY_FUNCTION);
6400b57cec5SDimitry Andric     return error;
6410b57cec5SDimitry Andric   }
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric 
GetFilePermissions(const FileSpec & file_spec,uint32_t & file_permissions)6440b57cec5SDimitry Andric Status Platform::GetFilePermissions(const FileSpec &file_spec,
6450b57cec5SDimitry Andric                                     uint32_t &file_permissions) {
6460b57cec5SDimitry Andric   if (IsHost()) {
6470b57cec5SDimitry Andric     auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
6480b57cec5SDimitry Andric     if (Value)
6490b57cec5SDimitry Andric       file_permissions = Value.get();
6500b57cec5SDimitry Andric     return Status(Value.getError());
6510b57cec5SDimitry Andric   } else {
6520b57cec5SDimitry Andric     Status error;
653349cc55cSDimitry Andric     error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
654349cc55cSDimitry Andric                                     GetPluginName(), LLVM_PRETTY_FUNCTION);
6550b57cec5SDimitry Andric     return error;
6560b57cec5SDimitry Andric   }
6570b57cec5SDimitry Andric }
6580b57cec5SDimitry Andric 
SetFilePermissions(const FileSpec & file_spec,uint32_t file_permissions)6590b57cec5SDimitry Andric Status Platform::SetFilePermissions(const FileSpec &file_spec,
6600b57cec5SDimitry Andric                                     uint32_t file_permissions) {
6610b57cec5SDimitry Andric   if (IsHost()) {
6620b57cec5SDimitry Andric     auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
6630b57cec5SDimitry Andric     return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
6640b57cec5SDimitry Andric   } else {
6650b57cec5SDimitry Andric     Status error;
666349cc55cSDimitry Andric     error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
667349cc55cSDimitry Andric                                     GetPluginName(), LLVM_PRETTY_FUNCTION);
6680b57cec5SDimitry Andric     return error;
6690b57cec5SDimitry Andric   }
6700b57cec5SDimitry Andric }
6710b57cec5SDimitry Andric 
OpenFile(const FileSpec & file_spec,File::OpenOptions flags,uint32_t mode,Status & error)67281ad6265SDimitry Andric user_id_t Platform::OpenFile(const FileSpec &file_spec,
67381ad6265SDimitry Andric                                    File::OpenOptions flags, uint32_t mode,
67481ad6265SDimitry Andric                                    Status &error) {
67581ad6265SDimitry Andric   if (IsHost())
67681ad6265SDimitry Andric     return FileCache::GetInstance().OpenFile(file_spec, flags, mode, error);
67781ad6265SDimitry Andric   return UINT64_MAX;
67881ad6265SDimitry Andric }
67981ad6265SDimitry Andric 
CloseFile(user_id_t fd,Status & error)68081ad6265SDimitry Andric bool Platform::CloseFile(user_id_t fd, Status &error) {
68181ad6265SDimitry Andric   if (IsHost())
68281ad6265SDimitry Andric     return FileCache::GetInstance().CloseFile(fd, error);
68381ad6265SDimitry Andric   return false;
68481ad6265SDimitry Andric }
68581ad6265SDimitry Andric 
GetFileSize(const FileSpec & file_spec)68681ad6265SDimitry Andric user_id_t Platform::GetFileSize(const FileSpec &file_spec) {
68781ad6265SDimitry Andric   if (!IsHost())
68881ad6265SDimitry Andric     return UINT64_MAX;
68981ad6265SDimitry Andric 
69081ad6265SDimitry Andric   uint64_t Size;
69181ad6265SDimitry Andric   if (llvm::sys::fs::file_size(file_spec.GetPath(), Size))
69281ad6265SDimitry Andric     return 0;
69381ad6265SDimitry Andric   return Size;
69481ad6265SDimitry Andric }
69581ad6265SDimitry Andric 
ReadFile(lldb::user_id_t fd,uint64_t offset,void * dst,uint64_t dst_len,Status & error)69681ad6265SDimitry Andric uint64_t Platform::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
69781ad6265SDimitry Andric                             uint64_t dst_len, Status &error) {
69881ad6265SDimitry Andric   if (IsHost())
69981ad6265SDimitry Andric     return FileCache::GetInstance().ReadFile(fd, offset, dst, dst_len, error);
70081ad6265SDimitry Andric   error.SetErrorStringWithFormatv(
70181ad6265SDimitry Andric       "Platform::ReadFile() is not supported in the {0} platform",
70281ad6265SDimitry Andric       GetPluginName());
70381ad6265SDimitry Andric   return -1;
70481ad6265SDimitry Andric }
70581ad6265SDimitry Andric 
WriteFile(lldb::user_id_t fd,uint64_t offset,const void * src,uint64_t src_len,Status & error)70681ad6265SDimitry Andric uint64_t Platform::WriteFile(lldb::user_id_t fd, uint64_t offset,
70781ad6265SDimitry Andric                              const void *src, uint64_t src_len, Status &error) {
70881ad6265SDimitry Andric   if (IsHost())
70981ad6265SDimitry Andric     return FileCache::GetInstance().WriteFile(fd, offset, src, src_len, error);
71081ad6265SDimitry Andric   error.SetErrorStringWithFormatv(
71181ad6265SDimitry Andric       "Platform::WriteFile() is not supported in the {0} platform",
71281ad6265SDimitry Andric       GetPluginName());
71381ad6265SDimitry Andric   return -1;
71481ad6265SDimitry Andric }
71581ad6265SDimitry Andric 
GetUserIDResolver()71681ad6265SDimitry Andric UserIDResolver &Platform::GetUserIDResolver() {
71781ad6265SDimitry Andric   if (IsHost())
71881ad6265SDimitry Andric     return HostInfo::GetUserIDResolver();
71981ad6265SDimitry Andric   return UserIDResolver::GetNoopResolver();
72081ad6265SDimitry Andric }
7210b57cec5SDimitry Andric 
GetHostname()7220b57cec5SDimitry Andric const char *Platform::GetHostname() {
7230b57cec5SDimitry Andric   if (IsHost())
7240b57cec5SDimitry Andric     return "127.0.0.1";
7250b57cec5SDimitry Andric 
72681ad6265SDimitry Andric   if (m_hostname.empty())
7270b57cec5SDimitry Andric     return nullptr;
72881ad6265SDimitry Andric   return m_hostname.c_str();
7290b57cec5SDimitry Andric }
7300b57cec5SDimitry Andric 
GetFullNameForDylib(ConstString basename)7310b57cec5SDimitry Andric ConstString Platform::GetFullNameForDylib(ConstString basename) {
7320b57cec5SDimitry Andric   return basename;
7330b57cec5SDimitry Andric }
7340b57cec5SDimitry Andric 
SetRemoteWorkingDirectory(const FileSpec & working_dir)7350b57cec5SDimitry Andric bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
73681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
7379dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')",
738bdd1243dSDimitry Andric             working_dir.GetPath().c_str());
7390b57cec5SDimitry Andric   m_working_dir = working_dir;
7400b57cec5SDimitry Andric   return true;
7410b57cec5SDimitry Andric }
7420b57cec5SDimitry Andric 
SetOSVersion(llvm::VersionTuple version)7430b57cec5SDimitry Andric bool Platform::SetOSVersion(llvm::VersionTuple version) {
7440b57cec5SDimitry Andric   if (IsHost()) {
7450b57cec5SDimitry Andric     // We don't need anyone setting the OS version for the host platform, we
7460b57cec5SDimitry Andric     // should be able to figure it out by calling HostInfo::GetOSVersion(...).
7470b57cec5SDimitry Andric     return false;
7480b57cec5SDimitry Andric   } else {
7490b57cec5SDimitry Andric     // We have a remote platform, allow setting the target OS version if we
7500b57cec5SDimitry Andric     // aren't connected, since if we are connected, we should be able to
7510b57cec5SDimitry Andric     // request the remote OS version from the connected platform.
7520b57cec5SDimitry Andric     if (IsConnected())
7530b57cec5SDimitry Andric       return false;
7540b57cec5SDimitry Andric     else {
7550b57cec5SDimitry Andric       // We aren't connected and we might want to set the OS version ahead of
7560b57cec5SDimitry Andric       // time before we connect so we can peruse files and use a local SDK or
7570b57cec5SDimitry Andric       // PDK cache of support files to disassemble or do other things.
7580b57cec5SDimitry Andric       m_os_version = version;
7590b57cec5SDimitry Andric       return true;
7600b57cec5SDimitry Andric     }
7610b57cec5SDimitry Andric   }
7620b57cec5SDimitry Andric   return false;
7630b57cec5SDimitry Andric }
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric Status
ResolveExecutable(const ModuleSpec & module_spec,lldb::ModuleSP & exe_module_sp,const FileSpecList * module_search_paths_ptr)7660b57cec5SDimitry Andric Platform::ResolveExecutable(const ModuleSpec &module_spec,
7670b57cec5SDimitry Andric                             lldb::ModuleSP &exe_module_sp,
7680b57cec5SDimitry Andric                             const FileSpecList *module_search_paths_ptr) {
7690b57cec5SDimitry Andric   Status error;
770349cc55cSDimitry Andric 
7710b57cec5SDimitry Andric   if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
7720b57cec5SDimitry Andric     if (module_spec.GetArchitecture().IsValid()) {
7730b57cec5SDimitry Andric       error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
7740b57cec5SDimitry Andric                                           module_search_paths_ptr, nullptr,
7750b57cec5SDimitry Andric                                           nullptr);
7760b57cec5SDimitry Andric     } else {
7770b57cec5SDimitry Andric       // No valid architecture was specified, ask the platform for the
7780b57cec5SDimitry Andric       // architectures that we should be using (in the correct order) and see
7790b57cec5SDimitry Andric       // if we can find a match that way
7800b57cec5SDimitry Andric       ModuleSpec arch_module_spec(module_spec);
78181ad6265SDimitry Andric       ArchSpec process_host_arch;
78281ad6265SDimitry Andric       for (const ArchSpec &arch :
78381ad6265SDimitry Andric            GetSupportedArchitectures(process_host_arch)) {
784349cc55cSDimitry Andric         arch_module_spec.GetArchitecture() = arch;
7850b57cec5SDimitry Andric         error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
7860b57cec5SDimitry Andric                                             module_search_paths_ptr, nullptr,
7870b57cec5SDimitry Andric                                             nullptr);
7880b57cec5SDimitry Andric         // Did we find an executable using one of the
7890b57cec5SDimitry Andric         if (error.Success() && exe_module_sp)
7900b57cec5SDimitry Andric           break;
7910b57cec5SDimitry Andric       }
7920b57cec5SDimitry Andric     }
7930b57cec5SDimitry Andric   } else {
794349cc55cSDimitry Andric     error.SetErrorStringWithFormat(
795349cc55cSDimitry Andric         "'%s' does not exist", module_spec.GetFileSpec().GetPath().c_str());
7960b57cec5SDimitry Andric   }
7970b57cec5SDimitry Andric   return error;
7980b57cec5SDimitry Andric }
7990b57cec5SDimitry Andric 
800349cc55cSDimitry Andric Status
ResolveRemoteExecutable(const ModuleSpec & module_spec,lldb::ModuleSP & exe_module_sp,const FileSpecList * module_search_paths_ptr)801349cc55cSDimitry Andric Platform::ResolveRemoteExecutable(const ModuleSpec &module_spec,
802349cc55cSDimitry Andric                             lldb::ModuleSP &exe_module_sp,
803349cc55cSDimitry Andric                             const FileSpecList *module_search_paths_ptr) {
804349cc55cSDimitry Andric   Status error;
805349cc55cSDimitry Andric 
806349cc55cSDimitry Andric   // We may connect to a process and use the provided executable (Don't use
807349cc55cSDimitry Andric   // local $PATH).
808349cc55cSDimitry Andric   ModuleSpec resolved_module_spec(module_spec);
809349cc55cSDimitry Andric 
810349cc55cSDimitry Andric   // Resolve any executable within a bundle on MacOSX
811349cc55cSDimitry Andric   Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
812349cc55cSDimitry Andric 
813349cc55cSDimitry Andric   if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()) ||
814349cc55cSDimitry Andric       module_spec.GetUUID().IsValid()) {
815349cc55cSDimitry Andric     if (resolved_module_spec.GetArchitecture().IsValid() ||
816349cc55cSDimitry Andric         resolved_module_spec.GetUUID().IsValid()) {
817349cc55cSDimitry Andric       error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
818349cc55cSDimitry Andric                                           module_search_paths_ptr, nullptr,
819349cc55cSDimitry Andric                                           nullptr);
820349cc55cSDimitry Andric 
821349cc55cSDimitry Andric       if (exe_module_sp && exe_module_sp->GetObjectFile())
822349cc55cSDimitry Andric         return error;
823349cc55cSDimitry Andric       exe_module_sp.reset();
824349cc55cSDimitry Andric     }
825349cc55cSDimitry Andric     // No valid architecture was specified or the exact arch wasn't found so
826349cc55cSDimitry Andric     // ask the platform for the architectures that we should be using (in the
827349cc55cSDimitry Andric     // correct order) and see if we can find a match that way
828349cc55cSDimitry Andric     StreamString arch_names;
829349cc55cSDimitry Andric     llvm::ListSeparator LS;
83081ad6265SDimitry Andric     ArchSpec process_host_arch;
83181ad6265SDimitry Andric     for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
832349cc55cSDimitry Andric       resolved_module_spec.GetArchitecture() = arch;
833349cc55cSDimitry Andric       error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
834349cc55cSDimitry Andric                                           module_search_paths_ptr, nullptr,
835349cc55cSDimitry Andric                                           nullptr);
836349cc55cSDimitry Andric       // Did we find an executable using one of the
837349cc55cSDimitry Andric       if (error.Success()) {
838349cc55cSDimitry Andric         if (exe_module_sp && exe_module_sp->GetObjectFile())
839349cc55cSDimitry Andric           break;
840349cc55cSDimitry Andric         else
841349cc55cSDimitry Andric           error.SetErrorToGenericError();
842349cc55cSDimitry Andric       }
843349cc55cSDimitry Andric 
844349cc55cSDimitry Andric       arch_names << LS << arch.GetArchitectureName();
845349cc55cSDimitry Andric     }
846349cc55cSDimitry Andric 
847349cc55cSDimitry Andric     if (error.Fail() || !exe_module_sp) {
848349cc55cSDimitry Andric       if (FileSystem::Instance().Readable(resolved_module_spec.GetFileSpec())) {
849349cc55cSDimitry Andric         error.SetErrorStringWithFormatv(
850349cc55cSDimitry Andric             "'{0}' doesn't contain any '{1}' platform architectures: {2}",
851349cc55cSDimitry Andric             resolved_module_spec.GetFileSpec(), GetPluginName(),
852349cc55cSDimitry Andric             arch_names.GetData());
853349cc55cSDimitry Andric       } else {
854349cc55cSDimitry Andric         error.SetErrorStringWithFormatv("'{0}' is not readable",
855349cc55cSDimitry Andric                                         resolved_module_spec.GetFileSpec());
856349cc55cSDimitry Andric       }
857349cc55cSDimitry Andric     }
858349cc55cSDimitry Andric   } else {
859349cc55cSDimitry Andric     error.SetErrorStringWithFormatv("'{0}' does not exist",
860349cc55cSDimitry Andric                                     resolved_module_spec.GetFileSpec());
861349cc55cSDimitry Andric   }
862349cc55cSDimitry Andric 
863349cc55cSDimitry Andric   return error;
864349cc55cSDimitry Andric }
865349cc55cSDimitry Andric 
ResolveSymbolFile(Target & target,const ModuleSpec & sym_spec,FileSpec & sym_file)8660b57cec5SDimitry Andric Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
8670b57cec5SDimitry Andric                                    FileSpec &sym_file) {
8680b57cec5SDimitry Andric   Status error;
8690b57cec5SDimitry Andric   if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
8700b57cec5SDimitry Andric     sym_file = sym_spec.GetSymbolFileSpec();
8710b57cec5SDimitry Andric   else
8720b57cec5SDimitry Andric     error.SetErrorString("unable to resolve symbol file");
8730b57cec5SDimitry Andric   return error;
8740b57cec5SDimitry Andric }
8750b57cec5SDimitry Andric 
ResolveRemotePath(const FileSpec & platform_path,FileSpec & resolved_platform_path)8760b57cec5SDimitry Andric bool Platform::ResolveRemotePath(const FileSpec &platform_path,
8770b57cec5SDimitry Andric                                  FileSpec &resolved_platform_path) {
8780b57cec5SDimitry Andric   resolved_platform_path = platform_path;
8790b57cec5SDimitry Andric   FileSystem::Instance().Resolve(resolved_platform_path);
8800b57cec5SDimitry Andric   return true;
8810b57cec5SDimitry Andric }
8820b57cec5SDimitry Andric 
GetSystemArchitecture()8830b57cec5SDimitry Andric const ArchSpec &Platform::GetSystemArchitecture() {
8840b57cec5SDimitry Andric   if (IsHost()) {
8850b57cec5SDimitry Andric     if (!m_system_arch.IsValid()) {
8860b57cec5SDimitry Andric       // We have a local host platform
8870b57cec5SDimitry Andric       m_system_arch = HostInfo::GetArchitecture();
8880b57cec5SDimitry Andric       m_system_arch_set_while_connected = m_system_arch.IsValid();
8890b57cec5SDimitry Andric     }
8900b57cec5SDimitry Andric   } else {
8910b57cec5SDimitry Andric     // We have a remote platform. We can only fetch the remote system
8920b57cec5SDimitry Andric     // architecture if we are connected, and we don't want to do it more than
8930b57cec5SDimitry Andric     // once.
8940b57cec5SDimitry Andric 
8950b57cec5SDimitry Andric     const bool is_connected = IsConnected();
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric     bool fetch = false;
8980b57cec5SDimitry Andric     if (m_system_arch.IsValid()) {
8990b57cec5SDimitry Andric       // We have valid OS version info, check to make sure it wasn't manually
9000b57cec5SDimitry Andric       // set prior to connecting. If it was manually set prior to connecting,
9010b57cec5SDimitry Andric       // then lets fetch the actual OS version info if we are now connected.
9020b57cec5SDimitry Andric       if (is_connected && !m_system_arch_set_while_connected)
9030b57cec5SDimitry Andric         fetch = true;
9040b57cec5SDimitry Andric     } else {
9050b57cec5SDimitry Andric       // We don't have valid OS version info, fetch it if we are connected
9060b57cec5SDimitry Andric       fetch = is_connected;
9070b57cec5SDimitry Andric     }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric     if (fetch) {
9100b57cec5SDimitry Andric       m_system_arch = GetRemoteSystemArchitecture();
9110b57cec5SDimitry Andric       m_system_arch_set_while_connected = m_system_arch.IsValid();
9120b57cec5SDimitry Andric     }
9130b57cec5SDimitry Andric   }
9140b57cec5SDimitry Andric   return m_system_arch;
9150b57cec5SDimitry Andric }
9160b57cec5SDimitry Andric 
GetAugmentedArchSpec(llvm::StringRef triple)9170b57cec5SDimitry Andric ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
9180b57cec5SDimitry Andric   if (triple.empty())
9190b57cec5SDimitry Andric     return ArchSpec();
9200b57cec5SDimitry Andric   llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
9210b57cec5SDimitry Andric   if (!ArchSpec::ContainsOnlyArch(normalized_triple))
9220b57cec5SDimitry Andric     return ArchSpec(triple);
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric   if (auto kind = HostInfo::ParseArchitectureKind(triple))
9250b57cec5SDimitry Andric     return HostInfo::GetArchitecture(*kind);
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric   ArchSpec compatible_arch;
9280b57cec5SDimitry Andric   ArchSpec raw_arch(triple);
929bdd1243dSDimitry Andric   if (!IsCompatibleArchitecture(raw_arch, {}, ArchSpec::CompatibleMatch,
930bdd1243dSDimitry Andric                                 &compatible_arch))
9310b57cec5SDimitry Andric     return raw_arch;
9320b57cec5SDimitry Andric 
9330b57cec5SDimitry Andric   if (!compatible_arch.IsValid())
9340b57cec5SDimitry Andric     return ArchSpec(normalized_triple);
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric   const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
9370b57cec5SDimitry Andric   if (normalized_triple.getVendorName().empty())
9380b57cec5SDimitry Andric     normalized_triple.setVendor(compatible_triple.getVendor());
9390b57cec5SDimitry Andric   if (normalized_triple.getOSName().empty())
9400b57cec5SDimitry Andric     normalized_triple.setOS(compatible_triple.getOS());
9410b57cec5SDimitry Andric   if (normalized_triple.getEnvironmentName().empty())
9420b57cec5SDimitry Andric     normalized_triple.setEnvironment(compatible_triple.getEnvironment());
9430b57cec5SDimitry Andric   return ArchSpec(normalized_triple);
9440b57cec5SDimitry Andric }
9450b57cec5SDimitry Andric 
ConnectRemote(Args & args)9460b57cec5SDimitry Andric Status Platform::ConnectRemote(Args &args) {
9470b57cec5SDimitry Andric   Status error;
9480b57cec5SDimitry Andric   if (IsHost())
949349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
950349cc55cSDimitry Andric         "The currently selected platform ({0}) is "
9510b57cec5SDimitry Andric         "the host platform and is always connected.",
952349cc55cSDimitry Andric         GetPluginName());
9530b57cec5SDimitry Andric   else
954349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
955349cc55cSDimitry Andric         "Platform::ConnectRemote() is not supported by {0}", GetPluginName());
9560b57cec5SDimitry Andric   return error;
9570b57cec5SDimitry Andric }
9580b57cec5SDimitry Andric 
DisconnectRemote()9590b57cec5SDimitry Andric Status Platform::DisconnectRemote() {
9600b57cec5SDimitry Andric   Status error;
9610b57cec5SDimitry Andric   if (IsHost())
962349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
963349cc55cSDimitry Andric         "The currently selected platform ({0}) is "
9640b57cec5SDimitry Andric         "the host platform and is always connected.",
965349cc55cSDimitry Andric         GetPluginName());
9660b57cec5SDimitry Andric   else
967349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
968349cc55cSDimitry Andric         "Platform::DisconnectRemote() is not supported by {0}",
969349cc55cSDimitry Andric         GetPluginName());
9700b57cec5SDimitry Andric   return error;
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric 
GetProcessInfo(lldb::pid_t pid,ProcessInstanceInfo & process_info)9730b57cec5SDimitry Andric bool Platform::GetProcessInfo(lldb::pid_t pid,
9740b57cec5SDimitry Andric                               ProcessInstanceInfo &process_info) {
9750b57cec5SDimitry Andric   // Take care of the host case so that each subclass can just call this
9760b57cec5SDimitry Andric   // function to get the host functionality.
9770b57cec5SDimitry Andric   if (IsHost())
9780b57cec5SDimitry Andric     return Host::GetProcessInfo(pid, process_info);
9790b57cec5SDimitry Andric   return false;
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric 
FindProcesses(const ProcessInstanceInfoMatch & match_info,ProcessInstanceInfoList & process_infos)9820b57cec5SDimitry Andric uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
9830b57cec5SDimitry Andric                                  ProcessInstanceInfoList &process_infos) {
9840b57cec5SDimitry Andric   // Take care of the host case so that each subclass can just call this
9850b57cec5SDimitry Andric   // function to get the host functionality.
9860b57cec5SDimitry Andric   uint32_t match_count = 0;
9870b57cec5SDimitry Andric   if (IsHost())
9880b57cec5SDimitry Andric     match_count = Host::FindProcesses(match_info, process_infos);
9890b57cec5SDimitry Andric   return match_count;
9900b57cec5SDimitry Andric }
9910b57cec5SDimitry Andric 
GetAllProcesses()9925f757f3fSDimitry Andric ProcessInstanceInfoList Platform::GetAllProcesses() {
9935f757f3fSDimitry Andric   ProcessInstanceInfoList processes;
9945f757f3fSDimitry Andric   ProcessInstanceInfoMatch match;
9955f757f3fSDimitry Andric   assert(match.MatchAllProcesses());
9965f757f3fSDimitry Andric   FindProcesses(match, processes);
9975f757f3fSDimitry Andric   return processes;
9985f757f3fSDimitry Andric }
9995f757f3fSDimitry Andric 
LaunchProcess(ProcessLaunchInfo & launch_info)10000b57cec5SDimitry Andric Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
10010b57cec5SDimitry Andric   Status error;
100281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
10039dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::%s()", __FUNCTION__);
10040b57cec5SDimitry Andric 
10050b57cec5SDimitry Andric   // Take care of the host case so that each subclass can just call this
10060b57cec5SDimitry Andric   // function to get the host functionality.
10070b57cec5SDimitry Andric   if (IsHost()) {
10080b57cec5SDimitry Andric     if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
10090b57cec5SDimitry Andric       launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric     if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
10120b57cec5SDimitry Andric       const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
10130b57cec5SDimitry Andric       const bool first_arg_is_full_shell_command = false;
10140b57cec5SDimitry Andric       uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
10150b57cec5SDimitry Andric       if (log) {
10160b57cec5SDimitry Andric         const FileSpec &shell = launch_info.GetShell();
10170b57cec5SDimitry Andric         std::string shell_str = (shell) ? shell.GetPath() : "<null>";
10189dba64beSDimitry Andric         LLDB_LOGF(log,
10190b57cec5SDimitry Andric                   "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
10200b57cec5SDimitry Andric                   ", shell is '%s'",
10210b57cec5SDimitry Andric                   __FUNCTION__, num_resumes, shell_str.c_str());
10220b57cec5SDimitry Andric       }
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric       if (!launch_info.ConvertArgumentsForLaunchingInShell(
1025e8d8bef9SDimitry Andric               error, will_debug, first_arg_is_full_shell_command, num_resumes))
10260b57cec5SDimitry Andric         return error;
10270b57cec5SDimitry Andric     } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
10280b57cec5SDimitry Andric       error = ShellExpandArguments(launch_info);
10290b57cec5SDimitry Andric       if (error.Fail()) {
10300b57cec5SDimitry Andric         error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
10310b57cec5SDimitry Andric                                        "consider launching with 'process "
10320b57cec5SDimitry Andric                                        "launch'.",
10330b57cec5SDimitry Andric                                        error.AsCString("unknown"));
10340b57cec5SDimitry Andric         return error;
10350b57cec5SDimitry Andric       }
10360b57cec5SDimitry Andric     }
10370b57cec5SDimitry Andric 
10389dba64beSDimitry Andric     LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32,
10390b57cec5SDimitry Andric               __FUNCTION__, launch_info.GetResumeCount());
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric     error = Host::LaunchProcess(launch_info);
10420b57cec5SDimitry Andric   } else
10430b57cec5SDimitry Andric     error.SetErrorString(
10440b57cec5SDimitry Andric         "base lldb_private::Platform class can't launch remote processes");
10450b57cec5SDimitry Andric   return error;
10460b57cec5SDimitry Andric }
10470b57cec5SDimitry Andric 
ShellExpandArguments(ProcessLaunchInfo & launch_info)10480b57cec5SDimitry Andric Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
10490b57cec5SDimitry Andric   if (IsHost())
10500b57cec5SDimitry Andric     return Host::ShellExpandArguments(launch_info);
10510b57cec5SDimitry Andric   return Status("base lldb_private::Platform class can't expand arguments");
10520b57cec5SDimitry Andric }
10530b57cec5SDimitry Andric 
KillProcess(const lldb::pid_t pid)10540b57cec5SDimitry Andric Status Platform::KillProcess(const lldb::pid_t pid) {
105581ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
10569dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric   if (!IsHost()) {
10590b57cec5SDimitry Andric     return Status(
1060349cc55cSDimitry Andric         "base lldb_private::Platform class can't kill remote processes");
10610b57cec5SDimitry Andric   }
1062349cc55cSDimitry Andric   Host::Kill(pid, SIGKILL);
10630b57cec5SDimitry Andric   return Status();
10640b57cec5SDimitry Andric }
10650b57cec5SDimitry Andric 
DebugProcess(ProcessLaunchInfo & launch_info,Debugger & debugger,Target & target,Status & error)1066349cc55cSDimitry Andric lldb::ProcessSP Platform::DebugProcess(ProcessLaunchInfo &launch_info,
1067349cc55cSDimitry Andric                                        Debugger &debugger, Target &target,
10680b57cec5SDimitry Andric                                        Status &error) {
106981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
107006c3fb27SDimitry Andric   LLDB_LOG(log, "target = {0}", &target);
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric   ProcessSP process_sp;
10730b57cec5SDimitry Andric   // Make sure we stop at the entry point
10740b57cec5SDimitry Andric   launch_info.GetFlags().Set(eLaunchFlagDebug);
10750b57cec5SDimitry Andric   // We always launch the process we are going to debug in a separate process
10760b57cec5SDimitry Andric   // group, since then we can handle ^C interrupts ourselves w/o having to
10770b57cec5SDimitry Andric   // worry about the target getting them as well.
10780b57cec5SDimitry Andric   launch_info.SetLaunchInSeparateProcessGroup(true);
10790b57cec5SDimitry Andric 
10800b57cec5SDimitry Andric   // Allow any StructuredData process-bound plugins to adjust the launch info
10810b57cec5SDimitry Andric   // if needed
10820b57cec5SDimitry Andric   size_t i = 0;
10830b57cec5SDimitry Andric   bool iteration_complete = false;
10840b57cec5SDimitry Andric   // Note iteration can't simply go until a nullptr callback is returned, as it
10850b57cec5SDimitry Andric   // is valid for a plugin to not supply a filter.
10860b57cec5SDimitry Andric   auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
10870b57cec5SDimitry Andric   for (auto filter_callback = get_filter_func(i, iteration_complete);
10880b57cec5SDimitry Andric        !iteration_complete;
10890b57cec5SDimitry Andric        filter_callback = get_filter_func(++i, iteration_complete)) {
10900b57cec5SDimitry Andric     if (filter_callback) {
10910b57cec5SDimitry Andric       // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
1092349cc55cSDimitry Andric       error = (*filter_callback)(launch_info, &target);
10930b57cec5SDimitry Andric       if (!error.Success()) {
10949dba64beSDimitry Andric         LLDB_LOGF(log,
10959dba64beSDimitry Andric                   "Platform::%s() StructuredDataPlugin launch "
10960b57cec5SDimitry Andric                   "filter failed.",
10970b57cec5SDimitry Andric                   __FUNCTION__);
10980b57cec5SDimitry Andric         return process_sp;
10990b57cec5SDimitry Andric       }
11000b57cec5SDimitry Andric     }
11010b57cec5SDimitry Andric   }
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric   error = LaunchProcess(launch_info);
11040b57cec5SDimitry Andric   if (error.Success()) {
11059dba64beSDimitry Andric     LLDB_LOGF(log,
11069dba64beSDimitry Andric               "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")",
11070b57cec5SDimitry Andric               __FUNCTION__, launch_info.GetProcessID());
11080b57cec5SDimitry Andric     if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
11090b57cec5SDimitry Andric       ProcessAttachInfo attach_info(launch_info);
1110349cc55cSDimitry Andric       process_sp = Attach(attach_info, debugger, &target, error);
11110b57cec5SDimitry Andric       if (process_sp) {
1112349cc55cSDimitry Andric         LLDB_LOG(log, "Attach() succeeded, Process plugin: {0}",
1113349cc55cSDimitry Andric                  process_sp->GetPluginName());
11140b57cec5SDimitry Andric         launch_info.SetHijackListener(attach_info.GetHijackListener());
11150b57cec5SDimitry Andric 
11160b57cec5SDimitry Andric         // Since we attached to the process, it will think it needs to detach
11170b57cec5SDimitry Andric         // if the process object just goes away without an explicit call to
11180b57cec5SDimitry Andric         // Process::Kill() or Process::Detach(), so let it know to kill the
11190b57cec5SDimitry Andric         // process if this happens.
11200b57cec5SDimitry Andric         process_sp->SetShouldDetach(false);
11210b57cec5SDimitry Andric 
11220b57cec5SDimitry Andric         // If we didn't have any file actions, the pseudo terminal might have
11235ffd83dbSDimitry Andric         // been used where the secondary side was given as the file to open for
1124349cc55cSDimitry Andric         // stdin/out/err after we have already opened the primary so we can
11250b57cec5SDimitry Andric         // read/write stdin/out/err.
11265ffd83dbSDimitry Andric         int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
11270b57cec5SDimitry Andric         if (pty_fd != PseudoTerminal::invalid_fd) {
11280b57cec5SDimitry Andric           process_sp->SetSTDIOFileDescriptor(pty_fd);
11290b57cec5SDimitry Andric         }
11300b57cec5SDimitry Andric       } else {
11319dba64beSDimitry Andric         LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__,
11320b57cec5SDimitry Andric                   error.AsCString());
11330b57cec5SDimitry Andric       }
11340b57cec5SDimitry Andric     } else {
11359dba64beSDimitry Andric       LLDB_LOGF(log,
11369dba64beSDimitry Andric                 "Platform::%s LaunchProcess() returned launch_info with "
11370b57cec5SDimitry Andric                 "invalid process id",
11380b57cec5SDimitry Andric                 __FUNCTION__);
11390b57cec5SDimitry Andric     }
11400b57cec5SDimitry Andric   } else {
11419dba64beSDimitry Andric     LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
11420b57cec5SDimitry Andric               error.AsCString());
11430b57cec5SDimitry Andric   }
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric   return process_sp;
11460b57cec5SDimitry Andric }
11470b57cec5SDimitry Andric 
1148349cc55cSDimitry Andric std::vector<ArchSpec>
CreateArchList(llvm::ArrayRef<llvm::Triple::ArchType> archs,llvm::Triple::OSType os)1149349cc55cSDimitry Andric Platform::CreateArchList(llvm::ArrayRef<llvm::Triple::ArchType> archs,
1150349cc55cSDimitry Andric                          llvm::Triple::OSType os) {
1151349cc55cSDimitry Andric   std::vector<ArchSpec> list;
1152349cc55cSDimitry Andric   for(auto arch : archs) {
1153349cc55cSDimitry Andric     llvm::Triple triple;
1154349cc55cSDimitry Andric     triple.setArch(arch);
1155349cc55cSDimitry Andric     triple.setOS(os);
1156349cc55cSDimitry Andric     list.push_back(ArchSpec(triple));
1157349cc55cSDimitry Andric   }
1158349cc55cSDimitry Andric   return list;
1159349cc55cSDimitry Andric }
1160349cc55cSDimitry Andric 
11610b57cec5SDimitry Andric /// Lets a platform answer if it is compatible with a given
11620b57cec5SDimitry Andric /// architecture and the target triple contained within.
IsCompatibleArchitecture(const ArchSpec & arch,const ArchSpec & process_host_arch,ArchSpec::MatchType match,ArchSpec * compatible_arch_ptr)11630b57cec5SDimitry Andric bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
116481ad6265SDimitry Andric                                         const ArchSpec &process_host_arch,
1165bdd1243dSDimitry Andric                                         ArchSpec::MatchType match,
11660b57cec5SDimitry Andric                                         ArchSpec *compatible_arch_ptr) {
11670b57cec5SDimitry Andric   // If the architecture is invalid, we must answer true...
11680b57cec5SDimitry Andric   if (arch.IsValid()) {
11690b57cec5SDimitry Andric     ArchSpec platform_arch;
117081ad6265SDimitry Andric     for (const ArchSpec &platform_arch :
117181ad6265SDimitry Andric          GetSupportedArchitectures(process_host_arch)) {
1172bdd1243dSDimitry Andric       if (arch.IsMatch(platform_arch, match)) {
11730b57cec5SDimitry Andric         if (compatible_arch_ptr)
11740b57cec5SDimitry Andric           *compatible_arch_ptr = platform_arch;
11750b57cec5SDimitry Andric         return true;
11760b57cec5SDimitry Andric       }
11770b57cec5SDimitry Andric     }
11780b57cec5SDimitry Andric   }
11790b57cec5SDimitry Andric   if (compatible_arch_ptr)
11800b57cec5SDimitry Andric     compatible_arch_ptr->Clear();
11810b57cec5SDimitry Andric   return false;
11820b57cec5SDimitry Andric }
11830b57cec5SDimitry Andric 
PutFile(const FileSpec & source,const FileSpec & destination,uint32_t uid,uint32_t gid)11840b57cec5SDimitry Andric Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
11850b57cec5SDimitry Andric                          uint32_t uid, uint32_t gid) {
118681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
11879dba64beSDimitry Andric   LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
11880b57cec5SDimitry Andric 
11899dba64beSDimitry Andric   auto source_open_options =
1190349cc55cSDimitry Andric       File::eOpenOptionReadOnly | File::eOpenOptionCloseOnExec;
11910b57cec5SDimitry Andric   namespace fs = llvm::sys::fs;
11920b57cec5SDimitry Andric   if (fs::is_symlink_file(source.GetPath()))
11930b57cec5SDimitry Andric     source_open_options |= File::eOpenOptionDontFollowSymlinks;
11940b57cec5SDimitry Andric 
11959dba64beSDimitry Andric   auto source_file = FileSystem::Instance().Open(source, source_open_options,
11969dba64beSDimitry Andric                                                  lldb::eFilePermissionsUserRW);
11979dba64beSDimitry Andric   if (!source_file)
11989dba64beSDimitry Andric     return Status(source_file.takeError());
11999dba64beSDimitry Andric   Status error;
12009dba64beSDimitry Andric   uint32_t permissions = source_file.get()->GetPermissions(error);
12010b57cec5SDimitry Andric   if (permissions == 0)
12020b57cec5SDimitry Andric     permissions = lldb::eFilePermissionsFileDefault;
12030b57cec5SDimitry Andric 
12040b57cec5SDimitry Andric   lldb::user_id_t dest_file = OpenFile(
1205349cc55cSDimitry Andric       destination, File::eOpenOptionCanCreate | File::eOpenOptionWriteOnly |
12060b57cec5SDimitry Andric                        File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
12070b57cec5SDimitry Andric       permissions, error);
12089dba64beSDimitry Andric   LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
12090b57cec5SDimitry Andric 
12100b57cec5SDimitry Andric   if (error.Fail())
12110b57cec5SDimitry Andric     return error;
12120b57cec5SDimitry Andric   if (dest_file == UINT64_MAX)
12130b57cec5SDimitry Andric     return Status("unable to open target file");
121481ad6265SDimitry Andric   lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
12150b57cec5SDimitry Andric   uint64_t offset = 0;
12160b57cec5SDimitry Andric   for (;;) {
12170b57cec5SDimitry Andric     size_t bytes_read = buffer_sp->GetByteSize();
12189dba64beSDimitry Andric     error = source_file.get()->Read(buffer_sp->GetBytes(), bytes_read);
12190b57cec5SDimitry Andric     if (error.Fail() || bytes_read == 0)
12200b57cec5SDimitry Andric       break;
12210b57cec5SDimitry Andric 
12220b57cec5SDimitry Andric     const uint64_t bytes_written =
12230b57cec5SDimitry Andric         WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
12240b57cec5SDimitry Andric     if (error.Fail())
12250b57cec5SDimitry Andric       break;
12260b57cec5SDimitry Andric 
12270b57cec5SDimitry Andric     offset += bytes_written;
12280b57cec5SDimitry Andric     if (bytes_written != bytes_read) {
12290b57cec5SDimitry Andric       // We didn't write the correct number of bytes, so adjust the file
12300b57cec5SDimitry Andric       // position in the source file we are reading from...
12319dba64beSDimitry Andric       source_file.get()->SeekFromStart(offset);
12320b57cec5SDimitry Andric     }
12330b57cec5SDimitry Andric   }
12340b57cec5SDimitry Andric   CloseFile(dest_file, error);
12350b57cec5SDimitry Andric 
12360b57cec5SDimitry Andric   if (uid == UINT32_MAX && gid == UINT32_MAX)
12370b57cec5SDimitry Andric     return error;
12380b57cec5SDimitry Andric 
12390b57cec5SDimitry Andric   // TODO: ChownFile?
12400b57cec5SDimitry Andric 
12410b57cec5SDimitry Andric   return error;
12420b57cec5SDimitry Andric }
12430b57cec5SDimitry Andric 
GetFile(const FileSpec & source,const FileSpec & destination)12440b57cec5SDimitry Andric Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
12450b57cec5SDimitry Andric   Status error("unimplemented");
12460b57cec5SDimitry Andric   return error;
12470b57cec5SDimitry Andric }
12480b57cec5SDimitry Andric 
12490b57cec5SDimitry Andric Status
CreateSymlink(const FileSpec & src,const FileSpec & dst)12500b57cec5SDimitry Andric Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
12510b57cec5SDimitry Andric                         const FileSpec &dst) // The symlink points to dst
12520b57cec5SDimitry Andric {
125381ad6265SDimitry Andric   if (IsHost())
125481ad6265SDimitry Andric     return FileSystem::Instance().Symlink(src, dst);
125581ad6265SDimitry Andric   return Status("unimplemented");
12560b57cec5SDimitry Andric }
12570b57cec5SDimitry Andric 
GetFileExists(const lldb_private::FileSpec & file_spec)12580b57cec5SDimitry Andric bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
125981ad6265SDimitry Andric   if (IsHost())
126081ad6265SDimitry Andric     return FileSystem::Instance().Exists(file_spec);
12610b57cec5SDimitry Andric   return false;
12620b57cec5SDimitry Andric }
12630b57cec5SDimitry Andric 
Unlink(const FileSpec & path)12640b57cec5SDimitry Andric Status Platform::Unlink(const FileSpec &path) {
126581ad6265SDimitry Andric   if (IsHost())
126681ad6265SDimitry Andric     return llvm::sys::fs::remove(path.GetPath());
126781ad6265SDimitry Andric   return Status("unimplemented");
12680b57cec5SDimitry Andric }
12690b57cec5SDimitry Andric 
GetMmapArgumentList(const ArchSpec & arch,addr_t addr,addr_t length,unsigned prot,unsigned flags,addr_t fd,addr_t offset)12700b57cec5SDimitry Andric MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
12710b57cec5SDimitry Andric                                           addr_t length, unsigned prot,
12720b57cec5SDimitry Andric                                           unsigned flags, addr_t fd,
12730b57cec5SDimitry Andric                                           addr_t offset) {
12740b57cec5SDimitry Andric   uint64_t flags_platform = 0;
12750b57cec5SDimitry Andric   if (flags & eMmapFlagsPrivate)
12760b57cec5SDimitry Andric     flags_platform |= MAP_PRIVATE;
12770b57cec5SDimitry Andric   if (flags & eMmapFlagsAnon)
12780b57cec5SDimitry Andric     flags_platform |= MAP_ANON;
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
12810b57cec5SDimitry Andric   return args;
12820b57cec5SDimitry Andric }
12830b57cec5SDimitry Andric 
RunShellCommand(llvm::StringRef command,const FileSpec & working_dir,int * status_ptr,int * signo_ptr,std::string * command_output,const Timeout<std::micro> & timeout)12840b57cec5SDimitry Andric lldb_private::Status Platform::RunShellCommand(
1285e8d8bef9SDimitry Andric     llvm::StringRef command,
1286e8d8bef9SDimitry Andric     const FileSpec &
1287e8d8bef9SDimitry Andric         working_dir, // Pass empty FileSpec to use the current working directory
1288e8d8bef9SDimitry Andric     int *status_ptr, // Pass nullptr if you don't want the process exit status
1289e8d8bef9SDimitry Andric     int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1290e8d8bef9SDimitry Andric                     // process to exit
1291e8d8bef9SDimitry Andric     std::string
1292e8d8bef9SDimitry Andric         *command_output, // Pass nullptr if you don't want the command output
1293e8d8bef9SDimitry Andric     const Timeout<std::micro> &timeout) {
1294e8d8bef9SDimitry Andric   return RunShellCommand(llvm::StringRef(), command, working_dir, status_ptr,
1295e8d8bef9SDimitry Andric                          signo_ptr, command_output, timeout);
1296e8d8bef9SDimitry Andric }
1297e8d8bef9SDimitry Andric 
RunShellCommand(llvm::StringRef shell,llvm::StringRef command,const FileSpec & working_dir,int * status_ptr,int * signo_ptr,std::string * command_output,const Timeout<std::micro> & timeout)1298e8d8bef9SDimitry Andric lldb_private::Status Platform::RunShellCommand(
1299e8d8bef9SDimitry Andric     llvm::StringRef shell,   // Pass empty if you want to use the default
1300e8d8bef9SDimitry Andric                              // shell interpreter
1301e8d8bef9SDimitry Andric     llvm::StringRef command, // Shouldn't be empty
13020b57cec5SDimitry Andric     const FileSpec &
13030b57cec5SDimitry Andric         working_dir, // Pass empty FileSpec to use the current working directory
13040b57cec5SDimitry Andric     int *status_ptr, // Pass nullptr if you don't want the process exit status
13050b57cec5SDimitry Andric     int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
13060b57cec5SDimitry Andric                     // process to exit
13070b57cec5SDimitry Andric     std::string
13080b57cec5SDimitry Andric         *command_output, // Pass nullptr if you don't want the command output
13090b57cec5SDimitry Andric     const Timeout<std::micro> &timeout) {
13100b57cec5SDimitry Andric   if (IsHost())
1311e8d8bef9SDimitry Andric     return Host::RunShellCommand(shell, command, working_dir, status_ptr,
1312e8d8bef9SDimitry Andric                                  signo_ptr, command_output, timeout);
131381ad6265SDimitry Andric   return Status("unable to run a remote command without a platform");
13140b57cec5SDimitry Andric }
13150b57cec5SDimitry Andric 
CalculateMD5(const FileSpec & file_spec,uint64_t & low,uint64_t & high)13160b57cec5SDimitry Andric bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
13170b57cec5SDimitry Andric                             uint64_t &high) {
13180b57cec5SDimitry Andric   if (!IsHost())
13190b57cec5SDimitry Andric     return false;
13200b57cec5SDimitry Andric   auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
13210b57cec5SDimitry Andric   if (!Result)
13220b57cec5SDimitry Andric     return false;
13230b57cec5SDimitry Andric   std::tie(high, low) = Result->words();
13240b57cec5SDimitry Andric   return true;
13250b57cec5SDimitry Andric }
13260b57cec5SDimitry Andric 
SetLocalCacheDirectory(const char * local)13270b57cec5SDimitry Andric void Platform::SetLocalCacheDirectory(const char *local) {
13280b57cec5SDimitry Andric   m_local_cache_directory.assign(local);
13290b57cec5SDimitry Andric }
13300b57cec5SDimitry Andric 
GetLocalCacheDirectory()13310b57cec5SDimitry Andric const char *Platform::GetLocalCacheDirectory() {
13320b57cec5SDimitry Andric   return m_local_cache_directory.c_str();
13330b57cec5SDimitry Andric }
13340b57cec5SDimitry Andric 
13350b57cec5SDimitry Andric static constexpr OptionDefinition g_rsync_option_table[] = {
13360b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
13370b57cec5SDimitry Andric      {}, 0, eArgTypeNone, "Enable rsync."},
13380b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
13390b57cec5SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
13400b57cec5SDimitry Andric      "Platform-specific options required for rsync to work."},
13410b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
13420b57cec5SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
13430b57cec5SDimitry Andric      "Platform-specific rsync prefix put before the remote path."},
13440b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
13450b57cec5SDimitry Andric      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
13460b57cec5SDimitry Andric      "Do not automatically fill in the remote hostname when composing the "
13470b57cec5SDimitry Andric      "rsync command."},
13480b57cec5SDimitry Andric };
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric static constexpr OptionDefinition g_ssh_option_table[] = {
13510b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
13520b57cec5SDimitry Andric      {}, 0, eArgTypeNone, "Enable SSH."},
13530b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
13540b57cec5SDimitry Andric      nullptr, {}, 0, eArgTypeCommandName,
13550b57cec5SDimitry Andric      "Platform-specific options required for SSH to work."},
13560b57cec5SDimitry Andric };
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric static constexpr OptionDefinition g_caching_option_table[] = {
13590b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
13600b57cec5SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
13610b57cec5SDimitry Andric      "Path in which to store local copies of files."},
13620b57cec5SDimitry Andric };
13630b57cec5SDimitry Andric 
GetDefinitions()13640b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
1365bdd1243dSDimitry Andric   return llvm::ArrayRef(g_rsync_option_table);
13660b57cec5SDimitry Andric }
13670b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)13680b57cec5SDimitry Andric void OptionGroupPlatformRSync::OptionParsingStarting(
13690b57cec5SDimitry Andric     ExecutionContext *execution_context) {
13700b57cec5SDimitry Andric   m_rsync = false;
13710b57cec5SDimitry Andric   m_rsync_opts.clear();
13720b57cec5SDimitry Andric   m_rsync_prefix.clear();
13730b57cec5SDimitry Andric   m_ignores_remote_hostname = false;
13740b57cec5SDimitry Andric }
13750b57cec5SDimitry Andric 
13760b57cec5SDimitry Andric lldb_private::Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)13770b57cec5SDimitry Andric OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
13780b57cec5SDimitry Andric                                          llvm::StringRef option_arg,
13790b57cec5SDimitry Andric                                          ExecutionContext *execution_context) {
13800b57cec5SDimitry Andric   Status error;
13810b57cec5SDimitry Andric   char short_option = (char)GetDefinitions()[option_idx].short_option;
13820b57cec5SDimitry Andric   switch (short_option) {
13830b57cec5SDimitry Andric   case 'r':
13840b57cec5SDimitry Andric     m_rsync = true;
13850b57cec5SDimitry Andric     break;
13860b57cec5SDimitry Andric 
13870b57cec5SDimitry Andric   case 'R':
13885ffd83dbSDimitry Andric     m_rsync_opts.assign(std::string(option_arg));
13890b57cec5SDimitry Andric     break;
13900b57cec5SDimitry Andric 
13910b57cec5SDimitry Andric   case 'P':
13925ffd83dbSDimitry Andric     m_rsync_prefix.assign(std::string(option_arg));
13930b57cec5SDimitry Andric     break;
13940b57cec5SDimitry Andric 
13950b57cec5SDimitry Andric   case 'i':
13960b57cec5SDimitry Andric     m_ignores_remote_hostname = true;
13970b57cec5SDimitry Andric     break;
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric   default:
14000b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
14010b57cec5SDimitry Andric     break;
14020b57cec5SDimitry Andric   }
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric   return error;
14050b57cec5SDimitry Andric }
14060b57cec5SDimitry Andric 
14070b57cec5SDimitry Andric lldb::BreakpointSP
SetThreadCreationBreakpoint(lldb_private::Target & target)14080b57cec5SDimitry Andric Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
14090b57cec5SDimitry Andric   return lldb::BreakpointSP();
14100b57cec5SDimitry Andric }
14110b57cec5SDimitry Andric 
GetDefinitions()14120b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
1413bdd1243dSDimitry Andric   return llvm::ArrayRef(g_ssh_option_table);
14140b57cec5SDimitry Andric }
14150b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)14160b57cec5SDimitry Andric void OptionGroupPlatformSSH::OptionParsingStarting(
14170b57cec5SDimitry Andric     ExecutionContext *execution_context) {
14180b57cec5SDimitry Andric   m_ssh = false;
14190b57cec5SDimitry Andric   m_ssh_opts.clear();
14200b57cec5SDimitry Andric }
14210b57cec5SDimitry Andric 
14220b57cec5SDimitry Andric lldb_private::Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)14230b57cec5SDimitry Andric OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
14240b57cec5SDimitry Andric                                        llvm::StringRef option_arg,
14250b57cec5SDimitry Andric                                        ExecutionContext *execution_context) {
14260b57cec5SDimitry Andric   Status error;
14270b57cec5SDimitry Andric   char short_option = (char)GetDefinitions()[option_idx].short_option;
14280b57cec5SDimitry Andric   switch (short_option) {
14290b57cec5SDimitry Andric   case 's':
14300b57cec5SDimitry Andric     m_ssh = true;
14310b57cec5SDimitry Andric     break;
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric   case 'S':
14345ffd83dbSDimitry Andric     m_ssh_opts.assign(std::string(option_arg));
14350b57cec5SDimitry Andric     break;
14360b57cec5SDimitry Andric 
14370b57cec5SDimitry Andric   default:
14380b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
14390b57cec5SDimitry Andric     break;
14400b57cec5SDimitry Andric   }
14410b57cec5SDimitry Andric 
14420b57cec5SDimitry Andric   return error;
14430b57cec5SDimitry Andric }
14440b57cec5SDimitry Andric 
GetDefinitions()14450b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
1446bdd1243dSDimitry Andric   return llvm::ArrayRef(g_caching_option_table);
14470b57cec5SDimitry Andric }
14480b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)14490b57cec5SDimitry Andric void OptionGroupPlatformCaching::OptionParsingStarting(
14500b57cec5SDimitry Andric     ExecutionContext *execution_context) {
14510b57cec5SDimitry Andric   m_cache_dir.clear();
14520b57cec5SDimitry Andric }
14530b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)14540b57cec5SDimitry Andric lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
14550b57cec5SDimitry Andric     uint32_t option_idx, llvm::StringRef option_arg,
14560b57cec5SDimitry Andric     ExecutionContext *execution_context) {
14570b57cec5SDimitry Andric   Status error;
14580b57cec5SDimitry Andric   char short_option = (char)GetDefinitions()[option_idx].short_option;
14590b57cec5SDimitry Andric   switch (short_option) {
14600b57cec5SDimitry Andric   case 'c':
14615ffd83dbSDimitry Andric     m_cache_dir.assign(std::string(option_arg));
14620b57cec5SDimitry Andric     break;
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric   default:
14650b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
14660b57cec5SDimitry Andric     break;
14670b57cec5SDimitry Andric   }
14680b57cec5SDimitry Andric 
14690b57cec5SDimitry Andric   return error;
14700b57cec5SDimitry Andric }
14710b57cec5SDimitry Andric 
GetEnvironment()147281ad6265SDimitry Andric Environment Platform::GetEnvironment() {
147381ad6265SDimitry Andric   if (IsHost())
147481ad6265SDimitry Andric     return Host::GetEnvironment();
147581ad6265SDimitry Andric   return Environment();
147681ad6265SDimitry Andric }
14770b57cec5SDimitry Andric 
GetTrapHandlerSymbolNames()14780b57cec5SDimitry Andric const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
14790b57cec5SDimitry Andric   if (!m_calculated_trap_handlers) {
14800b57cec5SDimitry Andric     std::lock_guard<std::mutex> guard(m_mutex);
14810b57cec5SDimitry Andric     if (!m_calculated_trap_handlers) {
14820b57cec5SDimitry Andric       CalculateTrapHandlerSymbolNames();
14830b57cec5SDimitry Andric       m_calculated_trap_handlers = true;
14840b57cec5SDimitry Andric     }
14850b57cec5SDimitry Andric   }
14860b57cec5SDimitry Andric   return m_trap_handlers;
14870b57cec5SDimitry Andric }
14880b57cec5SDimitry Andric 
1489349cc55cSDimitry Andric Status
GetCachedExecutable(ModuleSpec & module_spec,lldb::ModuleSP & module_sp,const FileSpecList * module_search_paths_ptr)1490349cc55cSDimitry Andric Platform::GetCachedExecutable(ModuleSpec &module_spec,
1491349cc55cSDimitry Andric                               lldb::ModuleSP &module_sp,
1492349cc55cSDimitry Andric                               const FileSpecList *module_search_paths_ptr) {
14934824e7fdSDimitry Andric   FileSpec platform_spec = module_spec.GetFileSpec();
14944824e7fdSDimitry Andric   Status error = GetRemoteSharedModule(
1495349cc55cSDimitry Andric       module_spec, nullptr, module_sp,
14960b57cec5SDimitry Andric       [&](const ModuleSpec &spec) {
1497349cc55cSDimitry Andric         return ResolveRemoteExecutable(spec, module_sp,
1498349cc55cSDimitry Andric                                        module_search_paths_ptr);
14990b57cec5SDimitry Andric       },
15000b57cec5SDimitry Andric       nullptr);
15014824e7fdSDimitry Andric   if (error.Success()) {
15024824e7fdSDimitry Andric     module_spec.GetFileSpec() = module_sp->GetFileSpec();
15034824e7fdSDimitry Andric     module_spec.GetPlatformFileSpec() = platform_spec;
15044824e7fdSDimitry Andric   }
15054824e7fdSDimitry Andric 
15064824e7fdSDimitry Andric   return error;
15070b57cec5SDimitry Andric }
15080b57cec5SDimitry Andric 
GetRemoteSharedModule(const ModuleSpec & module_spec,Process * process,lldb::ModuleSP & module_sp,const ModuleResolver & module_resolver,bool * did_create_ptr)15090b57cec5SDimitry Andric Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
15100b57cec5SDimitry Andric                                        Process *process,
15110b57cec5SDimitry Andric                                        lldb::ModuleSP &module_sp,
15120b57cec5SDimitry Andric                                        const ModuleResolver &module_resolver,
15130b57cec5SDimitry Andric                                        bool *did_create_ptr) {
15140b57cec5SDimitry Andric   // Get module information from a target.
15150b57cec5SDimitry Andric   ModuleSpec resolved_module_spec;
151681ad6265SDimitry Andric   ArchSpec process_host_arch;
15170b57cec5SDimitry Andric   bool got_module_spec = false;
15180b57cec5SDimitry Andric   if (process) {
151981ad6265SDimitry Andric     process_host_arch = process->GetSystemArchitecture();
15200b57cec5SDimitry Andric     // Try to get module information from the process
15210b57cec5SDimitry Andric     if (process->GetModuleSpec(module_spec.GetFileSpec(),
15220b57cec5SDimitry Andric                                module_spec.GetArchitecture(),
15230b57cec5SDimitry Andric                                resolved_module_spec)) {
15240b57cec5SDimitry Andric       if (!module_spec.GetUUID().IsValid() ||
15250b57cec5SDimitry Andric           module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
15260b57cec5SDimitry Andric         got_module_spec = true;
15270b57cec5SDimitry Andric       }
15280b57cec5SDimitry Andric     }
15290b57cec5SDimitry Andric   }
15300b57cec5SDimitry Andric 
15310b57cec5SDimitry Andric   if (!module_spec.GetArchitecture().IsValid()) {
15320b57cec5SDimitry Andric     Status error;
15330b57cec5SDimitry Andric     // No valid architecture was specified, ask the platform for the
15340b57cec5SDimitry Andric     // architectures that we should be using (in the correct order) and see if
15350b57cec5SDimitry Andric     // we can find a match that way
15360b57cec5SDimitry Andric     ModuleSpec arch_module_spec(module_spec);
153781ad6265SDimitry Andric     for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
1538349cc55cSDimitry Andric       arch_module_spec.GetArchitecture() = arch;
15390b57cec5SDimitry Andric       error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
15400b57cec5SDimitry Andric                                           nullptr, nullptr);
15410b57cec5SDimitry Andric       // Did we find an executable using one of the
15420b57cec5SDimitry Andric       if (error.Success() && module_sp)
15430b57cec5SDimitry Andric         break;
15440b57cec5SDimitry Andric     }
1545eaeb601bSDimitry Andric     if (module_sp) {
1546eaeb601bSDimitry Andric       resolved_module_spec = arch_module_spec;
15470b57cec5SDimitry Andric       got_module_spec = true;
15480b57cec5SDimitry Andric     }
1549eaeb601bSDimitry Andric   }
15500b57cec5SDimitry Andric 
15510b57cec5SDimitry Andric   if (!got_module_spec) {
15520b57cec5SDimitry Andric     // Get module information from a target.
1553eaeb601bSDimitry Andric     if (GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
15540b57cec5SDimitry Andric                       resolved_module_spec)) {
15550b57cec5SDimitry Andric       if (!module_spec.GetUUID().IsValid() ||
15560b57cec5SDimitry Andric           module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1557eaeb601bSDimitry Andric         got_module_spec = true;
1558eaeb601bSDimitry Andric       }
1559eaeb601bSDimitry Andric     }
1560eaeb601bSDimitry Andric   }
1561eaeb601bSDimitry Andric 
1562eaeb601bSDimitry Andric   if (!got_module_spec) {
1563eaeb601bSDimitry Andric     // Fall back to the given module resolver, which may have its own
1564eaeb601bSDimitry Andric     // search logic.
15650b57cec5SDimitry Andric     return module_resolver(module_spec);
15660b57cec5SDimitry Andric   }
15670b57cec5SDimitry Andric 
15680b57cec5SDimitry Andric   // If we are looking for a specific UUID, make sure resolved_module_spec has
15690b57cec5SDimitry Andric   // the same one before we search.
15700b57cec5SDimitry Andric   if (module_spec.GetUUID().IsValid()) {
15710b57cec5SDimitry Andric     resolved_module_spec.GetUUID() = module_spec.GetUUID();
15720b57cec5SDimitry Andric   }
15730b57cec5SDimitry Andric 
15745f757f3fSDimitry Andric   // Call locate module callback if set. This allows users to implement their
15755f757f3fSDimitry Andric   // own module cache system. For example, to leverage build system artifacts,
15765f757f3fSDimitry Andric   // to bypass pulling files from remote platform, or to search symbol files
15775f757f3fSDimitry Andric   // from symbol servers.
15785f757f3fSDimitry Andric   FileSpec symbol_file_spec;
15795f757f3fSDimitry Andric   CallLocateModuleCallbackIfSet(resolved_module_spec, module_sp,
15805f757f3fSDimitry Andric                                 symbol_file_spec, did_create_ptr);
15815f757f3fSDimitry Andric   if (module_sp) {
15825f757f3fSDimitry Andric     // The module is loaded.
15835f757f3fSDimitry Andric     if (symbol_file_spec) {
15845f757f3fSDimitry Andric       // 1. module_sp:loaded, symbol_file_spec:set
15855f757f3fSDimitry Andric       //      The callback found a module file and a symbol file for this
15865f757f3fSDimitry Andric       //      resolved_module_spec. Set the symbol file to the module.
15875f757f3fSDimitry Andric       module_sp->SetSymbolFileFileSpec(symbol_file_spec);
15885f757f3fSDimitry Andric     } else {
15895f757f3fSDimitry Andric       // 2. module_sp:loaded, symbol_file_spec:empty
15905f757f3fSDimitry Andric       //      The callback only found a module file for this
15915f757f3fSDimitry Andric       //      resolved_module_spec.
15925f757f3fSDimitry Andric     }
15930b57cec5SDimitry Andric     return Status();
15940b57cec5SDimitry Andric   }
15950b57cec5SDimitry Andric 
15965f757f3fSDimitry Andric   // The module is not loaded by CallLocateModuleCallbackIfSet.
15975f757f3fSDimitry Andric   // 3. module_sp:empty, symbol_file_spec:set
15985f757f3fSDimitry Andric   //      The callback only found a symbol file for the module. We continue to
15995f757f3fSDimitry Andric   //      find a module file for this resolved_module_spec. and we will call
16005f757f3fSDimitry Andric   //      module_sp->SetSymbolFileFileSpec with the symbol_file_spec later.
16015f757f3fSDimitry Andric   // 4. module_sp:empty, symbol_file_spec:empty
16025f757f3fSDimitry Andric   //      The callback is not set. Or the callback did not find any module
16035f757f3fSDimitry Andric   //      files nor any symbol files. Or the callback failed, or something
16045f757f3fSDimitry Andric   //      went wrong. We continue to find a module file for this
16055f757f3fSDimitry Andric   //      resolved_module_spec.
16065f757f3fSDimitry Andric 
16075f757f3fSDimitry Andric   // Trying to find a module by UUID on local file system.
16085f757f3fSDimitry Andric   const Status error = module_resolver(resolved_module_spec);
16095f757f3fSDimitry Andric   if (error.Success()) {
16105f757f3fSDimitry Andric     if (module_sp && symbol_file_spec) {
16115f757f3fSDimitry Andric       // Set the symbol file to the module if the locate modudle callback was
16125f757f3fSDimitry Andric       // called and returned only a symbol file.
16135f757f3fSDimitry Andric       module_sp->SetSymbolFileFileSpec(symbol_file_spec);
16145f757f3fSDimitry Andric     }
16150b57cec5SDimitry Andric     return error;
16160b57cec5SDimitry Andric   }
16170b57cec5SDimitry Andric 
16185f757f3fSDimitry Andric   // Fallback to call GetCachedSharedModule on failure.
16195f757f3fSDimitry Andric   if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr)) {
16205f757f3fSDimitry Andric     if (module_sp && symbol_file_spec) {
16215f757f3fSDimitry Andric       // Set the symbol file to the module if the locate modudle callback was
16225f757f3fSDimitry Andric       // called and returned only a symbol file.
16235f757f3fSDimitry Andric       module_sp->SetSymbolFileFileSpec(symbol_file_spec);
16245f757f3fSDimitry Andric     }
16255f757f3fSDimitry Andric     return Status();
16265f757f3fSDimitry Andric   }
16275f757f3fSDimitry Andric 
16285f757f3fSDimitry Andric   return Status("Failed to call GetCachedSharedModule");
16295f757f3fSDimitry Andric }
16305f757f3fSDimitry Andric 
CallLocateModuleCallbackIfSet(const ModuleSpec & module_spec,lldb::ModuleSP & module_sp,FileSpec & symbol_file_spec,bool * did_create_ptr)16315f757f3fSDimitry Andric void Platform::CallLocateModuleCallbackIfSet(const ModuleSpec &module_spec,
16325f757f3fSDimitry Andric                                              lldb::ModuleSP &module_sp,
16335f757f3fSDimitry Andric                                              FileSpec &symbol_file_spec,
16345f757f3fSDimitry Andric                                              bool *did_create_ptr) {
16355f757f3fSDimitry Andric   if (!m_locate_module_callback) {
16365f757f3fSDimitry Andric     // Locate module callback is not set.
16375f757f3fSDimitry Andric     return;
16385f757f3fSDimitry Andric   }
16395f757f3fSDimitry Andric 
16405f757f3fSDimitry Andric   FileSpec module_file_spec;
16415f757f3fSDimitry Andric   Status error =
16425f757f3fSDimitry Andric       m_locate_module_callback(module_spec, module_file_spec, symbol_file_spec);
16435f757f3fSDimitry Andric 
16445f757f3fSDimitry Andric   // Locate module callback is set and called. Check the error.
16455f757f3fSDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
16465f757f3fSDimitry Andric   if (error.Fail()) {
16475f757f3fSDimitry Andric     LLDB_LOGF(log, "%s: locate module callback failed: %s",
16485f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, error.AsCString());
16495f757f3fSDimitry Andric     return;
16505f757f3fSDimitry Andric   }
16515f757f3fSDimitry Andric 
16525f757f3fSDimitry Andric   // The locate module callback was succeeded.
16535f757f3fSDimitry Andric   // Check the module_file_spec and symbol_file_spec values.
16545f757f3fSDimitry Andric   // 1. module:empty  symbol:empty  -> Failure
16555f757f3fSDimitry Andric   //    - The callback did not return any files.
16565f757f3fSDimitry Andric   // 2. module:exists symbol:exists -> Success
16575f757f3fSDimitry Andric   //    - The callback returned a module file and a symbol file.
16585f757f3fSDimitry Andric   // 3. module:exists symbol:empty  -> Success
16595f757f3fSDimitry Andric   //    - The callback returned only a module file.
16605f757f3fSDimitry Andric   // 4. module:empty  symbol:exists -> Success
16615f757f3fSDimitry Andric   //    - The callback returned only a symbol file.
16625f757f3fSDimitry Andric   //      For example, a breakpad symbol text file.
16635f757f3fSDimitry Andric   if (!module_file_spec && !symbol_file_spec) {
16645f757f3fSDimitry Andric     // This is '1. module:empty  symbol:empty  -> Failure'
16655f757f3fSDimitry Andric     // The callback did not return any files.
16665f757f3fSDimitry Andric     LLDB_LOGF(log,
16675f757f3fSDimitry Andric               "%s: locate module callback did not set both "
16685f757f3fSDimitry Andric               "module_file_spec and symbol_file_spec",
16695f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION);
16705f757f3fSDimitry Andric     return;
16715f757f3fSDimitry Andric   }
16725f757f3fSDimitry Andric 
16735f757f3fSDimitry Andric   // If the callback returned a module file, it should exist.
16745f757f3fSDimitry Andric   if (module_file_spec && !FileSystem::Instance().Exists(module_file_spec)) {
16755f757f3fSDimitry Andric     LLDB_LOGF(log,
16765f757f3fSDimitry Andric               "%s: locate module callback set a non-existent file to "
16775f757f3fSDimitry Andric               "module_file_spec: %s",
16785f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str());
16795f757f3fSDimitry Andric     // Clear symbol_file_spec for the error.
16805f757f3fSDimitry Andric     symbol_file_spec.Clear();
16815f757f3fSDimitry Andric     return;
16825f757f3fSDimitry Andric   }
16835f757f3fSDimitry Andric 
16845f757f3fSDimitry Andric   // If the callback returned a symbol file, it should exist.
16855f757f3fSDimitry Andric   if (symbol_file_spec && !FileSystem::Instance().Exists(symbol_file_spec)) {
16865f757f3fSDimitry Andric     LLDB_LOGF(log,
16875f757f3fSDimitry Andric               "%s: locate module callback set a non-existent file to "
16885f757f3fSDimitry Andric               "symbol_file_spec: %s",
16895f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, symbol_file_spec.GetPath().c_str());
16905f757f3fSDimitry Andric     // Clear symbol_file_spec for the error.
16915f757f3fSDimitry Andric     symbol_file_spec.Clear();
16925f757f3fSDimitry Andric     return;
16935f757f3fSDimitry Andric   }
16945f757f3fSDimitry Andric 
16955f757f3fSDimitry Andric   if (!module_file_spec && symbol_file_spec) {
16965f757f3fSDimitry Andric     // This is '4. module:empty  symbol:exists -> Success'
16975f757f3fSDimitry Andric     // The locate module callback returned only a symbol file. For example,
16985f757f3fSDimitry Andric     // a breakpad symbol text file. GetRemoteSharedModule will use this returned
16995f757f3fSDimitry Andric     // symbol_file_spec.
17005f757f3fSDimitry Andric     LLDB_LOGF(log, "%s: locate module callback succeeded: symbol=%s",
17015f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, symbol_file_spec.GetPath().c_str());
17025f757f3fSDimitry Andric     return;
17035f757f3fSDimitry Andric   }
17045f757f3fSDimitry Andric 
17055f757f3fSDimitry Andric   // This is one of the following.
17065f757f3fSDimitry Andric   // - 2. module:exists symbol:exists -> Success
17075f757f3fSDimitry Andric   //    - The callback returned a module file and a symbol file.
17085f757f3fSDimitry Andric   // - 3. module:exists symbol:empty  -> Success
17095f757f3fSDimitry Andric   //    - The callback returned Only a module file.
17105f757f3fSDimitry Andric   // Load the module file.
17115f757f3fSDimitry Andric   auto cached_module_spec(module_spec);
17125f757f3fSDimitry Andric   cached_module_spec.GetUUID().Clear(); // Clear UUID since it may contain md5
17135f757f3fSDimitry Andric                                         // content hash instead of real UUID.
17145f757f3fSDimitry Andric   cached_module_spec.GetFileSpec() = module_file_spec;
17155f757f3fSDimitry Andric   cached_module_spec.GetPlatformFileSpec() = module_spec.GetFileSpec();
17165f757f3fSDimitry Andric   cached_module_spec.SetObjectOffset(0);
17175f757f3fSDimitry Andric 
17185f757f3fSDimitry Andric   error = ModuleList::GetSharedModule(cached_module_spec, module_sp, nullptr,
17195f757f3fSDimitry Andric                                       nullptr, did_create_ptr, false);
17205f757f3fSDimitry Andric   if (error.Success() && module_sp) {
17215f757f3fSDimitry Andric     // Succeeded to load the module file.
17225f757f3fSDimitry Andric     LLDB_LOGF(log, "%s: locate module callback succeeded: module=%s symbol=%s",
17235f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str(),
17245f757f3fSDimitry Andric               symbol_file_spec.GetPath().c_str());
17255f757f3fSDimitry Andric   } else {
17265f757f3fSDimitry Andric     LLDB_LOGF(log,
17275f757f3fSDimitry Andric               "%s: locate module callback succeeded but failed to load: "
17285f757f3fSDimitry Andric               "module=%s symbol=%s",
17295f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str(),
17305f757f3fSDimitry Andric               symbol_file_spec.GetPath().c_str());
17315f757f3fSDimitry Andric     // Clear module_sp and symbol_file_spec for the error.
17325f757f3fSDimitry Andric     module_sp.reset();
17335f757f3fSDimitry Andric     symbol_file_spec.Clear();
17345f757f3fSDimitry Andric   }
17355f757f3fSDimitry Andric }
17365f757f3fSDimitry Andric 
GetCachedSharedModule(const ModuleSpec & module_spec,lldb::ModuleSP & module_sp,bool * did_create_ptr)17370b57cec5SDimitry Andric bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
17380b57cec5SDimitry Andric                                      lldb::ModuleSP &module_sp,
17390b57cec5SDimitry Andric                                      bool *did_create_ptr) {
1740349cc55cSDimitry Andric   if (IsHost() || !GetGlobalPlatformProperties().GetUseModuleCache() ||
1741349cc55cSDimitry Andric       !GetGlobalPlatformProperties().GetModuleCacheDirectory())
17420b57cec5SDimitry Andric     return false;
17430b57cec5SDimitry Andric 
174481ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
17450b57cec5SDimitry Andric 
17460b57cec5SDimitry Andric   // Check local cache for a module.
17470b57cec5SDimitry Andric   auto error = m_module_cache->GetAndPut(
17480b57cec5SDimitry Andric       GetModuleCacheRoot(), GetCacheHostname(), module_spec,
17490b57cec5SDimitry Andric       [this](const ModuleSpec &module_spec,
17500b57cec5SDimitry Andric              const FileSpec &tmp_download_file_spec) {
17510b57cec5SDimitry Andric         return DownloadModuleSlice(
17520b57cec5SDimitry Andric             module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
17530b57cec5SDimitry Andric             module_spec.GetObjectSize(), tmp_download_file_spec);
17540b57cec5SDimitry Andric 
17550b57cec5SDimitry Andric       },
17560b57cec5SDimitry Andric       [this](const ModuleSP &module_sp,
17570b57cec5SDimitry Andric              const FileSpec &tmp_download_file_spec) {
17580b57cec5SDimitry Andric         return DownloadSymbolFile(module_sp, tmp_download_file_spec);
17590b57cec5SDimitry Andric       },
17600b57cec5SDimitry Andric       module_sp, did_create_ptr);
17610b57cec5SDimitry Andric   if (error.Success())
17620b57cec5SDimitry Andric     return true;
17630b57cec5SDimitry Andric 
17649dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s",
17650b57cec5SDimitry Andric             __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
17660b57cec5SDimitry Andric             error.AsCString());
17670b57cec5SDimitry Andric   return false;
17680b57cec5SDimitry Andric }
17690b57cec5SDimitry Andric 
DownloadModuleSlice(const FileSpec & src_file_spec,const uint64_t src_offset,const uint64_t src_size,const FileSpec & dst_file_spec)17700b57cec5SDimitry Andric Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
17710b57cec5SDimitry Andric                                      const uint64_t src_offset,
17720b57cec5SDimitry Andric                                      const uint64_t src_size,
17730b57cec5SDimitry Andric                                      const FileSpec &dst_file_spec) {
17740b57cec5SDimitry Andric   Status error;
17750b57cec5SDimitry Andric 
17760b57cec5SDimitry Andric   std::error_code EC;
17779dba64beSDimitry Andric   llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::OF_None);
17780b57cec5SDimitry Andric   if (EC) {
17790b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unable to open destination file: %s",
17800b57cec5SDimitry Andric                                    dst_file_spec.GetPath().c_str());
17810b57cec5SDimitry Andric     return error;
17820b57cec5SDimitry Andric   }
17830b57cec5SDimitry Andric 
1784349cc55cSDimitry Andric   auto src_fd = OpenFile(src_file_spec, File::eOpenOptionReadOnly,
17850b57cec5SDimitry Andric                          lldb::eFilePermissionsFileDefault, error);
17860b57cec5SDimitry Andric 
17870b57cec5SDimitry Andric   if (error.Fail()) {
17880b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unable to open source file: %s",
17890b57cec5SDimitry Andric                                    error.AsCString());
17900b57cec5SDimitry Andric     return error;
17910b57cec5SDimitry Andric   }
17920b57cec5SDimitry Andric 
179306c3fb27SDimitry Andric   std::vector<char> buffer(512 * 1024);
17940b57cec5SDimitry Andric   auto offset = src_offset;
17950b57cec5SDimitry Andric   uint64_t total_bytes_read = 0;
17960b57cec5SDimitry Andric   while (total_bytes_read < src_size) {
17970b57cec5SDimitry Andric     const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
17980b57cec5SDimitry Andric                                   src_size - total_bytes_read);
17990b57cec5SDimitry Andric     const uint64_t n_read =
18000b57cec5SDimitry Andric         ReadFile(src_fd, offset, &buffer[0], to_read, error);
18010b57cec5SDimitry Andric     if (error.Fail())
18020b57cec5SDimitry Andric       break;
18030b57cec5SDimitry Andric     if (n_read == 0) {
18040b57cec5SDimitry Andric       error.SetErrorString("read 0 bytes");
18050b57cec5SDimitry Andric       break;
18060b57cec5SDimitry Andric     }
18070b57cec5SDimitry Andric     offset += n_read;
18080b57cec5SDimitry Andric     total_bytes_read += n_read;
18090b57cec5SDimitry Andric     dst.write(&buffer[0], n_read);
18100b57cec5SDimitry Andric   }
18110b57cec5SDimitry Andric 
18120b57cec5SDimitry Andric   Status close_error;
18130b57cec5SDimitry Andric   CloseFile(src_fd, close_error); // Ignoring close error.
18140b57cec5SDimitry Andric 
18150b57cec5SDimitry Andric   return error;
18160b57cec5SDimitry Andric }
18170b57cec5SDimitry Andric 
DownloadSymbolFile(const lldb::ModuleSP & module_sp,const FileSpec & dst_file_spec)18180b57cec5SDimitry Andric Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
18190b57cec5SDimitry Andric                                     const FileSpec &dst_file_spec) {
18200b57cec5SDimitry Andric   return Status(
18210b57cec5SDimitry Andric       "Symbol file downloading not supported by the default platform.");
18220b57cec5SDimitry Andric }
18230b57cec5SDimitry Andric 
GetModuleCacheRoot()18240b57cec5SDimitry Andric FileSpec Platform::GetModuleCacheRoot() {
1825349cc55cSDimitry Andric   auto dir_spec = GetGlobalPlatformProperties().GetModuleCacheDirectory();
182681ad6265SDimitry Andric   dir_spec.AppendPathComponent(GetPluginName());
18270b57cec5SDimitry Andric   return dir_spec;
18280b57cec5SDimitry Andric }
18290b57cec5SDimitry Andric 
GetCacheHostname()18300b57cec5SDimitry Andric const char *Platform::GetCacheHostname() { return GetHostname(); }
18310b57cec5SDimitry Andric 
GetRemoteUnixSignals()18320b57cec5SDimitry Andric const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
18330b57cec5SDimitry Andric   static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
18340b57cec5SDimitry Andric   return s_default_unix_signals_sp;
18350b57cec5SDimitry Andric }
18360b57cec5SDimitry Andric 
GetUnixSignals()18370b57cec5SDimitry Andric UnixSignalsSP Platform::GetUnixSignals() {
18380b57cec5SDimitry Andric   if (IsHost())
18390b57cec5SDimitry Andric     return UnixSignals::CreateForHost();
18400b57cec5SDimitry Andric   return GetRemoteUnixSignals();
18410b57cec5SDimitry Andric }
18420b57cec5SDimitry Andric 
LoadImage(lldb_private::Process * process,const lldb_private::FileSpec & local_file,const lldb_private::FileSpec & remote_file,lldb_private::Status & error)18430b57cec5SDimitry Andric uint32_t Platform::LoadImage(lldb_private::Process *process,
18440b57cec5SDimitry Andric                              const lldb_private::FileSpec &local_file,
18450b57cec5SDimitry Andric                              const lldb_private::FileSpec &remote_file,
18460b57cec5SDimitry Andric                              lldb_private::Status &error) {
18470b57cec5SDimitry Andric   if (local_file && remote_file) {
18480b57cec5SDimitry Andric     // Both local and remote file was specified. Install the local file to the
18490b57cec5SDimitry Andric     // given location.
18500b57cec5SDimitry Andric     if (IsRemote() || local_file != remote_file) {
18510b57cec5SDimitry Andric       error = Install(local_file, remote_file);
18520b57cec5SDimitry Andric       if (error.Fail())
18530b57cec5SDimitry Andric         return LLDB_INVALID_IMAGE_TOKEN;
18540b57cec5SDimitry Andric     }
18550b57cec5SDimitry Andric     return DoLoadImage(process, remote_file, nullptr, error);
18560b57cec5SDimitry Andric   }
18570b57cec5SDimitry Andric 
18580b57cec5SDimitry Andric   if (local_file) {
18590b57cec5SDimitry Andric     // Only local file was specified. Install it to the current working
18600b57cec5SDimitry Andric     // directory.
18610b57cec5SDimitry Andric     FileSpec target_file = GetWorkingDirectory();
18620b57cec5SDimitry Andric     target_file.AppendPathComponent(local_file.GetFilename().AsCString());
18630b57cec5SDimitry Andric     if (IsRemote() || local_file != target_file) {
18640b57cec5SDimitry Andric       error = Install(local_file, target_file);
18650b57cec5SDimitry Andric       if (error.Fail())
18660b57cec5SDimitry Andric         return LLDB_INVALID_IMAGE_TOKEN;
18670b57cec5SDimitry Andric     }
18680b57cec5SDimitry Andric     return DoLoadImage(process, target_file, nullptr, error);
18690b57cec5SDimitry Andric   }
18700b57cec5SDimitry Andric 
18710b57cec5SDimitry Andric   if (remote_file) {
18720b57cec5SDimitry Andric     // Only remote file was specified so we don't have to do any copying
18730b57cec5SDimitry Andric     return DoLoadImage(process, remote_file, nullptr, error);
18740b57cec5SDimitry Andric   }
18750b57cec5SDimitry Andric 
18760b57cec5SDimitry Andric   error.SetErrorString("Neither local nor remote file was specified");
18770b57cec5SDimitry Andric   return LLDB_INVALID_IMAGE_TOKEN;
18780b57cec5SDimitry Andric }
18790b57cec5SDimitry Andric 
DoLoadImage(lldb_private::Process * process,const lldb_private::FileSpec & remote_file,const std::vector<std::string> * paths,lldb_private::Status & error,lldb_private::FileSpec * loaded_image)18800b57cec5SDimitry Andric uint32_t Platform::DoLoadImage(lldb_private::Process *process,
18810b57cec5SDimitry Andric                                const lldb_private::FileSpec &remote_file,
18820b57cec5SDimitry Andric                                const std::vector<std::string> *paths,
18830b57cec5SDimitry Andric                                lldb_private::Status &error,
18840b57cec5SDimitry Andric                                lldb_private::FileSpec *loaded_image) {
18850b57cec5SDimitry Andric   error.SetErrorString("LoadImage is not supported on the current platform");
18860b57cec5SDimitry Andric   return LLDB_INVALID_IMAGE_TOKEN;
18870b57cec5SDimitry Andric }
18880b57cec5SDimitry Andric 
LoadImageUsingPaths(lldb_private::Process * process,const lldb_private::FileSpec & remote_filename,const std::vector<std::string> & paths,lldb_private::Status & error,lldb_private::FileSpec * loaded_path)18890b57cec5SDimitry Andric uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
18900b57cec5SDimitry Andric                                const lldb_private::FileSpec &remote_filename,
18910b57cec5SDimitry Andric                                const std::vector<std::string> &paths,
18920b57cec5SDimitry Andric                                lldb_private::Status &error,
18930b57cec5SDimitry Andric                                lldb_private::FileSpec *loaded_path)
18940b57cec5SDimitry Andric {
18950b57cec5SDimitry Andric   FileSpec file_to_use;
18960b57cec5SDimitry Andric   if (remote_filename.IsAbsolute())
18970b57cec5SDimitry Andric     file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
18980b57cec5SDimitry Andric 
18990b57cec5SDimitry Andric                            remote_filename.GetPathStyle());
19000b57cec5SDimitry Andric   else
19010b57cec5SDimitry Andric     file_to_use = remote_filename;
19020b57cec5SDimitry Andric 
19030b57cec5SDimitry Andric   return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
19040b57cec5SDimitry Andric }
19050b57cec5SDimitry Andric 
UnloadImage(lldb_private::Process * process,uint32_t image_token)19060b57cec5SDimitry Andric Status Platform::UnloadImage(lldb_private::Process *process,
19070b57cec5SDimitry Andric                              uint32_t image_token) {
19080b57cec5SDimitry Andric   return Status("UnloadImage is not supported on the current platform");
19090b57cec5SDimitry Andric }
19100b57cec5SDimitry Andric 
ConnectProcess(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Target * target,Status & error)19110b57cec5SDimitry Andric lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
19120b57cec5SDimitry Andric                                          llvm::StringRef plugin_name,
19135ffd83dbSDimitry Andric                                          Debugger &debugger, Target *target,
19145ffd83dbSDimitry Andric                                          Status &error) {
19155ffd83dbSDimitry Andric   return DoConnectProcess(connect_url, plugin_name, debugger, nullptr, target,
19165ffd83dbSDimitry Andric                           error);
19175ffd83dbSDimitry Andric }
19185ffd83dbSDimitry Andric 
ConnectProcessSynchronous(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Stream & stream,Target * target,Status & error)19195ffd83dbSDimitry Andric lldb::ProcessSP Platform::ConnectProcessSynchronous(
19205ffd83dbSDimitry Andric     llvm::StringRef connect_url, llvm::StringRef plugin_name,
19215ffd83dbSDimitry Andric     Debugger &debugger, Stream &stream, Target *target, Status &error) {
19225ffd83dbSDimitry Andric   return DoConnectProcess(connect_url, plugin_name, debugger, &stream, target,
19235ffd83dbSDimitry Andric                           error);
19245ffd83dbSDimitry Andric }
19255ffd83dbSDimitry Andric 
DoConnectProcess(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Stream * stream,Target * target,Status & error)19265ffd83dbSDimitry Andric lldb::ProcessSP Platform::DoConnectProcess(llvm::StringRef connect_url,
19275ffd83dbSDimitry Andric                                            llvm::StringRef plugin_name,
19285ffd83dbSDimitry Andric                                            Debugger &debugger, Stream *stream,
19295ffd83dbSDimitry Andric                                            Target *target, Status &error) {
19300b57cec5SDimitry Andric   error.Clear();
19310b57cec5SDimitry Andric 
19320b57cec5SDimitry Andric   if (!target) {
1933bdd1243dSDimitry Andric     ArchSpec arch = Target::GetDefaultArchitecture();
19340b57cec5SDimitry Andric 
1935bdd1243dSDimitry Andric     const char *triple =
1936bdd1243dSDimitry Andric         arch.IsValid() ? arch.GetTriple().getTriple().c_str() : "";
19370b57cec5SDimitry Andric 
19380b57cec5SDimitry Andric     TargetSP new_target_sp;
19390b57cec5SDimitry Andric     error = debugger.GetTargetList().CreateTarget(
19400b57cec5SDimitry Andric         debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
19410b57cec5SDimitry Andric 
1942bdd1243dSDimitry Andric     target = new_target_sp.get();
1943bdd1243dSDimitry Andric     if (!target || error.Fail()) {
19440b57cec5SDimitry Andric       return nullptr;
1945bdd1243dSDimitry Andric     }
1946bdd1243dSDimitry Andric   }
19470b57cec5SDimitry Andric 
19480b57cec5SDimitry Andric   lldb::ProcessSP process_sp =
1949e8d8bef9SDimitry Andric       target->CreateProcess(debugger.GetListener(), plugin_name, nullptr, true);
19505ffd83dbSDimitry Andric 
19510b57cec5SDimitry Andric   if (!process_sp)
19520b57cec5SDimitry Andric     return nullptr;
19530b57cec5SDimitry Andric 
19545ffd83dbSDimitry Andric   // If this private method is called with a stream we are synchronous.
19555ffd83dbSDimitry Andric   const bool synchronous = stream != nullptr;
19565ffd83dbSDimitry Andric 
19575ffd83dbSDimitry Andric   ListenerSP listener_sp(
19585ffd83dbSDimitry Andric       Listener::MakeListener("lldb.Process.ConnectProcess.hijack"));
19595ffd83dbSDimitry Andric   if (synchronous)
19605ffd83dbSDimitry Andric     process_sp->HijackProcessEvents(listener_sp);
19615ffd83dbSDimitry Andric 
19625ffd83dbSDimitry Andric   error = process_sp->ConnectRemote(connect_url);
19635ffd83dbSDimitry Andric   if (error.Fail()) {
19645ffd83dbSDimitry Andric     if (synchronous)
19655ffd83dbSDimitry Andric       process_sp->RestoreProcessEvents();
19660b57cec5SDimitry Andric     return nullptr;
19675ffd83dbSDimitry Andric   }
19685ffd83dbSDimitry Andric 
19695ffd83dbSDimitry Andric   if (synchronous) {
19705ffd83dbSDimitry Andric     EventSP event_sp;
1971bdd1243dSDimitry Andric     process_sp->WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp,
19725ffd83dbSDimitry Andric                                      nullptr);
19735ffd83dbSDimitry Andric     process_sp->RestoreProcessEvents();
19745ffd83dbSDimitry Andric     bool pop_process_io_handler = false;
197506c3fb27SDimitry Andric     // This is a user-level stop, so we allow recognizers to select frames.
197606c3fb27SDimitry Andric     Process::HandleProcessStateChangedEvent(
197706c3fb27SDimitry Andric         event_sp, stream, SelectMostRelevantFrame, pop_process_io_handler);
19785ffd83dbSDimitry Andric   }
19790b57cec5SDimitry Andric 
19800b57cec5SDimitry Andric   return process_sp;
19810b57cec5SDimitry Andric }
19820b57cec5SDimitry Andric 
ConnectToWaitingProcesses(lldb_private::Debugger & debugger,lldb_private::Status & error)19830b57cec5SDimitry Andric size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
19840b57cec5SDimitry Andric                                            lldb_private::Status &error) {
19850b57cec5SDimitry Andric   error.Clear();
19860b57cec5SDimitry Andric   return 0;
19870b57cec5SDimitry Andric }
19880b57cec5SDimitry Andric 
GetSoftwareBreakpointTrapOpcode(Target & target,BreakpointSite * bp_site)19890b57cec5SDimitry Andric size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
19900b57cec5SDimitry Andric                                                  BreakpointSite *bp_site) {
19910b57cec5SDimitry Andric   ArchSpec arch = target.GetArchitecture();
19925ffd83dbSDimitry Andric   assert(arch.IsValid());
19930b57cec5SDimitry Andric   const uint8_t *trap_opcode = nullptr;
19940b57cec5SDimitry Andric   size_t trap_opcode_size = 0;
19950b57cec5SDimitry Andric 
19960b57cec5SDimitry Andric   switch (arch.GetMachine()) {
19979dba64beSDimitry Andric   case llvm::Triple::aarch64_32:
19980b57cec5SDimitry Andric   case llvm::Triple::aarch64: {
19990b57cec5SDimitry Andric     static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
20000b57cec5SDimitry Andric     trap_opcode = g_aarch64_opcode;
20010b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_aarch64_opcode);
20020b57cec5SDimitry Andric   } break;
20030b57cec5SDimitry Andric 
20049dba64beSDimitry Andric   case llvm::Triple::arc: {
20059dba64beSDimitry Andric     static const uint8_t g_hex_opcode[] = { 0xff, 0x7f };
20069dba64beSDimitry Andric     trap_opcode = g_hex_opcode;
20079dba64beSDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20089dba64beSDimitry Andric   } break;
20099dba64beSDimitry Andric 
20100b57cec5SDimitry Andric   // TODO: support big-endian arm and thumb trap codes.
20110b57cec5SDimitry Andric   case llvm::Triple::arm: {
20120b57cec5SDimitry Andric     // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
20130b57cec5SDimitry Andric     // linux kernel does otherwise.
20140b57cec5SDimitry Andric     static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
20150b57cec5SDimitry Andric     static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
20160b57cec5SDimitry Andric 
20175f757f3fSDimitry Andric     lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetConstituentAtIndex(0));
20180b57cec5SDimitry Andric     AddressClass addr_class = AddressClass::eUnknown;
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric     if (bp_loc_sp) {
20210b57cec5SDimitry Andric       addr_class = bp_loc_sp->GetAddress().GetAddressClass();
20220b57cec5SDimitry Andric       if (addr_class == AddressClass::eUnknown &&
20230b57cec5SDimitry Andric           (bp_loc_sp->GetAddress().GetFileAddress() & 1))
20240b57cec5SDimitry Andric         addr_class = AddressClass::eCodeAlternateISA;
20250b57cec5SDimitry Andric     }
20260b57cec5SDimitry Andric 
20270b57cec5SDimitry Andric     if (addr_class == AddressClass::eCodeAlternateISA) {
20280b57cec5SDimitry Andric       trap_opcode = g_thumb_breakpoint_opcode;
20290b57cec5SDimitry Andric       trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
20300b57cec5SDimitry Andric     } else {
20310b57cec5SDimitry Andric       trap_opcode = g_arm_breakpoint_opcode;
20320b57cec5SDimitry Andric       trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
20330b57cec5SDimitry Andric     }
20340b57cec5SDimitry Andric   } break;
20350b57cec5SDimitry Andric 
20365ffd83dbSDimitry Andric   case llvm::Triple::avr: {
20375ffd83dbSDimitry Andric     static const uint8_t g_hex_opcode[] = {0x98, 0x95};
20385ffd83dbSDimitry Andric     trap_opcode = g_hex_opcode;
20395ffd83dbSDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20405ffd83dbSDimitry Andric   } break;
20415ffd83dbSDimitry Andric 
20420b57cec5SDimitry Andric   case llvm::Triple::mips:
20430b57cec5SDimitry Andric   case llvm::Triple::mips64: {
20440b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
20450b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
20460b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20470b57cec5SDimitry Andric   } break;
20480b57cec5SDimitry Andric 
20490b57cec5SDimitry Andric   case llvm::Triple::mipsel:
20500b57cec5SDimitry Andric   case llvm::Triple::mips64el: {
20510b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
20520b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
20530b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20540b57cec5SDimitry Andric   } break;
20550b57cec5SDimitry Andric 
205606c3fb27SDimitry Andric   case llvm::Triple::msp430: {
205706c3fb27SDimitry Andric     static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
205806c3fb27SDimitry Andric     trap_opcode = g_msp430_opcode;
205906c3fb27SDimitry Andric     trap_opcode_size = sizeof(g_msp430_opcode);
206006c3fb27SDimitry Andric   } break;
206106c3fb27SDimitry Andric 
20620b57cec5SDimitry Andric   case llvm::Triple::systemz: {
20630b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x00, 0x01};
20640b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
20650b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20660b57cec5SDimitry Andric   } break;
20670b57cec5SDimitry Andric 
20680b57cec5SDimitry Andric   case llvm::Triple::hexagon: {
20690b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
20700b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
20710b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20720b57cec5SDimitry Andric   } break;
20730b57cec5SDimitry Andric 
20740b57cec5SDimitry Andric   case llvm::Triple::ppc:
20750b57cec5SDimitry Andric   case llvm::Triple::ppc64: {
20760b57cec5SDimitry Andric     static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
20770b57cec5SDimitry Andric     trap_opcode = g_ppc_opcode;
20780b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_ppc_opcode);
20790b57cec5SDimitry Andric   } break;
20800b57cec5SDimitry Andric 
20810b57cec5SDimitry Andric   case llvm::Triple::ppc64le: {
20820b57cec5SDimitry Andric     static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
20830b57cec5SDimitry Andric     trap_opcode = g_ppc64le_opcode;
20840b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_ppc64le_opcode);
20850b57cec5SDimitry Andric   } break;
20860b57cec5SDimitry Andric 
20870b57cec5SDimitry Andric   case llvm::Triple::x86:
20880b57cec5SDimitry Andric   case llvm::Triple::x86_64: {
20890b57cec5SDimitry Andric     static const uint8_t g_i386_opcode[] = {0xCC};
20900b57cec5SDimitry Andric     trap_opcode = g_i386_opcode;
20910b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_i386_opcode);
20920b57cec5SDimitry Andric   } break;
20930b57cec5SDimitry Andric 
2094bdd1243dSDimitry Andric   case llvm::Triple::riscv32:
2095bdd1243dSDimitry Andric   case llvm::Triple::riscv64: {
2096bdd1243dSDimitry Andric     static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00}; // ebreak
2097bdd1243dSDimitry Andric     static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90}; // c.ebreak
2098bdd1243dSDimitry Andric     if (arch.GetFlags() & ArchSpec::eRISCV_rvc) {
2099bdd1243dSDimitry Andric       trap_opcode = g_riscv_opcode_c;
2100bdd1243dSDimitry Andric       trap_opcode_size = sizeof(g_riscv_opcode_c);
2101bdd1243dSDimitry Andric     } else {
2102bdd1243dSDimitry Andric       trap_opcode = g_riscv_opcode;
2103bdd1243dSDimitry Andric       trap_opcode_size = sizeof(g_riscv_opcode);
2104bdd1243dSDimitry Andric     }
2105bdd1243dSDimitry Andric   } break;
2106bdd1243dSDimitry Andric 
2107bdd1243dSDimitry Andric   case llvm::Triple::loongarch32:
2108bdd1243dSDimitry Andric   case llvm::Triple::loongarch64: {
2109bdd1243dSDimitry Andric     static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
2110bdd1243dSDimitry Andric                                                  0x00}; // break 0x5
2111bdd1243dSDimitry Andric     trap_opcode = g_loongarch_opcode;
2112bdd1243dSDimitry Andric     trap_opcode_size = sizeof(g_loongarch_opcode);
2113bdd1243dSDimitry Andric   } break;
2114bdd1243dSDimitry Andric 
21150b57cec5SDimitry Andric   default:
21165ffd83dbSDimitry Andric     return 0;
21170b57cec5SDimitry Andric   }
21180b57cec5SDimitry Andric 
21190b57cec5SDimitry Andric   assert(bp_site);
21200b57cec5SDimitry Andric   if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
21210b57cec5SDimitry Andric     return trap_opcode_size;
21220b57cec5SDimitry Andric 
21230b57cec5SDimitry Andric   return 0;
21240b57cec5SDimitry Andric }
212504eeddc0SDimitry Andric 
GetSiginfoType(const llvm::Triple & triple)212604eeddc0SDimitry Andric CompilerType Platform::GetSiginfoType(const llvm::Triple& triple) {
212704eeddc0SDimitry Andric   return CompilerType();
212804eeddc0SDimitry Andric }
212981ad6265SDimitry Andric 
GetExtraStartupCommands()213081ad6265SDimitry Andric Args Platform::GetExtraStartupCommands() {
213181ad6265SDimitry Andric   return {};
213281ad6265SDimitry Andric }
213381ad6265SDimitry Andric 
SetLocateModuleCallback(LocateModuleCallback callback)213406c3fb27SDimitry Andric void Platform::SetLocateModuleCallback(LocateModuleCallback callback) {
213506c3fb27SDimitry Andric   m_locate_module_callback = callback;
213606c3fb27SDimitry Andric }
213706c3fb27SDimitry Andric 
GetLocateModuleCallback() const213806c3fb27SDimitry Andric Platform::LocateModuleCallback Platform::GetLocateModuleCallback() const {
213906c3fb27SDimitry Andric   return m_locate_module_callback;
214006c3fb27SDimitry Andric }
214106c3fb27SDimitry Andric 
GetOrCreate(llvm::StringRef name)214281ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(llvm::StringRef name) {
214381ad6265SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
214481ad6265SDimitry Andric   for (const PlatformSP &platform_sp : m_platforms) {
214581ad6265SDimitry Andric     if (platform_sp->GetName() == name)
214681ad6265SDimitry Andric       return platform_sp;
214781ad6265SDimitry Andric   }
214881ad6265SDimitry Andric   return Create(name);
214981ad6265SDimitry Andric }
215081ad6265SDimitry Andric 
GetOrCreate(const ArchSpec & arch,const ArchSpec & process_host_arch,ArchSpec * platform_arch_ptr,Status & error)215181ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
215281ad6265SDimitry Andric                                      const ArchSpec &process_host_arch,
215381ad6265SDimitry Andric                                      ArchSpec *platform_arch_ptr,
215481ad6265SDimitry Andric                                      Status &error) {
215581ad6265SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
215681ad6265SDimitry Andric   // First try exact arch matches across all platforms already created
215781ad6265SDimitry Andric   for (const auto &platform_sp : m_platforms) {
2158bdd1243dSDimitry Andric     if (platform_sp->IsCompatibleArchitecture(
2159bdd1243dSDimitry Andric             arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr))
216081ad6265SDimitry Andric       return platform_sp;
216181ad6265SDimitry Andric   }
216281ad6265SDimitry Andric 
216381ad6265SDimitry Andric   // Next try compatible arch matches across all platforms already created
216481ad6265SDimitry Andric   for (const auto &platform_sp : m_platforms) {
2165bdd1243dSDimitry Andric     if (platform_sp->IsCompatibleArchitecture(arch, process_host_arch,
2166bdd1243dSDimitry Andric                                               ArchSpec::CompatibleMatch,
216781ad6265SDimitry Andric                                               platform_arch_ptr))
216881ad6265SDimitry Andric       return platform_sp;
216981ad6265SDimitry Andric   }
217081ad6265SDimitry Andric 
217181ad6265SDimitry Andric   PlatformCreateInstance create_callback;
217281ad6265SDimitry Andric   // First try exact arch matches across all platform plug-ins
217381ad6265SDimitry Andric   uint32_t idx;
217481ad6265SDimitry Andric   for (idx = 0;
217581ad6265SDimitry Andric        (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
217681ad6265SDimitry Andric        ++idx) {
217781ad6265SDimitry Andric     PlatformSP platform_sp = create_callback(false, &arch);
2178bdd1243dSDimitry Andric     if (platform_sp &&
2179bdd1243dSDimitry Andric         platform_sp->IsCompatibleArchitecture(
2180bdd1243dSDimitry Andric             arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr)) {
218181ad6265SDimitry Andric       m_platforms.push_back(platform_sp);
218281ad6265SDimitry Andric       return platform_sp;
218381ad6265SDimitry Andric     }
218481ad6265SDimitry Andric   }
218581ad6265SDimitry Andric   // Next try compatible arch matches across all platform plug-ins
218681ad6265SDimitry Andric   for (idx = 0;
218781ad6265SDimitry Andric        (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
218881ad6265SDimitry Andric        ++idx) {
218981ad6265SDimitry Andric     PlatformSP platform_sp = create_callback(false, &arch);
219081ad6265SDimitry Andric     if (platform_sp && platform_sp->IsCompatibleArchitecture(
2191bdd1243dSDimitry Andric                            arch, process_host_arch, ArchSpec::CompatibleMatch,
2192bdd1243dSDimitry Andric                            platform_arch_ptr)) {
219381ad6265SDimitry Andric       m_platforms.push_back(platform_sp);
219481ad6265SDimitry Andric       return platform_sp;
219581ad6265SDimitry Andric     }
219681ad6265SDimitry Andric   }
219781ad6265SDimitry Andric   if (platform_arch_ptr)
219881ad6265SDimitry Andric     platform_arch_ptr->Clear();
219981ad6265SDimitry Andric   return nullptr;
220081ad6265SDimitry Andric }
220181ad6265SDimitry Andric 
GetOrCreate(const ArchSpec & arch,const ArchSpec & process_host_arch,ArchSpec * platform_arch_ptr)220281ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
220381ad6265SDimitry Andric                                      const ArchSpec &process_host_arch,
220481ad6265SDimitry Andric                                      ArchSpec *platform_arch_ptr) {
220581ad6265SDimitry Andric   Status error;
220681ad6265SDimitry Andric   if (arch.IsValid())
220781ad6265SDimitry Andric     return GetOrCreate(arch, process_host_arch, platform_arch_ptr, error);
220881ad6265SDimitry Andric   return nullptr;
220981ad6265SDimitry Andric }
221081ad6265SDimitry Andric 
GetOrCreate(llvm::ArrayRef<ArchSpec> archs,const ArchSpec & process_host_arch,std::vector<PlatformSP> & candidates)221181ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
221281ad6265SDimitry Andric                                      const ArchSpec &process_host_arch,
221381ad6265SDimitry Andric                                      std::vector<PlatformSP> &candidates) {
221481ad6265SDimitry Andric   candidates.clear();
221581ad6265SDimitry Andric   candidates.reserve(archs.size());
221681ad6265SDimitry Andric 
221781ad6265SDimitry Andric   if (archs.empty())
221881ad6265SDimitry Andric     return nullptr;
221981ad6265SDimitry Andric 
222081ad6265SDimitry Andric   PlatformSP host_platform_sp = Platform::GetHostPlatform();
222181ad6265SDimitry Andric 
222281ad6265SDimitry Andric   // Prefer the selected platform if it matches at least one architecture.
222381ad6265SDimitry Andric   if (m_selected_platform_sp) {
222481ad6265SDimitry Andric     for (const ArchSpec &arch : archs) {
222581ad6265SDimitry Andric       if (m_selected_platform_sp->IsCompatibleArchitecture(
2226bdd1243dSDimitry Andric               arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
222781ad6265SDimitry Andric         return m_selected_platform_sp;
222881ad6265SDimitry Andric     }
222981ad6265SDimitry Andric   }
223081ad6265SDimitry Andric 
223181ad6265SDimitry Andric   // Prefer the host platform if it matches at least one architecture.
223281ad6265SDimitry Andric   if (host_platform_sp) {
223381ad6265SDimitry Andric     for (const ArchSpec &arch : archs) {
2234bdd1243dSDimitry Andric       if (host_platform_sp->IsCompatibleArchitecture(
2235bdd1243dSDimitry Andric               arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
223681ad6265SDimitry Andric         return host_platform_sp;
223781ad6265SDimitry Andric     }
223881ad6265SDimitry Andric   }
223981ad6265SDimitry Andric 
224081ad6265SDimitry Andric   // Collect a list of candidate platforms for the architectures.
224181ad6265SDimitry Andric   for (const ArchSpec &arch : archs) {
224281ad6265SDimitry Andric     if (PlatformSP platform = GetOrCreate(arch, process_host_arch, nullptr))
224381ad6265SDimitry Andric       candidates.push_back(platform);
224481ad6265SDimitry Andric   }
224581ad6265SDimitry Andric 
224681ad6265SDimitry Andric   // The selected or host platform didn't match any of the architectures. If
224781ad6265SDimitry Andric   // the same platform supports all architectures then that's the obvious next
224881ad6265SDimitry Andric   // best thing.
224981ad6265SDimitry Andric   if (candidates.size() == archs.size()) {
2250bdd1243dSDimitry Andric     if (llvm::all_of(candidates, [&](const PlatformSP &p) -> bool {
225181ad6265SDimitry Andric           return p->GetName() == candidates.front()->GetName();
225281ad6265SDimitry Andric         })) {
225381ad6265SDimitry Andric       return candidates.front();
225481ad6265SDimitry Andric     }
225581ad6265SDimitry Andric   }
225681ad6265SDimitry Andric 
225781ad6265SDimitry Andric   // At this point we either have no platforms that match the given
225881ad6265SDimitry Andric   // architectures or multiple platforms with no good way to disambiguate
225981ad6265SDimitry Andric   // between them.
226081ad6265SDimitry Andric   return nullptr;
226181ad6265SDimitry Andric }
226281ad6265SDimitry Andric 
Create(llvm::StringRef name)226381ad6265SDimitry Andric PlatformSP PlatformList::Create(llvm::StringRef name) {
226481ad6265SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
226581ad6265SDimitry Andric   PlatformSP platform_sp = Platform::Create(name);
226681ad6265SDimitry Andric   m_platforms.push_back(platform_sp);
226781ad6265SDimitry Andric   return platform_sp;
226881ad6265SDimitry Andric }
2269bdd1243dSDimitry Andric 
LoadPlatformBinaryAndSetup(Process * process,lldb::addr_t addr,bool notify)2270bdd1243dSDimitry Andric bool PlatformList::LoadPlatformBinaryAndSetup(Process *process,
2271bdd1243dSDimitry Andric                                               lldb::addr_t addr, bool notify) {
2272bdd1243dSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
2273bdd1243dSDimitry Andric 
2274bdd1243dSDimitry Andric   PlatformCreateInstance create_callback;
2275bdd1243dSDimitry Andric   for (int idx = 0;
2276bdd1243dSDimitry Andric        (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
2277bdd1243dSDimitry Andric        ++idx) {
2278bdd1243dSDimitry Andric     ArchSpec arch;
2279bdd1243dSDimitry Andric     PlatformSP platform_sp = create_callback(true, &arch);
2280bdd1243dSDimitry Andric     if (platform_sp) {
2281bdd1243dSDimitry Andric       if (platform_sp->LoadPlatformBinaryAndSetup(process, addr, notify))
2282bdd1243dSDimitry Andric         return true;
2283bdd1243dSDimitry Andric     }
2284bdd1243dSDimitry Andric   }
2285bdd1243dSDimitry Andric   return false;
2286bdd1243dSDimitry Andric }
2287