blob: ba78469b58eb8821da6a06e741028f24709c98a3 [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>
Jonas Devlieghere796ac802019-02-11 23:13:08 +000012#include <memory>
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000013#include <vector>
14
Eugene Zelenko9394d7722016-02-18 00:10:17 +000015#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Path.h"
17
Greg Clayton4116e932012-05-15 02:33:01 +000018#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000019#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000020#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000021#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000023#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000024#include "lldb/Core/StreamFile.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000025#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000026#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000027#include "lldb/Host/HostInfo.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000028#include "lldb/Host/OptionParser.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000029#include "lldb/Interpreter/OptionValueProperties.h"
30#include "lldb/Interpreter/Property.h"
31#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000032#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000033#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000034#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000035#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000036#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000037#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000038#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000039#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000040#include "lldb/Utility/Status.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000041#include "lldb/Utility/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000042
Zachary Turner7d86ee52017-03-08 17:56:08 +000043#include "llvm/Support/FileSystem.h"
44
Adrian Prantl05097242018-04-30 16:49:04 +000045// Define these constants from POSIX mman.h rather than include the file so
46// that they will be correct even when compiled on Linux.
Robert Flack96ad3de2015-05-09 15:53:31 +000047#define MAP_PRIVATE 2
48#define MAP_ANON 0x1000
49
Greg Claytone996fd32011-03-08 22:40:15 +000050using namespace lldb;
51using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000052
53static uint32_t g_initialize_count = 0;
54
Adrian Prantl05097242018-04-30 16:49:04 +000055// Use a singleton function for g_local_platform_sp to avoid init constructors
56// since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000057static PlatformSP &GetHostPlatformSP() {
58 static PlatformSP g_platform_sp;
59 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000063
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000064namespace {
65
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000066static constexpr PropertyDefinition g_properties[] = {
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000067#define LLDB_PROPERTIES_platform
Jonas Devlieghere463a48e2019-07-26 18:14:08 +000068#include "Properties.inc"
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000069};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000070
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000071enum {
72#define LLDB_PROPERTIES_platform
Jonas Devlieghere463a48e2019-07-26 18:14:08 +000073#include "PropertiesEnum.inc"
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000074};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078ConstString PlatformProperties::GetSettingName() {
79 static ConstString g_setting_name("platform");
80 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000081}
82
Kate Stoneb9c1b512016-09-06 20:57:50 +000083PlatformProperties::PlatformProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +000084 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 auto module_cache_dir = GetModuleCacheDirectory();
88 if (module_cache_dir)
89 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 llvm::SmallString<64> user_home_dir;
92 if (!llvm::sys::path::home_directory(user_home_dir))
93 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000094
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000095 module_cache_dir = FileSpec(user_home_dir.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 module_cache_dir.AppendPathComponent(".lldb");
97 module_cache_dir.AppendPathComponent("module_cache");
98 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101bool PlatformProperties::GetUseModuleCache() const {
102 const auto idx = ePropertyUseModuleCache;
103 return m_collection_sp->GetPropertyAtIndexAsBoolean(
104 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
108 return m_collection_sp->SetPropertyAtIndexAsBoolean(
109 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112FileSpec PlatformProperties::GetModuleCacheDirectory() const {
113 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
114 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
118 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
119 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000120}
121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000123///
124/// There should only be one of these for each host that LLDB runs
125/// upon that should be statically compiled in and registered using
126/// preprocessor macros or other similar build mechanisms.
127///
128/// This platform will be used as the default platform when launching
129/// or attaching to processes unless another platform is specified.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
131
132static std::vector<PlatformSP> &GetPlatformList() {
133 static std::vector<PlatformSP> g_platform_list;
134 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137static std::recursive_mutex &GetPlatformListMutex() {
138 static std::recursive_mutex g_mutex;
139 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144void Platform::Terminate() {
145 if (g_initialize_count > 0) {
146 if (--g_initialize_count == 0) {
147 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
148 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000149 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000151}
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
154 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
155 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
159 // The native platform should use its static void Platform::Initialize()
160 // function to register itself as the native platform.
161 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 if (platform_sp) {
164 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
165 GetPlatformList().push_back(platform_sp);
166 }
Greg Claytone996fd32011-03-08 22:40:15 +0000167}
168
Zachary Turner97206d52017-05-12 04:51:55 +0000169Status Platform::GetFileWithUUID(const FileSpec &platform_file,
170 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 // Default to the local case
172 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000173 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000174}
175
Greg Clayton91c0e742013-01-11 23:44:27 +0000176FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177Platform::LocateExecutableScriptingResources(Target *target, Module &module,
178 Stream *feedback_stream) {
179 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182// PlatformSP
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000183// Platform::FindPlugin (Process *process, ConstString plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000184//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000185// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000186// if (plugin_name)
187// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188// create_callback =
189// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000190// if (create_callback)
191// {
192// ArchSpec arch;
193// if (process)
194// {
195// arch = process->GetTarget().GetArchitecture();
196// }
197// PlatformSP platform_sp(create_callback(process, &arch));
198// if (platform_sp)
199// return platform_sp;
200// }
201// }
202// else
203// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204// for (uint32_t idx = 0; (create_callback =
205// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
206// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000207// {
208// PlatformSP platform_sp(create_callback(process, nullptr));
209// if (platform_sp)
210// return platform_sp;
211// }
212// }
213// return PlatformSP();
214//}
Jason Molenda1c627542013-04-05 01:03:25 +0000215
Zachary Turner97206d52017-05-12 04:51:55 +0000216Status Platform::GetSharedModule(const ModuleSpec &module_spec,
217 Process *process, ModuleSP &module_sp,
218 const FileSpecList *module_search_paths_ptr,
219 ModuleSP *old_module_sp_ptr,
220 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 if (IsHost())
222 return ModuleList::GetSharedModule(
223 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
224 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000225
Pavel Labath1f8639a2018-08-28 16:32:46 +0000226 // Module resolver lambda.
227 auto resolver = [&](const ModuleSpec &spec) {
228 Status error(eErrorTypeGeneric);
229 ModuleSpec resolved_spec;
230 // Check if we have sysroot set.
231 if (m_sdk_sysroot) {
232 // Prepend sysroot to module spec.
233 resolved_spec = spec;
234 resolved_spec.GetFileSpec().PrependPathComponent(
235 m_sdk_sysroot.GetStringRef());
236 // Try to get shared module with resolved spec.
237 error = ModuleList::GetSharedModule(
238 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
239 did_create_ptr, false);
240 }
241 // If we don't have sysroot or it didn't work then
242 // try original module spec.
243 if (!error.Success()) {
244 resolved_spec = spec;
245 error = ModuleList::GetSharedModule(
246 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
247 did_create_ptr, false);
248 }
249 if (error.Success() && module_sp)
250 module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
251 return error;
252 };
253
254 return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000256}
257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
259 const ArchSpec &arch, ModuleSpec &module_spec) {
260 ModuleSpecList module_specs;
261 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
262 module_specs) == 0)
263 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 ModuleSpec matched_module_spec;
266 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
267 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000268}
269
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000270PlatformSP Platform::Find(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 if (name) {
272 static ConstString g_host_platform_name("host");
273 if (name == g_host_platform_name)
274 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
277 for (const auto &platform_sp : GetPlatformList()) {
278 if (platform_sp->GetName() == name)
279 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000280 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 }
282 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000283}
284
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000285PlatformSP Platform::Create(ConstString name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 PlatformCreateInstance create_callback = nullptr;
287 lldb::PlatformSP platform_sp;
288 if (name) {
289 static ConstString g_host_platform_name("host");
290 if (name == g_host_platform_name)
291 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000292
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 create_callback =
294 PluginManager::GetPlatformCreateCallbackForPluginName(name);
295 if (create_callback)
296 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000297 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 error.SetErrorStringWithFormat(
299 "unable to find a plug-in for the platform named \"%s\"",
300 name.GetCString());
301 } else
302 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000303
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 if (platform_sp) {
305 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
306 GetPlatformList().push_back(platform_sp);
307 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000310}
311
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000313 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 lldb::PlatformSP platform_sp;
315 if (arch.IsValid()) {
316 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000317 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 // First try exact arch matches across all platforms already created
319 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
320 for (const auto &platform_sp : GetPlatformList()) {
321 if (platform_sp->IsCompatibleArchitecture(arch, true,
322 platform_arch_ptr))
323 return platform_sp;
324 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000325
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 // Next try compatible arch matches across all platforms already created
327 for (const auto &platform_sp : GetPlatformList()) {
328 if (platform_sp->IsCompatibleArchitecture(arch, false,
329 platform_arch_ptr))
330 return platform_sp;
331 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333
334 PlatformCreateInstance create_callback;
335 // First try exact arch matches across all platform plug-ins
336 uint32_t idx;
337 for (idx = 0; (create_callback =
338 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
339 ++idx) {
340 if (create_callback) {
341 platform_sp = create_callback(false, &arch);
342 if (platform_sp &&
343 platform_sp->IsCompatibleArchitecture(arch, true,
344 platform_arch_ptr)) {
345 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
346 GetPlatformList().push_back(platform_sp);
347 return platform_sp;
348 }
349 }
350 }
351 // Next try compatible arch matches across all platform plug-ins
352 for (idx = 0; (create_callback =
353 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
354 ++idx) {
355 if (create_callback) {
356 platform_sp = create_callback(false, &arch);
357 if (platform_sp &&
358 platform_sp->IsCompatibleArchitecture(arch, false,
359 platform_arch_ptr)) {
360 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
361 GetPlatformList().push_back(platform_sp);
362 return platform_sp;
363 }
364 }
365 }
366 } else
367 error.SetErrorString("invalid platform name");
368 if (platform_arch_ptr)
369 platform_arch_ptr->Clear();
370 platform_sp.reset();
371 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000372}
373
Pavel Labath7263f1b2017-10-31 10:56:03 +0000374ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
375 if (platform)
376 return platform->GetAugmentedArchSpec(triple);
377 return HostInfo::GetAugmentedArchSpec(triple);
378}
379
Greg Claytone996fd32011-03-08 22:40:15 +0000380/// Default Constructor
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000381Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 : m_is_host(is_host), m_os_version_set_while_connected(false),
383 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000384 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000385 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
386 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
387 m_ignores_remote_hostname(false), m_trap_handlers(),
388 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 m_module_cache(llvm::make_unique<ModuleCache>()) {
390 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000391 LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000392}
393
Greg Claytone996fd32011-03-08 22:40:15 +0000394/// Destructor.
395///
396/// The destructor is virtual since this class is designed to be
397/// inherited from by the plug-in instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398Platform::~Platform() {
399 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000400 LLDB_LOGF(log, "%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000401}
402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 std::string s;
405 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 ArchSpec arch(GetSystemArchitecture());
408 if (arch.IsValid()) {
409 if (!arch.GetTriple().str().empty()) {
410 strm.Printf(" Triple: ");
411 arch.DumpTriple(strm);
412 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000413 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000415
Pavel Labath2272c482018-06-18 15:02:23 +0000416 llvm::VersionTuple os_version = GetOSVersion();
417 if (!os_version.empty()) {
418 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 if (GetOSBuildString(s))
421 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 strm.EOL();
424 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (GetOSKernelDescription(s))
427 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 if (IsHost()) {
430 strm.Printf(" Hostname: %s\n", GetHostname());
431 } else {
432 const bool is_connected = IsConnected();
433 if (is_connected)
434 strm.Printf(" Hostname: %s\n", GetHostname());
435 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
436 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 if (GetWorkingDirectory()) {
439 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
440 }
441 if (!IsConnected())
442 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 std::string specific_info(GetPlatformSpecificConnectionInformation());
445
446 if (!specific_info.empty())
447 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000448}
449
Pavel Labath2272c482018-06-18 15:02:23 +0000450llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000454 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000456 m_os_version = HostInfo::GetOSVersion();
457 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000458 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 } else {
460 // We have a remote platform. We can only fetch the remote
461 // OS version if we are connected, and we don't want to do it
462 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000465
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000467 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000468 // We have valid OS version info, check to make sure it wasn't manually
469 // set prior to connecting. If it was manually set prior to connecting,
470 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 if (is_connected && !m_os_version_set_while_connected)
472 fetch = true;
473 } else {
474 // We don't have valid OS version info, fetch it if we are connected
475 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000476 }
477
Pavel Labath2272c482018-06-18 15:02:23 +0000478 if (fetch)
479 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 }
481
Pavel Labath2272c482018-06-18 15:02:23 +0000482 if (!m_os_version.empty())
483 return m_os_version;
484 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000485 // Check with the process in case it can answer the question if a process
486 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000487 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 }
Pavel Labath2272c482018-06-18 15:02:23 +0000489 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000490}
Greg Clayton1cb64962011-03-24 04:28:38 +0000491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492bool Platform::GetOSBuildString(std::string &s) {
493 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000496#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000498#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000500#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 else
502 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000503}
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505bool Platform::GetOSKernelDescription(std::string &s) {
506 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000507#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000509#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000511#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 else
513 return GetRemoteOSKernelDescription(s);
514}
515
516void Platform::AddClangModuleCompilationOptions(
517 Target *target, std::vector<std::string> &options) {
518 std::vector<std::string> default_compilation_options = {
519 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
520
521 options.insert(options.end(), default_compilation_options.begin(),
522 default_compilation_options.end());
523}
524
525FileSpec Platform::GetWorkingDirectory() {
526 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000527 llvm::SmallString<64> cwd;
528 if (llvm::sys::fs::current_path(cwd))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000529 return {};
530 else {
531 FileSpec file_spec(cwd);
532 FileSystem::Instance().Resolve(file_spec);
533 return file_spec;
534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 } else {
536 if (!m_working_dir)
537 m_working_dir = GetRemoteWorkingDirectory();
538 return m_working_dir;
539 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000540}
541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542struct RecurseCopyBaton {
543 const FileSpec &dst;
544 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000545 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000546};
547
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000548static FileSystem::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000549RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000550 llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000552 FileSpec src(path);
Zachary Turner7d86ee52017-03-08 17:56:08 +0000553 namespace fs = llvm::sys::fs;
554 switch (ft) {
555 case fs::file_type::fifo_file:
556 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 // we have no way to copy pipes and sockets - ignore them and continue
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000558 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000560
Zachary Turner7d86ee52017-03-08 17:56:08 +0000561 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 // make the new directory and get in there
563 FileSpec dst_dir = rc_baton->dst;
564 if (!dst_dir.GetFilename())
565 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000566 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 dst_dir, lldb::eFilePermissionsDirectoryDefault);
568 if (error.Fail()) {
569 rc_baton->error.SetErrorStringWithFormat(
570 "unable to setup directory %s on remote end", dst_dir.GetCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000571 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573
574 // now recurse
575 std::string src_dir_path(src.GetPath());
576
Adrian Prantl05097242018-04-30 16:49:04 +0000577 // Make a filespec that only fills in the directory of a FileSpec so when
578 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 FileSpec recurse_dst;
580 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000581 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
582 Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000583 FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
584 RecurseCopy_Callback, &rc_baton2);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 if (rc_baton2.error.Fail()) {
586 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000587 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000589 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 } break;
591
Zachary Turner7d86ee52017-03-08 17:56:08 +0000592 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 // copy the file and keep going
594 FileSpec dst_file = rc_baton->dst;
595 if (!dst_file.GetFilename())
596 dst_file.GetFilename() = src.GetFilename();
597
598 FileSpec src_resolved;
599
Jonas Devlieghere46376962018-10-31 21:49:27 +0000600 rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601
602 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000603 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604
605 rc_baton->error =
606 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
607
608 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000609 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000611 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 } break;
613
Zachary Turner7d86ee52017-03-08 17:56:08 +0000614 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 // copy the file and keep going
616 FileSpec dst_file = rc_baton->dst;
617 if (!dst_file.GetFilename())
618 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000619 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 if (err.Fail()) {
621 rc_baton->error.SetErrorString(err.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000622 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000624 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 } break;
626
Zachary Turner7d86ee52017-03-08 17:56:08 +0000627 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 rc_baton->error.SetErrorStringWithFormat(
629 "invalid file detected during copy: %s", src.GetPath().c_str());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000630 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 break;
632 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000633 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000634}
635
Zachary Turner97206d52017-05-12 04:51:55 +0000636Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
637 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638
639 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000640 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')",
641 src.GetPath().c_str(), dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 FileSpec fixed_dst(dst);
643
644 if (!fixed_dst.GetFilename())
645 fixed_dst.GetFilename() = src.GetFilename();
646
647 FileSpec working_dir = GetWorkingDirectory();
648
649 if (dst) {
650 if (dst.GetDirectory()) {
651 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
652 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
653 fixed_dst.GetDirectory() = dst.GetDirectory();
654 }
Adrian Prantl05097242018-04-30 16:49:04 +0000655 // If the fixed destination file doesn't have a directory yet, then we
656 // must have a relative path. We will resolve this relative path against
657 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658 if (!fixed_dst.GetDirectory()) {
659 FileSpec relative_spec;
660 std::string path;
661 if (working_dir) {
662 relative_spec = working_dir;
663 relative_spec.AppendPathComponent(dst.GetPath());
664 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
665 } else {
666 error.SetErrorStringWithFormat(
667 "platform working directory must be valid for relative path '%s'",
668 dst.GetPath().c_str());
669 return error;
670 }
671 }
672 } else {
673 if (working_dir) {
674 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
675 } else {
676 error.SetErrorStringWithFormat(
677 "platform working directory must be valid for relative path '%s'",
678 dst.GetPath().c_str());
679 return error;
680 }
681 }
682 } else {
683 if (working_dir) {
684 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
685 } else {
686 error.SetErrorStringWithFormat("platform working directory must be valid "
687 "when destination directory is empty");
688 return error;
689 }
690 }
691
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000692 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
693 src.GetPath().c_str(), dst.GetPath().c_str(),
694 fixed_dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695
696 if (GetSupportsRSync()) {
697 error = PutFile(src, dst);
698 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000699 namespace fs = llvm::sys::fs;
700 switch (fs::get_file_type(src.GetPath(), false)) {
701 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000702 llvm::sys::fs::remove(fixed_dst.GetPath());
Jonas Devlieghere7c5310b2018-11-01 15:47:33 +0000703 uint32_t permissions = FileSystem::Instance().GetPermissions(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 if (permissions == 0)
705 permissions = eFilePermissionsDirectoryDefault;
706 error = MakeDirectory(fixed_dst, permissions);
707 if (error.Success()) {
708 // Make a filespec that only fills in the directory of a FileSpec so
709 // when we enumerate we can quickly fill in the filename for dst copies
710 FileSpec recurse_dst;
711 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
712 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000713 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000714 FileSystem::Instance().EnumerateDirectory(
715 src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716 return baton.error;
717 }
718 } break;
719
Zachary Turner7d86ee52017-03-08 17:56:08 +0000720 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000721 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000722 error = PutFile(src, fixed_dst);
723 break;
724
Zachary Turner7d86ee52017-03-08 17:56:08 +0000725 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000726 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727 FileSpec src_resolved;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000728 error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 if (error.Success())
730 error = CreateSymlink(dst, src_resolved);
731 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000732 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 error.SetErrorString("platform install doesn't handle pipes");
734 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000735 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 error.SetErrorString("platform install doesn't handle sockets");
737 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000738 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 error.SetErrorString(
740 "platform install doesn't handle non file or directory items");
741 break;
742 }
743 }
744 return error;
745}
746
747bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
748 if (IsHost()) {
749 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000750 LLDB_LOG(log, "{0}", file_spec);
751 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
752 LLDB_LOG(log, "error: {0}", ec.message());
753 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000755 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 } else {
757 m_working_dir.Clear();
758 return SetRemoteWorkingDirectory(file_spec);
759 }
760}
761
Zachary Turner97206d52017-05-12 04:51:55 +0000762Status Platform::MakeDirectory(const FileSpec &file_spec,
763 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000765 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000767 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
769 GetPluginName().GetCString(),
770 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000771 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000773}
774
Zachary Turner97206d52017-05-12 04:51:55 +0000775Status Platform::GetFilePermissions(const FileSpec &file_spec,
776 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000777 if (IsHost()) {
778 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
779 if (Value)
780 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000781 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000782 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000783 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
785 GetPluginName().GetCString(),
786 LLVM_PRETTY_FUNCTION);
787 return error;
788 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000789}
790
Zachary Turner97206d52017-05-12 04:51:55 +0000791Status Platform::SetFilePermissions(const FileSpec &file_spec,
792 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000793 if (IsHost()) {
794 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
795 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
796 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000797 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
799 GetPluginName().GetCString(),
800 LLVM_PRETTY_FUNCTION);
801 return error;
802 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000803}
804
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805ConstString Platform::GetName() { return GetPluginName(); }
806
807const char *Platform::GetHostname() {
808 if (IsHost())
809 return "127.0.0.1";
810
811 if (m_name.empty())
812 return nullptr;
813 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000814}
815
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816ConstString Platform::GetFullNameForDylib(ConstString basename) {
817 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000818}
819
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
821 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000822 LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')",
823 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 m_working_dir = working_dir;
825 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000826}
827
Pavel Labath2272c482018-06-18 15:02:23 +0000828bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000829 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000830 // We don't need anyone setting the OS version for the host platform, we
831 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000832 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000834 // We have a remote platform, allow setting the target OS version if we
835 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 // request the remote OS version from the connected platform.
837 if (IsConnected())
838 return false;
839 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000840 // We aren't connected and we might want to set the OS version ahead of
841 // time before we connect so we can peruse files and use a local SDK or
842 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000843 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000845 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846 }
847 return false;
848}
849
Zachary Turner97206d52017-05-12 04:51:55 +0000850Status
851Platform::ResolveExecutable(const ModuleSpec &module_spec,
852 lldb::ModuleSP &exe_module_sp,
853 const FileSpecList *module_search_paths_ptr) {
854 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000855 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000856 if (module_spec.GetArchitecture().IsValid()) {
857 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
858 module_search_paths_ptr, nullptr,
859 nullptr);
860 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000861 // No valid architecture was specified, ask the platform for the
862 // architectures that we should be using (in the correct order) and see
863 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864 ModuleSpec arch_module_spec(module_spec);
865 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
866 idx, arch_module_spec.GetArchitecture());
867 ++idx) {
868 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
869 module_search_paths_ptr, nullptr,
870 nullptr);
871 // Did we find an executable using one of the
872 if (error.Success() && exe_module_sp)
873 break;
874 }
Greg Claytone996fd32011-03-08 22:40:15 +0000875 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876 } else {
877 error.SetErrorStringWithFormat("'%s' does not exist",
878 module_spec.GetFileSpec().GetPath().c_str());
879 }
880 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000881}
882
Zachary Turner97206d52017-05-12 04:51:55 +0000883Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
884 FileSpec &sym_file) {
885 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000886 if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887 sym_file = sym_spec.GetSymbolFileSpec();
888 else
889 error.SetErrorString("unable to resolve symbol file");
890 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000891}
892
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893bool Platform::ResolveRemotePath(const FileSpec &platform_path,
894 FileSpec &resolved_platform_path) {
895 resolved_platform_path = platform_path;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000896 FileSystem::Instance().Resolve(resolved_platform_path);
897 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898}
899
900const ArchSpec &Platform::GetSystemArchitecture() {
901 if (IsHost()) {
902 if (!m_system_arch.IsValid()) {
903 // We have a local host platform
904 m_system_arch = HostInfo::GetArchitecture();
905 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000906 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000908 // We have a remote platform. We can only fetch the remote system
909 // architecture if we are connected, and we don't want to do it more than
910 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000911
Kate Stoneb9c1b512016-09-06 20:57:50 +0000912 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000913
Kate Stoneb9c1b512016-09-06 20:57:50 +0000914 bool fetch = false;
915 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000916 // We have valid OS version info, check to make sure it wasn't manually
917 // set prior to connecting. If it was manually set prior to connecting,
918 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 if (is_connected && !m_system_arch_set_while_connected)
920 fetch = true;
921 } else {
922 // We don't have valid OS version info, fetch it if we are connected
923 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925
926 if (fetch) {
927 m_system_arch = GetRemoteSystemArchitecture();
928 m_system_arch_set_while_connected = m_system_arch.IsValid();
929 }
930 }
931 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000932}
933
Pavel Labath7263f1b2017-10-31 10:56:03 +0000934ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
935 if (triple.empty())
936 return ArchSpec();
937 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
938 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
939 return ArchSpec(triple);
940
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000941 if (auto kind = HostInfo::ParseArchitectureKind(triple))
942 return HostInfo::GetArchitecture(*kind);
943
Pavel Labath7263f1b2017-10-31 10:56:03 +0000944 ArchSpec compatible_arch;
945 ArchSpec raw_arch(triple);
946 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
947 return raw_arch;
948
949 if (!compatible_arch.IsValid())
950 return ArchSpec(normalized_triple);
951
952 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
953 if (normalized_triple.getVendorName().empty())
954 normalized_triple.setVendor(compatible_triple.getVendor());
955 if (normalized_triple.getOSName().empty())
956 normalized_triple.setOS(compatible_triple.getOS());
957 if (normalized_triple.getEnvironmentName().empty())
958 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
959 return ArchSpec(normalized_triple);
960}
961
Zachary Turner97206d52017-05-12 04:51:55 +0000962Status Platform::ConnectRemote(Args &args) {
963 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964 if (IsHost())
965 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
966 "the host platform and is always connected.",
967 GetPluginName().GetCString());
968 else
969 error.SetErrorStringWithFormat(
970 "Platform::ConnectRemote() is not supported by %s",
971 GetPluginName().GetCString());
972 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000973}
974
Zachary Turner97206d52017-05-12 04:51:55 +0000975Status Platform::DisconnectRemote() {
976 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 if (IsHost())
978 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
979 "the host platform and is always connected.",
980 GetPluginName().GetCString());
981 else
982 error.SetErrorStringWithFormat(
983 "Platform::DisconnectRemote() is not supported by %s",
984 GetPluginName().GetCString());
985 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000986}
Greg Clayton32e0a752011-03-30 18:16:51 +0000987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988bool Platform::GetProcessInfo(lldb::pid_t pid,
989 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000990 // Take care of the host case so that each subclass can just call this
991 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 if (IsHost())
993 return Host::GetProcessInfo(pid, process_info);
994 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000995}
996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
998 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +0000999 // Take care of the host case so that each subclass can just call this
1000 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 uint32_t match_count = 0;
1002 if (IsHost())
1003 match_count = Host::FindProcesses(match_info, process_infos);
1004 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001005}
Greg Clayton8b82f082011-04-12 05:54:46 +00001006
Zachary Turner97206d52017-05-12 04:51:55 +00001007Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1008 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001010 LLDB_LOGF(log, "Platform::%s()", __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011
Adrian Prantl05097242018-04-30 16:49:04 +00001012 // Take care of the host case so that each subclass can just call this
1013 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 if (IsHost()) {
1015 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1016 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1017
1018 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1019 const bool is_localhost = true;
1020 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1021 const bool first_arg_is_full_shell_command = false;
1022 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1023 if (log) {
1024 const FileSpec &shell = launch_info.GetShell();
Jason Molenda3b687612019-02-08 23:36:25 +00001025 std::string shell_str = (shell) ? shell.GetPath() : "<null>";
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001026 LLDB_LOGF(log,
1027 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1028 ", shell is '%s'",
1029 __FUNCTION__, num_resumes, shell_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 }
1031
1032 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1033 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1034 num_resumes))
1035 return error;
1036 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1037 error = ShellExpandArguments(launch_info);
1038 if (error.Fail()) {
1039 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1040 "consider launching with 'process "
1041 "launch'.",
1042 error.AsCString("unknown"));
1043 return error;
1044 }
1045 }
1046
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001047 LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32,
1048 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 error = Host::LaunchProcess(launch_info);
1051 } else
1052 error.SetErrorString(
1053 "base lldb_private::Platform class can't launch remote processes");
1054 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001055}
1056
Zachary Turner97206d52017-05-12 04:51:55 +00001057Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 if (IsHost())
1059 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001060 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001061}
1062
Zachary Turner97206d52017-05-12 04:51:55 +00001063Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001065 LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001066
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001068 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 size_t num_debuggers = Debugger::GetNumDebuggers();
1070 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1071 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1072 lldb_private::TargetList &targets = debugger->GetTargetList();
1073 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1074 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1075 if (process->GetID() == pid)
1076 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001077 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001079
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001081 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 "base lldb_private::Platform class can't kill remote processes unless "
1083 "they are controlled by a process plugin");
1084 }
1085 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001086 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001087}
1088
Greg Clayton8b82f082011-04-12 05:54:46 +00001089lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1091 Target *target, // Can be nullptr, if nullptr create a
1092 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001093 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001095 LLDB_LOGF(log, "Platform::%s entered (target %p)", __FUNCTION__,
1096 static_cast<void *>(target));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097
1098 ProcessSP process_sp;
1099 // Make sure we stop at the entry point
1100 launch_info.GetFlags().Set(eLaunchFlagDebug);
1101 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001102 // group, since then we can handle ^C interrupts ourselves w/o having to
1103 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 launch_info.SetLaunchInSeparateProcessGroup(true);
1105
1106 // Allow any StructuredData process-bound plugins to adjust the launch info
1107 // if needed
1108 size_t i = 0;
1109 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001110 // Note iteration can't simply go until a nullptr callback is returned, as it
1111 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1113 for (auto filter_callback = get_filter_func(i, iteration_complete);
1114 !iteration_complete;
1115 filter_callback = get_filter_func(++i, iteration_complete)) {
1116 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001117 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 error = (*filter_callback)(launch_info, target);
1119 if (!error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001120 LLDB_LOGF(log,
1121 "Platform::%s() StructuredDataPlugin launch "
1122 "filter failed.",
1123 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 return process_sp;
1125 }
1126 }
1127 }
1128
1129 error = LaunchProcess(launch_info);
1130 if (error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001131 LLDB_LOGF(log,
1132 "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")",
1133 __FUNCTION__, launch_info.GetProcessID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1135 ProcessAttachInfo attach_info(launch_info);
1136 process_sp = Attach(attach_info, debugger, target, error);
1137 if (process_sp) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001138 LLDB_LOGF(log, "Platform::%s Attach() succeeded, Process plugin: %s",
1139 __FUNCTION__, process_sp->GetPluginName().AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001141
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 // Since we attached to the process, it will think it needs to detach
1143 // if the process object just goes away without an explicit call to
1144 // Process::Kill() or Process::Detach(), so let it know to kill the
1145 // process if this happens.
1146 process_sp->SetShouldDetach(false);
1147
Adrian Prantl05097242018-04-30 16:49:04 +00001148 // If we didn't have any file actions, the pseudo terminal might have
1149 // been used where the slave side was given as the file to open for
1150 // stdin/out/err after we have already opened the master so we can
1151 // read/write stdin/out/err.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001153 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 process_sp->SetSTDIOFileDescriptor(pty_fd);
1155 }
1156 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001157 LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__,
1158 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 }
1160 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001161 LLDB_LOGF(log,
1162 "Platform::%s LaunchProcess() returned launch_info with "
1163 "invalid process id",
1164 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165 }
1166 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001167 LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1168 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 }
1170
1171 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001172}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001173
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001174lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001175Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1176 ArchSpec *platform_arch_ptr) {
1177 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001178 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 if (arch.IsValid())
1180 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1181 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001182}
1183
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001184/// Lets a platform answer if it is compatible with a given
1185/// architecture and the target triple contained within.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1187 bool exact_arch_match,
1188 ArchSpec *compatible_arch_ptr) {
1189 // If the architecture is invalid, we must answer true...
1190 if (arch.IsValid()) {
1191 ArchSpec platform_arch;
1192 // Try for an exact architecture match first.
1193 if (exact_arch_match) {
1194 for (uint32_t arch_idx = 0;
1195 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1196 ++arch_idx) {
1197 if (arch.IsExactMatch(platform_arch)) {
1198 if (compatible_arch_ptr)
1199 *compatible_arch_ptr = platform_arch;
1200 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001201 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 }
1203 } else {
1204 for (uint32_t arch_idx = 0;
1205 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1206 ++arch_idx) {
1207 if (arch.IsCompatibleMatch(platform_arch)) {
1208 if (compatible_arch_ptr)
1209 *compatible_arch_ptr = platform_arch;
1210 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 }
1215 if (compatible_arch_ptr)
1216 compatible_arch_ptr->Clear();
1217 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001218}
1219
Zachary Turner97206d52017-05-12 04:51:55 +00001220Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1221 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001223 LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001224
Kate Stoneb9c1b512016-09-06 20:57:50 +00001225 uint32_t source_open_options =
1226 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001227 namespace fs = llvm::sys::fs;
1228 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001230
Jonas Devlieghere50bc1ed2018-11-02 22:34:51 +00001231 File source_file;
1232 Status error = FileSystem::Instance().Open(
1233 source_file, source, source_open_options, lldb::eFilePermissionsUserRW);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234 uint32_t permissions = source_file.GetPermissions(error);
1235 if (permissions == 0)
1236 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001237
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001239 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240 lldb::user_id_t dest_file = OpenFile(
1241 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1242 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1243 permissions, error);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001244 LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001245
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246 if (error.Fail())
1247 return error;
1248 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001249 return Status("unable to open target file");
Jason Molendaeb9902a2018-12-07 00:35:26 +00001250 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251 uint64_t offset = 0;
1252 for (;;) {
1253 size_t bytes_read = buffer_sp->GetByteSize();
1254 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1255 if (error.Fail() || bytes_read == 0)
1256 break;
1257
1258 const uint64_t bytes_written =
1259 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001260 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001262
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 offset += bytes_written;
1264 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001265 // We didn't write the correct number of bytes, so adjust the file
1266 // position in the source file we are reading from...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001268 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 }
1270 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001271
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001273 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274
1275 // TODO: ChownFile?
1276
1277 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001278}
1279
Zachary Turner97206d52017-05-12 04:51:55 +00001280Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1281 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001283}
1284
Zachary Turner97206d52017-05-12 04:51:55 +00001285Status
1286Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1287 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001288{
Zachary Turner97206d52017-05-12 04:51:55 +00001289 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001291}
1292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1294 return false;
1295}
1296
Zachary Turner97206d52017-05-12 04:51:55 +00001297Status Platform::Unlink(const FileSpec &path) {
1298 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 return error;
1300}
1301
Ed Maste37c40af2017-08-16 12:55:02 +00001302MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1303 addr_t length, unsigned prot,
1304 unsigned flags, addr_t fd,
1305 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306 uint64_t flags_platform = 0;
1307 if (flags & eMmapFlagsPrivate)
1308 flags_platform |= MAP_PRIVATE;
1309 if (flags & eMmapFlagsAnon)
1310 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001311
1312 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1313 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314}
1315
Zachary Turner97206d52017-05-12 04:51:55 +00001316lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 const char *command, // Shouldn't be nullptr
1318 const FileSpec &
1319 working_dir, // Pass empty FileSpec to use the current working directory
1320 int *status_ptr, // Pass nullptr if you don't want the process exit status
1321 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1322 // process to exit
1323 std::string
1324 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001325 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 if (IsHost())
1327 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
Pavel Labath19dd1a02018-05-10 10:46:03 +00001328 command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 else
Zachary Turner97206d52017-05-12 04:51:55 +00001330 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331}
1332
1333bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1334 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001335 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001336 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001337 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1338 if (!Result)
1339 return false;
1340 std::tie(high, low) = Result->words();
1341 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001342}
1343
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344void Platform::SetLocalCacheDirectory(const char *local) {
1345 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001346}
1347
Kate Stoneb9c1b512016-09-06 20:57:50 +00001348const char *Platform::GetLocalCacheDirectory() {
1349 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001350}
Greg Claytonfbb76342013-11-20 21:07:01 +00001351
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001352static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001354 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001356 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 "Platform-specific options required for rsync to work."},
1358 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001359 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360 "Platform-specific rsync prefix put before the remote path."},
1361 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001362 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363 "Do not automatically fill in the remote hostname when composing the "
1364 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001365};
1366
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001367static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001369 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001371 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001372 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001373};
1374
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001375static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001377 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001379};
1380
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001381llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001382 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001383}
1384
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385void OptionGroupPlatformRSync::OptionParsingStarting(
1386 ExecutionContext *execution_context) {
1387 m_rsync = false;
1388 m_rsync_opts.clear();
1389 m_rsync_prefix.clear();
1390 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001391}
1392
Zachary Turner97206d52017-05-12 04:51:55 +00001393lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001394OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001395 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001397 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 char short_option = (char)GetDefinitions()[option_idx].short_option;
1399 switch (short_option) {
1400 case 'r':
1401 m_rsync = true;
1402 break;
1403
1404 case 'R':
1405 m_rsync_opts.assign(option_arg);
1406 break;
1407
1408 case 'P':
1409 m_rsync_prefix.assign(option_arg);
1410 break;
1411
1412 case 'i':
1413 m_ignores_remote_hostname = true;
1414 break;
1415
1416 default:
1417 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1418 break;
1419 }
1420
1421 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001422}
1423
Greg Clayton4116e932012-05-15 02:33:01 +00001424lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1426 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001427}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001428
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001429llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001430 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001431}
1432
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433void OptionGroupPlatformSSH::OptionParsingStarting(
1434 ExecutionContext *execution_context) {
1435 m_ssh = false;
1436 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001437}
1438
Zachary Turner97206d52017-05-12 04:51:55 +00001439lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001440OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001441 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001443 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 char short_option = (char)GetDefinitions()[option_idx].short_option;
1445 switch (short_option) {
1446 case 's':
1447 m_ssh = true;
1448 break;
1449
1450 case 'S':
1451 m_ssh_opts.assign(option_arg);
1452 break;
1453
1454 default:
1455 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1456 break;
1457 }
1458
1459 return error;
1460}
1461
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001462llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001463 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464}
1465
1466void OptionGroupPlatformCaching::OptionParsingStarting(
1467 ExecutionContext *execution_context) {
1468 m_cache_dir.clear();
1469}
1470
Zachary Turner97206d52017-05-12 04:51:55 +00001471lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001472 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001474 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 char short_option = (char)GetDefinitions()[option_idx].short_option;
1476 switch (short_option) {
1477 case 'c':
1478 m_cache_dir.assign(option_arg);
1479 break;
1480
1481 default:
1482 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1483 break;
1484 }
1485
1486 return error;
1487}
1488
Pavel Labath62930e52018-01-10 11:57:31 +00001489Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001490
1491const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1492 if (!m_calculated_trap_handlers) {
1493 std::lock_guard<std::mutex> guard(m_mutex);
1494 if (!m_calculated_trap_handlers) {
1495 CalculateTrapHandlerSymbolNames();
1496 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001497 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 }
1499 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001500}
1501
Zachary Turner97206d52017-05-12 04:51:55 +00001502Status Platform::GetCachedExecutable(
1503 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1504 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 const auto platform_spec = module_spec.GetFileSpec();
1506 const auto error = LoadCachedExecutable(
1507 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1508 if (error.Success()) {
1509 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1510 module_spec.GetPlatformFileSpec() = platform_spec;
1511 }
1512
1513 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001514}
1515
Zachary Turner97206d52017-05-12 04:51:55 +00001516Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1518 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1519 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1520 [&](const ModuleSpec &spec) {
1521 return remote_platform.ResolveExecutable(
1522 spec, module_sp, module_search_paths_ptr);
1523 },
1524 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001525}
1526
Zachary Turner97206d52017-05-12 04:51:55 +00001527Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1528 Process *process,
1529 lldb::ModuleSP &module_sp,
1530 const ModuleResolver &module_resolver,
1531 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001532 // Get module information from a target.
1533 ModuleSpec resolved_module_spec;
1534 bool got_module_spec = false;
1535 if (process) {
1536 // Try to get module information from the process
1537 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1538 module_spec.GetArchitecture(),
1539 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001540 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1542 got_module_spec = true;
1543 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001544 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001546
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001547 if (!module_spec.GetArchitecture().IsValid()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001548 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001549 // No valid architecture was specified, ask the platform for the
1550 // architectures that we should be using (in the correct order) and see if
1551 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001552 ModuleSpec arch_module_spec(module_spec);
1553 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1554 idx, arch_module_spec.GetArchitecture());
1555 ++idx) {
1556 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1557 nullptr, nullptr);
1558 // Did we find an executable using one of the
1559 if (error.Success() && module_sp)
1560 break;
1561 }
1562 if (module_sp)
1563 got_module_spec = true;
1564 }
1565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001567 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1569 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001570 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1572 return module_resolver(module_spec);
1573 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001574 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001576
Kate Stoneb9c1b512016-09-06 20:57:50 +00001577 // If we are looking for a specific UUID, make sure resolved_module_spec has
1578 // the same one before we search.
1579 if (module_spec.GetUUID().IsValid()) {
1580 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1581 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001582
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 // Trying to find a module by UUID on local file system.
1584 const auto error = module_resolver(resolved_module_spec);
1585 if (error.Fail()) {
1586 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001587 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001589
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001591}
1592
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1594 lldb::ModuleSP &module_sp,
1595 bool *did_create_ptr) {
1596 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1597 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001598 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599
1600 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1601
1602 // Check local cache for a module.
1603 auto error = m_module_cache->GetAndPut(
1604 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1605 [this](const ModuleSpec &module_spec,
1606 const FileSpec &tmp_download_file_spec) {
1607 return DownloadModuleSlice(
1608 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1609 module_spec.GetObjectSize(), tmp_download_file_spec);
1610
1611 },
1612 [this](const ModuleSP &module_sp,
1613 const FileSpec &tmp_download_file_spec) {
1614 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1615 },
1616 module_sp, did_create_ptr);
1617 if (error.Success())
1618 return true;
1619
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001620 LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s",
1621 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1622 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001624}
1625
Zachary Turner97206d52017-05-12 04:51:55 +00001626Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1627 const uint64_t src_offset,
1628 const uint64_t src_size,
1629 const FileSpec &dst_file_spec) {
1630 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001631
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001632 std::error_code EC;
1633 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1634 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001635 error.SetErrorStringWithFormat("unable to open destination file: %s",
1636 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001637 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001639
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1641 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001642
Kate Stoneb9c1b512016-09-06 20:57:50 +00001643 if (error.Fail()) {
1644 error.SetErrorStringWithFormat("unable to open source file: %s",
1645 error.AsCString());
1646 return error;
1647 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001648
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 std::vector<char> buffer(1024);
1650 auto offset = src_offset;
1651 uint64_t total_bytes_read = 0;
1652 while (total_bytes_read < src_size) {
1653 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1654 src_size - total_bytes_read);
1655 const uint64_t n_read =
1656 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001657 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 break;
1659 if (n_read == 0) {
1660 error.SetErrorString("read 0 bytes");
1661 break;
1662 }
1663 offset += n_read;
1664 total_bytes_read += n_read;
1665 dst.write(&buffer[0], n_read);
1666 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001667
Zachary Turner97206d52017-05-12 04:51:55 +00001668 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669 CloseFile(src_fd, close_error); // Ignoring close error.
1670
1671 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001672}
1673
Zachary Turner97206d52017-05-12 04:51:55 +00001674Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1675 const FileSpec &dst_file_spec) {
1676 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001678}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001679
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680FileSpec Platform::GetModuleCacheRoot() {
1681 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1682 dir_spec.AppendPathComponent(GetName().AsCString());
1683 return dir_spec;
1684}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001685
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001687
Kate Stoneb9c1b512016-09-06 20:57:50 +00001688const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1689 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1690 return s_default_unix_signals_sp;
1691}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001692
Zachary Turnera89ce432019-03-06 18:20:23 +00001693UnixSignalsSP Platform::GetUnixSignals() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001694 if (IsHost())
Zachary Turnera89ce432019-03-06 18:20:23 +00001695 return UnixSignals::CreateForHost();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696 return GetRemoteUnixSignals();
1697}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001698
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699uint32_t Platform::LoadImage(lldb_private::Process *process,
1700 const lldb_private::FileSpec &local_file,
1701 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001702 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001703 if (local_file && remote_file) {
1704 // Both local and remote file was specified. Install the local file to the
1705 // given location.
1706 if (IsRemote() || local_file != remote_file) {
1707 error = Install(local_file, remote_file);
1708 if (error.Fail())
1709 return LLDB_INVALID_IMAGE_TOKEN;
1710 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001711 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001713
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714 if (local_file) {
1715 // Only local file was specified. Install it to the current working
1716 // directory.
1717 FileSpec target_file = GetWorkingDirectory();
1718 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1719 if (IsRemote() || local_file != target_file) {
1720 error = Install(local_file, target_file);
1721 if (error.Fail())
1722 return LLDB_INVALID_IMAGE_TOKEN;
1723 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001724 return DoLoadImage(process, target_file, nullptr, error);
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001725 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001726
Kate Stoneb9c1b512016-09-06 20:57:50 +00001727 if (remote_file) {
1728 // Only remote file was specified so we don't have to do any copying
Jim Ingham0d231f72018-06-28 20:02:11 +00001729 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001731
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 error.SetErrorString("Neither local nor remote file was specified");
1733 return LLDB_INVALID_IMAGE_TOKEN;
1734}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001735
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1737 const lldb_private::FileSpec &remote_file,
Jim Ingham0d231f72018-06-28 20:02:11 +00001738 const std::vector<std::string> *paths,
1739 lldb_private::Status &error,
1740 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 error.SetErrorString("LoadImage is not supported on the current platform");
1742 return LLDB_INVALID_IMAGE_TOKEN;
1743}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001744
Jim Ingham0d231f72018-06-28 20:02:11 +00001745uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1746 const lldb_private::FileSpec &remote_filename,
1747 const std::vector<std::string> &paths,
1748 lldb_private::Status &error,
1749 lldb_private::FileSpec *loaded_path)
1750{
1751 FileSpec file_to_use;
1752 if (remote_filename.IsAbsolute())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001753 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1754
1755 remote_filename.GetPathStyle());
Jim Ingham0d231f72018-06-28 20:02:11 +00001756 else
1757 file_to_use = remote_filename;
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001758
Jim Ingham0d231f72018-06-28 20:02:11 +00001759 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1760}
1761
Zachary Turner97206d52017-05-12 04:51:55 +00001762Status Platform::UnloadImage(lldb_private::Process *process,
1763 uint32_t image_token) {
1764 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001765}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001766
Zachary Turner31659452016-11-17 21:15:14 +00001767lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1768 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769 lldb_private::Debugger &debugger,
1770 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001771 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001773
Kate Stoneb9c1b512016-09-06 20:57:50 +00001774 if (!target) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001775 ArchSpec arch;
1776 if (target && target->GetArchitecture().IsValid())
1777 arch = target->GetArchitecture();
1778 else
1779 arch = Target::GetDefaultArchitecture();
1780
1781 const char *triple = "";
1782 if (arch.IsValid())
1783 triple = arch.GetTriple().getTriple().c_str();
1784
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001786 error = debugger.GetTargetList().CreateTarget(
Jason Molendadf9f7962018-10-23 23:45:56 +00001787 debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 target = new_target_sp.get();
1789 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 if (!target || error.Fail())
1792 return nullptr;
1793
1794 debugger.GetTargetList().SetSelectedTarget(target);
1795
1796 lldb::ProcessSP process_sp =
1797 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1798 if (!process_sp)
1799 return nullptr;
1800
1801 error =
1802 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1803 if (error.Fail())
1804 return nullptr;
1805
1806 return process_sp;
1807}
1808
1809size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001810 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 error.Clear();
1812 return 0;
1813}
1814
1815size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1816 BreakpointSite *bp_site) {
1817 ArchSpec arch = target.GetArchitecture();
1818 const uint8_t *trap_opcode = nullptr;
1819 size_t trap_opcode_size = 0;
1820
1821 switch (arch.GetMachine()) {
1822 case llvm::Triple::aarch64: {
1823 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1824 trap_opcode = g_aarch64_opcode;
1825 trap_opcode_size = sizeof(g_aarch64_opcode);
1826 } break;
1827
1828 // TODO: support big-endian arm and thumb trap codes.
1829 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001830 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1831 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001832 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1833 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1834
1835 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001836 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001837
1838 if (bp_loc_sp) {
1839 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001840 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001841 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001842 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001843 }
1844
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001845 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001846 trap_opcode = g_thumb_breakpoint_opcode;
1847 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1848 } else {
1849 trap_opcode = g_arm_breakpoint_opcode;
1850 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1851 }
1852 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001853
Kate Stoneb9c1b512016-09-06 20:57:50 +00001854 case llvm::Triple::mips:
1855 case llvm::Triple::mips64: {
1856 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1857 trap_opcode = g_hex_opcode;
1858 trap_opcode_size = sizeof(g_hex_opcode);
1859 } break;
1860
1861 case llvm::Triple::mipsel:
1862 case llvm::Triple::mips64el: {
1863 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1864 trap_opcode = g_hex_opcode;
1865 trap_opcode_size = sizeof(g_hex_opcode);
1866 } break;
1867
1868 case llvm::Triple::systemz: {
1869 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1870 trap_opcode = g_hex_opcode;
1871 trap_opcode_size = sizeof(g_hex_opcode);
1872 } break;
1873
1874 case llvm::Triple::hexagon: {
1875 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1876 trap_opcode = g_hex_opcode;
1877 trap_opcode_size = sizeof(g_hex_opcode);
1878 } break;
1879
1880 case llvm::Triple::ppc:
1881 case llvm::Triple::ppc64: {
1882 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1883 trap_opcode = g_ppc_opcode;
1884 trap_opcode_size = sizeof(g_ppc_opcode);
1885 } break;
1886
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001887 case llvm::Triple::ppc64le: {
1888 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1889 trap_opcode = g_ppc64le_opcode;
1890 trap_opcode_size = sizeof(g_ppc64le_opcode);
1891 } break;
1892
Kate Stoneb9c1b512016-09-06 20:57:50 +00001893 case llvm::Triple::x86:
1894 case llvm::Triple::x86_64: {
1895 static const uint8_t g_i386_opcode[] = {0xCC};
1896 trap_opcode = g_i386_opcode;
1897 trap_opcode_size = sizeof(g_i386_opcode);
1898 } break;
1899
1900 default:
David Blaikiea322f362017-01-06 00:38:06 +00001901 llvm_unreachable(
1902 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903 }
1904
1905 assert(bp_site);
1906 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1907 return trap_opcode_size;
1908
1909 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001910}