blob: 710f82eaefa9c6f915ff4367972a21934e639c29 [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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000121///
122/// There should only be one of these for each host that LLDB runs
123/// upon that should be statically compiled in and registered using
124/// preprocessor macros or other similar build mechanisms.
125///
126/// This platform will be used as the default platform when launching
127/// or attaching to processes unless another platform is specified.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
129
130static std::vector<PlatformSP> &GetPlatformList() {
131 static std::vector<PlatformSP> g_platform_list;
132 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000133}
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135static std::recursive_mutex &GetPlatformListMutex() {
136 static std::recursive_mutex g_mutex;
137 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142void Platform::Terminate() {
143 if (g_initialize_count > 0) {
144 if (--g_initialize_count == 0) {
145 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
146 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000147 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000149}
150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
152 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
153 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
157 // The native platform should use its static void Platform::Initialize()
158 // function to register itself as the native platform.
159 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 if (platform_sp) {
162 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
163 GetPlatformList().push_back(platform_sp);
164 }
Greg Claytone996fd32011-03-08 22:40:15 +0000165}
166
Zachary Turner97206d52017-05-12 04:51:55 +0000167Status Platform::GetFileWithUUID(const FileSpec &platform_file,
168 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 // Default to the local case
170 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000171 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000172}
173
Greg Clayton91c0e742013-01-11 23:44:27 +0000174FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175Platform::LocateExecutableScriptingResources(Target *target, Module &module,
176 Stream *feedback_stream) {
177 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000178}
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180// PlatformSP
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000181// Platform::FindPlugin (Process *process, ConstString plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000182//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000183// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000184// if (plugin_name)
185// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186// create_callback =
187// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000188// if (create_callback)
189// {
190// ArchSpec arch;
191// if (process)
192// {
193// arch = process->GetTarget().GetArchitecture();
194// }
195// PlatformSP platform_sp(create_callback(process, &arch));
196// if (platform_sp)
197// return platform_sp;
198// }
199// }
200// else
201// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202// for (uint32_t idx = 0; (create_callback =
203// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
204// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000205// {
206// PlatformSP platform_sp(create_callback(process, nullptr));
207// if (platform_sp)
208// return platform_sp;
209// }
210// }
211// return PlatformSP();
212//}
Jason Molenda1c627542013-04-05 01:03:25 +0000213
Zachary Turner97206d52017-05-12 04:51:55 +0000214Status Platform::GetSharedModule(const ModuleSpec &module_spec,
215 Process *process, ModuleSP &module_sp,
216 const FileSpecList *module_search_paths_ptr,
217 ModuleSP *old_module_sp_ptr,
218 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 if (IsHost())
220 return ModuleList::GetSharedModule(
221 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
222 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000223
Pavel Labath1f8639a2018-08-28 16:32:46 +0000224 // Module resolver lambda.
225 auto resolver = [&](const ModuleSpec &spec) {
226 Status error(eErrorTypeGeneric);
227 ModuleSpec resolved_spec;
228 // Check if we have sysroot set.
229 if (m_sdk_sysroot) {
230 // Prepend sysroot to module spec.
231 resolved_spec = spec;
232 resolved_spec.GetFileSpec().PrependPathComponent(
233 m_sdk_sysroot.GetStringRef());
234 // Try to get shared module with resolved spec.
235 error = ModuleList::GetSharedModule(
236 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
237 did_create_ptr, false);
238 }
239 // If we don't have sysroot or it didn't work then
240 // try original module spec.
241 if (!error.Success()) {
242 resolved_spec = spec;
243 error = ModuleList::GetSharedModule(
244 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
245 did_create_ptr, false);
246 }
247 if (error.Success() && module_sp)
248 module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
249 return error;
250 };
251
252 return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
257 const ArchSpec &arch, ModuleSpec &module_spec) {
258 ModuleSpecList module_specs;
259 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
260 module_specs) == 0)
261 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 ModuleSpec matched_module_spec;
264 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
265 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000266}
267
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000268PlatformSP Platform::Find(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 if (name) {
270 static ConstString g_host_platform_name("host");
271 if (name == g_host_platform_name)
272 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
275 for (const auto &platform_sp : GetPlatformList()) {
276 if (platform_sp->GetName() == name)
277 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 }
280 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000281}
282
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000283PlatformSP Platform::Create(ConstString name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 PlatformCreateInstance create_callback = nullptr;
285 lldb::PlatformSP platform_sp;
286 if (name) {
287 static ConstString g_host_platform_name("host");
288 if (name == g_host_platform_name)
289 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 create_callback =
292 PluginManager::GetPlatformCreateCallbackForPluginName(name);
293 if (create_callback)
294 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000295 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 error.SetErrorStringWithFormat(
297 "unable to find a plug-in for the platform named \"%s\"",
298 name.GetCString());
299 } else
300 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000301
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 if (platform_sp) {
303 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
304 GetPlatformList().push_back(platform_sp);
305 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000306
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000308}
309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000311 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 lldb::PlatformSP platform_sp;
313 if (arch.IsValid()) {
314 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000315 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 // First try exact arch matches across all platforms already created
317 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
318 for (const auto &platform_sp : GetPlatformList()) {
319 if (platform_sp->IsCompatibleArchitecture(arch, true,
320 platform_arch_ptr))
321 return platform_sp;
322 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 // Next try compatible arch matches across all platforms already created
325 for (const auto &platform_sp : GetPlatformList()) {
326 if (platform_sp->IsCompatibleArchitecture(arch, false,
327 platform_arch_ptr))
328 return platform_sp;
329 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000330 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331
332 PlatformCreateInstance create_callback;
333 // First try exact arch matches across all platform plug-ins
334 uint32_t idx;
335 for (idx = 0; (create_callback =
336 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
337 ++idx) {
338 if (create_callback) {
339 platform_sp = create_callback(false, &arch);
340 if (platform_sp &&
341 platform_sp->IsCompatibleArchitecture(arch, true,
342 platform_arch_ptr)) {
343 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
344 GetPlatformList().push_back(platform_sp);
345 return platform_sp;
346 }
347 }
348 }
349 // Next try compatible arch matches across all platform plug-ins
350 for (idx = 0; (create_callback =
351 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
352 ++idx) {
353 if (create_callback) {
354 platform_sp = create_callback(false, &arch);
355 if (platform_sp &&
356 platform_sp->IsCompatibleArchitecture(arch, false,
357 platform_arch_ptr)) {
358 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
359 GetPlatformList().push_back(platform_sp);
360 return platform_sp;
361 }
362 }
363 }
364 } else
365 error.SetErrorString("invalid platform name");
366 if (platform_arch_ptr)
367 platform_arch_ptr->Clear();
368 platform_sp.reset();
369 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000370}
371
Pavel Labath7263f1b2017-10-31 10:56:03 +0000372ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
373 if (platform)
374 return platform->GetAugmentedArchSpec(triple);
375 return HostInfo::GetAugmentedArchSpec(triple);
376}
377
Greg Claytone996fd32011-03-08 22:40:15 +0000378/// Default Constructor
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000379Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 : m_is_host(is_host), m_os_version_set_while_connected(false),
381 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000382 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000383 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
384 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
385 m_ignores_remote_hostname(false), m_trap_handlers(),
386 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 m_module_cache(llvm::make_unique<ModuleCache>()) {
388 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
389 if (log)
390 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000391}
392
Greg Claytone996fd32011-03-08 22:40:15 +0000393/// Destructor.
394///
395/// The destructor is virtual since this class is designed to be
396/// inherited from by the plug-in instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397Platform::~Platform() {
398 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
399 if (log)
400 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000401}
402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 std::string s;
405 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 ArchSpec arch(GetSystemArchitecture());
408 if (arch.IsValid()) {
409 if (!arch.GetTriple().str().empty()) {
410 strm.Printf(" Triple: ");
411 arch.DumpTriple(strm);
412 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000413 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000415
Pavel Labath2272c482018-06-18 15:02:23 +0000416 llvm::VersionTuple os_version = GetOSVersion();
417 if (!os_version.empty()) {
418 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 if (GetOSBuildString(s))
421 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 strm.EOL();
424 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (GetOSKernelDescription(s))
427 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 if (IsHost()) {
430 strm.Printf(" Hostname: %s\n", GetHostname());
431 } else {
432 const bool is_connected = IsConnected();
433 if (is_connected)
434 strm.Printf(" Hostname: %s\n", GetHostname());
435 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
436 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 if (GetWorkingDirectory()) {
439 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
440 }
441 if (!IsConnected())
442 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000443
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444 std::string specific_info(GetPlatformSpecificConnectionInformation());
445
446 if (!specific_info.empty())
447 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000448}
449
Pavel Labath2272c482018-06-18 15:02:23 +0000450llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000454 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000456 m_os_version = HostInfo::GetOSVersion();
457 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000458 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 } else {
460 // We have a remote platform. We can only fetch the remote
461 // OS version if we are connected, and we don't want to do it
462 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000465
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000467 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000468 // We have valid OS version info, check to make sure it wasn't manually
469 // set prior to connecting. If it was manually set prior to connecting,
470 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 if (is_connected && !m_os_version_set_while_connected)
472 fetch = true;
473 } else {
474 // We don't have valid OS version info, fetch it if we are connected
475 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000476 }
477
Pavel Labath2272c482018-06-18 15:02:23 +0000478 if (fetch)
479 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 }
481
Pavel Labath2272c482018-06-18 15:02:23 +0000482 if (!m_os_version.empty())
483 return m_os_version;
484 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000485 // Check with the process in case it can answer the question if a process
486 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000487 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 }
Pavel Labath2272c482018-06-18 15:02:23 +0000489 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000490}
Greg Clayton1cb64962011-03-24 04:28:38 +0000491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492bool Platform::GetOSBuildString(std::string &s) {
493 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000496#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000498#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000500#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 else
502 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000503}
504
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505bool Platform::GetOSKernelDescription(std::string &s) {
506 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000507#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000509#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000511#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 else
513 return GetRemoteOSKernelDescription(s);
514}
515
516void Platform::AddClangModuleCompilationOptions(
517 Target *target, std::vector<std::string> &options) {
518 std::vector<std::string> default_compilation_options = {
519 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
520
521 options.insert(options.end(), default_compilation_options.begin(),
522 default_compilation_options.end());
523}
524
525FileSpec Platform::GetWorkingDirectory() {
526 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000527 llvm::SmallString<64> cwd;
528 if (llvm::sys::fs::current_path(cwd))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000529 return {};
530 else {
531 FileSpec file_spec(cwd);
532 FileSystem::Instance().Resolve(file_spec);
533 return file_spec;
534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 } else {
536 if (!m_working_dir)
537 m_working_dir = GetRemoteWorkingDirectory();
538 return m_working_dir;
539 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000540}
541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542struct RecurseCopyBaton {
543 const FileSpec &dst;
544 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000545 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000546};
547
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000548static FileSystem::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000549RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000550 llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000552 FileSpec src(path);
Zachary Turner7d86ee52017-03-08 17:56:08 +0000553 namespace fs = llvm::sys::fs;
554 switch (ft) {
555 case fs::file_type::fifo_file:
556 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 // we have no way to copy pipes and sockets - ignore them and continue
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000558 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000560
Zachary Turner7d86ee52017-03-08 17:56:08 +0000561 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 // make the new directory and get in there
563 FileSpec dst_dir = rc_baton->dst;
564 if (!dst_dir.GetFilename())
565 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000566 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 dst_dir, lldb::eFilePermissionsDirectoryDefault);
568 if (error.Fail()) {
569 rc_baton->error.SetErrorStringWithFormat(
570 "unable to setup directory %s on remote end", dst_dir.GetCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000571 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573
574 // now recurse
575 std::string src_dir_path(src.GetPath());
576
Adrian Prantl05097242018-04-30 16:49:04 +0000577 // Make a filespec that only fills in the directory of a FileSpec so when
578 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 FileSpec recurse_dst;
580 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000581 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
582 Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000583 FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
584 RecurseCopy_Callback, &rc_baton2);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 if (rc_baton2.error.Fail()) {
586 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000587 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000589 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 } break;
591
Zachary Turner7d86ee52017-03-08 17:56:08 +0000592 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 // copy the file and keep going
594 FileSpec dst_file = rc_baton->dst;
595 if (!dst_file.GetFilename())
596 dst_file.GetFilename() = src.GetFilename();
597
598 FileSpec src_resolved;
599
Jonas Devlieghere46376962018-10-31 21:49:27 +0000600 rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601
602 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000603 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604
605 rc_baton->error =
606 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
607
608 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000609 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000611 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 } break;
613
Zachary Turner7d86ee52017-03-08 17:56:08 +0000614 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 // copy the file and keep going
616 FileSpec dst_file = rc_baton->dst;
617 if (!dst_file.GetFilename())
618 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000619 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 if (err.Fail()) {
621 rc_baton->error.SetErrorString(err.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000622 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000624 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 } break;
626
Zachary Turner7d86ee52017-03-08 17:56:08 +0000627 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 rc_baton->error.SetErrorStringWithFormat(
629 "invalid file detected during copy: %s", src.GetPath().c_str());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000630 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 break;
632 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000633 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000634}
635
Zachary Turner97206d52017-05-12 04:51:55 +0000636Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
637 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638
639 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
640 if (log)
641 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
642 dst.GetPath().c_str());
643 FileSpec fixed_dst(dst);
644
645 if (!fixed_dst.GetFilename())
646 fixed_dst.GetFilename() = src.GetFilename();
647
648 FileSpec working_dir = GetWorkingDirectory();
649
650 if (dst) {
651 if (dst.GetDirectory()) {
652 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
653 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
654 fixed_dst.GetDirectory() = dst.GetDirectory();
655 }
Adrian Prantl05097242018-04-30 16:49:04 +0000656 // If the fixed destination file doesn't have a directory yet, then we
657 // must have a relative path. We will resolve this relative path against
658 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659 if (!fixed_dst.GetDirectory()) {
660 FileSpec relative_spec;
661 std::string path;
662 if (working_dir) {
663 relative_spec = working_dir;
664 relative_spec.AppendPathComponent(dst.GetPath());
665 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
666 } else {
667 error.SetErrorStringWithFormat(
668 "platform working directory must be valid for relative path '%s'",
669 dst.GetPath().c_str());
670 return error;
671 }
672 }
673 } else {
674 if (working_dir) {
675 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
676 } else {
677 error.SetErrorStringWithFormat(
678 "platform working directory must be valid for relative path '%s'",
679 dst.GetPath().c_str());
680 return error;
681 }
682 }
683 } else {
684 if (working_dir) {
685 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
686 } else {
687 error.SetErrorStringWithFormat("platform working directory must be valid "
688 "when destination directory is empty");
689 return error;
690 }
691 }
692
693 if (log)
694 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
695 src.GetPath().c_str(), dst.GetPath().c_str(),
696 fixed_dst.GetPath().c_str());
697
698 if (GetSupportsRSync()) {
699 error = PutFile(src, dst);
700 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000701 namespace fs = llvm::sys::fs;
702 switch (fs::get_file_type(src.GetPath(), false)) {
703 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000704 llvm::sys::fs::remove(fixed_dst.GetPath());
Jonas Devlieghere7c5310b2018-11-01 15:47:33 +0000705 uint32_t permissions = FileSystem::Instance().GetPermissions(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706 if (permissions == 0)
707 permissions = eFilePermissionsDirectoryDefault;
708 error = MakeDirectory(fixed_dst, permissions);
709 if (error.Success()) {
710 // Make a filespec that only fills in the directory of a FileSpec so
711 // when we enumerate we can quickly fill in the filename for dst copies
712 FileSpec recurse_dst;
713 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
714 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000715 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000716 FileSystem::Instance().EnumerateDirectory(
717 src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 return baton.error;
719 }
720 } break;
721
Zachary Turner7d86ee52017-03-08 17:56:08 +0000722 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000723 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 error = PutFile(src, fixed_dst);
725 break;
726
Zachary Turner7d86ee52017-03-08 17:56:08 +0000727 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000728 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729 FileSpec src_resolved;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000730 error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 if (error.Success())
732 error = CreateSymlink(dst, src_resolved);
733 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000734 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 error.SetErrorString("platform install doesn't handle pipes");
736 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000737 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 error.SetErrorString("platform install doesn't handle sockets");
739 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000740 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 error.SetErrorString(
742 "platform install doesn't handle non file or directory items");
743 break;
744 }
745 }
746 return error;
747}
748
749bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
750 if (IsHost()) {
751 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000752 LLDB_LOG(log, "{0}", file_spec);
753 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
754 LLDB_LOG(log, "error: {0}", ec.message());
755 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000757 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 } else {
759 m_working_dir.Clear();
760 return SetRemoteWorkingDirectory(file_spec);
761 }
762}
763
Zachary Turner97206d52017-05-12 04:51:55 +0000764Status Platform::MakeDirectory(const FileSpec &file_spec,
765 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000767 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000769 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
771 GetPluginName().GetCString(),
772 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000773 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000775}
776
Zachary Turner97206d52017-05-12 04:51:55 +0000777Status Platform::GetFilePermissions(const FileSpec &file_spec,
778 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000779 if (IsHost()) {
780 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
781 if (Value)
782 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000783 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000784 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000785 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
787 GetPluginName().GetCString(),
788 LLVM_PRETTY_FUNCTION);
789 return error;
790 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000791}
792
Zachary Turner97206d52017-05-12 04:51:55 +0000793Status Platform::SetFilePermissions(const FileSpec &file_spec,
794 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000795 if (IsHost()) {
796 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
797 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
798 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000799 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
801 GetPluginName().GetCString(),
802 LLVM_PRETTY_FUNCTION);
803 return error;
804 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000805}
806
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807ConstString Platform::GetName() { return GetPluginName(); }
808
809const char *Platform::GetHostname() {
810 if (IsHost())
811 return "127.0.0.1";
812
813 if (m_name.empty())
814 return nullptr;
815 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000816}
817
Kate Stoneb9c1b512016-09-06 20:57:50 +0000818ConstString Platform::GetFullNameForDylib(ConstString basename) {
819 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000820}
821
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
823 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
824 if (log)
825 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000826 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 m_working_dir = working_dir;
828 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000829}
830
Pavel Labath2272c482018-06-18 15:02:23 +0000831bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000833 // We don't need anyone setting the OS version for the host platform, we
834 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000835 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000837 // We have a remote platform, allow setting the target OS version if we
838 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 // request the remote OS version from the connected platform.
840 if (IsConnected())
841 return false;
842 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000843 // We aren't connected and we might want to set the OS version ahead of
844 // time before we connect so we can peruse files and use a local SDK or
845 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000846 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000848 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000849 }
850 return false;
851}
852
Zachary Turner97206d52017-05-12 04:51:55 +0000853Status
854Platform::ResolveExecutable(const ModuleSpec &module_spec,
855 lldb::ModuleSP &exe_module_sp,
856 const FileSpecList *module_search_paths_ptr) {
857 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000858 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 if (module_spec.GetArchitecture().IsValid()) {
860 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
861 module_search_paths_ptr, nullptr,
862 nullptr);
863 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000864 // No valid architecture was specified, ask the platform for the
865 // architectures that we should be using (in the correct order) and see
866 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000867 ModuleSpec arch_module_spec(module_spec);
868 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
869 idx, arch_module_spec.GetArchitecture());
870 ++idx) {
871 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
872 module_search_paths_ptr, nullptr,
873 nullptr);
874 // Did we find an executable using one of the
875 if (error.Success() && exe_module_sp)
876 break;
877 }
Greg Claytone996fd32011-03-08 22:40:15 +0000878 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879 } else {
880 error.SetErrorStringWithFormat("'%s' does not exist",
881 module_spec.GetFileSpec().GetPath().c_str());
882 }
883 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000884}
885
Zachary Turner97206d52017-05-12 04:51:55 +0000886Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
887 FileSpec &sym_file) {
888 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000889 if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000890 sym_file = sym_spec.GetSymbolFileSpec();
891 else
892 error.SetErrorString("unable to resolve symbol file");
893 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000894}
895
Kate Stoneb9c1b512016-09-06 20:57:50 +0000896bool Platform::ResolveRemotePath(const FileSpec &platform_path,
897 FileSpec &resolved_platform_path) {
898 resolved_platform_path = platform_path;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000899 FileSystem::Instance().Resolve(resolved_platform_path);
900 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901}
902
903const ArchSpec &Platform::GetSystemArchitecture() {
904 if (IsHost()) {
905 if (!m_system_arch.IsValid()) {
906 // We have a local host platform
907 m_system_arch = HostInfo::GetArchitecture();
908 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000909 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000911 // We have a remote platform. We can only fetch the remote system
912 // architecture if we are connected, and we don't want to do it more than
913 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000914
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000916
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 bool fetch = false;
918 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000919 // We have valid OS version info, check to make sure it wasn't manually
920 // set prior to connecting. If it was manually set prior to connecting,
921 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 if (is_connected && !m_system_arch_set_while_connected)
923 fetch = true;
924 } else {
925 // We don't have valid OS version info, fetch it if we are connected
926 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000927 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000928
929 if (fetch) {
930 m_system_arch = GetRemoteSystemArchitecture();
931 m_system_arch_set_while_connected = m_system_arch.IsValid();
932 }
933 }
934 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000935}
936
Pavel Labath7263f1b2017-10-31 10:56:03 +0000937ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
938 if (triple.empty())
939 return ArchSpec();
940 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
941 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
942 return ArchSpec(triple);
943
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000944 if (auto kind = HostInfo::ParseArchitectureKind(triple))
945 return HostInfo::GetArchitecture(*kind);
946
Pavel Labath7263f1b2017-10-31 10:56:03 +0000947 ArchSpec compatible_arch;
948 ArchSpec raw_arch(triple);
949 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
950 return raw_arch;
951
952 if (!compatible_arch.IsValid())
953 return ArchSpec(normalized_triple);
954
955 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
956 if (normalized_triple.getVendorName().empty())
957 normalized_triple.setVendor(compatible_triple.getVendor());
958 if (normalized_triple.getOSName().empty())
959 normalized_triple.setOS(compatible_triple.getOS());
960 if (normalized_triple.getEnvironmentName().empty())
961 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
962 return ArchSpec(normalized_triple);
963}
964
Zachary Turner97206d52017-05-12 04:51:55 +0000965Status Platform::ConnectRemote(Args &args) {
966 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 if (IsHost())
968 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
969 "the host platform and is always connected.",
970 GetPluginName().GetCString());
971 else
972 error.SetErrorStringWithFormat(
973 "Platform::ConnectRemote() is not supported by %s",
974 GetPluginName().GetCString());
975 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000976}
977
Zachary Turner97206d52017-05-12 04:51:55 +0000978Status Platform::DisconnectRemote() {
979 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 if (IsHost())
981 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
982 "the host platform and is always connected.",
983 GetPluginName().GetCString());
984 else
985 error.SetErrorStringWithFormat(
986 "Platform::DisconnectRemote() is not supported by %s",
987 GetPluginName().GetCString());
988 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000989}
Greg Clayton32e0a752011-03-30 18:16:51 +0000990
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991bool Platform::GetProcessInfo(lldb::pid_t pid,
992 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000993 // Take care of the host case so that each subclass can just call this
994 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000995 if (IsHost())
996 return Host::GetProcessInfo(pid, process_info);
997 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000998}
999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1001 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +00001002 // Take care of the host case so that each subclass can just call this
1003 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 uint32_t match_count = 0;
1005 if (IsHost())
1006 match_count = Host::FindProcesses(match_info, process_infos);
1007 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001008}
Greg Clayton8b82f082011-04-12 05:54:46 +00001009
Zachary Turner97206d52017-05-12 04:51:55 +00001010Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1011 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1013 if (log)
1014 log->Printf("Platform::%s()", __FUNCTION__);
1015
Adrian Prantl05097242018-04-30 16:49:04 +00001016 // Take care of the host case so that each subclass can just call this
1017 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 if (IsHost()) {
1019 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1020 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1021
1022 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1023 const bool is_localhost = true;
1024 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1025 const bool first_arg_is_full_shell_command = false;
1026 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1027 if (log) {
1028 const FileSpec &shell = launch_info.GetShell();
Jason Molenda3b687612019-02-08 23:36:25 +00001029 std::string shell_str = (shell) ? shell.GetPath() : "<null>";
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 log->Printf(
1031 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1032 ", shell is '%s'",
Jason Molenda3b687612019-02-08 23:36:25 +00001033 __FUNCTION__, num_resumes, shell_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 }
1035
1036 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1037 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1038 num_resumes))
1039 return error;
1040 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1041 error = ShellExpandArguments(launch_info);
1042 if (error.Fail()) {
1043 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1044 "consider launching with 'process "
1045 "launch'.",
1046 error.AsCString("unknown"));
1047 return error;
1048 }
1049 }
1050
Todd Fialaac33cc92014-10-09 01:02:08 +00001051 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1053 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001054
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 error = Host::LaunchProcess(launch_info);
1056 } else
1057 error.SetErrorString(
1058 "base lldb_private::Platform class can't launch remote processes");
1059 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001060}
1061
Zachary Turner97206d52017-05-12 04:51:55 +00001062Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 if (IsHost())
1064 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001065 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001066}
1067
Zachary Turner97206d52017-05-12 04:51:55 +00001068Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1070 if (log)
1071 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001074 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 size_t num_debuggers = Debugger::GetNumDebuggers();
1076 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1077 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1078 lldb_private::TargetList &targets = debugger->GetTargetList();
1079 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1080 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1081 if (process->GetID() == pid)
1082 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001085
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001087 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 "base lldb_private::Platform class can't kill remote processes unless "
1089 "they are controlled by a process plugin");
1090 }
1091 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001092 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001093}
1094
Greg Clayton8b82f082011-04-12 05:54:46 +00001095lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1097 Target *target, // Can be nullptr, if nullptr create a
1098 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001099 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1101 if (log)
1102 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1103 static_cast<void *>(target));
1104
1105 ProcessSP process_sp;
1106 // Make sure we stop at the entry point
1107 launch_info.GetFlags().Set(eLaunchFlagDebug);
1108 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001109 // group, since then we can handle ^C interrupts ourselves w/o having to
1110 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111 launch_info.SetLaunchInSeparateProcessGroup(true);
1112
1113 // Allow any StructuredData process-bound plugins to adjust the launch info
1114 // if needed
1115 size_t i = 0;
1116 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001117 // Note iteration can't simply go until a nullptr callback is returned, as it
1118 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1120 for (auto filter_callback = get_filter_func(i, iteration_complete);
1121 !iteration_complete;
1122 filter_callback = get_filter_func(++i, iteration_complete)) {
1123 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001124 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125 error = (*filter_callback)(launch_info, target);
1126 if (!error.Success()) {
1127 if (log)
1128 log->Printf("Platform::%s() StructuredDataPlugin launch "
1129 "filter failed.",
1130 __FUNCTION__);
1131 return process_sp;
1132 }
1133 }
1134 }
1135
1136 error = LaunchProcess(launch_info);
1137 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001138 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1140 ")",
1141 __FUNCTION__, launch_info.GetProcessID());
1142 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1143 ProcessAttachInfo attach_info(launch_info);
1144 process_sp = Attach(attach_info, debugger, target, error);
1145 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001146 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1148 __FUNCTION__, process_sp->GetPluginName().AsCString());
1149 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001150
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 // Since we attached to the process, it will think it needs to detach
1152 // if the process object just goes away without an explicit call to
1153 // Process::Kill() or Process::Detach(), so let it know to kill the
1154 // process if this happens.
1155 process_sp->SetShouldDetach(false);
1156
Adrian Prantl05097242018-04-30 16:49:04 +00001157 // If we didn't have any file actions, the pseudo terminal might have
1158 // been used where the slave side was given as the file to open for
1159 // stdin/out/err after we have already opened the master so we can
1160 // read/write stdin/out/err.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001162 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 process_sp->SetSTDIOFileDescriptor(pty_fd);
1164 }
1165 } else {
1166 if (log)
1167 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1168 error.AsCString());
1169 }
1170 } else {
1171 if (log)
1172 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1173 "invalid process id",
1174 __FUNCTION__);
1175 }
1176 } else {
1177 if (log)
1178 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1179 error.AsCString());
1180 }
1181
1182 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001183}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001184
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001185lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1187 ArchSpec *platform_arch_ptr) {
1188 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001189 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190 if (arch.IsValid())
1191 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1192 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001193}
1194
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001195/// Lets a platform answer if it is compatible with a given
1196/// architecture and the target triple contained within.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1198 bool exact_arch_match,
1199 ArchSpec *compatible_arch_ptr) {
1200 // If the architecture is invalid, we must answer true...
1201 if (arch.IsValid()) {
1202 ArchSpec platform_arch;
1203 // Try for an exact architecture match first.
1204 if (exact_arch_match) {
1205 for (uint32_t arch_idx = 0;
1206 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1207 ++arch_idx) {
1208 if (arch.IsExactMatch(platform_arch)) {
1209 if (compatible_arch_ptr)
1210 *compatible_arch_ptr = platform_arch;
1211 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001212 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 }
1214 } else {
1215 for (uint32_t arch_idx = 0;
1216 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1217 ++arch_idx) {
1218 if (arch.IsCompatibleMatch(platform_arch)) {
1219 if (compatible_arch_ptr)
1220 *compatible_arch_ptr = platform_arch;
1221 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001222 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001224 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001225 }
1226 if (compatible_arch_ptr)
1227 compatible_arch_ptr->Clear();
1228 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001229}
1230
Zachary Turner97206d52017-05-12 04:51:55 +00001231Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1232 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001233 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1234 if (log)
1235 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001236
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237 uint32_t source_open_options =
1238 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001239 namespace fs = llvm::sys::fs;
1240 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001241 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001242
Jonas Devlieghere50bc1ed2018-11-02 22:34:51 +00001243 File source_file;
1244 Status error = FileSystem::Instance().Open(
1245 source_file, source, source_open_options, lldb::eFilePermissionsUserRW);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246 uint32_t permissions = source_file.GetPermissions(error);
1247 if (permissions == 0)
1248 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 if (!source_file.IsValid())
Zachary Turner97206d52017-05-12 04:51:55 +00001251 return Status("PutFile: unable to open source file");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 lldb::user_id_t dest_file = OpenFile(
1253 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1254 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1255 permissions, error);
1256 if (log)
1257 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001258
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 if (error.Fail())
1260 return error;
1261 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001262 return Status("unable to open target file");
Jason Molendaeb9902a2018-12-07 00:35:26 +00001263 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264 uint64_t offset = 0;
1265 for (;;) {
1266 size_t bytes_read = buffer_sp->GetByteSize();
1267 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1268 if (error.Fail() || bytes_read == 0)
1269 break;
1270
1271 const uint64_t bytes_written =
1272 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001273 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 offset += bytes_written;
1277 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001278 // We didn't write the correct number of bytes, so adjust the file
1279 // position in the source file we are reading from...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001281 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 }
1283 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001284
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001286 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001287
1288 // TODO: ChownFile?
1289
1290 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001291}
1292
Zachary Turner97206d52017-05-12 04:51:55 +00001293Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1294 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001296}
1297
Zachary Turner97206d52017-05-12 04:51:55 +00001298Status
1299Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1300 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001301{
Zachary Turner97206d52017-05-12 04:51:55 +00001302 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001304}
1305
Kate Stoneb9c1b512016-09-06 20:57:50 +00001306bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1307 return false;
1308}
1309
Zachary Turner97206d52017-05-12 04:51:55 +00001310Status Platform::Unlink(const FileSpec &path) {
1311 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312 return error;
1313}
1314
Ed Maste37c40af2017-08-16 12:55:02 +00001315MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1316 addr_t length, unsigned prot,
1317 unsigned flags, addr_t fd,
1318 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 uint64_t flags_platform = 0;
1320 if (flags & eMmapFlagsPrivate)
1321 flags_platform |= MAP_PRIVATE;
1322 if (flags & eMmapFlagsAnon)
1323 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001324
1325 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1326 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327}
1328
Zachary Turner97206d52017-05-12 04:51:55 +00001329lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 const char *command, // Shouldn't be nullptr
1331 const FileSpec &
1332 working_dir, // Pass empty FileSpec to use the current working directory
1333 int *status_ptr, // Pass nullptr if you don't want the process exit status
1334 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1335 // process to exit
1336 std::string
1337 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001338 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 if (IsHost())
1340 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
Pavel Labath19dd1a02018-05-10 10:46:03 +00001341 command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 else
Zachary Turner97206d52017-05-12 04:51:55 +00001343 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344}
1345
1346bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1347 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001348 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001349 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001350 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1351 if (!Result)
1352 return false;
1353 std::tie(high, low) = Result->words();
1354 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001355}
1356
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357void Platform::SetLocalCacheDirectory(const char *local) {
1358 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001359}
1360
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361const char *Platform::GetLocalCacheDirectory() {
1362 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001363}
Greg Claytonfbb76342013-11-20 21:07:01 +00001364
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001365static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001367 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001369 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 "Platform-specific options required for rsync to work."},
1371 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001372 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 "Platform-specific rsync prefix put before the remote path."},
1374 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001375 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 "Do not automatically fill in the remote hostname when composing the "
1377 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001378};
1379
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001380static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001382 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001384 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001386};
1387
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001388static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001389 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001390 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001392};
1393
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001394llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001395 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001396}
1397
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398void OptionGroupPlatformRSync::OptionParsingStarting(
1399 ExecutionContext *execution_context) {
1400 m_rsync = false;
1401 m_rsync_opts.clear();
1402 m_rsync_prefix.clear();
1403 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001404}
1405
Zachary Turner97206d52017-05-12 04:51:55 +00001406lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001407OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001408 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001410 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 char short_option = (char)GetDefinitions()[option_idx].short_option;
1412 switch (short_option) {
1413 case 'r':
1414 m_rsync = true;
1415 break;
1416
1417 case 'R':
1418 m_rsync_opts.assign(option_arg);
1419 break;
1420
1421 case 'P':
1422 m_rsync_prefix.assign(option_arg);
1423 break;
1424
1425 case 'i':
1426 m_ignores_remote_hostname = true;
1427 break;
1428
1429 default:
1430 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1431 break;
1432 }
1433
1434 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001435}
1436
Greg Clayton4116e932012-05-15 02:33:01 +00001437lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1439 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001440}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001441
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001442llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001443 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001444}
1445
Kate Stoneb9c1b512016-09-06 20:57:50 +00001446void OptionGroupPlatformSSH::OptionParsingStarting(
1447 ExecutionContext *execution_context) {
1448 m_ssh = false;
1449 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001450}
1451
Zachary Turner97206d52017-05-12 04:51:55 +00001452lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001453OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001454 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001456 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457 char short_option = (char)GetDefinitions()[option_idx].short_option;
1458 switch (short_option) {
1459 case 's':
1460 m_ssh = true;
1461 break;
1462
1463 case 'S':
1464 m_ssh_opts.assign(option_arg);
1465 break;
1466
1467 default:
1468 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1469 break;
1470 }
1471
1472 return error;
1473}
1474
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001475llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001476 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477}
1478
1479void OptionGroupPlatformCaching::OptionParsingStarting(
1480 ExecutionContext *execution_context) {
1481 m_cache_dir.clear();
1482}
1483
Zachary Turner97206d52017-05-12 04:51:55 +00001484lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001485 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001487 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 char short_option = (char)GetDefinitions()[option_idx].short_option;
1489 switch (short_option) {
1490 case 'c':
1491 m_cache_dir.assign(option_arg);
1492 break;
1493
1494 default:
1495 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1496 break;
1497 }
1498
1499 return error;
1500}
1501
Pavel Labath62930e52018-01-10 11:57:31 +00001502Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503
1504const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1505 if (!m_calculated_trap_handlers) {
1506 std::lock_guard<std::mutex> guard(m_mutex);
1507 if (!m_calculated_trap_handlers) {
1508 CalculateTrapHandlerSymbolNames();
1509 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001510 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 }
1512 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001513}
1514
Zachary Turner97206d52017-05-12 04:51:55 +00001515Status Platform::GetCachedExecutable(
1516 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1517 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518 const auto platform_spec = module_spec.GetFileSpec();
1519 const auto error = LoadCachedExecutable(
1520 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1521 if (error.Success()) {
1522 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1523 module_spec.GetPlatformFileSpec() = platform_spec;
1524 }
1525
1526 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001527}
1528
Zachary Turner97206d52017-05-12 04:51:55 +00001529Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1531 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1532 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1533 [&](const ModuleSpec &spec) {
1534 return remote_platform.ResolveExecutable(
1535 spec, module_sp, module_search_paths_ptr);
1536 },
1537 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001538}
1539
Zachary Turner97206d52017-05-12 04:51:55 +00001540Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1541 Process *process,
1542 lldb::ModuleSP &module_sp,
1543 const ModuleResolver &module_resolver,
1544 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 // Get module information from a target.
1546 ModuleSpec resolved_module_spec;
1547 bool got_module_spec = false;
1548 if (process) {
1549 // Try to get module information from the process
1550 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1551 module_spec.GetArchitecture(),
1552 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001553 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1555 got_module_spec = true;
1556 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001557 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001558 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001559
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001560 if (!module_spec.GetArchitecture().IsValid()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001561 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001562 // No valid architecture was specified, ask the platform for the
1563 // architectures that we should be using (in the correct order) and see if
1564 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001565 ModuleSpec arch_module_spec(module_spec);
1566 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1567 idx, arch_module_spec.GetArchitecture());
1568 ++idx) {
1569 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1570 nullptr, nullptr);
1571 // Did we find an executable using one of the
1572 if (error.Success() && module_sp)
1573 break;
1574 }
1575 if (module_sp)
1576 got_module_spec = true;
1577 }
1578
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001580 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1582 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001583 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001584 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1585 return module_resolver(module_spec);
1586 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001587 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001589
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 // If we are looking for a specific UUID, make sure resolved_module_spec has
1591 // the same one before we search.
1592 if (module_spec.GetUUID().IsValid()) {
1593 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1594 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001595
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 // Trying to find a module by UUID on local file system.
1597 const auto error = module_resolver(resolved_module_spec);
1598 if (error.Fail()) {
1599 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001600 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001602
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001604}
1605
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1607 lldb::ModuleSP &module_sp,
1608 bool *did_create_ptr) {
1609 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1610 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001611 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612
1613 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1614
1615 // Check local cache for a module.
1616 auto error = m_module_cache->GetAndPut(
1617 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1618 [this](const ModuleSpec &module_spec,
1619 const FileSpec &tmp_download_file_spec) {
1620 return DownloadModuleSlice(
1621 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1622 module_spec.GetObjectSize(), tmp_download_file_spec);
1623
1624 },
1625 [this](const ModuleSP &module_sp,
1626 const FileSpec &tmp_download_file_spec) {
1627 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1628 },
1629 module_sp, did_create_ptr);
1630 if (error.Success())
1631 return true;
1632
1633 if (log)
1634 log->Printf("Platform::%s - module %s not found in local cache: %s",
1635 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1636 error.AsCString());
1637 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001638}
1639
Zachary Turner97206d52017-05-12 04:51:55 +00001640Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1641 const uint64_t src_offset,
1642 const uint64_t src_size,
1643 const FileSpec &dst_file_spec) {
1644 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001645
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001646 std::error_code EC;
1647 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None);
1648 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 error.SetErrorStringWithFormat("unable to open destination file: %s",
1650 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001651 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001653
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1655 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001656
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657 if (error.Fail()) {
1658 error.SetErrorStringWithFormat("unable to open source file: %s",
1659 error.AsCString());
1660 return error;
1661 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001662
Kate Stoneb9c1b512016-09-06 20:57:50 +00001663 std::vector<char> buffer(1024);
1664 auto offset = src_offset;
1665 uint64_t total_bytes_read = 0;
1666 while (total_bytes_read < src_size) {
1667 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1668 src_size - total_bytes_read);
1669 const uint64_t n_read =
1670 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001671 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001672 break;
1673 if (n_read == 0) {
1674 error.SetErrorString("read 0 bytes");
1675 break;
1676 }
1677 offset += n_read;
1678 total_bytes_read += n_read;
1679 dst.write(&buffer[0], n_read);
1680 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001681
Zachary Turner97206d52017-05-12 04:51:55 +00001682 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 CloseFile(src_fd, close_error); // Ignoring close error.
1684
1685 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001686}
1687
Zachary Turner97206d52017-05-12 04:51:55 +00001688Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1689 const FileSpec &dst_file_spec) {
1690 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001691 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001692}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001693
Kate Stoneb9c1b512016-09-06 20:57:50 +00001694FileSpec Platform::GetModuleCacheRoot() {
1695 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1696 dir_spec.AppendPathComponent(GetName().AsCString());
1697 return dir_spec;
1698}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001699
Kate Stoneb9c1b512016-09-06 20:57:50 +00001700const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001701
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1703 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1704 return s_default_unix_signals_sp;
1705}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001706
Zachary Turnera89ce432019-03-06 18:20:23 +00001707UnixSignalsSP Platform::GetUnixSignals() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708 if (IsHost())
Zachary Turnera89ce432019-03-06 18:20:23 +00001709 return UnixSignals::CreateForHost();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 return GetRemoteUnixSignals();
1711}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001712
Kate Stoneb9c1b512016-09-06 20:57:50 +00001713uint32_t Platform::LoadImage(lldb_private::Process *process,
1714 const lldb_private::FileSpec &local_file,
1715 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001716 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001717 if (local_file && remote_file) {
1718 // Both local and remote file was specified. Install the local file to the
1719 // given location.
1720 if (IsRemote() || local_file != remote_file) {
1721 error = Install(local_file, remote_file);
1722 if (error.Fail())
1723 return LLDB_INVALID_IMAGE_TOKEN;
1724 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001725 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001727
Kate Stoneb9c1b512016-09-06 20:57:50 +00001728 if (local_file) {
1729 // Only local file was specified. Install it to the current working
1730 // directory.
1731 FileSpec target_file = GetWorkingDirectory();
1732 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1733 if (IsRemote() || local_file != target_file) {
1734 error = Install(local_file, target_file);
1735 if (error.Fail())
1736 return LLDB_INVALID_IMAGE_TOKEN;
1737 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001738 return DoLoadImage(process, target_file, nullptr, error);
1739 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001740
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 if (remote_file) {
1742 // Only remote file was specified so we don't have to do any copying
Jim Ingham0d231f72018-06-28 20:02:11 +00001743 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001745
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 error.SetErrorString("Neither local nor remote file was specified");
1747 return LLDB_INVALID_IMAGE_TOKEN;
1748}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001749
Kate Stoneb9c1b512016-09-06 20:57:50 +00001750uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1751 const lldb_private::FileSpec &remote_file,
Jim Ingham0d231f72018-06-28 20:02:11 +00001752 const std::vector<std::string> *paths,
1753 lldb_private::Status &error,
1754 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001755 error.SetErrorString("LoadImage is not supported on the current platform");
1756 return LLDB_INVALID_IMAGE_TOKEN;
1757}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001758
Jim Ingham0d231f72018-06-28 20:02:11 +00001759uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1760 const lldb_private::FileSpec &remote_filename,
1761 const std::vector<std::string> &paths,
1762 lldb_private::Status &error,
1763 lldb_private::FileSpec *loaded_path)
1764{
1765 FileSpec file_to_use;
1766 if (remote_filename.IsAbsolute())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001767 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1768
1769 remote_filename.GetPathStyle());
Jim Ingham0d231f72018-06-28 20:02:11 +00001770 else
1771 file_to_use = remote_filename;
1772
1773 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1774}
1775
Zachary Turner97206d52017-05-12 04:51:55 +00001776Status Platform::UnloadImage(lldb_private::Process *process,
1777 uint32_t image_token) {
1778 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001780
Zachary Turner31659452016-11-17 21:15:14 +00001781lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1782 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783 lldb_private::Debugger &debugger,
1784 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001785 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001787
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 if (!target) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001789 ArchSpec arch;
1790 if (target && target->GetArchitecture().IsValid())
1791 arch = target->GetArchitecture();
1792 else
1793 arch = Target::GetDefaultArchitecture();
1794
1795 const char *triple = "";
1796 if (arch.IsValid())
1797 triple = arch.GetTriple().getTriple().c_str();
1798
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001800 error = debugger.GetTargetList().CreateTarget(
Jason Molendadf9f7962018-10-23 23:45:56 +00001801 debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802 target = new_target_sp.get();
1803 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001804
Kate Stoneb9c1b512016-09-06 20:57:50 +00001805 if (!target || error.Fail())
1806 return nullptr;
1807
1808 debugger.GetTargetList().SetSelectedTarget(target);
1809
1810 lldb::ProcessSP process_sp =
1811 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1812 if (!process_sp)
1813 return nullptr;
1814
1815 error =
1816 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1817 if (error.Fail())
1818 return nullptr;
1819
1820 return process_sp;
1821}
1822
1823size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001824 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 error.Clear();
1826 return 0;
1827}
1828
1829size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1830 BreakpointSite *bp_site) {
1831 ArchSpec arch = target.GetArchitecture();
1832 const uint8_t *trap_opcode = nullptr;
1833 size_t trap_opcode_size = 0;
1834
1835 switch (arch.GetMachine()) {
1836 case llvm::Triple::aarch64: {
1837 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1838 trap_opcode = g_aarch64_opcode;
1839 trap_opcode_size = sizeof(g_aarch64_opcode);
1840 } break;
1841
1842 // TODO: support big-endian arm and thumb trap codes.
1843 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001844 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1845 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001846 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1847 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1848
1849 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001850 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001851
1852 if (bp_loc_sp) {
1853 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001854 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001855 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001856 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001857 }
1858
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001859 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001860 trap_opcode = g_thumb_breakpoint_opcode;
1861 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1862 } else {
1863 trap_opcode = g_arm_breakpoint_opcode;
1864 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1865 }
1866 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001867
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868 case llvm::Triple::mips:
1869 case llvm::Triple::mips64: {
1870 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1871 trap_opcode = g_hex_opcode;
1872 trap_opcode_size = sizeof(g_hex_opcode);
1873 } break;
1874
1875 case llvm::Triple::mipsel:
1876 case llvm::Triple::mips64el: {
1877 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1878 trap_opcode = g_hex_opcode;
1879 trap_opcode_size = sizeof(g_hex_opcode);
1880 } break;
1881
1882 case llvm::Triple::systemz: {
1883 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1884 trap_opcode = g_hex_opcode;
1885 trap_opcode_size = sizeof(g_hex_opcode);
1886 } break;
1887
1888 case llvm::Triple::hexagon: {
1889 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1890 trap_opcode = g_hex_opcode;
1891 trap_opcode_size = sizeof(g_hex_opcode);
1892 } break;
1893
1894 case llvm::Triple::ppc:
1895 case llvm::Triple::ppc64: {
1896 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1897 trap_opcode = g_ppc_opcode;
1898 trap_opcode_size = sizeof(g_ppc_opcode);
1899 } break;
1900
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001901 case llvm::Triple::ppc64le: {
1902 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1903 trap_opcode = g_ppc64le_opcode;
1904 trap_opcode_size = sizeof(g_ppc64le_opcode);
1905 } break;
1906
Kate Stoneb9c1b512016-09-06 20:57:50 +00001907 case llvm::Triple::x86:
1908 case llvm::Triple::x86_64: {
1909 static const uint8_t g_i386_opcode[] = {0xCC};
1910 trap_opcode = g_i386_opcode;
1911 trap_opcode_size = sizeof(g_i386_opcode);
1912 } break;
1913
1914 default:
David Blaikiea322f362017-01-06 00:38:06 +00001915 llvm_unreachable(
1916 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001917 }
1918
1919 assert(bp_site);
1920 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1921 return trap_opcode_size;
1922
1923 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001924}