1 //===-- Platform.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/Platform.h"
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/BreakpointIDList.h"
17 #include "lldb/Core/Error.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Core/ModuleSpec.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Host/FileSpec.h"
22 #include "lldb/Host/Host.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
27 using namespace lldb_private;
29 // Use a singleton function for g_local_platform_sp to avoid init
30 // constructors since LLDB is often part of a shared library
32 GetDefaultPlatformSP ()
34 static PlatformSP g_default_platform_sp;
35 return g_default_platform_sp;
39 GetConnectedPlatformListMutex ()
41 static Mutex g_remote_connected_platforms_mutex (Mutex::eMutexTypeRecursive);
42 return g_remote_connected_platforms_mutex;
44 static std::vector<PlatformSP> &
45 GetConnectedPlatformList ()
47 static std::vector<PlatformSP> g_remote_connected_platforms;
48 return g_remote_connected_platforms;
53 Platform::GetHostPlatformName ()
58 //------------------------------------------------------------------
59 /// Get the native host platform plug-in.
61 /// There should only be one of these for each host that LLDB runs
62 /// upon that should be statically compiled in and registered using
63 /// preprocessor macros or other similar build mechanisms.
65 /// This platform will be used as the default platform when launching
66 /// or attaching to processes unless another platform is specified.
67 //------------------------------------------------------------------
69 Platform::GetDefaultPlatform ()
71 return GetDefaultPlatformSP ();
75 Platform::SetDefaultPlatform (const lldb::PlatformSP &platform_sp)
77 // The native platform should use its static void Platform::Initialize()
78 // function to register itself as the native platform.
79 GetDefaultPlatformSP () = platform_sp;
83 Platform::GetFile (const FileSpec &platform_file,
87 // Default to the local case
88 local_file = platform_file;
93 Platform::LocateExecutableScriptingResources (Target *target, Module &module)
95 return FileSpecList();
99 Platform::FindPlugin (Process *process, const ConstString &plugin_name)
101 PlatformCreateInstance create_callback = NULL;
104 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
110 arch = process->GetTarget().GetArchitecture();
112 std::unique_ptr<Platform> instance_ap(create_callback(process, &arch));
113 if (instance_ap.get())
114 return instance_ap.release();
119 for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx)
121 std::unique_ptr<Platform> instance_ap(create_callback(process, nullptr));
122 if (instance_ap.get())
123 return instance_ap.release();
130 Platform::GetSharedModule (const ModuleSpec &module_spec,
132 const FileSpecList *module_search_paths_ptr,
133 ModuleSP *old_module_sp_ptr,
134 bool *did_create_ptr)
136 // Don't do any path remapping for the default implementation
137 // of the platform GetSharedModule function, just call through
138 // to our static ModuleList function. Platform subclasses that
139 // implement remote debugging, might have a developer kits
140 // installed that have cached versions of the files for the
141 // remote target, or might implement a download and cache
142 // locally implementation.
143 const bool always_create = false;
144 return ModuleList::GetSharedModule (module_spec,
146 module_search_paths_ptr,
153 Platform::Create (const char *platform_name, Error &error)
155 PlatformCreateInstance create_callback = NULL;
156 lldb::PlatformSP platform_sp;
157 if (platform_name && platform_name[0])
159 ConstString const_platform_name (platform_name);
160 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (const_platform_name);
162 platform_sp.reset(create_callback(true, NULL));
164 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", platform_name);
167 error.SetErrorString ("invalid platform name");
173 Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error)
175 lldb::PlatformSP platform_sp;
179 PlatformCreateInstance create_callback;
180 // First try exact arch matches across all platform plug-ins
182 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
186 platform_sp.reset(create_callback(false, &arch));
187 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr))
191 // Next try compatible arch matches across all platform plug-ins
193 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx)
197 platform_sp.reset(create_callback(false, &arch));
198 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr))
204 error.SetErrorString ("invalid platform name");
205 if (platform_arch_ptr)
206 platform_arch_ptr->Clear();
212 Platform::GetNumConnectedRemotePlatforms ()
214 Mutex::Locker locker (GetConnectedPlatformListMutex ());
215 return GetConnectedPlatformList().size();
219 Platform::GetConnectedRemotePlatformAtIndex (uint32_t idx)
221 PlatformSP platform_sp;
223 Mutex::Locker locker (GetConnectedPlatformListMutex ());
224 if (idx < GetConnectedPlatformList().size())
225 platform_sp = GetConnectedPlatformList ()[idx];
230 //------------------------------------------------------------------
231 /// Default Constructor
232 //------------------------------------------------------------------
233 Platform::Platform (bool is_host) :
235 m_os_version_set_while_connected (false),
236 m_system_arch_set_while_connected (false),
241 m_major_os_version (UINT32_MAX),
242 m_minor_os_version (UINT32_MAX),
243 m_update_os_version (UINT32_MAX),
245 m_uid_map_mutex (Mutex::eMutexTypeNormal),
246 m_gid_map_mutex (Mutex::eMutexTypeNormal),
249 m_max_uid_name_len (0),
250 m_max_gid_name_len (0)
252 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
254 log->Printf ("%p Platform::Platform()", this);
257 //------------------------------------------------------------------
260 /// The destructor is virtual since this class is designed to be
261 /// inherited from by the plug-in instance.
262 //------------------------------------------------------------------
263 Platform::~Platform()
265 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
267 log->Printf ("%p Platform::~Platform()", this);
271 Platform::GetStatus (Stream &strm)
273 uint32_t major = UINT32_MAX;
274 uint32_t minor = UINT32_MAX;
275 uint32_t update = UINT32_MAX;
277 strm.Printf (" Platform: %s\n", GetPluginName().GetCString());
279 ArchSpec arch (GetSystemArchitecture());
282 if (!arch.GetTriple().str().empty())
283 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str());
286 if (GetOSVersion(major, minor, update))
288 strm.Printf("OS Version: %u", major);
289 if (minor != UINT32_MAX)
290 strm.Printf(".%u", minor);
291 if (update != UINT32_MAX)
292 strm.Printf(".%u", update);
294 if (GetOSBuildString (s))
295 strm.Printf(" (%s)", s.c_str());
300 if (GetOSKernelDescription (s))
301 strm.Printf(" Kernel: %s\n", s.c_str());
305 strm.Printf(" Hostname: %s\n", GetHostname());
309 const bool is_connected = IsConnected();
311 strm.Printf(" Hostname: %s\n", GetHostname());
312 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
318 Platform::GetOSVersion (uint32_t &major,
322 bool success = m_major_os_version != UINT32_MAX;
327 // We have a local host platform
328 success = Host::GetOSVersion (m_major_os_version,
330 m_update_os_version);
331 m_os_version_set_while_connected = success;
336 // We have a remote platform. We can only fetch the remote
337 // OS version if we are connected, and we don't want to do it
340 const bool is_connected = IsConnected();
345 // We have valid OS version info, check to make sure it wasn't
346 // manually set prior to connecting. If it was manually set prior
347 // to connecting, then lets fetch the actual OS version info
348 // if we are now connected.
349 if (is_connected && !m_os_version_set_while_connected)
354 // We don't have valid OS version info, fetch it if we are connected
355 fetch = is_connected;
360 success = GetRemoteOSVersion ();
361 m_os_version_set_while_connected = success;
367 major = m_major_os_version;
368 minor = m_minor_os_version;
369 update = m_update_os_version;
375 Platform::GetOSBuildString (std::string &s)
378 return Host::GetOSBuildString (s);
380 return GetRemoteOSBuildString (s);
384 Platform::GetOSKernelDescription (std::string &s)
387 return Host::GetOSKernelDescription (s);
389 return GetRemoteOSKernelDescription (s);
395 const char *name = GetHostname();
396 if (name == NULL || name[0] == '\0')
397 return GetPluginName();
398 return ConstString (name);
402 Platform::GetHostname ()
409 return m_name.c_str();
413 Platform::GetUserName (uint32_t uid)
415 const char *user_name = GetCachedUserName(uid);
421 if (Host::GetUserName(uid, name))
422 return SetCachedUserName (uid, name.c_str(), name.size());
428 Platform::GetGroupName (uint32_t gid)
430 const char *group_name = GetCachedGroupName(gid);
436 if (Host::GetGroupName(gid, name))
437 return SetCachedGroupName (gid, name.c_str(), name.size());
443 Platform::SetOSVersion (uint32_t major,
449 // We don't need anyone setting the OS version for the host platform,
450 // we should be able to figure it out by calling Host::GetOSVersion(...).
455 // We have a remote platform, allow setting the target OS version if
456 // we aren't connected, since if we are connected, we should be able to
457 // request the remote OS version from the connected platform.
462 // We aren't connected and we might want to set the OS version
463 // ahead of time before we connect so we can peruse files and
464 // use a local SDK or PDK cache of support files to disassemble
465 // or do other things.
466 m_major_os_version = major;
467 m_minor_os_version = minor;
468 m_update_os_version = update;
477 Platform::ResolveExecutable (const FileSpec &exe_file,
478 const ArchSpec &exe_arch,
479 lldb::ModuleSP &exe_module_sp,
480 const FileSpecList *module_search_paths_ptr)
483 if (exe_file.Exists())
485 ModuleSpec module_spec (exe_file, exe_arch);
486 if (module_spec.GetArchitecture().IsValid())
488 error = ModuleList::GetSharedModule (module_spec,
490 module_search_paths_ptr,
496 // No valid architecture was specified, ask the platform for
497 // the architectures that we should be using (in the correct order)
498 // and see if we can find a match that way
499 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
501 error = ModuleList::GetSharedModule (module_spec,
503 module_search_paths_ptr,
506 // Did we find an executable using one of the
507 if (error.Success() && exe_module_sp)
514 error.SetErrorStringWithFormat ("'%s' does not exist",
515 exe_file.GetPath().c_str());
521 Platform::ResolveSymbolFile (Target &target,
522 const ModuleSpec &sym_spec,
526 if (sym_spec.GetSymbolFileSpec().Exists())
527 sym_file = sym_spec.GetSymbolFileSpec();
529 error.SetErrorString("unable to resolve symbol file");
537 Platform::ResolveRemotePath (const FileSpec &platform_path,
538 FileSpec &resolved_platform_path)
540 resolved_platform_path = platform_path;
541 return resolved_platform_path.ResolvePath();
546 Platform::GetSystemArchitecture()
550 if (!m_system_arch.IsValid())
552 // We have a local host platform
553 m_system_arch = Host::GetArchitecture();
554 m_system_arch_set_while_connected = m_system_arch.IsValid();
559 // We have a remote platform. We can only fetch the remote
560 // system architecture if we are connected, and we don't want to do it
563 const bool is_connected = IsConnected();
566 if (m_system_arch.IsValid())
568 // We have valid OS version info, check to make sure it wasn't
569 // manually set prior to connecting. If it was manually set prior
570 // to connecting, then lets fetch the actual OS version info
571 // if we are now connected.
572 if (is_connected && !m_system_arch_set_while_connected)
577 // We don't have valid OS version info, fetch it if we are connected
578 fetch = is_connected;
583 m_system_arch = GetRemoteSystemArchitecture ();
584 m_system_arch_set_while_connected = m_system_arch.IsValid();
587 return m_system_arch;
592 Platform::ConnectRemote (Args& args)
596 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
598 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString());
603 Platform::DisconnectRemote ()
607 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString());
609 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString());
614 Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
616 // Take care of the host case so that each subclass can just
617 // call this function to get the host functionality.
619 return Host::GetProcessInfo (pid, process_info);
624 Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info,
625 ProcessInstanceInfoList &process_infos)
627 // Take care of the host case so that each subclass can just
628 // call this function to get the host functionality.
629 uint32_t match_count = 0;
631 match_count = Host::FindProcesses (match_info, process_infos);
637 Platform::LaunchProcess (ProcessLaunchInfo &launch_info)
640 // Take care of the host case so that each subclass can just
641 // call this function to get the host functionality.
644 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
645 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY);
647 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell))
649 const bool is_localhost = true;
650 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
651 const bool first_arg_is_full_shell_command = false;
652 if (!launch_info.ConvertArgumentsForLaunchingInShell (error,
655 first_arg_is_full_shell_command))
659 error = Host::LaunchProcess (launch_info);
662 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes");
667 Platform::DebugProcess (ProcessLaunchInfo &launch_info,
669 Target *target, // Can be NULL, if NULL create a new target, else use existing one
673 ProcessSP process_sp;
674 // Make sure we stop at the entry point
675 launch_info.GetFlags ().Set (eLaunchFlagDebug);
676 // We always launch the process we are going to debug in a separate process
677 // group, since then we can handle ^C interrupts ourselves w/o having to worry
678 // about the target getting them as well.
679 launch_info.SetLaunchInSeparateProcessGroup(true);
681 error = LaunchProcess (launch_info);
684 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
686 ProcessAttachInfo attach_info (launch_info);
687 process_sp = Attach (attach_info, debugger, target, listener, error);
690 // Since we attached to the process, it will think it needs to detach
691 // if the process object just goes away without an explicit call to
692 // Process::Kill() or Process::Detach(), so let it know to kill the
693 // process if this happens.
694 process_sp->SetShouldDetach (false);
696 // If we didn't have any file actions, the pseudo terminal might
697 // have been used where the slave side was given as the file to
698 // open for stdin/out/err after we have already opened the master
699 // so we can read/write stdin/out/err.
700 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
701 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd)
703 process_sp->SetSTDIOFileDescriptor(pty_fd);
713 Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr)
715 lldb::PlatformSP platform_sp;
718 platform_sp = Platform::Create (arch, platform_arch_ptr, error);
723 //------------------------------------------------------------------
724 /// Lets a platform answer if it is compatible with a given
725 /// architecture and the target triple contained within.
726 //------------------------------------------------------------------
728 Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr)
730 // If the architecture is invalid, we must answer true...
733 ArchSpec platform_arch;
734 // Try for an exact architecture match first.
735 if (exact_arch_match)
737 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
739 if (arch.IsExactMatch(platform_arch))
741 if (compatible_arch_ptr)
742 *compatible_arch_ptr = platform_arch;
749 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx)
751 if (arch.IsCompatibleMatch(platform_arch))
753 if (compatible_arch_ptr)
754 *compatible_arch_ptr = platform_arch;
760 if (compatible_arch_ptr)
761 compatible_arch_ptr->Clear();
768 Platform::SetThreadCreationBreakpoint (lldb_private::Target &target)
770 return lldb::BreakpointSP();
774 Platform::GetEnvironment (StringList &environment)