blob: 658607c7fca98e571a0bbd9e065bb0724131031c [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
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000070static constexpr PropertyDefinition g_properties[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000072 {}, "Use module cache."},
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000074 {}, "Root directory for cached modules."},
75 {nullptr, OptionValue::eTypeInvalid, false, 0, 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
Pavel Labath1f8639a2018-08-28 16:32:46 +0000231 // Module resolver lambda.
232 auto resolver = [&](const ModuleSpec &spec) {
233 Status error(eErrorTypeGeneric);
234 ModuleSpec resolved_spec;
235 // Check if we have sysroot set.
236 if (m_sdk_sysroot) {
237 // Prepend sysroot to module spec.
238 resolved_spec = spec;
239 resolved_spec.GetFileSpec().PrependPathComponent(
240 m_sdk_sysroot.GetStringRef());
241 // Try to get shared module with resolved spec.
242 error = ModuleList::GetSharedModule(
243 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
244 did_create_ptr, false);
245 }
246 // If we don't have sysroot or it didn't work then
247 // try original module spec.
248 if (!error.Success()) {
249 resolved_spec = spec;
250 error = ModuleList::GetSharedModule(
251 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
252 did_create_ptr, false);
253 }
254 if (error.Success() && module_sp)
255 module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
256 return error;
257 };
258
259 return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000261}
262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
264 const ArchSpec &arch, ModuleSpec &module_spec) {
265 ModuleSpecList module_specs;
266 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
267 module_specs) == 0)
268 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 ModuleSpec matched_module_spec;
271 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
272 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000273}
274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275PlatformSP Platform::Find(const ConstString &name) {
276 if (name) {
277 static ConstString g_host_platform_name("host");
278 if (name == g_host_platform_name)
279 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
282 for (const auto &platform_sp : GetPlatformList()) {
283 if (platform_sp->GetName() == name)
284 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000285 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 }
287 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000288}
289
Zachary Turner97206d52017-05-12 04:51:55 +0000290PlatformSP Platform::Create(const ConstString &name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 PlatformCreateInstance create_callback = nullptr;
292 lldb::PlatformSP platform_sp;
293 if (name) {
294 static ConstString g_host_platform_name("host");
295 if (name == g_host_platform_name)
296 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000297
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 create_callback =
299 PluginManager::GetPlatformCreateCallbackForPluginName(name);
300 if (create_callback)
301 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000302 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 error.SetErrorStringWithFormat(
304 "unable to find a plug-in for the platform named \"%s\"",
305 name.GetCString());
306 } else
307 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 if (platform_sp) {
310 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
311 GetPlatformList().push_back(platform_sp);
312 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000315}
316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000318 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 lldb::PlatformSP platform_sp;
320 if (arch.IsValid()) {
321 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000322 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 // First try exact arch matches across all platforms already created
324 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
325 for (const auto &platform_sp : GetPlatformList()) {
326 if (platform_sp->IsCompatibleArchitecture(arch, true,
327 platform_arch_ptr))
328 return platform_sp;
329 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 // Next try compatible arch matches across all platforms already created
332 for (const auto &platform_sp : GetPlatformList()) {
333 if (platform_sp->IsCompatibleArchitecture(arch, false,
334 platform_arch_ptr))
335 return platform_sp;
336 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000337 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338
339 PlatformCreateInstance create_callback;
340 // First try exact arch matches across all platform plug-ins
341 uint32_t idx;
342 for (idx = 0; (create_callback =
343 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
344 ++idx) {
345 if (create_callback) {
346 platform_sp = create_callback(false, &arch);
347 if (platform_sp &&
348 platform_sp->IsCompatibleArchitecture(arch, true,
349 platform_arch_ptr)) {
350 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
351 GetPlatformList().push_back(platform_sp);
352 return platform_sp;
353 }
354 }
355 }
356 // Next try compatible arch matches across all platform plug-ins
357 for (idx = 0; (create_callback =
358 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
359 ++idx) {
360 if (create_callback) {
361 platform_sp = create_callback(false, &arch);
362 if (platform_sp &&
363 platform_sp->IsCompatibleArchitecture(arch, false,
364 platform_arch_ptr)) {
365 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
366 GetPlatformList().push_back(platform_sp);
367 return platform_sp;
368 }
369 }
370 }
371 } else
372 error.SetErrorString("invalid platform name");
373 if (platform_arch_ptr)
374 platform_arch_ptr->Clear();
375 platform_sp.reset();
376 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000377}
378
Pavel Labath7263f1b2017-10-31 10:56:03 +0000379ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
380 if (platform)
381 return platform->GetAugmentedArchSpec(triple);
382 return HostInfo::GetAugmentedArchSpec(triple);
383}
384
Greg Claytone996fd32011-03-08 22:40:15 +0000385//------------------------------------------------------------------
386/// Default Constructor
387//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000388Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 : m_is_host(is_host), m_os_version_set_while_connected(false),
390 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000391 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
392 m_uid_map(), m_gid_map(), m_max_uid_name_len(0), m_max_gid_name_len(0),
393 m_supports_rsync(false), m_rsync_opts(), m_rsync_prefix(),
394 m_supports_ssh(false), m_ssh_opts(), m_ignores_remote_hostname(false),
395 m_trap_handlers(), m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 m_module_cache(llvm::make_unique<ModuleCache>()) {
397 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
398 if (log)
399 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000400}
401
402//------------------------------------------------------------------
403/// Destructor.
404///
405/// The destructor is virtual since this class is designed to be
406/// inherited from by the plug-in instance.
407//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408Platform::~Platform() {
409 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
410 if (log)
411 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000412}
413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 std::string s;
416 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 ArchSpec arch(GetSystemArchitecture());
419 if (arch.IsValid()) {
420 if (!arch.GetTriple().str().empty()) {
421 strm.Printf(" Triple: ");
422 arch.DumpTriple(strm);
423 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000424 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000426
Pavel Labath2272c482018-06-18 15:02:23 +0000427 llvm::VersionTuple os_version = GetOSVersion();
428 if (!os_version.empty()) {
429 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 if (GetOSBuildString(s))
432 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 strm.EOL();
435 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000436
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 if (GetOSKernelDescription(s))
438 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 if (IsHost()) {
441 strm.Printf(" Hostname: %s\n", GetHostname());
442 } else {
443 const bool is_connected = IsConnected();
444 if (is_connected)
445 strm.Printf(" Hostname: %s\n", GetHostname());
446 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
447 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 if (GetWorkingDirectory()) {
450 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
451 }
452 if (!IsConnected())
453 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000454
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 std::string specific_info(GetPlatformSpecificConnectionInformation());
456
457 if (!specific_info.empty())
458 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000459}
460
Pavel Labath2272c482018-06-18 15:02:23 +0000461llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000465 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000467 m_os_version = HostInfo::GetOSVersion();
468 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000469 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 } else {
471 // We have a remote platform. We can only fetch the remote
472 // OS version if we are connected, and we don't want to do it
473 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000474
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000476
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000478 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000479 // We have valid OS version info, check to make sure it wasn't manually
480 // set prior to connecting. If it was manually set prior to connecting,
481 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 if (is_connected && !m_os_version_set_while_connected)
483 fetch = true;
484 } else {
485 // We don't have valid OS version info, fetch it if we are connected
486 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000487 }
488
Pavel Labath2272c482018-06-18 15:02:23 +0000489 if (fetch)
490 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 }
492
Pavel Labath2272c482018-06-18 15:02:23 +0000493 if (!m_os_version.empty())
494 return m_os_version;
495 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000496 // Check with the process in case it can answer the question if a process
497 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000498 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 }
Pavel Labath2272c482018-06-18 15:02:23 +0000500 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000501}
Greg Clayton1cb64962011-03-24 04:28:38 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503bool Platform::GetOSBuildString(std::string &s) {
504 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000505
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000507#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000509#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000511#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 else
513 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000514}
515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516bool Platform::GetOSKernelDescription(std::string &s) {
517 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000518#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000520#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000522#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 else
524 return GetRemoteOSKernelDescription(s);
525}
526
527void Platform::AddClangModuleCompilationOptions(
528 Target *target, std::vector<std::string> &options) {
529 std::vector<std::string> default_compilation_options = {
530 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
531
532 options.insert(options.end(), default_compilation_options.begin(),
533 default_compilation_options.end());
534}
535
536FileSpec Platform::GetWorkingDirectory() {
537 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000538 llvm::SmallString<64> cwd;
539 if (llvm::sys::fs::current_path(cwd))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 return FileSpec{};
Pavel Labath1d5855b2017-01-23 15:56:45 +0000541 else
542 return FileSpec(cwd, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 } else {
544 if (!m_working_dir)
545 m_working_dir = GetRemoteWorkingDirectory();
546 return m_working_dir;
547 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000548}
549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550struct RecurseCopyBaton {
551 const FileSpec &dst;
552 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000553 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000554};
555
Greg Claytonfbb76342013-11-20 21:07:01 +0000556static FileSpec::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000557RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 const FileSpec &src) {
559 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000560 namespace fs = llvm::sys::fs;
561 switch (ft) {
562 case fs::file_type::fifo_file:
563 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 // we have no way to copy pipes and sockets - ignore them and continue
565 return FileSpec::eEnumerateDirectoryResultNext;
566 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000567
Zachary Turner7d86ee52017-03-08 17:56:08 +0000568 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 // make the new directory and get in there
570 FileSpec dst_dir = rc_baton->dst;
571 if (!dst_dir.GetFilename())
572 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000573 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000574 dst_dir, lldb::eFilePermissionsDirectoryDefault);
575 if (error.Fail()) {
576 rc_baton->error.SetErrorStringWithFormat(
577 "unable to setup directory %s on remote end", dst_dir.GetCString());
578 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580
581 // now recurse
582 std::string src_dir_path(src.GetPath());
583
Adrian Prantl05097242018-04-30 16:49:04 +0000584 // Make a filespec that only fills in the directory of a FileSpec so when
585 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 FileSpec recurse_dst;
587 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000588 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
589 Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000590 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 RecurseCopy_Callback, &rc_baton2);
592 if (rc_baton2.error.Fail()) {
593 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
594 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
595 }
596 return FileSpec::eEnumerateDirectoryResultNext;
597 } break;
598
Zachary Turner7d86ee52017-03-08 17:56:08 +0000599 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000600 // copy the file and keep going
601 FileSpec dst_file = rc_baton->dst;
602 if (!dst_file.GetFilename())
603 dst_file.GetFilename() = src.GetFilename();
604
605 FileSpec src_resolved;
606
607 rc_baton->error = FileSystem::Readlink(src, src_resolved);
608
609 if (rc_baton->error.Fail())
610 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
611
612 rc_baton->error =
613 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
614
615 if (rc_baton->error.Fail())
616 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
617
618 return FileSpec::eEnumerateDirectoryResultNext;
619 } break;
620
Zachary Turner7d86ee52017-03-08 17:56:08 +0000621 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 // copy the file and keep going
623 FileSpec dst_file = rc_baton->dst;
624 if (!dst_file.GetFilename())
625 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000626 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 if (err.Fail()) {
628 rc_baton->error.SetErrorString(err.AsCString());
629 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
630 }
631 return FileSpec::eEnumerateDirectoryResultNext;
632 } break;
633
Zachary Turner7d86ee52017-03-08 17:56:08 +0000634 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 rc_baton->error.SetErrorStringWithFormat(
636 "invalid file detected during copy: %s", src.GetPath().c_str());
637 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
638 break;
639 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000640 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000641}
642
Zachary Turner97206d52017-05-12 04:51:55 +0000643Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
644 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645
646 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
647 if (log)
648 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
649 dst.GetPath().c_str());
650 FileSpec fixed_dst(dst);
651
652 if (!fixed_dst.GetFilename())
653 fixed_dst.GetFilename() = src.GetFilename();
654
655 FileSpec working_dir = GetWorkingDirectory();
656
657 if (dst) {
658 if (dst.GetDirectory()) {
659 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
660 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
661 fixed_dst.GetDirectory() = dst.GetDirectory();
662 }
Adrian Prantl05097242018-04-30 16:49:04 +0000663 // If the fixed destination file doesn't have a directory yet, then we
664 // must have a relative path. We will resolve this relative path against
665 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000666 if (!fixed_dst.GetDirectory()) {
667 FileSpec relative_spec;
668 std::string path;
669 if (working_dir) {
670 relative_spec = working_dir;
671 relative_spec.AppendPathComponent(dst.GetPath());
672 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
673 } else {
674 error.SetErrorStringWithFormat(
675 "platform working directory must be valid for relative path '%s'",
676 dst.GetPath().c_str());
677 return error;
678 }
679 }
680 } else {
681 if (working_dir) {
682 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
683 } else {
684 error.SetErrorStringWithFormat(
685 "platform working directory must be valid for relative path '%s'",
686 dst.GetPath().c_str());
687 return error;
688 }
689 }
690 } else {
691 if (working_dir) {
692 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
693 } else {
694 error.SetErrorStringWithFormat("platform working directory must be valid "
695 "when destination directory is empty");
696 return error;
697 }
698 }
699
700 if (log)
701 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
702 src.GetPath().c_str(), dst.GetPath().c_str(),
703 fixed_dst.GetPath().c_str());
704
705 if (GetSupportsRSync()) {
706 error = PutFile(src, dst);
707 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000708 namespace fs = llvm::sys::fs;
709 switch (fs::get_file_type(src.GetPath(), false)) {
710 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000711 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 uint32_t permissions = src.GetPermissions();
713 if (permissions == 0)
714 permissions = eFilePermissionsDirectoryDefault;
715 error = MakeDirectory(fixed_dst, permissions);
716 if (error.Success()) {
717 // Make a filespec that only fills in the directory of a FileSpec so
718 // when we enumerate we can quickly fill in the filename for dst copies
719 FileSpec recurse_dst;
720 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
721 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000722 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000723 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 RecurseCopy_Callback, &baton);
725 return baton.error;
726 }
727 } break;
728
Zachary Turner7d86ee52017-03-08 17:56:08 +0000729 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000730 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 error = PutFile(src, fixed_dst);
732 break;
733
Zachary Turner7d86ee52017-03-08 17:56:08 +0000734 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000735 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 FileSpec src_resolved;
737 error = FileSystem::Readlink(src, src_resolved);
738 if (error.Success())
739 error = CreateSymlink(dst, src_resolved);
740 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000741 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 error.SetErrorString("platform install doesn't handle pipes");
743 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000744 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 error.SetErrorString("platform install doesn't handle sockets");
746 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000747 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 error.SetErrorString(
749 "platform install doesn't handle non file or directory items");
750 break;
751 }
752 }
753 return error;
754}
755
756bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
757 if (IsHost()) {
758 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000759 LLDB_LOG(log, "{0}", file_spec);
760 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
761 LLDB_LOG(log, "error: {0}", ec.message());
762 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000764 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 } else {
766 m_working_dir.Clear();
767 return SetRemoteWorkingDirectory(file_spec);
768 }
769}
770
Zachary Turner97206d52017-05-12 04:51:55 +0000771Status Platform::MakeDirectory(const FileSpec &file_spec,
772 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000774 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000776 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
778 GetPluginName().GetCString(),
779 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000780 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000782}
783
Zachary Turner97206d52017-05-12 04:51:55 +0000784Status Platform::GetFilePermissions(const FileSpec &file_spec,
785 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000786 if (IsHost()) {
787 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
788 if (Value)
789 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000790 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000791 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000792 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
794 GetPluginName().GetCString(),
795 LLVM_PRETTY_FUNCTION);
796 return error;
797 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000798}
799
Zachary Turner97206d52017-05-12 04:51:55 +0000800Status Platform::SetFilePermissions(const FileSpec &file_spec,
801 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000802 if (IsHost()) {
803 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
804 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
805 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000806 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
808 GetPluginName().GetCString(),
809 LLVM_PRETTY_FUNCTION);
810 return error;
811 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000812}
813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814ConstString Platform::GetName() { return GetPluginName(); }
815
816const char *Platform::GetHostname() {
817 if (IsHost())
818 return "127.0.0.1";
819
820 if (m_name.empty())
821 return nullptr;
822 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000823}
824
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825ConstString Platform::GetFullNameForDylib(ConstString basename) {
826 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000827}
828
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
830 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
831 if (log)
832 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000833 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 m_working_dir = working_dir;
835 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000836}
837
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838const char *Platform::GetUserName(uint32_t uid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000839#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 const char *user_name = GetCachedUserName(uid);
841 if (user_name)
842 return user_name;
843 if (IsHost()) {
844 std::string name;
845 if (HostInfo::LookupUserName(uid, name))
846 return SetCachedUserName(uid, name.c_str(), name.size());
847 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000848#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 return nullptr;
Greg Clayton1cb64962011-03-24 04:28:38 +0000850}
851
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852const char *Platform::GetGroupName(uint32_t gid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000853#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 const char *group_name = GetCachedGroupName(gid);
855 if (group_name)
856 return group_name;
857 if (IsHost()) {
858 std::string name;
859 if (HostInfo::LookupGroupName(gid, name))
860 return SetCachedGroupName(gid, name.c_str(), name.size());
861 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000862#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 return nullptr;
Greg Clayton32e0a752011-03-30 18:16:51 +0000864}
Greg Clayton1cb64962011-03-24 04:28:38 +0000865
Pavel Labath2272c482018-06-18 15:02:23 +0000866bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000868 // We don't need anyone setting the OS version for the host platform, we
869 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000870 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000872 // We have a remote platform, allow setting the target OS version if we
873 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 // request the remote OS version from the connected platform.
875 if (IsConnected())
876 return false;
877 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000878 // We aren't connected and we might want to set the OS version ahead of
879 // time before we connect so we can peruse files and use a local SDK or
880 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000881 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000883 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884 }
885 return false;
886}
887
Zachary Turner97206d52017-05-12 04:51:55 +0000888Status
889Platform::ResolveExecutable(const ModuleSpec &module_spec,
890 lldb::ModuleSP &exe_module_sp,
891 const FileSpecList *module_search_paths_ptr) {
892 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 if (module_spec.GetFileSpec().Exists()) {
894 if (module_spec.GetArchitecture().IsValid()) {
895 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
896 module_search_paths_ptr, nullptr,
897 nullptr);
898 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000899 // No valid architecture was specified, ask the platform for the
900 // architectures that we should be using (in the correct order) and see
901 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 ModuleSpec arch_module_spec(module_spec);
903 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
904 idx, arch_module_spec.GetArchitecture());
905 ++idx) {
906 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
907 module_search_paths_ptr, nullptr,
908 nullptr);
909 // Did we find an executable using one of the
910 if (error.Success() && exe_module_sp)
911 break;
912 }
Greg Claytone996fd32011-03-08 22:40:15 +0000913 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914 } else {
915 error.SetErrorStringWithFormat("'%s' does not exist",
916 module_spec.GetFileSpec().GetPath().c_str());
917 }
918 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000919}
920
Zachary Turner97206d52017-05-12 04:51:55 +0000921Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
922 FileSpec &sym_file) {
923 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 if (sym_spec.GetSymbolFileSpec().Exists())
925 sym_file = sym_spec.GetSymbolFileSpec();
926 else
927 error.SetErrorString("unable to resolve symbol file");
928 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000929}
930
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931bool Platform::ResolveRemotePath(const FileSpec &platform_path,
932 FileSpec &resolved_platform_path) {
933 resolved_platform_path = platform_path;
934 return resolved_platform_path.ResolvePath();
935}
936
937const ArchSpec &Platform::GetSystemArchitecture() {
938 if (IsHost()) {
939 if (!m_system_arch.IsValid()) {
940 // We have a local host platform
941 m_system_arch = HostInfo::GetArchitecture();
942 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000943 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000944 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000945 // We have a remote platform. We can only fetch the remote system
946 // architecture if we are connected, and we don't want to do it more than
947 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000950
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 bool fetch = false;
952 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000953 // We have valid OS version info, check to make sure it wasn't manually
954 // set prior to connecting. If it was manually set prior to connecting,
955 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 if (is_connected && !m_system_arch_set_while_connected)
957 fetch = true;
958 } else {
959 // We don't have valid OS version info, fetch it if we are connected
960 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000961 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962
963 if (fetch) {
964 m_system_arch = GetRemoteSystemArchitecture();
965 m_system_arch_set_while_connected = m_system_arch.IsValid();
966 }
967 }
968 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000969}
970
Pavel Labath7263f1b2017-10-31 10:56:03 +0000971ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
972 if (triple.empty())
973 return ArchSpec();
974 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
975 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
976 return ArchSpec(triple);
977
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000978 if (auto kind = HostInfo::ParseArchitectureKind(triple))
979 return HostInfo::GetArchitecture(*kind);
980
Pavel Labath7263f1b2017-10-31 10:56:03 +0000981 ArchSpec compatible_arch;
982 ArchSpec raw_arch(triple);
983 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
984 return raw_arch;
985
986 if (!compatible_arch.IsValid())
987 return ArchSpec(normalized_triple);
988
989 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
990 if (normalized_triple.getVendorName().empty())
991 normalized_triple.setVendor(compatible_triple.getVendor());
992 if (normalized_triple.getOSName().empty())
993 normalized_triple.setOS(compatible_triple.getOS());
994 if (normalized_triple.getEnvironmentName().empty())
995 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
996 return ArchSpec(normalized_triple);
997}
998
Zachary Turner97206d52017-05-12 04:51:55 +0000999Status Platform::ConnectRemote(Args &args) {
1000 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 if (IsHost())
1002 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1003 "the host platform and is always connected.",
1004 GetPluginName().GetCString());
1005 else
1006 error.SetErrorStringWithFormat(
1007 "Platform::ConnectRemote() is not supported by %s",
1008 GetPluginName().GetCString());
1009 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001010}
1011
Zachary Turner97206d52017-05-12 04:51:55 +00001012Status Platform::DisconnectRemote() {
1013 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 if (IsHost())
1015 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1016 "the host platform and is always connected.",
1017 GetPluginName().GetCString());
1018 else
1019 error.SetErrorStringWithFormat(
1020 "Platform::DisconnectRemote() is not supported by %s",
1021 GetPluginName().GetCString());
1022 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001023}
Greg Clayton32e0a752011-03-30 18:16:51 +00001024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025bool Platform::GetProcessInfo(lldb::pid_t pid,
1026 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +00001027 // Take care of the host case so that each subclass can just call this
1028 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 if (IsHost())
1030 return Host::GetProcessInfo(pid, process_info);
1031 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00001032}
1033
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1035 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +00001036 // Take care of the host case so that each subclass can just call this
1037 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 uint32_t match_count = 0;
1039 if (IsHost())
1040 match_count = Host::FindProcesses(match_info, process_infos);
1041 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001042}
Greg Clayton8b82f082011-04-12 05:54:46 +00001043
Zachary Turner97206d52017-05-12 04:51:55 +00001044Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1045 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1047 if (log)
1048 log->Printf("Platform::%s()", __FUNCTION__);
1049
Adrian Prantl05097242018-04-30 16:49:04 +00001050 // Take care of the host case so that each subclass can just call this
1051 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 if (IsHost()) {
1053 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1054 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1055
1056 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1057 const bool is_localhost = true;
1058 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1059 const bool first_arg_is_full_shell_command = false;
1060 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1061 if (log) {
1062 const FileSpec &shell = launch_info.GetShell();
1063 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
1064 log->Printf(
1065 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1066 ", shell is '%s'",
1067 __FUNCTION__, num_resumes, shell_str);
1068 }
1069
1070 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1071 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1072 num_resumes))
1073 return error;
1074 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1075 error = ShellExpandArguments(launch_info);
1076 if (error.Fail()) {
1077 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1078 "consider launching with 'process "
1079 "launch'.",
1080 error.AsCString("unknown"));
1081 return error;
1082 }
1083 }
1084
Todd Fialaac33cc92014-10-09 01:02:08 +00001085 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1087 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001088
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 error = Host::LaunchProcess(launch_info);
1090 } else
1091 error.SetErrorString(
1092 "base lldb_private::Platform class can't launch remote processes");
1093 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001094}
1095
Zachary Turner97206d52017-05-12 04:51:55 +00001096Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 if (IsHost())
1098 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001099 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001100}
1101
Zachary Turner97206d52017-05-12 04:51:55 +00001102Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001103 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1104 if (log)
1105 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001106
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001108 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 size_t num_debuggers = Debugger::GetNumDebuggers();
1110 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1111 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1112 lldb_private::TargetList &targets = debugger->GetTargetList();
1113 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1114 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1115 if (process->GetID() == pid)
1116 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001117 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001119
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001121 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 "base lldb_private::Platform class can't kill remote processes unless "
1123 "they are controlled by a process plugin");
1124 }
1125 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001126 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001127}
1128
Greg Clayton8b82f082011-04-12 05:54:46 +00001129lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1131 Target *target, // Can be nullptr, if nullptr create a
1132 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001133 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1135 if (log)
1136 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1137 static_cast<void *>(target));
1138
1139 ProcessSP process_sp;
1140 // Make sure we stop at the entry point
1141 launch_info.GetFlags().Set(eLaunchFlagDebug);
1142 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001143 // group, since then we can handle ^C interrupts ourselves w/o having to
1144 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 launch_info.SetLaunchInSeparateProcessGroup(true);
1146
1147 // Allow any StructuredData process-bound plugins to adjust the launch info
1148 // if needed
1149 size_t i = 0;
1150 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001151 // Note iteration can't simply go until a nullptr callback is returned, as it
1152 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1154 for (auto filter_callback = get_filter_func(i, iteration_complete);
1155 !iteration_complete;
1156 filter_callback = get_filter_func(++i, iteration_complete)) {
1157 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001158 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 error = (*filter_callback)(launch_info, target);
1160 if (!error.Success()) {
1161 if (log)
1162 log->Printf("Platform::%s() StructuredDataPlugin launch "
1163 "filter failed.",
1164 __FUNCTION__);
1165 return process_sp;
1166 }
1167 }
1168 }
1169
1170 error = LaunchProcess(launch_info);
1171 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001172 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1174 ")",
1175 __FUNCTION__, launch_info.GetProcessID());
1176 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1177 ProcessAttachInfo attach_info(launch_info);
1178 process_sp = Attach(attach_info, debugger, target, error);
1179 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001180 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1182 __FUNCTION__, process_sp->GetPluginName().AsCString());
1183 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001184
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185 // Since we attached to the process, it will think it needs to detach
1186 // if the process object just goes away without an explicit call to
1187 // Process::Kill() or Process::Detach(), so let it know to kill the
1188 // process if this happens.
1189 process_sp->SetShouldDetach(false);
1190
Adrian Prantl05097242018-04-30 16:49:04 +00001191 // If we didn't have any file actions, the pseudo terminal might have
1192 // been used where the slave side was given as the file to open for
1193 // stdin/out/err after we have already opened the master so we can
1194 // read/write stdin/out/err.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001196 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 process_sp->SetSTDIOFileDescriptor(pty_fd);
1198 }
1199 } else {
1200 if (log)
1201 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1202 error.AsCString());
1203 }
1204 } else {
1205 if (log)
1206 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1207 "invalid process id",
1208 __FUNCTION__);
1209 }
1210 } else {
1211 if (log)
1212 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1213 error.AsCString());
1214 }
1215
1216 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001217}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001218
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001219lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1221 ArchSpec *platform_arch_ptr) {
1222 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001223 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 if (arch.IsValid())
1225 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1226 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001227}
1228
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001229//------------------------------------------------------------------
1230/// Lets a platform answer if it is compatible with a given
1231/// architecture and the target triple contained within.
1232//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1234 bool exact_arch_match,
1235 ArchSpec *compatible_arch_ptr) {
1236 // If the architecture is invalid, we must answer true...
1237 if (arch.IsValid()) {
1238 ArchSpec platform_arch;
1239 // Try for an exact architecture match first.
1240 if (exact_arch_match) {
1241 for (uint32_t arch_idx = 0;
1242 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1243 ++arch_idx) {
1244 if (arch.IsExactMatch(platform_arch)) {
1245 if (compatible_arch_ptr)
1246 *compatible_arch_ptr = platform_arch;
1247 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001248 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 }
1250 } else {
1251 for (uint32_t arch_idx = 0;
1252 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1253 ++arch_idx) {
1254 if (arch.IsCompatibleMatch(platform_arch)) {
1255 if (compatible_arch_ptr)
1256 *compatible_arch_ptr = platform_arch;
1257 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001258 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001260 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 }
1262 if (compatible_arch_ptr)
1263 compatible_arch_ptr->Clear();
1264 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001265}
1266
Zachary Turner97206d52017-05-12 04:51:55 +00001267Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1268 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1270 if (log)
1271 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001272
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273 uint32_t source_open_options =
1274 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001275 namespace fs = llvm::sys::fs;
1276 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001278
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
Zachary Turner97206d52017-05-12 04:51:55 +00001280 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 uint32_t permissions = source_file.GetPermissions(error);
1282 if (permissions == 0)
1283 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001284
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001286 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287 lldb::user_id_t dest_file = OpenFile(
1288 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1289 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1290 permissions, error);
1291 if (log)
1292 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001293
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294 if (error.Fail())
1295 return error;
1296 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001297 return Status("unable to open target file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1299 uint64_t offset = 0;
1300 for (;;) {
1301 size_t bytes_read = buffer_sp->GetByteSize();
1302 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1303 if (error.Fail() || bytes_read == 0)
1304 break;
1305
1306 const uint64_t bytes_written =
1307 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001308 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001310
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311 offset += bytes_written;
1312 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001313 // We didn't write the correct number of bytes, so adjust the file
1314 // position in the source file we are reading from...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001316 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 }
1318 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001321 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001322
1323 // TODO: ChownFile?
1324
1325 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001326}
1327
Zachary Turner97206d52017-05-12 04:51:55 +00001328Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1329 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001331}
1332
Zachary Turner97206d52017-05-12 04:51:55 +00001333Status
1334Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1335 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001336{
Zachary Turner97206d52017-05-12 04:51:55 +00001337 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001339}
1340
Kate Stoneb9c1b512016-09-06 20:57:50 +00001341bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1342 return false;
1343}
1344
Zachary Turner97206d52017-05-12 04:51:55 +00001345Status Platform::Unlink(const FileSpec &path) {
1346 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 return error;
1348}
1349
Ed Maste37c40af2017-08-16 12:55:02 +00001350MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1351 addr_t length, unsigned prot,
1352 unsigned flags, addr_t fd,
1353 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001354 uint64_t flags_platform = 0;
1355 if (flags & eMmapFlagsPrivate)
1356 flags_platform |= MAP_PRIVATE;
1357 if (flags & eMmapFlagsAnon)
1358 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001359
1360 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1361 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362}
1363
Zachary Turner97206d52017-05-12 04:51:55 +00001364lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 const char *command, // Shouldn't be nullptr
1366 const FileSpec &
1367 working_dir, // Pass empty FileSpec to use the current working directory
1368 int *status_ptr, // Pass nullptr if you don't want the process exit status
1369 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1370 // process to exit
1371 std::string
1372 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001373 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 if (IsHost())
1375 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
Pavel Labath19dd1a02018-05-10 10:46:03 +00001376 command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 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
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001400static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001402 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001404 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001405 "Platform-specific options required for rsync to work."},
1406 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001407 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408 "Platform-specific rsync prefix put before the remote path."},
1409 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001410 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 "Do not automatically fill in the remote hostname when composing the "
1412 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001413};
1414
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001415static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001417 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001419 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001421};
1422
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001423static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001425 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 "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 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001760 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 }
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 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001773 return DoLoadImage(process, target_file, nullptr, 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
Jim Ingham0d231f72018-06-28 20:02:11 +00001778 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779 }
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,
Jim Ingham0d231f72018-06-28 20:02:11 +00001787 const std::vector<std::string> *paths,
1788 lldb_private::Status &error,
1789 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001790 error.SetErrorString("LoadImage is not supported on the current platform");
1791 return LLDB_INVALID_IMAGE_TOKEN;
1792}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001793
Jim Ingham0d231f72018-06-28 20:02:11 +00001794uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1795 const lldb_private::FileSpec &remote_filename,
1796 const std::vector<std::string> &paths,
1797 lldb_private::Status &error,
1798 lldb_private::FileSpec *loaded_path)
1799{
1800 FileSpec file_to_use;
1801 if (remote_filename.IsAbsolute())
1802 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1803 false,
1804 remote_filename.GetPathStyle());
1805 else
1806 file_to_use = remote_filename;
1807
1808 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1809}
1810
Zachary Turner97206d52017-05-12 04:51:55 +00001811Status Platform::UnloadImage(lldb_private::Process *process,
1812 uint32_t image_token) {
1813 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001815
Zachary Turner31659452016-11-17 21:15:14 +00001816lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1817 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 lldb_private::Debugger &debugger,
1819 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001820 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001822
Kate Stoneb9c1b512016-09-06 20:57:50 +00001823 if (!target) {
1824 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001825 error = debugger.GetTargetList().CreateTarget(
1826 debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 target = new_target_sp.get();
1828 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001829
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830 if (!target || error.Fail())
1831 return nullptr;
1832
1833 debugger.GetTargetList().SetSelectedTarget(target);
1834
1835 lldb::ProcessSP process_sp =
1836 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1837 if (!process_sp)
1838 return nullptr;
1839
1840 error =
1841 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1842 if (error.Fail())
1843 return nullptr;
1844
1845 return process_sp;
1846}
1847
1848size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001849 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001850 error.Clear();
1851 return 0;
1852}
1853
1854size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1855 BreakpointSite *bp_site) {
1856 ArchSpec arch = target.GetArchitecture();
1857 const uint8_t *trap_opcode = nullptr;
1858 size_t trap_opcode_size = 0;
1859
1860 switch (arch.GetMachine()) {
1861 case llvm::Triple::aarch64: {
1862 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1863 trap_opcode = g_aarch64_opcode;
1864 trap_opcode_size = sizeof(g_aarch64_opcode);
1865 } break;
1866
1867 // TODO: support big-endian arm and thumb trap codes.
1868 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001869 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1870 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001871 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1872 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1873
1874 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001875 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001876
1877 if (bp_loc_sp) {
1878 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001879 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001880 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001881 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001882 }
1883
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001884 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001885 trap_opcode = g_thumb_breakpoint_opcode;
1886 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1887 } else {
1888 trap_opcode = g_arm_breakpoint_opcode;
1889 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1890 }
1891 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001892
Kate Stoneb9c1b512016-09-06 20:57:50 +00001893 case llvm::Triple::mips:
1894 case llvm::Triple::mips64: {
1895 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1896 trap_opcode = g_hex_opcode;
1897 trap_opcode_size = sizeof(g_hex_opcode);
1898 } break;
1899
1900 case llvm::Triple::mipsel:
1901 case llvm::Triple::mips64el: {
1902 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1903 trap_opcode = g_hex_opcode;
1904 trap_opcode_size = sizeof(g_hex_opcode);
1905 } break;
1906
1907 case llvm::Triple::systemz: {
1908 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1909 trap_opcode = g_hex_opcode;
1910 trap_opcode_size = sizeof(g_hex_opcode);
1911 } break;
1912
1913 case llvm::Triple::hexagon: {
1914 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1915 trap_opcode = g_hex_opcode;
1916 trap_opcode_size = sizeof(g_hex_opcode);
1917 } break;
1918
1919 case llvm::Triple::ppc:
1920 case llvm::Triple::ppc64: {
1921 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1922 trap_opcode = g_ppc_opcode;
1923 trap_opcode_size = sizeof(g_ppc_opcode);
1924 } break;
1925
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001926 case llvm::Triple::ppc64le: {
1927 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1928 trap_opcode = g_ppc64le_opcode;
1929 trap_opcode_size = sizeof(g_ppc64le_opcode);
1930 } break;
1931
Kate Stoneb9c1b512016-09-06 20:57:50 +00001932 case llvm::Triple::x86:
1933 case llvm::Triple::x86_64: {
1934 static const uint8_t g_i386_opcode[] = {0xCC};
1935 trap_opcode = g_i386_opcode;
1936 trap_opcode_size = sizeof(g_i386_opcode);
1937 } break;
1938
1939 default:
David Blaikiea322f362017-01-06 00:38:06 +00001940 llvm_unreachable(
1941 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001942 }
1943
1944 assert(bp_site);
1945 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1946 return trap_opcode_size;
1947
1948 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001949}