blob: 57375215981f1c3b84b6853d9f4c46fb33df11e9 [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());
Greg Claytonfbb76342013-11-20 21:07:01 +0000878 if (path)
879 {
880 if (chdir(path.GetCString()) == 0)
881 return true;
882 }
883 return false;
884 }
885 else
886 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000887 m_working_dir.Clear();
Greg Claytonfbb76342013-11-20 21:07:01 +0000888 return SetRemoteWorkingDirectory(path);
889 }
890}
891
892Error
893Platform::MakeDirectory (const char *path, uint32_t permissions)
894{
895 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000896 return FileSystem::MakeDirectory(path, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000897 else
898 {
899 Error error;
900 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
901 return error;
902 }
903}
904
905Error
906Platform::GetFilePermissions (const char *path, uint32_t &file_permissions)
907{
908 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000909 return FileSystem::GetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000910 else
911 {
912 Error error;
913 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
914 return error;
915 }
916}
917
918Error
919Platform::SetFilePermissions (const char *path, uint32_t file_permissions)
920{
921 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000922 return FileSystem::SetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000923 else
924 {
925 Error error;
926 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
927 return error;
928 }
929}
930
931ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000932Platform::GetName ()
933{
Greg Claytonfbb76342013-11-20 21:07:01 +0000934 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000935}
936
937const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000938Platform::GetHostname ()
939{
Greg Claytonab65b342011-04-13 22:47:15 +0000940 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000941 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000942
943 if (m_name.empty())
944 return NULL;
945 return m_name.c_str();
946}
947
Greg Clayton5fb8f792013-12-02 19:35:49 +0000948bool
949Platform::SetRemoteWorkingDirectory(const ConstString &path)
950{
951 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
952 if (log)
953 log->Printf("Platform::SetRemoteWorkingDirectory('%s')", path.GetCString());
954 m_working_dir = path;
955 return true;
956}
957
Greg Clayton32e0a752011-03-30 18:16:51 +0000958const char *
959Platform::GetUserName (uint32_t uid)
960{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000961#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000962 const char *user_name = GetCachedUserName(uid);
963 if (user_name)
964 return user_name;
965 if (IsHost())
966 {
967 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000968 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000969 return SetCachedUserName (uid, name.c_str(), name.size());
970 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000971#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000972 return NULL;
973}
974
Greg Clayton32e0a752011-03-30 18:16:51 +0000975const char *
976Platform::GetGroupName (uint32_t gid)
977{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000978#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000979 const char *group_name = GetCachedGroupName(gid);
980 if (group_name)
981 return group_name;
982 if (IsHost())
983 {
984 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000985 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000986 return SetCachedGroupName (gid, name.c_str(), name.size());
987 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000988#endif
Greg Clayton32e0a752011-03-30 18:16:51 +0000989 return NULL;
990}
Greg Clayton1cb64962011-03-24 04:28:38 +0000991
Greg Claytonded470d2011-03-19 01:12:21 +0000992bool
993Platform::SetOSVersion (uint32_t major,
994 uint32_t minor,
995 uint32_t update)
996{
997 if (IsHost())
998 {
Zachary Turner97a14e62014-08-19 17:18:29 +0000999 // We don't need anyone setting the OS version for the host platform,
1000 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +00001001 return false;
1002 }
1003 else
1004 {
1005 // We have a remote platform, allow setting the target OS version if
1006 // we aren't connected, since if we are connected, we should be able to
1007 // request the remote OS version from the connected platform.
1008 if (IsConnected())
1009 return false;
1010 else
1011 {
1012 // We aren't connected and we might want to set the OS version
1013 // ahead of time before we connect so we can peruse files and
1014 // use a local SDK or PDK cache of support files to disassemble
1015 // or do other things.
1016 m_major_os_version = major;
1017 m_minor_os_version = minor;
1018 m_update_os_version = update;
1019 return true;
1020 }
1021 }
1022 return false;
1023}
1024
1025
Greg Claytone996fd32011-03-08 22:40:15 +00001026Error
Greg Clayton8012cad2014-11-17 19:39:20 +00001027Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001028 lldb::ModuleSP &exe_module_sp,
1029 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +00001030{
1031 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +00001032 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +00001033 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001034 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +00001035 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001036 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001037 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001038 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001039 NULL,
1040 NULL);
1041 }
1042 else
1043 {
1044 // No valid architecture was specified, ask the platform for
1045 // the architectures that we should be using (in the correct order)
1046 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +00001047 ModuleSpec arch_module_spec(module_spec);
1048 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +00001049 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001050 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001051 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001052 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001053 NULL,
1054 NULL);
1055 // Did we find an executable using one of the
1056 if (error.Success() && exe_module_sp)
1057 break;
1058 }
1059 }
1060 }
1061 else
1062 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001063 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +00001064 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +00001065 }
1066 return error;
1067}
1068
Greg Clayton103f0282012-09-12 02:03:59 +00001069Error
1070Platform::ResolveSymbolFile (Target &target,
1071 const ModuleSpec &sym_spec,
1072 FileSpec &sym_file)
1073{
1074 Error error;
1075 if (sym_spec.GetSymbolFileSpec().Exists())
1076 sym_file = sym_spec.GetSymbolFileSpec();
1077 else
1078 error.SetErrorString("unable to resolve symbol file");
1079 return error;
1080
1081}
1082
1083
1084
Greg Claytonaa516842011-08-11 16:25:18 +00001085bool
1086Platform::ResolveRemotePath (const FileSpec &platform_path,
1087 FileSpec &resolved_platform_path)
1088{
1089 resolved_platform_path = platform_path;
1090 return resolved_platform_path.ResolvePath();
1091}
1092
Greg Claytonded470d2011-03-19 01:12:21 +00001093
1094const ArchSpec &
1095Platform::GetSystemArchitecture()
1096{
1097 if (IsHost())
1098 {
1099 if (!m_system_arch.IsValid())
1100 {
1101 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +00001102 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +00001103 m_system_arch_set_while_connected = m_system_arch.IsValid();
1104 }
1105 }
1106 else
1107 {
1108 // We have a remote platform. We can only fetch the remote
1109 // system architecture if we are connected, and we don't want to do it
1110 // more than once.
1111
1112 const bool is_connected = IsConnected();
1113
1114 bool fetch = false;
1115 if (m_system_arch.IsValid())
1116 {
1117 // We have valid OS version info, check to make sure it wasn't
1118 // manually set prior to connecting. If it was manually set prior
1119 // to connecting, then lets fetch the actual OS version info
1120 // if we are now connected.
1121 if (is_connected && !m_system_arch_set_while_connected)
1122 fetch = true;
1123 }
1124 else
1125 {
1126 // We don't have valid OS version info, fetch it if we are connected
1127 fetch = is_connected;
1128 }
1129
1130 if (fetch)
1131 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001132 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001133 m_system_arch_set_while_connected = m_system_arch.IsValid();
1134 }
1135 }
1136 return m_system_arch;
1137}
1138
1139
Greg Claytone996fd32011-03-08 22:40:15 +00001140Error
Greg Claytond314e812011-03-23 00:09:55 +00001141Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001142{
1143 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001144 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001145 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001146 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001147 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001148 return error;
1149}
1150
1151Error
Greg Claytond314e812011-03-23 00:09:55 +00001152Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001153{
1154 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001155 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001156 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001157 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001158 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001159 return error;
1160}
Greg Clayton32e0a752011-03-30 18:16:51 +00001161
1162bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001163Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001164{
1165 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001166 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001167 if (IsHost())
1168 return Host::GetProcessInfo (pid, process_info);
1169 return false;
1170}
1171
1172uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001173Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1174 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001175{
Greg Clayton8b82f082011-04-12 05:54:46 +00001176 // Take care of the host case so that each subclass can just
1177 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001178 uint32_t match_count = 0;
1179 if (IsHost())
1180 match_count = Host::FindProcesses (match_info, process_infos);
1181 return match_count;
1182}
Greg Clayton8b82f082011-04-12 05:54:46 +00001183
1184
1185Error
1186Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1187{
1188 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001189 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1190 if (log)
1191 log->Printf ("Platform::%s()", __FUNCTION__);
1192
1193 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001194 // call this function to get the host functionality.
1195 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001196 {
1197 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1198 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1199
1200 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1201 {
1202 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001203 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1204 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001205 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001206 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001207 {
1208 const FileSpec &shell = launch_info.GetShell();
1209 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001210 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1211 __FUNCTION__,
1212 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001213 shell_str);
1214 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001215
Greg Claytond1cf11a2012-04-14 01:42:46 +00001216 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1217 is_localhost,
1218 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001219 first_arg_is_full_shell_command,
1220 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001221 return error;
1222 }
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001223 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments))
Enrico Granatad7a83a92015-02-10 03:06:24 +00001224 {
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001225 error = ShellExpandArguments(launch_info);
Enrico Granata83a14372015-02-20 21:48:38 +00001226 if (error.Fail())
Enrico Granatad7a83a92015-02-10 03:06:24 +00001227 return error;
Enrico Granatad7a83a92015-02-10 03:06:24 +00001228 }
Greg Clayton84db9102012-03-26 23:03:23 +00001229
Todd Fialaac33cc92014-10-09 01:02:08 +00001230 if (log)
1231 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1232
Greg Clayton8b82f082011-04-12 05:54:46 +00001233 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001234 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001235 else
1236 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1237 return error;
1238}
1239
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001240Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001241Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +00001242{
1243 if (IsHost())
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001244 return Host::ShellExpandArguments(launch_info);
1245 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001246}
1247
1248Error
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001249Platform::KillProcess (const lldb::pid_t pid)
1250{
1251 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1252 if (log)
1253 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
1254
1255 if (!IsHost ())
1256 return Error ("base lldb_private::Platform class can't launch remote processes");
1257
1258 Host::Kill (pid, SIGTERM);
1259 return Error();
1260}
1261
Greg Clayton8b82f082011-04-12 05:54:46 +00001262lldb::ProcessSP
1263Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1264 Debugger &debugger,
1265 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001266 Error &error)
1267{
Todd Fialaac33cc92014-10-09 01:02:08 +00001268 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1269 if (log)
1270 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1271
Greg Clayton8b82f082011-04-12 05:54:46 +00001272 ProcessSP process_sp;
1273 // Make sure we stop at the entry point
1274 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001275 // We always launch the process we are going to debug in a separate process
1276 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1277 // about the target getting them as well.
1278 launch_info.SetLaunchInSeparateProcessGroup(true);
1279
Greg Clayton8b82f082011-04-12 05:54:46 +00001280 error = LaunchProcess (launch_info);
1281 if (error.Success())
1282 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001283 if (log)
1284 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001285 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001286 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001287 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001288 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001289 if (process_sp)
1290 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001291 if (log)
1292 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001293 launch_info.SetHijackListener(attach_info.GetHijackListener());
1294
Greg Claytone24c4ac2011-11-17 04:46:02 +00001295 // Since we attached to the process, it will think it needs to detach
1296 // if the process object just goes away without an explicit call to
1297 // Process::Kill() or Process::Detach(), so let it know to kill the
1298 // process if this happens.
1299 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001300
1301 // If we didn't have any file actions, the pseudo terminal might
1302 // have been used where the slave side was given as the file to
1303 // open for stdin/out/err after we have already opened the master
1304 // so we can read/write stdin/out/err.
1305 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1306 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1307 {
1308 process_sp->SetSTDIOFileDescriptor(pty_fd);
1309 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001310 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001311 else
1312 {
1313 if (log)
1314 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1315 }
1316 }
1317 else
1318 {
1319 if (log)
1320 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001321 }
1322 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001323 else
1324 {
1325 if (log)
1326 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1327 }
1328
Greg Clayton8b82f082011-04-12 05:54:46 +00001329 return process_sp;
1330}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001331
1332
1333lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001334Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001335{
1336 lldb::PlatformSP platform_sp;
1337 Error error;
1338 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001339 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001340 return platform_sp;
1341}
1342
1343
1344//------------------------------------------------------------------
1345/// Lets a platform answer if it is compatible with a given
1346/// architecture and the target triple contained within.
1347//------------------------------------------------------------------
1348bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001349Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001350{
1351 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001352 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001353 {
Greg Clayton70512312012-05-08 01:45:38 +00001354 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001355 // Try for an exact architecture match first.
1356 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001357 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001358 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001359 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001360 if (arch.IsExactMatch(platform_arch))
1361 {
1362 if (compatible_arch_ptr)
1363 *compatible_arch_ptr = platform_arch;
1364 return true;
1365 }
1366 }
1367 }
1368 else
1369 {
1370 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1371 {
1372 if (arch.IsCompatibleMatch(platform_arch))
1373 {
1374 if (compatible_arch_ptr)
1375 *compatible_arch_ptr = platform_arch;
1376 return true;
1377 }
Greg Clayton70512312012-05-08 01:45:38 +00001378 }
1379 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001380 }
Greg Clayton70512312012-05-08 01:45:38 +00001381 if (compatible_arch_ptr)
1382 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001383 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001384}
1385
Daniel Maleae0f8f572013-08-26 23:57:52 +00001386Error
1387Platform::PutFile (const FileSpec& source,
1388 const FileSpec& destination,
1389 uint32_t uid,
1390 uint32_t gid)
1391{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001392 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1393 if (log)
1394 log->Printf("[PutFile] Using block by block transfer....\n");
1395
Pavel Labath646b0642015-02-17 16:07:52 +00001396 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001397 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1398 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1399
1400 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1401 Error error;
1402 uint32_t permissions = source_file.GetPermissions(error);
1403 if (permissions == 0)
1404 permissions = lldb::eFilePermissionsFileDefault;
1405
1406 if (!source_file.IsValid())
1407 return Error("PutFile: unable to open source file");
1408 lldb::user_id_t dest_file = OpenFile (destination,
1409 File::eOpenOptionCanCreate |
1410 File::eOpenOptionWrite |
Pavel Labath646b0642015-02-17 16:07:52 +00001411 File::eOpenOptionTruncate |
1412 File::eOpenOptionCloseOnExec,
Vince Harron1b5a74e2015-01-21 22:42:49 +00001413 permissions,
1414 error);
1415 if (log)
1416 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1417
1418 if (error.Fail())
1419 return error;
1420 if (dest_file == UINT64_MAX)
1421 return Error("unable to open target file");
1422 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1423 uint64_t offset = 0;
1424 for (;;)
1425 {
1426 size_t bytes_read = buffer_sp->GetByteSize();
1427 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1428 if (error.Fail() || bytes_read == 0)
1429 break;
1430
1431 const uint64_t bytes_written = WriteFile(dest_file, offset,
1432 buffer_sp->GetBytes(), bytes_read, error);
1433 if (error.Fail())
1434 break;
1435
1436 offset += bytes_written;
1437 if (bytes_written != bytes_read)
1438 {
1439 // We didn't write the correct number of bytes, so adjust
1440 // the file position in the source file we are reading from...
1441 source_file.SeekFromStart(offset);
1442 }
1443 }
1444 CloseFile(dest_file, error);
1445
1446 if (uid == UINT32_MAX && gid == UINT32_MAX)
1447 return error;
1448
1449 // TODO: ChownFile?
1450
Daniel Maleae0f8f572013-08-26 23:57:52 +00001451 return error;
1452}
1453
1454Error
1455Platform::GetFile (const FileSpec& source,
1456 const FileSpec& destination)
1457{
1458 Error error("unimplemented");
1459 return error;
1460}
1461
Greg Claytonfbb76342013-11-20 21:07:01 +00001462Error
1463Platform::CreateSymlink (const char *src, // The name of the link is in src
1464 const char *dst)// The symlink points to dst
1465{
1466 Error error("unimplemented");
1467 return error;
1468}
1469
Daniel Maleae0f8f572013-08-26 23:57:52 +00001470bool
1471Platform::GetFileExists (const lldb_private::FileSpec& file_spec)
1472{
1473 return false;
1474}
1475
Greg Claytonfbb76342013-11-20 21:07:01 +00001476Error
1477Platform::Unlink (const char *path)
1478{
1479 Error error("unimplemented");
1480 return error;
1481}
1482
Robert Flack96ad3de2015-05-09 15:53:31 +00001483uint64_t
1484Platform::ConvertMmapFlagsToPlatform(unsigned flags)
1485{
1486 uint64_t flags_platform = 0;
1487 if (flags & eMmapFlagsPrivate)
1488 flags_platform |= MAP_PRIVATE;
1489 if (flags & eMmapFlagsAnon)
1490 flags_platform |= MAP_ANON;
1491 return flags_platform;
1492}
Greg Claytonfbb76342013-11-20 21:07:01 +00001493
Daniel Maleae0f8f572013-08-26 23:57:52 +00001494lldb_private::Error
1495Platform::RunShellCommand (const char *command, // Shouldn't be NULL
1496 const char *working_dir, // Pass NULL to use the current working directory
1497 int *status_ptr, // Pass NULL if you don't want the process exit status
1498 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1499 std::string *command_output, // Pass NULL if you don't want the command output
1500 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
1501{
1502 if (IsHost())
1503 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1504 else
1505 return Error("unimplemented");
1506}
1507
1508
1509bool
1510Platform::CalculateMD5 (const FileSpec& file_spec,
1511 uint64_t &low,
1512 uint64_t &high)
1513{
1514 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001515 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001516 else
1517 return false;
1518}
1519
Todd Fialaaf245d12014-06-30 21:05:18 +00001520Error
1521Platform::LaunchNativeProcess (
1522 ProcessLaunchInfo &launch_info,
1523 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1524 NativeProcessProtocolSP &process_sp)
1525{
1526 // Platforms should override this implementation if they want to
1527 // support lldb-gdbserver.
1528 return Error("unimplemented");
1529}
1530
1531Error
1532Platform::AttachNativeProcess (lldb::pid_t pid,
1533 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1534 NativeProcessProtocolSP &process_sp)
1535{
1536 // Platforms should override this implementation if they want to
1537 // support lldb-gdbserver.
1538 return Error("unimplemented");
1539}
1540
Daniel Maleae0f8f572013-08-26 23:57:52 +00001541void
1542Platform::SetLocalCacheDirectory (const char* local)
1543{
1544 m_local_cache_directory.assign(local);
1545}
1546
1547const char*
1548Platform::GetLocalCacheDirectory ()
1549{
1550 return m_local_cache_directory.c_str();
1551}
1552
1553static OptionDefinition
1554g_rsync_option_table[] =
1555{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001556 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1557 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1558 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1559 { 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 +00001560};
1561
1562static OptionDefinition
1563g_ssh_option_table[] =
1564{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001565 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1566 { 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 +00001567};
1568
1569static OptionDefinition
1570g_caching_option_table[] =
1571{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001572 { 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 +00001573};
1574
1575OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1576{
1577}
1578
1579OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1580{
1581}
1582
1583const lldb_private::OptionDefinition*
1584OptionGroupPlatformRSync::GetDefinitions ()
1585{
1586 return g_rsync_option_table;
1587}
1588
1589void
1590OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1591{
1592 m_rsync = false;
1593 m_rsync_opts.clear();
1594 m_rsync_prefix.clear();
1595 m_ignores_remote_hostname = false;
1596}
1597
1598lldb_private::Error
1599OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1600 uint32_t option_idx,
1601 const char *option_arg)
1602{
1603 Error error;
1604 char short_option = (char) GetDefinitions()[option_idx].short_option;
1605 switch (short_option)
1606 {
1607 case 'r':
1608 m_rsync = true;
1609 break;
1610
1611 case 'R':
1612 m_rsync_opts.assign(option_arg);
1613 break;
1614
1615 case 'P':
1616 m_rsync_prefix.assign(option_arg);
1617 break;
1618
1619 case 'i':
1620 m_ignores_remote_hostname = true;
1621 break;
1622
1623 default:
1624 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1625 break;
1626 }
1627
1628 return error;
1629}
1630
1631uint32_t
1632OptionGroupPlatformRSync::GetNumDefinitions ()
1633{
1634 return llvm::array_lengthof(g_rsync_option_table);
1635}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001636
Greg Clayton4116e932012-05-15 02:33:01 +00001637lldb::BreakpointSP
1638Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1639{
1640 return lldb::BreakpointSP();
1641}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001642
Daniel Maleae0f8f572013-08-26 23:57:52 +00001643OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1644{
1645}
1646
1647OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1648{
1649}
1650
1651const lldb_private::OptionDefinition*
1652OptionGroupPlatformSSH::GetDefinitions ()
1653{
1654 return g_ssh_option_table;
1655}
1656
1657void
1658OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1659{
1660 m_ssh = false;
1661 m_ssh_opts.clear();
1662}
1663
1664lldb_private::Error
1665OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1666 uint32_t option_idx,
1667 const char *option_arg)
1668{
1669 Error error;
1670 char short_option = (char) GetDefinitions()[option_idx].short_option;
1671 switch (short_option)
1672 {
1673 case 's':
1674 m_ssh = true;
1675 break;
1676
1677 case 'S':
1678 m_ssh_opts.assign(option_arg);
1679 break;
1680
1681 default:
1682 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1683 break;
1684 }
1685
1686 return error;
1687}
1688
1689uint32_t
1690OptionGroupPlatformSSH::GetNumDefinitions ()
1691{
1692 return llvm::array_lengthof(g_ssh_option_table);
1693}
1694
1695OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1696{
1697}
1698
1699OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1700{
1701}
1702
1703const lldb_private::OptionDefinition*
1704OptionGroupPlatformCaching::GetDefinitions ()
1705{
1706 return g_caching_option_table;
1707}
1708
1709void
1710OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1711{
1712 m_cache_dir.clear();
1713}
1714
1715lldb_private::Error
1716OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1717 uint32_t option_idx,
1718 const char *option_arg)
1719{
1720 Error error;
1721 char short_option = (char) GetDefinitions()[option_idx].short_option;
1722 switch (short_option)
1723 {
1724 case 'c':
1725 m_cache_dir.assign(option_arg);
1726 break;
1727
1728 default:
1729 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1730 break;
1731 }
1732
1733 return error;
1734}
1735
1736uint32_t
1737OptionGroupPlatformCaching::GetNumDefinitions ()
1738{
1739 return llvm::array_lengthof(g_caching_option_table);
1740}
1741
Greg Clayton67cc0632012-08-22 17:17:09 +00001742size_t
1743Platform::GetEnvironment (StringList &environment)
1744{
1745 environment.Clear();
1746 return false;
1747}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001748
1749const std::vector<ConstString> &
1750Platform::GetTrapHandlerSymbolNames ()
1751{
1752 if (!m_calculated_trap_handlers)
1753 {
Greg Clayton7597b352015-02-02 20:45:17 +00001754 Mutex::Locker locker (m_mutex);
Jason Molenda4da87062014-05-23 23:11:27 +00001755 if (!m_calculated_trap_handlers)
1756 {
1757 CalculateTrapHandlerSymbolNames();
1758 m_calculated_trap_handlers = true;
1759 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001760 }
1761 return m_trap_handlers;
1762}
1763
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001764Error
1765Platform::GetCachedExecutable (ModuleSpec &module_spec,
1766 lldb::ModuleSP &module_sp,
1767 const FileSpecList *module_search_paths_ptr,
1768 Platform &remote_platform)
1769{
1770 const auto platform_spec = module_spec.GetFileSpec ();
1771 const auto error = LoadCachedExecutable (module_spec,
1772 module_sp,
1773 module_search_paths_ptr,
1774 remote_platform);
1775 if (error.Success ())
1776 {
1777 module_spec.GetFileSpec () = module_sp->GetFileSpec ();
1778 module_spec.GetPlatformFileSpec () = platform_spec;
1779 }
1780
1781 return error;
1782}
1783
1784Error
1785Platform::LoadCachedExecutable (const ModuleSpec &module_spec,
1786 lldb::ModuleSP &module_sp,
1787 const FileSpecList *module_search_paths_ptr,
1788 Platform &remote_platform)
1789{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001790 return GetRemoteSharedModule (module_spec,
1791 nullptr,
1792 module_sp,
1793 [&](const ModuleSpec &spec)
1794 {
1795 return remote_platform.ResolveExecutable (
1796 spec, module_sp, module_search_paths_ptr);
1797 },
1798 nullptr);
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001799}
1800
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001801Error
1802Platform::GetRemoteSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001803 Process* process,
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001804 lldb::ModuleSP &module_sp,
1805 const ModuleResolver &module_resolver,
1806 bool *did_create_ptr)
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001807{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001808 // Get module information from a target.
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001809 ModuleSpec resolved_module_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001810 bool got_module_spec = false;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001811 if (process)
1812 {
1813 // Try to get module information from the process
1814 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001815 got_module_spec = true;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001816 }
1817
1818 if (!got_module_spec)
1819 {
1820 // Get module information from a target.
1821 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001822 return module_resolver (module_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001823 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001824
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001825 // Trying to find a module by UUID on local file system.
1826 const auto error = module_resolver (resolved_module_spec);
1827 if (error.Fail ())
1828 {
1829 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr))
1830 return Error ();
1831 }
1832
1833 return error;
1834}
1835
1836bool
1837Platform::GetCachedSharedModule (const ModuleSpec &module_spec,
1838 lldb::ModuleSP &module_sp,
1839 bool *did_create_ptr)
1840{
1841 if (IsHost() ||
1842 !GetGlobalPlatformProperties ()->GetUseModuleCache ())
1843 return false;
1844
1845 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
1846
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001847 // Check local cache for a module.
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001848 auto error = m_module_cache->GetAndPut (
1849 GetModuleCacheRoot (),
1850 GetCacheHostname (),
1851 module_spec,
Oleksiy Vyalov919ef9d2015-05-07 15:28:49 +00001852 [=](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec)
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001853 {
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001854 return DownloadModuleSlice (module_spec.GetFileSpec (),
1855 module_spec.GetObjectOffset (),
1856 module_spec.GetObjectSize (),
1857 tmp_download_file_spec);
1858
1859 },
1860 module_sp,
1861 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001862 if (error.Success ())
1863 return true;
1864
1865 if (log)
1866 log->Printf("Platform::%s - module %s not found in local cache: %s",
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001867 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001868 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001869}
1870
1871Error
1872Platform::DownloadModuleSlice (const FileSpec& src_file_spec,
1873 const uint64_t src_offset,
1874 const uint64_t src_size,
1875 const FileSpec& dst_file_spec)
1876{
1877 Error error;
1878
1879 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1880 if (!dst.is_open())
1881 {
1882 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ());
1883 return error;
1884 }
1885
1886 auto src_fd = OpenFile (src_file_spec,
1887 File::eOpenOptionRead,
1888 lldb::eFilePermissionsFileDefault,
1889 error);
1890
1891 if (error.Fail ())
1892 {
1893 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ());
1894 return error;
1895 }
1896
1897 std::vector<char> buffer (1024);
1898 auto offset = src_offset;
1899 uint64_t total_bytes_read = 0;
1900 while (total_bytes_read < src_size)
1901 {
1902 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read);
1903 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error);
1904 if (error.Fail ())
1905 break;
1906 if (n_read == 0)
1907 {
1908 error.SetErrorString ("read 0 bytes");
1909 break;
1910 }
1911 offset += n_read;
1912 total_bytes_read += n_read;
1913 dst.write (&buffer[0], n_read);
1914 }
1915
1916 Error close_error;
1917 CloseFile (src_fd, close_error); // Ignoring close error.
1918
1919 return error;
1920}
1921
1922FileSpec
1923Platform::GetModuleCacheRoot ()
1924{
1925 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory ();
1926 dir_spec.AppendPathComponent (GetName ().AsCString ());
1927 return dir_spec;
1928}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001929
1930const char *
1931Platform::GetCacheHostname ()
1932{
1933 return GetHostname ();
1934}