blob: f5dd66a3e59796f96dab1eff8b6fc5d7b34ad1c2 [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
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Greg Clayton4116e932012-05-15 02:33:01 +000016#include "lldb/Breakpoint/BreakpointIDList.h"
Greg Claytone996fd32011-03-08 22:40:15 +000017#include "lldb/Core/Error.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000018#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000022#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000023#include "lldb/Host/Host.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000024#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000025#include "lldb/Target/Target.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000026#include "lldb/Utility/Utils.h"
Greg Claytone996fd32011-03-08 22:40:15 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31// Use a singleton function for g_local_platform_sp to avoid init
32// constructors since LLDB is often part of a shared library
33static PlatformSP&
34GetDefaultPlatformSP ()
35{
36 static PlatformSP g_default_platform_sp;
37 return g_default_platform_sp;
38}
39
Greg Claytone996fd32011-03-08 22:40:15 +000040static Mutex &
41GetConnectedPlatformListMutex ()
42{
43 static Mutex g_remote_connected_platforms_mutex (Mutex::eMutexTypeRecursive);
44 return g_remote_connected_platforms_mutex;
45}
46static std::vector<PlatformSP> &
47GetConnectedPlatformList ()
48{
49 static std::vector<PlatformSP> g_remote_connected_platforms;
50 return g_remote_connected_platforms;
51}
52
Greg Claytonab65b342011-04-13 22:47:15 +000053
54const char *
55Platform::GetHostPlatformName ()
56{
57 return "host";
58}
59
Greg Claytone996fd32011-03-08 22:40:15 +000060//------------------------------------------------------------------
61/// Get the native host platform plug-in.
62///
63/// There should only be one of these for each host that LLDB runs
64/// upon that should be statically compiled in and registered using
65/// preprocessor macros or other similar build mechanisms.
66///
67/// This platform will be used as the default platform when launching
68/// or attaching to processes unless another platform is specified.
69//------------------------------------------------------------------
70PlatformSP
71Platform::GetDefaultPlatform ()
72{
73 return GetDefaultPlatformSP ();
74}
75
76void
77Platform::SetDefaultPlatform (const lldb::PlatformSP &platform_sp)
78{
79 // The native platform should use its static void Platform::Initialize()
80 // function to register itself as the native platform.
81 GetDefaultPlatformSP () = platform_sp;
82}
83
Greg Claytone996fd32011-03-08 22:40:15 +000084Error
Steve Puccifc995722014-01-17 18:18:31 +000085Platform::GetFileWithUUID (const FileSpec &platform_file,
86 const UUID *uuid_ptr,
87 FileSpec &local_file)
Greg Claytone996fd32011-03-08 22:40:15 +000088{
89 // Default to the local case
90 local_file = platform_file;
91 return Error();
92}
93
Greg Clayton91c0e742013-01-11 23:44:27 +000094FileSpecList
Enrico Granatafe7295d2014-08-16 00:32:58 +000095Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +000096{
Greg Clayton91c0e742013-01-11 23:44:27 +000097 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +000098}
99
Jason Molenda1c627542013-04-05 01:03:25 +0000100Platform*
Greg Clayton57abc5d2013-05-10 21:47:16 +0000101Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Jason Molenda1c627542013-04-05 01:03:25 +0000102{
103 PlatformCreateInstance create_callback = NULL;
104 if (plugin_name)
105 {
106 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
107 if (create_callback)
108 {
109 ArchSpec arch;
110 if (process)
111 {
112 arch = process->GetTarget().GetArchitecture();
113 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000114 std::unique_ptr<Platform> instance_ap(create_callback(process, &arch));
Jason Molenda1c627542013-04-05 01:03:25 +0000115 if (instance_ap.get())
116 return instance_ap.release();
117 }
118 }
119 else
120 {
121 for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
122 {
Matt Kopecef143712013-06-03 18:00:07 +0000123 std::unique_ptr<Platform> instance_ap(create_callback(process, nullptr));
Jason Molenda1c627542013-04-05 01:03:25 +0000124 if (instance_ap.get())
125 return instance_ap.release();
126 }
127 }
128 return NULL;
129}
130
Greg Clayton32e0a752011-03-30 18:16:51 +0000131Error
Greg Claytonb9a01b32012-02-26 05:51:37 +0000132Platform::GetSharedModule (const ModuleSpec &module_spec,
Greg Clayton32e0a752011-03-30 18:16:51 +0000133 ModuleSP &module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000134 const FileSpecList *module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000135 ModuleSP *old_module_sp_ptr,
136 bool *did_create_ptr)
137{
138 // Don't do any path remapping for the default implementation
139 // of the platform GetSharedModule function, just call through
140 // to our static ModuleList function. Platform subclasses that
141 // implement remote debugging, might have a developer kits
142 // installed that have cached versions of the files for the
143 // remote target, or might implement a download and cache
144 // locally implementation.
145 const bool always_create = false;
Greg Claytonb9a01b32012-02-26 05:51:37 +0000146 return ModuleList::GetSharedModule (module_spec,
Greg Clayton32e0a752011-03-30 18:16:51 +0000147 module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000148 module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000149 old_module_sp_ptr,
150 did_create_ptr,
151 always_create);
152}
153
Greg Claytone996fd32011-03-08 22:40:15 +0000154PlatformSP
Greg Claytonded470d2011-03-19 01:12:21 +0000155Platform::Create (const char *platform_name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000156{
157 PlatformCreateInstance create_callback = NULL;
158 lldb::PlatformSP platform_sp;
Greg Claytonded470d2011-03-19 01:12:21 +0000159 if (platform_name && platform_name[0])
Greg Claytone996fd32011-03-08 22:40:15 +0000160 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000161 ConstString const_platform_name (platform_name);
162 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (const_platform_name);
Greg Claytone996fd32011-03-08 22:40:15 +0000163 if (create_callback)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000164 platform_sp.reset(create_callback(true, NULL));
Greg Claytone996fd32011-03-08 22:40:15 +0000165 else
Greg Claytonded470d2011-03-19 01:12:21 +0000166 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", platform_name);
Greg Claytone996fd32011-03-08 22:40:15 +0000167 }
168 else
Greg Claytonded470d2011-03-19 01:12:21 +0000169 error.SetErrorString ("invalid platform name");
Greg Claytone996fd32011-03-08 22:40:15 +0000170 return platform_sp;
171}
172
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000173
174PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000175Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000176{
177 lldb::PlatformSP platform_sp;
178 if (arch.IsValid())
179 {
Greg Clayton70512312012-05-08 01:45:38 +0000180 uint32_t idx;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000181 PlatformCreateInstance create_callback;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000182 // First try exact arch matches across all platform plug-ins
183 bool exact = true;
Greg Clayton70512312012-05-08 01:45:38 +0000184 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000185 {
186 if (create_callback)
Greg Clayton1e0c8842013-01-11 20:49:54 +0000187 {
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000188 platform_sp.reset(create_callback(false, &arch));
Greg Clayton1e0c8842013-01-11 20:49:54 +0000189 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr))
190 return platform_sp;
191 }
192 }
193 // Next try compatible arch matches across all platform plug-ins
194 exact = false;
195 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
196 {
197 if (create_callback)
198 {
199 platform_sp.reset(create_callback(false, &arch));
200 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr))
201 return platform_sp;
202 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000203 }
204 }
205 else
206 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000207 if (platform_arch_ptr)
208 platform_arch_ptr->Clear();
209 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000210 return platform_sp;
211}
212
Greg Claytone996fd32011-03-08 22:40:15 +0000213uint32_t
214Platform::GetNumConnectedRemotePlatforms ()
215{
216 Mutex::Locker locker (GetConnectedPlatformListMutex ());
217 return GetConnectedPlatformList().size();
218}
219
220PlatformSP
221Platform::GetConnectedRemotePlatformAtIndex (uint32_t idx)
222{
223 PlatformSP platform_sp;
224 {
225 Mutex::Locker locker (GetConnectedPlatformListMutex ());
226 if (idx < GetConnectedPlatformList().size())
227 platform_sp = GetConnectedPlatformList ()[idx];
228 }
229 return platform_sp;
230}
231
232//------------------------------------------------------------------
233/// Default Constructor
234//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000235Platform::Platform (bool is_host) :
236 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000237 m_os_version_set_while_connected (false),
238 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000239 m_sdk_sysroot (),
240 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000241 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000242 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000243 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000244 m_major_os_version (UINT32_MAX),
245 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000246 m_update_os_version (UINT32_MAX),
247 m_system_arch(),
248 m_uid_map_mutex (Mutex::eMutexTypeNormal),
249 m_gid_map_mutex (Mutex::eMutexTypeNormal),
250 m_uid_map(),
251 m_gid_map(),
252 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000253 m_max_gid_name_len (0),
254 m_supports_rsync (false),
255 m_rsync_opts (),
256 m_rsync_prefix (),
257 m_supports_ssh (false),
258 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000259 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000260 m_trap_handlers(),
Jason Molenda4da87062014-05-23 23:11:27 +0000261 m_calculated_trap_handlers (false),
262 m_trap_handler_mutex()
Greg Claytone996fd32011-03-08 22:40:15 +0000263{
Greg Clayton5160ce52013-03-27 23:08:40 +0000264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000265 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000266 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000267}
268
269//------------------------------------------------------------------
270/// Destructor.
271///
272/// The destructor is virtual since this class is designed to be
273/// inherited from by the plug-in instance.
274//------------------------------------------------------------------
275Platform::~Platform()
276{
Greg Clayton5160ce52013-03-27 23:08:40 +0000277 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000278 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000279 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000280}
281
Greg Clayton1cb64962011-03-24 04:28:38 +0000282void
283Platform::GetStatus (Stream &strm)
284{
285 uint32_t major = UINT32_MAX;
286 uint32_t minor = UINT32_MAX;
287 uint32_t update = UINT32_MAX;
288 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000289 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000290
291 ArchSpec arch (GetSystemArchitecture());
292 if (arch.IsValid())
293 {
294 if (!arch.GetTriple().str().empty())
Greg Clayton32e0a752011-03-30 18:16:51 +0000295 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000296 }
297
298 if (GetOSVersion(major, minor, update))
299 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000300 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000301 if (minor != UINT32_MAX)
302 strm.Printf(".%u", minor);
303 if (update != UINT32_MAX)
304 strm.Printf(".%u", update);
305
306 if (GetOSBuildString (s))
307 strm.Printf(" (%s)", s.c_str());
308
309 strm.EOL();
310 }
311
312 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000313 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000314
315 if (IsHost())
316 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000317 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000318 }
319 else
320 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000321 const bool is_connected = IsConnected();
322 if (is_connected)
323 strm.Printf(" Hostname: %s\n", GetHostname());
324 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000325 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000326
Greg Claytonfbb76342013-11-20 21:07:01 +0000327 if (GetWorkingDirectory())
328 {
329 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
330 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000331 if (!IsConnected())
332 return;
333
334 std::string specific_info(GetPlatformSpecificConnectionInformation());
335
336 if (specific_info.empty() == false)
337 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000338}
339
Greg Claytonded470d2011-03-19 01:12:21 +0000340
341bool
342Platform::GetOSVersion (uint32_t &major,
343 uint32_t &minor,
344 uint32_t &update)
345{
346 bool success = m_major_os_version != UINT32_MAX;
347 if (IsHost())
348 {
349 if (!success)
350 {
351 // We have a local host platform
352 success = Host::GetOSVersion (m_major_os_version,
353 m_minor_os_version,
354 m_update_os_version);
355 m_os_version_set_while_connected = success;
356 }
357 }
358 else
359 {
360 // We have a remote platform. We can only fetch the remote
361 // OS version if we are connected, and we don't want to do it
362 // more than once.
363
364 const bool is_connected = IsConnected();
365
Greg Clayton1cb64962011-03-24 04:28:38 +0000366 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000367 if (success)
368 {
369 // We have valid OS version info, check to make sure it wasn't
370 // manually set prior to connecting. If it was manually set prior
371 // to connecting, then lets fetch the actual OS version info
372 // if we are now connected.
373 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000374 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000375 }
376 else
377 {
378 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000379 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000380 }
381
Greg Clayton1cb64962011-03-24 04:28:38 +0000382 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000383 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000384 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000385 m_os_version_set_while_connected = success;
386 }
387 }
388
389 if (success)
390 {
391 major = m_major_os_version;
392 minor = m_minor_os_version;
393 update = m_update_os_version;
394 }
395 return success;
396}
Greg Clayton1cb64962011-03-24 04:28:38 +0000397
398bool
399Platform::GetOSBuildString (std::string &s)
400{
401 if (IsHost())
402 return Host::GetOSBuildString (s);
403 else
404 return GetRemoteOSBuildString (s);
405}
406
407bool
408Platform::GetOSKernelDescription (std::string &s)
409{
410 if (IsHost())
411 return Host::GetOSKernelDescription (s);
412 else
413 return GetRemoteOSKernelDescription (s);
414}
415
Greg Clayton57abc5d2013-05-10 21:47:16 +0000416ConstString
Greg Claytonfbb76342013-11-20 21:07:01 +0000417Platform::GetWorkingDirectory ()
418{
419 if (IsHost())
420 {
421 char cwd[PATH_MAX];
422 if (getcwd(cwd, sizeof(cwd)))
423 return ConstString(cwd);
424 else
425 return ConstString();
426 }
427 else
428 {
429 if (!m_working_dir)
430 m_working_dir = GetRemoteWorkingDirectory();
431 return m_working_dir;
432 }
433}
434
435
436struct RecurseCopyBaton
437{
438 const FileSpec& dst;
439 Platform *platform_ptr;
440 Error error;
441};
442
443
444static FileSpec::EnumerateDirectoryResult
445RecurseCopy_Callback (void *baton,
446 FileSpec::FileType file_type,
447 const FileSpec &src)
448{
449 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
450 switch (file_type)
451 {
452 case FileSpec::eFileTypePipe:
453 case FileSpec::eFileTypeSocket:
454 // we have no way to copy pipes and sockets - ignore them and continue
455 return FileSpec::eEnumerateDirectoryResultNext;
456 break;
457
458 case FileSpec::eFileTypeDirectory:
459 {
460 // make the new directory and get in there
461 FileSpec dst_dir = rc_baton->dst;
462 if (!dst_dir.GetFilename())
463 dst_dir.GetFilename() = src.GetLastPathComponent();
464 std::string dst_dir_path (dst_dir.GetPath());
465 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir_path.c_str(), lldb::eFilePermissionsDirectoryDefault);
466 if (error.Fail())
467 {
468 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", dst_dir_path.c_str());
469 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
470 }
471
472 // now recurse
473 std::string src_dir_path (src.GetPath());
474
475 // Make a filespec that only fills in the directory of a FileSpec so
476 // when we enumerate we can quickly fill in the filename for dst copies
477 FileSpec recurse_dst;
478 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
479 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
480 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
481 if (rc_baton2.error.Fail())
482 {
483 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
484 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
485 }
486 return FileSpec::eEnumerateDirectoryResultNext;
487 }
488 break;
489
490 case FileSpec::eFileTypeSymbolicLink:
491 {
492 // copy the file and keep going
493 FileSpec dst_file = rc_baton->dst;
494 if (!dst_file.GetFilename())
495 dst_file.GetFilename() = src.GetFilename();
496
497 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000498
499 rc_baton->error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000500
501 if (rc_baton->error.Fail())
502 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
503
504 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file.GetPath().c_str(), buf);
505
506 if (rc_baton->error.Fail())
507 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
508
509 return FileSpec::eEnumerateDirectoryResultNext;
510 }
511 break;
512 case FileSpec::eFileTypeRegular:
513 {
514 // copy the file and keep going
515 FileSpec dst_file = rc_baton->dst;
516 if (!dst_file.GetFilename())
517 dst_file.GetFilename() = src.GetFilename();
518 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
519 if (err.Fail())
520 {
521 rc_baton->error.SetErrorString(err.AsCString());
522 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
523 }
524 return FileSpec::eEnumerateDirectoryResultNext;
525 }
526 break;
527
528 case FileSpec::eFileTypeInvalid:
529 case FileSpec::eFileTypeOther:
530 case FileSpec::eFileTypeUnknown:
Todd Fialaaf245d12014-06-30 21:05:18 +0000531 default:
Greg Claytonfbb76342013-11-20 21:07:01 +0000532 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
533 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
534 break;
535 }
536}
537
538Error
539Platform::Install (const FileSpec& src, const FileSpec& dst)
540{
541 Error error;
542
543 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
544 if (log)
545 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
546 FileSpec fixed_dst(dst);
547
548 if (!fixed_dst.GetFilename())
549 fixed_dst.GetFilename() = src.GetFilename();
550
551 ConstString working_dir = GetWorkingDirectory();
552
553 if (dst)
554 {
555 if (dst.GetDirectory())
556 {
557 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
558 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
559 {
560 fixed_dst.GetDirectory() = dst.GetDirectory();
561 }
562 // If the fixed destination file doesn't have a directory yet,
563 // then we must have a relative path. We will resolve this relative
564 // path against the platform's working directory
565 if (!fixed_dst.GetDirectory())
566 {
567 FileSpec relative_spec;
568 std::string path;
569 if (working_dir)
570 {
571 relative_spec.SetFile(working_dir.GetCString(), false);
572 relative_spec.AppendPathComponent(dst.GetPath().c_str());
573 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
574 }
575 else
576 {
577 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
578 return error;
579 }
580 }
581 }
582 else
583 {
584 if (working_dir)
585 {
586 fixed_dst.GetDirectory() = working_dir;
587 }
588 else
589 {
590 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
591 return error;
592 }
593 }
594 }
595 else
596 {
597 if (working_dir)
598 {
599 fixed_dst.GetDirectory() = working_dir;
600 }
601 else
602 {
603 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
604 return error;
605 }
606 }
607
608 if (log)
609 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
610
611 if (GetSupportsRSync())
612 {
613 error = PutFile(src, dst);
614 }
615 else
616 {
617 switch (src.GetFileType())
618 {
619 case FileSpec::eFileTypeDirectory:
620 {
621 if (GetFileExists (fixed_dst))
622 Unlink (fixed_dst.GetPath().c_str());
623 uint32_t permissions = src.GetPermissions();
624 if (permissions == 0)
625 permissions = eFilePermissionsDirectoryDefault;
626 std::string dst_dir_path(fixed_dst.GetPath());
627 error = MakeDirectory(dst_dir_path.c_str(), permissions);
628 if (error.Success())
629 {
630 // Make a filespec that only fills in the directory of a FileSpec so
631 // when we enumerate we can quickly fill in the filename for dst copies
632 FileSpec recurse_dst;
633 recurse_dst.GetDirectory().SetCString(dst_dir_path.c_str());
634 std::string src_dir_path (src.GetPath());
635 RecurseCopyBaton baton = { recurse_dst, this, Error() };
636 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
637 return baton.error;
638 }
639 }
640 break;
641
642 case FileSpec::eFileTypeRegular:
643 if (GetFileExists (fixed_dst))
644 Unlink (fixed_dst.GetPath().c_str());
645 error = PutFile(src, fixed_dst);
646 break;
647
648 case FileSpec::eFileTypeSymbolicLink:
649 {
650 if (GetFileExists (fixed_dst))
651 Unlink (fixed_dst.GetPath().c_str());
652 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000653 error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000654 if (error.Success())
655 error = CreateSymlink(dst.GetPath().c_str(), buf);
656 }
657 break;
658 case FileSpec::eFileTypePipe:
659 error.SetErrorString("platform install doesn't handle pipes");
660 break;
661 case FileSpec::eFileTypeSocket:
662 error.SetErrorString("platform install doesn't handle sockets");
663 break;
664 case FileSpec::eFileTypeInvalid:
665 case FileSpec::eFileTypeUnknown:
666 case FileSpec::eFileTypeOther:
667 error.SetErrorString("platform install doesn't handle non file or directory items");
668 break;
669 }
670 }
671 return error;
672}
673
674bool
675Platform::SetWorkingDirectory (const ConstString &path)
676{
677 if (IsHost())
678 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000679 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
680 if (log)
681 log->Printf("Platform::SetWorkingDirectory('%s')", path.GetCString());
Colin Riley909bb7a2013-11-26 15:10:46 +0000682#ifdef _WIN32
683 // Not implemented on Windows
684 return false;
685#else
Greg Claytonfbb76342013-11-20 21:07:01 +0000686 if (path)
687 {
688 if (chdir(path.GetCString()) == 0)
689 return true;
690 }
691 return false;
Colin Riley909bb7a2013-11-26 15:10:46 +0000692#endif
Greg Claytonfbb76342013-11-20 21:07:01 +0000693 }
694 else
695 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000696 m_working_dir.Clear();
Greg Claytonfbb76342013-11-20 21:07:01 +0000697 return SetRemoteWorkingDirectory(path);
698 }
699}
700
701Error
702Platform::MakeDirectory (const char *path, uint32_t permissions)
703{
704 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000705 return FileSystem::MakeDirectory(path, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000706 else
707 {
708 Error error;
709 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
710 return error;
711 }
712}
713
714Error
715Platform::GetFilePermissions (const char *path, uint32_t &file_permissions)
716{
717 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000718 return FileSystem::GetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000719 else
720 {
721 Error error;
722 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
723 return error;
724 }
725}
726
727Error
728Platform::SetFilePermissions (const char *path, uint32_t file_permissions)
729{
730 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000731 return FileSystem::SetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000732 else
733 {
734 Error error;
735 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
736 return error;
737 }
738}
739
740ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000741Platform::GetName ()
742{
Greg Claytonfbb76342013-11-20 21:07:01 +0000743 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000744}
745
746const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000747Platform::GetHostname ()
748{
Greg Claytonab65b342011-04-13 22:47:15 +0000749 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000750 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000751
752 if (m_name.empty())
753 return NULL;
754 return m_name.c_str();
755}
756
Greg Clayton5fb8f792013-12-02 19:35:49 +0000757bool
758Platform::SetRemoteWorkingDirectory(const ConstString &path)
759{
760 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
761 if (log)
762 log->Printf("Platform::SetRemoteWorkingDirectory('%s')", path.GetCString());
763 m_working_dir = path;
764 return true;
765}
766
Greg Clayton32e0a752011-03-30 18:16:51 +0000767const char *
768Platform::GetUserName (uint32_t uid)
769{
770 const char *user_name = GetCachedUserName(uid);
771 if (user_name)
772 return user_name;
773 if (IsHost())
774 {
775 std::string name;
776 if (Host::GetUserName(uid, name))
777 return SetCachedUserName (uid, name.c_str(), name.size());
778 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000779 return NULL;
780}
781
Greg Clayton32e0a752011-03-30 18:16:51 +0000782const char *
783Platform::GetGroupName (uint32_t gid)
784{
785 const char *group_name = GetCachedGroupName(gid);
786 if (group_name)
787 return group_name;
788 if (IsHost())
789 {
790 std::string name;
791 if (Host::GetGroupName(gid, name))
792 return SetCachedGroupName (gid, name.c_str(), name.size());
793 }
794 return NULL;
795}
Greg Clayton1cb64962011-03-24 04:28:38 +0000796
Greg Claytonded470d2011-03-19 01:12:21 +0000797bool
798Platform::SetOSVersion (uint32_t major,
799 uint32_t minor,
800 uint32_t update)
801{
802 if (IsHost())
803 {
804 // We don't need anyone setting the OS version for the host platform,
805 // we should be able to figure it out by calling Host::GetOSVersion(...).
806 return false;
807 }
808 else
809 {
810 // We have a remote platform, allow setting the target OS version if
811 // we aren't connected, since if we are connected, we should be able to
812 // request the remote OS version from the connected platform.
813 if (IsConnected())
814 return false;
815 else
816 {
817 // We aren't connected and we might want to set the OS version
818 // ahead of time before we connect so we can peruse files and
819 // use a local SDK or PDK cache of support files to disassemble
820 // or do other things.
821 m_major_os_version = major;
822 m_minor_os_version = minor;
823 m_update_os_version = update;
824 return true;
825 }
826 }
827 return false;
828}
829
830
Greg Claytone996fd32011-03-08 22:40:15 +0000831Error
832Platform::ResolveExecutable (const FileSpec &exe_file,
833 const ArchSpec &exe_arch,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000834 lldb::ModuleSP &exe_module_sp,
835 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +0000836{
837 Error error;
838 if (exe_file.Exists())
839 {
Greg Claytonb9a01b32012-02-26 05:51:37 +0000840 ModuleSpec module_spec (exe_file, exe_arch);
841 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +0000842 {
Greg Claytonb9a01b32012-02-26 05:51:37 +0000843 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +0000844 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000845 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +0000846 NULL,
847 NULL);
848 }
849 else
850 {
851 // No valid architecture was specified, ask the platform for
852 // the architectures that we should be using (in the correct order)
853 // and see if we can find a match that way
Greg Claytonb9a01b32012-02-26 05:51:37 +0000854 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +0000855 {
Greg Claytonb9a01b32012-02-26 05:51:37 +0000856 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +0000857 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000858 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +0000859 NULL,
860 NULL);
861 // Did we find an executable using one of the
862 if (error.Success() && exe_module_sp)
863 break;
864 }
865 }
866 }
867 else
868 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000869 error.SetErrorStringWithFormat ("'%s' does not exist",
870 exe_file.GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +0000871 }
872 return error;
873}
874
Greg Clayton103f0282012-09-12 02:03:59 +0000875Error
876Platform::ResolveSymbolFile (Target &target,
877 const ModuleSpec &sym_spec,
878 FileSpec &sym_file)
879{
880 Error error;
881 if (sym_spec.GetSymbolFileSpec().Exists())
882 sym_file = sym_spec.GetSymbolFileSpec();
883 else
884 error.SetErrorString("unable to resolve symbol file");
885 return error;
886
887}
888
889
890
Greg Claytonaa516842011-08-11 16:25:18 +0000891bool
892Platform::ResolveRemotePath (const FileSpec &platform_path,
893 FileSpec &resolved_platform_path)
894{
895 resolved_platform_path = platform_path;
896 return resolved_platform_path.ResolvePath();
897}
898
Greg Claytonded470d2011-03-19 01:12:21 +0000899
900const ArchSpec &
901Platform::GetSystemArchitecture()
902{
903 if (IsHost())
904 {
905 if (!m_system_arch.IsValid())
906 {
907 // We have a local host platform
908 m_system_arch = Host::GetArchitecture();
909 m_system_arch_set_while_connected = m_system_arch.IsValid();
910 }
911 }
912 else
913 {
914 // We have a remote platform. We can only fetch the remote
915 // system architecture if we are connected, and we don't want to do it
916 // more than once.
917
918 const bool is_connected = IsConnected();
919
920 bool fetch = false;
921 if (m_system_arch.IsValid())
922 {
923 // We have valid OS version info, check to make sure it wasn't
924 // manually set prior to connecting. If it was manually set prior
925 // to connecting, then lets fetch the actual OS version info
926 // if we are now connected.
927 if (is_connected && !m_system_arch_set_while_connected)
928 fetch = true;
929 }
930 else
931 {
932 // We don't have valid OS version info, fetch it if we are connected
933 fetch = is_connected;
934 }
935
936 if (fetch)
937 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000938 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +0000939 m_system_arch_set_while_connected = m_system_arch.IsValid();
940 }
941 }
942 return m_system_arch;
943}
944
945
Greg Claytone996fd32011-03-08 22:40:15 +0000946Error
Greg Claytond314e812011-03-23 00:09:55 +0000947Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +0000948{
949 Error error;
Greg Claytond314e812011-03-23 00:09:55 +0000950 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +0000951 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +0000952 else
Greg Clayton57abc5d2013-05-10 21:47:16 +0000953 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000954 return error;
955}
956
957Error
Greg Claytond314e812011-03-23 00:09:55 +0000958Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +0000959{
960 Error error;
Greg Claytond314e812011-03-23 00:09:55 +0000961 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +0000962 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +0000963 else
Greg Clayton57abc5d2013-05-10 21:47:16 +0000964 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000965 return error;
966}
Greg Clayton32e0a752011-03-30 18:16:51 +0000967
968bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000969Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +0000970{
971 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +0000972 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +0000973 if (IsHost())
974 return Host::GetProcessInfo (pid, process_info);
975 return false;
976}
977
978uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +0000979Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
980 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +0000981{
Greg Clayton8b82f082011-04-12 05:54:46 +0000982 // Take care of the host case so that each subclass can just
983 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +0000984 uint32_t match_count = 0;
985 if (IsHost())
986 match_count = Host::FindProcesses (match_info, process_infos);
987 return match_count;
988}
Greg Clayton8b82f082011-04-12 05:54:46 +0000989
990
991Error
992Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
993{
994 Error error;
995 // Take care of the host case so that each subclass can just
996 // call this function to get the host functionality.
997 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +0000998 {
999 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1000 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1001
1002 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1003 {
1004 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001005 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1006 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001007 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Greg Claytond1cf11a2012-04-14 01:42:46 +00001008 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1009 is_localhost,
1010 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001011 first_arg_is_full_shell_command,
1012 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001013 return error;
1014 }
1015
Greg Clayton8b82f082011-04-12 05:54:46 +00001016 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001017 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001018 else
1019 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1020 return error;
1021}
1022
1023lldb::ProcessSP
1024Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1025 Debugger &debugger,
1026 Target *target, // Can be NULL, if NULL create a new target, else use existing one
1027 Listener &listener,
1028 Error &error)
1029{
1030 ProcessSP process_sp;
1031 // Make sure we stop at the entry point
1032 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001033 // We always launch the process we are going to debug in a separate process
1034 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1035 // about the target getting them as well.
1036 launch_info.SetLaunchInSeparateProcessGroup(true);
1037
Greg Clayton8b82f082011-04-12 05:54:46 +00001038 error = LaunchProcess (launch_info);
1039 if (error.Success())
1040 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001041 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001042 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001043 ProcessAttachInfo attach_info (launch_info);
1044 process_sp = Attach (attach_info, debugger, target, listener, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001045 if (process_sp)
1046 {
Greg Clayton44d93782014-01-27 23:43:24 +00001047 launch_info.SetHijackListener(attach_info.GetHijackListener());
1048
Greg Claytone24c4ac2011-11-17 04:46:02 +00001049 // Since we attached to the process, it will think it needs to detach
1050 // if the process object just goes away without an explicit call to
1051 // Process::Kill() or Process::Detach(), so let it know to kill the
1052 // process if this happens.
1053 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001054
1055 // If we didn't have any file actions, the pseudo terminal might
1056 // have been used where the slave side was given as the file to
1057 // open for stdin/out/err after we have already opened the master
1058 // so we can read/write stdin/out/err.
1059 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1060 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1061 {
1062 process_sp->SetSTDIOFileDescriptor(pty_fd);
1063 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001064 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001065 }
1066 }
1067 return process_sp;
1068}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001069
1070
1071lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001072Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001073{
1074 lldb::PlatformSP platform_sp;
1075 Error error;
1076 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001077 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001078 return platform_sp;
1079}
1080
1081
1082//------------------------------------------------------------------
1083/// Lets a platform answer if it is compatible with a given
1084/// architecture and the target triple contained within.
1085//------------------------------------------------------------------
1086bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001087Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001088{
1089 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001090 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001091 {
Greg Clayton70512312012-05-08 01:45:38 +00001092 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001093 // Try for an exact architecture match first.
1094 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001095 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001096 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001097 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001098 if (arch.IsExactMatch(platform_arch))
1099 {
1100 if (compatible_arch_ptr)
1101 *compatible_arch_ptr = platform_arch;
1102 return true;
1103 }
1104 }
1105 }
1106 else
1107 {
1108 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1109 {
1110 if (arch.IsCompatibleMatch(platform_arch))
1111 {
1112 if (compatible_arch_ptr)
1113 *compatible_arch_ptr = platform_arch;
1114 return true;
1115 }
Greg Clayton70512312012-05-08 01:45:38 +00001116 }
1117 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001118 }
Greg Clayton70512312012-05-08 01:45:38 +00001119 if (compatible_arch_ptr)
1120 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001121 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001122}
1123
Daniel Maleae0f8f572013-08-26 23:57:52 +00001124Error
1125Platform::PutFile (const FileSpec& source,
1126 const FileSpec& destination,
1127 uint32_t uid,
1128 uint32_t gid)
1129{
1130 Error error("unimplemented");
1131 return error;
1132}
1133
1134Error
1135Platform::GetFile (const FileSpec& source,
1136 const FileSpec& destination)
1137{
1138 Error error("unimplemented");
1139 return error;
1140}
1141
Greg Claytonfbb76342013-11-20 21:07:01 +00001142Error
1143Platform::CreateSymlink (const char *src, // The name of the link is in src
1144 const char *dst)// The symlink points to dst
1145{
1146 Error error("unimplemented");
1147 return error;
1148}
1149
Daniel Maleae0f8f572013-08-26 23:57:52 +00001150bool
1151Platform::GetFileExists (const lldb_private::FileSpec& file_spec)
1152{
1153 return false;
1154}
1155
Greg Claytonfbb76342013-11-20 21:07:01 +00001156Error
1157Platform::Unlink (const char *path)
1158{
1159 Error error("unimplemented");
1160 return error;
1161}
1162
1163
1164
Daniel Maleae0f8f572013-08-26 23:57:52 +00001165lldb_private::Error
1166Platform::RunShellCommand (const char *command, // Shouldn't be NULL
1167 const char *working_dir, // Pass NULL to use the current working directory
1168 int *status_ptr, // Pass NULL if you don't want the process exit status
1169 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1170 std::string *command_output, // Pass NULL if you don't want the command output
1171 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
1172{
1173 if (IsHost())
1174 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1175 else
1176 return Error("unimplemented");
1177}
1178
1179
1180bool
1181Platform::CalculateMD5 (const FileSpec& file_spec,
1182 uint64_t &low,
1183 uint64_t &high)
1184{
1185 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001186 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001187 else
1188 return false;
1189}
1190
Todd Fialaaf245d12014-06-30 21:05:18 +00001191Error
1192Platform::LaunchNativeProcess (
1193 ProcessLaunchInfo &launch_info,
1194 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1195 NativeProcessProtocolSP &process_sp)
1196{
1197 // Platforms should override this implementation if they want to
1198 // support lldb-gdbserver.
1199 return Error("unimplemented");
1200}
1201
1202Error
1203Platform::AttachNativeProcess (lldb::pid_t pid,
1204 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1205 NativeProcessProtocolSP &process_sp)
1206{
1207 // Platforms should override this implementation if they want to
1208 // support lldb-gdbserver.
1209 return Error("unimplemented");
1210}
1211
Daniel Maleae0f8f572013-08-26 23:57:52 +00001212void
1213Platform::SetLocalCacheDirectory (const char* local)
1214{
1215 m_local_cache_directory.assign(local);
1216}
1217
1218const char*
1219Platform::GetLocalCacheDirectory ()
1220{
1221 return m_local_cache_directory.c_str();
1222}
1223
1224static OptionDefinition
1225g_rsync_option_table[] =
1226{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001227 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1228 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1229 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1230 { 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 +00001231};
1232
1233static OptionDefinition
1234g_ssh_option_table[] =
1235{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001236 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1237 { 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 +00001238};
1239
1240static OptionDefinition
1241g_caching_option_table[] =
1242{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001243 { 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 +00001244};
1245
1246OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1247{
1248}
1249
1250OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1251{
1252}
1253
1254const lldb_private::OptionDefinition*
1255OptionGroupPlatformRSync::GetDefinitions ()
1256{
1257 return g_rsync_option_table;
1258}
1259
1260void
1261OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1262{
1263 m_rsync = false;
1264 m_rsync_opts.clear();
1265 m_rsync_prefix.clear();
1266 m_ignores_remote_hostname = false;
1267}
1268
1269lldb_private::Error
1270OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1271 uint32_t option_idx,
1272 const char *option_arg)
1273{
1274 Error error;
1275 char short_option = (char) GetDefinitions()[option_idx].short_option;
1276 switch (short_option)
1277 {
1278 case 'r':
1279 m_rsync = true;
1280 break;
1281
1282 case 'R':
1283 m_rsync_opts.assign(option_arg);
1284 break;
1285
1286 case 'P':
1287 m_rsync_prefix.assign(option_arg);
1288 break;
1289
1290 case 'i':
1291 m_ignores_remote_hostname = true;
1292 break;
1293
1294 default:
1295 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1296 break;
1297 }
1298
1299 return error;
1300}
1301
1302uint32_t
1303OptionGroupPlatformRSync::GetNumDefinitions ()
1304{
1305 return llvm::array_lengthof(g_rsync_option_table);
1306}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001307
Greg Clayton4116e932012-05-15 02:33:01 +00001308lldb::BreakpointSP
1309Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1310{
1311 return lldb::BreakpointSP();
1312}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001313
Daniel Maleae0f8f572013-08-26 23:57:52 +00001314OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1315{
1316}
1317
1318OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1319{
1320}
1321
1322const lldb_private::OptionDefinition*
1323OptionGroupPlatformSSH::GetDefinitions ()
1324{
1325 return g_ssh_option_table;
1326}
1327
1328void
1329OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1330{
1331 m_ssh = false;
1332 m_ssh_opts.clear();
1333}
1334
1335lldb_private::Error
1336OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1337 uint32_t option_idx,
1338 const char *option_arg)
1339{
1340 Error error;
1341 char short_option = (char) GetDefinitions()[option_idx].short_option;
1342 switch (short_option)
1343 {
1344 case 's':
1345 m_ssh = true;
1346 break;
1347
1348 case 'S':
1349 m_ssh_opts.assign(option_arg);
1350 break;
1351
1352 default:
1353 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1354 break;
1355 }
1356
1357 return error;
1358}
1359
1360uint32_t
1361OptionGroupPlatformSSH::GetNumDefinitions ()
1362{
1363 return llvm::array_lengthof(g_ssh_option_table);
1364}
1365
1366OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1367{
1368}
1369
1370OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1371{
1372}
1373
1374const lldb_private::OptionDefinition*
1375OptionGroupPlatformCaching::GetDefinitions ()
1376{
1377 return g_caching_option_table;
1378}
1379
1380void
1381OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1382{
1383 m_cache_dir.clear();
1384}
1385
1386lldb_private::Error
1387OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1388 uint32_t option_idx,
1389 const char *option_arg)
1390{
1391 Error error;
1392 char short_option = (char) GetDefinitions()[option_idx].short_option;
1393 switch (short_option)
1394 {
1395 case 'c':
1396 m_cache_dir.assign(option_arg);
1397 break;
1398
1399 default:
1400 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1401 break;
1402 }
1403
1404 return error;
1405}
1406
1407uint32_t
1408OptionGroupPlatformCaching::GetNumDefinitions ()
1409{
1410 return llvm::array_lengthof(g_caching_option_table);
1411}
1412
Greg Clayton67cc0632012-08-22 17:17:09 +00001413size_t
1414Platform::GetEnvironment (StringList &environment)
1415{
1416 environment.Clear();
1417 return false;
1418}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001419
1420const std::vector<ConstString> &
1421Platform::GetTrapHandlerSymbolNames ()
1422{
1423 if (!m_calculated_trap_handlers)
1424 {
Jason Molenda4da87062014-05-23 23:11:27 +00001425 Mutex::Locker locker (m_trap_handler_mutex);
1426 if (!m_calculated_trap_handlers)
1427 {
1428 CalculateTrapHandlerSymbolNames();
1429 m_calculated_trap_handlers = true;
1430 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001431 }
1432 return m_trap_handlers;
1433}
1434