blob: 79ef63881359b5369f303d8b8467797cc76c86f5 [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[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +000068 {}, "Use module cache."},
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
Tatyana Krasnukhae40db052018-09-27 07:11:58 +000070 {}, "Root directory for cached modules."}};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072enum { ePropertyUseModuleCache, ePropertyModuleCacheDirectory };
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076ConstString PlatformProperties::GetSettingName() {
77 static ConstString g_setting_name("platform");
78 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081PlatformProperties::PlatformProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +000082 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 auto module_cache_dir = GetModuleCacheDirectory();
86 if (module_cache_dir)
87 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 llvm::SmallString<64> user_home_dir;
90 if (!llvm::sys::path::home_directory(user_home_dir))
91 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000092
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000093 module_cache_dir = FileSpec(user_home_dir.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 module_cache_dir.AppendPathComponent(".lldb");
95 module_cache_dir.AppendPathComponent("module_cache");
96 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099bool PlatformProperties::GetUseModuleCache() const {
100 const auto idx = ePropertyUseModuleCache;
101 return m_collection_sp->GetPropertyAtIndexAsBoolean(
102 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000103}
104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
106 return m_collection_sp->SetPropertyAtIndexAsBoolean(
107 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110FileSpec PlatformProperties::GetModuleCacheDirectory() const {
111 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
112 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
116 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
117 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000118}
119
Greg Claytone996fd32011-03-08 22:40:15 +0000120//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000122///
123/// There should only be one of these for each host that LLDB runs
124/// upon that should be statically compiled in and registered using
125/// preprocessor macros or other similar build mechanisms.
126///
127/// This platform will be used as the default platform when launching
128/// or attaching to processes unless another platform is specified.
129//------------------------------------------------------------------
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//------------------------------------------------------------------
381/// Default Constructor
382//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000383Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 : m_is_host(is_host), m_os_version_set_while_connected(false),
385 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000386 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000387 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
388 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
389 m_ignores_remote_hostname(false), m_trap_handlers(),
390 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 m_module_cache(llvm::make_unique<ModuleCache>()) {
392 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
393 if (log)
394 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000395}
396
397//------------------------------------------------------------------
398/// Destructor.
399///
400/// The destructor is virtual since this class is designed to be
401/// inherited from by the plug-in instance.
402//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403Platform::~Platform() {
404 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
405 if (log)
406 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000407}
408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 std::string s;
411 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000412
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 ArchSpec arch(GetSystemArchitecture());
414 if (arch.IsValid()) {
415 if (!arch.GetTriple().str().empty()) {
416 strm.Printf(" Triple: ");
417 arch.DumpTriple(strm);
418 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000419 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000421
Pavel Labath2272c482018-06-18 15:02:23 +0000422 llvm::VersionTuple os_version = GetOSVersion();
423 if (!os_version.empty()) {
424 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (GetOSBuildString(s))
427 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 strm.EOL();
430 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 if (GetOSKernelDescription(s))
433 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 if (IsHost()) {
436 strm.Printf(" Hostname: %s\n", GetHostname());
437 } else {
438 const bool is_connected = IsConnected();
439 if (is_connected)
440 strm.Printf(" Hostname: %s\n", GetHostname());
441 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
442 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 if (GetWorkingDirectory()) {
445 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
446 }
447 if (!IsConnected())
448 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 std::string specific_info(GetPlatformSpecificConnectionInformation());
451
452 if (!specific_info.empty())
453 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000454}
455
Pavel Labath2272c482018-06-18 15:02:23 +0000456llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000458
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000460 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000462 m_os_version = HostInfo::GetOSVersion();
463 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000464 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 } else {
466 // We have a remote platform. We can only fetch the remote
467 // OS version if we are connected, and we don't want to do it
468 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000471
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000473 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000474 // We have valid OS version info, check to make sure it wasn't manually
475 // set prior to connecting. If it was manually set prior to connecting,
476 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 if (is_connected && !m_os_version_set_while_connected)
478 fetch = true;
479 } else {
480 // We don't have valid OS version info, fetch it if we are connected
481 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000482 }
483
Pavel Labath2272c482018-06-18 15:02:23 +0000484 if (fetch)
485 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 }
487
Pavel Labath2272c482018-06-18 15:02:23 +0000488 if (!m_os_version.empty())
489 return m_os_version;
490 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000491 // Check with the process in case it can answer the question if a process
492 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000493 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 }
Pavel Labath2272c482018-06-18 15:02:23 +0000495 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000496}
Greg Clayton1cb64962011-03-24 04:28:38 +0000497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498bool Platform::GetOSBuildString(std::string &s) {
499 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000500
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000502#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000504#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000506#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 else
508 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000509}
510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511bool Platform::GetOSKernelDescription(std::string &s) {
512 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000513#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000515#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000517#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 else
519 return GetRemoteOSKernelDescription(s);
520}
521
522void Platform::AddClangModuleCompilationOptions(
523 Target *target, std::vector<std::string> &options) {
524 std::vector<std::string> default_compilation_options = {
525 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
526
527 options.insert(options.end(), default_compilation_options.begin(),
528 default_compilation_options.end());
529}
530
531FileSpec Platform::GetWorkingDirectory() {
532 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000533 llvm::SmallString<64> cwd;
534 if (llvm::sys::fs::current_path(cwd))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000535 return {};
536 else {
537 FileSpec file_spec(cwd);
538 FileSystem::Instance().Resolve(file_spec);
539 return file_spec;
540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000541 } else {
542 if (!m_working_dir)
543 m_working_dir = GetRemoteWorkingDirectory();
544 return m_working_dir;
545 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000546}
547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548struct RecurseCopyBaton {
549 const FileSpec &dst;
550 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000551 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000552};
553
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000554static FileSystem::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000555RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000556 llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000558 FileSpec src(path);
Zachary Turner7d86ee52017-03-08 17:56:08 +0000559 namespace fs = llvm::sys::fs;
560 switch (ft) {
561 case fs::file_type::fifo_file:
562 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 // we have no way to copy pipes and sockets - ignore them and continue
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000564 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000566
Zachary Turner7d86ee52017-03-08 17:56:08 +0000567 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 // make the new directory and get in there
569 FileSpec dst_dir = rc_baton->dst;
570 if (!dst_dir.GetFilename())
571 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000572 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 dst_dir, lldb::eFilePermissionsDirectoryDefault);
574 if (error.Fail()) {
575 rc_baton->error.SetErrorStringWithFormat(
576 "unable to setup directory %s on remote end", dst_dir.GetCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000577 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000578 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579
580 // now recurse
581 std::string src_dir_path(src.GetPath());
582
Adrian Prantl05097242018-04-30 16:49:04 +0000583 // Make a filespec that only fills in the directory of a FileSpec so when
584 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 FileSpec recurse_dst;
586 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000587 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
588 Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000589 FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
590 RecurseCopy_Callback, &rc_baton2);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 if (rc_baton2.error.Fail()) {
592 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000593 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000595 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 } break;
597
Zachary Turner7d86ee52017-03-08 17:56:08 +0000598 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 // copy the file and keep going
600 FileSpec dst_file = rc_baton->dst;
601 if (!dst_file.GetFilename())
602 dst_file.GetFilename() = src.GetFilename();
603
604 FileSpec src_resolved;
605
Jonas Devlieghere46376962018-10-31 21:49:27 +0000606 rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607
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
611 rc_baton->error =
612 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
613
614 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000615 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000617 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 } break;
619
Zachary Turner7d86ee52017-03-08 17:56:08 +0000620 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 // copy the file and keep going
622 FileSpec dst_file = rc_baton->dst;
623 if (!dst_file.GetFilename())
624 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000625 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626 if (err.Fail()) {
627 rc_baton->error.SetErrorString(err.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000628 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000630 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 } break;
632
Zachary Turner7d86ee52017-03-08 17:56:08 +0000633 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634 rc_baton->error.SetErrorStringWithFormat(
635 "invalid file detected during copy: %s", src.GetPath().c_str());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000636 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637 break;
638 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000639 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000640}
641
Zachary Turner97206d52017-05-12 04:51:55 +0000642Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
643 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644
645 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
646 if (log)
647 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
648 dst.GetPath().c_str());
649 FileSpec fixed_dst(dst);
650
651 if (!fixed_dst.GetFilename())
652 fixed_dst.GetFilename() = src.GetFilename();
653
654 FileSpec working_dir = GetWorkingDirectory();
655
656 if (dst) {
657 if (dst.GetDirectory()) {
658 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
659 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
660 fixed_dst.GetDirectory() = dst.GetDirectory();
661 }
Adrian Prantl05097242018-04-30 16:49:04 +0000662 // If the fixed destination file doesn't have a directory yet, then we
663 // must have a relative path. We will resolve this relative path against
664 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 if (!fixed_dst.GetDirectory()) {
666 FileSpec relative_spec;
667 std::string path;
668 if (working_dir) {
669 relative_spec = working_dir;
670 relative_spec.AppendPathComponent(dst.GetPath());
671 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
672 } else {
673 error.SetErrorStringWithFormat(
674 "platform working directory must be valid for relative path '%s'",
675 dst.GetPath().c_str());
676 return error;
677 }
678 }
679 } else {
680 if (working_dir) {
681 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
682 } else {
683 error.SetErrorStringWithFormat(
684 "platform working directory must be valid for relative path '%s'",
685 dst.GetPath().c_str());
686 return error;
687 }
688 }
689 } else {
690 if (working_dir) {
691 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
692 } else {
693 error.SetErrorStringWithFormat("platform working directory must be valid "
694 "when destination directory is empty");
695 return error;
696 }
697 }
698
699 if (log)
700 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
701 src.GetPath().c_str(), dst.GetPath().c_str(),
702 fixed_dst.GetPath().c_str());
703
704 if (GetSupportsRSync()) {
705 error = PutFile(src, dst);
706 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000707 namespace fs = llvm::sys::fs;
708 switch (fs::get_file_type(src.GetPath(), false)) {
709 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000710 llvm::sys::fs::remove(fixed_dst.GetPath());
Jonas Devlieghere7c5310b2018-11-01 15:47:33 +0000711 uint32_t permissions = FileSystem::Instance().GetPermissions(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 if (permissions == 0)
713 permissions = eFilePermissionsDirectoryDefault;
714 error = MakeDirectory(fixed_dst, permissions);
715 if (error.Success()) {
716 // Make a filespec that only fills in the directory of a FileSpec so
717 // when we enumerate we can quickly fill in the filename for dst copies
718 FileSpec recurse_dst;
719 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
720 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000721 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000722 FileSystem::Instance().EnumerateDirectory(
723 src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 return baton.error;
725 }
726 } break;
727
Zachary Turner7d86ee52017-03-08 17:56:08 +0000728 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000729 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 error = PutFile(src, fixed_dst);
731 break;
732
Zachary Turner7d86ee52017-03-08 17:56:08 +0000733 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000734 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 FileSpec src_resolved;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000736 error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 if (error.Success())
738 error = CreateSymlink(dst, src_resolved);
739 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000740 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 error.SetErrorString("platform install doesn't handle pipes");
742 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000743 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 error.SetErrorString("platform install doesn't handle sockets");
745 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000746 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 error.SetErrorString(
748 "platform install doesn't handle non file or directory items");
749 break;
750 }
751 }
752 return error;
753}
754
755bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
756 if (IsHost()) {
757 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000758 LLDB_LOG(log, "{0}", file_spec);
759 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
760 LLDB_LOG(log, "error: {0}", ec.message());
761 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000763 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 } else {
765 m_working_dir.Clear();
766 return SetRemoteWorkingDirectory(file_spec);
767 }
768}
769
Zachary Turner97206d52017-05-12 04:51:55 +0000770Status Platform::MakeDirectory(const FileSpec &file_spec,
771 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000773 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000775 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
777 GetPluginName().GetCString(),
778 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000779 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000781}
782
Zachary Turner97206d52017-05-12 04:51:55 +0000783Status Platform::GetFilePermissions(const FileSpec &file_spec,
784 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000785 if (IsHost()) {
786 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
787 if (Value)
788 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000789 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000790 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000791 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
793 GetPluginName().GetCString(),
794 LLVM_PRETTY_FUNCTION);
795 return error;
796 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000797}
798
Zachary Turner97206d52017-05-12 04:51:55 +0000799Status Platform::SetFilePermissions(const FileSpec &file_spec,
800 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000801 if (IsHost()) {
802 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
803 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
804 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000805 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
807 GetPluginName().GetCString(),
808 LLVM_PRETTY_FUNCTION);
809 return error;
810 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000811}
812
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813ConstString Platform::GetName() { return GetPluginName(); }
814
815const char *Platform::GetHostname() {
816 if (IsHost())
817 return "127.0.0.1";
818
819 if (m_name.empty())
820 return nullptr;
821 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000822}
823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824ConstString Platform::GetFullNameForDylib(ConstString basename) {
825 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000826}
827
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
829 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
830 if (log)
831 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000832 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 m_working_dir = working_dir;
834 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000835}
836
Pavel Labath2272c482018-06-18 15:02:23 +0000837bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000839 // We don't need anyone setting the OS version for the host platform, we
840 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000841 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000843 // We have a remote platform, allow setting the target OS version if we
844 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 // request the remote OS version from the connected platform.
846 if (IsConnected())
847 return false;
848 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000849 // We aren't connected and we might want to set the OS version ahead of
850 // time before we connect so we can peruse files and use a local SDK or
851 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000852 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000854 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 }
856 return false;
857}
858
Zachary Turner97206d52017-05-12 04:51:55 +0000859Status
860Platform::ResolveExecutable(const ModuleSpec &module_spec,
861 lldb::ModuleSP &exe_module_sp,
862 const FileSpecList *module_search_paths_ptr) {
863 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000864 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 if (module_spec.GetArchitecture().IsValid()) {
866 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
867 module_search_paths_ptr, nullptr,
868 nullptr);
869 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000870 // No valid architecture was specified, ask the platform for the
871 // architectures that we should be using (in the correct order) and see
872 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873 ModuleSpec arch_module_spec(module_spec);
874 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
875 idx, arch_module_spec.GetArchitecture());
876 ++idx) {
877 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
878 module_search_paths_ptr, nullptr,
879 nullptr);
880 // Did we find an executable using one of the
881 if (error.Success() && exe_module_sp)
882 break;
883 }
Greg Claytone996fd32011-03-08 22:40:15 +0000884 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 } else {
886 error.SetErrorStringWithFormat("'%s' does not exist",
887 module_spec.GetFileSpec().GetPath().c_str());
888 }
889 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000890}
891
Zachary Turner97206d52017-05-12 04:51:55 +0000892Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
893 FileSpec &sym_file) {
894 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000895 if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896 sym_file = sym_spec.GetSymbolFileSpec();
897 else
898 error.SetErrorString("unable to resolve symbol file");
899 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000900}
901
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902bool Platform::ResolveRemotePath(const FileSpec &platform_path,
903 FileSpec &resolved_platform_path) {
904 resolved_platform_path = platform_path;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000905 FileSystem::Instance().Resolve(resolved_platform_path);
906 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907}
908
909const ArchSpec &Platform::GetSystemArchitecture() {
910 if (IsHost()) {
911 if (!m_system_arch.IsValid()) {
912 // We have a local host platform
913 m_system_arch = HostInfo::GetArchitecture();
914 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000915 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000917 // We have a remote platform. We can only fetch the remote system
918 // architecture if we are connected, and we don't want to do it more than
919 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000920
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000922
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 bool fetch = false;
924 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000925 // We have valid OS version info, check to make sure it wasn't manually
926 // set prior to connecting. If it was manually set prior to connecting,
927 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928 if (is_connected && !m_system_arch_set_while_connected)
929 fetch = true;
930 } else {
931 // We don't have valid OS version info, fetch it if we are connected
932 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000933 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934
935 if (fetch) {
936 m_system_arch = GetRemoteSystemArchitecture();
937 m_system_arch_set_while_connected = m_system_arch.IsValid();
938 }
939 }
940 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000941}
942
Pavel Labath7263f1b2017-10-31 10:56:03 +0000943ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
944 if (triple.empty())
945 return ArchSpec();
946 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
947 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
948 return ArchSpec(triple);
949
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000950 if (auto kind = HostInfo::ParseArchitectureKind(triple))
951 return HostInfo::GetArchitecture(*kind);
952
Pavel Labath7263f1b2017-10-31 10:56:03 +0000953 ArchSpec compatible_arch;
954 ArchSpec raw_arch(triple);
955 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
956 return raw_arch;
957
958 if (!compatible_arch.IsValid())
959 return ArchSpec(normalized_triple);
960
961 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
962 if (normalized_triple.getVendorName().empty())
963 normalized_triple.setVendor(compatible_triple.getVendor());
964 if (normalized_triple.getOSName().empty())
965 normalized_triple.setOS(compatible_triple.getOS());
966 if (normalized_triple.getEnvironmentName().empty())
967 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
968 return ArchSpec(normalized_triple);
969}
970
Zachary Turner97206d52017-05-12 04:51:55 +0000971Status Platform::ConnectRemote(Args &args) {
972 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973 if (IsHost())
974 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
975 "the host platform and is always connected.",
976 GetPluginName().GetCString());
977 else
978 error.SetErrorStringWithFormat(
979 "Platform::ConnectRemote() is not supported by %s",
980 GetPluginName().GetCString());
981 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000982}
983
Zachary Turner97206d52017-05-12 04:51:55 +0000984Status Platform::DisconnectRemote() {
985 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 if (IsHost())
987 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
988 "the host platform and is always connected.",
989 GetPluginName().GetCString());
990 else
991 error.SetErrorStringWithFormat(
992 "Platform::DisconnectRemote() is not supported by %s",
993 GetPluginName().GetCString());
994 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000995}
Greg Clayton32e0a752011-03-30 18:16:51 +0000996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997bool Platform::GetProcessInfo(lldb::pid_t pid,
998 ProcessInstanceInfo &process_info) {
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 if (IsHost())
1002 return Host::GetProcessInfo(pid, process_info);
1003 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00001004}
1005
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1007 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +00001008 // Take care of the host case so that each subclass can just call this
1009 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 uint32_t match_count = 0;
1011 if (IsHost())
1012 match_count = Host::FindProcesses(match_info, process_infos);
1013 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001014}
Greg Clayton8b82f082011-04-12 05:54:46 +00001015
Zachary Turner97206d52017-05-12 04:51:55 +00001016Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1017 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1019 if (log)
1020 log->Printf("Platform::%s()", __FUNCTION__);
1021
Adrian Prantl05097242018-04-30 16:49:04 +00001022 // Take care of the host case so that each subclass can just call this
1023 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001024 if (IsHost()) {
1025 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1026 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1027
1028 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1029 const bool is_localhost = true;
1030 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1031 const bool first_arg_is_full_shell_command = false;
1032 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1033 if (log) {
1034 const FileSpec &shell = launch_info.GetShell();
Jason Molenda3b687612019-02-08 23:36:25 +00001035 std::string shell_str = (shell) ? shell.GetPath() : "<null>";
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 log->Printf(
1037 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1038 ", shell is '%s'",
Jason Molenda3b687612019-02-08 23:36:25 +00001039 __FUNCTION__, num_resumes, shell_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 }
1041
1042 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1043 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1044 num_resumes))
1045 return error;
1046 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1047 error = ShellExpandArguments(launch_info);
1048 if (error.Fail()) {
1049 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1050 "consider launching with 'process "
1051 "launch'.",
1052 error.AsCString("unknown"));
1053 return error;
1054 }
1055 }
1056
Todd Fialaac33cc92014-10-09 01:02:08 +00001057 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1059 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 error = Host::LaunchProcess(launch_info);
1062 } else
1063 error.SetErrorString(
1064 "base lldb_private::Platform class can't launch remote processes");
1065 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001066}
1067
Zachary Turner97206d52017-05-12 04:51:55 +00001068Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 if (IsHost())
1070 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001071 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001072}
1073
Zachary Turner97206d52017-05-12 04:51:55 +00001074Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1076 if (log)
1077 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001078
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001080 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001081 size_t num_debuggers = Debugger::GetNumDebuggers();
1082 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1083 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1084 lldb_private::TargetList &targets = debugger->GetTargetList();
1085 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1086 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1087 if (process->GetID() == pid)
1088 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001091
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001093 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 "base lldb_private::Platform class can't kill remote processes unless "
1095 "they are controlled by a process plugin");
1096 }
1097 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001098 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001099}
1100
Greg Clayton8b82f082011-04-12 05:54:46 +00001101lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1103 Target *target, // Can be nullptr, if nullptr create a
1104 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001105 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1107 if (log)
1108 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1109 static_cast<void *>(target));
1110
1111 ProcessSP process_sp;
1112 // Make sure we stop at the entry point
1113 launch_info.GetFlags().Set(eLaunchFlagDebug);
1114 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001115 // group, since then we can handle ^C interrupts ourselves w/o having to
1116 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117 launch_info.SetLaunchInSeparateProcessGroup(true);
1118
1119 // Allow any StructuredData process-bound plugins to adjust the launch info
1120 // if needed
1121 size_t i = 0;
1122 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001123 // Note iteration can't simply go until a nullptr callback is returned, as it
1124 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1126 for (auto filter_callback = get_filter_func(i, iteration_complete);
1127 !iteration_complete;
1128 filter_callback = get_filter_func(++i, iteration_complete)) {
1129 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001130 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001131 error = (*filter_callback)(launch_info, target);
1132 if (!error.Success()) {
1133 if (log)
1134 log->Printf("Platform::%s() StructuredDataPlugin launch "
1135 "filter failed.",
1136 __FUNCTION__);
1137 return process_sp;
1138 }
1139 }
1140 }
1141
1142 error = LaunchProcess(launch_info);
1143 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001144 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1146 ")",
1147 __FUNCTION__, launch_info.GetProcessID());
1148 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1149 ProcessAttachInfo attach_info(launch_info);
1150 process_sp = Attach(attach_info, debugger, target, error);
1151 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001152 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1154 __FUNCTION__, process_sp->GetPluginName().AsCString());
1155 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001156
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 // Since we attached to the process, it will think it needs to detach
1158 // if the process object just goes away without an explicit call to
1159 // Process::Kill() or Process::Detach(), so let it know to kill the
1160 // process if this happens.
1161 process_sp->SetShouldDetach(false);
1162
Adrian Prantl05097242018-04-30 16:49:04 +00001163 // If we didn't have any file actions, the pseudo terminal might have
1164 // been used where the slave side was given as the file to open for
1165 // stdin/out/err after we have already opened the master so we can
1166 // read/write stdin/out/err.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001168 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 process_sp->SetSTDIOFileDescriptor(pty_fd);
1170 }
1171 } else {
1172 if (log)
1173 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1174 error.AsCString());
1175 }
1176 } else {
1177 if (log)
1178 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1179 "invalid process id",
1180 __FUNCTION__);
1181 }
1182 } else {
1183 if (log)
1184 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1185 error.AsCString());
1186 }
1187
1188 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001189}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001190
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001191lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1193 ArchSpec *platform_arch_ptr) {
1194 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001195 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196 if (arch.IsValid())
1197 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1198 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001199}
1200
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001201//------------------------------------------------------------------
1202/// Lets a platform answer if it is compatible with a given
1203/// architecture and the target triple contained within.
1204//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1206 bool exact_arch_match,
1207 ArchSpec *compatible_arch_ptr) {
1208 // If the architecture is invalid, we must answer true...
1209 if (arch.IsValid()) {
1210 ArchSpec platform_arch;
1211 // Try for an exact architecture match first.
1212 if (exact_arch_match) {
1213 for (uint32_t arch_idx = 0;
1214 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1215 ++arch_idx) {
1216 if (arch.IsExactMatch(platform_arch)) {
1217 if (compatible_arch_ptr)
1218 *compatible_arch_ptr = platform_arch;
1219 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001220 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001221 }
1222 } else {
1223 for (uint32_t arch_idx = 0;
1224 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1225 ++arch_idx) {
1226 if (arch.IsCompatibleMatch(platform_arch)) {
1227 if (compatible_arch_ptr)
1228 *compatible_arch_ptr = platform_arch;
1229 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001230 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001232 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233 }
1234 if (compatible_arch_ptr)
1235 compatible_arch_ptr->Clear();
1236 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001237}
1238
Zachary Turner97206d52017-05-12 04:51:55 +00001239Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1240 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001241 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1242 if (log)
1243 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001244
Kate Stoneb9c1b512016-09-06 20:57:50 +00001245 uint32_t source_open_options =
1246 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001247 namespace fs = llvm::sys::fs;
1248 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001250
Jonas Devlieghere50bc1ed2018-11-02 22:34:51 +00001251 File source_file;
1252 Status error = FileSystem::Instance().Open(
1253 source_file, source, source_open_options, lldb::eFilePermissionsUserRW);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 uint32_t permissions = source_file.GetPermissions(error);
1255 if (permissions == 0)
1256 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001257
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001259 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 lldb::user_id_t dest_file = OpenFile(
1261 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1262 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1263 permissions, error);
1264 if (log)
1265 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001266
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 if (error.Fail())
1268 return error;
1269 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001270 return Status("unable to open target file");
Jason Molendaeb9902a2018-12-07 00:35:26 +00001271 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272 uint64_t offset = 0;
1273 for (;;) {
1274 size_t bytes_read = buffer_sp->GetByteSize();
1275 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1276 if (error.Fail() || bytes_read == 0)
1277 break;
1278
1279 const uint64_t bytes_written =
1280 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001281 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001283
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284 offset += bytes_written;
1285 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001286 // We didn't write the correct number of bytes, so adjust the file
1287 // position in the source file we are reading from...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001289 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 }
1291 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001294 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295
1296 // TODO: ChownFile?
1297
1298 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001299}
1300
Zachary Turner97206d52017-05-12 04:51:55 +00001301Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1302 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001304}
1305
Zachary Turner97206d52017-05-12 04:51:55 +00001306Status
1307Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1308 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001309{
Zachary Turner97206d52017-05-12 04:51:55 +00001310 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001312}
1313
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1315 return false;
1316}
1317
Zachary Turner97206d52017-05-12 04:51:55 +00001318Status Platform::Unlink(const FileSpec &path) {
1319 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 return error;
1321}
1322
Ed Maste37c40af2017-08-16 12:55:02 +00001323MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1324 addr_t length, unsigned prot,
1325 unsigned flags, addr_t fd,
1326 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 uint64_t flags_platform = 0;
1328 if (flags & eMmapFlagsPrivate)
1329 flags_platform |= MAP_PRIVATE;
1330 if (flags & eMmapFlagsAnon)
1331 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001332
1333 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1334 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335}
1336
Zachary Turner97206d52017-05-12 04:51:55 +00001337lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 const char *command, // Shouldn't be nullptr
1339 const FileSpec &
1340 working_dir, // Pass empty FileSpec to use the current working directory
1341 int *status_ptr, // Pass nullptr if you don't want the process exit status
1342 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1343 // process to exit
1344 std::string
1345 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001346 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 if (IsHost())
1348 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
Pavel Labath19dd1a02018-05-10 10:46:03 +00001349 command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350 else
Zachary Turner97206d52017-05-12 04:51:55 +00001351 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352}
1353
1354bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1355 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001356 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001357 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001358 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1359 if (!Result)
1360 return false;
1361 std::tie(high, low) = Result->words();
1362 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001363}
1364
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365void Platform::SetLocalCacheDirectory(const char *local) {
1366 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001367}
1368
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369const char *Platform::GetLocalCacheDirectory() {
1370 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001371}
Greg Claytonfbb76342013-11-20 21:07:01 +00001372
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001373static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001375 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001377 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 "Platform-specific options required for rsync to work."},
1379 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001380 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 "Platform-specific rsync prefix put before the remote path."},
1382 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001383 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 "Do not automatically fill in the remote hostname when composing the "
1385 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001386};
1387
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001388static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001390 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001392 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001394};
1395
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001396static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001398 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001400};
1401
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001402llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001403 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001404}
1405
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406void OptionGroupPlatformRSync::OptionParsingStarting(
1407 ExecutionContext *execution_context) {
1408 m_rsync = false;
1409 m_rsync_opts.clear();
1410 m_rsync_prefix.clear();
1411 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001412}
1413
Zachary Turner97206d52017-05-12 04:51:55 +00001414lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001415OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001416 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001418 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419 char short_option = (char)GetDefinitions()[option_idx].short_option;
1420 switch (short_option) {
1421 case 'r':
1422 m_rsync = true;
1423 break;
1424
1425 case 'R':
1426 m_rsync_opts.assign(option_arg);
1427 break;
1428
1429 case 'P':
1430 m_rsync_prefix.assign(option_arg);
1431 break;
1432
1433 case 'i':
1434 m_ignores_remote_hostname = true;
1435 break;
1436
1437 default:
1438 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1439 break;
1440 }
1441
1442 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001443}
1444
Greg Clayton4116e932012-05-15 02:33:01 +00001445lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1447 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001448}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001449
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001450llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001451 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001452}
1453
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454void OptionGroupPlatformSSH::OptionParsingStarting(
1455 ExecutionContext *execution_context) {
1456 m_ssh = false;
1457 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001458}
1459
Zachary Turner97206d52017-05-12 04:51:55 +00001460lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001461OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001462 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001464 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465 char short_option = (char)GetDefinitions()[option_idx].short_option;
1466 switch (short_option) {
1467 case 's':
1468 m_ssh = true;
1469 break;
1470
1471 case 'S':
1472 m_ssh_opts.assign(option_arg);
1473 break;
1474
1475 default:
1476 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1477 break;
1478 }
1479
1480 return error;
1481}
1482
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001483llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001484 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485}
1486
1487void OptionGroupPlatformCaching::OptionParsingStarting(
1488 ExecutionContext *execution_context) {
1489 m_cache_dir.clear();
1490}
1491
Zachary Turner97206d52017-05-12 04:51:55 +00001492lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001493 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001495 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 char short_option = (char)GetDefinitions()[option_idx].short_option;
1497 switch (short_option) {
1498 case 'c':
1499 m_cache_dir.assign(option_arg);
1500 break;
1501
1502 default:
1503 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1504 break;
1505 }
1506
1507 return error;
1508}
1509
Pavel Labath62930e52018-01-10 11:57:31 +00001510Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511
1512const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1513 if (!m_calculated_trap_handlers) {
1514 std::lock_guard<std::mutex> guard(m_mutex);
1515 if (!m_calculated_trap_handlers) {
1516 CalculateTrapHandlerSymbolNames();
1517 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001518 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 }
1520 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001521}
1522
Zachary Turner97206d52017-05-12 04:51:55 +00001523Status Platform::GetCachedExecutable(
1524 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1525 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 const auto platform_spec = module_spec.GetFileSpec();
1527 const auto error = LoadCachedExecutable(
1528 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1529 if (error.Success()) {
1530 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1531 module_spec.GetPlatformFileSpec() = platform_spec;
1532 }
1533
1534 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001535}
1536
Zachary Turner97206d52017-05-12 04:51:55 +00001537Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1539 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1540 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1541 [&](const ModuleSpec &spec) {
1542 return remote_platform.ResolveExecutable(
1543 spec, module_sp, module_search_paths_ptr);
1544 },
1545 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001546}
1547
Zachary Turner97206d52017-05-12 04:51:55 +00001548Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1549 Process *process,
1550 lldb::ModuleSP &module_sp,
1551 const ModuleResolver &module_resolver,
1552 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 // Get module information from a target.
1554 ModuleSpec resolved_module_spec;
1555 bool got_module_spec = false;
1556 if (process) {
1557 // Try to get module information from the process
1558 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1559 module_spec.GetArchitecture(),
1560 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001561 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001562 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1563 got_module_spec = true;
1564 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001565 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001567
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001568 if (!module_spec.GetArchitecture().IsValid()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001569 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001570 // No valid architecture was specified, ask the platform for the
1571 // architectures that we should be using (in the correct order) and see if
1572 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001573 ModuleSpec arch_module_spec(module_spec);
1574 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1575 idx, arch_module_spec.GetArchitecture());
1576 ++idx) {
1577 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1578 nullptr, nullptr);
1579 // Did we find an executable using one of the
1580 if (error.Success() && module_sp)
1581 break;
1582 }
1583 if (module_sp)
1584 got_module_spec = true;
1585 }
1586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001588 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1590 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001591 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1593 return module_resolver(module_spec);
1594 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001595 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001597
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 // If we are looking for a specific UUID, make sure resolved_module_spec has
1599 // the same one before we search.
1600 if (module_spec.GetUUID().IsValid()) {
1601 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1602 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001603
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604 // Trying to find a module by UUID on local file system.
1605 const auto error = module_resolver(resolved_module_spec);
1606 if (error.Fail()) {
1607 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001608 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001610
Kate Stoneb9c1b512016-09-06 20:57:50 +00001611 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001612}
1613
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1615 lldb::ModuleSP &module_sp,
1616 bool *did_create_ptr) {
1617 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1618 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001619 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620
1621 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1622
1623 // Check local cache for a module.
1624 auto error = m_module_cache->GetAndPut(
1625 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1626 [this](const ModuleSpec &module_spec,
1627 const FileSpec &tmp_download_file_spec) {
1628 return DownloadModuleSlice(
1629 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1630 module_spec.GetObjectSize(), tmp_download_file_spec);
1631
1632 },
1633 [this](const ModuleSP &module_sp,
1634 const FileSpec &tmp_download_file_spec) {
1635 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1636 },
1637 module_sp, did_create_ptr);
1638 if (error.Success())
1639 return true;
1640
1641 if (log)
1642 log->Printf("Platform::%s - module %s not found in local cache: %s",
1643 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1644 error.AsCString());
1645 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001646}
1647
Zachary Turner97206d52017-05-12 04:51:55 +00001648Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1649 const uint64_t src_offset,
1650 const uint64_t src_size,
1651 const FileSpec &dst_file_spec) {
1652 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001653
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001654 std::error_code EC;
1655 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1656 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657 error.SetErrorStringWithFormat("unable to open destination file: %s",
1658 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001659 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001661
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1663 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001664
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665 if (error.Fail()) {
1666 error.SetErrorStringWithFormat("unable to open source file: %s",
1667 error.AsCString());
1668 return error;
1669 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001670
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 std::vector<char> buffer(1024);
1672 auto offset = src_offset;
1673 uint64_t total_bytes_read = 0;
1674 while (total_bytes_read < src_size) {
1675 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1676 src_size - total_bytes_read);
1677 const uint64_t n_read =
1678 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001679 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680 break;
1681 if (n_read == 0) {
1682 error.SetErrorString("read 0 bytes");
1683 break;
1684 }
1685 offset += n_read;
1686 total_bytes_read += n_read;
1687 dst.write(&buffer[0], n_read);
1688 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001689
Zachary Turner97206d52017-05-12 04:51:55 +00001690 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001691 CloseFile(src_fd, close_error); // Ignoring close error.
1692
1693 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001694}
1695
Zachary Turner97206d52017-05-12 04:51:55 +00001696Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1697 const FileSpec &dst_file_spec) {
1698 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001700}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001701
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702FileSpec Platform::GetModuleCacheRoot() {
1703 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1704 dir_spec.AppendPathComponent(GetName().AsCString());
1705 return dir_spec;
1706}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001707
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001709
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1711 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1712 return s_default_unix_signals_sp;
1713}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001714
Zachary Turnera89ce432019-03-06 18:20:23 +00001715UnixSignalsSP Platform::GetUnixSignals() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 if (IsHost())
Zachary Turnera89ce432019-03-06 18:20:23 +00001717 return UnixSignals::CreateForHost();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001718 return GetRemoteUnixSignals();
1719}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001720
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721uint32_t Platform::LoadImage(lldb_private::Process *process,
1722 const lldb_private::FileSpec &local_file,
1723 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001724 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725 if (local_file && remote_file) {
1726 // Both local and remote file was specified. Install the local file to the
1727 // given location.
1728 if (IsRemote() || local_file != remote_file) {
1729 error = Install(local_file, remote_file);
1730 if (error.Fail())
1731 return LLDB_INVALID_IMAGE_TOKEN;
1732 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001733 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001735
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 if (local_file) {
1737 // Only local file was specified. Install it to the current working
1738 // directory.
1739 FileSpec target_file = GetWorkingDirectory();
1740 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1741 if (IsRemote() || local_file != target_file) {
1742 error = Install(local_file, target_file);
1743 if (error.Fail())
1744 return LLDB_INVALID_IMAGE_TOKEN;
1745 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001746 return DoLoadImage(process, target_file, nullptr, error);
1747 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001748
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749 if (remote_file) {
1750 // Only remote file was specified so we don't have to do any copying
Jim Ingham0d231f72018-06-28 20:02:11 +00001751 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001752 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001753
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754 error.SetErrorString("Neither local nor remote file was specified");
1755 return LLDB_INVALID_IMAGE_TOKEN;
1756}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1759 const lldb_private::FileSpec &remote_file,
Jim Ingham0d231f72018-06-28 20:02:11 +00001760 const std::vector<std::string> *paths,
1761 lldb_private::Status &error,
1762 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 error.SetErrorString("LoadImage is not supported on the current platform");
1764 return LLDB_INVALID_IMAGE_TOKEN;
1765}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001766
Jim Ingham0d231f72018-06-28 20:02:11 +00001767uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1768 const lldb_private::FileSpec &remote_filename,
1769 const std::vector<std::string> &paths,
1770 lldb_private::Status &error,
1771 lldb_private::FileSpec *loaded_path)
1772{
1773 FileSpec file_to_use;
1774 if (remote_filename.IsAbsolute())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001775 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1776
1777 remote_filename.GetPathStyle());
Jim Ingham0d231f72018-06-28 20:02:11 +00001778 else
1779 file_to_use = remote_filename;
1780
1781 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1782}
1783
Zachary Turner97206d52017-05-12 04:51:55 +00001784Status Platform::UnloadImage(lldb_private::Process *process,
1785 uint32_t image_token) {
1786 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001788
Zachary Turner31659452016-11-17 21:15:14 +00001789lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1790 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 lldb_private::Debugger &debugger,
1792 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001793 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001794 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001795
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 if (!target) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001797 ArchSpec arch;
1798 if (target && target->GetArchitecture().IsValid())
1799 arch = target->GetArchitecture();
1800 else
1801 arch = Target::GetDefaultArchitecture();
1802
1803 const char *triple = "";
1804 if (arch.IsValid())
1805 triple = arch.GetTriple().getTriple().c_str();
1806
Kate Stoneb9c1b512016-09-06 20:57:50 +00001807 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001808 error = debugger.GetTargetList().CreateTarget(
Jason Molendadf9f7962018-10-23 23:45:56 +00001809 debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001810 target = new_target_sp.get();
1811 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001812
Kate Stoneb9c1b512016-09-06 20:57:50 +00001813 if (!target || error.Fail())
1814 return nullptr;
1815
1816 debugger.GetTargetList().SetSelectedTarget(target);
1817
1818 lldb::ProcessSP process_sp =
1819 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1820 if (!process_sp)
1821 return nullptr;
1822
1823 error =
1824 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1825 if (error.Fail())
1826 return nullptr;
1827
1828 return process_sp;
1829}
1830
1831size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001832 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001833 error.Clear();
1834 return 0;
1835}
1836
1837size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1838 BreakpointSite *bp_site) {
1839 ArchSpec arch = target.GetArchitecture();
1840 const uint8_t *trap_opcode = nullptr;
1841 size_t trap_opcode_size = 0;
1842
1843 switch (arch.GetMachine()) {
1844 case llvm::Triple::aarch64: {
1845 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1846 trap_opcode = g_aarch64_opcode;
1847 trap_opcode_size = sizeof(g_aarch64_opcode);
1848 } break;
1849
1850 // TODO: support big-endian arm and thumb trap codes.
1851 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001852 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1853 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001854 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1855 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1856
1857 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001858 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001859
1860 if (bp_loc_sp) {
1861 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001862 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001863 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001864 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001865 }
1866
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001867 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868 trap_opcode = g_thumb_breakpoint_opcode;
1869 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1870 } else {
1871 trap_opcode = g_arm_breakpoint_opcode;
1872 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1873 }
1874 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001875
Kate Stoneb9c1b512016-09-06 20:57:50 +00001876 case llvm::Triple::mips:
1877 case llvm::Triple::mips64: {
1878 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1879 trap_opcode = g_hex_opcode;
1880 trap_opcode_size = sizeof(g_hex_opcode);
1881 } break;
1882
1883 case llvm::Triple::mipsel:
1884 case llvm::Triple::mips64el: {
1885 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1886 trap_opcode = g_hex_opcode;
1887 trap_opcode_size = sizeof(g_hex_opcode);
1888 } break;
1889
1890 case llvm::Triple::systemz: {
1891 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1892 trap_opcode = g_hex_opcode;
1893 trap_opcode_size = sizeof(g_hex_opcode);
1894 } break;
1895
1896 case llvm::Triple::hexagon: {
1897 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1898 trap_opcode = g_hex_opcode;
1899 trap_opcode_size = sizeof(g_hex_opcode);
1900 } break;
1901
1902 case llvm::Triple::ppc:
1903 case llvm::Triple::ppc64: {
1904 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1905 trap_opcode = g_ppc_opcode;
1906 trap_opcode_size = sizeof(g_ppc_opcode);
1907 } break;
1908
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001909 case llvm::Triple::ppc64le: {
1910 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1911 trap_opcode = g_ppc64le_opcode;
1912 trap_opcode_size = sizeof(g_ppc64le_opcode);
1913 } break;
1914
Kate Stoneb9c1b512016-09-06 20:57:50 +00001915 case llvm::Triple::x86:
1916 case llvm::Triple::x86_64: {
1917 static const uint8_t g_i386_opcode[] = {0xCC};
1918 trap_opcode = g_i386_opcode;
1919 trap_opcode_size = sizeof(g_i386_opcode);
1920 } break;
1921
1922 default:
David Blaikiea322f362017-01-06 00:38:06 +00001923 llvm_unreachable(
1924 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001925 }
1926
1927 assert(bp_site);
1928 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1929 return trap_opcode_size;
1930
1931 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001932}