xref: /freebsd-src/contrib/llvm-project/lldb/source/Target/Platform.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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
560b57cec5SDimitry Andric static PlatformSP &GetHostPlatformSP() {
570b57cec5SDimitry Andric   static PlatformSP g_platform_sp;
580b57cec5SDimitry Andric   return g_platform_sp;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
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 
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 
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 
990b57cec5SDimitry 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 
1050b57cec5SDimitry Andric bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
10606c3fb27SDimitry Andric   return SetPropertyAtIndex(ePropertyUseModuleCache, use_module_cache);
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric FileSpec PlatformProperties::GetModuleCacheDirectory() const {
11006c3fb27SDimitry Andric   return GetPropertyAtIndexAs<FileSpec>(ePropertyModuleCacheDirectory, {});
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
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 
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.
1340b57cec5SDimitry Andric PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
1350b57cec5SDimitry Andric 
13681ad6265SDimitry Andric void Platform::Initialize() {}
1370b57cec5SDimitry Andric 
13881ad6265SDimitry Andric void Platform::Terminate() {}
1390b57cec5SDimitry Andric 
140349cc55cSDimitry Andric PlatformProperties &Platform::GetGlobalPlatformProperties() {
141349cc55cSDimitry Andric   static PlatformProperties g_settings;
142349cc55cSDimitry Andric   return g_settings;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
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 
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
1590b57cec5SDimitry Andric Platform::LocateExecutableScriptingResources(Target *target, Module &module,
16006c3fb27SDimitry Andric                                              Stream &feedback_stream) {
1610b57cec5SDimitry Andric   return FileSpecList();
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
164eaeb601bSDimitry Andric Status Platform::GetSharedModule(
165eaeb601bSDimitry Andric     const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
1660b57cec5SDimitry Andric     const FileSpecList *module_search_paths_ptr,
167eaeb601bSDimitry Andric     llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
1680b57cec5SDimitry Andric   if (IsHost())
169eaeb601bSDimitry Andric     return ModuleList::GetSharedModule(module_spec, module_sp,
170eaeb601bSDimitry Andric                                        module_search_paths_ptr, old_modules,
1710b57cec5SDimitry Andric                                        did_create_ptr, false);
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   // Module resolver lambda.
1740b57cec5SDimitry Andric   auto resolver = [&](const ModuleSpec &spec) {
1750b57cec5SDimitry Andric     Status error(eErrorTypeGeneric);
1760b57cec5SDimitry Andric     ModuleSpec resolved_spec;
1770b57cec5SDimitry Andric     // Check if we have sysroot set.
17806c3fb27SDimitry Andric     if (!m_sdk_sysroot.empty()) {
1790b57cec5SDimitry Andric       // Prepend sysroot to module spec.
1800b57cec5SDimitry Andric       resolved_spec = spec;
18106c3fb27SDimitry Andric       resolved_spec.GetFileSpec().PrependPathComponent(m_sdk_sysroot);
1820b57cec5SDimitry Andric       // Try to get shared module with resolved spec.
183eaeb601bSDimitry Andric       error = ModuleList::GetSharedModule(resolved_spec, module_sp,
184eaeb601bSDimitry Andric                                           module_search_paths_ptr, old_modules,
1850b57cec5SDimitry Andric                                           did_create_ptr, false);
1860b57cec5SDimitry Andric     }
1870b57cec5SDimitry Andric     // If we don't have sysroot or it didn't work then
1880b57cec5SDimitry Andric     // try original module spec.
1890b57cec5SDimitry Andric     if (!error.Success()) {
1900b57cec5SDimitry Andric       resolved_spec = spec;
191eaeb601bSDimitry Andric       error = ModuleList::GetSharedModule(resolved_spec, module_sp,
192eaeb601bSDimitry Andric                                           module_search_paths_ptr, old_modules,
1930b57cec5SDimitry Andric                                           did_create_ptr, false);
1940b57cec5SDimitry Andric     }
1950b57cec5SDimitry Andric     if (error.Success() && module_sp)
1960b57cec5SDimitry Andric       module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
1970b57cec5SDimitry Andric     return error;
1980b57cec5SDimitry Andric   };
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
2010b57cec5SDimitry Andric                                did_create_ptr);
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
2050b57cec5SDimitry Andric                              const ArchSpec &arch, ModuleSpec &module_spec) {
2060b57cec5SDimitry Andric   ModuleSpecList module_specs;
2070b57cec5SDimitry Andric   if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
2080b57cec5SDimitry Andric                                           module_specs) == 0)
2090b57cec5SDimitry Andric     return false;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   ModuleSpec matched_module_spec;
2120b57cec5SDimitry Andric   return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
2130b57cec5SDimitry Andric                                              module_spec);
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric 
21681ad6265SDimitry Andric PlatformSP Platform::Create(llvm::StringRef name) {
21781ad6265SDimitry Andric   lldb::PlatformSP platform_sp;
21881ad6265SDimitry Andric   if (name == GetHostPlatformName())
2190b57cec5SDimitry Andric     return GetHostPlatform();
2200b57cec5SDimitry Andric 
22181ad6265SDimitry Andric   if (PlatformCreateInstance create_callback =
22281ad6265SDimitry Andric           PluginManager::GetPlatformCreateCallbackForPluginName(name))
22381ad6265SDimitry Andric     return create_callback(true, nullptr);
22481ad6265SDimitry Andric   return nullptr;
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
2280b57cec5SDimitry Andric   if (platform)
2290b57cec5SDimitry Andric     return platform->GetAugmentedArchSpec(triple);
2300b57cec5SDimitry Andric   return HostInfo::GetAugmentedArchSpec(triple);
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric /// Default Constructor
2340b57cec5SDimitry Andric Platform::Platform(bool is_host)
2350b57cec5SDimitry Andric     : m_is_host(is_host), m_os_version_set_while_connected(false),
23681ad6265SDimitry Andric       m_system_arch_set_while_connected(false), m_max_uid_name_len(0),
23781ad6265SDimitry Andric       m_max_gid_name_len(0), m_supports_rsync(false), m_rsync_opts(),
23881ad6265SDimitry Andric       m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
2390b57cec5SDimitry Andric       m_ignores_remote_hostname(false), m_trap_handlers(),
2400b57cec5SDimitry Andric       m_calculated_trap_handlers(false),
2419dba64beSDimitry Andric       m_module_cache(std::make_unique<ModuleCache>()) {
24281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
2439dba64beSDimitry Andric   LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this));
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
246349cc55cSDimitry Andric Platform::~Platform() = default;
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric void Platform::GetStatus(Stream &strm) {
249349cc55cSDimitry Andric   strm.Format("  Platform: {0}\n", GetPluginName());
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric   ArchSpec arch(GetSystemArchitecture());
2520b57cec5SDimitry Andric   if (arch.IsValid()) {
2530b57cec5SDimitry Andric     if (!arch.GetTriple().str().empty()) {
2540b57cec5SDimitry Andric       strm.Printf("    Triple: ");
255480093f4SDimitry Andric       arch.DumpTriple(strm.AsRawOstream());
2560b57cec5SDimitry Andric       strm.EOL();
2570b57cec5SDimitry Andric     }
2580b57cec5SDimitry Andric   }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   llvm::VersionTuple os_version = GetOSVersion();
2610b57cec5SDimitry Andric   if (!os_version.empty()) {
2620b57cec5SDimitry Andric     strm.Format("OS Version: {0}", os_version.getAsString());
2630b57cec5SDimitry Andric 
264bdd1243dSDimitry Andric     if (std::optional<std::string> s = GetOSBuildString())
265349cc55cSDimitry Andric       strm.Format(" ({0})", *s);
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric     strm.EOL();
2680b57cec5SDimitry Andric   }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   if (IsHost()) {
2710b57cec5SDimitry Andric     strm.Printf("  Hostname: %s\n", GetHostname());
2720b57cec5SDimitry Andric   } else {
2730b57cec5SDimitry Andric     const bool is_connected = IsConnected();
2740b57cec5SDimitry Andric     if (is_connected)
2750b57cec5SDimitry Andric       strm.Printf("  Hostname: %s\n", GetHostname());
2760b57cec5SDimitry Andric     strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
2770b57cec5SDimitry Andric   }
2780b57cec5SDimitry Andric 
27906c3fb27SDimitry Andric   if (const std::string &sdk_root = GetSDKRootDirectory(); !sdk_root.empty())
28006c3fb27SDimitry Andric     strm.Format("   Sysroot: {0}\n", sdk_root);
28106c3fb27SDimitry Andric 
2820b57cec5SDimitry Andric   if (GetWorkingDirectory()) {
283bdd1243dSDimitry Andric     strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetPath().c_str());
2840b57cec5SDimitry Andric   }
2850b57cec5SDimitry Andric   if (!IsConnected())
2860b57cec5SDimitry Andric     return;
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   std::string specific_info(GetPlatformSpecificConnectionInformation());
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   if (!specific_info.empty())
2910b57cec5SDimitry Andric     strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
2925ffd83dbSDimitry Andric 
293bdd1243dSDimitry Andric   if (std::optional<std::string> s = GetOSKernelDescription())
294349cc55cSDimitry Andric     strm.Format("    Kernel: {0}\n", *s);
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric llvm::VersionTuple Platform::GetOSVersion(Process *process) {
2980b57cec5SDimitry Andric   std::lock_guard<std::mutex> guard(m_mutex);
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric   if (IsHost()) {
3010b57cec5SDimitry Andric     if (m_os_version.empty()) {
3020b57cec5SDimitry Andric       // We have a local host platform
3030b57cec5SDimitry Andric       m_os_version = HostInfo::GetOSVersion();
3040b57cec5SDimitry Andric       m_os_version_set_while_connected = !m_os_version.empty();
3050b57cec5SDimitry Andric     }
3060b57cec5SDimitry Andric   } else {
3070b57cec5SDimitry Andric     // We have a remote platform. We can only fetch the remote
3080b57cec5SDimitry Andric     // OS version if we are connected, and we don't want to do it
3090b57cec5SDimitry Andric     // more than once.
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric     const bool is_connected = IsConnected();
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric     bool fetch = false;
3140b57cec5SDimitry Andric     if (!m_os_version.empty()) {
3150b57cec5SDimitry Andric       // We have valid OS version info, check to make sure it wasn't manually
3160b57cec5SDimitry Andric       // set prior to connecting. If it was manually set prior to connecting,
3170b57cec5SDimitry Andric       // then lets fetch the actual OS version info if we are now connected.
3180b57cec5SDimitry Andric       if (is_connected && !m_os_version_set_while_connected)
3190b57cec5SDimitry Andric         fetch = true;
3200b57cec5SDimitry Andric     } else {
3210b57cec5SDimitry Andric       // We don't have valid OS version info, fetch it if we are connected
3220b57cec5SDimitry Andric       fetch = is_connected;
3230b57cec5SDimitry Andric     }
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric     if (fetch)
3260b57cec5SDimitry Andric       m_os_version_set_while_connected = GetRemoteOSVersion();
3270b57cec5SDimitry Andric   }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   if (!m_os_version.empty())
3300b57cec5SDimitry Andric     return m_os_version;
3310b57cec5SDimitry Andric   if (process) {
3320b57cec5SDimitry Andric     // Check with the process in case it can answer the question if a process
3330b57cec5SDimitry Andric     // was provided
3340b57cec5SDimitry Andric     return process->GetHostOSVersion();
3350b57cec5SDimitry Andric   }
3360b57cec5SDimitry Andric   return llvm::VersionTuple();
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric 
339bdd1243dSDimitry Andric std::optional<std::string> Platform::GetOSBuildString() {
3400b57cec5SDimitry Andric   if (IsHost())
341349cc55cSDimitry Andric     return HostInfo::GetOSBuildString();
342349cc55cSDimitry Andric   return GetRemoteOSBuildString();
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric 
345bdd1243dSDimitry Andric std::optional<std::string> Platform::GetOSKernelDescription() {
3460b57cec5SDimitry Andric   if (IsHost())
347349cc55cSDimitry Andric     return HostInfo::GetOSKernelDescription();
348349cc55cSDimitry Andric   return GetRemoteOSKernelDescription();
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric void Platform::AddClangModuleCompilationOptions(
3520b57cec5SDimitry Andric     Target *target, std::vector<std::string> &options) {
3530b57cec5SDimitry Andric   std::vector<std::string> default_compilation_options = {
3540b57cec5SDimitry Andric       "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   options.insert(options.end(), default_compilation_options.begin(),
3570b57cec5SDimitry Andric                  default_compilation_options.end());
3580b57cec5SDimitry Andric }
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric FileSpec Platform::GetWorkingDirectory() {
3610b57cec5SDimitry Andric   if (IsHost()) {
3620b57cec5SDimitry Andric     llvm::SmallString<64> cwd;
3630b57cec5SDimitry Andric     if (llvm::sys::fs::current_path(cwd))
3640b57cec5SDimitry Andric       return {};
3650b57cec5SDimitry Andric     else {
3660b57cec5SDimitry Andric       FileSpec file_spec(cwd);
3670b57cec5SDimitry Andric       FileSystem::Instance().Resolve(file_spec);
3680b57cec5SDimitry Andric       return file_spec;
3690b57cec5SDimitry Andric     }
3700b57cec5SDimitry Andric   } else {
3710b57cec5SDimitry Andric     if (!m_working_dir)
3720b57cec5SDimitry Andric       m_working_dir = GetRemoteWorkingDirectory();
3730b57cec5SDimitry Andric     return m_working_dir;
3740b57cec5SDimitry Andric   }
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric struct RecurseCopyBaton {
3780b57cec5SDimitry Andric   const FileSpec &dst;
3790b57cec5SDimitry Andric   Platform *platform_ptr;
3800b57cec5SDimitry Andric   Status error;
3810b57cec5SDimitry Andric };
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric static FileSystem::EnumerateDirectoryResult
3840b57cec5SDimitry Andric RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
3850b57cec5SDimitry Andric                      llvm::StringRef path) {
3860b57cec5SDimitry Andric   RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
3870b57cec5SDimitry Andric   FileSpec src(path);
3880b57cec5SDimitry Andric   namespace fs = llvm::sys::fs;
3890b57cec5SDimitry Andric   switch (ft) {
3900b57cec5SDimitry Andric   case fs::file_type::fifo_file:
3910b57cec5SDimitry Andric   case fs::file_type::socket_file:
3920b57cec5SDimitry Andric     // we have no way to copy pipes and sockets - ignore them and continue
3930b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
3940b57cec5SDimitry Andric     break;
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   case fs::file_type::directory_file: {
3970b57cec5SDimitry Andric     // make the new directory and get in there
3980b57cec5SDimitry Andric     FileSpec dst_dir = rc_baton->dst;
3990b57cec5SDimitry Andric     if (!dst_dir.GetFilename())
40006c3fb27SDimitry Andric       dst_dir.SetFilename(src.GetFilename());
4010b57cec5SDimitry Andric     Status error = rc_baton->platform_ptr->MakeDirectory(
4020b57cec5SDimitry Andric         dst_dir, lldb::eFilePermissionsDirectoryDefault);
4030b57cec5SDimitry Andric     if (error.Fail()) {
4040b57cec5SDimitry Andric       rc_baton->error.SetErrorStringWithFormat(
405bdd1243dSDimitry Andric           "unable to setup directory %s on remote end",
406bdd1243dSDimitry Andric           dst_dir.GetPath().c_str());
4070b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4080b57cec5SDimitry Andric     }
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric     // now recurse
4110b57cec5SDimitry Andric     std::string src_dir_path(src.GetPath());
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric     // Make a filespec that only fills in the directory of a FileSpec so when
4140b57cec5SDimitry Andric     // we enumerate we can quickly fill in the filename for dst copies
4150b57cec5SDimitry Andric     FileSpec recurse_dst;
416bdd1243dSDimitry Andric     recurse_dst.SetDirectory(dst_dir.GetPathAsConstString());
4170b57cec5SDimitry Andric     RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
4180b57cec5SDimitry Andric                                   Status()};
4190b57cec5SDimitry Andric     FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
4200b57cec5SDimitry Andric                                               RecurseCopy_Callback, &rc_baton2);
4210b57cec5SDimitry Andric     if (rc_baton2.error.Fail()) {
4220b57cec5SDimitry Andric       rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
4230b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4240b57cec5SDimitry Andric     }
4250b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
4260b57cec5SDimitry Andric   } break;
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   case fs::file_type::symlink_file: {
4290b57cec5SDimitry Andric     // copy the file and keep going
4300b57cec5SDimitry Andric     FileSpec dst_file = rc_baton->dst;
4310b57cec5SDimitry Andric     if (!dst_file.GetFilename())
432bdd1243dSDimitry Andric       dst_file.SetFilename(src.GetFilename());
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric     FileSpec src_resolved;
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric     rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric     if (rc_baton->error.Fail())
4390b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric     rc_baton->error =
4420b57cec5SDimitry Andric         rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric     if (rc_baton->error.Fail())
4450b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
4480b57cec5SDimitry Andric   } break;
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric   case fs::file_type::regular_file: {
4510b57cec5SDimitry Andric     // copy the file and keep going
4520b57cec5SDimitry Andric     FileSpec dst_file = rc_baton->dst;
4530b57cec5SDimitry Andric     if (!dst_file.GetFilename())
454bdd1243dSDimitry Andric       dst_file.SetFilename(src.GetFilename());
4550b57cec5SDimitry Andric     Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
4560b57cec5SDimitry Andric     if (err.Fail()) {
4570b57cec5SDimitry Andric       rc_baton->error.SetErrorString(err.AsCString());
4580b57cec5SDimitry Andric       return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4590b57cec5SDimitry Andric     }
4600b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultNext;
4610b57cec5SDimitry Andric   } break;
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   default:
4640b57cec5SDimitry Andric     rc_baton->error.SetErrorStringWithFormat(
4650b57cec5SDimitry Andric         "invalid file detected during copy: %s", src.GetPath().c_str());
4660b57cec5SDimitry Andric     return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
4670b57cec5SDimitry Andric     break;
4680b57cec5SDimitry Andric   }
4690b57cec5SDimitry Andric   llvm_unreachable("Unhandled file_type!");
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
4730b57cec5SDimitry Andric   Status error;
4740b57cec5SDimitry Andric 
47581ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
4769dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')",
4779dba64beSDimitry Andric             src.GetPath().c_str(), dst.GetPath().c_str());
4780b57cec5SDimitry Andric   FileSpec fixed_dst(dst);
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   if (!fixed_dst.GetFilename())
481bdd1243dSDimitry Andric     fixed_dst.SetFilename(src.GetFilename());
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   FileSpec working_dir = GetWorkingDirectory();
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   if (dst) {
4860b57cec5SDimitry Andric     if (dst.GetDirectory()) {
4870b57cec5SDimitry Andric       const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
4880b57cec5SDimitry Andric       if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
489bdd1243dSDimitry Andric         fixed_dst.SetDirectory(dst.GetDirectory());
4900b57cec5SDimitry Andric       }
4910b57cec5SDimitry Andric       // If the fixed destination file doesn't have a directory yet, then we
4920b57cec5SDimitry Andric       // must have a relative path. We will resolve this relative path against
4930b57cec5SDimitry Andric       // the platform's working directory
4940b57cec5SDimitry Andric       if (!fixed_dst.GetDirectory()) {
4950b57cec5SDimitry Andric         FileSpec relative_spec;
4960b57cec5SDimitry Andric         std::string path;
4970b57cec5SDimitry Andric         if (working_dir) {
4980b57cec5SDimitry Andric           relative_spec = working_dir;
4990b57cec5SDimitry Andric           relative_spec.AppendPathComponent(dst.GetPath());
500bdd1243dSDimitry Andric           fixed_dst.SetDirectory(relative_spec.GetDirectory());
5010b57cec5SDimitry Andric         } else {
5020b57cec5SDimitry Andric           error.SetErrorStringWithFormat(
5030b57cec5SDimitry Andric               "platform working directory must be valid for relative path '%s'",
5040b57cec5SDimitry Andric               dst.GetPath().c_str());
5050b57cec5SDimitry Andric           return error;
5060b57cec5SDimitry Andric         }
5070b57cec5SDimitry Andric       }
5080b57cec5SDimitry Andric     } else {
5090b57cec5SDimitry Andric       if (working_dir) {
510bdd1243dSDimitry Andric         fixed_dst.SetDirectory(working_dir.GetPathAsConstString());
5110b57cec5SDimitry Andric       } else {
5120b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
5130b57cec5SDimitry Andric             "platform working directory must be valid for relative path '%s'",
5140b57cec5SDimitry Andric             dst.GetPath().c_str());
5150b57cec5SDimitry Andric         return error;
5160b57cec5SDimitry Andric       }
5170b57cec5SDimitry Andric     }
5180b57cec5SDimitry Andric   } else {
5190b57cec5SDimitry Andric     if (working_dir) {
520bdd1243dSDimitry Andric       fixed_dst.SetDirectory(working_dir.GetPathAsConstString());
5210b57cec5SDimitry Andric     } else {
5220b57cec5SDimitry Andric       error.SetErrorStringWithFormat("platform working directory must be valid "
5230b57cec5SDimitry Andric                                      "when destination directory is empty");
5240b57cec5SDimitry Andric       return error;
5250b57cec5SDimitry Andric     }
5260b57cec5SDimitry Andric   }
5270b57cec5SDimitry Andric 
5289dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
5290b57cec5SDimitry Andric             src.GetPath().c_str(), dst.GetPath().c_str(),
5300b57cec5SDimitry Andric             fixed_dst.GetPath().c_str());
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric   if (GetSupportsRSync()) {
5330b57cec5SDimitry Andric     error = PutFile(src, dst);
5340b57cec5SDimitry Andric   } else {
5350b57cec5SDimitry Andric     namespace fs = llvm::sys::fs;
5360b57cec5SDimitry Andric     switch (fs::get_file_type(src.GetPath(), false)) {
5370b57cec5SDimitry Andric     case fs::file_type::directory_file: {
5380b57cec5SDimitry Andric       llvm::sys::fs::remove(fixed_dst.GetPath());
5390b57cec5SDimitry Andric       uint32_t permissions = FileSystem::Instance().GetPermissions(src);
5400b57cec5SDimitry Andric       if (permissions == 0)
5410b57cec5SDimitry Andric         permissions = eFilePermissionsDirectoryDefault;
5420b57cec5SDimitry Andric       error = MakeDirectory(fixed_dst, permissions);
5430b57cec5SDimitry Andric       if (error.Success()) {
5440b57cec5SDimitry Andric         // Make a filespec that only fills in the directory of a FileSpec so
5450b57cec5SDimitry Andric         // when we enumerate we can quickly fill in the filename for dst copies
5460b57cec5SDimitry Andric         FileSpec recurse_dst;
547bdd1243dSDimitry Andric         recurse_dst.SetDirectory(fixed_dst.GetPathAsConstString());
5480b57cec5SDimitry Andric         std::string src_dir_path(src.GetPath());
5490b57cec5SDimitry Andric         RecurseCopyBaton baton = {recurse_dst, this, Status()};
5500b57cec5SDimitry Andric         FileSystem::Instance().EnumerateDirectory(
5510b57cec5SDimitry Andric             src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
5520b57cec5SDimitry Andric         return baton.error;
5530b57cec5SDimitry Andric       }
5540b57cec5SDimitry Andric     } break;
5550b57cec5SDimitry Andric 
5560b57cec5SDimitry Andric     case fs::file_type::regular_file:
5570b57cec5SDimitry Andric       llvm::sys::fs::remove(fixed_dst.GetPath());
5580b57cec5SDimitry Andric       error = PutFile(src, fixed_dst);
5590b57cec5SDimitry Andric       break;
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric     case fs::file_type::symlink_file: {
5620b57cec5SDimitry Andric       llvm::sys::fs::remove(fixed_dst.GetPath());
5630b57cec5SDimitry Andric       FileSpec src_resolved;
5640b57cec5SDimitry Andric       error = FileSystem::Instance().Readlink(src, src_resolved);
5650b57cec5SDimitry Andric       if (error.Success())
5660b57cec5SDimitry Andric         error = CreateSymlink(dst, src_resolved);
5670b57cec5SDimitry Andric     } break;
5680b57cec5SDimitry Andric     case fs::file_type::fifo_file:
5690b57cec5SDimitry Andric       error.SetErrorString("platform install doesn't handle pipes");
5700b57cec5SDimitry Andric       break;
5710b57cec5SDimitry Andric     case fs::file_type::socket_file:
5720b57cec5SDimitry Andric       error.SetErrorString("platform install doesn't handle sockets");
5730b57cec5SDimitry Andric       break;
5740b57cec5SDimitry Andric     default:
5750b57cec5SDimitry Andric       error.SetErrorString(
5760b57cec5SDimitry Andric           "platform install doesn't handle non file or directory items");
5770b57cec5SDimitry Andric       break;
5780b57cec5SDimitry Andric     }
5790b57cec5SDimitry Andric   }
5800b57cec5SDimitry Andric   return error;
5810b57cec5SDimitry Andric }
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
5840b57cec5SDimitry Andric   if (IsHost()) {
58581ad6265SDimitry Andric     Log *log = GetLog(LLDBLog::Platform);
5860b57cec5SDimitry Andric     LLDB_LOG(log, "{0}", file_spec);
5870b57cec5SDimitry Andric     if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
5880b57cec5SDimitry Andric       LLDB_LOG(log, "error: {0}", ec.message());
5890b57cec5SDimitry Andric       return false;
5900b57cec5SDimitry Andric     }
5910b57cec5SDimitry Andric     return true;
5920b57cec5SDimitry Andric   } else {
5930b57cec5SDimitry Andric     m_working_dir.Clear();
5940b57cec5SDimitry Andric     return SetRemoteWorkingDirectory(file_spec);
5950b57cec5SDimitry Andric   }
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric Status Platform::MakeDirectory(const FileSpec &file_spec,
5990b57cec5SDimitry Andric                                uint32_t permissions) {
6000b57cec5SDimitry Andric   if (IsHost())
6010b57cec5SDimitry Andric     return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
6020b57cec5SDimitry Andric   else {
6030b57cec5SDimitry Andric     Status error;
604349cc55cSDimitry Andric     error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
605349cc55cSDimitry Andric                                     GetPluginName(), LLVM_PRETTY_FUNCTION);
6060b57cec5SDimitry Andric     return error;
6070b57cec5SDimitry Andric   }
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric Status Platform::GetFilePermissions(const FileSpec &file_spec,
6110b57cec5SDimitry Andric                                     uint32_t &file_permissions) {
6120b57cec5SDimitry Andric   if (IsHost()) {
6130b57cec5SDimitry Andric     auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
6140b57cec5SDimitry Andric     if (Value)
6150b57cec5SDimitry Andric       file_permissions = Value.get();
6160b57cec5SDimitry Andric     return Status(Value.getError());
6170b57cec5SDimitry Andric   } else {
6180b57cec5SDimitry Andric     Status error;
619349cc55cSDimitry Andric     error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
620349cc55cSDimitry Andric                                     GetPluginName(), LLVM_PRETTY_FUNCTION);
6210b57cec5SDimitry Andric     return error;
6220b57cec5SDimitry Andric   }
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric Status Platform::SetFilePermissions(const FileSpec &file_spec,
6260b57cec5SDimitry Andric                                     uint32_t file_permissions) {
6270b57cec5SDimitry Andric   if (IsHost()) {
6280b57cec5SDimitry Andric     auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
6290b57cec5SDimitry Andric     return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
6300b57cec5SDimitry Andric   } else {
6310b57cec5SDimitry Andric     Status error;
632349cc55cSDimitry Andric     error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
633349cc55cSDimitry Andric                                     GetPluginName(), LLVM_PRETTY_FUNCTION);
6340b57cec5SDimitry Andric     return error;
6350b57cec5SDimitry Andric   }
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric 
63881ad6265SDimitry Andric user_id_t Platform::OpenFile(const FileSpec &file_spec,
63981ad6265SDimitry Andric                                    File::OpenOptions flags, uint32_t mode,
64081ad6265SDimitry Andric                                    Status &error) {
64181ad6265SDimitry Andric   if (IsHost())
64281ad6265SDimitry Andric     return FileCache::GetInstance().OpenFile(file_spec, flags, mode, error);
64381ad6265SDimitry Andric   return UINT64_MAX;
64481ad6265SDimitry Andric }
64581ad6265SDimitry Andric 
64681ad6265SDimitry Andric bool Platform::CloseFile(user_id_t fd, Status &error) {
64781ad6265SDimitry Andric   if (IsHost())
64881ad6265SDimitry Andric     return FileCache::GetInstance().CloseFile(fd, error);
64981ad6265SDimitry Andric   return false;
65081ad6265SDimitry Andric }
65181ad6265SDimitry Andric 
65281ad6265SDimitry Andric user_id_t Platform::GetFileSize(const FileSpec &file_spec) {
65381ad6265SDimitry Andric   if (!IsHost())
65481ad6265SDimitry Andric     return UINT64_MAX;
65581ad6265SDimitry Andric 
65681ad6265SDimitry Andric   uint64_t Size;
65781ad6265SDimitry Andric   if (llvm::sys::fs::file_size(file_spec.GetPath(), Size))
65881ad6265SDimitry Andric     return 0;
65981ad6265SDimitry Andric   return Size;
66081ad6265SDimitry Andric }
66181ad6265SDimitry Andric 
66281ad6265SDimitry Andric uint64_t Platform::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
66381ad6265SDimitry Andric                             uint64_t dst_len, Status &error) {
66481ad6265SDimitry Andric   if (IsHost())
66581ad6265SDimitry Andric     return FileCache::GetInstance().ReadFile(fd, offset, dst, dst_len, error);
66681ad6265SDimitry Andric   error.SetErrorStringWithFormatv(
66781ad6265SDimitry Andric       "Platform::ReadFile() is not supported in the {0} platform",
66881ad6265SDimitry Andric       GetPluginName());
66981ad6265SDimitry Andric   return -1;
67081ad6265SDimitry Andric }
67181ad6265SDimitry Andric 
67281ad6265SDimitry Andric uint64_t Platform::WriteFile(lldb::user_id_t fd, uint64_t offset,
67381ad6265SDimitry Andric                              const void *src, uint64_t src_len, Status &error) {
67481ad6265SDimitry Andric   if (IsHost())
67581ad6265SDimitry Andric     return FileCache::GetInstance().WriteFile(fd, offset, src, src_len, error);
67681ad6265SDimitry Andric   error.SetErrorStringWithFormatv(
67781ad6265SDimitry Andric       "Platform::WriteFile() is not supported in the {0} platform",
67881ad6265SDimitry Andric       GetPluginName());
67981ad6265SDimitry Andric   return -1;
68081ad6265SDimitry Andric }
68181ad6265SDimitry Andric 
68281ad6265SDimitry Andric UserIDResolver &Platform::GetUserIDResolver() {
68381ad6265SDimitry Andric   if (IsHost())
68481ad6265SDimitry Andric     return HostInfo::GetUserIDResolver();
68581ad6265SDimitry Andric   return UserIDResolver::GetNoopResolver();
68681ad6265SDimitry Andric }
6870b57cec5SDimitry Andric 
6880b57cec5SDimitry Andric const char *Platform::GetHostname() {
6890b57cec5SDimitry Andric   if (IsHost())
6900b57cec5SDimitry Andric     return "127.0.0.1";
6910b57cec5SDimitry Andric 
69281ad6265SDimitry Andric   if (m_hostname.empty())
6930b57cec5SDimitry Andric     return nullptr;
69481ad6265SDimitry Andric   return m_hostname.c_str();
6950b57cec5SDimitry Andric }
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric ConstString Platform::GetFullNameForDylib(ConstString basename) {
6980b57cec5SDimitry Andric   return basename;
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
70281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
7039dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')",
704bdd1243dSDimitry Andric             working_dir.GetPath().c_str());
7050b57cec5SDimitry Andric   m_working_dir = working_dir;
7060b57cec5SDimitry Andric   return true;
7070b57cec5SDimitry Andric }
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric bool Platform::SetOSVersion(llvm::VersionTuple version) {
7100b57cec5SDimitry Andric   if (IsHost()) {
7110b57cec5SDimitry Andric     // We don't need anyone setting the OS version for the host platform, we
7120b57cec5SDimitry Andric     // should be able to figure it out by calling HostInfo::GetOSVersion(...).
7130b57cec5SDimitry Andric     return false;
7140b57cec5SDimitry Andric   } else {
7150b57cec5SDimitry Andric     // We have a remote platform, allow setting the target OS version if we
7160b57cec5SDimitry Andric     // aren't connected, since if we are connected, we should be able to
7170b57cec5SDimitry Andric     // request the remote OS version from the connected platform.
7180b57cec5SDimitry Andric     if (IsConnected())
7190b57cec5SDimitry Andric       return false;
7200b57cec5SDimitry Andric     else {
7210b57cec5SDimitry Andric       // We aren't connected and we might want to set the OS version ahead of
7220b57cec5SDimitry Andric       // time before we connect so we can peruse files and use a local SDK or
7230b57cec5SDimitry Andric       // PDK cache of support files to disassemble or do other things.
7240b57cec5SDimitry Andric       m_os_version = version;
7250b57cec5SDimitry Andric       return true;
7260b57cec5SDimitry Andric     }
7270b57cec5SDimitry Andric   }
7280b57cec5SDimitry Andric   return false;
7290b57cec5SDimitry Andric }
7300b57cec5SDimitry Andric 
7310b57cec5SDimitry Andric Status
7320b57cec5SDimitry Andric Platform::ResolveExecutable(const ModuleSpec &module_spec,
7330b57cec5SDimitry Andric                             lldb::ModuleSP &exe_module_sp,
7340b57cec5SDimitry Andric                             const FileSpecList *module_search_paths_ptr) {
735349cc55cSDimitry Andric 
736349cc55cSDimitry Andric   // We may connect to a process and use the provided executable (Don't use
737349cc55cSDimitry Andric   // local $PATH).
738349cc55cSDimitry Andric   ModuleSpec resolved_module_spec(module_spec);
739349cc55cSDimitry Andric 
740349cc55cSDimitry Andric   // Resolve any executable within a bundle on MacOSX
741349cc55cSDimitry Andric   Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
742349cc55cSDimitry Andric 
743*0fca6ea1SDimitry Andric   if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()) &&
744*0fca6ea1SDimitry Andric       !module_spec.GetUUID().IsValid())
745*0fca6ea1SDimitry Andric     return Status::createWithFormat("'{0}' does not exist",
746*0fca6ea1SDimitry Andric                                     resolved_module_spec.GetFileSpec());
747*0fca6ea1SDimitry Andric 
748349cc55cSDimitry Andric   if (resolved_module_spec.GetArchitecture().IsValid() ||
749349cc55cSDimitry Andric       resolved_module_spec.GetUUID().IsValid()) {
750*0fca6ea1SDimitry Andric     Status error =
751*0fca6ea1SDimitry Andric         ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
752*0fca6ea1SDimitry Andric                                     module_search_paths_ptr, nullptr, nullptr);
753349cc55cSDimitry Andric 
754349cc55cSDimitry Andric     if (exe_module_sp && exe_module_sp->GetObjectFile())
755349cc55cSDimitry Andric       return error;
756349cc55cSDimitry Andric     exe_module_sp.reset();
757349cc55cSDimitry Andric   }
758*0fca6ea1SDimitry Andric   // No valid architecture was specified or the exact arch wasn't found.
759*0fca6ea1SDimitry Andric   // Ask the platform for the architectures that we should be using (in the
760*0fca6ea1SDimitry Andric   // correct order) and see if we can find a match that way.
761349cc55cSDimitry Andric   StreamString arch_names;
762349cc55cSDimitry Andric   llvm::ListSeparator LS;
76381ad6265SDimitry Andric   ArchSpec process_host_arch;
764*0fca6ea1SDimitry Andric   Status error;
76581ad6265SDimitry Andric   for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
766349cc55cSDimitry Andric     resolved_module_spec.GetArchitecture() = arch;
767*0fca6ea1SDimitry Andric     error =
768*0fca6ea1SDimitry Andric         ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
769*0fca6ea1SDimitry Andric                                     module_search_paths_ptr, nullptr, nullptr);
770349cc55cSDimitry Andric     if (error.Success()) {
771349cc55cSDimitry Andric       if (exe_module_sp && exe_module_sp->GetObjectFile())
772349cc55cSDimitry Andric         break;
773349cc55cSDimitry Andric       error.SetErrorToGenericError();
774349cc55cSDimitry Andric     }
775349cc55cSDimitry Andric 
776349cc55cSDimitry Andric     arch_names << LS << arch.GetArchitectureName();
777349cc55cSDimitry Andric   }
778349cc55cSDimitry Andric 
779*0fca6ea1SDimitry Andric   if (exe_module_sp && error.Success())
780*0fca6ea1SDimitry Andric     return {};
781*0fca6ea1SDimitry Andric 
782*0fca6ea1SDimitry Andric   if (!FileSystem::Instance().Readable(resolved_module_spec.GetFileSpec()))
783*0fca6ea1SDimitry Andric     return Status::createWithFormat("'{0}' is not readable",
784*0fca6ea1SDimitry Andric                                     resolved_module_spec.GetFileSpec());
785*0fca6ea1SDimitry Andric 
786*0fca6ea1SDimitry Andric   if (!ObjectFile::IsObjectFile(resolved_module_spec.GetFileSpec()))
787*0fca6ea1SDimitry Andric     return Status::createWithFormat("'{0}' is not a valid executable",
788*0fca6ea1SDimitry Andric                                     resolved_module_spec.GetFileSpec());
789*0fca6ea1SDimitry Andric 
790*0fca6ea1SDimitry Andric   return Status::createWithFormat(
791349cc55cSDimitry Andric       "'{0}' doesn't contain any '{1}' platform architectures: {2}",
792349cc55cSDimitry Andric       resolved_module_spec.GetFileSpec(), GetPluginName(),
793349cc55cSDimitry Andric       arch_names.GetData());
794349cc55cSDimitry Andric }
795349cc55cSDimitry Andric 
7960b57cec5SDimitry Andric Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
7970b57cec5SDimitry Andric                                    FileSpec &sym_file) {
7980b57cec5SDimitry Andric   Status error;
7990b57cec5SDimitry Andric   if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
8000b57cec5SDimitry Andric     sym_file = sym_spec.GetSymbolFileSpec();
8010b57cec5SDimitry Andric   else
8020b57cec5SDimitry Andric     error.SetErrorString("unable to resolve symbol file");
8030b57cec5SDimitry Andric   return error;
8040b57cec5SDimitry Andric }
8050b57cec5SDimitry Andric 
8060b57cec5SDimitry Andric bool Platform::ResolveRemotePath(const FileSpec &platform_path,
8070b57cec5SDimitry Andric                                  FileSpec &resolved_platform_path) {
8080b57cec5SDimitry Andric   resolved_platform_path = platform_path;
8090b57cec5SDimitry Andric   FileSystem::Instance().Resolve(resolved_platform_path);
8100b57cec5SDimitry Andric   return true;
8110b57cec5SDimitry Andric }
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric const ArchSpec &Platform::GetSystemArchitecture() {
8140b57cec5SDimitry Andric   if (IsHost()) {
8150b57cec5SDimitry Andric     if (!m_system_arch.IsValid()) {
8160b57cec5SDimitry Andric       // We have a local host platform
8170b57cec5SDimitry Andric       m_system_arch = HostInfo::GetArchitecture();
8180b57cec5SDimitry Andric       m_system_arch_set_while_connected = m_system_arch.IsValid();
8190b57cec5SDimitry Andric     }
8200b57cec5SDimitry Andric   } else {
8210b57cec5SDimitry Andric     // We have a remote platform. We can only fetch the remote system
8220b57cec5SDimitry Andric     // architecture if we are connected, and we don't want to do it more than
8230b57cec5SDimitry Andric     // once.
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric     const bool is_connected = IsConnected();
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric     bool fetch = false;
8280b57cec5SDimitry Andric     if (m_system_arch.IsValid()) {
8290b57cec5SDimitry Andric       // We have valid OS version info, check to make sure it wasn't manually
8300b57cec5SDimitry Andric       // set prior to connecting. If it was manually set prior to connecting,
8310b57cec5SDimitry Andric       // then lets fetch the actual OS version info if we are now connected.
8320b57cec5SDimitry Andric       if (is_connected && !m_system_arch_set_while_connected)
8330b57cec5SDimitry Andric         fetch = true;
8340b57cec5SDimitry Andric     } else {
8350b57cec5SDimitry Andric       // We don't have valid OS version info, fetch it if we are connected
8360b57cec5SDimitry Andric       fetch = is_connected;
8370b57cec5SDimitry Andric     }
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric     if (fetch) {
8400b57cec5SDimitry Andric       m_system_arch = GetRemoteSystemArchitecture();
8410b57cec5SDimitry Andric       m_system_arch_set_while_connected = m_system_arch.IsValid();
8420b57cec5SDimitry Andric     }
8430b57cec5SDimitry Andric   }
8440b57cec5SDimitry Andric   return m_system_arch;
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
8480b57cec5SDimitry Andric   if (triple.empty())
8490b57cec5SDimitry Andric     return ArchSpec();
8500b57cec5SDimitry Andric   llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
8510b57cec5SDimitry Andric   if (!ArchSpec::ContainsOnlyArch(normalized_triple))
8520b57cec5SDimitry Andric     return ArchSpec(triple);
8530b57cec5SDimitry Andric 
8540b57cec5SDimitry Andric   if (auto kind = HostInfo::ParseArchitectureKind(triple))
8550b57cec5SDimitry Andric     return HostInfo::GetArchitecture(*kind);
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric   ArchSpec compatible_arch;
8580b57cec5SDimitry Andric   ArchSpec raw_arch(triple);
859bdd1243dSDimitry Andric   if (!IsCompatibleArchitecture(raw_arch, {}, ArchSpec::CompatibleMatch,
860bdd1243dSDimitry Andric                                 &compatible_arch))
8610b57cec5SDimitry Andric     return raw_arch;
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric   if (!compatible_arch.IsValid())
8640b57cec5SDimitry Andric     return ArchSpec(normalized_triple);
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric   const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
8670b57cec5SDimitry Andric   if (normalized_triple.getVendorName().empty())
8680b57cec5SDimitry Andric     normalized_triple.setVendor(compatible_triple.getVendor());
8690b57cec5SDimitry Andric   if (normalized_triple.getOSName().empty())
8700b57cec5SDimitry Andric     normalized_triple.setOS(compatible_triple.getOS());
8710b57cec5SDimitry Andric   if (normalized_triple.getEnvironmentName().empty())
8720b57cec5SDimitry Andric     normalized_triple.setEnvironment(compatible_triple.getEnvironment());
8730b57cec5SDimitry Andric   return ArchSpec(normalized_triple);
8740b57cec5SDimitry Andric }
8750b57cec5SDimitry Andric 
8760b57cec5SDimitry Andric Status Platform::ConnectRemote(Args &args) {
8770b57cec5SDimitry Andric   Status error;
8780b57cec5SDimitry Andric   if (IsHost())
879349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
880349cc55cSDimitry Andric         "The currently selected platform ({0}) is "
8810b57cec5SDimitry Andric         "the host platform and is always connected.",
882349cc55cSDimitry Andric         GetPluginName());
8830b57cec5SDimitry Andric   else
884349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
885349cc55cSDimitry Andric         "Platform::ConnectRemote() is not supported by {0}", GetPluginName());
8860b57cec5SDimitry Andric   return error;
8870b57cec5SDimitry Andric }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric Status Platform::DisconnectRemote() {
8900b57cec5SDimitry Andric   Status error;
8910b57cec5SDimitry Andric   if (IsHost())
892349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
893349cc55cSDimitry Andric         "The currently selected platform ({0}) is "
8940b57cec5SDimitry Andric         "the host platform and is always connected.",
895349cc55cSDimitry Andric         GetPluginName());
8960b57cec5SDimitry Andric   else
897349cc55cSDimitry Andric     error.SetErrorStringWithFormatv(
898349cc55cSDimitry Andric         "Platform::DisconnectRemote() is not supported by {0}",
899349cc55cSDimitry Andric         GetPluginName());
9000b57cec5SDimitry Andric   return error;
9010b57cec5SDimitry Andric }
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric bool Platform::GetProcessInfo(lldb::pid_t pid,
9040b57cec5SDimitry Andric                               ProcessInstanceInfo &process_info) {
9050b57cec5SDimitry Andric   // Take care of the host case so that each subclass can just call this
9060b57cec5SDimitry Andric   // function to get the host functionality.
9070b57cec5SDimitry Andric   if (IsHost())
9080b57cec5SDimitry Andric     return Host::GetProcessInfo(pid, process_info);
9090b57cec5SDimitry Andric   return false;
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
9130b57cec5SDimitry Andric                                  ProcessInstanceInfoList &process_infos) {
9140b57cec5SDimitry Andric   // Take care of the host case so that each subclass can just call this
9150b57cec5SDimitry Andric   // function to get the host functionality.
9160b57cec5SDimitry Andric   uint32_t match_count = 0;
9170b57cec5SDimitry Andric   if (IsHost())
9180b57cec5SDimitry Andric     match_count = Host::FindProcesses(match_info, process_infos);
9190b57cec5SDimitry Andric   return match_count;
9200b57cec5SDimitry Andric }
9210b57cec5SDimitry Andric 
9225f757f3fSDimitry Andric ProcessInstanceInfoList Platform::GetAllProcesses() {
9235f757f3fSDimitry Andric   ProcessInstanceInfoList processes;
9245f757f3fSDimitry Andric   ProcessInstanceInfoMatch match;
9255f757f3fSDimitry Andric   assert(match.MatchAllProcesses());
9265f757f3fSDimitry Andric   FindProcesses(match, processes);
9275f757f3fSDimitry Andric   return processes;
9285f757f3fSDimitry Andric }
9295f757f3fSDimitry Andric 
9300b57cec5SDimitry Andric Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
9310b57cec5SDimitry Andric   Status error;
93281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
9339dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::%s()", __FUNCTION__);
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   // Take care of the host case so that each subclass can just call this
9360b57cec5SDimitry Andric   // function to get the host functionality.
9370b57cec5SDimitry Andric   if (IsHost()) {
9380b57cec5SDimitry Andric     if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
9390b57cec5SDimitry Andric       launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
9400b57cec5SDimitry Andric 
9410b57cec5SDimitry Andric     if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
9420b57cec5SDimitry Andric       const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
9430b57cec5SDimitry Andric       const bool first_arg_is_full_shell_command = false;
9440b57cec5SDimitry Andric       uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
9450b57cec5SDimitry Andric       if (log) {
9460b57cec5SDimitry Andric         const FileSpec &shell = launch_info.GetShell();
9470b57cec5SDimitry Andric         std::string shell_str = (shell) ? shell.GetPath() : "<null>";
9489dba64beSDimitry Andric         LLDB_LOGF(log,
9490b57cec5SDimitry Andric                   "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
9500b57cec5SDimitry Andric                   ", shell is '%s'",
9510b57cec5SDimitry Andric                   __FUNCTION__, num_resumes, shell_str.c_str());
9520b57cec5SDimitry Andric       }
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric       if (!launch_info.ConvertArgumentsForLaunchingInShell(
955e8d8bef9SDimitry Andric               error, will_debug, first_arg_is_full_shell_command, num_resumes))
9560b57cec5SDimitry Andric         return error;
9570b57cec5SDimitry Andric     } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
9580b57cec5SDimitry Andric       error = ShellExpandArguments(launch_info);
9590b57cec5SDimitry Andric       if (error.Fail()) {
9600b57cec5SDimitry Andric         error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
9610b57cec5SDimitry Andric                                        "consider launching with 'process "
9620b57cec5SDimitry Andric                                        "launch'.",
9630b57cec5SDimitry Andric                                        error.AsCString("unknown"));
9640b57cec5SDimitry Andric         return error;
9650b57cec5SDimitry Andric       }
9660b57cec5SDimitry Andric     }
9670b57cec5SDimitry Andric 
9689dba64beSDimitry Andric     LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32,
9690b57cec5SDimitry Andric               __FUNCTION__, launch_info.GetResumeCount());
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric     error = Host::LaunchProcess(launch_info);
9720b57cec5SDimitry Andric   } else
9730b57cec5SDimitry Andric     error.SetErrorString(
9740b57cec5SDimitry Andric         "base lldb_private::Platform class can't launch remote processes");
9750b57cec5SDimitry Andric   return error;
9760b57cec5SDimitry Andric }
9770b57cec5SDimitry Andric 
9780b57cec5SDimitry Andric Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
9790b57cec5SDimitry Andric   if (IsHost())
9800b57cec5SDimitry Andric     return Host::ShellExpandArguments(launch_info);
9810b57cec5SDimitry Andric   return Status("base lldb_private::Platform class can't expand arguments");
9820b57cec5SDimitry Andric }
9830b57cec5SDimitry Andric 
9840b57cec5SDimitry Andric Status Platform::KillProcess(const lldb::pid_t pid) {
98581ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
9869dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric   if (!IsHost()) {
9890b57cec5SDimitry Andric     return Status(
990349cc55cSDimitry Andric         "base lldb_private::Platform class can't kill remote processes");
9910b57cec5SDimitry Andric   }
992349cc55cSDimitry Andric   Host::Kill(pid, SIGKILL);
9930b57cec5SDimitry Andric   return Status();
9940b57cec5SDimitry Andric }
9950b57cec5SDimitry Andric 
996349cc55cSDimitry Andric lldb::ProcessSP Platform::DebugProcess(ProcessLaunchInfo &launch_info,
997349cc55cSDimitry Andric                                        Debugger &debugger, Target &target,
9980b57cec5SDimitry Andric                                        Status &error) {
99981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
100006c3fb27SDimitry Andric   LLDB_LOG(log, "target = {0}", &target);
10010b57cec5SDimitry Andric 
10020b57cec5SDimitry Andric   ProcessSP process_sp;
10030b57cec5SDimitry Andric   // Make sure we stop at the entry point
10040b57cec5SDimitry Andric   launch_info.GetFlags().Set(eLaunchFlagDebug);
10050b57cec5SDimitry Andric   // We always launch the process we are going to debug in a separate process
10060b57cec5SDimitry Andric   // group, since then we can handle ^C interrupts ourselves w/o having to
10070b57cec5SDimitry Andric   // worry about the target getting them as well.
10080b57cec5SDimitry Andric   launch_info.SetLaunchInSeparateProcessGroup(true);
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric   // Allow any StructuredData process-bound plugins to adjust the launch info
10110b57cec5SDimitry Andric   // if needed
10120b57cec5SDimitry Andric   size_t i = 0;
10130b57cec5SDimitry Andric   bool iteration_complete = false;
10140b57cec5SDimitry Andric   // Note iteration can't simply go until a nullptr callback is returned, as it
10150b57cec5SDimitry Andric   // is valid for a plugin to not supply a filter.
10160b57cec5SDimitry Andric   auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
10170b57cec5SDimitry Andric   for (auto filter_callback = get_filter_func(i, iteration_complete);
10180b57cec5SDimitry Andric        !iteration_complete;
10190b57cec5SDimitry Andric        filter_callback = get_filter_func(++i, iteration_complete)) {
10200b57cec5SDimitry Andric     if (filter_callback) {
10210b57cec5SDimitry Andric       // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
1022349cc55cSDimitry Andric       error = (*filter_callback)(launch_info, &target);
10230b57cec5SDimitry Andric       if (!error.Success()) {
10249dba64beSDimitry Andric         LLDB_LOGF(log,
10259dba64beSDimitry Andric                   "Platform::%s() StructuredDataPlugin launch "
10260b57cec5SDimitry Andric                   "filter failed.",
10270b57cec5SDimitry Andric                   __FUNCTION__);
10280b57cec5SDimitry Andric         return process_sp;
10290b57cec5SDimitry Andric       }
10300b57cec5SDimitry Andric     }
10310b57cec5SDimitry Andric   }
10320b57cec5SDimitry Andric 
10330b57cec5SDimitry Andric   error = LaunchProcess(launch_info);
10340b57cec5SDimitry Andric   if (error.Success()) {
10359dba64beSDimitry Andric     LLDB_LOGF(log,
10369dba64beSDimitry Andric               "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")",
10370b57cec5SDimitry Andric               __FUNCTION__, launch_info.GetProcessID());
10380b57cec5SDimitry Andric     if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
10390b57cec5SDimitry Andric       ProcessAttachInfo attach_info(launch_info);
1040349cc55cSDimitry Andric       process_sp = Attach(attach_info, debugger, &target, error);
10410b57cec5SDimitry Andric       if (process_sp) {
1042349cc55cSDimitry Andric         LLDB_LOG(log, "Attach() succeeded, Process plugin: {0}",
1043349cc55cSDimitry Andric                  process_sp->GetPluginName());
10440b57cec5SDimitry Andric         launch_info.SetHijackListener(attach_info.GetHijackListener());
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric         // Since we attached to the process, it will think it needs to detach
10470b57cec5SDimitry Andric         // if the process object just goes away without an explicit call to
10480b57cec5SDimitry Andric         // Process::Kill() or Process::Detach(), so let it know to kill the
10490b57cec5SDimitry Andric         // process if this happens.
10500b57cec5SDimitry Andric         process_sp->SetShouldDetach(false);
10510b57cec5SDimitry Andric 
10520b57cec5SDimitry Andric         // If we didn't have any file actions, the pseudo terminal might have
10535ffd83dbSDimitry Andric         // been used where the secondary side was given as the file to open for
1054349cc55cSDimitry Andric         // stdin/out/err after we have already opened the primary so we can
10550b57cec5SDimitry Andric         // read/write stdin/out/err.
10565ffd83dbSDimitry Andric         int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
10570b57cec5SDimitry Andric         if (pty_fd != PseudoTerminal::invalid_fd) {
10580b57cec5SDimitry Andric           process_sp->SetSTDIOFileDescriptor(pty_fd);
10590b57cec5SDimitry Andric         }
10600b57cec5SDimitry Andric       } else {
10619dba64beSDimitry Andric         LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__,
10620b57cec5SDimitry Andric                   error.AsCString());
10630b57cec5SDimitry Andric       }
10640b57cec5SDimitry Andric     } else {
10659dba64beSDimitry Andric       LLDB_LOGF(log,
10669dba64beSDimitry Andric                 "Platform::%s LaunchProcess() returned launch_info with "
10670b57cec5SDimitry Andric                 "invalid process id",
10680b57cec5SDimitry Andric                 __FUNCTION__);
10690b57cec5SDimitry Andric     }
10700b57cec5SDimitry Andric   } else {
10719dba64beSDimitry Andric     LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
10720b57cec5SDimitry Andric               error.AsCString());
10730b57cec5SDimitry Andric   }
10740b57cec5SDimitry Andric 
10750b57cec5SDimitry Andric   return process_sp;
10760b57cec5SDimitry Andric }
10770b57cec5SDimitry Andric 
1078349cc55cSDimitry Andric std::vector<ArchSpec>
1079349cc55cSDimitry Andric Platform::CreateArchList(llvm::ArrayRef<llvm::Triple::ArchType> archs,
1080349cc55cSDimitry Andric                          llvm::Triple::OSType os) {
1081349cc55cSDimitry Andric   std::vector<ArchSpec> list;
1082349cc55cSDimitry Andric   for(auto arch : archs) {
1083349cc55cSDimitry Andric     llvm::Triple triple;
1084349cc55cSDimitry Andric     triple.setArch(arch);
1085349cc55cSDimitry Andric     triple.setOS(os);
1086349cc55cSDimitry Andric     list.push_back(ArchSpec(triple));
1087349cc55cSDimitry Andric   }
1088349cc55cSDimitry Andric   return list;
1089349cc55cSDimitry Andric }
1090349cc55cSDimitry Andric 
10910b57cec5SDimitry Andric /// Lets a platform answer if it is compatible with a given
10920b57cec5SDimitry Andric /// architecture and the target triple contained within.
10930b57cec5SDimitry Andric bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
109481ad6265SDimitry Andric                                         const ArchSpec &process_host_arch,
1095bdd1243dSDimitry Andric                                         ArchSpec::MatchType match,
10960b57cec5SDimitry Andric                                         ArchSpec *compatible_arch_ptr) {
10970b57cec5SDimitry Andric   // If the architecture is invalid, we must answer true...
10980b57cec5SDimitry Andric   if (arch.IsValid()) {
10990b57cec5SDimitry Andric     ArchSpec platform_arch;
110081ad6265SDimitry Andric     for (const ArchSpec &platform_arch :
110181ad6265SDimitry Andric          GetSupportedArchitectures(process_host_arch)) {
1102bdd1243dSDimitry Andric       if (arch.IsMatch(platform_arch, match)) {
11030b57cec5SDimitry Andric         if (compatible_arch_ptr)
11040b57cec5SDimitry Andric           *compatible_arch_ptr = platform_arch;
11050b57cec5SDimitry Andric         return true;
11060b57cec5SDimitry Andric       }
11070b57cec5SDimitry Andric     }
11080b57cec5SDimitry Andric   }
11090b57cec5SDimitry Andric   if (compatible_arch_ptr)
11100b57cec5SDimitry Andric     compatible_arch_ptr->Clear();
11110b57cec5SDimitry Andric   return false;
11120b57cec5SDimitry Andric }
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
11150b57cec5SDimitry Andric                          uint32_t uid, uint32_t gid) {
111681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
11179dba64beSDimitry Andric   LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
11180b57cec5SDimitry Andric 
11199dba64beSDimitry Andric   auto source_open_options =
1120349cc55cSDimitry Andric       File::eOpenOptionReadOnly | File::eOpenOptionCloseOnExec;
11210b57cec5SDimitry Andric   namespace fs = llvm::sys::fs;
11220b57cec5SDimitry Andric   if (fs::is_symlink_file(source.GetPath()))
11230b57cec5SDimitry Andric     source_open_options |= File::eOpenOptionDontFollowSymlinks;
11240b57cec5SDimitry Andric 
11259dba64beSDimitry Andric   auto source_file = FileSystem::Instance().Open(source, source_open_options,
11269dba64beSDimitry Andric                                                  lldb::eFilePermissionsUserRW);
11279dba64beSDimitry Andric   if (!source_file)
11289dba64beSDimitry Andric     return Status(source_file.takeError());
11299dba64beSDimitry Andric   Status error;
1130*0fca6ea1SDimitry Andric 
1131*0fca6ea1SDimitry Andric   bool requires_upload = true;
1132*0fca6ea1SDimitry Andric   llvm::ErrorOr<llvm::MD5::MD5Result> remote_md5 = CalculateMD5(destination);
1133*0fca6ea1SDimitry Andric   if (std::error_code ec = remote_md5.getError()) {
1134*0fca6ea1SDimitry Andric     LLDB_LOG(log, "[PutFile] couldn't get md5 sum of destination: {0}",
1135*0fca6ea1SDimitry Andric              ec.message());
1136*0fca6ea1SDimitry Andric   } else {
1137*0fca6ea1SDimitry Andric     llvm::ErrorOr<llvm::MD5::MD5Result> local_md5 =
1138*0fca6ea1SDimitry Andric         llvm::sys::fs::md5_contents(source.GetPath());
1139*0fca6ea1SDimitry Andric     if (std::error_code ec = local_md5.getError()) {
1140*0fca6ea1SDimitry Andric       LLDB_LOG(log, "[PutFile] couldn't get md5 sum of source: {0}",
1141*0fca6ea1SDimitry Andric                ec.message());
1142*0fca6ea1SDimitry Andric     } else {
1143*0fca6ea1SDimitry Andric       LLDB_LOGF(log, "[PutFile] destination md5: %016" PRIx64 "%016" PRIx64,
1144*0fca6ea1SDimitry Andric                 remote_md5->high(), remote_md5->low());
1145*0fca6ea1SDimitry Andric       LLDB_LOGF(log, "[PutFile]       local md5: %016" PRIx64 "%016" PRIx64,
1146*0fca6ea1SDimitry Andric                 local_md5->high(), local_md5->low());
1147*0fca6ea1SDimitry Andric       requires_upload = *remote_md5 != *local_md5;
1148*0fca6ea1SDimitry Andric     }
1149*0fca6ea1SDimitry Andric   }
1150*0fca6ea1SDimitry Andric 
1151*0fca6ea1SDimitry Andric   if (!requires_upload) {
1152*0fca6ea1SDimitry Andric     LLDB_LOGF(log, "[PutFile] skipping PutFile because md5sums match");
1153*0fca6ea1SDimitry Andric     return error;
1154*0fca6ea1SDimitry Andric   }
1155*0fca6ea1SDimitry Andric 
11569dba64beSDimitry Andric   uint32_t permissions = source_file.get()->GetPermissions(error);
11570b57cec5SDimitry Andric   if (permissions == 0)
1158*0fca6ea1SDimitry Andric     permissions = lldb::eFilePermissionsUserRWX;
11590b57cec5SDimitry Andric 
11600b57cec5SDimitry Andric   lldb::user_id_t dest_file = OpenFile(
1161349cc55cSDimitry Andric       destination, File::eOpenOptionCanCreate | File::eOpenOptionWriteOnly |
11620b57cec5SDimitry Andric                        File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
11630b57cec5SDimitry Andric       permissions, error);
11649dba64beSDimitry Andric   LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric   if (error.Fail())
11670b57cec5SDimitry Andric     return error;
11680b57cec5SDimitry Andric   if (dest_file == UINT64_MAX)
11690b57cec5SDimitry Andric     return Status("unable to open target file");
117081ad6265SDimitry Andric   lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
11710b57cec5SDimitry Andric   uint64_t offset = 0;
11720b57cec5SDimitry Andric   for (;;) {
11730b57cec5SDimitry Andric     size_t bytes_read = buffer_sp->GetByteSize();
11749dba64beSDimitry Andric     error = source_file.get()->Read(buffer_sp->GetBytes(), bytes_read);
11750b57cec5SDimitry Andric     if (error.Fail() || bytes_read == 0)
11760b57cec5SDimitry Andric       break;
11770b57cec5SDimitry Andric 
11780b57cec5SDimitry Andric     const uint64_t bytes_written =
11790b57cec5SDimitry Andric         WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
11800b57cec5SDimitry Andric     if (error.Fail())
11810b57cec5SDimitry Andric       break;
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric     offset += bytes_written;
11840b57cec5SDimitry Andric     if (bytes_written != bytes_read) {
11850b57cec5SDimitry Andric       // We didn't write the correct number of bytes, so adjust the file
11860b57cec5SDimitry Andric       // position in the source file we are reading from...
11879dba64beSDimitry Andric       source_file.get()->SeekFromStart(offset);
11880b57cec5SDimitry Andric     }
11890b57cec5SDimitry Andric   }
11900b57cec5SDimitry Andric   CloseFile(dest_file, error);
11910b57cec5SDimitry Andric 
11920b57cec5SDimitry Andric   if (uid == UINT32_MAX && gid == UINT32_MAX)
11930b57cec5SDimitry Andric     return error;
11940b57cec5SDimitry Andric 
11950b57cec5SDimitry Andric   // TODO: ChownFile?
11960b57cec5SDimitry Andric 
11970b57cec5SDimitry Andric   return error;
11980b57cec5SDimitry Andric }
11990b57cec5SDimitry Andric 
12000b57cec5SDimitry Andric Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
12010b57cec5SDimitry Andric   Status error("unimplemented");
12020b57cec5SDimitry Andric   return error;
12030b57cec5SDimitry Andric }
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric Status
12060b57cec5SDimitry Andric Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
12070b57cec5SDimitry Andric                         const FileSpec &dst) // The symlink points to dst
12080b57cec5SDimitry Andric {
120981ad6265SDimitry Andric   if (IsHost())
121081ad6265SDimitry Andric     return FileSystem::Instance().Symlink(src, dst);
121181ad6265SDimitry Andric   return Status("unimplemented");
12120b57cec5SDimitry Andric }
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
121581ad6265SDimitry Andric   if (IsHost())
121681ad6265SDimitry Andric     return FileSystem::Instance().Exists(file_spec);
12170b57cec5SDimitry Andric   return false;
12180b57cec5SDimitry Andric }
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric Status Platform::Unlink(const FileSpec &path) {
122181ad6265SDimitry Andric   if (IsHost())
122281ad6265SDimitry Andric     return llvm::sys::fs::remove(path.GetPath());
122381ad6265SDimitry Andric   return Status("unimplemented");
12240b57cec5SDimitry Andric }
12250b57cec5SDimitry Andric 
12260b57cec5SDimitry Andric MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
12270b57cec5SDimitry Andric                                           addr_t length, unsigned prot,
12280b57cec5SDimitry Andric                                           unsigned flags, addr_t fd,
12290b57cec5SDimitry Andric                                           addr_t offset) {
12300b57cec5SDimitry Andric   uint64_t flags_platform = 0;
12310b57cec5SDimitry Andric   if (flags & eMmapFlagsPrivate)
12320b57cec5SDimitry Andric     flags_platform |= MAP_PRIVATE;
12330b57cec5SDimitry Andric   if (flags & eMmapFlagsAnon)
12340b57cec5SDimitry Andric     flags_platform |= MAP_ANON;
12350b57cec5SDimitry Andric 
12360b57cec5SDimitry Andric   MmapArgList args({addr, length, prot, flags_platform, fd, offset});
12370b57cec5SDimitry Andric   return args;
12380b57cec5SDimitry Andric }
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric lldb_private::Status Platform::RunShellCommand(
1241e8d8bef9SDimitry Andric     llvm::StringRef command,
1242e8d8bef9SDimitry Andric     const FileSpec &
1243e8d8bef9SDimitry Andric         working_dir, // Pass empty FileSpec to use the current working directory
1244e8d8bef9SDimitry Andric     int *status_ptr, // Pass nullptr if you don't want the process exit status
1245e8d8bef9SDimitry Andric     int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1246e8d8bef9SDimitry Andric                     // process to exit
1247e8d8bef9SDimitry Andric     std::string
1248e8d8bef9SDimitry Andric         *command_output, // Pass nullptr if you don't want the command output
1249e8d8bef9SDimitry Andric     const Timeout<std::micro> &timeout) {
1250e8d8bef9SDimitry Andric   return RunShellCommand(llvm::StringRef(), command, working_dir, status_ptr,
1251e8d8bef9SDimitry Andric                          signo_ptr, command_output, timeout);
1252e8d8bef9SDimitry Andric }
1253e8d8bef9SDimitry Andric 
1254e8d8bef9SDimitry Andric lldb_private::Status Platform::RunShellCommand(
1255e8d8bef9SDimitry Andric     llvm::StringRef shell,   // Pass empty if you want to use the default
1256e8d8bef9SDimitry Andric                              // shell interpreter
1257e8d8bef9SDimitry Andric     llvm::StringRef command, // Shouldn't be empty
12580b57cec5SDimitry Andric     const FileSpec &
12590b57cec5SDimitry Andric         working_dir, // Pass empty FileSpec to use the current working directory
12600b57cec5SDimitry Andric     int *status_ptr, // Pass nullptr if you don't want the process exit status
12610b57cec5SDimitry Andric     int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
12620b57cec5SDimitry Andric                     // process to exit
12630b57cec5SDimitry Andric     std::string
12640b57cec5SDimitry Andric         *command_output, // Pass nullptr if you don't want the command output
12650b57cec5SDimitry Andric     const Timeout<std::micro> &timeout) {
12660b57cec5SDimitry Andric   if (IsHost())
1267e8d8bef9SDimitry Andric     return Host::RunShellCommand(shell, command, working_dir, status_ptr,
1268e8d8bef9SDimitry Andric                                  signo_ptr, command_output, timeout);
126981ad6265SDimitry Andric   return Status("unable to run a remote command without a platform");
12700b57cec5SDimitry Andric }
12710b57cec5SDimitry Andric 
1272*0fca6ea1SDimitry Andric llvm::ErrorOr<llvm::MD5::MD5Result>
1273*0fca6ea1SDimitry Andric Platform::CalculateMD5(const FileSpec &file_spec) {
12740b57cec5SDimitry Andric   if (!IsHost())
1275*0fca6ea1SDimitry Andric     return std::make_error_code(std::errc::not_supported);
1276*0fca6ea1SDimitry Andric   return llvm::sys::fs::md5_contents(file_spec.GetPath());
12770b57cec5SDimitry Andric }
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric void Platform::SetLocalCacheDirectory(const char *local) {
12800b57cec5SDimitry Andric   m_local_cache_directory.assign(local);
12810b57cec5SDimitry Andric }
12820b57cec5SDimitry Andric 
12830b57cec5SDimitry Andric const char *Platform::GetLocalCacheDirectory() {
12840b57cec5SDimitry Andric   return m_local_cache_directory.c_str();
12850b57cec5SDimitry Andric }
12860b57cec5SDimitry Andric 
12870b57cec5SDimitry Andric static constexpr OptionDefinition g_rsync_option_table[] = {
12880b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
12890b57cec5SDimitry Andric      {}, 0, eArgTypeNone, "Enable rsync."},
12900b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
12910b57cec5SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
12920b57cec5SDimitry Andric      "Platform-specific options required for rsync to work."},
12930b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
12940b57cec5SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
12950b57cec5SDimitry Andric      "Platform-specific rsync prefix put before the remote path."},
12960b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
12970b57cec5SDimitry Andric      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
12980b57cec5SDimitry Andric      "Do not automatically fill in the remote hostname when composing the "
12990b57cec5SDimitry Andric      "rsync command."},
13000b57cec5SDimitry Andric };
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric static constexpr OptionDefinition g_ssh_option_table[] = {
13030b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
13040b57cec5SDimitry Andric      {}, 0, eArgTypeNone, "Enable SSH."},
13050b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
13060b57cec5SDimitry Andric      nullptr, {}, 0, eArgTypeCommandName,
13070b57cec5SDimitry Andric      "Platform-specific options required for SSH to work."},
13080b57cec5SDimitry Andric };
13090b57cec5SDimitry Andric 
13100b57cec5SDimitry Andric static constexpr OptionDefinition g_caching_option_table[] = {
13110b57cec5SDimitry Andric     {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
13120b57cec5SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
13130b57cec5SDimitry Andric      "Path in which to store local copies of files."},
13140b57cec5SDimitry Andric };
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
1317bdd1243dSDimitry Andric   return llvm::ArrayRef(g_rsync_option_table);
13180b57cec5SDimitry Andric }
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric void OptionGroupPlatformRSync::OptionParsingStarting(
13210b57cec5SDimitry Andric     ExecutionContext *execution_context) {
13220b57cec5SDimitry Andric   m_rsync = false;
13230b57cec5SDimitry Andric   m_rsync_opts.clear();
13240b57cec5SDimitry Andric   m_rsync_prefix.clear();
13250b57cec5SDimitry Andric   m_ignores_remote_hostname = false;
13260b57cec5SDimitry Andric }
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric lldb_private::Status
13290b57cec5SDimitry Andric OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
13300b57cec5SDimitry Andric                                          llvm::StringRef option_arg,
13310b57cec5SDimitry Andric                                          ExecutionContext *execution_context) {
13320b57cec5SDimitry Andric   Status error;
13330b57cec5SDimitry Andric   char short_option = (char)GetDefinitions()[option_idx].short_option;
13340b57cec5SDimitry Andric   switch (short_option) {
13350b57cec5SDimitry Andric   case 'r':
13360b57cec5SDimitry Andric     m_rsync = true;
13370b57cec5SDimitry Andric     break;
13380b57cec5SDimitry Andric 
13390b57cec5SDimitry Andric   case 'R':
13405ffd83dbSDimitry Andric     m_rsync_opts.assign(std::string(option_arg));
13410b57cec5SDimitry Andric     break;
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric   case 'P':
13445ffd83dbSDimitry Andric     m_rsync_prefix.assign(std::string(option_arg));
13450b57cec5SDimitry Andric     break;
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric   case 'i':
13480b57cec5SDimitry Andric     m_ignores_remote_hostname = true;
13490b57cec5SDimitry Andric     break;
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric   default:
13520b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
13530b57cec5SDimitry Andric     break;
13540b57cec5SDimitry Andric   }
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric   return error;
13570b57cec5SDimitry Andric }
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric lldb::BreakpointSP
13600b57cec5SDimitry Andric Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
13610b57cec5SDimitry Andric   return lldb::BreakpointSP();
13620b57cec5SDimitry Andric }
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
1365bdd1243dSDimitry Andric   return llvm::ArrayRef(g_ssh_option_table);
13660b57cec5SDimitry Andric }
13670b57cec5SDimitry Andric 
13680b57cec5SDimitry Andric void OptionGroupPlatformSSH::OptionParsingStarting(
13690b57cec5SDimitry Andric     ExecutionContext *execution_context) {
13700b57cec5SDimitry Andric   m_ssh = false;
13710b57cec5SDimitry Andric   m_ssh_opts.clear();
13720b57cec5SDimitry Andric }
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric lldb_private::Status
13750b57cec5SDimitry Andric OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
13760b57cec5SDimitry Andric                                        llvm::StringRef option_arg,
13770b57cec5SDimitry Andric                                        ExecutionContext *execution_context) {
13780b57cec5SDimitry Andric   Status error;
13790b57cec5SDimitry Andric   char short_option = (char)GetDefinitions()[option_idx].short_option;
13800b57cec5SDimitry Andric   switch (short_option) {
13810b57cec5SDimitry Andric   case 's':
13820b57cec5SDimitry Andric     m_ssh = true;
13830b57cec5SDimitry Andric     break;
13840b57cec5SDimitry Andric 
13850b57cec5SDimitry Andric   case 'S':
13865ffd83dbSDimitry Andric     m_ssh_opts.assign(std::string(option_arg));
13870b57cec5SDimitry Andric     break;
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric   default:
13900b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
13910b57cec5SDimitry Andric     break;
13920b57cec5SDimitry Andric   }
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric   return error;
13950b57cec5SDimitry Andric }
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
1398bdd1243dSDimitry Andric   return llvm::ArrayRef(g_caching_option_table);
13990b57cec5SDimitry Andric }
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric void OptionGroupPlatformCaching::OptionParsingStarting(
14020b57cec5SDimitry Andric     ExecutionContext *execution_context) {
14030b57cec5SDimitry Andric   m_cache_dir.clear();
14040b57cec5SDimitry Andric }
14050b57cec5SDimitry Andric 
14060b57cec5SDimitry Andric lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
14070b57cec5SDimitry Andric     uint32_t option_idx, llvm::StringRef option_arg,
14080b57cec5SDimitry Andric     ExecutionContext *execution_context) {
14090b57cec5SDimitry Andric   Status error;
14100b57cec5SDimitry Andric   char short_option = (char)GetDefinitions()[option_idx].short_option;
14110b57cec5SDimitry Andric   switch (short_option) {
14120b57cec5SDimitry Andric   case 'c':
14135ffd83dbSDimitry Andric     m_cache_dir.assign(std::string(option_arg));
14140b57cec5SDimitry Andric     break;
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric   default:
14170b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
14180b57cec5SDimitry Andric     break;
14190b57cec5SDimitry Andric   }
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric   return error;
14220b57cec5SDimitry Andric }
14230b57cec5SDimitry Andric 
142481ad6265SDimitry Andric Environment Platform::GetEnvironment() {
142581ad6265SDimitry Andric   if (IsHost())
142681ad6265SDimitry Andric     return Host::GetEnvironment();
142781ad6265SDimitry Andric   return Environment();
142881ad6265SDimitry Andric }
14290b57cec5SDimitry Andric 
14300b57cec5SDimitry Andric const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
14310b57cec5SDimitry Andric   if (!m_calculated_trap_handlers) {
14320b57cec5SDimitry Andric     std::lock_guard<std::mutex> guard(m_mutex);
14330b57cec5SDimitry Andric     if (!m_calculated_trap_handlers) {
14340b57cec5SDimitry Andric       CalculateTrapHandlerSymbolNames();
14350b57cec5SDimitry Andric       m_calculated_trap_handlers = true;
14360b57cec5SDimitry Andric     }
14370b57cec5SDimitry Andric   }
14380b57cec5SDimitry Andric   return m_trap_handlers;
14390b57cec5SDimitry Andric }
14400b57cec5SDimitry Andric 
1441349cc55cSDimitry Andric Status
1442349cc55cSDimitry Andric Platform::GetCachedExecutable(ModuleSpec &module_spec,
1443349cc55cSDimitry Andric                               lldb::ModuleSP &module_sp,
1444349cc55cSDimitry Andric                               const FileSpecList *module_search_paths_ptr) {
14454824e7fdSDimitry Andric   FileSpec platform_spec = module_spec.GetFileSpec();
14464824e7fdSDimitry Andric   Status error = GetRemoteSharedModule(
1447349cc55cSDimitry Andric       module_spec, nullptr, module_sp,
14480b57cec5SDimitry Andric       [&](const ModuleSpec &spec) {
1449*0fca6ea1SDimitry Andric         return Platform::ResolveExecutable(spec, module_sp,
1450349cc55cSDimitry Andric                                            module_search_paths_ptr);
14510b57cec5SDimitry Andric       },
14520b57cec5SDimitry Andric       nullptr);
14534824e7fdSDimitry Andric   if (error.Success()) {
14544824e7fdSDimitry Andric     module_spec.GetFileSpec() = module_sp->GetFileSpec();
14554824e7fdSDimitry Andric     module_spec.GetPlatformFileSpec() = platform_spec;
14564824e7fdSDimitry Andric   }
14574824e7fdSDimitry Andric 
14584824e7fdSDimitry Andric   return error;
14590b57cec5SDimitry Andric }
14600b57cec5SDimitry Andric 
14610b57cec5SDimitry Andric Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
14620b57cec5SDimitry Andric                                        Process *process,
14630b57cec5SDimitry Andric                                        lldb::ModuleSP &module_sp,
14640b57cec5SDimitry Andric                                        const ModuleResolver &module_resolver,
14650b57cec5SDimitry Andric                                        bool *did_create_ptr) {
14660b57cec5SDimitry Andric   // Get module information from a target.
14670b57cec5SDimitry Andric   ModuleSpec resolved_module_spec;
146881ad6265SDimitry Andric   ArchSpec process_host_arch;
14690b57cec5SDimitry Andric   bool got_module_spec = false;
14700b57cec5SDimitry Andric   if (process) {
147181ad6265SDimitry Andric     process_host_arch = process->GetSystemArchitecture();
14720b57cec5SDimitry Andric     // Try to get module information from the process
14730b57cec5SDimitry Andric     if (process->GetModuleSpec(module_spec.GetFileSpec(),
14740b57cec5SDimitry Andric                                module_spec.GetArchitecture(),
14750b57cec5SDimitry Andric                                resolved_module_spec)) {
14760b57cec5SDimitry Andric       if (!module_spec.GetUUID().IsValid() ||
14770b57cec5SDimitry Andric           module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
14780b57cec5SDimitry Andric         got_module_spec = true;
14790b57cec5SDimitry Andric       }
14800b57cec5SDimitry Andric     }
14810b57cec5SDimitry Andric   }
14820b57cec5SDimitry Andric 
14830b57cec5SDimitry Andric   if (!module_spec.GetArchitecture().IsValid()) {
14840b57cec5SDimitry Andric     Status error;
14850b57cec5SDimitry Andric     // No valid architecture was specified, ask the platform for the
14860b57cec5SDimitry Andric     // architectures that we should be using (in the correct order) and see if
14870b57cec5SDimitry Andric     // we can find a match that way
14880b57cec5SDimitry Andric     ModuleSpec arch_module_spec(module_spec);
148981ad6265SDimitry Andric     for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
1490349cc55cSDimitry Andric       arch_module_spec.GetArchitecture() = arch;
14910b57cec5SDimitry Andric       error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
14920b57cec5SDimitry Andric                                           nullptr, nullptr);
14930b57cec5SDimitry Andric       // Did we find an executable using one of the
14940b57cec5SDimitry Andric       if (error.Success() && module_sp)
14950b57cec5SDimitry Andric         break;
14960b57cec5SDimitry Andric     }
1497eaeb601bSDimitry Andric     if (module_sp) {
1498eaeb601bSDimitry Andric       resolved_module_spec = arch_module_spec;
14990b57cec5SDimitry Andric       got_module_spec = true;
15000b57cec5SDimitry Andric     }
1501eaeb601bSDimitry Andric   }
15020b57cec5SDimitry Andric 
15030b57cec5SDimitry Andric   if (!got_module_spec) {
15040b57cec5SDimitry Andric     // Get module information from a target.
1505eaeb601bSDimitry Andric     if (GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
15060b57cec5SDimitry Andric                       resolved_module_spec)) {
15070b57cec5SDimitry Andric       if (!module_spec.GetUUID().IsValid() ||
15080b57cec5SDimitry Andric           module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1509eaeb601bSDimitry Andric         got_module_spec = true;
1510eaeb601bSDimitry Andric       }
1511eaeb601bSDimitry Andric     }
1512eaeb601bSDimitry Andric   }
1513eaeb601bSDimitry Andric 
1514eaeb601bSDimitry Andric   if (!got_module_spec) {
1515eaeb601bSDimitry Andric     // Fall back to the given module resolver, which may have its own
1516eaeb601bSDimitry Andric     // search logic.
15170b57cec5SDimitry Andric     return module_resolver(module_spec);
15180b57cec5SDimitry Andric   }
15190b57cec5SDimitry Andric 
15200b57cec5SDimitry Andric   // If we are looking for a specific UUID, make sure resolved_module_spec has
15210b57cec5SDimitry Andric   // the same one before we search.
15220b57cec5SDimitry Andric   if (module_spec.GetUUID().IsValid()) {
15230b57cec5SDimitry Andric     resolved_module_spec.GetUUID() = module_spec.GetUUID();
15240b57cec5SDimitry Andric   }
15250b57cec5SDimitry Andric 
15265f757f3fSDimitry Andric   // Call locate module callback if set. This allows users to implement their
15275f757f3fSDimitry Andric   // own module cache system. For example, to leverage build system artifacts,
15285f757f3fSDimitry Andric   // to bypass pulling files from remote platform, or to search symbol files
15295f757f3fSDimitry Andric   // from symbol servers.
15305f757f3fSDimitry Andric   FileSpec symbol_file_spec;
15315f757f3fSDimitry Andric   CallLocateModuleCallbackIfSet(resolved_module_spec, module_sp,
15325f757f3fSDimitry Andric                                 symbol_file_spec, did_create_ptr);
15335f757f3fSDimitry Andric   if (module_sp) {
15345f757f3fSDimitry Andric     // The module is loaded.
15355f757f3fSDimitry Andric     if (symbol_file_spec) {
15365f757f3fSDimitry Andric       // 1. module_sp:loaded, symbol_file_spec:set
15375f757f3fSDimitry Andric       //      The callback found a module file and a symbol file for this
15385f757f3fSDimitry Andric       //      resolved_module_spec. Set the symbol file to the module.
15395f757f3fSDimitry Andric       module_sp->SetSymbolFileFileSpec(symbol_file_spec);
15405f757f3fSDimitry Andric     } else {
15415f757f3fSDimitry Andric       // 2. module_sp:loaded, symbol_file_spec:empty
15425f757f3fSDimitry Andric       //      The callback only found a module file for this
15435f757f3fSDimitry Andric       //      resolved_module_spec.
15445f757f3fSDimitry Andric     }
15450b57cec5SDimitry Andric     return Status();
15460b57cec5SDimitry Andric   }
15470b57cec5SDimitry Andric 
15485f757f3fSDimitry Andric   // The module is not loaded by CallLocateModuleCallbackIfSet.
15495f757f3fSDimitry Andric   // 3. module_sp:empty, symbol_file_spec:set
15505f757f3fSDimitry Andric   //      The callback only found a symbol file for the module. We continue to
15515f757f3fSDimitry Andric   //      find a module file for this resolved_module_spec. and we will call
15525f757f3fSDimitry Andric   //      module_sp->SetSymbolFileFileSpec with the symbol_file_spec later.
15535f757f3fSDimitry Andric   // 4. module_sp:empty, symbol_file_spec:empty
15545f757f3fSDimitry Andric   //      The callback is not set. Or the callback did not find any module
15555f757f3fSDimitry Andric   //      files nor any symbol files. Or the callback failed, or something
15565f757f3fSDimitry Andric   //      went wrong. We continue to find a module file for this
15575f757f3fSDimitry Andric   //      resolved_module_spec.
15585f757f3fSDimitry Andric 
15595f757f3fSDimitry Andric   // Trying to find a module by UUID on local file system.
15605f757f3fSDimitry Andric   const Status error = module_resolver(resolved_module_spec);
15615f757f3fSDimitry Andric   if (error.Success()) {
15625f757f3fSDimitry Andric     if (module_sp && symbol_file_spec) {
15635f757f3fSDimitry Andric       // Set the symbol file to the module if the locate modudle callback was
15645f757f3fSDimitry Andric       // called and returned only a symbol file.
15655f757f3fSDimitry Andric       module_sp->SetSymbolFileFileSpec(symbol_file_spec);
15665f757f3fSDimitry Andric     }
15670b57cec5SDimitry Andric     return error;
15680b57cec5SDimitry Andric   }
15690b57cec5SDimitry Andric 
15705f757f3fSDimitry Andric   // Fallback to call GetCachedSharedModule on failure.
15715f757f3fSDimitry Andric   if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr)) {
15725f757f3fSDimitry Andric     if (module_sp && symbol_file_spec) {
15735f757f3fSDimitry Andric       // Set the symbol file to the module if the locate modudle callback was
15745f757f3fSDimitry Andric       // called and returned only a symbol file.
15755f757f3fSDimitry Andric       module_sp->SetSymbolFileFileSpec(symbol_file_spec);
15765f757f3fSDimitry Andric     }
15775f757f3fSDimitry Andric     return Status();
15785f757f3fSDimitry Andric   }
15795f757f3fSDimitry Andric 
15805f757f3fSDimitry Andric   return Status("Failed to call GetCachedSharedModule");
15815f757f3fSDimitry Andric }
15825f757f3fSDimitry Andric 
15835f757f3fSDimitry Andric void Platform::CallLocateModuleCallbackIfSet(const ModuleSpec &module_spec,
15845f757f3fSDimitry Andric                                              lldb::ModuleSP &module_sp,
15855f757f3fSDimitry Andric                                              FileSpec &symbol_file_spec,
15865f757f3fSDimitry Andric                                              bool *did_create_ptr) {
15875f757f3fSDimitry Andric   if (!m_locate_module_callback) {
15885f757f3fSDimitry Andric     // Locate module callback is not set.
15895f757f3fSDimitry Andric     return;
15905f757f3fSDimitry Andric   }
15915f757f3fSDimitry Andric 
15925f757f3fSDimitry Andric   FileSpec module_file_spec;
15935f757f3fSDimitry Andric   Status error =
15945f757f3fSDimitry Andric       m_locate_module_callback(module_spec, module_file_spec, symbol_file_spec);
15955f757f3fSDimitry Andric 
15965f757f3fSDimitry Andric   // Locate module callback is set and called. Check the error.
15975f757f3fSDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
15985f757f3fSDimitry Andric   if (error.Fail()) {
15995f757f3fSDimitry Andric     LLDB_LOGF(log, "%s: locate module callback failed: %s",
16005f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, error.AsCString());
16015f757f3fSDimitry Andric     return;
16025f757f3fSDimitry Andric   }
16035f757f3fSDimitry Andric 
16045f757f3fSDimitry Andric   // The locate module callback was succeeded.
16055f757f3fSDimitry Andric   // Check the module_file_spec and symbol_file_spec values.
16065f757f3fSDimitry Andric   // 1. module:empty  symbol:empty  -> Failure
16075f757f3fSDimitry Andric   //    - The callback did not return any files.
16085f757f3fSDimitry Andric   // 2. module:exists symbol:exists -> Success
16095f757f3fSDimitry Andric   //    - The callback returned a module file and a symbol file.
16105f757f3fSDimitry Andric   // 3. module:exists symbol:empty  -> Success
16115f757f3fSDimitry Andric   //    - The callback returned only a module file.
16125f757f3fSDimitry Andric   // 4. module:empty  symbol:exists -> Success
16135f757f3fSDimitry Andric   //    - The callback returned only a symbol file.
16145f757f3fSDimitry Andric   //      For example, a breakpad symbol text file.
16155f757f3fSDimitry Andric   if (!module_file_spec && !symbol_file_spec) {
16165f757f3fSDimitry Andric     // This is '1. module:empty  symbol:empty  -> Failure'
16175f757f3fSDimitry Andric     // The callback did not return any files.
16185f757f3fSDimitry Andric     LLDB_LOGF(log,
16195f757f3fSDimitry Andric               "%s: locate module callback did not set both "
16205f757f3fSDimitry Andric               "module_file_spec and symbol_file_spec",
16215f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION);
16225f757f3fSDimitry Andric     return;
16235f757f3fSDimitry Andric   }
16245f757f3fSDimitry Andric 
16255f757f3fSDimitry Andric   // If the callback returned a module file, it should exist.
16265f757f3fSDimitry Andric   if (module_file_spec && !FileSystem::Instance().Exists(module_file_spec)) {
16275f757f3fSDimitry Andric     LLDB_LOGF(log,
16285f757f3fSDimitry Andric               "%s: locate module callback set a non-existent file to "
16295f757f3fSDimitry Andric               "module_file_spec: %s",
16305f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str());
16315f757f3fSDimitry Andric     // Clear symbol_file_spec for the error.
16325f757f3fSDimitry Andric     symbol_file_spec.Clear();
16335f757f3fSDimitry Andric     return;
16345f757f3fSDimitry Andric   }
16355f757f3fSDimitry Andric 
16365f757f3fSDimitry Andric   // If the callback returned a symbol file, it should exist.
16375f757f3fSDimitry Andric   if (symbol_file_spec && !FileSystem::Instance().Exists(symbol_file_spec)) {
16385f757f3fSDimitry Andric     LLDB_LOGF(log,
16395f757f3fSDimitry Andric               "%s: locate module callback set a non-existent file to "
16405f757f3fSDimitry Andric               "symbol_file_spec: %s",
16415f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, symbol_file_spec.GetPath().c_str());
16425f757f3fSDimitry Andric     // Clear symbol_file_spec for the error.
16435f757f3fSDimitry Andric     symbol_file_spec.Clear();
16445f757f3fSDimitry Andric     return;
16455f757f3fSDimitry Andric   }
16465f757f3fSDimitry Andric 
16475f757f3fSDimitry Andric   if (!module_file_spec && symbol_file_spec) {
16485f757f3fSDimitry Andric     // This is '4. module:empty  symbol:exists -> Success'
16495f757f3fSDimitry Andric     // The locate module callback returned only a symbol file. For example,
16505f757f3fSDimitry Andric     // a breakpad symbol text file. GetRemoteSharedModule will use this returned
16515f757f3fSDimitry Andric     // symbol_file_spec.
16525f757f3fSDimitry Andric     LLDB_LOGF(log, "%s: locate module callback succeeded: symbol=%s",
16535f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, symbol_file_spec.GetPath().c_str());
16545f757f3fSDimitry Andric     return;
16555f757f3fSDimitry Andric   }
16565f757f3fSDimitry Andric 
16575f757f3fSDimitry Andric   // This is one of the following.
16585f757f3fSDimitry Andric   // - 2. module:exists symbol:exists -> Success
16595f757f3fSDimitry Andric   //    - The callback returned a module file and a symbol file.
16605f757f3fSDimitry Andric   // - 3. module:exists symbol:empty  -> Success
16615f757f3fSDimitry Andric   //    - The callback returned Only a module file.
16625f757f3fSDimitry Andric   // Load the module file.
16635f757f3fSDimitry Andric   auto cached_module_spec(module_spec);
16645f757f3fSDimitry Andric   cached_module_spec.GetUUID().Clear(); // Clear UUID since it may contain md5
16655f757f3fSDimitry Andric                                         // content hash instead of real UUID.
16665f757f3fSDimitry Andric   cached_module_spec.GetFileSpec() = module_file_spec;
16675f757f3fSDimitry Andric   cached_module_spec.GetPlatformFileSpec() = module_spec.GetFileSpec();
16685f757f3fSDimitry Andric   cached_module_spec.SetObjectOffset(0);
16695f757f3fSDimitry Andric 
16705f757f3fSDimitry Andric   error = ModuleList::GetSharedModule(cached_module_spec, module_sp, nullptr,
16715f757f3fSDimitry Andric                                       nullptr, did_create_ptr, false);
16725f757f3fSDimitry Andric   if (error.Success() && module_sp) {
16735f757f3fSDimitry Andric     // Succeeded to load the module file.
16745f757f3fSDimitry Andric     LLDB_LOGF(log, "%s: locate module callback succeeded: module=%s symbol=%s",
16755f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str(),
16765f757f3fSDimitry Andric               symbol_file_spec.GetPath().c_str());
16775f757f3fSDimitry Andric   } else {
16785f757f3fSDimitry Andric     LLDB_LOGF(log,
16795f757f3fSDimitry Andric               "%s: locate module callback succeeded but failed to load: "
16805f757f3fSDimitry Andric               "module=%s symbol=%s",
16815f757f3fSDimitry Andric               LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str(),
16825f757f3fSDimitry Andric               symbol_file_spec.GetPath().c_str());
16835f757f3fSDimitry Andric     // Clear module_sp and symbol_file_spec for the error.
16845f757f3fSDimitry Andric     module_sp.reset();
16855f757f3fSDimitry Andric     symbol_file_spec.Clear();
16865f757f3fSDimitry Andric   }
16875f757f3fSDimitry Andric }
16885f757f3fSDimitry Andric 
16890b57cec5SDimitry Andric bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
16900b57cec5SDimitry Andric                                      lldb::ModuleSP &module_sp,
16910b57cec5SDimitry Andric                                      bool *did_create_ptr) {
1692349cc55cSDimitry Andric   if (IsHost() || !GetGlobalPlatformProperties().GetUseModuleCache() ||
1693349cc55cSDimitry Andric       !GetGlobalPlatformProperties().GetModuleCacheDirectory())
16940b57cec5SDimitry Andric     return false;
16950b57cec5SDimitry Andric 
169681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Platform);
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric   // Check local cache for a module.
16990b57cec5SDimitry Andric   auto error = m_module_cache->GetAndPut(
17000b57cec5SDimitry Andric       GetModuleCacheRoot(), GetCacheHostname(), module_spec,
17010b57cec5SDimitry Andric       [this](const ModuleSpec &module_spec,
17020b57cec5SDimitry Andric              const FileSpec &tmp_download_file_spec) {
17030b57cec5SDimitry Andric         return DownloadModuleSlice(
17040b57cec5SDimitry Andric             module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
17050b57cec5SDimitry Andric             module_spec.GetObjectSize(), tmp_download_file_spec);
17060b57cec5SDimitry Andric 
17070b57cec5SDimitry Andric       },
17080b57cec5SDimitry Andric       [this](const ModuleSP &module_sp,
17090b57cec5SDimitry Andric              const FileSpec &tmp_download_file_spec) {
17100b57cec5SDimitry Andric         return DownloadSymbolFile(module_sp, tmp_download_file_spec);
17110b57cec5SDimitry Andric       },
17120b57cec5SDimitry Andric       module_sp, did_create_ptr);
17130b57cec5SDimitry Andric   if (error.Success())
17140b57cec5SDimitry Andric     return true;
17150b57cec5SDimitry Andric 
17169dba64beSDimitry Andric   LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s",
17170b57cec5SDimitry Andric             __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
17180b57cec5SDimitry Andric             error.AsCString());
17190b57cec5SDimitry Andric   return false;
17200b57cec5SDimitry Andric }
17210b57cec5SDimitry Andric 
17220b57cec5SDimitry Andric Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
17230b57cec5SDimitry Andric                                      const uint64_t src_offset,
17240b57cec5SDimitry Andric                                      const uint64_t src_size,
17250b57cec5SDimitry Andric                                      const FileSpec &dst_file_spec) {
17260b57cec5SDimitry Andric   Status error;
17270b57cec5SDimitry Andric 
17280b57cec5SDimitry Andric   std::error_code EC;
17299dba64beSDimitry Andric   llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::OF_None);
17300b57cec5SDimitry Andric   if (EC) {
17310b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unable to open destination file: %s",
17320b57cec5SDimitry Andric                                    dst_file_spec.GetPath().c_str());
17330b57cec5SDimitry Andric     return error;
17340b57cec5SDimitry Andric   }
17350b57cec5SDimitry Andric 
1736349cc55cSDimitry Andric   auto src_fd = OpenFile(src_file_spec, File::eOpenOptionReadOnly,
17370b57cec5SDimitry Andric                          lldb::eFilePermissionsFileDefault, error);
17380b57cec5SDimitry Andric 
17390b57cec5SDimitry Andric   if (error.Fail()) {
17400b57cec5SDimitry Andric     error.SetErrorStringWithFormat("unable to open source file: %s",
17410b57cec5SDimitry Andric                                    error.AsCString());
17420b57cec5SDimitry Andric     return error;
17430b57cec5SDimitry Andric   }
17440b57cec5SDimitry Andric 
174506c3fb27SDimitry Andric   std::vector<char> buffer(512 * 1024);
17460b57cec5SDimitry Andric   auto offset = src_offset;
17470b57cec5SDimitry Andric   uint64_t total_bytes_read = 0;
17480b57cec5SDimitry Andric   while (total_bytes_read < src_size) {
17490b57cec5SDimitry Andric     const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
17500b57cec5SDimitry Andric                                   src_size - total_bytes_read);
17510b57cec5SDimitry Andric     const uint64_t n_read =
17520b57cec5SDimitry Andric         ReadFile(src_fd, offset, &buffer[0], to_read, error);
17530b57cec5SDimitry Andric     if (error.Fail())
17540b57cec5SDimitry Andric       break;
17550b57cec5SDimitry Andric     if (n_read == 0) {
17560b57cec5SDimitry Andric       error.SetErrorString("read 0 bytes");
17570b57cec5SDimitry Andric       break;
17580b57cec5SDimitry Andric     }
17590b57cec5SDimitry Andric     offset += n_read;
17600b57cec5SDimitry Andric     total_bytes_read += n_read;
17610b57cec5SDimitry Andric     dst.write(&buffer[0], n_read);
17620b57cec5SDimitry Andric   }
17630b57cec5SDimitry Andric 
17640b57cec5SDimitry Andric   Status close_error;
17650b57cec5SDimitry Andric   CloseFile(src_fd, close_error); // Ignoring close error.
17660b57cec5SDimitry Andric 
17670b57cec5SDimitry Andric   return error;
17680b57cec5SDimitry Andric }
17690b57cec5SDimitry Andric 
17700b57cec5SDimitry Andric Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
17710b57cec5SDimitry Andric                                     const FileSpec &dst_file_spec) {
17720b57cec5SDimitry Andric   return Status(
17730b57cec5SDimitry Andric       "Symbol file downloading not supported by the default platform.");
17740b57cec5SDimitry Andric }
17750b57cec5SDimitry Andric 
17760b57cec5SDimitry Andric FileSpec Platform::GetModuleCacheRoot() {
1777349cc55cSDimitry Andric   auto dir_spec = GetGlobalPlatformProperties().GetModuleCacheDirectory();
177881ad6265SDimitry Andric   dir_spec.AppendPathComponent(GetPluginName());
17790b57cec5SDimitry Andric   return dir_spec;
17800b57cec5SDimitry Andric }
17810b57cec5SDimitry Andric 
17820b57cec5SDimitry Andric const char *Platform::GetCacheHostname() { return GetHostname(); }
17830b57cec5SDimitry Andric 
17840b57cec5SDimitry Andric const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
17850b57cec5SDimitry Andric   static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
17860b57cec5SDimitry Andric   return s_default_unix_signals_sp;
17870b57cec5SDimitry Andric }
17880b57cec5SDimitry Andric 
17890b57cec5SDimitry Andric UnixSignalsSP Platform::GetUnixSignals() {
17900b57cec5SDimitry Andric   if (IsHost())
17910b57cec5SDimitry Andric     return UnixSignals::CreateForHost();
17920b57cec5SDimitry Andric   return GetRemoteUnixSignals();
17930b57cec5SDimitry Andric }
17940b57cec5SDimitry Andric 
17950b57cec5SDimitry Andric uint32_t Platform::LoadImage(lldb_private::Process *process,
17960b57cec5SDimitry Andric                              const lldb_private::FileSpec &local_file,
17970b57cec5SDimitry Andric                              const lldb_private::FileSpec &remote_file,
17980b57cec5SDimitry Andric                              lldb_private::Status &error) {
17990b57cec5SDimitry Andric   if (local_file && remote_file) {
18000b57cec5SDimitry Andric     // Both local and remote file was specified. Install the local file to the
18010b57cec5SDimitry Andric     // given location.
18020b57cec5SDimitry Andric     if (IsRemote() || local_file != remote_file) {
18030b57cec5SDimitry Andric       error = Install(local_file, remote_file);
18040b57cec5SDimitry Andric       if (error.Fail())
18050b57cec5SDimitry Andric         return LLDB_INVALID_IMAGE_TOKEN;
18060b57cec5SDimitry Andric     }
18070b57cec5SDimitry Andric     return DoLoadImage(process, remote_file, nullptr, error);
18080b57cec5SDimitry Andric   }
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric   if (local_file) {
18110b57cec5SDimitry Andric     // Only local file was specified. Install it to the current working
18120b57cec5SDimitry Andric     // directory.
18130b57cec5SDimitry Andric     FileSpec target_file = GetWorkingDirectory();
18140b57cec5SDimitry Andric     target_file.AppendPathComponent(local_file.GetFilename().AsCString());
18150b57cec5SDimitry Andric     if (IsRemote() || local_file != target_file) {
18160b57cec5SDimitry Andric       error = Install(local_file, target_file);
18170b57cec5SDimitry Andric       if (error.Fail())
18180b57cec5SDimitry Andric         return LLDB_INVALID_IMAGE_TOKEN;
18190b57cec5SDimitry Andric     }
18200b57cec5SDimitry Andric     return DoLoadImage(process, target_file, nullptr, error);
18210b57cec5SDimitry Andric   }
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric   if (remote_file) {
18240b57cec5SDimitry Andric     // Only remote file was specified so we don't have to do any copying
18250b57cec5SDimitry Andric     return DoLoadImage(process, remote_file, nullptr, error);
18260b57cec5SDimitry Andric   }
18270b57cec5SDimitry Andric 
18280b57cec5SDimitry Andric   error.SetErrorString("Neither local nor remote file was specified");
18290b57cec5SDimitry Andric   return LLDB_INVALID_IMAGE_TOKEN;
18300b57cec5SDimitry Andric }
18310b57cec5SDimitry Andric 
18320b57cec5SDimitry Andric uint32_t Platform::DoLoadImage(lldb_private::Process *process,
18330b57cec5SDimitry Andric                                const lldb_private::FileSpec &remote_file,
18340b57cec5SDimitry Andric                                const std::vector<std::string> *paths,
18350b57cec5SDimitry Andric                                lldb_private::Status &error,
18360b57cec5SDimitry Andric                                lldb_private::FileSpec *loaded_image) {
18370b57cec5SDimitry Andric   error.SetErrorString("LoadImage is not supported on the current platform");
18380b57cec5SDimitry Andric   return LLDB_INVALID_IMAGE_TOKEN;
18390b57cec5SDimitry Andric }
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
18420b57cec5SDimitry Andric                                const lldb_private::FileSpec &remote_filename,
18430b57cec5SDimitry Andric                                const std::vector<std::string> &paths,
18440b57cec5SDimitry Andric                                lldb_private::Status &error,
18450b57cec5SDimitry Andric                                lldb_private::FileSpec *loaded_path)
18460b57cec5SDimitry Andric {
18470b57cec5SDimitry Andric   FileSpec file_to_use;
18480b57cec5SDimitry Andric   if (remote_filename.IsAbsolute())
18490b57cec5SDimitry Andric     file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric                            remote_filename.GetPathStyle());
18520b57cec5SDimitry Andric   else
18530b57cec5SDimitry Andric     file_to_use = remote_filename;
18540b57cec5SDimitry Andric 
18550b57cec5SDimitry Andric   return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
18560b57cec5SDimitry Andric }
18570b57cec5SDimitry Andric 
18580b57cec5SDimitry Andric Status Platform::UnloadImage(lldb_private::Process *process,
18590b57cec5SDimitry Andric                              uint32_t image_token) {
18600b57cec5SDimitry Andric   return Status("UnloadImage is not supported on the current platform");
18610b57cec5SDimitry Andric }
18620b57cec5SDimitry Andric 
18630b57cec5SDimitry Andric lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
18640b57cec5SDimitry Andric                                          llvm::StringRef plugin_name,
18655ffd83dbSDimitry Andric                                          Debugger &debugger, Target *target,
18665ffd83dbSDimitry Andric                                          Status &error) {
18675ffd83dbSDimitry Andric   return DoConnectProcess(connect_url, plugin_name, debugger, nullptr, target,
18685ffd83dbSDimitry Andric                           error);
18695ffd83dbSDimitry Andric }
18705ffd83dbSDimitry Andric 
18715ffd83dbSDimitry Andric lldb::ProcessSP Platform::ConnectProcessSynchronous(
18725ffd83dbSDimitry Andric     llvm::StringRef connect_url, llvm::StringRef plugin_name,
18735ffd83dbSDimitry Andric     Debugger &debugger, Stream &stream, Target *target, Status &error) {
18745ffd83dbSDimitry Andric   return DoConnectProcess(connect_url, plugin_name, debugger, &stream, target,
18755ffd83dbSDimitry Andric                           error);
18765ffd83dbSDimitry Andric }
18775ffd83dbSDimitry Andric 
18785ffd83dbSDimitry Andric lldb::ProcessSP Platform::DoConnectProcess(llvm::StringRef connect_url,
18795ffd83dbSDimitry Andric                                            llvm::StringRef plugin_name,
18805ffd83dbSDimitry Andric                                            Debugger &debugger, Stream *stream,
18815ffd83dbSDimitry Andric                                            Target *target, Status &error) {
18820b57cec5SDimitry Andric   error.Clear();
18830b57cec5SDimitry Andric 
18840b57cec5SDimitry Andric   if (!target) {
1885bdd1243dSDimitry Andric     ArchSpec arch = Target::GetDefaultArchitecture();
18860b57cec5SDimitry Andric 
1887bdd1243dSDimitry Andric     const char *triple =
1888bdd1243dSDimitry Andric         arch.IsValid() ? arch.GetTriple().getTriple().c_str() : "";
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric     TargetSP new_target_sp;
18910b57cec5SDimitry Andric     error = debugger.GetTargetList().CreateTarget(
18920b57cec5SDimitry Andric         debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
18930b57cec5SDimitry Andric 
1894bdd1243dSDimitry Andric     target = new_target_sp.get();
1895bdd1243dSDimitry Andric     if (!target || error.Fail()) {
18960b57cec5SDimitry Andric       return nullptr;
1897bdd1243dSDimitry Andric     }
1898bdd1243dSDimitry Andric   }
18990b57cec5SDimitry Andric 
19000b57cec5SDimitry Andric   lldb::ProcessSP process_sp =
1901e8d8bef9SDimitry Andric       target->CreateProcess(debugger.GetListener(), plugin_name, nullptr, true);
19025ffd83dbSDimitry Andric 
19030b57cec5SDimitry Andric   if (!process_sp)
19040b57cec5SDimitry Andric     return nullptr;
19050b57cec5SDimitry Andric 
19065ffd83dbSDimitry Andric   // If this private method is called with a stream we are synchronous.
19075ffd83dbSDimitry Andric   const bool synchronous = stream != nullptr;
19085ffd83dbSDimitry Andric 
19095ffd83dbSDimitry Andric   ListenerSP listener_sp(
19105ffd83dbSDimitry Andric       Listener::MakeListener("lldb.Process.ConnectProcess.hijack"));
19115ffd83dbSDimitry Andric   if (synchronous)
19125ffd83dbSDimitry Andric     process_sp->HijackProcessEvents(listener_sp);
19135ffd83dbSDimitry Andric 
19145ffd83dbSDimitry Andric   error = process_sp->ConnectRemote(connect_url);
19155ffd83dbSDimitry Andric   if (error.Fail()) {
19165ffd83dbSDimitry Andric     if (synchronous)
19175ffd83dbSDimitry Andric       process_sp->RestoreProcessEvents();
19180b57cec5SDimitry Andric     return nullptr;
19195ffd83dbSDimitry Andric   }
19205ffd83dbSDimitry Andric 
19215ffd83dbSDimitry Andric   if (synchronous) {
19225ffd83dbSDimitry Andric     EventSP event_sp;
1923bdd1243dSDimitry Andric     process_sp->WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp,
19245ffd83dbSDimitry Andric                                      nullptr);
19255ffd83dbSDimitry Andric     process_sp->RestoreProcessEvents();
19265ffd83dbSDimitry Andric     bool pop_process_io_handler = false;
192706c3fb27SDimitry Andric     // This is a user-level stop, so we allow recognizers to select frames.
192806c3fb27SDimitry Andric     Process::HandleProcessStateChangedEvent(
192906c3fb27SDimitry Andric         event_sp, stream, SelectMostRelevantFrame, pop_process_io_handler);
19305ffd83dbSDimitry Andric   }
19310b57cec5SDimitry Andric 
19320b57cec5SDimitry Andric   return process_sp;
19330b57cec5SDimitry Andric }
19340b57cec5SDimitry Andric 
19350b57cec5SDimitry Andric size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
19360b57cec5SDimitry Andric                                            lldb_private::Status &error) {
19370b57cec5SDimitry Andric   error.Clear();
19380b57cec5SDimitry Andric   return 0;
19390b57cec5SDimitry Andric }
19400b57cec5SDimitry Andric 
19410b57cec5SDimitry Andric size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
19420b57cec5SDimitry Andric                                                  BreakpointSite *bp_site) {
19430b57cec5SDimitry Andric   ArchSpec arch = target.GetArchitecture();
19445ffd83dbSDimitry Andric   assert(arch.IsValid());
19450b57cec5SDimitry Andric   const uint8_t *trap_opcode = nullptr;
19460b57cec5SDimitry Andric   size_t trap_opcode_size = 0;
19470b57cec5SDimitry Andric 
19480b57cec5SDimitry Andric   switch (arch.GetMachine()) {
19499dba64beSDimitry Andric   case llvm::Triple::aarch64_32:
19500b57cec5SDimitry Andric   case llvm::Triple::aarch64: {
19510b57cec5SDimitry Andric     static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
19520b57cec5SDimitry Andric     trap_opcode = g_aarch64_opcode;
19530b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_aarch64_opcode);
19540b57cec5SDimitry Andric   } break;
19550b57cec5SDimitry Andric 
19569dba64beSDimitry Andric   case llvm::Triple::arc: {
19579dba64beSDimitry Andric     static const uint8_t g_hex_opcode[] = { 0xff, 0x7f };
19589dba64beSDimitry Andric     trap_opcode = g_hex_opcode;
19599dba64beSDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
19609dba64beSDimitry Andric   } break;
19619dba64beSDimitry Andric 
19620b57cec5SDimitry Andric   // TODO: support big-endian arm and thumb trap codes.
19630b57cec5SDimitry Andric   case llvm::Triple::arm: {
19640b57cec5SDimitry Andric     // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
19650b57cec5SDimitry Andric     // linux kernel does otherwise.
19660b57cec5SDimitry Andric     static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
19670b57cec5SDimitry Andric     static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
19680b57cec5SDimitry Andric 
19695f757f3fSDimitry Andric     lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetConstituentAtIndex(0));
19700b57cec5SDimitry Andric     AddressClass addr_class = AddressClass::eUnknown;
19710b57cec5SDimitry Andric 
19720b57cec5SDimitry Andric     if (bp_loc_sp) {
19730b57cec5SDimitry Andric       addr_class = bp_loc_sp->GetAddress().GetAddressClass();
19740b57cec5SDimitry Andric       if (addr_class == AddressClass::eUnknown &&
19750b57cec5SDimitry Andric           (bp_loc_sp->GetAddress().GetFileAddress() & 1))
19760b57cec5SDimitry Andric         addr_class = AddressClass::eCodeAlternateISA;
19770b57cec5SDimitry Andric     }
19780b57cec5SDimitry Andric 
19790b57cec5SDimitry Andric     if (addr_class == AddressClass::eCodeAlternateISA) {
19800b57cec5SDimitry Andric       trap_opcode = g_thumb_breakpoint_opcode;
19810b57cec5SDimitry Andric       trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
19820b57cec5SDimitry Andric     } else {
19830b57cec5SDimitry Andric       trap_opcode = g_arm_breakpoint_opcode;
19840b57cec5SDimitry Andric       trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
19850b57cec5SDimitry Andric     }
19860b57cec5SDimitry Andric   } break;
19870b57cec5SDimitry Andric 
19885ffd83dbSDimitry Andric   case llvm::Triple::avr: {
19895ffd83dbSDimitry Andric     static const uint8_t g_hex_opcode[] = {0x98, 0x95};
19905ffd83dbSDimitry Andric     trap_opcode = g_hex_opcode;
19915ffd83dbSDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
19925ffd83dbSDimitry Andric   } break;
19935ffd83dbSDimitry Andric 
19940b57cec5SDimitry Andric   case llvm::Triple::mips:
19950b57cec5SDimitry Andric   case llvm::Triple::mips64: {
19960b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
19970b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
19980b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
19990b57cec5SDimitry Andric   } break;
20000b57cec5SDimitry Andric 
20010b57cec5SDimitry Andric   case llvm::Triple::mipsel:
20020b57cec5SDimitry Andric   case llvm::Triple::mips64el: {
20030b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
20040b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
20050b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20060b57cec5SDimitry Andric   } break;
20070b57cec5SDimitry Andric 
200806c3fb27SDimitry Andric   case llvm::Triple::msp430: {
200906c3fb27SDimitry Andric     static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
201006c3fb27SDimitry Andric     trap_opcode = g_msp430_opcode;
201106c3fb27SDimitry Andric     trap_opcode_size = sizeof(g_msp430_opcode);
201206c3fb27SDimitry Andric   } break;
201306c3fb27SDimitry Andric 
20140b57cec5SDimitry Andric   case llvm::Triple::systemz: {
20150b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x00, 0x01};
20160b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
20170b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20180b57cec5SDimitry Andric   } break;
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric   case llvm::Triple::hexagon: {
20210b57cec5SDimitry Andric     static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
20220b57cec5SDimitry Andric     trap_opcode = g_hex_opcode;
20230b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_hex_opcode);
20240b57cec5SDimitry Andric   } break;
20250b57cec5SDimitry Andric 
20260b57cec5SDimitry Andric   case llvm::Triple::ppc:
20270b57cec5SDimitry Andric   case llvm::Triple::ppc64: {
20280b57cec5SDimitry Andric     static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
20290b57cec5SDimitry Andric     trap_opcode = g_ppc_opcode;
20300b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_ppc_opcode);
20310b57cec5SDimitry Andric   } break;
20320b57cec5SDimitry Andric 
20330b57cec5SDimitry Andric   case llvm::Triple::ppc64le: {
20340b57cec5SDimitry Andric     static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
20350b57cec5SDimitry Andric     trap_opcode = g_ppc64le_opcode;
20360b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_ppc64le_opcode);
20370b57cec5SDimitry Andric   } break;
20380b57cec5SDimitry Andric 
20390b57cec5SDimitry Andric   case llvm::Triple::x86:
20400b57cec5SDimitry Andric   case llvm::Triple::x86_64: {
20410b57cec5SDimitry Andric     static const uint8_t g_i386_opcode[] = {0xCC};
20420b57cec5SDimitry Andric     trap_opcode = g_i386_opcode;
20430b57cec5SDimitry Andric     trap_opcode_size = sizeof(g_i386_opcode);
20440b57cec5SDimitry Andric   } break;
20450b57cec5SDimitry Andric 
2046bdd1243dSDimitry Andric   case llvm::Triple::riscv32:
2047bdd1243dSDimitry Andric   case llvm::Triple::riscv64: {
2048bdd1243dSDimitry Andric     static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00}; // ebreak
2049bdd1243dSDimitry Andric     static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90}; // c.ebreak
2050bdd1243dSDimitry Andric     if (arch.GetFlags() & ArchSpec::eRISCV_rvc) {
2051bdd1243dSDimitry Andric       trap_opcode = g_riscv_opcode_c;
2052bdd1243dSDimitry Andric       trap_opcode_size = sizeof(g_riscv_opcode_c);
2053bdd1243dSDimitry Andric     } else {
2054bdd1243dSDimitry Andric       trap_opcode = g_riscv_opcode;
2055bdd1243dSDimitry Andric       trap_opcode_size = sizeof(g_riscv_opcode);
2056bdd1243dSDimitry Andric     }
2057bdd1243dSDimitry Andric   } break;
2058bdd1243dSDimitry Andric 
2059bdd1243dSDimitry Andric   case llvm::Triple::loongarch32:
2060bdd1243dSDimitry Andric   case llvm::Triple::loongarch64: {
2061bdd1243dSDimitry Andric     static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
2062bdd1243dSDimitry Andric                                                  0x00}; // break 0x5
2063bdd1243dSDimitry Andric     trap_opcode = g_loongarch_opcode;
2064bdd1243dSDimitry Andric     trap_opcode_size = sizeof(g_loongarch_opcode);
2065bdd1243dSDimitry Andric   } break;
2066bdd1243dSDimitry Andric 
20670b57cec5SDimitry Andric   default:
20685ffd83dbSDimitry Andric     return 0;
20690b57cec5SDimitry Andric   }
20700b57cec5SDimitry Andric 
20710b57cec5SDimitry Andric   assert(bp_site);
20720b57cec5SDimitry Andric   if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
20730b57cec5SDimitry Andric     return trap_opcode_size;
20740b57cec5SDimitry Andric 
20750b57cec5SDimitry Andric   return 0;
20760b57cec5SDimitry Andric }
207704eeddc0SDimitry Andric 
207804eeddc0SDimitry Andric CompilerType Platform::GetSiginfoType(const llvm::Triple& triple) {
207904eeddc0SDimitry Andric   return CompilerType();
208004eeddc0SDimitry Andric }
208181ad6265SDimitry Andric 
208281ad6265SDimitry Andric Args Platform::GetExtraStartupCommands() {
208381ad6265SDimitry Andric   return {};
208481ad6265SDimitry Andric }
208581ad6265SDimitry Andric 
208606c3fb27SDimitry Andric void Platform::SetLocateModuleCallback(LocateModuleCallback callback) {
208706c3fb27SDimitry Andric   m_locate_module_callback = callback;
208806c3fb27SDimitry Andric }
208906c3fb27SDimitry Andric 
209006c3fb27SDimitry Andric Platform::LocateModuleCallback Platform::GetLocateModuleCallback() const {
209106c3fb27SDimitry Andric   return m_locate_module_callback;
209206c3fb27SDimitry Andric }
209306c3fb27SDimitry Andric 
209481ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(llvm::StringRef name) {
209581ad6265SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
209681ad6265SDimitry Andric   for (const PlatformSP &platform_sp : m_platforms) {
209781ad6265SDimitry Andric     if (platform_sp->GetName() == name)
209881ad6265SDimitry Andric       return platform_sp;
209981ad6265SDimitry Andric   }
210081ad6265SDimitry Andric   return Create(name);
210181ad6265SDimitry Andric }
210281ad6265SDimitry Andric 
210381ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
210481ad6265SDimitry Andric                                      const ArchSpec &process_host_arch,
210581ad6265SDimitry Andric                                      ArchSpec *platform_arch_ptr,
210681ad6265SDimitry Andric                                      Status &error) {
210781ad6265SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
210881ad6265SDimitry Andric   // First try exact arch matches across all platforms already created
210981ad6265SDimitry Andric   for (const auto &platform_sp : m_platforms) {
2110bdd1243dSDimitry Andric     if (platform_sp->IsCompatibleArchitecture(
2111bdd1243dSDimitry Andric             arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr))
211281ad6265SDimitry Andric       return platform_sp;
211381ad6265SDimitry Andric   }
211481ad6265SDimitry Andric 
211581ad6265SDimitry Andric   // Next try compatible arch matches across all platforms already created
211681ad6265SDimitry Andric   for (const auto &platform_sp : m_platforms) {
2117bdd1243dSDimitry Andric     if (platform_sp->IsCompatibleArchitecture(arch, process_host_arch,
2118bdd1243dSDimitry Andric                                               ArchSpec::CompatibleMatch,
211981ad6265SDimitry Andric                                               platform_arch_ptr))
212081ad6265SDimitry Andric       return platform_sp;
212181ad6265SDimitry Andric   }
212281ad6265SDimitry Andric 
212381ad6265SDimitry Andric   PlatformCreateInstance create_callback;
212481ad6265SDimitry Andric   // First try exact arch matches across all platform plug-ins
212581ad6265SDimitry Andric   uint32_t idx;
212681ad6265SDimitry Andric   for (idx = 0;
212781ad6265SDimitry Andric        (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
212881ad6265SDimitry Andric        ++idx) {
212981ad6265SDimitry Andric     PlatformSP platform_sp = create_callback(false, &arch);
2130bdd1243dSDimitry Andric     if (platform_sp &&
2131bdd1243dSDimitry Andric         platform_sp->IsCompatibleArchitecture(
2132bdd1243dSDimitry Andric             arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr)) {
213381ad6265SDimitry Andric       m_platforms.push_back(platform_sp);
213481ad6265SDimitry Andric       return platform_sp;
213581ad6265SDimitry Andric     }
213681ad6265SDimitry Andric   }
213781ad6265SDimitry Andric   // Next try compatible arch matches across all platform plug-ins
213881ad6265SDimitry Andric   for (idx = 0;
213981ad6265SDimitry Andric        (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
214081ad6265SDimitry Andric        ++idx) {
214181ad6265SDimitry Andric     PlatformSP platform_sp = create_callback(false, &arch);
214281ad6265SDimitry Andric     if (platform_sp && platform_sp->IsCompatibleArchitecture(
2143bdd1243dSDimitry Andric                            arch, process_host_arch, ArchSpec::CompatibleMatch,
2144bdd1243dSDimitry Andric                            platform_arch_ptr)) {
214581ad6265SDimitry Andric       m_platforms.push_back(platform_sp);
214681ad6265SDimitry Andric       return platform_sp;
214781ad6265SDimitry Andric     }
214881ad6265SDimitry Andric   }
214981ad6265SDimitry Andric   if (platform_arch_ptr)
215081ad6265SDimitry Andric     platform_arch_ptr->Clear();
215181ad6265SDimitry Andric   return nullptr;
215281ad6265SDimitry Andric }
215381ad6265SDimitry Andric 
215481ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
215581ad6265SDimitry Andric                                      const ArchSpec &process_host_arch,
215681ad6265SDimitry Andric                                      ArchSpec *platform_arch_ptr) {
215781ad6265SDimitry Andric   Status error;
215881ad6265SDimitry Andric   if (arch.IsValid())
215981ad6265SDimitry Andric     return GetOrCreate(arch, process_host_arch, platform_arch_ptr, error);
216081ad6265SDimitry Andric   return nullptr;
216181ad6265SDimitry Andric }
216281ad6265SDimitry Andric 
216381ad6265SDimitry Andric PlatformSP PlatformList::GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
216481ad6265SDimitry Andric                                      const ArchSpec &process_host_arch,
216581ad6265SDimitry Andric                                      std::vector<PlatformSP> &candidates) {
216681ad6265SDimitry Andric   candidates.clear();
216781ad6265SDimitry Andric   candidates.reserve(archs.size());
216881ad6265SDimitry Andric 
216981ad6265SDimitry Andric   if (archs.empty())
217081ad6265SDimitry Andric     return nullptr;
217181ad6265SDimitry Andric 
217281ad6265SDimitry Andric   PlatformSP host_platform_sp = Platform::GetHostPlatform();
217381ad6265SDimitry Andric 
217481ad6265SDimitry Andric   // Prefer the selected platform if it matches at least one architecture.
217581ad6265SDimitry Andric   if (m_selected_platform_sp) {
217681ad6265SDimitry Andric     for (const ArchSpec &arch : archs) {
217781ad6265SDimitry Andric       if (m_selected_platform_sp->IsCompatibleArchitecture(
2178bdd1243dSDimitry Andric               arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
217981ad6265SDimitry Andric         return m_selected_platform_sp;
218081ad6265SDimitry Andric     }
218181ad6265SDimitry Andric   }
218281ad6265SDimitry Andric 
218381ad6265SDimitry Andric   // Prefer the host platform if it matches at least one architecture.
218481ad6265SDimitry Andric   if (host_platform_sp) {
218581ad6265SDimitry Andric     for (const ArchSpec &arch : archs) {
2186bdd1243dSDimitry Andric       if (host_platform_sp->IsCompatibleArchitecture(
2187bdd1243dSDimitry Andric               arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
218881ad6265SDimitry Andric         return host_platform_sp;
218981ad6265SDimitry Andric     }
219081ad6265SDimitry Andric   }
219181ad6265SDimitry Andric 
219281ad6265SDimitry Andric   // Collect a list of candidate platforms for the architectures.
219381ad6265SDimitry Andric   for (const ArchSpec &arch : archs) {
219481ad6265SDimitry Andric     if (PlatformSP platform = GetOrCreate(arch, process_host_arch, nullptr))
219581ad6265SDimitry Andric       candidates.push_back(platform);
219681ad6265SDimitry Andric   }
219781ad6265SDimitry Andric 
219881ad6265SDimitry Andric   // The selected or host platform didn't match any of the architectures. If
219981ad6265SDimitry Andric   // the same platform supports all architectures then that's the obvious next
220081ad6265SDimitry Andric   // best thing.
220181ad6265SDimitry Andric   if (candidates.size() == archs.size()) {
2202bdd1243dSDimitry Andric     if (llvm::all_of(candidates, [&](const PlatformSP &p) -> bool {
220381ad6265SDimitry Andric           return p->GetName() == candidates.front()->GetName();
220481ad6265SDimitry Andric         })) {
220581ad6265SDimitry Andric       return candidates.front();
220681ad6265SDimitry Andric     }
220781ad6265SDimitry Andric   }
220881ad6265SDimitry Andric 
220981ad6265SDimitry Andric   // At this point we either have no platforms that match the given
221081ad6265SDimitry Andric   // architectures or multiple platforms with no good way to disambiguate
221181ad6265SDimitry Andric   // between them.
221281ad6265SDimitry Andric   return nullptr;
221381ad6265SDimitry Andric }
221481ad6265SDimitry Andric 
221581ad6265SDimitry Andric PlatformSP PlatformList::Create(llvm::StringRef name) {
221681ad6265SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
221781ad6265SDimitry Andric   PlatformSP platform_sp = Platform::Create(name);
221881ad6265SDimitry Andric   m_platforms.push_back(platform_sp);
221981ad6265SDimitry Andric   return platform_sp;
222081ad6265SDimitry Andric }
2221bdd1243dSDimitry Andric 
2222bdd1243dSDimitry Andric bool PlatformList::LoadPlatformBinaryAndSetup(Process *process,
2223bdd1243dSDimitry Andric                                               lldb::addr_t addr, bool notify) {
2224bdd1243dSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
2225bdd1243dSDimitry Andric 
2226bdd1243dSDimitry Andric   PlatformCreateInstance create_callback;
2227bdd1243dSDimitry Andric   for (int idx = 0;
2228bdd1243dSDimitry Andric        (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
2229bdd1243dSDimitry Andric        ++idx) {
2230bdd1243dSDimitry Andric     ArchSpec arch;
2231bdd1243dSDimitry Andric     PlatformSP platform_sp = create_callback(true, &arch);
2232bdd1243dSDimitry Andric     if (platform_sp) {
2233bdd1243dSDimitry Andric       if (platform_sp->LoadPlatformBinaryAndSetup(process, addr, notify))
2234bdd1243dSDimitry Andric         return true;
2235bdd1243dSDimitry Andric     }
2236bdd1243dSDimitry Andric   }
2237bdd1243dSDimitry Andric   return false;
2238bdd1243dSDimitry Andric }
2239