blob: 498facf8e0d025fb517a4e34a107edabb889963c [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>
13#include <fstream>
14#include <vector>
15
Greg Claytone996fd32011-03-08 22:40:15 +000016// Other libraries and framework includes
Eugene Zelenko9394d7722016-02-18 00:10:17 +000017#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
19
Greg Claytone996fd32011-03-08 22:40:15 +000020// Project includes
Greg Clayton4116e932012-05-15 02:33:01 +000021#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000022#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000023#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000024#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000026#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000027#include "lldb/Core/StreamFile.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000028#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000029#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000030#include "lldb/Host/HostInfo.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000031#include "lldb/Host/OptionParser.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000032#include "lldb/Interpreter/OptionValueProperties.h"
33#include "lldb/Interpreter/Property.h"
34#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000035#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000036#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000037#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000038#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000039#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000040#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000041#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000042#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000043#include "lldb/Utility/Status.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000044#include "lldb/Utility/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000045
Zachary Turner7d86ee52017-03-08 17:56:08 +000046#include "llvm/Support/FileSystem.h"
47
Robert Flack96ad3de2015-05-09 15:53:31 +000048// Define these constants from POSIX mman.h rather than include the file
49// so that they will be correct even when compiled on Linux.
50#define MAP_PRIVATE 2
51#define MAP_ANON 0x1000
52
Greg Claytone996fd32011-03-08 22:40:15 +000053using namespace lldb;
54using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000055
56static uint32_t g_initialize_count = 0;
57
Greg Claytone996fd32011-03-08 22:40:15 +000058// Use a singleton function for g_local_platform_sp to avoid init
59// constructors since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000060static PlatformSP &GetHostPlatformSP() {
61 static PlatformSP g_platform_sp;
62 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000063}
64
Kate Stoneb9c1b512016-09-06 20:57:50 +000065const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000066
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000067namespace {
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069PropertyDefinition g_properties[] = {
70 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
71 nullptr, "Use module cache."},
72 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
73 nullptr, "Root directory for cached modules."},
74 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076enum { ePropertyUseModuleCache, ePropertyModuleCacheDirectory };
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080ConstString PlatformProperties::GetSettingName() {
81 static ConstString g_setting_name("platform");
82 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000083}
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085PlatformProperties::PlatformProperties() {
86 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
87 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 auto module_cache_dir = GetModuleCacheDirectory();
90 if (module_cache_dir)
91 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 llvm::SmallString<64> user_home_dir;
94 if (!llvm::sys::path::home_directory(user_home_dir))
95 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 module_cache_dir = FileSpec(user_home_dir.c_str(), false);
98 module_cache_dir.AppendPathComponent(".lldb");
99 module_cache_dir.AppendPathComponent("module_cache");
100 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103bool PlatformProperties::GetUseModuleCache() const {
104 const auto idx = ePropertyUseModuleCache;
105 return m_collection_sp->GetPropertyAtIndexAsBoolean(
106 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
110 return m_collection_sp->SetPropertyAtIndexAsBoolean(
111 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114FileSpec PlatformProperties::GetModuleCacheDirectory() const {
115 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
116 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000117}
118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
120 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
121 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000122}
123
Greg Claytone996fd32011-03-08 22:40:15 +0000124//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000126///
127/// There should only be one of these for each host that LLDB runs
128/// upon that should be statically compiled in and registered using
129/// preprocessor macros or other similar build mechanisms.
130///
131/// This platform will be used as the default platform when launching
132/// or attaching to processes unless another platform is specified.
133//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
135
136static std::vector<PlatformSP> &GetPlatformList() {
137 static std::vector<PlatformSP> g_platform_list;
138 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141static std::recursive_mutex &GetPlatformListMutex() {
142 static std::recursive_mutex g_mutex;
143 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148void Platform::Terminate() {
149 if (g_initialize_count > 0) {
150 if (--g_initialize_count == 0) {
151 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
152 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000153 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
158 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
159 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000160}
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
163 // The native platform should use its static void Platform::Initialize()
164 // function to register itself as the native platform.
165 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 if (platform_sp) {
168 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
169 GetPlatformList().push_back(platform_sp);
170 }
Greg Claytone996fd32011-03-08 22:40:15 +0000171}
172
Zachary Turner97206d52017-05-12 04:51:55 +0000173Status Platform::GetFileWithUUID(const FileSpec &platform_file,
174 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 // Default to the local case
176 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000177 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000178}
179
Greg Clayton91c0e742013-01-11 23:44:27 +0000180FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181Platform::LocateExecutableScriptingResources(Target *target, Module &module,
182 Stream *feedback_stream) {
183 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000184}
185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186// PlatformSP
187// Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000188//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000189// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000190// if (plugin_name)
191// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192// create_callback =
193// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000194// if (create_callback)
195// {
196// ArchSpec arch;
197// if (process)
198// {
199// arch = process->GetTarget().GetArchitecture();
200// }
201// PlatformSP platform_sp(create_callback(process, &arch));
202// if (platform_sp)
203// return platform_sp;
204// }
205// }
206// else
207// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208// for (uint32_t idx = 0; (create_callback =
209// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
210// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000211// {
212// PlatformSP platform_sp(create_callback(process, nullptr));
213// if (platform_sp)
214// return platform_sp;
215// }
216// }
217// return PlatformSP();
218//}
Jason Molenda1c627542013-04-05 01:03:25 +0000219
Zachary Turner97206d52017-05-12 04:51:55 +0000220Status Platform::GetSharedModule(const ModuleSpec &module_spec,
221 Process *process, ModuleSP &module_sp,
222 const FileSpecList *module_search_paths_ptr,
223 ModuleSP *old_module_sp_ptr,
224 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 if (IsHost())
226 return ModuleList::GetSharedModule(
227 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
228 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 return GetRemoteSharedModule(module_spec, process, module_sp,
231 [&](const ModuleSpec &spec) {
Zachary Turner97206d52017-05-12 04:51:55 +0000232 Status error = ModuleList::GetSharedModule(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 spec, module_sp, module_search_paths_ptr,
234 old_module_sp_ptr, did_create_ptr, false);
235 if (error.Success() && module_sp)
236 module_sp->SetPlatformFileSpec(
237 spec.GetFileSpec());
238 return error;
239 },
240 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000241}
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
244 const ArchSpec &arch, ModuleSpec &module_spec) {
245 ModuleSpecList module_specs;
246 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
247 module_specs) == 0)
248 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 ModuleSpec matched_module_spec;
251 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
252 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000253}
254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255PlatformSP Platform::Find(const ConstString &name) {
256 if (name) {
257 static ConstString g_host_platform_name("host");
258 if (name == g_host_platform_name)
259 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
262 for (const auto &platform_sp : GetPlatformList()) {
263 if (platform_sp->GetName() == name)
264 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000265 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 }
267 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000268}
269
Zachary Turner97206d52017-05-12 04:51:55 +0000270PlatformSP Platform::Create(const ConstString &name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 PlatformCreateInstance create_callback = nullptr;
272 lldb::PlatformSP platform_sp;
273 if (name) {
274 static ConstString g_host_platform_name("host");
275 if (name == g_host_platform_name)
276 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 create_callback =
279 PluginManager::GetPlatformCreateCallbackForPluginName(name);
280 if (create_callback)
281 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000282 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 error.SetErrorStringWithFormat(
284 "unable to find a plug-in for the platform named \"%s\"",
285 name.GetCString());
286 } else
287 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (platform_sp) {
290 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
291 GetPlatformList().push_back(platform_sp);
292 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000295}
296
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000298 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 lldb::PlatformSP platform_sp;
300 if (arch.IsValid()) {
301 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000302 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 // First try exact arch matches across all platforms already created
304 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
305 for (const auto &platform_sp : GetPlatformList()) {
306 if (platform_sp->IsCompatibleArchitecture(arch, true,
307 platform_arch_ptr))
308 return platform_sp;
309 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311 // Next try compatible arch matches across all platforms already created
312 for (const auto &platform_sp : GetPlatformList()) {
313 if (platform_sp->IsCompatibleArchitecture(arch, false,
314 platform_arch_ptr))
315 return platform_sp;
316 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000317 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318
319 PlatformCreateInstance create_callback;
320 // First try exact arch matches across all platform plug-ins
321 uint32_t idx;
322 for (idx = 0; (create_callback =
323 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
324 ++idx) {
325 if (create_callback) {
326 platform_sp = create_callback(false, &arch);
327 if (platform_sp &&
328 platform_sp->IsCompatibleArchitecture(arch, true,
329 platform_arch_ptr)) {
330 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
331 GetPlatformList().push_back(platform_sp);
332 return platform_sp;
333 }
334 }
335 }
336 // Next try compatible arch matches across all platform plug-ins
337 for (idx = 0; (create_callback =
338 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
339 ++idx) {
340 if (create_callback) {
341 platform_sp = create_callback(false, &arch);
342 if (platform_sp &&
343 platform_sp->IsCompatibleArchitecture(arch, false,
344 platform_arch_ptr)) {
345 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
346 GetPlatformList().push_back(platform_sp);
347 return platform_sp;
348 }
349 }
350 }
351 } else
352 error.SetErrorString("invalid platform name");
353 if (platform_arch_ptr)
354 platform_arch_ptr->Clear();
355 platform_sp.reset();
356 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000357}
358
Greg Claytone996fd32011-03-08 22:40:15 +0000359//------------------------------------------------------------------
360/// Default Constructor
361//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000362Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 : m_is_host(is_host), m_os_version_set_while_connected(false),
364 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
365 m_working_dir(), m_remote_url(), m_name(), m_major_os_version(UINT32_MAX),
366 m_minor_os_version(UINT32_MAX), m_update_os_version(UINT32_MAX),
367 m_system_arch(), m_mutex(), m_uid_map(), m_gid_map(),
368 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
369 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
370 m_ignores_remote_hostname(false), m_trap_handlers(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000371 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 m_module_cache(llvm::make_unique<ModuleCache>()) {
373 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
374 if (log)
375 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000376}
377
378//------------------------------------------------------------------
379/// Destructor.
380///
381/// The destructor is virtual since this class is designed to be
382/// inherited from by the plug-in instance.
383//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384Platform::~Platform() {
385 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
386 if (log)
387 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000388}
389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390void Platform::GetStatus(Stream &strm) {
391 uint32_t major = UINT32_MAX;
392 uint32_t minor = UINT32_MAX;
393 uint32_t update = UINT32_MAX;
394 std::string s;
395 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 ArchSpec arch(GetSystemArchitecture());
398 if (arch.IsValid()) {
399 if (!arch.GetTriple().str().empty()) {
400 strm.Printf(" Triple: ");
401 arch.DumpTriple(strm);
402 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 if (GetOSVersion(major, minor, update)) {
407 strm.Printf("OS Version: %u", major);
408 if (minor != UINT32_MAX)
409 strm.Printf(".%u", minor);
410 if (update != UINT32_MAX)
411 strm.Printf(".%u", update);
Greg Clayton1cb64962011-03-24 04:28:38 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 if (GetOSBuildString(s))
414 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 strm.EOL();
417 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 if (GetOSKernelDescription(s))
420 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 if (IsHost()) {
423 strm.Printf(" Hostname: %s\n", GetHostname());
424 } else {
425 const bool is_connected = IsConnected();
426 if (is_connected)
427 strm.Printf(" Hostname: %s\n", GetHostname());
428 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
429 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 if (GetWorkingDirectory()) {
432 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
433 }
434 if (!IsConnected())
435 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000436
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 std::string specific_info(GetPlatformSpecificConnectionInformation());
438
439 if (!specific_info.empty())
440 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000441}
442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443bool Platform::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update,
444 Process *process) {
445 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000446
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 bool success = m_major_os_version != UINT32_MAX;
448 if (IsHost()) {
449 if (!success) {
450 // We have a local host platform
451 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version,
452 m_update_os_version);
453 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000454 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 } else {
456 // We have a remote platform. We can only fetch the remote
457 // OS version if we are connected, and we don't want to do it
458 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000461
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 bool fetch = false;
463 if (success) {
464 // We have valid OS version info, check to make sure it wasn't
465 // manually set prior to connecting. If it was manually set prior
466 // to connecting, then lets fetch the actual OS version info
467 // if we are now connected.
468 if (is_connected && !m_os_version_set_while_connected)
469 fetch = true;
470 } else {
471 // We don't have valid OS version info, fetch it if we are connected
472 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000473 }
474
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 if (fetch) {
476 success = GetRemoteOSVersion();
477 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000478 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 }
480
481 if (success) {
482 major = m_major_os_version;
483 minor = m_minor_os_version;
484 update = m_update_os_version;
485 } else if (process) {
486 // Check with the process in case it can answer the question if
487 // a process was provided
488 return process->GetHostOSVersion(major, minor, update);
489 }
490 return success;
Greg Claytonded470d2011-03-19 01:12:21 +0000491}
Greg Clayton1cb64962011-03-24 04:28:38 +0000492
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493bool Platform::GetOSBuildString(std::string &s) {
494 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000497#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000499#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000501#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 else
503 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000504}
505
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506bool Platform::GetOSKernelDescription(std::string &s) {
507 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000508#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000510#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000512#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 else
514 return GetRemoteOSKernelDescription(s);
515}
516
517void Platform::AddClangModuleCompilationOptions(
518 Target *target, std::vector<std::string> &options) {
519 std::vector<std::string> default_compilation_options = {
520 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
521
522 options.insert(options.end(), default_compilation_options.begin(),
523 default_compilation_options.end());
524}
525
526FileSpec Platform::GetWorkingDirectory() {
527 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000528 llvm::SmallString<64> cwd;
529 if (llvm::sys::fs::current_path(cwd))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 return FileSpec{};
Pavel Labath1d5855b2017-01-23 15:56:45 +0000531 else
532 return FileSpec(cwd, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 } else {
534 if (!m_working_dir)
535 m_working_dir = GetRemoteWorkingDirectory();
536 return m_working_dir;
537 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000538}
539
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540struct RecurseCopyBaton {
541 const FileSpec &dst;
542 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000543 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000544};
545
Greg Claytonfbb76342013-11-20 21:07:01 +0000546static FileSpec::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000547RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548 const FileSpec &src) {
549 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000550 namespace fs = llvm::sys::fs;
551 switch (ft) {
552 case fs::file_type::fifo_file:
553 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 // we have no way to copy pipes and sockets - ignore them and continue
555 return FileSpec::eEnumerateDirectoryResultNext;
556 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000557
Zachary Turner7d86ee52017-03-08 17:56:08 +0000558 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 // make the new directory and get in there
560 FileSpec dst_dir = rc_baton->dst;
561 if (!dst_dir.GetFilename())
562 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000563 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 dst_dir, lldb::eFilePermissionsDirectoryDefault);
565 if (error.Fail()) {
566 rc_baton->error.SetErrorStringWithFormat(
567 "unable to setup directory %s on remote end", dst_dir.GetCString());
568 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000569 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570
571 // now recurse
572 std::string src_dir_path(src.GetPath());
573
574 // Make a filespec that only fills in the directory of a FileSpec so
575 // when we enumerate we can quickly fill in the filename for dst copies
576 FileSpec recurse_dst;
577 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000578 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
579 Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000580 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581 RecurseCopy_Callback, &rc_baton2);
582 if (rc_baton2.error.Fail()) {
583 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
584 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
585 }
586 return FileSpec::eEnumerateDirectoryResultNext;
587 } break;
588
Zachary Turner7d86ee52017-03-08 17:56:08 +0000589 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 // copy the file and keep going
591 FileSpec dst_file = rc_baton->dst;
592 if (!dst_file.GetFilename())
593 dst_file.GetFilename() = src.GetFilename();
594
595 FileSpec src_resolved;
596
597 rc_baton->error = FileSystem::Readlink(src, src_resolved);
598
599 if (rc_baton->error.Fail())
600 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
601
602 rc_baton->error =
603 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
604
605 if (rc_baton->error.Fail())
606 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
607
608 return FileSpec::eEnumerateDirectoryResultNext;
609 } break;
610
Zachary Turner7d86ee52017-03-08 17:56:08 +0000611 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 // copy the file and keep going
613 FileSpec dst_file = rc_baton->dst;
614 if (!dst_file.GetFilename())
615 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000616 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617 if (err.Fail()) {
618 rc_baton->error.SetErrorString(err.AsCString());
619 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
620 }
621 return FileSpec::eEnumerateDirectoryResultNext;
622 } break;
623
Zachary Turner7d86ee52017-03-08 17:56:08 +0000624 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 rc_baton->error.SetErrorStringWithFormat(
626 "invalid file detected during copy: %s", src.GetPath().c_str());
627 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
628 break;
629 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000630 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000631}
632
Zachary Turner97206d52017-05-12 04:51:55 +0000633Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
634 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635
636 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
637 if (log)
638 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
639 dst.GetPath().c_str());
640 FileSpec fixed_dst(dst);
641
642 if (!fixed_dst.GetFilename())
643 fixed_dst.GetFilename() = src.GetFilename();
644
645 FileSpec working_dir = GetWorkingDirectory();
646
647 if (dst) {
648 if (dst.GetDirectory()) {
649 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
650 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
651 fixed_dst.GetDirectory() = dst.GetDirectory();
652 }
653 // If the fixed destination file doesn't have a directory yet,
654 // then we must have a relative path. We will resolve this relative
655 // path against the platform's working directory
656 if (!fixed_dst.GetDirectory()) {
657 FileSpec relative_spec;
658 std::string path;
659 if (working_dir) {
660 relative_spec = working_dir;
661 relative_spec.AppendPathComponent(dst.GetPath());
662 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
663 } else {
664 error.SetErrorStringWithFormat(
665 "platform working directory must be valid for relative path '%s'",
666 dst.GetPath().c_str());
667 return error;
668 }
669 }
670 } else {
671 if (working_dir) {
672 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
673 } else {
674 error.SetErrorStringWithFormat(
675 "platform working directory must be valid for relative path '%s'",
676 dst.GetPath().c_str());
677 return error;
678 }
679 }
680 } else {
681 if (working_dir) {
682 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
683 } else {
684 error.SetErrorStringWithFormat("platform working directory must be valid "
685 "when destination directory is empty");
686 return error;
687 }
688 }
689
690 if (log)
691 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
692 src.GetPath().c_str(), dst.GetPath().c_str(),
693 fixed_dst.GetPath().c_str());
694
695 if (GetSupportsRSync()) {
696 error = PutFile(src, dst);
697 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000698 namespace fs = llvm::sys::fs;
699 switch (fs::get_file_type(src.GetPath(), false)) {
700 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000701 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702 uint32_t permissions = src.GetPermissions();
703 if (permissions == 0)
704 permissions = eFilePermissionsDirectoryDefault;
705 error = MakeDirectory(fixed_dst, permissions);
706 if (error.Success()) {
707 // Make a filespec that only fills in the directory of a FileSpec so
708 // when we enumerate we can quickly fill in the filename for dst copies
709 FileSpec recurse_dst;
710 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
711 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000712 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000713 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714 RecurseCopy_Callback, &baton);
715 return baton.error;
716 }
717 } break;
718
Zachary Turner7d86ee52017-03-08 17:56:08 +0000719 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000720 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000721 error = PutFile(src, fixed_dst);
722 break;
723
Zachary Turner7d86ee52017-03-08 17:56:08 +0000724 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000725 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 FileSpec src_resolved;
727 error = FileSystem::Readlink(src, src_resolved);
728 if (error.Success())
729 error = CreateSymlink(dst, src_resolved);
730 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000731 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 error.SetErrorString("platform install doesn't handle pipes");
733 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000734 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 error.SetErrorString("platform install doesn't handle sockets");
736 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000737 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 error.SetErrorString(
739 "platform install doesn't handle non file or directory items");
740 break;
741 }
742 }
743 return error;
744}
745
746bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
747 if (IsHost()) {
748 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000749 LLDB_LOG(log, "{0}", file_spec);
750 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
751 LLDB_LOG(log, "error: {0}", ec.message());
752 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000754 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 } else {
756 m_working_dir.Clear();
757 return SetRemoteWorkingDirectory(file_spec);
758 }
759}
760
Zachary Turner97206d52017-05-12 04:51:55 +0000761Status Platform::MakeDirectory(const FileSpec &file_spec,
762 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000764 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000766 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
768 GetPluginName().GetCString(),
769 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000770 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000772}
773
Zachary Turner97206d52017-05-12 04:51:55 +0000774Status Platform::GetFilePermissions(const FileSpec &file_spec,
775 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000776 if (IsHost()) {
777 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
778 if (Value)
779 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000780 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000781 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000782 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
784 GetPluginName().GetCString(),
785 LLVM_PRETTY_FUNCTION);
786 return error;
787 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000788}
789
Zachary Turner97206d52017-05-12 04:51:55 +0000790Status Platform::SetFilePermissions(const FileSpec &file_spec,
791 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000792 if (IsHost()) {
793 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
794 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
795 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000796 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
798 GetPluginName().GetCString(),
799 LLVM_PRETTY_FUNCTION);
800 return error;
801 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000802}
803
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804ConstString Platform::GetName() { return GetPluginName(); }
805
806const char *Platform::GetHostname() {
807 if (IsHost())
808 return "127.0.0.1";
809
810 if (m_name.empty())
811 return nullptr;
812 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000813}
814
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815ConstString Platform::GetFullNameForDylib(ConstString basename) {
816 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000817}
818
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
820 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
821 if (log)
822 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000823 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 m_working_dir = working_dir;
825 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000826}
827
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828const char *Platform::GetUserName(uint32_t uid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000829#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 const char *user_name = GetCachedUserName(uid);
831 if (user_name)
832 return user_name;
833 if (IsHost()) {
834 std::string name;
835 if (HostInfo::LookupUserName(uid, name))
836 return SetCachedUserName(uid, name.c_str(), name.size());
837 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000838#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 return nullptr;
Greg Clayton1cb64962011-03-24 04:28:38 +0000840}
841
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842const char *Platform::GetGroupName(uint32_t gid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000843#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 const char *group_name = GetCachedGroupName(gid);
845 if (group_name)
846 return group_name;
847 if (IsHost()) {
848 std::string name;
849 if (HostInfo::LookupGroupName(gid, name))
850 return SetCachedGroupName(gid, name.c_str(), name.size());
851 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000852#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 return nullptr;
Greg Clayton32e0a752011-03-30 18:16:51 +0000854}
Greg Clayton1cb64962011-03-24 04:28:38 +0000855
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856bool Platform::SetOSVersion(uint32_t major, uint32_t minor, uint32_t update) {
857 if (IsHost()) {
858 // We don't need anyone setting the OS version for the host platform,
859 // we should be able to figure it out by calling
860 // HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000861 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 } else {
863 // We have a remote platform, allow setting the target OS version if
864 // we aren't connected, since if we are connected, we should be able to
865 // request the remote OS version from the connected platform.
866 if (IsConnected())
867 return false;
868 else {
869 // We aren't connected and we might want to set the OS version
870 // ahead of time before we connect so we can peruse files and
871 // use a local SDK or PDK cache of support files to disassemble
872 // or do other things.
873 m_major_os_version = major;
874 m_minor_os_version = minor;
875 m_update_os_version = update;
876 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000877 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 }
879 return false;
880}
881
Zachary Turner97206d52017-05-12 04:51:55 +0000882Status
883Platform::ResolveExecutable(const ModuleSpec &module_spec,
884 lldb::ModuleSP &exe_module_sp,
885 const FileSpecList *module_search_paths_ptr) {
886 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887 if (module_spec.GetFileSpec().Exists()) {
888 if (module_spec.GetArchitecture().IsValid()) {
889 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
890 module_search_paths_ptr, nullptr,
891 nullptr);
892 } else {
893 // No valid architecture was specified, ask the platform for
894 // the architectures that we should be using (in the correct order)
895 // and see if we can find a match that way
896 ModuleSpec arch_module_spec(module_spec);
897 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
898 idx, arch_module_spec.GetArchitecture());
899 ++idx) {
900 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
901 module_search_paths_ptr, nullptr,
902 nullptr);
903 // Did we find an executable using one of the
904 if (error.Success() && exe_module_sp)
905 break;
906 }
Greg Claytone996fd32011-03-08 22:40:15 +0000907 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 } else {
909 error.SetErrorStringWithFormat("'%s' does not exist",
910 module_spec.GetFileSpec().GetPath().c_str());
911 }
912 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000913}
914
Zachary Turner97206d52017-05-12 04:51:55 +0000915Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
916 FileSpec &sym_file) {
917 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 if (sym_spec.GetSymbolFileSpec().Exists())
919 sym_file = sym_spec.GetSymbolFileSpec();
920 else
921 error.SetErrorString("unable to resolve symbol file");
922 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000923}
924
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925bool Platform::ResolveRemotePath(const FileSpec &platform_path,
926 FileSpec &resolved_platform_path) {
927 resolved_platform_path = platform_path;
928 return resolved_platform_path.ResolvePath();
929}
930
931const ArchSpec &Platform::GetSystemArchitecture() {
932 if (IsHost()) {
933 if (!m_system_arch.IsValid()) {
934 // We have a local host platform
935 m_system_arch = HostInfo::GetArchitecture();
936 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000937 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938 } else {
939 // We have a remote platform. We can only fetch the remote
940 // system architecture if we are connected, and we don't want to do it
941 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000942
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000944
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945 bool fetch = false;
946 if (m_system_arch.IsValid()) {
947 // We have valid OS version info, check to make sure it wasn't
948 // manually set prior to connecting. If it was manually set prior
949 // to connecting, then lets fetch the actual OS version info
950 // if we are now connected.
951 if (is_connected && !m_system_arch_set_while_connected)
952 fetch = true;
953 } else {
954 // We don't have valid OS version info, fetch it if we are connected
955 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000956 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957
958 if (fetch) {
959 m_system_arch = GetRemoteSystemArchitecture();
960 m_system_arch_set_while_connected = m_system_arch.IsValid();
961 }
962 }
963 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000964}
965
Zachary Turner97206d52017-05-12 04:51:55 +0000966Status Platform::ConnectRemote(Args &args) {
967 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 if (IsHost())
969 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
970 "the host platform and is always connected.",
971 GetPluginName().GetCString());
972 else
973 error.SetErrorStringWithFormat(
974 "Platform::ConnectRemote() is not supported by %s",
975 GetPluginName().GetCString());
976 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000977}
978
Zachary Turner97206d52017-05-12 04:51:55 +0000979Status Platform::DisconnectRemote() {
980 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 if (IsHost())
982 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
983 "the host platform and is always connected.",
984 GetPluginName().GetCString());
985 else
986 error.SetErrorStringWithFormat(
987 "Platform::DisconnectRemote() is not supported by %s",
988 GetPluginName().GetCString());
989 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000990}
Greg Clayton32e0a752011-03-30 18:16:51 +0000991
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992bool Platform::GetProcessInfo(lldb::pid_t pid,
993 ProcessInstanceInfo &process_info) {
994 // Take care of the host case so that each subclass can just
995 // call this function to get the host functionality.
996 if (IsHost())
997 return Host::GetProcessInfo(pid, process_info);
998 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000999}
1000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1002 ProcessInstanceInfoList &process_infos) {
1003 // Take care of the host case so that each subclass can just
1004 // call this function to get the host functionality.
1005 uint32_t match_count = 0;
1006 if (IsHost())
1007 match_count = Host::FindProcesses(match_info, process_infos);
1008 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001009}
Greg Clayton8b82f082011-04-12 05:54:46 +00001010
Zachary Turner97206d52017-05-12 04:51:55 +00001011Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1012 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1014 if (log)
1015 log->Printf("Platform::%s()", __FUNCTION__);
1016
1017 // Take care of the host case so that each subclass can just
1018 // call this function to get the host functionality.
1019 if (IsHost()) {
1020 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1021 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1022
1023 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1024 const bool is_localhost = true;
1025 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1026 const bool first_arg_is_full_shell_command = false;
1027 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1028 if (log) {
1029 const FileSpec &shell = launch_info.GetShell();
1030 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
1031 log->Printf(
1032 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1033 ", shell is '%s'",
1034 __FUNCTION__, num_resumes, shell_str);
1035 }
1036
1037 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1038 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1039 num_resumes))
1040 return error;
1041 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1042 error = ShellExpandArguments(launch_info);
1043 if (error.Fail()) {
1044 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1045 "consider launching with 'process "
1046 "launch'.",
1047 error.AsCString("unknown"));
1048 return error;
1049 }
1050 }
1051
Todd Fialaac33cc92014-10-09 01:02:08 +00001052 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1054 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 error = Host::LaunchProcess(launch_info);
1057 } else
1058 error.SetErrorString(
1059 "base lldb_private::Platform class can't launch remote processes");
1060 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001061}
1062
Zachary Turner97206d52017-05-12 04:51:55 +00001063Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 if (IsHost())
1065 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001066 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001067}
1068
Zachary Turner97206d52017-05-12 04:51:55 +00001069Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1071 if (log)
1072 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001073
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074 // Try to find a process plugin to handle this Kill request. If we can't,
1075 // fall back to
1076 // the default OS implementation.
1077 size_t num_debuggers = Debugger::GetNumDebuggers();
1078 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1079 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1080 lldb_private::TargetList &targets = debugger->GetTargetList();
1081 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1082 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1083 if (process->GetID() == pid)
1084 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001087
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001089 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 "base lldb_private::Platform class can't kill remote processes unless "
1091 "they are controlled by a process plugin");
1092 }
1093 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001094 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001095}
1096
Greg Clayton8b82f082011-04-12 05:54:46 +00001097lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1099 Target *target, // Can be nullptr, if nullptr create a
1100 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001101 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1103 if (log)
1104 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1105 static_cast<void *>(target));
1106
1107 ProcessSP process_sp;
1108 // Make sure we stop at the entry point
1109 launch_info.GetFlags().Set(eLaunchFlagDebug);
1110 // We always launch the process we are going to debug in a separate process
1111 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1112 // about the target getting them as well.
1113 launch_info.SetLaunchInSeparateProcessGroup(true);
1114
1115 // Allow any StructuredData process-bound plugins to adjust the launch info
1116 // if needed
1117 size_t i = 0;
1118 bool iteration_complete = false;
1119 // Note iteration can't simply go until a nullptr callback is returned, as
1120 // it is valid for a plugin to not supply a filter.
1121 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1122 for (auto filter_callback = get_filter_func(i, iteration_complete);
1123 !iteration_complete;
1124 filter_callback = get_filter_func(++i, iteration_complete)) {
1125 if (filter_callback) {
1126 // Give this ProcessLaunchInfo filter a chance to adjust the launch
1127 // info.
1128 error = (*filter_callback)(launch_info, target);
1129 if (!error.Success()) {
1130 if (log)
1131 log->Printf("Platform::%s() StructuredDataPlugin launch "
1132 "filter failed.",
1133 __FUNCTION__);
1134 return process_sp;
1135 }
1136 }
1137 }
1138
1139 error = LaunchProcess(launch_info);
1140 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001141 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1143 ")",
1144 __FUNCTION__, launch_info.GetProcessID());
1145 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1146 ProcessAttachInfo attach_info(launch_info);
1147 process_sp = Attach(attach_info, debugger, target, error);
1148 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001149 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1151 __FUNCTION__, process_sp->GetPluginName().AsCString());
1152 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001153
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 // Since we attached to the process, it will think it needs to detach
1155 // if the process object just goes away without an explicit call to
1156 // Process::Kill() or Process::Detach(), so let it know to kill the
1157 // process if this happens.
1158 process_sp->SetShouldDetach(false);
1159
1160 // If we didn't have any file actions, the pseudo terminal might
1161 // have been used where the slave side was given as the file to
1162 // open for stdin/out/err after we have already opened the master
1163 // so we can read/write stdin/out/err.
1164 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1165 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) {
1166 process_sp->SetSTDIOFileDescriptor(pty_fd);
1167 }
1168 } else {
1169 if (log)
1170 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1171 error.AsCString());
1172 }
1173 } else {
1174 if (log)
1175 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1176 "invalid process id",
1177 __FUNCTION__);
1178 }
1179 } else {
1180 if (log)
1181 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1182 error.AsCString());
1183 }
1184
1185 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001186}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001187
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001188lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001189Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1190 ArchSpec *platform_arch_ptr) {
1191 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001192 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 if (arch.IsValid())
1194 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1195 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001196}
1197
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001198//------------------------------------------------------------------
1199/// Lets a platform answer if it is compatible with a given
1200/// architecture and the target triple contained within.
1201//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1203 bool exact_arch_match,
1204 ArchSpec *compatible_arch_ptr) {
1205 // If the architecture is invalid, we must answer true...
1206 if (arch.IsValid()) {
1207 ArchSpec platform_arch;
1208 // Try for an exact architecture match first.
1209 if (exact_arch_match) {
1210 for (uint32_t arch_idx = 0;
1211 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1212 ++arch_idx) {
1213 if (arch.IsExactMatch(platform_arch)) {
1214 if (compatible_arch_ptr)
1215 *compatible_arch_ptr = platform_arch;
1216 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 }
1219 } else {
1220 for (uint32_t arch_idx = 0;
1221 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1222 ++arch_idx) {
1223 if (arch.IsCompatibleMatch(platform_arch)) {
1224 if (compatible_arch_ptr)
1225 *compatible_arch_ptr = platform_arch;
1226 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001227 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001229 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 }
1231 if (compatible_arch_ptr)
1232 compatible_arch_ptr->Clear();
1233 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001234}
1235
Zachary Turner97206d52017-05-12 04:51:55 +00001236Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1237 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1239 if (log)
1240 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001241
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 uint32_t source_open_options =
1243 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001244 namespace fs = llvm::sys::fs;
1245 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001247
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
Zachary Turner97206d52017-05-12 04:51:55 +00001249 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 uint32_t permissions = source_file.GetPermissions(error);
1251 if (permissions == 0)
1252 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001253
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001255 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 lldb::user_id_t dest_file = OpenFile(
1257 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1258 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1259 permissions, error);
1260 if (log)
1261 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001262
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 if (error.Fail())
1264 return error;
1265 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001266 return Status("unable to open target file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1268 uint64_t offset = 0;
1269 for (;;) {
1270 size_t bytes_read = buffer_sp->GetByteSize();
1271 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1272 if (error.Fail() || bytes_read == 0)
1273 break;
1274
1275 const uint64_t bytes_written =
1276 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001277 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001279
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280 offset += bytes_written;
1281 if (bytes_written != bytes_read) {
1282 // We didn't write the correct number of bytes, so adjust
1283 // the file position in the source file we are reading from...
1284 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001285 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 }
1287 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001290 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291
1292 // TODO: ChownFile?
1293
1294 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001295}
1296
Zachary Turner97206d52017-05-12 04:51:55 +00001297Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1298 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001300}
1301
Zachary Turner97206d52017-05-12 04:51:55 +00001302Status
1303Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1304 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001305{
Zachary Turner97206d52017-05-12 04:51:55 +00001306 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001308}
1309
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1311 return false;
1312}
1313
Zachary Turner97206d52017-05-12 04:51:55 +00001314Status Platform::Unlink(const FileSpec &path) {
1315 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316 return error;
1317}
1318
1319uint64_t Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch,
1320 unsigned flags) {
1321 uint64_t flags_platform = 0;
1322 if (flags & eMmapFlagsPrivate)
1323 flags_platform |= MAP_PRIVATE;
1324 if (flags & eMmapFlagsAnon)
1325 flags_platform |= MAP_ANON;
1326 return flags_platform;
1327}
1328
Zachary Turner97206d52017-05-12 04:51:55 +00001329lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 const char *command, // Shouldn't be nullptr
1331 const FileSpec &
1332 working_dir, // Pass empty FileSpec to use the current working directory
1333 int *status_ptr, // Pass nullptr if you don't want the process exit status
1334 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1335 // process to exit
1336 std::string
1337 *command_output, // Pass nullptr if you don't want the command output
1338 uint32_t
1339 timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001340{
Kate Stoneb9c1b512016-09-06 20:57:50 +00001341 if (IsHost())
1342 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
1343 command_output, timeout_sec);
1344 else
Zachary Turner97206d52017-05-12 04:51:55 +00001345 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346}
1347
1348bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1349 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001350 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001351 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001352 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1353 if (!Result)
1354 return false;
1355 std::tie(high, low) = Result->words();
1356 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001357}
1358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359void Platform::SetLocalCacheDirectory(const char *local) {
1360 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001361}
1362
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363const char *Platform::GetLocalCacheDirectory() {
1364 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001365}
Greg Claytonfbb76342013-11-20 21:07:01 +00001366
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367static OptionDefinition g_rsync_option_table[] = {
1368 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
1369 nullptr, 0, eArgTypeNone, "Enable rsync."},
1370 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
1371 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1372 "Platform-specific options required for rsync to work."},
1373 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
1374 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1375 "Platform-specific rsync prefix put before the remote path."},
1376 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
1377 OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,
1378 "Do not automatically fill in the remote hostname when composing the "
1379 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001380};
1381
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382static OptionDefinition g_ssh_option_table[] = {
1383 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
1384 nullptr, 0, eArgTypeNone, "Enable SSH."},
1385 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
1386 nullptr, nullptr, 0, eArgTypeCommandName,
1387 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001388};
1389
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390static OptionDefinition g_caching_option_table[] = {
1391 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
1392 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePath,
1393 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001394};
1395
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001396llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001397 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001398}
1399
Kate Stoneb9c1b512016-09-06 20:57:50 +00001400void OptionGroupPlatformRSync::OptionParsingStarting(
1401 ExecutionContext *execution_context) {
1402 m_rsync = false;
1403 m_rsync_opts.clear();
1404 m_rsync_prefix.clear();
1405 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001406}
1407
Zachary Turner97206d52017-05-12 04:51:55 +00001408lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001409OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001410 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001412 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 char short_option = (char)GetDefinitions()[option_idx].short_option;
1414 switch (short_option) {
1415 case 'r':
1416 m_rsync = true;
1417 break;
1418
1419 case 'R':
1420 m_rsync_opts.assign(option_arg);
1421 break;
1422
1423 case 'P':
1424 m_rsync_prefix.assign(option_arg);
1425 break;
1426
1427 case 'i':
1428 m_ignores_remote_hostname = true;
1429 break;
1430
1431 default:
1432 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1433 break;
1434 }
1435
1436 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001437}
1438
Greg Clayton4116e932012-05-15 02:33:01 +00001439lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1441 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001442}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001443
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001444llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001445 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001446}
1447
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448void OptionGroupPlatformSSH::OptionParsingStarting(
1449 ExecutionContext *execution_context) {
1450 m_ssh = false;
1451 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001452}
1453
Zachary Turner97206d52017-05-12 04:51:55 +00001454lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001455OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001456 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001458 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 char short_option = (char)GetDefinitions()[option_idx].short_option;
1460 switch (short_option) {
1461 case 's':
1462 m_ssh = true;
1463 break;
1464
1465 case 'S':
1466 m_ssh_opts.assign(option_arg);
1467 break;
1468
1469 default:
1470 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1471 break;
1472 }
1473
1474 return error;
1475}
1476
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001477llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001478 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479}
1480
1481void OptionGroupPlatformCaching::OptionParsingStarting(
1482 ExecutionContext *execution_context) {
1483 m_cache_dir.clear();
1484}
1485
Zachary Turner97206d52017-05-12 04:51:55 +00001486lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001487 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001489 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 char short_option = (char)GetDefinitions()[option_idx].short_option;
1491 switch (short_option) {
1492 case 'c':
1493 m_cache_dir.assign(option_arg);
1494 break;
1495
1496 default:
1497 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1498 break;
1499 }
1500
1501 return error;
1502}
1503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504size_t Platform::GetEnvironment(StringList &environment) {
1505 environment.Clear();
1506 return false;
1507}
1508
1509const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1510 if (!m_calculated_trap_handlers) {
1511 std::lock_guard<std::mutex> guard(m_mutex);
1512 if (!m_calculated_trap_handlers) {
1513 CalculateTrapHandlerSymbolNames();
1514 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 }
1517 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001518}
1519
Zachary Turner97206d52017-05-12 04:51:55 +00001520Status Platform::GetCachedExecutable(
1521 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1522 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 const auto platform_spec = module_spec.GetFileSpec();
1524 const auto error = LoadCachedExecutable(
1525 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1526 if (error.Success()) {
1527 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1528 module_spec.GetPlatformFileSpec() = platform_spec;
1529 }
1530
1531 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001532}
1533
Zachary Turner97206d52017-05-12 04:51:55 +00001534Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1536 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1537 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1538 [&](const ModuleSpec &spec) {
1539 return remote_platform.ResolveExecutable(
1540 spec, module_sp, module_search_paths_ptr);
1541 },
1542 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001543}
1544
Zachary Turner97206d52017-05-12 04:51:55 +00001545Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1546 Process *process,
1547 lldb::ModuleSP &module_sp,
1548 const ModuleResolver &module_resolver,
1549 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 // Get module information from a target.
1551 ModuleSpec resolved_module_spec;
1552 bool got_module_spec = false;
1553 if (process) {
1554 // Try to get module information from the process
1555 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1556 module_spec.GetArchitecture(),
1557 resolved_module_spec)) {
1558 if (module_spec.GetUUID().IsValid() == false ||
1559 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1560 got_module_spec = true;
1561 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001562 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001564
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001565 if (module_spec.GetArchitecture().IsValid() == false) {
Zachary Turner97206d52017-05-12 04:51:55 +00001566 Status error;
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001567 // No valid architecture was specified, ask the platform for
1568 // the architectures that we should be using (in the correct order)
1569 // and see if we can find a match that way
1570 ModuleSpec arch_module_spec(module_spec);
1571 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1572 idx, arch_module_spec.GetArchitecture());
1573 ++idx) {
1574 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1575 nullptr, nullptr);
1576 // Did we find an executable using one of the
1577 if (error.Success() && module_sp)
1578 break;
1579 }
1580 if (module_sp)
1581 got_module_spec = true;
1582 }
1583
Kate Stoneb9c1b512016-09-06 20:57:50 +00001584 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001585 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1587 resolved_module_spec)) {
1588 if (module_spec.GetUUID().IsValid() == false ||
1589 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1590 return module_resolver(module_spec);
1591 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001592 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001594
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595 // If we are looking for a specific UUID, make sure resolved_module_spec has
1596 // the same one before we search.
1597 if (module_spec.GetUUID().IsValid()) {
1598 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1599 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001600
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601 // Trying to find a module by UUID on local file system.
1602 const auto error = module_resolver(resolved_module_spec);
1603 if (error.Fail()) {
1604 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001605 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001607
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001609}
1610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1612 lldb::ModuleSP &module_sp,
1613 bool *did_create_ptr) {
1614 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1615 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001616 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617
1618 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1619
1620 // Check local cache for a module.
1621 auto error = m_module_cache->GetAndPut(
1622 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1623 [this](const ModuleSpec &module_spec,
1624 const FileSpec &tmp_download_file_spec) {
1625 return DownloadModuleSlice(
1626 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1627 module_spec.GetObjectSize(), tmp_download_file_spec);
1628
1629 },
1630 [this](const ModuleSP &module_sp,
1631 const FileSpec &tmp_download_file_spec) {
1632 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1633 },
1634 module_sp, did_create_ptr);
1635 if (error.Success())
1636 return true;
1637
1638 if (log)
1639 log->Printf("Platform::%s - module %s not found in local cache: %s",
1640 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1641 error.AsCString());
1642 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001643}
1644
Zachary Turner97206d52017-05-12 04:51:55 +00001645Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1646 const uint64_t src_offset,
1647 const uint64_t src_size,
1648 const FileSpec &dst_file_spec) {
1649 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001650
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001651 std::error_code EC;
1652 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1653 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 error.SetErrorStringWithFormat("unable to open destination file: %s",
1655 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001656 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001658
Kate Stoneb9c1b512016-09-06 20:57:50 +00001659 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1660 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001661
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662 if (error.Fail()) {
1663 error.SetErrorStringWithFormat("unable to open source file: %s",
1664 error.AsCString());
1665 return error;
1666 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001667
Kate Stoneb9c1b512016-09-06 20:57:50 +00001668 std::vector<char> buffer(1024);
1669 auto offset = src_offset;
1670 uint64_t total_bytes_read = 0;
1671 while (total_bytes_read < src_size) {
1672 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1673 src_size - total_bytes_read);
1674 const uint64_t n_read =
1675 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001676 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677 break;
1678 if (n_read == 0) {
1679 error.SetErrorString("read 0 bytes");
1680 break;
1681 }
1682 offset += n_read;
1683 total_bytes_read += n_read;
1684 dst.write(&buffer[0], n_read);
1685 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001686
Zachary Turner97206d52017-05-12 04:51:55 +00001687 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001688 CloseFile(src_fd, close_error); // Ignoring close error.
1689
1690 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001691}
1692
Zachary Turner97206d52017-05-12 04:51:55 +00001693Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1694 const FileSpec &dst_file_spec) {
1695 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001697}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001698
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699FileSpec Platform::GetModuleCacheRoot() {
1700 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1701 dir_spec.AppendPathComponent(GetName().AsCString());
1702 return dir_spec;
1703}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001704
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001706
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1708 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1709 return s_default_unix_signals_sp;
1710}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001711
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712const UnixSignalsSP &Platform::GetUnixSignals() {
1713 if (IsHost())
1714 return Host::GetUnixSignals();
1715 return GetRemoteUnixSignals();
1716}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001717
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718uint32_t Platform::LoadImage(lldb_private::Process *process,
1719 const lldb_private::FileSpec &local_file,
1720 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001721 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001722 if (local_file && remote_file) {
1723 // Both local and remote file was specified. Install the local file to the
1724 // given location.
1725 if (IsRemote() || local_file != remote_file) {
1726 error = Install(local_file, remote_file);
1727 if (error.Fail())
1728 return LLDB_INVALID_IMAGE_TOKEN;
1729 }
1730 return DoLoadImage(process, remote_file, error);
1731 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001732
Kate Stoneb9c1b512016-09-06 20:57:50 +00001733 if (local_file) {
1734 // Only local file was specified. Install it to the current working
1735 // directory.
1736 FileSpec target_file = GetWorkingDirectory();
1737 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1738 if (IsRemote() || local_file != target_file) {
1739 error = Install(local_file, target_file);
1740 if (error.Fail())
1741 return LLDB_INVALID_IMAGE_TOKEN;
1742 }
1743 return DoLoadImage(process, target_file, error);
1744 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001745
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 if (remote_file) {
1747 // Only remote file was specified so we don't have to do any copying
1748 return DoLoadImage(process, remote_file, error);
1749 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001750
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 error.SetErrorString("Neither local nor remote file was specified");
1752 return LLDB_INVALID_IMAGE_TOKEN;
1753}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001754
Kate Stoneb9c1b512016-09-06 20:57:50 +00001755uint32_t Platform::DoLoadImage(lldb_private::Process *process,
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 error.SetErrorString("LoadImage is not supported on the current platform");
1759 return LLDB_INVALID_IMAGE_TOKEN;
1760}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001761
Zachary Turner97206d52017-05-12 04:51:55 +00001762Status Platform::UnloadImage(lldb_private::Process *process,
1763 uint32_t image_token) {
1764 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001765}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001766
Zachary Turner31659452016-11-17 21:15:14 +00001767lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1768 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769 lldb_private::Debugger &debugger,
1770 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001771 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001773
Kate Stoneb9c1b512016-09-06 20:57:50 +00001774 if (!target) {
1775 TargetSP new_target_sp;
Zachary Turnera47464b2016-11-18 20:44:46 +00001776 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
1777 nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001778 target = new_target_sp.get();
1779 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001780
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 if (!target || error.Fail())
1782 return nullptr;
1783
1784 debugger.GetTargetList().SetSelectedTarget(target);
1785
1786 lldb::ProcessSP process_sp =
1787 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1788 if (!process_sp)
1789 return nullptr;
1790
1791 error =
1792 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1793 if (error.Fail())
1794 return nullptr;
1795
1796 return process_sp;
1797}
1798
1799size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001800 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801 error.Clear();
1802 return 0;
1803}
1804
1805size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1806 BreakpointSite *bp_site) {
1807 ArchSpec arch = target.GetArchitecture();
1808 const uint8_t *trap_opcode = nullptr;
1809 size_t trap_opcode_size = 0;
1810
1811 switch (arch.GetMachine()) {
1812 case llvm::Triple::aarch64: {
1813 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1814 trap_opcode = g_aarch64_opcode;
1815 trap_opcode_size = sizeof(g_aarch64_opcode);
1816 } break;
1817
1818 // TODO: support big-endian arm and thumb trap codes.
1819 case llvm::Triple::arm: {
1820 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
1821 // but the linux kernel does otherwise.
1822 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1823 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1824
1825 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
1826 AddressClass addr_class = eAddressClassUnknown;
1827
1828 if (bp_loc_sp) {
1829 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
1830 if (addr_class == eAddressClassUnknown &&
1831 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
1832 addr_class = eAddressClassCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001833 }
1834
Kate Stoneb9c1b512016-09-06 20:57:50 +00001835 if (addr_class == eAddressClassCodeAlternateISA) {
1836 trap_opcode = g_thumb_breakpoint_opcode;
1837 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1838 } else {
1839 trap_opcode = g_arm_breakpoint_opcode;
1840 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1841 }
1842 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001843
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844 case llvm::Triple::mips:
1845 case llvm::Triple::mips64: {
1846 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1847 trap_opcode = g_hex_opcode;
1848 trap_opcode_size = sizeof(g_hex_opcode);
1849 } break;
1850
1851 case llvm::Triple::mipsel:
1852 case llvm::Triple::mips64el: {
1853 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1854 trap_opcode = g_hex_opcode;
1855 trap_opcode_size = sizeof(g_hex_opcode);
1856 } break;
1857
1858 case llvm::Triple::systemz: {
1859 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1860 trap_opcode = g_hex_opcode;
1861 trap_opcode_size = sizeof(g_hex_opcode);
1862 } break;
1863
1864 case llvm::Triple::hexagon: {
1865 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1866 trap_opcode = g_hex_opcode;
1867 trap_opcode_size = sizeof(g_hex_opcode);
1868 } break;
1869
1870 case llvm::Triple::ppc:
1871 case llvm::Triple::ppc64: {
1872 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1873 trap_opcode = g_ppc_opcode;
1874 trap_opcode_size = sizeof(g_ppc_opcode);
1875 } break;
1876
1877 case llvm::Triple::x86:
1878 case llvm::Triple::x86_64: {
1879 static const uint8_t g_i386_opcode[] = {0xCC};
1880 trap_opcode = g_i386_opcode;
1881 trap_opcode_size = sizeof(g_i386_opcode);
1882 } break;
1883
1884 default:
David Blaikiea322f362017-01-06 00:38:06 +00001885 llvm_unreachable(
1886 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 }
1888
1889 assert(bp_site);
1890 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1891 return trap_opcode_size;
1892
1893 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001894}