blob: c8afe1fe68a6902ce4682ae76337238e7e727659 [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
10#include "lldb/Target/Platform.h"
11
12// C Includes
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000013
Greg Claytone996fd32011-03-08 22:40:15 +000014// C++ Includes
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000015#include <algorithm>
16#include <fstream>
17#include <vector>
18
Greg Claytone996fd32011-03-08 22:40:15 +000019// Other libraries and framework includes
20// Project includes
Greg Clayton4116e932012-05-15 02:33:01 +000021#include "lldb/Breakpoint/BreakpointIDList.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000022#include "lldb/Core/DataBufferHeap.h"
Greg Claytone996fd32011-03-08 22:40:15 +000023#include "lldb/Core/Error.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000024#include "lldb/Core/Log.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000025#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000026#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000027#include "lldb/Core/PluginManager.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"
Greg Clayton8b82f082011-04-12 05:54:46 +000036#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000037#include "lldb/Target/Target.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000038#include "lldb/Utility/Utils.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000039#include "llvm/Support/FileSystem.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000040
41#include "Utility/ModuleCache.h"
Greg Claytone996fd32011-03-08 22:40:15 +000042
Robert Flack96ad3de2015-05-09 15:53:31 +000043// Define these constants from POSIX mman.h rather than include the file
44// so that they will be correct even when compiled on Linux.
45#define MAP_PRIVATE 2
46#define MAP_ANON 0x1000
47
Greg Claytone996fd32011-03-08 22:40:15 +000048using namespace lldb;
49using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000050
51static uint32_t g_initialize_count = 0;
52
Greg Claytone996fd32011-03-08 22:40:15 +000053// Use a singleton function for g_local_platform_sp to avoid init
54// constructors since LLDB is often part of a shared library
55static PlatformSP&
Greg Clayton615eb7e2014-09-19 20:11:50 +000056GetHostPlatformSP ()
Greg Claytone996fd32011-03-08 22:40:15 +000057{
Greg Clayton615eb7e2014-09-19 20:11:50 +000058 static PlatformSP g_platform_sp;
59 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000060}
61
Greg Claytonab65b342011-04-13 22:47:15 +000062const char *
63Platform::GetHostPlatformName ()
64{
65 return "host";
66}
67
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000068namespace {
69
70 PropertyDefinition
71 g_properties[] =
72 {
73 { "use-module-cache" , OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "Use module cache." },
74 { "module-cache-directory", OptionValue::eTypeFileSpec, true, 0 , nullptr, nullptr, "Root directory for cached modules." },
75 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
76 };
77
78 enum
79 {
80 ePropertyUseModuleCache,
81 ePropertyModuleCacheDirectory
82 };
83
84} // namespace
85
86
87ConstString
88PlatformProperties::GetSettingName ()
89{
90 static ConstString g_setting_name("platform");
91 return g_setting_name;
92}
93
94PlatformProperties::PlatformProperties ()
95{
96 m_collection_sp.reset (new OptionValueProperties (GetSettingName ()));
97 m_collection_sp->Initialize (g_properties);
98
99 auto module_cache_dir = GetModuleCacheDirectory ();
100 if (!module_cache_dir)
101 {
102 if (!HostInfo::GetLLDBPath (ePathTypeGlobalLLDBTempSystemDir, module_cache_dir))
103 module_cache_dir = FileSpec ("/tmp/lldb", false);
104 module_cache_dir.AppendPathComponent ("module_cache");
105 SetModuleCacheDirectory (module_cache_dir);
106 }
107}
108
109bool
110PlatformProperties::GetUseModuleCache () const
111{
112 const auto idx = ePropertyUseModuleCache;
113 return m_collection_sp->GetPropertyAtIndexAsBoolean (
114 nullptr, idx, g_properties[idx].default_uint_value != 0);
115}
116
117bool
118PlatformProperties::SetUseModuleCache (bool use_module_cache)
119{
120 return m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, ePropertyUseModuleCache, use_module_cache);
121}
122
123FileSpec
124PlatformProperties::GetModuleCacheDirectory () const
125{
126 return m_collection_sp->GetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory);
127}
128
129bool
130PlatformProperties::SetModuleCacheDirectory (const FileSpec& dir_spec)
131{
132 return m_collection_sp->SetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory, dir_spec);
133}
134
Greg Claytone996fd32011-03-08 22:40:15 +0000135//------------------------------------------------------------------
136/// Get the native host platform plug-in.
137///
138/// There should only be one of these for each host that LLDB runs
139/// upon that should be statically compiled in and registered using
140/// preprocessor macros or other similar build mechanisms.
141///
142/// This platform will be used as the default platform when launching
143/// or attaching to processes unless another platform is specified.
144//------------------------------------------------------------------
145PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000146Platform::GetHostPlatform ()
Greg Claytone996fd32011-03-08 22:40:15 +0000147{
Greg Clayton615eb7e2014-09-19 20:11:50 +0000148 return GetHostPlatformSP ();
149}
150
151static std::vector<PlatformSP> &
152GetPlatformList()
153{
154 static std::vector<PlatformSP> g_platform_list;
155 return g_platform_list;
156}
157
158static Mutex &
159GetPlatformListMutex ()
160{
161 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
162 return g_mutex;
Greg Claytone996fd32011-03-08 22:40:15 +0000163}
164
165void
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000166Platform::Initialize ()
167{
168 g_initialize_count++;
169}
170
171void
172Platform::Terminate ()
173{
174 if (g_initialize_count > 0)
175 {
176 if (--g_initialize_count == 0)
177 {
178 Mutex::Locker locker(GetPlatformListMutex ());
179 GetPlatformList().clear();
180 }
181 }
182}
183
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000184const PlatformPropertiesSP &
185Platform::GetGlobalPlatformProperties ()
186{
187 static const auto g_settings_sp (std::make_shared<PlatformProperties> ());
188 return g_settings_sp;
189}
190
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000191void
Greg Clayton615eb7e2014-09-19 20:11:50 +0000192Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp)
Greg Claytone996fd32011-03-08 22:40:15 +0000193{
194 // The native platform should use its static void Platform::Initialize()
195 // function to register itself as the native platform.
Greg Clayton615eb7e2014-09-19 20:11:50 +0000196 GetHostPlatformSP () = platform_sp;
197
198 if (platform_sp)
199 {
200 Mutex::Locker locker(GetPlatformListMutex ());
201 GetPlatformList().push_back(platform_sp);
202 }
Greg Claytone996fd32011-03-08 22:40:15 +0000203}
204
Greg Claytone996fd32011-03-08 22:40:15 +0000205Error
Steve Puccifc995722014-01-17 18:18:31 +0000206Platform::GetFileWithUUID (const FileSpec &platform_file,
207 const UUID *uuid_ptr,
208 FileSpec &local_file)
Greg Claytone996fd32011-03-08 22:40:15 +0000209{
210 // Default to the local case
211 local_file = platform_file;
212 return Error();
213}
214
Greg Clayton91c0e742013-01-11 23:44:27 +0000215FileSpecList
Enrico Granatafe7295d2014-08-16 00:32:58 +0000216Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +0000217{
Greg Clayton91c0e742013-01-11 23:44:27 +0000218 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000219}
220
Greg Clayton615eb7e2014-09-19 20:11:50 +0000221//PlatformSP
222//Platform::FindPlugin (Process *process, const ConstString &plugin_name)
223//{
224// PlatformCreateInstance create_callback = NULL;
225// if (plugin_name)
226// {
227// create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
228// if (create_callback)
229// {
230// ArchSpec arch;
231// if (process)
232// {
233// arch = process->GetTarget().GetArchitecture();
234// }
235// PlatformSP platform_sp(create_callback(process, &arch));
236// if (platform_sp)
237// return platform_sp;
238// }
239// }
240// else
241// {
242// for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
243// {
244// PlatformSP platform_sp(create_callback(process, nullptr));
245// if (platform_sp)
246// return platform_sp;
247// }
248// }
249// return PlatformSP();
250//}
Jason Molenda1c627542013-04-05 01:03:25 +0000251
Greg Clayton32e0a752011-03-30 18:16:51 +0000252Error
Greg Claytonb9a01b32012-02-26 05:51:37 +0000253Platform::GetSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000254 Process* process,
Greg Clayton32e0a752011-03-30 18:16:51 +0000255 ModuleSP &module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000256 const FileSpecList *module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000257 ModuleSP *old_module_sp_ptr,
258 bool *did_create_ptr)
259{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000260 if (IsHost ())
261 return ModuleList::GetSharedModule (module_spec,
262 module_sp,
263 module_search_paths_ptr,
264 old_module_sp_ptr,
265 did_create_ptr,
266 false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000267
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000268 return GetRemoteSharedModule (module_spec,
269 process,
270 module_sp,
271 [&](const ModuleSpec &spec)
272 {
273 return ModuleList::GetSharedModule (
274 spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr, false);
275 },
276 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000277}
278
279bool
280Platform::GetModuleSpec (const FileSpec& module_file_spec,
281 const ArchSpec& arch,
282 ModuleSpec &module_spec)
283{
284 ModuleSpecList module_specs;
285 if (ObjectFile::GetModuleSpecifications (module_file_spec, 0, 0, module_specs) == 0)
286 return false;
287
288 ModuleSpec matched_module_spec;
289 return module_specs.FindMatchingModuleSpec (ModuleSpec (module_file_spec, arch),
290 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000291}
292
Greg Claytone996fd32011-03-08 22:40:15 +0000293PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000294Platform::Find (const ConstString &name)
295{
296 if (name)
297 {
298 static ConstString g_host_platform_name ("host");
299 if (name == g_host_platform_name)
300 return GetHostPlatform();
301
302 Mutex::Locker locker(GetPlatformListMutex ());
303 for (const auto &platform_sp : GetPlatformList())
304 {
305 if (platform_sp->GetName() == name)
306 return platform_sp;
307 }
308 }
309 return PlatformSP();
310}
311
312PlatformSP
313Platform::Create (const ConstString &name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000314{
315 PlatformCreateInstance create_callback = NULL;
316 lldb::PlatformSP platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000317 if (name)
Greg Claytone996fd32011-03-08 22:40:15 +0000318 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000319 static ConstString g_host_platform_name ("host");
320 if (name == g_host_platform_name)
321 return GetHostPlatform();
322
323 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name);
Greg Claytone996fd32011-03-08 22:40:15 +0000324 if (create_callback)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000325 platform_sp = create_callback(true, NULL);
Greg Claytone996fd32011-03-08 22:40:15 +0000326 else
Greg Clayton615eb7e2014-09-19 20:11:50 +0000327 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000328 }
329 else
Greg Claytonded470d2011-03-19 01:12:21 +0000330 error.SetErrorString ("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000331
332 if (platform_sp)
333 {
334 Mutex::Locker locker(GetPlatformListMutex ());
335 GetPlatformList().push_back(platform_sp);
336 }
337
Greg Claytone996fd32011-03-08 22:40:15 +0000338 return platform_sp;
339}
340
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000341
342PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000343Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000344{
345 lldb::PlatformSP platform_sp;
346 if (arch.IsValid())
347 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000348 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000349 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000350 // First try exact arch matches across all platforms already created
351 Mutex::Locker locker(GetPlatformListMutex ());
352 for (const auto &platform_sp : GetPlatformList())
Greg Clayton1e0c8842013-01-11 20:49:54 +0000353 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000354 if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
355 return platform_sp;
356 }
357
358 // Next try compatible arch matches across all platforms already created
359 for (const auto &platform_sp : GetPlatformList())
360 {
361 if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
Greg Clayton1e0c8842013-01-11 20:49:54 +0000362 return platform_sp;
363 }
364 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000365
366 PlatformCreateInstance create_callback;
367 // First try exact arch matches across all platform plug-ins
368 uint32_t idx;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000369 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
370 {
371 if (create_callback)
372 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000373 platform_sp = create_callback(false, &arch);
374 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
375 {
376 Mutex::Locker locker(GetPlatformListMutex ());
377 GetPlatformList().push_back(platform_sp);
Greg Clayton1e0c8842013-01-11 20:49:54 +0000378 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000379 }
380 }
381 }
382 // Next try compatible arch matches across all platform plug-ins
383 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
384 {
385 if (create_callback)
386 {
387 platform_sp = create_callback(false, &arch);
388 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
389 {
390 Mutex::Locker locker(GetPlatformListMutex ());
391 GetPlatformList().push_back(platform_sp);
392 return platform_sp;
393 }
Greg Clayton1e0c8842013-01-11 20:49:54 +0000394 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000395 }
396 }
397 else
398 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000399 if (platform_arch_ptr)
400 platform_arch_ptr->Clear();
401 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000402 return platform_sp;
403}
404
Greg Claytone996fd32011-03-08 22:40:15 +0000405//------------------------------------------------------------------
406/// Default Constructor
407//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000408Platform::Platform (bool is_host) :
409 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000410 m_os_version_set_while_connected (false),
411 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000412 m_sdk_sysroot (),
413 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000414 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000415 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000416 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000417 m_major_os_version (UINT32_MAX),
418 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000419 m_update_os_version (UINT32_MAX),
420 m_system_arch(),
Greg Clayton7597b352015-02-02 20:45:17 +0000421 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +0000422 m_uid_map(),
423 m_gid_map(),
424 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000425 m_max_gid_name_len (0),
426 m_supports_rsync (false),
427 m_rsync_opts (),
428 m_rsync_prefix (),
429 m_supports_ssh (false),
430 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000431 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000432 m_trap_handlers(),
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000433 m_calculated_trap_handlers (false),
434 m_module_cache (llvm::make_unique<ModuleCache> ())
Greg Claytone996fd32011-03-08 22:40:15 +0000435{
Greg Clayton5160ce52013-03-27 23:08:40 +0000436 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000437 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000438 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000439}
440
441//------------------------------------------------------------------
442/// Destructor.
443///
444/// The destructor is virtual since this class is designed to be
445/// inherited from by the plug-in instance.
446//------------------------------------------------------------------
447Platform::~Platform()
448{
Greg Clayton5160ce52013-03-27 23:08:40 +0000449 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000450 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000451 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000452}
453
Greg Clayton1cb64962011-03-24 04:28:38 +0000454void
455Platform::GetStatus (Stream &strm)
456{
457 uint32_t major = UINT32_MAX;
458 uint32_t minor = UINT32_MAX;
459 uint32_t update = UINT32_MAX;
460 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000461 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000462
463 ArchSpec arch (GetSystemArchitecture());
464 if (arch.IsValid())
465 {
466 if (!arch.GetTriple().str().empty())
Greg Clayton32e0a752011-03-30 18:16:51 +0000467 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000468 }
469
470 if (GetOSVersion(major, minor, update))
471 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000472 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000473 if (minor != UINT32_MAX)
474 strm.Printf(".%u", minor);
475 if (update != UINT32_MAX)
476 strm.Printf(".%u", update);
477
478 if (GetOSBuildString (s))
479 strm.Printf(" (%s)", s.c_str());
480
481 strm.EOL();
482 }
483
484 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000485 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000486
487 if (IsHost())
488 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000489 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000490 }
491 else
492 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000493 const bool is_connected = IsConnected();
494 if (is_connected)
495 strm.Printf(" Hostname: %s\n", GetHostname());
496 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000497 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000498
Greg Claytonfbb76342013-11-20 21:07:01 +0000499 if (GetWorkingDirectory())
500 {
501 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
502 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000503 if (!IsConnected())
504 return;
505
506 std::string specific_info(GetPlatformSpecificConnectionInformation());
507
508 if (specific_info.empty() == false)
509 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000510}
511
Greg Claytonded470d2011-03-19 01:12:21 +0000512
513bool
514Platform::GetOSVersion (uint32_t &major,
515 uint32_t &minor,
516 uint32_t &update)
517{
Greg Clayton7597b352015-02-02 20:45:17 +0000518 Mutex::Locker locker (m_mutex);
519
Greg Claytonded470d2011-03-19 01:12:21 +0000520 bool success = m_major_os_version != UINT32_MAX;
521 if (IsHost())
522 {
523 if (!success)
524 {
525 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000526 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000527 m_os_version_set_while_connected = success;
528 }
529 }
530 else
531 {
532 // We have a remote platform. We can only fetch the remote
533 // OS version if we are connected, and we don't want to do it
534 // more than once.
535
536 const bool is_connected = IsConnected();
537
Greg Clayton1cb64962011-03-24 04:28:38 +0000538 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000539 if (success)
540 {
541 // We have valid OS version info, check to make sure it wasn't
542 // manually set prior to connecting. If it was manually set prior
543 // to connecting, then lets fetch the actual OS version info
544 // if we are now connected.
545 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000546 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000547 }
548 else
549 {
550 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000551 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000552 }
553
Greg Clayton1cb64962011-03-24 04:28:38 +0000554 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000555 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000556 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000557 m_os_version_set_while_connected = success;
558 }
559 }
560
561 if (success)
562 {
563 major = m_major_os_version;
564 minor = m_minor_os_version;
565 update = m_update_os_version;
566 }
567 return success;
568}
Greg Clayton1cb64962011-03-24 04:28:38 +0000569
570bool
571Platform::GetOSBuildString (std::string &s)
572{
Zachary Turner97a14e62014-08-19 17:18:29 +0000573 s.clear();
574
Greg Clayton1cb64962011-03-24 04:28:38 +0000575 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000576#if !defined(__linux__)
577 return HostInfo::GetOSBuildString(s);
578#else
579 return false;
580#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000581 else
582 return GetRemoteOSBuildString (s);
583}
584
585bool
586Platform::GetOSKernelDescription (std::string &s)
587{
588 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000589#if !defined(__linux__)
590 return HostInfo::GetOSKernelDescription(s);
591#else
592 return false;
593#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000594 else
595 return GetRemoteOSKernelDescription (s);
596}
597
Sean Callanan5dc29812014-12-05 01:16:31 +0000598void
Greg Claytoncd6bbba2015-01-22 18:25:49 +0000599Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options)
Sean Callanan5dc29812014-12-05 01:16:31 +0000600{
601 std::vector<std::string> default_compilation_options =
602 {
603 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"
604 };
605
606 options.insert(options.end(),
607 default_compilation_options.begin(),
608 default_compilation_options.end());
609}
610
611
Greg Clayton57abc5d2013-05-10 21:47:16 +0000612ConstString
Greg Claytonfbb76342013-11-20 21:07:01 +0000613Platform::GetWorkingDirectory ()
614{
615 if (IsHost())
616 {
617 char cwd[PATH_MAX];
618 if (getcwd(cwd, sizeof(cwd)))
619 return ConstString(cwd);
620 else
621 return ConstString();
622 }
623 else
624 {
625 if (!m_working_dir)
626 m_working_dir = GetRemoteWorkingDirectory();
627 return m_working_dir;
628 }
629}
630
631
632struct RecurseCopyBaton
633{
634 const FileSpec& dst;
635 Platform *platform_ptr;
636 Error error;
637};
638
639
640static FileSpec::EnumerateDirectoryResult
641RecurseCopy_Callback (void *baton,
642 FileSpec::FileType file_type,
643 const FileSpec &src)
644{
645 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
646 switch (file_type)
647 {
648 case FileSpec::eFileTypePipe:
649 case FileSpec::eFileTypeSocket:
650 // we have no way to copy pipes and sockets - ignore them and continue
651 return FileSpec::eEnumerateDirectoryResultNext;
652 break;
653
654 case FileSpec::eFileTypeDirectory:
655 {
656 // make the new directory and get in there
657 FileSpec dst_dir = rc_baton->dst;
658 if (!dst_dir.GetFilename())
659 dst_dir.GetFilename() = src.GetLastPathComponent();
660 std::string dst_dir_path (dst_dir.GetPath());
661 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir_path.c_str(), lldb::eFilePermissionsDirectoryDefault);
662 if (error.Fail())
663 {
664 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", dst_dir_path.c_str());
665 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
666 }
667
668 // now recurse
669 std::string src_dir_path (src.GetPath());
670
671 // Make a filespec that only fills in the directory of a FileSpec so
672 // when we enumerate we can quickly fill in the filename for dst copies
673 FileSpec recurse_dst;
674 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
675 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
676 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
677 if (rc_baton2.error.Fail())
678 {
679 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
680 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
681 }
682 return FileSpec::eEnumerateDirectoryResultNext;
683 }
684 break;
685
686 case FileSpec::eFileTypeSymbolicLink:
687 {
688 // copy the file and keep going
689 FileSpec dst_file = rc_baton->dst;
690 if (!dst_file.GetFilename())
691 dst_file.GetFilename() = src.GetFilename();
692
693 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000694
695 rc_baton->error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000696
697 if (rc_baton->error.Fail())
698 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
699
700 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file.GetPath().c_str(), buf);
701
702 if (rc_baton->error.Fail())
703 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
704
705 return FileSpec::eEnumerateDirectoryResultNext;
706 }
707 break;
708 case FileSpec::eFileTypeRegular:
709 {
710 // copy the file and keep going
711 FileSpec dst_file = rc_baton->dst;
712 if (!dst_file.GetFilename())
713 dst_file.GetFilename() = src.GetFilename();
714 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
715 if (err.Fail())
716 {
717 rc_baton->error.SetErrorString(err.AsCString());
718 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
719 }
720 return FileSpec::eEnumerateDirectoryResultNext;
721 }
722 break;
723
724 case FileSpec::eFileTypeInvalid:
725 case FileSpec::eFileTypeOther:
726 case FileSpec::eFileTypeUnknown:
727 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
728 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
729 break;
730 }
David Majnemer8faf9372014-09-16 06:34:29 +0000731 llvm_unreachable("Unhandled FileSpec::FileType!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000732}
733
734Error
735Platform::Install (const FileSpec& src, const FileSpec& dst)
736{
737 Error error;
738
739 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
740 if (log)
741 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
742 FileSpec fixed_dst(dst);
743
744 if (!fixed_dst.GetFilename())
745 fixed_dst.GetFilename() = src.GetFilename();
746
747 ConstString working_dir = GetWorkingDirectory();
748
749 if (dst)
750 {
751 if (dst.GetDirectory())
752 {
753 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
754 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
755 {
756 fixed_dst.GetDirectory() = dst.GetDirectory();
757 }
758 // If the fixed destination file doesn't have a directory yet,
759 // then we must have a relative path. We will resolve this relative
760 // path against the platform's working directory
761 if (!fixed_dst.GetDirectory())
762 {
763 FileSpec relative_spec;
764 std::string path;
765 if (working_dir)
766 {
767 relative_spec.SetFile(working_dir.GetCString(), false);
768 relative_spec.AppendPathComponent(dst.GetPath().c_str());
769 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
770 }
771 else
772 {
773 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
774 return error;
775 }
776 }
777 }
778 else
779 {
780 if (working_dir)
781 {
782 fixed_dst.GetDirectory() = working_dir;
783 }
784 else
785 {
786 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
787 return error;
788 }
789 }
790 }
791 else
792 {
793 if (working_dir)
794 {
795 fixed_dst.GetDirectory() = working_dir;
796 }
797 else
798 {
799 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
800 return error;
801 }
802 }
803
804 if (log)
805 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
806
807 if (GetSupportsRSync())
808 {
809 error = PutFile(src, dst);
810 }
811 else
812 {
813 switch (src.GetFileType())
814 {
815 case FileSpec::eFileTypeDirectory:
816 {
817 if (GetFileExists (fixed_dst))
818 Unlink (fixed_dst.GetPath().c_str());
819 uint32_t permissions = src.GetPermissions();
820 if (permissions == 0)
821 permissions = eFilePermissionsDirectoryDefault;
822 std::string dst_dir_path(fixed_dst.GetPath());
823 error = MakeDirectory(dst_dir_path.c_str(), permissions);
824 if (error.Success())
825 {
826 // Make a filespec that only fills in the directory of a FileSpec so
827 // when we enumerate we can quickly fill in the filename for dst copies
828 FileSpec recurse_dst;
829 recurse_dst.GetDirectory().SetCString(dst_dir_path.c_str());
830 std::string src_dir_path (src.GetPath());
831 RecurseCopyBaton baton = { recurse_dst, this, Error() };
832 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
833 return baton.error;
834 }
835 }
836 break;
837
838 case FileSpec::eFileTypeRegular:
839 if (GetFileExists (fixed_dst))
840 Unlink (fixed_dst.GetPath().c_str());
841 error = PutFile(src, fixed_dst);
842 break;
843
844 case FileSpec::eFileTypeSymbolicLink:
845 {
846 if (GetFileExists (fixed_dst))
847 Unlink (fixed_dst.GetPath().c_str());
848 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000849 error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000850 if (error.Success())
851 error = CreateSymlink(dst.GetPath().c_str(), buf);
852 }
853 break;
854 case FileSpec::eFileTypePipe:
855 error.SetErrorString("platform install doesn't handle pipes");
856 break;
857 case FileSpec::eFileTypeSocket:
858 error.SetErrorString("platform install doesn't handle sockets");
859 break;
860 case FileSpec::eFileTypeInvalid:
861 case FileSpec::eFileTypeUnknown:
862 case FileSpec::eFileTypeOther:
863 error.SetErrorString("platform install doesn't handle non file or directory items");
864 break;
865 }
866 }
867 return error;
868}
869
870bool
871Platform::SetWorkingDirectory (const ConstString &path)
872{
873 if (IsHost())
874 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000875 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
876 if (log)
877 log->Printf("Platform::SetWorkingDirectory('%s')", path.GetCString());
Colin Riley909bb7a2013-11-26 15:10:46 +0000878#ifdef _WIN32
879 // Not implemented on Windows
880 return false;
881#else
Greg Claytonfbb76342013-11-20 21:07:01 +0000882 if (path)
883 {
884 if (chdir(path.GetCString()) == 0)
885 return true;
886 }
887 return false;
Colin Riley909bb7a2013-11-26 15:10:46 +0000888#endif
Greg Claytonfbb76342013-11-20 21:07:01 +0000889 }
890 else
891 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000892 m_working_dir.Clear();
Greg Claytonfbb76342013-11-20 21:07:01 +0000893 return SetRemoteWorkingDirectory(path);
894 }
895}
896
897Error
898Platform::MakeDirectory (const char *path, uint32_t permissions)
899{
900 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000901 return FileSystem::MakeDirectory(path, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000902 else
903 {
904 Error error;
905 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
906 return error;
907 }
908}
909
910Error
911Platform::GetFilePermissions (const char *path, uint32_t &file_permissions)
912{
913 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000914 return FileSystem::GetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000915 else
916 {
917 Error error;
918 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
919 return error;
920 }
921}
922
923Error
924Platform::SetFilePermissions (const char *path, uint32_t file_permissions)
925{
926 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000927 return FileSystem::SetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000928 else
929 {
930 Error error;
931 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
932 return error;
933 }
934}
935
936ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000937Platform::GetName ()
938{
Greg Claytonfbb76342013-11-20 21:07:01 +0000939 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000940}
941
942const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000943Platform::GetHostname ()
944{
Greg Claytonab65b342011-04-13 22:47:15 +0000945 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000946 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000947
948 if (m_name.empty())
949 return NULL;
950 return m_name.c_str();
951}
952
Greg Clayton5fb8f792013-12-02 19:35:49 +0000953bool
954Platform::SetRemoteWorkingDirectory(const ConstString &path)
955{
956 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
957 if (log)
958 log->Printf("Platform::SetRemoteWorkingDirectory('%s')", path.GetCString());
959 m_working_dir = path;
960 return true;
961}
962
Greg Clayton32e0a752011-03-30 18:16:51 +0000963const char *
964Platform::GetUserName (uint32_t uid)
965{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000966#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000967 const char *user_name = GetCachedUserName(uid);
968 if (user_name)
969 return user_name;
970 if (IsHost())
971 {
972 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000973 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000974 return SetCachedUserName (uid, name.c_str(), name.size());
975 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000976#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000977 return NULL;
978}
979
Greg Clayton32e0a752011-03-30 18:16:51 +0000980const char *
981Platform::GetGroupName (uint32_t gid)
982{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000983#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000984 const char *group_name = GetCachedGroupName(gid);
985 if (group_name)
986 return group_name;
987 if (IsHost())
988 {
989 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000990 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000991 return SetCachedGroupName (gid, name.c_str(), name.size());
992 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000993#endif
Greg Clayton32e0a752011-03-30 18:16:51 +0000994 return NULL;
995}
Greg Clayton1cb64962011-03-24 04:28:38 +0000996
Greg Claytonded470d2011-03-19 01:12:21 +0000997bool
998Platform::SetOSVersion (uint32_t major,
999 uint32_t minor,
1000 uint32_t update)
1001{
1002 if (IsHost())
1003 {
Zachary Turner97a14e62014-08-19 17:18:29 +00001004 // We don't need anyone setting the OS version for the host platform,
1005 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +00001006 return false;
1007 }
1008 else
1009 {
1010 // We have a remote platform, allow setting the target OS version if
1011 // we aren't connected, since if we are connected, we should be able to
1012 // request the remote OS version from the connected platform.
1013 if (IsConnected())
1014 return false;
1015 else
1016 {
1017 // We aren't connected and we might want to set the OS version
1018 // ahead of time before we connect so we can peruse files and
1019 // use a local SDK or PDK cache of support files to disassemble
1020 // or do other things.
1021 m_major_os_version = major;
1022 m_minor_os_version = minor;
1023 m_update_os_version = update;
1024 return true;
1025 }
1026 }
1027 return false;
1028}
1029
1030
Greg Claytone996fd32011-03-08 22:40:15 +00001031Error
Greg Clayton8012cad2014-11-17 19:39:20 +00001032Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001033 lldb::ModuleSP &exe_module_sp,
1034 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +00001035{
1036 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +00001037 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +00001038 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001039 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +00001040 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001041 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001042 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001043 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001044 NULL,
1045 NULL);
1046 }
1047 else
1048 {
1049 // No valid architecture was specified, ask the platform for
1050 // the architectures that we should be using (in the correct order)
1051 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +00001052 ModuleSpec arch_module_spec(module_spec);
1053 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +00001054 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001055 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001056 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001057 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001058 NULL,
1059 NULL);
1060 // Did we find an executable using one of the
1061 if (error.Success() && exe_module_sp)
1062 break;
1063 }
1064 }
1065 }
1066 else
1067 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001068 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +00001069 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +00001070 }
1071 return error;
1072}
1073
Greg Clayton103f0282012-09-12 02:03:59 +00001074Error
1075Platform::ResolveSymbolFile (Target &target,
1076 const ModuleSpec &sym_spec,
1077 FileSpec &sym_file)
1078{
1079 Error error;
1080 if (sym_spec.GetSymbolFileSpec().Exists())
1081 sym_file = sym_spec.GetSymbolFileSpec();
1082 else
1083 error.SetErrorString("unable to resolve symbol file");
1084 return error;
1085
1086}
1087
1088
1089
Greg Claytonaa516842011-08-11 16:25:18 +00001090bool
1091Platform::ResolveRemotePath (const FileSpec &platform_path,
1092 FileSpec &resolved_platform_path)
1093{
1094 resolved_platform_path = platform_path;
1095 return resolved_platform_path.ResolvePath();
1096}
1097
Greg Claytonded470d2011-03-19 01:12:21 +00001098
1099const ArchSpec &
1100Platform::GetSystemArchitecture()
1101{
1102 if (IsHost())
1103 {
1104 if (!m_system_arch.IsValid())
1105 {
1106 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +00001107 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +00001108 m_system_arch_set_while_connected = m_system_arch.IsValid();
1109 }
1110 }
1111 else
1112 {
1113 // We have a remote platform. We can only fetch the remote
1114 // system architecture if we are connected, and we don't want to do it
1115 // more than once.
1116
1117 const bool is_connected = IsConnected();
1118
1119 bool fetch = false;
1120 if (m_system_arch.IsValid())
1121 {
1122 // We have valid OS version info, check to make sure it wasn't
1123 // manually set prior to connecting. If it was manually set prior
1124 // to connecting, then lets fetch the actual OS version info
1125 // if we are now connected.
1126 if (is_connected && !m_system_arch_set_while_connected)
1127 fetch = true;
1128 }
1129 else
1130 {
1131 // We don't have valid OS version info, fetch it if we are connected
1132 fetch = is_connected;
1133 }
1134
1135 if (fetch)
1136 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001137 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001138 m_system_arch_set_while_connected = m_system_arch.IsValid();
1139 }
1140 }
1141 return m_system_arch;
1142}
1143
1144
Greg Claytone996fd32011-03-08 22:40:15 +00001145Error
Greg Claytond314e812011-03-23 00:09:55 +00001146Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001147{
1148 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001149 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001150 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001151 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001152 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001153 return error;
1154}
1155
1156Error
Greg Claytond314e812011-03-23 00:09:55 +00001157Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001158{
1159 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001160 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001161 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001162 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001163 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001164 return error;
1165}
Greg Clayton32e0a752011-03-30 18:16:51 +00001166
1167bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001168Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001169{
1170 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001171 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001172 if (IsHost())
1173 return Host::GetProcessInfo (pid, process_info);
1174 return false;
1175}
1176
1177uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001178Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1179 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001180{
Greg Clayton8b82f082011-04-12 05:54:46 +00001181 // Take care of the host case so that each subclass can just
1182 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001183 uint32_t match_count = 0;
1184 if (IsHost())
1185 match_count = Host::FindProcesses (match_info, process_infos);
1186 return match_count;
1187}
Greg Clayton8b82f082011-04-12 05:54:46 +00001188
1189
1190Error
1191Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1192{
1193 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001194 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1195 if (log)
1196 log->Printf ("Platform::%s()", __FUNCTION__);
1197
1198 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001199 // call this function to get the host functionality.
1200 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001201 {
1202 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1203 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1204
1205 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1206 {
1207 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001208 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1209 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001210 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001211 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001212 {
1213 const FileSpec &shell = launch_info.GetShell();
1214 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001215 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1216 __FUNCTION__,
1217 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001218 shell_str);
1219 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001220
Greg Claytond1cf11a2012-04-14 01:42:46 +00001221 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1222 is_localhost,
1223 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001224 first_arg_is_full_shell_command,
1225 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001226 return error;
1227 }
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001228 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments))
Enrico Granatad7a83a92015-02-10 03:06:24 +00001229 {
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001230 error = ShellExpandArguments(launch_info);
Enrico Granata83a14372015-02-20 21:48:38 +00001231 if (error.Fail())
Enrico Granatad7a83a92015-02-10 03:06:24 +00001232 return error;
Enrico Granatad7a83a92015-02-10 03:06:24 +00001233 }
Greg Clayton84db9102012-03-26 23:03:23 +00001234
Todd Fialaac33cc92014-10-09 01:02:08 +00001235 if (log)
1236 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1237
Greg Clayton8b82f082011-04-12 05:54:46 +00001238 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001239 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001240 else
1241 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1242 return error;
1243}
1244
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001245Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001246Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +00001247{
1248 if (IsHost())
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001249 return Host::ShellExpandArguments(launch_info);
1250 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001251}
1252
1253Error
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001254Platform::KillProcess (const lldb::pid_t pid)
1255{
1256 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1257 if (log)
1258 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
1259
1260 if (!IsHost ())
1261 return Error ("base lldb_private::Platform class can't launch remote processes");
1262
1263 Host::Kill (pid, SIGTERM);
1264 return Error();
1265}
1266
Greg Clayton8b82f082011-04-12 05:54:46 +00001267lldb::ProcessSP
1268Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1269 Debugger &debugger,
1270 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001271 Error &error)
1272{
Todd Fialaac33cc92014-10-09 01:02:08 +00001273 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1274 if (log)
1275 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1276
Greg Clayton8b82f082011-04-12 05:54:46 +00001277 ProcessSP process_sp;
1278 // Make sure we stop at the entry point
1279 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001280 // We always launch the process we are going to debug in a separate process
1281 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1282 // about the target getting them as well.
1283 launch_info.SetLaunchInSeparateProcessGroup(true);
1284
Greg Clayton8b82f082011-04-12 05:54:46 +00001285 error = LaunchProcess (launch_info);
1286 if (error.Success())
1287 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001288 if (log)
1289 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001290 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001291 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001292 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001293 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001294 if (process_sp)
1295 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001296 if (log)
1297 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001298 launch_info.SetHijackListener(attach_info.GetHijackListener());
1299
Greg Claytone24c4ac2011-11-17 04:46:02 +00001300 // Since we attached to the process, it will think it needs to detach
1301 // if the process object just goes away without an explicit call to
1302 // Process::Kill() or Process::Detach(), so let it know to kill the
1303 // process if this happens.
1304 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001305
1306 // If we didn't have any file actions, the pseudo terminal might
1307 // have been used where the slave side was given as the file to
1308 // open for stdin/out/err after we have already opened the master
1309 // so we can read/write stdin/out/err.
1310 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1311 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1312 {
1313 process_sp->SetSTDIOFileDescriptor(pty_fd);
1314 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001315 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001316 else
1317 {
1318 if (log)
1319 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1320 }
1321 }
1322 else
1323 {
1324 if (log)
1325 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001326 }
1327 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001328 else
1329 {
1330 if (log)
1331 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1332 }
1333
Greg Clayton8b82f082011-04-12 05:54:46 +00001334 return process_sp;
1335}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001336
1337
1338lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001339Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001340{
1341 lldb::PlatformSP platform_sp;
1342 Error error;
1343 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001344 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001345 return platform_sp;
1346}
1347
1348
1349//------------------------------------------------------------------
1350/// Lets a platform answer if it is compatible with a given
1351/// architecture and the target triple contained within.
1352//------------------------------------------------------------------
1353bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001354Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001355{
1356 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001357 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001358 {
Greg Clayton70512312012-05-08 01:45:38 +00001359 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001360 // Try for an exact architecture match first.
1361 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001362 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001363 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001364 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001365 if (arch.IsExactMatch(platform_arch))
1366 {
1367 if (compatible_arch_ptr)
1368 *compatible_arch_ptr = platform_arch;
1369 return true;
1370 }
1371 }
1372 }
1373 else
1374 {
1375 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1376 {
1377 if (arch.IsCompatibleMatch(platform_arch))
1378 {
1379 if (compatible_arch_ptr)
1380 *compatible_arch_ptr = platform_arch;
1381 return true;
1382 }
Greg Clayton70512312012-05-08 01:45:38 +00001383 }
1384 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001385 }
Greg Clayton70512312012-05-08 01:45:38 +00001386 if (compatible_arch_ptr)
1387 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001388 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001389}
1390
Daniel Maleae0f8f572013-08-26 23:57:52 +00001391Error
1392Platform::PutFile (const FileSpec& source,
1393 const FileSpec& destination,
1394 uint32_t uid,
1395 uint32_t gid)
1396{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001397 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1398 if (log)
1399 log->Printf("[PutFile] Using block by block transfer....\n");
1400
Pavel Labath646b0642015-02-17 16:07:52 +00001401 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001402 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1403 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1404
1405 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1406 Error error;
1407 uint32_t permissions = source_file.GetPermissions(error);
1408 if (permissions == 0)
1409 permissions = lldb::eFilePermissionsFileDefault;
1410
1411 if (!source_file.IsValid())
1412 return Error("PutFile: unable to open source file");
1413 lldb::user_id_t dest_file = OpenFile (destination,
1414 File::eOpenOptionCanCreate |
1415 File::eOpenOptionWrite |
Pavel Labath646b0642015-02-17 16:07:52 +00001416 File::eOpenOptionTruncate |
1417 File::eOpenOptionCloseOnExec,
Vince Harron1b5a74e2015-01-21 22:42:49 +00001418 permissions,
1419 error);
1420 if (log)
1421 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1422
1423 if (error.Fail())
1424 return error;
1425 if (dest_file == UINT64_MAX)
1426 return Error("unable to open target file");
1427 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1428 uint64_t offset = 0;
1429 for (;;)
1430 {
1431 size_t bytes_read = buffer_sp->GetByteSize();
1432 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1433 if (error.Fail() || bytes_read == 0)
1434 break;
1435
1436 const uint64_t bytes_written = WriteFile(dest_file, offset,
1437 buffer_sp->GetBytes(), bytes_read, error);
1438 if (error.Fail())
1439 break;
1440
1441 offset += bytes_written;
1442 if (bytes_written != bytes_read)
1443 {
1444 // We didn't write the correct number of bytes, so adjust
1445 // the file position in the source file we are reading from...
1446 source_file.SeekFromStart(offset);
1447 }
1448 }
1449 CloseFile(dest_file, error);
1450
1451 if (uid == UINT32_MAX && gid == UINT32_MAX)
1452 return error;
1453
1454 // TODO: ChownFile?
1455
Daniel Maleae0f8f572013-08-26 23:57:52 +00001456 return error;
1457}
1458
1459Error
1460Platform::GetFile (const FileSpec& source,
1461 const FileSpec& destination)
1462{
1463 Error error("unimplemented");
1464 return error;
1465}
1466
Greg Claytonfbb76342013-11-20 21:07:01 +00001467Error
1468Platform::CreateSymlink (const char *src, // The name of the link is in src
1469 const char *dst)// The symlink points to dst
1470{
1471 Error error("unimplemented");
1472 return error;
1473}
1474
Daniel Maleae0f8f572013-08-26 23:57:52 +00001475bool
1476Platform::GetFileExists (const lldb_private::FileSpec& file_spec)
1477{
1478 return false;
1479}
1480
Greg Claytonfbb76342013-11-20 21:07:01 +00001481Error
1482Platform::Unlink (const char *path)
1483{
1484 Error error("unimplemented");
1485 return error;
1486}
1487
Robert Flack96ad3de2015-05-09 15:53:31 +00001488uint64_t
1489Platform::ConvertMmapFlagsToPlatform(unsigned flags)
1490{
1491 uint64_t flags_platform = 0;
1492 if (flags & eMmapFlagsPrivate)
1493 flags_platform |= MAP_PRIVATE;
1494 if (flags & eMmapFlagsAnon)
1495 flags_platform |= MAP_ANON;
1496 return flags_platform;
1497}
Greg Claytonfbb76342013-11-20 21:07:01 +00001498
Daniel Maleae0f8f572013-08-26 23:57:52 +00001499lldb_private::Error
1500Platform::RunShellCommand (const char *command, // Shouldn't be NULL
1501 const char *working_dir, // Pass NULL to use the current working directory
1502 int *status_ptr, // Pass NULL if you don't want the process exit status
1503 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1504 std::string *command_output, // Pass NULL if you don't want the command output
1505 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
1506{
1507 if (IsHost())
1508 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1509 else
1510 return Error("unimplemented");
1511}
1512
1513
1514bool
1515Platform::CalculateMD5 (const FileSpec& file_spec,
1516 uint64_t &low,
1517 uint64_t &high)
1518{
1519 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001520 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001521 else
1522 return false;
1523}
1524
Todd Fialaaf245d12014-06-30 21:05:18 +00001525Error
1526Platform::LaunchNativeProcess (
1527 ProcessLaunchInfo &launch_info,
1528 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1529 NativeProcessProtocolSP &process_sp)
1530{
1531 // Platforms should override this implementation if they want to
1532 // support lldb-gdbserver.
1533 return Error("unimplemented");
1534}
1535
1536Error
1537Platform::AttachNativeProcess (lldb::pid_t pid,
1538 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1539 NativeProcessProtocolSP &process_sp)
1540{
1541 // Platforms should override this implementation if they want to
1542 // support lldb-gdbserver.
1543 return Error("unimplemented");
1544}
1545
Daniel Maleae0f8f572013-08-26 23:57:52 +00001546void
1547Platform::SetLocalCacheDirectory (const char* local)
1548{
1549 m_local_cache_directory.assign(local);
1550}
1551
1552const char*
1553Platform::GetLocalCacheDirectory ()
1554{
1555 return m_local_cache_directory.c_str();
1556}
1557
1558static OptionDefinition
1559g_rsync_option_table[] =
1560{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001561 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1562 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1563 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1564 { LLDB_OPT_SET_ALL, false, "ignore-remote-hostname" , 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Do not automatically fill in the remote hostname when composing the rsync command." },
Daniel Maleae0f8f572013-08-26 23:57:52 +00001565};
1566
1567static OptionDefinition
1568g_ssh_option_table[] =
1569{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001570 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1571 { LLDB_OPT_SET_ALL, false, "ssh-opts" , 'S', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for SSH to work." },
Daniel Maleae0f8f572013-08-26 23:57:52 +00001572};
1573
1574static OptionDefinition
1575g_caching_option_table[] =
1576{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001577 { LLDB_OPT_SET_ALL, false, "local-cache-dir" , 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePath , "Path in which to store local copies of files." },
Daniel Maleae0f8f572013-08-26 23:57:52 +00001578};
1579
1580OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1581{
1582}
1583
1584OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1585{
1586}
1587
1588const lldb_private::OptionDefinition*
1589OptionGroupPlatformRSync::GetDefinitions ()
1590{
1591 return g_rsync_option_table;
1592}
1593
1594void
1595OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1596{
1597 m_rsync = false;
1598 m_rsync_opts.clear();
1599 m_rsync_prefix.clear();
1600 m_ignores_remote_hostname = false;
1601}
1602
1603lldb_private::Error
1604OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1605 uint32_t option_idx,
1606 const char *option_arg)
1607{
1608 Error error;
1609 char short_option = (char) GetDefinitions()[option_idx].short_option;
1610 switch (short_option)
1611 {
1612 case 'r':
1613 m_rsync = true;
1614 break;
1615
1616 case 'R':
1617 m_rsync_opts.assign(option_arg);
1618 break;
1619
1620 case 'P':
1621 m_rsync_prefix.assign(option_arg);
1622 break;
1623
1624 case 'i':
1625 m_ignores_remote_hostname = true;
1626 break;
1627
1628 default:
1629 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1630 break;
1631 }
1632
1633 return error;
1634}
1635
1636uint32_t
1637OptionGroupPlatformRSync::GetNumDefinitions ()
1638{
1639 return llvm::array_lengthof(g_rsync_option_table);
1640}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001641
Greg Clayton4116e932012-05-15 02:33:01 +00001642lldb::BreakpointSP
1643Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1644{
1645 return lldb::BreakpointSP();
1646}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001647
Daniel Maleae0f8f572013-08-26 23:57:52 +00001648OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1649{
1650}
1651
1652OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1653{
1654}
1655
1656const lldb_private::OptionDefinition*
1657OptionGroupPlatformSSH::GetDefinitions ()
1658{
1659 return g_ssh_option_table;
1660}
1661
1662void
1663OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1664{
1665 m_ssh = false;
1666 m_ssh_opts.clear();
1667}
1668
1669lldb_private::Error
1670OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1671 uint32_t option_idx,
1672 const char *option_arg)
1673{
1674 Error error;
1675 char short_option = (char) GetDefinitions()[option_idx].short_option;
1676 switch (short_option)
1677 {
1678 case 's':
1679 m_ssh = true;
1680 break;
1681
1682 case 'S':
1683 m_ssh_opts.assign(option_arg);
1684 break;
1685
1686 default:
1687 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1688 break;
1689 }
1690
1691 return error;
1692}
1693
1694uint32_t
1695OptionGroupPlatformSSH::GetNumDefinitions ()
1696{
1697 return llvm::array_lengthof(g_ssh_option_table);
1698}
1699
1700OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1701{
1702}
1703
1704OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1705{
1706}
1707
1708const lldb_private::OptionDefinition*
1709OptionGroupPlatformCaching::GetDefinitions ()
1710{
1711 return g_caching_option_table;
1712}
1713
1714void
1715OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1716{
1717 m_cache_dir.clear();
1718}
1719
1720lldb_private::Error
1721OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1722 uint32_t option_idx,
1723 const char *option_arg)
1724{
1725 Error error;
1726 char short_option = (char) GetDefinitions()[option_idx].short_option;
1727 switch (short_option)
1728 {
1729 case 'c':
1730 m_cache_dir.assign(option_arg);
1731 break;
1732
1733 default:
1734 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1735 break;
1736 }
1737
1738 return error;
1739}
1740
1741uint32_t
1742OptionGroupPlatformCaching::GetNumDefinitions ()
1743{
1744 return llvm::array_lengthof(g_caching_option_table);
1745}
1746
Greg Clayton67cc0632012-08-22 17:17:09 +00001747size_t
1748Platform::GetEnvironment (StringList &environment)
1749{
1750 environment.Clear();
1751 return false;
1752}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001753
1754const std::vector<ConstString> &
1755Platform::GetTrapHandlerSymbolNames ()
1756{
1757 if (!m_calculated_trap_handlers)
1758 {
Greg Clayton7597b352015-02-02 20:45:17 +00001759 Mutex::Locker locker (m_mutex);
Jason Molenda4da87062014-05-23 23:11:27 +00001760 if (!m_calculated_trap_handlers)
1761 {
1762 CalculateTrapHandlerSymbolNames();
1763 m_calculated_trap_handlers = true;
1764 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001765 }
1766 return m_trap_handlers;
1767}
1768
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001769Error
1770Platform::GetCachedExecutable (ModuleSpec &module_spec,
1771 lldb::ModuleSP &module_sp,
1772 const FileSpecList *module_search_paths_ptr,
1773 Platform &remote_platform)
1774{
1775 const auto platform_spec = module_spec.GetFileSpec ();
1776 const auto error = LoadCachedExecutable (module_spec,
1777 module_sp,
1778 module_search_paths_ptr,
1779 remote_platform);
1780 if (error.Success ())
1781 {
1782 module_spec.GetFileSpec () = module_sp->GetFileSpec ();
1783 module_spec.GetPlatformFileSpec () = platform_spec;
1784 }
1785
1786 return error;
1787}
1788
1789Error
1790Platform::LoadCachedExecutable (const ModuleSpec &module_spec,
1791 lldb::ModuleSP &module_sp,
1792 const FileSpecList *module_search_paths_ptr,
1793 Platform &remote_platform)
1794{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001795 return GetRemoteSharedModule (module_spec,
1796 nullptr,
1797 module_sp,
1798 [&](const ModuleSpec &spec)
1799 {
1800 return remote_platform.ResolveExecutable (
1801 spec, module_sp, module_search_paths_ptr);
1802 },
1803 nullptr);
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001804}
1805
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001806Error
1807Platform::GetRemoteSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001808 Process* process,
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001809 lldb::ModuleSP &module_sp,
1810 const ModuleResolver &module_resolver,
1811 bool *did_create_ptr)
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001812{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001813 // Get module information from a target.
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001814 ModuleSpec resolved_module_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001815 bool got_module_spec = false;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001816 if (process)
1817 {
1818 // Try to get module information from the process
1819 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001820 got_module_spec = true;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001821 }
1822
1823 if (!got_module_spec)
1824 {
1825 // Get module information from a target.
1826 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001827 return module_resolver (module_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001828 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001829
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001830 // Trying to find a module by UUID on local file system.
1831 const auto error = module_resolver (resolved_module_spec);
1832 if (error.Fail ())
1833 {
1834 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr))
1835 return Error ();
1836 }
1837
1838 return error;
1839}
1840
1841bool
1842Platform::GetCachedSharedModule (const ModuleSpec &module_spec,
1843 lldb::ModuleSP &module_sp,
1844 bool *did_create_ptr)
1845{
1846 if (IsHost() ||
1847 !GetGlobalPlatformProperties ()->GetUseModuleCache ())
1848 return false;
1849
1850 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
1851
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001852 // Check local cache for a module.
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001853 auto error = m_module_cache->GetAndPut (
1854 GetModuleCacheRoot (),
1855 GetCacheHostname (),
1856 module_spec,
Oleksiy Vyalov919ef9d2015-05-07 15:28:49 +00001857 [=](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec)
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001858 {
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001859 return DownloadModuleSlice (module_spec.GetFileSpec (),
1860 module_spec.GetObjectOffset (),
1861 module_spec.GetObjectSize (),
1862 tmp_download_file_spec);
1863
1864 },
1865 module_sp,
1866 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001867 if (error.Success ())
1868 return true;
1869
1870 if (log)
1871 log->Printf("Platform::%s - module %s not found in local cache: %s",
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001872 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001873 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001874}
1875
1876Error
1877Platform::DownloadModuleSlice (const FileSpec& src_file_spec,
1878 const uint64_t src_offset,
1879 const uint64_t src_size,
1880 const FileSpec& dst_file_spec)
1881{
1882 Error error;
1883
1884 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1885 if (!dst.is_open())
1886 {
1887 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ());
1888 return error;
1889 }
1890
1891 auto src_fd = OpenFile (src_file_spec,
1892 File::eOpenOptionRead,
1893 lldb::eFilePermissionsFileDefault,
1894 error);
1895
1896 if (error.Fail ())
1897 {
1898 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ());
1899 return error;
1900 }
1901
1902 std::vector<char> buffer (1024);
1903 auto offset = src_offset;
1904 uint64_t total_bytes_read = 0;
1905 while (total_bytes_read < src_size)
1906 {
1907 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read);
1908 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error);
1909 if (error.Fail ())
1910 break;
1911 if (n_read == 0)
1912 {
1913 error.SetErrorString ("read 0 bytes");
1914 break;
1915 }
1916 offset += n_read;
1917 total_bytes_read += n_read;
1918 dst.write (&buffer[0], n_read);
1919 }
1920
1921 Error close_error;
1922 CloseFile (src_fd, close_error); // Ignoring close error.
1923
1924 return error;
1925}
1926
1927FileSpec
1928Platform::GetModuleCacheRoot ()
1929{
1930 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory ();
1931 dir_spec.AppendPathComponent (GetName ().AsCString ());
1932 return dir_spec;
1933}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001934
1935const char *
1936Platform::GetCacheHostname ()
1937{
1938 return GetHostname ();
1939}