blob: fe73be2d05b91f2dcc3f01ec168e31df92f22b3d [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"
Zachary Turner97a14e62014-08-19 17:18:29 +000024#include "lldb/Host/HostInfo.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000025#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000026#include "lldb/Target/Target.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000027#include "lldb/Utility/Utils.h"
Greg Claytone996fd32011-03-08 22:40:15 +000028
29using namespace lldb;
30using namespace lldb_private;
31
32// Use a singleton function for g_local_platform_sp to avoid init
33// constructors since LLDB is often part of a shared library
34static PlatformSP&
35GetDefaultPlatformSP ()
36{
37 static PlatformSP g_default_platform_sp;
38 return g_default_platform_sp;
39}
40
Greg Claytone996fd32011-03-08 22:40:15 +000041static Mutex &
42GetConnectedPlatformListMutex ()
43{
44 static Mutex g_remote_connected_platforms_mutex (Mutex::eMutexTypeRecursive);
45 return g_remote_connected_platforms_mutex;
46}
47static std::vector<PlatformSP> &
48GetConnectedPlatformList ()
49{
50 static std::vector<PlatformSP> g_remote_connected_platforms;
51 return g_remote_connected_platforms;
52}
53
Greg Claytonab65b342011-04-13 22:47:15 +000054
55const char *
56Platform::GetHostPlatformName ()
57{
58 return "host";
59}
60
Greg Claytone996fd32011-03-08 22:40:15 +000061//------------------------------------------------------------------
62/// Get the native host platform plug-in.
63///
64/// There should only be one of these for each host that LLDB runs
65/// upon that should be statically compiled in and registered using
66/// preprocessor macros or other similar build mechanisms.
67///
68/// This platform will be used as the default platform when launching
69/// or attaching to processes unless another platform is specified.
70//------------------------------------------------------------------
71PlatformSP
72Platform::GetDefaultPlatform ()
73{
74 return GetDefaultPlatformSP ();
75}
76
77void
78Platform::SetDefaultPlatform (const lldb::PlatformSP &platform_sp)
79{
80 // The native platform should use its static void Platform::Initialize()
81 // function to register itself as the native platform.
82 GetDefaultPlatformSP () = platform_sp;
83}
84
Greg Claytone996fd32011-03-08 22:40:15 +000085Error
Steve Puccifc995722014-01-17 18:18:31 +000086Platform::GetFileWithUUID (const FileSpec &platform_file,
87 const UUID *uuid_ptr,
88 FileSpec &local_file)
Greg Claytone996fd32011-03-08 22:40:15 +000089{
90 // Default to the local case
91 local_file = platform_file;
92 return Error();
93}
94
Greg Clayton91c0e742013-01-11 23:44:27 +000095FileSpecList
Enrico Granatafe7295d2014-08-16 00:32:58 +000096Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +000097{
Greg Clayton91c0e742013-01-11 23:44:27 +000098 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +000099}
100
Jason Molenda1c627542013-04-05 01:03:25 +0000101Platform*
Greg Clayton57abc5d2013-05-10 21:47:16 +0000102Platform::FindPlugin (Process *process, const ConstString &plugin_name)
Jason Molenda1c627542013-04-05 01:03:25 +0000103{
104 PlatformCreateInstance create_callback = NULL;
105 if (plugin_name)
106 {
107 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
108 if (create_callback)
109 {
110 ArchSpec arch;
111 if (process)
112 {
113 arch = process->GetTarget().GetArchitecture();
114 }
Greg Clayton7b0992d2013-04-18 22:45:39 +0000115 std::unique_ptr<Platform> instance_ap(create_callback(process, &arch));
Jason Molenda1c627542013-04-05 01:03:25 +0000116 if (instance_ap.get())
117 return instance_ap.release();
118 }
119 }
120 else
121 {
122 for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
123 {
Matt Kopecef143712013-06-03 18:00:07 +0000124 std::unique_ptr<Platform> instance_ap(create_callback(process, nullptr));
Jason Molenda1c627542013-04-05 01:03:25 +0000125 if (instance_ap.get())
126 return instance_ap.release();
127 }
128 }
129 return NULL;
130}
131
Greg Clayton32e0a752011-03-30 18:16:51 +0000132Error
Greg Claytonb9a01b32012-02-26 05:51:37 +0000133Platform::GetSharedModule (const ModuleSpec &module_spec,
Greg Clayton32e0a752011-03-30 18:16:51 +0000134 ModuleSP &module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000135 const FileSpecList *module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000136 ModuleSP *old_module_sp_ptr,
137 bool *did_create_ptr)
138{
139 // Don't do any path remapping for the default implementation
140 // of the platform GetSharedModule function, just call through
141 // to our static ModuleList function. Platform subclasses that
142 // implement remote debugging, might have a developer kits
143 // installed that have cached versions of the files for the
144 // remote target, or might implement a download and cache
145 // locally implementation.
146 const bool always_create = false;
Greg Claytonb9a01b32012-02-26 05:51:37 +0000147 return ModuleList::GetSharedModule (module_spec,
Greg Clayton32e0a752011-03-30 18:16:51 +0000148 module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000149 module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000150 old_module_sp_ptr,
151 did_create_ptr,
152 always_create);
153}
154
Greg Claytone996fd32011-03-08 22:40:15 +0000155PlatformSP
Greg Claytonded470d2011-03-19 01:12:21 +0000156Platform::Create (const char *platform_name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000157{
158 PlatformCreateInstance create_callback = NULL;
159 lldb::PlatformSP platform_sp;
Greg Claytonded470d2011-03-19 01:12:21 +0000160 if (platform_name && platform_name[0])
Greg Claytone996fd32011-03-08 22:40:15 +0000161 {
Greg Clayton57abc5d2013-05-10 21:47:16 +0000162 ConstString const_platform_name (platform_name);
163 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (const_platform_name);
Greg Claytone996fd32011-03-08 22:40:15 +0000164 if (create_callback)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000165 platform_sp.reset(create_callback(true, NULL));
Greg Claytone996fd32011-03-08 22:40:15 +0000166 else
Greg Claytonded470d2011-03-19 01:12:21 +0000167 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", platform_name);
Greg Claytone996fd32011-03-08 22:40:15 +0000168 }
169 else
Greg Claytonded470d2011-03-19 01:12:21 +0000170 error.SetErrorString ("invalid platform name");
Greg Claytone996fd32011-03-08 22:40:15 +0000171 return platform_sp;
172}
173
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000174
175PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000176Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000177{
178 lldb::PlatformSP platform_sp;
179 if (arch.IsValid())
180 {
Greg Clayton70512312012-05-08 01:45:38 +0000181 uint32_t idx;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000182 PlatformCreateInstance create_callback;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000183 // First try exact arch matches across all platform plug-ins
184 bool exact = true;
Greg Clayton70512312012-05-08 01:45:38 +0000185 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000186 {
187 if (create_callback)
Greg Clayton1e0c8842013-01-11 20:49:54 +0000188 {
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000189 platform_sp.reset(create_callback(false, &arch));
Greg Clayton1e0c8842013-01-11 20:49:54 +0000190 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr))
191 return platform_sp;
192 }
193 }
194 // Next try compatible arch matches across all platform plug-ins
195 exact = false;
196 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
197 {
198 if (create_callback)
199 {
200 platform_sp.reset(create_callback(false, &arch));
201 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr))
202 return platform_sp;
203 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000204 }
205 }
206 else
207 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000208 if (platform_arch_ptr)
209 platform_arch_ptr->Clear();
210 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000211 return platform_sp;
212}
213
Greg Claytone996fd32011-03-08 22:40:15 +0000214uint32_t
215Platform::GetNumConnectedRemotePlatforms ()
216{
217 Mutex::Locker locker (GetConnectedPlatformListMutex ());
218 return GetConnectedPlatformList().size();
219}
220
221PlatformSP
222Platform::GetConnectedRemotePlatformAtIndex (uint32_t idx)
223{
224 PlatformSP platform_sp;
225 {
226 Mutex::Locker locker (GetConnectedPlatformListMutex ());
227 if (idx < GetConnectedPlatformList().size())
228 platform_sp = GetConnectedPlatformList ()[idx];
229 }
230 return platform_sp;
231}
232
233//------------------------------------------------------------------
234/// Default Constructor
235//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000236Platform::Platform (bool is_host) :
237 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000238 m_os_version_set_while_connected (false),
239 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000240 m_sdk_sysroot (),
241 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000242 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000243 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000244 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000245 m_major_os_version (UINT32_MAX),
246 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000247 m_update_os_version (UINT32_MAX),
248 m_system_arch(),
249 m_uid_map_mutex (Mutex::eMutexTypeNormal),
250 m_gid_map_mutex (Mutex::eMutexTypeNormal),
251 m_uid_map(),
252 m_gid_map(),
253 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000254 m_max_gid_name_len (0),
255 m_supports_rsync (false),
256 m_rsync_opts (),
257 m_rsync_prefix (),
258 m_supports_ssh (false),
259 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000260 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000261 m_trap_handlers(),
Jason Molenda4da87062014-05-23 23:11:27 +0000262 m_calculated_trap_handlers (false),
263 m_trap_handler_mutex()
Greg Claytone996fd32011-03-08 22:40:15 +0000264{
Greg Clayton5160ce52013-03-27 23:08:40 +0000265 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000266 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000267 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000268}
269
270//------------------------------------------------------------------
271/// Destructor.
272///
273/// The destructor is virtual since this class is designed to be
274/// inherited from by the plug-in instance.
275//------------------------------------------------------------------
276Platform::~Platform()
277{
Greg Clayton5160ce52013-03-27 23:08:40 +0000278 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000279 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000280 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000281}
282
Greg Clayton1cb64962011-03-24 04:28:38 +0000283void
284Platform::GetStatus (Stream &strm)
285{
286 uint32_t major = UINT32_MAX;
287 uint32_t minor = UINT32_MAX;
288 uint32_t update = UINT32_MAX;
289 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000290 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000291
292 ArchSpec arch (GetSystemArchitecture());
293 if (arch.IsValid())
294 {
295 if (!arch.GetTriple().str().empty())
Greg Clayton32e0a752011-03-30 18:16:51 +0000296 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000297 }
298
299 if (GetOSVersion(major, minor, update))
300 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000301 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000302 if (minor != UINT32_MAX)
303 strm.Printf(".%u", minor);
304 if (update != UINT32_MAX)
305 strm.Printf(".%u", update);
306
307 if (GetOSBuildString (s))
308 strm.Printf(" (%s)", s.c_str());
309
310 strm.EOL();
311 }
312
313 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000314 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000315
316 if (IsHost())
317 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000318 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000319 }
320 else
321 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000322 const bool is_connected = IsConnected();
323 if (is_connected)
324 strm.Printf(" Hostname: %s\n", GetHostname());
325 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000326 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000327
Greg Claytonfbb76342013-11-20 21:07:01 +0000328 if (GetWorkingDirectory())
329 {
330 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
331 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000332 if (!IsConnected())
333 return;
334
335 std::string specific_info(GetPlatformSpecificConnectionInformation());
336
337 if (specific_info.empty() == false)
338 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000339}
340
Greg Claytonded470d2011-03-19 01:12:21 +0000341
342bool
343Platform::GetOSVersion (uint32_t &major,
344 uint32_t &minor,
345 uint32_t &update)
346{
347 bool success = m_major_os_version != UINT32_MAX;
348 if (IsHost())
349 {
350 if (!success)
351 {
352 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000353 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000354 m_os_version_set_while_connected = success;
355 }
356 }
357 else
358 {
359 // We have a remote platform. We can only fetch the remote
360 // OS version if we are connected, and we don't want to do it
361 // more than once.
362
363 const bool is_connected = IsConnected();
364
Greg Clayton1cb64962011-03-24 04:28:38 +0000365 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000366 if (success)
367 {
368 // We have valid OS version info, check to make sure it wasn't
369 // manually set prior to connecting. If it was manually set prior
370 // to connecting, then lets fetch the actual OS version info
371 // if we are now connected.
372 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000373 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000374 }
375 else
376 {
377 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000378 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000379 }
380
Greg Clayton1cb64962011-03-24 04:28:38 +0000381 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000382 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000383 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000384 m_os_version_set_while_connected = success;
385 }
386 }
387
388 if (success)
389 {
390 major = m_major_os_version;
391 minor = m_minor_os_version;
392 update = m_update_os_version;
393 }
394 return success;
395}
Greg Clayton1cb64962011-03-24 04:28:38 +0000396
397bool
398Platform::GetOSBuildString (std::string &s)
399{
Zachary Turner97a14e62014-08-19 17:18:29 +0000400 s.clear();
401
Greg Clayton1cb64962011-03-24 04:28:38 +0000402 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000403#if !defined(__linux__)
404 return HostInfo::GetOSBuildString(s);
405#else
406 return false;
407#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000408 else
409 return GetRemoteOSBuildString (s);
410}
411
412bool
413Platform::GetOSKernelDescription (std::string &s)
414{
415 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000416#if !defined(__linux__)
417 return HostInfo::GetOSKernelDescription(s);
418#else
419 return false;
420#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000421 else
422 return GetRemoteOSKernelDescription (s);
423}
424
Greg Clayton57abc5d2013-05-10 21:47:16 +0000425ConstString
Greg Claytonfbb76342013-11-20 21:07:01 +0000426Platform::GetWorkingDirectory ()
427{
428 if (IsHost())
429 {
430 char cwd[PATH_MAX];
431 if (getcwd(cwd, sizeof(cwd)))
432 return ConstString(cwd);
433 else
434 return ConstString();
435 }
436 else
437 {
438 if (!m_working_dir)
439 m_working_dir = GetRemoteWorkingDirectory();
440 return m_working_dir;
441 }
442}
443
444
445struct RecurseCopyBaton
446{
447 const FileSpec& dst;
448 Platform *platform_ptr;
449 Error error;
450};
451
452
453static FileSpec::EnumerateDirectoryResult
454RecurseCopy_Callback (void *baton,
455 FileSpec::FileType file_type,
456 const FileSpec &src)
457{
458 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
459 switch (file_type)
460 {
461 case FileSpec::eFileTypePipe:
462 case FileSpec::eFileTypeSocket:
463 // we have no way to copy pipes and sockets - ignore them and continue
464 return FileSpec::eEnumerateDirectoryResultNext;
465 break;
466
467 case FileSpec::eFileTypeDirectory:
468 {
469 // make the new directory and get in there
470 FileSpec dst_dir = rc_baton->dst;
471 if (!dst_dir.GetFilename())
472 dst_dir.GetFilename() = src.GetLastPathComponent();
473 std::string dst_dir_path (dst_dir.GetPath());
474 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir_path.c_str(), lldb::eFilePermissionsDirectoryDefault);
475 if (error.Fail())
476 {
477 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", dst_dir_path.c_str());
478 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
479 }
480
481 // now recurse
482 std::string src_dir_path (src.GetPath());
483
484 // Make a filespec that only fills in the directory of a FileSpec so
485 // when we enumerate we can quickly fill in the filename for dst copies
486 FileSpec recurse_dst;
487 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
488 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
489 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
490 if (rc_baton2.error.Fail())
491 {
492 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
493 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
494 }
495 return FileSpec::eEnumerateDirectoryResultNext;
496 }
497 break;
498
499 case FileSpec::eFileTypeSymbolicLink:
500 {
501 // copy the file and keep going
502 FileSpec dst_file = rc_baton->dst;
503 if (!dst_file.GetFilename())
504 dst_file.GetFilename() = src.GetFilename();
505
506 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000507
508 rc_baton->error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000509
510 if (rc_baton->error.Fail())
511 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
512
513 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file.GetPath().c_str(), buf);
514
515 if (rc_baton->error.Fail())
516 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
517
518 return FileSpec::eEnumerateDirectoryResultNext;
519 }
520 break;
521 case FileSpec::eFileTypeRegular:
522 {
523 // copy the file and keep going
524 FileSpec dst_file = rc_baton->dst;
525 if (!dst_file.GetFilename())
526 dst_file.GetFilename() = src.GetFilename();
527 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
528 if (err.Fail())
529 {
530 rc_baton->error.SetErrorString(err.AsCString());
531 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
532 }
533 return FileSpec::eEnumerateDirectoryResultNext;
534 }
535 break;
536
537 case FileSpec::eFileTypeInvalid:
538 case FileSpec::eFileTypeOther:
539 case FileSpec::eFileTypeUnknown:
Todd Fialaaf245d12014-06-30 21:05:18 +0000540 default:
Greg Claytonfbb76342013-11-20 21:07:01 +0000541 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
542 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
543 break;
544 }
545}
546
547Error
548Platform::Install (const FileSpec& src, const FileSpec& dst)
549{
550 Error error;
551
552 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
553 if (log)
554 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
555 FileSpec fixed_dst(dst);
556
557 if (!fixed_dst.GetFilename())
558 fixed_dst.GetFilename() = src.GetFilename();
559
560 ConstString working_dir = GetWorkingDirectory();
561
562 if (dst)
563 {
564 if (dst.GetDirectory())
565 {
566 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
567 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
568 {
569 fixed_dst.GetDirectory() = dst.GetDirectory();
570 }
571 // If the fixed destination file doesn't have a directory yet,
572 // then we must have a relative path. We will resolve this relative
573 // path against the platform's working directory
574 if (!fixed_dst.GetDirectory())
575 {
576 FileSpec relative_spec;
577 std::string path;
578 if (working_dir)
579 {
580 relative_spec.SetFile(working_dir.GetCString(), false);
581 relative_spec.AppendPathComponent(dst.GetPath().c_str());
582 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
583 }
584 else
585 {
586 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
587 return error;
588 }
589 }
590 }
591 else
592 {
593 if (working_dir)
594 {
595 fixed_dst.GetDirectory() = working_dir;
596 }
597 else
598 {
599 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
600 return error;
601 }
602 }
603 }
604 else
605 {
606 if (working_dir)
607 {
608 fixed_dst.GetDirectory() = working_dir;
609 }
610 else
611 {
612 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
613 return error;
614 }
615 }
616
617 if (log)
618 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
619
620 if (GetSupportsRSync())
621 {
622 error = PutFile(src, dst);
623 }
624 else
625 {
626 switch (src.GetFileType())
627 {
628 case FileSpec::eFileTypeDirectory:
629 {
630 if (GetFileExists (fixed_dst))
631 Unlink (fixed_dst.GetPath().c_str());
632 uint32_t permissions = src.GetPermissions();
633 if (permissions == 0)
634 permissions = eFilePermissionsDirectoryDefault;
635 std::string dst_dir_path(fixed_dst.GetPath());
636 error = MakeDirectory(dst_dir_path.c_str(), permissions);
637 if (error.Success())
638 {
639 // Make a filespec that only fills in the directory of a FileSpec so
640 // when we enumerate we can quickly fill in the filename for dst copies
641 FileSpec recurse_dst;
642 recurse_dst.GetDirectory().SetCString(dst_dir_path.c_str());
643 std::string src_dir_path (src.GetPath());
644 RecurseCopyBaton baton = { recurse_dst, this, Error() };
645 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
646 return baton.error;
647 }
648 }
649 break;
650
651 case FileSpec::eFileTypeRegular:
652 if (GetFileExists (fixed_dst))
653 Unlink (fixed_dst.GetPath().c_str());
654 error = PutFile(src, fixed_dst);
655 break;
656
657 case FileSpec::eFileTypeSymbolicLink:
658 {
659 if (GetFileExists (fixed_dst))
660 Unlink (fixed_dst.GetPath().c_str());
661 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000662 error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000663 if (error.Success())
664 error = CreateSymlink(dst.GetPath().c_str(), buf);
665 }
666 break;
667 case FileSpec::eFileTypePipe:
668 error.SetErrorString("platform install doesn't handle pipes");
669 break;
670 case FileSpec::eFileTypeSocket:
671 error.SetErrorString("platform install doesn't handle sockets");
672 break;
673 case FileSpec::eFileTypeInvalid:
674 case FileSpec::eFileTypeUnknown:
675 case FileSpec::eFileTypeOther:
676 error.SetErrorString("platform install doesn't handle non file or directory items");
677 break;
678 }
679 }
680 return error;
681}
682
683bool
684Platform::SetWorkingDirectory (const ConstString &path)
685{
686 if (IsHost())
687 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000688 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
689 if (log)
690 log->Printf("Platform::SetWorkingDirectory('%s')", path.GetCString());
Colin Riley909bb7a2013-11-26 15:10:46 +0000691#ifdef _WIN32
692 // Not implemented on Windows
693 return false;
694#else
Greg Claytonfbb76342013-11-20 21:07:01 +0000695 if (path)
696 {
697 if (chdir(path.GetCString()) == 0)
698 return true;
699 }
700 return false;
Colin Riley909bb7a2013-11-26 15:10:46 +0000701#endif
Greg Claytonfbb76342013-11-20 21:07:01 +0000702 }
703 else
704 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000705 m_working_dir.Clear();
Greg Claytonfbb76342013-11-20 21:07:01 +0000706 return SetRemoteWorkingDirectory(path);
707 }
708}
709
710Error
711Platform::MakeDirectory (const char *path, uint32_t permissions)
712{
713 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000714 return FileSystem::MakeDirectory(path, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000715 else
716 {
717 Error error;
718 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
719 return error;
720 }
721}
722
723Error
724Platform::GetFilePermissions (const char *path, uint32_t &file_permissions)
725{
726 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000727 return FileSystem::GetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000728 else
729 {
730 Error error;
731 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
732 return error;
733 }
734}
735
736Error
737Platform::SetFilePermissions (const char *path, uint32_t file_permissions)
738{
739 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000740 return FileSystem::SetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000741 else
742 {
743 Error error;
744 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
745 return error;
746 }
747}
748
749ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000750Platform::GetName ()
751{
Greg Claytonfbb76342013-11-20 21:07:01 +0000752 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000753}
754
755const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000756Platform::GetHostname ()
757{
Greg Claytonab65b342011-04-13 22:47:15 +0000758 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000759 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000760
761 if (m_name.empty())
762 return NULL;
763 return m_name.c_str();
764}
765
Greg Clayton5fb8f792013-12-02 19:35:49 +0000766bool
767Platform::SetRemoteWorkingDirectory(const ConstString &path)
768{
769 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
770 if (log)
771 log->Printf("Platform::SetRemoteWorkingDirectory('%s')", path.GetCString());
772 m_working_dir = path;
773 return true;
774}
775
Greg Clayton32e0a752011-03-30 18:16:51 +0000776const char *
777Platform::GetUserName (uint32_t uid)
778{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000779#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000780 const char *user_name = GetCachedUserName(uid);
781 if (user_name)
782 return user_name;
783 if (IsHost())
784 {
785 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000786 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000787 return SetCachedUserName (uid, name.c_str(), name.size());
788 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000789#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000790 return NULL;
791}
792
Greg Clayton32e0a752011-03-30 18:16:51 +0000793const char *
794Platform::GetGroupName (uint32_t gid)
795{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000796#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000797 const char *group_name = GetCachedGroupName(gid);
798 if (group_name)
799 return group_name;
800 if (IsHost())
801 {
802 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000803 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000804 return SetCachedGroupName (gid, name.c_str(), name.size());
805 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000806#endif
Greg Clayton32e0a752011-03-30 18:16:51 +0000807 return NULL;
808}
Greg Clayton1cb64962011-03-24 04:28:38 +0000809
Greg Claytonded470d2011-03-19 01:12:21 +0000810bool
811Platform::SetOSVersion (uint32_t major,
812 uint32_t minor,
813 uint32_t update)
814{
815 if (IsHost())
816 {
Zachary Turner97a14e62014-08-19 17:18:29 +0000817 // We don't need anyone setting the OS version for the host platform,
818 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000819 return false;
820 }
821 else
822 {
823 // We have a remote platform, allow setting the target OS version if
824 // we aren't connected, since if we are connected, we should be able to
825 // request the remote OS version from the connected platform.
826 if (IsConnected())
827 return false;
828 else
829 {
830 // We aren't connected and we might want to set the OS version
831 // ahead of time before we connect so we can peruse files and
832 // use a local SDK or PDK cache of support files to disassemble
833 // or do other things.
834 m_major_os_version = major;
835 m_minor_os_version = minor;
836 m_update_os_version = update;
837 return true;
838 }
839 }
840 return false;
841}
842
843
Greg Claytone996fd32011-03-08 22:40:15 +0000844Error
845Platform::ResolveExecutable (const FileSpec &exe_file,
846 const ArchSpec &exe_arch,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000847 lldb::ModuleSP &exe_module_sp,
848 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +0000849{
850 Error error;
851 if (exe_file.Exists())
852 {
Greg Claytonb9a01b32012-02-26 05:51:37 +0000853 ModuleSpec module_spec (exe_file, exe_arch);
854 if (module_spec.GetArchitecture().IsValid())
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 }
862 else
863 {
864 // No valid architecture was specified, ask the platform for
865 // the architectures that we should be using (in the correct order)
866 // and see if we can find a match that way
Greg Claytonb9a01b32012-02-26 05:51:37 +0000867 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +0000868 {
Greg Claytonb9a01b32012-02-26 05:51:37 +0000869 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +0000870 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000871 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +0000872 NULL,
873 NULL);
874 // Did we find an executable using one of the
875 if (error.Success() && exe_module_sp)
876 break;
877 }
878 }
879 }
880 else
881 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000882 error.SetErrorStringWithFormat ("'%s' does not exist",
883 exe_file.GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +0000884 }
885 return error;
886}
887
Greg Clayton103f0282012-09-12 02:03:59 +0000888Error
889Platform::ResolveSymbolFile (Target &target,
890 const ModuleSpec &sym_spec,
891 FileSpec &sym_file)
892{
893 Error error;
894 if (sym_spec.GetSymbolFileSpec().Exists())
895 sym_file = sym_spec.GetSymbolFileSpec();
896 else
897 error.SetErrorString("unable to resolve symbol file");
898 return error;
899
900}
901
902
903
Greg Claytonaa516842011-08-11 16:25:18 +0000904bool
905Platform::ResolveRemotePath (const FileSpec &platform_path,
906 FileSpec &resolved_platform_path)
907{
908 resolved_platform_path = platform_path;
909 return resolved_platform_path.ResolvePath();
910}
911
Greg Claytonded470d2011-03-19 01:12:21 +0000912
913const ArchSpec &
914Platform::GetSystemArchitecture()
915{
916 if (IsHost())
917 {
918 if (!m_system_arch.IsValid())
919 {
920 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +0000921 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +0000922 m_system_arch_set_while_connected = m_system_arch.IsValid();
923 }
924 }
925 else
926 {
927 // We have a remote platform. We can only fetch the remote
928 // system architecture if we are connected, and we don't want to do it
929 // more than once.
930
931 const bool is_connected = IsConnected();
932
933 bool fetch = false;
934 if (m_system_arch.IsValid())
935 {
936 // We have valid OS version info, check to make sure it wasn't
937 // manually set prior to connecting. If it was manually set prior
938 // to connecting, then lets fetch the actual OS version info
939 // if we are now connected.
940 if (is_connected && !m_system_arch_set_while_connected)
941 fetch = true;
942 }
943 else
944 {
945 // We don't have valid OS version info, fetch it if we are connected
946 fetch = is_connected;
947 }
948
949 if (fetch)
950 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000951 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +0000952 m_system_arch_set_while_connected = m_system_arch.IsValid();
953 }
954 }
955 return m_system_arch;
956}
957
958
Greg Claytone996fd32011-03-08 22:40:15 +0000959Error
Greg Claytond314e812011-03-23 00:09:55 +0000960Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +0000961{
962 Error error;
Greg Claytond314e812011-03-23 00:09:55 +0000963 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +0000964 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +0000965 else
Greg Clayton57abc5d2013-05-10 21:47:16 +0000966 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000967 return error;
968}
969
970Error
Greg Claytond314e812011-03-23 00:09:55 +0000971Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +0000972{
973 Error error;
Greg Claytond314e812011-03-23 00:09:55 +0000974 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +0000975 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +0000976 else
Greg Clayton57abc5d2013-05-10 21:47:16 +0000977 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000978 return error;
979}
Greg Clayton32e0a752011-03-30 18:16:51 +0000980
981bool
Greg Clayton8b82f082011-04-12 05:54:46 +0000982Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +0000983{
984 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +0000985 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +0000986 if (IsHost())
987 return Host::GetProcessInfo (pid, process_info);
988 return false;
989}
990
991uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +0000992Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
993 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +0000994{
Greg Clayton8b82f082011-04-12 05:54:46 +0000995 // Take care of the host case so that each subclass can just
996 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +0000997 uint32_t match_count = 0;
998 if (IsHost())
999 match_count = Host::FindProcesses (match_info, process_infos);
1000 return match_count;
1001}
Greg Clayton8b82f082011-04-12 05:54:46 +00001002
1003
1004Error
1005Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1006{
1007 Error error;
1008 // Take care of the host case so that each subclass can just
1009 // call this function to get the host functionality.
1010 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001011 {
1012 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1013 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1014
1015 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1016 {
1017 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001018 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1019 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001020 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Greg Claytond1cf11a2012-04-14 01:42:46 +00001021 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1022 is_localhost,
1023 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001024 first_arg_is_full_shell_command,
1025 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001026 return error;
1027 }
1028
Greg Clayton8b82f082011-04-12 05:54:46 +00001029 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001030 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001031 else
1032 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1033 return error;
1034}
1035
1036lldb::ProcessSP
1037Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1038 Debugger &debugger,
1039 Target *target, // Can be NULL, if NULL create a new target, else use existing one
1040 Listener &listener,
1041 Error &error)
1042{
1043 ProcessSP process_sp;
1044 // Make sure we stop at the entry point
1045 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001046 // We always launch the process we are going to debug in a separate process
1047 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1048 // about the target getting them as well.
1049 launch_info.SetLaunchInSeparateProcessGroup(true);
1050
Greg Clayton8b82f082011-04-12 05:54:46 +00001051 error = LaunchProcess (launch_info);
1052 if (error.Success())
1053 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001054 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001055 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001056 ProcessAttachInfo attach_info (launch_info);
1057 process_sp = Attach (attach_info, debugger, target, listener, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001058 if (process_sp)
1059 {
Greg Clayton44d93782014-01-27 23:43:24 +00001060 launch_info.SetHijackListener(attach_info.GetHijackListener());
1061
Greg Claytone24c4ac2011-11-17 04:46:02 +00001062 // Since we attached to the process, it will think it needs to detach
1063 // if the process object just goes away without an explicit call to
1064 // Process::Kill() or Process::Detach(), so let it know to kill the
1065 // process if this happens.
1066 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001067
1068 // If we didn't have any file actions, the pseudo terminal might
1069 // have been used where the slave side was given as the file to
1070 // open for stdin/out/err after we have already opened the master
1071 // so we can read/write stdin/out/err.
1072 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1073 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1074 {
1075 process_sp->SetSTDIOFileDescriptor(pty_fd);
1076 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001077 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001078 }
1079 }
1080 return process_sp;
1081}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001082
1083
1084lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001085Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001086{
1087 lldb::PlatformSP platform_sp;
1088 Error error;
1089 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001090 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001091 return platform_sp;
1092}
1093
1094
1095//------------------------------------------------------------------
1096/// Lets a platform answer if it is compatible with a given
1097/// architecture and the target triple contained within.
1098//------------------------------------------------------------------
1099bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001100Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001101{
1102 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001103 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001104 {
Greg Clayton70512312012-05-08 01:45:38 +00001105 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001106 // Try for an exact architecture match first.
1107 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001108 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001109 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001110 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001111 if (arch.IsExactMatch(platform_arch))
1112 {
1113 if (compatible_arch_ptr)
1114 *compatible_arch_ptr = platform_arch;
1115 return true;
1116 }
1117 }
1118 }
1119 else
1120 {
1121 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1122 {
1123 if (arch.IsCompatibleMatch(platform_arch))
1124 {
1125 if (compatible_arch_ptr)
1126 *compatible_arch_ptr = platform_arch;
1127 return true;
1128 }
Greg Clayton70512312012-05-08 01:45:38 +00001129 }
1130 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001131 }
Greg Clayton70512312012-05-08 01:45:38 +00001132 if (compatible_arch_ptr)
1133 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001134 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001135}
1136
Daniel Maleae0f8f572013-08-26 23:57:52 +00001137Error
1138Platform::PutFile (const FileSpec& source,
1139 const FileSpec& destination,
1140 uint32_t uid,
1141 uint32_t gid)
1142{
1143 Error error("unimplemented");
1144 return error;
1145}
1146
1147Error
1148Platform::GetFile (const FileSpec& source,
1149 const FileSpec& destination)
1150{
1151 Error error("unimplemented");
1152 return error;
1153}
1154
Greg Claytonfbb76342013-11-20 21:07:01 +00001155Error
1156Platform::CreateSymlink (const char *src, // The name of the link is in src
1157 const char *dst)// The symlink points to dst
1158{
1159 Error error("unimplemented");
1160 return error;
1161}
1162
Daniel Maleae0f8f572013-08-26 23:57:52 +00001163bool
1164Platform::GetFileExists (const lldb_private::FileSpec& file_spec)
1165{
1166 return false;
1167}
1168
Greg Claytonfbb76342013-11-20 21:07:01 +00001169Error
1170Platform::Unlink (const char *path)
1171{
1172 Error error("unimplemented");
1173 return error;
1174}
1175
1176
1177
Daniel Maleae0f8f572013-08-26 23:57:52 +00001178lldb_private::Error
1179Platform::RunShellCommand (const char *command, // Shouldn't be NULL
1180 const char *working_dir, // Pass NULL to use the current working directory
1181 int *status_ptr, // Pass NULL if you don't want the process exit status
1182 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1183 std::string *command_output, // Pass NULL if you don't want the command output
1184 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
1185{
1186 if (IsHost())
1187 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1188 else
1189 return Error("unimplemented");
1190}
1191
1192
1193bool
1194Platform::CalculateMD5 (const FileSpec& file_spec,
1195 uint64_t &low,
1196 uint64_t &high)
1197{
1198 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001199 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001200 else
1201 return false;
1202}
1203
Todd Fialaaf245d12014-06-30 21:05:18 +00001204Error
1205Platform::LaunchNativeProcess (
1206 ProcessLaunchInfo &launch_info,
1207 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1208 NativeProcessProtocolSP &process_sp)
1209{
1210 // Platforms should override this implementation if they want to
1211 // support lldb-gdbserver.
1212 return Error("unimplemented");
1213}
1214
1215Error
1216Platform::AttachNativeProcess (lldb::pid_t pid,
1217 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1218 NativeProcessProtocolSP &process_sp)
1219{
1220 // Platforms should override this implementation if they want to
1221 // support lldb-gdbserver.
1222 return Error("unimplemented");
1223}
1224
Daniel Maleae0f8f572013-08-26 23:57:52 +00001225void
1226Platform::SetLocalCacheDirectory (const char* local)
1227{
1228 m_local_cache_directory.assign(local);
1229}
1230
1231const char*
1232Platform::GetLocalCacheDirectory ()
1233{
1234 return m_local_cache_directory.c_str();
1235}
1236
1237static OptionDefinition
1238g_rsync_option_table[] =
1239{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001240 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1241 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1242 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1243 { 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 +00001244};
1245
1246static OptionDefinition
1247g_ssh_option_table[] =
1248{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001249 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1250 { 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 +00001251};
1252
1253static OptionDefinition
1254g_caching_option_table[] =
1255{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001256 { 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 +00001257};
1258
1259OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1260{
1261}
1262
1263OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1264{
1265}
1266
1267const lldb_private::OptionDefinition*
1268OptionGroupPlatformRSync::GetDefinitions ()
1269{
1270 return g_rsync_option_table;
1271}
1272
1273void
1274OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1275{
1276 m_rsync = false;
1277 m_rsync_opts.clear();
1278 m_rsync_prefix.clear();
1279 m_ignores_remote_hostname = false;
1280}
1281
1282lldb_private::Error
1283OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1284 uint32_t option_idx,
1285 const char *option_arg)
1286{
1287 Error error;
1288 char short_option = (char) GetDefinitions()[option_idx].short_option;
1289 switch (short_option)
1290 {
1291 case 'r':
1292 m_rsync = true;
1293 break;
1294
1295 case 'R':
1296 m_rsync_opts.assign(option_arg);
1297 break;
1298
1299 case 'P':
1300 m_rsync_prefix.assign(option_arg);
1301 break;
1302
1303 case 'i':
1304 m_ignores_remote_hostname = true;
1305 break;
1306
1307 default:
1308 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1309 break;
1310 }
1311
1312 return error;
1313}
1314
1315uint32_t
1316OptionGroupPlatformRSync::GetNumDefinitions ()
1317{
1318 return llvm::array_lengthof(g_rsync_option_table);
1319}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001320
Greg Clayton4116e932012-05-15 02:33:01 +00001321lldb::BreakpointSP
1322Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1323{
1324 return lldb::BreakpointSP();
1325}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001326
Daniel Maleae0f8f572013-08-26 23:57:52 +00001327OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1328{
1329}
1330
1331OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1332{
1333}
1334
1335const lldb_private::OptionDefinition*
1336OptionGroupPlatformSSH::GetDefinitions ()
1337{
1338 return g_ssh_option_table;
1339}
1340
1341void
1342OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1343{
1344 m_ssh = false;
1345 m_ssh_opts.clear();
1346}
1347
1348lldb_private::Error
1349OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1350 uint32_t option_idx,
1351 const char *option_arg)
1352{
1353 Error error;
1354 char short_option = (char) GetDefinitions()[option_idx].short_option;
1355 switch (short_option)
1356 {
1357 case 's':
1358 m_ssh = true;
1359 break;
1360
1361 case 'S':
1362 m_ssh_opts.assign(option_arg);
1363 break;
1364
1365 default:
1366 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1367 break;
1368 }
1369
1370 return error;
1371}
1372
1373uint32_t
1374OptionGroupPlatformSSH::GetNumDefinitions ()
1375{
1376 return llvm::array_lengthof(g_ssh_option_table);
1377}
1378
1379OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1380{
1381}
1382
1383OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1384{
1385}
1386
1387const lldb_private::OptionDefinition*
1388OptionGroupPlatformCaching::GetDefinitions ()
1389{
1390 return g_caching_option_table;
1391}
1392
1393void
1394OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1395{
1396 m_cache_dir.clear();
1397}
1398
1399lldb_private::Error
1400OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1401 uint32_t option_idx,
1402 const char *option_arg)
1403{
1404 Error error;
1405 char short_option = (char) GetDefinitions()[option_idx].short_option;
1406 switch (short_option)
1407 {
1408 case 'c':
1409 m_cache_dir.assign(option_arg);
1410 break;
1411
1412 default:
1413 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1414 break;
1415 }
1416
1417 return error;
1418}
1419
1420uint32_t
1421OptionGroupPlatformCaching::GetNumDefinitions ()
1422{
1423 return llvm::array_lengthof(g_caching_option_table);
1424}
1425
Greg Clayton67cc0632012-08-22 17:17:09 +00001426size_t
1427Platform::GetEnvironment (StringList &environment)
1428{
1429 environment.Clear();
1430 return false;
1431}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001432
1433const std::vector<ConstString> &
1434Platform::GetTrapHandlerSymbolNames ()
1435{
1436 if (!m_calculated_trap_handlers)
1437 {
Jason Molenda4da87062014-05-23 23:11:27 +00001438 Mutex::Locker locker (m_trap_handler_mutex);
1439 if (!m_calculated_trap_handlers)
1440 {
1441 CalculateTrapHandlerSymbolNames();
1442 m_calculated_trap_handlers = true;
1443 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001444 }
1445 return m_trap_handlers;
1446}
1447