blob: 61484834b51d0127aa28341c152ddbc876409f36 [file] [log] [blame]
Greg Claytone996fd32011-03-08 22:40:15 +00001//===-- Platform.cpp --------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Greg Claytone996fd32011-03-08 22:40:15 +00006//
7//===----------------------------------------------------------------------===//
8
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00009#include <algorithm>
Pavel Labathbc4987b2018-04-05 16:59:36 +000010#include <csignal>
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000011#include <fstream>
12#include <vector>
13
Eugene Zelenko9394d7722016-02-18 00:10:17 +000014#include "llvm/Support/FileSystem.h"
15#include "llvm/Support/Path.h"
16
Greg Clayton4116e932012-05-15 02:33:01 +000017#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000018#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000019#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000020#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000022#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000023#include "lldb/Core/StreamFile.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000024#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000025#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000026#include "lldb/Host/HostInfo.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000027#include "lldb/Host/OptionParser.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000028#include "lldb/Interpreter/OptionValueProperties.h"
29#include "lldb/Interpreter/Property.h"
30#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000031#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000033#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000034#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000035#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000036#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000037#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000038#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000039#include "lldb/Utility/Status.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000040#include "lldb/Utility/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000041
Zachary Turner7d86ee52017-03-08 17:56:08 +000042#include "llvm/Support/FileSystem.h"
43
Adrian Prantl05097242018-04-30 16:49:04 +000044// Define these constants from POSIX mman.h rather than include the file so
45// that they will be correct even when compiled on Linux.
Robert Flack96ad3de2015-05-09 15:53:31 +000046#define MAP_PRIVATE 2
47#define MAP_ANON 0x1000
48
Greg Claytone996fd32011-03-08 22:40:15 +000049using namespace lldb;
50using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000051
52static uint32_t g_initialize_count = 0;
53
Adrian Prantl05097242018-04-30 16:49:04 +000054// Use a singleton function for g_local_platform_sp to avoid init constructors
55// since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000056static PlatformSP &GetHostPlatformSP() {
57 static PlatformSP g_platform_sp;
58 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000062
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000063namespace {
64
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000065static constexpr PropertyDefinition g_properties[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000067 {}, "Use module cache."},
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
Tatyana Krasnukhae40db052018-09-27 07:11:58 +000069 {}, "Root directory for cached modules."}};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071enum { ePropertyUseModuleCache, ePropertyModuleCacheDirectory };
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075ConstString PlatformProperties::GetSettingName() {
76 static ConstString g_setting_name("platform");
77 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080PlatformProperties::PlatformProperties() {
81 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
82 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 auto module_cache_dir = GetModuleCacheDirectory();
85 if (module_cache_dir)
86 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 llvm::SmallString<64> user_home_dir;
89 if (!llvm::sys::path::home_directory(user_home_dir))
90 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000091
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000092 module_cache_dir = FileSpec(user_home_dir.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 module_cache_dir.AppendPathComponent(".lldb");
94 module_cache_dir.AppendPathComponent("module_cache");
95 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098bool PlatformProperties::GetUseModuleCache() const {
99 const auto idx = ePropertyUseModuleCache;
100 return m_collection_sp->GetPropertyAtIndexAsBoolean(
101 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
105 return m_collection_sp->SetPropertyAtIndexAsBoolean(
106 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109FileSpec PlatformProperties::GetModuleCacheDirectory() const {
110 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
111 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
115 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
116 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000117}
118
Greg Claytone996fd32011-03-08 22:40:15 +0000119//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000121///
122/// There should only be one of these for each host that LLDB runs
123/// upon that should be statically compiled in and registered using
124/// preprocessor macros or other similar build mechanisms.
125///
126/// This platform will be used as the default platform when launching
127/// or attaching to processes unless another platform is specified.
128//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
130
131static std::vector<PlatformSP> &GetPlatformList() {
132 static std::vector<PlatformSP> g_platform_list;
133 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000134}
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136static std::recursive_mutex &GetPlatformListMutex() {
137 static std::recursive_mutex g_mutex;
138 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143void Platform::Terminate() {
144 if (g_initialize_count > 0) {
145 if (--g_initialize_count == 0) {
146 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
147 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000148 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000150}
151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
153 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
154 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000155}
156
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
158 // The native platform should use its static void Platform::Initialize()
159 // function to register itself as the native platform.
160 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 if (platform_sp) {
163 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
164 GetPlatformList().push_back(platform_sp);
165 }
Greg Claytone996fd32011-03-08 22:40:15 +0000166}
167
Zachary Turner97206d52017-05-12 04:51:55 +0000168Status Platform::GetFileWithUUID(const FileSpec &platform_file,
169 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 // Default to the local case
171 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000172 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000173}
174
Greg Clayton91c0e742013-01-11 23:44:27 +0000175FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176Platform::LocateExecutableScriptingResources(Target *target, Module &module,
177 Stream *feedback_stream) {
178 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181// PlatformSP
182// Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000183//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000184// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000185// if (plugin_name)
186// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187// create_callback =
188// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000189// if (create_callback)
190// {
191// ArchSpec arch;
192// if (process)
193// {
194// arch = process->GetTarget().GetArchitecture();
195// }
196// PlatformSP platform_sp(create_callback(process, &arch));
197// if (platform_sp)
198// return platform_sp;
199// }
200// }
201// else
202// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203// for (uint32_t idx = 0; (create_callback =
204// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
205// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000206// {
207// PlatformSP platform_sp(create_callback(process, nullptr));
208// if (platform_sp)
209// return platform_sp;
210// }
211// }
212// return PlatformSP();
213//}
Jason Molenda1c627542013-04-05 01:03:25 +0000214
Zachary Turner97206d52017-05-12 04:51:55 +0000215Status Platform::GetSharedModule(const ModuleSpec &module_spec,
216 Process *process, ModuleSP &module_sp,
217 const FileSpecList *module_search_paths_ptr,
218 ModuleSP *old_module_sp_ptr,
219 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 if (IsHost())
221 return ModuleList::GetSharedModule(
222 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
223 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000224
Pavel Labath1f8639a2018-08-28 16:32:46 +0000225 // Module resolver lambda.
226 auto resolver = [&](const ModuleSpec &spec) {
227 Status error(eErrorTypeGeneric);
228 ModuleSpec resolved_spec;
229 // Check if we have sysroot set.
230 if (m_sdk_sysroot) {
231 // Prepend sysroot to module spec.
232 resolved_spec = spec;
233 resolved_spec.GetFileSpec().PrependPathComponent(
234 m_sdk_sysroot.GetStringRef());
235 // Try to get shared module with resolved spec.
236 error = ModuleList::GetSharedModule(
237 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
238 did_create_ptr, false);
239 }
240 // If we don't have sysroot or it didn't work then
241 // try original module spec.
242 if (!error.Success()) {
243 resolved_spec = spec;
244 error = ModuleList::GetSharedModule(
245 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
246 did_create_ptr, false);
247 }
248 if (error.Success() && module_sp)
249 module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
250 return error;
251 };
252
253 return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000255}
256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
258 const ArchSpec &arch, ModuleSpec &module_spec) {
259 ModuleSpecList module_specs;
260 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
261 module_specs) == 0)
262 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 ModuleSpec matched_module_spec;
265 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
266 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269PlatformSP Platform::Find(const ConstString &name) {
270 if (name) {
271 static ConstString g_host_platform_name("host");
272 if (name == g_host_platform_name)
273 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
276 for (const auto &platform_sp : GetPlatformList()) {
277 if (platform_sp->GetName() == name)
278 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000279 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 }
281 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000282}
283
Zachary Turner97206d52017-05-12 04:51:55 +0000284PlatformSP Platform::Create(const ConstString &name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 PlatformCreateInstance create_callback = nullptr;
286 lldb::PlatformSP platform_sp;
287 if (name) {
288 static ConstString g_host_platform_name("host");
289 if (name == g_host_platform_name)
290 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 create_callback =
293 PluginManager::GetPlatformCreateCallbackForPluginName(name);
294 if (create_callback)
295 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000296 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 error.SetErrorStringWithFormat(
298 "unable to find a plug-in for the platform named \"%s\"",
299 name.GetCString());
300 } else
301 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000302
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 if (platform_sp) {
304 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
305 GetPlatformList().push_back(platform_sp);
306 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000309}
310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000312 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 lldb::PlatformSP platform_sp;
314 if (arch.IsValid()) {
315 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000316 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 // First try exact arch matches across all platforms already created
318 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
319 for (const auto &platform_sp : GetPlatformList()) {
320 if (platform_sp->IsCompatibleArchitecture(arch, true,
321 platform_arch_ptr))
322 return platform_sp;
323 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 // Next try compatible arch matches across all platforms already created
326 for (const auto &platform_sp : GetPlatformList()) {
327 if (platform_sp->IsCompatibleArchitecture(arch, false,
328 platform_arch_ptr))
329 return platform_sp;
330 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000331 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332
333 PlatformCreateInstance create_callback;
334 // First try exact arch matches across all platform plug-ins
335 uint32_t idx;
336 for (idx = 0; (create_callback =
337 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
338 ++idx) {
339 if (create_callback) {
340 platform_sp = create_callback(false, &arch);
341 if (platform_sp &&
342 platform_sp->IsCompatibleArchitecture(arch, true,
343 platform_arch_ptr)) {
344 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
345 GetPlatformList().push_back(platform_sp);
346 return platform_sp;
347 }
348 }
349 }
350 // Next try compatible arch matches across all platform plug-ins
351 for (idx = 0; (create_callback =
352 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
353 ++idx) {
354 if (create_callback) {
355 platform_sp = create_callback(false, &arch);
356 if (platform_sp &&
357 platform_sp->IsCompatibleArchitecture(arch, false,
358 platform_arch_ptr)) {
359 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
360 GetPlatformList().push_back(platform_sp);
361 return platform_sp;
362 }
363 }
364 }
365 } else
366 error.SetErrorString("invalid platform name");
367 if (platform_arch_ptr)
368 platform_arch_ptr->Clear();
369 platform_sp.reset();
370 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000371}
372
Pavel Labath7263f1b2017-10-31 10:56:03 +0000373ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
374 if (platform)
375 return platform->GetAugmentedArchSpec(triple);
376 return HostInfo::GetAugmentedArchSpec(triple);
377}
378
Greg Claytone996fd32011-03-08 22:40:15 +0000379//------------------------------------------------------------------
380/// Default Constructor
381//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000382Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 : m_is_host(is_host), m_os_version_set_while_connected(false),
384 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000385 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
386 m_uid_map(), m_gid_map(), m_max_uid_name_len(0), m_max_gid_name_len(0),
387 m_supports_rsync(false), m_rsync_opts(), m_rsync_prefix(),
388 m_supports_ssh(false), m_ssh_opts(), m_ignores_remote_hostname(false),
389 m_trap_handlers(), m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 m_module_cache(llvm::make_unique<ModuleCache>()) {
391 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
392 if (log)
393 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000394}
395
396//------------------------------------------------------------------
397/// Destructor.
398///
399/// The destructor is virtual since this class is designed to be
400/// inherited from by the plug-in instance.
401//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402Platform::~Platform() {
403 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
404 if (log)
405 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000406}
407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 std::string s;
410 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 ArchSpec arch(GetSystemArchitecture());
413 if (arch.IsValid()) {
414 if (!arch.GetTriple().str().empty()) {
415 strm.Printf(" Triple: ");
416 arch.DumpTriple(strm);
417 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000418 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000420
Pavel Labath2272c482018-06-18 15:02:23 +0000421 llvm::VersionTuple os_version = GetOSVersion();
422 if (!os_version.empty()) {
423 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 if (GetOSBuildString(s))
426 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 strm.EOL();
429 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 if (GetOSKernelDescription(s))
432 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 if (IsHost()) {
435 strm.Printf(" Hostname: %s\n", GetHostname());
436 } else {
437 const bool is_connected = IsConnected();
438 if (is_connected)
439 strm.Printf(" Hostname: %s\n", GetHostname());
440 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
441 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 if (GetWorkingDirectory()) {
444 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
445 }
446 if (!IsConnected())
447 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 std::string specific_info(GetPlatformSpecificConnectionInformation());
450
451 if (!specific_info.empty())
452 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000453}
454
Pavel Labath2272c482018-06-18 15:02:23 +0000455llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000459 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000461 m_os_version = HostInfo::GetOSVersion();
462 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000463 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 } else {
465 // We have a remote platform. We can only fetch the remote
466 // OS version if we are connected, and we don't want to do it
467 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000470
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000472 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000473 // We have valid OS version info, check to make sure it wasn't manually
474 // set prior to connecting. If it was manually set prior to connecting,
475 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 if (is_connected && !m_os_version_set_while_connected)
477 fetch = true;
478 } else {
479 // We don't have valid OS version info, fetch it if we are connected
480 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000481 }
482
Pavel Labath2272c482018-06-18 15:02:23 +0000483 if (fetch)
484 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 }
486
Pavel Labath2272c482018-06-18 15:02:23 +0000487 if (!m_os_version.empty())
488 return m_os_version;
489 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000490 // Check with the process in case it can answer the question if a process
491 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000492 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 }
Pavel Labath2272c482018-06-18 15:02:23 +0000494 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000495}
Greg Clayton1cb64962011-03-24 04:28:38 +0000496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497bool Platform::GetOSBuildString(std::string &s) {
498 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000501#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000503#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000505#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 else
507 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000508}
509
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510bool Platform::GetOSKernelDescription(std::string &s) {
511 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000512#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000514#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000516#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 else
518 return GetRemoteOSKernelDescription(s);
519}
520
521void Platform::AddClangModuleCompilationOptions(
522 Target *target, std::vector<std::string> &options) {
523 std::vector<std::string> default_compilation_options = {
524 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
525
526 options.insert(options.end(), default_compilation_options.begin(),
527 default_compilation_options.end());
528}
529
530FileSpec Platform::GetWorkingDirectory() {
531 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000532 llvm::SmallString<64> cwd;
533 if (llvm::sys::fs::current_path(cwd))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000534 return {};
535 else {
536 FileSpec file_spec(cwd);
537 FileSystem::Instance().Resolve(file_spec);
538 return file_spec;
539 }
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
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000553static FileSystem::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000554RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000555 llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000557 FileSpec src(path);
Zachary Turner7d86ee52017-03-08 17:56:08 +0000558 namespace fs = llvm::sys::fs;
559 switch (ft) {
560 case fs::file_type::fifo_file:
561 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 // we have no way to copy pipes and sockets - ignore them and continue
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000563 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000565
Zachary Turner7d86ee52017-03-08 17:56:08 +0000566 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 // make the new directory and get in there
568 FileSpec dst_dir = rc_baton->dst;
569 if (!dst_dir.GetFilename())
570 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000571 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 dst_dir, lldb::eFilePermissionsDirectoryDefault);
573 if (error.Fail()) {
574 rc_baton->error.SetErrorStringWithFormat(
575 "unable to setup directory %s on remote end", dst_dir.GetCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000576 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000577 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578
579 // now recurse
580 std::string src_dir_path(src.GetPath());
581
Adrian Prantl05097242018-04-30 16:49:04 +0000582 // Make a filespec that only fills in the directory of a FileSpec so when
583 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 FileSpec recurse_dst;
585 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000586 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
587 Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000588 FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
589 RecurseCopy_Callback, &rc_baton2);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 if (rc_baton2.error.Fail()) {
591 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000592 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000594 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000595 } break;
596
Zachary Turner7d86ee52017-03-08 17:56:08 +0000597 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 // copy the file and keep going
599 FileSpec dst_file = rc_baton->dst;
600 if (!dst_file.GetFilename())
601 dst_file.GetFilename() = src.GetFilename();
602
603 FileSpec src_resolved;
604
Jonas Devlieghere46376962018-10-31 21:49:27 +0000605 rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000606
607 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000608 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609
610 rc_baton->error =
611 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
612
613 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000614 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000616 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617 } break;
618
Zachary Turner7d86ee52017-03-08 17:56:08 +0000619 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 // copy the file and keep going
621 FileSpec dst_file = rc_baton->dst;
622 if (!dst_file.GetFilename())
623 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000624 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 if (err.Fail()) {
626 rc_baton->error.SetErrorString(err.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000627 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000629 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 } break;
631
Zachary Turner7d86ee52017-03-08 17:56:08 +0000632 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 rc_baton->error.SetErrorStringWithFormat(
634 "invalid file detected during copy: %s", src.GetPath().c_str());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000635 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 break;
637 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000638 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000639}
640
Zachary Turner97206d52017-05-12 04:51:55 +0000641Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
642 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643
644 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
645 if (log)
646 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
647 dst.GetPath().c_str());
648 FileSpec fixed_dst(dst);
649
650 if (!fixed_dst.GetFilename())
651 fixed_dst.GetFilename() = src.GetFilename();
652
653 FileSpec working_dir = GetWorkingDirectory();
654
655 if (dst) {
656 if (dst.GetDirectory()) {
657 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
658 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
659 fixed_dst.GetDirectory() = dst.GetDirectory();
660 }
Adrian Prantl05097242018-04-30 16:49:04 +0000661 // If the fixed destination file doesn't have a directory yet, then we
662 // must have a relative path. We will resolve this relative path against
663 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 if (!fixed_dst.GetDirectory()) {
665 FileSpec relative_spec;
666 std::string path;
667 if (working_dir) {
668 relative_spec = working_dir;
669 relative_spec.AppendPathComponent(dst.GetPath());
670 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
671 } else {
672 error.SetErrorStringWithFormat(
673 "platform working directory must be valid for relative path '%s'",
674 dst.GetPath().c_str());
675 return error;
676 }
677 }
678 } else {
679 if (working_dir) {
680 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
681 } else {
682 error.SetErrorStringWithFormat(
683 "platform working directory must be valid for relative path '%s'",
684 dst.GetPath().c_str());
685 return error;
686 }
687 }
688 } else {
689 if (working_dir) {
690 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
691 } else {
692 error.SetErrorStringWithFormat("platform working directory must be valid "
693 "when destination directory is empty");
694 return error;
695 }
696 }
697
698 if (log)
699 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
700 src.GetPath().c_str(), dst.GetPath().c_str(),
701 fixed_dst.GetPath().c_str());
702
703 if (GetSupportsRSync()) {
704 error = PutFile(src, dst);
705 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000706 namespace fs = llvm::sys::fs;
707 switch (fs::get_file_type(src.GetPath(), false)) {
708 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000709 llvm::sys::fs::remove(fixed_dst.GetPath());
Jonas Devlieghere7c5310b2018-11-01 15:47:33 +0000710 uint32_t permissions = FileSystem::Instance().GetPermissions(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 if (permissions == 0)
712 permissions = eFilePermissionsDirectoryDefault;
713 error = MakeDirectory(fixed_dst, permissions);
714 if (error.Success()) {
715 // Make a filespec that only fills in the directory of a FileSpec so
716 // when we enumerate we can quickly fill in the filename for dst copies
717 FileSpec recurse_dst;
718 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
719 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000720 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000721 FileSystem::Instance().EnumerateDirectory(
722 src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000723 return baton.error;
724 }
725 } break;
726
Zachary Turner7d86ee52017-03-08 17:56:08 +0000727 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000728 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 error = PutFile(src, fixed_dst);
730 break;
731
Zachary Turner7d86ee52017-03-08 17:56:08 +0000732 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000733 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734 FileSpec src_resolved;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000735 error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 if (error.Success())
737 error = CreateSymlink(dst, src_resolved);
738 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000739 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 error.SetErrorString("platform install doesn't handle pipes");
741 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000742 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 error.SetErrorString("platform install doesn't handle sockets");
744 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000745 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746 error.SetErrorString(
747 "platform install doesn't handle non file or directory items");
748 break;
749 }
750 }
751 return error;
752}
753
754bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
755 if (IsHost()) {
756 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000757 LLDB_LOG(log, "{0}", file_spec);
758 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
759 LLDB_LOG(log, "error: {0}", ec.message());
760 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000762 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000763 } else {
764 m_working_dir.Clear();
765 return SetRemoteWorkingDirectory(file_spec);
766 }
767}
768
Zachary Turner97206d52017-05-12 04:51:55 +0000769Status Platform::MakeDirectory(const FileSpec &file_spec,
770 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000772 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000774 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
776 GetPluginName().GetCString(),
777 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000778 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000780}
781
Zachary Turner97206d52017-05-12 04:51:55 +0000782Status Platform::GetFilePermissions(const FileSpec &file_spec,
783 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000784 if (IsHost()) {
785 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
786 if (Value)
787 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000788 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000789 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000790 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
792 GetPluginName().GetCString(),
793 LLVM_PRETTY_FUNCTION);
794 return error;
795 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000796}
797
Zachary Turner97206d52017-05-12 04:51:55 +0000798Status Platform::SetFilePermissions(const FileSpec &file_spec,
799 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000800 if (IsHost()) {
801 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
802 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
803 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000804 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
806 GetPluginName().GetCString(),
807 LLVM_PRETTY_FUNCTION);
808 return error;
809 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000810}
811
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812ConstString Platform::GetName() { return GetPluginName(); }
813
814const char *Platform::GetHostname() {
815 if (IsHost())
816 return "127.0.0.1";
817
818 if (m_name.empty())
819 return nullptr;
820 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000821}
822
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823ConstString Platform::GetFullNameForDylib(ConstString basename) {
824 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000825}
826
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
828 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
829 if (log)
830 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000831 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 m_working_dir = working_dir;
833 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000834}
835
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836const char *Platform::GetUserName(uint32_t uid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000837#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 const char *user_name = GetCachedUserName(uid);
839 if (user_name)
840 return user_name;
841 if (IsHost()) {
842 std::string name;
843 if (HostInfo::LookupUserName(uid, name))
844 return SetCachedUserName(uid, name.c_str(), name.size());
845 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000846#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 return nullptr;
Greg Clayton1cb64962011-03-24 04:28:38 +0000848}
849
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850const char *Platform::GetGroupName(uint32_t gid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000851#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 const char *group_name = GetCachedGroupName(gid);
853 if (group_name)
854 return group_name;
855 if (IsHost()) {
856 std::string name;
857 if (HostInfo::LookupGroupName(gid, name))
858 return SetCachedGroupName(gid, name.c_str(), name.size());
859 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000860#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 return nullptr;
Greg Clayton32e0a752011-03-30 18:16:51 +0000862}
Greg Clayton1cb64962011-03-24 04:28:38 +0000863
Pavel Labath2272c482018-06-18 15:02:23 +0000864bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000866 // We don't need anyone setting the OS version for the host platform, we
867 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000868 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000870 // We have a remote platform, allow setting the target OS version if we
871 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 // request the remote OS version from the connected platform.
873 if (IsConnected())
874 return false;
875 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000876 // We aren't connected and we might want to set the OS version ahead of
877 // time before we connect so we can peruse files and use a local SDK or
878 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000879 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000881 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 }
883 return false;
884}
885
Zachary Turner97206d52017-05-12 04:51:55 +0000886Status
887Platform::ResolveExecutable(const ModuleSpec &module_spec,
888 lldb::ModuleSP &exe_module_sp,
889 const FileSpecList *module_search_paths_ptr) {
890 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000891 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000892 if (module_spec.GetArchitecture().IsValid()) {
893 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
894 module_search_paths_ptr, nullptr,
895 nullptr);
896 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000897 // No valid architecture was specified, ask the platform for the
898 // architectures that we should be using (in the correct order) and see
899 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 ModuleSpec arch_module_spec(module_spec);
901 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
902 idx, arch_module_spec.GetArchitecture());
903 ++idx) {
904 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
905 module_search_paths_ptr, nullptr,
906 nullptr);
907 // Did we find an executable using one of the
908 if (error.Success() && exe_module_sp)
909 break;
910 }
Greg Claytone996fd32011-03-08 22:40:15 +0000911 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 } else {
913 error.SetErrorStringWithFormat("'%s' does not exist",
914 module_spec.GetFileSpec().GetPath().c_str());
915 }
916 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000917}
918
Zachary Turner97206d52017-05-12 04:51:55 +0000919Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
920 FileSpec &sym_file) {
921 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000922 if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 sym_file = sym_spec.GetSymbolFileSpec();
924 else
925 error.SetErrorString("unable to resolve symbol file");
926 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000927}
928
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929bool Platform::ResolveRemotePath(const FileSpec &platform_path,
930 FileSpec &resolved_platform_path) {
931 resolved_platform_path = platform_path;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000932 FileSystem::Instance().Resolve(resolved_platform_path);
933 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934}
935
936const ArchSpec &Platform::GetSystemArchitecture() {
937 if (IsHost()) {
938 if (!m_system_arch.IsValid()) {
939 // We have a local host platform
940 m_system_arch = HostInfo::GetArchitecture();
941 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000942 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000944 // We have a remote platform. We can only fetch the remote system
945 // architecture if we are connected, and we don't want to do it more than
946 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000947
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 bool fetch = false;
951 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000952 // We have valid OS version info, check to make sure it wasn't manually
953 // set prior to connecting. If it was manually set prior to connecting,
954 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 if (is_connected && !m_system_arch_set_while_connected)
956 fetch = true;
957 } else {
958 // We don't have valid OS version info, fetch it if we are connected
959 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000960 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000961
962 if (fetch) {
963 m_system_arch = GetRemoteSystemArchitecture();
964 m_system_arch_set_while_connected = m_system_arch.IsValid();
965 }
966 }
967 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000968}
969
Pavel Labath7263f1b2017-10-31 10:56:03 +0000970ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
971 if (triple.empty())
972 return ArchSpec();
973 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
974 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
975 return ArchSpec(triple);
976
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000977 if (auto kind = HostInfo::ParseArchitectureKind(triple))
978 return HostInfo::GetArchitecture(*kind);
979
Pavel Labath7263f1b2017-10-31 10:56:03 +0000980 ArchSpec compatible_arch;
981 ArchSpec raw_arch(triple);
982 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
983 return raw_arch;
984
985 if (!compatible_arch.IsValid())
986 return ArchSpec(normalized_triple);
987
988 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
989 if (normalized_triple.getVendorName().empty())
990 normalized_triple.setVendor(compatible_triple.getVendor());
991 if (normalized_triple.getOSName().empty())
992 normalized_triple.setOS(compatible_triple.getOS());
993 if (normalized_triple.getEnvironmentName().empty())
994 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
995 return ArchSpec(normalized_triple);
996}
997
Zachary Turner97206d52017-05-12 04:51:55 +0000998Status Platform::ConnectRemote(Args &args) {
999 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 if (IsHost())
1001 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1002 "the host platform and is always connected.",
1003 GetPluginName().GetCString());
1004 else
1005 error.SetErrorStringWithFormat(
1006 "Platform::ConnectRemote() is not supported by %s",
1007 GetPluginName().GetCString());
1008 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001009}
1010
Zachary Turner97206d52017-05-12 04:51:55 +00001011Status Platform::DisconnectRemote() {
1012 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 if (IsHost())
1014 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
1015 "the host platform and is always connected.",
1016 GetPluginName().GetCString());
1017 else
1018 error.SetErrorStringWithFormat(
1019 "Platform::DisconnectRemote() is not supported by %s",
1020 GetPluginName().GetCString());
1021 return error;
Greg Claytone996fd32011-03-08 22:40:15 +00001022}
Greg Clayton32e0a752011-03-30 18:16:51 +00001023
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024bool Platform::GetProcessInfo(lldb::pid_t pid,
1025 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +00001026 // Take care of the host case so that each subclass can just call this
1027 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028 if (IsHost())
1029 return Host::GetProcessInfo(pid, process_info);
1030 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00001031}
1032
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1034 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +00001035 // Take care of the host case so that each subclass can just call this
1036 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 uint32_t match_count = 0;
1038 if (IsHost())
1039 match_count = Host::FindProcesses(match_info, process_infos);
1040 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001041}
Greg Clayton8b82f082011-04-12 05:54:46 +00001042
Zachary Turner97206d52017-05-12 04:51:55 +00001043Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1044 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1046 if (log)
1047 log->Printf("Platform::%s()", __FUNCTION__);
1048
Adrian Prantl05097242018-04-30 16:49:04 +00001049 // Take care of the host case so that each subclass can just call this
1050 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 if (IsHost()) {
1052 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1053 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1054
1055 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1056 const bool is_localhost = true;
1057 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1058 const bool first_arg_is_full_shell_command = false;
1059 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1060 if (log) {
1061 const FileSpec &shell = launch_info.GetShell();
1062 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
1063 log->Printf(
1064 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1065 ", shell is '%s'",
1066 __FUNCTION__, num_resumes, shell_str);
1067 }
1068
1069 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1070 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1071 num_resumes))
1072 return error;
1073 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1074 error = ShellExpandArguments(launch_info);
1075 if (error.Fail()) {
1076 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1077 "consider launching with 'process "
1078 "launch'.",
1079 error.AsCString("unknown"));
1080 return error;
1081 }
1082 }
1083
Todd Fialaac33cc92014-10-09 01:02:08 +00001084 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1086 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001087
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 error = Host::LaunchProcess(launch_info);
1089 } else
1090 error.SetErrorString(
1091 "base lldb_private::Platform class can't launch remote processes");
1092 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001093}
1094
Zachary Turner97206d52017-05-12 04:51:55 +00001095Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 if (IsHost())
1097 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001098 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001099}
1100
Zachary Turner97206d52017-05-12 04:51:55 +00001101Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1103 if (log)
1104 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001105
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001107 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 size_t num_debuggers = Debugger::GetNumDebuggers();
1109 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1110 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1111 lldb_private::TargetList &targets = debugger->GetTargetList();
1112 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1113 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1114 if (process->GetID() == pid)
1115 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001116 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001118
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001120 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121 "base lldb_private::Platform class can't kill remote processes unless "
1122 "they are controlled by a process plugin");
1123 }
1124 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001125 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001126}
1127
Greg Clayton8b82f082011-04-12 05:54:46 +00001128lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1130 Target *target, // Can be nullptr, if nullptr create a
1131 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001132 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001133 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1134 if (log)
1135 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1136 static_cast<void *>(target));
1137
1138 ProcessSP process_sp;
1139 // Make sure we stop at the entry point
1140 launch_info.GetFlags().Set(eLaunchFlagDebug);
1141 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001142 // group, since then we can handle ^C interrupts ourselves w/o having to
1143 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 launch_info.SetLaunchInSeparateProcessGroup(true);
1145
1146 // Allow any StructuredData process-bound plugins to adjust the launch info
1147 // if needed
1148 size_t i = 0;
1149 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001150 // Note iteration can't simply go until a nullptr callback is returned, as it
1151 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1153 for (auto filter_callback = get_filter_func(i, iteration_complete);
1154 !iteration_complete;
1155 filter_callback = get_filter_func(++i, iteration_complete)) {
1156 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001157 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158 error = (*filter_callback)(launch_info, target);
1159 if (!error.Success()) {
1160 if (log)
1161 log->Printf("Platform::%s() StructuredDataPlugin launch "
1162 "filter failed.",
1163 __FUNCTION__);
1164 return process_sp;
1165 }
1166 }
1167 }
1168
1169 error = LaunchProcess(launch_info);
1170 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001171 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1173 ")",
1174 __FUNCTION__, launch_info.GetProcessID());
1175 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1176 ProcessAttachInfo attach_info(launch_info);
1177 process_sp = Attach(attach_info, debugger, target, error);
1178 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001179 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1181 __FUNCTION__, process_sp->GetPluginName().AsCString());
1182 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001183
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184 // Since we attached to the process, it will think it needs to detach
1185 // if the process object just goes away without an explicit call to
1186 // Process::Kill() or Process::Detach(), so let it know to kill the
1187 // process if this happens.
1188 process_sp->SetShouldDetach(false);
1189
Adrian Prantl05097242018-04-30 16:49:04 +00001190 // If we didn't have any file actions, the pseudo terminal might have
1191 // been used where the slave side was given as the file to open for
1192 // stdin/out/err after we have already opened the master so we can
1193 // read/write stdin/out/err.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001194 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001195 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196 process_sp->SetSTDIOFileDescriptor(pty_fd);
1197 }
1198 } else {
1199 if (log)
1200 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1201 error.AsCString());
1202 }
1203 } else {
1204 if (log)
1205 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1206 "invalid process id",
1207 __FUNCTION__);
1208 }
1209 } else {
1210 if (log)
1211 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1212 error.AsCString());
1213 }
1214
1215 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001216}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001217
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001218lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1220 ArchSpec *platform_arch_ptr) {
1221 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001222 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 if (arch.IsValid())
1224 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1225 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001226}
1227
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001228//------------------------------------------------------------------
1229/// Lets a platform answer if it is compatible with a given
1230/// architecture and the target triple contained within.
1231//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1233 bool exact_arch_match,
1234 ArchSpec *compatible_arch_ptr) {
1235 // If the architecture is invalid, we must answer true...
1236 if (arch.IsValid()) {
1237 ArchSpec platform_arch;
1238 // Try for an exact architecture match first.
1239 if (exact_arch_match) {
1240 for (uint32_t arch_idx = 0;
1241 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1242 ++arch_idx) {
1243 if (arch.IsExactMatch(platform_arch)) {
1244 if (compatible_arch_ptr)
1245 *compatible_arch_ptr = platform_arch;
1246 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001247 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248 }
1249 } else {
1250 for (uint32_t arch_idx = 0;
1251 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1252 ++arch_idx) {
1253 if (arch.IsCompatibleMatch(platform_arch)) {
1254 if (compatible_arch_ptr)
1255 *compatible_arch_ptr = platform_arch;
1256 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001257 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001259 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 }
1261 if (compatible_arch_ptr)
1262 compatible_arch_ptr->Clear();
1263 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001264}
1265
Zachary Turner97206d52017-05-12 04:51:55 +00001266Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1267 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001268 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1269 if (log)
1270 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001271
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 uint32_t source_open_options =
1273 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001274 namespace fs = llvm::sys::fs;
1275 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001277
Jonas Devlieghere50bc1ed2018-11-02 22:34:51 +00001278 File source_file;
1279 Status error = FileSystem::Instance().Open(
1280 source_file, source, source_open_options, lldb::eFilePermissionsUserRW);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 uint32_t permissions = source_file.GetPermissions(error);
1282 if (permissions == 0)
1283 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001284
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001286 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287 lldb::user_id_t dest_file = OpenFile(
1288 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1289 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1290 permissions, error);
1291 if (log)
1292 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001293
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294 if (error.Fail())
1295 return error;
1296 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001297 return Status("unable to open target file");
Jason Molendaeb9902a2018-12-07 00:35:26 +00001298 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 uint64_t offset = 0;
1300 for (;;) {
1301 size_t bytes_read = buffer_sp->GetByteSize();
1302 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1303 if (error.Fail() || bytes_read == 0)
1304 break;
1305
1306 const uint64_t bytes_written =
1307 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001308 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001310
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311 offset += bytes_written;
1312 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001313 // We didn't write the correct number of bytes, so adjust the file
1314 // position in the source file we are reading from...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001316 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 }
1318 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001321 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001322
1323 // TODO: ChownFile?
1324
1325 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001326}
1327
Zachary Turner97206d52017-05-12 04:51:55 +00001328Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1329 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001331}
1332
Zachary Turner97206d52017-05-12 04:51:55 +00001333Status
1334Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1335 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001336{
Zachary Turner97206d52017-05-12 04:51:55 +00001337 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001339}
1340
Kate Stoneb9c1b512016-09-06 20:57:50 +00001341bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1342 return false;
1343}
1344
Zachary Turner97206d52017-05-12 04:51:55 +00001345Status Platform::Unlink(const FileSpec &path) {
1346 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 return error;
1348}
1349
Ed Maste37c40af2017-08-16 12:55:02 +00001350MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1351 addr_t length, unsigned prot,
1352 unsigned flags, addr_t fd,
1353 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001354 uint64_t flags_platform = 0;
1355 if (flags & eMmapFlagsPrivate)
1356 flags_platform |= MAP_PRIVATE;
1357 if (flags & eMmapFlagsAnon)
1358 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001359
1360 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1361 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362}
1363
Zachary Turner97206d52017-05-12 04:51:55 +00001364lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 const char *command, // Shouldn't be nullptr
1366 const FileSpec &
1367 working_dir, // Pass empty FileSpec to use the current working directory
1368 int *status_ptr, // Pass nullptr if you don't want the process exit status
1369 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1370 // process to exit
1371 std::string
1372 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001373 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 if (IsHost())
1375 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
Pavel Labath19dd1a02018-05-10 10:46:03 +00001376 command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 else
Zachary Turner97206d52017-05-12 04:51:55 +00001378 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379}
1380
1381bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1382 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001383 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001384 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001385 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1386 if (!Result)
1387 return false;
1388 std::tie(high, low) = Result->words();
1389 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001390}
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392void Platform::SetLocalCacheDirectory(const char *local) {
1393 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001394}
1395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396const char *Platform::GetLocalCacheDirectory() {
1397 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001398}
Greg Claytonfbb76342013-11-20 21:07:01 +00001399
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001400static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001402 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001404 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001405 "Platform-specific options required for rsync to work."},
1406 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001407 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408 "Platform-specific rsync prefix put before the remote path."},
1409 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001410 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 "Do not automatically fill in the remote hostname when composing the "
1412 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001413};
1414
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001415static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001417 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001419 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001421};
1422
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001423static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001425 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001427};
1428
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001429llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001430 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001431}
1432
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433void OptionGroupPlatformRSync::OptionParsingStarting(
1434 ExecutionContext *execution_context) {
1435 m_rsync = false;
1436 m_rsync_opts.clear();
1437 m_rsync_prefix.clear();
1438 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001439}
1440
Zachary Turner97206d52017-05-12 04:51:55 +00001441lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001442OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001443 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001445 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446 char short_option = (char)GetDefinitions()[option_idx].short_option;
1447 switch (short_option) {
1448 case 'r':
1449 m_rsync = true;
1450 break;
1451
1452 case 'R':
1453 m_rsync_opts.assign(option_arg);
1454 break;
1455
1456 case 'P':
1457 m_rsync_prefix.assign(option_arg);
1458 break;
1459
1460 case 'i':
1461 m_ignores_remote_hostname = true;
1462 break;
1463
1464 default:
1465 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1466 break;
1467 }
1468
1469 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001470}
1471
Greg Clayton4116e932012-05-15 02:33:01 +00001472lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1474 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001475}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001476
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001477llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001478 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001479}
1480
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481void OptionGroupPlatformSSH::OptionParsingStarting(
1482 ExecutionContext *execution_context) {
1483 m_ssh = false;
1484 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001485}
1486
Zachary Turner97206d52017-05-12 04:51:55 +00001487lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001488OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001489 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001491 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 char short_option = (char)GetDefinitions()[option_idx].short_option;
1493 switch (short_option) {
1494 case 's':
1495 m_ssh = true;
1496 break;
1497
1498 case 'S':
1499 m_ssh_opts.assign(option_arg);
1500 break;
1501
1502 default:
1503 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1504 break;
1505 }
1506
1507 return error;
1508}
1509
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001510llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001511 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512}
1513
1514void OptionGroupPlatformCaching::OptionParsingStarting(
1515 ExecutionContext *execution_context) {
1516 m_cache_dir.clear();
1517}
1518
Zachary Turner97206d52017-05-12 04:51:55 +00001519lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001520 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001522 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 char short_option = (char)GetDefinitions()[option_idx].short_option;
1524 switch (short_option) {
1525 case 'c':
1526 m_cache_dir.assign(option_arg);
1527 break;
1528
1529 default:
1530 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1531 break;
1532 }
1533
1534 return error;
1535}
1536
Pavel Labath62930e52018-01-10 11:57:31 +00001537Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538
1539const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1540 if (!m_calculated_trap_handlers) {
1541 std::lock_guard<std::mutex> guard(m_mutex);
1542 if (!m_calculated_trap_handlers) {
1543 CalculateTrapHandlerSymbolNames();
1544 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001545 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001546 }
1547 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001548}
1549
Zachary Turner97206d52017-05-12 04:51:55 +00001550Status Platform::GetCachedExecutable(
1551 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1552 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 const auto platform_spec = module_spec.GetFileSpec();
1554 const auto error = LoadCachedExecutable(
1555 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1556 if (error.Success()) {
1557 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1558 module_spec.GetPlatformFileSpec() = platform_spec;
1559 }
1560
1561 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001562}
1563
Zachary Turner97206d52017-05-12 04:51:55 +00001564Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1566 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1567 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1568 [&](const ModuleSpec &spec) {
1569 return remote_platform.ResolveExecutable(
1570 spec, module_sp, module_search_paths_ptr);
1571 },
1572 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001573}
1574
Zachary Turner97206d52017-05-12 04:51:55 +00001575Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1576 Process *process,
1577 lldb::ModuleSP &module_sp,
1578 const ModuleResolver &module_resolver,
1579 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580 // Get module information from a target.
1581 ModuleSpec resolved_module_spec;
1582 bool got_module_spec = false;
1583 if (process) {
1584 // Try to get module information from the process
1585 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1586 module_spec.GetArchitecture(),
1587 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001588 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1590 got_module_spec = true;
1591 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001592 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001594
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001595 if (!module_spec.GetArchitecture().IsValid()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001596 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001597 // No valid architecture was specified, ask the platform for the
1598 // architectures that we should be using (in the correct order) and see if
1599 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001600 ModuleSpec arch_module_spec(module_spec);
1601 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1602 idx, arch_module_spec.GetArchitecture());
1603 ++idx) {
1604 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1605 nullptr, nullptr);
1606 // Did we find an executable using one of the
1607 if (error.Success() && module_sp)
1608 break;
1609 }
1610 if (module_sp)
1611 got_module_spec = true;
1612 }
1613
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001615 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1617 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001618 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1620 return module_resolver(module_spec);
1621 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001622 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 // If we are looking for a specific UUID, make sure resolved_module_spec has
1626 // the same one before we search.
1627 if (module_spec.GetUUID().IsValid()) {
1628 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1629 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001630
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 // Trying to find a module by UUID on local file system.
1632 const auto error = module_resolver(resolved_module_spec);
1633 if (error.Fail()) {
1634 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001635 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001637
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001639}
1640
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1642 lldb::ModuleSP &module_sp,
1643 bool *did_create_ptr) {
1644 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1645 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001646 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647
1648 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1649
1650 // Check local cache for a module.
1651 auto error = m_module_cache->GetAndPut(
1652 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1653 [this](const ModuleSpec &module_spec,
1654 const FileSpec &tmp_download_file_spec) {
1655 return DownloadModuleSlice(
1656 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1657 module_spec.GetObjectSize(), tmp_download_file_spec);
1658
1659 },
1660 [this](const ModuleSP &module_sp,
1661 const FileSpec &tmp_download_file_spec) {
1662 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1663 },
1664 module_sp, did_create_ptr);
1665 if (error.Success())
1666 return true;
1667
1668 if (log)
1669 log->Printf("Platform::%s - module %s not found in local cache: %s",
1670 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1671 error.AsCString());
1672 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001673}
1674
Zachary Turner97206d52017-05-12 04:51:55 +00001675Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1676 const uint64_t src_offset,
1677 const uint64_t src_size,
1678 const FileSpec &dst_file_spec) {
1679 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001680
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001681 std::error_code EC;
1682 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1683 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 error.SetErrorStringWithFormat("unable to open destination file: %s",
1685 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001686 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001687 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001688
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1690 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001691
Kate Stoneb9c1b512016-09-06 20:57:50 +00001692 if (error.Fail()) {
1693 error.SetErrorStringWithFormat("unable to open source file: %s",
1694 error.AsCString());
1695 return error;
1696 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001697
Kate Stoneb9c1b512016-09-06 20:57:50 +00001698 std::vector<char> buffer(1024);
1699 auto offset = src_offset;
1700 uint64_t total_bytes_read = 0;
1701 while (total_bytes_read < src_size) {
1702 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1703 src_size - total_bytes_read);
1704 const uint64_t n_read =
1705 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001706 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 break;
1708 if (n_read == 0) {
1709 error.SetErrorString("read 0 bytes");
1710 break;
1711 }
1712 offset += n_read;
1713 total_bytes_read += n_read;
1714 dst.write(&buffer[0], n_read);
1715 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001716
Zachary Turner97206d52017-05-12 04:51:55 +00001717 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718 CloseFile(src_fd, close_error); // Ignoring close error.
1719
1720 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001721}
1722
Zachary Turner97206d52017-05-12 04:51:55 +00001723Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1724 const FileSpec &dst_file_spec) {
1725 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001727}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001728
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729FileSpec Platform::GetModuleCacheRoot() {
1730 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1731 dir_spec.AppendPathComponent(GetName().AsCString());
1732 return dir_spec;
1733}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001734
Kate Stoneb9c1b512016-09-06 20:57:50 +00001735const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001736
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1738 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1739 return s_default_unix_signals_sp;
1740}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001741
Kate Stoneb9c1b512016-09-06 20:57:50 +00001742const UnixSignalsSP &Platform::GetUnixSignals() {
1743 if (IsHost())
1744 return Host::GetUnixSignals();
1745 return GetRemoteUnixSignals();
1746}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001747
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748uint32_t Platform::LoadImage(lldb_private::Process *process,
1749 const lldb_private::FileSpec &local_file,
1750 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001751 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 if (local_file && remote_file) {
1753 // Both local and remote file was specified. Install the local file to the
1754 // given location.
1755 if (IsRemote() || local_file != remote_file) {
1756 error = Install(local_file, remote_file);
1757 if (error.Fail())
1758 return LLDB_INVALID_IMAGE_TOKEN;
1759 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001760 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001762
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 if (local_file) {
1764 // Only local file was specified. Install it to the current working
1765 // directory.
1766 FileSpec target_file = GetWorkingDirectory();
1767 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1768 if (IsRemote() || local_file != target_file) {
1769 error = Install(local_file, target_file);
1770 if (error.Fail())
1771 return LLDB_INVALID_IMAGE_TOKEN;
1772 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001773 return DoLoadImage(process, target_file, nullptr, error);
1774 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001775
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776 if (remote_file) {
1777 // Only remote file was specified so we don't have to do any copying
Jim Ingham0d231f72018-06-28 20:02:11 +00001778 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001780
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 error.SetErrorString("Neither local nor remote file was specified");
1782 return LLDB_INVALID_IMAGE_TOKEN;
1783}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001784
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1786 const lldb_private::FileSpec &remote_file,
Jim Ingham0d231f72018-06-28 20:02:11 +00001787 const std::vector<std::string> *paths,
1788 lldb_private::Status &error,
1789 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001790 error.SetErrorString("LoadImage is not supported on the current platform");
1791 return LLDB_INVALID_IMAGE_TOKEN;
1792}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001793
Jim Ingham0d231f72018-06-28 20:02:11 +00001794uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1795 const lldb_private::FileSpec &remote_filename,
1796 const std::vector<std::string> &paths,
1797 lldb_private::Status &error,
1798 lldb_private::FileSpec *loaded_path)
1799{
1800 FileSpec file_to_use;
1801 if (remote_filename.IsAbsolute())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001802 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1803
1804 remote_filename.GetPathStyle());
Jim Ingham0d231f72018-06-28 20:02:11 +00001805 else
1806 file_to_use = remote_filename;
1807
1808 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1809}
1810
Zachary Turner97206d52017-05-12 04:51:55 +00001811Status Platform::UnloadImage(lldb_private::Process *process,
1812 uint32_t image_token) {
1813 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001815
Zachary Turner31659452016-11-17 21:15:14 +00001816lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1817 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 lldb_private::Debugger &debugger,
1819 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001820 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001822
Kate Stoneb9c1b512016-09-06 20:57:50 +00001823 if (!target) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001824 ArchSpec arch;
1825 if (target && target->GetArchitecture().IsValid())
1826 arch = target->GetArchitecture();
1827 else
1828 arch = Target::GetDefaultArchitecture();
1829
1830 const char *triple = "";
1831 if (arch.IsValid())
1832 triple = arch.GetTriple().getTriple().c_str();
1833
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001835 error = debugger.GetTargetList().CreateTarget(
Jason Molendadf9f7962018-10-23 23:45:56 +00001836 debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001837 target = new_target_sp.get();
1838 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001839
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 if (!target || error.Fail())
1841 return nullptr;
1842
1843 debugger.GetTargetList().SetSelectedTarget(target);
1844
1845 lldb::ProcessSP process_sp =
1846 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1847 if (!process_sp)
1848 return nullptr;
1849
1850 error =
1851 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1852 if (error.Fail())
1853 return nullptr;
1854
1855 return process_sp;
1856}
1857
1858size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001859 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001860 error.Clear();
1861 return 0;
1862}
1863
1864size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1865 BreakpointSite *bp_site) {
1866 ArchSpec arch = target.GetArchitecture();
1867 const uint8_t *trap_opcode = nullptr;
1868 size_t trap_opcode_size = 0;
1869
1870 switch (arch.GetMachine()) {
1871 case llvm::Triple::aarch64: {
1872 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1873 trap_opcode = g_aarch64_opcode;
1874 trap_opcode_size = sizeof(g_aarch64_opcode);
1875 } break;
1876
1877 // TODO: support big-endian arm and thumb trap codes.
1878 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001879 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1880 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001881 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1882 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1883
1884 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001885 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886
1887 if (bp_loc_sp) {
1888 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001889 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001891 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001892 }
1893
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001894 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895 trap_opcode = g_thumb_breakpoint_opcode;
1896 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1897 } else {
1898 trap_opcode = g_arm_breakpoint_opcode;
1899 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1900 }
1901 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001902
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903 case llvm::Triple::mips:
1904 case llvm::Triple::mips64: {
1905 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1906 trap_opcode = g_hex_opcode;
1907 trap_opcode_size = sizeof(g_hex_opcode);
1908 } break;
1909
1910 case llvm::Triple::mipsel:
1911 case llvm::Triple::mips64el: {
1912 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1913 trap_opcode = g_hex_opcode;
1914 trap_opcode_size = sizeof(g_hex_opcode);
1915 } break;
1916
1917 case llvm::Triple::systemz: {
1918 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1919 trap_opcode = g_hex_opcode;
1920 trap_opcode_size = sizeof(g_hex_opcode);
1921 } break;
1922
1923 case llvm::Triple::hexagon: {
1924 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1925 trap_opcode = g_hex_opcode;
1926 trap_opcode_size = sizeof(g_hex_opcode);
1927 } break;
1928
1929 case llvm::Triple::ppc:
1930 case llvm::Triple::ppc64: {
1931 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1932 trap_opcode = g_ppc_opcode;
1933 trap_opcode_size = sizeof(g_ppc_opcode);
1934 } break;
1935
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001936 case llvm::Triple::ppc64le: {
1937 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1938 trap_opcode = g_ppc64le_opcode;
1939 trap_opcode_size = sizeof(g_ppc64le_opcode);
1940 } break;
1941
Kate Stoneb9c1b512016-09-06 20:57:50 +00001942 case llvm::Triple::x86:
1943 case llvm::Triple::x86_64: {
1944 static const uint8_t g_i386_opcode[] = {0xCC};
1945 trap_opcode = g_i386_opcode;
1946 trap_opcode_size = sizeof(g_i386_opcode);
1947 } break;
1948
1949 default:
David Blaikiea322f362017-01-06 00:38:06 +00001950 llvm_unreachable(
1951 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001952 }
1953
1954 assert(bp_site);
1955 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1956 return trap_opcode_size;
1957
1958 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001959}