blob: e5d1f47347f85a2760df31d639186ffd6564e15d [file] [log] [blame]
Greg Claytone996fd32011-03-08 22:40:15 +00001//===-- Platform.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Claytone996fd32011-03-08 22:40:15 +000010// C Includes
11// C++ Includes
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000012#include <algorithm>
Pavel Labathbc4987b2018-04-05 16:59:36 +000013#include <csignal>
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000014#include <fstream>
15#include <vector>
16
Greg Claytone996fd32011-03-08 22:40:15 +000017// Other libraries and framework includes
Eugene Zelenko9394d7722016-02-18 00:10:17 +000018#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20
Greg Claytone996fd32011-03-08 22:40:15 +000021// Project includes
Greg Clayton4116e932012-05-15 02:33:01 +000022#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000023#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000024#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000025#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000026#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000027#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000028#include "lldb/Core/StreamFile.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000029#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000030#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000031#include "lldb/Host/HostInfo.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000032#include "lldb/Host/OptionParser.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000033#include "lldb/Interpreter/OptionValueProperties.h"
34#include "lldb/Interpreter/Property.h"
35#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000036#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000037#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000038#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000039#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000040#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000041#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000042#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000043#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000044#include "lldb/Utility/Status.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000045#include "lldb/Utility/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000046
Zachary Turner7d86ee52017-03-08 17:56:08 +000047#include "llvm/Support/FileSystem.h"
48
Adrian Prantl05097242018-04-30 16:49:04 +000049// Define these constants from POSIX mman.h rather than include the file so
50// that they will be correct even when compiled on Linux.
Robert Flack96ad3de2015-05-09 15:53:31 +000051#define MAP_PRIVATE 2
52#define MAP_ANON 0x1000
53
Greg Claytone996fd32011-03-08 22:40:15 +000054using namespace lldb;
55using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000056
57static uint32_t g_initialize_count = 0;
58
Adrian Prantl05097242018-04-30 16:49:04 +000059// Use a singleton function for g_local_platform_sp to avoid init constructors
60// since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000061static PlatformSP &GetHostPlatformSP() {
62 static PlatformSP g_platform_sp;
63 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000064}
65
Kate Stoneb9c1b512016-09-06 20:57:50 +000066const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000067
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000068namespace {
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070PropertyDefinition g_properties[] = {
71 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
72 nullptr, "Use module cache."},
73 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
74 nullptr, "Root directory for cached modules."},
75 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077enum { ePropertyUseModuleCache, ePropertyModuleCacheDirectory };
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081ConstString PlatformProperties::GetSettingName() {
82 static ConstString g_setting_name("platform");
83 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086PlatformProperties::PlatformProperties() {
87 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
88 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 auto module_cache_dir = GetModuleCacheDirectory();
91 if (module_cache_dir)
92 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 llvm::SmallString<64> user_home_dir;
95 if (!llvm::sys::path::home_directory(user_home_dir))
96 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 module_cache_dir = FileSpec(user_home_dir.c_str(), false);
99 module_cache_dir.AppendPathComponent(".lldb");
100 module_cache_dir.AppendPathComponent("module_cache");
101 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104bool PlatformProperties::GetUseModuleCache() const {
105 const auto idx = ePropertyUseModuleCache;
106 return m_collection_sp->GetPropertyAtIndexAsBoolean(
107 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
111 return m_collection_sp->SetPropertyAtIndexAsBoolean(
112 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115FileSpec PlatformProperties::GetModuleCacheDirectory() const {
116 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
117 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000118}
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
121 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
122 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000123}
124
Greg Claytone996fd32011-03-08 22:40:15 +0000125//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000127///
128/// There should only be one of these for each host that LLDB runs
129/// upon that should be statically compiled in and registered using
130/// preprocessor macros or other similar build mechanisms.
131///
132/// This platform will be used as the default platform when launching
133/// or attaching to processes unless another platform is specified.
134//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
136
137static std::vector<PlatformSP> &GetPlatformList() {
138 static std::vector<PlatformSP> g_platform_list;
139 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142static std::recursive_mutex &GetPlatformListMutex() {
143 static std::recursive_mutex g_mutex;
144 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000145}
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149void Platform::Terminate() {
150 if (g_initialize_count > 0) {
151 if (--g_initialize_count == 0) {
152 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
153 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
159 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
160 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
164 // The native platform should use its static void Platform::Initialize()
165 // function to register itself as the native platform.
166 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 if (platform_sp) {
169 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
170 GetPlatformList().push_back(platform_sp);
171 }
Greg Claytone996fd32011-03-08 22:40:15 +0000172}
173
Zachary Turner97206d52017-05-12 04:51:55 +0000174Status Platform::GetFileWithUUID(const FileSpec &platform_file,
175 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 // Default to the local case
177 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000178 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000179}
180
Greg Clayton91c0e742013-01-11 23:44:27 +0000181FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182Platform::LocateExecutableScriptingResources(Target *target, Module &module,
183 Stream *feedback_stream) {
184 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187// PlatformSP
188// Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000189//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000190// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000191// if (plugin_name)
192// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193// create_callback =
194// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000195// if (create_callback)
196// {
197// ArchSpec arch;
198// if (process)
199// {
200// arch = process->GetTarget().GetArchitecture();
201// }
202// PlatformSP platform_sp(create_callback(process, &arch));
203// if (platform_sp)
204// return platform_sp;
205// }
206// }
207// else
208// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209// for (uint32_t idx = 0; (create_callback =
210// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
211// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000212// {
213// PlatformSP platform_sp(create_callback(process, nullptr));
214// if (platform_sp)
215// return platform_sp;
216// }
217// }
218// return PlatformSP();
219//}
Jason Molenda1c627542013-04-05 01:03:25 +0000220
Zachary Turner97206d52017-05-12 04:51:55 +0000221Status Platform::GetSharedModule(const ModuleSpec &module_spec,
222 Process *process, ModuleSP &module_sp,
223 const FileSpecList *module_search_paths_ptr,
224 ModuleSP *old_module_sp_ptr,
225 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 if (IsHost())
227 return ModuleList::GetSharedModule(
228 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
229 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 return GetRemoteSharedModule(module_spec, process, module_sp,
232 [&](const ModuleSpec &spec) {
Zachary Turner97206d52017-05-12 04:51:55 +0000233 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 spec, module_sp, module_search_paths_ptr,
235 old_module_sp_ptr, did_create_ptr, false);
236 if (error.Success() && module_sp)
237 module_sp->SetPlatformFileSpec(
238 spec.GetFileSpec());
239 return error;
240 },
241 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
245 const ArchSpec &arch, ModuleSpec &module_spec) {
246 ModuleSpecList module_specs;
247 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
248 module_specs) == 0)
249 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 ModuleSpec matched_module_spec;
252 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
253 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256PlatformSP Platform::Find(const ConstString &name) {
257 if (name) {
258 static ConstString g_host_platform_name("host");
259 if (name == g_host_platform_name)
260 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
263 for (const auto &platform_sp : GetPlatformList()) {
264 if (platform_sp->GetName() == name)
265 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000266 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 }
268 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000269}
270
Zachary Turner97206d52017-05-12 04:51:55 +0000271PlatformSP Platform::Create(const ConstString &name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 PlatformCreateInstance create_callback = nullptr;
273 lldb::PlatformSP platform_sp;
274 if (name) {
275 static ConstString g_host_platform_name("host");
276 if (name == g_host_platform_name)
277 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 create_callback =
280 PluginManager::GetPlatformCreateCallbackForPluginName(name);
281 if (create_callback)
282 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000283 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 error.SetErrorStringWithFormat(
285 "unable to find a plug-in for the platform named \"%s\"",
286 name.GetCString());
287 } else
288 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 if (platform_sp) {
291 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
292 GetPlatformList().push_back(platform_sp);
293 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000296}
297
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000299 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 lldb::PlatformSP platform_sp;
301 if (arch.IsValid()) {
302 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000303 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 // First try exact arch matches across all platforms already created
305 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
306 for (const auto &platform_sp : GetPlatformList()) {
307 if (platform_sp->IsCompatibleArchitecture(arch, true,
308 platform_arch_ptr))
309 return platform_sp;
310 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000311
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 // Next try compatible arch matches across all platforms already created
313 for (const auto &platform_sp : GetPlatformList()) {
314 if (platform_sp->IsCompatibleArchitecture(arch, false,
315 platform_arch_ptr))
316 return platform_sp;
317 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000318 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319
320 PlatformCreateInstance create_callback;
321 // First try exact arch matches across all platform plug-ins
322 uint32_t idx;
323 for (idx = 0; (create_callback =
324 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
325 ++idx) {
326 if (create_callback) {
327 platform_sp = create_callback(false, &arch);
328 if (platform_sp &&
329 platform_sp->IsCompatibleArchitecture(arch, true,
330 platform_arch_ptr)) {
331 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
332 GetPlatformList().push_back(platform_sp);
333 return platform_sp;
334 }
335 }
336 }
337 // Next try compatible arch matches across all platform plug-ins
338 for (idx = 0; (create_callback =
339 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
340 ++idx) {
341 if (create_callback) {
342 platform_sp = create_callback(false, &arch);
343 if (platform_sp &&
344 platform_sp->IsCompatibleArchitecture(arch, false,
345 platform_arch_ptr)) {
346 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
347 GetPlatformList().push_back(platform_sp);
348 return platform_sp;
349 }
350 }
351 }
352 } else
353 error.SetErrorString("invalid platform name");
354 if (platform_arch_ptr)
355 platform_arch_ptr->Clear();
356 platform_sp.reset();
357 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000358}
359
Pavel Labath7263f1b2017-10-31 10:56:03 +0000360ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
361 if (platform)
362 return platform->GetAugmentedArchSpec(triple);
363 return HostInfo::GetAugmentedArchSpec(triple);
364}
365
Greg Claytone996fd32011-03-08 22:40:15 +0000366//------------------------------------------------------------------
367/// Default Constructor
368//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000369Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 : m_is_host(is_host), m_os_version_set_while_connected(false),
371 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
372 m_working_dir(), m_remote_url(), m_name(), m_major_os_version(UINT32_MAX),
373 m_minor_os_version(UINT32_MAX), m_update_os_version(UINT32_MAX),
374 m_system_arch(), m_mutex(), m_uid_map(), m_gid_map(),
375 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
376 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
377 m_ignores_remote_hostname(false), m_trap_handlers(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000378 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 m_module_cache(llvm::make_unique<ModuleCache>()) {
380 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
381 if (log)
382 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000383}
384
385//------------------------------------------------------------------
386/// Destructor.
387///
388/// The destructor is virtual since this class is designed to be
389/// inherited from by the plug-in instance.
390//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391Platform::~Platform() {
392 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
393 if (log)
394 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000395}
396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397void Platform::GetStatus(Stream &strm) {
398 uint32_t major = UINT32_MAX;
399 uint32_t minor = UINT32_MAX;
400 uint32_t update = UINT32_MAX;
401 std::string s;
402 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 ArchSpec arch(GetSystemArchitecture());
405 if (arch.IsValid()) {
406 if (!arch.GetTriple().str().empty()) {
407 strm.Printf(" Triple: ");
408 arch.DumpTriple(strm);
409 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000410 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 if (GetOSVersion(major, minor, update)) {
414 strm.Printf("OS Version: %u", major);
415 if (minor != UINT32_MAX)
416 strm.Printf(".%u", minor);
417 if (update != UINT32_MAX)
418 strm.Printf(".%u", update);
Greg Clayton1cb64962011-03-24 04:28:38 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 if (GetOSBuildString(s))
421 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 strm.EOL();
424 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (GetOSKernelDescription(s))
427 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 if (IsHost()) {
430 strm.Printf(" Hostname: %s\n", GetHostname());
431 } else {
432 const bool is_connected = IsConnected();
433 if (is_connected)
434 strm.Printf(" Hostname: %s\n", GetHostname());
435 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
436 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 if (GetWorkingDirectory()) {
439 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
440 }
441 if (!IsConnected())
442 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 std::string specific_info(GetPlatformSpecificConnectionInformation());
445
446 if (!specific_info.empty())
447 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000448}
449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450bool Platform::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update,
451 Process *process) {
452 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 bool success = m_major_os_version != UINT32_MAX;
455 if (IsHost()) {
456 if (!success) {
457 // We have a local host platform
458 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version,
459 m_update_os_version);
460 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 } else {
463 // We have a remote platform. We can only fetch the remote
464 // OS version if we are connected, and we don't want to do it
465 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 bool fetch = false;
470 if (success) {
Adrian Prantl05097242018-04-30 16:49:04 +0000471 // We have valid OS version info, check to make sure it wasn't manually
472 // set prior to connecting. If it was manually set prior to connecting,
473 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 if (is_connected && !m_os_version_set_while_connected)
475 fetch = true;
476 } else {
477 // We don't have valid OS version info, fetch it if we are connected
478 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000479 }
480
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481 if (fetch) {
482 success = GetRemoteOSVersion();
483 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000484 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 }
486
487 if (success) {
488 major = m_major_os_version;
489 minor = m_minor_os_version;
490 update = m_update_os_version;
491 } else if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000492 // Check with the process in case it can answer the question if a process
493 // was provided
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 return process->GetHostOSVersion(major, minor, update);
495 }
496 return success;
Greg Claytonded470d2011-03-19 01:12:21 +0000497}
Greg Clayton1cb64962011-03-24 04:28:38 +0000498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499bool Platform::GetOSBuildString(std::string &s) {
500 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000501
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000503#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000505#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000507#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 else
509 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000510}
511
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512bool Platform::GetOSKernelDescription(std::string &s) {
513 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000514#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000516#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000518#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 else
520 return GetRemoteOSKernelDescription(s);
521}
522
523void Platform::AddClangModuleCompilationOptions(
524 Target *target, std::vector<std::string> &options) {
525 std::vector<std::string> default_compilation_options = {
526 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
527
528 options.insert(options.end(), default_compilation_options.begin(),
529 default_compilation_options.end());
530}
531
532FileSpec Platform::GetWorkingDirectory() {
533 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000534 llvm::SmallString<64> cwd;
535 if (llvm::sys::fs::current_path(cwd))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536 return FileSpec{};
Pavel Labath1d5855b2017-01-23 15:56:45 +0000537 else
538 return FileSpec(cwd, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 } else {
540 if (!m_working_dir)
541 m_working_dir = GetRemoteWorkingDirectory();
542 return m_working_dir;
543 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000544}
545
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546struct RecurseCopyBaton {
547 const FileSpec &dst;
548 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000549 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000550};
551
Greg Claytonfbb76342013-11-20 21:07:01 +0000552static FileSpec::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000553RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 const FileSpec &src) {
555 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000556 namespace fs = llvm::sys::fs;
557 switch (ft) {
558 case fs::file_type::fifo_file:
559 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 // we have no way to copy pipes and sockets - ignore them and continue
561 return FileSpec::eEnumerateDirectoryResultNext;
562 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000563
Zachary Turner7d86ee52017-03-08 17:56:08 +0000564 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 // make the new directory and get in there
566 FileSpec dst_dir = rc_baton->dst;
567 if (!dst_dir.GetFilename())
568 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000569 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 dst_dir, lldb::eFilePermissionsDirectoryDefault);
571 if (error.Fail()) {
572 rc_baton->error.SetErrorStringWithFormat(
573 "unable to setup directory %s on remote end", dst_dir.GetCString());
574 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000575 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576
577 // now recurse
578 std::string src_dir_path(src.GetPath());
579
Adrian Prantl05097242018-04-30 16:49:04 +0000580 // Make a filespec that only fills in the directory of a FileSpec so when
581 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 FileSpec recurse_dst;
583 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000584 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
585 Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000586 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 RecurseCopy_Callback, &rc_baton2);
588 if (rc_baton2.error.Fail()) {
589 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
590 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
591 }
592 return FileSpec::eEnumerateDirectoryResultNext;
593 } break;
594
Zachary Turner7d86ee52017-03-08 17:56:08 +0000595 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 // copy the file and keep going
597 FileSpec dst_file = rc_baton->dst;
598 if (!dst_file.GetFilename())
599 dst_file.GetFilename() = src.GetFilename();
600
601 FileSpec src_resolved;
602
603 rc_baton->error = FileSystem::Readlink(src, src_resolved);
604
605 if (rc_baton->error.Fail())
606 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
607
608 rc_baton->error =
609 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
610
611 if (rc_baton->error.Fail())
612 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
613
614 return FileSpec::eEnumerateDirectoryResultNext;
615 } break;
616
Zachary Turner7d86ee52017-03-08 17:56:08 +0000617 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 // copy the file and keep going
619 FileSpec dst_file = rc_baton->dst;
620 if (!dst_file.GetFilename())
621 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000622 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 if (err.Fail()) {
624 rc_baton->error.SetErrorString(err.AsCString());
625 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
626 }
627 return FileSpec::eEnumerateDirectoryResultNext;
628 } break;
629
Zachary Turner7d86ee52017-03-08 17:56:08 +0000630 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 rc_baton->error.SetErrorStringWithFormat(
632 "invalid file detected during copy: %s", src.GetPath().c_str());
633 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
634 break;
635 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000636 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000637}
638
Zachary Turner97206d52017-05-12 04:51:55 +0000639Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
640 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641
642 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
643 if (log)
644 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
645 dst.GetPath().c_str());
646 FileSpec fixed_dst(dst);
647
648 if (!fixed_dst.GetFilename())
649 fixed_dst.GetFilename() = src.GetFilename();
650
651 FileSpec working_dir = GetWorkingDirectory();
652
653 if (dst) {
654 if (dst.GetDirectory()) {
655 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
656 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
657 fixed_dst.GetDirectory() = dst.GetDirectory();
658 }
Adrian Prantl05097242018-04-30 16:49:04 +0000659 // If the fixed destination file doesn't have a directory yet, then we
660 // must have a relative path. We will resolve this relative path against
661 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 if (!fixed_dst.GetDirectory()) {
663 FileSpec relative_spec;
664 std::string path;
665 if (working_dir) {
666 relative_spec = working_dir;
667 relative_spec.AppendPathComponent(dst.GetPath());
668 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
669 } else {
670 error.SetErrorStringWithFormat(
671 "platform working directory must be valid for relative path '%s'",
672 dst.GetPath().c_str());
673 return error;
674 }
675 }
676 } else {
677 if (working_dir) {
678 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
679 } else {
680 error.SetErrorStringWithFormat(
681 "platform working directory must be valid for relative path '%s'",
682 dst.GetPath().c_str());
683 return error;
684 }
685 }
686 } else {
687 if (working_dir) {
688 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
689 } else {
690 error.SetErrorStringWithFormat("platform working directory must be valid "
691 "when destination directory is empty");
692 return error;
693 }
694 }
695
696 if (log)
697 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
698 src.GetPath().c_str(), dst.GetPath().c_str(),
699 fixed_dst.GetPath().c_str());
700
701 if (GetSupportsRSync()) {
702 error = PutFile(src, dst);
703 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000704 namespace fs = llvm::sys::fs;
705 switch (fs::get_file_type(src.GetPath(), false)) {
706 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000707 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 uint32_t permissions = src.GetPermissions();
709 if (permissions == 0)
710 permissions = eFilePermissionsDirectoryDefault;
711 error = MakeDirectory(fixed_dst, permissions);
712 if (error.Success()) {
713 // Make a filespec that only fills in the directory of a FileSpec so
714 // when we enumerate we can quickly fill in the filename for dst copies
715 FileSpec recurse_dst;
716 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
717 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000718 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000719 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 RecurseCopy_Callback, &baton);
721 return baton.error;
722 }
723 } break;
724
Zachary Turner7d86ee52017-03-08 17:56:08 +0000725 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000726 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 error = PutFile(src, fixed_dst);
728 break;
729
Zachary Turner7d86ee52017-03-08 17:56:08 +0000730 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000731 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 FileSpec src_resolved;
733 error = FileSystem::Readlink(src, src_resolved);
734 if (error.Success())
735 error = CreateSymlink(dst, src_resolved);
736 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000737 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 error.SetErrorString("platform install doesn't handle pipes");
739 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000740 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 error.SetErrorString("platform install doesn't handle sockets");
742 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000743 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 error.SetErrorString(
745 "platform install doesn't handle non file or directory items");
746 break;
747 }
748 }
749 return error;
750}
751
752bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
753 if (IsHost()) {
754 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000755 LLDB_LOG(log, "{0}", file_spec);
756 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
757 LLDB_LOG(log, "error: {0}", ec.message());
758 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000760 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 } else {
762 m_working_dir.Clear();
763 return SetRemoteWorkingDirectory(file_spec);
764 }
765}
766
Zachary Turner97206d52017-05-12 04:51:55 +0000767Status Platform::MakeDirectory(const FileSpec &file_spec,
768 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000770 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000772 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
774 GetPluginName().GetCString(),
775 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000776 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000778}
779
Zachary Turner97206d52017-05-12 04:51:55 +0000780Status Platform::GetFilePermissions(const FileSpec &file_spec,
781 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000782 if (IsHost()) {
783 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
784 if (Value)
785 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000786 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000787 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000788 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
790 GetPluginName().GetCString(),
791 LLVM_PRETTY_FUNCTION);
792 return error;
793 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000794}
795
Zachary Turner97206d52017-05-12 04:51:55 +0000796Status Platform::SetFilePermissions(const FileSpec &file_spec,
797 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000798 if (IsHost()) {
799 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
800 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
801 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000802 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
804 GetPluginName().GetCString(),
805 LLVM_PRETTY_FUNCTION);
806 return error;
807 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000808}
809
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810ConstString Platform::GetName() { return GetPluginName(); }
811
812const char *Platform::GetHostname() {
813 if (IsHost())
814 return "127.0.0.1";
815
816 if (m_name.empty())
817 return nullptr;
818 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000819}
820
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821ConstString Platform::GetFullNameForDylib(ConstString basename) {
822 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000823}
824
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
826 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
827 if (log)
828 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000829 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 m_working_dir = working_dir;
831 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000832}
833
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834const char *Platform::GetUserName(uint32_t uid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000835#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 const char *user_name = GetCachedUserName(uid);
837 if (user_name)
838 return user_name;
839 if (IsHost()) {
840 std::string name;
841 if (HostInfo::LookupUserName(uid, name))
842 return SetCachedUserName(uid, name.c_str(), name.size());
843 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000844#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 return nullptr;
Greg Clayton1cb64962011-03-24 04:28:38 +0000846}
847
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848const char *Platform::GetGroupName(uint32_t gid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000849#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 const char *group_name = GetCachedGroupName(gid);
851 if (group_name)
852 return group_name;
853 if (IsHost()) {
854 std::string name;
855 if (HostInfo::LookupGroupName(gid, name))
856 return SetCachedGroupName(gid, name.c_str(), name.size());
857 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000858#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 return nullptr;
Greg Clayton32e0a752011-03-30 18:16:51 +0000860}
Greg Clayton1cb64962011-03-24 04:28:38 +0000861
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862bool Platform::SetOSVersion(uint32_t major, uint32_t minor, uint32_t update) {
863 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000864 // We don't need anyone setting the OS version for the host platform, we
865 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000866 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000868 // We have a remote platform, allow setting the target OS version if we
869 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 // request the remote OS version from the connected platform.
871 if (IsConnected())
872 return false;
873 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000874 // We aren't connected and we might want to set the OS version ahead of
875 // time before we connect so we can peruse files and use a local SDK or
876 // PDK cache of support files to disassemble or do other things.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000877 m_major_os_version = major;
878 m_minor_os_version = minor;
879 m_update_os_version = update;
880 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 }
883 return false;
884}
885
Zachary Turner97206d52017-05-12 04:51:55 +0000886Status
887Platform::ResolveExecutable(const ModuleSpec &module_spec,
888 lldb::ModuleSP &exe_module_sp,
889 const FileSpecList *module_search_paths_ptr) {
890 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 if (module_spec.GetFileSpec().Exists()) {
892 if (module_spec.GetArchitecture().IsValid()) {
893 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
894 module_search_paths_ptr, nullptr,
895 nullptr);
896 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000897 // No valid architecture was specified, ask the platform for the
898 // architectures that we should be using (in the correct order) and see
899 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 ModuleSpec arch_module_spec(module_spec);
901 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
902 idx, arch_module_spec.GetArchitecture());
903 ++idx) {
904 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
905 module_search_paths_ptr, nullptr,
906 nullptr);
907 // Did we find an executable using one of the
908 if (error.Success() && exe_module_sp)
909 break;
910 }
Greg Claytone996fd32011-03-08 22:40:15 +0000911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 } else {
913 error.SetErrorStringWithFormat("'%s' does not exist",
914 module_spec.GetFileSpec().GetPath().c_str());
915 }
916 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000917}
918
Zachary Turner97206d52017-05-12 04:51:55 +0000919Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
920 FileSpec &sym_file) {
921 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 if (sym_spec.GetSymbolFileSpec().Exists())
923 sym_file = sym_spec.GetSymbolFileSpec();
924 else
925 error.SetErrorString("unable to resolve symbol file");
926 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000927}
928
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929bool Platform::ResolveRemotePath(const FileSpec &platform_path,
930 FileSpec &resolved_platform_path) {
931 resolved_platform_path = platform_path;
932 return resolved_platform_path.ResolvePath();
933}
934
935const ArchSpec &Platform::GetSystemArchitecture() {
936 if (IsHost()) {
937 if (!m_system_arch.IsValid()) {
938 // We have a local host platform
939 m_system_arch = HostInfo::GetArchitecture();
940 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000941 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000942 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000943 // We have a remote platform. We can only fetch the remote system
944 // architecture if we are connected, and we don't want to do it more than
945 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000946
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 bool fetch = false;
950 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000951 // We have valid OS version info, check to make sure it wasn't manually
952 // set prior to connecting. If it was manually set prior to connecting,
953 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954 if (is_connected && !m_system_arch_set_while_connected)
955 fetch = true;
956 } else {
957 // We don't have valid OS version info, fetch it if we are connected
958 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000959 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960
961 if (fetch) {
962 m_system_arch = GetRemoteSystemArchitecture();
963 m_system_arch_set_while_connected = m_system_arch.IsValid();
964 }
965 }
966 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000967}
968
Pavel Labath7263f1b2017-10-31 10:56:03 +0000969ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
970 if (triple.empty())
971 return ArchSpec();
972 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
973 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
974 return ArchSpec(triple);
975
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000976 if (auto kind = HostInfo::ParseArchitectureKind(triple))
977 return HostInfo::GetArchitecture(*kind);
978
Pavel Labath7263f1b2017-10-31 10:56:03 +0000979 ArchSpec compatible_arch;
980 ArchSpec raw_arch(triple);
981 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
982 return raw_arch;
983
984 if (!compatible_arch.IsValid())
985 return ArchSpec(normalized_triple);
986
987 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
988 if (normalized_triple.getVendorName().empty())
989 normalized_triple.setVendor(compatible_triple.getVendor());
990 if (normalized_triple.getOSName().empty())
991 normalized_triple.setOS(compatible_triple.getOS());
992 if (normalized_triple.getEnvironmentName().empty())
993 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
994 return ArchSpec(normalized_triple);
995}
996
Zachary Turner97206d52017-05-12 04:51:55 +0000997Status Platform::ConnectRemote(Args &args) {
998 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 if (IsHost())
1000 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1001 "the host platform and is always connected.",
1002 GetPluginName().GetCString());
1003 else
1004 error.SetErrorStringWithFormat(
1005 "Platform::ConnectRemote() is not supported by %s",
1006 GetPluginName().GetCString());
1007 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001008}
1009
Zachary Turner97206d52017-05-12 04:51:55 +00001010Status Platform::DisconnectRemote() {
1011 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 if (IsHost())
1013 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1014 "the host platform and is always connected.",
1015 GetPluginName().GetCString());
1016 else
1017 error.SetErrorStringWithFormat(
1018 "Platform::DisconnectRemote() is not supported by %s",
1019 GetPluginName().GetCString());
1020 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001021}
Greg Clayton32e0a752011-03-30 18:16:51 +00001022
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023bool Platform::GetProcessInfo(lldb::pid_t pid,
1024 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +00001025 // Take care of the host case so that each subclass can just call this
1026 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027 if (IsHost())
1028 return Host::GetProcessInfo(pid, process_info);
1029 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00001030}
1031
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1033 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +00001034 // Take care of the host case so that each subclass can just call this
1035 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 uint32_t match_count = 0;
1037 if (IsHost())
1038 match_count = Host::FindProcesses(match_info, process_infos);
1039 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001040}
Greg Clayton8b82f082011-04-12 05:54:46 +00001041
Zachary Turner97206d52017-05-12 04:51:55 +00001042Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1043 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1045 if (log)
1046 log->Printf("Platform::%s()", __FUNCTION__);
1047
Adrian Prantl05097242018-04-30 16:49:04 +00001048 // Take care of the host case so that each subclass can just call this
1049 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 if (IsHost()) {
1051 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1052 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1053
1054 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1055 const bool is_localhost = true;
1056 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1057 const bool first_arg_is_full_shell_command = false;
1058 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1059 if (log) {
1060 const FileSpec &shell = launch_info.GetShell();
1061 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
1062 log->Printf(
1063 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1064 ", shell is '%s'",
1065 __FUNCTION__, num_resumes, shell_str);
1066 }
1067
1068 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1069 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1070 num_resumes))
1071 return error;
1072 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1073 error = ShellExpandArguments(launch_info);
1074 if (error.Fail()) {
1075 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1076 "consider launching with 'process "
1077 "launch'.",
1078 error.AsCString("unknown"));
1079 return error;
1080 }
1081 }
1082
Todd Fialaac33cc92014-10-09 01:02:08 +00001083 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1085 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001086
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 error = Host::LaunchProcess(launch_info);
1088 } else
1089 error.SetErrorString(
1090 "base lldb_private::Platform class can't launch remote processes");
1091 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001092}
1093
Zachary Turner97206d52017-05-12 04:51:55 +00001094Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 if (IsHost())
1096 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001097 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001098}
1099
Zachary Turner97206d52017-05-12 04:51:55 +00001100Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1102 if (log)
1103 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001104
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001106 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 size_t num_debuggers = Debugger::GetNumDebuggers();
1108 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1109 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1110 lldb_private::TargetList &targets = debugger->GetTargetList();
1111 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1112 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1113 if (process->GetID() == pid)
1114 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001115 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001116 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001117
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001119 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 "base lldb_private::Platform class can't kill remote processes unless "
1121 "they are controlled by a process plugin");
1122 }
1123 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001124 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001125}
1126
Greg Clayton8b82f082011-04-12 05:54:46 +00001127lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001128Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1129 Target *target, // Can be nullptr, if nullptr create a
1130 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001131 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1133 if (log)
1134 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1135 static_cast<void *>(target));
1136
1137 ProcessSP process_sp;
1138 // Make sure we stop at the entry point
1139 launch_info.GetFlags().Set(eLaunchFlagDebug);
1140 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001141 // group, since then we can handle ^C interrupts ourselves w/o having to
1142 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143 launch_info.SetLaunchInSeparateProcessGroup(true);
1144
1145 // Allow any StructuredData process-bound plugins to adjust the launch info
1146 // if needed
1147 size_t i = 0;
1148 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001149 // Note iteration can't simply go until a nullptr callback is returned, as it
1150 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1152 for (auto filter_callback = get_filter_func(i, iteration_complete);
1153 !iteration_complete;
1154 filter_callback = get_filter_func(++i, iteration_complete)) {
1155 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001156 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 error = (*filter_callback)(launch_info, target);
1158 if (!error.Success()) {
1159 if (log)
1160 log->Printf("Platform::%s() StructuredDataPlugin launch "
1161 "filter failed.",
1162 __FUNCTION__);
1163 return process_sp;
1164 }
1165 }
1166 }
1167
1168 error = LaunchProcess(launch_info);
1169 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001170 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1172 ")",
1173 __FUNCTION__, launch_info.GetProcessID());
1174 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1175 ProcessAttachInfo attach_info(launch_info);
1176 process_sp = Attach(attach_info, debugger, target, error);
1177 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001178 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1180 __FUNCTION__, process_sp->GetPluginName().AsCString());
1181 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001182
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 // Since we attached to the process, it will think it needs to detach
1184 // if the process object just goes away without an explicit call to
1185 // Process::Kill() or Process::Detach(), so let it know to kill the
1186 // process if this happens.
1187 process_sp->SetShouldDetach(false);
1188
Adrian Prantl05097242018-04-30 16:49:04 +00001189 // If we didn't have any file actions, the pseudo terminal might have
1190 // been used where the slave side was given as the file to open for
1191 // stdin/out/err after we have already opened the master so we can
1192 // read/write stdin/out/err.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001194 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 process_sp->SetSTDIOFileDescriptor(pty_fd);
1196 }
1197 } else {
1198 if (log)
1199 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1200 error.AsCString());
1201 }
1202 } else {
1203 if (log)
1204 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1205 "invalid process id",
1206 __FUNCTION__);
1207 }
1208 } else {
1209 if (log)
1210 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1211 error.AsCString());
1212 }
1213
1214 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001215}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001216
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001217lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1219 ArchSpec *platform_arch_ptr) {
1220 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001221 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 if (arch.IsValid())
1223 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1224 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001225}
1226
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001227//------------------------------------------------------------------
1228/// Lets a platform answer if it is compatible with a given
1229/// architecture and the target triple contained within.
1230//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1232 bool exact_arch_match,
1233 ArchSpec *compatible_arch_ptr) {
1234 // If the architecture is invalid, we must answer true...
1235 if (arch.IsValid()) {
1236 ArchSpec platform_arch;
1237 // Try for an exact architecture match first.
1238 if (exact_arch_match) {
1239 for (uint32_t arch_idx = 0;
1240 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1241 ++arch_idx) {
1242 if (arch.IsExactMatch(platform_arch)) {
1243 if (compatible_arch_ptr)
1244 *compatible_arch_ptr = platform_arch;
1245 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001246 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247 }
1248 } else {
1249 for (uint32_t arch_idx = 0;
1250 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1251 ++arch_idx) {
1252 if (arch.IsCompatibleMatch(platform_arch)) {
1253 if (compatible_arch_ptr)
1254 *compatible_arch_ptr = platform_arch;
1255 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001256 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001258 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 }
1260 if (compatible_arch_ptr)
1261 compatible_arch_ptr->Clear();
1262 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001263}
1264
Zachary Turner97206d52017-05-12 04:51:55 +00001265Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1266 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1268 if (log)
1269 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001270
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 uint32_t source_open_options =
1272 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001273 namespace fs = llvm::sys::fs;
1274 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
Zachary Turner97206d52017-05-12 04:51:55 +00001278 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279 uint32_t permissions = source_file.GetPermissions(error);
1280 if (permissions == 0)
1281 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001282
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001284 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 lldb::user_id_t dest_file = OpenFile(
1286 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1287 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1288 permissions, error);
1289 if (log)
1290 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001291
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 if (error.Fail())
1293 return error;
1294 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001295 return Status("unable to open target file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1297 uint64_t offset = 0;
1298 for (;;) {
1299 size_t bytes_read = buffer_sp->GetByteSize();
1300 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1301 if (error.Fail() || bytes_read == 0)
1302 break;
1303
1304 const uint64_t bytes_written =
1305 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001306 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 offset += bytes_written;
1310 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001311 // We didn't write the correct number of bytes, so adjust the file
1312 // position in the source file we are reading from...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 }
1316 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001317
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001319 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320
1321 // TODO: ChownFile?
1322
1323 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001324}
1325
Zachary Turner97206d52017-05-12 04:51:55 +00001326Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1327 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001329}
1330
Zachary Turner97206d52017-05-12 04:51:55 +00001331Status
1332Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1333 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001334{
Zachary Turner97206d52017-05-12 04:51:55 +00001335 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001337}
1338
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1340 return false;
1341}
1342
Zachary Turner97206d52017-05-12 04:51:55 +00001343Status Platform::Unlink(const FileSpec &path) {
1344 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 return error;
1346}
1347
Ed Maste37c40af2017-08-16 12:55:02 +00001348MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1349 addr_t length, unsigned prot,
1350 unsigned flags, addr_t fd,
1351 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352 uint64_t flags_platform = 0;
1353 if (flags & eMmapFlagsPrivate)
1354 flags_platform |= MAP_PRIVATE;
1355 if (flags & eMmapFlagsAnon)
1356 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001357
1358 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1359 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360}
1361
Zachary Turner97206d52017-05-12 04:51:55 +00001362lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363 const char *command, // Shouldn't be nullptr
1364 const FileSpec &
1365 working_dir, // Pass empty FileSpec to use the current working directory
1366 int *status_ptr, // Pass nullptr if you don't want the process exit status
1367 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1368 // process to exit
1369 std::string
1370 *command_output, // Pass nullptr if you don't want the command output
1371 uint32_t
1372 timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001373{
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 if (IsHost())
1375 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
1376 command_output, timeout_sec);
1377 else
Zachary Turner97206d52017-05-12 04:51:55 +00001378 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379}
1380
1381bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1382 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001383 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001384 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001385 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1386 if (!Result)
1387 return false;
1388 std::tie(high, low) = Result->words();
1389 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001390}
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392void Platform::SetLocalCacheDirectory(const char *local) {
1393 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001394}
1395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396const char *Platform::GetLocalCacheDirectory() {
1397 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001398}
Greg Claytonfbb76342013-11-20 21:07:01 +00001399
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400static OptionDefinition g_rsync_option_table[] = {
1401 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
1402 nullptr, 0, eArgTypeNone, "Enable rsync."},
1403 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
1404 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1405 "Platform-specific options required for rsync to work."},
1406 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
1407 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1408 "Platform-specific rsync prefix put before the remote path."},
1409 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
1410 OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,
1411 "Do not automatically fill in the remote hostname when composing the "
1412 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001413};
1414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415static OptionDefinition g_ssh_option_table[] = {
1416 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
1417 nullptr, 0, eArgTypeNone, "Enable SSH."},
1418 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
1419 nullptr, nullptr, 0, eArgTypeCommandName,
1420 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001421};
1422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423static OptionDefinition g_caching_option_table[] = {
1424 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
1425 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePath,
1426 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001427};
1428
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001429llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001430 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001431}
1432
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433void OptionGroupPlatformRSync::OptionParsingStarting(
1434 ExecutionContext *execution_context) {
1435 m_rsync = false;
1436 m_rsync_opts.clear();
1437 m_rsync_prefix.clear();
1438 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001439}
1440
Zachary Turner97206d52017-05-12 04:51:55 +00001441lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001442OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001443 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001445 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446 char short_option = (char)GetDefinitions()[option_idx].short_option;
1447 switch (short_option) {
1448 case 'r':
1449 m_rsync = true;
1450 break;
1451
1452 case 'R':
1453 m_rsync_opts.assign(option_arg);
1454 break;
1455
1456 case 'P':
1457 m_rsync_prefix.assign(option_arg);
1458 break;
1459
1460 case 'i':
1461 m_ignores_remote_hostname = true;
1462 break;
1463
1464 default:
1465 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1466 break;
1467 }
1468
1469 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001470}
1471
Greg Clayton4116e932012-05-15 02:33:01 +00001472lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1474 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001475}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001476
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001477llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001478 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001479}
1480
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481void OptionGroupPlatformSSH::OptionParsingStarting(
1482 ExecutionContext *execution_context) {
1483 m_ssh = false;
1484 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001485}
1486
Zachary Turner97206d52017-05-12 04:51:55 +00001487lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001488OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001489 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001491 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 char short_option = (char)GetDefinitions()[option_idx].short_option;
1493 switch (short_option) {
1494 case 's':
1495 m_ssh = true;
1496 break;
1497
1498 case 'S':
1499 m_ssh_opts.assign(option_arg);
1500 break;
1501
1502 default:
1503 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1504 break;
1505 }
1506
1507 return error;
1508}
1509
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001510llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001511 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512}
1513
1514void OptionGroupPlatformCaching::OptionParsingStarting(
1515 ExecutionContext *execution_context) {
1516 m_cache_dir.clear();
1517}
1518
Zachary Turner97206d52017-05-12 04:51:55 +00001519lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001520 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001522 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 char short_option = (char)GetDefinitions()[option_idx].short_option;
1524 switch (short_option) {
1525 case 'c':
1526 m_cache_dir.assign(option_arg);
1527 break;
1528
1529 default:
1530 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1531 break;
1532 }
1533
1534 return error;
1535}
1536
Pavel Labath62930e52018-01-10 11:57:31 +00001537Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538
1539const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1540 if (!m_calculated_trap_handlers) {
1541 std::lock_guard<std::mutex> guard(m_mutex);
1542 if (!m_calculated_trap_handlers) {
1543 CalculateTrapHandlerSymbolNames();
1544 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001545 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001546 }
1547 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001548}
1549
Zachary Turner97206d52017-05-12 04:51:55 +00001550Status Platform::GetCachedExecutable(
1551 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1552 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 const auto platform_spec = module_spec.GetFileSpec();
1554 const auto error = LoadCachedExecutable(
1555 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1556 if (error.Success()) {
1557 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1558 module_spec.GetPlatformFileSpec() = platform_spec;
1559 }
1560
1561 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001562}
1563
Zachary Turner97206d52017-05-12 04:51:55 +00001564Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1566 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1567 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1568 [&](const ModuleSpec &spec) {
1569 return remote_platform.ResolveExecutable(
1570 spec, module_sp, module_search_paths_ptr);
1571 },
1572 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001573}
1574
Zachary Turner97206d52017-05-12 04:51:55 +00001575Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1576 Process *process,
1577 lldb::ModuleSP &module_sp,
1578 const ModuleResolver &module_resolver,
1579 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580 // Get module information from a target.
1581 ModuleSpec resolved_module_spec;
1582 bool got_module_spec = false;
1583 if (process) {
1584 // Try to get module information from the process
1585 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1586 module_spec.GetArchitecture(),
1587 resolved_module_spec)) {
1588 if (module_spec.GetUUID().IsValid() == false ||
1589 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1590 got_module_spec = true;
1591 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001592 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001594
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001595 if (module_spec.GetArchitecture().IsValid() == false) {
Zachary Turner97206d52017-05-12 04:51:55 +00001596 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001597 // No valid architecture was specified, ask the platform for the
1598 // architectures that we should be using (in the correct order) and see if
1599 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001600 ModuleSpec arch_module_spec(module_spec);
1601 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1602 idx, arch_module_spec.GetArchitecture());
1603 ++idx) {
1604 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1605 nullptr, nullptr);
1606 // Did we find an executable using one of the
1607 if (error.Success() && module_sp)
1608 break;
1609 }
1610 if (module_sp)
1611 got_module_spec = true;
1612 }
1613
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001615 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1617 resolved_module_spec)) {
1618 if (module_spec.GetUUID().IsValid() == false ||
1619 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1620 return module_resolver(module_spec);
1621 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001622 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 // If we are looking for a specific UUID, make sure resolved_module_spec has
1626 // the same one before we search.
1627 if (module_spec.GetUUID().IsValid()) {
1628 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1629 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001630
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 // Trying to find a module by UUID on local file system.
1632 const auto error = module_resolver(resolved_module_spec);
1633 if (error.Fail()) {
1634 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001635 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001637
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001639}
1640
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1642 lldb::ModuleSP &module_sp,
1643 bool *did_create_ptr) {
1644 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1645 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001646 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647
1648 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1649
1650 // Check local cache for a module.
1651 auto error = m_module_cache->GetAndPut(
1652 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1653 [this](const ModuleSpec &module_spec,
1654 const FileSpec &tmp_download_file_spec) {
1655 return DownloadModuleSlice(
1656 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1657 module_spec.GetObjectSize(), tmp_download_file_spec);
1658
1659 },
1660 [this](const ModuleSP &module_sp,
1661 const FileSpec &tmp_download_file_spec) {
1662 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1663 },
1664 module_sp, did_create_ptr);
1665 if (error.Success())
1666 return true;
1667
1668 if (log)
1669 log->Printf("Platform::%s - module %s not found in local cache: %s",
1670 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1671 error.AsCString());
1672 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001673}
1674
Zachary Turner97206d52017-05-12 04:51:55 +00001675Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1676 const uint64_t src_offset,
1677 const uint64_t src_size,
1678 const FileSpec &dst_file_spec) {
1679 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001680
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001681 std::error_code EC;
1682 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1683 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 error.SetErrorStringWithFormat("unable to open destination file: %s",
1685 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001686 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001688
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1690 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001691
Kate Stoneb9c1b512016-09-06 20:57:50 +00001692 if (error.Fail()) {
1693 error.SetErrorStringWithFormat("unable to open source file: %s",
1694 error.AsCString());
1695 return error;
1696 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001697
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 std::vector<char> buffer(1024);
1699 auto offset = src_offset;
1700 uint64_t total_bytes_read = 0;
1701 while (total_bytes_read < src_size) {
1702 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1703 src_size - total_bytes_read);
1704 const uint64_t n_read =
1705 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001706 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 break;
1708 if (n_read == 0) {
1709 error.SetErrorString("read 0 bytes");
1710 break;
1711 }
1712 offset += n_read;
1713 total_bytes_read += n_read;
1714 dst.write(&buffer[0], n_read);
1715 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001716
Zachary Turner97206d52017-05-12 04:51:55 +00001717 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718 CloseFile(src_fd, close_error); // Ignoring close error.
1719
1720 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001721}
1722
Zachary Turner97206d52017-05-12 04:51:55 +00001723Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1724 const FileSpec &dst_file_spec) {
1725 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001727}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001728
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729FileSpec Platform::GetModuleCacheRoot() {
1730 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1731 dir_spec.AppendPathComponent(GetName().AsCString());
1732 return dir_spec;
1733}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001734
Kate Stoneb9c1b512016-09-06 20:57:50 +00001735const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001736
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1738 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1739 return s_default_unix_signals_sp;
1740}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001741
Kate Stoneb9c1b512016-09-06 20:57:50 +00001742const UnixSignalsSP &Platform::GetUnixSignals() {
1743 if (IsHost())
1744 return Host::GetUnixSignals();
1745 return GetRemoteUnixSignals();
1746}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001747
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748uint32_t Platform::LoadImage(lldb_private::Process *process,
1749 const lldb_private::FileSpec &local_file,
1750 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001751 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 if (local_file && remote_file) {
1753 // Both local and remote file was specified. Install the local file to the
1754 // given location.
1755 if (IsRemote() || local_file != remote_file) {
1756 error = Install(local_file, remote_file);
1757 if (error.Fail())
1758 return LLDB_INVALID_IMAGE_TOKEN;
1759 }
1760 return DoLoadImage(process, remote_file, error);
1761 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001762
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 if (local_file) {
1764 // Only local file was specified. Install it to the current working
1765 // directory.
1766 FileSpec target_file = GetWorkingDirectory();
1767 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1768 if (IsRemote() || local_file != target_file) {
1769 error = Install(local_file, target_file);
1770 if (error.Fail())
1771 return LLDB_INVALID_IMAGE_TOKEN;
1772 }
1773 return DoLoadImage(process, target_file, error);
1774 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001775
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776 if (remote_file) {
1777 // Only remote file was specified so we don't have to do any copying
1778 return DoLoadImage(process, remote_file, error);
1779 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001780
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 error.SetErrorString("Neither local nor remote file was specified");
1782 return LLDB_INVALID_IMAGE_TOKEN;
1783}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001784
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1786 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001787 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 error.SetErrorString("LoadImage is not supported on the current platform");
1789 return LLDB_INVALID_IMAGE_TOKEN;
1790}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001791
Zachary Turner97206d52017-05-12 04:51:55 +00001792Status Platform::UnloadImage(lldb_private::Process *process,
1793 uint32_t image_token) {
1794 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001796
Zachary Turner31659452016-11-17 21:15:14 +00001797lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1798 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 lldb_private::Debugger &debugger,
1800 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001801 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001803
Kate Stoneb9c1b512016-09-06 20:57:50 +00001804 if (!target) {
1805 TargetSP new_target_sp;
Zachary Turnera47464b2016-11-18 20:44:46 +00001806 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
1807 nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 target = new_target_sp.get();
1809 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001810
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 if (!target || error.Fail())
1812 return nullptr;
1813
1814 debugger.GetTargetList().SetSelectedTarget(target);
1815
1816 lldb::ProcessSP process_sp =
1817 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1818 if (!process_sp)
1819 return nullptr;
1820
1821 error =
1822 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1823 if (error.Fail())
1824 return nullptr;
1825
1826 return process_sp;
1827}
1828
1829size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001830 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001831 error.Clear();
1832 return 0;
1833}
1834
1835size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1836 BreakpointSite *bp_site) {
1837 ArchSpec arch = target.GetArchitecture();
1838 const uint8_t *trap_opcode = nullptr;
1839 size_t trap_opcode_size = 0;
1840
1841 switch (arch.GetMachine()) {
1842 case llvm::Triple::aarch64: {
1843 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1844 trap_opcode = g_aarch64_opcode;
1845 trap_opcode_size = sizeof(g_aarch64_opcode);
1846 } break;
1847
1848 // TODO: support big-endian arm and thumb trap codes.
1849 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001850 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1851 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001852 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1853 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1854
1855 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
1856 AddressClass addr_class = eAddressClassUnknown;
1857
1858 if (bp_loc_sp) {
1859 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
1860 if (addr_class == eAddressClassUnknown &&
1861 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
1862 addr_class = eAddressClassCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001863 }
1864
Kate Stoneb9c1b512016-09-06 20:57:50 +00001865 if (addr_class == eAddressClassCodeAlternateISA) {
1866 trap_opcode = g_thumb_breakpoint_opcode;
1867 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1868 } else {
1869 trap_opcode = g_arm_breakpoint_opcode;
1870 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1871 }
1872 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001873
Kate Stoneb9c1b512016-09-06 20:57:50 +00001874 case llvm::Triple::mips:
1875 case llvm::Triple::mips64: {
1876 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1877 trap_opcode = g_hex_opcode;
1878 trap_opcode_size = sizeof(g_hex_opcode);
1879 } break;
1880
1881 case llvm::Triple::mipsel:
1882 case llvm::Triple::mips64el: {
1883 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1884 trap_opcode = g_hex_opcode;
1885 trap_opcode_size = sizeof(g_hex_opcode);
1886 } break;
1887
1888 case llvm::Triple::systemz: {
1889 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1890 trap_opcode = g_hex_opcode;
1891 trap_opcode_size = sizeof(g_hex_opcode);
1892 } break;
1893
1894 case llvm::Triple::hexagon: {
1895 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1896 trap_opcode = g_hex_opcode;
1897 trap_opcode_size = sizeof(g_hex_opcode);
1898 } break;
1899
1900 case llvm::Triple::ppc:
1901 case llvm::Triple::ppc64: {
1902 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1903 trap_opcode = g_ppc_opcode;
1904 trap_opcode_size = sizeof(g_ppc_opcode);
1905 } break;
1906
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001907 case llvm::Triple::ppc64le: {
1908 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1909 trap_opcode = g_ppc64le_opcode;
1910 trap_opcode_size = sizeof(g_ppc64le_opcode);
1911 } break;
1912
Kate Stoneb9c1b512016-09-06 20:57:50 +00001913 case llvm::Triple::x86:
1914 case llvm::Triple::x86_64: {
1915 static const uint8_t g_i386_opcode[] = {0xCC};
1916 trap_opcode = g_i386_opcode;
1917 trap_opcode_size = sizeof(g_i386_opcode);
1918 } break;
1919
1920 default:
David Blaikiea322f362017-01-06 00:38:06 +00001921 llvm_unreachable(
1922 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001923 }
1924
1925 assert(bp_site);
1926 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1927 return trap_opcode_size;
1928
1929 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001930}