blob: acafa8b35c59e7ba3edc80fbb38c2ca1e6dc0578 [file] [log] [blame]
Greg Claytone996fd32011-03-08 22:40:15 +00001//===-- Platform.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Claytone996fd32011-03-08 22:40:15 +000010// C Includes
11// C++ Includes
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000012#include <algorithm>
13#include <fstream>
14#include <vector>
15
Greg Claytone996fd32011-03-08 22:40:15 +000016// Other libraries and framework includes
Eugene Zelenko9394d7722016-02-18 00:10:17 +000017#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
19
Greg Claytone996fd32011-03-08 22:40:15 +000020// Project includes
Greg Clayton4116e932012-05-15 02:33:01 +000021#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000022#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000023#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000024#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000026#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000027#include "lldb/Core/StreamFile.h"
Enrico Granatad7a83a92015-02-10 03:06:24 +000028#include "lldb/Core/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000029#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000030#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000031#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000032#include "lldb/Host/HostInfo.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000033#include "lldb/Interpreter/OptionValueProperties.h"
34#include "lldb/Interpreter/Property.h"
35#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000036#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000037#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000038#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000039#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000040#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000041#include "lldb/Utility/DataBufferHeap.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000042#include "lldb/Utility/Error.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000043#include "lldb/Utility/Log.h"
Greg Claytone996fd32011-03-08 22:40:15 +000044
Zachary Turner7d86ee52017-03-08 17:56:08 +000045#include "llvm/Support/FileSystem.h"
46
Robert Flack96ad3de2015-05-09 15:53:31 +000047// Define these constants from POSIX mman.h rather than include the file
48// so that they will be correct even when compiled on Linux.
49#define MAP_PRIVATE 2
50#define MAP_ANON 0x1000
51
Greg Claytone996fd32011-03-08 22:40:15 +000052using namespace lldb;
53using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000054
55static uint32_t g_initialize_count = 0;
56
Greg Claytone996fd32011-03-08 22:40:15 +000057// Use a singleton function for g_local_platform_sp to avoid init
58// constructors since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000059static PlatformSP &GetHostPlatformSP() {
60 static PlatformSP g_platform_sp;
61 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000062}
63
Kate Stoneb9c1b512016-09-06 20:57:50 +000064const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000065
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000066namespace {
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068PropertyDefinition g_properties[] = {
69 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
70 nullptr, "Use module cache."},
71 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
72 nullptr, "Root directory for cached modules."},
73 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075enum { ePropertyUseModuleCache, ePropertyModuleCacheDirectory };
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079ConstString PlatformProperties::GetSettingName() {
80 static ConstString g_setting_name("platform");
81 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084PlatformProperties::PlatformProperties() {
85 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
86 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 auto module_cache_dir = GetModuleCacheDirectory();
89 if (module_cache_dir)
90 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 llvm::SmallString<64> user_home_dir;
93 if (!llvm::sys::path::home_directory(user_home_dir))
94 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 module_cache_dir = FileSpec(user_home_dir.c_str(), false);
97 module_cache_dir.AppendPathComponent(".lldb");
98 module_cache_dir.AppendPathComponent("module_cache");
99 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102bool PlatformProperties::GetUseModuleCache() const {
103 const auto idx = ePropertyUseModuleCache;
104 return m_collection_sp->GetPropertyAtIndexAsBoolean(
105 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000106}
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
109 return m_collection_sp->SetPropertyAtIndexAsBoolean(
110 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000111}
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113FileSpec PlatformProperties::GetModuleCacheDirectory() const {
114 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
115 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
119 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
120 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000121}
122
Greg Claytone996fd32011-03-08 22:40:15 +0000123//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000125///
126/// There should only be one of these for each host that LLDB runs
127/// upon that should be statically compiled in and registered using
128/// preprocessor macros or other similar build mechanisms.
129///
130/// This platform will be used as the default platform when launching
131/// or attaching to processes unless another platform is specified.
132//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
134
135static std::vector<PlatformSP> &GetPlatformList() {
136 static std::vector<PlatformSP> g_platform_list;
137 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140static std::recursive_mutex &GetPlatformListMutex() {
141 static std::recursive_mutex g_mutex;
142 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147void Platform::Terminate() {
148 if (g_initialize_count > 0) {
149 if (--g_initialize_count == 0) {
150 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
151 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
157 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
158 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000159}
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
162 // The native platform should use its static void Platform::Initialize()
163 // function to register itself as the native platform.
164 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 if (platform_sp) {
167 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
168 GetPlatformList().push_back(platform_sp);
169 }
Greg Claytone996fd32011-03-08 22:40:15 +0000170}
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172Error Platform::GetFileWithUUID(const FileSpec &platform_file,
173 const UUID *uuid_ptr, FileSpec &local_file) {
174 // Default to the local case
175 local_file = platform_file;
Mehdi Aminic1edf562016-11-11 04:29:25 +0000176 return Error();
Greg Claytone996fd32011-03-08 22:40:15 +0000177}
178
Greg Clayton91c0e742013-01-11 23:44:27 +0000179FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180Platform::LocateExecutableScriptingResources(Target *target, Module &module,
181 Stream *feedback_stream) {
182 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000183}
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185// PlatformSP
186// Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000187//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000188// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000189// if (plugin_name)
190// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191// create_callback =
192// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000193// if (create_callback)
194// {
195// ArchSpec arch;
196// if (process)
197// {
198// arch = process->GetTarget().GetArchitecture();
199// }
200// PlatformSP platform_sp(create_callback(process, &arch));
201// if (platform_sp)
202// return platform_sp;
203// }
204// }
205// else
206// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207// for (uint32_t idx = 0; (create_callback =
208// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
209// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000210// {
211// PlatformSP platform_sp(create_callback(process, nullptr));
212// if (platform_sp)
213// return platform_sp;
214// }
215// }
216// return PlatformSP();
217//}
Jason Molenda1c627542013-04-05 01:03:25 +0000218
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219Error Platform::GetSharedModule(const ModuleSpec &module_spec, Process *process,
220 ModuleSP &module_sp,
221 const FileSpecList *module_search_paths_ptr,
222 ModuleSP *old_module_sp_ptr,
223 bool *did_create_ptr) {
224 if (IsHost())
225 return ModuleList::GetSharedModule(
226 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
227 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 return GetRemoteSharedModule(module_spec, process, module_sp,
230 [&](const ModuleSpec &spec) {
231 Error error = ModuleList::GetSharedModule(
232 spec, module_sp, module_search_paths_ptr,
233 old_module_sp_ptr, did_create_ptr, false);
234 if (error.Success() && module_sp)
235 module_sp->SetPlatformFileSpec(
236 spec.GetFileSpec());
237 return error;
238 },
239 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
243 const ArchSpec &arch, ModuleSpec &module_spec) {
244 ModuleSpecList module_specs;
245 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
246 module_specs) == 0)
247 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 ModuleSpec matched_module_spec;
250 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
251 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254PlatformSP Platform::Find(const ConstString &name) {
255 if (name) {
256 static ConstString g_host_platform_name("host");
257 if (name == g_host_platform_name)
258 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
261 for (const auto &platform_sp : GetPlatformList()) {
262 if (platform_sp->GetName() == name)
263 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000264 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 }
266 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269PlatformSP Platform::Create(const ConstString &name, Error &error) {
270 PlatformCreateInstance create_callback = nullptr;
271 lldb::PlatformSP platform_sp;
272 if (name) {
273 static ConstString g_host_platform_name("host");
274 if (name == g_host_platform_name)
275 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 create_callback =
278 PluginManager::GetPlatformCreateCallbackForPluginName(name);
279 if (create_callback)
280 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000281 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 error.SetErrorStringWithFormat(
283 "unable to find a plug-in for the platform named \"%s\"",
284 name.GetCString());
285 } else
286 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 if (platform_sp) {
289 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
290 GetPlatformList().push_back(platform_sp);
291 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000292
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000294}
295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
297 Error &error) {
298 lldb::PlatformSP platform_sp;
299 if (arch.IsValid()) {
300 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000301 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 // First try exact arch matches across all platforms already created
303 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
304 for (const auto &platform_sp : GetPlatformList()) {
305 if (platform_sp->IsCompatibleArchitecture(arch, true,
306 platform_arch_ptr))
307 return platform_sp;
308 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000309
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 // Next try compatible arch matches across all platforms already created
311 for (const auto &platform_sp : GetPlatformList()) {
312 if (platform_sp->IsCompatibleArchitecture(arch, false,
313 platform_arch_ptr))
314 return platform_sp;
315 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000316 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317
318 PlatformCreateInstance create_callback;
319 // First try exact arch matches across all platform plug-ins
320 uint32_t idx;
321 for (idx = 0; (create_callback =
322 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
323 ++idx) {
324 if (create_callback) {
325 platform_sp = create_callback(false, &arch);
326 if (platform_sp &&
327 platform_sp->IsCompatibleArchitecture(arch, true,
328 platform_arch_ptr)) {
329 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
330 GetPlatformList().push_back(platform_sp);
331 return platform_sp;
332 }
333 }
334 }
335 // Next try compatible arch matches across all platform plug-ins
336 for (idx = 0; (create_callback =
337 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
338 ++idx) {
339 if (create_callback) {
340 platform_sp = create_callback(false, &arch);
341 if (platform_sp &&
342 platform_sp->IsCompatibleArchitecture(arch, false,
343 platform_arch_ptr)) {
344 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
345 GetPlatformList().push_back(platform_sp);
346 return platform_sp;
347 }
348 }
349 }
350 } else
351 error.SetErrorString("invalid platform name");
352 if (platform_arch_ptr)
353 platform_arch_ptr->Clear();
354 platform_sp.reset();
355 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000356}
357
Greg Claytone996fd32011-03-08 22:40:15 +0000358//------------------------------------------------------------------
359/// Default Constructor
360//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000361Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 : m_is_host(is_host), m_os_version_set_while_connected(false),
363 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
364 m_working_dir(), m_remote_url(), m_name(), m_major_os_version(UINT32_MAX),
365 m_minor_os_version(UINT32_MAX), m_update_os_version(UINT32_MAX),
366 m_system_arch(), m_mutex(), m_uid_map(), m_gid_map(),
367 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
368 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
369 m_ignores_remote_hostname(false), m_trap_handlers(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000370 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 m_module_cache(llvm::make_unique<ModuleCache>()) {
372 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
373 if (log)
374 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000375}
376
377//------------------------------------------------------------------
378/// Destructor.
379///
380/// The destructor is virtual since this class is designed to be
381/// inherited from by the plug-in instance.
382//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383Platform::~Platform() {
384 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
385 if (log)
386 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389void Platform::GetStatus(Stream &strm) {
390 uint32_t major = UINT32_MAX;
391 uint32_t minor = UINT32_MAX;
392 uint32_t update = UINT32_MAX;
393 std::string s;
394 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 ArchSpec arch(GetSystemArchitecture());
397 if (arch.IsValid()) {
398 if (!arch.GetTriple().str().empty()) {
399 strm.Printf(" Triple: ");
400 arch.DumpTriple(strm);
401 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000402 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 if (GetOSVersion(major, minor, update)) {
406 strm.Printf("OS Version: %u", major);
407 if (minor != UINT32_MAX)
408 strm.Printf(".%u", minor);
409 if (update != UINT32_MAX)
410 strm.Printf(".%u", update);
Greg Clayton1cb64962011-03-24 04:28:38 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 if (GetOSBuildString(s))
413 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 strm.EOL();
416 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 if (GetOSKernelDescription(s))
419 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 if (IsHost()) {
422 strm.Printf(" Hostname: %s\n", GetHostname());
423 } else {
424 const bool is_connected = IsConnected();
425 if (is_connected)
426 strm.Printf(" Hostname: %s\n", GetHostname());
427 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
428 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 if (GetWorkingDirectory()) {
431 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
432 }
433 if (!IsConnected())
434 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 std::string specific_info(GetPlatformSpecificConnectionInformation());
437
438 if (!specific_info.empty())
439 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000440}
441
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442bool Platform::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update,
443 Process *process) {
444 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000445
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 bool success = m_major_os_version != UINT32_MAX;
447 if (IsHost()) {
448 if (!success) {
449 // We have a local host platform
450 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version,
451 m_update_os_version);
452 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000453 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 } else {
455 // We have a remote platform. We can only fetch the remote
456 // OS version if we are connected, and we don't want to do it
457 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000458
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 bool fetch = false;
462 if (success) {
463 // We have valid OS version info, check to make sure it wasn't
464 // manually set prior to connecting. If it was manually set prior
465 // to connecting, then lets fetch the actual OS version info
466 // if we are now connected.
467 if (is_connected && !m_os_version_set_while_connected)
468 fetch = true;
469 } else {
470 // We don't have valid OS version info, fetch it if we are connected
471 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000472 }
473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 if (fetch) {
475 success = GetRemoteOSVersion();
476 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000477 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 }
479
480 if (success) {
481 major = m_major_os_version;
482 minor = m_minor_os_version;
483 update = m_update_os_version;
484 } else if (process) {
485 // Check with the process in case it can answer the question if
486 // a process was provided
487 return process->GetHostOSVersion(major, minor, update);
488 }
489 return success;
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))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 return FileSpec{};
Pavel Labath1d5855b2017-01-23 15:56:45 +0000530 else
531 return FileSpec(cwd, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 } else {
533 if (!m_working_dir)
534 m_working_dir = GetRemoteWorkingDirectory();
535 return m_working_dir;
536 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000537}
538
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539struct RecurseCopyBaton {
540 const FileSpec &dst;
541 Platform *platform_ptr;
542 Error error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000543};
544
Greg Claytonfbb76342013-11-20 21:07:01 +0000545static FileSpec::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000546RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 const FileSpec &src) {
548 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000549 namespace fs = llvm::sys::fs;
550 switch (ft) {
551 case fs::file_type::fifo_file:
552 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553 // we have no way to copy pipes and sockets - ignore them and continue
554 return FileSpec::eEnumerateDirectoryResultNext;
555 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000556
Zachary Turner7d86ee52017-03-08 17:56:08 +0000557 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 // make the new directory and get in there
559 FileSpec dst_dir = rc_baton->dst;
560 if (!dst_dir.GetFilename())
561 dst_dir.GetFilename() = src.GetLastPathComponent();
562 Error error = rc_baton->platform_ptr->MakeDirectory(
563 dst_dir, lldb::eFilePermissionsDirectoryDefault);
564 if (error.Fail()) {
565 rc_baton->error.SetErrorStringWithFormat(
566 "unable to setup directory %s on remote end", dst_dir.GetCString());
567 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000568 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569
570 // now recurse
571 std::string src_dir_path(src.GetPath());
572
573 // Make a filespec that only fills in the directory of a FileSpec so
574 // when we enumerate we can quickly fill in the filename for dst copies
575 FileSpec recurse_dst;
576 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
577 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr, Error()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000578 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 RecurseCopy_Callback, &rc_baton2);
580 if (rc_baton2.error.Fail()) {
581 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
582 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
583 }
584 return FileSpec::eEnumerateDirectoryResultNext;
585 } break;
586
Zachary Turner7d86ee52017-03-08 17:56:08 +0000587 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 // copy the file and keep going
589 FileSpec dst_file = rc_baton->dst;
590 if (!dst_file.GetFilename())
591 dst_file.GetFilename() = src.GetFilename();
592
593 FileSpec src_resolved;
594
595 rc_baton->error = FileSystem::Readlink(src, src_resolved);
596
597 if (rc_baton->error.Fail())
598 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
599
600 rc_baton->error =
601 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
602
603 if (rc_baton->error.Fail())
604 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
605
606 return FileSpec::eEnumerateDirectoryResultNext;
607 } break;
608
Zachary Turner7d86ee52017-03-08 17:56:08 +0000609 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 // copy the file and keep going
611 FileSpec dst_file = rc_baton->dst;
612 if (!dst_file.GetFilename())
613 dst_file.GetFilename() = src.GetFilename();
614 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
615 if (err.Fail()) {
616 rc_baton->error.SetErrorString(err.AsCString());
617 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
618 }
619 return FileSpec::eEnumerateDirectoryResultNext;
620 } break;
621
Zachary Turner7d86ee52017-03-08 17:56:08 +0000622 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 rc_baton->error.SetErrorStringWithFormat(
624 "invalid file detected during copy: %s", src.GetPath().c_str());
625 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
626 break;
627 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000628 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000629}
630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631Error Platform::Install(const FileSpec &src, const FileSpec &dst) {
632 Error error;
633
634 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
635 if (log)
636 log->Printf("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(),
637 dst.GetPath().c_str());
638 FileSpec fixed_dst(dst);
639
640 if (!fixed_dst.GetFilename())
641 fixed_dst.GetFilename() = src.GetFilename();
642
643 FileSpec working_dir = GetWorkingDirectory();
644
645 if (dst) {
646 if (dst.GetDirectory()) {
647 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
648 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
649 fixed_dst.GetDirectory() = dst.GetDirectory();
650 }
651 // If the fixed destination file doesn't have a directory yet,
652 // then we must have a relative path. We will resolve this relative
653 // path against the platform's working directory
654 if (!fixed_dst.GetDirectory()) {
655 FileSpec relative_spec;
656 std::string path;
657 if (working_dir) {
658 relative_spec = working_dir;
659 relative_spec.AppendPathComponent(dst.GetPath());
660 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
661 } else {
662 error.SetErrorStringWithFormat(
663 "platform working directory must be valid for relative path '%s'",
664 dst.GetPath().c_str());
665 return error;
666 }
667 }
668 } else {
669 if (working_dir) {
670 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
671 } else {
672 error.SetErrorStringWithFormat(
673 "platform working directory must be valid for relative path '%s'",
674 dst.GetPath().c_str());
675 return error;
676 }
677 }
678 } else {
679 if (working_dir) {
680 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
681 } else {
682 error.SetErrorStringWithFormat("platform working directory must be valid "
683 "when destination directory is empty");
684 return error;
685 }
686 }
687
688 if (log)
689 log->Printf("Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
690 src.GetPath().c_str(), dst.GetPath().c_str(),
691 fixed_dst.GetPath().c_str());
692
693 if (GetSupportsRSync()) {
694 error = PutFile(src, dst);
695 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000696 namespace fs = llvm::sys::fs;
697 switch (fs::get_file_type(src.GetPath(), false)) {
698 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000699 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700 uint32_t permissions = src.GetPermissions();
701 if (permissions == 0)
702 permissions = eFilePermissionsDirectoryDefault;
703 error = MakeDirectory(fixed_dst, permissions);
704 if (error.Success()) {
705 // Make a filespec that only fills in the directory of a FileSpec so
706 // when we enumerate we can quickly fill in the filename for dst copies
707 FileSpec recurse_dst;
708 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
709 std::string src_dir_path(src.GetPath());
710 RecurseCopyBaton baton = {recurse_dst, this, Error()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000711 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 RecurseCopy_Callback, &baton);
713 return baton.error;
714 }
715 } break;
716
Zachary Turner7d86ee52017-03-08 17:56:08 +0000717 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000718 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 error = PutFile(src, fixed_dst);
720 break;
721
Zachary Turner7d86ee52017-03-08 17:56:08 +0000722 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000723 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 FileSpec src_resolved;
725 error = FileSystem::Readlink(src, src_resolved);
726 if (error.Success())
727 error = CreateSymlink(dst, src_resolved);
728 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000729 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 error.SetErrorString("platform install doesn't handle pipes");
731 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000732 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 error.SetErrorString("platform install doesn't handle sockets");
734 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000735 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000736 error.SetErrorString(
737 "platform install doesn't handle non file or directory items");
738 break;
739 }
740 }
741 return error;
742}
743
744bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
745 if (IsHost()) {
746 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000747 LLDB_LOG(log, "{0}", file_spec);
748 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
749 LLDB_LOG(log, "error: {0}", ec.message());
750 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000752 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 } else {
754 m_working_dir.Clear();
755 return SetRemoteWorkingDirectory(file_spec);
756 }
757}
758
759Error Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions) {
760 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000761 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 else {
Greg Claytonfbb76342013-11-20 21:07:01 +0000763 Error error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
765 GetPluginName().GetCString(),
766 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000767 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000769}
770
Kate Stoneb9c1b512016-09-06 20:57:50 +0000771Error Platform::GetFilePermissions(const FileSpec &file_spec,
772 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000773 if (IsHost()) {
774 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
775 if (Value)
776 file_permissions = Value.get();
777 return Error(Value.getError());
778 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 Error error;
780 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
781 GetPluginName().GetCString(),
782 LLVM_PRETTY_FUNCTION);
783 return error;
784 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000785}
786
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787Error Platform::SetFilePermissions(const FileSpec &file_spec,
788 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000789 if (IsHost()) {
790 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
791 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
792 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 Error error;
794 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
795 GetPluginName().GetCString(),
796 LLVM_PRETTY_FUNCTION);
797 return error;
798 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000799}
800
Kate Stoneb9c1b512016-09-06 20:57:50 +0000801ConstString Platform::GetName() { return GetPluginName(); }
802
803const char *Platform::GetHostname() {
804 if (IsHost())
805 return "127.0.0.1";
806
807 if (m_name.empty())
808 return nullptr;
809 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000810}
811
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812ConstString Platform::GetFullNameForDylib(ConstString basename) {
813 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000814}
815
Kate Stoneb9c1b512016-09-06 20:57:50 +0000816bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
817 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
818 if (log)
819 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
Chaoren Lind3173f32015-05-29 19:52:29 +0000820 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 m_working_dir = working_dir;
822 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000823}
824
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825const char *Platform::GetUserName(uint32_t uid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000826#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 const char *user_name = GetCachedUserName(uid);
828 if (user_name)
829 return user_name;
830 if (IsHost()) {
831 std::string name;
832 if (HostInfo::LookupUserName(uid, name))
833 return SetCachedUserName(uid, name.c_str(), name.size());
834 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000835#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 return nullptr;
Greg Clayton1cb64962011-03-24 04:28:38 +0000837}
838
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839const char *Platform::GetGroupName(uint32_t gid) {
Zachary Turnerb245eca2014-08-21 20:02:17 +0000840#if !defined(LLDB_DISABLE_POSIX)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 const char *group_name = GetCachedGroupName(gid);
842 if (group_name)
843 return group_name;
844 if (IsHost()) {
845 std::string name;
846 if (HostInfo::LookupGroupName(gid, name))
847 return SetCachedGroupName(gid, name.c_str(), name.size());
848 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000849#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 return nullptr;
Greg Clayton32e0a752011-03-30 18:16:51 +0000851}
Greg Clayton1cb64962011-03-24 04:28:38 +0000852
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853bool Platform::SetOSVersion(uint32_t major, uint32_t minor, uint32_t update) {
854 if (IsHost()) {
855 // We don't need anyone setting the OS version for the host platform,
856 // we should be able to figure it out by calling
857 // HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000858 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 } else {
860 // We have a remote platform, allow setting the target OS version if
861 // we aren't connected, since if we are connected, we should be able to
862 // request the remote OS version from the connected platform.
863 if (IsConnected())
864 return false;
865 else {
866 // We aren't connected and we might want to set the OS version
867 // ahead of time before we connect so we can peruse files and
868 // use a local SDK or PDK cache of support files to disassemble
869 // or do other things.
870 m_major_os_version = major;
871 m_minor_os_version = minor;
872 m_update_os_version = update;
873 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000874 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 }
876 return false;
877}
878
879Error Platform::ResolveExecutable(const ModuleSpec &module_spec,
880 lldb::ModuleSP &exe_module_sp,
881 const FileSpecList *module_search_paths_ptr) {
882 Error error;
883 if (module_spec.GetFileSpec().Exists()) {
884 if (module_spec.GetArchitecture().IsValid()) {
885 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
886 module_search_paths_ptr, nullptr,
887 nullptr);
888 } else {
889 // No valid architecture was specified, ask the platform for
890 // the architectures that we should be using (in the correct order)
891 // and see if we can find a match that way
892 ModuleSpec arch_module_spec(module_spec);
893 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
894 idx, arch_module_spec.GetArchitecture());
895 ++idx) {
896 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
897 module_search_paths_ptr, nullptr,
898 nullptr);
899 // Did we find an executable using one of the
900 if (error.Success() && exe_module_sp)
901 break;
902 }
Greg Claytone996fd32011-03-08 22:40:15 +0000903 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904 } else {
905 error.SetErrorStringWithFormat("'%s' does not exist",
906 module_spec.GetFileSpec().GetPath().c_str());
907 }
908 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000909}
910
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911Error Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
912 FileSpec &sym_file) {
913 Error error;
914 if (sym_spec.GetSymbolFileSpec().Exists())
915 sym_file = sym_spec.GetSymbolFileSpec();
916 else
917 error.SetErrorString("unable to resolve symbol file");
918 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000919}
920
Kate Stoneb9c1b512016-09-06 20:57:50 +0000921bool Platform::ResolveRemotePath(const FileSpec &platform_path,
922 FileSpec &resolved_platform_path) {
923 resolved_platform_path = platform_path;
924 return resolved_platform_path.ResolvePath();
925}
926
927const ArchSpec &Platform::GetSystemArchitecture() {
928 if (IsHost()) {
929 if (!m_system_arch.IsValid()) {
930 // We have a local host platform
931 m_system_arch = HostInfo::GetArchitecture();
932 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000933 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 } else {
935 // We have a remote platform. We can only fetch the remote
936 // system architecture if we are connected, and we don't want to do it
937 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000938
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000940
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 bool fetch = false;
942 if (m_system_arch.IsValid()) {
943 // We have valid OS version info, check to make sure it wasn't
944 // manually set prior to connecting. If it was manually set prior
945 // to connecting, then lets fetch the actual OS version info
946 // if we are now connected.
947 if (is_connected && !m_system_arch_set_while_connected)
948 fetch = true;
949 } else {
950 // We don't have valid OS version info, fetch it if we are connected
951 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953
954 if (fetch) {
955 m_system_arch = GetRemoteSystemArchitecture();
956 m_system_arch_set_while_connected = m_system_arch.IsValid();
957 }
958 }
959 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000960}
961
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962Error Platform::ConnectRemote(Args &args) {
963 Error error;
964 if (IsHost())
965 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
966 "the host platform and is always connected.",
967 GetPluginName().GetCString());
968 else
969 error.SetErrorStringWithFormat(
970 "Platform::ConnectRemote() is not supported by %s",
971 GetPluginName().GetCString());
972 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000973}
974
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975Error Platform::DisconnectRemote() {
976 Error error;
977 if (IsHost())
978 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
979 "the host platform and is always connected.",
980 GetPluginName().GetCString());
981 else
982 error.SetErrorStringWithFormat(
983 "Platform::DisconnectRemote() is not supported by %s",
984 GetPluginName().GetCString());
985 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000986}
Greg Clayton32e0a752011-03-30 18:16:51 +0000987
Kate Stoneb9c1b512016-09-06 20:57:50 +0000988bool Platform::GetProcessInfo(lldb::pid_t pid,
989 ProcessInstanceInfo &process_info) {
990 // Take care of the host case so that each subclass can just
991 // call this function to get the host functionality.
992 if (IsHost())
993 return Host::GetProcessInfo(pid, process_info);
994 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000995}
996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
998 ProcessInstanceInfoList &process_infos) {
999 // Take care of the host case so that each subclass can just
1000 // call this function to get the host functionality.
1001 uint32_t match_count = 0;
1002 if (IsHost())
1003 match_count = Host::FindProcesses(match_info, process_infos);
1004 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001005}
Greg Clayton8b82f082011-04-12 05:54:46 +00001006
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007Error Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1008 Error error;
1009 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1010 if (log)
1011 log->Printf("Platform::%s()", __FUNCTION__);
1012
1013 // Take care of the host case so that each subclass can just
1014 // call this function to get the host functionality.
1015 if (IsHost()) {
1016 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1017 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1018
1019 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1020 const bool is_localhost = true;
1021 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1022 const bool first_arg_is_full_shell_command = false;
1023 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1024 if (log) {
1025 const FileSpec &shell = launch_info.GetShell();
1026 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
1027 log->Printf(
1028 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1029 ", shell is '%s'",
1030 __FUNCTION__, num_resumes, shell_str);
1031 }
1032
1033 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1034 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1035 num_resumes))
1036 return error;
1037 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1038 error = ShellExpandArguments(launch_info);
1039 if (error.Fail()) {
1040 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1041 "consider launching with 'process "
1042 "launch'.",
1043 error.AsCString("unknown"));
1044 return error;
1045 }
1046 }
1047
Todd Fialaac33cc92014-10-09 01:02:08 +00001048 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001049 log->Printf("Platform::%s final launch_info resume count: %" PRIu32,
1050 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001051
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 error = Host::LaunchProcess(launch_info);
1053 } else
1054 error.SetErrorString(
1055 "base lldb_private::Platform class can't launch remote processes");
1056 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001057}
1058
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059Error Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
1060 if (IsHost())
1061 return Host::ShellExpandArguments(launch_info);
1062 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001063}
1064
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065Error Platform::KillProcess(const lldb::pid_t pid) {
1066 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1067 if (log)
1068 log->Printf("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001069
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070 // Try to find a process plugin to handle this Kill request. If we can't,
1071 // fall back to
1072 // the default OS implementation.
1073 size_t num_debuggers = Debugger::GetNumDebuggers();
1074 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1075 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1076 lldb_private::TargetList &targets = debugger->GetTargetList();
1077 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1078 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1079 if (process->GetID() == pid)
1080 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001083
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 if (!IsHost()) {
1085 return Error(
1086 "base lldb_private::Platform class can't kill remote processes unless "
1087 "they are controlled by a process plugin");
1088 }
1089 Host::Kill(pid, SIGTERM);
Mehdi Aminic1edf562016-11-11 04:29:25 +00001090 return Error();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001091}
1092
Greg Clayton8b82f082011-04-12 05:54:46 +00001093lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1095 Target *target, // Can be nullptr, if nullptr create a
1096 // new target, else use existing one
1097 Error &error) {
1098 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1099 if (log)
1100 log->Printf("Platform::%s entered (target %p)", __FUNCTION__,
1101 static_cast<void *>(target));
1102
1103 ProcessSP process_sp;
1104 // Make sure we stop at the entry point
1105 launch_info.GetFlags().Set(eLaunchFlagDebug);
1106 // We always launch the process we are going to debug in a separate process
1107 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1108 // about the target getting them as well.
1109 launch_info.SetLaunchInSeparateProcessGroup(true);
1110
1111 // Allow any StructuredData process-bound plugins to adjust the launch info
1112 // if needed
1113 size_t i = 0;
1114 bool iteration_complete = false;
1115 // Note iteration can't simply go until a nullptr callback is returned, as
1116 // it is valid for a plugin to not supply a filter.
1117 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1118 for (auto filter_callback = get_filter_func(i, iteration_complete);
1119 !iteration_complete;
1120 filter_callback = get_filter_func(++i, iteration_complete)) {
1121 if (filter_callback) {
1122 // Give this ProcessLaunchInfo filter a chance to adjust the launch
1123 // info.
1124 error = (*filter_callback)(launch_info, target);
1125 if (!error.Success()) {
1126 if (log)
1127 log->Printf("Platform::%s() StructuredDataPlugin launch "
1128 "filter failed.",
1129 __FUNCTION__);
1130 return process_sp;
1131 }
1132 }
1133 }
1134
1135 error = LaunchProcess(launch_info);
1136 if (error.Success()) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001137 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 log->Printf("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64
1139 ")",
1140 __FUNCTION__, launch_info.GetProcessID());
1141 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1142 ProcessAttachInfo attach_info(launch_info);
1143 process_sp = Attach(attach_info, debugger, target, error);
1144 if (process_sp) {
Todd Fialaac33cc92014-10-09 01:02:08 +00001145 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146 log->Printf("Platform::%s Attach() succeeded, Process plugin: %s",
1147 __FUNCTION__, process_sp->GetPluginName().AsCString());
1148 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001149
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 // Since we attached to the process, it will think it needs to detach
1151 // if the process object just goes away without an explicit call to
1152 // Process::Kill() or Process::Detach(), so let it know to kill the
1153 // process if this happens.
1154 process_sp->SetShouldDetach(false);
1155
1156 // If we didn't have any file actions, the pseudo terminal might
1157 // have been used where the slave side was given as the file to
1158 // open for stdin/out/err after we have already opened the master
1159 // so we can read/write stdin/out/err.
1160 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1161 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) {
1162 process_sp->SetSTDIOFileDescriptor(pty_fd);
1163 }
1164 } else {
1165 if (log)
1166 log->Printf("Platform::%s Attach() failed: %s", __FUNCTION__,
1167 error.AsCString());
1168 }
1169 } else {
1170 if (log)
1171 log->Printf("Platform::%s LaunchProcess() returned launch_info with "
1172 "invalid process id",
1173 __FUNCTION__);
1174 }
1175 } else {
1176 if (log)
1177 log->Printf("Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1178 error.AsCString());
1179 }
1180
1181 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001182}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001183
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001184lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1186 ArchSpec *platform_arch_ptr) {
1187 lldb::PlatformSP platform_sp;
1188 Error error;
1189 if (arch.IsValid())
1190 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1191 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001192}
1193
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001194//------------------------------------------------------------------
1195/// Lets a platform answer if it is compatible with a given
1196/// architecture and the target triple contained within.
1197//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001198bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1199 bool exact_arch_match,
1200 ArchSpec *compatible_arch_ptr) {
1201 // If the architecture is invalid, we must answer true...
1202 if (arch.IsValid()) {
1203 ArchSpec platform_arch;
1204 // Try for an exact architecture match first.
1205 if (exact_arch_match) {
1206 for (uint32_t arch_idx = 0;
1207 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1208 ++arch_idx) {
1209 if (arch.IsExactMatch(platform_arch)) {
1210 if (compatible_arch_ptr)
1211 *compatible_arch_ptr = platform_arch;
1212 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 }
1215 } else {
1216 for (uint32_t arch_idx = 0;
1217 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1218 ++arch_idx) {
1219 if (arch.IsCompatibleMatch(platform_arch)) {
1220 if (compatible_arch_ptr)
1221 *compatible_arch_ptr = platform_arch;
1222 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001223 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001225 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001226 }
1227 if (compatible_arch_ptr)
1228 compatible_arch_ptr->Clear();
1229 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001230}
1231
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232Error Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1233 uint32_t uid, uint32_t gid) {
1234 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
1235 if (log)
1236 log->Printf("[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001237
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238 uint32_t source_open_options =
1239 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001240 namespace fs = llvm::sys::fs;
1241 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001243
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1245 Error error;
1246 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())
1251 return Error("PutFile: unable to open source file");
1252 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)
1262 return Error("unable to open target file");
1263 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1264 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) {
1278 // We didn't write the correct number of bytes, so adjust
1279 // the file position in the source file we are reading from...
1280 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
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293Error Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1294 Error error("unimplemented");
1295 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001296}
1297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298Error Platform::CreateSymlink(
1299 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{
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302 Error error("unimplemented");
1303 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
1310Error Platform::Unlink(const FileSpec &path) {
1311 Error error("unimplemented");
1312 return error;
1313}
1314
1315uint64_t Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch,
1316 unsigned flags) {
1317 uint64_t flags_platform = 0;
1318 if (flags & eMmapFlagsPrivate)
1319 flags_platform |= MAP_PRIVATE;
1320 if (flags & eMmapFlagsAnon)
1321 flags_platform |= MAP_ANON;
1322 return flags_platform;
1323}
1324
1325lldb_private::Error Platform::RunShellCommand(
1326 const char *command, // Shouldn't be nullptr
1327 const FileSpec &
1328 working_dir, // Pass empty FileSpec to use the current working directory
1329 int *status_ptr, // Pass nullptr if you don't want the process exit status
1330 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1331 // process to exit
1332 std::string
1333 *command_output, // Pass nullptr if you don't want the command output
1334 uint32_t
1335 timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001336{
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 if (IsHost())
1338 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
1339 command_output, timeout_sec);
1340 else
1341 return Error("unimplemented");
1342}
1343
1344bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1345 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001346 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001347 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001348 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1349 if (!Result)
1350 return false;
1351 std::tie(high, low) = Result->words();
1352 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001353}
1354
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355void Platform::SetLocalCacheDirectory(const char *local) {
1356 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001357}
1358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359const char *Platform::GetLocalCacheDirectory() {
1360 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001361}
Greg Claytonfbb76342013-11-20 21:07:01 +00001362
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363static OptionDefinition g_rsync_option_table[] = {
1364 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
1365 nullptr, 0, eArgTypeNone, "Enable rsync."},
1366 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
1367 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1368 "Platform-specific options required for rsync to work."},
1369 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
1370 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1371 "Platform-specific rsync prefix put before the remote path."},
1372 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
1373 OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,
1374 "Do not automatically fill in the remote hostname when composing the "
1375 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001376};
1377
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378static OptionDefinition g_ssh_option_table[] = {
1379 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
1380 nullptr, 0, eArgTypeNone, "Enable SSH."},
1381 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
1382 nullptr, nullptr, 0, eArgTypeCommandName,
1383 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001384};
1385
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386static OptionDefinition g_caching_option_table[] = {
1387 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
1388 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePath,
1389 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001390};
1391
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001392llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001393 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001394}
1395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396void OptionGroupPlatformRSync::OptionParsingStarting(
1397 ExecutionContext *execution_context) {
1398 m_rsync = false;
1399 m_rsync_opts.clear();
1400 m_rsync_prefix.clear();
1401 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001402}
1403
1404lldb_private::Error
Todd Fialae1cfbc72016-08-11 23:51:28 +00001405OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001406 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407 ExecutionContext *execution_context) {
1408 Error error;
1409 char short_option = (char)GetDefinitions()[option_idx].short_option;
1410 switch (short_option) {
1411 case 'r':
1412 m_rsync = true;
1413 break;
1414
1415 case 'R':
1416 m_rsync_opts.assign(option_arg);
1417 break;
1418
1419 case 'P':
1420 m_rsync_prefix.assign(option_arg);
1421 break;
1422
1423 case 'i':
1424 m_ignores_remote_hostname = true;
1425 break;
1426
1427 default:
1428 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1429 break;
1430 }
1431
1432 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001433}
1434
Greg Clayton4116e932012-05-15 02:33:01 +00001435lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1437 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001438}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001439
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001440llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001441 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001442}
1443
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444void OptionGroupPlatformSSH::OptionParsingStarting(
1445 ExecutionContext *execution_context) {
1446 m_ssh = false;
1447 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001448}
1449
1450lldb_private::Error
Todd Fialae1cfbc72016-08-11 23:51:28 +00001451OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001452 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453 ExecutionContext *execution_context) {
1454 Error error;
1455 char short_option = (char)GetDefinitions()[option_idx].short_option;
1456 switch (short_option) {
1457 case 's':
1458 m_ssh = true;
1459 break;
1460
1461 case 'S':
1462 m_ssh_opts.assign(option_arg);
1463 break;
1464
1465 default:
1466 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1467 break;
1468 }
1469
1470 return error;
1471}
1472
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001473llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001474 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475}
1476
1477void OptionGroupPlatformCaching::OptionParsingStarting(
1478 ExecutionContext *execution_context) {
1479 m_cache_dir.clear();
1480}
1481
1482lldb_private::Error OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001483 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001484 ExecutionContext *execution_context) {
1485 Error error;
1486 char short_option = (char)GetDefinitions()[option_idx].short_option;
1487 switch (short_option) {
1488 case 'c':
1489 m_cache_dir.assign(option_arg);
1490 break;
1491
1492 default:
1493 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1494 break;
1495 }
1496
1497 return error;
1498}
1499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500size_t Platform::GetEnvironment(StringList &environment) {
1501 environment.Clear();
1502 return false;
1503}
1504
1505const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1506 if (!m_calculated_trap_handlers) {
1507 std::lock_guard<std::mutex> guard(m_mutex);
1508 if (!m_calculated_trap_handlers) {
1509 CalculateTrapHandlerSymbolNames();
1510 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001511 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512 }
1513 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001514}
1515
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516Error Platform::GetCachedExecutable(ModuleSpec &module_spec,
1517 lldb::ModuleSP &module_sp,
1518 const FileSpecList *module_search_paths_ptr,
1519 Platform &remote_platform) {
1520 const auto platform_spec = module_spec.GetFileSpec();
1521 const auto error = LoadCachedExecutable(
1522 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1523 if (error.Success()) {
1524 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1525 module_spec.GetPlatformFileSpec() = platform_spec;
1526 }
1527
1528 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001529}
1530
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531Error Platform::LoadCachedExecutable(
1532 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1533 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1534 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1535 [&](const ModuleSpec &spec) {
1536 return remote_platform.ResolveExecutable(
1537 spec, module_sp, module_search_paths_ptr);
1538 },
1539 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001540}
1541
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542Error Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1543 Process *process,
1544 lldb::ModuleSP &module_sp,
1545 const ModuleResolver &module_resolver,
1546 bool *did_create_ptr) {
1547 // Get module information from a target.
1548 ModuleSpec resolved_module_spec;
1549 bool got_module_spec = false;
1550 if (process) {
1551 // Try to get module information from the process
1552 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1553 module_spec.GetArchitecture(),
1554 resolved_module_spec)) {
1555 if (module_spec.GetUUID().IsValid() == false ||
1556 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1557 got_module_spec = true;
1558 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001559 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001560 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001561
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001562 if (module_spec.GetArchitecture().IsValid() == false) {
1563 Error error;
1564 // No valid architecture was specified, ask the platform for
1565 // the architectures that we should be using (in the correct order)
1566 // and see if we can find a match that way
1567 ModuleSpec arch_module_spec(module_spec);
1568 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1569 idx, arch_module_spec.GetArchitecture());
1570 ++idx) {
1571 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1572 nullptr, nullptr);
1573 // Did we find an executable using one of the
1574 if (error.Success() && module_sp)
1575 break;
1576 }
1577 if (module_sp)
1578 got_module_spec = true;
1579 }
1580
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001582 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1584 resolved_module_spec)) {
1585 if (module_spec.GetUUID().IsValid() == false ||
1586 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1587 return module_resolver(module_spec);
1588 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001589 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001591
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 // If we are looking for a specific UUID, make sure resolved_module_spec has
1593 // the same one before we search.
1594 if (module_spec.GetUUID().IsValid()) {
1595 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1596 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001597
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 // Trying to find a module by UUID on local file system.
1599 const auto error = module_resolver(resolved_module_spec);
1600 if (error.Fail()) {
1601 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Mehdi Aminic1edf562016-11-11 04:29:25 +00001602 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001604
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001606}
1607
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1609 lldb::ModuleSP &module_sp,
1610 bool *did_create_ptr) {
1611 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1612 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001613 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614
1615 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1616
1617 // Check local cache for a module.
1618 auto error = m_module_cache->GetAndPut(
1619 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1620 [this](const ModuleSpec &module_spec,
1621 const FileSpec &tmp_download_file_spec) {
1622 return DownloadModuleSlice(
1623 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1624 module_spec.GetObjectSize(), tmp_download_file_spec);
1625
1626 },
1627 [this](const ModuleSP &module_sp,
1628 const FileSpec &tmp_download_file_spec) {
1629 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1630 },
1631 module_sp, did_create_ptr);
1632 if (error.Success())
1633 return true;
1634
1635 if (log)
1636 log->Printf("Platform::%s - module %s not found in local cache: %s",
1637 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1638 error.AsCString());
1639 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001640}
1641
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642Error Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1643 const uint64_t src_offset,
1644 const uint64_t src_size,
1645 const FileSpec &dst_file_spec) {
1646 Error error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001647
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 std::ofstream dst(dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1649 if (!dst.is_open()) {
1650 error.SetErrorStringWithFormat("unable to open destination file: %s",
1651 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001652 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001654
Kate Stoneb9c1b512016-09-06 20:57:50 +00001655 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1656 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001657
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 if (error.Fail()) {
1659 error.SetErrorStringWithFormat("unable to open source file: %s",
1660 error.AsCString());
1661 return error;
1662 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001663
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664 std::vector<char> buffer(1024);
1665 auto offset = src_offset;
1666 uint64_t total_bytes_read = 0;
1667 while (total_bytes_read < src_size) {
1668 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1669 src_size - total_bytes_read);
1670 const uint64_t n_read =
1671 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001672 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673 break;
1674 if (n_read == 0) {
1675 error.SetErrorString("read 0 bytes");
1676 break;
1677 }
1678 offset += n_read;
1679 total_bytes_read += n_read;
1680 dst.write(&buffer[0], n_read);
1681 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001682
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 Error close_error;
1684 CloseFile(src_fd, close_error); // Ignoring close error.
1685
1686 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001687}
1688
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689Error Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1690 const FileSpec &dst_file_spec) {
1691 return Error(
1692 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001693}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001694
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695FileSpec Platform::GetModuleCacheRoot() {
1696 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1697 dir_spec.AppendPathComponent(GetName().AsCString());
1698 return dir_spec;
1699}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001700
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001702
Kate Stoneb9c1b512016-09-06 20:57:50 +00001703const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1704 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1705 return s_default_unix_signals_sp;
1706}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001707
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708const UnixSignalsSP &Platform::GetUnixSignals() {
1709 if (IsHost())
1710 return Host::GetUnixSignals();
1711 return GetRemoteUnixSignals();
1712}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001713
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714uint32_t Platform::LoadImage(lldb_private::Process *process,
1715 const lldb_private::FileSpec &local_file,
1716 const lldb_private::FileSpec &remote_file,
1717 lldb_private::Error &error) {
1718 if (local_file && remote_file) {
1719 // Both local and remote file was specified. Install the local file to the
1720 // given location.
1721 if (IsRemote() || local_file != remote_file) {
1722 error = Install(local_file, remote_file);
1723 if (error.Fail())
1724 return LLDB_INVALID_IMAGE_TOKEN;
1725 }
1726 return DoLoadImage(process, remote_file, error);
1727 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001728
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729 if (local_file) {
1730 // Only local file was specified. Install it to the current working
1731 // directory.
1732 FileSpec target_file = GetWorkingDirectory();
1733 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1734 if (IsRemote() || local_file != target_file) {
1735 error = Install(local_file, target_file);
1736 if (error.Fail())
1737 return LLDB_INVALID_IMAGE_TOKEN;
1738 }
1739 return DoLoadImage(process, target_file, error);
1740 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001741
Kate Stoneb9c1b512016-09-06 20:57:50 +00001742 if (remote_file) {
1743 // Only remote file was specified so we don't have to do any copying
1744 return DoLoadImage(process, remote_file, error);
1745 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001746
Kate Stoneb9c1b512016-09-06 20:57:50 +00001747 error.SetErrorString("Neither local nor remote file was specified");
1748 return LLDB_INVALID_IMAGE_TOKEN;
1749}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001750
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1752 const lldb_private::FileSpec &remote_file,
1753 lldb_private::Error &error) {
1754 error.SetErrorString("LoadImage is not supported on the current platform");
1755 return LLDB_INVALID_IMAGE_TOKEN;
1756}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758Error Platform::UnloadImage(lldb_private::Process *process,
1759 uint32_t image_token) {
1760 return Error("UnloadImage is not supported on the current platform");
1761}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001762
Zachary Turner31659452016-11-17 21:15:14 +00001763lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1764 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001765 lldb_private::Debugger &debugger,
1766 lldb_private::Target *target,
1767 lldb_private::Error &error) {
1768 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001769
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 if (!target) {
1771 TargetSP new_target_sp;
Zachary Turnera47464b2016-11-18 20:44:46 +00001772 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
1773 nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001774 target = new_target_sp.get();
1775 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001776
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777 if (!target || error.Fail())
1778 return nullptr;
1779
1780 debugger.GetTargetList().SetSelectedTarget(target);
1781
1782 lldb::ProcessSP process_sp =
1783 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1784 if (!process_sp)
1785 return nullptr;
1786
1787 error =
1788 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1789 if (error.Fail())
1790 return nullptr;
1791
1792 return process_sp;
1793}
1794
1795size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
1796 lldb_private::Error &error) {
1797 error.Clear();
1798 return 0;
1799}
1800
1801size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1802 BreakpointSite *bp_site) {
1803 ArchSpec arch = target.GetArchitecture();
1804 const uint8_t *trap_opcode = nullptr;
1805 size_t trap_opcode_size = 0;
1806
1807 switch (arch.GetMachine()) {
1808 case llvm::Triple::aarch64: {
1809 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1810 trap_opcode = g_aarch64_opcode;
1811 trap_opcode_size = sizeof(g_aarch64_opcode);
1812 } break;
1813
1814 // TODO: support big-endian arm and thumb trap codes.
1815 case llvm::Triple::arm: {
1816 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
1817 // but the linux kernel does otherwise.
1818 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1819 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1820
1821 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
1822 AddressClass addr_class = eAddressClassUnknown;
1823
1824 if (bp_loc_sp) {
1825 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
1826 if (addr_class == eAddressClassUnknown &&
1827 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
1828 addr_class = eAddressClassCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001829 }
1830
Kate Stoneb9c1b512016-09-06 20:57:50 +00001831 if (addr_class == eAddressClassCodeAlternateISA) {
1832 trap_opcode = g_thumb_breakpoint_opcode;
1833 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1834 } else {
1835 trap_opcode = g_arm_breakpoint_opcode;
1836 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1837 }
1838 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001839
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 case llvm::Triple::mips:
1841 case llvm::Triple::mips64: {
1842 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1843 trap_opcode = g_hex_opcode;
1844 trap_opcode_size = sizeof(g_hex_opcode);
1845 } break;
1846
1847 case llvm::Triple::mipsel:
1848 case llvm::Triple::mips64el: {
1849 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1850 trap_opcode = g_hex_opcode;
1851 trap_opcode_size = sizeof(g_hex_opcode);
1852 } break;
1853
1854 case llvm::Triple::systemz: {
1855 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1856 trap_opcode = g_hex_opcode;
1857 trap_opcode_size = sizeof(g_hex_opcode);
1858 } break;
1859
1860 case llvm::Triple::hexagon: {
1861 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1862 trap_opcode = g_hex_opcode;
1863 trap_opcode_size = sizeof(g_hex_opcode);
1864 } break;
1865
1866 case llvm::Triple::ppc:
1867 case llvm::Triple::ppc64: {
1868 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1869 trap_opcode = g_ppc_opcode;
1870 trap_opcode_size = sizeof(g_ppc_opcode);
1871 } break;
1872
1873 case llvm::Triple::x86:
1874 case llvm::Triple::x86_64: {
1875 static const uint8_t g_i386_opcode[] = {0xCC};
1876 trap_opcode = g_i386_opcode;
1877 trap_opcode_size = sizeof(g_i386_opcode);
1878 } break;
1879
1880 default:
David Blaikiea322f362017-01-06 00:38:06 +00001881 llvm_unreachable(
1882 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001883 }
1884
1885 assert(bp_site);
1886 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1887 return trap_opcode_size;
1888
1889 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001890}