blob: 0ed2b510fb0384914bae4a2a38131aba7bf74a33 [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
Robert Flack96ad3de2015-05-09 15:53:31 +000049// Define these constants from POSIX mman.h rather than include the file
50// so that they will be correct even when compiled on Linux.
51#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
Greg Claytone996fd32011-03-08 22:40:15 +000059// Use a singleton function for g_local_platform_sp to avoid init
60// constructors since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000061static PlatformSP &GetHostPlatformSP() {
62 static PlatformSP g_platform_sp;
63 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000064}
65
Kate Stoneb9c1b512016-09-06 20:57:50 +000066const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000067
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000068namespace {
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070PropertyDefinition g_properties[] = {
71 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
72 nullptr, "Use module cache."},
73 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
74 nullptr, "Root directory for cached modules."},
75 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077enum { ePropertyUseModuleCache, ePropertyModuleCacheDirectory };
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081ConstString PlatformProperties::GetSettingName() {
82 static ConstString g_setting_name("platform");
83 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086PlatformProperties::PlatformProperties() {
87 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
88 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 auto module_cache_dir = GetModuleCacheDirectory();
91 if (module_cache_dir)
92 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 llvm::SmallString<64> user_home_dir;
95 if (!llvm::sys::path::home_directory(user_home_dir))
96 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 module_cache_dir = FileSpec(user_home_dir.c_str(), false);
99 module_cache_dir.AppendPathComponent(".lldb");
100 module_cache_dir.AppendPathComponent("module_cache");
101 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104bool PlatformProperties::GetUseModuleCache() const {
105 const auto idx = ePropertyUseModuleCache;
106 return m_collection_sp->GetPropertyAtIndexAsBoolean(
107 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
111 return m_collection_sp->SetPropertyAtIndexAsBoolean(
112 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115FileSpec PlatformProperties::GetModuleCacheDirectory() const {
116 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
117 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000118}
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
121 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
122 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000123}
124
Greg Claytone996fd32011-03-08 22:40:15 +0000125//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000127///
128/// There should only be one of these for each host that LLDB runs
129/// upon that should be statically compiled in and registered using
130/// preprocessor macros or other similar build mechanisms.
131///
132/// This platform will be used as the default platform when launching
133/// or attaching to processes unless another platform is specified.
134//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
136
137static std::vector<PlatformSP> &GetPlatformList() {
138 static std::vector<PlatformSP> g_platform_list;
139 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142static std::recursive_mutex &GetPlatformListMutex() {
143 static std::recursive_mutex g_mutex;
144 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000145}
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149void Platform::Terminate() {
150 if (g_initialize_count > 0) {
151 if (--g_initialize_count == 0) {
152 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
153 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
159 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
160 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
164 // The native platform should use its static void Platform::Initialize()
165 // function to register itself as the native platform.
166 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 if (platform_sp) {
169 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
170 GetPlatformList().push_back(platform_sp);
171 }
Greg Claytone996fd32011-03-08 22:40:15 +0000172}
173
Zachary Turner97206d52017-05-12 04:51:55 +0000174Status Platform::GetFileWithUUID(const FileSpec &platform_file,
175 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 // Default to the local case
177 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000178 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000179}
180
Greg Clayton91c0e742013-01-11 23:44:27 +0000181FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182Platform::LocateExecutableScriptingResources(Target *target, Module &module,
183 Stream *feedback_stream) {
184 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187// PlatformSP
188// Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000189//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000190// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000191// if (plugin_name)
192// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193// create_callback =
194// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000195// if (create_callback)
196// {
197// ArchSpec arch;
198// if (process)
199// {
200// arch = process->GetTarget().GetArchitecture();
201// }
202// PlatformSP platform_sp(create_callback(process, &arch));
203// if (platform_sp)
204// return platform_sp;
205// }
206// }
207// else
208// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209// for (uint32_t idx = 0; (create_callback =
210// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
211// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000212// {
213// PlatformSP platform_sp(create_callback(process, nullptr));
214// if (platform_sp)
215// return platform_sp;
216// }
217// }
218// return PlatformSP();
219//}
Jason Molenda1c627542013-04-05 01:03:25 +0000220
Zachary Turner97206d52017-05-12 04:51:55 +0000221Status Platform::GetSharedModule(const ModuleSpec &module_spec,
222 Process *process, ModuleSP &module_sp,
223 const FileSpecList *module_search_paths_ptr,
224 ModuleSP *old_module_sp_ptr,
225 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 if (IsHost())
227 return ModuleList::GetSharedModule(
228 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
229 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 return GetRemoteSharedModule(module_spec, process, module_sp,
232 [&](const ModuleSpec &spec) {
Zachary Turner97206d52017-05-12 04:51:55 +0000233 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 spec, module_sp, module_search_paths_ptr,
235 old_module_sp_ptr, did_create_ptr, false);
236 if (error.Success() && module_sp)
237 module_sp->SetPlatformFileSpec(
238 spec.GetFileSpec());
239 return error;
240 },
241 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
245 const ArchSpec &arch, ModuleSpec &module_spec) {
246 ModuleSpecList module_specs;
247 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
248 module_specs) == 0)
249 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 ModuleSpec matched_module_spec;
252 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
253 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256PlatformSP Platform::Find(const ConstString &name) {
257 if (name) {
258 static ConstString g_host_platform_name("host");
259 if (name == g_host_platform_name)
260 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
263 for (const auto &platform_sp : GetPlatformList()) {
264 if (platform_sp->GetName() == name)
265 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000266 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 }
268 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000269}
270
Zachary Turner97206d52017-05-12 04:51:55 +0000271PlatformSP Platform::Create(const ConstString &name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 PlatformCreateInstance create_callback = nullptr;
273 lldb::PlatformSP platform_sp;
274 if (name) {
275 static ConstString g_host_platform_name("host");
276 if (name == g_host_platform_name)
277 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 create_callback =
280 PluginManager::GetPlatformCreateCallbackForPluginName(name);
281 if (create_callback)
282 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000283 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 error.SetErrorStringWithFormat(
285 "unable to find a plug-in for the platform named \"%s\"",
286 name.GetCString());
287 } else
288 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 if (platform_sp) {
291 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
292 GetPlatformList().push_back(platform_sp);
293 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000296}
297
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000299 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 lldb::PlatformSP platform_sp;
301 if (arch.IsValid()) {
302 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000303 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 // First try exact arch matches across all platforms already created
305 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
306 for (const auto &platform_sp : GetPlatformList()) {
307 if (platform_sp->IsCompatibleArchitecture(arch, true,
308 platform_arch_ptr))
309 return platform_sp;
310 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000311
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 // Next try compatible arch matches across all platforms already created
313 for (const auto &platform_sp : GetPlatformList()) {
314 if (platform_sp->IsCompatibleArchitecture(arch, false,
315 platform_arch_ptr))
316 return platform_sp;
317 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000318 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319
320 PlatformCreateInstance create_callback;
321 // First try exact arch matches across all platform plug-ins
322 uint32_t idx;
323 for (idx = 0; (create_callback =
324 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
325 ++idx) {
326 if (create_callback) {
327 platform_sp = create_callback(false, &arch);
328 if (platform_sp &&
329 platform_sp->IsCompatibleArchitecture(arch, true,
330 platform_arch_ptr)) {
331 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
332 GetPlatformList().push_back(platform_sp);
333 return platform_sp;
334 }
335 }
336 }
337 // Next try compatible arch matches across all platform plug-ins
338 for (idx = 0; (create_callback =
339 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
340 ++idx) {
341 if (create_callback) {
342 platform_sp = create_callback(false, &arch);
343 if (platform_sp &&
344 platform_sp->IsCompatibleArchitecture(arch, false,
345 platform_arch_ptr)) {
346 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
347 GetPlatformList().push_back(platform_sp);
348 return platform_sp;
349 }
350 }
351 }
352 } else
353 error.SetErrorString("invalid platform name");
354 if (platform_arch_ptr)
355 platform_arch_ptr->Clear();
356 platform_sp.reset();
357 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000358}
359
Pavel Labath7263f1b2017-10-31 10:56:03 +0000360ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
361 if (platform)
362 return platform->GetAugmentedArchSpec(triple);
363 return HostInfo::GetAugmentedArchSpec(triple);
364}
365
Greg Claytone996fd32011-03-08 22:40:15 +0000366//------------------------------------------------------------------
367/// Default Constructor
368//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000369Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 : m_is_host(is_host), m_os_version_set_while_connected(false),
371 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
372 m_working_dir(), m_remote_url(), m_name(), m_major_os_version(UINT32_MAX),
373 m_minor_os_version(UINT32_MAX), m_update_os_version(UINT32_MAX),
374 m_system_arch(), m_mutex(), m_uid_map(), m_gid_map(),
375 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
376 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
377 m_ignores_remote_hostname(false), m_trap_handlers(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000378 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 m_module_cache(llvm::make_unique<ModuleCache>()) {
380 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
381 if (log)
382 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000383}
384
385//------------------------------------------------------------------
386/// Destructor.
387///
388/// The destructor is virtual since this class is designed to be
389/// inherited from by the plug-in instance.
390//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391Platform::~Platform() {
392 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
393 if (log)
394 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000395}
396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397void Platform::GetStatus(Stream &strm) {
398 uint32_t major = UINT32_MAX;
399 uint32_t minor = UINT32_MAX;
400 uint32_t update = UINT32_MAX;
401 std::string s;
402 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 ArchSpec arch(GetSystemArchitecture());
405 if (arch.IsValid()) {
406 if (!arch.GetTriple().str().empty()) {
407 strm.Printf(" Triple: ");
408 arch.DumpTriple(strm);
409 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000410 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 if (GetOSVersion(major, minor, update)) {
414 strm.Printf("OS Version: %u", major);
415 if (minor != UINT32_MAX)
416 strm.Printf(".%u", minor);
417 if (update != UINT32_MAX)
418 strm.Printf(".%u", update);
Greg Clayton1cb64962011-03-24 04:28:38 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 if (GetOSBuildString(s))
421 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 strm.EOL();
424 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (GetOSKernelDescription(s))
427 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 if (IsHost()) {
430 strm.Printf(" Hostname: %s\n", GetHostname());
431 } else {
432 const bool is_connected = IsConnected();
433 if (is_connected)
434 strm.Printf(" Hostname: %s\n", GetHostname());
435 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
436 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 if (GetWorkingDirectory()) {
439 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
440 }
441 if (!IsConnected())
442 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 std::string specific_info(GetPlatformSpecificConnectionInformation());
445
446 if (!specific_info.empty())
447 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000448}
449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450bool Platform::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update,
451 Process *process) {
452 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 bool success = m_major_os_version != UINT32_MAX;
455 if (IsHost()) {
456 if (!success) {
457 // We have a local host platform
458 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version,
459 m_update_os_version);
460 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 } else {
463 // We have a remote platform. We can only fetch the remote
464 // OS version if we are connected, and we don't want to do it
465 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 bool fetch = false;
470 if (success) {
471 // We have valid OS version info, check to make sure it wasn't
472 // manually set prior to connecting. If it was manually set prior
473 // to connecting, then lets fetch the actual OS version info
474 // if we are now connected.
475 if (is_connected && !m_os_version_set_while_connected)
476 fetch = true;
477 } else {
478 // We don't have valid OS version info, fetch it if we are connected
479 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000480 }
481
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 if (fetch) {
483 success = GetRemoteOSVersion();
484 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000485 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 }
487
488 if (success) {
489 major = m_major_os_version;
490 minor = m_minor_os_version;
491 update = m_update_os_version;
492 } else if (process) {
493 // Check with the process in case it can answer the question if
494 // a process was provided
495 return process->GetHostOSVersion(major, minor, update);
496 }
497 return success;
Greg Claytonded470d2011-03-19 01:12:21 +0000498}
Greg Clayton1cb64962011-03-24 04:28:38 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500bool Platform::GetOSBuildString(std::string &s) {
501 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000504#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000506#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000508#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 else
510 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000511}
512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513bool Platform::GetOSKernelDescription(std::string &s) {
514 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000515#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000517#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000519#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 else
521 return GetRemoteOSKernelDescription(s);
522}
523
524void Platform::AddClangModuleCompilationOptions(
525 Target *target, std::vector<std::string> &options) {
526 std::vector<std::string> default_compilation_options = {
527 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
528
529 options.insert(options.end(), default_compilation_options.begin(),
530 default_compilation_options.end());
531}
532
533FileSpec Platform::GetWorkingDirectory() {
534 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000535 llvm::SmallString<64> cwd;
536 if (llvm::sys::fs::current_path(cwd))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 return FileSpec{};
Pavel Labath1d5855b2017-01-23 15:56:45 +0000538 else
539 return FileSpec(cwd, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 } else {
541 if (!m_working_dir)
542 m_working_dir = GetRemoteWorkingDirectory();
543 return m_working_dir;
544 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000545}
546
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547struct RecurseCopyBaton {
548 const FileSpec &dst;
549 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000550 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000551};
552
Greg Claytonfbb76342013-11-20 21:07:01 +0000553static FileSpec::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000554RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 const FileSpec &src) {
556 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000557 namespace fs = llvm::sys::fs;
558 switch (ft) {
559 case fs::file_type::fifo_file:
560 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 // we have no way to copy pipes and sockets - ignore them and continue
562 return FileSpec::eEnumerateDirectoryResultNext;
563 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000564
Zachary Turner7d86ee52017-03-08 17:56:08 +0000565 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 // make the new directory and get in there
567 FileSpec dst_dir = rc_baton->dst;
568 if (!dst_dir.GetFilename())
569 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000570 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 dst_dir, lldb::eFilePermissionsDirectoryDefault);
572 if (error.Fail()) {
573 rc_baton->error.SetErrorStringWithFormat(
574 "unable to setup directory %s on remote end", dst_dir.GetCString());
575 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577
578 // now recurse
579 std::string src_dir_path(src.GetPath());
580
581 // Make a filespec that only fills in the directory of a FileSpec so
582 // when we enumerate we can quickly fill in the filename for dst copies
583 FileSpec recurse_dst;
584 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000585 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
586 Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000587 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 RecurseCopy_Callback, &rc_baton2);
589 if (rc_baton2.error.Fail()) {
590 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
591 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
592 }
593 return FileSpec::eEnumerateDirectoryResultNext;
594 } break;
595
Zachary Turner7d86ee52017-03-08 17:56:08 +0000596 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 // copy the file and keep going
598 FileSpec dst_file = rc_baton->dst;
599 if (!dst_file.GetFilename())
600 dst_file.GetFilename() = src.GetFilename();
601
602 FileSpec src_resolved;
603
604 rc_baton->error = FileSystem::Readlink(src, src_resolved);
605
606 if (rc_baton->error.Fail())
607 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
608
609 rc_baton->error =
610 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
611
612 if (rc_baton->error.Fail())
613 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
614
615 return FileSpec::eEnumerateDirectoryResultNext;
616 } break;
617
Zachary Turner7d86ee52017-03-08 17:56:08 +0000618 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 // copy the file and keep going
620 FileSpec dst_file = rc_baton->dst;
621 if (!dst_file.GetFilename())
622 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000623 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 if (err.Fail()) {
625 rc_baton->error.SetErrorString(err.AsCString());
626 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
627 }
628 return FileSpec::eEnumerateDirectoryResultNext;
629 } break;
630
Zachary Turner7d86ee52017-03-08 17:56:08 +0000631 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 rc_baton->error.SetErrorStringWithFormat(
633 "invalid file detected during copy: %s", src.GetPath().c_str());
634 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
635 break;
636 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000637 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000638}
639
Zachary Turner97206d52017-05-12 04:51:55 +0000640Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
641 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642
643 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
644 if (log)
645 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
646 dst.GetPath().c_str());
647 FileSpec fixed_dst(dst);
648
649 if (!fixed_dst.GetFilename())
650 fixed_dst.GetFilename() = src.GetFilename();
651
652 FileSpec working_dir = GetWorkingDirectory();
653
654 if (dst) {
655 if (dst.GetDirectory()) {
656 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
657 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
658 fixed_dst.GetDirectory() = dst.GetDirectory();
659 }
660 // If the fixed destination file doesn't have a directory yet,
661 // then we must have a relative path. We will resolve this relative
662 // path against the platform's working directory
663 if (!fixed_dst.GetDirectory()) {
664 FileSpec relative_spec;
665 std::string path;
666 if (working_dir) {
667 relative_spec = working_dir;
668 relative_spec.AppendPathComponent(dst.GetPath());
669 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
670 } else {
671 error.SetErrorStringWithFormat(
672 "platform working directory must be valid for relative path '%s'",
673 dst.GetPath().c_str());
674 return error;
675 }
676 }
677 } else {
678 if (working_dir) {
679 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
680 } else {
681 error.SetErrorStringWithFormat(
682 "platform working directory must be valid for relative path '%s'",
683 dst.GetPath().c_str());
684 return error;
685 }
686 }
687 } else {
688 if (working_dir) {
689 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
690 } else {
691 error.SetErrorStringWithFormat("platform working directory must be valid "
692 "when destination directory is empty");
693 return error;
694 }
695 }
696
697 if (log)
698 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
699 src.GetPath().c_str(), dst.GetPath().c_str(),
700 fixed_dst.GetPath().c_str());
701
702 if (GetSupportsRSync()) {
703 error = PutFile(src, dst);
704 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000705 namespace fs = llvm::sys::fs;
706 switch (fs::get_file_type(src.GetPath(), false)) {
707 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000708 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000709 uint32_t permissions = src.GetPermissions();
710 if (permissions == 0)
711 permissions = eFilePermissionsDirectoryDefault;
712 error = MakeDirectory(fixed_dst, permissions);
713 if (error.Success()) {
714 // Make a filespec that only fills in the directory of a FileSpec so
715 // when we enumerate we can quickly fill in the filename for dst copies
716 FileSpec recurse_dst;
717 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
718 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000719 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000720 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 RecurseCopy_Callback, &baton);
722 return baton.error;
723 }
724 } break;
725
Zachary Turner7d86ee52017-03-08 17:56:08 +0000726 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000727 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 error = PutFile(src, fixed_dst);
729 break;
730
Zachary Turner7d86ee52017-03-08 17:56:08 +0000731 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000732 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 FileSpec src_resolved;
734 error = FileSystem::Readlink(src, src_resolved);
735 if (error.Success())
736 error = CreateSymlink(dst, src_resolved);
737 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000738 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 error.SetErrorString("platform install doesn't handle pipes");
740 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000741 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 error.SetErrorString("platform install doesn't handle sockets");
743 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000744 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745 error.SetErrorString(
746 "platform install doesn't handle non file or directory items");
747 break;
748 }
749 }
750 return error;
751}
752
753bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
754 if (IsHost()) {
755 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000756 LLDB_LOG(log, "{0}", file_spec);
757 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
758 LLDB_LOG(log, "error: {0}", ec.message());
759 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000761 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 } else {
763 m_working_dir.Clear();
764 return SetRemoteWorkingDirectory(file_spec);
765 }
766}
767
Zachary Turner97206d52017-05-12 04:51:55 +0000768Status Platform::MakeDirectory(const FileSpec &file_spec,
769 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000771 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000773 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
775 GetPluginName().GetCString(),
776 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000777 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000779}
780
Zachary Turner97206d52017-05-12 04:51:55 +0000781Status Platform::GetFilePermissions(const FileSpec &file_spec,
782 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000783 if (IsHost()) {
784 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
785 if (Value)
786 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000787 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000788 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000789 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
791 GetPluginName().GetCString(),
792 LLVM_PRETTY_FUNCTION);
793 return error;
794 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000795}
796
Zachary Turner97206d52017-05-12 04:51:55 +0000797Status Platform::SetFilePermissions(const FileSpec &file_spec,
798 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000799 if (IsHost()) {
800 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
801 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
802 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000803 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
805 GetPluginName().GetCString(),
806 LLVM_PRETTY_FUNCTION);
807 return error;
808 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000809}
810
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811ConstString Platform::GetName() { return GetPluginName(); }
812
813const char *Platform::GetHostname() {
814 if (IsHost())
815 return "127.0.0.1";
816
817 if (m_name.empty())
818 return nullptr;
819 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000820}
821
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822ConstString Platform::GetFullNameForDylib(ConstString basename) {
823 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000824}
825
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
827 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
828 if (log)
829 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000830 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 m_working_dir = working_dir;
832 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000833}
834
Kate Stoneb9c1b512016-09-06 20:57:50 +0000835const char *Platform::GetUserName(uint32_t uid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000836#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 const char *user_name = GetCachedUserName(uid);
838 if (user_name)
839 return user_name;
840 if (IsHost()) {
841 std::string name;
842 if (HostInfo::LookupUserName(uid, name))
843 return SetCachedUserName(uid, name.c_str(), name.size());
844 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000845#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 return nullptr;
Greg Clayton1cb64962011-03-24 04:28:38 +0000847}
848
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849const char *Platform::GetGroupName(uint32_t gid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000850#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851 const char *group_name = GetCachedGroupName(gid);
852 if (group_name)
853 return group_name;
854 if (IsHost()) {
855 std::string name;
856 if (HostInfo::LookupGroupName(gid, name))
857 return SetCachedGroupName(gid, name.c_str(), name.size());
858 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000859#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860 return nullptr;
Greg Clayton32e0a752011-03-30 18:16:51 +0000861}
Greg Clayton1cb64962011-03-24 04:28:38 +0000862
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863bool Platform::SetOSVersion(uint32_t major, uint32_t minor, uint32_t update) {
864 if (IsHost()) {
865 // We don't need anyone setting the OS version for the host platform,
866 // we should be able to figure it out by calling
867 // HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000868 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869 } else {
870 // We have a remote platform, allow setting the target OS version if
871 // we aren't connected, since if we are connected, we should be able to
872 // request the remote OS version from the connected platform.
873 if (IsConnected())
874 return false;
875 else {
876 // We aren't connected and we might want to set the OS version
877 // ahead of time before we connect so we can peruse files and
878 // use a local SDK or PDK cache of support files to disassemble
879 // or do other things.
880 m_major_os_version = major;
881 m_minor_os_version = minor;
882 m_update_os_version = update;
883 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000884 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 }
886 return false;
887}
888
Zachary Turner97206d52017-05-12 04:51:55 +0000889Status
890Platform::ResolveExecutable(const ModuleSpec &module_spec,
891 lldb::ModuleSP &exe_module_sp,
892 const FileSpecList *module_search_paths_ptr) {
893 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000894 if (module_spec.GetFileSpec().Exists()) {
895 if (module_spec.GetArchitecture().IsValid()) {
896 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
897 module_search_paths_ptr, nullptr,
898 nullptr);
899 } else {
900 // No valid architecture was specified, ask the platform for
901 // the architectures that we should be using (in the correct order)
902 // and see if we can find a match that way
903 ModuleSpec arch_module_spec(module_spec);
904 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
905 idx, arch_module_spec.GetArchitecture());
906 ++idx) {
907 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
908 module_search_paths_ptr, nullptr,
909 nullptr);
910 // Did we find an executable using one of the
911 if (error.Success() && exe_module_sp)
912 break;
913 }
Greg Claytone996fd32011-03-08 22:40:15 +0000914 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 } else {
916 error.SetErrorStringWithFormat("'%s' does not exist",
917 module_spec.GetFileSpec().GetPath().c_str());
918 }
919 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000920}
921
Zachary Turner97206d52017-05-12 04:51:55 +0000922Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
923 FileSpec &sym_file) {
924 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 if (sym_spec.GetSymbolFileSpec().Exists())
926 sym_file = sym_spec.GetSymbolFileSpec();
927 else
928 error.SetErrorString("unable to resolve symbol file");
929 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000930}
931
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932bool Platform::ResolveRemotePath(const FileSpec &platform_path,
933 FileSpec &resolved_platform_path) {
934 resolved_platform_path = platform_path;
935 return resolved_platform_path.ResolvePath();
936}
937
938const ArchSpec &Platform::GetSystemArchitecture() {
939 if (IsHost()) {
940 if (!m_system_arch.IsValid()) {
941 // We have a local host platform
942 m_system_arch = HostInfo::GetArchitecture();
943 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000944 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945 } else {
946 // We have a remote platform. We can only fetch the remote
947 // system architecture if we are connected, and we don't want to do it
948 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000951
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952 bool fetch = false;
953 if (m_system_arch.IsValid()) {
954 // We have valid OS version info, check to make sure it wasn't
955 // manually set prior to connecting. If it was manually set prior
956 // to connecting, then lets fetch the actual OS version info
957 // if we are now connected.
958 if (is_connected && !m_system_arch_set_while_connected)
959 fetch = true;
960 } else {
961 // We don't have valid OS version info, fetch it if we are connected
962 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000963 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964
965 if (fetch) {
966 m_system_arch = GetRemoteSystemArchitecture();
967 m_system_arch_set_while_connected = m_system_arch.IsValid();
968 }
969 }
970 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000971}
972
Pavel Labath7263f1b2017-10-31 10:56:03 +0000973ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
974 if (triple.empty())
975 return ArchSpec();
976 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
977 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
978 return ArchSpec(triple);
979
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000980 if (auto kind = HostInfo::ParseArchitectureKind(triple))
981 return HostInfo::GetArchitecture(*kind);
982
Pavel Labath7263f1b2017-10-31 10:56:03 +0000983 ArchSpec compatible_arch;
984 ArchSpec raw_arch(triple);
985 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
986 return raw_arch;
987
988 if (!compatible_arch.IsValid())
989 return ArchSpec(normalized_triple);
990
991 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
992 if (normalized_triple.getVendorName().empty())
993 normalized_triple.setVendor(compatible_triple.getVendor());
994 if (normalized_triple.getOSName().empty())
995 normalized_triple.setOS(compatible_triple.getOS());
996 if (normalized_triple.getEnvironmentName().empty())
997 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
998 return ArchSpec(normalized_triple);
999}
1000
Zachary Turner97206d52017-05-12 04:51:55 +00001001Status Platform::ConnectRemote(Args &args) {
1002 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001003 if (IsHost())
1004 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1005 "the host platform and is always connected.",
1006 GetPluginName().GetCString());
1007 else
1008 error.SetErrorStringWithFormat(
1009 "Platform::ConnectRemote() is not supported by %s",
1010 GetPluginName().GetCString());
1011 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001012}
1013
Zachary Turner97206d52017-05-12 04:51:55 +00001014Status Platform::DisconnectRemote() {
1015 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 if (IsHost())
1017 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1018 "the host platform and is always connected.",
1019 GetPluginName().GetCString());
1020 else
1021 error.SetErrorStringWithFormat(
1022 "Platform::DisconnectRemote() is not supported by %s",
1023 GetPluginName().GetCString());
1024 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001025}
Greg Clayton32e0a752011-03-30 18:16:51 +00001026
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027bool Platform::GetProcessInfo(lldb::pid_t pid,
1028 ProcessInstanceInfo &process_info) {
1029 // Take care of the host case so that each subclass can just
1030 // call this function to get the host functionality.
1031 if (IsHost())
1032 return Host::GetProcessInfo(pid, process_info);
1033 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00001034}
1035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1037 ProcessInstanceInfoList &process_infos) {
1038 // Take care of the host case so that each subclass can just
1039 // call this function to get the host functionality.
1040 uint32_t match_count = 0;
1041 if (IsHost())
1042 match_count = Host::FindProcesses(match_info, process_infos);
1043 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001044}
Greg Clayton8b82f082011-04-12 05:54:46 +00001045
Zachary Turner97206d52017-05-12 04:51:55 +00001046Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1047 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1049 if (log)
1050 log->Printf("Platform::%s()", __FUNCTION__);
1051
1052 // Take care of the host case so that each subclass can just
1053 // call this function to get the host functionality.
1054 if (IsHost()) {
1055 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1056 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1057
1058 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1059 const bool is_localhost = true;
1060 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1061 const bool first_arg_is_full_shell_command = false;
1062 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1063 if (log) {
1064 const FileSpec &shell = launch_info.GetShell();
1065 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
1066 log->Printf(
1067 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1068 ", shell is '%s'",
1069 __FUNCTION__, num_resumes, shell_str);
1070 }
1071
1072 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1073 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1074 num_resumes))
1075 return error;
1076 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1077 error = ShellExpandArguments(launch_info);
1078 if (error.Fail()) {
1079 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1080 "consider launching with 'process "
1081 "launch'.",
1082 error.AsCString("unknown"));
1083 return error;
1084 }
1085 }
1086
Todd Fialaac33cc92014-10-09 01:02:08 +00001087 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1089 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001090
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 error = Host::LaunchProcess(launch_info);
1092 } else
1093 error.SetErrorString(
1094 "base lldb_private::Platform class can't launch remote processes");
1095 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001096}
1097
Zachary Turner97206d52017-05-12 04:51:55 +00001098Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 if (IsHost())
1100 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001101 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001102}
1103
Zachary Turner97206d52017-05-12 04:51:55 +00001104Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1106 if (log)
1107 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 // Try to find a process plugin to handle this Kill request. If we can't,
1110 // fall back to
1111 // the default OS implementation.
1112 size_t num_debuggers = Debugger::GetNumDebuggers();
1113 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1114 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1115 lldb_private::TargetList &targets = debugger->GetTargetList();
1116 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1117 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1118 if (process->GetID() == pid)
1119 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001120 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001122
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001124 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 "base lldb_private::Platform class can't kill remote processes unless "
1126 "they are controlled by a process plugin");
1127 }
1128 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001129 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001130}
1131
Greg Clayton8b82f082011-04-12 05:54:46 +00001132lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1134 Target *target, // Can be nullptr, if nullptr create a
1135 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001136 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1138 if (log)
1139 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1140 static_cast<void *>(target));
1141
1142 ProcessSP process_sp;
1143 // Make sure we stop at the entry point
1144 launch_info.GetFlags().Set(eLaunchFlagDebug);
1145 // We always launch the process we are going to debug in a separate process
1146 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1147 // about the target getting them as well.
1148 launch_info.SetLaunchInSeparateProcessGroup(true);
1149
1150 // Allow any StructuredData process-bound plugins to adjust the launch info
1151 // if needed
1152 size_t i = 0;
1153 bool iteration_complete = false;
1154 // Note iteration can't simply go until a nullptr callback is returned, as
1155 // it is valid for a plugin to not supply a filter.
1156 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1157 for (auto filter_callback = get_filter_func(i, iteration_complete);
1158 !iteration_complete;
1159 filter_callback = get_filter_func(++i, iteration_complete)) {
1160 if (filter_callback) {
1161 // Give this ProcessLaunchInfo filter a chance to adjust the launch
1162 // info.
1163 error = (*filter_callback)(launch_info, target);
1164 if (!error.Success()) {
1165 if (log)
1166 log->Printf("Platform::%s() StructuredDataPlugin launch "
1167 "filter failed.",
1168 __FUNCTION__);
1169 return process_sp;
1170 }
1171 }
1172 }
1173
1174 error = LaunchProcess(launch_info);
1175 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001176 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1178 ")",
1179 __FUNCTION__, launch_info.GetProcessID());
1180 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1181 ProcessAttachInfo attach_info(launch_info);
1182 process_sp = Attach(attach_info, debugger, target, error);
1183 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001184 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1186 __FUNCTION__, process_sp->GetPluginName().AsCString());
1187 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001188
Kate Stoneb9c1b512016-09-06 20:57:50 +00001189 // Since we attached to the process, it will think it needs to detach
1190 // if the process object just goes away without an explicit call to
1191 // Process::Kill() or Process::Detach(), so let it know to kill the
1192 // process if this happens.
1193 process_sp->SetShouldDetach(false);
1194
1195 // If we didn't have any file actions, the pseudo terminal might
1196 // have been used where the slave side was given as the file to
1197 // open for stdin/out/err after we have already opened the master
1198 // so we can read/write stdin/out/err.
1199 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001200 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001201 process_sp->SetSTDIOFileDescriptor(pty_fd);
1202 }
1203 } else {
1204 if (log)
1205 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1206 error.AsCString());
1207 }
1208 } else {
1209 if (log)
1210 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1211 "invalid process id",
1212 __FUNCTION__);
1213 }
1214 } else {
1215 if (log)
1216 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1217 error.AsCString());
1218 }
1219
1220 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001221}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001222
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001223lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1225 ArchSpec *platform_arch_ptr) {
1226 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001227 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 if (arch.IsValid())
1229 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1230 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001231}
1232
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001233//------------------------------------------------------------------
1234/// Lets a platform answer if it is compatible with a given
1235/// architecture and the target triple contained within.
1236//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1238 bool exact_arch_match,
1239 ArchSpec *compatible_arch_ptr) {
1240 // If the architecture is invalid, we must answer true...
1241 if (arch.IsValid()) {
1242 ArchSpec platform_arch;
1243 // Try for an exact architecture match first.
1244 if (exact_arch_match) {
1245 for (uint32_t arch_idx = 0;
1246 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1247 ++arch_idx) {
1248 if (arch.IsExactMatch(platform_arch)) {
1249 if (compatible_arch_ptr)
1250 *compatible_arch_ptr = platform_arch;
1251 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253 }
1254 } else {
1255 for (uint32_t arch_idx = 0;
1256 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1257 ++arch_idx) {
1258 if (arch.IsCompatibleMatch(platform_arch)) {
1259 if (compatible_arch_ptr)
1260 *compatible_arch_ptr = platform_arch;
1261 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001262 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001264 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001265 }
1266 if (compatible_arch_ptr)
1267 compatible_arch_ptr->Clear();
1268 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001269}
1270
Zachary Turner97206d52017-05-12 04:51:55 +00001271Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1272 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1274 if (log)
1275 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001276
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277 uint32_t source_open_options =
1278 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001279 namespace fs = llvm::sys::fs;
1280 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001282
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
Zachary Turner97206d52017-05-12 04:51:55 +00001284 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 uint32_t permissions = source_file.GetPermissions(error);
1286 if (permissions == 0)
1287 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001290 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291 lldb::user_id_t dest_file = OpenFile(
1292 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1293 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1294 permissions, error);
1295 if (log)
1296 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 if (error.Fail())
1299 return error;
1300 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001301 return Status("unable to open target file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1303 uint64_t offset = 0;
1304 for (;;) {
1305 size_t bytes_read = buffer_sp->GetByteSize();
1306 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1307 if (error.Fail() || bytes_read == 0)
1308 break;
1309
1310 const uint64_t bytes_written =
1311 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001312 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001314
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 offset += bytes_written;
1316 if (bytes_written != bytes_read) {
1317 // We didn't write the correct number of bytes, so adjust
1318 // the file position in the source file we are reading from...
1319 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001320 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321 }
1322 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001323
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001325 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326
1327 // TODO: ChownFile?
1328
1329 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001330}
1331
Zachary Turner97206d52017-05-12 04:51:55 +00001332Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1333 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001335}
1336
Zachary Turner97206d52017-05-12 04:51:55 +00001337Status
1338Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1339 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001340{
Zachary Turner97206d52017-05-12 04:51:55 +00001341 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001343}
1344
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1346 return false;
1347}
1348
Zachary Turner97206d52017-05-12 04:51:55 +00001349Status Platform::Unlink(const FileSpec &path) {
1350 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351 return error;
1352}
1353
Ed Maste37c40af2017-08-16 12:55:02 +00001354MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1355 addr_t length, unsigned prot,
1356 unsigned flags, addr_t fd,
1357 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 uint64_t flags_platform = 0;
1359 if (flags & eMmapFlagsPrivate)
1360 flags_platform |= MAP_PRIVATE;
1361 if (flags & eMmapFlagsAnon)
1362 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001363
1364 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1365 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366}
1367
Zachary Turner97206d52017-05-12 04:51:55 +00001368lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369 const char *command, // Shouldn't be nullptr
1370 const FileSpec &
1371 working_dir, // Pass empty FileSpec to use the current working directory
1372 int *status_ptr, // Pass nullptr if you don't want the process exit status
1373 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1374 // process to exit
1375 std::string
1376 *command_output, // Pass nullptr if you don't want the command output
1377 uint32_t
1378 timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001379{
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 if (IsHost())
1381 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
1382 command_output, timeout_sec);
1383 else
Zachary Turner97206d52017-05-12 04:51:55 +00001384 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385}
1386
1387bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1388 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001389 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001390 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001391 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1392 if (!Result)
1393 return false;
1394 std::tie(high, low) = Result->words();
1395 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001396}
1397
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398void Platform::SetLocalCacheDirectory(const char *local) {
1399 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001400}
1401
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402const char *Platform::GetLocalCacheDirectory() {
1403 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001404}
Greg Claytonfbb76342013-11-20 21:07:01 +00001405
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406static OptionDefinition g_rsync_option_table[] = {
1407 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
1408 nullptr, 0, eArgTypeNone, "Enable rsync."},
1409 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
1410 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1411 "Platform-specific options required for rsync to work."},
1412 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
1413 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1414 "Platform-specific rsync prefix put before the remote path."},
1415 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
1416 OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,
1417 "Do not automatically fill in the remote hostname when composing the "
1418 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001419};
1420
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421static OptionDefinition g_ssh_option_table[] = {
1422 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
1423 nullptr, 0, eArgTypeNone, "Enable SSH."},
1424 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
1425 nullptr, nullptr, 0, eArgTypeCommandName,
1426 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001427};
1428
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429static OptionDefinition g_caching_option_table[] = {
1430 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
1431 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePath,
1432 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001433};
1434
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001435llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001436 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001437}
1438
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439void OptionGroupPlatformRSync::OptionParsingStarting(
1440 ExecutionContext *execution_context) {
1441 m_rsync = false;
1442 m_rsync_opts.clear();
1443 m_rsync_prefix.clear();
1444 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001445}
1446
Zachary Turner97206d52017-05-12 04:51:55 +00001447lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001448OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001449 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001451 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 char short_option = (char)GetDefinitions()[option_idx].short_option;
1453 switch (short_option) {
1454 case 'r':
1455 m_rsync = true;
1456 break;
1457
1458 case 'R':
1459 m_rsync_opts.assign(option_arg);
1460 break;
1461
1462 case 'P':
1463 m_rsync_prefix.assign(option_arg);
1464 break;
1465
1466 case 'i':
1467 m_ignores_remote_hostname = true;
1468 break;
1469
1470 default:
1471 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1472 break;
1473 }
1474
1475 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001476}
1477
Greg Clayton4116e932012-05-15 02:33:01 +00001478lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1480 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001481}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001482
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001483llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001484 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001485}
1486
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487void OptionGroupPlatformSSH::OptionParsingStarting(
1488 ExecutionContext *execution_context) {
1489 m_ssh = false;
1490 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001491}
1492
Zachary Turner97206d52017-05-12 04:51:55 +00001493lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001494OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001495 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001497 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 char short_option = (char)GetDefinitions()[option_idx].short_option;
1499 switch (short_option) {
1500 case 's':
1501 m_ssh = true;
1502 break;
1503
1504 case 'S':
1505 m_ssh_opts.assign(option_arg);
1506 break;
1507
1508 default:
1509 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1510 break;
1511 }
1512
1513 return error;
1514}
1515
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001516llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001517 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518}
1519
1520void OptionGroupPlatformCaching::OptionParsingStarting(
1521 ExecutionContext *execution_context) {
1522 m_cache_dir.clear();
1523}
1524
Zachary Turner97206d52017-05-12 04:51:55 +00001525lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001526 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001528 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529 char short_option = (char)GetDefinitions()[option_idx].short_option;
1530 switch (short_option) {
1531 case 'c':
1532 m_cache_dir.assign(option_arg);
1533 break;
1534
1535 default:
1536 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1537 break;
1538 }
1539
1540 return error;
1541}
1542
Pavel Labath62930e52018-01-10 11:57:31 +00001543Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544
1545const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1546 if (!m_calculated_trap_handlers) {
1547 std::lock_guard<std::mutex> guard(m_mutex);
1548 if (!m_calculated_trap_handlers) {
1549 CalculateTrapHandlerSymbolNames();
1550 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001552 }
1553 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001554}
1555
Zachary Turner97206d52017-05-12 04:51:55 +00001556Status Platform::GetCachedExecutable(
1557 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1558 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 const auto platform_spec = module_spec.GetFileSpec();
1560 const auto error = LoadCachedExecutable(
1561 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1562 if (error.Success()) {
1563 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1564 module_spec.GetPlatformFileSpec() = platform_spec;
1565 }
1566
1567 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001568}
1569
Zachary Turner97206d52017-05-12 04:51:55 +00001570Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1572 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1573 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1574 [&](const ModuleSpec &spec) {
1575 return remote_platform.ResolveExecutable(
1576 spec, module_sp, module_search_paths_ptr);
1577 },
1578 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001579}
1580
Zachary Turner97206d52017-05-12 04:51:55 +00001581Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1582 Process *process,
1583 lldb::ModuleSP &module_sp,
1584 const ModuleResolver &module_resolver,
1585 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 // Get module information from a target.
1587 ModuleSpec resolved_module_spec;
1588 bool got_module_spec = false;
1589 if (process) {
1590 // Try to get module information from the process
1591 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1592 module_spec.GetArchitecture(),
1593 resolved_module_spec)) {
1594 if (module_spec.GetUUID().IsValid() == false ||
1595 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1596 got_module_spec = true;
1597 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001598 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001600
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001601 if (module_spec.GetArchitecture().IsValid() == false) {
Zachary Turner97206d52017-05-12 04:51:55 +00001602 Status error;
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001603 // No valid architecture was specified, ask the platform for
1604 // the architectures that we should be using (in the correct order)
1605 // and see if we can find a match that way
1606 ModuleSpec arch_module_spec(module_spec);
1607 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1608 idx, arch_module_spec.GetArchitecture());
1609 ++idx) {
1610 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1611 nullptr, nullptr);
1612 // Did we find an executable using one of the
1613 if (error.Success() && module_sp)
1614 break;
1615 }
1616 if (module_sp)
1617 got_module_spec = true;
1618 }
1619
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001621 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1623 resolved_module_spec)) {
1624 if (module_spec.GetUUID().IsValid() == false ||
1625 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1626 return module_resolver(module_spec);
1627 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001628 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001630
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 // If we are looking for a specific UUID, make sure resolved_module_spec has
1632 // the same one before we search.
1633 if (module_spec.GetUUID().IsValid()) {
1634 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1635 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001636
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 // Trying to find a module by UUID on local file system.
1638 const auto error = module_resolver(resolved_module_spec);
1639 if (error.Fail()) {
1640 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001641 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001643
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001645}
1646
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1648 lldb::ModuleSP &module_sp,
1649 bool *did_create_ptr) {
1650 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1651 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001652 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653
1654 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1655
1656 // Check local cache for a module.
1657 auto error = m_module_cache->GetAndPut(
1658 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1659 [this](const ModuleSpec &module_spec,
1660 const FileSpec &tmp_download_file_spec) {
1661 return DownloadModuleSlice(
1662 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1663 module_spec.GetObjectSize(), tmp_download_file_spec);
1664
1665 },
1666 [this](const ModuleSP &module_sp,
1667 const FileSpec &tmp_download_file_spec) {
1668 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1669 },
1670 module_sp, did_create_ptr);
1671 if (error.Success())
1672 return true;
1673
1674 if (log)
1675 log->Printf("Platform::%s - module %s not found in local cache: %s",
1676 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1677 error.AsCString());
1678 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001679}
1680
Zachary Turner97206d52017-05-12 04:51:55 +00001681Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1682 const uint64_t src_offset,
1683 const uint64_t src_size,
1684 const FileSpec &dst_file_spec) {
1685 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001686
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001687 std::error_code EC;
1688 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1689 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001690 error.SetErrorStringWithFormat("unable to open destination file: %s",
1691 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001692 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001693 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001694
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1696 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001697
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 if (error.Fail()) {
1699 error.SetErrorStringWithFormat("unable to open source file: %s",
1700 error.AsCString());
1701 return error;
1702 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001703
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704 std::vector<char> buffer(1024);
1705 auto offset = src_offset;
1706 uint64_t total_bytes_read = 0;
1707 while (total_bytes_read < src_size) {
1708 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1709 src_size - total_bytes_read);
1710 const uint64_t n_read =
1711 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001712 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001713 break;
1714 if (n_read == 0) {
1715 error.SetErrorString("read 0 bytes");
1716 break;
1717 }
1718 offset += n_read;
1719 total_bytes_read += n_read;
1720 dst.write(&buffer[0], n_read);
1721 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001722
Zachary Turner97206d52017-05-12 04:51:55 +00001723 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724 CloseFile(src_fd, close_error); // Ignoring close error.
1725
1726 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001727}
1728
Zachary Turner97206d52017-05-12 04:51:55 +00001729Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1730 const FileSpec &dst_file_spec) {
1731 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001733}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001734
Kate Stoneb9c1b512016-09-06 20:57:50 +00001735FileSpec Platform::GetModuleCacheRoot() {
1736 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1737 dir_spec.AppendPathComponent(GetName().AsCString());
1738 return dir_spec;
1739}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001740
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001742
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1744 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1745 return s_default_unix_signals_sp;
1746}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001747
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748const UnixSignalsSP &Platform::GetUnixSignals() {
1749 if (IsHost())
1750 return Host::GetUnixSignals();
1751 return GetRemoteUnixSignals();
1752}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001753
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754uint32_t Platform::LoadImage(lldb_private::Process *process,
1755 const lldb_private::FileSpec &local_file,
1756 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001757 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 if (local_file && remote_file) {
1759 // Both local and remote file was specified. Install the local file to the
1760 // given location.
1761 if (IsRemote() || local_file != remote_file) {
1762 error = Install(local_file, remote_file);
1763 if (error.Fail())
1764 return LLDB_INVALID_IMAGE_TOKEN;
1765 }
1766 return DoLoadImage(process, remote_file, error);
1767 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001768
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769 if (local_file) {
1770 // Only local file was specified. Install it to the current working
1771 // directory.
1772 FileSpec target_file = GetWorkingDirectory();
1773 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1774 if (IsRemote() || local_file != target_file) {
1775 error = Install(local_file, target_file);
1776 if (error.Fail())
1777 return LLDB_INVALID_IMAGE_TOKEN;
1778 }
1779 return DoLoadImage(process, target_file, error);
1780 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001781
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782 if (remote_file) {
1783 // Only remote file was specified so we don't have to do any copying
1784 return DoLoadImage(process, remote_file, error);
1785 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001786
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787 error.SetErrorString("Neither local nor remote file was specified");
1788 return LLDB_INVALID_IMAGE_TOKEN;
1789}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1792 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001793 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001794 error.SetErrorString("LoadImage is not supported on the current platform");
1795 return LLDB_INVALID_IMAGE_TOKEN;
1796}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001797
Zachary Turner97206d52017-05-12 04:51:55 +00001798Status Platform::UnloadImage(lldb_private::Process *process,
1799 uint32_t image_token) {
1800 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001802
Zachary Turner31659452016-11-17 21:15:14 +00001803lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1804 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001805 lldb_private::Debugger &debugger,
1806 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001807 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001809
Kate Stoneb9c1b512016-09-06 20:57:50 +00001810 if (!target) {
1811 TargetSP new_target_sp;
Zachary Turnera47464b2016-11-18 20:44:46 +00001812 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
1813 nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814 target = new_target_sp.get();
1815 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001816
Kate Stoneb9c1b512016-09-06 20:57:50 +00001817 if (!target || error.Fail())
1818 return nullptr;
1819
1820 debugger.GetTargetList().SetSelectedTarget(target);
1821
1822 lldb::ProcessSP process_sp =
1823 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1824 if (!process_sp)
1825 return nullptr;
1826
1827 error =
1828 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1829 if (error.Fail())
1830 return nullptr;
1831
1832 return process_sp;
1833}
1834
1835size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001836 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001837 error.Clear();
1838 return 0;
1839}
1840
1841size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1842 BreakpointSite *bp_site) {
1843 ArchSpec arch = target.GetArchitecture();
1844 const uint8_t *trap_opcode = nullptr;
1845 size_t trap_opcode_size = 0;
1846
1847 switch (arch.GetMachine()) {
1848 case llvm::Triple::aarch64: {
1849 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1850 trap_opcode = g_aarch64_opcode;
1851 trap_opcode_size = sizeof(g_aarch64_opcode);
1852 } break;
1853
1854 // TODO: support big-endian arm and thumb trap codes.
1855 case llvm::Triple::arm: {
1856 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
1857 // but the linux kernel does otherwise.
1858 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1859 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1860
1861 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
1862 AddressClass addr_class = eAddressClassUnknown;
1863
1864 if (bp_loc_sp) {
1865 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
1866 if (addr_class == eAddressClassUnknown &&
1867 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
1868 addr_class = eAddressClassCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001869 }
1870
Kate Stoneb9c1b512016-09-06 20:57:50 +00001871 if (addr_class == eAddressClassCodeAlternateISA) {
1872 trap_opcode = g_thumb_breakpoint_opcode;
1873 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1874 } else {
1875 trap_opcode = g_arm_breakpoint_opcode;
1876 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1877 }
1878 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001879
Kate Stoneb9c1b512016-09-06 20:57:50 +00001880 case llvm::Triple::mips:
1881 case llvm::Triple::mips64: {
1882 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1883 trap_opcode = g_hex_opcode;
1884 trap_opcode_size = sizeof(g_hex_opcode);
1885 } break;
1886
1887 case llvm::Triple::mipsel:
1888 case llvm::Triple::mips64el: {
1889 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1890 trap_opcode = g_hex_opcode;
1891 trap_opcode_size = sizeof(g_hex_opcode);
1892 } break;
1893
1894 case llvm::Triple::systemz: {
1895 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1896 trap_opcode = g_hex_opcode;
1897 trap_opcode_size = sizeof(g_hex_opcode);
1898 } break;
1899
1900 case llvm::Triple::hexagon: {
1901 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1902 trap_opcode = g_hex_opcode;
1903 trap_opcode_size = sizeof(g_hex_opcode);
1904 } break;
1905
1906 case llvm::Triple::ppc:
1907 case llvm::Triple::ppc64: {
1908 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1909 trap_opcode = g_ppc_opcode;
1910 trap_opcode_size = sizeof(g_ppc_opcode);
1911 } break;
1912
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001913 case llvm::Triple::ppc64le: {
1914 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1915 trap_opcode = g_ppc64le_opcode;
1916 trap_opcode_size = sizeof(g_ppc64le_opcode);
1917 } break;
1918
Kate Stoneb9c1b512016-09-06 20:57:50 +00001919 case llvm::Triple::x86:
1920 case llvm::Triple::x86_64: {
1921 static const uint8_t g_i386_opcode[] = {0xCC};
1922 trap_opcode = g_i386_opcode;
1923 trap_opcode_size = sizeof(g_i386_opcode);
1924 } break;
1925
1926 default:
David Blaikiea322f362017-01-06 00:38:06 +00001927 llvm_unreachable(
1928 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001929 }
1930
1931 assert(bp_site);
1932 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1933 return trap_opcode_size;
1934
1935 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001936}