1cfca06d7SDimitry Andric //===-- Platform.cpp ------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
95e95aa85SEd Maste #include <algorithm>
10f73363f1SDimitry Andric #include <csignal>
115e95aa85SEd Maste #include <fstream>
125f29bb8aSDimitry Andric #include <memory>
13e3b55780SDimitry Andric #include <optional>
145e95aa85SEd Maste #include <vector>
155e95aa85SEd Maste
16f034231aSEd Maste #include "lldb/Breakpoint/BreakpointIDList.h"
17f3fbd1c0SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
185e95aa85SEd Maste #include "lldb/Core/Debugger.h"
195e95aa85SEd Maste #include "lldb/Core/Module.h"
20f034231aSEd Maste #include "lldb/Core/ModuleSpec.h"
21f034231aSEd Maste #include "lldb/Core/PluginManager.h"
22145449b1SDimitry Andric #include "lldb/Host/FileCache.h"
230cac4ca3SEd Maste #include "lldb/Host/FileSystem.h"
24f034231aSEd Maste #include "lldb/Host/Host.h"
250cac4ca3SEd Maste #include "lldb/Host/HostInfo.h"
2674a628f7SDimitry Andric #include "lldb/Host/OptionParser.h"
27cfca06d7SDimitry Andric #include "lldb/Interpreter/OptionValueFileSpec.h"
285e95aa85SEd Maste #include "lldb/Interpreter/OptionValueProperties.h"
295e95aa85SEd Maste #include "lldb/Interpreter/Property.h"
305e95aa85SEd Maste #include "lldb/Symbol/ObjectFile.h"
3174a628f7SDimitry Andric #include "lldb/Target/ModuleCache.h"
3214f1b3e8SDimitry Andric #include "lldb/Target/Platform.h"
33f034231aSEd Maste #include "lldb/Target/Process.h"
34f034231aSEd Maste #include "lldb/Target/Target.h"
35027f1c96SDimitry Andric #include "lldb/Target/UnixSignals.h"
3674a628f7SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
3774a628f7SDimitry Andric #include "lldb/Utility/FileSpec.h"
38145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
3974a628f7SDimitry Andric #include "lldb/Utility/Log.h"
40b76161e4SDimitry Andric #include "lldb/Utility/Status.h"
411b306c26SDimitry Andric #include "lldb/Utility/StructuredData.h"
42145449b1SDimitry Andric #include "llvm/ADT/STLExtras.h"
4374a628f7SDimitry Andric #include "llvm/Support/FileSystem.h"
44cfca06d7SDimitry Andric #include "llvm/Support/Path.h"
455e95aa85SEd Maste
46f73363f1SDimitry Andric // Define these constants from POSIX mman.h rather than include the file so
47f73363f1SDimitry Andric // that they will be correct even when compiled on Linux.
485e95aa85SEd Maste #define MAP_PRIVATE 2
495e95aa85SEd Maste #define MAP_ANON 0x1000
50f034231aSEd Maste
51f034231aSEd Maste using namespace lldb;
52f034231aSEd Maste using namespace lldb_private;
53f034231aSEd Maste
54f73363f1SDimitry Andric // Use a singleton function for g_local_platform_sp to avoid init constructors
55f73363f1SDimitry Andric // since LLDB is often part of a shared library
GetHostPlatformSP()5614f1b3e8SDimitry Andric static PlatformSP &GetHostPlatformSP() {
57205afe67SEd Maste static PlatformSP g_platform_sp;
58205afe67SEd Maste return g_platform_sp;
59f034231aSEd Maste }
60f034231aSEd Maste
GetHostPlatformName()6114f1b3e8SDimitry Andric const char *Platform::GetHostPlatformName() { return "host"; }
62f034231aSEd Maste
635e95aa85SEd Maste namespace {
645e95aa85SEd Maste
65ead24645SDimitry Andric #define LLDB_PROPERTIES_platform
66ead24645SDimitry Andric #include "TargetProperties.inc"
675e95aa85SEd Maste
68ead24645SDimitry Andric enum {
69ead24645SDimitry Andric #define LLDB_PROPERTIES_platform
70ead24645SDimitry Andric #include "TargetPropertiesEnum.inc"
71ead24645SDimitry Andric };
725e95aa85SEd Maste
735e95aa85SEd Maste } // namespace
745e95aa85SEd Maste
GetSettingName()75b1c73532SDimitry Andric llvm::StringRef PlatformProperties::GetSettingName() {
76b1c73532SDimitry Andric static constexpr llvm::StringLiteral g_setting_name("platform");
775e95aa85SEd Maste return g_setting_name;
785e95aa85SEd Maste }
795e95aa85SEd Maste
PlatformProperties()8014f1b3e8SDimitry Andric PlatformProperties::PlatformProperties() {
815f29bb8aSDimitry Andric m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
82ead24645SDimitry Andric m_collection_sp->Initialize(g_platform_properties);
835e95aa85SEd Maste
845e95aa85SEd Maste auto module_cache_dir = GetModuleCacheDirectory();
85e81d9d49SDimitry Andric if (module_cache_dir)
86e81d9d49SDimitry Andric return;
87e81d9d49SDimitry Andric
88e81d9d49SDimitry Andric llvm::SmallString<64> user_home_dir;
89b60736ecSDimitry Andric if (!FileSystem::Instance().GetHomeDirectory(user_home_dir))
90e81d9d49SDimitry Andric return;
91e81d9d49SDimitry Andric
9294994d37SDimitry Andric module_cache_dir = FileSpec(user_home_dir.c_str());
93e81d9d49SDimitry Andric module_cache_dir.AppendPathComponent(".lldb");
945e95aa85SEd Maste module_cache_dir.AppendPathComponent("module_cache");
95cfca06d7SDimitry Andric SetDefaultModuleCacheDirectory(module_cache_dir);
965e95aa85SEd Maste SetModuleCacheDirectory(module_cache_dir);
975e95aa85SEd Maste }
985e95aa85SEd Maste
GetUseModuleCache() const9914f1b3e8SDimitry Andric bool PlatformProperties::GetUseModuleCache() const {
1005e95aa85SEd Maste const auto idx = ePropertyUseModuleCache;
1017fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
1027fa27ce4SDimitry Andric idx, g_platform_properties[idx].default_uint_value != 0);
1035e95aa85SEd Maste }
1045e95aa85SEd Maste
SetUseModuleCache(bool use_module_cache)10514f1b3e8SDimitry Andric bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
1067fa27ce4SDimitry Andric return SetPropertyAtIndex(ePropertyUseModuleCache, use_module_cache);
1075e95aa85SEd Maste }
1085e95aa85SEd Maste
GetModuleCacheDirectory() const10914f1b3e8SDimitry Andric FileSpec PlatformProperties::GetModuleCacheDirectory() const {
1107fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpec>(ePropertyModuleCacheDirectory, {});
1115e95aa85SEd Maste }
1125e95aa85SEd Maste
SetModuleCacheDirectory(const FileSpec & dir_spec)11314f1b3e8SDimitry Andric bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
1147fa27ce4SDimitry Andric return m_collection_sp->SetPropertyAtIndex(ePropertyModuleCacheDirectory,
1157fa27ce4SDimitry Andric dir_spec);
1165e95aa85SEd Maste }
1175e95aa85SEd Maste
SetDefaultModuleCacheDirectory(const FileSpec & dir_spec)118cfca06d7SDimitry Andric void PlatformProperties::SetDefaultModuleCacheDirectory(
119cfca06d7SDimitry Andric const FileSpec &dir_spec) {
120cfca06d7SDimitry Andric auto f_spec_opt = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(
1217fa27ce4SDimitry Andric ePropertyModuleCacheDirectory);
122cfca06d7SDimitry Andric assert(f_spec_opt);
123cfca06d7SDimitry Andric f_spec_opt->SetDefaultValue(dir_spec);
124cfca06d7SDimitry Andric }
125cfca06d7SDimitry Andric
126f034231aSEd Maste /// Get the native host platform plug-in.
127f034231aSEd Maste ///
128f034231aSEd Maste /// There should only be one of these for each host that LLDB runs
129f034231aSEd Maste /// upon that should be statically compiled in and registered using
130f034231aSEd Maste /// preprocessor macros or other similar build mechanisms.
131f034231aSEd Maste ///
132f034231aSEd Maste /// This platform will be used as the default platform when launching
133f034231aSEd Maste /// or attaching to processes unless another platform is specified.
GetHostPlatform()13414f1b3e8SDimitry Andric PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
135205afe67SEd Maste
Initialize()136145449b1SDimitry Andric void Platform::Initialize() {}
137205afe67SEd Maste
Terminate()138145449b1SDimitry Andric void Platform::Terminate() {}
1395e95aa85SEd Maste
GetGlobalPlatformProperties()140c0981da4SDimitry Andric PlatformProperties &Platform::GetGlobalPlatformProperties() {
141c0981da4SDimitry Andric static PlatformProperties g_settings;
142c0981da4SDimitry Andric return g_settings;
1435e95aa85SEd Maste }
1445e95aa85SEd Maste
SetHostPlatform(const lldb::PlatformSP & platform_sp)14514f1b3e8SDimitry Andric void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
146f034231aSEd Maste // The native platform should use its static void Platform::Initialize()
147f034231aSEd Maste // function to register itself as the native platform.
148205afe67SEd Maste GetHostPlatformSP() = platform_sp;
149f034231aSEd Maste }
150f034231aSEd Maste
GetFileWithUUID(const FileSpec & platform_file,const UUID * uuid_ptr,FileSpec & local_file)151b76161e4SDimitry Andric Status Platform::GetFileWithUUID(const FileSpec &platform_file,
15214f1b3e8SDimitry Andric const UUID *uuid_ptr, FileSpec &local_file) {
153f034231aSEd Maste // Default to the local case
154f034231aSEd Maste local_file = platform_file;
155b76161e4SDimitry Andric return Status();
156f034231aSEd Maste }
157f034231aSEd Maste
158f034231aSEd Maste FileSpecList
LocateExecutableScriptingResources(Target * target,Module & module,Stream & feedback_stream)15914f1b3e8SDimitry Andric Platform::LocateExecutableScriptingResources(Target *target, Module &module,
1607fa27ce4SDimitry Andric Stream &feedback_stream) {
161f034231aSEd Maste return FileSpecList();
162f034231aSEd Maste }
163f034231aSEd Maste
GetSharedModule(const ModuleSpec & module_spec,Process * process,ModuleSP & module_sp,const FileSpecList * module_search_paths_ptr,llvm::SmallVectorImpl<lldb::ModuleSP> * old_modules,bool * did_create_ptr)164b60736ecSDimitry Andric Status Platform::GetSharedModule(
165b60736ecSDimitry Andric const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
166f034231aSEd Maste const FileSpecList *module_search_paths_ptr,
167b60736ecSDimitry Andric llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
1685e95aa85SEd Maste if (IsHost())
169b60736ecSDimitry Andric return ModuleList::GetSharedModule(module_spec, module_sp,
170b60736ecSDimitry Andric module_search_paths_ptr, old_modules,
17114f1b3e8SDimitry Andric did_create_ptr, false);
1725e95aa85SEd Maste
17394994d37SDimitry Andric // Module resolver lambda.
17494994d37SDimitry Andric auto resolver = [&](const ModuleSpec &spec) {
17594994d37SDimitry Andric Status error(eErrorTypeGeneric);
17694994d37SDimitry Andric ModuleSpec resolved_spec;
17794994d37SDimitry Andric // Check if we have sysroot set.
1787fa27ce4SDimitry Andric if (!m_sdk_sysroot.empty()) {
17994994d37SDimitry Andric // Prepend sysroot to module spec.
18094994d37SDimitry Andric resolved_spec = spec;
1817fa27ce4SDimitry Andric resolved_spec.GetFileSpec().PrependPathComponent(m_sdk_sysroot);
18294994d37SDimitry Andric // Try to get shared module with resolved spec.
183b60736ecSDimitry Andric error = ModuleList::GetSharedModule(resolved_spec, module_sp,
184b60736ecSDimitry Andric module_search_paths_ptr, old_modules,
18594994d37SDimitry Andric did_create_ptr, false);
18694994d37SDimitry Andric }
18794994d37SDimitry Andric // If we don't have sysroot or it didn't work then
18894994d37SDimitry Andric // try original module spec.
18994994d37SDimitry Andric if (!error.Success()) {
19094994d37SDimitry Andric resolved_spec = spec;
191b60736ecSDimitry Andric error = ModuleList::GetSharedModule(resolved_spec, module_sp,
192b60736ecSDimitry Andric module_search_paths_ptr, old_modules,
19394994d37SDimitry Andric did_create_ptr, false);
19494994d37SDimitry Andric }
195e81d9d49SDimitry Andric if (error.Success() && module_sp)
19694994d37SDimitry Andric module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
197e81d9d49SDimitry Andric return error;
19894994d37SDimitry Andric };
19994994d37SDimitry Andric
20094994d37SDimitry Andric return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
2015e95aa85SEd Maste did_create_ptr);
2025e95aa85SEd Maste }
2035e95aa85SEd Maste
GetModuleSpec(const FileSpec & module_file_spec,const ArchSpec & arch,ModuleSpec & module_spec)20414f1b3e8SDimitry Andric bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
20514f1b3e8SDimitry Andric const ArchSpec &arch, ModuleSpec &module_spec) {
2065e95aa85SEd Maste ModuleSpecList module_specs;
20714f1b3e8SDimitry Andric if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
20814f1b3e8SDimitry Andric module_specs) == 0)
2095e95aa85SEd Maste return false;
2105e95aa85SEd Maste
2115e95aa85SEd Maste ModuleSpec matched_module_spec;
2125e95aa85SEd Maste return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
2135e95aa85SEd Maste module_spec);
214f034231aSEd Maste }
215f034231aSEd Maste
Create(llvm::StringRef name)216145449b1SDimitry Andric PlatformSP Platform::Create(llvm::StringRef name) {
217145449b1SDimitry Andric lldb::PlatformSP platform_sp;
218145449b1SDimitry Andric if (name == GetHostPlatformName())
219205afe67SEd Maste return GetHostPlatform();
220205afe67SEd Maste
221145449b1SDimitry Andric if (PlatformCreateInstance create_callback =
222145449b1SDimitry Andric PluginManager::GetPlatformCreateCallbackForPluginName(name))
223145449b1SDimitry Andric return create_callback(true, nullptr);
224145449b1SDimitry Andric return nullptr;
225f034231aSEd Maste }
226f034231aSEd Maste
GetAugmentedArchSpec(Platform * platform,llvm::StringRef triple)227ef5d0b5eSDimitry Andric ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
228ef5d0b5eSDimitry Andric if (platform)
229ef5d0b5eSDimitry Andric return platform->GetAugmentedArchSpec(triple);
230ef5d0b5eSDimitry Andric return HostInfo::GetAugmentedArchSpec(triple);
231ef5d0b5eSDimitry Andric }
232ef5d0b5eSDimitry Andric
233f034231aSEd Maste /// Default Constructor
Platform(bool is_host)234f3fbd1c0SDimitry Andric Platform::Platform(bool is_host)
23514f1b3e8SDimitry Andric : m_is_host(is_host), m_os_version_set_while_connected(false),
236145449b1SDimitry Andric m_system_arch_set_while_connected(false), m_max_uid_name_len(0),
237145449b1SDimitry Andric m_max_gid_name_len(0), m_supports_rsync(false), m_rsync_opts(),
238145449b1SDimitry Andric m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
2395f29bb8aSDimitry Andric m_ignores_remote_hostname(false), m_trap_handlers(),
2405f29bb8aSDimitry Andric m_calculated_trap_handlers(false),
241ead24645SDimitry Andric m_module_cache(std::make_unique<ModuleCache>()) {
242145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Object);
243ead24645SDimitry Andric LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this));
244f034231aSEd Maste }
245f034231aSEd Maste
246c0981da4SDimitry Andric Platform::~Platform() = default;
247f034231aSEd Maste
GetStatus(Stream & strm)24814f1b3e8SDimitry Andric void Platform::GetStatus(Stream &strm) {
249c0981da4SDimitry Andric strm.Format(" Platform: {0}\n", GetPluginName());
250f034231aSEd Maste
251f034231aSEd Maste ArchSpec arch(GetSystemArchitecture());
25214f1b3e8SDimitry Andric if (arch.IsValid()) {
25314f1b3e8SDimitry Andric if (!arch.GetTriple().str().empty()) {
254e81d9d49SDimitry Andric strm.Printf(" Triple: ");
255706b4fc4SDimitry Andric arch.DumpTriple(strm.AsRawOstream());
256e81d9d49SDimitry Andric strm.EOL();
257e81d9d49SDimitry Andric }
258f034231aSEd Maste }
259f034231aSEd Maste
260f73363f1SDimitry Andric llvm::VersionTuple os_version = GetOSVersion();
261f73363f1SDimitry Andric if (!os_version.empty()) {
262f73363f1SDimitry Andric strm.Format("OS Version: {0}", os_version.getAsString());
263f034231aSEd Maste
264e3b55780SDimitry Andric if (std::optional<std::string> s = GetOSBuildString())
265c0981da4SDimitry Andric strm.Format(" ({0})", *s);
266f034231aSEd Maste
267f034231aSEd Maste strm.EOL();
268f034231aSEd Maste }
269f034231aSEd Maste
27014f1b3e8SDimitry Andric if (IsHost()) {
271f034231aSEd Maste strm.Printf(" Hostname: %s\n", GetHostname());
27214f1b3e8SDimitry Andric } else {
273f034231aSEd Maste const bool is_connected = IsConnected();
274f034231aSEd Maste if (is_connected)
275f034231aSEd Maste strm.Printf(" Hostname: %s\n", GetHostname());
276f034231aSEd Maste strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
277f034231aSEd Maste }
278f21a844fSEd Maste
2797fa27ce4SDimitry Andric if (const std::string &sdk_root = GetSDKRootDirectory(); !sdk_root.empty())
2807fa27ce4SDimitry Andric strm.Format(" Sysroot: {0}\n", sdk_root);
2817fa27ce4SDimitry Andric
28214f1b3e8SDimitry Andric if (GetWorkingDirectory()) {
283e3b55780SDimitry Andric strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetPath().c_str());
28486758c71SEd Maste }
285f21a844fSEd Maste if (!IsConnected())
286f21a844fSEd Maste return;
287f21a844fSEd Maste
288f21a844fSEd Maste std::string specific_info(GetPlatformSpecificConnectionInformation());
289f21a844fSEd Maste
290f3fbd1c0SDimitry Andric if (!specific_info.empty())
291f21a844fSEd Maste strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
292cfca06d7SDimitry Andric
293e3b55780SDimitry Andric if (std::optional<std::string> s = GetOSKernelDescription())
294c0981da4SDimitry Andric strm.Format(" Kernel: {0}\n", *s);
295f034231aSEd Maste }
296f034231aSEd Maste
GetOSVersion(Process * process)297f73363f1SDimitry Andric llvm::VersionTuple Platform::GetOSVersion(Process *process) {
298f3fbd1c0SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
29912bd4897SEd Maste
30014f1b3e8SDimitry Andric if (IsHost()) {
301f73363f1SDimitry Andric if (m_os_version.empty()) {
302f034231aSEd Maste // We have a local host platform
303f73363f1SDimitry Andric m_os_version = HostInfo::GetOSVersion();
304f73363f1SDimitry Andric m_os_version_set_while_connected = !m_os_version.empty();
305f034231aSEd Maste }
30614f1b3e8SDimitry Andric } else {
307f034231aSEd Maste // We have a remote platform. We can only fetch the remote
308f034231aSEd Maste // OS version if we are connected, and we don't want to do it
309f034231aSEd Maste // more than once.
310f034231aSEd Maste
311f034231aSEd Maste const bool is_connected = IsConnected();
312f034231aSEd Maste
313f034231aSEd Maste bool fetch = false;
314f73363f1SDimitry Andric if (!m_os_version.empty()) {
315f73363f1SDimitry Andric // We have valid OS version info, check to make sure it wasn't manually
316f73363f1SDimitry Andric // set prior to connecting. If it was manually set prior to connecting,
317f73363f1SDimitry Andric // then lets fetch the actual OS version info if we are now connected.
318f034231aSEd Maste if (is_connected && !m_os_version_set_while_connected)
319f034231aSEd Maste fetch = true;
32014f1b3e8SDimitry Andric } else {
321f034231aSEd Maste // We don't have valid OS version info, fetch it if we are connected
322f034231aSEd Maste fetch = is_connected;
323f034231aSEd Maste }
324f034231aSEd Maste
325f73363f1SDimitry Andric if (fetch)
326f73363f1SDimitry Andric m_os_version_set_while_connected = GetRemoteOSVersion();
327f034231aSEd Maste }
328f034231aSEd Maste
329f73363f1SDimitry Andric if (!m_os_version.empty())
330f73363f1SDimitry Andric return m_os_version;
331f73363f1SDimitry Andric if (process) {
332f73363f1SDimitry Andric // Check with the process in case it can answer the question if a process
333f73363f1SDimitry Andric // was provided
334f73363f1SDimitry Andric return process->GetHostOSVersion();
335e81d9d49SDimitry Andric }
336f73363f1SDimitry Andric return llvm::VersionTuple();
337f034231aSEd Maste }
338f034231aSEd Maste
GetOSBuildString()339e3b55780SDimitry Andric std::optional<std::string> Platform::GetOSBuildString() {
340f034231aSEd Maste if (IsHost())
341c0981da4SDimitry Andric return HostInfo::GetOSBuildString();
342c0981da4SDimitry Andric return GetRemoteOSBuildString();
343f034231aSEd Maste }
344f034231aSEd Maste
GetOSKernelDescription()345e3b55780SDimitry Andric std::optional<std::string> Platform::GetOSKernelDescription() {
346f034231aSEd Maste if (IsHost())
347c0981da4SDimitry Andric return HostInfo::GetOSKernelDescription();
348c0981da4SDimitry Andric return GetRemoteOSKernelDescription();
349f034231aSEd Maste }
350f034231aSEd Maste
AddClangModuleCompilationOptions(Target * target,std::vector<std::string> & options)35114f1b3e8SDimitry Andric void Platform::AddClangModuleCompilationOptions(
35214f1b3e8SDimitry Andric Target *target, std::vector<std::string> &options) {
35314f1b3e8SDimitry Andric std::vector<std::string> default_compilation_options = {
35414f1b3e8SDimitry Andric "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
355205afe67SEd Maste
35614f1b3e8SDimitry Andric options.insert(options.end(), default_compilation_options.begin(),
357205afe67SEd Maste default_compilation_options.end());
358205afe67SEd Maste }
359205afe67SEd Maste
GetWorkingDirectory()36014f1b3e8SDimitry Andric FileSpec Platform::GetWorkingDirectory() {
36114f1b3e8SDimitry Andric if (IsHost()) {
36274a628f7SDimitry Andric llvm::SmallString<64> cwd;
36374a628f7SDimitry Andric if (llvm::sys::fs::current_path(cwd))
36494994d37SDimitry Andric return {};
36594994d37SDimitry Andric else {
36694994d37SDimitry Andric FileSpec file_spec(cwd);
36794994d37SDimitry Andric FileSystem::Instance().Resolve(file_spec);
36894994d37SDimitry Andric return file_spec;
36994994d37SDimitry Andric }
37014f1b3e8SDimitry Andric } else {
37186758c71SEd Maste if (!m_working_dir)
37286758c71SEd Maste m_working_dir = GetRemoteWorkingDirectory();
37386758c71SEd Maste return m_working_dir;
37486758c71SEd Maste }
37586758c71SEd Maste }
37686758c71SEd Maste
37714f1b3e8SDimitry Andric struct RecurseCopyBaton {
37886758c71SEd Maste const FileSpec &dst;
37986758c71SEd Maste Platform *platform_ptr;
380b76161e4SDimitry Andric Status error;
38186758c71SEd Maste };
38286758c71SEd Maste
38394994d37SDimitry Andric static FileSystem::EnumerateDirectoryResult
RecurseCopy_Callback(void * baton,llvm::sys::fs::file_type ft,llvm::StringRef path)38474a628f7SDimitry Andric RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
38594994d37SDimitry Andric llvm::StringRef path) {
38686758c71SEd Maste RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
38794994d37SDimitry Andric FileSpec src(path);
38874a628f7SDimitry Andric namespace fs = llvm::sys::fs;
38974a628f7SDimitry Andric switch (ft) {
39074a628f7SDimitry Andric case fs::file_type::fifo_file:
39174a628f7SDimitry Andric case fs::file_type::socket_file:
39286758c71SEd Maste // we have no way to copy pipes and sockets - ignore them and continue
39394994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultNext;
39486758c71SEd Maste break;
39586758c71SEd Maste
39674a628f7SDimitry Andric case fs::file_type::directory_file: {
39786758c71SEd Maste // make the new directory and get in there
39886758c71SEd Maste FileSpec dst_dir = rc_baton->dst;
39986758c71SEd Maste if (!dst_dir.GetFilename())
4007fa27ce4SDimitry Andric dst_dir.SetFilename(src.GetFilename());
401b76161e4SDimitry Andric Status error = rc_baton->platform_ptr->MakeDirectory(
40214f1b3e8SDimitry Andric dst_dir, lldb::eFilePermissionsDirectoryDefault);
40314f1b3e8SDimitry Andric if (error.Fail()) {
40414f1b3e8SDimitry Andric rc_baton->error.SetErrorStringWithFormat(
405e3b55780SDimitry Andric "unable to setup directory %s on remote end",
406e3b55780SDimitry Andric dst_dir.GetPath().c_str());
40794994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
40886758c71SEd Maste }
40986758c71SEd Maste
41086758c71SEd Maste // now recurse
41186758c71SEd Maste std::string src_dir_path(src.GetPath());
41286758c71SEd Maste
413f73363f1SDimitry Andric // Make a filespec that only fills in the directory of a FileSpec so when
414f73363f1SDimitry Andric // we enumerate we can quickly fill in the filename for dst copies
41586758c71SEd Maste FileSpec recurse_dst;
416e3b55780SDimitry Andric recurse_dst.SetDirectory(dst_dir.GetPathAsConstString());
417b76161e4SDimitry Andric RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
418b76161e4SDimitry Andric Status()};
41994994d37SDimitry Andric FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
42014f1b3e8SDimitry Andric RecurseCopy_Callback, &rc_baton2);
42114f1b3e8SDimitry Andric if (rc_baton2.error.Fail()) {
42286758c71SEd Maste rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
42394994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
42486758c71SEd Maste }
42594994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultNext;
42614f1b3e8SDimitry Andric } break;
42786758c71SEd Maste
42874a628f7SDimitry Andric case fs::file_type::symlink_file: {
42986758c71SEd Maste // copy the file and keep going
43086758c71SEd Maste FileSpec dst_file = rc_baton->dst;
43186758c71SEd Maste if (!dst_file.GetFilename())
432e3b55780SDimitry Andric dst_file.SetFilename(src.GetFilename());
43386758c71SEd Maste
4345e95aa85SEd Maste FileSpec src_resolved;
43586758c71SEd Maste
43694994d37SDimitry Andric rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
43786758c71SEd Maste
43886758c71SEd Maste if (rc_baton->error.Fail())
43994994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
44086758c71SEd Maste
44114f1b3e8SDimitry Andric rc_baton->error =
44214f1b3e8SDimitry Andric rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
44386758c71SEd Maste
44486758c71SEd Maste if (rc_baton->error.Fail())
44594994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
44686758c71SEd Maste
44794994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultNext;
44814f1b3e8SDimitry Andric } break;
449f3fbd1c0SDimitry Andric
45074a628f7SDimitry Andric case fs::file_type::regular_file: {
45186758c71SEd Maste // copy the file and keep going
45286758c71SEd Maste FileSpec dst_file = rc_baton->dst;
45386758c71SEd Maste if (!dst_file.GetFilename())
454e3b55780SDimitry Andric dst_file.SetFilename(src.GetFilename());
455b76161e4SDimitry Andric Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
45614f1b3e8SDimitry Andric if (err.Fail()) {
45786758c71SEd Maste rc_baton->error.SetErrorString(err.AsCString());
45894994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
45986758c71SEd Maste }
46094994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultNext;
46114f1b3e8SDimitry Andric } break;
46286758c71SEd Maste
46374a628f7SDimitry Andric default:
46414f1b3e8SDimitry Andric rc_baton->error.SetErrorStringWithFormat(
46514f1b3e8SDimitry Andric "invalid file detected during copy: %s", src.GetPath().c_str());
46694994d37SDimitry Andric return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
46786758c71SEd Maste break;
46886758c71SEd Maste }
46974a628f7SDimitry Andric llvm_unreachable("Unhandled file_type!");
47086758c71SEd Maste }
47186758c71SEd Maste
Install(const FileSpec & src,const FileSpec & dst)472b76161e4SDimitry Andric Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
473b76161e4SDimitry Andric Status error;
47486758c71SEd Maste
475145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
476ead24645SDimitry Andric LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')",
477ead24645SDimitry Andric src.GetPath().c_str(), dst.GetPath().c_str());
47886758c71SEd Maste FileSpec fixed_dst(dst);
47986758c71SEd Maste
48086758c71SEd Maste if (!fixed_dst.GetFilename())
481e3b55780SDimitry Andric fixed_dst.SetFilename(src.GetFilename());
48286758c71SEd Maste
4835e95aa85SEd Maste FileSpec working_dir = GetWorkingDirectory();
48486758c71SEd Maste
48514f1b3e8SDimitry Andric if (dst) {
48614f1b3e8SDimitry Andric if (dst.GetDirectory()) {
48786758c71SEd Maste const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
48814f1b3e8SDimitry Andric if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
489e3b55780SDimitry Andric fixed_dst.SetDirectory(dst.GetDirectory());
49086758c71SEd Maste }
491f73363f1SDimitry Andric // If the fixed destination file doesn't have a directory yet, then we
492f73363f1SDimitry Andric // must have a relative path. We will resolve this relative path against
493f73363f1SDimitry Andric // the platform's working directory
49414f1b3e8SDimitry Andric if (!fixed_dst.GetDirectory()) {
49586758c71SEd Maste FileSpec relative_spec;
49686758c71SEd Maste std::string path;
49714f1b3e8SDimitry Andric if (working_dir) {
4985e95aa85SEd Maste relative_spec = working_dir;
4995e95aa85SEd Maste relative_spec.AppendPathComponent(dst.GetPath());
500e3b55780SDimitry Andric fixed_dst.SetDirectory(relative_spec.GetDirectory());
50114f1b3e8SDimitry Andric } else {
50214f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
50314f1b3e8SDimitry Andric "platform working directory must be valid for relative path '%s'",
50414f1b3e8SDimitry Andric dst.GetPath().c_str());
50586758c71SEd Maste return error;
50686758c71SEd Maste }
50786758c71SEd Maste }
50814f1b3e8SDimitry Andric } else {
50914f1b3e8SDimitry Andric if (working_dir) {
510e3b55780SDimitry Andric fixed_dst.SetDirectory(working_dir.GetPathAsConstString());
51114f1b3e8SDimitry Andric } else {
51214f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
51314f1b3e8SDimitry Andric "platform working directory must be valid for relative path '%s'",
51414f1b3e8SDimitry Andric dst.GetPath().c_str());
51586758c71SEd Maste return error;
51686758c71SEd Maste }
51786758c71SEd Maste }
51814f1b3e8SDimitry Andric } else {
51914f1b3e8SDimitry Andric if (working_dir) {
520e3b55780SDimitry Andric fixed_dst.SetDirectory(working_dir.GetPathAsConstString());
52114f1b3e8SDimitry Andric } else {
52214f1b3e8SDimitry Andric error.SetErrorStringWithFormat("platform working directory must be valid "
52314f1b3e8SDimitry Andric "when destination directory is empty");
52486758c71SEd Maste return error;
52586758c71SEd Maste }
52686758c71SEd Maste }
52786758c71SEd Maste
528ead24645SDimitry Andric LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
52914f1b3e8SDimitry Andric src.GetPath().c_str(), dst.GetPath().c_str(),
53014f1b3e8SDimitry Andric fixed_dst.GetPath().c_str());
53186758c71SEd Maste
53214f1b3e8SDimitry Andric if (GetSupportsRSync()) {
53386758c71SEd Maste error = PutFile(src, dst);
53414f1b3e8SDimitry Andric } else {
53574a628f7SDimitry Andric namespace fs = llvm::sys::fs;
53674a628f7SDimitry Andric switch (fs::get_file_type(src.GetPath(), false)) {
53774a628f7SDimitry Andric case fs::file_type::directory_file: {
53874a628f7SDimitry Andric llvm::sys::fs::remove(fixed_dst.GetPath());
53994994d37SDimitry Andric uint32_t permissions = FileSystem::Instance().GetPermissions(src);
54086758c71SEd Maste if (permissions == 0)
54186758c71SEd Maste permissions = eFilePermissionsDirectoryDefault;
5425e95aa85SEd Maste error = MakeDirectory(fixed_dst, permissions);
54314f1b3e8SDimitry Andric if (error.Success()) {
54486758c71SEd Maste // Make a filespec that only fills in the directory of a FileSpec so
54586758c71SEd Maste // when we enumerate we can quickly fill in the filename for dst copies
54686758c71SEd Maste FileSpec recurse_dst;
547e3b55780SDimitry Andric recurse_dst.SetDirectory(fixed_dst.GetPathAsConstString());
54886758c71SEd Maste std::string src_dir_path(src.GetPath());
549b76161e4SDimitry Andric RecurseCopyBaton baton = {recurse_dst, this, Status()};
55094994d37SDimitry Andric FileSystem::Instance().EnumerateDirectory(
55194994d37SDimitry Andric src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
55286758c71SEd Maste return baton.error;
55386758c71SEd Maste }
55414f1b3e8SDimitry Andric } break;
55586758c71SEd Maste
55674a628f7SDimitry Andric case fs::file_type::regular_file:
55774a628f7SDimitry Andric llvm::sys::fs::remove(fixed_dst.GetPath());
55886758c71SEd Maste error = PutFile(src, fixed_dst);
55986758c71SEd Maste break;
56086758c71SEd Maste
56174a628f7SDimitry Andric case fs::file_type::symlink_file: {
56274a628f7SDimitry Andric llvm::sys::fs::remove(fixed_dst.GetPath());
5635e95aa85SEd Maste FileSpec src_resolved;
56494994d37SDimitry Andric error = FileSystem::Instance().Readlink(src, src_resolved);
56586758c71SEd Maste if (error.Success())
5665e95aa85SEd Maste error = CreateSymlink(dst, src_resolved);
56714f1b3e8SDimitry Andric } break;
56874a628f7SDimitry Andric case fs::file_type::fifo_file:
56986758c71SEd Maste error.SetErrorString("platform install doesn't handle pipes");
57086758c71SEd Maste break;
57174a628f7SDimitry Andric case fs::file_type::socket_file:
57286758c71SEd Maste error.SetErrorString("platform install doesn't handle sockets");
57386758c71SEd Maste break;
57474a628f7SDimitry Andric default:
57514f1b3e8SDimitry Andric error.SetErrorString(
57614f1b3e8SDimitry Andric "platform install doesn't handle non file or directory items");
57786758c71SEd Maste break;
57886758c71SEd Maste }
57986758c71SEd Maste }
58086758c71SEd Maste return error;
58186758c71SEd Maste }
58286758c71SEd Maste
SetWorkingDirectory(const FileSpec & file_spec)58314f1b3e8SDimitry Andric bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
58414f1b3e8SDimitry Andric if (IsHost()) {
585145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
58674a628f7SDimitry Andric LLDB_LOG(log, "{0}", file_spec);
58774a628f7SDimitry Andric if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
58874a628f7SDimitry Andric LLDB_LOG(log, "error: {0}", ec.message());
58986758c71SEd Maste return false;
59074a628f7SDimitry Andric }
59174a628f7SDimitry Andric return true;
59214f1b3e8SDimitry Andric } else {
59386758c71SEd Maste m_working_dir.Clear();
5945e95aa85SEd Maste return SetRemoteWorkingDirectory(file_spec);
59586758c71SEd Maste }
59686758c71SEd Maste }
59786758c71SEd Maste
MakeDirectory(const FileSpec & file_spec,uint32_t permissions)598b76161e4SDimitry Andric Status Platform::MakeDirectory(const FileSpec &file_spec,
599b76161e4SDimitry Andric uint32_t permissions) {
60086758c71SEd Maste if (IsHost())
60174a628f7SDimitry Andric return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
60214f1b3e8SDimitry Andric else {
603b76161e4SDimitry Andric Status error;
604c0981da4SDimitry Andric error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
605c0981da4SDimitry Andric GetPluginName(), LLVM_PRETTY_FUNCTION);
60686758c71SEd Maste return error;
60786758c71SEd Maste }
60886758c71SEd Maste }
60986758c71SEd Maste
GetFilePermissions(const FileSpec & file_spec,uint32_t & file_permissions)610b76161e4SDimitry Andric Status Platform::GetFilePermissions(const FileSpec &file_spec,
61114f1b3e8SDimitry Andric uint32_t &file_permissions) {
61274a628f7SDimitry Andric if (IsHost()) {
61374a628f7SDimitry Andric auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
61474a628f7SDimitry Andric if (Value)
61574a628f7SDimitry Andric file_permissions = Value.get();
616b76161e4SDimitry Andric return Status(Value.getError());
61774a628f7SDimitry Andric } else {
618b76161e4SDimitry Andric Status error;
619c0981da4SDimitry Andric error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
620c0981da4SDimitry Andric GetPluginName(), LLVM_PRETTY_FUNCTION);
62186758c71SEd Maste return error;
62286758c71SEd Maste }
62386758c71SEd Maste }
62486758c71SEd Maste
SetFilePermissions(const FileSpec & file_spec,uint32_t file_permissions)625b76161e4SDimitry Andric Status Platform::SetFilePermissions(const FileSpec &file_spec,
62614f1b3e8SDimitry Andric uint32_t file_permissions) {
62774a628f7SDimitry Andric if (IsHost()) {
62874a628f7SDimitry Andric auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
62974a628f7SDimitry Andric return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
63074a628f7SDimitry Andric } else {
631b76161e4SDimitry Andric Status error;
632c0981da4SDimitry Andric error.SetErrorStringWithFormatv("remote platform {0} doesn't support {1}",
633c0981da4SDimitry Andric GetPluginName(), LLVM_PRETTY_FUNCTION);
63486758c71SEd Maste return error;
63586758c71SEd Maste }
63686758c71SEd Maste }
63786758c71SEd Maste
OpenFile(const FileSpec & file_spec,File::OpenOptions flags,uint32_t mode,Status & error)638145449b1SDimitry Andric user_id_t Platform::OpenFile(const FileSpec &file_spec,
639145449b1SDimitry Andric File::OpenOptions flags, uint32_t mode,
640145449b1SDimitry Andric Status &error) {
641145449b1SDimitry Andric if (IsHost())
642145449b1SDimitry Andric return FileCache::GetInstance().OpenFile(file_spec, flags, mode, error);
643145449b1SDimitry Andric return UINT64_MAX;
644145449b1SDimitry Andric }
645145449b1SDimitry Andric
CloseFile(user_id_t fd,Status & error)646145449b1SDimitry Andric bool Platform::CloseFile(user_id_t fd, Status &error) {
647145449b1SDimitry Andric if (IsHost())
648145449b1SDimitry Andric return FileCache::GetInstance().CloseFile(fd, error);
649145449b1SDimitry Andric return false;
650145449b1SDimitry Andric }
651145449b1SDimitry Andric
GetFileSize(const FileSpec & file_spec)652145449b1SDimitry Andric user_id_t Platform::GetFileSize(const FileSpec &file_spec) {
653145449b1SDimitry Andric if (!IsHost())
654145449b1SDimitry Andric return UINT64_MAX;
655145449b1SDimitry Andric
656145449b1SDimitry Andric uint64_t Size;
657145449b1SDimitry Andric if (llvm::sys::fs::file_size(file_spec.GetPath(), Size))
658145449b1SDimitry Andric return 0;
659145449b1SDimitry Andric return Size;
660145449b1SDimitry Andric }
661145449b1SDimitry Andric
ReadFile(lldb::user_id_t fd,uint64_t offset,void * dst,uint64_t dst_len,Status & error)662145449b1SDimitry Andric uint64_t Platform::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
663145449b1SDimitry Andric uint64_t dst_len, Status &error) {
664145449b1SDimitry Andric if (IsHost())
665145449b1SDimitry Andric return FileCache::GetInstance().ReadFile(fd, offset, dst, dst_len, error);
666145449b1SDimitry Andric error.SetErrorStringWithFormatv(
667145449b1SDimitry Andric "Platform::ReadFile() is not supported in the {0} platform",
668145449b1SDimitry Andric GetPluginName());
669145449b1SDimitry Andric return -1;
670145449b1SDimitry Andric }
671145449b1SDimitry Andric
WriteFile(lldb::user_id_t fd,uint64_t offset,const void * src,uint64_t src_len,Status & error)672145449b1SDimitry Andric uint64_t Platform::WriteFile(lldb::user_id_t fd, uint64_t offset,
673145449b1SDimitry Andric const void *src, uint64_t src_len, Status &error) {
674145449b1SDimitry Andric if (IsHost())
675145449b1SDimitry Andric return FileCache::GetInstance().WriteFile(fd, offset, src, src_len, error);
676145449b1SDimitry Andric error.SetErrorStringWithFormatv(
677145449b1SDimitry Andric "Platform::WriteFile() is not supported in the {0} platform",
678145449b1SDimitry Andric GetPluginName());
679145449b1SDimitry Andric return -1;
680145449b1SDimitry Andric }
681145449b1SDimitry Andric
GetUserIDResolver()682145449b1SDimitry Andric UserIDResolver &Platform::GetUserIDResolver() {
683145449b1SDimitry Andric if (IsHost())
684145449b1SDimitry Andric return HostInfo::GetUserIDResolver();
685145449b1SDimitry Andric return UserIDResolver::GetNoopResolver();
686145449b1SDimitry Andric }
687f034231aSEd Maste
GetHostname()68814f1b3e8SDimitry Andric const char *Platform::GetHostname() {
689f034231aSEd Maste if (IsHost())
6900cac4ca3SEd Maste return "127.0.0.1";
691f034231aSEd Maste
692145449b1SDimitry Andric if (m_hostname.empty())
693f3fbd1c0SDimitry Andric return nullptr;
694145449b1SDimitry Andric return m_hostname.c_str();
695f034231aSEd Maste }
696f034231aSEd Maste
GetFullNameForDylib(ConstString basename)69714f1b3e8SDimitry Andric ConstString Platform::GetFullNameForDylib(ConstString basename) {
698e81d9d49SDimitry Andric return basename;
699e81d9d49SDimitry Andric }
700e81d9d49SDimitry Andric
SetRemoteWorkingDirectory(const FileSpec & working_dir)70114f1b3e8SDimitry Andric bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
702145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
703ead24645SDimitry Andric LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')",
704e3b55780SDimitry Andric working_dir.GetPath().c_str());
7055e95aa85SEd Maste m_working_dir = working_dir;
70686758c71SEd Maste return true;
70786758c71SEd Maste }
70886758c71SEd Maste
SetOSVersion(llvm::VersionTuple version)709f73363f1SDimitry Andric bool Platform::SetOSVersion(llvm::VersionTuple version) {
71014f1b3e8SDimitry Andric if (IsHost()) {
711f73363f1SDimitry Andric // We don't need anyone setting the OS version for the host platform, we
712f73363f1SDimitry Andric // should be able to figure it out by calling HostInfo::GetOSVersion(...).
713f034231aSEd Maste return false;
71414f1b3e8SDimitry Andric } else {
715f73363f1SDimitry Andric // We have a remote platform, allow setting the target OS version if we
716f73363f1SDimitry Andric // aren't connected, since if we are connected, we should be able to
717f034231aSEd Maste // request the remote OS version from the connected platform.
718f034231aSEd Maste if (IsConnected())
719f034231aSEd Maste return false;
72014f1b3e8SDimitry Andric else {
721f73363f1SDimitry Andric // We aren't connected and we might want to set the OS version ahead of
722f73363f1SDimitry Andric // time before we connect so we can peruse files and use a local SDK or
723f73363f1SDimitry Andric // PDK cache of support files to disassemble or do other things.
724f73363f1SDimitry Andric m_os_version = version;
725f034231aSEd Maste return true;
726f034231aSEd Maste }
727f034231aSEd Maste }
728f034231aSEd Maste return false;
729f034231aSEd Maste }
730f034231aSEd Maste
731b76161e4SDimitry Andric Status
ResolveExecutable(const ModuleSpec & module_spec,lldb::ModuleSP & exe_module_sp,const FileSpecList * module_search_paths_ptr)732b76161e4SDimitry Andric Platform::ResolveExecutable(const ModuleSpec &module_spec,
733f034231aSEd Maste lldb::ModuleSP &exe_module_sp,
73414f1b3e8SDimitry Andric const FileSpecList *module_search_paths_ptr) {
735c0981da4SDimitry Andric
736c0981da4SDimitry Andric // We may connect to a process and use the provided executable (Don't use
737c0981da4SDimitry Andric // local $PATH).
738c0981da4SDimitry Andric ModuleSpec resolved_module_spec(module_spec);
739c0981da4SDimitry Andric
740c0981da4SDimitry Andric // Resolve any executable within a bundle on MacOSX
741c0981da4SDimitry Andric Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
742c0981da4SDimitry Andric
743ac9a064cSDimitry Andric if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()) &&
744ac9a064cSDimitry Andric !module_spec.GetUUID().IsValid())
745ac9a064cSDimitry Andric return Status::createWithFormat("'{0}' does not exist",
746ac9a064cSDimitry Andric resolved_module_spec.GetFileSpec());
747ac9a064cSDimitry Andric
748c0981da4SDimitry Andric if (resolved_module_spec.GetArchitecture().IsValid() ||
749c0981da4SDimitry Andric resolved_module_spec.GetUUID().IsValid()) {
750ac9a064cSDimitry Andric Status error =
751ac9a064cSDimitry Andric ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
752ac9a064cSDimitry Andric module_search_paths_ptr, nullptr, nullptr);
753c0981da4SDimitry Andric
754c0981da4SDimitry Andric if (exe_module_sp && exe_module_sp->GetObjectFile())
755c0981da4SDimitry Andric return error;
756c0981da4SDimitry Andric exe_module_sp.reset();
757c0981da4SDimitry Andric }
758ac9a064cSDimitry Andric // No valid architecture was specified or the exact arch wasn't found.
759ac9a064cSDimitry Andric // Ask the platform for the architectures that we should be using (in the
760ac9a064cSDimitry Andric // correct order) and see if we can find a match that way.
761c0981da4SDimitry Andric StreamString arch_names;
762c0981da4SDimitry Andric llvm::ListSeparator LS;
763145449b1SDimitry Andric ArchSpec process_host_arch;
764ac9a064cSDimitry Andric Status error;
765145449b1SDimitry Andric for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
766c0981da4SDimitry Andric resolved_module_spec.GetArchitecture() = arch;
767ac9a064cSDimitry Andric error =
768ac9a064cSDimitry Andric ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
769ac9a064cSDimitry Andric module_search_paths_ptr, nullptr, nullptr);
770c0981da4SDimitry Andric if (error.Success()) {
771c0981da4SDimitry Andric if (exe_module_sp && exe_module_sp->GetObjectFile())
772c0981da4SDimitry Andric break;
773c0981da4SDimitry Andric error.SetErrorToGenericError();
774c0981da4SDimitry Andric }
775c0981da4SDimitry Andric
776c0981da4SDimitry Andric arch_names << LS << arch.GetArchitectureName();
777c0981da4SDimitry Andric }
778c0981da4SDimitry Andric
779ac9a064cSDimitry Andric if (exe_module_sp && error.Success())
780ac9a064cSDimitry Andric return {};
781ac9a064cSDimitry Andric
782ac9a064cSDimitry Andric if (!FileSystem::Instance().Readable(resolved_module_spec.GetFileSpec()))
783ac9a064cSDimitry Andric return Status::createWithFormat("'{0}' is not readable",
784ac9a064cSDimitry Andric resolved_module_spec.GetFileSpec());
785ac9a064cSDimitry Andric
786ac9a064cSDimitry Andric if (!ObjectFile::IsObjectFile(resolved_module_spec.GetFileSpec()))
787ac9a064cSDimitry Andric return Status::createWithFormat("'{0}' is not a valid executable",
788ac9a064cSDimitry Andric resolved_module_spec.GetFileSpec());
789ac9a064cSDimitry Andric
790ac9a064cSDimitry Andric return Status::createWithFormat(
791c0981da4SDimitry Andric "'{0}' doesn't contain any '{1}' platform architectures: {2}",
792c0981da4SDimitry Andric resolved_module_spec.GetFileSpec(), GetPluginName(),
793c0981da4SDimitry Andric arch_names.GetData());
794c0981da4SDimitry Andric }
795c0981da4SDimitry Andric
ResolveSymbolFile(Target & target,const ModuleSpec & sym_spec,FileSpec & sym_file)796b76161e4SDimitry Andric Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
79714f1b3e8SDimitry Andric FileSpec &sym_file) {
798b76161e4SDimitry Andric Status error;
79994994d37SDimitry Andric if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
800f034231aSEd Maste sym_file = sym_spec.GetSymbolFileSpec();
801f034231aSEd Maste else
802f034231aSEd Maste error.SetErrorString("unable to resolve symbol file");
803f034231aSEd Maste return error;
804f034231aSEd Maste }
805f034231aSEd Maste
ResolveRemotePath(const FileSpec & platform_path,FileSpec & resolved_platform_path)80614f1b3e8SDimitry Andric bool Platform::ResolveRemotePath(const FileSpec &platform_path,
80714f1b3e8SDimitry Andric FileSpec &resolved_platform_path) {
808f034231aSEd Maste resolved_platform_path = platform_path;
80994994d37SDimitry Andric FileSystem::Instance().Resolve(resolved_platform_path);
81094994d37SDimitry Andric return true;
811f034231aSEd Maste }
812f034231aSEd Maste
GetSystemArchitecture()81314f1b3e8SDimitry Andric const ArchSpec &Platform::GetSystemArchitecture() {
81414f1b3e8SDimitry Andric if (IsHost()) {
81514f1b3e8SDimitry Andric if (!m_system_arch.IsValid()) {
816f034231aSEd Maste // We have a local host platform
8170cac4ca3SEd Maste m_system_arch = HostInfo::GetArchitecture();
818f034231aSEd Maste m_system_arch_set_while_connected = m_system_arch.IsValid();
819f034231aSEd Maste }
82014f1b3e8SDimitry Andric } else {
821f73363f1SDimitry Andric // We have a remote platform. We can only fetch the remote system
822f73363f1SDimitry Andric // architecture if we are connected, and we don't want to do it more than
823f73363f1SDimitry Andric // once.
824f034231aSEd Maste
825f034231aSEd Maste const bool is_connected = IsConnected();
826f034231aSEd Maste
827f034231aSEd Maste bool fetch = false;
82814f1b3e8SDimitry Andric if (m_system_arch.IsValid()) {
829f73363f1SDimitry Andric // We have valid OS version info, check to make sure it wasn't manually
830f73363f1SDimitry Andric // set prior to connecting. If it was manually set prior to connecting,
831f73363f1SDimitry Andric // then lets fetch the actual OS version info if we are now connected.
832f034231aSEd Maste if (is_connected && !m_system_arch_set_while_connected)
833f034231aSEd Maste fetch = true;
83414f1b3e8SDimitry Andric } else {
835f034231aSEd Maste // We don't have valid OS version info, fetch it if we are connected
836f034231aSEd Maste fetch = is_connected;
837f034231aSEd Maste }
838f034231aSEd Maste
83914f1b3e8SDimitry Andric if (fetch) {
840f034231aSEd Maste m_system_arch = GetRemoteSystemArchitecture();
841f034231aSEd Maste m_system_arch_set_while_connected = m_system_arch.IsValid();
842f034231aSEd Maste }
843f034231aSEd Maste }
844f034231aSEd Maste return m_system_arch;
845f034231aSEd Maste }
846f034231aSEd Maste
GetAugmentedArchSpec(llvm::StringRef triple)847ef5d0b5eSDimitry Andric ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
848ef5d0b5eSDimitry Andric if (triple.empty())
849ef5d0b5eSDimitry Andric return ArchSpec();
850ef5d0b5eSDimitry Andric llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
851ef5d0b5eSDimitry Andric if (!ArchSpec::ContainsOnlyArch(normalized_triple))
852ef5d0b5eSDimitry Andric return ArchSpec(triple);
853ef5d0b5eSDimitry Andric
854ef5d0b5eSDimitry Andric if (auto kind = HostInfo::ParseArchitectureKind(triple))
855ef5d0b5eSDimitry Andric return HostInfo::GetArchitecture(*kind);
856ef5d0b5eSDimitry Andric
857ef5d0b5eSDimitry Andric ArchSpec compatible_arch;
858ef5d0b5eSDimitry Andric ArchSpec raw_arch(triple);
859e3b55780SDimitry Andric if (!IsCompatibleArchitecture(raw_arch, {}, ArchSpec::CompatibleMatch,
860e3b55780SDimitry Andric &compatible_arch))
861ef5d0b5eSDimitry Andric return raw_arch;
862ef5d0b5eSDimitry Andric
863ef5d0b5eSDimitry Andric if (!compatible_arch.IsValid())
864ef5d0b5eSDimitry Andric return ArchSpec(normalized_triple);
865ef5d0b5eSDimitry Andric
866ef5d0b5eSDimitry Andric const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
867ef5d0b5eSDimitry Andric if (normalized_triple.getVendorName().empty())
868ef5d0b5eSDimitry Andric normalized_triple.setVendor(compatible_triple.getVendor());
869ef5d0b5eSDimitry Andric if (normalized_triple.getOSName().empty())
870ef5d0b5eSDimitry Andric normalized_triple.setOS(compatible_triple.getOS());
871ef5d0b5eSDimitry Andric if (normalized_triple.getEnvironmentName().empty())
872ef5d0b5eSDimitry Andric normalized_triple.setEnvironment(compatible_triple.getEnvironment());
873ef5d0b5eSDimitry Andric return ArchSpec(normalized_triple);
874ef5d0b5eSDimitry Andric }
875ef5d0b5eSDimitry Andric
ConnectRemote(Args & args)876b76161e4SDimitry Andric Status Platform::ConnectRemote(Args &args) {
877b76161e4SDimitry Andric Status error;
878f034231aSEd Maste if (IsHost())
879c0981da4SDimitry Andric error.SetErrorStringWithFormatv(
880c0981da4SDimitry Andric "The currently selected platform ({0}) is "
88114f1b3e8SDimitry Andric "the host platform and is always connected.",
882c0981da4SDimitry Andric GetPluginName());
883f034231aSEd Maste else
884c0981da4SDimitry Andric error.SetErrorStringWithFormatv(
885c0981da4SDimitry Andric "Platform::ConnectRemote() is not supported by {0}", GetPluginName());
886f034231aSEd Maste return error;
887f034231aSEd Maste }
888f034231aSEd Maste
DisconnectRemote()889b76161e4SDimitry Andric Status Platform::DisconnectRemote() {
890b76161e4SDimitry Andric Status error;
891f034231aSEd Maste if (IsHost())
892c0981da4SDimitry Andric error.SetErrorStringWithFormatv(
893c0981da4SDimitry Andric "The currently selected platform ({0}) is "
89414f1b3e8SDimitry Andric "the host platform and is always connected.",
895c0981da4SDimitry Andric GetPluginName());
896f034231aSEd Maste else
897c0981da4SDimitry Andric error.SetErrorStringWithFormatv(
898c0981da4SDimitry Andric "Platform::DisconnectRemote() is not supported by {0}",
899c0981da4SDimitry Andric GetPluginName());
900f034231aSEd Maste return error;
901f034231aSEd Maste }
902f034231aSEd Maste
GetProcessInfo(lldb::pid_t pid,ProcessInstanceInfo & process_info)90314f1b3e8SDimitry Andric bool Platform::GetProcessInfo(lldb::pid_t pid,
90414f1b3e8SDimitry Andric ProcessInstanceInfo &process_info) {
905f73363f1SDimitry Andric // Take care of the host case so that each subclass can just call this
906f73363f1SDimitry Andric // function to get the host functionality.
907f034231aSEd Maste if (IsHost())
908f034231aSEd Maste return Host::GetProcessInfo(pid, process_info);
909f034231aSEd Maste return false;
910f034231aSEd Maste }
911f034231aSEd Maste
FindProcesses(const ProcessInstanceInfoMatch & match_info,ProcessInstanceInfoList & process_infos)91214f1b3e8SDimitry Andric uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
91314f1b3e8SDimitry Andric ProcessInstanceInfoList &process_infos) {
914f73363f1SDimitry Andric // Take care of the host case so that each subclass can just call this
915f73363f1SDimitry Andric // function to get the host functionality.
916f034231aSEd Maste uint32_t match_count = 0;
917f034231aSEd Maste if (IsHost())
918f034231aSEd Maste match_count = Host::FindProcesses(match_info, process_infos);
919f034231aSEd Maste return match_count;
920f034231aSEd Maste }
921f034231aSEd Maste
GetAllProcesses()922b1c73532SDimitry Andric ProcessInstanceInfoList Platform::GetAllProcesses() {
923b1c73532SDimitry Andric ProcessInstanceInfoList processes;
924b1c73532SDimitry Andric ProcessInstanceInfoMatch match;
925b1c73532SDimitry Andric assert(match.MatchAllProcesses());
926b1c73532SDimitry Andric FindProcesses(match, processes);
927b1c73532SDimitry Andric return processes;
928b1c73532SDimitry Andric }
929b1c73532SDimitry Andric
LaunchProcess(ProcessLaunchInfo & launch_info)930b76161e4SDimitry Andric Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
931b76161e4SDimitry Andric Status error;
932145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
933ead24645SDimitry Andric LLDB_LOGF(log, "Platform::%s()", __FUNCTION__);
934205afe67SEd Maste
935f73363f1SDimitry Andric // Take care of the host case so that each subclass can just call this
936f73363f1SDimitry Andric // function to get the host functionality.
93714f1b3e8SDimitry Andric if (IsHost()) {
938f034231aSEd Maste if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
939f034231aSEd Maste launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
940f034231aSEd Maste
94114f1b3e8SDimitry Andric if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
942f034231aSEd Maste const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
943f034231aSEd Maste const bool first_arg_is_full_shell_command = false;
944f21a844fSEd Maste uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
94514f1b3e8SDimitry Andric if (log) {
946205afe67SEd Maste const FileSpec &shell = launch_info.GetShell();
9475f29bb8aSDimitry Andric std::string shell_str = (shell) ? shell.GetPath() : "<null>";
948ead24645SDimitry Andric LLDB_LOGF(log,
94914f1b3e8SDimitry Andric "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
95014f1b3e8SDimitry Andric ", shell is '%s'",
9515f29bb8aSDimitry Andric __FUNCTION__, num_resumes, shell_str.c_str());
952205afe67SEd Maste }
953205afe67SEd Maste
95414f1b3e8SDimitry Andric if (!launch_info.ConvertArgumentsForLaunchingInShell(
955b60736ecSDimitry Andric error, will_debug, first_arg_is_full_shell_command, num_resumes))
956f034231aSEd Maste return error;
95714f1b3e8SDimitry Andric } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
9585e95aa85SEd Maste error = ShellExpandArguments(launch_info);
95914f1b3e8SDimitry Andric if (error.Fail()) {
96014f1b3e8SDimitry Andric error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
96114f1b3e8SDimitry Andric "consider launching with 'process "
96214f1b3e8SDimitry Andric "launch'.",
963f3fbd1c0SDimitry Andric error.AsCString("unknown"));
9645e95aa85SEd Maste return error;
9655e95aa85SEd Maste }
966f3fbd1c0SDimitry Andric }
967f034231aSEd Maste
968ead24645SDimitry Andric LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32,
96914f1b3e8SDimitry Andric __FUNCTION__, launch_info.GetResumeCount());
970205afe67SEd Maste
971f034231aSEd Maste error = Host::LaunchProcess(launch_info);
97214f1b3e8SDimitry Andric } else
97314f1b3e8SDimitry Andric error.SetErrorString(
97414f1b3e8SDimitry Andric "base lldb_private::Platform class can't launch remote processes");
975f034231aSEd Maste return error;
976f034231aSEd Maste }
977f034231aSEd Maste
ShellExpandArguments(ProcessLaunchInfo & launch_info)978b76161e4SDimitry Andric Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
9795e95aa85SEd Maste if (IsHost())
9805e95aa85SEd Maste return Host::ShellExpandArguments(launch_info);
981b76161e4SDimitry Andric return Status("base lldb_private::Platform class can't expand arguments");
9825e95aa85SEd Maste }
9835e95aa85SEd Maste
KillProcess(const lldb::pid_t pid)984b76161e4SDimitry Andric Status Platform::KillProcess(const lldb::pid_t pid) {
985145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
986ead24645SDimitry Andric LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
98712bd4897SEd Maste
98814f1b3e8SDimitry Andric if (!IsHost()) {
989b76161e4SDimitry Andric return Status(
990c0981da4SDimitry Andric "base lldb_private::Platform class can't kill remote processes");
9915e95aa85SEd Maste }
992c0981da4SDimitry Andric Host::Kill(pid, SIGKILL);
993b76161e4SDimitry Andric return Status();
99412bd4897SEd Maste }
99512bd4897SEd Maste
DebugProcess(ProcessLaunchInfo & launch_info,Debugger & debugger,Target & target,Status & error)996c0981da4SDimitry Andric lldb::ProcessSP Platform::DebugProcess(ProcessLaunchInfo &launch_info,
997c0981da4SDimitry Andric Debugger &debugger, Target &target,
998b76161e4SDimitry Andric Status &error) {
999145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
10007fa27ce4SDimitry Andric LLDB_LOG(log, "target = {0}", &target);
1001205afe67SEd Maste
1002f034231aSEd Maste ProcessSP process_sp;
1003f034231aSEd Maste // Make sure we stop at the entry point
1004f034231aSEd Maste launch_info.GetFlags().Set(eLaunchFlagDebug);
1005f034231aSEd Maste // We always launch the process we are going to debug in a separate process
1006f73363f1SDimitry Andric // group, since then we can handle ^C interrupts ourselves w/o having to
1007f73363f1SDimitry Andric // worry about the target getting them as well.
1008f034231aSEd Maste launch_info.SetLaunchInSeparateProcessGroup(true);
1009f034231aSEd Maste
101014f1b3e8SDimitry Andric // Allow any StructuredData process-bound plugins to adjust the launch info
101114f1b3e8SDimitry Andric // if needed
101214f1b3e8SDimitry Andric size_t i = 0;
101314f1b3e8SDimitry Andric bool iteration_complete = false;
1014f73363f1SDimitry Andric // Note iteration can't simply go until a nullptr callback is returned, as it
1015f73363f1SDimitry Andric // is valid for a plugin to not supply a filter.
101614f1b3e8SDimitry Andric auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
101714f1b3e8SDimitry Andric for (auto filter_callback = get_filter_func(i, iteration_complete);
101814f1b3e8SDimitry Andric !iteration_complete;
101914f1b3e8SDimitry Andric filter_callback = get_filter_func(++i, iteration_complete)) {
102014f1b3e8SDimitry Andric if (filter_callback) {
1021f73363f1SDimitry Andric // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
1022c0981da4SDimitry Andric error = (*filter_callback)(launch_info, &target);
102314f1b3e8SDimitry Andric if (!error.Success()) {
1024ead24645SDimitry Andric LLDB_LOGF(log,
1025ead24645SDimitry Andric "Platform::%s() StructuredDataPlugin launch "
102614f1b3e8SDimitry Andric "filter failed.",
102714f1b3e8SDimitry Andric __FUNCTION__);
102814f1b3e8SDimitry Andric return process_sp;
102914f1b3e8SDimitry Andric }
103014f1b3e8SDimitry Andric }
103114f1b3e8SDimitry Andric }
103214f1b3e8SDimitry Andric
103314f1b3e8SDimitry Andric error = LaunchProcess(launch_info);
103414f1b3e8SDimitry Andric if (error.Success()) {
1035ead24645SDimitry Andric LLDB_LOGF(log,
1036ead24645SDimitry Andric "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")",
103714f1b3e8SDimitry Andric __FUNCTION__, launch_info.GetProcessID());
103814f1b3e8SDimitry Andric if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1039f034231aSEd Maste ProcessAttachInfo attach_info(launch_info);
1040c0981da4SDimitry Andric process_sp = Attach(attach_info, debugger, &target, error);
104114f1b3e8SDimitry Andric if (process_sp) {
1042c0981da4SDimitry Andric LLDB_LOG(log, "Attach() succeeded, Process plugin: {0}",
1043c0981da4SDimitry Andric process_sp->GetPluginName());
1044866dcdacSEd Maste launch_info.SetHijackListener(attach_info.GetHijackListener());
1045866dcdacSEd Maste
1046f034231aSEd Maste // Since we attached to the process, it will think it needs to detach
1047f034231aSEd Maste // if the process object just goes away without an explicit call to
1048f034231aSEd Maste // Process::Kill() or Process::Detach(), so let it know to kill the
1049f034231aSEd Maste // process if this happens.
1050f034231aSEd Maste process_sp->SetShouldDetach(false);
1051f034231aSEd Maste
1052f73363f1SDimitry Andric // If we didn't have any file actions, the pseudo terminal might have
1053cfca06d7SDimitry Andric // been used where the secondary side was given as the file to open for
1054c0981da4SDimitry Andric // stdin/out/err after we have already opened the primary so we can
1055f73363f1SDimitry Andric // read/write stdin/out/err.
1056cfca06d7SDimitry Andric int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
1057ef5d0b5eSDimitry Andric if (pty_fd != PseudoTerminal::invalid_fd) {
1058f034231aSEd Maste process_sp->SetSTDIOFileDescriptor(pty_fd);
1059f034231aSEd Maste }
106014f1b3e8SDimitry Andric } else {
1061ead24645SDimitry Andric LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__,
106214f1b3e8SDimitry Andric error.AsCString());
1063f034231aSEd Maste }
106414f1b3e8SDimitry Andric } else {
1065ead24645SDimitry Andric LLDB_LOGF(log,
1066ead24645SDimitry Andric "Platform::%s LaunchProcess() returned launch_info with "
106714f1b3e8SDimitry Andric "invalid process id",
106814f1b3e8SDimitry Andric __FUNCTION__);
1069205afe67SEd Maste }
107014f1b3e8SDimitry Andric } else {
1071ead24645SDimitry Andric LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
107214f1b3e8SDimitry Andric error.AsCString());
1073205afe67SEd Maste }
1074205afe67SEd Maste
1075f034231aSEd Maste return process_sp;
1076f034231aSEd Maste }
1077f034231aSEd Maste
1078c0981da4SDimitry Andric std::vector<ArchSpec>
CreateArchList(llvm::ArrayRef<llvm::Triple::ArchType> archs,llvm::Triple::OSType os)1079c0981da4SDimitry Andric Platform::CreateArchList(llvm::ArrayRef<llvm::Triple::ArchType> archs,
1080c0981da4SDimitry Andric llvm::Triple::OSType os) {
1081c0981da4SDimitry Andric std::vector<ArchSpec> list;
1082c0981da4SDimitry Andric for(auto arch : archs) {
1083c0981da4SDimitry Andric llvm::Triple triple;
1084c0981da4SDimitry Andric triple.setArch(arch);
1085c0981da4SDimitry Andric triple.setOS(os);
1086c0981da4SDimitry Andric list.push_back(ArchSpec(triple));
1087c0981da4SDimitry Andric }
1088c0981da4SDimitry Andric return list;
1089c0981da4SDimitry Andric }
1090c0981da4SDimitry Andric
1091f034231aSEd Maste /// Lets a platform answer if it is compatible with a given
1092f034231aSEd Maste /// architecture and the target triple contained within.
IsCompatibleArchitecture(const ArchSpec & arch,const ArchSpec & process_host_arch,ArchSpec::MatchType match,ArchSpec * compatible_arch_ptr)109314f1b3e8SDimitry Andric bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1094145449b1SDimitry Andric const ArchSpec &process_host_arch,
1095e3b55780SDimitry Andric ArchSpec::MatchType match,
109614f1b3e8SDimitry Andric ArchSpec *compatible_arch_ptr) {
1097f034231aSEd Maste // If the architecture is invalid, we must answer true...
109814f1b3e8SDimitry Andric if (arch.IsValid()) {
1099f034231aSEd Maste ArchSpec platform_arch;
1100145449b1SDimitry Andric for (const ArchSpec &platform_arch :
1101145449b1SDimitry Andric GetSupportedArchitectures(process_host_arch)) {
1102e3b55780SDimitry Andric if (arch.IsMatch(platform_arch, match)) {
1103f034231aSEd Maste if (compatible_arch_ptr)
1104f034231aSEd Maste *compatible_arch_ptr = platform_arch;
1105f034231aSEd Maste return true;
1106f034231aSEd Maste }
1107f034231aSEd Maste }
1108f034231aSEd Maste }
1109f034231aSEd Maste if (compatible_arch_ptr)
1110f034231aSEd Maste compatible_arch_ptr->Clear();
1111f034231aSEd Maste return false;
1112f034231aSEd Maste }
1113f034231aSEd Maste
PutFile(const FileSpec & source,const FileSpec & destination,uint32_t uid,uint32_t gid)1114b76161e4SDimitry Andric Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
111514f1b3e8SDimitry Andric uint32_t uid, uint32_t gid) {
1116145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
1117ead24645SDimitry Andric LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
111812bd4897SEd Maste
1119ead24645SDimitry Andric auto source_open_options =
1120c0981da4SDimitry Andric File::eOpenOptionReadOnly | File::eOpenOptionCloseOnExec;
112174a628f7SDimitry Andric namespace fs = llvm::sys::fs;
112274a628f7SDimitry Andric if (fs::is_symlink_file(source.GetPath()))
1123f3fbd1c0SDimitry Andric source_open_options |= File::eOpenOptionDontFollowSymlinks;
112412bd4897SEd Maste
1125ead24645SDimitry Andric auto source_file = FileSystem::Instance().Open(source, source_open_options,
1126ead24645SDimitry Andric lldb::eFilePermissionsUserRW);
1127ead24645SDimitry Andric if (!source_file)
1128ead24645SDimitry Andric return Status(source_file.takeError());
1129ead24645SDimitry Andric Status error;
1130ac9a064cSDimitry Andric
1131ac9a064cSDimitry Andric bool requires_upload = true;
1132ac9a064cSDimitry Andric llvm::ErrorOr<llvm::MD5::MD5Result> remote_md5 = CalculateMD5(destination);
1133ac9a064cSDimitry Andric if (std::error_code ec = remote_md5.getError()) {
1134ac9a064cSDimitry Andric LLDB_LOG(log, "[PutFile] couldn't get md5 sum of destination: {0}",
1135ac9a064cSDimitry Andric ec.message());
1136ac9a064cSDimitry Andric } else {
1137ac9a064cSDimitry Andric llvm::ErrorOr<llvm::MD5::MD5Result> local_md5 =
1138ac9a064cSDimitry Andric llvm::sys::fs::md5_contents(source.GetPath());
1139ac9a064cSDimitry Andric if (std::error_code ec = local_md5.getError()) {
1140ac9a064cSDimitry Andric LLDB_LOG(log, "[PutFile] couldn't get md5 sum of source: {0}",
1141ac9a064cSDimitry Andric ec.message());
1142ac9a064cSDimitry Andric } else {
1143ac9a064cSDimitry Andric LLDB_LOGF(log, "[PutFile] destination md5: %016" PRIx64 "%016" PRIx64,
1144ac9a064cSDimitry Andric remote_md5->high(), remote_md5->low());
1145ac9a064cSDimitry Andric LLDB_LOGF(log, "[PutFile] local md5: %016" PRIx64 "%016" PRIx64,
1146ac9a064cSDimitry Andric local_md5->high(), local_md5->low());
1147ac9a064cSDimitry Andric requires_upload = *remote_md5 != *local_md5;
1148ac9a064cSDimitry Andric }
1149ac9a064cSDimitry Andric }
1150ac9a064cSDimitry Andric
1151ac9a064cSDimitry Andric if (!requires_upload) {
1152ac9a064cSDimitry Andric LLDB_LOGF(log, "[PutFile] skipping PutFile because md5sums match");
1153ac9a064cSDimitry Andric return error;
1154ac9a064cSDimitry Andric }
1155ac9a064cSDimitry Andric
1156ead24645SDimitry Andric uint32_t permissions = source_file.get()->GetPermissions(error);
115712bd4897SEd Maste if (permissions == 0)
1158ac9a064cSDimitry Andric permissions = lldb::eFilePermissionsUserRWX;
115912bd4897SEd Maste
116014f1b3e8SDimitry Andric lldb::user_id_t dest_file = OpenFile(
1161c0981da4SDimitry Andric destination, File::eOpenOptionCanCreate | File::eOpenOptionWriteOnly |
116214f1b3e8SDimitry Andric File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
116314f1b3e8SDimitry Andric permissions, error);
1164ead24645SDimitry Andric LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
116512bd4897SEd Maste
116612bd4897SEd Maste if (error.Fail())
116712bd4897SEd Maste return error;
116812bd4897SEd Maste if (dest_file == UINT64_MAX)
1169b76161e4SDimitry Andric return Status("unable to open target file");
1170145449b1SDimitry Andric lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
117112bd4897SEd Maste uint64_t offset = 0;
117214f1b3e8SDimitry Andric for (;;) {
117312bd4897SEd Maste size_t bytes_read = buffer_sp->GetByteSize();
1174ead24645SDimitry Andric error = source_file.get()->Read(buffer_sp->GetBytes(), bytes_read);
117512bd4897SEd Maste if (error.Fail() || bytes_read == 0)
117612bd4897SEd Maste break;
117712bd4897SEd Maste
117814f1b3e8SDimitry Andric const uint64_t bytes_written =
117914f1b3e8SDimitry Andric WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
118012bd4897SEd Maste if (error.Fail())
118112bd4897SEd Maste break;
118212bd4897SEd Maste
118312bd4897SEd Maste offset += bytes_written;
118414f1b3e8SDimitry Andric if (bytes_written != bytes_read) {
1185f73363f1SDimitry Andric // We didn't write the correct number of bytes, so adjust the file
1186f73363f1SDimitry Andric // position in the source file we are reading from...
1187ead24645SDimitry Andric source_file.get()->SeekFromStart(offset);
118812bd4897SEd Maste }
118912bd4897SEd Maste }
119012bd4897SEd Maste CloseFile(dest_file, error);
119112bd4897SEd Maste
119212bd4897SEd Maste if (uid == UINT32_MAX && gid == UINT32_MAX)
119312bd4897SEd Maste return error;
119412bd4897SEd Maste
119512bd4897SEd Maste // TODO: ChownFile?
119612bd4897SEd Maste
1197f21a844fSEd Maste return error;
1198f21a844fSEd Maste }
1199f21a844fSEd Maste
GetFile(const FileSpec & source,const FileSpec & destination)1200b76161e4SDimitry Andric Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1201b76161e4SDimitry Andric Status error("unimplemented");
1202f21a844fSEd Maste return error;
1203f21a844fSEd Maste }
1204f21a844fSEd Maste
1205b76161e4SDimitry Andric Status
CreateSymlink(const FileSpec & src,const FileSpec & dst)1206b76161e4SDimitry Andric Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
12075e95aa85SEd Maste const FileSpec &dst) // The symlink points to dst
120886758c71SEd Maste {
1209145449b1SDimitry Andric if (IsHost())
1210145449b1SDimitry Andric return FileSystem::Instance().Symlink(src, dst);
1211145449b1SDimitry Andric return Status("unimplemented");
121286758c71SEd Maste }
121386758c71SEd Maste
GetFileExists(const lldb_private::FileSpec & file_spec)121414f1b3e8SDimitry Andric bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1215145449b1SDimitry Andric if (IsHost())
1216145449b1SDimitry Andric return FileSystem::Instance().Exists(file_spec);
1217f21a844fSEd Maste return false;
1218f21a844fSEd Maste }
1219f21a844fSEd Maste
Unlink(const FileSpec & path)1220b76161e4SDimitry Andric Status Platform::Unlink(const FileSpec &path) {
1221145449b1SDimitry Andric if (IsHost())
1222145449b1SDimitry Andric return llvm::sys::fs::remove(path.GetPath());
1223145449b1SDimitry Andric return Status("unimplemented");
122486758c71SEd Maste }
122586758c71SEd Maste
GetMmapArgumentList(const ArchSpec & arch,addr_t addr,addr_t length,unsigned prot,unsigned flags,addr_t fd,addr_t offset)1226ef5d0b5eSDimitry Andric MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1227ef5d0b5eSDimitry Andric addr_t length, unsigned prot,
1228ef5d0b5eSDimitry Andric unsigned flags, addr_t fd,
1229ef5d0b5eSDimitry Andric addr_t offset) {
12305e95aa85SEd Maste uint64_t flags_platform = 0;
12315e95aa85SEd Maste if (flags & eMmapFlagsPrivate)
12325e95aa85SEd Maste flags_platform |= MAP_PRIVATE;
12335e95aa85SEd Maste if (flags & eMmapFlagsAnon)
12345e95aa85SEd Maste flags_platform |= MAP_ANON;
1235ef5d0b5eSDimitry Andric
1236ef5d0b5eSDimitry Andric MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1237ef5d0b5eSDimitry Andric return args;
12385e95aa85SEd Maste }
123986758c71SEd Maste
RunShellCommand(llvm::StringRef command,const FileSpec & working_dir,int * status_ptr,int * signo_ptr,std::string * command_output,const Timeout<std::micro> & timeout)1240b76161e4SDimitry Andric lldb_private::Status Platform::RunShellCommand(
1241b60736ecSDimitry Andric llvm::StringRef command,
1242b60736ecSDimitry Andric const FileSpec &
1243b60736ecSDimitry Andric working_dir, // Pass empty FileSpec to use the current working directory
1244b60736ecSDimitry Andric int *status_ptr, // Pass nullptr if you don't want the process exit status
1245b60736ecSDimitry Andric int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1246b60736ecSDimitry Andric // process to exit
1247b60736ecSDimitry Andric std::string
1248b60736ecSDimitry Andric *command_output, // Pass nullptr if you don't want the command output
1249b60736ecSDimitry Andric const Timeout<std::micro> &timeout) {
1250b60736ecSDimitry Andric return RunShellCommand(llvm::StringRef(), command, working_dir, status_ptr,
1251b60736ecSDimitry Andric signo_ptr, command_output, timeout);
1252b60736ecSDimitry Andric }
1253b60736ecSDimitry Andric
RunShellCommand(llvm::StringRef shell,llvm::StringRef command,const FileSpec & working_dir,int * status_ptr,int * signo_ptr,std::string * command_output,const Timeout<std::micro> & timeout)1254b60736ecSDimitry Andric lldb_private::Status Platform::RunShellCommand(
1255b60736ecSDimitry Andric llvm::StringRef shell, // Pass empty if you want to use the default
1256b60736ecSDimitry Andric // shell interpreter
1257b60736ecSDimitry Andric llvm::StringRef command, // Shouldn't be empty
125814f1b3e8SDimitry Andric const FileSpec &
125914f1b3e8SDimitry Andric working_dir, // Pass empty FileSpec to use the current working directory
1260f3fbd1c0SDimitry Andric int *status_ptr, // Pass nullptr if you don't want the process exit status
126114f1b3e8SDimitry Andric int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
126214f1b3e8SDimitry Andric // process to exit
126314f1b3e8SDimitry Andric std::string
126414f1b3e8SDimitry Andric *command_output, // Pass nullptr if you don't want the command output
1265f73363f1SDimitry Andric const Timeout<std::micro> &timeout) {
1266f21a844fSEd Maste if (IsHost())
1267b60736ecSDimitry Andric return Host::RunShellCommand(shell, command, working_dir, status_ptr,
1268b60736ecSDimitry Andric signo_ptr, command_output, timeout);
1269145449b1SDimitry Andric return Status("unable to run a remote command without a platform");
1270f21a844fSEd Maste }
1271f21a844fSEd Maste
1272ac9a064cSDimitry Andric llvm::ErrorOr<llvm::MD5::MD5Result>
CalculateMD5(const FileSpec & file_spec)1273ac9a064cSDimitry Andric Platform::CalculateMD5(const FileSpec &file_spec) {
127474a628f7SDimitry Andric if (!IsHost())
1275ac9a064cSDimitry Andric return std::make_error_code(std::errc::not_supported);
1276ac9a064cSDimitry Andric return llvm::sys::fs::md5_contents(file_spec.GetPath());
1277f21a844fSEd Maste }
1278f21a844fSEd Maste
SetLocalCacheDirectory(const char * local)127914f1b3e8SDimitry Andric void Platform::SetLocalCacheDirectory(const char *local) {
1280f21a844fSEd Maste m_local_cache_directory.assign(local);
1281f21a844fSEd Maste }
1282f21a844fSEd Maste
GetLocalCacheDirectory()128314f1b3e8SDimitry Andric const char *Platform::GetLocalCacheDirectory() {
1284f21a844fSEd Maste return m_local_cache_directory.c_str();
1285f21a844fSEd Maste }
1286f21a844fSEd Maste
128794994d37SDimitry Andric static constexpr OptionDefinition g_rsync_option_table[] = {
128814f1b3e8SDimitry Andric {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
128994994d37SDimitry Andric {}, 0, eArgTypeNone, "Enable rsync."},
129014f1b3e8SDimitry Andric {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
129194994d37SDimitry Andric OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
129214f1b3e8SDimitry Andric "Platform-specific options required for rsync to work."},
129314f1b3e8SDimitry Andric {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
129494994d37SDimitry Andric OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
129514f1b3e8SDimitry Andric "Platform-specific rsync prefix put before the remote path."},
129614f1b3e8SDimitry Andric {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
129794994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
129814f1b3e8SDimitry Andric "Do not automatically fill in the remote hostname when composing the "
129914f1b3e8SDimitry Andric "rsync command."},
1300f21a844fSEd Maste };
1301f21a844fSEd Maste
130294994d37SDimitry Andric static constexpr OptionDefinition g_ssh_option_table[] = {
130314f1b3e8SDimitry Andric {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
130494994d37SDimitry Andric {}, 0, eArgTypeNone, "Enable SSH."},
130514f1b3e8SDimitry Andric {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
130694994d37SDimitry Andric nullptr, {}, 0, eArgTypeCommandName,
130714f1b3e8SDimitry Andric "Platform-specific options required for SSH to work."},
1308f21a844fSEd Maste };
1309f21a844fSEd Maste
131094994d37SDimitry Andric static constexpr OptionDefinition g_caching_option_table[] = {
131114f1b3e8SDimitry Andric {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
131294994d37SDimitry Andric OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
131314f1b3e8SDimitry Andric "Path in which to store local copies of files."},
1314f21a844fSEd Maste };
1315f21a844fSEd Maste
GetDefinitions()131614f1b3e8SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
1317e3b55780SDimitry Andric return llvm::ArrayRef(g_rsync_option_table);
1318f21a844fSEd Maste }
1319f21a844fSEd Maste
OptionParsingStarting(ExecutionContext * execution_context)132014f1b3e8SDimitry Andric void OptionGroupPlatformRSync::OptionParsingStarting(
132114f1b3e8SDimitry Andric ExecutionContext *execution_context) {
1322f21a844fSEd Maste m_rsync = false;
1323f21a844fSEd Maste m_rsync_opts.clear();
1324f21a844fSEd Maste m_rsync_prefix.clear();
1325f21a844fSEd Maste m_ignores_remote_hostname = false;
1326f21a844fSEd Maste }
1327f21a844fSEd Maste
1328b76161e4SDimitry Andric lldb_private::Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)132914f1b3e8SDimitry Andric OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
133014f1b3e8SDimitry Andric llvm::StringRef option_arg,
133114f1b3e8SDimitry Andric ExecutionContext *execution_context) {
1332b76161e4SDimitry Andric Status error;
1333f21a844fSEd Maste char short_option = (char)GetDefinitions()[option_idx].short_option;
133414f1b3e8SDimitry Andric switch (short_option) {
1335f21a844fSEd Maste case 'r':
1336f21a844fSEd Maste m_rsync = true;
1337f21a844fSEd Maste break;
1338f21a844fSEd Maste
1339f21a844fSEd Maste case 'R':
1340cfca06d7SDimitry Andric m_rsync_opts.assign(std::string(option_arg));
1341f21a844fSEd Maste break;
1342f21a844fSEd Maste
1343f21a844fSEd Maste case 'P':
1344cfca06d7SDimitry Andric m_rsync_prefix.assign(std::string(option_arg));
1345f21a844fSEd Maste break;
1346f21a844fSEd Maste
1347f21a844fSEd Maste case 'i':
1348f21a844fSEd Maste m_ignores_remote_hostname = true;
1349f21a844fSEd Maste break;
1350f21a844fSEd Maste
1351f21a844fSEd Maste default:
1352f21a844fSEd Maste error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1353f21a844fSEd Maste break;
1354f21a844fSEd Maste }
1355f21a844fSEd Maste
1356f21a844fSEd Maste return error;
1357f21a844fSEd Maste }
1358f21a844fSEd Maste
1359f034231aSEd Maste lldb::BreakpointSP
SetThreadCreationBreakpoint(lldb_private::Target & target)136014f1b3e8SDimitry Andric Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1361f034231aSEd Maste return lldb::BreakpointSP();
1362f034231aSEd Maste }
1363f034231aSEd Maste
GetDefinitions()136414f1b3e8SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
1365e3b55780SDimitry Andric return llvm::ArrayRef(g_ssh_option_table);
1366f21a844fSEd Maste }
1367f21a844fSEd Maste
OptionParsingStarting(ExecutionContext * execution_context)136814f1b3e8SDimitry Andric void OptionGroupPlatformSSH::OptionParsingStarting(
136914f1b3e8SDimitry Andric ExecutionContext *execution_context) {
1370f21a844fSEd Maste m_ssh = false;
1371f21a844fSEd Maste m_ssh_opts.clear();
1372f21a844fSEd Maste }
1373f21a844fSEd Maste
1374b76161e4SDimitry Andric lldb_private::Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)137514f1b3e8SDimitry Andric OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
137614f1b3e8SDimitry Andric llvm::StringRef option_arg,
137714f1b3e8SDimitry Andric ExecutionContext *execution_context) {
1378b76161e4SDimitry Andric Status error;
1379f21a844fSEd Maste char short_option = (char)GetDefinitions()[option_idx].short_option;
138014f1b3e8SDimitry Andric switch (short_option) {
1381f21a844fSEd Maste case 's':
1382f21a844fSEd Maste m_ssh = true;
1383f21a844fSEd Maste break;
1384f21a844fSEd Maste
1385f21a844fSEd Maste case 'S':
1386cfca06d7SDimitry Andric m_ssh_opts.assign(std::string(option_arg));
1387f21a844fSEd Maste break;
1388f21a844fSEd Maste
1389f21a844fSEd Maste default:
1390f21a844fSEd Maste error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1391f21a844fSEd Maste break;
1392f21a844fSEd Maste }
1393f21a844fSEd Maste
1394f21a844fSEd Maste return error;
1395f21a844fSEd Maste }
1396f21a844fSEd Maste
GetDefinitions()139714f1b3e8SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
1398e3b55780SDimitry Andric return llvm::ArrayRef(g_caching_option_table);
1399f21a844fSEd Maste }
1400f21a844fSEd Maste
OptionParsingStarting(ExecutionContext * execution_context)140114f1b3e8SDimitry Andric void OptionGroupPlatformCaching::OptionParsingStarting(
140214f1b3e8SDimitry Andric ExecutionContext *execution_context) {
1403f21a844fSEd Maste m_cache_dir.clear();
1404f21a844fSEd Maste }
1405f21a844fSEd Maste
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)1406b76161e4SDimitry Andric lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
140714f1b3e8SDimitry Andric uint32_t option_idx, llvm::StringRef option_arg,
140814f1b3e8SDimitry Andric ExecutionContext *execution_context) {
1409b76161e4SDimitry Andric Status error;
1410f21a844fSEd Maste char short_option = (char)GetDefinitions()[option_idx].short_option;
141114f1b3e8SDimitry Andric switch (short_option) {
1412f21a844fSEd Maste case 'c':
1413cfca06d7SDimitry Andric m_cache_dir.assign(std::string(option_arg));
1414f21a844fSEd Maste break;
1415f21a844fSEd Maste
1416f21a844fSEd Maste default:
1417f21a844fSEd Maste error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1418f21a844fSEd Maste break;
1419f21a844fSEd Maste }
1420f21a844fSEd Maste
1421f21a844fSEd Maste return error;
1422f21a844fSEd Maste }
1423f21a844fSEd Maste
GetEnvironment()1424145449b1SDimitry Andric Environment Platform::GetEnvironment() {
1425145449b1SDimitry Andric if (IsHost())
1426145449b1SDimitry Andric return Host::GetEnvironment();
1427145449b1SDimitry Andric return Environment();
1428145449b1SDimitry Andric }
1429866dcdacSEd Maste
GetTrapHandlerSymbolNames()143014f1b3e8SDimitry Andric const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
143114f1b3e8SDimitry Andric if (!m_calculated_trap_handlers) {
1432f3fbd1c0SDimitry Andric std::lock_guard<std::mutex> guard(m_mutex);
143314f1b3e8SDimitry Andric if (!m_calculated_trap_handlers) {
1434866dcdacSEd Maste CalculateTrapHandlerSymbolNames();
1435866dcdacSEd Maste m_calculated_trap_handlers = true;
1436866dcdacSEd Maste }
14370cac4ca3SEd Maste }
1438866dcdacSEd Maste return m_trap_handlers;
1439866dcdacSEd Maste }
1440866dcdacSEd Maste
1441c0981da4SDimitry Andric Status
GetCachedExecutable(ModuleSpec & module_spec,lldb::ModuleSP & module_sp,const FileSpecList * module_search_paths_ptr)1442c0981da4SDimitry Andric Platform::GetCachedExecutable(ModuleSpec &module_spec,
1443c0981da4SDimitry Andric lldb::ModuleSP &module_sp,
1444c0981da4SDimitry Andric const FileSpecList *module_search_paths_ptr) {
1445f65dcba8SDimitry Andric FileSpec platform_spec = module_spec.GetFileSpec();
1446f65dcba8SDimitry Andric Status error = GetRemoteSharedModule(
1447c0981da4SDimitry Andric module_spec, nullptr, module_sp,
144814f1b3e8SDimitry Andric [&](const ModuleSpec &spec) {
1449ac9a064cSDimitry Andric return Platform::ResolveExecutable(spec, module_sp,
1450c0981da4SDimitry Andric module_search_paths_ptr);
14515e95aa85SEd Maste },
14525e95aa85SEd Maste nullptr);
1453f65dcba8SDimitry Andric if (error.Success()) {
1454f65dcba8SDimitry Andric module_spec.GetFileSpec() = module_sp->GetFileSpec();
1455f65dcba8SDimitry Andric module_spec.GetPlatformFileSpec() = platform_spec;
1456f65dcba8SDimitry Andric }
1457f65dcba8SDimitry Andric
1458f65dcba8SDimitry Andric return error;
14595e95aa85SEd Maste }
14605e95aa85SEd Maste
GetRemoteSharedModule(const ModuleSpec & module_spec,Process * process,lldb::ModuleSP & module_sp,const ModuleResolver & module_resolver,bool * did_create_ptr)1461b76161e4SDimitry Andric Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
14625e95aa85SEd Maste Process *process,
14635e95aa85SEd Maste lldb::ModuleSP &module_sp,
14645e95aa85SEd Maste const ModuleResolver &module_resolver,
146514f1b3e8SDimitry Andric bool *did_create_ptr) {
14665e95aa85SEd Maste // Get module information from a target.
14675e95aa85SEd Maste ModuleSpec resolved_module_spec;
1468145449b1SDimitry Andric ArchSpec process_host_arch;
14695e95aa85SEd Maste bool got_module_spec = false;
147014f1b3e8SDimitry Andric if (process) {
1471145449b1SDimitry Andric process_host_arch = process->GetSystemArchitecture();
14725e95aa85SEd Maste // Try to get module information from the process
147314f1b3e8SDimitry Andric if (process->GetModuleSpec(module_spec.GetFileSpec(),
147414f1b3e8SDimitry Andric module_spec.GetArchitecture(),
147514f1b3e8SDimitry Andric resolved_module_spec)) {
147694994d37SDimitry Andric if (!module_spec.GetUUID().IsValid() ||
147714f1b3e8SDimitry Andric module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
14785e95aa85SEd Maste got_module_spec = true;
14795e95aa85SEd Maste }
1480f3fbd1c0SDimitry Andric }
1481f3fbd1c0SDimitry Andric }
14825e95aa85SEd Maste
148394994d37SDimitry Andric if (!module_spec.GetArchitecture().IsValid()) {
1484b76161e4SDimitry Andric Status error;
1485f73363f1SDimitry Andric // No valid architecture was specified, ask the platform for the
1486f73363f1SDimitry Andric // architectures that we should be using (in the correct order) and see if
1487f73363f1SDimitry Andric // we can find a match that way
148814f1b3e8SDimitry Andric ModuleSpec arch_module_spec(module_spec);
1489145449b1SDimitry Andric for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
1490c0981da4SDimitry Andric arch_module_spec.GetArchitecture() = arch;
149114f1b3e8SDimitry Andric error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
149214f1b3e8SDimitry Andric nullptr, nullptr);
149314f1b3e8SDimitry Andric // Did we find an executable using one of the
149414f1b3e8SDimitry Andric if (error.Success() && module_sp)
149514f1b3e8SDimitry Andric break;
149614f1b3e8SDimitry Andric }
1497b60736ecSDimitry Andric if (module_sp) {
1498b60736ecSDimitry Andric resolved_module_spec = arch_module_spec;
149914f1b3e8SDimitry Andric got_module_spec = true;
150014f1b3e8SDimitry Andric }
1501b60736ecSDimitry Andric }
150214f1b3e8SDimitry Andric
150314f1b3e8SDimitry Andric if (!got_module_spec) {
15045e95aa85SEd Maste // Get module information from a target.
1505b60736ecSDimitry Andric if (GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
150614f1b3e8SDimitry Andric resolved_module_spec)) {
150794994d37SDimitry Andric if (!module_spec.GetUUID().IsValid() ||
150814f1b3e8SDimitry Andric module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1509b60736ecSDimitry Andric got_module_spec = true;
1510b60736ecSDimitry Andric }
1511b60736ecSDimitry Andric }
1512b60736ecSDimitry Andric }
1513b60736ecSDimitry Andric
1514b60736ecSDimitry Andric if (!got_module_spec) {
1515b60736ecSDimitry Andric // Fall back to the given module resolver, which may have its own
1516b60736ecSDimitry Andric // search logic.
15175e95aa85SEd Maste return module_resolver(module_spec);
15185e95aa85SEd Maste }
1519f3fbd1c0SDimitry Andric
152014f1b3e8SDimitry Andric // If we are looking for a specific UUID, make sure resolved_module_spec has
152114f1b3e8SDimitry Andric // the same one before we search.
152214f1b3e8SDimitry Andric if (module_spec.GetUUID().IsValid()) {
1523f3fbd1c0SDimitry Andric resolved_module_spec.GetUUID() = module_spec.GetUUID();
1524f3fbd1c0SDimitry Andric }
15255e95aa85SEd Maste
1526b1c73532SDimitry Andric // Call locate module callback if set. This allows users to implement their
1527b1c73532SDimitry Andric // own module cache system. For example, to leverage build system artifacts,
1528b1c73532SDimitry Andric // to bypass pulling files from remote platform, or to search symbol files
1529b1c73532SDimitry Andric // from symbol servers.
1530b1c73532SDimitry Andric FileSpec symbol_file_spec;
1531b1c73532SDimitry Andric CallLocateModuleCallbackIfSet(resolved_module_spec, module_sp,
1532b1c73532SDimitry Andric symbol_file_spec, did_create_ptr);
1533b1c73532SDimitry Andric if (module_sp) {
1534b1c73532SDimitry Andric // The module is loaded.
1535b1c73532SDimitry Andric if (symbol_file_spec) {
1536b1c73532SDimitry Andric // 1. module_sp:loaded, symbol_file_spec:set
1537b1c73532SDimitry Andric // The callback found a module file and a symbol file for this
1538b1c73532SDimitry Andric // resolved_module_spec. Set the symbol file to the module.
1539b1c73532SDimitry Andric module_sp->SetSymbolFileFileSpec(symbol_file_spec);
1540b1c73532SDimitry Andric } else {
1541b1c73532SDimitry Andric // 2. module_sp:loaded, symbol_file_spec:empty
1542b1c73532SDimitry Andric // The callback only found a module file for this
1543b1c73532SDimitry Andric // resolved_module_spec.
1544b1c73532SDimitry Andric }
1545b76161e4SDimitry Andric return Status();
15465e95aa85SEd Maste }
15475e95aa85SEd Maste
1548b1c73532SDimitry Andric // The module is not loaded by CallLocateModuleCallbackIfSet.
1549b1c73532SDimitry Andric // 3. module_sp:empty, symbol_file_spec:set
1550b1c73532SDimitry Andric // The callback only found a symbol file for the module. We continue to
1551b1c73532SDimitry Andric // find a module file for this resolved_module_spec. and we will call
1552b1c73532SDimitry Andric // module_sp->SetSymbolFileFileSpec with the symbol_file_spec later.
1553b1c73532SDimitry Andric // 4. module_sp:empty, symbol_file_spec:empty
1554b1c73532SDimitry Andric // The callback is not set. Or the callback did not find any module
1555b1c73532SDimitry Andric // files nor any symbol files. Or the callback failed, or something
1556b1c73532SDimitry Andric // went wrong. We continue to find a module file for this
1557b1c73532SDimitry Andric // resolved_module_spec.
1558b1c73532SDimitry Andric
1559b1c73532SDimitry Andric // Trying to find a module by UUID on local file system.
1560b1c73532SDimitry Andric const Status error = module_resolver(resolved_module_spec);
1561b1c73532SDimitry Andric if (error.Success()) {
1562b1c73532SDimitry Andric if (module_sp && symbol_file_spec) {
1563b1c73532SDimitry Andric // Set the symbol file to the module if the locate modudle callback was
1564b1c73532SDimitry Andric // called and returned only a symbol file.
1565b1c73532SDimitry Andric module_sp->SetSymbolFileFileSpec(symbol_file_spec);
1566b1c73532SDimitry Andric }
15675e95aa85SEd Maste return error;
15685e95aa85SEd Maste }
15695e95aa85SEd Maste
1570b1c73532SDimitry Andric // Fallback to call GetCachedSharedModule on failure.
1571b1c73532SDimitry Andric if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr)) {
1572b1c73532SDimitry Andric if (module_sp && symbol_file_spec) {
1573b1c73532SDimitry Andric // Set the symbol file to the module if the locate modudle callback was
1574b1c73532SDimitry Andric // called and returned only a symbol file.
1575b1c73532SDimitry Andric module_sp->SetSymbolFileFileSpec(symbol_file_spec);
1576b1c73532SDimitry Andric }
1577b1c73532SDimitry Andric return Status();
1578b1c73532SDimitry Andric }
1579b1c73532SDimitry Andric
1580b1c73532SDimitry Andric return Status("Failed to call GetCachedSharedModule");
1581b1c73532SDimitry Andric }
1582b1c73532SDimitry Andric
CallLocateModuleCallbackIfSet(const ModuleSpec & module_spec,lldb::ModuleSP & module_sp,FileSpec & symbol_file_spec,bool * did_create_ptr)1583b1c73532SDimitry Andric void Platform::CallLocateModuleCallbackIfSet(const ModuleSpec &module_spec,
1584b1c73532SDimitry Andric lldb::ModuleSP &module_sp,
1585b1c73532SDimitry Andric FileSpec &symbol_file_spec,
1586b1c73532SDimitry Andric bool *did_create_ptr) {
1587b1c73532SDimitry Andric if (!m_locate_module_callback) {
1588b1c73532SDimitry Andric // Locate module callback is not set.
1589b1c73532SDimitry Andric return;
1590b1c73532SDimitry Andric }
1591b1c73532SDimitry Andric
1592b1c73532SDimitry Andric FileSpec module_file_spec;
1593b1c73532SDimitry Andric Status error =
1594b1c73532SDimitry Andric m_locate_module_callback(module_spec, module_file_spec, symbol_file_spec);
1595b1c73532SDimitry Andric
1596b1c73532SDimitry Andric // Locate module callback is set and called. Check the error.
1597b1c73532SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
1598b1c73532SDimitry Andric if (error.Fail()) {
1599b1c73532SDimitry Andric LLDB_LOGF(log, "%s: locate module callback failed: %s",
1600b1c73532SDimitry Andric LLVM_PRETTY_FUNCTION, error.AsCString());
1601b1c73532SDimitry Andric return;
1602b1c73532SDimitry Andric }
1603b1c73532SDimitry Andric
1604b1c73532SDimitry Andric // The locate module callback was succeeded.
1605b1c73532SDimitry Andric // Check the module_file_spec and symbol_file_spec values.
1606b1c73532SDimitry Andric // 1. module:empty symbol:empty -> Failure
1607b1c73532SDimitry Andric // - The callback did not return any files.
1608b1c73532SDimitry Andric // 2. module:exists symbol:exists -> Success
1609b1c73532SDimitry Andric // - The callback returned a module file and a symbol file.
1610b1c73532SDimitry Andric // 3. module:exists symbol:empty -> Success
1611b1c73532SDimitry Andric // - The callback returned only a module file.
1612b1c73532SDimitry Andric // 4. module:empty symbol:exists -> Success
1613b1c73532SDimitry Andric // - The callback returned only a symbol file.
1614b1c73532SDimitry Andric // For example, a breakpad symbol text file.
1615b1c73532SDimitry Andric if (!module_file_spec && !symbol_file_spec) {
1616b1c73532SDimitry Andric // This is '1. module:empty symbol:empty -> Failure'
1617b1c73532SDimitry Andric // The callback did not return any files.
1618b1c73532SDimitry Andric LLDB_LOGF(log,
1619b1c73532SDimitry Andric "%s: locate module callback did not set both "
1620b1c73532SDimitry Andric "module_file_spec and symbol_file_spec",
1621b1c73532SDimitry Andric LLVM_PRETTY_FUNCTION);
1622b1c73532SDimitry Andric return;
1623b1c73532SDimitry Andric }
1624b1c73532SDimitry Andric
1625b1c73532SDimitry Andric // If the callback returned a module file, it should exist.
1626b1c73532SDimitry Andric if (module_file_spec && !FileSystem::Instance().Exists(module_file_spec)) {
1627b1c73532SDimitry Andric LLDB_LOGF(log,
1628b1c73532SDimitry Andric "%s: locate module callback set a non-existent file to "
1629b1c73532SDimitry Andric "module_file_spec: %s",
1630b1c73532SDimitry Andric LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str());
1631b1c73532SDimitry Andric // Clear symbol_file_spec for the error.
1632b1c73532SDimitry Andric symbol_file_spec.Clear();
1633b1c73532SDimitry Andric return;
1634b1c73532SDimitry Andric }
1635b1c73532SDimitry Andric
1636b1c73532SDimitry Andric // If the callback returned a symbol file, it should exist.
1637b1c73532SDimitry Andric if (symbol_file_spec && !FileSystem::Instance().Exists(symbol_file_spec)) {
1638b1c73532SDimitry Andric LLDB_LOGF(log,
1639b1c73532SDimitry Andric "%s: locate module callback set a non-existent file to "
1640b1c73532SDimitry Andric "symbol_file_spec: %s",
1641b1c73532SDimitry Andric LLVM_PRETTY_FUNCTION, symbol_file_spec.GetPath().c_str());
1642b1c73532SDimitry Andric // Clear symbol_file_spec for the error.
1643b1c73532SDimitry Andric symbol_file_spec.Clear();
1644b1c73532SDimitry Andric return;
1645b1c73532SDimitry Andric }
1646b1c73532SDimitry Andric
1647b1c73532SDimitry Andric if (!module_file_spec && symbol_file_spec) {
1648b1c73532SDimitry Andric // This is '4. module:empty symbol:exists -> Success'
1649b1c73532SDimitry Andric // The locate module callback returned only a symbol file. For example,
1650b1c73532SDimitry Andric // a breakpad symbol text file. GetRemoteSharedModule will use this returned
1651b1c73532SDimitry Andric // symbol_file_spec.
1652b1c73532SDimitry Andric LLDB_LOGF(log, "%s: locate module callback succeeded: symbol=%s",
1653b1c73532SDimitry Andric LLVM_PRETTY_FUNCTION, symbol_file_spec.GetPath().c_str());
1654b1c73532SDimitry Andric return;
1655b1c73532SDimitry Andric }
1656b1c73532SDimitry Andric
1657b1c73532SDimitry Andric // This is one of the following.
1658b1c73532SDimitry Andric // - 2. module:exists symbol:exists -> Success
1659b1c73532SDimitry Andric // - The callback returned a module file and a symbol file.
1660b1c73532SDimitry Andric // - 3. module:exists symbol:empty -> Success
1661b1c73532SDimitry Andric // - The callback returned Only a module file.
1662b1c73532SDimitry Andric // Load the module file.
1663b1c73532SDimitry Andric auto cached_module_spec(module_spec);
1664b1c73532SDimitry Andric cached_module_spec.GetUUID().Clear(); // Clear UUID since it may contain md5
1665b1c73532SDimitry Andric // content hash instead of real UUID.
1666b1c73532SDimitry Andric cached_module_spec.GetFileSpec() = module_file_spec;
1667b1c73532SDimitry Andric cached_module_spec.GetPlatformFileSpec() = module_spec.GetFileSpec();
1668b1c73532SDimitry Andric cached_module_spec.SetObjectOffset(0);
1669b1c73532SDimitry Andric
1670b1c73532SDimitry Andric error = ModuleList::GetSharedModule(cached_module_spec, module_sp, nullptr,
1671b1c73532SDimitry Andric nullptr, did_create_ptr, false);
1672b1c73532SDimitry Andric if (error.Success() && module_sp) {
1673b1c73532SDimitry Andric // Succeeded to load the module file.
1674b1c73532SDimitry Andric LLDB_LOGF(log, "%s: locate module callback succeeded: module=%s symbol=%s",
1675b1c73532SDimitry Andric LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str(),
1676b1c73532SDimitry Andric symbol_file_spec.GetPath().c_str());
1677b1c73532SDimitry Andric } else {
1678b1c73532SDimitry Andric LLDB_LOGF(log,
1679b1c73532SDimitry Andric "%s: locate module callback succeeded but failed to load: "
1680b1c73532SDimitry Andric "module=%s symbol=%s",
1681b1c73532SDimitry Andric LLVM_PRETTY_FUNCTION, module_file_spec.GetPath().c_str(),
1682b1c73532SDimitry Andric symbol_file_spec.GetPath().c_str());
1683b1c73532SDimitry Andric // Clear module_sp and symbol_file_spec for the error.
1684b1c73532SDimitry Andric module_sp.reset();
1685b1c73532SDimitry Andric symbol_file_spec.Clear();
1686b1c73532SDimitry Andric }
1687b1c73532SDimitry Andric }
1688b1c73532SDimitry Andric
GetCachedSharedModule(const ModuleSpec & module_spec,lldb::ModuleSP & module_sp,bool * did_create_ptr)168914f1b3e8SDimitry Andric bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
16905e95aa85SEd Maste lldb::ModuleSP &module_sp,
169114f1b3e8SDimitry Andric bool *did_create_ptr) {
1692c0981da4SDimitry Andric if (IsHost() || !GetGlobalPlatformProperties().GetUseModuleCache() ||
1693c0981da4SDimitry Andric !GetGlobalPlatformProperties().GetModuleCacheDirectory())
16945e95aa85SEd Maste return false;
16955e95aa85SEd Maste
1696145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Platform);
16975e95aa85SEd Maste
16985e95aa85SEd Maste // Check local cache for a module.
16995e95aa85SEd Maste auto error = m_module_cache->GetAndPut(
170014f1b3e8SDimitry Andric GetModuleCacheRoot(), GetCacheHostname(), module_spec,
170114f1b3e8SDimitry Andric [this](const ModuleSpec &module_spec,
170214f1b3e8SDimitry Andric const FileSpec &tmp_download_file_spec) {
170314f1b3e8SDimitry Andric return DownloadModuleSlice(
170414f1b3e8SDimitry Andric module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
170514f1b3e8SDimitry Andric module_spec.GetObjectSize(), tmp_download_file_spec);
17065e95aa85SEd Maste
17075e95aa85SEd Maste },
170814f1b3e8SDimitry Andric [this](const ModuleSP &module_sp,
170914f1b3e8SDimitry Andric const FileSpec &tmp_download_file_spec) {
1710e81d9d49SDimitry Andric return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1711e81d9d49SDimitry Andric },
171214f1b3e8SDimitry Andric module_sp, did_create_ptr);
17135e95aa85SEd Maste if (error.Success())
17145e95aa85SEd Maste return true;
17155e95aa85SEd Maste
1716ead24645SDimitry Andric LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s",
171714f1b3e8SDimitry Andric __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
171814f1b3e8SDimitry Andric error.AsCString());
17195e95aa85SEd Maste return false;
17205e95aa85SEd Maste }
17215e95aa85SEd Maste
DownloadModuleSlice(const FileSpec & src_file_spec,const uint64_t src_offset,const uint64_t src_size,const FileSpec & dst_file_spec)1722b76161e4SDimitry Andric Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
17235e95aa85SEd Maste const uint64_t src_offset,
17245e95aa85SEd Maste const uint64_t src_size,
172514f1b3e8SDimitry Andric const FileSpec &dst_file_spec) {
1726b76161e4SDimitry Andric Status error;
17275e95aa85SEd Maste
172874a628f7SDimitry Andric std::error_code EC;
1729ead24645SDimitry Andric llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::OF_None);
173074a628f7SDimitry Andric if (EC) {
173114f1b3e8SDimitry Andric error.SetErrorStringWithFormat("unable to open destination file: %s",
173214f1b3e8SDimitry Andric dst_file_spec.GetPath().c_str());
17335e95aa85SEd Maste return error;
17345e95aa85SEd Maste }
17355e95aa85SEd Maste
1736c0981da4SDimitry Andric auto src_fd = OpenFile(src_file_spec, File::eOpenOptionReadOnly,
173714f1b3e8SDimitry Andric lldb::eFilePermissionsFileDefault, error);
17385e95aa85SEd Maste
173914f1b3e8SDimitry Andric if (error.Fail()) {
174014f1b3e8SDimitry Andric error.SetErrorStringWithFormat("unable to open source file: %s",
174114f1b3e8SDimitry Andric error.AsCString());
17425e95aa85SEd Maste return error;
17435e95aa85SEd Maste }
17445e95aa85SEd Maste
17457fa27ce4SDimitry Andric std::vector<char> buffer(512 * 1024);
17465e95aa85SEd Maste auto offset = src_offset;
17475e95aa85SEd Maste uint64_t total_bytes_read = 0;
174814f1b3e8SDimitry Andric while (total_bytes_read < src_size) {
174914f1b3e8SDimitry Andric const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
175014f1b3e8SDimitry Andric src_size - total_bytes_read);
175114f1b3e8SDimitry Andric const uint64_t n_read =
175214f1b3e8SDimitry Andric ReadFile(src_fd, offset, &buffer[0], to_read, error);
17535e95aa85SEd Maste if (error.Fail())
17545e95aa85SEd Maste break;
175514f1b3e8SDimitry Andric if (n_read == 0) {
17565e95aa85SEd Maste error.SetErrorString("read 0 bytes");
17575e95aa85SEd Maste break;
17585e95aa85SEd Maste }
17595e95aa85SEd Maste offset += n_read;
17605e95aa85SEd Maste total_bytes_read += n_read;
17615e95aa85SEd Maste dst.write(&buffer[0], n_read);
17625e95aa85SEd Maste }
17635e95aa85SEd Maste
1764b76161e4SDimitry Andric Status close_error;
17655e95aa85SEd Maste CloseFile(src_fd, close_error); // Ignoring close error.
17665e95aa85SEd Maste
17675e95aa85SEd Maste return error;
17685e95aa85SEd Maste }
17695e95aa85SEd Maste
DownloadSymbolFile(const lldb::ModuleSP & module_sp,const FileSpec & dst_file_spec)1770b76161e4SDimitry Andric Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
177114f1b3e8SDimitry Andric const FileSpec &dst_file_spec) {
1772b76161e4SDimitry Andric return Status(
177314f1b3e8SDimitry Andric "Symbol file downloading not supported by the default platform.");
1774e81d9d49SDimitry Andric }
1775e81d9d49SDimitry Andric
GetModuleCacheRoot()177614f1b3e8SDimitry Andric FileSpec Platform::GetModuleCacheRoot() {
1777c0981da4SDimitry Andric auto dir_spec = GetGlobalPlatformProperties().GetModuleCacheDirectory();
1778145449b1SDimitry Andric dir_spec.AppendPathComponent(GetPluginName());
17795e95aa85SEd Maste return dir_spec;
17805e95aa85SEd Maste }
17815e95aa85SEd Maste
GetCacheHostname()178214f1b3e8SDimitry Andric const char *Platform::GetCacheHostname() { return GetHostname(); }
1783027f1c96SDimitry Andric
GetRemoteUnixSignals()178414f1b3e8SDimitry Andric const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1785027f1c96SDimitry Andric static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1786027f1c96SDimitry Andric return s_default_unix_signals_sp;
1787027f1c96SDimitry Andric }
1788027f1c96SDimitry Andric
GetUnixSignals()17895f29bb8aSDimitry Andric UnixSignalsSP Platform::GetUnixSignals() {
1790027f1c96SDimitry Andric if (IsHost())
17915f29bb8aSDimitry Andric return UnixSignals::CreateForHost();
1792027f1c96SDimitry Andric return GetRemoteUnixSignals();
1793027f1c96SDimitry Andric }
1794e81d9d49SDimitry Andric
LoadImage(lldb_private::Process * process,const lldb_private::FileSpec & local_file,const lldb_private::FileSpec & remote_file,lldb_private::Status & error)179514f1b3e8SDimitry Andric uint32_t Platform::LoadImage(lldb_private::Process *process,
1796e81d9d49SDimitry Andric const lldb_private::FileSpec &local_file,
1797e81d9d49SDimitry Andric const lldb_private::FileSpec &remote_file,
1798b76161e4SDimitry Andric lldb_private::Status &error) {
179914f1b3e8SDimitry Andric if (local_file && remote_file) {
180014f1b3e8SDimitry Andric // Both local and remote file was specified. Install the local file to the
180114f1b3e8SDimitry Andric // given location.
180214f1b3e8SDimitry Andric if (IsRemote() || local_file != remote_file) {
1803e81d9d49SDimitry Andric error = Install(local_file, remote_file);
1804e81d9d49SDimitry Andric if (error.Fail())
1805e81d9d49SDimitry Andric return LLDB_INVALID_IMAGE_TOKEN;
1806e81d9d49SDimitry Andric }
1807f73363f1SDimitry Andric return DoLoadImage(process, remote_file, nullptr, error);
1808e81d9d49SDimitry Andric }
1809e81d9d49SDimitry Andric
181014f1b3e8SDimitry Andric if (local_file) {
181114f1b3e8SDimitry Andric // Only local file was specified. Install it to the current working
181214f1b3e8SDimitry Andric // directory.
1813e81d9d49SDimitry Andric FileSpec target_file = GetWorkingDirectory();
1814e81d9d49SDimitry Andric target_file.AppendPathComponent(local_file.GetFilename().AsCString());
181514f1b3e8SDimitry Andric if (IsRemote() || local_file != target_file) {
1816e81d9d49SDimitry Andric error = Install(local_file, target_file);
1817e81d9d49SDimitry Andric if (error.Fail())
1818e81d9d49SDimitry Andric return LLDB_INVALID_IMAGE_TOKEN;
1819e81d9d49SDimitry Andric }
1820f73363f1SDimitry Andric return DoLoadImage(process, target_file, nullptr, error);
1821e81d9d49SDimitry Andric }
1822e81d9d49SDimitry Andric
182314f1b3e8SDimitry Andric if (remote_file) {
1824e81d9d49SDimitry Andric // Only remote file was specified so we don't have to do any copying
1825f73363f1SDimitry Andric return DoLoadImage(process, remote_file, nullptr, error);
1826e81d9d49SDimitry Andric }
1827e81d9d49SDimitry Andric
1828e81d9d49SDimitry Andric error.SetErrorString("Neither local nor remote file was specified");
1829e81d9d49SDimitry Andric return LLDB_INVALID_IMAGE_TOKEN;
1830e81d9d49SDimitry Andric }
1831e81d9d49SDimitry Andric
DoLoadImage(lldb_private::Process * process,const lldb_private::FileSpec & remote_file,const std::vector<std::string> * paths,lldb_private::Status & error,lldb_private::FileSpec * loaded_image)183214f1b3e8SDimitry Andric uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1833e81d9d49SDimitry Andric const lldb_private::FileSpec &remote_file,
1834f73363f1SDimitry Andric const std::vector<std::string> *paths,
1835f73363f1SDimitry Andric lldb_private::Status &error,
1836f73363f1SDimitry Andric lldb_private::FileSpec *loaded_image) {
1837e81d9d49SDimitry Andric error.SetErrorString("LoadImage is not supported on the current platform");
1838e81d9d49SDimitry Andric return LLDB_INVALID_IMAGE_TOKEN;
1839e81d9d49SDimitry Andric }
1840e81d9d49SDimitry Andric
LoadImageUsingPaths(lldb_private::Process * process,const lldb_private::FileSpec & remote_filename,const std::vector<std::string> & paths,lldb_private::Status & error,lldb_private::FileSpec * loaded_path)1841f73363f1SDimitry Andric uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1842f73363f1SDimitry Andric const lldb_private::FileSpec &remote_filename,
1843f73363f1SDimitry Andric const std::vector<std::string> &paths,
1844f73363f1SDimitry Andric lldb_private::Status &error,
1845f73363f1SDimitry Andric lldb_private::FileSpec *loaded_path)
1846f73363f1SDimitry Andric {
1847f73363f1SDimitry Andric FileSpec file_to_use;
1848f73363f1SDimitry Andric if (remote_filename.IsAbsolute())
1849f73363f1SDimitry Andric file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
185094994d37SDimitry Andric
1851f73363f1SDimitry Andric remote_filename.GetPathStyle());
1852f73363f1SDimitry Andric else
1853f73363f1SDimitry Andric file_to_use = remote_filename;
1854f73363f1SDimitry Andric
1855f73363f1SDimitry Andric return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1856f73363f1SDimitry Andric }
1857f73363f1SDimitry Andric
UnloadImage(lldb_private::Process * process,uint32_t image_token)1858b76161e4SDimitry Andric Status Platform::UnloadImage(lldb_private::Process *process,
185914f1b3e8SDimitry Andric uint32_t image_token) {
1860b76161e4SDimitry Andric return Status("UnloadImage is not supported on the current platform");
1861e81d9d49SDimitry Andric }
1862e81d9d49SDimitry Andric
ConnectProcess(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Target * target,Status & error)186314f1b3e8SDimitry Andric lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
186414f1b3e8SDimitry Andric llvm::StringRef plugin_name,
1865cfca06d7SDimitry Andric Debugger &debugger, Target *target,
1866cfca06d7SDimitry Andric Status &error) {
1867cfca06d7SDimitry Andric return DoConnectProcess(connect_url, plugin_name, debugger, nullptr, target,
1868cfca06d7SDimitry Andric error);
1869cfca06d7SDimitry Andric }
1870cfca06d7SDimitry Andric
ConnectProcessSynchronous(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Stream & stream,Target * target,Status & error)1871cfca06d7SDimitry Andric lldb::ProcessSP Platform::ConnectProcessSynchronous(
1872cfca06d7SDimitry Andric llvm::StringRef connect_url, llvm::StringRef plugin_name,
1873cfca06d7SDimitry Andric Debugger &debugger, Stream &stream, Target *target, Status &error) {
1874cfca06d7SDimitry Andric return DoConnectProcess(connect_url, plugin_name, debugger, &stream, target,
1875cfca06d7SDimitry Andric error);
1876cfca06d7SDimitry Andric }
1877cfca06d7SDimitry Andric
DoConnectProcess(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Stream * stream,Target * target,Status & error)1878cfca06d7SDimitry Andric lldb::ProcessSP Platform::DoConnectProcess(llvm::StringRef connect_url,
1879cfca06d7SDimitry Andric llvm::StringRef plugin_name,
1880cfca06d7SDimitry Andric Debugger &debugger, Stream *stream,
1881cfca06d7SDimitry Andric Target *target, Status &error) {
1882e81d9d49SDimitry Andric error.Clear();
1883e81d9d49SDimitry Andric
188414f1b3e8SDimitry Andric if (!target) {
1885e3b55780SDimitry Andric ArchSpec arch = Target::GetDefaultArchitecture();
188694994d37SDimitry Andric
1887e3b55780SDimitry Andric const char *triple =
1888e3b55780SDimitry Andric arch.IsValid() ? arch.GetTriple().getTriple().c_str() : "";
188994994d37SDimitry Andric
1890e81d9d49SDimitry Andric TargetSP new_target_sp;
189194994d37SDimitry Andric error = debugger.GetTargetList().CreateTarget(
189294994d37SDimitry Andric debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
1893e81d9d49SDimitry Andric
1894e3b55780SDimitry Andric target = new_target_sp.get();
1895e3b55780SDimitry Andric if (!target || error.Fail()) {
1896e81d9d49SDimitry Andric return nullptr;
1897e3b55780SDimitry Andric }
1898e3b55780SDimitry Andric }
1899e81d9d49SDimitry Andric
190014f1b3e8SDimitry Andric lldb::ProcessSP process_sp =
1901b60736ecSDimitry Andric target->CreateProcess(debugger.GetListener(), plugin_name, nullptr, true);
1902cfca06d7SDimitry Andric
1903e81d9d49SDimitry Andric if (!process_sp)
1904e81d9d49SDimitry Andric return nullptr;
1905e81d9d49SDimitry Andric
1906cfca06d7SDimitry Andric // If this private method is called with a stream we are synchronous.
1907cfca06d7SDimitry Andric const bool synchronous = stream != nullptr;
1908cfca06d7SDimitry Andric
1909cfca06d7SDimitry Andric ListenerSP listener_sp(
1910cfca06d7SDimitry Andric Listener::MakeListener("lldb.Process.ConnectProcess.hijack"));
1911cfca06d7SDimitry Andric if (synchronous)
1912cfca06d7SDimitry Andric process_sp->HijackProcessEvents(listener_sp);
1913cfca06d7SDimitry Andric
1914cfca06d7SDimitry Andric error = process_sp->ConnectRemote(connect_url);
1915cfca06d7SDimitry Andric if (error.Fail()) {
1916cfca06d7SDimitry Andric if (synchronous)
1917cfca06d7SDimitry Andric process_sp->RestoreProcessEvents();
1918e81d9d49SDimitry Andric return nullptr;
1919cfca06d7SDimitry Andric }
1920cfca06d7SDimitry Andric
1921cfca06d7SDimitry Andric if (synchronous) {
1922cfca06d7SDimitry Andric EventSP event_sp;
1923e3b55780SDimitry Andric process_sp->WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp,
1924cfca06d7SDimitry Andric nullptr);
1925cfca06d7SDimitry Andric process_sp->RestoreProcessEvents();
1926cfca06d7SDimitry Andric bool pop_process_io_handler = false;
19277fa27ce4SDimitry Andric // This is a user-level stop, so we allow recognizers to select frames.
19287fa27ce4SDimitry Andric Process::HandleProcessStateChangedEvent(
19297fa27ce4SDimitry Andric event_sp, stream, SelectMostRelevantFrame, pop_process_io_handler);
1930cfca06d7SDimitry Andric }
1931e81d9d49SDimitry Andric
1932e81d9d49SDimitry Andric return process_sp;
1933e81d9d49SDimitry Andric }
1934e81d9d49SDimitry Andric
ConnectToWaitingProcesses(lldb_private::Debugger & debugger,lldb_private::Status & error)193514f1b3e8SDimitry Andric size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
1936b76161e4SDimitry Andric lldb_private::Status &error) {
1937e81d9d49SDimitry Andric error.Clear();
1938e81d9d49SDimitry Andric return 0;
1939e81d9d49SDimitry Andric }
1940f3fbd1c0SDimitry Andric
GetSoftwareBreakpointTrapOpcode(Target & target,BreakpointSite * bp_site)194114f1b3e8SDimitry Andric size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
194214f1b3e8SDimitry Andric BreakpointSite *bp_site) {
1943f3fbd1c0SDimitry Andric ArchSpec arch = target.GetArchitecture();
1944cfca06d7SDimitry Andric assert(arch.IsValid());
1945f3fbd1c0SDimitry Andric const uint8_t *trap_opcode = nullptr;
1946f3fbd1c0SDimitry Andric size_t trap_opcode_size = 0;
1947f3fbd1c0SDimitry Andric
194814f1b3e8SDimitry Andric switch (arch.GetMachine()) {
1949ead24645SDimitry Andric case llvm::Triple::aarch64_32:
195014f1b3e8SDimitry Andric case llvm::Triple::aarch64: {
1951f3fbd1c0SDimitry Andric static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1952f3fbd1c0SDimitry Andric trap_opcode = g_aarch64_opcode;
1953f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_aarch64_opcode);
195414f1b3e8SDimitry Andric } break;
1955f3fbd1c0SDimitry Andric
1956ead24645SDimitry Andric case llvm::Triple::arc: {
1957ead24645SDimitry Andric static const uint8_t g_hex_opcode[] = { 0xff, 0x7f };
1958ead24645SDimitry Andric trap_opcode = g_hex_opcode;
1959ead24645SDimitry Andric trap_opcode_size = sizeof(g_hex_opcode);
1960ead24645SDimitry Andric } break;
1961ead24645SDimitry Andric
1962f3fbd1c0SDimitry Andric // TODO: support big-endian arm and thumb trap codes.
196314f1b3e8SDimitry Andric case llvm::Triple::arm: {
1964f73363f1SDimitry Andric // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1965f73363f1SDimitry Andric // linux kernel does otherwise.
1966f3fbd1c0SDimitry Andric static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1967f3fbd1c0SDimitry Andric static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1968f3fbd1c0SDimitry Andric
1969b1c73532SDimitry Andric lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetConstituentAtIndex(0));
1970f73363f1SDimitry Andric AddressClass addr_class = AddressClass::eUnknown;
1971f3fbd1c0SDimitry Andric
197214f1b3e8SDimitry Andric if (bp_loc_sp) {
1973f3fbd1c0SDimitry Andric addr_class = bp_loc_sp->GetAddress().GetAddressClass();
1974f73363f1SDimitry Andric if (addr_class == AddressClass::eUnknown &&
197514f1b3e8SDimitry Andric (bp_loc_sp->GetAddress().GetFileAddress() & 1))
1976f73363f1SDimitry Andric addr_class = AddressClass::eCodeAlternateISA;
1977f3fbd1c0SDimitry Andric }
1978f3fbd1c0SDimitry Andric
1979f73363f1SDimitry Andric if (addr_class == AddressClass::eCodeAlternateISA) {
1980f3fbd1c0SDimitry Andric trap_opcode = g_thumb_breakpoint_opcode;
1981f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
198214f1b3e8SDimitry Andric } else {
1983f3fbd1c0SDimitry Andric trap_opcode = g_arm_breakpoint_opcode;
1984f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1985f3fbd1c0SDimitry Andric }
198614f1b3e8SDimitry Andric } break;
1987f3fbd1c0SDimitry Andric
1988cfca06d7SDimitry Andric case llvm::Triple::avr: {
1989cfca06d7SDimitry Andric static const uint8_t g_hex_opcode[] = {0x98, 0x95};
1990cfca06d7SDimitry Andric trap_opcode = g_hex_opcode;
1991cfca06d7SDimitry Andric trap_opcode_size = sizeof(g_hex_opcode);
1992cfca06d7SDimitry Andric } break;
1993cfca06d7SDimitry Andric
1994f3fbd1c0SDimitry Andric case llvm::Triple::mips:
199514f1b3e8SDimitry Andric case llvm::Triple::mips64: {
1996f3fbd1c0SDimitry Andric static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1997f3fbd1c0SDimitry Andric trap_opcode = g_hex_opcode;
1998f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_hex_opcode);
199914f1b3e8SDimitry Andric } break;
2000f3fbd1c0SDimitry Andric
2001f3fbd1c0SDimitry Andric case llvm::Triple::mipsel:
200214f1b3e8SDimitry Andric case llvm::Triple::mips64el: {
2003f3fbd1c0SDimitry Andric static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
2004f3fbd1c0SDimitry Andric trap_opcode = g_hex_opcode;
2005f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_hex_opcode);
200614f1b3e8SDimitry Andric } break;
2007f3fbd1c0SDimitry Andric
20087fa27ce4SDimitry Andric case llvm::Triple::msp430: {
20097fa27ce4SDimitry Andric static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
20107fa27ce4SDimitry Andric trap_opcode = g_msp430_opcode;
20117fa27ce4SDimitry Andric trap_opcode_size = sizeof(g_msp430_opcode);
20127fa27ce4SDimitry Andric } break;
20137fa27ce4SDimitry Andric
201414f1b3e8SDimitry Andric case llvm::Triple::systemz: {
2015f3fbd1c0SDimitry Andric static const uint8_t g_hex_opcode[] = {0x00, 0x01};
2016f3fbd1c0SDimitry Andric trap_opcode = g_hex_opcode;
2017f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_hex_opcode);
201814f1b3e8SDimitry Andric } break;
2019f3fbd1c0SDimitry Andric
202014f1b3e8SDimitry Andric case llvm::Triple::hexagon: {
2021f3fbd1c0SDimitry Andric static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
2022f3fbd1c0SDimitry Andric trap_opcode = g_hex_opcode;
2023f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_hex_opcode);
202414f1b3e8SDimitry Andric } break;
2025f3fbd1c0SDimitry Andric
2026f3fbd1c0SDimitry Andric case llvm::Triple::ppc:
202714f1b3e8SDimitry Andric case llvm::Triple::ppc64: {
2028f3fbd1c0SDimitry Andric static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
2029f3fbd1c0SDimitry Andric trap_opcode = g_ppc_opcode;
2030f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_ppc_opcode);
203114f1b3e8SDimitry Andric } break;
2032f3fbd1c0SDimitry Andric
2033ef5d0b5eSDimitry Andric case llvm::Triple::ppc64le: {
2034ef5d0b5eSDimitry Andric static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
2035ef5d0b5eSDimitry Andric trap_opcode = g_ppc64le_opcode;
2036ef5d0b5eSDimitry Andric trap_opcode_size = sizeof(g_ppc64le_opcode);
2037ef5d0b5eSDimitry Andric } break;
2038ef5d0b5eSDimitry Andric
2039f3fbd1c0SDimitry Andric case llvm::Triple::x86:
204014f1b3e8SDimitry Andric case llvm::Triple::x86_64: {
2041f3fbd1c0SDimitry Andric static const uint8_t g_i386_opcode[] = {0xCC};
2042f3fbd1c0SDimitry Andric trap_opcode = g_i386_opcode;
2043f3fbd1c0SDimitry Andric trap_opcode_size = sizeof(g_i386_opcode);
204414f1b3e8SDimitry Andric } break;
2045f3fbd1c0SDimitry Andric
2046e3b55780SDimitry Andric case llvm::Triple::riscv32:
2047e3b55780SDimitry Andric case llvm::Triple::riscv64: {
2048e3b55780SDimitry Andric static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00}; // ebreak
2049e3b55780SDimitry Andric static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90}; // c.ebreak
2050e3b55780SDimitry Andric if (arch.GetFlags() & ArchSpec::eRISCV_rvc) {
2051e3b55780SDimitry Andric trap_opcode = g_riscv_opcode_c;
2052e3b55780SDimitry Andric trap_opcode_size = sizeof(g_riscv_opcode_c);
2053e3b55780SDimitry Andric } else {
2054e3b55780SDimitry Andric trap_opcode = g_riscv_opcode;
2055e3b55780SDimitry Andric trap_opcode_size = sizeof(g_riscv_opcode);
2056e3b55780SDimitry Andric }
2057e3b55780SDimitry Andric } break;
2058e3b55780SDimitry Andric
2059e3b55780SDimitry Andric case llvm::Triple::loongarch32:
2060e3b55780SDimitry Andric case llvm::Triple::loongarch64: {
2061e3b55780SDimitry Andric static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
2062e3b55780SDimitry Andric 0x00}; // break 0x5
2063e3b55780SDimitry Andric trap_opcode = g_loongarch_opcode;
2064e3b55780SDimitry Andric trap_opcode_size = sizeof(g_loongarch_opcode);
2065e3b55780SDimitry Andric } break;
2066e3b55780SDimitry Andric
2067f3fbd1c0SDimitry Andric default:
2068cfca06d7SDimitry Andric return 0;
2069f3fbd1c0SDimitry Andric }
2070f3fbd1c0SDimitry Andric
2071f3fbd1c0SDimitry Andric assert(bp_site);
2072f3fbd1c0SDimitry Andric if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
2073f3fbd1c0SDimitry Andric return trap_opcode_size;
2074f3fbd1c0SDimitry Andric
2075f3fbd1c0SDimitry Andric return 0;
2076f3fbd1c0SDimitry Andric }
20776f8fc217SDimitry Andric
GetSiginfoType(const llvm::Triple & triple)20786f8fc217SDimitry Andric CompilerType Platform::GetSiginfoType(const llvm::Triple& triple) {
20796f8fc217SDimitry Andric return CompilerType();
20806f8fc217SDimitry Andric }
2081145449b1SDimitry Andric
GetExtraStartupCommands()2082145449b1SDimitry Andric Args Platform::GetExtraStartupCommands() {
2083145449b1SDimitry Andric return {};
2084145449b1SDimitry Andric }
2085145449b1SDimitry Andric
SetLocateModuleCallback(LocateModuleCallback callback)20867fa27ce4SDimitry Andric void Platform::SetLocateModuleCallback(LocateModuleCallback callback) {
20877fa27ce4SDimitry Andric m_locate_module_callback = callback;
20887fa27ce4SDimitry Andric }
20897fa27ce4SDimitry Andric
GetLocateModuleCallback() const20907fa27ce4SDimitry Andric Platform::LocateModuleCallback Platform::GetLocateModuleCallback() const {
20917fa27ce4SDimitry Andric return m_locate_module_callback;
20927fa27ce4SDimitry Andric }
20937fa27ce4SDimitry Andric
GetOrCreate(llvm::StringRef name)2094145449b1SDimitry Andric PlatformSP PlatformList::GetOrCreate(llvm::StringRef name) {
2095145449b1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
2096145449b1SDimitry Andric for (const PlatformSP &platform_sp : m_platforms) {
2097145449b1SDimitry Andric if (platform_sp->GetName() == name)
2098145449b1SDimitry Andric return platform_sp;
2099145449b1SDimitry Andric }
2100145449b1SDimitry Andric return Create(name);
2101145449b1SDimitry Andric }
2102145449b1SDimitry Andric
GetOrCreate(const ArchSpec & arch,const ArchSpec & process_host_arch,ArchSpec * platform_arch_ptr,Status & error)2103145449b1SDimitry Andric PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
2104145449b1SDimitry Andric const ArchSpec &process_host_arch,
2105145449b1SDimitry Andric ArchSpec *platform_arch_ptr,
2106145449b1SDimitry Andric Status &error) {
2107145449b1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
2108145449b1SDimitry Andric // First try exact arch matches across all platforms already created
2109145449b1SDimitry Andric for (const auto &platform_sp : m_platforms) {
2110e3b55780SDimitry Andric if (platform_sp->IsCompatibleArchitecture(
2111e3b55780SDimitry Andric arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr))
2112145449b1SDimitry Andric return platform_sp;
2113145449b1SDimitry Andric }
2114145449b1SDimitry Andric
2115145449b1SDimitry Andric // Next try compatible arch matches across all platforms already created
2116145449b1SDimitry Andric for (const auto &platform_sp : m_platforms) {
2117e3b55780SDimitry Andric if (platform_sp->IsCompatibleArchitecture(arch, process_host_arch,
2118e3b55780SDimitry Andric ArchSpec::CompatibleMatch,
2119145449b1SDimitry Andric platform_arch_ptr))
2120145449b1SDimitry Andric return platform_sp;
2121145449b1SDimitry Andric }
2122145449b1SDimitry Andric
2123145449b1SDimitry Andric PlatformCreateInstance create_callback;
2124145449b1SDimitry Andric // First try exact arch matches across all platform plug-ins
2125145449b1SDimitry Andric uint32_t idx;
2126145449b1SDimitry Andric for (idx = 0;
2127145449b1SDimitry Andric (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
2128145449b1SDimitry Andric ++idx) {
2129145449b1SDimitry Andric PlatformSP platform_sp = create_callback(false, &arch);
2130e3b55780SDimitry Andric if (platform_sp &&
2131e3b55780SDimitry Andric platform_sp->IsCompatibleArchitecture(
2132e3b55780SDimitry Andric arch, process_host_arch, ArchSpec::ExactMatch, platform_arch_ptr)) {
2133145449b1SDimitry Andric m_platforms.push_back(platform_sp);
2134145449b1SDimitry Andric return platform_sp;
2135145449b1SDimitry Andric }
2136145449b1SDimitry Andric }
2137145449b1SDimitry Andric // Next try compatible arch matches across all platform plug-ins
2138145449b1SDimitry Andric for (idx = 0;
2139145449b1SDimitry Andric (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
2140145449b1SDimitry Andric ++idx) {
2141145449b1SDimitry Andric PlatformSP platform_sp = create_callback(false, &arch);
2142145449b1SDimitry Andric if (platform_sp && platform_sp->IsCompatibleArchitecture(
2143e3b55780SDimitry Andric arch, process_host_arch, ArchSpec::CompatibleMatch,
2144e3b55780SDimitry Andric platform_arch_ptr)) {
2145145449b1SDimitry Andric m_platforms.push_back(platform_sp);
2146145449b1SDimitry Andric return platform_sp;
2147145449b1SDimitry Andric }
2148145449b1SDimitry Andric }
2149145449b1SDimitry Andric if (platform_arch_ptr)
2150145449b1SDimitry Andric platform_arch_ptr->Clear();
2151145449b1SDimitry Andric return nullptr;
2152145449b1SDimitry Andric }
2153145449b1SDimitry Andric
GetOrCreate(const ArchSpec & arch,const ArchSpec & process_host_arch,ArchSpec * platform_arch_ptr)2154145449b1SDimitry Andric PlatformSP PlatformList::GetOrCreate(const ArchSpec &arch,
2155145449b1SDimitry Andric const ArchSpec &process_host_arch,
2156145449b1SDimitry Andric ArchSpec *platform_arch_ptr) {
2157145449b1SDimitry Andric Status error;
2158145449b1SDimitry Andric if (arch.IsValid())
2159145449b1SDimitry Andric return GetOrCreate(arch, process_host_arch, platform_arch_ptr, error);
2160145449b1SDimitry Andric return nullptr;
2161145449b1SDimitry Andric }
2162145449b1SDimitry Andric
GetOrCreate(llvm::ArrayRef<ArchSpec> archs,const ArchSpec & process_host_arch,std::vector<PlatformSP> & candidates)2163145449b1SDimitry Andric PlatformSP PlatformList::GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
2164145449b1SDimitry Andric const ArchSpec &process_host_arch,
2165145449b1SDimitry Andric std::vector<PlatformSP> &candidates) {
2166145449b1SDimitry Andric candidates.clear();
2167145449b1SDimitry Andric candidates.reserve(archs.size());
2168145449b1SDimitry Andric
2169145449b1SDimitry Andric if (archs.empty())
2170145449b1SDimitry Andric return nullptr;
2171145449b1SDimitry Andric
2172145449b1SDimitry Andric PlatformSP host_platform_sp = Platform::GetHostPlatform();
2173145449b1SDimitry Andric
2174145449b1SDimitry Andric // Prefer the selected platform if it matches at least one architecture.
2175145449b1SDimitry Andric if (m_selected_platform_sp) {
2176145449b1SDimitry Andric for (const ArchSpec &arch : archs) {
2177145449b1SDimitry Andric if (m_selected_platform_sp->IsCompatibleArchitecture(
2178e3b55780SDimitry Andric arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
2179145449b1SDimitry Andric return m_selected_platform_sp;
2180145449b1SDimitry Andric }
2181145449b1SDimitry Andric }
2182145449b1SDimitry Andric
2183145449b1SDimitry Andric // Prefer the host platform if it matches at least one architecture.
2184145449b1SDimitry Andric if (host_platform_sp) {
2185145449b1SDimitry Andric for (const ArchSpec &arch : archs) {
2186e3b55780SDimitry Andric if (host_platform_sp->IsCompatibleArchitecture(
2187e3b55780SDimitry Andric arch, process_host_arch, ArchSpec::CompatibleMatch, nullptr))
2188145449b1SDimitry Andric return host_platform_sp;
2189145449b1SDimitry Andric }
2190145449b1SDimitry Andric }
2191145449b1SDimitry Andric
2192145449b1SDimitry Andric // Collect a list of candidate platforms for the architectures.
2193145449b1SDimitry Andric for (const ArchSpec &arch : archs) {
2194145449b1SDimitry Andric if (PlatformSP platform = GetOrCreate(arch, process_host_arch, nullptr))
2195145449b1SDimitry Andric candidates.push_back(platform);
2196145449b1SDimitry Andric }
2197145449b1SDimitry Andric
2198145449b1SDimitry Andric // The selected or host platform didn't match any of the architectures. If
2199145449b1SDimitry Andric // the same platform supports all architectures then that's the obvious next
2200145449b1SDimitry Andric // best thing.
2201145449b1SDimitry Andric if (candidates.size() == archs.size()) {
2202e3b55780SDimitry Andric if (llvm::all_of(candidates, [&](const PlatformSP &p) -> bool {
2203145449b1SDimitry Andric return p->GetName() == candidates.front()->GetName();
2204145449b1SDimitry Andric })) {
2205145449b1SDimitry Andric return candidates.front();
2206145449b1SDimitry Andric }
2207145449b1SDimitry Andric }
2208145449b1SDimitry Andric
2209145449b1SDimitry Andric // At this point we either have no platforms that match the given
2210145449b1SDimitry Andric // architectures or multiple platforms with no good way to disambiguate
2211145449b1SDimitry Andric // between them.
2212145449b1SDimitry Andric return nullptr;
2213145449b1SDimitry Andric }
2214145449b1SDimitry Andric
Create(llvm::StringRef name)2215145449b1SDimitry Andric PlatformSP PlatformList::Create(llvm::StringRef name) {
2216145449b1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
2217145449b1SDimitry Andric PlatformSP platform_sp = Platform::Create(name);
2218145449b1SDimitry Andric m_platforms.push_back(platform_sp);
2219145449b1SDimitry Andric return platform_sp;
2220145449b1SDimitry Andric }
2221e3b55780SDimitry Andric
LoadPlatformBinaryAndSetup(Process * process,lldb::addr_t addr,bool notify)2222e3b55780SDimitry Andric bool PlatformList::LoadPlatformBinaryAndSetup(Process *process,
2223e3b55780SDimitry Andric lldb::addr_t addr, bool notify) {
2224e3b55780SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
2225e3b55780SDimitry Andric
2226e3b55780SDimitry Andric PlatformCreateInstance create_callback;
2227e3b55780SDimitry Andric for (int idx = 0;
2228e3b55780SDimitry Andric (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx));
2229e3b55780SDimitry Andric ++idx) {
2230e3b55780SDimitry Andric ArchSpec arch;
2231e3b55780SDimitry Andric PlatformSP platform_sp = create_callback(true, &arch);
2232e3b55780SDimitry Andric if (platform_sp) {
2233e3b55780SDimitry Andric if (platform_sp->LoadPlatformBinaryAndSetup(process, addr, notify))
2234e3b55780SDimitry Andric return true;
2235e3b55780SDimitry Andric }
2236e3b55780SDimitry Andric }
2237e3b55780SDimitry Andric return false;
2238e3b55780SDimitry Andric }
2239