blob: 83a8769953f00011a7a6248addc314170baef9aa [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&
Greg Clayton615eb7e2014-09-19 20:11:50 +000035GetHostPlatformSP ()
Greg Claytone996fd32011-03-08 22:40:15 +000036{
Greg Clayton615eb7e2014-09-19 20:11:50 +000037 static PlatformSP g_platform_sp;
38 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000039}
40
Greg Claytonab65b342011-04-13 22:47:15 +000041const char *
42Platform::GetHostPlatformName ()
43{
44 return "host";
45}
46
Greg Claytone996fd32011-03-08 22:40:15 +000047//------------------------------------------------------------------
48/// Get the native host platform plug-in.
49///
50/// There should only be one of these for each host that LLDB runs
51/// upon that should be statically compiled in and registered using
52/// preprocessor macros or other similar build mechanisms.
53///
54/// This platform will be used as the default platform when launching
55/// or attaching to processes unless another platform is specified.
56//------------------------------------------------------------------
57PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +000058Platform::GetHostPlatform ()
Greg Claytone996fd32011-03-08 22:40:15 +000059{
Greg Clayton615eb7e2014-09-19 20:11:50 +000060 return GetHostPlatformSP ();
61}
62
63static std::vector<PlatformSP> &
64GetPlatformList()
65{
66 static std::vector<PlatformSP> g_platform_list;
67 return g_platform_list;
68}
69
70static Mutex &
71GetPlatformListMutex ()
72{
73 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
74 return g_mutex;
Greg Claytone996fd32011-03-08 22:40:15 +000075}
76
77void
Greg Clayton615eb7e2014-09-19 20:11:50 +000078Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp)
Greg Claytone996fd32011-03-08 22:40:15 +000079{
80 // The native platform should use its static void Platform::Initialize()
81 // function to register itself as the native platform.
Greg Clayton615eb7e2014-09-19 20:11:50 +000082 GetHostPlatformSP () = platform_sp;
83
84 if (platform_sp)
85 {
86 Mutex::Locker locker(GetPlatformListMutex ());
87 GetPlatformList().push_back(platform_sp);
88 }
Greg Claytone996fd32011-03-08 22:40:15 +000089}
90
Greg Claytone996fd32011-03-08 22:40:15 +000091Error
Steve Puccifc995722014-01-17 18:18:31 +000092Platform::GetFileWithUUID (const FileSpec &platform_file,
93 const UUID *uuid_ptr,
94 FileSpec &local_file)
Greg Claytone996fd32011-03-08 22:40:15 +000095{
96 // Default to the local case
97 local_file = platform_file;
98 return Error();
99}
100
Greg Clayton91c0e742013-01-11 23:44:27 +0000101FileSpecList
Enrico Granatafe7295d2014-08-16 00:32:58 +0000102Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +0000103{
Greg Clayton91c0e742013-01-11 23:44:27 +0000104 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000105}
106
Greg Clayton615eb7e2014-09-19 20:11:50 +0000107//PlatformSP
108//Platform::FindPlugin (Process *process, const ConstString &plugin_name)
109//{
110// PlatformCreateInstance create_callback = NULL;
111// if (plugin_name)
112// {
113// create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
114// if (create_callback)
115// {
116// ArchSpec arch;
117// if (process)
118// {
119// arch = process->GetTarget().GetArchitecture();
120// }
121// PlatformSP platform_sp(create_callback(process, &arch));
122// if (platform_sp)
123// return platform_sp;
124// }
125// }
126// else
127// {
128// for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
129// {
130// PlatformSP platform_sp(create_callback(process, nullptr));
131// if (platform_sp)
132// return platform_sp;
133// }
134// }
135// return PlatformSP();
136//}
Jason Molenda1c627542013-04-05 01:03:25 +0000137
Greg Clayton32e0a752011-03-30 18:16:51 +0000138Error
Greg Claytonb9a01b32012-02-26 05:51:37 +0000139Platform::GetSharedModule (const ModuleSpec &module_spec,
Greg Clayton32e0a752011-03-30 18:16:51 +0000140 ModuleSP &module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000141 const FileSpecList *module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000142 ModuleSP *old_module_sp_ptr,
143 bool *did_create_ptr)
144{
145 // Don't do any path remapping for the default implementation
146 // of the platform GetSharedModule function, just call through
147 // to our static ModuleList function. Platform subclasses that
148 // implement remote debugging, might have a developer kits
149 // installed that have cached versions of the files for the
150 // remote target, or might implement a download and cache
151 // locally implementation.
152 const bool always_create = false;
Greg Claytonb9a01b32012-02-26 05:51:37 +0000153 return ModuleList::GetSharedModule (module_spec,
Greg Clayton32e0a752011-03-30 18:16:51 +0000154 module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000155 module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000156 old_module_sp_ptr,
157 did_create_ptr,
158 always_create);
159}
160
Greg Claytone996fd32011-03-08 22:40:15 +0000161PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000162Platform::Find (const ConstString &name)
163{
164 if (name)
165 {
166 static ConstString g_host_platform_name ("host");
167 if (name == g_host_platform_name)
168 return GetHostPlatform();
169
170 Mutex::Locker locker(GetPlatformListMutex ());
171 for (const auto &platform_sp : GetPlatformList())
172 {
173 if (platform_sp->GetName() == name)
174 return platform_sp;
175 }
176 }
177 return PlatformSP();
178}
179
180PlatformSP
181Platform::Create (const ConstString &name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000182{
183 PlatformCreateInstance create_callback = NULL;
184 lldb::PlatformSP platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000185 if (name)
Greg Claytone996fd32011-03-08 22:40:15 +0000186 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000187 static ConstString g_host_platform_name ("host");
188 if (name == g_host_platform_name)
189 return GetHostPlatform();
190
191 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name);
Greg Claytone996fd32011-03-08 22:40:15 +0000192 if (create_callback)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000193 platform_sp = create_callback(true, NULL);
Greg Claytone996fd32011-03-08 22:40:15 +0000194 else
Greg Clayton615eb7e2014-09-19 20:11:50 +0000195 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000196 }
197 else
Greg Claytonded470d2011-03-19 01:12:21 +0000198 error.SetErrorString ("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000199
200 if (platform_sp)
201 {
202 Mutex::Locker locker(GetPlatformListMutex ());
203 GetPlatformList().push_back(platform_sp);
204 }
205
Greg Claytone996fd32011-03-08 22:40:15 +0000206 return platform_sp;
207}
208
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000209
210PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000211Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000212{
213 lldb::PlatformSP platform_sp;
214 if (arch.IsValid())
215 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000216 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000217 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000218 // First try exact arch matches across all platforms already created
219 Mutex::Locker locker(GetPlatformListMutex ());
220 for (const auto &platform_sp : GetPlatformList())
Greg Clayton1e0c8842013-01-11 20:49:54 +0000221 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000222 if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
223 return platform_sp;
224 }
225
226 // Next try compatible arch matches across all platforms already created
227 for (const auto &platform_sp : GetPlatformList())
228 {
229 if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
Greg Clayton1e0c8842013-01-11 20:49:54 +0000230 return platform_sp;
231 }
232 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000233
234 PlatformCreateInstance create_callback;
235 // First try exact arch matches across all platform plug-ins
236 uint32_t idx;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000237 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
238 {
239 if (create_callback)
240 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000241 platform_sp = create_callback(false, &arch);
242 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
243 {
244 Mutex::Locker locker(GetPlatformListMutex ());
245 GetPlatformList().push_back(platform_sp);
Greg Clayton1e0c8842013-01-11 20:49:54 +0000246 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000247 }
248 }
249 }
250 // Next try compatible arch matches across all platform plug-ins
251 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
252 {
253 if (create_callback)
254 {
255 platform_sp = create_callback(false, &arch);
256 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
257 {
258 Mutex::Locker locker(GetPlatformListMutex ());
259 GetPlatformList().push_back(platform_sp);
260 return platform_sp;
261 }
Greg Clayton1e0c8842013-01-11 20:49:54 +0000262 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000263 }
264 }
265 else
266 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000267 if (platform_arch_ptr)
268 platform_arch_ptr->Clear();
269 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000270 return platform_sp;
271}
272
Greg Claytone996fd32011-03-08 22:40:15 +0000273//------------------------------------------------------------------
274/// Default Constructor
275//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000276Platform::Platform (bool is_host) :
277 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000278 m_os_version_set_while_connected (false),
279 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000280 m_sdk_sysroot (),
281 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000282 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000283 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000284 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000285 m_major_os_version (UINT32_MAX),
286 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000287 m_update_os_version (UINT32_MAX),
288 m_system_arch(),
289 m_uid_map_mutex (Mutex::eMutexTypeNormal),
290 m_gid_map_mutex (Mutex::eMutexTypeNormal),
291 m_uid_map(),
292 m_gid_map(),
293 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000294 m_max_gid_name_len (0),
295 m_supports_rsync (false),
296 m_rsync_opts (),
297 m_rsync_prefix (),
298 m_supports_ssh (false),
299 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000300 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000301 m_trap_handlers(),
Jason Molenda4da87062014-05-23 23:11:27 +0000302 m_calculated_trap_handlers (false),
303 m_trap_handler_mutex()
Greg Claytone996fd32011-03-08 22:40:15 +0000304{
Greg Clayton5160ce52013-03-27 23:08:40 +0000305 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000306 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000307 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000308}
309
310//------------------------------------------------------------------
311/// Destructor.
312///
313/// The destructor is virtual since this class is designed to be
314/// inherited from by the plug-in instance.
315//------------------------------------------------------------------
316Platform::~Platform()
317{
Greg Clayton5160ce52013-03-27 23:08:40 +0000318 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000319 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000320 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000321}
322
Greg Clayton1cb64962011-03-24 04:28:38 +0000323void
324Platform::GetStatus (Stream &strm)
325{
326 uint32_t major = UINT32_MAX;
327 uint32_t minor = UINT32_MAX;
328 uint32_t update = UINT32_MAX;
329 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000330 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000331
332 ArchSpec arch (GetSystemArchitecture());
333 if (arch.IsValid())
334 {
335 if (!arch.GetTriple().str().empty())
Greg Clayton32e0a752011-03-30 18:16:51 +0000336 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000337 }
338
339 if (GetOSVersion(major, minor, update))
340 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000341 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000342 if (minor != UINT32_MAX)
343 strm.Printf(".%u", minor);
344 if (update != UINT32_MAX)
345 strm.Printf(".%u", update);
346
347 if (GetOSBuildString (s))
348 strm.Printf(" (%s)", s.c_str());
349
350 strm.EOL();
351 }
352
353 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000354 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000355
356 if (IsHost())
357 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000358 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000359 }
360 else
361 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000362 const bool is_connected = IsConnected();
363 if (is_connected)
364 strm.Printf(" Hostname: %s\n", GetHostname());
365 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000366 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000367
Greg Claytonfbb76342013-11-20 21:07:01 +0000368 if (GetWorkingDirectory())
369 {
370 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
371 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000372 if (!IsConnected())
373 return;
374
375 std::string specific_info(GetPlatformSpecificConnectionInformation());
376
377 if (specific_info.empty() == false)
378 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000379}
380
Greg Claytonded470d2011-03-19 01:12:21 +0000381
382bool
383Platform::GetOSVersion (uint32_t &major,
384 uint32_t &minor,
385 uint32_t &update)
386{
387 bool success = m_major_os_version != UINT32_MAX;
388 if (IsHost())
389 {
390 if (!success)
391 {
392 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000393 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000394 m_os_version_set_while_connected = success;
395 }
396 }
397 else
398 {
399 // We have a remote platform. We can only fetch the remote
400 // OS version if we are connected, and we don't want to do it
401 // more than once.
402
403 const bool is_connected = IsConnected();
404
Greg Clayton1cb64962011-03-24 04:28:38 +0000405 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000406 if (success)
407 {
408 // We have valid OS version info, check to make sure it wasn't
409 // manually set prior to connecting. If it was manually set prior
410 // to connecting, then lets fetch the actual OS version info
411 // if we are now connected.
412 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000413 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000414 }
415 else
416 {
417 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000418 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000419 }
420
Greg Clayton1cb64962011-03-24 04:28:38 +0000421 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000422 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000423 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000424 m_os_version_set_while_connected = success;
425 }
426 }
427
428 if (success)
429 {
430 major = m_major_os_version;
431 minor = m_minor_os_version;
432 update = m_update_os_version;
433 }
434 return success;
435}
Greg Clayton1cb64962011-03-24 04:28:38 +0000436
437bool
438Platform::GetOSBuildString (std::string &s)
439{
Zachary Turner97a14e62014-08-19 17:18:29 +0000440 s.clear();
441
Greg Clayton1cb64962011-03-24 04:28:38 +0000442 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000443#if !defined(__linux__)
444 return HostInfo::GetOSBuildString(s);
445#else
446 return false;
447#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000448 else
449 return GetRemoteOSBuildString (s);
450}
451
452bool
453Platform::GetOSKernelDescription (std::string &s)
454{
455 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000456#if !defined(__linux__)
457 return HostInfo::GetOSKernelDescription(s);
458#else
459 return false;
460#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000461 else
462 return GetRemoteOSKernelDescription (s);
463}
464
Sean Callanan5dc29812014-12-05 01:16:31 +0000465void
Greg Claytoncd6bbba2015-01-22 18:25:49 +0000466Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options)
Sean Callanan5dc29812014-12-05 01:16:31 +0000467{
468 std::vector<std::string> default_compilation_options =
469 {
470 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"
471 };
472
473 options.insert(options.end(),
474 default_compilation_options.begin(),
475 default_compilation_options.end());
476}
477
478
Greg Clayton57abc5d2013-05-10 21:47:16 +0000479ConstString
Greg Claytonfbb76342013-11-20 21:07:01 +0000480Platform::GetWorkingDirectory ()
481{
482 if (IsHost())
483 {
484 char cwd[PATH_MAX];
485 if (getcwd(cwd, sizeof(cwd)))
486 return ConstString(cwd);
487 else
488 return ConstString();
489 }
490 else
491 {
492 if (!m_working_dir)
493 m_working_dir = GetRemoteWorkingDirectory();
494 return m_working_dir;
495 }
496}
497
498
499struct RecurseCopyBaton
500{
501 const FileSpec& dst;
502 Platform *platform_ptr;
503 Error error;
504};
505
506
507static FileSpec::EnumerateDirectoryResult
508RecurseCopy_Callback (void *baton,
509 FileSpec::FileType file_type,
510 const FileSpec &src)
511{
512 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
513 switch (file_type)
514 {
515 case FileSpec::eFileTypePipe:
516 case FileSpec::eFileTypeSocket:
517 // we have no way to copy pipes and sockets - ignore them and continue
518 return FileSpec::eEnumerateDirectoryResultNext;
519 break;
520
521 case FileSpec::eFileTypeDirectory:
522 {
523 // make the new directory and get in there
524 FileSpec dst_dir = rc_baton->dst;
525 if (!dst_dir.GetFilename())
526 dst_dir.GetFilename() = src.GetLastPathComponent();
527 std::string dst_dir_path (dst_dir.GetPath());
528 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir_path.c_str(), lldb::eFilePermissionsDirectoryDefault);
529 if (error.Fail())
530 {
531 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", dst_dir_path.c_str());
532 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
533 }
534
535 // now recurse
536 std::string src_dir_path (src.GetPath());
537
538 // Make a filespec that only fills in the directory of a FileSpec so
539 // when we enumerate we can quickly fill in the filename for dst copies
540 FileSpec recurse_dst;
541 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
542 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
543 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
544 if (rc_baton2.error.Fail())
545 {
546 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
547 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
548 }
549 return FileSpec::eEnumerateDirectoryResultNext;
550 }
551 break;
552
553 case FileSpec::eFileTypeSymbolicLink:
554 {
555 // copy the file and keep going
556 FileSpec dst_file = rc_baton->dst;
557 if (!dst_file.GetFilename())
558 dst_file.GetFilename() = src.GetFilename();
559
560 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000561
562 rc_baton->error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000563
564 if (rc_baton->error.Fail())
565 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
566
567 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file.GetPath().c_str(), buf);
568
569 if (rc_baton->error.Fail())
570 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
571
572 return FileSpec::eEnumerateDirectoryResultNext;
573 }
574 break;
575 case FileSpec::eFileTypeRegular:
576 {
577 // copy the file and keep going
578 FileSpec dst_file = rc_baton->dst;
579 if (!dst_file.GetFilename())
580 dst_file.GetFilename() = src.GetFilename();
581 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
582 if (err.Fail())
583 {
584 rc_baton->error.SetErrorString(err.AsCString());
585 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
586 }
587 return FileSpec::eEnumerateDirectoryResultNext;
588 }
589 break;
590
591 case FileSpec::eFileTypeInvalid:
592 case FileSpec::eFileTypeOther:
593 case FileSpec::eFileTypeUnknown:
594 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
595 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
596 break;
597 }
David Majnemer8faf9372014-09-16 06:34:29 +0000598 llvm_unreachable("Unhandled FileSpec::FileType!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000599}
600
601Error
602Platform::Install (const FileSpec& src, const FileSpec& dst)
603{
604 Error error;
605
606 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
607 if (log)
608 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
609 FileSpec fixed_dst(dst);
610
611 if (!fixed_dst.GetFilename())
612 fixed_dst.GetFilename() = src.GetFilename();
613
614 ConstString working_dir = GetWorkingDirectory();
615
616 if (dst)
617 {
618 if (dst.GetDirectory())
619 {
620 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
621 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
622 {
623 fixed_dst.GetDirectory() = dst.GetDirectory();
624 }
625 // If the fixed destination file doesn't have a directory yet,
626 // then we must have a relative path. We will resolve this relative
627 // path against the platform's working directory
628 if (!fixed_dst.GetDirectory())
629 {
630 FileSpec relative_spec;
631 std::string path;
632 if (working_dir)
633 {
634 relative_spec.SetFile(working_dir.GetCString(), false);
635 relative_spec.AppendPathComponent(dst.GetPath().c_str());
636 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
637 }
638 else
639 {
640 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
641 return error;
642 }
643 }
644 }
645 else
646 {
647 if (working_dir)
648 {
649 fixed_dst.GetDirectory() = working_dir;
650 }
651 else
652 {
653 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
654 return error;
655 }
656 }
657 }
658 else
659 {
660 if (working_dir)
661 {
662 fixed_dst.GetDirectory() = working_dir;
663 }
664 else
665 {
666 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
667 return error;
668 }
669 }
670
671 if (log)
672 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
673
674 if (GetSupportsRSync())
675 {
676 error = PutFile(src, dst);
677 }
678 else
679 {
680 switch (src.GetFileType())
681 {
682 case FileSpec::eFileTypeDirectory:
683 {
684 if (GetFileExists (fixed_dst))
685 Unlink (fixed_dst.GetPath().c_str());
686 uint32_t permissions = src.GetPermissions();
687 if (permissions == 0)
688 permissions = eFilePermissionsDirectoryDefault;
689 std::string dst_dir_path(fixed_dst.GetPath());
690 error = MakeDirectory(dst_dir_path.c_str(), permissions);
691 if (error.Success())
692 {
693 // Make a filespec that only fills in the directory of a FileSpec so
694 // when we enumerate we can quickly fill in the filename for dst copies
695 FileSpec recurse_dst;
696 recurse_dst.GetDirectory().SetCString(dst_dir_path.c_str());
697 std::string src_dir_path (src.GetPath());
698 RecurseCopyBaton baton = { recurse_dst, this, Error() };
699 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
700 return baton.error;
701 }
702 }
703 break;
704
705 case FileSpec::eFileTypeRegular:
706 if (GetFileExists (fixed_dst))
707 Unlink (fixed_dst.GetPath().c_str());
708 error = PutFile(src, fixed_dst);
709 break;
710
711 case FileSpec::eFileTypeSymbolicLink:
712 {
713 if (GetFileExists (fixed_dst))
714 Unlink (fixed_dst.GetPath().c_str());
715 char buf[PATH_MAX];
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000716 error = FileSystem::Readlink(src.GetPath().c_str(), buf, sizeof(buf));
Greg Claytonfbb76342013-11-20 21:07:01 +0000717 if (error.Success())
718 error = CreateSymlink(dst.GetPath().c_str(), buf);
719 }
720 break;
721 case FileSpec::eFileTypePipe:
722 error.SetErrorString("platform install doesn't handle pipes");
723 break;
724 case FileSpec::eFileTypeSocket:
725 error.SetErrorString("platform install doesn't handle sockets");
726 break;
727 case FileSpec::eFileTypeInvalid:
728 case FileSpec::eFileTypeUnknown:
729 case FileSpec::eFileTypeOther:
730 error.SetErrorString("platform install doesn't handle non file or directory items");
731 break;
732 }
733 }
734 return error;
735}
736
737bool
738Platform::SetWorkingDirectory (const ConstString &path)
739{
740 if (IsHost())
741 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000742 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
743 if (log)
744 log->Printf("Platform::SetWorkingDirectory('%s')", path.GetCString());
Colin Riley909bb7a2013-11-26 15:10:46 +0000745#ifdef _WIN32
746 // Not implemented on Windows
747 return false;
748#else
Greg Claytonfbb76342013-11-20 21:07:01 +0000749 if (path)
750 {
751 if (chdir(path.GetCString()) == 0)
752 return true;
753 }
754 return false;
Colin Riley909bb7a2013-11-26 15:10:46 +0000755#endif
Greg Claytonfbb76342013-11-20 21:07:01 +0000756 }
757 else
758 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000759 m_working_dir.Clear();
Greg Claytonfbb76342013-11-20 21:07:01 +0000760 return SetRemoteWorkingDirectory(path);
761 }
762}
763
764Error
765Platform::MakeDirectory (const char *path, uint32_t permissions)
766{
767 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000768 return FileSystem::MakeDirectory(path, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000769 else
770 {
771 Error error;
772 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
773 return error;
774 }
775}
776
777Error
778Platform::GetFilePermissions (const char *path, uint32_t &file_permissions)
779{
780 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000781 return FileSystem::GetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000782 else
783 {
784 Error error;
785 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
786 return error;
787 }
788}
789
790Error
791Platform::SetFilePermissions (const char *path, uint32_t file_permissions)
792{
793 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000794 return FileSystem::SetFilePermissions(path, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000795 else
796 {
797 Error error;
798 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
799 return error;
800 }
801}
802
803ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000804Platform::GetName ()
805{
Greg Claytonfbb76342013-11-20 21:07:01 +0000806 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000807}
808
809const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000810Platform::GetHostname ()
811{
Greg Claytonab65b342011-04-13 22:47:15 +0000812 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000813 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000814
815 if (m_name.empty())
816 return NULL;
817 return m_name.c_str();
818}
819
Greg Clayton5fb8f792013-12-02 19:35:49 +0000820bool
821Platform::SetRemoteWorkingDirectory(const ConstString &path)
822{
823 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
824 if (log)
825 log->Printf("Platform::SetRemoteWorkingDirectory('%s')", path.GetCString());
826 m_working_dir = path;
827 return true;
828}
829
Greg Clayton32e0a752011-03-30 18:16:51 +0000830const char *
831Platform::GetUserName (uint32_t uid)
832{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000833#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000834 const char *user_name = GetCachedUserName(uid);
835 if (user_name)
836 return user_name;
837 if (IsHost())
838 {
839 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000840 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000841 return SetCachedUserName (uid, name.c_str(), name.size());
842 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000843#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000844 return NULL;
845}
846
Greg Clayton32e0a752011-03-30 18:16:51 +0000847const char *
848Platform::GetGroupName (uint32_t gid)
849{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000850#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000851 const char *group_name = GetCachedGroupName(gid);
852 if (group_name)
853 return group_name;
854 if (IsHost())
855 {
856 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000857 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000858 return SetCachedGroupName (gid, name.c_str(), name.size());
859 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000860#endif
Greg Clayton32e0a752011-03-30 18:16:51 +0000861 return NULL;
862}
Greg Clayton1cb64962011-03-24 04:28:38 +0000863
Greg Claytonded470d2011-03-19 01:12:21 +0000864bool
865Platform::SetOSVersion (uint32_t major,
866 uint32_t minor,
867 uint32_t update)
868{
869 if (IsHost())
870 {
Zachary Turner97a14e62014-08-19 17:18:29 +0000871 // We don't need anyone setting the OS version for the host platform,
872 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000873 return false;
874 }
875 else
876 {
877 // We have a remote platform, allow setting the target OS version if
878 // we aren't connected, since if we are connected, we should be able to
879 // request the remote OS version from the connected platform.
880 if (IsConnected())
881 return false;
882 else
883 {
884 // We aren't connected and we might want to set the OS version
885 // ahead of time before we connect so we can peruse files and
886 // use a local SDK or PDK cache of support files to disassemble
887 // or do other things.
888 m_major_os_version = major;
889 m_minor_os_version = minor;
890 m_update_os_version = update;
891 return true;
892 }
893 }
894 return false;
895}
896
897
Greg Claytone996fd32011-03-08 22:40:15 +0000898Error
Greg Clayton8012cad2014-11-17 19:39:20 +0000899Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000900 lldb::ModuleSP &exe_module_sp,
901 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +0000902{
903 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +0000904 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +0000905 {
Greg Claytonb9a01b32012-02-26 05:51:37 +0000906 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +0000907 {
Greg Claytonb9a01b32012-02-26 05:51:37 +0000908 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +0000909 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000910 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +0000911 NULL,
912 NULL);
913 }
914 else
915 {
916 // No valid architecture was specified, ask the platform for
917 // the architectures that we should be using (in the correct order)
918 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +0000919 ModuleSpec arch_module_spec(module_spec);
920 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +0000921 {
Greg Clayton8012cad2014-11-17 19:39:20 +0000922 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +0000923 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000924 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +0000925 NULL,
926 NULL);
927 // Did we find an executable using one of the
928 if (error.Success() && exe_module_sp)
929 break;
930 }
931 }
932 }
933 else
934 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000935 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +0000936 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +0000937 }
938 return error;
939}
940
Greg Clayton103f0282012-09-12 02:03:59 +0000941Error
942Platform::ResolveSymbolFile (Target &target,
943 const ModuleSpec &sym_spec,
944 FileSpec &sym_file)
945{
946 Error error;
947 if (sym_spec.GetSymbolFileSpec().Exists())
948 sym_file = sym_spec.GetSymbolFileSpec();
949 else
950 error.SetErrorString("unable to resolve symbol file");
951 return error;
952
953}
954
955
956
Greg Claytonaa516842011-08-11 16:25:18 +0000957bool
958Platform::ResolveRemotePath (const FileSpec &platform_path,
959 FileSpec &resolved_platform_path)
960{
961 resolved_platform_path = platform_path;
962 return resolved_platform_path.ResolvePath();
963}
964
Greg Claytonded470d2011-03-19 01:12:21 +0000965
966const ArchSpec &
967Platform::GetSystemArchitecture()
968{
969 if (IsHost())
970 {
971 if (!m_system_arch.IsValid())
972 {
973 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +0000974 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +0000975 m_system_arch_set_while_connected = m_system_arch.IsValid();
976 }
977 }
978 else
979 {
980 // We have a remote platform. We can only fetch the remote
981 // system architecture if we are connected, and we don't want to do it
982 // more than once.
983
984 const bool is_connected = IsConnected();
985
986 bool fetch = false;
987 if (m_system_arch.IsValid())
988 {
989 // We have valid OS version info, check to make sure it wasn't
990 // manually set prior to connecting. If it was manually set prior
991 // to connecting, then lets fetch the actual OS version info
992 // if we are now connected.
993 if (is_connected && !m_system_arch_set_while_connected)
994 fetch = true;
995 }
996 else
997 {
998 // We don't have valid OS version info, fetch it if we are connected
999 fetch = is_connected;
1000 }
1001
1002 if (fetch)
1003 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001004 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001005 m_system_arch_set_while_connected = m_system_arch.IsValid();
1006 }
1007 }
1008 return m_system_arch;
1009}
1010
1011
Greg Claytone996fd32011-03-08 22:40:15 +00001012Error
Greg Claytond314e812011-03-23 00:09:55 +00001013Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001014{
1015 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001016 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001017 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001018 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001019 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001020 return error;
1021}
1022
1023Error
Greg Claytond314e812011-03-23 00:09:55 +00001024Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001025{
1026 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001027 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001028 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001029 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001030 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001031 return error;
1032}
Greg Clayton32e0a752011-03-30 18:16:51 +00001033
1034bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001035Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001036{
1037 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001038 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001039 if (IsHost())
1040 return Host::GetProcessInfo (pid, process_info);
1041 return false;
1042}
1043
1044uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001045Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1046 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001047{
Greg Clayton8b82f082011-04-12 05:54:46 +00001048 // Take care of the host case so that each subclass can just
1049 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001050 uint32_t match_count = 0;
1051 if (IsHost())
1052 match_count = Host::FindProcesses (match_info, process_infos);
1053 return match_count;
1054}
Greg Clayton8b82f082011-04-12 05:54:46 +00001055
1056
1057Error
1058Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1059{
1060 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001061 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1062 if (log)
1063 log->Printf ("Platform::%s()", __FUNCTION__);
1064
1065 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001066 // call this function to get the host functionality.
1067 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001068 {
1069 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1070 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1071
1072 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1073 {
1074 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001075 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1076 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001077 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001078 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001079 {
1080 const FileSpec &shell = launch_info.GetShell();
1081 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001082 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1083 __FUNCTION__,
1084 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001085 shell_str);
1086 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001087
Greg Claytond1cf11a2012-04-14 01:42:46 +00001088 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1089 is_localhost,
1090 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001091 first_arg_is_full_shell_command,
1092 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001093 return error;
1094 }
1095
Todd Fialaac33cc92014-10-09 01:02:08 +00001096 if (log)
1097 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1098
Greg Clayton8b82f082011-04-12 05:54:46 +00001099 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001100 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001101 else
1102 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1103 return error;
1104}
1105
1106lldb::ProcessSP
1107Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1108 Debugger &debugger,
1109 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001110 Error &error)
1111{
Todd Fialaac33cc92014-10-09 01:02:08 +00001112 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1113 if (log)
1114 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1115
Greg Clayton8b82f082011-04-12 05:54:46 +00001116 ProcessSP process_sp;
1117 // Make sure we stop at the entry point
1118 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001119 // We always launch the process we are going to debug in a separate process
1120 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1121 // about the target getting them as well.
1122 launch_info.SetLaunchInSeparateProcessGroup(true);
1123
Greg Clayton8b82f082011-04-12 05:54:46 +00001124 error = LaunchProcess (launch_info);
1125 if (error.Success())
1126 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001127 if (log)
1128 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001129 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001130 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001131 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001132 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001133 if (process_sp)
1134 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001135 if (log)
1136 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001137 launch_info.SetHijackListener(attach_info.GetHijackListener());
1138
Greg Claytone24c4ac2011-11-17 04:46:02 +00001139 // Since we attached to the process, it will think it needs to detach
1140 // if the process object just goes away without an explicit call to
1141 // Process::Kill() or Process::Detach(), so let it know to kill the
1142 // process if this happens.
1143 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001144
1145 // If we didn't have any file actions, the pseudo terminal might
1146 // have been used where the slave side was given as the file to
1147 // open for stdin/out/err after we have already opened the master
1148 // so we can read/write stdin/out/err.
1149 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1150 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1151 {
1152 process_sp->SetSTDIOFileDescriptor(pty_fd);
1153 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001154 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001155 else
1156 {
1157 if (log)
1158 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1159 }
1160 }
1161 else
1162 {
1163 if (log)
1164 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001165 }
1166 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001167 else
1168 {
1169 if (log)
1170 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1171 }
1172
Greg Clayton8b82f082011-04-12 05:54:46 +00001173 return process_sp;
1174}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001175
1176
1177lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001178Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001179{
1180 lldb::PlatformSP platform_sp;
1181 Error error;
1182 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001183 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001184 return platform_sp;
1185}
1186
1187
1188//------------------------------------------------------------------
1189/// Lets a platform answer if it is compatible with a given
1190/// architecture and the target triple contained within.
1191//------------------------------------------------------------------
1192bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001193Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001194{
1195 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001196 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001197 {
Greg Clayton70512312012-05-08 01:45:38 +00001198 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001199 // Try for an exact architecture match first.
1200 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001201 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001202 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001203 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001204 if (arch.IsExactMatch(platform_arch))
1205 {
1206 if (compatible_arch_ptr)
1207 *compatible_arch_ptr = platform_arch;
1208 return true;
1209 }
1210 }
1211 }
1212 else
1213 {
1214 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1215 {
1216 if (arch.IsCompatibleMatch(platform_arch))
1217 {
1218 if (compatible_arch_ptr)
1219 *compatible_arch_ptr = platform_arch;
1220 return true;
1221 }
Greg Clayton70512312012-05-08 01:45:38 +00001222 }
1223 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001224 }
Greg Clayton70512312012-05-08 01:45:38 +00001225 if (compatible_arch_ptr)
1226 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001227 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001228}
1229
Daniel Maleae0f8f572013-08-26 23:57:52 +00001230Error
1231Platform::PutFile (const FileSpec& source,
1232 const FileSpec& destination,
1233 uint32_t uid,
1234 uint32_t gid)
1235{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001236 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1237 if (log)
1238 log->Printf("[PutFile] Using block by block transfer....\n");
1239
1240 uint32_t source_open_options = File::eOpenOptionRead;
1241 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1242 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1243
1244 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1245 Error error;
1246 uint32_t permissions = source_file.GetPermissions(error);
1247 if (permissions == 0)
1248 permissions = lldb::eFilePermissionsFileDefault;
1249
1250 if (!source_file.IsValid())
1251 return Error("PutFile: unable to open source file");
1252 lldb::user_id_t dest_file = OpenFile (destination,
1253 File::eOpenOptionCanCreate |
1254 File::eOpenOptionWrite |
1255 File::eOpenOptionTruncate,
1256 permissions,
1257 error);
1258 if (log)
1259 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1260
1261 if (error.Fail())
1262 return error;
1263 if (dest_file == UINT64_MAX)
1264 return Error("unable to open target file");
1265 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1266 uint64_t offset = 0;
1267 for (;;)
1268 {
1269 size_t bytes_read = buffer_sp->GetByteSize();
1270 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1271 if (error.Fail() || bytes_read == 0)
1272 break;
1273
1274 const uint64_t bytes_written = WriteFile(dest_file, offset,
1275 buffer_sp->GetBytes(), bytes_read, error);
1276 if (error.Fail())
1277 break;
1278
1279 offset += bytes_written;
1280 if (bytes_written != bytes_read)
1281 {
1282 // We didn't write the correct number of bytes, so adjust
1283 // the file position in the source file we are reading from...
1284 source_file.SeekFromStart(offset);
1285 }
1286 }
1287 CloseFile(dest_file, error);
1288
1289 if (uid == UINT32_MAX && gid == UINT32_MAX)
1290 return error;
1291
1292 // TODO: ChownFile?
1293
Daniel Maleae0f8f572013-08-26 23:57:52 +00001294 return error;
1295}
1296
1297Error
1298Platform::GetFile (const FileSpec& source,
1299 const FileSpec& destination)
1300{
1301 Error error("unimplemented");
1302 return error;
1303}
1304
Greg Claytonfbb76342013-11-20 21:07:01 +00001305Error
1306Platform::CreateSymlink (const char *src, // The name of the link is in src
1307 const char *dst)// The symlink points to dst
1308{
1309 Error error("unimplemented");
1310 return error;
1311}
1312
Daniel Maleae0f8f572013-08-26 23:57:52 +00001313bool
1314Platform::GetFileExists (const lldb_private::FileSpec& file_spec)
1315{
1316 return false;
1317}
1318
Greg Claytonfbb76342013-11-20 21:07:01 +00001319Error
1320Platform::Unlink (const char *path)
1321{
1322 Error error("unimplemented");
1323 return error;
1324}
1325
1326
1327
Daniel Maleae0f8f572013-08-26 23:57:52 +00001328lldb_private::Error
1329Platform::RunShellCommand (const char *command, // Shouldn't be NULL
1330 const char *working_dir, // Pass NULL to use the current working directory
1331 int *status_ptr, // Pass NULL if you don't want the process exit status
1332 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1333 std::string *command_output, // Pass NULL if you don't want the command output
1334 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
1335{
1336 if (IsHost())
1337 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1338 else
1339 return Error("unimplemented");
1340}
1341
1342
1343bool
1344Platform::CalculateMD5 (const FileSpec& file_spec,
1345 uint64_t &low,
1346 uint64_t &high)
1347{
1348 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001349 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001350 else
1351 return false;
1352}
1353
Todd Fialaaf245d12014-06-30 21:05:18 +00001354Error
1355Platform::LaunchNativeProcess (
1356 ProcessLaunchInfo &launch_info,
1357 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1358 NativeProcessProtocolSP &process_sp)
1359{
1360 // Platforms should override this implementation if they want to
1361 // support lldb-gdbserver.
1362 return Error("unimplemented");
1363}
1364
1365Error
1366Platform::AttachNativeProcess (lldb::pid_t pid,
1367 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1368 NativeProcessProtocolSP &process_sp)
1369{
1370 // Platforms should override this implementation if they want to
1371 // support lldb-gdbserver.
1372 return Error("unimplemented");
1373}
1374
Daniel Maleae0f8f572013-08-26 23:57:52 +00001375void
1376Platform::SetLocalCacheDirectory (const char* local)
1377{
1378 m_local_cache_directory.assign(local);
1379}
1380
1381const char*
1382Platform::GetLocalCacheDirectory ()
1383{
1384 return m_local_cache_directory.c_str();
1385}
1386
1387static OptionDefinition
1388g_rsync_option_table[] =
1389{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001390 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1391 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1392 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1393 { 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 +00001394};
1395
1396static OptionDefinition
1397g_ssh_option_table[] =
1398{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001399 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1400 { 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 +00001401};
1402
1403static OptionDefinition
1404g_caching_option_table[] =
1405{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001406 { 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 +00001407};
1408
1409OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1410{
1411}
1412
1413OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1414{
1415}
1416
1417const lldb_private::OptionDefinition*
1418OptionGroupPlatformRSync::GetDefinitions ()
1419{
1420 return g_rsync_option_table;
1421}
1422
1423void
1424OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1425{
1426 m_rsync = false;
1427 m_rsync_opts.clear();
1428 m_rsync_prefix.clear();
1429 m_ignores_remote_hostname = false;
1430}
1431
1432lldb_private::Error
1433OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1434 uint32_t option_idx,
1435 const char *option_arg)
1436{
1437 Error error;
1438 char short_option = (char) GetDefinitions()[option_idx].short_option;
1439 switch (short_option)
1440 {
1441 case 'r':
1442 m_rsync = true;
1443 break;
1444
1445 case 'R':
1446 m_rsync_opts.assign(option_arg);
1447 break;
1448
1449 case 'P':
1450 m_rsync_prefix.assign(option_arg);
1451 break;
1452
1453 case 'i':
1454 m_ignores_remote_hostname = true;
1455 break;
1456
1457 default:
1458 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1459 break;
1460 }
1461
1462 return error;
1463}
1464
1465uint32_t
1466OptionGroupPlatformRSync::GetNumDefinitions ()
1467{
1468 return llvm::array_lengthof(g_rsync_option_table);
1469}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001470
Greg Clayton4116e932012-05-15 02:33:01 +00001471lldb::BreakpointSP
1472Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1473{
1474 return lldb::BreakpointSP();
1475}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001476
Daniel Maleae0f8f572013-08-26 23:57:52 +00001477OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1478{
1479}
1480
1481OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1482{
1483}
1484
1485const lldb_private::OptionDefinition*
1486OptionGroupPlatformSSH::GetDefinitions ()
1487{
1488 return g_ssh_option_table;
1489}
1490
1491void
1492OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1493{
1494 m_ssh = false;
1495 m_ssh_opts.clear();
1496}
1497
1498lldb_private::Error
1499OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1500 uint32_t option_idx,
1501 const char *option_arg)
1502{
1503 Error error;
1504 char short_option = (char) GetDefinitions()[option_idx].short_option;
1505 switch (short_option)
1506 {
1507 case 's':
1508 m_ssh = true;
1509 break;
1510
1511 case 'S':
1512 m_ssh_opts.assign(option_arg);
1513 break;
1514
1515 default:
1516 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1517 break;
1518 }
1519
1520 return error;
1521}
1522
1523uint32_t
1524OptionGroupPlatformSSH::GetNumDefinitions ()
1525{
1526 return llvm::array_lengthof(g_ssh_option_table);
1527}
1528
1529OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1530{
1531}
1532
1533OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1534{
1535}
1536
1537const lldb_private::OptionDefinition*
1538OptionGroupPlatformCaching::GetDefinitions ()
1539{
1540 return g_caching_option_table;
1541}
1542
1543void
1544OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1545{
1546 m_cache_dir.clear();
1547}
1548
1549lldb_private::Error
1550OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1551 uint32_t option_idx,
1552 const char *option_arg)
1553{
1554 Error error;
1555 char short_option = (char) GetDefinitions()[option_idx].short_option;
1556 switch (short_option)
1557 {
1558 case 'c':
1559 m_cache_dir.assign(option_arg);
1560 break;
1561
1562 default:
1563 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1564 break;
1565 }
1566
1567 return error;
1568}
1569
1570uint32_t
1571OptionGroupPlatformCaching::GetNumDefinitions ()
1572{
1573 return llvm::array_lengthof(g_caching_option_table);
1574}
1575
Greg Clayton67cc0632012-08-22 17:17:09 +00001576size_t
1577Platform::GetEnvironment (StringList &environment)
1578{
1579 environment.Clear();
1580 return false;
1581}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001582
1583const std::vector<ConstString> &
1584Platform::GetTrapHandlerSymbolNames ()
1585{
1586 if (!m_calculated_trap_handlers)
1587 {
Jason Molenda4da87062014-05-23 23:11:27 +00001588 Mutex::Locker locker (m_trap_handler_mutex);
1589 if (!m_calculated_trap_handlers)
1590 {
1591 CalculateTrapHandlerSymbolNames();
1592 m_calculated_trap_handlers = true;
1593 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001594 }
1595 return m_trap_handlers;
1596}
1597