blob: 8a82f5e609f52ca369b598eef18d0a9b867595b7 [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 Vyalovd21ca282015-10-01 17:48:57 +000042#include "llvm/Support/Path.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000043
44#include "Utility/ModuleCache.h"
Greg Claytone996fd32011-03-08 22:40:15 +000045
Adrian McCarthy3f989d42015-10-26 21:38:41 +000046
Robert Flack96ad3de2015-05-09 15:53:31 +000047// Define these constants from POSIX mman.h rather than include the file
48// so that they will be correct even when compiled on Linux.
49#define MAP_PRIVATE 2
50#define MAP_ANON 0x1000
51
Greg Claytone996fd32011-03-08 22:40:15 +000052using namespace lldb;
53using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000054
55static uint32_t g_initialize_count = 0;
56
Greg Claytone996fd32011-03-08 22:40:15 +000057// Use a singleton function for g_local_platform_sp to avoid init
58// constructors since LLDB is often part of a shared library
59static PlatformSP&
Greg Clayton615eb7e2014-09-19 20:11:50 +000060GetHostPlatformSP ()
Greg Claytone996fd32011-03-08 22:40:15 +000061{
Greg Clayton615eb7e2014-09-19 20:11:50 +000062 static PlatformSP g_platform_sp;
63 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000064}
65
Greg Claytonab65b342011-04-13 22:47:15 +000066const char *
67Platform::GetHostPlatformName ()
68{
69 return "host";
70}
71
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000072namespace {
73
74 PropertyDefinition
75 g_properties[] =
76 {
77 { "use-module-cache" , OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "Use module cache." },
78 { "module-cache-directory", OptionValue::eTypeFileSpec, true, 0 , nullptr, nullptr, "Root directory for cached modules." },
79 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
80 };
81
82 enum
83 {
84 ePropertyUseModuleCache,
85 ePropertyModuleCacheDirectory
86 };
87
88} // namespace
89
90
91ConstString
92PlatformProperties::GetSettingName ()
93{
94 static ConstString g_setting_name("platform");
95 return g_setting_name;
96}
97
98PlatformProperties::PlatformProperties ()
99{
100 m_collection_sp.reset (new OptionValueProperties (GetSettingName ()));
101 m_collection_sp->Initialize (g_properties);
102
103 auto module_cache_dir = GetModuleCacheDirectory ();
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +0000104 if (module_cache_dir)
105 return;
106
107 llvm::SmallString<64> user_home_dir;
108 if (!llvm::sys::path::home_directory (user_home_dir))
109 return;
110
111 module_cache_dir = FileSpec (user_home_dir.c_str(), false);
112 module_cache_dir.AppendPathComponent (".lldb");
113 module_cache_dir.AppendPathComponent ("module_cache");
114 SetModuleCacheDirectory (module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000115}
116
117bool
118PlatformProperties::GetUseModuleCache () const
119{
120 const auto idx = ePropertyUseModuleCache;
121 return m_collection_sp->GetPropertyAtIndexAsBoolean (
122 nullptr, idx, g_properties[idx].default_uint_value != 0);
123}
124
125bool
126PlatformProperties::SetUseModuleCache (bool use_module_cache)
127{
128 return m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, ePropertyUseModuleCache, use_module_cache);
129}
130
131FileSpec
132PlatformProperties::GetModuleCacheDirectory () const
133{
134 return m_collection_sp->GetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory);
135}
136
137bool
138PlatformProperties::SetModuleCacheDirectory (const FileSpec& dir_spec)
139{
140 return m_collection_sp->SetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory, dir_spec);
141}
142
Greg Claytone996fd32011-03-08 22:40:15 +0000143//------------------------------------------------------------------
144/// Get the native host platform plug-in.
145///
146/// There should only be one of these for each host that LLDB runs
147/// upon that should be statically compiled in and registered using
148/// preprocessor macros or other similar build mechanisms.
149///
150/// This platform will be used as the default platform when launching
151/// or attaching to processes unless another platform is specified.
152//------------------------------------------------------------------
153PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000154Platform::GetHostPlatform ()
Greg Claytone996fd32011-03-08 22:40:15 +0000155{
Greg Clayton615eb7e2014-09-19 20:11:50 +0000156 return GetHostPlatformSP ();
157}
158
159static std::vector<PlatformSP> &
160GetPlatformList()
161{
162 static std::vector<PlatformSP> g_platform_list;
163 return g_platform_list;
164}
165
166static Mutex &
167GetPlatformListMutex ()
168{
169 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
170 return g_mutex;
Greg Claytone996fd32011-03-08 22:40:15 +0000171}
172
173void
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000174Platform::Initialize ()
175{
176 g_initialize_count++;
177}
178
179void
180Platform::Terminate ()
181{
182 if (g_initialize_count > 0)
183 {
184 if (--g_initialize_count == 0)
185 {
186 Mutex::Locker locker(GetPlatformListMutex ());
187 GetPlatformList().clear();
188 }
189 }
190}
191
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000192const PlatformPropertiesSP &
193Platform::GetGlobalPlatformProperties ()
194{
195 static const auto g_settings_sp (std::make_shared<PlatformProperties> ());
196 return g_settings_sp;
197}
198
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000199void
Greg Clayton615eb7e2014-09-19 20:11:50 +0000200Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp)
Greg Claytone996fd32011-03-08 22:40:15 +0000201{
202 // The native platform should use its static void Platform::Initialize()
203 // function to register itself as the native platform.
Greg Clayton615eb7e2014-09-19 20:11:50 +0000204 GetHostPlatformSP () = platform_sp;
205
206 if (platform_sp)
207 {
208 Mutex::Locker locker(GetPlatformListMutex ());
209 GetPlatformList().push_back(platform_sp);
210 }
Greg Claytone996fd32011-03-08 22:40:15 +0000211}
212
Greg Claytone996fd32011-03-08 22:40:15 +0000213Error
Steve Puccifc995722014-01-17 18:18:31 +0000214Platform::GetFileWithUUID (const FileSpec &platform_file,
215 const UUID *uuid_ptr,
216 FileSpec &local_file)
Greg Claytone996fd32011-03-08 22:40:15 +0000217{
218 // Default to the local case
219 local_file = platform_file;
220 return Error();
221}
222
Greg Clayton91c0e742013-01-11 23:44:27 +0000223FileSpecList
Enrico Granatafe7295d2014-08-16 00:32:58 +0000224Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +0000225{
Greg Clayton91c0e742013-01-11 23:44:27 +0000226 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000227}
228
Greg Clayton615eb7e2014-09-19 20:11:50 +0000229//PlatformSP
230//Platform::FindPlugin (Process *process, const ConstString &plugin_name)
231//{
232// PlatformCreateInstance create_callback = NULL;
233// if (plugin_name)
234// {
235// create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
236// if (create_callback)
237// {
238// ArchSpec arch;
239// if (process)
240// {
241// arch = process->GetTarget().GetArchitecture();
242// }
243// PlatformSP platform_sp(create_callback(process, &arch));
244// if (platform_sp)
245// return platform_sp;
246// }
247// }
248// else
249// {
250// for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
251// {
252// PlatformSP platform_sp(create_callback(process, nullptr));
253// if (platform_sp)
254// return platform_sp;
255// }
256// }
257// return PlatformSP();
258//}
Jason Molenda1c627542013-04-05 01:03:25 +0000259
Greg Clayton32e0a752011-03-30 18:16:51 +0000260Error
Greg Claytonb9a01b32012-02-26 05:51:37 +0000261Platform::GetSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000262 Process* process,
Greg Clayton32e0a752011-03-30 18:16:51 +0000263 ModuleSP &module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000264 const FileSpecList *module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000265 ModuleSP *old_module_sp_ptr,
266 bool *did_create_ptr)
267{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000268 if (IsHost ())
269 return ModuleList::GetSharedModule (module_spec,
270 module_sp,
271 module_search_paths_ptr,
272 old_module_sp_ptr,
273 did_create_ptr,
274 false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000275
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000276 return GetRemoteSharedModule (module_spec,
277 process,
278 module_sp,
279 [&](const ModuleSpec &spec)
280 {
Tamas Berghammer980662e2015-09-04 12:42:41 +0000281 Error error = ModuleList::GetSharedModule (
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000282 spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr, false);
Tamas Berghammer980662e2015-09-04 12:42:41 +0000283 if (error.Success() && module_sp)
284 module_sp->SetPlatformFileSpec(spec.GetFileSpec());
285 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000286 },
287 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000288}
289
290bool
291Platform::GetModuleSpec (const FileSpec& module_file_spec,
292 const ArchSpec& arch,
293 ModuleSpec &module_spec)
294{
295 ModuleSpecList module_specs;
296 if (ObjectFile::GetModuleSpecifications (module_file_spec, 0, 0, module_specs) == 0)
297 return false;
298
299 ModuleSpec matched_module_spec;
300 return module_specs.FindMatchingModuleSpec (ModuleSpec (module_file_spec, arch),
301 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000302}
303
Greg Claytone996fd32011-03-08 22:40:15 +0000304PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000305Platform::Find (const ConstString &name)
306{
307 if (name)
308 {
309 static ConstString g_host_platform_name ("host");
310 if (name == g_host_platform_name)
311 return GetHostPlatform();
312
313 Mutex::Locker locker(GetPlatformListMutex ());
314 for (const auto &platform_sp : GetPlatformList())
315 {
316 if (platform_sp->GetName() == name)
317 return platform_sp;
318 }
319 }
320 return PlatformSP();
321}
322
323PlatformSP
324Platform::Create (const ConstString &name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000325{
326 PlatformCreateInstance create_callback = NULL;
327 lldb::PlatformSP platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000328 if (name)
Greg Claytone996fd32011-03-08 22:40:15 +0000329 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000330 static ConstString g_host_platform_name ("host");
331 if (name == g_host_platform_name)
332 return GetHostPlatform();
333
334 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name);
Greg Claytone996fd32011-03-08 22:40:15 +0000335 if (create_callback)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000336 platform_sp = create_callback(true, NULL);
Greg Claytone996fd32011-03-08 22:40:15 +0000337 else
Greg Clayton615eb7e2014-09-19 20:11:50 +0000338 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000339 }
340 else
Greg Claytonded470d2011-03-19 01:12:21 +0000341 error.SetErrorString ("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000342
343 if (platform_sp)
344 {
345 Mutex::Locker locker(GetPlatformListMutex ());
346 GetPlatformList().push_back(platform_sp);
347 }
348
Greg Claytone996fd32011-03-08 22:40:15 +0000349 return platform_sp;
350}
351
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000352
353PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000354Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000355{
356 lldb::PlatformSP platform_sp;
357 if (arch.IsValid())
358 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000359 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000360 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000361 // First try exact arch matches across all platforms already created
362 Mutex::Locker locker(GetPlatformListMutex ());
363 for (const auto &platform_sp : GetPlatformList())
Greg Clayton1e0c8842013-01-11 20:49:54 +0000364 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000365 if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
366 return platform_sp;
367 }
368
369 // Next try compatible arch matches across all platforms already created
370 for (const auto &platform_sp : GetPlatformList())
371 {
372 if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
Greg Clayton1e0c8842013-01-11 20:49:54 +0000373 return platform_sp;
374 }
375 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000376
377 PlatformCreateInstance create_callback;
378 // First try exact arch matches across all platform plug-ins
379 uint32_t idx;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000380 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
381 {
382 if (create_callback)
383 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000384 platform_sp = create_callback(false, &arch);
385 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
386 {
387 Mutex::Locker locker(GetPlatformListMutex ());
388 GetPlatformList().push_back(platform_sp);
Greg Clayton1e0c8842013-01-11 20:49:54 +0000389 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000390 }
391 }
392 }
393 // Next try compatible arch matches across all platform plug-ins
394 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
395 {
396 if (create_callback)
397 {
398 platform_sp = create_callback(false, &arch);
399 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
400 {
401 Mutex::Locker locker(GetPlatformListMutex ());
402 GetPlatformList().push_back(platform_sp);
403 return platform_sp;
404 }
Greg Clayton1e0c8842013-01-11 20:49:54 +0000405 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000406 }
407 }
408 else
409 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000410 if (platform_arch_ptr)
411 platform_arch_ptr->Clear();
412 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000413 return platform_sp;
414}
415
Greg Claytone996fd32011-03-08 22:40:15 +0000416//------------------------------------------------------------------
417/// Default Constructor
418//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000419Platform::Platform (bool is_host) :
420 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000421 m_os_version_set_while_connected (false),
422 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000423 m_sdk_sysroot (),
424 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000425 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000426 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000427 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000428 m_major_os_version (UINT32_MAX),
429 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000430 m_update_os_version (UINT32_MAX),
431 m_system_arch(),
Greg Clayton7597b352015-02-02 20:45:17 +0000432 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +0000433 m_uid_map(),
434 m_gid_map(),
435 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000436 m_max_gid_name_len (0),
437 m_supports_rsync (false),
438 m_rsync_opts (),
439 m_rsync_prefix (),
440 m_supports_ssh (false),
441 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000442 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000443 m_trap_handlers(),
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000444 m_calculated_trap_handlers (false),
445 m_module_cache (llvm::make_unique<ModuleCache> ())
Greg Claytone996fd32011-03-08 22:40:15 +0000446{
Greg Clayton5160ce52013-03-27 23:08:40 +0000447 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000448 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000449 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000450}
451
452//------------------------------------------------------------------
453/// Destructor.
454///
455/// The destructor is virtual since this class is designed to be
456/// inherited from by the plug-in instance.
457//------------------------------------------------------------------
458Platform::~Platform()
459{
Greg Clayton5160ce52013-03-27 23:08:40 +0000460 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000461 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000462 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000463}
464
Greg Clayton1cb64962011-03-24 04:28:38 +0000465void
466Platform::GetStatus (Stream &strm)
467{
468 uint32_t major = UINT32_MAX;
469 uint32_t minor = UINT32_MAX;
470 uint32_t update = UINT32_MAX;
471 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000472 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000473
474 ArchSpec arch (GetSystemArchitecture());
475 if (arch.IsValid())
476 {
477 if (!arch.GetTriple().str().empty())
Todd Fiala7df337f2015-10-13 23:41:19 +0000478 {
479 strm.Printf(" Triple: ");
480 arch.DumpTriple(strm);
481 strm.EOL();
482 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000483 }
484
485 if (GetOSVersion(major, minor, update))
486 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000487 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000488 if (minor != UINT32_MAX)
489 strm.Printf(".%u", minor);
490 if (update != UINT32_MAX)
491 strm.Printf(".%u", update);
492
493 if (GetOSBuildString (s))
494 strm.Printf(" (%s)", s.c_str());
495
496 strm.EOL();
497 }
498
499 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000500 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000501
502 if (IsHost())
503 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000504 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000505 }
506 else
507 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000508 const bool is_connected = IsConnected();
509 if (is_connected)
510 strm.Printf(" Hostname: %s\n", GetHostname());
511 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000512 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000513
Greg Claytonfbb76342013-11-20 21:07:01 +0000514 if (GetWorkingDirectory())
515 {
516 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
517 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000518 if (!IsConnected())
519 return;
520
521 std::string specific_info(GetPlatformSpecificConnectionInformation());
522
523 if (specific_info.empty() == false)
524 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000525}
526
Greg Claytonded470d2011-03-19 01:12:21 +0000527
528bool
529Platform::GetOSVersion (uint32_t &major,
530 uint32_t &minor,
Jim Ingham13c30d22015-11-05 22:33:17 +0000531 uint32_t &update,
532 Process *process)
Greg Claytonded470d2011-03-19 01:12:21 +0000533{
Greg Clayton7597b352015-02-02 20:45:17 +0000534 Mutex::Locker locker (m_mutex);
535
Greg Claytonded470d2011-03-19 01:12:21 +0000536 bool success = m_major_os_version != UINT32_MAX;
537 if (IsHost())
538 {
539 if (!success)
540 {
541 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000542 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000543 m_os_version_set_while_connected = success;
544 }
545 }
546 else
547 {
548 // We have a remote platform. We can only fetch the remote
549 // OS version if we are connected, and we don't want to do it
550 // more than once.
551
552 const bool is_connected = IsConnected();
553
Greg Clayton1cb64962011-03-24 04:28:38 +0000554 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000555 if (success)
556 {
557 // We have valid OS version info, check to make sure it wasn't
558 // manually set prior to connecting. If it was manually set prior
559 // to connecting, then lets fetch the actual OS version info
560 // if we are now connected.
561 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000562 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000563 }
564 else
565 {
566 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000567 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000568 }
569
Greg Clayton1cb64962011-03-24 04:28:38 +0000570 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000571 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000572 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000573 m_os_version_set_while_connected = success;
574 }
575 }
576
577 if (success)
578 {
579 major = m_major_os_version;
580 minor = m_minor_os_version;
581 update = m_update_os_version;
582 }
Jim Ingham13c30d22015-11-05 22:33:17 +0000583 else if (process)
584 {
585 // Check with the process in case it can answer the question if
586 // a process was provided
587 return process->GetHostOSVersion(major, minor, update);
588 }
Greg Claytonded470d2011-03-19 01:12:21 +0000589 return success;
590}
Greg Clayton1cb64962011-03-24 04:28:38 +0000591
592bool
593Platform::GetOSBuildString (std::string &s)
594{
Zachary Turner97a14e62014-08-19 17:18:29 +0000595 s.clear();
596
Greg Clayton1cb64962011-03-24 04:28:38 +0000597 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000598#if !defined(__linux__)
599 return HostInfo::GetOSBuildString(s);
600#else
601 return false;
602#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000603 else
604 return GetRemoteOSBuildString (s);
605}
606
607bool
608Platform::GetOSKernelDescription (std::string &s)
609{
610 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000611#if !defined(__linux__)
612 return HostInfo::GetOSKernelDescription(s);
613#else
614 return false;
615#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000616 else
617 return GetRemoteOSKernelDescription (s);
618}
619
Sean Callanan5dc29812014-12-05 01:16:31 +0000620void
Greg Claytoncd6bbba2015-01-22 18:25:49 +0000621Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options)
Sean Callanan5dc29812014-12-05 01:16:31 +0000622{
623 std::vector<std::string> default_compilation_options =
624 {
625 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"
626 };
627
628 options.insert(options.end(),
629 default_compilation_options.begin(),
630 default_compilation_options.end());
631}
632
633
Chaoren Lind3173f32015-05-29 19:52:29 +0000634FileSpec
Greg Claytonfbb76342013-11-20 21:07:01 +0000635Platform::GetWorkingDirectory ()
636{
637 if (IsHost())
638 {
639 char cwd[PATH_MAX];
640 if (getcwd(cwd, sizeof(cwd)))
Chaoren Lind3173f32015-05-29 19:52:29 +0000641 return FileSpec{cwd, true};
Greg Claytonfbb76342013-11-20 21:07:01 +0000642 else
Chaoren Lind3173f32015-05-29 19:52:29 +0000643 return FileSpec{};
Greg Claytonfbb76342013-11-20 21:07:01 +0000644 }
645 else
646 {
647 if (!m_working_dir)
648 m_working_dir = GetRemoteWorkingDirectory();
649 return m_working_dir;
650 }
651}
652
653
654struct RecurseCopyBaton
655{
656 const FileSpec& dst;
657 Platform *platform_ptr;
658 Error error;
659};
660
661
662static FileSpec::EnumerateDirectoryResult
663RecurseCopy_Callback (void *baton,
664 FileSpec::FileType file_type,
665 const FileSpec &src)
666{
667 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
668 switch (file_type)
669 {
670 case FileSpec::eFileTypePipe:
671 case FileSpec::eFileTypeSocket:
672 // we have no way to copy pipes and sockets - ignore them and continue
673 return FileSpec::eEnumerateDirectoryResultNext;
674 break;
675
676 case FileSpec::eFileTypeDirectory:
677 {
678 // make the new directory and get in there
679 FileSpec dst_dir = rc_baton->dst;
680 if (!dst_dir.GetFilename())
681 dst_dir.GetFilename() = src.GetLastPathComponent();
Chaoren Lind3173f32015-05-29 19:52:29 +0000682 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault);
Greg Claytonfbb76342013-11-20 21:07:01 +0000683 if (error.Fail())
684 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000685 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end",
686 dst_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000687 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
688 }
689
690 // now recurse
691 std::string src_dir_path (src.GetPath());
692
693 // Make a filespec that only fills in the directory of a FileSpec so
694 // when we enumerate we can quickly fill in the filename for dst copies
695 FileSpec recurse_dst;
696 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
697 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
698 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
699 if (rc_baton2.error.Fail())
700 {
701 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
702 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
703 }
704 return FileSpec::eEnumerateDirectoryResultNext;
705 }
706 break;
707
708 case FileSpec::eFileTypeSymbolicLink:
709 {
710 // copy the file and keep going
711 FileSpec dst_file = rc_baton->dst;
712 if (!dst_file.GetFilename())
713 dst_file.GetFilename() = src.GetFilename();
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000714
Chaoren Lind3173f32015-05-29 19:52:29 +0000715 FileSpec src_resolved;
716
717 rc_baton->error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000718
719 if (rc_baton->error.Fail())
720 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Chaoren Lind3173f32015-05-29 19:52:29 +0000721
722 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000723
724 if (rc_baton->error.Fail())
725 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
726
727 return FileSpec::eEnumerateDirectoryResultNext;
728 }
729 break;
730 case FileSpec::eFileTypeRegular:
731 {
732 // copy the file and keep going
733 FileSpec dst_file = rc_baton->dst;
734 if (!dst_file.GetFilename())
735 dst_file.GetFilename() = src.GetFilename();
736 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
737 if (err.Fail())
738 {
739 rc_baton->error.SetErrorString(err.AsCString());
740 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
741 }
742 return FileSpec::eEnumerateDirectoryResultNext;
743 }
744 break;
745
746 case FileSpec::eFileTypeInvalid:
747 case FileSpec::eFileTypeOther:
748 case FileSpec::eFileTypeUnknown:
749 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
750 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
751 break;
752 }
David Majnemer8faf9372014-09-16 06:34:29 +0000753 llvm_unreachable("Unhandled FileSpec::FileType!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000754}
755
756Error
757Platform::Install (const FileSpec& src, const FileSpec& dst)
758{
759 Error error;
760
761 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
762 if (log)
763 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
764 FileSpec fixed_dst(dst);
765
766 if (!fixed_dst.GetFilename())
767 fixed_dst.GetFilename() = src.GetFilename();
768
Chaoren Lind3173f32015-05-29 19:52:29 +0000769 FileSpec working_dir = GetWorkingDirectory();
Greg Claytonfbb76342013-11-20 21:07:01 +0000770
771 if (dst)
772 {
773 if (dst.GetDirectory())
774 {
775 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
776 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
777 {
778 fixed_dst.GetDirectory() = dst.GetDirectory();
779 }
780 // If the fixed destination file doesn't have a directory yet,
781 // then we must have a relative path. We will resolve this relative
782 // path against the platform's working directory
783 if (!fixed_dst.GetDirectory())
784 {
785 FileSpec relative_spec;
786 std::string path;
787 if (working_dir)
788 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000789 relative_spec = working_dir;
790 relative_spec.AppendPathComponent(dst.GetPath());
Greg Claytonfbb76342013-11-20 21:07:01 +0000791 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
792 }
793 else
794 {
795 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
796 return error;
797 }
798 }
799 }
800 else
801 {
802 if (working_dir)
803 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000804 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000805 }
806 else
807 {
808 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
809 return error;
810 }
811 }
812 }
813 else
814 {
815 if (working_dir)
816 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000817 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000818 }
819 else
820 {
821 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
822 return error;
823 }
824 }
825
826 if (log)
827 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
828
829 if (GetSupportsRSync())
830 {
831 error = PutFile(src, dst);
832 }
833 else
834 {
835 switch (src.GetFileType())
836 {
837 case FileSpec::eFileTypeDirectory:
838 {
839 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000840 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000841 uint32_t permissions = src.GetPermissions();
842 if (permissions == 0)
843 permissions = eFilePermissionsDirectoryDefault;
Chaoren Lind3173f32015-05-29 19:52:29 +0000844 error = MakeDirectory(fixed_dst, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000845 if (error.Success())
846 {
847 // Make a filespec that only fills in the directory of a FileSpec so
848 // when we enumerate we can quickly fill in the filename for dst copies
849 FileSpec recurse_dst;
Chaoren Lind3173f32015-05-29 19:52:29 +0000850 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000851 std::string src_dir_path (src.GetPath());
852 RecurseCopyBaton baton = { recurse_dst, this, Error() };
853 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
854 return baton.error;
855 }
856 }
857 break;
858
859 case FileSpec::eFileTypeRegular:
860 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000861 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000862 error = PutFile(src, fixed_dst);
863 break;
864
865 case FileSpec::eFileTypeSymbolicLink:
866 {
867 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000868 Unlink(fixed_dst);
869 FileSpec src_resolved;
870 error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000871 if (error.Success())
Chaoren Lind3173f32015-05-29 19:52:29 +0000872 error = CreateSymlink(dst, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000873 }
874 break;
875 case FileSpec::eFileTypePipe:
876 error.SetErrorString("platform install doesn't handle pipes");
877 break;
878 case FileSpec::eFileTypeSocket:
879 error.SetErrorString("platform install doesn't handle sockets");
880 break;
881 case FileSpec::eFileTypeInvalid:
882 case FileSpec::eFileTypeUnknown:
883 case FileSpec::eFileTypeOther:
884 error.SetErrorString("platform install doesn't handle non file or directory items");
885 break;
886 }
887 }
888 return error;
889}
890
891bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000892Platform::SetWorkingDirectory(const FileSpec &file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000893{
894 if (IsHost())
895 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000896 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
897 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000898 log->Printf("Platform::SetWorkingDirectory('%s')",
899 file_spec.GetCString());
900 if (file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000901 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000902 if (::chdir(file_spec.GetCString()) == 0)
Greg Claytonfbb76342013-11-20 21:07:01 +0000903 return true;
904 }
905 return false;
906 }
907 else
908 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000909 m_working_dir.Clear();
Chaoren Lind3173f32015-05-29 19:52:29 +0000910 return SetRemoteWorkingDirectory(file_spec);
Greg Claytonfbb76342013-11-20 21:07:01 +0000911 }
912}
913
914Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000915Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000916{
917 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000918 return FileSystem::MakeDirectory(file_spec, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000919 else
920 {
921 Error error;
922 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
923 return error;
924 }
925}
926
927Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000928Platform::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000929{
930 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000931 return FileSystem::GetFilePermissions(file_spec, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000932 else
933 {
934 Error error;
935 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
936 return error;
937 }
938}
939
940Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000941Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000942{
943 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000944 return FileSystem::SetFilePermissions(file_spec, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000945 else
946 {
947 Error error;
948 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
949 return error;
950 }
951}
952
953ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000954Platform::GetName ()
955{
Greg Claytonfbb76342013-11-20 21:07:01 +0000956 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000957}
958
959const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000960Platform::GetHostname ()
961{
Greg Claytonab65b342011-04-13 22:47:15 +0000962 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000963 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000964
965 if (m_name.empty())
966 return NULL;
967 return m_name.c_str();
968}
969
Enrico Granata6e25aee2015-08-27 00:53:57 +0000970ConstString
971Platform::GetFullNameForDylib (ConstString basename)
972{
973 return basename;
974}
975
Greg Clayton5fb8f792013-12-02 19:35:49 +0000976bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000977Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir)
Greg Clayton5fb8f792013-12-02 19:35:49 +0000978{
979 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
980 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000981 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
982 working_dir.GetCString());
983 m_working_dir = working_dir;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000984 return true;
985}
986
Greg Clayton32e0a752011-03-30 18:16:51 +0000987const char *
988Platform::GetUserName (uint32_t uid)
989{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000990#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000991 const char *user_name = GetCachedUserName(uid);
992 if (user_name)
993 return user_name;
994 if (IsHost())
995 {
996 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000997 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000998 return SetCachedUserName (uid, name.c_str(), name.size());
999 }
Zachary Turnerb245eca2014-08-21 20:02:17 +00001000#endif
Greg Clayton1cb64962011-03-24 04:28:38 +00001001 return NULL;
1002}
1003
Greg Clayton32e0a752011-03-30 18:16:51 +00001004const char *
1005Platform::GetGroupName (uint32_t gid)
1006{
Zachary Turnerb245eca2014-08-21 20:02:17 +00001007#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +00001008 const char *group_name = GetCachedGroupName(gid);
1009 if (group_name)
1010 return group_name;
1011 if (IsHost())
1012 {
1013 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +00001014 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +00001015 return SetCachedGroupName (gid, name.c_str(), name.size());
1016 }
Zachary Turnerb245eca2014-08-21 20:02:17 +00001017#endif
Greg Clayton32e0a752011-03-30 18:16:51 +00001018 return NULL;
1019}
Greg Clayton1cb64962011-03-24 04:28:38 +00001020
Greg Claytonded470d2011-03-19 01:12:21 +00001021bool
1022Platform::SetOSVersion (uint32_t major,
1023 uint32_t minor,
1024 uint32_t update)
1025{
1026 if (IsHost())
1027 {
Zachary Turner97a14e62014-08-19 17:18:29 +00001028 // We don't need anyone setting the OS version for the host platform,
1029 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +00001030 return false;
1031 }
1032 else
1033 {
1034 // We have a remote platform, allow setting the target OS version if
1035 // we aren't connected, since if we are connected, we should be able to
1036 // request the remote OS version from the connected platform.
1037 if (IsConnected())
1038 return false;
1039 else
1040 {
1041 // We aren't connected and we might want to set the OS version
1042 // ahead of time before we connect so we can peruse files and
1043 // use a local SDK or PDK cache of support files to disassemble
1044 // or do other things.
1045 m_major_os_version = major;
1046 m_minor_os_version = minor;
1047 m_update_os_version = update;
1048 return true;
1049 }
1050 }
1051 return false;
1052}
1053
1054
Greg Claytone996fd32011-03-08 22:40:15 +00001055Error
Greg Clayton8012cad2014-11-17 19:39:20 +00001056Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001057 lldb::ModuleSP &exe_module_sp,
1058 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +00001059{
1060 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +00001061 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +00001062 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001063 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +00001064 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001065 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001066 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001067 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001068 NULL,
1069 NULL);
1070 }
1071 else
1072 {
1073 // No valid architecture was specified, ask the platform for
1074 // the architectures that we should be using (in the correct order)
1075 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +00001076 ModuleSpec arch_module_spec(module_spec);
1077 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +00001078 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001079 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001080 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001081 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001082 NULL,
1083 NULL);
1084 // Did we find an executable using one of the
1085 if (error.Success() && exe_module_sp)
1086 break;
1087 }
1088 }
1089 }
1090 else
1091 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001092 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +00001093 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +00001094 }
1095 return error;
1096}
1097
Greg Clayton103f0282012-09-12 02:03:59 +00001098Error
1099Platform::ResolveSymbolFile (Target &target,
1100 const ModuleSpec &sym_spec,
1101 FileSpec &sym_file)
1102{
1103 Error error;
1104 if (sym_spec.GetSymbolFileSpec().Exists())
1105 sym_file = sym_spec.GetSymbolFileSpec();
1106 else
1107 error.SetErrorString("unable to resolve symbol file");
1108 return error;
1109
1110}
1111
1112
1113
Greg Claytonaa516842011-08-11 16:25:18 +00001114bool
1115Platform::ResolveRemotePath (const FileSpec &platform_path,
1116 FileSpec &resolved_platform_path)
1117{
1118 resolved_platform_path = platform_path;
1119 return resolved_platform_path.ResolvePath();
1120}
1121
Greg Claytonded470d2011-03-19 01:12:21 +00001122
1123const ArchSpec &
1124Platform::GetSystemArchitecture()
1125{
1126 if (IsHost())
1127 {
1128 if (!m_system_arch.IsValid())
1129 {
1130 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +00001131 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +00001132 m_system_arch_set_while_connected = m_system_arch.IsValid();
1133 }
1134 }
1135 else
1136 {
1137 // We have a remote platform. We can only fetch the remote
1138 // system architecture if we are connected, and we don't want to do it
1139 // more than once.
1140
1141 const bool is_connected = IsConnected();
1142
1143 bool fetch = false;
1144 if (m_system_arch.IsValid())
1145 {
1146 // We have valid OS version info, check to make sure it wasn't
1147 // manually set prior to connecting. If it was manually set prior
1148 // to connecting, then lets fetch the actual OS version info
1149 // if we are now connected.
1150 if (is_connected && !m_system_arch_set_while_connected)
1151 fetch = true;
1152 }
1153 else
1154 {
1155 // We don't have valid OS version info, fetch it if we are connected
1156 fetch = is_connected;
1157 }
1158
1159 if (fetch)
1160 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001161 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001162 m_system_arch_set_while_connected = m_system_arch.IsValid();
1163 }
1164 }
1165 return m_system_arch;
1166}
1167
1168
Greg Claytone996fd32011-03-08 22:40:15 +00001169Error
Greg Claytond314e812011-03-23 00:09:55 +00001170Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001171{
1172 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001173 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001174 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001175 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001176 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001177 return error;
1178}
1179
1180Error
Greg Claytond314e812011-03-23 00:09:55 +00001181Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001182{
1183 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001184 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001185 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001186 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001187 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001188 return error;
1189}
Greg Clayton32e0a752011-03-30 18:16:51 +00001190
1191bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001192Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001193{
1194 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001195 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001196 if (IsHost())
1197 return Host::GetProcessInfo (pid, process_info);
1198 return false;
1199}
1200
1201uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001202Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1203 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001204{
Greg Clayton8b82f082011-04-12 05:54:46 +00001205 // Take care of the host case so that each subclass can just
1206 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001207 uint32_t match_count = 0;
1208 if (IsHost())
1209 match_count = Host::FindProcesses (match_info, process_infos);
1210 return match_count;
1211}
Greg Clayton8b82f082011-04-12 05:54:46 +00001212
1213
1214Error
1215Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1216{
1217 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001218 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1219 if (log)
1220 log->Printf ("Platform::%s()", __FUNCTION__);
1221
1222 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001223 // call this function to get the host functionality.
1224 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001225 {
1226 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1227 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1228
1229 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1230 {
1231 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001232 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1233 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001234 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001235 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001236 {
1237 const FileSpec &shell = launch_info.GetShell();
1238 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001239 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1240 __FUNCTION__,
1241 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001242 shell_str);
1243 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001244
Greg Claytond1cf11a2012-04-14 01:42:46 +00001245 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1246 is_localhost,
1247 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001248 first_arg_is_full_shell_command,
1249 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001250 return error;
1251 }
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001252 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments))
Enrico Granatad7a83a92015-02-10 03:06:24 +00001253 {
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001254 error = ShellExpandArguments(launch_info);
Enrico Granata83a14372015-02-20 21:48:38 +00001255 if (error.Fail())
Enrico Granatad7a83a92015-02-10 03:06:24 +00001256 return error;
Enrico Granatad7a83a92015-02-10 03:06:24 +00001257 }
Greg Clayton84db9102012-03-26 23:03:23 +00001258
Todd Fialaac33cc92014-10-09 01:02:08 +00001259 if (log)
1260 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1261
Greg Clayton8b82f082011-04-12 05:54:46 +00001262 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001263 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001264 else
1265 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1266 return error;
1267}
1268
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001269Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001270Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +00001271{
1272 if (IsHost())
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001273 return Host::ShellExpandArguments(launch_info);
1274 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001275}
1276
1277Error
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001278Platform::KillProcess (const lldb::pid_t pid)
1279{
1280 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1281 if (log)
1282 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
1283
Zachary Turner7271bab2015-05-13 19:44:24 +00001284 // Try to find a process plugin to handle this Kill request. If we can't, fall back to
1285 // the default OS implementation.
1286 size_t num_debuggers = Debugger::GetNumDebuggers();
1287 for (size_t didx = 0; didx < num_debuggers; ++didx)
1288 {
1289 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1290 lldb_private::TargetList &targets = debugger->GetTargetList();
1291 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx)
1292 {
1293 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1294 if (process->GetID() == pid)
1295 return process->Destroy(true);
1296 }
1297 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001298
Zachary Turner7271bab2015-05-13 19:44:24 +00001299 if (!IsHost())
1300 {
1301 return Error("base lldb_private::Platform class can't kill remote processes unless "
1302 "they are controlled by a process plugin");
1303 }
1304 Host::Kill(pid, SIGTERM);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001305 return Error();
1306}
1307
Greg Clayton8b82f082011-04-12 05:54:46 +00001308lldb::ProcessSP
1309Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1310 Debugger &debugger,
1311 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001312 Error &error)
1313{
Todd Fialaac33cc92014-10-09 01:02:08 +00001314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1315 if (log)
1316 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1317
Greg Clayton8b82f082011-04-12 05:54:46 +00001318 ProcessSP process_sp;
1319 // Make sure we stop at the entry point
1320 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001321 // We always launch the process we are going to debug in a separate process
1322 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1323 // about the target getting them as well.
1324 launch_info.SetLaunchInSeparateProcessGroup(true);
1325
Greg Clayton8b82f082011-04-12 05:54:46 +00001326 error = LaunchProcess (launch_info);
1327 if (error.Success())
1328 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001329 if (log)
1330 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001331 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001332 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001333 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001334 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001335 if (process_sp)
1336 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001337 if (log)
1338 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001339 launch_info.SetHijackListener(attach_info.GetHijackListener());
1340
Greg Claytone24c4ac2011-11-17 04:46:02 +00001341 // Since we attached to the process, it will think it needs to detach
1342 // if the process object just goes away without an explicit call to
1343 // Process::Kill() or Process::Detach(), so let it know to kill the
1344 // process if this happens.
1345 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001346
1347 // If we didn't have any file actions, the pseudo terminal might
1348 // have been used where the slave side was given as the file to
1349 // open for stdin/out/err after we have already opened the master
1350 // so we can read/write stdin/out/err.
1351 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1352 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1353 {
1354 process_sp->SetSTDIOFileDescriptor(pty_fd);
1355 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001356 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001357 else
1358 {
1359 if (log)
1360 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1361 }
1362 }
1363 else
1364 {
1365 if (log)
1366 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001367 }
1368 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001369 else
1370 {
1371 if (log)
1372 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1373 }
1374
Greg Clayton8b82f082011-04-12 05:54:46 +00001375 return process_sp;
1376}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001377
1378
1379lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001380Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001381{
1382 lldb::PlatformSP platform_sp;
1383 Error error;
1384 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001385 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001386 return platform_sp;
1387}
1388
1389
1390//------------------------------------------------------------------
1391/// Lets a platform answer if it is compatible with a given
1392/// architecture and the target triple contained within.
1393//------------------------------------------------------------------
1394bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001395Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001396{
1397 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001398 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001399 {
Greg Clayton70512312012-05-08 01:45:38 +00001400 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001401 // Try for an exact architecture match first.
1402 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001403 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001404 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001405 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001406 if (arch.IsExactMatch(platform_arch))
1407 {
1408 if (compatible_arch_ptr)
1409 *compatible_arch_ptr = platform_arch;
1410 return true;
1411 }
1412 }
1413 }
1414 else
1415 {
1416 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1417 {
1418 if (arch.IsCompatibleMatch(platform_arch))
1419 {
1420 if (compatible_arch_ptr)
1421 *compatible_arch_ptr = platform_arch;
1422 return true;
1423 }
Greg Clayton70512312012-05-08 01:45:38 +00001424 }
1425 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001426 }
Greg Clayton70512312012-05-08 01:45:38 +00001427 if (compatible_arch_ptr)
1428 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001429 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001430}
1431
Daniel Maleae0f8f572013-08-26 23:57:52 +00001432Error
1433Platform::PutFile (const FileSpec& source,
1434 const FileSpec& destination,
1435 uint32_t uid,
1436 uint32_t gid)
1437{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001438 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1439 if (log)
1440 log->Printf("[PutFile] Using block by block transfer....\n");
1441
Pavel Labath646b0642015-02-17 16:07:52 +00001442 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001443 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1444 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1445
1446 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1447 Error error;
1448 uint32_t permissions = source_file.GetPermissions(error);
1449 if (permissions == 0)
1450 permissions = lldb::eFilePermissionsFileDefault;
1451
1452 if (!source_file.IsValid())
1453 return Error("PutFile: unable to open source file");
1454 lldb::user_id_t dest_file = OpenFile (destination,
1455 File::eOpenOptionCanCreate |
1456 File::eOpenOptionWrite |
Pavel Labath646b0642015-02-17 16:07:52 +00001457 File::eOpenOptionTruncate |
1458 File::eOpenOptionCloseOnExec,
Vince Harron1b5a74e2015-01-21 22:42:49 +00001459 permissions,
1460 error);
1461 if (log)
1462 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1463
1464 if (error.Fail())
1465 return error;
1466 if (dest_file == UINT64_MAX)
1467 return Error("unable to open target file");
1468 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1469 uint64_t offset = 0;
1470 for (;;)
1471 {
1472 size_t bytes_read = buffer_sp->GetByteSize();
1473 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1474 if (error.Fail() || bytes_read == 0)
1475 break;
1476
1477 const uint64_t bytes_written = WriteFile(dest_file, offset,
1478 buffer_sp->GetBytes(), bytes_read, error);
1479 if (error.Fail())
1480 break;
1481
1482 offset += bytes_written;
1483 if (bytes_written != bytes_read)
1484 {
1485 // We didn't write the correct number of bytes, so adjust
1486 // the file position in the source file we are reading from...
1487 source_file.SeekFromStart(offset);
1488 }
1489 }
1490 CloseFile(dest_file, error);
1491
1492 if (uid == UINT32_MAX && gid == UINT32_MAX)
1493 return error;
1494
1495 // TODO: ChownFile?
1496
Daniel Maleae0f8f572013-08-26 23:57:52 +00001497 return error;
1498}
1499
1500Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001501Platform::GetFile(const FileSpec &source,
1502 const FileSpec &destination)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001503{
1504 Error error("unimplemented");
1505 return error;
1506}
1507
Greg Claytonfbb76342013-11-20 21:07:01 +00001508Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001509Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1510 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001511{
1512 Error error("unimplemented");
1513 return error;
1514}
1515
Daniel Maleae0f8f572013-08-26 23:57:52 +00001516bool
Chaoren Lind3173f32015-05-29 19:52:29 +00001517Platform::GetFileExists(const lldb_private::FileSpec &file_spec)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001518{
1519 return false;
1520}
1521
Greg Claytonfbb76342013-11-20 21:07:01 +00001522Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001523Platform::Unlink(const FileSpec &path)
Greg Claytonfbb76342013-11-20 21:07:01 +00001524{
1525 Error error("unimplemented");
1526 return error;
1527}
1528
Robert Flack96ad3de2015-05-09 15:53:31 +00001529uint64_t
Mohit K. Bhakkade0d8c422015-06-30 06:29:12 +00001530Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags)
Robert Flack96ad3de2015-05-09 15:53:31 +00001531{
1532 uint64_t flags_platform = 0;
1533 if (flags & eMmapFlagsPrivate)
1534 flags_platform |= MAP_PRIVATE;
1535 if (flags & eMmapFlagsAnon)
1536 flags_platform |= MAP_ANON;
1537 return flags_platform;
1538}
Greg Claytonfbb76342013-11-20 21:07:01 +00001539
Daniel Maleae0f8f572013-08-26 23:57:52 +00001540lldb_private::Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001541Platform::RunShellCommand(const char *command, // Shouldn't be NULL
1542 const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory
1543 int *status_ptr, // Pass NULL if you don't want the process exit status
1544 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1545 std::string *command_output, // Pass NULL if you don't want the command output
1546 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001547{
1548 if (IsHost())
1549 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1550 else
1551 return Error("unimplemented");
1552}
1553
1554
1555bool
1556Platform::CalculateMD5 (const FileSpec& file_spec,
1557 uint64_t &low,
1558 uint64_t &high)
1559{
1560 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001561 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001562 else
1563 return false;
1564}
1565
1566void
1567Platform::SetLocalCacheDirectory (const char* local)
1568{
1569 m_local_cache_directory.assign(local);
1570}
1571
1572const char*
1573Platform::GetLocalCacheDirectory ()
1574{
1575 return m_local_cache_directory.c_str();
1576}
1577
1578static OptionDefinition
1579g_rsync_option_table[] =
1580{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001581 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1582 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1583 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1584 { 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 +00001585};
1586
1587static OptionDefinition
1588g_ssh_option_table[] =
1589{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001590 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1591 { 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 +00001592};
1593
1594static OptionDefinition
1595g_caching_option_table[] =
1596{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001597 { 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 +00001598};
1599
1600OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1601{
1602}
1603
1604OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1605{
1606}
1607
1608const lldb_private::OptionDefinition*
1609OptionGroupPlatformRSync::GetDefinitions ()
1610{
1611 return g_rsync_option_table;
1612}
1613
1614void
1615OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1616{
1617 m_rsync = false;
1618 m_rsync_opts.clear();
1619 m_rsync_prefix.clear();
1620 m_ignores_remote_hostname = false;
1621}
1622
1623lldb_private::Error
1624OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1625 uint32_t option_idx,
1626 const char *option_arg)
1627{
1628 Error error;
1629 char short_option = (char) GetDefinitions()[option_idx].short_option;
1630 switch (short_option)
1631 {
1632 case 'r':
1633 m_rsync = true;
1634 break;
1635
1636 case 'R':
1637 m_rsync_opts.assign(option_arg);
1638 break;
1639
1640 case 'P':
1641 m_rsync_prefix.assign(option_arg);
1642 break;
1643
1644 case 'i':
1645 m_ignores_remote_hostname = true;
1646 break;
1647
1648 default:
1649 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1650 break;
1651 }
1652
1653 return error;
1654}
1655
1656uint32_t
1657OptionGroupPlatformRSync::GetNumDefinitions ()
1658{
1659 return llvm::array_lengthof(g_rsync_option_table);
1660}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001661
Greg Clayton4116e932012-05-15 02:33:01 +00001662lldb::BreakpointSP
1663Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1664{
1665 return lldb::BreakpointSP();
1666}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001667
Daniel Maleae0f8f572013-08-26 23:57:52 +00001668OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1669{
1670}
1671
1672OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1673{
1674}
1675
1676const lldb_private::OptionDefinition*
1677OptionGroupPlatformSSH::GetDefinitions ()
1678{
1679 return g_ssh_option_table;
1680}
1681
1682void
1683OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1684{
1685 m_ssh = false;
1686 m_ssh_opts.clear();
1687}
1688
1689lldb_private::Error
1690OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1691 uint32_t option_idx,
1692 const char *option_arg)
1693{
1694 Error error;
1695 char short_option = (char) GetDefinitions()[option_idx].short_option;
1696 switch (short_option)
1697 {
1698 case 's':
1699 m_ssh = true;
1700 break;
1701
1702 case 'S':
1703 m_ssh_opts.assign(option_arg);
1704 break;
1705
1706 default:
1707 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1708 break;
1709 }
1710
1711 return error;
1712}
1713
1714uint32_t
1715OptionGroupPlatformSSH::GetNumDefinitions ()
1716{
1717 return llvm::array_lengthof(g_ssh_option_table);
1718}
1719
1720OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1721{
1722}
1723
1724OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1725{
1726}
1727
1728const lldb_private::OptionDefinition*
1729OptionGroupPlatformCaching::GetDefinitions ()
1730{
1731 return g_caching_option_table;
1732}
1733
1734void
1735OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1736{
1737 m_cache_dir.clear();
1738}
1739
1740lldb_private::Error
1741OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1742 uint32_t option_idx,
1743 const char *option_arg)
1744{
1745 Error error;
1746 char short_option = (char) GetDefinitions()[option_idx].short_option;
1747 switch (short_option)
1748 {
1749 case 'c':
1750 m_cache_dir.assign(option_arg);
1751 break;
1752
1753 default:
1754 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1755 break;
1756 }
1757
1758 return error;
1759}
1760
1761uint32_t
1762OptionGroupPlatformCaching::GetNumDefinitions ()
1763{
1764 return llvm::array_lengthof(g_caching_option_table);
1765}
1766
Greg Clayton67cc0632012-08-22 17:17:09 +00001767size_t
1768Platform::GetEnvironment (StringList &environment)
1769{
1770 environment.Clear();
1771 return false;
1772}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001773
1774const std::vector<ConstString> &
1775Platform::GetTrapHandlerSymbolNames ()
1776{
1777 if (!m_calculated_trap_handlers)
1778 {
Greg Clayton7597b352015-02-02 20:45:17 +00001779 Mutex::Locker locker (m_mutex);
Jason Molenda4da87062014-05-23 23:11:27 +00001780 if (!m_calculated_trap_handlers)
1781 {
1782 CalculateTrapHandlerSymbolNames();
1783 m_calculated_trap_handlers = true;
1784 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001785 }
1786 return m_trap_handlers;
1787}
1788
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001789Error
1790Platform::GetCachedExecutable (ModuleSpec &module_spec,
1791 lldb::ModuleSP &module_sp,
1792 const FileSpecList *module_search_paths_ptr,
1793 Platform &remote_platform)
1794{
1795 const auto platform_spec = module_spec.GetFileSpec ();
1796 const auto error = LoadCachedExecutable (module_spec,
1797 module_sp,
1798 module_search_paths_ptr,
1799 remote_platform);
1800 if (error.Success ())
1801 {
1802 module_spec.GetFileSpec () = module_sp->GetFileSpec ();
1803 module_spec.GetPlatformFileSpec () = platform_spec;
1804 }
1805
1806 return error;
1807}
1808
1809Error
1810Platform::LoadCachedExecutable (const ModuleSpec &module_spec,
1811 lldb::ModuleSP &module_sp,
1812 const FileSpecList *module_search_paths_ptr,
1813 Platform &remote_platform)
1814{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001815 return GetRemoteSharedModule (module_spec,
1816 nullptr,
1817 module_sp,
1818 [&](const ModuleSpec &spec)
1819 {
1820 return remote_platform.ResolveExecutable (
1821 spec, module_sp, module_search_paths_ptr);
1822 },
1823 nullptr);
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001824}
1825
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001826Error
1827Platform::GetRemoteSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001828 Process* process,
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001829 lldb::ModuleSP &module_sp,
1830 const ModuleResolver &module_resolver,
1831 bool *did_create_ptr)
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001832{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001833 // Get module information from a target.
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001834 ModuleSpec resolved_module_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001835 bool got_module_spec = false;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001836 if (process)
1837 {
1838 // Try to get module information from the process
1839 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001840 got_module_spec = true;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001841 }
1842
1843 if (!got_module_spec)
1844 {
1845 // Get module information from a target.
1846 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001847 return module_resolver (module_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001848 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001849
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001850 // Trying to find a module by UUID on local file system.
1851 const auto error = module_resolver (resolved_module_spec);
1852 if (error.Fail ())
1853 {
1854 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr))
1855 return Error ();
1856 }
1857
1858 return error;
1859}
1860
1861bool
1862Platform::GetCachedSharedModule (const ModuleSpec &module_spec,
1863 lldb::ModuleSP &module_sp,
1864 bool *did_create_ptr)
1865{
1866 if (IsHost() ||
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +00001867 !GetGlobalPlatformProperties ()->GetUseModuleCache () ||
1868 !GetGlobalPlatformProperties ()->GetModuleCacheDirectory ())
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001869 return false;
1870
1871 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
1872
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001873 // Check local cache for a module.
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001874 auto error = m_module_cache->GetAndPut (
1875 GetModuleCacheRoot (),
1876 GetCacheHostname (),
1877 module_spec,
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001878 [this](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec)
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001879 {
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001880 return DownloadModuleSlice (module_spec.GetFileSpec (),
1881 module_spec.GetObjectOffset (),
1882 module_spec.GetObjectSize (),
1883 tmp_download_file_spec);
1884
1885 },
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001886 [this](const ModuleSP& module_sp, const FileSpec& tmp_download_file_spec)
1887 {
1888 return DownloadSymbolFile (module_sp, tmp_download_file_spec);
1889 },
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001890 module_sp,
1891 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001892 if (error.Success ())
1893 return true;
1894
1895 if (log)
1896 log->Printf("Platform::%s - module %s not found in local cache: %s",
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001897 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001898 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001899}
1900
1901Error
1902Platform::DownloadModuleSlice (const FileSpec& src_file_spec,
1903 const uint64_t src_offset,
1904 const uint64_t src_size,
1905 const FileSpec& dst_file_spec)
1906{
1907 Error error;
1908
1909 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1910 if (!dst.is_open())
1911 {
1912 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ());
1913 return error;
1914 }
1915
1916 auto src_fd = OpenFile (src_file_spec,
1917 File::eOpenOptionRead,
1918 lldb::eFilePermissionsFileDefault,
1919 error);
1920
1921 if (error.Fail ())
1922 {
1923 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ());
1924 return error;
1925 }
1926
1927 std::vector<char> buffer (1024);
1928 auto offset = src_offset;
1929 uint64_t total_bytes_read = 0;
1930 while (total_bytes_read < src_size)
1931 {
1932 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read);
1933 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error);
1934 if (error.Fail ())
1935 break;
1936 if (n_read == 0)
1937 {
1938 error.SetErrorString ("read 0 bytes");
1939 break;
1940 }
1941 offset += n_read;
1942 total_bytes_read += n_read;
1943 dst.write (&buffer[0], n_read);
1944 }
1945
1946 Error close_error;
1947 CloseFile (src_fd, close_error); // Ignoring close error.
1948
1949 return error;
1950}
1951
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001952Error
1953Platform::DownloadSymbolFile (const lldb::ModuleSP& module_sp, const FileSpec& dst_file_spec)
1954{
1955 return Error ("Symbol file downloading not supported by the default platform.");
1956}
1957
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001958FileSpec
1959Platform::GetModuleCacheRoot ()
1960{
1961 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory ();
1962 dir_spec.AppendPathComponent (GetName ().AsCString ());
1963 return dir_spec;
1964}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001965
1966const char *
1967Platform::GetCacheHostname ()
1968{
1969 return GetHostname ();
1970}
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001971
1972const UnixSignalsSP &
1973Platform::GetRemoteUnixSignals()
1974{
1975 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1976 return s_default_unix_signals_sp;
1977}
1978
1979const UnixSignalsSP &
1980Platform::GetUnixSignals()
1981{
1982 if (IsHost())
1983 return Host::GetUnixSignals();
1984 return GetRemoteUnixSignals();
1985}
Tamas Berghammer3cb132a2015-12-02 11:58:51 +00001986
1987uint32_t
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00001988Platform::LoadImage(lldb_private::Process* process,
1989 const lldb_private::FileSpec& local_file,
1990 const lldb_private::FileSpec& remote_file,
1991 lldb_private::Error& error)
1992{
1993 if (local_file && remote_file)
1994 {
1995 // Both local and remote file was specified. Install the local file to the given location.
1996 if (IsRemote() || local_file != remote_file)
1997 {
1998 error = Install(local_file, remote_file);
1999 if (error.Fail())
2000 return LLDB_INVALID_IMAGE_TOKEN;
2001 }
2002 return DoLoadImage(process, remote_file, error);
2003 }
2004
2005 if (local_file)
2006 {
2007 // Only local file was specified. Install it to the current working directory.
2008 FileSpec target_file = GetWorkingDirectory();
2009 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
2010 if (IsRemote() || local_file != target_file)
2011 {
2012 error = Install(local_file, target_file);
2013 if (error.Fail())
2014 return LLDB_INVALID_IMAGE_TOKEN;
2015 }
2016 return DoLoadImage(process, target_file, error);
2017 }
2018
2019 if (remote_file)
2020 {
2021 // Only remote file was specified so we don't have to do any copying
2022 return DoLoadImage(process, remote_file, error);
2023 }
2024
2025 error.SetErrorString("Neither local nor remote file was specified");
2026 return LLDB_INVALID_IMAGE_TOKEN;
2027}
2028
2029uint32_t
2030Platform::DoLoadImage (lldb_private::Process* process,
2031 const lldb_private::FileSpec& remote_file,
2032 lldb_private::Error& error)
Tamas Berghammer3cb132a2015-12-02 11:58:51 +00002033{
2034 error.SetErrorString("LoadImage is not supported on the current platform");
2035 return LLDB_INVALID_IMAGE_TOKEN;
2036}
2037
2038Error
2039Platform::UnloadImage(lldb_private::Process* process, uint32_t image_token)
2040{
Tamas Berghammer4fbd67a2015-12-08 13:43:59 +00002041 return Error("UnloadImage is not supported on the current platform");
Tamas Berghammer3cb132a2015-12-02 11:58:51 +00002042}