blob: e3136cc3fe449bcdef369b5fc5ab3c421852b34c [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000021#include "Utility/ModuleCache.h"
Greg Clayton4116e932012-05-15 02:33:01 +000022#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000023#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000024#include "lldb/Core/DataBufferHeap.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000025#include "lldb/Core/Debugger.h"
Greg Claytone996fd32011-03-08 22:40:15 +000026#include "lldb/Core/Error.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000027#include "lldb/Core/Log.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000028#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000029#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000030#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000031#include "lldb/Core/StreamFile.h"
Enrico Granatad7a83a92015-02-10 03:06:24 +000032#include "lldb/Core/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000033#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000034#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000035#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000036#include "lldb/Host/HostInfo.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000037#include "lldb/Interpreter/OptionValueProperties.h"
38#include "lldb/Interpreter/Property.h"
39#include "lldb/Symbol/ObjectFile.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000040#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000041#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000042#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000043#include "lldb/Target/UnixSignals.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000044#include "lldb/Utility/Utils.h"
Greg Claytone996fd32011-03-08 22:40:15 +000045
Robert Flack96ad3de2015-05-09 15:53:31 +000046// Define these constants from POSIX mman.h rather than include the file
47// so that they will be correct even when compiled on Linux.
48#define MAP_PRIVATE 2
49#define MAP_ANON 0x1000
50
Greg Claytone996fd32011-03-08 22:40:15 +000051using namespace lldb;
52using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000053
54static uint32_t g_initialize_count = 0;
55
Greg Claytone996fd32011-03-08 22:40:15 +000056// Use a singleton function for g_local_platform_sp to avoid init
57// constructors since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000058static PlatformSP &GetHostPlatformSP() {
59 static PlatformSP g_platform_sp;
60 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000064
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000065namespace {
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067PropertyDefinition g_properties[] = {
68 {"use-module-cache", OptionValue::eTypeBoolean, true, true, nullptr,
69 nullptr, "Use module cache."},
70 {"module-cache-directory", OptionValue::eTypeFileSpec, true, 0, nullptr,
71 nullptr, "Root directory for cached modules."},
72 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074enum { ePropertyUseModuleCache, ePropertyModuleCacheDirectory };
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078ConstString PlatformProperties::GetSettingName() {
79 static ConstString g_setting_name("platform");
80 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000081}
82
Kate Stoneb9c1b512016-09-06 20:57:50 +000083PlatformProperties::PlatformProperties() {
84 m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
85 m_collection_sp->Initialize(g_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 auto module_cache_dir = GetModuleCacheDirectory();
88 if (module_cache_dir)
89 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 llvm::SmallString<64> user_home_dir;
92 if (!llvm::sys::path::home_directory(user_home_dir))
93 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 module_cache_dir = FileSpec(user_home_dir.c_str(), false);
96 module_cache_dir.AppendPathComponent(".lldb");
97 module_cache_dir.AppendPathComponent("module_cache");
98 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101bool PlatformProperties::GetUseModuleCache() const {
102 const auto idx = ePropertyUseModuleCache;
103 return m_collection_sp->GetPropertyAtIndexAsBoolean(
104 nullptr, idx, g_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
108 return m_collection_sp->SetPropertyAtIndexAsBoolean(
109 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112FileSpec PlatformProperties::GetModuleCacheDirectory() const {
113 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
114 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
118 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
119 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000120}
121
Greg Claytone996fd32011-03-08 22:40:15 +0000122//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000124///
125/// There should only be one of these for each host that LLDB runs
126/// upon that should be statically compiled in and registered using
127/// preprocessor macros or other similar build mechanisms.
128///
129/// This platform will be used as the default platform when launching
130/// or attaching to processes unless another platform is specified.
131//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
133
134static std::vector<PlatformSP> &GetPlatformList() {
135 static std::vector<PlatformSP> g_platform_list;
136 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000137}
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139static std::recursive_mutex &GetPlatformListMutex() {
140 static std::recursive_mutex g_mutex;
141 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000142}
143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146void Platform::Terminate() {
147 if (g_initialize_count > 0) {
148 if (--g_initialize_count == 0) {
149 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
150 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000153}
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
156 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
157 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
161 // The native platform should use its static void Platform::Initialize()
162 // function to register itself as the native platform.
163 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 if (platform_sp) {
166 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
167 GetPlatformList().push_back(platform_sp);
168 }
Greg Claytone996fd32011-03-08 22:40:15 +0000169}
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171Error Platform::GetFileWithUUID(const FileSpec &platform_file,
172 const UUID *uuid_ptr, FileSpec &local_file) {
173 // Default to the local case
174 local_file = platform_file;
Mehdi Aminic1edf562016-11-11 04:29:25 +0000175 return Error();
Greg Claytone996fd32011-03-08 22:40:15 +0000176}
177
Greg Clayton91c0e742013-01-11 23:44:27 +0000178FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179Platform::LocateExecutableScriptingResources(Target *target, Module &module,
180 Stream *feedback_stream) {
181 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184// PlatformSP
185// Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000186//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000187// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000188// if (plugin_name)
189// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190// create_callback =
191// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000192// if (create_callback)
193// {
194// ArchSpec arch;
195// if (process)
196// {
197// arch = process->GetTarget().GetArchitecture();
198// }
199// PlatformSP platform_sp(create_callback(process, &arch));
200// if (platform_sp)
201// return platform_sp;
202// }
203// }
204// else
205// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206// for (uint32_t idx = 0; (create_callback =
207// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
208// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000209// {
210// PlatformSP platform_sp(create_callback(process, nullptr));
211// if (platform_sp)
212// return platform_sp;
213// }
214// }
215// return PlatformSP();
216//}
Jason Molenda1c627542013-04-05 01:03:25 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218Error Platform::GetSharedModule(const ModuleSpec &module_spec, Process *process,
219 ModuleSP &module_sp,
220 const FileSpecList *module_search_paths_ptr,
221 ModuleSP *old_module_sp_ptr,
222 bool *did_create_ptr) {
223 if (IsHost())
224 return ModuleList::GetSharedModule(
225 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
226 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 return GetRemoteSharedModule(module_spec, process, module_sp,
229 [&](const ModuleSpec &spec) {
230 Error error = ModuleList::GetSharedModule(
231 spec, module_sp, module_search_paths_ptr,
232 old_module_sp_ptr, did_create_ptr, false);
233 if (error.Success() && module_sp)
234 module_sp->SetPlatformFileSpec(
235 spec.GetFileSpec());
236 return error;
237 },
238 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
242 const ArchSpec &arch, ModuleSpec &module_spec) {
243 ModuleSpecList module_specs;
244 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
245 module_specs) == 0)
246 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 ModuleSpec matched_module_spec;
249 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
250 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000251}
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253PlatformSP Platform::Find(const ConstString &name) {
254 if (name) {
255 static ConstString g_host_platform_name("host");
256 if (name == g_host_platform_name)
257 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
260 for (const auto &platform_sp : GetPlatformList()) {
261 if (platform_sp->GetName() == name)
262 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000263 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 }
265 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000266}
267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268PlatformSP Platform::Create(const ConstString &name, Error &error) {
269 PlatformCreateInstance create_callback = nullptr;
270 lldb::PlatformSP platform_sp;
271 if (name) {
272 static ConstString g_host_platform_name("host");
273 if (name == g_host_platform_name)
274 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 create_callback =
277 PluginManager::GetPlatformCreateCallbackForPluginName(name);
278 if (create_callback)
279 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000280 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 error.SetErrorStringWithFormat(
282 "unable to find a plug-in for the platform named \"%s\"",
283 name.GetCString());
284 } else
285 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 if (platform_sp) {
288 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
289 GetPlatformList().push_back(platform_sp);
290 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000293}
294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
296 Error &error) {
297 lldb::PlatformSP platform_sp;
298 if (arch.IsValid()) {
299 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000300 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 // First try exact arch matches across all platforms already created
302 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
303 for (const auto &platform_sp : GetPlatformList()) {
304 if (platform_sp->IsCompatibleArchitecture(arch, true,
305 platform_arch_ptr))
306 return platform_sp;
307 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 // Next try compatible arch matches across all platforms already created
310 for (const auto &platform_sp : GetPlatformList()) {
311 if (platform_sp->IsCompatibleArchitecture(arch, false,
312 platform_arch_ptr))
313 return platform_sp;
314 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000315 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316
317 PlatformCreateInstance create_callback;
318 // First try exact arch matches across all platform plug-ins
319 uint32_t idx;
320 for (idx = 0; (create_callback =
321 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
322 ++idx) {
323 if (create_callback) {
324 platform_sp = create_callback(false, &arch);
325 if (platform_sp &&
326 platform_sp->IsCompatibleArchitecture(arch, true,
327 platform_arch_ptr)) {
328 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
329 GetPlatformList().push_back(platform_sp);
330 return platform_sp;
331 }
332 }
333 }
334 // Next try compatible arch matches across all platform plug-ins
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, false,
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 } else
350 error.SetErrorString("invalid platform name");
351 if (platform_arch_ptr)
352 platform_arch_ptr->Clear();
353 platform_sp.reset();
354 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000355}
356
Greg Claytone996fd32011-03-08 22:40:15 +0000357//------------------------------------------------------------------
358/// Default Constructor
359//------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000360Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 : m_is_host(is_host), m_os_version_set_while_connected(false),
362 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
363 m_working_dir(), m_remote_url(), m_name(), m_major_os_version(UINT32_MAX),
364 m_minor_os_version(UINT32_MAX), m_update_os_version(UINT32_MAX),
365 m_system_arch(), m_mutex(), m_uid_map(), m_gid_map(),
366 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
367 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
368 m_ignores_remote_hostname(false), m_trap_handlers(),
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000369 m_calculated_trap_handlers(false),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 m_module_cache(llvm::make_unique<ModuleCache>()) {
371 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
372 if (log)
373 log->Printf("%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000374}
375
376//------------------------------------------------------------------
377/// Destructor.
378///
379/// The destructor is virtual since this class is designed to be
380/// inherited from by the plug-in instance.
381//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382Platform::~Platform() {
383 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
384 if (log)
385 log->Printf("%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000386}
387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388void Platform::GetStatus(Stream &strm) {
389 uint32_t major = UINT32_MAX;
390 uint32_t minor = UINT32_MAX;
391 uint32_t update = UINT32_MAX;
392 std::string s;
393 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 ArchSpec arch(GetSystemArchitecture());
396 if (arch.IsValid()) {
397 if (!arch.GetTriple().str().empty()) {
398 strm.Printf(" Triple: ");
399 arch.DumpTriple(strm);
400 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 if (GetOSVersion(major, minor, update)) {
405 strm.Printf("OS Version: %u", major);
406 if (minor != UINT32_MAX)
407 strm.Printf(".%u", minor);
408 if (update != UINT32_MAX)
409 strm.Printf(".%u", update);
Greg Clayton1cb64962011-03-24 04:28:38 +0000410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 if (GetOSBuildString(s))
412 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 strm.EOL();
415 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000416
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417 if (GetOSKernelDescription(s))
418 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 if (IsHost()) {
421 strm.Printf(" Hostname: %s\n", GetHostname());
422 } else {
423 const bool is_connected = IsConnected();
424 if (is_connected)
425 strm.Printf(" Hostname: %s\n", GetHostname());
426 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
427 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000428
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 if (GetWorkingDirectory()) {
430 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
431 }
432 if (!IsConnected())
433 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 std::string specific_info(GetPlatformSpecificConnectionInformation());
436
437 if (!specific_info.empty())
438 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000439}
440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441bool Platform::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update,
442 Process *process) {
443 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 bool success = m_major_os_version != UINT32_MAX;
446 if (IsHost()) {
447 if (!success) {
448 // We have a local host platform
449 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version,
450 m_update_os_version);
451 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000452 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 } else {
454 // We have a remote platform. We can only fetch the remote
455 // OS version if we are connected, and we don't want to do it
456 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 bool fetch = false;
461 if (success) {
462 // We have valid OS version info, check to make sure it wasn't
463 // manually set prior to connecting. If it was manually set prior
464 // to connecting, then lets fetch the actual OS version info
465 // if we are now connected.
466 if (is_connected && !m_os_version_set_while_connected)
467 fetch = true;
468 } else {
469 // We don't have valid OS version info, fetch it if we are connected
470 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000471 }
472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 if (fetch) {
474 success = GetRemoteOSVersion();
475 m_os_version_set_while_connected = success;
Greg Claytonded470d2011-03-19 01:12:21 +0000476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 }
478
479 if (success) {
480 major = m_major_os_version;
481 minor = m_minor_os_version;
482 update = m_update_os_version;
483 } else if (process) {
484 // Check with the process in case it can answer the question if
485 // a process was provided
486 return process->GetHostOSVersion(major, minor, update);
487 }
488 return success;
Greg Claytonded470d2011-03-19 01:12:21 +0000489}
Greg Clayton1cb64962011-03-24 04:28:38 +0000490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491bool Platform::GetOSBuildString(std::string &s) {
492 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000495#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000497#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000499#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 else
501 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000502}
503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504bool Platform::GetOSKernelDescription(std::string &s) {
505 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000506#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000508#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000510#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 else
512 return GetRemoteOSKernelDescription(s);
513}
514
515void Platform::AddClangModuleCompilationOptions(
516 Target *target, std::vector<std::string> &options) {
517 std::vector<std::string> default_compilation_options = {
518 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
519
520 options.insert(options.end(), default_compilation_options.begin(),
521 default_compilation_options.end());
522}
523
524FileSpec Platform::GetWorkingDirectory() {
525 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000526 llvm::SmallString<64> cwd;
527 if (llvm::sys::fs::current_path(cwd))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 return FileSpec{};
Pavel Labath1d5855b2017-01-23 15:56:45 +0000529 else
530 return FileSpec(cwd, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 } else {
532 if (!m_working_dir)
533 m_working_dir = GetRemoteWorkingDirectory();
534 return m_working_dir;
535 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000536}
537
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538struct RecurseCopyBaton {
539 const FileSpec &dst;
540 Platform *platform_ptr;
541 Error error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000542};
543
Greg Claytonfbb76342013-11-20 21:07:01 +0000544static FileSpec::EnumerateDirectoryResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545RecurseCopy_Callback(void *baton, FileSpec::FileType file_type,
546 const FileSpec &src) {
547 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
548 switch (file_type) {
549 case FileSpec::eFileTypePipe:
550 case FileSpec::eFileTypeSocket:
551 // we have no way to copy pipes and sockets - ignore them and continue
552 return FileSpec::eEnumerateDirectoryResultNext;
553 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 case FileSpec::eFileTypeDirectory: {
556 // make the new directory and get in there
557 FileSpec dst_dir = rc_baton->dst;
558 if (!dst_dir.GetFilename())
559 dst_dir.GetFilename() = src.GetLastPathComponent();
560 Error error = rc_baton->platform_ptr->MakeDirectory(
561 dst_dir, lldb::eFilePermissionsDirectoryDefault);
562 if (error.Fail()) {
563 rc_baton->error.SetErrorStringWithFormat(
564 "unable to setup directory %s on remote end", dst_dir.GetCString());
565 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000566 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567
568 // now recurse
569 std::string src_dir_path(src.GetPath());
570
571 // Make a filespec that only fills in the directory of a FileSpec so
572 // when we enumerate we can quickly fill in the filename for dst copies
573 FileSpec recurse_dst;
574 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
575 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr, Error()};
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000576 FileSpec::EnumerateDirectory(src_dir_path, true, true, true,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 RecurseCopy_Callback, &rc_baton2);
578 if (rc_baton2.error.Fail()) {
579 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
580 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
581 }
582 return FileSpec::eEnumerateDirectoryResultNext;
583 } break;
584
585 case FileSpec::eFileTypeSymbolicLink: {
586 // copy the file and keep going
587 FileSpec dst_file = rc_baton->dst;
588 if (!dst_file.GetFilename())
589 dst_file.GetFilename() = src.GetFilename();
590
591 FileSpec src_resolved;
592
593 rc_baton->error = FileSystem::Readlink(src, src_resolved);
594
595 if (rc_baton->error.Fail())
596 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
597
598 rc_baton->error =
599 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
600
601 if (rc_baton->error.Fail())
602 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
603
604 return FileSpec::eEnumerateDirectoryResultNext;
605 } break;
606
607 case FileSpec::eFileTypeRegular: {
608 // copy the file and keep going
609 FileSpec dst_file = rc_baton->dst;
610 if (!dst_file.GetFilename())
611 dst_file.GetFilename() = src.GetFilename();
612 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
613 if (err.Fail()) {
614 rc_baton->error.SetErrorString(err.AsCString());
615 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
616 }
617 return FileSpec::eEnumerateDirectoryResultNext;
618 } break;
619
620 case FileSpec::eFileTypeInvalid:
621 case FileSpec::eFileTypeOther:
622 case FileSpec::eFileTypeUnknown:
623 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 }
628 llvm_unreachable("Unhandled FileSpec::FileType!");
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 {
696 switch (src.GetFileType()) {
697 case FileSpec::eFileTypeDirectory: {
698 if (GetFileExists(fixed_dst))
699 Unlink(fixed_dst);
700 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
717 case FileSpec::eFileTypeRegular:
718 if (GetFileExists(fixed_dst))
719 Unlink(fixed_dst);
720 error = PutFile(src, fixed_dst);
721 break;
722
723 case FileSpec::eFileTypeSymbolicLink: {
724 if (GetFileExists(fixed_dst))
725 Unlink(fixed_dst);
726 FileSpec src_resolved;
727 error = FileSystem::Readlink(src, src_resolved);
728 if (error.Success())
729 error = CreateSymlink(dst, src_resolved);
730 } break;
731 case FileSpec::eFileTypePipe:
732 error.SetErrorString("platform install doesn't handle pipes");
733 break;
734 case FileSpec::eFileTypeSocket:
735 error.SetErrorString("platform install doesn't handle sockets");
736 break;
737 case FileSpec::eFileTypeInvalid:
738 case FileSpec::eFileTypeUnknown:
739 case FileSpec::eFileTypeOther:
740 error.SetErrorString(
741 "platform install doesn't handle non file or directory items");
742 break;
743 }
744 }
745 return error;
746}
747
748bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
749 if (IsHost()) {
750 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000751 LLDB_LOG(log, "{0}", file_spec);
752 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
753 LLDB_LOG(log, "error: {0}", ec.message());
754 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000756 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 } else {
758 m_working_dir.Clear();
759 return SetRemoteWorkingDirectory(file_spec);
760 }
761}
762
763Error Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions) {
764 if (IsHost())
765 return FileSystem::MakeDirectory(file_spec, permissions);
766 else {
Greg Claytonfbb76342013-11-20 21:07:01 +0000767 Error error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
769 GetPluginName().GetCString(),
770 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000771 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000773}
774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775Error Platform::GetFilePermissions(const FileSpec &file_spec,
776 uint32_t &file_permissions) {
777 if (IsHost())
778 return FileSystem::GetFilePermissions(file_spec, file_permissions);
779 else {
780 Error error;
781 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
782 GetPluginName().GetCString(),
783 LLVM_PRETTY_FUNCTION);
784 return error;
785 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000786}
787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788Error Platform::SetFilePermissions(const FileSpec &file_spec,
789 uint32_t file_permissions) {
790 if (IsHost())
791 return FileSystem::SetFilePermissions(file_spec, file_permissions);
792 else {
793 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;
1240 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1241 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001242
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1244 Error error;
1245 uint32_t permissions = source_file.GetPermissions(error);
1246 if (permissions == 0)
1247 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001248
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249 if (!source_file.IsValid())
1250 return Error("PutFile: unable to open source file");
1251 lldb::user_id_t dest_file = OpenFile(
1252 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1253 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1254 permissions, error);
1255 if (log)
1256 log->Printf("dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001257
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258 if (error.Fail())
1259 return error;
1260 if (dest_file == UINT64_MAX)
1261 return Error("unable to open target file");
1262 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1263 uint64_t offset = 0;
1264 for (;;) {
1265 size_t bytes_read = buffer_sp->GetByteSize();
1266 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1267 if (error.Fail() || bytes_read == 0)
1268 break;
1269
1270 const uint64_t bytes_written =
1271 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001272 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275 offset += bytes_written;
1276 if (bytes_written != bytes_read) {
1277 // We didn't write the correct number of bytes, so adjust
1278 // the file position in the source file we are reading from...
1279 source_file.SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001280 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281 }
1282 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001283
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001285 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286
1287 // TODO: ChownFile?
1288
1289 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001290}
1291
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292Error Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1293 Error error("unimplemented");
1294 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001295}
1296
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297Error Platform::CreateSymlink(
1298 const FileSpec &src, // The name of the link is in src
1299 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001300{
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 Error error("unimplemented");
1302 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001303}
1304
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1306 return false;
1307}
1308
1309Error Platform::Unlink(const FileSpec &path) {
1310 Error error("unimplemented");
1311 return error;
1312}
1313
1314uint64_t Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch,
1315 unsigned flags) {
1316 uint64_t flags_platform = 0;
1317 if (flags & eMmapFlagsPrivate)
1318 flags_platform |= MAP_PRIVATE;
1319 if (flags & eMmapFlagsAnon)
1320 flags_platform |= MAP_ANON;
1321 return flags_platform;
1322}
1323
1324lldb_private::Error Platform::RunShellCommand(
1325 const char *command, // Shouldn't be nullptr
1326 const FileSpec &
1327 working_dir, // Pass empty FileSpec to use the current working directory
1328 int *status_ptr, // Pass nullptr if you don't want the process exit status
1329 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1330 // process to exit
1331 std::string
1332 *command_output, // Pass nullptr if you don't want the command output
1333 uint32_t
1334 timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001335{
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 if (IsHost())
1337 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
1338 command_output, timeout_sec);
1339 else
1340 return Error("unimplemented");
1341}
1342
1343bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1344 uint64_t &high) {
1345 if (IsHost())
1346 return FileSystem::CalculateMD5(file_spec, low, high);
1347 else
Daniel Maleae0f8f572013-08-26 23:57:52 +00001348 return false;
1349}
1350
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351void Platform::SetLocalCacheDirectory(const char *local) {
1352 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001353}
1354
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355const char *Platform::GetLocalCacheDirectory() {
1356 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001357}
Greg Claytonfbb76342013-11-20 21:07:01 +00001358
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359static OptionDefinition g_rsync_option_table[] = {
1360 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
1361 nullptr, 0, eArgTypeNone, "Enable rsync."},
1362 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
1363 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1364 "Platform-specific options required for rsync to work."},
1365 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
1366 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommandName,
1367 "Platform-specific rsync prefix put before the remote path."},
1368 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
1369 OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone,
1370 "Do not automatically fill in the remote hostname when composing the "
1371 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001372};
1373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374static OptionDefinition g_ssh_option_table[] = {
1375 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
1376 nullptr, 0, eArgTypeNone, "Enable SSH."},
1377 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
1378 nullptr, nullptr, 0, eArgTypeCommandName,
1379 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001380};
1381
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382static OptionDefinition g_caching_option_table[] = {
1383 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
1384 OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePath,
1385 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001386};
1387
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001388llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001389 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001390}
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392void OptionGroupPlatformRSync::OptionParsingStarting(
1393 ExecutionContext *execution_context) {
1394 m_rsync = false;
1395 m_rsync_opts.clear();
1396 m_rsync_prefix.clear();
1397 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001398}
1399
1400lldb_private::Error
Todd Fialae1cfbc72016-08-11 23:51:28 +00001401OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001402 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 ExecutionContext *execution_context) {
1404 Error error;
1405 char short_option = (char)GetDefinitions()[option_idx].short_option;
1406 switch (short_option) {
1407 case 'r':
1408 m_rsync = true;
1409 break;
1410
1411 case 'R':
1412 m_rsync_opts.assign(option_arg);
1413 break;
1414
1415 case 'P':
1416 m_rsync_prefix.assign(option_arg);
1417 break;
1418
1419 case 'i':
1420 m_ignores_remote_hostname = true;
1421 break;
1422
1423 default:
1424 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1425 break;
1426 }
1427
1428 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001429}
1430
Greg Clayton4116e932012-05-15 02:33:01 +00001431lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1433 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001434}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001435
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001436llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001437 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001438}
1439
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440void OptionGroupPlatformSSH::OptionParsingStarting(
1441 ExecutionContext *execution_context) {
1442 m_ssh = false;
1443 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001444}
1445
1446lldb_private::Error
Todd Fialae1cfbc72016-08-11 23:51:28 +00001447OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001448 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449 ExecutionContext *execution_context) {
1450 Error error;
1451 char short_option = (char)GetDefinitions()[option_idx].short_option;
1452 switch (short_option) {
1453 case 's':
1454 m_ssh = true;
1455 break;
1456
1457 case 'S':
1458 m_ssh_opts.assign(option_arg);
1459 break;
1460
1461 default:
1462 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1463 break;
1464 }
1465
1466 return error;
1467}
1468
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001469llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001470 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471}
1472
1473void OptionGroupPlatformCaching::OptionParsingStarting(
1474 ExecutionContext *execution_context) {
1475 m_cache_dir.clear();
1476}
1477
1478lldb_private::Error OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001479 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480 ExecutionContext *execution_context) {
1481 Error error;
1482 char short_option = (char)GetDefinitions()[option_idx].short_option;
1483 switch (short_option) {
1484 case 'c':
1485 m_cache_dir.assign(option_arg);
1486 break;
1487
1488 default:
1489 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1490 break;
1491 }
1492
1493 return error;
1494}
1495
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496size_t Platform::GetEnvironment(StringList &environment) {
1497 environment.Clear();
1498 return false;
1499}
1500
1501const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1502 if (!m_calculated_trap_handlers) {
1503 std::lock_guard<std::mutex> guard(m_mutex);
1504 if (!m_calculated_trap_handlers) {
1505 CalculateTrapHandlerSymbolNames();
1506 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001507 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508 }
1509 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001510}
1511
Kate Stoneb9c1b512016-09-06 20:57:50 +00001512Error Platform::GetCachedExecutable(ModuleSpec &module_spec,
1513 lldb::ModuleSP &module_sp,
1514 const FileSpecList *module_search_paths_ptr,
1515 Platform &remote_platform) {
1516 const auto platform_spec = module_spec.GetFileSpec();
1517 const auto error = LoadCachedExecutable(
1518 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1519 if (error.Success()) {
1520 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1521 module_spec.GetPlatformFileSpec() = platform_spec;
1522 }
1523
1524 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001525}
1526
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527Error Platform::LoadCachedExecutable(
1528 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1529 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1530 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1531 [&](const ModuleSpec &spec) {
1532 return remote_platform.ResolveExecutable(
1533 spec, module_sp, module_search_paths_ptr);
1534 },
1535 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001536}
1537
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538Error Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1539 Process *process,
1540 lldb::ModuleSP &module_sp,
1541 const ModuleResolver &module_resolver,
1542 bool *did_create_ptr) {
1543 // Get module information from a target.
1544 ModuleSpec resolved_module_spec;
1545 bool got_module_spec = false;
1546 if (process) {
1547 // Try to get module information from the process
1548 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1549 module_spec.GetArchitecture(),
1550 resolved_module_spec)) {
1551 if (module_spec.GetUUID().IsValid() == false ||
1552 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1553 got_module_spec = true;
1554 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001555 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001556 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001557
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001558 if (module_spec.GetArchitecture().IsValid() == false) {
1559 Error error;
1560 // No valid architecture was specified, ask the platform for
1561 // the architectures that we should be using (in the correct order)
1562 // and see if we can find a match that way
1563 ModuleSpec arch_module_spec(module_spec);
1564 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1565 idx, arch_module_spec.GetArchitecture());
1566 ++idx) {
1567 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1568 nullptr, nullptr);
1569 // Did we find an executable using one of the
1570 if (error.Success() && module_sp)
1571 break;
1572 }
1573 if (module_sp)
1574 got_module_spec = true;
1575 }
1576
Kate Stoneb9c1b512016-09-06 20:57:50 +00001577 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001578 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1580 resolved_module_spec)) {
1581 if (module_spec.GetUUID().IsValid() == false ||
1582 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1583 return module_resolver(module_spec);
1584 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001585 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001587
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 // If we are looking for a specific UUID, make sure resolved_module_spec has
1589 // the same one before we search.
1590 if (module_spec.GetUUID().IsValid()) {
1591 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1592 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001593
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594 // Trying to find a module by UUID on local file system.
1595 const auto error = module_resolver(resolved_module_spec);
1596 if (error.Fail()) {
1597 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Mehdi Aminic1edf562016-11-11 04:29:25 +00001598 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001600
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001602}
1603
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1605 lldb::ModuleSP &module_sp,
1606 bool *did_create_ptr) {
1607 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1608 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001609 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610
1611 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1612
1613 // Check local cache for a module.
1614 auto error = m_module_cache->GetAndPut(
1615 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1616 [this](const ModuleSpec &module_spec,
1617 const FileSpec &tmp_download_file_spec) {
1618 return DownloadModuleSlice(
1619 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1620 module_spec.GetObjectSize(), tmp_download_file_spec);
1621
1622 },
1623 [this](const ModuleSP &module_sp,
1624 const FileSpec &tmp_download_file_spec) {
1625 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1626 },
1627 module_sp, did_create_ptr);
1628 if (error.Success())
1629 return true;
1630
1631 if (log)
1632 log->Printf("Platform::%s - module %s not found in local cache: %s",
1633 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1634 error.AsCString());
1635 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001636}
1637
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638Error Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1639 const uint64_t src_offset,
1640 const uint64_t src_size,
1641 const FileSpec &dst_file_spec) {
1642 Error error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001643
Kate Stoneb9c1b512016-09-06 20:57:50 +00001644 std::ofstream dst(dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1645 if (!dst.is_open()) {
1646 error.SetErrorStringWithFormat("unable to open destination file: %s",
1647 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001648 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001650
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1652 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001653
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 if (error.Fail()) {
1655 error.SetErrorStringWithFormat("unable to open source file: %s",
1656 error.AsCString());
1657 return error;
1658 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001659
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 std::vector<char> buffer(1024);
1661 auto offset = src_offset;
1662 uint64_t total_bytes_read = 0;
1663 while (total_bytes_read < src_size) {
1664 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1665 src_size - total_bytes_read);
1666 const uint64_t n_read =
1667 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001668 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669 break;
1670 if (n_read == 0) {
1671 error.SetErrorString("read 0 bytes");
1672 break;
1673 }
1674 offset += n_read;
1675 total_bytes_read += n_read;
1676 dst.write(&buffer[0], n_read);
1677 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001678
Kate Stoneb9c1b512016-09-06 20:57:50 +00001679 Error close_error;
1680 CloseFile(src_fd, close_error); // Ignoring close error.
1681
1682 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001683}
1684
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685Error Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1686 const FileSpec &dst_file_spec) {
1687 return Error(
1688 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001689}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001690
Kate Stoneb9c1b512016-09-06 20:57:50 +00001691FileSpec Platform::GetModuleCacheRoot() {
1692 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1693 dir_spec.AppendPathComponent(GetName().AsCString());
1694 return dir_spec;
1695}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001696
Kate Stoneb9c1b512016-09-06 20:57:50 +00001697const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001698
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1700 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1701 return s_default_unix_signals_sp;
1702}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001703
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704const UnixSignalsSP &Platform::GetUnixSignals() {
1705 if (IsHost())
1706 return Host::GetUnixSignals();
1707 return GetRemoteUnixSignals();
1708}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001709
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710uint32_t Platform::LoadImage(lldb_private::Process *process,
1711 const lldb_private::FileSpec &local_file,
1712 const lldb_private::FileSpec &remote_file,
1713 lldb_private::Error &error) {
1714 if (local_file && remote_file) {
1715 // Both local and remote file was specified. Install the local file to the
1716 // given location.
1717 if (IsRemote() || local_file != remote_file) {
1718 error = Install(local_file, remote_file);
1719 if (error.Fail())
1720 return LLDB_INVALID_IMAGE_TOKEN;
1721 }
1722 return DoLoadImage(process, remote_file, error);
1723 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001724
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725 if (local_file) {
1726 // Only local file was specified. Install it to the current working
1727 // directory.
1728 FileSpec target_file = GetWorkingDirectory();
1729 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1730 if (IsRemote() || local_file != target_file) {
1731 error = Install(local_file, target_file);
1732 if (error.Fail())
1733 return LLDB_INVALID_IMAGE_TOKEN;
1734 }
1735 return DoLoadImage(process, target_file, error);
1736 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001737
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 if (remote_file) {
1739 // Only remote file was specified so we don't have to do any copying
1740 return DoLoadImage(process, remote_file, error);
1741 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001742
Kate Stoneb9c1b512016-09-06 20:57:50 +00001743 error.SetErrorString("Neither local nor remote file was specified");
1744 return LLDB_INVALID_IMAGE_TOKEN;
1745}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001746
Kate Stoneb9c1b512016-09-06 20:57:50 +00001747uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1748 const lldb_private::FileSpec &remote_file,
1749 lldb_private::Error &error) {
1750 error.SetErrorString("LoadImage is not supported on the current platform");
1751 return LLDB_INVALID_IMAGE_TOKEN;
1752}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001753
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754Error Platform::UnloadImage(lldb_private::Process *process,
1755 uint32_t image_token) {
1756 return Error("UnloadImage is not supported on the current platform");
1757}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001758
Zachary Turner31659452016-11-17 21:15:14 +00001759lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1760 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 lldb_private::Debugger &debugger,
1762 lldb_private::Target *target,
1763 lldb_private::Error &error) {
1764 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001765
Kate Stoneb9c1b512016-09-06 20:57:50 +00001766 if (!target) {
1767 TargetSP new_target_sp;
Zachary Turnera47464b2016-11-18 20:44:46 +00001768 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
1769 nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 target = new_target_sp.get();
1771 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001772
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 if (!target || error.Fail())
1774 return nullptr;
1775
1776 debugger.GetTargetList().SetSelectedTarget(target);
1777
1778 lldb::ProcessSP process_sp =
1779 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1780 if (!process_sp)
1781 return nullptr;
1782
1783 error =
1784 process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url);
1785 if (error.Fail())
1786 return nullptr;
1787
1788 return process_sp;
1789}
1790
1791size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
1792 lldb_private::Error &error) {
1793 error.Clear();
1794 return 0;
1795}
1796
1797size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1798 BreakpointSite *bp_site) {
1799 ArchSpec arch = target.GetArchitecture();
1800 const uint8_t *trap_opcode = nullptr;
1801 size_t trap_opcode_size = 0;
1802
1803 switch (arch.GetMachine()) {
1804 case llvm::Triple::aarch64: {
1805 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1806 trap_opcode = g_aarch64_opcode;
1807 trap_opcode_size = sizeof(g_aarch64_opcode);
1808 } break;
1809
1810 // TODO: support big-endian arm and thumb trap codes.
1811 case llvm::Triple::arm: {
1812 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
1813 // but the linux kernel does otherwise.
1814 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1815 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1816
1817 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
1818 AddressClass addr_class = eAddressClassUnknown;
1819
1820 if (bp_loc_sp) {
1821 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
1822 if (addr_class == eAddressClassUnknown &&
1823 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
1824 addr_class = eAddressClassCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001825 }
1826
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827 if (addr_class == eAddressClassCodeAlternateISA) {
1828 trap_opcode = g_thumb_breakpoint_opcode;
1829 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1830 } else {
1831 trap_opcode = g_arm_breakpoint_opcode;
1832 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1833 }
1834 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001835
Kate Stoneb9c1b512016-09-06 20:57:50 +00001836 case llvm::Triple::mips:
1837 case llvm::Triple::mips64: {
1838 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1839 trap_opcode = g_hex_opcode;
1840 trap_opcode_size = sizeof(g_hex_opcode);
1841 } break;
1842
1843 case llvm::Triple::mipsel:
1844 case llvm::Triple::mips64el: {
1845 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1846 trap_opcode = g_hex_opcode;
1847 trap_opcode_size = sizeof(g_hex_opcode);
1848 } break;
1849
1850 case llvm::Triple::systemz: {
1851 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1852 trap_opcode = g_hex_opcode;
1853 trap_opcode_size = sizeof(g_hex_opcode);
1854 } break;
1855
1856 case llvm::Triple::hexagon: {
1857 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1858 trap_opcode = g_hex_opcode;
1859 trap_opcode_size = sizeof(g_hex_opcode);
1860 } break;
1861
1862 case llvm::Triple::ppc:
1863 case llvm::Triple::ppc64: {
1864 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1865 trap_opcode = g_ppc_opcode;
1866 trap_opcode_size = sizeof(g_ppc_opcode);
1867 } break;
1868
1869 case llvm::Triple::x86:
1870 case llvm::Triple::x86_64: {
1871 static const uint8_t g_i386_opcode[] = {0xCC};
1872 trap_opcode = g_i386_opcode;
1873 trap_opcode_size = sizeof(g_i386_opcode);
1874 } break;
1875
1876 default:
David Blaikiea322f362017-01-06 00:38:06 +00001877 llvm_unreachable(
1878 "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879 }
1880
1881 assert(bp_site);
1882 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1883 return trap_opcode_size;
1884
1885 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001886}