blob: 33446a9fb3acf7d7126656f0f9bc8f3323c6a88e [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
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +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,
531 uint32_t &update)
532{
Greg Clayton7597b352015-02-02 20:45:17 +0000533 Mutex::Locker locker (m_mutex);
534
Greg Claytonded470d2011-03-19 01:12:21 +0000535 bool success = m_major_os_version != UINT32_MAX;
536 if (IsHost())
537 {
538 if (!success)
539 {
540 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000541 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000542 m_os_version_set_while_connected = success;
543 }
544 }
545 else
546 {
547 // We have a remote platform. We can only fetch the remote
548 // OS version if we are connected, and we don't want to do it
549 // more than once.
550
551 const bool is_connected = IsConnected();
552
Greg Clayton1cb64962011-03-24 04:28:38 +0000553 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000554 if (success)
555 {
556 // We have valid OS version info, check to make sure it wasn't
557 // manually set prior to connecting. If it was manually set prior
558 // to connecting, then lets fetch the actual OS version info
559 // if we are now connected.
560 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000561 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000562 }
563 else
564 {
565 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000566 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000567 }
568
Greg Clayton1cb64962011-03-24 04:28:38 +0000569 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000570 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000571 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000572 m_os_version_set_while_connected = success;
573 }
574 }
575
576 if (success)
577 {
578 major = m_major_os_version;
579 minor = m_minor_os_version;
580 update = m_update_os_version;
581 }
582 return success;
583}
Greg Clayton1cb64962011-03-24 04:28:38 +0000584
585bool
586Platform::GetOSBuildString (std::string &s)
587{
Zachary Turner97a14e62014-08-19 17:18:29 +0000588 s.clear();
589
Greg Clayton1cb64962011-03-24 04:28:38 +0000590 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000591#if !defined(__linux__)
592 return HostInfo::GetOSBuildString(s);
593#else
594 return false;
595#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000596 else
597 return GetRemoteOSBuildString (s);
598}
599
600bool
601Platform::GetOSKernelDescription (std::string &s)
602{
603 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000604#if !defined(__linux__)
605 return HostInfo::GetOSKernelDescription(s);
606#else
607 return false;
608#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000609 else
610 return GetRemoteOSKernelDescription (s);
611}
612
Sean Callanan5dc29812014-12-05 01:16:31 +0000613void
Greg Claytoncd6bbba2015-01-22 18:25:49 +0000614Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options)
Sean Callanan5dc29812014-12-05 01:16:31 +0000615{
616 std::vector<std::string> default_compilation_options =
617 {
618 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"
619 };
620
621 options.insert(options.end(),
622 default_compilation_options.begin(),
623 default_compilation_options.end());
624}
625
626
Chaoren Lind3173f32015-05-29 19:52:29 +0000627FileSpec
Greg Claytonfbb76342013-11-20 21:07:01 +0000628Platform::GetWorkingDirectory ()
629{
630 if (IsHost())
631 {
632 char cwd[PATH_MAX];
633 if (getcwd(cwd, sizeof(cwd)))
Chaoren Lind3173f32015-05-29 19:52:29 +0000634 return FileSpec{cwd, true};
Greg Claytonfbb76342013-11-20 21:07:01 +0000635 else
Chaoren Lind3173f32015-05-29 19:52:29 +0000636 return FileSpec{};
Greg Claytonfbb76342013-11-20 21:07:01 +0000637 }
638 else
639 {
640 if (!m_working_dir)
641 m_working_dir = GetRemoteWorkingDirectory();
642 return m_working_dir;
643 }
644}
645
646
647struct RecurseCopyBaton
648{
649 const FileSpec& dst;
650 Platform *platform_ptr;
651 Error error;
652};
653
654
655static FileSpec::EnumerateDirectoryResult
656RecurseCopy_Callback (void *baton,
657 FileSpec::FileType file_type,
658 const FileSpec &src)
659{
660 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
661 switch (file_type)
662 {
663 case FileSpec::eFileTypePipe:
664 case FileSpec::eFileTypeSocket:
665 // we have no way to copy pipes and sockets - ignore them and continue
666 return FileSpec::eEnumerateDirectoryResultNext;
667 break;
668
669 case FileSpec::eFileTypeDirectory:
670 {
671 // make the new directory and get in there
672 FileSpec dst_dir = rc_baton->dst;
673 if (!dst_dir.GetFilename())
674 dst_dir.GetFilename() = src.GetLastPathComponent();
Chaoren Lind3173f32015-05-29 19:52:29 +0000675 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault);
Greg Claytonfbb76342013-11-20 21:07:01 +0000676 if (error.Fail())
677 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000678 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end",
679 dst_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000680 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
681 }
682
683 // now recurse
684 std::string src_dir_path (src.GetPath());
685
686 // Make a filespec that only fills in the directory of a FileSpec so
687 // when we enumerate we can quickly fill in the filename for dst copies
688 FileSpec recurse_dst;
689 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
690 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
691 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
692 if (rc_baton2.error.Fail())
693 {
694 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
695 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
696 }
697 return FileSpec::eEnumerateDirectoryResultNext;
698 }
699 break;
700
701 case FileSpec::eFileTypeSymbolicLink:
702 {
703 // copy the file and keep going
704 FileSpec dst_file = rc_baton->dst;
705 if (!dst_file.GetFilename())
706 dst_file.GetFilename() = src.GetFilename();
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000707
Chaoren Lind3173f32015-05-29 19:52:29 +0000708 FileSpec src_resolved;
709
710 rc_baton->error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000711
712 if (rc_baton->error.Fail())
713 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Chaoren Lind3173f32015-05-29 19:52:29 +0000714
715 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000716
717 if (rc_baton->error.Fail())
718 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
719
720 return FileSpec::eEnumerateDirectoryResultNext;
721 }
722 break;
723 case FileSpec::eFileTypeRegular:
724 {
725 // copy the file and keep going
726 FileSpec dst_file = rc_baton->dst;
727 if (!dst_file.GetFilename())
728 dst_file.GetFilename() = src.GetFilename();
729 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
730 if (err.Fail())
731 {
732 rc_baton->error.SetErrorString(err.AsCString());
733 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
734 }
735 return FileSpec::eEnumerateDirectoryResultNext;
736 }
737 break;
738
739 case FileSpec::eFileTypeInvalid:
740 case FileSpec::eFileTypeOther:
741 case FileSpec::eFileTypeUnknown:
742 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
743 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
744 break;
745 }
David Majnemer8faf9372014-09-16 06:34:29 +0000746 llvm_unreachable("Unhandled FileSpec::FileType!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000747}
748
749Error
750Platform::Install (const FileSpec& src, const FileSpec& dst)
751{
752 Error error;
753
754 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
755 if (log)
756 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
757 FileSpec fixed_dst(dst);
758
759 if (!fixed_dst.GetFilename())
760 fixed_dst.GetFilename() = src.GetFilename();
761
Chaoren Lind3173f32015-05-29 19:52:29 +0000762 FileSpec working_dir = GetWorkingDirectory();
Greg Claytonfbb76342013-11-20 21:07:01 +0000763
764 if (dst)
765 {
766 if (dst.GetDirectory())
767 {
768 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
769 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
770 {
771 fixed_dst.GetDirectory() = dst.GetDirectory();
772 }
773 // If the fixed destination file doesn't have a directory yet,
774 // then we must have a relative path. We will resolve this relative
775 // path against the platform's working directory
776 if (!fixed_dst.GetDirectory())
777 {
778 FileSpec relative_spec;
779 std::string path;
780 if (working_dir)
781 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000782 relative_spec = working_dir;
783 relative_spec.AppendPathComponent(dst.GetPath());
Greg Claytonfbb76342013-11-20 21:07:01 +0000784 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
785 }
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 for relative path '%s'", dst.GetPath().c_str());
802 return error;
803 }
804 }
805 }
806 else
807 {
808 if (working_dir)
809 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000810 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000811 }
812 else
813 {
814 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
815 return error;
816 }
817 }
818
819 if (log)
820 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
821
822 if (GetSupportsRSync())
823 {
824 error = PutFile(src, dst);
825 }
826 else
827 {
828 switch (src.GetFileType())
829 {
830 case FileSpec::eFileTypeDirectory:
831 {
832 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000833 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000834 uint32_t permissions = src.GetPermissions();
835 if (permissions == 0)
836 permissions = eFilePermissionsDirectoryDefault;
Chaoren Lind3173f32015-05-29 19:52:29 +0000837 error = MakeDirectory(fixed_dst, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000838 if (error.Success())
839 {
840 // Make a filespec that only fills in the directory of a FileSpec so
841 // when we enumerate we can quickly fill in the filename for dst copies
842 FileSpec recurse_dst;
Chaoren Lind3173f32015-05-29 19:52:29 +0000843 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000844 std::string src_dir_path (src.GetPath());
845 RecurseCopyBaton baton = { recurse_dst, this, Error() };
846 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
847 return baton.error;
848 }
849 }
850 break;
851
852 case FileSpec::eFileTypeRegular:
853 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000854 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000855 error = PutFile(src, fixed_dst);
856 break;
857
858 case FileSpec::eFileTypeSymbolicLink:
859 {
860 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000861 Unlink(fixed_dst);
862 FileSpec src_resolved;
863 error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000864 if (error.Success())
Chaoren Lind3173f32015-05-29 19:52:29 +0000865 error = CreateSymlink(dst, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000866 }
867 break;
868 case FileSpec::eFileTypePipe:
869 error.SetErrorString("platform install doesn't handle pipes");
870 break;
871 case FileSpec::eFileTypeSocket:
872 error.SetErrorString("platform install doesn't handle sockets");
873 break;
874 case FileSpec::eFileTypeInvalid:
875 case FileSpec::eFileTypeUnknown:
876 case FileSpec::eFileTypeOther:
877 error.SetErrorString("platform install doesn't handle non file or directory items");
878 break;
879 }
880 }
881 return error;
882}
883
884bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000885Platform::SetWorkingDirectory(const FileSpec &file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000886{
887 if (IsHost())
888 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000889 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
890 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000891 log->Printf("Platform::SetWorkingDirectory('%s')",
892 file_spec.GetCString());
893 if (file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000894 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000895 if (::chdir(file_spec.GetCString()) == 0)
Greg Claytonfbb76342013-11-20 21:07:01 +0000896 return true;
897 }
898 return false;
899 }
900 else
901 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000902 m_working_dir.Clear();
Chaoren Lind3173f32015-05-29 19:52:29 +0000903 return SetRemoteWorkingDirectory(file_spec);
Greg Claytonfbb76342013-11-20 21:07:01 +0000904 }
905}
906
907Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000908Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000909{
910 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000911 return FileSystem::MakeDirectory(file_spec, 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::GetFilePermissions(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::GetFilePermissions(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
933Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000934Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000935{
936 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000937 return FileSystem::SetFilePermissions(file_spec, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000938 else
939 {
940 Error error;
941 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
942 return error;
943 }
944}
945
946ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000947Platform::GetName ()
948{
Greg Claytonfbb76342013-11-20 21:07:01 +0000949 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000950}
951
952const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000953Platform::GetHostname ()
954{
Greg Claytonab65b342011-04-13 22:47:15 +0000955 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000956 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000957
958 if (m_name.empty())
959 return NULL;
960 return m_name.c_str();
961}
962
Enrico Granata6e25aee2015-08-27 00:53:57 +0000963ConstString
964Platform::GetFullNameForDylib (ConstString basename)
965{
966 return basename;
967}
968
Greg Clayton5fb8f792013-12-02 19:35:49 +0000969bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000970Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir)
Greg Clayton5fb8f792013-12-02 19:35:49 +0000971{
972 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
973 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000974 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
975 working_dir.GetCString());
976 m_working_dir = working_dir;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000977 return true;
978}
979
Greg Clayton32e0a752011-03-30 18:16:51 +0000980const char *
981Platform::GetUserName (uint32_t uid)
982{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000983#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000984 const char *user_name = GetCachedUserName(uid);
985 if (user_name)
986 return user_name;
987 if (IsHost())
988 {
989 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000990 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000991 return SetCachedUserName (uid, name.c_str(), name.size());
992 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000993#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000994 return NULL;
995}
996
Greg Clayton32e0a752011-03-30 18:16:51 +0000997const char *
998Platform::GetGroupName (uint32_t gid)
999{
Zachary Turnerb245eca2014-08-21 20:02:17 +00001000#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +00001001 const char *group_name = GetCachedGroupName(gid);
1002 if (group_name)
1003 return group_name;
1004 if (IsHost())
1005 {
1006 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +00001007 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +00001008 return SetCachedGroupName (gid, name.c_str(), name.size());
1009 }
Zachary Turnerb245eca2014-08-21 20:02:17 +00001010#endif
Greg Clayton32e0a752011-03-30 18:16:51 +00001011 return NULL;
1012}
Greg Clayton1cb64962011-03-24 04:28:38 +00001013
Greg Claytonded470d2011-03-19 01:12:21 +00001014bool
1015Platform::SetOSVersion (uint32_t major,
1016 uint32_t minor,
1017 uint32_t update)
1018{
1019 if (IsHost())
1020 {
Zachary Turner97a14e62014-08-19 17:18:29 +00001021 // We don't need anyone setting the OS version for the host platform,
1022 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +00001023 return false;
1024 }
1025 else
1026 {
1027 // We have a remote platform, allow setting the target OS version if
1028 // we aren't connected, since if we are connected, we should be able to
1029 // request the remote OS version from the connected platform.
1030 if (IsConnected())
1031 return false;
1032 else
1033 {
1034 // We aren't connected and we might want to set the OS version
1035 // ahead of time before we connect so we can peruse files and
1036 // use a local SDK or PDK cache of support files to disassemble
1037 // or do other things.
1038 m_major_os_version = major;
1039 m_minor_os_version = minor;
1040 m_update_os_version = update;
1041 return true;
1042 }
1043 }
1044 return false;
1045}
1046
1047
Greg Claytone996fd32011-03-08 22:40:15 +00001048Error
Greg Clayton8012cad2014-11-17 19:39:20 +00001049Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001050 lldb::ModuleSP &exe_module_sp,
1051 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +00001052{
1053 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +00001054 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +00001055 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001056 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +00001057 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001058 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001059 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001060 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001061 NULL,
1062 NULL);
1063 }
1064 else
1065 {
1066 // No valid architecture was specified, ask the platform for
1067 // the architectures that we should be using (in the correct order)
1068 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +00001069 ModuleSpec arch_module_spec(module_spec);
1070 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +00001071 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001072 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001073 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001074 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001075 NULL,
1076 NULL);
1077 // Did we find an executable using one of the
1078 if (error.Success() && exe_module_sp)
1079 break;
1080 }
1081 }
1082 }
1083 else
1084 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001085 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +00001086 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +00001087 }
1088 return error;
1089}
1090
Greg Clayton103f0282012-09-12 02:03:59 +00001091Error
1092Platform::ResolveSymbolFile (Target &target,
1093 const ModuleSpec &sym_spec,
1094 FileSpec &sym_file)
1095{
1096 Error error;
1097 if (sym_spec.GetSymbolFileSpec().Exists())
1098 sym_file = sym_spec.GetSymbolFileSpec();
1099 else
1100 error.SetErrorString("unable to resolve symbol file");
1101 return error;
1102
1103}
1104
1105
1106
Greg Claytonaa516842011-08-11 16:25:18 +00001107bool
1108Platform::ResolveRemotePath (const FileSpec &platform_path,
1109 FileSpec &resolved_platform_path)
1110{
1111 resolved_platform_path = platform_path;
1112 return resolved_platform_path.ResolvePath();
1113}
1114
Greg Claytonded470d2011-03-19 01:12:21 +00001115
1116const ArchSpec &
1117Platform::GetSystemArchitecture()
1118{
1119 if (IsHost())
1120 {
1121 if (!m_system_arch.IsValid())
1122 {
1123 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +00001124 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +00001125 m_system_arch_set_while_connected = m_system_arch.IsValid();
1126 }
1127 }
1128 else
1129 {
1130 // We have a remote platform. We can only fetch the remote
1131 // system architecture if we are connected, and we don't want to do it
1132 // more than once.
1133
1134 const bool is_connected = IsConnected();
1135
1136 bool fetch = false;
1137 if (m_system_arch.IsValid())
1138 {
1139 // We have valid OS version info, check to make sure it wasn't
1140 // manually set prior to connecting. If it was manually set prior
1141 // to connecting, then lets fetch the actual OS version info
1142 // if we are now connected.
1143 if (is_connected && !m_system_arch_set_while_connected)
1144 fetch = true;
1145 }
1146 else
1147 {
1148 // We don't have valid OS version info, fetch it if we are connected
1149 fetch = is_connected;
1150 }
1151
1152 if (fetch)
1153 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001154 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001155 m_system_arch_set_while_connected = m_system_arch.IsValid();
1156 }
1157 }
1158 return m_system_arch;
1159}
1160
1161
Greg Claytone996fd32011-03-08 22:40:15 +00001162Error
Greg Claytond314e812011-03-23 00:09:55 +00001163Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001164{
1165 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001166 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001167 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001168 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001169 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001170 return error;
1171}
1172
1173Error
Greg Claytond314e812011-03-23 00:09:55 +00001174Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001175{
1176 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001177 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001178 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001179 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001180 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001181 return error;
1182}
Greg Clayton32e0a752011-03-30 18:16:51 +00001183
1184bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001185Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001186{
1187 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001188 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001189 if (IsHost())
1190 return Host::GetProcessInfo (pid, process_info);
1191 return false;
1192}
1193
1194uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001195Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1196 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001197{
Greg Clayton8b82f082011-04-12 05:54:46 +00001198 // Take care of the host case so that each subclass can just
1199 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001200 uint32_t match_count = 0;
1201 if (IsHost())
1202 match_count = Host::FindProcesses (match_info, process_infos);
1203 return match_count;
1204}
Greg Clayton8b82f082011-04-12 05:54:46 +00001205
1206
1207Error
1208Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1209{
1210 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001211 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1212 if (log)
1213 log->Printf ("Platform::%s()", __FUNCTION__);
1214
1215 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001216 // call this function to get the host functionality.
1217 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001218 {
1219 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1220 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1221
1222 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1223 {
1224 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001225 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1226 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001227 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001228 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001229 {
1230 const FileSpec &shell = launch_info.GetShell();
1231 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001232 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1233 __FUNCTION__,
1234 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001235 shell_str);
1236 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001237
Greg Claytond1cf11a2012-04-14 01:42:46 +00001238 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1239 is_localhost,
1240 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001241 first_arg_is_full_shell_command,
1242 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001243 return error;
1244 }
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001245 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments))
Enrico Granatad7a83a92015-02-10 03:06:24 +00001246 {
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001247 error = ShellExpandArguments(launch_info);
Enrico Granata83a14372015-02-20 21:48:38 +00001248 if (error.Fail())
Enrico Granatad7a83a92015-02-10 03:06:24 +00001249 return error;
Enrico Granatad7a83a92015-02-10 03:06:24 +00001250 }
Greg Clayton84db9102012-03-26 23:03:23 +00001251
Todd Fialaac33cc92014-10-09 01:02:08 +00001252 if (log)
1253 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1254
Greg Clayton8b82f082011-04-12 05:54:46 +00001255 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001256 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001257 else
1258 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1259 return error;
1260}
1261
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001262Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001263Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +00001264{
1265 if (IsHost())
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001266 return Host::ShellExpandArguments(launch_info);
1267 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001268}
1269
1270Error
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001271Platform::KillProcess (const lldb::pid_t pid)
1272{
1273 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1274 if (log)
1275 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
1276
Zachary Turner7271bab2015-05-13 19:44:24 +00001277 // Try to find a process plugin to handle this Kill request. If we can't, fall back to
1278 // the default OS implementation.
1279 size_t num_debuggers = Debugger::GetNumDebuggers();
1280 for (size_t didx = 0; didx < num_debuggers; ++didx)
1281 {
1282 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1283 lldb_private::TargetList &targets = debugger->GetTargetList();
1284 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx)
1285 {
1286 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1287 if (process->GetID() == pid)
1288 return process->Destroy(true);
1289 }
1290 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001291
Zachary Turner7271bab2015-05-13 19:44:24 +00001292 if (!IsHost())
1293 {
1294 return Error("base lldb_private::Platform class can't kill remote processes unless "
1295 "they are controlled by a process plugin");
1296 }
1297 Host::Kill(pid, SIGTERM);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001298 return Error();
1299}
1300
Greg Clayton8b82f082011-04-12 05:54:46 +00001301lldb::ProcessSP
1302Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1303 Debugger &debugger,
1304 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001305 Error &error)
1306{
Todd Fialaac33cc92014-10-09 01:02:08 +00001307 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1308 if (log)
1309 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1310
Greg Clayton8b82f082011-04-12 05:54:46 +00001311 ProcessSP process_sp;
1312 // Make sure we stop at the entry point
1313 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001314 // We always launch the process we are going to debug in a separate process
1315 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1316 // about the target getting them as well.
1317 launch_info.SetLaunchInSeparateProcessGroup(true);
1318
Greg Clayton8b82f082011-04-12 05:54:46 +00001319 error = LaunchProcess (launch_info);
1320 if (error.Success())
1321 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001322 if (log)
1323 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001324 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001325 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001326 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001327 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001328 if (process_sp)
1329 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001330 if (log)
1331 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001332 launch_info.SetHijackListener(attach_info.GetHijackListener());
1333
Greg Claytone24c4ac2011-11-17 04:46:02 +00001334 // Since we attached to the process, it will think it needs to detach
1335 // if the process object just goes away without an explicit call to
1336 // Process::Kill() or Process::Detach(), so let it know to kill the
1337 // process if this happens.
1338 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001339
1340 // If we didn't have any file actions, the pseudo terminal might
1341 // have been used where the slave side was given as the file to
1342 // open for stdin/out/err after we have already opened the master
1343 // so we can read/write stdin/out/err.
1344 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1345 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1346 {
1347 process_sp->SetSTDIOFileDescriptor(pty_fd);
1348 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001349 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001350 else
1351 {
1352 if (log)
1353 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1354 }
1355 }
1356 else
1357 {
1358 if (log)
1359 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001360 }
1361 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001362 else
1363 {
1364 if (log)
1365 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1366 }
1367
Greg Clayton8b82f082011-04-12 05:54:46 +00001368 return process_sp;
1369}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001370
1371
1372lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001373Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001374{
1375 lldb::PlatformSP platform_sp;
1376 Error error;
1377 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001378 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001379 return platform_sp;
1380}
1381
1382
1383//------------------------------------------------------------------
1384/// Lets a platform answer if it is compatible with a given
1385/// architecture and the target triple contained within.
1386//------------------------------------------------------------------
1387bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001388Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001389{
1390 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001391 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001392 {
Greg Clayton70512312012-05-08 01:45:38 +00001393 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001394 // Try for an exact architecture match first.
1395 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001396 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001397 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001398 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001399 if (arch.IsExactMatch(platform_arch))
1400 {
1401 if (compatible_arch_ptr)
1402 *compatible_arch_ptr = platform_arch;
1403 return true;
1404 }
1405 }
1406 }
1407 else
1408 {
1409 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1410 {
1411 if (arch.IsCompatibleMatch(platform_arch))
1412 {
1413 if (compatible_arch_ptr)
1414 *compatible_arch_ptr = platform_arch;
1415 return true;
1416 }
Greg Clayton70512312012-05-08 01:45:38 +00001417 }
1418 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001419 }
Greg Clayton70512312012-05-08 01:45:38 +00001420 if (compatible_arch_ptr)
1421 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001422 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001423}
1424
Daniel Maleae0f8f572013-08-26 23:57:52 +00001425Error
1426Platform::PutFile (const FileSpec& source,
1427 const FileSpec& destination,
1428 uint32_t uid,
1429 uint32_t gid)
1430{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001431 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1432 if (log)
1433 log->Printf("[PutFile] Using block by block transfer....\n");
1434
Pavel Labath646b0642015-02-17 16:07:52 +00001435 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001436 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1437 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1438
1439 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1440 Error error;
1441 uint32_t permissions = source_file.GetPermissions(error);
1442 if (permissions == 0)
1443 permissions = lldb::eFilePermissionsFileDefault;
1444
1445 if (!source_file.IsValid())
1446 return Error("PutFile: unable to open source file");
1447 lldb::user_id_t dest_file = OpenFile (destination,
1448 File::eOpenOptionCanCreate |
1449 File::eOpenOptionWrite |
Pavel Labath646b0642015-02-17 16:07:52 +00001450 File::eOpenOptionTruncate |
1451 File::eOpenOptionCloseOnExec,
Vince Harron1b5a74e2015-01-21 22:42:49 +00001452 permissions,
1453 error);
1454 if (log)
1455 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1456
1457 if (error.Fail())
1458 return error;
1459 if (dest_file == UINT64_MAX)
1460 return Error("unable to open target file");
1461 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1462 uint64_t offset = 0;
1463 for (;;)
1464 {
1465 size_t bytes_read = buffer_sp->GetByteSize();
1466 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1467 if (error.Fail() || bytes_read == 0)
1468 break;
1469
1470 const uint64_t bytes_written = WriteFile(dest_file, offset,
1471 buffer_sp->GetBytes(), bytes_read, error);
1472 if (error.Fail())
1473 break;
1474
1475 offset += bytes_written;
1476 if (bytes_written != bytes_read)
1477 {
1478 // We didn't write the correct number of bytes, so adjust
1479 // the file position in the source file we are reading from...
1480 source_file.SeekFromStart(offset);
1481 }
1482 }
1483 CloseFile(dest_file, error);
1484
1485 if (uid == UINT32_MAX && gid == UINT32_MAX)
1486 return error;
1487
1488 // TODO: ChownFile?
1489
Daniel Maleae0f8f572013-08-26 23:57:52 +00001490 return error;
1491}
1492
1493Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001494Platform::GetFile(const FileSpec &source,
1495 const FileSpec &destination)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001496{
1497 Error error("unimplemented");
1498 return error;
1499}
1500
Greg Claytonfbb76342013-11-20 21:07:01 +00001501Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001502Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1503 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001504{
1505 Error error("unimplemented");
1506 return error;
1507}
1508
Daniel Maleae0f8f572013-08-26 23:57:52 +00001509bool
Chaoren Lind3173f32015-05-29 19:52:29 +00001510Platform::GetFileExists(const lldb_private::FileSpec &file_spec)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001511{
1512 return false;
1513}
1514
Greg Claytonfbb76342013-11-20 21:07:01 +00001515Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001516Platform::Unlink(const FileSpec &path)
Greg Claytonfbb76342013-11-20 21:07:01 +00001517{
1518 Error error("unimplemented");
1519 return error;
1520}
1521
Robert Flack96ad3de2015-05-09 15:53:31 +00001522uint64_t
Mohit K. Bhakkade0d8c422015-06-30 06:29:12 +00001523Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags)
Robert Flack96ad3de2015-05-09 15:53:31 +00001524{
1525 uint64_t flags_platform = 0;
1526 if (flags & eMmapFlagsPrivate)
1527 flags_platform |= MAP_PRIVATE;
1528 if (flags & eMmapFlagsAnon)
1529 flags_platform |= MAP_ANON;
1530 return flags_platform;
1531}
Greg Claytonfbb76342013-11-20 21:07:01 +00001532
Daniel Maleae0f8f572013-08-26 23:57:52 +00001533lldb_private::Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001534Platform::RunShellCommand(const char *command, // Shouldn't be NULL
1535 const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory
1536 int *status_ptr, // Pass NULL if you don't want the process exit status
1537 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1538 std::string *command_output, // Pass NULL if you don't want the command output
1539 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001540{
1541 if (IsHost())
1542 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1543 else
1544 return Error("unimplemented");
1545}
1546
1547
1548bool
1549Platform::CalculateMD5 (const FileSpec& file_spec,
1550 uint64_t &low,
1551 uint64_t &high)
1552{
1553 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001554 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001555 else
1556 return false;
1557}
1558
1559void
1560Platform::SetLocalCacheDirectory (const char* local)
1561{
1562 m_local_cache_directory.assign(local);
1563}
1564
1565const char*
1566Platform::GetLocalCacheDirectory ()
1567{
1568 return m_local_cache_directory.c_str();
1569}
1570
1571static OptionDefinition
1572g_rsync_option_table[] =
1573{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001574 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1575 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1576 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1577 { 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 +00001578};
1579
1580static OptionDefinition
1581g_ssh_option_table[] =
1582{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001583 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1584 { 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 +00001585};
1586
1587static OptionDefinition
1588g_caching_option_table[] =
1589{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001590 { 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 +00001591};
1592
1593OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1594{
1595}
1596
1597OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1598{
1599}
1600
1601const lldb_private::OptionDefinition*
1602OptionGroupPlatformRSync::GetDefinitions ()
1603{
1604 return g_rsync_option_table;
1605}
1606
1607void
1608OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1609{
1610 m_rsync = false;
1611 m_rsync_opts.clear();
1612 m_rsync_prefix.clear();
1613 m_ignores_remote_hostname = false;
1614}
1615
1616lldb_private::Error
1617OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1618 uint32_t option_idx,
1619 const char *option_arg)
1620{
1621 Error error;
1622 char short_option = (char) GetDefinitions()[option_idx].short_option;
1623 switch (short_option)
1624 {
1625 case 'r':
1626 m_rsync = true;
1627 break;
1628
1629 case 'R':
1630 m_rsync_opts.assign(option_arg);
1631 break;
1632
1633 case 'P':
1634 m_rsync_prefix.assign(option_arg);
1635 break;
1636
1637 case 'i':
1638 m_ignores_remote_hostname = true;
1639 break;
1640
1641 default:
1642 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1643 break;
1644 }
1645
1646 return error;
1647}
1648
1649uint32_t
1650OptionGroupPlatformRSync::GetNumDefinitions ()
1651{
1652 return llvm::array_lengthof(g_rsync_option_table);
1653}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001654
Greg Clayton4116e932012-05-15 02:33:01 +00001655lldb::BreakpointSP
1656Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1657{
1658 return lldb::BreakpointSP();
1659}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001660
Daniel Maleae0f8f572013-08-26 23:57:52 +00001661OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1662{
1663}
1664
1665OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1666{
1667}
1668
1669const lldb_private::OptionDefinition*
1670OptionGroupPlatformSSH::GetDefinitions ()
1671{
1672 return g_ssh_option_table;
1673}
1674
1675void
1676OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1677{
1678 m_ssh = false;
1679 m_ssh_opts.clear();
1680}
1681
1682lldb_private::Error
1683OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1684 uint32_t option_idx,
1685 const char *option_arg)
1686{
1687 Error error;
1688 char short_option = (char) GetDefinitions()[option_idx].short_option;
1689 switch (short_option)
1690 {
1691 case 's':
1692 m_ssh = true;
1693 break;
1694
1695 case 'S':
1696 m_ssh_opts.assign(option_arg);
1697 break;
1698
1699 default:
1700 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1701 break;
1702 }
1703
1704 return error;
1705}
1706
1707uint32_t
1708OptionGroupPlatformSSH::GetNumDefinitions ()
1709{
1710 return llvm::array_lengthof(g_ssh_option_table);
1711}
1712
1713OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1714{
1715}
1716
1717OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1718{
1719}
1720
1721const lldb_private::OptionDefinition*
1722OptionGroupPlatformCaching::GetDefinitions ()
1723{
1724 return g_caching_option_table;
1725}
1726
1727void
1728OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1729{
1730 m_cache_dir.clear();
1731}
1732
1733lldb_private::Error
1734OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1735 uint32_t option_idx,
1736 const char *option_arg)
1737{
1738 Error error;
1739 char short_option = (char) GetDefinitions()[option_idx].short_option;
1740 switch (short_option)
1741 {
1742 case 'c':
1743 m_cache_dir.assign(option_arg);
1744 break;
1745
1746 default:
1747 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1748 break;
1749 }
1750
1751 return error;
1752}
1753
1754uint32_t
1755OptionGroupPlatformCaching::GetNumDefinitions ()
1756{
1757 return llvm::array_lengthof(g_caching_option_table);
1758}
1759
Greg Clayton67cc0632012-08-22 17:17:09 +00001760size_t
1761Platform::GetEnvironment (StringList &environment)
1762{
1763 environment.Clear();
1764 return false;
1765}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001766
1767const std::vector<ConstString> &
1768Platform::GetTrapHandlerSymbolNames ()
1769{
1770 if (!m_calculated_trap_handlers)
1771 {
Greg Clayton7597b352015-02-02 20:45:17 +00001772 Mutex::Locker locker (m_mutex);
Jason Molenda4da87062014-05-23 23:11:27 +00001773 if (!m_calculated_trap_handlers)
1774 {
1775 CalculateTrapHandlerSymbolNames();
1776 m_calculated_trap_handlers = true;
1777 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001778 }
1779 return m_trap_handlers;
1780}
1781
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001782Error
1783Platform::GetCachedExecutable (ModuleSpec &module_spec,
1784 lldb::ModuleSP &module_sp,
1785 const FileSpecList *module_search_paths_ptr,
1786 Platform &remote_platform)
1787{
1788 const auto platform_spec = module_spec.GetFileSpec ();
1789 const auto error = LoadCachedExecutable (module_spec,
1790 module_sp,
1791 module_search_paths_ptr,
1792 remote_platform);
1793 if (error.Success ())
1794 {
1795 module_spec.GetFileSpec () = module_sp->GetFileSpec ();
1796 module_spec.GetPlatformFileSpec () = platform_spec;
1797 }
1798
1799 return error;
1800}
1801
1802Error
1803Platform::LoadCachedExecutable (const ModuleSpec &module_spec,
1804 lldb::ModuleSP &module_sp,
1805 const FileSpecList *module_search_paths_ptr,
1806 Platform &remote_platform)
1807{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001808 return GetRemoteSharedModule (module_spec,
1809 nullptr,
1810 module_sp,
1811 [&](const ModuleSpec &spec)
1812 {
1813 return remote_platform.ResolveExecutable (
1814 spec, module_sp, module_search_paths_ptr);
1815 },
1816 nullptr);
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001817}
1818
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001819Error
1820Platform::GetRemoteSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001821 Process* process,
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001822 lldb::ModuleSP &module_sp,
1823 const ModuleResolver &module_resolver,
1824 bool *did_create_ptr)
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001825{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001826 // Get module information from a target.
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001827 ModuleSpec resolved_module_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001828 bool got_module_spec = false;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001829 if (process)
1830 {
1831 // Try to get module information from the process
1832 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001833 got_module_spec = true;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001834 }
1835
1836 if (!got_module_spec)
1837 {
1838 // Get module information from a target.
1839 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001840 return module_resolver (module_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001841 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001842
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001843 // Trying to find a module by UUID on local file system.
1844 const auto error = module_resolver (resolved_module_spec);
1845 if (error.Fail ())
1846 {
1847 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr))
1848 return Error ();
1849 }
1850
1851 return error;
1852}
1853
1854bool
1855Platform::GetCachedSharedModule (const ModuleSpec &module_spec,
1856 lldb::ModuleSP &module_sp,
1857 bool *did_create_ptr)
1858{
1859 if (IsHost() ||
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +00001860 !GetGlobalPlatformProperties ()->GetUseModuleCache () ||
1861 !GetGlobalPlatformProperties ()->GetModuleCacheDirectory ())
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001862 return false;
1863
1864 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
1865
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001866 // Check local cache for a module.
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001867 auto error = m_module_cache->GetAndPut (
1868 GetModuleCacheRoot (),
1869 GetCacheHostname (),
1870 module_spec,
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001871 [this](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec)
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001872 {
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001873 return DownloadModuleSlice (module_spec.GetFileSpec (),
1874 module_spec.GetObjectOffset (),
1875 module_spec.GetObjectSize (),
1876 tmp_download_file_spec);
1877
1878 },
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001879 [this](const ModuleSP& module_sp, const FileSpec& tmp_download_file_spec)
1880 {
1881 return DownloadSymbolFile (module_sp, tmp_download_file_spec);
1882 },
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001883 module_sp,
1884 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001885 if (error.Success ())
1886 return true;
1887
1888 if (log)
1889 log->Printf("Platform::%s - module %s not found in local cache: %s",
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001890 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001891 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001892}
1893
1894Error
1895Platform::DownloadModuleSlice (const FileSpec& src_file_spec,
1896 const uint64_t src_offset,
1897 const uint64_t src_size,
1898 const FileSpec& dst_file_spec)
1899{
1900 Error error;
1901
1902 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1903 if (!dst.is_open())
1904 {
1905 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ());
1906 return error;
1907 }
1908
1909 auto src_fd = OpenFile (src_file_spec,
1910 File::eOpenOptionRead,
1911 lldb::eFilePermissionsFileDefault,
1912 error);
1913
1914 if (error.Fail ())
1915 {
1916 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ());
1917 return error;
1918 }
1919
1920 std::vector<char> buffer (1024);
1921 auto offset = src_offset;
1922 uint64_t total_bytes_read = 0;
1923 while (total_bytes_read < src_size)
1924 {
1925 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read);
1926 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error);
1927 if (error.Fail ())
1928 break;
1929 if (n_read == 0)
1930 {
1931 error.SetErrorString ("read 0 bytes");
1932 break;
1933 }
1934 offset += n_read;
1935 total_bytes_read += n_read;
1936 dst.write (&buffer[0], n_read);
1937 }
1938
1939 Error close_error;
1940 CloseFile (src_fd, close_error); // Ignoring close error.
1941
1942 return error;
1943}
1944
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001945Error
1946Platform::DownloadSymbolFile (const lldb::ModuleSP& module_sp, const FileSpec& dst_file_spec)
1947{
1948 return Error ("Symbol file downloading not supported by the default platform.");
1949}
1950
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001951FileSpec
1952Platform::GetModuleCacheRoot ()
1953{
1954 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory ();
1955 dir_spec.AppendPathComponent (GetName ().AsCString ());
1956 return dir_spec;
1957}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001958
1959const char *
1960Platform::GetCacheHostname ()
1961{
1962 return GetHostname ();
1963}
Chaoren Lin98d0a4b2015-07-14 01:09:28 +00001964
1965const UnixSignalsSP &
1966Platform::GetRemoteUnixSignals()
1967{
1968 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1969 return s_default_unix_signals_sp;
1970}
1971
1972const UnixSignalsSP &
1973Platform::GetUnixSignals()
1974{
1975 if (IsHost())
1976 return Host::GetUnixSignals();
1977 return GetRemoteUnixSignals();
1978}