blob: 4e07a16044c3a5ec40e097c9f3c4efe74ee3805c [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"
Zachary Turner7271bab2015-05-13 19:44:24 +000023#include "lldb/Core/Debugger.h"
Greg Claytone996fd32011-03-08 22:40:15 +000024#include "lldb/Core/Error.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000025#include "lldb/Core/Log.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000026#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000028#include "lldb/Core/PluginManager.h"
Enrico Granatad7a83a92015-02-10 03:06:24 +000029#include "lldb/Core/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000030#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000031#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000032#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000033#include "lldb/Host/HostInfo.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000034#include "lldb/Interpreter/OptionValueProperties.h"
35#include "lldb/Interpreter/Property.h"
36#include "lldb/Symbol/ObjectFile.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000037#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000038#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000039#include "lldb/Target/UnixSignals.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000040#include "lldb/Utility/Utils.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000041#include "llvm/Support/FileSystem.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000042
43#include "Utility/ModuleCache.h"
Greg Claytone996fd32011-03-08 22:40:15 +000044
Robert Flack96ad3de2015-05-09 15:53:31 +000045// Define these constants from POSIX mman.h rather than include the file
46// so that they will be correct even when compiled on Linux.
47#define MAP_PRIVATE 2
48#define MAP_ANON 0x1000
49
Greg Claytone996fd32011-03-08 22:40:15 +000050using namespace lldb;
51using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000052
53static uint32_t g_initialize_count = 0;
54
Greg Claytone996fd32011-03-08 22:40:15 +000055// Use a singleton function for g_local_platform_sp to avoid init
56// constructors since LLDB is often part of a shared library
57static PlatformSP&
Greg Clayton615eb7e2014-09-19 20:11:50 +000058GetHostPlatformSP ()
Greg Claytone996fd32011-03-08 22:40:15 +000059{
Greg Clayton615eb7e2014-09-19 20:11:50 +000060 static PlatformSP g_platform_sp;
61 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000062}
63
Greg Claytonab65b342011-04-13 22:47:15 +000064const char *
65Platform::GetHostPlatformName ()
66{
67 return "host";
68}
69
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000070namespace {
71
72 PropertyDefinition
73 g_properties[] =
74 {
75 { "use-module-cache" , OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "Use module cache." },
76 { "module-cache-directory", OptionValue::eTypeFileSpec, true, 0 , nullptr, nullptr, "Root directory for cached modules." },
77 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
78 };
79
80 enum
81 {
82 ePropertyUseModuleCache,
83 ePropertyModuleCacheDirectory
84 };
85
86} // namespace
87
88
89ConstString
90PlatformProperties::GetSettingName ()
91{
92 static ConstString g_setting_name("platform");
93 return g_setting_name;
94}
95
96PlatformProperties::PlatformProperties ()
97{
98 m_collection_sp.reset (new OptionValueProperties (GetSettingName ()));
99 m_collection_sp->Initialize (g_properties);
100
101 auto module_cache_dir = GetModuleCacheDirectory ();
102 if (!module_cache_dir)
103 {
104 if (!HostInfo::GetLLDBPath (ePathTypeGlobalLLDBTempSystemDir, module_cache_dir))
105 module_cache_dir = FileSpec ("/tmp/lldb", false);
106 module_cache_dir.AppendPathComponent ("module_cache");
107 SetModuleCacheDirectory (module_cache_dir);
108 }
109}
110
111bool
112PlatformProperties::GetUseModuleCache () const
113{
114 const auto idx = ePropertyUseModuleCache;
115 return m_collection_sp->GetPropertyAtIndexAsBoolean (
116 nullptr, idx, g_properties[idx].default_uint_value != 0);
117}
118
119bool
120PlatformProperties::SetUseModuleCache (bool use_module_cache)
121{
122 return m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, ePropertyUseModuleCache, use_module_cache);
123}
124
125FileSpec
126PlatformProperties::GetModuleCacheDirectory () const
127{
128 return m_collection_sp->GetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory);
129}
130
131bool
132PlatformProperties::SetModuleCacheDirectory (const FileSpec& dir_spec)
133{
134 return m_collection_sp->SetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory, dir_spec);
135}
136
Greg Claytone996fd32011-03-08 22:40:15 +0000137//------------------------------------------------------------------
138/// Get the native host platform plug-in.
139///
140/// There should only be one of these for each host that LLDB runs
141/// upon that should be statically compiled in and registered using
142/// preprocessor macros or other similar build mechanisms.
143///
144/// This platform will be used as the default platform when launching
145/// or attaching to processes unless another platform is specified.
146//------------------------------------------------------------------
147PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000148Platform::GetHostPlatform ()
Greg Claytone996fd32011-03-08 22:40:15 +0000149{
Greg Clayton615eb7e2014-09-19 20:11:50 +0000150 return GetHostPlatformSP ();
151}
152
153static std::vector<PlatformSP> &
154GetPlatformList()
155{
156 static std::vector<PlatformSP> g_platform_list;
157 return g_platform_list;
158}
159
160static Mutex &
161GetPlatformListMutex ()
162{
163 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
164 return g_mutex;
Greg Claytone996fd32011-03-08 22:40:15 +0000165}
166
167void
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000168Platform::Initialize ()
169{
170 g_initialize_count++;
171}
172
173void
174Platform::Terminate ()
175{
176 if (g_initialize_count > 0)
177 {
178 if (--g_initialize_count == 0)
179 {
180 Mutex::Locker locker(GetPlatformListMutex ());
181 GetPlatformList().clear();
182 }
183 }
184}
185
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000186const PlatformPropertiesSP &
187Platform::GetGlobalPlatformProperties ()
188{
189 static const auto g_settings_sp (std::make_shared<PlatformProperties> ());
190 return g_settings_sp;
191}
192
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000193void
Greg Clayton615eb7e2014-09-19 20:11:50 +0000194Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp)
Greg Claytone996fd32011-03-08 22:40:15 +0000195{
196 // The native platform should use its static void Platform::Initialize()
197 // function to register itself as the native platform.
Greg Clayton615eb7e2014-09-19 20:11:50 +0000198 GetHostPlatformSP () = platform_sp;
199
200 if (platform_sp)
201 {
202 Mutex::Locker locker(GetPlatformListMutex ());
203 GetPlatformList().push_back(platform_sp);
204 }
Greg Claytone996fd32011-03-08 22:40:15 +0000205}
206
Greg Claytone996fd32011-03-08 22:40:15 +0000207Error
Steve Puccifc995722014-01-17 18:18:31 +0000208Platform::GetFileWithUUID (const FileSpec &platform_file,
209 const UUID *uuid_ptr,
210 FileSpec &local_file)
Greg Claytone996fd32011-03-08 22:40:15 +0000211{
212 // Default to the local case
213 local_file = platform_file;
214 return Error();
215}
216
Greg Clayton91c0e742013-01-11 23:44:27 +0000217FileSpecList
Enrico Granatafe7295d2014-08-16 00:32:58 +0000218Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +0000219{
Greg Clayton91c0e742013-01-11 23:44:27 +0000220 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000221}
222
Greg Clayton615eb7e2014-09-19 20:11:50 +0000223//PlatformSP
224//Platform::FindPlugin (Process *process, const ConstString &plugin_name)
225//{
226// PlatformCreateInstance create_callback = NULL;
227// if (plugin_name)
228// {
229// create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
230// if (create_callback)
231// {
232// ArchSpec arch;
233// if (process)
234// {
235// arch = process->GetTarget().GetArchitecture();
236// }
237// PlatformSP platform_sp(create_callback(process, &arch));
238// if (platform_sp)
239// return platform_sp;
240// }
241// }
242// else
243// {
244// for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
245// {
246// PlatformSP platform_sp(create_callback(process, nullptr));
247// if (platform_sp)
248// return platform_sp;
249// }
250// }
251// return PlatformSP();
252//}
Jason Molenda1c627542013-04-05 01:03:25 +0000253
Greg Clayton32e0a752011-03-30 18:16:51 +0000254Error
Greg Claytonb9a01b32012-02-26 05:51:37 +0000255Platform::GetSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000256 Process* process,
Greg Clayton32e0a752011-03-30 18:16:51 +0000257 ModuleSP &module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000258 const FileSpecList *module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000259 ModuleSP *old_module_sp_ptr,
260 bool *did_create_ptr)
261{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000262 if (IsHost ())
263 return ModuleList::GetSharedModule (module_spec,
264 module_sp,
265 module_search_paths_ptr,
266 old_module_sp_ptr,
267 did_create_ptr,
268 false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000269
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000270 return GetRemoteSharedModule (module_spec,
271 process,
272 module_sp,
273 [&](const ModuleSpec &spec)
274 {
Tamas Berghammer980662e2015-09-04 12:42:41 +0000275 Error error = ModuleList::GetSharedModule (
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000276 spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr, false);
Tamas Berghammer980662e2015-09-04 12:42:41 +0000277 if (error.Success() && module_sp)
278 module_sp->SetPlatformFileSpec(spec.GetFileSpec());
279 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000280 },
281 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000282}
283
284bool
285Platform::GetModuleSpec (const FileSpec& module_file_spec,
286 const ArchSpec& arch,
287 ModuleSpec &module_spec)
288{
289 ModuleSpecList module_specs;
290 if (ObjectFile::GetModuleSpecifications (module_file_spec, 0, 0, module_specs) == 0)
291 return false;
292
293 ModuleSpec matched_module_spec;
294 return module_specs.FindMatchingModuleSpec (ModuleSpec (module_file_spec, arch),
295 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000296}
297
Greg Claytone996fd32011-03-08 22:40:15 +0000298PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000299Platform::Find (const ConstString &name)
300{
301 if (name)
302 {
303 static ConstString g_host_platform_name ("host");
304 if (name == g_host_platform_name)
305 return GetHostPlatform();
306
307 Mutex::Locker locker(GetPlatformListMutex ());
308 for (const auto &platform_sp : GetPlatformList())
309 {
310 if (platform_sp->GetName() == name)
311 return platform_sp;
312 }
313 }
314 return PlatformSP();
315}
316
317PlatformSP
318Platform::Create (const ConstString &name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000319{
320 PlatformCreateInstance create_callback = NULL;
321 lldb::PlatformSP platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000322 if (name)
Greg Claytone996fd32011-03-08 22:40:15 +0000323 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000324 static ConstString g_host_platform_name ("host");
325 if (name == g_host_platform_name)
326 return GetHostPlatform();
327
328 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name);
Greg Claytone996fd32011-03-08 22:40:15 +0000329 if (create_callback)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000330 platform_sp = create_callback(true, NULL);
Greg Claytone996fd32011-03-08 22:40:15 +0000331 else
Greg Clayton615eb7e2014-09-19 20:11:50 +0000332 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000333 }
334 else
Greg Claytonded470d2011-03-19 01:12:21 +0000335 error.SetErrorString ("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000336
337 if (platform_sp)
338 {
339 Mutex::Locker locker(GetPlatformListMutex ());
340 GetPlatformList().push_back(platform_sp);
341 }
342
Greg Claytone996fd32011-03-08 22:40:15 +0000343 return platform_sp;
344}
345
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000346
347PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000348Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000349{
350 lldb::PlatformSP platform_sp;
351 if (arch.IsValid())
352 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000353 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000354 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000355 // First try exact arch matches across all platforms already created
356 Mutex::Locker locker(GetPlatformListMutex ());
357 for (const auto &platform_sp : GetPlatformList())
Greg Clayton1e0c8842013-01-11 20:49:54 +0000358 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000359 if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
360 return platform_sp;
361 }
362
363 // Next try compatible arch matches across all platforms already created
364 for (const auto &platform_sp : GetPlatformList())
365 {
366 if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
Greg Clayton1e0c8842013-01-11 20:49:54 +0000367 return platform_sp;
368 }
369 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000370
371 PlatformCreateInstance create_callback;
372 // First try exact arch matches across all platform plug-ins
373 uint32_t idx;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000374 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
375 {
376 if (create_callback)
377 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000378 platform_sp = create_callback(false, &arch);
379 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
380 {
381 Mutex::Locker locker(GetPlatformListMutex ());
382 GetPlatformList().push_back(platform_sp);
Greg Clayton1e0c8842013-01-11 20:49:54 +0000383 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000384 }
385 }
386 }
387 // Next try compatible arch matches across all platform plug-ins
388 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
389 {
390 if (create_callback)
391 {
392 platform_sp = create_callback(false, &arch);
393 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
394 {
395 Mutex::Locker locker(GetPlatformListMutex ());
396 GetPlatformList().push_back(platform_sp);
397 return platform_sp;
398 }
Greg Clayton1e0c8842013-01-11 20:49:54 +0000399 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000400 }
401 }
402 else
403 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000404 if (platform_arch_ptr)
405 platform_arch_ptr->Clear();
406 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000407 return platform_sp;
408}
409
Greg Claytone996fd32011-03-08 22:40:15 +0000410//------------------------------------------------------------------
411/// Default Constructor
412//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000413Platform::Platform (bool is_host) :
414 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000415 m_os_version_set_while_connected (false),
416 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000417 m_sdk_sysroot (),
418 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000419 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000420 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000421 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000422 m_major_os_version (UINT32_MAX),
423 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000424 m_update_os_version (UINT32_MAX),
425 m_system_arch(),
Greg Clayton7597b352015-02-02 20:45:17 +0000426 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +0000427 m_uid_map(),
428 m_gid_map(),
429 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000430 m_max_gid_name_len (0),
431 m_supports_rsync (false),
432 m_rsync_opts (),
433 m_rsync_prefix (),
434 m_supports_ssh (false),
435 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000436 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000437 m_trap_handlers(),
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000438 m_calculated_trap_handlers (false),
439 m_module_cache (llvm::make_unique<ModuleCache> ())
Greg Claytone996fd32011-03-08 22:40:15 +0000440{
Greg Clayton5160ce52013-03-27 23:08:40 +0000441 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000442 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000443 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000444}
445
446//------------------------------------------------------------------
447/// Destructor.
448///
449/// The destructor is virtual since this class is designed to be
450/// inherited from by the plug-in instance.
451//------------------------------------------------------------------
452Platform::~Platform()
453{
Greg Clayton5160ce52013-03-27 23:08:40 +0000454 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000455 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000456 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000457}
458
Greg Clayton1cb64962011-03-24 04:28:38 +0000459void
460Platform::GetStatus (Stream &strm)
461{
462 uint32_t major = UINT32_MAX;
463 uint32_t minor = UINT32_MAX;
464 uint32_t update = UINT32_MAX;
465 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000466 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000467
468 ArchSpec arch (GetSystemArchitecture());
469 if (arch.IsValid())
470 {
471 if (!arch.GetTriple().str().empty())
Greg Clayton32e0a752011-03-30 18:16:51 +0000472 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000473 }
474
475 if (GetOSVersion(major, minor, update))
476 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000477 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000478 if (minor != UINT32_MAX)
479 strm.Printf(".%u", minor);
480 if (update != UINT32_MAX)
481 strm.Printf(".%u", update);
482
483 if (GetOSBuildString (s))
484 strm.Printf(" (%s)", s.c_str());
485
486 strm.EOL();
487 }
488
489 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000490 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000491
492 if (IsHost())
493 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000494 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000495 }
496 else
497 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000498 const bool is_connected = IsConnected();
499 if (is_connected)
500 strm.Printf(" Hostname: %s\n", GetHostname());
501 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000502 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000503
Greg Claytonfbb76342013-11-20 21:07:01 +0000504 if (GetWorkingDirectory())
505 {
506 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
507 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000508 if (!IsConnected())
509 return;
510
511 std::string specific_info(GetPlatformSpecificConnectionInformation());
512
513 if (specific_info.empty() == false)
514 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000515}
516
Greg Claytonded470d2011-03-19 01:12:21 +0000517
518bool
519Platform::GetOSVersion (uint32_t &major,
520 uint32_t &minor,
521 uint32_t &update)
522{
Greg Clayton7597b352015-02-02 20:45:17 +0000523 Mutex::Locker locker (m_mutex);
524
Greg Claytonded470d2011-03-19 01:12:21 +0000525 bool success = m_major_os_version != UINT32_MAX;
526 if (IsHost())
527 {
528 if (!success)
529 {
530 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000531 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000532 m_os_version_set_while_connected = success;
533 }
534 }
535 else
536 {
537 // We have a remote platform. We can only fetch the remote
538 // OS version if we are connected, and we don't want to do it
539 // more than once.
540
541 const bool is_connected = IsConnected();
542
Greg Clayton1cb64962011-03-24 04:28:38 +0000543 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000544 if (success)
545 {
546 // We have valid OS version info, check to make sure it wasn't
547 // manually set prior to connecting. If it was manually set prior
548 // to connecting, then lets fetch the actual OS version info
549 // if we are now connected.
550 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000551 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000552 }
553 else
554 {
555 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000556 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000557 }
558
Greg Clayton1cb64962011-03-24 04:28:38 +0000559 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000560 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000561 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000562 m_os_version_set_while_connected = success;
563 }
564 }
565
566 if (success)
567 {
568 major = m_major_os_version;
569 minor = m_minor_os_version;
570 update = m_update_os_version;
571 }
572 return success;
573}
Greg Clayton1cb64962011-03-24 04:28:38 +0000574
575bool
576Platform::GetOSBuildString (std::string &s)
577{
Zachary Turner97a14e62014-08-19 17:18:29 +0000578 s.clear();
579
Greg Clayton1cb64962011-03-24 04:28:38 +0000580 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000581#if !defined(__linux__)
582 return HostInfo::GetOSBuildString(s);
583#else
584 return false;
585#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000586 else
587 return GetRemoteOSBuildString (s);
588}
589
590bool
591Platform::GetOSKernelDescription (std::string &s)
592{
593 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000594#if !defined(__linux__)
595 return HostInfo::GetOSKernelDescription(s);
596#else
597 return false;
598#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000599 else
600 return GetRemoteOSKernelDescription (s);
601}
602
Sean Callanan5dc29812014-12-05 01:16:31 +0000603void
Greg Claytoncd6bbba2015-01-22 18:25:49 +0000604Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options)
Sean Callanan5dc29812014-12-05 01:16:31 +0000605{
606 std::vector<std::string> default_compilation_options =
607 {
608 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"
609 };
610
611 options.insert(options.end(),
612 default_compilation_options.begin(),
613 default_compilation_options.end());
614}
615
616
Chaoren Lind3173f32015-05-29 19:52:29 +0000617FileSpec
Greg Claytonfbb76342013-11-20 21:07:01 +0000618Platform::GetWorkingDirectory ()
619{
620 if (IsHost())
621 {
622 char cwd[PATH_MAX];
623 if (getcwd(cwd, sizeof(cwd)))
Chaoren Lind3173f32015-05-29 19:52:29 +0000624 return FileSpec{cwd, true};
Greg Claytonfbb76342013-11-20 21:07:01 +0000625 else
Chaoren Lind3173f32015-05-29 19:52:29 +0000626 return FileSpec{};
Greg Claytonfbb76342013-11-20 21:07:01 +0000627 }
628 else
629 {
630 if (!m_working_dir)
631 m_working_dir = GetRemoteWorkingDirectory();
632 return m_working_dir;
633 }
634}
635
636
637struct RecurseCopyBaton
638{
639 const FileSpec& dst;
640 Platform *platform_ptr;
641 Error error;
642};
643
644
645static FileSpec::EnumerateDirectoryResult
646RecurseCopy_Callback (void *baton,
647 FileSpec::FileType file_type,
648 const FileSpec &src)
649{
650 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
651 switch (file_type)
652 {
653 case FileSpec::eFileTypePipe:
654 case FileSpec::eFileTypeSocket:
655 // we have no way to copy pipes and sockets - ignore them and continue
656 return FileSpec::eEnumerateDirectoryResultNext;
657 break;
658
659 case FileSpec::eFileTypeDirectory:
660 {
661 // make the new directory and get in there
662 FileSpec dst_dir = rc_baton->dst;
663 if (!dst_dir.GetFilename())
664 dst_dir.GetFilename() = src.GetLastPathComponent();
Chaoren Lind3173f32015-05-29 19:52:29 +0000665 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault);
Greg Claytonfbb76342013-11-20 21:07:01 +0000666 if (error.Fail())
667 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000668 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end",
669 dst_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000670 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
671 }
672
673 // now recurse
674 std::string src_dir_path (src.GetPath());
675
676 // Make a filespec that only fills in the directory of a FileSpec so
677 // when we enumerate we can quickly fill in the filename for dst copies
678 FileSpec recurse_dst;
679 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
680 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
681 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
682 if (rc_baton2.error.Fail())
683 {
684 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
685 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
686 }
687 return FileSpec::eEnumerateDirectoryResultNext;
688 }
689 break;
690
691 case FileSpec::eFileTypeSymbolicLink:
692 {
693 // copy the file and keep going
694 FileSpec dst_file = rc_baton->dst;
695 if (!dst_file.GetFilename())
696 dst_file.GetFilename() = src.GetFilename();
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000697
Chaoren Lind3173f32015-05-29 19:52:29 +0000698 FileSpec src_resolved;
699
700 rc_baton->error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000701
702 if (rc_baton->error.Fail())
703 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Chaoren Lind3173f32015-05-29 19:52:29 +0000704
705 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000706
707 if (rc_baton->error.Fail())
708 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
709
710 return FileSpec::eEnumerateDirectoryResultNext;
711 }
712 break;
713 case FileSpec::eFileTypeRegular:
714 {
715 // copy the file and keep going
716 FileSpec dst_file = rc_baton->dst;
717 if (!dst_file.GetFilename())
718 dst_file.GetFilename() = src.GetFilename();
719 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
720 if (err.Fail())
721 {
722 rc_baton->error.SetErrorString(err.AsCString());
723 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
724 }
725 return FileSpec::eEnumerateDirectoryResultNext;
726 }
727 break;
728
729 case FileSpec::eFileTypeInvalid:
730 case FileSpec::eFileTypeOther:
731 case FileSpec::eFileTypeUnknown:
732 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
733 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
734 break;
735 }
David Majnemer8faf9372014-09-16 06:34:29 +0000736 llvm_unreachable("Unhandled FileSpec::FileType!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000737}
738
739Error
740Platform::Install (const FileSpec& src, const FileSpec& dst)
741{
742 Error error;
743
744 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
745 if (log)
746 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
747 FileSpec fixed_dst(dst);
748
749 if (!fixed_dst.GetFilename())
750 fixed_dst.GetFilename() = src.GetFilename();
751
Chaoren Lind3173f32015-05-29 19:52:29 +0000752 FileSpec working_dir = GetWorkingDirectory();
Greg Claytonfbb76342013-11-20 21:07:01 +0000753
754 if (dst)
755 {
756 if (dst.GetDirectory())
757 {
758 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
759 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
760 {
761 fixed_dst.GetDirectory() = dst.GetDirectory();
762 }
763 // If the fixed destination file doesn't have a directory yet,
764 // then we must have a relative path. We will resolve this relative
765 // path against the platform's working directory
766 if (!fixed_dst.GetDirectory())
767 {
768 FileSpec relative_spec;
769 std::string path;
770 if (working_dir)
771 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000772 relative_spec = working_dir;
773 relative_spec.AppendPathComponent(dst.GetPath());
Greg Claytonfbb76342013-11-20 21:07:01 +0000774 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
775 }
776 else
777 {
778 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
779 return error;
780 }
781 }
782 }
783 else
784 {
785 if (working_dir)
786 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000787 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000788 }
789 else
790 {
791 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
792 return error;
793 }
794 }
795 }
796 else
797 {
798 if (working_dir)
799 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000800 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000801 }
802 else
803 {
804 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
805 return error;
806 }
807 }
808
809 if (log)
810 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
811
812 if (GetSupportsRSync())
813 {
814 error = PutFile(src, dst);
815 }
816 else
817 {
818 switch (src.GetFileType())
819 {
820 case FileSpec::eFileTypeDirectory:
821 {
822 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000823 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000824 uint32_t permissions = src.GetPermissions();
825 if (permissions == 0)
826 permissions = eFilePermissionsDirectoryDefault;
Chaoren Lind3173f32015-05-29 19:52:29 +0000827 error = MakeDirectory(fixed_dst, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000828 if (error.Success())
829 {
830 // Make a filespec that only fills in the directory of a FileSpec so
831 // when we enumerate we can quickly fill in the filename for dst copies
832 FileSpec recurse_dst;
Chaoren Lind3173f32015-05-29 19:52:29 +0000833 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000834 std::string src_dir_path (src.GetPath());
835 RecurseCopyBaton baton = { recurse_dst, this, Error() };
836 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
837 return baton.error;
838 }
839 }
840 break;
841
842 case FileSpec::eFileTypeRegular:
843 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000844 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000845 error = PutFile(src, fixed_dst);
846 break;
847
848 case FileSpec::eFileTypeSymbolicLink:
849 {
850 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000851 Unlink(fixed_dst);
852 FileSpec src_resolved;
853 error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000854 if (error.Success())
Chaoren Lind3173f32015-05-29 19:52:29 +0000855 error = CreateSymlink(dst, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000856 }
857 break;
858 case FileSpec::eFileTypePipe:
859 error.SetErrorString("platform install doesn't handle pipes");
860 break;
861 case FileSpec::eFileTypeSocket:
862 error.SetErrorString("platform install doesn't handle sockets");
863 break;
864 case FileSpec::eFileTypeInvalid:
865 case FileSpec::eFileTypeUnknown:
866 case FileSpec::eFileTypeOther:
867 error.SetErrorString("platform install doesn't handle non file or directory items");
868 break;
869 }
870 }
871 return error;
872}
873
874bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000875Platform::SetWorkingDirectory(const FileSpec &file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000876{
877 if (IsHost())
878 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000879 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
880 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000881 log->Printf("Platform::SetWorkingDirectory('%s')",
882 file_spec.GetCString());
883 if (file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000884 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000885 if (::chdir(file_spec.GetCString()) == 0)
Greg Claytonfbb76342013-11-20 21:07:01 +0000886 return true;
887 }
888 return false;
889 }
890 else
891 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000892 m_working_dir.Clear();
Chaoren Lind3173f32015-05-29 19:52:29 +0000893 return SetRemoteWorkingDirectory(file_spec);
Greg Claytonfbb76342013-11-20 21:07:01 +0000894 }
895}
896
897Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000898Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000899{
900 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000901 return FileSystem::MakeDirectory(file_spec, 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
Chaoren Lind3173f32015-05-29 19:52:29 +0000911Platform::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000912{
913 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000914 return FileSystem::GetFilePermissions(file_spec, 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
Chaoren Lind3173f32015-05-29 19:52:29 +0000924Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000925{
926 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000927 return FileSystem::SetFilePermissions(file_spec, 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
Enrico Granata6e25aee2015-08-27 00:53:57 +0000953ConstString
954Platform::GetFullNameForDylib (ConstString basename)
955{
956 return basename;
957}
958
Greg Clayton5fb8f792013-12-02 19:35:49 +0000959bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000960Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir)
Greg Clayton5fb8f792013-12-02 19:35:49 +0000961{
962 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
963 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000964 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
965 working_dir.GetCString());
966 m_working_dir = working_dir;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000967 return true;
968}
969
Greg Clayton32e0a752011-03-30 18:16:51 +0000970const char *
971Platform::GetUserName (uint32_t uid)
972{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000973#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000974 const char *user_name = GetCachedUserName(uid);
975 if (user_name)
976 return user_name;
977 if (IsHost())
978 {
979 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000980 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000981 return SetCachedUserName (uid, name.c_str(), name.size());
982 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000983#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000984 return NULL;
985}
986
Greg Clayton32e0a752011-03-30 18:16:51 +0000987const char *
988Platform::GetGroupName (uint32_t gid)
989{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000990#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000991 const char *group_name = GetCachedGroupName(gid);
992 if (group_name)
993 return group_name;
994 if (IsHost())
995 {
996 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000997 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000998 return SetCachedGroupName (gid, name.c_str(), name.size());
999 }
Zachary Turnerb245eca2014-08-21 20:02:17 +00001000#endif
Greg Clayton32e0a752011-03-30 18:16:51 +00001001 return NULL;
1002}
Greg Clayton1cb64962011-03-24 04:28:38 +00001003
Greg Claytonded470d2011-03-19 01:12:21 +00001004bool
1005Platform::SetOSVersion (uint32_t major,
1006 uint32_t minor,
1007 uint32_t update)
1008{
1009 if (IsHost())
1010 {
Zachary Turner97a14e62014-08-19 17:18:29 +00001011 // We don't need anyone setting the OS version for the host platform,
1012 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +00001013 return false;
1014 }
1015 else
1016 {
1017 // We have a remote platform, allow setting the target OS version if
1018 // we aren't connected, since if we are connected, we should be able to
1019 // request the remote OS version from the connected platform.
1020 if (IsConnected())
1021 return false;
1022 else
1023 {
1024 // We aren't connected and we might want to set the OS version
1025 // ahead of time before we connect so we can peruse files and
1026 // use a local SDK or PDK cache of support files to disassemble
1027 // or do other things.
1028 m_major_os_version = major;
1029 m_minor_os_version = minor;
1030 m_update_os_version = update;
1031 return true;
1032 }
1033 }
1034 return false;
1035}
1036
1037
Greg Claytone996fd32011-03-08 22:40:15 +00001038Error
Greg Clayton8012cad2014-11-17 19:39:20 +00001039Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001040 lldb::ModuleSP &exe_module_sp,
1041 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +00001042{
1043 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +00001044 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +00001045 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001046 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +00001047 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001048 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001049 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001050 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001051 NULL,
1052 NULL);
1053 }
1054 else
1055 {
1056 // No valid architecture was specified, ask the platform for
1057 // the architectures that we should be using (in the correct order)
1058 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +00001059 ModuleSpec arch_module_spec(module_spec);
1060 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +00001061 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001062 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001063 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001064 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001065 NULL,
1066 NULL);
1067 // Did we find an executable using one of the
1068 if (error.Success() && exe_module_sp)
1069 break;
1070 }
1071 }
1072 }
1073 else
1074 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001075 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +00001076 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +00001077 }
1078 return error;
1079}
1080
Greg Clayton103f0282012-09-12 02:03:59 +00001081Error
1082Platform::ResolveSymbolFile (Target &target,
1083 const ModuleSpec &sym_spec,
1084 FileSpec &sym_file)
1085{
1086 Error error;
1087 if (sym_spec.GetSymbolFileSpec().Exists())
1088 sym_file = sym_spec.GetSymbolFileSpec();
1089 else
1090 error.SetErrorString("unable to resolve symbol file");
1091 return error;
1092
1093}
1094
1095
1096
Greg Claytonaa516842011-08-11 16:25:18 +00001097bool
1098Platform::ResolveRemotePath (const FileSpec &platform_path,
1099 FileSpec &resolved_platform_path)
1100{
1101 resolved_platform_path = platform_path;
1102 return resolved_platform_path.ResolvePath();
1103}
1104
Greg Claytonded470d2011-03-19 01:12:21 +00001105
1106const ArchSpec &
1107Platform::GetSystemArchitecture()
1108{
1109 if (IsHost())
1110 {
1111 if (!m_system_arch.IsValid())
1112 {
1113 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +00001114 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +00001115 m_system_arch_set_while_connected = m_system_arch.IsValid();
1116 }
1117 }
1118 else
1119 {
1120 // We have a remote platform. We can only fetch the remote
1121 // system architecture if we are connected, and we don't want to do it
1122 // more than once.
1123
1124 const bool is_connected = IsConnected();
1125
1126 bool fetch = false;
1127 if (m_system_arch.IsValid())
1128 {
1129 // We have valid OS version info, check to make sure it wasn't
1130 // manually set prior to connecting. If it was manually set prior
1131 // to connecting, then lets fetch the actual OS version info
1132 // if we are now connected.
1133 if (is_connected && !m_system_arch_set_while_connected)
1134 fetch = true;
1135 }
1136 else
1137 {
1138 // We don't have valid OS version info, fetch it if we are connected
1139 fetch = is_connected;
1140 }
1141
1142 if (fetch)
1143 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001144 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001145 m_system_arch_set_while_connected = m_system_arch.IsValid();
1146 }
1147 }
1148 return m_system_arch;
1149}
1150
1151
Greg Claytone996fd32011-03-08 22:40:15 +00001152Error
Greg Claytond314e812011-03-23 00:09:55 +00001153Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001154{
1155 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001156 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001157 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001158 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001159 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001160 return error;
1161}
1162
1163Error
Greg Claytond314e812011-03-23 00:09:55 +00001164Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001165{
1166 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001167 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001168 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001169 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001170 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001171 return error;
1172}
Greg Clayton32e0a752011-03-30 18:16:51 +00001173
1174bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001175Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001176{
1177 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001178 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001179 if (IsHost())
1180 return Host::GetProcessInfo (pid, process_info);
1181 return false;
1182}
1183
1184uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001185Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1186 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001187{
Greg Clayton8b82f082011-04-12 05:54:46 +00001188 // Take care of the host case so that each subclass can just
1189 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001190 uint32_t match_count = 0;
1191 if (IsHost())
1192 match_count = Host::FindProcesses (match_info, process_infos);
1193 return match_count;
1194}
Greg Clayton8b82f082011-04-12 05:54:46 +00001195
1196
1197Error
1198Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1199{
1200 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001201 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1202 if (log)
1203 log->Printf ("Platform::%s()", __FUNCTION__);
1204
1205 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001206 // call this function to get the host functionality.
1207 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001208 {
1209 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1210 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1211
1212 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1213 {
1214 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001215 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1216 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001217 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001218 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001219 {
1220 const FileSpec &shell = launch_info.GetShell();
1221 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001222 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1223 __FUNCTION__,
1224 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001225 shell_str);
1226 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001227
Greg Claytond1cf11a2012-04-14 01:42:46 +00001228 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1229 is_localhost,
1230 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001231 first_arg_is_full_shell_command,
1232 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001233 return error;
1234 }
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001235 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments))
Enrico Granatad7a83a92015-02-10 03:06:24 +00001236 {
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001237 error = ShellExpandArguments(launch_info);
Enrico Granata83a14372015-02-20 21:48:38 +00001238 if (error.Fail())
Enrico Granatad7a83a92015-02-10 03:06:24 +00001239 return error;
Enrico Granatad7a83a92015-02-10 03:06:24 +00001240 }
Greg Clayton84db9102012-03-26 23:03:23 +00001241
Todd Fialaac33cc92014-10-09 01:02:08 +00001242 if (log)
1243 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1244
Greg Clayton8b82f082011-04-12 05:54:46 +00001245 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001246 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001247 else
1248 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1249 return error;
1250}
1251
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001252Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001253Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +00001254{
1255 if (IsHost())
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001256 return Host::ShellExpandArguments(launch_info);
1257 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001258}
1259
1260Error
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001261Platform::KillProcess (const lldb::pid_t pid)
1262{
1263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1264 if (log)
1265 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
1266
Zachary Turner7271bab2015-05-13 19:44:24 +00001267 // Try to find a process plugin to handle this Kill request. If we can't, fall back to
1268 // the default OS implementation.
1269 size_t num_debuggers = Debugger::GetNumDebuggers();
1270 for (size_t didx = 0; didx < num_debuggers; ++didx)
1271 {
1272 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1273 lldb_private::TargetList &targets = debugger->GetTargetList();
1274 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx)
1275 {
1276 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1277 if (process->GetID() == pid)
1278 return process->Destroy(true);
1279 }
1280 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001281
Zachary Turner7271bab2015-05-13 19:44:24 +00001282 if (!IsHost())
1283 {
1284 return Error("base lldb_private::Platform class can't kill remote processes unless "
1285 "they are controlled by a process plugin");
1286 }
1287 Host::Kill(pid, SIGTERM);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001288 return Error();
1289}
1290
Greg Clayton8b82f082011-04-12 05:54:46 +00001291lldb::ProcessSP
1292Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1293 Debugger &debugger,
1294 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001295 Error &error)
1296{
Todd Fialaac33cc92014-10-09 01:02:08 +00001297 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1298 if (log)
1299 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1300
Greg Clayton8b82f082011-04-12 05:54:46 +00001301 ProcessSP process_sp;
1302 // Make sure we stop at the entry point
1303 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001304 // We always launch the process we are going to debug in a separate process
1305 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1306 // about the target getting them as well.
1307 launch_info.SetLaunchInSeparateProcessGroup(true);
1308
Greg Clayton8b82f082011-04-12 05:54:46 +00001309 error = LaunchProcess (launch_info);
1310 if (error.Success())
1311 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001312 if (log)
1313 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001314 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001315 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001316 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001317 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001318 if (process_sp)
1319 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001320 if (log)
1321 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001322 launch_info.SetHijackListener(attach_info.GetHijackListener());
1323
Greg Claytone24c4ac2011-11-17 04:46:02 +00001324 // Since we attached to the process, it will think it needs to detach
1325 // if the process object just goes away without an explicit call to
1326 // Process::Kill() or Process::Detach(), so let it know to kill the
1327 // process if this happens.
1328 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001329
1330 // If we didn't have any file actions, the pseudo terminal might
1331 // have been used where the slave side was given as the file to
1332 // open for stdin/out/err after we have already opened the master
1333 // so we can read/write stdin/out/err.
1334 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1335 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1336 {
1337 process_sp->SetSTDIOFileDescriptor(pty_fd);
1338 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001339 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001340 else
1341 {
1342 if (log)
1343 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1344 }
1345 }
1346 else
1347 {
1348 if (log)
1349 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001350 }
1351 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001352 else
1353 {
1354 if (log)
1355 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1356 }
1357
Greg Clayton8b82f082011-04-12 05:54:46 +00001358 return process_sp;
1359}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001360
1361
1362lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001363Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001364{
1365 lldb::PlatformSP platform_sp;
1366 Error error;
1367 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001368 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001369 return platform_sp;
1370}
1371
1372
1373//------------------------------------------------------------------
1374/// Lets a platform answer if it is compatible with a given
1375/// architecture and the target triple contained within.
1376//------------------------------------------------------------------
1377bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001378Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001379{
1380 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001381 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001382 {
Greg Clayton70512312012-05-08 01:45:38 +00001383 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001384 // Try for an exact architecture match first.
1385 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001386 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001387 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001388 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001389 if (arch.IsExactMatch(platform_arch))
1390 {
1391 if (compatible_arch_ptr)
1392 *compatible_arch_ptr = platform_arch;
1393 return true;
1394 }
1395 }
1396 }
1397 else
1398 {
1399 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1400 {
1401 if (arch.IsCompatibleMatch(platform_arch))
1402 {
1403 if (compatible_arch_ptr)
1404 *compatible_arch_ptr = platform_arch;
1405 return true;
1406 }
Greg Clayton70512312012-05-08 01:45:38 +00001407 }
1408 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001409 }
Greg Clayton70512312012-05-08 01:45:38 +00001410 if (compatible_arch_ptr)
1411 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001412 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001413}
1414
Daniel Maleae0f8f572013-08-26 23:57:52 +00001415Error
1416Platform::PutFile (const FileSpec& source,
1417 const FileSpec& destination,
1418 uint32_t uid,
1419 uint32_t gid)
1420{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001421 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1422 if (log)
1423 log->Printf("[PutFile] Using block by block transfer....\n");
1424
Pavel Labath646b0642015-02-17 16:07:52 +00001425 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001426 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1427 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1428
1429 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1430 Error error;
1431 uint32_t permissions = source_file.GetPermissions(error);
1432 if (permissions == 0)
1433 permissions = lldb::eFilePermissionsFileDefault;
1434
1435 if (!source_file.IsValid())
1436 return Error("PutFile: unable to open source file");
1437 lldb::user_id_t dest_file = OpenFile (destination,
1438 File::eOpenOptionCanCreate |
1439 File::eOpenOptionWrite |
Pavel Labath646b0642015-02-17 16:07:52 +00001440 File::eOpenOptionTruncate |
1441 File::eOpenOptionCloseOnExec,
Vince Harron1b5a74e2015-01-21 22:42:49 +00001442 permissions,
1443 error);
1444 if (log)
1445 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1446
1447 if (error.Fail())
1448 return error;
1449 if (dest_file == UINT64_MAX)
1450 return Error("unable to open target file");
1451 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1452 uint64_t offset = 0;
1453 for (;;)
1454 {
1455 size_t bytes_read = buffer_sp->GetByteSize();
1456 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1457 if (error.Fail() || bytes_read == 0)
1458 break;
1459
1460 const uint64_t bytes_written = WriteFile(dest_file, offset,
1461 buffer_sp->GetBytes(), bytes_read, error);
1462 if (error.Fail())
1463 break;
1464
1465 offset += bytes_written;
1466 if (bytes_written != bytes_read)
1467 {
1468 // We didn't write the correct number of bytes, so adjust
1469 // the file position in the source file we are reading from...
1470 source_file.SeekFromStart(offset);
1471 }
1472 }
1473 CloseFile(dest_file, error);
1474
1475 if (uid == UINT32_MAX && gid == UINT32_MAX)
1476 return error;
1477
1478 // TODO: ChownFile?
1479
Daniel Maleae0f8f572013-08-26 23:57:52 +00001480 return error;
1481}
1482
1483Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001484Platform::GetFile(const FileSpec &source,
1485 const FileSpec &destination)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001486{
1487 Error error("unimplemented");
1488 return error;
1489}
1490
Greg Claytonfbb76342013-11-20 21:07:01 +00001491Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001492Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1493 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001494{
1495 Error error("unimplemented");
1496 return error;
1497}
1498
Daniel Maleae0f8f572013-08-26 23:57:52 +00001499bool
Chaoren Lind3173f32015-05-29 19:52:29 +00001500Platform::GetFileExists(const lldb_private::FileSpec &file_spec)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001501{
1502 return false;
1503}
1504
Greg Claytonfbb76342013-11-20 21:07:01 +00001505Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001506Platform::Unlink(const FileSpec &path)
Greg Claytonfbb76342013-11-20 21:07:01 +00001507{
1508 Error error("unimplemented");
1509 return error;
1510}
1511
Robert Flack96ad3de2015-05-09 15:53:31 +00001512uint64_t
Mohit K. Bhakkade0d8c422015-06-30 06:29:12 +00001513Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags)
Robert Flack96ad3de2015-05-09 15:53:31 +00001514{
1515 uint64_t flags_platform = 0;
1516 if (flags & eMmapFlagsPrivate)
1517 flags_platform |= MAP_PRIVATE;
1518 if (flags & eMmapFlagsAnon)
1519 flags_platform |= MAP_ANON;
1520 return flags_platform;
1521}
Greg Claytonfbb76342013-11-20 21:07:01 +00001522
Daniel Maleae0f8f572013-08-26 23:57:52 +00001523lldb_private::Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001524Platform::RunShellCommand(const char *command, // Shouldn't be NULL
1525 const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory
1526 int *status_ptr, // Pass NULL if you don't want the process exit status
1527 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1528 std::string *command_output, // Pass NULL if you don't want the command output
1529 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001530{
1531 if (IsHost())
1532 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1533 else
1534 return Error("unimplemented");
1535}
1536
1537
1538bool
1539Platform::CalculateMD5 (const FileSpec& file_spec,
1540 uint64_t &low,
1541 uint64_t &high)
1542{
1543 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001544 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001545 else
1546 return false;
1547}
1548
1549void
1550Platform::SetLocalCacheDirectory (const char* local)
1551{
1552 m_local_cache_directory.assign(local);
1553}
1554
1555const char*
1556Platform::GetLocalCacheDirectory ()
1557{
1558 return m_local_cache_directory.c_str();
1559}
1560
1561static OptionDefinition
1562g_rsync_option_table[] =
1563{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001564 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1565 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1566 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1567 { 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 +00001568};
1569
1570static OptionDefinition
1571g_ssh_option_table[] =
1572{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001573 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1574 { 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 +00001575};
1576
1577static OptionDefinition
1578g_caching_option_table[] =
1579{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001580 { 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 +00001581};
1582
1583OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1584{
1585}
1586
1587OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1588{
1589}
1590
1591const lldb_private::OptionDefinition*
1592OptionGroupPlatformRSync::GetDefinitions ()
1593{
1594 return g_rsync_option_table;
1595}
1596
1597void
1598OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1599{
1600 m_rsync = false;
1601 m_rsync_opts.clear();
1602 m_rsync_prefix.clear();
1603 m_ignores_remote_hostname = false;
1604}
1605
1606lldb_private::Error
1607OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1608 uint32_t option_idx,
1609 const char *option_arg)
1610{
1611 Error error;
1612 char short_option = (char) GetDefinitions()[option_idx].short_option;
1613 switch (short_option)
1614 {
1615 case 'r':
1616 m_rsync = true;
1617 break;
1618
1619 case 'R':
1620 m_rsync_opts.assign(option_arg);
1621 break;
1622
1623 case 'P':
1624 m_rsync_prefix.assign(option_arg);
1625 break;
1626
1627 case 'i':
1628 m_ignores_remote_hostname = true;
1629 break;
1630
1631 default:
1632 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1633 break;
1634 }
1635
1636 return error;
1637}
1638
1639uint32_t
1640OptionGroupPlatformRSync::GetNumDefinitions ()
1641{
1642 return llvm::array_lengthof(g_rsync_option_table);
1643}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001644
Greg Clayton4116e932012-05-15 02:33:01 +00001645lldb::BreakpointSP
1646Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1647{
1648 return lldb::BreakpointSP();
1649}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001650
Daniel Maleae0f8f572013-08-26 23:57:52 +00001651OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1652{
1653}
1654
1655OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1656{
1657}
1658
1659const lldb_private::OptionDefinition*
1660OptionGroupPlatformSSH::GetDefinitions ()
1661{
1662 return g_ssh_option_table;
1663}
1664
1665void
1666OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1667{
1668 m_ssh = false;
1669 m_ssh_opts.clear();
1670}
1671
1672lldb_private::Error
1673OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1674 uint32_t option_idx,
1675 const char *option_arg)
1676{
1677 Error error;
1678 char short_option = (char) GetDefinitions()[option_idx].short_option;
1679 switch (short_option)
1680 {
1681 case 's':
1682 m_ssh = true;
1683 break;
1684
1685 case 'S':
1686 m_ssh_opts.assign(option_arg);
1687 break;
1688
1689 default:
1690 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1691 break;
1692 }
1693
1694 return error;
1695}
1696
1697uint32_t
1698OptionGroupPlatformSSH::GetNumDefinitions ()
1699{
1700 return llvm::array_lengthof(g_ssh_option_table);
1701}
1702
1703OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1704{
1705}
1706
1707OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1708{
1709}
1710
1711const lldb_private::OptionDefinition*
1712OptionGroupPlatformCaching::GetDefinitions ()
1713{
1714 return g_caching_option_table;
1715}
1716
1717void
1718OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1719{
1720 m_cache_dir.clear();
1721}
1722
1723lldb_private::Error
1724OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1725 uint32_t option_idx,
1726 const char *option_arg)
1727{
1728 Error error;
1729 char short_option = (char) GetDefinitions()[option_idx].short_option;
1730 switch (short_option)
1731 {
1732 case 'c':
1733 m_cache_dir.assign(option_arg);
1734 break;
1735
1736 default:
1737 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1738 break;
1739 }
1740
1741 return error;
1742}
1743
1744uint32_t
1745OptionGroupPlatformCaching::GetNumDefinitions ()
1746{
1747 return llvm::array_lengthof(g_caching_option_table);
1748}
1749
Greg Clayton67cc0632012-08-22 17:17:09 +00001750size_t
1751Platform::GetEnvironment (StringList &environment)
1752{
1753 environment.Clear();
1754 return false;
1755}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001756
1757const std::vector<ConstString> &
1758Platform::GetTrapHandlerSymbolNames ()
1759{
1760 if (!m_calculated_trap_handlers)
1761 {
Greg Clayton7597b352015-02-02 20:45:17 +00001762 Mutex::Locker locker (m_mutex);
Jason Molenda4da87062014-05-23 23:11:27 +00001763 if (!m_calculated_trap_handlers)
1764 {
1765 CalculateTrapHandlerSymbolNames();
1766 m_calculated_trap_handlers = true;
1767 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001768 }
1769 return m_trap_handlers;
1770}
1771
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001772Error
1773Platform::GetCachedExecutable (ModuleSpec &module_spec,
1774 lldb::ModuleSP &module_sp,
1775 const FileSpecList *module_search_paths_ptr,
1776 Platform &remote_platform)
1777{
1778 const auto platform_spec = module_spec.GetFileSpec ();
1779 const auto error = LoadCachedExecutable (module_spec,
1780 module_sp,
1781 module_search_paths_ptr,
1782 remote_platform);
1783 if (error.Success ())
1784 {
1785 module_spec.GetFileSpec () = module_sp->GetFileSpec ();
1786 module_spec.GetPlatformFileSpec () = platform_spec;
1787 }
1788
1789 return error;
1790}
1791
1792Error
1793Platform::LoadCachedExecutable (const ModuleSpec &module_spec,
1794 lldb::ModuleSP &module_sp,
1795 const FileSpecList *module_search_paths_ptr,
1796 Platform &remote_platform)
1797{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001798 return GetRemoteSharedModule (module_spec,
1799 nullptr,
1800 module_sp,
1801 [&](const ModuleSpec &spec)
1802 {
1803 return remote_platform.ResolveExecutable (
1804 spec, module_sp, module_search_paths_ptr);
1805 },
1806 nullptr);
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001807}
1808
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001809Error
1810Platform::GetRemoteSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001811 Process* process,
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001812 lldb::ModuleSP &module_sp,
1813 const ModuleResolver &module_resolver,
1814 bool *did_create_ptr)
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001815{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001816 // Get module information from a target.
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001817 ModuleSpec resolved_module_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001818 bool got_module_spec = false;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001819 if (process)
1820 {
1821 // Try to get module information from the process
1822 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001823 got_module_spec = true;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001824 }
1825
1826 if (!got_module_spec)
1827 {
1828 // Get module information from a target.
1829 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001830 return module_resolver (module_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001831 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001832
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001833 // Trying to find a module by UUID on local file system.
1834 const auto error = module_resolver (resolved_module_spec);
1835 if (error.Fail ())
1836 {
1837 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr))
1838 return Error ();
1839 }
1840
1841 return error;
1842}
1843
1844bool
1845Platform::GetCachedSharedModule (const ModuleSpec &module_spec,
1846 lldb::ModuleSP &module_sp,
1847 bool *did_create_ptr)
1848{
1849 if (IsHost() ||
1850 !GetGlobalPlatformProperties ()->GetUseModuleCache ())
1851 return false;
1852
1853 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
1854
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001855 // Check local cache for a module.
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001856 auto error = m_module_cache->GetAndPut (
1857 GetModuleCacheRoot (),
1858 GetCacheHostname (),
1859 module_spec,
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001860 [this](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec)
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001861 {
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001862 return DownloadModuleSlice (module_spec.GetFileSpec (),
1863 module_spec.GetObjectOffset (),
1864 module_spec.GetObjectSize (),
1865 tmp_download_file_spec);
1866
1867 },
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001868 [this](const ModuleSP& module_sp, const FileSpec& tmp_download_file_spec)
1869 {
1870 return DownloadSymbolFile (module_sp, tmp_download_file_spec);
1871 },
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001872 module_sp,
1873 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001874 if (error.Success ())
1875 return true;
1876
1877 if (log)
1878 log->Printf("Platform::%s - module %s not found in local cache: %s",
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001879 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001880 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001881}
1882
1883Error
1884Platform::DownloadModuleSlice (const FileSpec& src_file_spec,
1885 const uint64_t src_offset,
1886 const uint64_t src_size,
1887 const FileSpec& dst_file_spec)
1888{
1889 Error error;
1890
1891 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1892 if (!dst.is_open())
1893 {
1894 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ());
1895 return error;
1896 }
1897
1898 auto src_fd = OpenFile (src_file_spec,
1899 File::eOpenOptionRead,
1900 lldb::eFilePermissionsFileDefault,
1901 error);
1902
1903 if (error.Fail ())
1904 {
1905 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ());
1906 return error;
1907 }
1908
1909 std::vector<char> buffer (1024);
1910 auto offset = src_offset;
1911 uint64_t total_bytes_read = 0;
1912 while (total_bytes_read < src_size)
1913 {
1914 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read);
1915 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error);
1916 if (error.Fail ())
1917 break;
1918 if (n_read == 0)
1919 {
1920 error.SetErrorString ("read 0 bytes");
1921 break;
1922 }
1923 offset += n_read;
1924 total_bytes_read += n_read;
1925 dst.write (&buffer[0], n_read);
1926 }
1927
1928 Error close_error;
1929 CloseFile (src_fd, close_error); // Ignoring close error.
1930
1931 return error;
1932}
1933
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001934Error
1935Platform::DownloadSymbolFile (const lldb::ModuleSP& module_sp, const FileSpec& dst_file_spec)
1936{
1937 return Error ("Symbol file downloading not supported by the default platform.");
1938}
1939
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001940FileSpec
1941Platform::GetModuleCacheRoot ()
1942{
1943 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory ();
1944 dir_spec.AppendPathComponent (GetName ().AsCString ());
1945 return dir_spec;
1946}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001947
1948const char *
1949Platform::GetCacheHostname ()
1950{
1951 return GetHostname ();
1952}
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001953
1954const UnixSignalsSP &
1955Platform::GetRemoteUnixSignals()
1956{
1957 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1958 return s_default_unix_signals_sp;
1959}
1960
1961const UnixSignalsSP &
1962Platform::GetUnixSignals()
1963{
1964 if (IsHost())
1965 return Host::GetUnixSignals();
1966 return GetRemoteUnixSignals();
1967}