blob: fc349ee9670ee129ed7d6ca4f333b4ca63a4b675 [file] [log] [blame]
Greg Claytone996fd32011-03-08 22:40:15 +00001//===-- Platform.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Platform.h"
11
12// C Includes
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000013
Greg Claytone996fd32011-03-08 22:40:15 +000014// C++ Includes
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000015#include <algorithm>
16#include <fstream>
17#include <vector>
18
Greg Claytone996fd32011-03-08 22:40:15 +000019// Other libraries and framework includes
20// Project includes
Greg Clayton4116e932012-05-15 02:33:01 +000021#include "lldb/Breakpoint/BreakpointIDList.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000022#include "lldb/Core/DataBufferHeap.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000023#include "lldb/Core/Debugger.h"
Greg Claytone996fd32011-03-08 22:40:15 +000024#include "lldb/Core/Error.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000025#include "lldb/Core/Log.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000026#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000028#include "lldb/Core/PluginManager.h"
Enrico Granatad7a83a92015-02-10 03:06:24 +000029#include "lldb/Core/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000030#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000031#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000032#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000033#include "lldb/Host/HostInfo.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000034#include "lldb/Interpreter/OptionValueProperties.h"
35#include "lldb/Interpreter/Property.h"
36#include "lldb/Symbol/ObjectFile.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000037#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000038#include "lldb/Target/Target.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000039#include "lldb/Utility/Utils.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000040#include "llvm/Support/FileSystem.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000041
42#include "Utility/ModuleCache.h"
Greg Claytone996fd32011-03-08 22:40:15 +000043
Robert Flack96ad3de2015-05-09 15:53:31 +000044// Define these constants from POSIX mman.h rather than include the file
45// so that they will be correct even when compiled on Linux.
46#define MAP_PRIVATE 2
47#define MAP_ANON 0x1000
48
Greg Claytone996fd32011-03-08 22:40:15 +000049using namespace lldb;
50using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000051
52static uint32_t g_initialize_count = 0;
53
Greg Claytone996fd32011-03-08 22:40:15 +000054// Use a singleton function for g_local_platform_sp to avoid init
55// constructors since LLDB is often part of a shared library
56static PlatformSP&
Greg Clayton615eb7e2014-09-19 20:11:50 +000057GetHostPlatformSP ()
Greg Claytone996fd32011-03-08 22:40:15 +000058{
Greg Clayton615eb7e2014-09-19 20:11:50 +000059 static PlatformSP g_platform_sp;
60 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000061}
62
Greg Claytonab65b342011-04-13 22:47:15 +000063const char *
64Platform::GetHostPlatformName ()
65{
66 return "host";
67}
68
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000069namespace {
70
71 PropertyDefinition
72 g_properties[] =
73 {
74 { "use-module-cache" , OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "Use module cache." },
75 { "module-cache-directory", OptionValue::eTypeFileSpec, true, 0 , nullptr, nullptr, "Root directory for cached modules." },
76 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
77 };
78
79 enum
80 {
81 ePropertyUseModuleCache,
82 ePropertyModuleCacheDirectory
83 };
84
85} // namespace
86
87
88ConstString
89PlatformProperties::GetSettingName ()
90{
91 static ConstString g_setting_name("platform");
92 return g_setting_name;
93}
94
95PlatformProperties::PlatformProperties ()
96{
97 m_collection_sp.reset (new OptionValueProperties (GetSettingName ()));
98 m_collection_sp->Initialize (g_properties);
99
100 auto module_cache_dir = GetModuleCacheDirectory ();
101 if (!module_cache_dir)
102 {
103 if (!HostInfo::GetLLDBPath (ePathTypeGlobalLLDBTempSystemDir, module_cache_dir))
104 module_cache_dir = FileSpec ("/tmp/lldb", false);
105 module_cache_dir.AppendPathComponent ("module_cache");
106 SetModuleCacheDirectory (module_cache_dir);
107 }
108}
109
110bool
111PlatformProperties::GetUseModuleCache () const
112{
113 const auto idx = ePropertyUseModuleCache;
114 return m_collection_sp->GetPropertyAtIndexAsBoolean (
115 nullptr, idx, g_properties[idx].default_uint_value != 0);
116}
117
118bool
119PlatformProperties::SetUseModuleCache (bool use_module_cache)
120{
121 return m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, ePropertyUseModuleCache, use_module_cache);
122}
123
124FileSpec
125PlatformProperties::GetModuleCacheDirectory () const
126{
127 return m_collection_sp->GetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory);
128}
129
130bool
131PlatformProperties::SetModuleCacheDirectory (const FileSpec& dir_spec)
132{
133 return m_collection_sp->SetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory, dir_spec);
134}
135
Greg Claytone996fd32011-03-08 22:40:15 +0000136//------------------------------------------------------------------
137/// Get the native host platform plug-in.
138///
139/// There should only be one of these for each host that LLDB runs
140/// upon that should be statically compiled in and registered using
141/// preprocessor macros or other similar build mechanisms.
142///
143/// This platform will be used as the default platform when launching
144/// or attaching to processes unless another platform is specified.
145//------------------------------------------------------------------
146PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000147Platform::GetHostPlatform ()
Greg Claytone996fd32011-03-08 22:40:15 +0000148{
Greg Clayton615eb7e2014-09-19 20:11:50 +0000149 return GetHostPlatformSP ();
150}
151
152static std::vector<PlatformSP> &
153GetPlatformList()
154{
155 static std::vector<PlatformSP> g_platform_list;
156 return g_platform_list;
157}
158
159static Mutex &
160GetPlatformListMutex ()
161{
162 static Mutex g_mutex(Mutex::eMutexTypeRecursive);
163 return g_mutex;
Greg Claytone996fd32011-03-08 22:40:15 +0000164}
165
166void
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000167Platform::Initialize ()
168{
169 g_initialize_count++;
170}
171
172void
173Platform::Terminate ()
174{
175 if (g_initialize_count > 0)
176 {
177 if (--g_initialize_count == 0)
178 {
179 Mutex::Locker locker(GetPlatformListMutex ());
180 GetPlatformList().clear();
181 }
182 }
183}
184
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000185const PlatformPropertiesSP &
186Platform::GetGlobalPlatformProperties ()
187{
188 static const auto g_settings_sp (std::make_shared<PlatformProperties> ());
189 return g_settings_sp;
190}
191
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000192void
Greg Clayton615eb7e2014-09-19 20:11:50 +0000193Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp)
Greg Claytone996fd32011-03-08 22:40:15 +0000194{
195 // The native platform should use its static void Platform::Initialize()
196 // function to register itself as the native platform.
Greg Clayton615eb7e2014-09-19 20:11:50 +0000197 GetHostPlatformSP () = platform_sp;
198
199 if (platform_sp)
200 {
201 Mutex::Locker locker(GetPlatformListMutex ());
202 GetPlatformList().push_back(platform_sp);
203 }
Greg Claytone996fd32011-03-08 22:40:15 +0000204}
205
Greg Claytone996fd32011-03-08 22:40:15 +0000206Error
Steve Puccifc995722014-01-17 18:18:31 +0000207Platform::GetFileWithUUID (const FileSpec &platform_file,
208 const UUID *uuid_ptr,
209 FileSpec &local_file)
Greg Claytone996fd32011-03-08 22:40:15 +0000210{
211 // Default to the local case
212 local_file = platform_file;
213 return Error();
214}
215
Greg Clayton91c0e742013-01-11 23:44:27 +0000216FileSpecList
Enrico Granatafe7295d2014-08-16 00:32:58 +0000217Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream)
Enrico Granata17598482012-11-08 02:22:02 +0000218{
Greg Clayton91c0e742013-01-11 23:44:27 +0000219 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000220}
221
Greg Clayton615eb7e2014-09-19 20:11:50 +0000222//PlatformSP
223//Platform::FindPlugin (Process *process, const ConstString &plugin_name)
224//{
225// PlatformCreateInstance create_callback = NULL;
226// if (plugin_name)
227// {
228// create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
229// if (create_callback)
230// {
231// ArchSpec arch;
232// if (process)
233// {
234// arch = process->GetTarget().GetArchitecture();
235// }
236// PlatformSP platform_sp(create_callback(process, &arch));
237// if (platform_sp)
238// return platform_sp;
239// }
240// }
241// else
242// {
243// for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
244// {
245// PlatformSP platform_sp(create_callback(process, nullptr));
246// if (platform_sp)
247// return platform_sp;
248// }
249// }
250// return PlatformSP();
251//}
Jason Molenda1c627542013-04-05 01:03:25 +0000252
Greg Clayton32e0a752011-03-30 18:16:51 +0000253Error
Greg Claytonb9a01b32012-02-26 05:51:37 +0000254Platform::GetSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000255 Process* process,
Greg Clayton32e0a752011-03-30 18:16:51 +0000256 ModuleSP &module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000257 const FileSpecList *module_search_paths_ptr,
Greg Clayton32e0a752011-03-30 18:16:51 +0000258 ModuleSP *old_module_sp_ptr,
259 bool *did_create_ptr)
260{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000261 if (IsHost ())
262 return ModuleList::GetSharedModule (module_spec,
263 module_sp,
264 module_search_paths_ptr,
265 old_module_sp_ptr,
266 did_create_ptr,
267 false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000268
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +0000269 return GetRemoteSharedModule (module_spec,
270 process,
271 module_sp,
272 [&](const ModuleSpec &spec)
273 {
274 return ModuleList::GetSharedModule (
275 spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr, false);
276 },
277 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000278}
279
280bool
281Platform::GetModuleSpec (const FileSpec& module_file_spec,
282 const ArchSpec& arch,
283 ModuleSpec &module_spec)
284{
285 ModuleSpecList module_specs;
286 if (ObjectFile::GetModuleSpecifications (module_file_spec, 0, 0, module_specs) == 0)
287 return false;
288
289 ModuleSpec matched_module_spec;
290 return module_specs.FindMatchingModuleSpec (ModuleSpec (module_file_spec, arch),
291 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000292}
293
Greg Claytone996fd32011-03-08 22:40:15 +0000294PlatformSP
Greg Clayton615eb7e2014-09-19 20:11:50 +0000295Platform::Find (const ConstString &name)
296{
297 if (name)
298 {
299 static ConstString g_host_platform_name ("host");
300 if (name == g_host_platform_name)
301 return GetHostPlatform();
302
303 Mutex::Locker locker(GetPlatformListMutex ());
304 for (const auto &platform_sp : GetPlatformList())
305 {
306 if (platform_sp->GetName() == name)
307 return platform_sp;
308 }
309 }
310 return PlatformSP();
311}
312
313PlatformSP
314Platform::Create (const ConstString &name, Error &error)
Greg Claytone996fd32011-03-08 22:40:15 +0000315{
316 PlatformCreateInstance create_callback = NULL;
317 lldb::PlatformSP platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000318 if (name)
Greg Claytone996fd32011-03-08 22:40:15 +0000319 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000320 static ConstString g_host_platform_name ("host");
321 if (name == g_host_platform_name)
322 return GetHostPlatform();
323
324 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name);
Greg Claytone996fd32011-03-08 22:40:15 +0000325 if (create_callback)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000326 platform_sp = create_callback(true, NULL);
Greg Claytone996fd32011-03-08 22:40:15 +0000327 else
Greg Clayton615eb7e2014-09-19 20:11:50 +0000328 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +0000329 }
330 else
Greg Claytonded470d2011-03-19 01:12:21 +0000331 error.SetErrorString ("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000332
333 if (platform_sp)
334 {
335 Mutex::Locker locker(GetPlatformListMutex ());
336 GetPlatformList().push_back(platform_sp);
337 }
338
Greg Claytone996fd32011-03-08 22:40:15 +0000339 return platform_sp;
340}
341
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000342
343PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +0000344Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000345{
346 lldb::PlatformSP platform_sp;
347 if (arch.IsValid())
348 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000349 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000350 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000351 // First try exact arch matches across all platforms already created
352 Mutex::Locker locker(GetPlatformListMutex ());
353 for (const auto &platform_sp : GetPlatformList())
Greg Clayton1e0c8842013-01-11 20:49:54 +0000354 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000355 if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
356 return platform_sp;
357 }
358
359 // Next try compatible arch matches across all platforms already created
360 for (const auto &platform_sp : GetPlatformList())
361 {
362 if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
Greg Clayton1e0c8842013-01-11 20:49:54 +0000363 return platform_sp;
364 }
365 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000366
367 PlatformCreateInstance create_callback;
368 // First try exact arch matches across all platform plug-ins
369 uint32_t idx;
Greg Clayton1e0c8842013-01-11 20:49:54 +0000370 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
371 {
372 if (create_callback)
373 {
Greg Clayton615eb7e2014-09-19 20:11:50 +0000374 platform_sp = create_callback(false, &arch);
375 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr))
376 {
377 Mutex::Locker locker(GetPlatformListMutex ());
378 GetPlatformList().push_back(platform_sp);
Greg Clayton1e0c8842013-01-11 20:49:54 +0000379 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000380 }
381 }
382 }
383 // Next try compatible arch matches across all platform plug-ins
384 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
385 {
386 if (create_callback)
387 {
388 platform_sp = create_callback(false, &arch);
389 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr))
390 {
391 Mutex::Locker locker(GetPlatformListMutex ());
392 GetPlatformList().push_back(platform_sp);
393 return platform_sp;
394 }
Greg Clayton1e0c8842013-01-11 20:49:54 +0000395 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000396 }
397 }
398 else
399 error.SetErrorString ("invalid platform name");
Greg Clayton70512312012-05-08 01:45:38 +0000400 if (platform_arch_ptr)
401 platform_arch_ptr->Clear();
402 platform_sp.reset();
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000403 return platform_sp;
404}
405
Greg Claytone996fd32011-03-08 22:40:15 +0000406//------------------------------------------------------------------
407/// Default Constructor
408//------------------------------------------------------------------
Greg Claytonded470d2011-03-19 01:12:21 +0000409Platform::Platform (bool is_host) :
410 m_is_host (is_host),
Greg Claytonded470d2011-03-19 01:12:21 +0000411 m_os_version_set_while_connected (false),
412 m_system_arch_set_while_connected (false),
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000413 m_sdk_sysroot (),
414 m_sdk_build (),
Greg Claytonfbb76342013-11-20 21:07:01 +0000415 m_working_dir (),
Greg Claytonded470d2011-03-19 01:12:21 +0000416 m_remote_url (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000417 m_name (),
Greg Claytonded470d2011-03-19 01:12:21 +0000418 m_major_os_version (UINT32_MAX),
419 m_minor_os_version (UINT32_MAX),
Greg Clayton32e0a752011-03-30 18:16:51 +0000420 m_update_os_version (UINT32_MAX),
421 m_system_arch(),
Greg Clayton7597b352015-02-02 20:45:17 +0000422 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +0000423 m_uid_map(),
424 m_gid_map(),
425 m_max_uid_name_len (0),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000426 m_max_gid_name_len (0),
427 m_supports_rsync (false),
428 m_rsync_opts (),
429 m_rsync_prefix (),
430 m_supports_ssh (false),
431 m_ssh_opts (),
Jason Molenda6223db272014-02-13 07:11:08 +0000432 m_ignores_remote_hostname (false),
Jason Molenda2094dbf2014-02-13 23:11:45 +0000433 m_trap_handlers(),
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000434 m_calculated_trap_handlers (false),
435 m_module_cache (llvm::make_unique<ModuleCache> ())
Greg Claytone996fd32011-03-08 22:40:15 +0000436{
Greg Clayton5160ce52013-03-27 23:08:40 +0000437 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000438 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000439 log->Printf ("%p Platform::Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000440}
441
442//------------------------------------------------------------------
443/// Destructor.
444///
445/// The destructor is virtual since this class is designed to be
446/// inherited from by the plug-in instance.
447//------------------------------------------------------------------
448Platform::~Platform()
449{
Greg Clayton5160ce52013-03-27 23:08:40 +0000450 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Greg Clayton8b82f082011-04-12 05:54:46 +0000451 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000452 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000453}
454
Greg Clayton1cb64962011-03-24 04:28:38 +0000455void
456Platform::GetStatus (Stream &strm)
457{
458 uint32_t major = UINT32_MAX;
459 uint32_t minor = UINT32_MAX;
460 uint32_t update = UINT32_MAX;
461 std::string s;
Greg Clayton57abc5d2013-05-10 21:47:16 +0000462 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000463
464 ArchSpec arch (GetSystemArchitecture());
465 if (arch.IsValid())
466 {
467 if (!arch.GetTriple().str().empty())
Greg Clayton32e0a752011-03-30 18:16:51 +0000468 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000469 }
470
471 if (GetOSVersion(major, minor, update))
472 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000473 strm.Printf("OS Version: %u", major);
Greg Clayton1cb64962011-03-24 04:28:38 +0000474 if (minor != UINT32_MAX)
475 strm.Printf(".%u", minor);
476 if (update != UINT32_MAX)
477 strm.Printf(".%u", update);
478
479 if (GetOSBuildString (s))
480 strm.Printf(" (%s)", s.c_str());
481
482 strm.EOL();
483 }
484
485 if (GetOSKernelDescription (s))
Greg Clayton32e0a752011-03-30 18:16:51 +0000486 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000487
488 if (IsHost())
489 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000490 strm.Printf(" Hostname: %s\n", GetHostname());
Greg Clayton1cb64962011-03-24 04:28:38 +0000491 }
492 else
493 {
Greg Clayton32e0a752011-03-30 18:16:51 +0000494 const bool is_connected = IsConnected();
495 if (is_connected)
496 strm.Printf(" Hostname: %s\n", GetHostname());
497 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
Greg Clayton1cb64962011-03-24 04:28:38 +0000498 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000499
Greg Claytonfbb76342013-11-20 21:07:01 +0000500 if (GetWorkingDirectory())
501 {
502 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
503 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000504 if (!IsConnected())
505 return;
506
507 std::string specific_info(GetPlatformSpecificConnectionInformation());
508
509 if (specific_info.empty() == false)
510 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000511}
512
Greg Claytonded470d2011-03-19 01:12:21 +0000513
514bool
515Platform::GetOSVersion (uint32_t &major,
516 uint32_t &minor,
517 uint32_t &update)
518{
Greg Clayton7597b352015-02-02 20:45:17 +0000519 Mutex::Locker locker (m_mutex);
520
Greg Claytonded470d2011-03-19 01:12:21 +0000521 bool success = m_major_os_version != UINT32_MAX;
522 if (IsHost())
523 {
524 if (!success)
525 {
526 // We have a local host platform
Zachary Turner97a14e62014-08-19 17:18:29 +0000527 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version);
Greg Claytonded470d2011-03-19 01:12:21 +0000528 m_os_version_set_while_connected = success;
529 }
530 }
531 else
532 {
533 // We have a remote platform. We can only fetch the remote
534 // OS version if we are connected, and we don't want to do it
535 // more than once.
536
537 const bool is_connected = IsConnected();
538
Greg Clayton1cb64962011-03-24 04:28:38 +0000539 bool fetch = false;
Greg Claytonded470d2011-03-19 01:12:21 +0000540 if (success)
541 {
542 // We have valid OS version info, check to make sure it wasn't
543 // manually set prior to connecting. If it was manually set prior
544 // to connecting, then lets fetch the actual OS version info
545 // if we are now connected.
546 if (is_connected && !m_os_version_set_while_connected)
Greg Clayton1cb64962011-03-24 04:28:38 +0000547 fetch = true;
Greg Claytonded470d2011-03-19 01:12:21 +0000548 }
549 else
550 {
551 // We don't have valid OS version info, fetch it if we are connected
Greg Clayton1cb64962011-03-24 04:28:38 +0000552 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000553 }
554
Greg Clayton1cb64962011-03-24 04:28:38 +0000555 if (fetch)
Greg Claytonded470d2011-03-19 01:12:21 +0000556 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000557 success = GetRemoteOSVersion ();
Greg Claytonded470d2011-03-19 01:12:21 +0000558 m_os_version_set_while_connected = success;
559 }
560 }
561
562 if (success)
563 {
564 major = m_major_os_version;
565 minor = m_minor_os_version;
566 update = m_update_os_version;
567 }
568 return success;
569}
Greg Clayton1cb64962011-03-24 04:28:38 +0000570
571bool
572Platform::GetOSBuildString (std::string &s)
573{
Zachary Turner97a14e62014-08-19 17:18:29 +0000574 s.clear();
575
Greg Clayton1cb64962011-03-24 04:28:38 +0000576 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000577#if !defined(__linux__)
578 return HostInfo::GetOSBuildString(s);
579#else
580 return false;
581#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000582 else
583 return GetRemoteOSBuildString (s);
584}
585
586bool
587Platform::GetOSKernelDescription (std::string &s)
588{
589 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000590#if !defined(__linux__)
591 return HostInfo::GetOSKernelDescription(s);
592#else
593 return false;
594#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000595 else
596 return GetRemoteOSKernelDescription (s);
597}
598
Sean Callanan5dc29812014-12-05 01:16:31 +0000599void
Greg Claytoncd6bbba2015-01-22 18:25:49 +0000600Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options)
Sean Callanan5dc29812014-12-05 01:16:31 +0000601{
602 std::vector<std::string> default_compilation_options =
603 {
604 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"
605 };
606
607 options.insert(options.end(),
608 default_compilation_options.begin(),
609 default_compilation_options.end());
610}
611
612
Chaoren Lind3173f32015-05-29 19:52:29 +0000613FileSpec
Greg Claytonfbb76342013-11-20 21:07:01 +0000614Platform::GetWorkingDirectory ()
615{
616 if (IsHost())
617 {
618 char cwd[PATH_MAX];
619 if (getcwd(cwd, sizeof(cwd)))
Chaoren Lind3173f32015-05-29 19:52:29 +0000620 return FileSpec{cwd, true};
Greg Claytonfbb76342013-11-20 21:07:01 +0000621 else
Chaoren Lind3173f32015-05-29 19:52:29 +0000622 return FileSpec{};
Greg Claytonfbb76342013-11-20 21:07:01 +0000623 }
624 else
625 {
626 if (!m_working_dir)
627 m_working_dir = GetRemoteWorkingDirectory();
628 return m_working_dir;
629 }
630}
631
632
633struct RecurseCopyBaton
634{
635 const FileSpec& dst;
636 Platform *platform_ptr;
637 Error error;
638};
639
640
641static FileSpec::EnumerateDirectoryResult
642RecurseCopy_Callback (void *baton,
643 FileSpec::FileType file_type,
644 const FileSpec &src)
645{
646 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton;
647 switch (file_type)
648 {
649 case FileSpec::eFileTypePipe:
650 case FileSpec::eFileTypeSocket:
651 // we have no way to copy pipes and sockets - ignore them and continue
652 return FileSpec::eEnumerateDirectoryResultNext;
653 break;
654
655 case FileSpec::eFileTypeDirectory:
656 {
657 // make the new directory and get in there
658 FileSpec dst_dir = rc_baton->dst;
659 if (!dst_dir.GetFilename())
660 dst_dir.GetFilename() = src.GetLastPathComponent();
Chaoren Lind3173f32015-05-29 19:52:29 +0000661 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault);
Greg Claytonfbb76342013-11-20 21:07:01 +0000662 if (error.Fail())
663 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000664 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end",
665 dst_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000666 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
667 }
668
669 // now recurse
670 std::string src_dir_path (src.GetPath());
671
672 // Make a filespec that only fills in the directory of a FileSpec so
673 // when we enumerate we can quickly fill in the filename for dst copies
674 FileSpec recurse_dst;
675 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
676 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() };
677 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2);
678 if (rc_baton2.error.Fail())
679 {
680 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
681 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
682 }
683 return FileSpec::eEnumerateDirectoryResultNext;
684 }
685 break;
686
687 case FileSpec::eFileTypeSymbolicLink:
688 {
689 // copy the file and keep going
690 FileSpec dst_file = rc_baton->dst;
691 if (!dst_file.GetFilename())
692 dst_file.GetFilename() = src.GetFilename();
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000693
Chaoren Lind3173f32015-05-29 19:52:29 +0000694 FileSpec src_resolved;
695
696 rc_baton->error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000697
698 if (rc_baton->error.Fail())
699 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
Chaoren Lind3173f32015-05-29 19:52:29 +0000700
701 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000702
703 if (rc_baton->error.Fail())
704 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
705
706 return FileSpec::eEnumerateDirectoryResultNext;
707 }
708 break;
709 case FileSpec::eFileTypeRegular:
710 {
711 // copy the file and keep going
712 FileSpec dst_file = rc_baton->dst;
713 if (!dst_file.GetFilename())
714 dst_file.GetFilename() = src.GetFilename();
715 Error err = rc_baton->platform_ptr->PutFile(src, dst_file);
716 if (err.Fail())
717 {
718 rc_baton->error.SetErrorString(err.AsCString());
719 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
720 }
721 return FileSpec::eEnumerateDirectoryResultNext;
722 }
723 break;
724
725 case FileSpec::eFileTypeInvalid:
726 case FileSpec::eFileTypeOther:
727 case FileSpec::eFileTypeUnknown:
728 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str());
729 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out
730 break;
731 }
David Majnemer8faf9372014-09-16 06:34:29 +0000732 llvm_unreachable("Unhandled FileSpec::FileType!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000733}
734
735Error
736Platform::Install (const FileSpec& src, const FileSpec& dst)
737{
738 Error error;
739
740 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
741 if (log)
742 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str());
743 FileSpec fixed_dst(dst);
744
745 if (!fixed_dst.GetFilename())
746 fixed_dst.GetFilename() = src.GetFilename();
747
Chaoren Lind3173f32015-05-29 19:52:29 +0000748 FileSpec working_dir = GetWorkingDirectory();
Greg Claytonfbb76342013-11-20 21:07:01 +0000749
750 if (dst)
751 {
752 if (dst.GetDirectory())
753 {
754 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
755 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\')
756 {
757 fixed_dst.GetDirectory() = dst.GetDirectory();
758 }
759 // If the fixed destination file doesn't have a directory yet,
760 // then we must have a relative path. We will resolve this relative
761 // path against the platform's working directory
762 if (!fixed_dst.GetDirectory())
763 {
764 FileSpec relative_spec;
765 std::string path;
766 if (working_dir)
767 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000768 relative_spec = working_dir;
769 relative_spec.AppendPathComponent(dst.GetPath());
Greg Claytonfbb76342013-11-20 21:07:01 +0000770 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
771 }
772 else
773 {
774 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
775 return error;
776 }
777 }
778 }
779 else
780 {
781 if (working_dir)
782 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000783 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000784 }
785 else
786 {
787 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str());
788 return error;
789 }
790 }
791 }
792 else
793 {
794 if (working_dir)
795 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000796 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000797 }
798 else
799 {
800 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty");
801 return error;
802 }
803 }
804
805 if (log)
806 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str());
807
808 if (GetSupportsRSync())
809 {
810 error = PutFile(src, dst);
811 }
812 else
813 {
814 switch (src.GetFileType())
815 {
816 case FileSpec::eFileTypeDirectory:
817 {
818 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000819 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000820 uint32_t permissions = src.GetPermissions();
821 if (permissions == 0)
822 permissions = eFilePermissionsDirectoryDefault;
Chaoren Lind3173f32015-05-29 19:52:29 +0000823 error = MakeDirectory(fixed_dst, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000824 if (error.Success())
825 {
826 // Make a filespec that only fills in the directory of a FileSpec so
827 // when we enumerate we can quickly fill in the filename for dst copies
828 FileSpec recurse_dst;
Chaoren Lind3173f32015-05-29 19:52:29 +0000829 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +0000830 std::string src_dir_path (src.GetPath());
831 RecurseCopyBaton baton = { recurse_dst, this, Error() };
832 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton);
833 return baton.error;
834 }
835 }
836 break;
837
838 case FileSpec::eFileTypeRegular:
839 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000840 Unlink(fixed_dst);
Greg Claytonfbb76342013-11-20 21:07:01 +0000841 error = PutFile(src, fixed_dst);
842 break;
843
844 case FileSpec::eFileTypeSymbolicLink:
845 {
846 if (GetFileExists (fixed_dst))
Chaoren Lind3173f32015-05-29 19:52:29 +0000847 Unlink(fixed_dst);
848 FileSpec src_resolved;
849 error = FileSystem::Readlink(src, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000850 if (error.Success())
Chaoren Lind3173f32015-05-29 19:52:29 +0000851 error = CreateSymlink(dst, src_resolved);
Greg Claytonfbb76342013-11-20 21:07:01 +0000852 }
853 break;
854 case FileSpec::eFileTypePipe:
855 error.SetErrorString("platform install doesn't handle pipes");
856 break;
857 case FileSpec::eFileTypeSocket:
858 error.SetErrorString("platform install doesn't handle sockets");
859 break;
860 case FileSpec::eFileTypeInvalid:
861 case FileSpec::eFileTypeUnknown:
862 case FileSpec::eFileTypeOther:
863 error.SetErrorString("platform install doesn't handle non file or directory items");
864 break;
865 }
866 }
867 return error;
868}
869
870bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000871Platform::SetWorkingDirectory(const FileSpec &file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000872{
873 if (IsHost())
874 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000875 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
876 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000877 log->Printf("Platform::SetWorkingDirectory('%s')",
878 file_spec.GetCString());
879 if (file_spec)
Greg Claytonfbb76342013-11-20 21:07:01 +0000880 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000881 if (::chdir(file_spec.GetCString()) == 0)
Greg Claytonfbb76342013-11-20 21:07:01 +0000882 return true;
883 }
884 return false;
885 }
886 else
887 {
Greg Clayton5fb8f792013-12-02 19:35:49 +0000888 m_working_dir.Clear();
Chaoren Lind3173f32015-05-29 19:52:29 +0000889 return SetRemoteWorkingDirectory(file_spec);
Greg Claytonfbb76342013-11-20 21:07:01 +0000890 }
891}
892
893Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000894Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000895{
896 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000897 return FileSystem::MakeDirectory(file_spec, permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000898 else
899 {
900 Error error;
901 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
902 return error;
903 }
904}
905
906Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000907Platform::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000908{
909 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000910 return FileSystem::GetFilePermissions(file_spec, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000911 else
912 {
913 Error error;
914 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
915 return error;
916 }
917}
918
919Error
Chaoren Lind3173f32015-05-29 19:52:29 +0000920Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions)
Greg Claytonfbb76342013-11-20 21:07:01 +0000921{
922 if (IsHost())
Chaoren Lind3173f32015-05-29 19:52:29 +0000923 return FileSystem::SetFilePermissions(file_spec, file_permissions);
Greg Claytonfbb76342013-11-20 21:07:01 +0000924 else
925 {
926 Error error;
927 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__);
928 return error;
929 }
930}
931
932ConstString
Greg Clayton8b82f082011-04-12 05:54:46 +0000933Platform::GetName ()
934{
Greg Claytonfbb76342013-11-20 21:07:01 +0000935 return GetPluginName();
Greg Clayton8b82f082011-04-12 05:54:46 +0000936}
937
938const char *
Greg Clayton1cb64962011-03-24 04:28:38 +0000939Platform::GetHostname ()
940{
Greg Claytonab65b342011-04-13 22:47:15 +0000941 if (IsHost())
Greg Clayton16810922014-02-27 19:38:18 +0000942 return "127.0.0.1";
Greg Clayton32e0a752011-03-30 18:16:51 +0000943
944 if (m_name.empty())
945 return NULL;
946 return m_name.c_str();
947}
948
Greg Clayton5fb8f792013-12-02 19:35:49 +0000949bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000950Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir)
Greg Clayton5fb8f792013-12-02 19:35:49 +0000951{
952 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
953 if (log)
Chaoren Lind3173f32015-05-29 19:52:29 +0000954 log->Printf("Platform::SetRemoteWorkingDirectory('%s')",
955 working_dir.GetCString());
956 m_working_dir = working_dir;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000957 return true;
958}
959
Greg Clayton32e0a752011-03-30 18:16:51 +0000960const char *
961Platform::GetUserName (uint32_t uid)
962{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000963#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000964 const char *user_name = GetCachedUserName(uid);
965 if (user_name)
966 return user_name;
967 if (IsHost())
968 {
969 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000970 if (HostInfo::LookupUserName(uid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000971 return SetCachedUserName (uid, name.c_str(), name.size());
972 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000973#endif
Greg Clayton1cb64962011-03-24 04:28:38 +0000974 return NULL;
975}
976
Greg Clayton32e0a752011-03-30 18:16:51 +0000977const char *
978Platform::GetGroupName (uint32_t gid)
979{
Zachary Turnerb245eca2014-08-21 20:02:17 +0000980#if !defined(LLDB_DISABLE_POSIX)
Greg Clayton32e0a752011-03-30 18:16:51 +0000981 const char *group_name = GetCachedGroupName(gid);
982 if (group_name)
983 return group_name;
984 if (IsHost())
985 {
986 std::string name;
Zachary Turnerb245eca2014-08-21 20:02:17 +0000987 if (HostInfo::LookupGroupName(gid, name))
Greg Clayton32e0a752011-03-30 18:16:51 +0000988 return SetCachedGroupName (gid, name.c_str(), name.size());
989 }
Zachary Turnerb245eca2014-08-21 20:02:17 +0000990#endif
Greg Clayton32e0a752011-03-30 18:16:51 +0000991 return NULL;
992}
Greg Clayton1cb64962011-03-24 04:28:38 +0000993
Greg Claytonded470d2011-03-19 01:12:21 +0000994bool
995Platform::SetOSVersion (uint32_t major,
996 uint32_t minor,
997 uint32_t update)
998{
999 if (IsHost())
1000 {
Zachary Turner97a14e62014-08-19 17:18:29 +00001001 // We don't need anyone setting the OS version for the host platform,
1002 // we should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +00001003 return false;
1004 }
1005 else
1006 {
1007 // We have a remote platform, allow setting the target OS version if
1008 // we aren't connected, since if we are connected, we should be able to
1009 // request the remote OS version from the connected platform.
1010 if (IsConnected())
1011 return false;
1012 else
1013 {
1014 // We aren't connected and we might want to set the OS version
1015 // ahead of time before we connect so we can peruse files and
1016 // use a local SDK or PDK cache of support files to disassemble
1017 // or do other things.
1018 m_major_os_version = major;
1019 m_minor_os_version = minor;
1020 m_update_os_version = update;
1021 return true;
1022 }
1023 }
1024 return false;
1025}
1026
1027
Greg Claytone996fd32011-03-08 22:40:15 +00001028Error
Greg Clayton8012cad2014-11-17 19:39:20 +00001029Platform::ResolveExecutable (const ModuleSpec &module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001030 lldb::ModuleSP &exe_module_sp,
1031 const FileSpecList *module_search_paths_ptr)
Greg Claytone996fd32011-03-08 22:40:15 +00001032{
1033 Error error;
Greg Clayton8012cad2014-11-17 19:39:20 +00001034 if (module_spec.GetFileSpec().Exists())
Greg Claytone996fd32011-03-08 22:40:15 +00001035 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001036 if (module_spec.GetArchitecture().IsValid())
Greg Claytone996fd32011-03-08 22:40:15 +00001037 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001038 error = ModuleList::GetSharedModule (module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001039 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001040 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001041 NULL,
1042 NULL);
1043 }
1044 else
1045 {
1046 // No valid architecture was specified, ask the platform for
1047 // the architectures that we should be using (in the correct order)
1048 // and see if we can find a match that way
Greg Clayton8012cad2014-11-17 19:39:20 +00001049 ModuleSpec arch_module_spec(module_spec);
1050 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx)
Greg Claytone996fd32011-03-08 22:40:15 +00001051 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001052 error = ModuleList::GetSharedModule (arch_module_spec,
Greg Claytone996fd32011-03-08 22:40:15 +00001053 exe_module_sp,
Greg Claytonc859e2d2012-02-13 23:10:39 +00001054 module_search_paths_ptr,
Greg Claytone996fd32011-03-08 22:40:15 +00001055 NULL,
1056 NULL);
1057 // Did we find an executable using one of the
1058 if (error.Success() && exe_module_sp)
1059 break;
1060 }
1061 }
1062 }
1063 else
1064 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001065 error.SetErrorStringWithFormat ("'%s' does not exist",
Greg Clayton8012cad2014-11-17 19:39:20 +00001066 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytone996fd32011-03-08 22:40:15 +00001067 }
1068 return error;
1069}
1070
Greg Clayton103f0282012-09-12 02:03:59 +00001071Error
1072Platform::ResolveSymbolFile (Target &target,
1073 const ModuleSpec &sym_spec,
1074 FileSpec &sym_file)
1075{
1076 Error error;
1077 if (sym_spec.GetSymbolFileSpec().Exists())
1078 sym_file = sym_spec.GetSymbolFileSpec();
1079 else
1080 error.SetErrorString("unable to resolve symbol file");
1081 return error;
1082
1083}
1084
1085
1086
Greg Claytonaa516842011-08-11 16:25:18 +00001087bool
1088Platform::ResolveRemotePath (const FileSpec &platform_path,
1089 FileSpec &resolved_platform_path)
1090{
1091 resolved_platform_path = platform_path;
1092 return resolved_platform_path.ResolvePath();
1093}
1094
Greg Claytonded470d2011-03-19 01:12:21 +00001095
1096const ArchSpec &
1097Platform::GetSystemArchitecture()
1098{
1099 if (IsHost())
1100 {
1101 if (!m_system_arch.IsValid())
1102 {
1103 // We have a local host platform
Zachary Turner13b18262014-08-20 16:42:51 +00001104 m_system_arch = HostInfo::GetArchitecture();
Greg Claytonded470d2011-03-19 01:12:21 +00001105 m_system_arch_set_while_connected = m_system_arch.IsValid();
1106 }
1107 }
1108 else
1109 {
1110 // We have a remote platform. We can only fetch the remote
1111 // system architecture if we are connected, and we don't want to do it
1112 // more than once.
1113
1114 const bool is_connected = IsConnected();
1115
1116 bool fetch = false;
1117 if (m_system_arch.IsValid())
1118 {
1119 // We have valid OS version info, check to make sure it wasn't
1120 // manually set prior to connecting. If it was manually set prior
1121 // to connecting, then lets fetch the actual OS version info
1122 // if we are now connected.
1123 if (is_connected && !m_system_arch_set_while_connected)
1124 fetch = true;
1125 }
1126 else
1127 {
1128 // We don't have valid OS version info, fetch it if we are connected
1129 fetch = is_connected;
1130 }
1131
1132 if (fetch)
1133 {
Greg Clayton1cb64962011-03-24 04:28:38 +00001134 m_system_arch = GetRemoteSystemArchitecture ();
Greg Claytonded470d2011-03-19 01:12:21 +00001135 m_system_arch_set_while_connected = m_system_arch.IsValid();
1136 }
1137 }
1138 return m_system_arch;
1139}
1140
1141
Greg Claytone996fd32011-03-08 22:40:15 +00001142Error
Greg Claytond314e812011-03-23 00:09:55 +00001143Platform::ConnectRemote (Args& args)
Greg Claytone996fd32011-03-08 22:40:15 +00001144{
1145 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001146 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001147 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001148 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001149 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001150 return error;
1151}
1152
1153Error
Greg Claytond314e812011-03-23 00:09:55 +00001154Platform::DisconnectRemote ()
Greg Claytone996fd32011-03-08 22:40:15 +00001155{
1156 Error error;
Greg Claytond314e812011-03-23 00:09:55 +00001157 if (IsHost())
Greg Clayton57abc5d2013-05-10 21:47:16 +00001158 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
Greg Claytond314e812011-03-23 00:09:55 +00001159 else
Greg Clayton57abc5d2013-05-10 21:47:16 +00001160 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
Greg Claytone996fd32011-03-08 22:40:15 +00001161 return error;
1162}
Greg Clayton32e0a752011-03-30 18:16:51 +00001163
1164bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001165Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001166{
1167 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001168 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001169 if (IsHost())
1170 return Host::GetProcessInfo (pid, process_info);
1171 return false;
1172}
1173
1174uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001175Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1176 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001177{
Greg Clayton8b82f082011-04-12 05:54:46 +00001178 // Take care of the host case so that each subclass can just
1179 // call this function to get the host functionality.
Greg Clayton32e0a752011-03-30 18:16:51 +00001180 uint32_t match_count = 0;
1181 if (IsHost())
1182 match_count = Host::FindProcesses (match_info, process_infos);
1183 return match_count;
1184}
Greg Clayton8b82f082011-04-12 05:54:46 +00001185
1186
1187Error
1188Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
1189{
1190 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00001191 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1192 if (log)
1193 log->Printf ("Platform::%s()", __FUNCTION__);
1194
1195 // Take care of the host case so that each subclass can just
Greg Clayton8b82f082011-04-12 05:54:46 +00001196 // call this function to get the host functionality.
1197 if (IsHost())
Greg Clayton84db9102012-03-26 23:03:23 +00001198 {
1199 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1200 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
1201
1202 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
1203 {
1204 const bool is_localhost = true;
Greg Claytond1cf11a2012-04-14 01:42:46 +00001205 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1206 const bool first_arg_is_full_shell_command = false;
Jim Inghamd3990792013-09-11 18:23:22 +00001207 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info);
Todd Fialaac33cc92014-10-09 01:02:08 +00001208 if (log)
Zachary Turner10687b02014-10-20 17:46:43 +00001209 {
1210 const FileSpec &shell = launch_info.GetShell();
1211 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>";
Todd Fialaac33cc92014-10-09 01:02:08 +00001212 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'",
1213 __FUNCTION__,
1214 num_resumes,
Zachary Turner10687b02014-10-20 17:46:43 +00001215 shell_str);
1216 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001217
Greg Claytond1cf11a2012-04-14 01:42:46 +00001218 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
1219 is_localhost,
1220 will_debug,
Jim Inghamdf0ae222013-09-10 02:09:47 +00001221 first_arg_is_full_shell_command,
1222 num_resumes))
Greg Clayton84db9102012-03-26 23:03:23 +00001223 return error;
1224 }
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001225 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments))
Enrico Granatad7a83a92015-02-10 03:06:24 +00001226 {
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001227 error = ShellExpandArguments(launch_info);
Enrico Granata83a14372015-02-20 21:48:38 +00001228 if (error.Fail())
Enrico Granatad7a83a92015-02-10 03:06:24 +00001229 return error;
Enrico Granatad7a83a92015-02-10 03:06:24 +00001230 }
Greg Clayton84db9102012-03-26 23:03:23 +00001231
Todd Fialaac33cc92014-10-09 01:02:08 +00001232 if (log)
1233 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ());
1234
Greg Clayton8b82f082011-04-12 05:54:46 +00001235 error = Host::LaunchProcess (launch_info);
Greg Clayton84db9102012-03-26 23:03:23 +00001236 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001237 else
1238 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
1239 return error;
1240}
1241
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001242Error
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001243Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info)
Enrico Granata83a14372015-02-20 21:48:38 +00001244{
1245 if (IsHost())
Enrico Granatab38ef8c2015-02-20 22:20:30 +00001246 return Host::ShellExpandArguments(launch_info);
1247 return Error("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001248}
1249
1250Error
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001251Platform::KillProcess (const lldb::pid_t pid)
1252{
1253 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1254 if (log)
1255 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
1256
Zachary Turner7271bab2015-05-13 19:44:24 +00001257 // Try to find a process plugin to handle this Kill request. If we can't, fall back to
1258 // the default OS implementation.
1259 size_t num_debuggers = Debugger::GetNumDebuggers();
1260 for (size_t didx = 0; didx < num_debuggers; ++didx)
1261 {
1262 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1263 lldb_private::TargetList &targets = debugger->GetTargetList();
1264 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx)
1265 {
1266 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1267 if (process->GetID() == pid)
1268 return process->Destroy(true);
1269 }
1270 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001271
Zachary Turner7271bab2015-05-13 19:44:24 +00001272 if (!IsHost())
1273 {
1274 return Error("base lldb_private::Platform class can't kill remote processes unless "
1275 "they are controlled by a process plugin");
1276 }
1277 Host::Kill(pid, SIGTERM);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001278 return Error();
1279}
1280
Greg Clayton8b82f082011-04-12 05:54:46 +00001281lldb::ProcessSP
1282Platform::DebugProcess (ProcessLaunchInfo &launch_info,
1283 Debugger &debugger,
1284 Target *target, // Can be NULL, if NULL create a new target, else use existing one
Greg Clayton8b82f082011-04-12 05:54:46 +00001285 Error &error)
1286{
Todd Fialaac33cc92014-10-09 01:02:08 +00001287 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1288 if (log)
1289 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target));
1290
Greg Clayton8b82f082011-04-12 05:54:46 +00001291 ProcessSP process_sp;
1292 // Make sure we stop at the entry point
1293 launch_info.GetFlags ().Set (eLaunchFlagDebug);
Jim Inghamb4451b12012-06-01 01:22:13 +00001294 // We always launch the process we are going to debug in a separate process
1295 // group, since then we can handle ^C interrupts ourselves w/o having to worry
1296 // about the target getting them as well.
1297 launch_info.SetLaunchInSeparateProcessGroup(true);
1298
Greg Clayton8b82f082011-04-12 05:54:46 +00001299 error = LaunchProcess (launch_info);
1300 if (error.Success())
1301 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001302 if (log)
1303 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ());
Greg Clayton144f3a92011-11-15 03:53:30 +00001304 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton8b82f082011-04-12 05:54:46 +00001305 {
Greg Clayton144f3a92011-11-15 03:53:30 +00001306 ProcessAttachInfo attach_info (launch_info);
Greg Clayton8012cad2014-11-17 19:39:20 +00001307 process_sp = Attach (attach_info, debugger, target, error);
Greg Claytone24c4ac2011-11-17 04:46:02 +00001308 if (process_sp)
1309 {
Todd Fialaac33cc92014-10-09 01:02:08 +00001310 if (log)
1311 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ());
Greg Clayton44d93782014-01-27 23:43:24 +00001312 launch_info.SetHijackListener(attach_info.GetHijackListener());
1313
Greg Claytone24c4ac2011-11-17 04:46:02 +00001314 // Since we attached to the process, it will think it needs to detach
1315 // if the process object just goes away without an explicit call to
1316 // Process::Kill() or Process::Detach(), so let it know to kill the
1317 // process if this happens.
1318 process_sp->SetShouldDetach (false);
Greg Claytonee95ed52011-11-17 22:14:31 +00001319
1320 // If we didn't have any file actions, the pseudo terminal might
1321 // have been used where the slave side was given as the file to
1322 // open for stdin/out/err after we have already opened the master
1323 // so we can read/write stdin/out/err.
1324 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
1325 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
1326 {
1327 process_sp->SetSTDIOFileDescriptor(pty_fd);
1328 }
Greg Claytone24c4ac2011-11-17 04:46:02 +00001329 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001330 else
1331 {
1332 if (log)
1333 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ());
1334 }
1335 }
1336 else
1337 {
1338 if (log)
1339 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__);
Greg Clayton8b82f082011-04-12 05:54:46 +00001340 }
1341 }
Todd Fialaac33cc92014-10-09 01:02:08 +00001342 else
1343 {
1344 if (log)
1345 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ());
1346 }
1347
Greg Clayton8b82f082011-04-12 05:54:46 +00001348 return process_sp;
1349}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001350
1351
1352lldb::PlatformSP
Greg Clayton70512312012-05-08 01:45:38 +00001353Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001354{
1355 lldb::PlatformSP platform_sp;
1356 Error error;
1357 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +00001358 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001359 return platform_sp;
1360}
1361
1362
1363//------------------------------------------------------------------
1364/// Lets a platform answer if it is compatible with a given
1365/// architecture and the target triple contained within.
1366//------------------------------------------------------------------
1367bool
Greg Clayton1e0c8842013-01-11 20:49:54 +00001368Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001369{
1370 // If the architecture is invalid, we must answer true...
Greg Clayton70512312012-05-08 01:45:38 +00001371 if (arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001372 {
Greg Clayton70512312012-05-08 01:45:38 +00001373 ArchSpec platform_arch;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001374 // Try for an exact architecture match first.
1375 if (exact_arch_match)
Greg Clayton70512312012-05-08 01:45:38 +00001376 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001377 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
Greg Clayton70512312012-05-08 01:45:38 +00001378 {
Greg Clayton1e0c8842013-01-11 20:49:54 +00001379 if (arch.IsExactMatch(platform_arch))
1380 {
1381 if (compatible_arch_ptr)
1382 *compatible_arch_ptr = platform_arch;
1383 return true;
1384 }
1385 }
1386 }
1387 else
1388 {
1389 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
1390 {
1391 if (arch.IsCompatibleMatch(platform_arch))
1392 {
1393 if (compatible_arch_ptr)
1394 *compatible_arch_ptr = platform_arch;
1395 return true;
1396 }
Greg Clayton70512312012-05-08 01:45:38 +00001397 }
1398 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001399 }
Greg Clayton70512312012-05-08 01:45:38 +00001400 if (compatible_arch_ptr)
1401 compatible_arch_ptr->Clear();
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001402 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001403}
1404
Daniel Maleae0f8f572013-08-26 23:57:52 +00001405Error
1406Platform::PutFile (const FileSpec& source,
1407 const FileSpec& destination,
1408 uint32_t uid,
1409 uint32_t gid)
1410{
Vince Harron1b5a74e2015-01-21 22:42:49 +00001411 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
1412 if (log)
1413 log->Printf("[PutFile] Using block by block transfer....\n");
1414
Pavel Labath646b0642015-02-17 16:07:52 +00001415 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001416 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink)
1417 source_open_options |= File::eOpenoptionDontFollowSymlinks;
1418
1419 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW);
1420 Error error;
1421 uint32_t permissions = source_file.GetPermissions(error);
1422 if (permissions == 0)
1423 permissions = lldb::eFilePermissionsFileDefault;
1424
1425 if (!source_file.IsValid())
1426 return Error("PutFile: unable to open source file");
1427 lldb::user_id_t dest_file = OpenFile (destination,
1428 File::eOpenOptionCanCreate |
1429 File::eOpenOptionWrite |
Pavel Labath646b0642015-02-17 16:07:52 +00001430 File::eOpenOptionTruncate |
1431 File::eOpenOptionCloseOnExec,
Vince Harron1b5a74e2015-01-21 22:42:49 +00001432 permissions,
1433 error);
1434 if (log)
1435 log->Printf ("dest_file = %" PRIu64 "\n", dest_file);
1436
1437 if (error.Fail())
1438 return error;
1439 if (dest_file == UINT64_MAX)
1440 return Error("unable to open target file");
1441 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
1442 uint64_t offset = 0;
1443 for (;;)
1444 {
1445 size_t bytes_read = buffer_sp->GetByteSize();
1446 error = source_file.Read(buffer_sp->GetBytes(), bytes_read);
1447 if (error.Fail() || bytes_read == 0)
1448 break;
1449
1450 const uint64_t bytes_written = WriteFile(dest_file, offset,
1451 buffer_sp->GetBytes(), bytes_read, error);
1452 if (error.Fail())
1453 break;
1454
1455 offset += bytes_written;
1456 if (bytes_written != bytes_read)
1457 {
1458 // We didn't write the correct number of bytes, so adjust
1459 // the file position in the source file we are reading from...
1460 source_file.SeekFromStart(offset);
1461 }
1462 }
1463 CloseFile(dest_file, error);
1464
1465 if (uid == UINT32_MAX && gid == UINT32_MAX)
1466 return error;
1467
1468 // TODO: ChownFile?
1469
Daniel Maleae0f8f572013-08-26 23:57:52 +00001470 return error;
1471}
1472
1473Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001474Platform::GetFile(const FileSpec &source,
1475 const FileSpec &destination)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001476{
1477 Error error("unimplemented");
1478 return error;
1479}
1480
Greg Claytonfbb76342013-11-20 21:07:01 +00001481Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001482Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1483 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001484{
1485 Error error("unimplemented");
1486 return error;
1487}
1488
Daniel Maleae0f8f572013-08-26 23:57:52 +00001489bool
Chaoren Lind3173f32015-05-29 19:52:29 +00001490Platform::GetFileExists(const lldb_private::FileSpec &file_spec)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001491{
1492 return false;
1493}
1494
Greg Claytonfbb76342013-11-20 21:07:01 +00001495Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001496Platform::Unlink(const FileSpec &path)
Greg Claytonfbb76342013-11-20 21:07:01 +00001497{
1498 Error error("unimplemented");
1499 return error;
1500}
1501
Robert Flack96ad3de2015-05-09 15:53:31 +00001502uint64_t
1503Platform::ConvertMmapFlagsToPlatform(unsigned flags)
1504{
1505 uint64_t flags_platform = 0;
1506 if (flags & eMmapFlagsPrivate)
1507 flags_platform |= MAP_PRIVATE;
1508 if (flags & eMmapFlagsAnon)
1509 flags_platform |= MAP_ANON;
1510 return flags_platform;
1511}
Greg Claytonfbb76342013-11-20 21:07:01 +00001512
Daniel Maleae0f8f572013-08-26 23:57:52 +00001513lldb_private::Error
Chaoren Lind3173f32015-05-29 19:52:29 +00001514Platform::RunShellCommand(const char *command, // Shouldn't be NULL
1515 const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory
1516 int *status_ptr, // Pass NULL if you don't want the process exit status
1517 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
1518 std::string *command_output, // Pass NULL if you don't want the command output
1519 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
Daniel Maleae0f8f572013-08-26 23:57:52 +00001520{
1521 if (IsHost())
1522 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
1523 else
1524 return Error("unimplemented");
1525}
1526
1527
1528bool
1529Platform::CalculateMD5 (const FileSpec& file_spec,
1530 uint64_t &low,
1531 uint64_t &high)
1532{
1533 if (IsHost())
Zachary Turnerc00cf4a2014-08-15 22:04:21 +00001534 return FileSystem::CalculateMD5(file_spec, low, high);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001535 else
1536 return false;
1537}
1538
Todd Fialaaf245d12014-06-30 21:05:18 +00001539Error
1540Platform::LaunchNativeProcess (
1541 ProcessLaunchInfo &launch_info,
1542 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1543 NativeProcessProtocolSP &process_sp)
1544{
1545 // Platforms should override this implementation if they want to
1546 // support lldb-gdbserver.
1547 return Error("unimplemented");
1548}
1549
1550Error
1551Platform::AttachNativeProcess (lldb::pid_t pid,
1552 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
1553 NativeProcessProtocolSP &process_sp)
1554{
1555 // Platforms should override this implementation if they want to
1556 // support lldb-gdbserver.
1557 return Error("unimplemented");
1558}
1559
Daniel Maleae0f8f572013-08-26 23:57:52 +00001560void
1561Platform::SetLocalCacheDirectory (const char* local)
1562{
1563 m_local_cache_directory.assign(local);
1564}
1565
1566const char*
1567Platform::GetLocalCacheDirectory ()
1568{
1569 return m_local_cache_directory.c_str();
1570}
1571
1572static OptionDefinition
1573g_rsync_option_table[] =
1574{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001575 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." },
1576 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." },
1577 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." },
1578 { 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 +00001579};
1580
1581static OptionDefinition
1582g_ssh_option_table[] =
1583{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001584 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." },
1585 { 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 +00001586};
1587
1588static OptionDefinition
1589g_caching_option_table[] =
1590{
Zachary Turnerd37221d2014-07-09 16:31:49 +00001591 { 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 +00001592};
1593
1594OptionGroupPlatformRSync::OptionGroupPlatformRSync ()
1595{
1596}
1597
1598OptionGroupPlatformRSync::~OptionGroupPlatformRSync ()
1599{
1600}
1601
1602const lldb_private::OptionDefinition*
1603OptionGroupPlatformRSync::GetDefinitions ()
1604{
1605 return g_rsync_option_table;
1606}
1607
1608void
1609OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter)
1610{
1611 m_rsync = false;
1612 m_rsync_opts.clear();
1613 m_rsync_prefix.clear();
1614 m_ignores_remote_hostname = false;
1615}
1616
1617lldb_private::Error
1618OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter,
1619 uint32_t option_idx,
1620 const char *option_arg)
1621{
1622 Error error;
1623 char short_option = (char) GetDefinitions()[option_idx].short_option;
1624 switch (short_option)
1625 {
1626 case 'r':
1627 m_rsync = true;
1628 break;
1629
1630 case 'R':
1631 m_rsync_opts.assign(option_arg);
1632 break;
1633
1634 case 'P':
1635 m_rsync_prefix.assign(option_arg);
1636 break;
1637
1638 case 'i':
1639 m_ignores_remote_hostname = true;
1640 break;
1641
1642 default:
1643 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1644 break;
1645 }
1646
1647 return error;
1648}
1649
1650uint32_t
1651OptionGroupPlatformRSync::GetNumDefinitions ()
1652{
1653 return llvm::array_lengthof(g_rsync_option_table);
1654}
Greg Clayton1e0c8842013-01-11 20:49:54 +00001655
Greg Clayton4116e932012-05-15 02:33:01 +00001656lldb::BreakpointSP
1657Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
1658{
1659 return lldb::BreakpointSP();
1660}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001661
Daniel Maleae0f8f572013-08-26 23:57:52 +00001662OptionGroupPlatformSSH::OptionGroupPlatformSSH ()
1663{
1664}
1665
1666OptionGroupPlatformSSH::~OptionGroupPlatformSSH ()
1667{
1668}
1669
1670const lldb_private::OptionDefinition*
1671OptionGroupPlatformSSH::GetDefinitions ()
1672{
1673 return g_ssh_option_table;
1674}
1675
1676void
1677OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter)
1678{
1679 m_ssh = false;
1680 m_ssh_opts.clear();
1681}
1682
1683lldb_private::Error
1684OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter,
1685 uint32_t option_idx,
1686 const char *option_arg)
1687{
1688 Error error;
1689 char short_option = (char) GetDefinitions()[option_idx].short_option;
1690 switch (short_option)
1691 {
1692 case 's':
1693 m_ssh = true;
1694 break;
1695
1696 case 'S':
1697 m_ssh_opts.assign(option_arg);
1698 break;
1699
1700 default:
1701 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1702 break;
1703 }
1704
1705 return error;
1706}
1707
1708uint32_t
1709OptionGroupPlatformSSH::GetNumDefinitions ()
1710{
1711 return llvm::array_lengthof(g_ssh_option_table);
1712}
1713
1714OptionGroupPlatformCaching::OptionGroupPlatformCaching ()
1715{
1716}
1717
1718OptionGroupPlatformCaching::~OptionGroupPlatformCaching ()
1719{
1720}
1721
1722const lldb_private::OptionDefinition*
1723OptionGroupPlatformCaching::GetDefinitions ()
1724{
1725 return g_caching_option_table;
1726}
1727
1728void
1729OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter)
1730{
1731 m_cache_dir.clear();
1732}
1733
1734lldb_private::Error
1735OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter,
1736 uint32_t option_idx,
1737 const char *option_arg)
1738{
1739 Error error;
1740 char short_option = (char) GetDefinitions()[option_idx].short_option;
1741 switch (short_option)
1742 {
1743 case 'c':
1744 m_cache_dir.assign(option_arg);
1745 break;
1746
1747 default:
1748 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1749 break;
1750 }
1751
1752 return error;
1753}
1754
1755uint32_t
1756OptionGroupPlatformCaching::GetNumDefinitions ()
1757{
1758 return llvm::array_lengthof(g_caching_option_table);
1759}
1760
Greg Clayton67cc0632012-08-22 17:17:09 +00001761size_t
1762Platform::GetEnvironment (StringList &environment)
1763{
1764 environment.Clear();
1765 return false;
1766}
Jason Molenda2094dbf2014-02-13 23:11:45 +00001767
1768const std::vector<ConstString> &
1769Platform::GetTrapHandlerSymbolNames ()
1770{
1771 if (!m_calculated_trap_handlers)
1772 {
Greg Clayton7597b352015-02-02 20:45:17 +00001773 Mutex::Locker locker (m_mutex);
Jason Molenda4da87062014-05-23 23:11:27 +00001774 if (!m_calculated_trap_handlers)
1775 {
1776 CalculateTrapHandlerSymbolNames();
1777 m_calculated_trap_handlers = true;
1778 }
Jason Molenda2094dbf2014-02-13 23:11:45 +00001779 }
1780 return m_trap_handlers;
1781}
1782
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001783Error
1784Platform::GetCachedExecutable (ModuleSpec &module_spec,
1785 lldb::ModuleSP &module_sp,
1786 const FileSpecList *module_search_paths_ptr,
1787 Platform &remote_platform)
1788{
1789 const auto platform_spec = module_spec.GetFileSpec ();
1790 const auto error = LoadCachedExecutable (module_spec,
1791 module_sp,
1792 module_search_paths_ptr,
1793 remote_platform);
1794 if (error.Success ())
1795 {
1796 module_spec.GetFileSpec () = module_sp->GetFileSpec ();
1797 module_spec.GetPlatformFileSpec () = platform_spec;
1798 }
1799
1800 return error;
1801}
1802
1803Error
1804Platform::LoadCachedExecutable (const ModuleSpec &module_spec,
1805 lldb::ModuleSP &module_sp,
1806 const FileSpecList *module_search_paths_ptr,
1807 Platform &remote_platform)
1808{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001809 return GetRemoteSharedModule (module_spec,
1810 nullptr,
1811 module_sp,
1812 [&](const ModuleSpec &spec)
1813 {
1814 return remote_platform.ResolveExecutable (
1815 spec, module_sp, module_search_paths_ptr);
1816 },
1817 nullptr);
Oleksiy Vyalov64747212015-03-13 18:44:56 +00001818}
1819
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001820Error
1821Platform::GetRemoteSharedModule (const ModuleSpec &module_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001822 Process* process,
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001823 lldb::ModuleSP &module_sp,
1824 const ModuleResolver &module_resolver,
1825 bool *did_create_ptr)
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001826{
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001827 // Get module information from a target.
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001828 ModuleSpec resolved_module_spec;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001829 bool got_module_spec = false;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001830 if (process)
1831 {
1832 // Try to get module information from the process
1833 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001834 got_module_spec = true;
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001835 }
1836
1837 if (!got_module_spec)
1838 {
1839 // Get module information from a target.
1840 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec))
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001841 return module_resolver (module_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001842 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001843
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001844 // Trying to find a module by UUID on local file system.
1845 const auto error = module_resolver (resolved_module_spec);
1846 if (error.Fail ())
1847 {
1848 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr))
1849 return Error ();
1850 }
1851
1852 return error;
1853}
1854
1855bool
1856Platform::GetCachedSharedModule (const ModuleSpec &module_spec,
1857 lldb::ModuleSP &module_sp,
1858 bool *did_create_ptr)
1859{
1860 if (IsHost() ||
1861 !GetGlobalPlatformProperties ()->GetUseModuleCache ())
1862 return false;
1863
1864 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM);
1865
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001866 // Check local cache for a module.
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001867 auto error = m_module_cache->GetAndPut (
1868 GetModuleCacheRoot (),
1869 GetCacheHostname (),
1870 module_spec,
Oleksiy Vyalov919ef9d2015-05-07 15:28:49 +00001871 [=](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec)
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001872 {
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001873 return DownloadModuleSlice (module_spec.GetFileSpec (),
1874 module_spec.GetObjectOffset (),
1875 module_spec.GetObjectSize (),
1876 tmp_download_file_spec);
1877
1878 },
1879 module_sp,
1880 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001881 if (error.Success ())
1882 return true;
1883
1884 if (log)
1885 log->Printf("Platform::%s - module %s not found in local cache: %s",
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001886 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ());
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001887 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001888}
1889
1890Error
1891Platform::DownloadModuleSlice (const FileSpec& src_file_spec,
1892 const uint64_t src_offset,
1893 const uint64_t src_size,
1894 const FileSpec& dst_file_spec)
1895{
1896 Error error;
1897
1898 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary);
1899 if (!dst.is_open())
1900 {
1901 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ());
1902 return error;
1903 }
1904
1905 auto src_fd = OpenFile (src_file_spec,
1906 File::eOpenOptionRead,
1907 lldb::eFilePermissionsFileDefault,
1908 error);
1909
1910 if (error.Fail ())
1911 {
1912 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ());
1913 return error;
1914 }
1915
1916 std::vector<char> buffer (1024);
1917 auto offset = src_offset;
1918 uint64_t total_bytes_read = 0;
1919 while (total_bytes_read < src_size)
1920 {
1921 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read);
1922 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error);
1923 if (error.Fail ())
1924 break;
1925 if (n_read == 0)
1926 {
1927 error.SetErrorString ("read 0 bytes");
1928 break;
1929 }
1930 offset += n_read;
1931 total_bytes_read += n_read;
1932 dst.write (&buffer[0], n_read);
1933 }
1934
1935 Error close_error;
1936 CloseFile (src_fd, close_error); // Ignoring close error.
1937
1938 return error;
1939}
1940
1941FileSpec
1942Platform::GetModuleCacheRoot ()
1943{
1944 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory ();
1945 dir_spec.AppendPathComponent (GetName ().AsCString ());
1946 return dir_spec;
1947}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001948
1949const char *
1950Platform::GetCacheHostname ()
1951{
1952 return GetHostname ();
1953}