blob: 2b586933ccd4193b5a529de2d4531344bba6b52f [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 {
275 return ModuleList::GetSharedModule (
276 spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr, false);
277 },
278 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000279}
280
281bool
282Platform::GetModuleSpec (const FileSpec& module_file_spec,
283 const ArchSpec& arch,
284 ModuleSpec &module_spec)
285{
286 ModuleSpecList module_specs;
287 if (ObjectFile::GetModuleSpecifications (module_file_spec, 0, 0, module_specs) == 0)
288 return false;
289
290 ModuleSpec matched_module_spec;
291 return module_specs.FindMatchingModuleSpec (ModuleSpec (module_file_spec, arch),
292 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000293}
294
Greg Claytone996fd32011-03-08 22:40:15 +0000295PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000296Platform::Find (const ConstString &name)
297{
298 if (name)
299 {
300 static ConstString g_host_platform_name ("host");
301 if (name == g_host_platform_name)
302 return GetHostPlatform();
303
304 Mutex::Locker locker(GetPlatformListMutex ());
305 for (const auto &platform_sp : GetPlatformList())
306 {
307 if (platform_sp->GetName() == name)
308 return platform_sp;
309 }
310 }
311 return PlatformSP();
312}
313
314PlatformSP
315Platform::Create (const ConstString &name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000316{
317 PlatformCreateInstance create_callback = NULL;
318 lldb::PlatformSP platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000319 if (name)
Greg Claytone996fd32011-03-08 22:40:15 +0000320 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000321 static ConstString g_host_platform_name ("host");
322 if (name == g_host_platform_name)
323 return GetHostPlatform();
324
325 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name);
Greg Claytone996fd32011-03-08 22:40:15 +0000326 if (create_callback)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000327 platform_sp = create_callback(true, NULL);
Greg Claytone996fd32011-03-08 22:40:15 +0000328 else
Greg Clayton615eb7e2014-09-19 20:11:50 +0000329 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000330 }
331 else
Greg Claytonded470d2011-03-19 01:12:21 +0000332 error.SetErrorString ("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000333
334 if (platform_sp)
335 {
336 Mutex::Locker locker(GetPlatformListMutex ());
337 GetPlatformList().push_back(platform_sp);
338 }
339
Greg Claytone996fd32011-03-08 22:40:15 +0000340 return platform_sp;
341}
342
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000343
344PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000345Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000346{
347 lldb::PlatformSP platform_sp;
348 if (arch.IsValid())
349 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000350 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000351 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000352 // First try exact arch matches across all platforms already created
353 Mutex::Locker locker(GetPlatformListMutex ());
354 for (const auto &platform_sp : GetPlatformList())
Greg Clayton1e0c8842013-01-11 20:49:54 +0000355 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000356 if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
357 return platform_sp;
358 }
359
360 // Next try compatible arch matches across all platforms already created
361 for (const auto &platform_sp : GetPlatformList())
362 {
363 if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
Greg Clayton1e0c8842013-01-11 20:49:54 +0000364 return platform_sp;
365 }
366 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000367
368 PlatformCreateInstance create_callback;
369 // First try exact arch matches across all platform plug-ins
370 uint32_t idx;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000371 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
372 {
373 if (create_callback)
374 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000375 platform_sp = create_callback(false, &arch);
376 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
377 {
378 Mutex::Locker locker(GetPlatformListMutex ());
379 GetPlatformList().push_back(platform_sp);
Greg Clayton1e0c8842013-01-11 20:49:54 +0000380 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000381 }
382 }
383 }
384 // Next try compatible arch matches across all platform plug-ins
385 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
386 {
387 if (create_callback)
388 {
389 platform_sp = create_callback(false, &arch);
390 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
391 {
392 Mutex::Locker locker(GetPlatformListMutex ());
393 GetPlatformList().push_back(platform_sp);
394 return platform_sp;
395 }
Greg Clayton1e0c8842013-01-11 20:49:54 +0000396 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000397 }
398 }
399 else
400 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000401 if (platform_arch_ptr)
402 platform_arch_ptr->Clear();
403 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000404 return platform_sp;
405}
406
Greg Claytone996fd32011-03-08 22:40:15 +0000407//------------------------------------------------------------------
408/// Default Constructor
409//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000410Platform::Platform (bool is_host) :
411 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000412 m_os_version_set_while_connected (false),
413 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000414 m_sdk_sysroot (),
415 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000416 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000417 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000418 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000419 m_major_os_version (UINT32_MAX),
420 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000421 m_update_os_version (UINT32_MAX),
422 m_system_arch(),
Greg Clayton7597b352015-02-02 20:45:17 +0000423 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +0000424 m_uid_map(),
425 m_gid_map(),
426 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000427 m_max_gid_name_len (0),
428 m_supports_rsync (false),
429 m_rsync_opts (),
430 m_rsync_prefix (),
431 m_supports_ssh (false),
432 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000433 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000434 m_trap_handlers(),
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000435 m_calculated_trap_handlers (false),
436 m_module_cache (llvm::make_unique<ModuleCache> ())
Greg Claytone996fd32011-03-08 22:40:15 +0000437{
Greg Clayton5160ce52013-03-27 23:08:40 +0000438 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000439 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000440 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000441}
442
443//------------------------------------------------------------------
444/// Destructor.
445///
446/// The destructor is virtual since this class is designed to be
447/// inherited from by the plug-in instance.
448//------------------------------------------------------------------
449Platform::~Platform()
450{
Greg Clayton5160ce52013-03-27 23:08:40 +0000451 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000452 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000453 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000454}
455
Greg Clayton1cb64962011-03-24 04:28:38 +0000456void
457Platform::GetStatus (Stream &strm)
458{
459 uint32_t major = UINT32_MAX;
460 uint32_t minor = UINT32_MAX;
461 uint32_t update = UINT32_MAX;
462 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000463 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000464
465 ArchSpec arch (GetSystemArchitecture());
466 if (arch.IsValid())
467 {
468 if (!arch.GetTriple().str().empty())
Greg Clayton32e0a752011-03-30 18:16:51 +0000469 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000470 }
471
472 if (GetOSVersion(major, minor, update))
473 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000474 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000475 if (minor != UINT32_MAX)
476 strm.Printf(".%u", minor);
477 if (update != UINT32_MAX)
478 strm.Printf(".%u", update);
479
480 if (GetOSBuildString (s))
481 strm.Printf(" (%s)", s.c_str());
482
483 strm.EOL();
484 }
485
486 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000487 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000488
489 if (IsHost())
490 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000491 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000492 }
493 else
494 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000495 const bool is_connected = IsConnected();
496 if (is_connected)
497 strm.Printf(" Hostname: %s\n", GetHostname());
498 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000499 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000500
Greg Claytonfbb76342013-11-20 21:07:01 +0000501 if (GetWorkingDirectory())
502 {
503 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
504 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000505 if (!IsConnected())
506 return;
507
508 std::string specific_info(GetPlatformSpecificConnectionInformation());
509
510 if (specific_info.empty() == false)
511 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000512}
513
Greg Claytonded470d2011-03-19 01:12:21 +0000514
515bool
516Platform::GetOSVersion (uint32_t &major,
517 uint32_t &minor,
518 uint32_t &update)
519{
Greg Clayton7597b352015-02-02 20:45:17 +0000520 Mutex::Locker locker (m_mutex);
521
Greg Claytonded470d2011-03-19 01:12:21 +0000522 bool success = m_major_os_version != UINT32_MAX;
523 if (IsHost())
524 {
525 if (!success)
526 {
527 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000528 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000529 m_os_version_set_while_connected = success;
530 }
531 }
532 else
533 {
534 // We have a remote platform. We can only fetch the remote
535 // OS version if we are connected, and we don't want to do it
536 // more than once.
537
538 const bool is_connected = IsConnected();
539
Greg Clayton1cb64962011-03-24 04:28:38 +0000540 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000541 if (success)
542 {
543 // We have valid OS version info, check to make sure it wasn't
544 // manually set prior to connecting. If it was manually set prior
545 // to connecting, then lets fetch the actual OS version info
546 // if we are now connected.
547 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000548 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000549 }
550 else
551 {
552 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000553 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000554 }
555
Greg Clayton1cb64962011-03-24 04:28:38 +0000556 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000557 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000558 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000559 m_os_version_set_while_connected = success;
560 }
561 }
562
563 if (success)
564 {
565 major = m_major_os_version;
566 minor = m_minor_os_version;
567 update = m_update_os_version;
568 }
569 return success;
570}
Greg Clayton1cb64962011-03-24 04:28:38 +0000571
572bool
573Platform::GetOSBuildString (std::string &s)
574{
Zachary Turner97a14e62014-08-19 17:18:29 +0000575 s.clear();
576
Greg Clayton1cb64962011-03-24 04:28:38 +0000577 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000578#if !defined(__linux__)
579 return HostInfo::GetOSBuildString(s);
580#else
581 return false;
582#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000583 else
584 return GetRemoteOSBuildString (s);
585}
586
587bool
588Platform::GetOSKernelDescription (std::string &s)
589{
590 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000591#if !defined(__linux__)
592 return HostInfo::GetOSKernelDescription(s);
593#else
594 return false;
595#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000596 else
597 return GetRemoteOSKernelDescription (s);
598}
599
Sean Callanan5dc29812014-12-05 01:16:31 +0000600void
Greg Claytoncd6bbba2015-01-22 18:25:49 +0000601Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options)
Sean Callanan5dc29812014-12-05 01:16:31 +0000602{
603 std::vector<std::string> default_compilation_options =
604 {
605 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"
606 };
607
608 options.insert(options.end(),
609 default_compilation_options.begin(),
610 default_compilation_options.end());
611}
612
613
Chaoren Lind3173f32015-05-29 19:52:29 +0000614FileSpec
Greg Claytonfbb76342013-11-20 21:07:01 +0000615Platform::GetWorkingDirectory ()
616{
617 if (IsHost())
618 {
619 char cwd[PATH_MAX];
620 if (getcwd(cwd, sizeof(cwd)))
Chaoren Lind3173f32015-05-29 19:52:29 +0000621 return FileSpec{cwd, true};
Greg Claytonfbb76342013-11-20 21:07:01 +0000622 else
Chaoren Lind3173f32015-05-29 19:52:29 +0000623 return FileSpec{};
Greg Claytonfbb76342013-11-20 21:07:01 +0000624 }
625 else
626 {
627 if (!m_working_dir)
628 m_working_dir = GetRemoteWorkingDirectory();
629 return m_working_dir;
630 }
631}
632
633
634struct RecurseCopyBaton
635{
636 const FileSpec& dst;
637 Platform *platform_ptr;
638 Error error;
639};
640
641
642static FileSpec::EnumerateDirectoryResult
643RecurseCopy_Callback (void *baton,
644 FileSpec::FileType file_type,
645 const FileSpec &src)
646{
647 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
648 switch (file_type)
649 {
650 case FileSpec::eFileTypePipe:
651 case FileSpec::eFileTypeSocket:
652 // we have no way to copy pipes and sockets - ignore them and continue
653 return FileSpec::eEnumerateDirectoryResultNext;
654 break;
655
656 case FileSpec::eFileTypeDirectory:
657 {
658 // make the new directory and get in there
659 FileSpec dst_dir = rc_baton->dst;
660 if (!dst_dir.GetFilename())
661 dst_dir.GetFilename() = src.GetLastPathComponent();
Chaoren Lind3173f32015-05-29 19:52:29 +0000662 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault);
Greg Claytonfbb76342013-11-20 21:07:01 +0000663 if (error.Fail())
664 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000665 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end",
666 dst_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000667 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
668 }
669
670 // now recurse
671 std::string src_dir_path (src.GetPath());
672
673 // Make a filespec that only fills in the directory of a FileSpec so
674 // when we enumerate we can quickly fill in the filename for dst copies
675 FileSpec recurse_dst;
676 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
677 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
678 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
679 if (rc_baton2.error.Fail())
680 {
681 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
682 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
683 }
684 return FileSpec::eEnumerateDirectoryResultNext;
685 }
686 break;
687
688 case FileSpec::eFileTypeSymbolicLink:
689 {
690 // copy the file and keep going
691 FileSpec dst_file = rc_baton->dst;
692 if (!dst_file.GetFilename())
693 dst_file.GetFilename() = src.GetFilename();
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000694
Chaoren Lind3173f32015-05-29 19:52:29 +0000695 FileSpec src_resolved;
696
697 rc_baton->error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000698
699 if (rc_baton->error.Fail())
700 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Chaoren Lind3173f32015-05-29 19:52:29 +0000701
702 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000703
704 if (rc_baton->error.Fail())
705 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
706
707 return FileSpec::eEnumerateDirectoryResultNext;
708 }
709 break;
710 case FileSpec::eFileTypeRegular:
711 {
712 // copy the file and keep going
713 FileSpec dst_file = rc_baton->dst;
714 if (!dst_file.GetFilename())
715 dst_file.GetFilename() = src.GetFilename();
716 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
717 if (err.Fail())
718 {
719 rc_baton->error.SetErrorString(err.AsCString());
720 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
721 }
722 return FileSpec::eEnumerateDirectoryResultNext;
723 }
724 break;
725
726 case FileSpec::eFileTypeInvalid:
727 case FileSpec::eFileTypeOther:
728 case FileSpec::eFileTypeUnknown:
729 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
730 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
731 break;
732 }
David Majnemer8faf9372014-09-16 06:34:29 +0000733 llvm_unreachable("Unhandled FileSpec::FileType!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000734}
735
736Error
737Platform::Install (const FileSpec& src, const FileSpec& dst)
738{
739 Error error;
740
741 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
742 if (log)
743 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
744 FileSpec fixed_dst(dst);
745
746 if (!fixed_dst.GetFilename())
747 fixed_dst.GetFilename() = src.GetFilename();
748
Chaoren Lind3173f32015-05-29 19:52:29 +0000749 FileSpec working_dir = GetWorkingDirectory();
Greg Claytonfbb76342013-11-20 21:07:01 +0000750
751 if (dst)
752 {
753 if (dst.GetDirectory())
754 {
755 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
756 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
757 {
758 fixed_dst.GetDirectory() = dst.GetDirectory();
759 }
760 // If the fixed destination file doesn't have a directory yet,
761 // then we must have a relative path. We will resolve this relative
762 // path against the platform's working directory
763 if (!fixed_dst.GetDirectory())
764 {
765 FileSpec relative_spec;
766 std::string path;
767 if (working_dir)
768 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000769 relative_spec = working_dir;
770 relative_spec.AppendPathComponent(dst.GetPath());
Greg Claytonfbb76342013-11-20 21:07:01 +0000771 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
772 }
773 else
774 {
775 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
776 return error;
777 }
778 }
779 }
780 else
781 {
782 if (working_dir)
783 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000784 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000785 }
786 else
787 {
788 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
789 return error;
790 }
791 }
792 }
793 else
794 {
795 if (working_dir)
796 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000797 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000798 }
799 else
800 {
801 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
802 return error;
803 }
804 }
805
806 if (log)
807 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
808
809 if (GetSupportsRSync())
810 {
811 error = PutFile(src, dst);
812 }
813 else
814 {
815 switch (src.GetFileType())
816 {
817 case FileSpec::eFileTypeDirectory:
818 {
819 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000820 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000821 uint32_t permissions = src.GetPermissions();
822 if (permissions == 0)
823 permissions = eFilePermissionsDirectoryDefault;
Chaoren Lind3173f32015-05-29 19:52:29 +0000824 error = MakeDirectory(fixed_dst, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000825 if (error.Success())
826 {
827 // Make a filespec that only fills in the directory of a FileSpec so
828 // when we enumerate we can quickly fill in the filename for dst copies
829 FileSpec recurse_dst;
Chaoren Lind3173f32015-05-29 19:52:29 +0000830 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000831 std::string src_dir_path (src.GetPath());
832 RecurseCopyBaton baton = { recurse_dst, this, Error() };
833 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
834 return baton.error;
835 }
836 }
837 break;
838
839 case FileSpec::eFileTypeRegular:
840 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000841 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000842 error = PutFile(src, fixed_dst);
843 break;
844
845 case FileSpec::eFileTypeSymbolicLink:
846 {
847 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000848 Unlink(fixed_dst);
849 FileSpec src_resolved;
850 error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000851 if (error.Success())
Chaoren Lind3173f32015-05-29 19:52:29 +0000852 error = CreateSymlink(dst, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000853 }
854 break;
855 case FileSpec::eFileTypePipe:
856 error.SetErrorString("platform install doesn't handle pipes");
857 break;
858 case FileSpec::eFileTypeSocket:
859 error.SetErrorString("platform install doesn't handle sockets");
860 break;
861 case FileSpec::eFileTypeInvalid:
862 case FileSpec::eFileTypeUnknown:
863 case FileSpec::eFileTypeOther:
864 error.SetErrorString("platform install doesn't handle non file or directory items");
865 break;
866 }
867 }
868 return error;
869}
870
871bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000872Platform::SetWorkingDirectory(const FileSpec &file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000873{
874 if (IsHost())
875 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000876 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
877 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000878 log->Printf("Platform::SetWorkingDirectory('%s')",
879 file_spec.GetCString());
880 if (file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000881 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000882 if (::chdir(file_spec.GetCString()) == 0)
Greg Claytonfbb76342013-11-20 21:07:01 +0000883 return true;
884 }
885 return false;
886 }
887 else
888 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000889 m_working_dir.Clear();
Chaoren Lind3173f32015-05-29 19:52:29 +0000890 return SetRemoteWorkingDirectory(file_spec);
Greg Claytonfbb76342013-11-20 21:07:01 +0000891 }
892}
893
894Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000895Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000896{
897 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000898 return FileSystem::MakeDirectory(file_spec, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000899 else
900 {
901 Error error;
902 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
903 return error;
904 }
905}
906
907Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000908Platform::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000909{
910 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000911 return FileSystem::GetFilePermissions(file_spec, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000912 else
913 {
914 Error error;
915 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
916 return error;
917 }
918}
919
920Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000921Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000922{
923 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000924 return FileSystem::SetFilePermissions(file_spec, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000925 else
926 {
927 Error error;
928 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
929 return error;
930 }
931}
932
933ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000934Platform::GetName ()
935{
Greg Claytonfbb76342013-11-20 21:07:01 +0000936 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000937}
938
939const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000940Platform::GetHostname ()
941{
Greg Claytonab65b342011-04-13 22:47:15 +0000942 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000943 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000944
945 if (m_name.empty())
946 return NULL;
947 return m_name.c_str();
948}
949
Greg Clayton5fb8f792013-12-02 19:35:49 +0000950bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000951Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir)
Greg Clayton5fb8f792013-12-02 19:35:49 +0000952{
953 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
954 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000955 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
956 working_dir.GetCString());
957 m_working_dir = working_dir;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000958 return true;
959}
960
Greg Clayton32e0a752011-03-30 18:16:51 +0000961const char *
962Platform::GetUserName (uint32_t uid)
963{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000964#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000965 const char *user_name = GetCachedUserName(uid);
966 if (user_name)
967 return user_name;
968 if (IsHost())
969 {
970 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000971 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000972 return SetCachedUserName (uid, name.c_str(), name.size());
973 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000974#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000975 return NULL;
976}
977
Greg Clayton32e0a752011-03-30 18:16:51 +0000978const char *
979Platform::GetGroupName (uint32_t gid)
980{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000981#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000982 const char *group_name = GetCachedGroupName(gid);
983 if (group_name)
984 return group_name;
985 if (IsHost())
986 {
987 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000988 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000989 return SetCachedGroupName (gid, name.c_str(), name.size());
990 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000991#endif
Greg Clayton32e0a752011-03-30 18:16:51 +0000992 return NULL;
993}
Greg Clayton1cb64962011-03-24 04:28:38 +0000994
Greg Claytonded470d2011-03-19 01:12:21 +0000995bool
996Platform::SetOSVersion (uint32_t major,
997 uint32_t minor,
998 uint32_t update)
999{
1000 if (IsHost())
1001 {
Zachary Turner97a14e62014-08-19 17:18:29 +00001002 // We don't need anyone setting the OS version for the host platform,
1003 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +00001004 return false;
1005 }
1006 else
1007 {
1008 // We have a remote platform, allow setting the target OS version if
1009 // we aren't connected, since if we are connected, we should be able to
1010 // request the remote OS version from the connected platform.
1011 if (IsConnected())
1012 return false;
1013 else
1014 {
1015 // We aren't connected and we might want to set the OS version
1016 // ahead of time before we connect so we can peruse files and
1017 // use a local SDK or PDK cache of support files to disassemble
1018 // or do other things.
1019 m_major_os_version = major;
1020 m_minor_os_version = minor;
1021 m_update_os_version = update;
1022 return true;
1023 }
1024 }
1025 return false;
1026}
1027
1028
Greg Claytone996fd32011-03-08 22:40:15 +00001029Error
Greg Clayton8012cad2014-11-17 19:39:20 +00001030Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001031 lldb::ModuleSP &exe_module_sp,
1032 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +00001033{
1034 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +00001035 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +00001036 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001037 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +00001038 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001039 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001040 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001041 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001042 NULL,
1043 NULL);
1044 }
1045 else
1046 {
1047 // No valid architecture was specified, ask the platform for
1048 // the architectures that we should be using (in the correct order)
1049 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +00001050 ModuleSpec arch_module_spec(module_spec);
1051 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +00001052 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001053 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001054 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001055 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001056 NULL,
1057 NULL);
1058 // Did we find an executable using one of the
1059 if (error.Success() && exe_module_sp)
1060 break;
1061 }
1062 }
1063 }
1064 else
1065 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001066 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +00001067 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +00001068 }
1069 return error;
1070}
1071
Greg Clayton103f0282012-09-12 02:03:59 +00001072Error
1073Platform::ResolveSymbolFile (Target &target,
1074 const ModuleSpec &sym_spec,
1075 FileSpec &sym_file)
1076{
1077 Error error;
1078 if (sym_spec.GetSymbolFileSpec().Exists())
1079 sym_file = sym_spec.GetSymbolFileSpec();
1080 else
1081 error.SetErrorString("unable to resolve symbol file");
1082 return error;
1083
1084}
1085
1086
1087
Greg Claytonaa516842011-08-11 16:25:18 +00001088bool
1089Platform::ResolveRemotePath (const FileSpec &platform_path,
1090 FileSpec &resolved_platform_path)
1091{
1092 resolved_platform_path = platform_path;
1093 return resolved_platform_path.ResolvePath();
1094}
1095
Greg Claytonded470d2011-03-19 01:12:21 +00001096
1097const ArchSpec &
1098Platform::GetSystemArchitecture()
1099{
1100 if (IsHost())
1101 {
1102 if (!m_system_arch.IsValid())
1103 {
1104 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +00001105 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +00001106 m_system_arch_set_while_connected = m_system_arch.IsValid();
1107 }
1108 }
1109 else
1110 {
1111 // We have a remote platform. We can only fetch the remote
1112 // system architecture if we are connected, and we don't want to do it
1113 // more than once.
1114
1115 const bool is_connected = IsConnected();
1116
1117 bool fetch = false;
1118 if (m_system_arch.IsValid())
1119 {
1120 // We have valid OS version info, check to make sure it wasn't
1121 // manually set prior to connecting. If it was manually set prior
1122 // to connecting, then lets fetch the actual OS version info
1123 // if we are now connected.
1124 if (is_connected && !m_system_arch_set_while_connected)
1125 fetch = true;
1126 }
1127 else
1128 {
1129 // We don't have valid OS version info, fetch it if we are connected
1130 fetch = is_connected;
1131 }
1132
1133 if (fetch)
1134 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001135 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001136 m_system_arch_set_while_connected = m_system_arch.IsValid();
1137 }
1138 }
1139 return m_system_arch;
1140}
1141
1142
Greg Claytone996fd32011-03-08 22:40:15 +00001143Error
Greg Claytond314e812011-03-23 00:09:55 +00001144Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001145{
1146 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001147 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001148 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001149 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001150 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001151 return error;
1152}
1153
1154Error
Greg Claytond314e812011-03-23 00:09:55 +00001155Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001156{
1157 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001158 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001159 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001160 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001161 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001162 return error;
1163}
Greg Clayton32e0a752011-03-30 18:16:51 +00001164
1165bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001166Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001167{
1168 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001169 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001170 if (IsHost())
1171 return Host::GetProcessInfo (pid, process_info);
1172 return false;
1173}
1174
1175uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001176Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1177 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001178{
Greg Clayton8b82f082011-04-12 05:54:46 +00001179 // Take care of the host case so that each subclass can just
1180 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001181 uint32_t match_count = 0;
1182 if (IsHost())
1183 match_count = Host::FindProcesses (match_info, process_infos);
1184 return match_count;
1185}
Greg Clayton8b82f082011-04-12 05:54:46 +00001186
1187
1188Error
1189Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1190{
1191 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001192 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1193 if (log)
1194 log->Printf ("Platform::%s()", __FUNCTION__);
1195
1196 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001197 // call this function to get the host functionality.
1198 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001199 {
1200 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1201 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1202
1203 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1204 {
1205 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001206 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1207 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001208 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001209 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001210 {
1211 const FileSpec &shell = launch_info.GetShell();
1212 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001213 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1214 __FUNCTION__,
1215 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001216 shell_str);
1217 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001218
Greg Claytond1cf11a2012-04-14 01:42:46 +00001219 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1220 is_localhost,
1221 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001222 first_arg_is_full_shell_command,
1223 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001224 return error;
1225 }
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001226 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments))
Enrico Granatad7a83a92015-02-10 03:06:24 +00001227 {
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001228 error = ShellExpandArguments(launch_info);
Enrico Granata83a14372015-02-20 21:48:38 +00001229 if (error.Fail())
Enrico Granatad7a83a92015-02-10 03:06:24 +00001230 return error;
Enrico Granatad7a83a92015-02-10 03:06:24 +00001231 }
Greg Clayton84db9102012-03-26 23:03:23 +00001232
Todd Fialaac33cc92014-10-09 01:02:08 +00001233 if (log)
1234 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1235
Greg Clayton8b82f082011-04-12 05:54:46 +00001236 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001237 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001238 else
1239 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1240 return error;
1241}
1242
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001243Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001244Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +00001245{
1246 if (IsHost())
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001247 return Host::ShellExpandArguments(launch_info);
1248 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001249}
1250
1251Error
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001252Platform::KillProcess (const lldb::pid_t pid)
1253{
1254 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1255 if (log)
1256 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
1257
Zachary Turner7271bab2015-05-13 19:44:24 +00001258 // Try to find a process plugin to handle this Kill request. If we can't, fall back to
1259 // the default OS implementation.
1260 size_t num_debuggers = Debugger::GetNumDebuggers();
1261 for (size_t didx = 0; didx < num_debuggers; ++didx)
1262 {
1263 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1264 lldb_private::TargetList &targets = debugger->GetTargetList();
1265 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx)
1266 {
1267 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1268 if (process->GetID() == pid)
1269 return process->Destroy(true);
1270 }
1271 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001272
Zachary Turner7271bab2015-05-13 19:44:24 +00001273 if (!IsHost())
1274 {
1275 return Error("base lldb_private::Platform class can't kill remote processes unless "
1276 "they are controlled by a process plugin");
1277 }
1278 Host::Kill(pid, SIGTERM);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001279 return Error();
1280}
1281
Greg Clayton8b82f082011-04-12 05:54:46 +00001282lldb::ProcessSP
1283Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1284 Debugger &debugger,
1285 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001286 Error &error)
1287{
Todd Fialaac33cc92014-10-09 01:02:08 +00001288 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1289 if (log)
1290 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1291
Greg Clayton8b82f082011-04-12 05:54:46 +00001292 ProcessSP process_sp;
1293 // Make sure we stop at the entry point
1294 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001295 // We always launch the process we are going to debug in a separate process
1296 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1297 // about the target getting them as well.
1298 launch_info.SetLaunchInSeparateProcessGroup(true);
1299
Greg Clayton8b82f082011-04-12 05:54:46 +00001300 error = LaunchProcess (launch_info);
1301 if (error.Success())
1302 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001303 if (log)
1304 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001305 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001306 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001307 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001308 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001309 if (process_sp)
1310 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001311 if (log)
1312 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001313 launch_info.SetHijackListener(attach_info.GetHijackListener());
1314
Greg Claytone24c4ac2011-11-17 04:46:02 +00001315 // Since we attached to the process, it will think it needs to detach
1316 // if the process object just goes away without an explicit call to
1317 // Process::Kill() or Process::Detach(), so let it know to kill the
1318 // process if this happens.
1319 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001320
1321 // If we didn't have any file actions, the pseudo terminal might
1322 // have been used where the slave side was given as the file to
1323 // open for stdin/out/err after we have already opened the master
1324 // so we can read/write stdin/out/err.
1325 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1326 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1327 {
1328 process_sp->SetSTDIOFileDescriptor(pty_fd);
1329 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001330 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001331 else
1332 {
1333 if (log)
1334 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1335 }
1336 }
1337 else
1338 {
1339 if (log)
1340 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001341 }
1342 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001343 else
1344 {
1345 if (log)
1346 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1347 }
1348
Greg Clayton8b82f082011-04-12 05:54:46 +00001349 return process_sp;
1350}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001351
1352
1353lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001354Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001355{
1356 lldb::PlatformSP platform_sp;
1357 Error error;
1358 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001359 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001360 return platform_sp;
1361}
1362
1363
1364//------------------------------------------------------------------
1365/// Lets a platform answer if it is compatible with a given
1366/// architecture and the target triple contained within.
1367//------------------------------------------------------------------
1368bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001369Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001370{
1371 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001372 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001373 {
Greg Clayton70512312012-05-08 01:45:38 +00001374 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001375 // Try for an exact architecture match first.
1376 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001377 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001378 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001379 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001380 if (arch.IsExactMatch(platform_arch))
1381 {
1382 if (compatible_arch_ptr)
1383 *compatible_arch_ptr = platform_arch;
1384 return true;
1385 }
1386 }
1387 }
1388 else
1389 {
1390 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1391 {
1392 if (arch.IsCompatibleMatch(platform_arch))
1393 {
1394 if (compatible_arch_ptr)
1395 *compatible_arch_ptr = platform_arch;
1396 return true;
1397 }
Greg Clayton70512312012-05-08 01:45:38 +00001398 }
1399 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001400 }
Greg Clayton70512312012-05-08 01:45:38 +00001401 if (compatible_arch_ptr)
1402 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001403 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001404}
1405
Daniel Maleae0f8f572013-08-26 23:57:52 +00001406Error
1407Platform::PutFile (const FileSpec& source,
1408 const FileSpec& destination,
1409 uint32_t uid,
1410 uint32_t gid)
1411{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001412 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1413 if (log)
1414 log->Printf("[PutFile] Using block by block transfer....\n");
1415
Pavel Labath646b0642015-02-17 16:07:52 +00001416 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001417 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1418 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1419
1420 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1421 Error error;
1422 uint32_t permissions = source_file.GetPermissions(error);
1423 if (permissions == 0)
1424 permissions = lldb::eFilePermissionsFileDefault;
1425
1426 if (!source_file.IsValid())
1427 return Error("PutFile: unable to open source file");
1428 lldb::user_id_t dest_file = OpenFile (destination,
1429 File::eOpenOptionCanCreate |
1430 File::eOpenOptionWrite |
Pavel Labath646b0642015-02-17 16:07:52 +00001431 File::eOpenOptionTruncate |
1432 File::eOpenOptionCloseOnExec,
Vince Harron1b5a74e2015-01-21 22:42:49 +00001433 permissions,
1434 error);
1435 if (log)
1436 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1437
1438 if (error.Fail())
1439 return error;
1440 if (dest_file == UINT64_MAX)
1441 return Error("unable to open target file");
1442 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1443 uint64_t offset = 0;
1444 for (;;)
1445 {
1446 size_t bytes_read = buffer_sp->GetByteSize();
1447 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1448 if (error.Fail() || bytes_read == 0)
1449 break;
1450
1451 const uint64_t bytes_written = WriteFile(dest_file, offset,
1452 buffer_sp->GetBytes(), bytes_read, error);
1453 if (error.Fail())
1454 break;
1455
1456 offset += bytes_written;
1457 if (bytes_written != bytes_read)
1458 {
1459 // We didn't write the correct number of bytes, so adjust
1460 // the file position in the source file we are reading from...
1461 source_file.SeekFromStart(offset);
1462 }
1463 }
1464 CloseFile(dest_file, error);
1465
1466 if (uid == UINT32_MAX && gid == UINT32_MAX)
1467 return error;
1468
1469 // TODO: ChownFile?
1470
Daniel Maleae0f8f572013-08-26 23:57:52 +00001471 return error;
1472}
1473
1474Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001475Platform::GetFile(const FileSpec &source,
1476 const FileSpec &destination)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001477{
1478 Error error("unimplemented");
1479 return error;
1480}
1481
Greg Claytonfbb76342013-11-20 21:07:01 +00001482Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001483Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1484 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001485{
1486 Error error("unimplemented");
1487 return error;
1488}
1489
Daniel Maleae0f8f572013-08-26 23:57:52 +00001490bool
Chaoren Lind3173f32015-05-29 19:52:29 +00001491Platform::GetFileExists(const lldb_private::FileSpec &file_spec)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001492{
1493 return false;
1494}
1495
Greg Claytonfbb76342013-11-20 21:07:01 +00001496Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001497Platform::Unlink(const FileSpec &path)
Greg Claytonfbb76342013-11-20 21:07:01 +00001498{
1499 Error error("unimplemented");
1500 return error;
1501}
1502
Robert Flack96ad3de2015-05-09 15:53:31 +00001503uint64_t
Mohit K. Bhakkade0d8c422015-06-30 06:29:12 +00001504Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags)
Robert Flack96ad3de2015-05-09 15:53:31 +00001505{
1506 uint64_t flags_platform = 0;
1507 if (flags & eMmapFlagsPrivate)
1508 flags_platform |= MAP_PRIVATE;
1509 if (flags & eMmapFlagsAnon)
1510 flags_platform |= MAP_ANON;
1511 return flags_platform;
1512}
Greg Claytonfbb76342013-11-20 21:07:01 +00001513
Daniel Maleae0f8f572013-08-26 23:57:52 +00001514lldb_private::Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001515Platform::RunShellCommand(const char *command, // Shouldn't be NULL
1516 const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory
1517 int *status_ptr, // Pass NULL if you don't want the process exit status
1518 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1519 std::string *command_output, // Pass NULL if you don't want the command output
1520 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001521{
1522 if (IsHost())
1523 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1524 else
1525 return Error("unimplemented");
1526}
1527
1528
1529bool
1530Platform::CalculateMD5 (const FileSpec& file_spec,
1531 uint64_t &low,
1532 uint64_t &high)
1533{
1534 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001535 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001536 else
1537 return false;
1538}
1539
1540void
1541Platform::SetLocalCacheDirectory (const char* local)
1542{
1543 m_local_cache_directory.assign(local);
1544}
1545
1546const char*
1547Platform::GetLocalCacheDirectory ()
1548{
1549 return m_local_cache_directory.c_str();
1550}
1551
1552static OptionDefinition
1553g_rsync_option_table[] =
1554{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001555 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1556 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1557 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1558 { 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 +00001559};
1560
1561static OptionDefinition
1562g_ssh_option_table[] =
1563{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001564 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1565 { 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 +00001566};
1567
1568static OptionDefinition
1569g_caching_option_table[] =
1570{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001571 { 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 +00001572};
1573
1574OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1575{
1576}
1577
1578OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1579{
1580}
1581
1582const lldb_private::OptionDefinition*
1583OptionGroupPlatformRSync::GetDefinitions ()
1584{
1585 return g_rsync_option_table;
1586}
1587
1588void
1589OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1590{
1591 m_rsync = false;
1592 m_rsync_opts.clear();
1593 m_rsync_prefix.clear();
1594 m_ignores_remote_hostname = false;
1595}
1596
1597lldb_private::Error
1598OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1599 uint32_t option_idx,
1600 const char *option_arg)
1601{
1602 Error error;
1603 char short_option = (char) GetDefinitions()[option_idx].short_option;
1604 switch (short_option)
1605 {
1606 case 'r':
1607 m_rsync = true;
1608 break;
1609
1610 case 'R':
1611 m_rsync_opts.assign(option_arg);
1612 break;
1613
1614 case 'P':
1615 m_rsync_prefix.assign(option_arg);
1616 break;
1617
1618 case 'i':
1619 m_ignores_remote_hostname = true;
1620 break;
1621
1622 default:
1623 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1624 break;
1625 }
1626
1627 return error;
1628}
1629
1630uint32_t
1631OptionGroupPlatformRSync::GetNumDefinitions ()
1632{
1633 return llvm::array_lengthof(g_rsync_option_table);
1634}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001635
Greg Clayton4116e932012-05-15 02:33:01 +00001636lldb::BreakpointSP
1637Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1638{
1639 return lldb::BreakpointSP();
1640}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001641
Daniel Maleae0f8f572013-08-26 23:57:52 +00001642OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1643{
1644}
1645
1646OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1647{
1648}
1649
1650const lldb_private::OptionDefinition*
1651OptionGroupPlatformSSH::GetDefinitions ()
1652{
1653 return g_ssh_option_table;
1654}
1655
1656void
1657OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1658{
1659 m_ssh = false;
1660 m_ssh_opts.clear();
1661}
1662
1663lldb_private::Error
1664OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1665 uint32_t option_idx,
1666 const char *option_arg)
1667{
1668 Error error;
1669 char short_option = (char) GetDefinitions()[option_idx].short_option;
1670 switch (short_option)
1671 {
1672 case 's':
1673 m_ssh = true;
1674 break;
1675
1676 case 'S':
1677 m_ssh_opts.assign(option_arg);
1678 break;
1679
1680 default:
1681 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1682 break;
1683 }
1684
1685 return error;
1686}
1687
1688uint32_t
1689OptionGroupPlatformSSH::GetNumDefinitions ()
1690{
1691 return llvm::array_lengthof(g_ssh_option_table);
1692}
1693
1694OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1695{
1696}
1697
1698OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1699{
1700}
1701
1702const lldb_private::OptionDefinition*
1703OptionGroupPlatformCaching::GetDefinitions ()
1704{
1705 return g_caching_option_table;
1706}
1707
1708void
1709OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1710{
1711 m_cache_dir.clear();
1712}
1713
1714lldb_private::Error
1715OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1716 uint32_t option_idx,
1717 const char *option_arg)
1718{
1719 Error error;
1720 char short_option = (char) GetDefinitions()[option_idx].short_option;
1721 switch (short_option)
1722 {
1723 case 'c':
1724 m_cache_dir.assign(option_arg);
1725 break;
1726
1727 default:
1728 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1729 break;
1730 }
1731
1732 return error;
1733}
1734
1735uint32_t
1736OptionGroupPlatformCaching::GetNumDefinitions ()
1737{
1738 return llvm::array_lengthof(g_caching_option_table);
1739}
1740
Greg Clayton67cc0632012-08-22 17:17:09 +00001741size_t
1742Platform::GetEnvironment (StringList &environment)
1743{
1744 environment.Clear();
1745 return false;
1746}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001747
1748const std::vector<ConstString> &
1749Platform::GetTrapHandlerSymbolNames ()
1750{
1751 if (!m_calculated_trap_handlers)
1752 {
Greg Clayton7597b352015-02-02 20:45:17 +00001753 Mutex::Locker locker (m_mutex);
Jason Molenda4da87062014-05-23 23:11:27 +00001754 if (!m_calculated_trap_handlers)
1755 {
1756 CalculateTrapHandlerSymbolNames();
1757 m_calculated_trap_handlers = true;
1758 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001759 }
1760 return m_trap_handlers;
1761}
1762
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001763Error
1764Platform::GetCachedExecutable (ModuleSpec &module_spec,
1765 lldb::ModuleSP &module_sp,
1766 const FileSpecList *module_search_paths_ptr,
1767 Platform &remote_platform)
1768{
1769 const auto platform_spec = module_spec.GetFileSpec ();
1770 const auto error = LoadCachedExecutable (module_spec,
1771 module_sp,
1772 module_search_paths_ptr,
1773 remote_platform);
1774 if (error.Success ())
1775 {
1776 module_spec.GetFileSpec () = module_sp->GetFileSpec ();
1777 module_spec.GetPlatformFileSpec () = platform_spec;
1778 }
1779
1780 return error;
1781}
1782
1783Error
1784Platform::LoadCachedExecutable (const ModuleSpec &module_spec,
1785 lldb::ModuleSP &module_sp,
1786 const FileSpecList *module_search_paths_ptr,
1787 Platform &remote_platform)
1788{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001789 return GetRemoteSharedModule (module_spec,
1790 nullptr,
1791 module_sp,
1792 [&](const ModuleSpec &spec)
1793 {
1794 return remote_platform.ResolveExecutable (
1795 spec, module_sp, module_search_paths_ptr);
1796 },
1797 nullptr);
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001798}
1799
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001800Error
1801Platform::GetRemoteSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001802 Process* process,
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001803 lldb::ModuleSP &module_sp,
1804 const ModuleResolver &module_resolver,
1805 bool *did_create_ptr)
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001806{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001807 // Get module information from a target.
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001808 ModuleSpec resolved_module_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001809 bool got_module_spec = false;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001810 if (process)
1811 {
1812 // Try to get module information from the process
1813 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001814 got_module_spec = true;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001815 }
1816
1817 if (!got_module_spec)
1818 {
1819 // Get module information from a target.
1820 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001821 return module_resolver (module_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001822 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001823
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001824 // Trying to find a module by UUID on local file system.
1825 const auto error = module_resolver (resolved_module_spec);
1826 if (error.Fail ())
1827 {
1828 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr))
1829 return Error ();
1830 }
1831
1832 return error;
1833}
1834
1835bool
1836Platform::GetCachedSharedModule (const ModuleSpec &module_spec,
1837 lldb::ModuleSP &module_sp,
1838 bool *did_create_ptr)
1839{
1840 if (IsHost() ||
1841 !GetGlobalPlatformProperties ()->GetUseModuleCache ())
1842 return false;
1843
1844 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
1845
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001846 // Check local cache for a module.
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001847 auto error = m_module_cache->GetAndPut (
1848 GetModuleCacheRoot (),
1849 GetCacheHostname (),
1850 module_spec,
Oleksiy Vyalov919ef9d2015-05-07 15:28:49 +00001851 [=](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec)
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001852 {
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001853 return DownloadModuleSlice (module_spec.GetFileSpec (),
1854 module_spec.GetObjectOffset (),
1855 module_spec.GetObjectSize (),
1856 tmp_download_file_spec);
1857
1858 },
1859 module_sp,
1860 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001861 if (error.Success ())
1862 return true;
1863
1864 if (log)
1865 log->Printf("Platform::%s - module %s not found in local cache: %s",
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001866 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001867 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001868}
1869
1870Error
1871Platform::DownloadModuleSlice (const FileSpec& src_file_spec,
1872 const uint64_t src_offset,
1873 const uint64_t src_size,
1874 const FileSpec& dst_file_spec)
1875{
1876 Error error;
1877
1878 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1879 if (!dst.is_open())
1880 {
1881 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ());
1882 return error;
1883 }
1884
1885 auto src_fd = OpenFile (src_file_spec,
1886 File::eOpenOptionRead,
1887 lldb::eFilePermissionsFileDefault,
1888 error);
1889
1890 if (error.Fail ())
1891 {
1892 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ());
1893 return error;
1894 }
1895
1896 std::vector<char> buffer (1024);
1897 auto offset = src_offset;
1898 uint64_t total_bytes_read = 0;
1899 while (total_bytes_read < src_size)
1900 {
1901 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read);
1902 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error);
1903 if (error.Fail ())
1904 break;
1905 if (n_read == 0)
1906 {
1907 error.SetErrorString ("read 0 bytes");
1908 break;
1909 }
1910 offset += n_read;
1911 total_bytes_read += n_read;
1912 dst.write (&buffer[0], n_read);
1913 }
1914
1915 Error close_error;
1916 CloseFile (src_fd, close_error); // Ignoring close error.
1917
1918 return error;
1919}
1920
1921FileSpec
1922Platform::GetModuleCacheRoot ()
1923{
1924 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory ();
1925 dir_spec.AppendPathComponent (GetName ().AsCString ());
1926 return dir_spec;
1927}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001928
1929const char *
1930Platform::GetCacheHostname ()
1931{
1932 return GetHostname ();
1933}
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001934
1935const UnixSignalsSP &
1936Platform::GetRemoteUnixSignals()
1937{
1938 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1939 return s_default_unix_signals_sp;
1940}
1941
1942const UnixSignalsSP &
1943Platform::GetUnixSignals()
1944{
1945 if (IsHost())
1946 return Host::GetUnixSignals();
1947 return GetRemoteUnixSignals();
1948}