blob: c4089625ce096746ebaf1d5b3bfde184a203f68a [file] [log] [blame]
Greg Claytone996fd32011-03-08 22:40:15 +00001//===-- Platform.cpp --------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Greg Claytone996fd32011-03-08 22:40:15 +00006//
7//===----------------------------------------------------------------------===//
8
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00009#include <algorithm>
Pavel Labathbc4987b2018-04-05 16:59:36 +000010#include <csignal>
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000011#include <fstream>
Jonas Devlieghere796ac802019-02-11 23:13:08 +000012#include <memory>
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000013#include <vector>
14
Eugene Zelenko9394d7722016-02-18 00:10:17 +000015#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Path.h"
17
Greg Clayton4116e932012-05-15 02:33:01 +000018#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000019#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000020#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000021#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000023#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000024#include "lldb/Core/StreamFile.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000025#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000026#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000027#include "lldb/Host/HostInfo.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000028#include "lldb/Host/OptionParser.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000029#include "lldb/Interpreter/OptionValueProperties.h"
30#include "lldb/Interpreter/Property.h"
31#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000032#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000033#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000034#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000035#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000036#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000037#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000038#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000039#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000040#include "lldb/Utility/Status.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000041#include "lldb/Utility/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000042
Zachary Turner7d86ee52017-03-08 17:56:08 +000043#include "llvm/Support/FileSystem.h"
44
Adrian Prantl05097242018-04-30 16:49:04 +000045// Define these constants from POSIX mman.h rather than include the file so
46// that they will be correct even when compiled on Linux.
Robert Flack96ad3de2015-05-09 15:53:31 +000047#define MAP_PRIVATE 2
48#define MAP_ANON 0x1000
49
Greg Claytone996fd32011-03-08 22:40:15 +000050using namespace lldb;
51using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000052
53static uint32_t g_initialize_count = 0;
54
Adrian Prantl05097242018-04-30 16:49:04 +000055// Use a singleton function for g_local_platform_sp to avoid init constructors
56// since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000057static PlatformSP &GetHostPlatformSP() {
58 static PlatformSP g_platform_sp;
59 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000063
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000064namespace {
65
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000066#define LLDB_PROPERTIES_platform
Jordan Rupprecht6a253d32019-07-29 17:22:10 +000067#include "TargetProperties.inc"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000068
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000069enum {
70#define LLDB_PROPERTIES_platform
Jordan Rupprecht6a253d32019-07-29 17:22:10 +000071#include "TargetPropertiesEnum.inc"
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000072};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076ConstString PlatformProperties::GetSettingName() {
77 static ConstString g_setting_name("platform");
78 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081PlatformProperties::PlatformProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +000082 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Jonas Devliegherea8ea5952019-07-29 16:41:30 +000083 m_collection_sp->Initialize(g_platform_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 auto module_cache_dir = GetModuleCacheDirectory();
86 if (module_cache_dir)
87 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 llvm::SmallString<64> user_home_dir;
90 if (!llvm::sys::path::home_directory(user_home_dir))
91 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000092
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000093 module_cache_dir = FileSpec(user_home_dir.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 module_cache_dir.AppendPathComponent(".lldb");
95 module_cache_dir.AppendPathComponent("module_cache");
96 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099bool PlatformProperties::GetUseModuleCache() const {
100 const auto idx = ePropertyUseModuleCache;
101 return m_collection_sp->GetPropertyAtIndexAsBoolean(
Jonas Devliegherea8ea5952019-07-29 16:41:30 +0000102 nullptr, idx, g_platform_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000103}
104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
106 return m_collection_sp->SetPropertyAtIndexAsBoolean(
107 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110FileSpec PlatformProperties::GetModuleCacheDirectory() const {
111 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
112 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
116 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
117 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000118}
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000121///
122/// There should only be one of these for each host that LLDB runs
123/// upon that should be statically compiled in and registered using
124/// preprocessor macros or other similar build mechanisms.
125///
126/// This platform will be used as the default platform when launching
127/// or attaching to processes unless another platform is specified.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
129
130static std::vector<PlatformSP> &GetPlatformList() {
131 static std::vector<PlatformSP> g_platform_list;
132 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000133}
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135static std::recursive_mutex &GetPlatformListMutex() {
136 static std::recursive_mutex g_mutex;
137 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142void Platform::Terminate() {
143 if (g_initialize_count > 0) {
144 if (--g_initialize_count == 0) {
145 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
146 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000147 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000149}
150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
152 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
153 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
157 // The native platform should use its static void Platform::Initialize()
158 // function to register itself as the native platform.
159 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 if (platform_sp) {
162 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
163 GetPlatformList().push_back(platform_sp);
164 }
Greg Claytone996fd32011-03-08 22:40:15 +0000165}
166
Zachary Turner97206d52017-05-12 04:51:55 +0000167Status Platform::GetFileWithUUID(const FileSpec &platform_file,
168 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 // Default to the local case
170 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000171 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000172}
173
Greg Clayton91c0e742013-01-11 23:44:27 +0000174FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175Platform::LocateExecutableScriptingResources(Target *target, Module &module,
176 Stream *feedback_stream) {
177 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000178}
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180// PlatformSP
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000181// Platform::FindPlugin (Process *process, ConstString plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000182//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000183// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000184// if (plugin_name)
185// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186// create_callback =
187// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000188// if (create_callback)
189// {
190// ArchSpec arch;
191// if (process)
192// {
193// arch = process->GetTarget().GetArchitecture();
194// }
195// PlatformSP platform_sp(create_callback(process, &arch));
196// if (platform_sp)
197// return platform_sp;
198// }
199// }
200// else
201// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202// for (uint32_t idx = 0; (create_callback =
203// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
204// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000205// {
206// PlatformSP platform_sp(create_callback(process, nullptr));
207// if (platform_sp)
208// return platform_sp;
209// }
210// }
211// return PlatformSP();
212//}
Jason Molenda1c627542013-04-05 01:03:25 +0000213
Zachary Turner97206d52017-05-12 04:51:55 +0000214Status Platform::GetSharedModule(const ModuleSpec &module_spec,
215 Process *process, ModuleSP &module_sp,
216 const FileSpecList *module_search_paths_ptr,
217 ModuleSP *old_module_sp_ptr,
218 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 if (IsHost())
220 return ModuleList::GetSharedModule(
221 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
222 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000223
Pavel Labath1f8639a2018-08-28 16:32:46 +0000224 // Module resolver lambda.
225 auto resolver = [&](const ModuleSpec &spec) {
226 Status error(eErrorTypeGeneric);
227 ModuleSpec resolved_spec;
228 // Check if we have sysroot set.
229 if (m_sdk_sysroot) {
230 // Prepend sysroot to module spec.
231 resolved_spec = spec;
232 resolved_spec.GetFileSpec().PrependPathComponent(
233 m_sdk_sysroot.GetStringRef());
234 // Try to get shared module with resolved spec.
235 error = ModuleList::GetSharedModule(
236 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
237 did_create_ptr, false);
238 }
239 // If we don't have sysroot or it didn't work then
240 // try original module spec.
241 if (!error.Success()) {
242 resolved_spec = spec;
243 error = ModuleList::GetSharedModule(
244 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
245 did_create_ptr, false);
246 }
247 if (error.Success() && module_sp)
248 module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
249 return error;
250 };
251
252 return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
257 const ArchSpec &arch, ModuleSpec &module_spec) {
258 ModuleSpecList module_specs;
259 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
260 module_specs) == 0)
261 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 ModuleSpec matched_module_spec;
264 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
265 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000266}
267
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000268PlatformSP Platform::Find(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 if (name) {
270 static ConstString g_host_platform_name("host");
271 if (name == g_host_platform_name)
272 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
275 for (const auto &platform_sp : GetPlatformList()) {
276 if (platform_sp->GetName() == name)
277 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 }
280 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000281}
282
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000283PlatformSP Platform::Create(ConstString name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 PlatformCreateInstance create_callback = nullptr;
285 lldb::PlatformSP platform_sp;
286 if (name) {
287 static ConstString g_host_platform_name("host");
288 if (name == g_host_platform_name)
289 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 create_callback =
292 PluginManager::GetPlatformCreateCallbackForPluginName(name);
293 if (create_callback)
294 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000295 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 error.SetErrorStringWithFormat(
297 "unable to find a plug-in for the platform named \"%s\"",
298 name.GetCString());
299 } else
300 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000301
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 if (platform_sp) {
303 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
304 GetPlatformList().push_back(platform_sp);
305 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000306
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000308}
309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000311 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 lldb::PlatformSP platform_sp;
313 if (arch.IsValid()) {
314 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000315 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 // First try exact arch matches across all platforms already created
317 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
318 for (const auto &platform_sp : GetPlatformList()) {
319 if (platform_sp->IsCompatibleArchitecture(arch, true,
320 platform_arch_ptr))
321 return platform_sp;
322 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 // Next try compatible arch matches across all platforms already created
325 for (const auto &platform_sp : GetPlatformList()) {
326 if (platform_sp->IsCompatibleArchitecture(arch, false,
327 platform_arch_ptr))
328 return platform_sp;
329 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000330 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331
332 PlatformCreateInstance create_callback;
333 // First try exact arch matches across all platform plug-ins
334 uint32_t idx;
335 for (idx = 0; (create_callback =
336 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
337 ++idx) {
338 if (create_callback) {
339 platform_sp = create_callback(false, &arch);
340 if (platform_sp &&
341 platform_sp->IsCompatibleArchitecture(arch, true,
342 platform_arch_ptr)) {
343 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
344 GetPlatformList().push_back(platform_sp);
345 return platform_sp;
346 }
347 }
348 }
349 // Next try compatible arch matches across all platform plug-ins
350 for (idx = 0; (create_callback =
351 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
352 ++idx) {
353 if (create_callback) {
354 platform_sp = create_callback(false, &arch);
355 if (platform_sp &&
356 platform_sp->IsCompatibleArchitecture(arch, false,
357 platform_arch_ptr)) {
358 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
359 GetPlatformList().push_back(platform_sp);
360 return platform_sp;
361 }
362 }
363 }
364 } else
365 error.SetErrorString("invalid platform name");
366 if (platform_arch_ptr)
367 platform_arch_ptr->Clear();
368 platform_sp.reset();
369 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000370}
371
Pavel Labath7263f1b2017-10-31 10:56:03 +0000372ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
373 if (platform)
374 return platform->GetAugmentedArchSpec(triple);
375 return HostInfo::GetAugmentedArchSpec(triple);
376}
377
Greg Claytone996fd32011-03-08 22:40:15 +0000378/// Default Constructor
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000379Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 : m_is_host(is_host), m_os_version_set_while_connected(false),
381 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000382 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000383 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
384 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
385 m_ignores_remote_hostname(false), m_trap_handlers(),
386 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 m_module_cache(llvm::make_unique<ModuleCache>()) {
388 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000389 LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000390}
391
Greg Claytone996fd32011-03-08 22:40:15 +0000392/// Destructor.
393///
394/// The destructor is virtual since this class is designed to be
395/// inherited from by the plug-in instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396Platform::~Platform() {
397 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000398 LLDB_LOGF(log, "%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000399}
400
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 std::string s;
403 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 ArchSpec arch(GetSystemArchitecture());
406 if (arch.IsValid()) {
407 if (!arch.GetTriple().str().empty()) {
408 strm.Printf(" Triple: ");
409 arch.DumpTriple(strm);
410 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000413
Pavel Labath2272c482018-06-18 15:02:23 +0000414 llvm::VersionTuple os_version = GetOSVersion();
415 if (!os_version.empty()) {
416 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 if (GetOSBuildString(s))
419 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 strm.EOL();
422 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 if (GetOSKernelDescription(s))
425 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 if (IsHost()) {
428 strm.Printf(" Hostname: %s\n", GetHostname());
429 } else {
430 const bool is_connected = IsConnected();
431 if (is_connected)
432 strm.Printf(" Hostname: %s\n", GetHostname());
433 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
434 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 if (GetWorkingDirectory()) {
437 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
438 }
439 if (!IsConnected())
440 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 std::string specific_info(GetPlatformSpecificConnectionInformation());
443
444 if (!specific_info.empty())
445 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000446}
447
Pavel Labath2272c482018-06-18 15:02:23 +0000448llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000450
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000452 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000454 m_os_version = HostInfo::GetOSVersion();
455 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000456 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 } else {
458 // We have a remote platform. We can only fetch the remote
459 // OS version if we are connected, and we don't want to do it
460 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000461
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000465 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000466 // We have valid OS version info, check to make sure it wasn't manually
467 // set prior to connecting. If it was manually set prior to connecting,
468 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 if (is_connected && !m_os_version_set_while_connected)
470 fetch = true;
471 } else {
472 // We don't have valid OS version info, fetch it if we are connected
473 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000474 }
475
Pavel Labath2272c482018-06-18 15:02:23 +0000476 if (fetch)
477 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 }
479
Pavel Labath2272c482018-06-18 15:02:23 +0000480 if (!m_os_version.empty())
481 return m_os_version;
482 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000483 // Check with the process in case it can answer the question if a process
484 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000485 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 }
Pavel Labath2272c482018-06-18 15:02:23 +0000487 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000488}
Greg Clayton1cb64962011-03-24 04:28:38 +0000489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490bool Platform::GetOSBuildString(std::string &s) {
491 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000492
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000494#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000496#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000498#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 else
500 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000501}
502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503bool Platform::GetOSKernelDescription(std::string &s) {
504 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000505#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000507#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000509#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 else
511 return GetRemoteOSKernelDescription(s);
512}
513
514void Platform::AddClangModuleCompilationOptions(
515 Target *target, std::vector<std::string> &options) {
516 std::vector<std::string> default_compilation_options = {
517 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
518
519 options.insert(options.end(), default_compilation_options.begin(),
520 default_compilation_options.end());
521}
522
523FileSpec Platform::GetWorkingDirectory() {
524 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000525 llvm::SmallString<64> cwd;
526 if (llvm::sys::fs::current_path(cwd))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000527 return {};
528 else {
529 FileSpec file_spec(cwd);
530 FileSystem::Instance().Resolve(file_spec);
531 return file_spec;
532 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 } else {
534 if (!m_working_dir)
535 m_working_dir = GetRemoteWorkingDirectory();
536 return m_working_dir;
537 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000538}
539
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540struct RecurseCopyBaton {
541 const FileSpec &dst;
542 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000543 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000544};
545
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000546static FileSystem::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000547RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000548 llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000550 FileSpec src(path);
Zachary Turner7d86ee52017-03-08 17:56:08 +0000551 namespace fs = llvm::sys::fs;
552 switch (ft) {
553 case fs::file_type::fifo_file:
554 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 // we have no way to copy pipes and sockets - ignore them and continue
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000556 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000558
Zachary Turner7d86ee52017-03-08 17:56:08 +0000559 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 // make the new directory and get in there
561 FileSpec dst_dir = rc_baton->dst;
562 if (!dst_dir.GetFilename())
563 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000564 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 dst_dir, lldb::eFilePermissionsDirectoryDefault);
566 if (error.Fail()) {
567 rc_baton->error.SetErrorStringWithFormat(
568 "unable to setup directory %s on remote end", dst_dir.GetCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000569 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571
572 // now recurse
573 std::string src_dir_path(src.GetPath());
574
Adrian Prantl05097242018-04-30 16:49:04 +0000575 // Make a filespec that only fills in the directory of a FileSpec so when
576 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 FileSpec recurse_dst;
578 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000579 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
580 Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000581 FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
582 RecurseCopy_Callback, &rc_baton2);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 if (rc_baton2.error.Fail()) {
584 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000585 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000587 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 } break;
589
Zachary Turner7d86ee52017-03-08 17:56:08 +0000590 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 // copy the file and keep going
592 FileSpec dst_file = rc_baton->dst;
593 if (!dst_file.GetFilename())
594 dst_file.GetFilename() = src.GetFilename();
595
596 FileSpec src_resolved;
597
Jonas Devlieghere46376962018-10-31 21:49:27 +0000598 rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599
600 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000601 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602
603 rc_baton->error =
604 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
605
606 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000607 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000609 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 } break;
611
Zachary Turner7d86ee52017-03-08 17:56:08 +0000612 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000613 // copy the file and keep going
614 FileSpec dst_file = rc_baton->dst;
615 if (!dst_file.GetFilename())
616 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000617 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 if (err.Fail()) {
619 rc_baton->error.SetErrorString(err.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000620 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000622 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 } break;
624
Zachary Turner7d86ee52017-03-08 17:56:08 +0000625 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626 rc_baton->error.SetErrorStringWithFormat(
627 "invalid file detected during copy: %s", src.GetPath().c_str());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000628 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 break;
630 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000631 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000632}
633
Zachary Turner97206d52017-05-12 04:51:55 +0000634Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
635 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636
637 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000638 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')",
639 src.GetPath().c_str(), dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 FileSpec fixed_dst(dst);
641
642 if (!fixed_dst.GetFilename())
643 fixed_dst.GetFilename() = src.GetFilename();
644
645 FileSpec working_dir = GetWorkingDirectory();
646
647 if (dst) {
648 if (dst.GetDirectory()) {
649 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
650 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
651 fixed_dst.GetDirectory() = dst.GetDirectory();
652 }
Adrian Prantl05097242018-04-30 16:49:04 +0000653 // If the fixed destination file doesn't have a directory yet, then we
654 // must have a relative path. We will resolve this relative path against
655 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 if (!fixed_dst.GetDirectory()) {
657 FileSpec relative_spec;
658 std::string path;
659 if (working_dir) {
660 relative_spec = working_dir;
661 relative_spec.AppendPathComponent(dst.GetPath());
662 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
663 } else {
664 error.SetErrorStringWithFormat(
665 "platform working directory must be valid for relative path '%s'",
666 dst.GetPath().c_str());
667 return error;
668 }
669 }
670 } else {
671 if (working_dir) {
672 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
673 } else {
674 error.SetErrorStringWithFormat(
675 "platform working directory must be valid for relative path '%s'",
676 dst.GetPath().c_str());
677 return error;
678 }
679 }
680 } else {
681 if (working_dir) {
682 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
683 } else {
684 error.SetErrorStringWithFormat("platform working directory must be valid "
685 "when destination directory is empty");
686 return error;
687 }
688 }
689
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000690 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
691 src.GetPath().c_str(), dst.GetPath().c_str(),
692 fixed_dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693
694 if (GetSupportsRSync()) {
695 error = PutFile(src, dst);
696 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000697 namespace fs = llvm::sys::fs;
698 switch (fs::get_file_type(src.GetPath(), false)) {
699 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000700 llvm::sys::fs::remove(fixed_dst.GetPath());
Jonas Devlieghere7c5310b2018-11-01 15:47:33 +0000701 uint32_t permissions = FileSystem::Instance().GetPermissions(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702 if (permissions == 0)
703 permissions = eFilePermissionsDirectoryDefault;
704 error = MakeDirectory(fixed_dst, permissions);
705 if (error.Success()) {
706 // Make a filespec that only fills in the directory of a FileSpec so
707 // when we enumerate we can quickly fill in the filename for dst copies
708 FileSpec recurse_dst;
709 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
710 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000711 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000712 FileSystem::Instance().EnumerateDirectory(
713 src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714 return baton.error;
715 }
716 } break;
717
Zachary Turner7d86ee52017-03-08 17:56:08 +0000718 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000719 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 error = PutFile(src, fixed_dst);
721 break;
722
Zachary Turner7d86ee52017-03-08 17:56:08 +0000723 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000724 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 FileSpec src_resolved;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000726 error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 if (error.Success())
728 error = CreateSymlink(dst, src_resolved);
729 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000730 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 error.SetErrorString("platform install doesn't handle pipes");
732 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000733 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734 error.SetErrorString("platform install doesn't handle sockets");
735 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000736 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 error.SetErrorString(
738 "platform install doesn't handle non file or directory items");
739 break;
740 }
741 }
742 return error;
743}
744
745bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
746 if (IsHost()) {
747 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000748 LLDB_LOG(log, "{0}", file_spec);
749 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
750 LLDB_LOG(log, "error: {0}", ec.message());
751 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000753 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 } else {
755 m_working_dir.Clear();
756 return SetRemoteWorkingDirectory(file_spec);
757 }
758}
759
Zachary Turner97206d52017-05-12 04:51:55 +0000760Status Platform::MakeDirectory(const FileSpec &file_spec,
761 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000763 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000765 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
767 GetPluginName().GetCString(),
768 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000769 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000771}
772
Zachary Turner97206d52017-05-12 04:51:55 +0000773Status Platform::GetFilePermissions(const FileSpec &file_spec,
774 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000775 if (IsHost()) {
776 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
777 if (Value)
778 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000779 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000780 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000781 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
783 GetPluginName().GetCString(),
784 LLVM_PRETTY_FUNCTION);
785 return error;
786 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000787}
788
Zachary Turner97206d52017-05-12 04:51:55 +0000789Status Platform::SetFilePermissions(const FileSpec &file_spec,
790 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000791 if (IsHost()) {
792 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
793 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
794 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000795 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
797 GetPluginName().GetCString(),
798 LLVM_PRETTY_FUNCTION);
799 return error;
800 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000801}
802
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803ConstString Platform::GetName() { return GetPluginName(); }
804
805const char *Platform::GetHostname() {
806 if (IsHost())
807 return "127.0.0.1";
808
809 if (m_name.empty())
810 return nullptr;
811 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000812}
813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814ConstString Platform::GetFullNameForDylib(ConstString basename) {
815 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000816}
817
Kate Stoneb9c1b512016-09-06 20:57:50 +0000818bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
819 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000820 LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')",
821 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 m_working_dir = working_dir;
823 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000824}
825
Pavel Labath2272c482018-06-18 15:02:23 +0000826bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000828 // We don't need anyone setting the OS version for the host platform, we
829 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000830 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000832 // We have a remote platform, allow setting the target OS version if we
833 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 // request the remote OS version from the connected platform.
835 if (IsConnected())
836 return false;
837 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000838 // We aren't connected and we might want to set the OS version ahead of
839 // time before we connect so we can peruse files and use a local SDK or
840 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000841 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000843 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 }
845 return false;
846}
847
Zachary Turner97206d52017-05-12 04:51:55 +0000848Status
849Platform::ResolveExecutable(const ModuleSpec &module_spec,
850 lldb::ModuleSP &exe_module_sp,
851 const FileSpecList *module_search_paths_ptr) {
852 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000853 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 if (module_spec.GetArchitecture().IsValid()) {
855 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
856 module_search_paths_ptr, nullptr,
857 nullptr);
858 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000859 // No valid architecture was specified, ask the platform for the
860 // architectures that we should be using (in the correct order) and see
861 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 ModuleSpec arch_module_spec(module_spec);
863 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
864 idx, arch_module_spec.GetArchitecture());
865 ++idx) {
866 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
867 module_search_paths_ptr, nullptr,
868 nullptr);
869 // Did we find an executable using one of the
870 if (error.Success() && exe_module_sp)
871 break;
872 }
Greg Claytone996fd32011-03-08 22:40:15 +0000873 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 } else {
875 error.SetErrorStringWithFormat("'%s' does not exist",
876 module_spec.GetFileSpec().GetPath().c_str());
877 }
878 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000879}
880
Zachary Turner97206d52017-05-12 04:51:55 +0000881Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
882 FileSpec &sym_file) {
883 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000884 if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 sym_file = sym_spec.GetSymbolFileSpec();
886 else
887 error.SetErrorString("unable to resolve symbol file");
888 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000889}
890
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891bool Platform::ResolveRemotePath(const FileSpec &platform_path,
892 FileSpec &resolved_platform_path) {
893 resolved_platform_path = platform_path;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000894 FileSystem::Instance().Resolve(resolved_platform_path);
895 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896}
897
898const ArchSpec &Platform::GetSystemArchitecture() {
899 if (IsHost()) {
900 if (!m_system_arch.IsValid()) {
901 // We have a local host platform
902 m_system_arch = HostInfo::GetArchitecture();
903 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000906 // We have a remote platform. We can only fetch the remote system
907 // architecture if we are connected, and we don't want to do it more than
908 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000909
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 bool fetch = false;
913 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000914 // We have valid OS version info, check to make sure it wasn't manually
915 // set prior to connecting. If it was manually set prior to connecting,
916 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 if (is_connected && !m_system_arch_set_while_connected)
918 fetch = true;
919 } else {
920 // We don't have valid OS version info, fetch it if we are connected
921 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000922 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923
924 if (fetch) {
925 m_system_arch = GetRemoteSystemArchitecture();
926 m_system_arch_set_while_connected = m_system_arch.IsValid();
927 }
928 }
929 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000930}
931
Pavel Labath7263f1b2017-10-31 10:56:03 +0000932ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
933 if (triple.empty())
934 return ArchSpec();
935 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
936 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
937 return ArchSpec(triple);
938
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000939 if (auto kind = HostInfo::ParseArchitectureKind(triple))
940 return HostInfo::GetArchitecture(*kind);
941
Pavel Labath7263f1b2017-10-31 10:56:03 +0000942 ArchSpec compatible_arch;
943 ArchSpec raw_arch(triple);
944 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
945 return raw_arch;
946
947 if (!compatible_arch.IsValid())
948 return ArchSpec(normalized_triple);
949
950 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
951 if (normalized_triple.getVendorName().empty())
952 normalized_triple.setVendor(compatible_triple.getVendor());
953 if (normalized_triple.getOSName().empty())
954 normalized_triple.setOS(compatible_triple.getOS());
955 if (normalized_triple.getEnvironmentName().empty())
956 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
957 return ArchSpec(normalized_triple);
958}
959
Zachary Turner97206d52017-05-12 04:51:55 +0000960Status Platform::ConnectRemote(Args &args) {
961 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962 if (IsHost())
963 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
964 "the host platform and is always connected.",
965 GetPluginName().GetCString());
966 else
967 error.SetErrorStringWithFormat(
968 "Platform::ConnectRemote() is not supported by %s",
969 GetPluginName().GetCString());
970 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000971}
972
Zachary Turner97206d52017-05-12 04:51:55 +0000973Status Platform::DisconnectRemote() {
974 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 if (IsHost())
976 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
977 "the host platform and is always connected.",
978 GetPluginName().GetCString());
979 else
980 error.SetErrorStringWithFormat(
981 "Platform::DisconnectRemote() is not supported by %s",
982 GetPluginName().GetCString());
983 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000984}
Greg Clayton32e0a752011-03-30 18:16:51 +0000985
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986bool Platform::GetProcessInfo(lldb::pid_t pid,
987 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000988 // Take care of the host case so that each subclass can just call this
989 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 if (IsHost())
991 return Host::GetProcessInfo(pid, process_info);
992 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000993}
994
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
996 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +0000997 // Take care of the host case so that each subclass can just call this
998 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 uint32_t match_count = 0;
1000 if (IsHost())
1001 match_count = Host::FindProcesses(match_info, process_infos);
1002 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001003}
Greg Clayton8b82f082011-04-12 05:54:46 +00001004
Zachary Turner97206d52017-05-12 04:51:55 +00001005Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1006 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001008 LLDB_LOGF(log, "Platform::%s()", __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009
Adrian Prantl05097242018-04-30 16:49:04 +00001010 // Take care of the host case so that each subclass can just call this
1011 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 if (IsHost()) {
1013 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1014 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1015
1016 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1017 const bool is_localhost = true;
1018 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1019 const bool first_arg_is_full_shell_command = false;
1020 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1021 if (log) {
1022 const FileSpec &shell = launch_info.GetShell();
Jason Molenda3b687612019-02-08 23:36:25 +00001023 std::string shell_str = (shell) ? shell.GetPath() : "<null>";
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001024 LLDB_LOGF(log,
1025 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1026 ", shell is '%s'",
1027 __FUNCTION__, num_resumes, shell_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028 }
1029
1030 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1031 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1032 num_resumes))
1033 return error;
1034 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1035 error = ShellExpandArguments(launch_info);
1036 if (error.Fail()) {
1037 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1038 "consider launching with 'process "
1039 "launch'.",
1040 error.AsCString("unknown"));
1041 return error;
1042 }
1043 }
1044
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001045 LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32,
1046 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001047
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 error = Host::LaunchProcess(launch_info);
1049 } else
1050 error.SetErrorString(
1051 "base lldb_private::Platform class can't launch remote processes");
1052 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001053}
1054
Zachary Turner97206d52017-05-12 04:51:55 +00001055Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 if (IsHost())
1057 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001058 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001059}
1060
Zachary Turner97206d52017-05-12 04:51:55 +00001061Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001063 LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001066 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 size_t num_debuggers = Debugger::GetNumDebuggers();
1068 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1069 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1070 lldb_private::TargetList &targets = debugger->GetTargetList();
1071 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1072 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1073 if (process->GetID() == pid)
1074 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001077
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001079 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 "base lldb_private::Platform class can't kill remote processes unless "
1081 "they are controlled by a process plugin");
1082 }
1083 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001084 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001085}
1086
Greg Clayton8b82f082011-04-12 05:54:46 +00001087lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1089 Target *target, // Can be nullptr, if nullptr create a
1090 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001091 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001093 LLDB_LOGF(log, "Platform::%s entered (target %p)", __FUNCTION__,
1094 static_cast<void *>(target));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095
1096 ProcessSP process_sp;
1097 // Make sure we stop at the entry point
1098 launch_info.GetFlags().Set(eLaunchFlagDebug);
1099 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001100 // group, since then we can handle ^C interrupts ourselves w/o having to
1101 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 launch_info.SetLaunchInSeparateProcessGroup(true);
1103
1104 // Allow any StructuredData process-bound plugins to adjust the launch info
1105 // if needed
1106 size_t i = 0;
1107 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001108 // Note iteration can't simply go until a nullptr callback is returned, as it
1109 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1111 for (auto filter_callback = get_filter_func(i, iteration_complete);
1112 !iteration_complete;
1113 filter_callback = get_filter_func(++i, iteration_complete)) {
1114 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001115 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 error = (*filter_callback)(launch_info, target);
1117 if (!error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001118 LLDB_LOGF(log,
1119 "Platform::%s() StructuredDataPlugin launch "
1120 "filter failed.",
1121 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 return process_sp;
1123 }
1124 }
1125 }
1126
1127 error = LaunchProcess(launch_info);
1128 if (error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001129 LLDB_LOGF(log,
1130 "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")",
1131 __FUNCTION__, launch_info.GetProcessID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1133 ProcessAttachInfo attach_info(launch_info);
1134 process_sp = Attach(attach_info, debugger, target, error);
1135 if (process_sp) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001136 LLDB_LOGF(log, "Platform::%s Attach() succeeded, Process plugin: %s",
1137 __FUNCTION__, process_sp->GetPluginName().AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001139
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 // Since we attached to the process, it will think it needs to detach
1141 // if the process object just goes away without an explicit call to
1142 // Process::Kill() or Process::Detach(), so let it know to kill the
1143 // process if this happens.
1144 process_sp->SetShouldDetach(false);
1145
Adrian Prantl05097242018-04-30 16:49:04 +00001146 // If we didn't have any file actions, the pseudo terminal might have
1147 // been used where the slave side was given as the file to open for
1148 // stdin/out/err after we have already opened the master so we can
1149 // read/write stdin/out/err.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001151 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152 process_sp->SetSTDIOFileDescriptor(pty_fd);
1153 }
1154 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001155 LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__,
1156 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 }
1158 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001159 LLDB_LOGF(log,
1160 "Platform::%s LaunchProcess() returned launch_info with "
1161 "invalid process id",
1162 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 }
1164 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001165 LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1166 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167 }
1168
1169 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001170}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001171
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001172lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1174 ArchSpec *platform_arch_ptr) {
1175 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001176 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 if (arch.IsValid())
1178 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1179 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001180}
1181
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001182/// Lets a platform answer if it is compatible with a given
1183/// architecture and the target triple contained within.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1185 bool exact_arch_match,
1186 ArchSpec *compatible_arch_ptr) {
1187 // If the architecture is invalid, we must answer true...
1188 if (arch.IsValid()) {
1189 ArchSpec platform_arch;
1190 // Try for an exact architecture match first.
1191 if (exact_arch_match) {
1192 for (uint32_t arch_idx = 0;
1193 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1194 ++arch_idx) {
1195 if (arch.IsExactMatch(platform_arch)) {
1196 if (compatible_arch_ptr)
1197 *compatible_arch_ptr = platform_arch;
1198 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001199 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200 }
1201 } else {
1202 for (uint32_t arch_idx = 0;
1203 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1204 ++arch_idx) {
1205 if (arch.IsCompatibleMatch(platform_arch)) {
1206 if (compatible_arch_ptr)
1207 *compatible_arch_ptr = platform_arch;
1208 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001209 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 }
1213 if (compatible_arch_ptr)
1214 compatible_arch_ptr->Clear();
1215 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001216}
1217
Zachary Turner97206d52017-05-12 04:51:55 +00001218Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1219 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001221 LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001222
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 uint32_t source_open_options =
1224 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001225 namespace fs = llvm::sys::fs;
1226 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001227 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001228
Jonas Devlieghere50bc1ed2018-11-02 22:34:51 +00001229 File source_file;
1230 Status error = FileSystem::Instance().Open(
1231 source_file, source, source_open_options, lldb::eFilePermissionsUserRW);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232 uint32_t permissions = source_file.GetPermissions(error);
1233 if (permissions == 0)
1234 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001235
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001237 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238 lldb::user_id_t dest_file = OpenFile(
1239 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1240 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1241 permissions, error);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001242 LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001243
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 if (error.Fail())
1245 return error;
1246 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001247 return Status("unable to open target file");
Jason Molendaeb9902a2018-12-07 00:35:26 +00001248 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 uint64_t offset = 0;
1250 for (;;) {
1251 size_t bytes_read = buffer_sp->GetByteSize();
1252 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1253 if (error.Fail() || bytes_read == 0)
1254 break;
1255
1256 const uint64_t bytes_written =
1257 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001258 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001260
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 offset += bytes_written;
1262 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001263 // We didn't write the correct number of bytes, so adjust the file
1264 // position in the source file we are reading from...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001265 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001266 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 }
1268 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001269
Kate Stoneb9c1b512016-09-06 20:57:50 +00001270 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001271 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272
1273 // TODO: ChownFile?
1274
1275 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001276}
1277
Zachary Turner97206d52017-05-12 04:51:55 +00001278Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1279 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001281}
1282
Zachary Turner97206d52017-05-12 04:51:55 +00001283Status
1284Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1285 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001286{
Zachary Turner97206d52017-05-12 04:51:55 +00001287 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001289}
1290
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1292 return false;
1293}
1294
Zachary Turner97206d52017-05-12 04:51:55 +00001295Status Platform::Unlink(const FileSpec &path) {
1296 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297 return error;
1298}
1299
Ed Maste37c40af2017-08-16 12:55:02 +00001300MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1301 addr_t length, unsigned prot,
1302 unsigned flags, addr_t fd,
1303 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 uint64_t flags_platform = 0;
1305 if (flags & eMmapFlagsPrivate)
1306 flags_platform |= MAP_PRIVATE;
1307 if (flags & eMmapFlagsAnon)
1308 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001309
1310 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1311 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312}
1313
Zachary Turner97206d52017-05-12 04:51:55 +00001314lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 const char *command, // Shouldn't be nullptr
1316 const FileSpec &
1317 working_dir, // Pass empty FileSpec to use the current working directory
1318 int *status_ptr, // Pass nullptr if you don't want the process exit status
1319 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1320 // process to exit
1321 std::string
1322 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001323 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 if (IsHost())
1325 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
Pavel Labath19dd1a02018-05-10 10:46:03 +00001326 command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 else
Zachary Turner97206d52017-05-12 04:51:55 +00001328 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329}
1330
1331bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1332 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001333 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001334 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001335 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1336 if (!Result)
1337 return false;
1338 std::tie(high, low) = Result->words();
1339 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001340}
1341
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342void Platform::SetLocalCacheDirectory(const char *local) {
1343 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001344}
1345
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346const char *Platform::GetLocalCacheDirectory() {
1347 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001348}
Greg Claytonfbb76342013-11-20 21:07:01 +00001349
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001350static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001352 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001354 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 "Platform-specific options required for rsync to work."},
1356 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001357 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 "Platform-specific rsync prefix put before the remote path."},
1359 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001360 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 "Do not automatically fill in the remote hostname when composing the "
1362 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001363};
1364
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001365static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001367 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001369 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001371};
1372
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001373static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001375 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001377};
1378
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001379llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001380 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001381}
1382
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383void OptionGroupPlatformRSync::OptionParsingStarting(
1384 ExecutionContext *execution_context) {
1385 m_rsync = false;
1386 m_rsync_opts.clear();
1387 m_rsync_prefix.clear();
1388 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001389}
1390
Zachary Turner97206d52017-05-12 04:51:55 +00001391lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001392OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001393 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001394 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001395 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 char short_option = (char)GetDefinitions()[option_idx].short_option;
1397 switch (short_option) {
1398 case 'r':
1399 m_rsync = true;
1400 break;
1401
1402 case 'R':
1403 m_rsync_opts.assign(option_arg);
1404 break;
1405
1406 case 'P':
1407 m_rsync_prefix.assign(option_arg);
1408 break;
1409
1410 case 'i':
1411 m_ignores_remote_hostname = true;
1412 break;
1413
1414 default:
1415 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1416 break;
1417 }
1418
1419 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001420}
1421
Greg Clayton4116e932012-05-15 02:33:01 +00001422lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1424 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001425}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001426
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001427llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001428 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001429}
1430
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431void OptionGroupPlatformSSH::OptionParsingStarting(
1432 ExecutionContext *execution_context) {
1433 m_ssh = false;
1434 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001435}
1436
Zachary Turner97206d52017-05-12 04:51:55 +00001437lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001438OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001439 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001441 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 char short_option = (char)GetDefinitions()[option_idx].short_option;
1443 switch (short_option) {
1444 case 's':
1445 m_ssh = true;
1446 break;
1447
1448 case 'S':
1449 m_ssh_opts.assign(option_arg);
1450 break;
1451
1452 default:
1453 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1454 break;
1455 }
1456
1457 return error;
1458}
1459
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001460llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001461 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462}
1463
1464void OptionGroupPlatformCaching::OptionParsingStarting(
1465 ExecutionContext *execution_context) {
1466 m_cache_dir.clear();
1467}
1468
Zachary Turner97206d52017-05-12 04:51:55 +00001469lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001470 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001472 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 char short_option = (char)GetDefinitions()[option_idx].short_option;
1474 switch (short_option) {
1475 case 'c':
1476 m_cache_dir.assign(option_arg);
1477 break;
1478
1479 default:
1480 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1481 break;
1482 }
1483
1484 return error;
1485}
1486
Pavel Labath62930e52018-01-10 11:57:31 +00001487Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488
1489const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1490 if (!m_calculated_trap_handlers) {
1491 std::lock_guard<std::mutex> guard(m_mutex);
1492 if (!m_calculated_trap_handlers) {
1493 CalculateTrapHandlerSymbolNames();
1494 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001495 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 }
1497 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001498}
1499
Zachary Turner97206d52017-05-12 04:51:55 +00001500Status Platform::GetCachedExecutable(
1501 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1502 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 const auto platform_spec = module_spec.GetFileSpec();
1504 const auto error = LoadCachedExecutable(
1505 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1506 if (error.Success()) {
1507 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1508 module_spec.GetPlatformFileSpec() = platform_spec;
1509 }
1510
1511 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001512}
1513
Zachary Turner97206d52017-05-12 04:51:55 +00001514Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1516 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1517 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1518 [&](const ModuleSpec &spec) {
1519 return remote_platform.ResolveExecutable(
1520 spec, module_sp, module_search_paths_ptr);
1521 },
1522 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001523}
1524
Zachary Turner97206d52017-05-12 04:51:55 +00001525Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1526 Process *process,
1527 lldb::ModuleSP &module_sp,
1528 const ModuleResolver &module_resolver,
1529 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 // Get module information from a target.
1531 ModuleSpec resolved_module_spec;
1532 bool got_module_spec = false;
1533 if (process) {
1534 // Try to get module information from the process
1535 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1536 module_spec.GetArchitecture(),
1537 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001538 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1540 got_module_spec = true;
1541 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001544
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001545 if (!module_spec.GetArchitecture().IsValid()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001546 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001547 // No valid architecture was specified, ask the platform for the
1548 // architectures that we should be using (in the correct order) and see if
1549 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001550 ModuleSpec arch_module_spec(module_spec);
1551 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1552 idx, arch_module_spec.GetArchitecture());
1553 ++idx) {
1554 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1555 nullptr, nullptr);
1556 // Did we find an executable using one of the
1557 if (error.Success() && module_sp)
1558 break;
1559 }
1560 if (module_sp)
1561 got_module_spec = true;
1562 }
1563
Kate Stoneb9c1b512016-09-06 20:57:50 +00001564 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001565 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1567 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001568 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1570 return module_resolver(module_spec);
1571 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001574
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 // If we are looking for a specific UUID, make sure resolved_module_spec has
1576 // the same one before we search.
1577 if (module_spec.GetUUID().IsValid()) {
1578 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1579 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001580
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 // Trying to find a module by UUID on local file system.
1582 const auto error = module_resolver(resolved_module_spec);
1583 if (error.Fail()) {
1584 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001585 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001587
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001589}
1590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1592 lldb::ModuleSP &module_sp,
1593 bool *did_create_ptr) {
1594 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1595 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001596 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597
1598 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1599
1600 // Check local cache for a module.
1601 auto error = m_module_cache->GetAndPut(
1602 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1603 [this](const ModuleSpec &module_spec,
1604 const FileSpec &tmp_download_file_spec) {
1605 return DownloadModuleSlice(
1606 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1607 module_spec.GetObjectSize(), tmp_download_file_spec);
1608
1609 },
1610 [this](const ModuleSP &module_sp,
1611 const FileSpec &tmp_download_file_spec) {
1612 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1613 },
1614 module_sp, did_create_ptr);
1615 if (error.Success())
1616 return true;
1617
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001618 LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s",
1619 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1620 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001622}
1623
Zachary Turner97206d52017-05-12 04:51:55 +00001624Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1625 const uint64_t src_offset,
1626 const uint64_t src_size,
1627 const FileSpec &dst_file_spec) {
1628 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001629
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001630 std::error_code EC;
1631 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1632 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 error.SetErrorStringWithFormat("unable to open destination file: %s",
1634 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001635 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001637
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1639 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001640
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641 if (error.Fail()) {
1642 error.SetErrorStringWithFormat("unable to open source file: %s",
1643 error.AsCString());
1644 return error;
1645 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001646
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647 std::vector<char> buffer(1024);
1648 auto offset = src_offset;
1649 uint64_t total_bytes_read = 0;
1650 while (total_bytes_read < src_size) {
1651 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1652 src_size - total_bytes_read);
1653 const uint64_t n_read =
1654 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001655 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656 break;
1657 if (n_read == 0) {
1658 error.SetErrorString("read 0 bytes");
1659 break;
1660 }
1661 offset += n_read;
1662 total_bytes_read += n_read;
1663 dst.write(&buffer[0], n_read);
1664 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001665
Zachary Turner97206d52017-05-12 04:51:55 +00001666 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 CloseFile(src_fd, close_error); // Ignoring close error.
1668
1669 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001670}
1671
Zachary Turner97206d52017-05-12 04:51:55 +00001672Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1673 const FileSpec &dst_file_spec) {
1674 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001676}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001677
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678FileSpec Platform::GetModuleCacheRoot() {
1679 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1680 dir_spec.AppendPathComponent(GetName().AsCString());
1681 return dir_spec;
1682}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001683
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001685
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1687 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1688 return s_default_unix_signals_sp;
1689}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001690
Zachary Turnera89ce432019-03-06 18:20:23 +00001691UnixSignalsSP Platform::GetUnixSignals() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001692 if (IsHost())
Zachary Turnera89ce432019-03-06 18:20:23 +00001693 return UnixSignals::CreateForHost();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001694 return GetRemoteUnixSignals();
1695}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001696
Kate Stoneb9c1b512016-09-06 20:57:50 +00001697uint32_t Platform::LoadImage(lldb_private::Process *process,
1698 const lldb_private::FileSpec &local_file,
1699 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001700 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701 if (local_file && remote_file) {
1702 // Both local and remote file was specified. Install the local file to the
1703 // given location.
1704 if (IsRemote() || local_file != remote_file) {
1705 error = Install(local_file, remote_file);
1706 if (error.Fail())
1707 return LLDB_INVALID_IMAGE_TOKEN;
1708 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001709 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001711
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712 if (local_file) {
1713 // Only local file was specified. Install it to the current working
1714 // directory.
1715 FileSpec target_file = GetWorkingDirectory();
1716 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1717 if (IsRemote() || local_file != target_file) {
1718 error = Install(local_file, target_file);
1719 if (error.Fail())
1720 return LLDB_INVALID_IMAGE_TOKEN;
1721 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001722 return DoLoadImage(process, target_file, nullptr, error);
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001723 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001724
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725 if (remote_file) {
1726 // Only remote file was specified so we don't have to do any copying
Jim Ingham0d231f72018-06-28 20:02:11 +00001727 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001728 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001729
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730 error.SetErrorString("Neither local nor remote file was specified");
1731 return LLDB_INVALID_IMAGE_TOKEN;
1732}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001733
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1735 const lldb_private::FileSpec &remote_file,
Jim Ingham0d231f72018-06-28 20:02:11 +00001736 const std::vector<std::string> *paths,
1737 lldb_private::Status &error,
1738 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001739 error.SetErrorString("LoadImage is not supported on the current platform");
1740 return LLDB_INVALID_IMAGE_TOKEN;
1741}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001742
Jim Ingham0d231f72018-06-28 20:02:11 +00001743uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1744 const lldb_private::FileSpec &remote_filename,
1745 const std::vector<std::string> &paths,
1746 lldb_private::Status &error,
1747 lldb_private::FileSpec *loaded_path)
1748{
1749 FileSpec file_to_use;
1750 if (remote_filename.IsAbsolute())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001751 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1752
1753 remote_filename.GetPathStyle());
Jim Ingham0d231f72018-06-28 20:02:11 +00001754 else
1755 file_to_use = remote_filename;
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001756
Jim Ingham0d231f72018-06-28 20:02:11 +00001757 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1758}
1759
Zachary Turner97206d52017-05-12 04:51:55 +00001760Status Platform::UnloadImage(lldb_private::Process *process,
1761 uint32_t image_token) {
1762 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001764
Zachary Turner31659452016-11-17 21:15:14 +00001765lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1766 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001767 lldb_private::Debugger &debugger,
1768 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001769 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001771
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772 if (!target) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001773 ArchSpec arch;
1774 if (target && target->GetArchitecture().IsValid())
1775 arch = target->GetArchitecture();
1776 else
1777 arch = Target::GetDefaultArchitecture();
1778
1779 const char *triple = "";
1780 if (arch.IsValid())
1781 triple = arch.GetTriple().getTriple().c_str();
1782
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001784 error = debugger.GetTargetList().CreateTarget(
Jason Molendadf9f7962018-10-23 23:45:56 +00001785 debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 target = new_target_sp.get();
1787 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001788
Kate Stoneb9c1b512016-09-06 20:57:50 +00001789 if (!target || error.Fail())
1790 return nullptr;
1791
1792 debugger.GetTargetList().SetSelectedTarget(target);
1793
1794 lldb::ProcessSP process_sp =
1795 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1796 if (!process_sp)
1797 return nullptr;
1798
1799 error =
1800 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1801 if (error.Fail())
1802 return nullptr;
1803
1804 return process_sp;
1805}
1806
1807size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001808 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001809 error.Clear();
1810 return 0;
1811}
1812
1813size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1814 BreakpointSite *bp_site) {
1815 ArchSpec arch = target.GetArchitecture();
1816 const uint8_t *trap_opcode = nullptr;
1817 size_t trap_opcode_size = 0;
1818
1819 switch (arch.GetMachine()) {
1820 case llvm::Triple::aarch64: {
1821 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1822 trap_opcode = g_aarch64_opcode;
1823 trap_opcode_size = sizeof(g_aarch64_opcode);
1824 } break;
1825
1826 // TODO: support big-endian arm and thumb trap codes.
1827 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001828 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1829 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1831 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1832
1833 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001834 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001835
1836 if (bp_loc_sp) {
1837 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001838 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001839 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001840 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001841 }
1842
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001843 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844 trap_opcode = g_thumb_breakpoint_opcode;
1845 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1846 } else {
1847 trap_opcode = g_arm_breakpoint_opcode;
1848 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1849 }
1850 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001851
Kate Stoneb9c1b512016-09-06 20:57:50 +00001852 case llvm::Triple::mips:
1853 case llvm::Triple::mips64: {
1854 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1855 trap_opcode = g_hex_opcode;
1856 trap_opcode_size = sizeof(g_hex_opcode);
1857 } break;
1858
1859 case llvm::Triple::mipsel:
1860 case llvm::Triple::mips64el: {
1861 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1862 trap_opcode = g_hex_opcode;
1863 trap_opcode_size = sizeof(g_hex_opcode);
1864 } break;
1865
1866 case llvm::Triple::systemz: {
1867 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1868 trap_opcode = g_hex_opcode;
1869 trap_opcode_size = sizeof(g_hex_opcode);
1870 } break;
1871
1872 case llvm::Triple::hexagon: {
1873 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1874 trap_opcode = g_hex_opcode;
1875 trap_opcode_size = sizeof(g_hex_opcode);
1876 } break;
1877
1878 case llvm::Triple::ppc:
1879 case llvm::Triple::ppc64: {
1880 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1881 trap_opcode = g_ppc_opcode;
1882 trap_opcode_size = sizeof(g_ppc_opcode);
1883 } break;
1884
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001885 case llvm::Triple::ppc64le: {
1886 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1887 trap_opcode = g_ppc64le_opcode;
1888 trap_opcode_size = sizeof(g_ppc64le_opcode);
1889 } break;
1890
Kate Stoneb9c1b512016-09-06 20:57:50 +00001891 case llvm::Triple::x86:
1892 case llvm::Triple::x86_64: {
1893 static const uint8_t g_i386_opcode[] = {0xCC};
1894 trap_opcode = g_i386_opcode;
1895 trap_opcode_size = sizeof(g_i386_opcode);
1896 } break;
1897
1898 default:
David Blaikiea322f362017-01-06 00:38:06 +00001899 llvm_unreachable(
1900 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001901 }
1902
1903 assert(bp_site);
1904 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1905 return trap_opcode_size;
1906
1907 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001908}