Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1 | //===-- Platform.cpp --------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Target/Platform.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Error.h" |
| 17 | #include "lldb/Core/PluginManager.h" |
| 18 | #include "lldb/Host/FileSpec.h" |
| 19 | #include "lldb/Target/Target.h" |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | // Use a singleton function for g_local_platform_sp to avoid init |
| 25 | // constructors since LLDB is often part of a shared library |
| 26 | static PlatformSP& |
| 27 | GetDefaultPlatformSP () |
| 28 | { |
| 29 | static PlatformSP g_default_platform_sp; |
| 30 | return g_default_platform_sp; |
| 31 | } |
| 32 | |
| 33 | static PlatformSP& |
| 34 | GetSelectedPlatformSP () |
| 35 | { |
| 36 | static PlatformSP g_selected_platform_sp; |
| 37 | return g_selected_platform_sp; |
| 38 | } |
| 39 | |
| 40 | static Mutex & |
| 41 | GetConnectedPlatformListMutex () |
| 42 | { |
| 43 | static Mutex g_remote_connected_platforms_mutex (Mutex::eMutexTypeRecursive); |
| 44 | return g_remote_connected_platforms_mutex; |
| 45 | } |
| 46 | static std::vector<PlatformSP> & |
| 47 | GetConnectedPlatformList () |
| 48 | { |
| 49 | static std::vector<PlatformSP> g_remote_connected_platforms; |
| 50 | return g_remote_connected_platforms; |
| 51 | } |
| 52 | |
| 53 | //------------------------------------------------------------------ |
| 54 | /// Get the native host platform plug-in. |
| 55 | /// |
| 56 | /// There should only be one of these for each host that LLDB runs |
| 57 | /// upon that should be statically compiled in and registered using |
| 58 | /// preprocessor macros or other similar build mechanisms. |
| 59 | /// |
| 60 | /// This platform will be used as the default platform when launching |
| 61 | /// or attaching to processes unless another platform is specified. |
| 62 | //------------------------------------------------------------------ |
| 63 | PlatformSP |
| 64 | Platform::GetDefaultPlatform () |
| 65 | { |
| 66 | return GetDefaultPlatformSP (); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | Platform::SetDefaultPlatform (const lldb::PlatformSP &platform_sp) |
| 71 | { |
| 72 | // The native platform should use its static void Platform::Initialize() |
| 73 | // function to register itself as the native platform. |
| 74 | GetDefaultPlatformSP () = platform_sp; |
| 75 | } |
| 76 | |
| 77 | PlatformSP |
| 78 | Platform::GetSelectedPlatform () |
| 79 | { |
| 80 | PlatformSP platform_sp (GetSelectedPlatformSP ()); |
| 81 | if (!platform_sp) |
| 82 | platform_sp = GetDefaultPlatform (); |
| 83 | return platform_sp; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | Platform::SetSelectedPlatform (const lldb::PlatformSP &platform_sp) |
| 88 | { |
| 89 | // The native platform should use its static void Platform::Initialize() |
| 90 | // function to register itself as the native platform. |
| 91 | GetSelectedPlatformSP () = platform_sp; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | Error |
| 96 | Platform::GetFile (const FileSpec &platform_file, FileSpec &local_file) |
| 97 | { |
| 98 | // Default to the local case |
| 99 | local_file = platform_file; |
| 100 | return Error(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | PlatformSP |
| 105 | Platform::ConnectRemote (const char *platform_name, const char *remote_connect_url, Error &error) |
| 106 | { |
| 107 | PlatformCreateInstance create_callback = NULL; |
| 108 | lldb::PlatformSP platform_sp; |
| 109 | if (platform_name) |
| 110 | { |
| 111 | create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (platform_name); |
| 112 | if (create_callback) |
| 113 | { |
| 114 | platform_sp.reset(create_callback()); |
| 115 | if (platform_sp) |
| 116 | error = platform_sp->ConnectRemote (remote_connect_url); |
| 117 | else |
| 118 | error.SetErrorStringWithFormat ("unable to create a platform instance of \"%s\"", platform_name); |
| 119 | } |
| 120 | else |
| 121 | error.SetErrorStringWithFormat ("invalid platform name \"%s\"", platform_name); |
| 122 | } |
| 123 | else |
| 124 | error.SetErrorString ("Empty platform name"); |
| 125 | return platform_sp; |
| 126 | } |
| 127 | |
| 128 | uint32_t |
| 129 | Platform::GetNumConnectedRemotePlatforms () |
| 130 | { |
| 131 | Mutex::Locker locker (GetConnectedPlatformListMutex ()); |
| 132 | return GetConnectedPlatformList().size(); |
| 133 | } |
| 134 | |
| 135 | PlatformSP |
| 136 | Platform::GetConnectedRemotePlatformAtIndex (uint32_t idx) |
| 137 | { |
| 138 | PlatformSP platform_sp; |
| 139 | { |
| 140 | Mutex::Locker locker (GetConnectedPlatformListMutex ()); |
| 141 | if (idx < GetConnectedPlatformList().size()) |
| 142 | platform_sp = GetConnectedPlatformList ()[idx]; |
| 143 | } |
| 144 | return platform_sp; |
| 145 | } |
| 146 | |
| 147 | //------------------------------------------------------------------ |
| 148 | /// Default Constructor |
| 149 | //------------------------------------------------------------------ |
| 150 | Platform::Platform () : |
| 151 | m_remote_url () |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | //------------------------------------------------------------------ |
| 156 | /// Destructor. |
| 157 | /// |
| 158 | /// The destructor is virtual since this class is designed to be |
| 159 | /// inherited from by the plug-in instance. |
| 160 | //------------------------------------------------------------------ |
| 161 | Platform::~Platform() |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | Error |
| 166 | Platform::ResolveExecutable (const FileSpec &exe_file, |
| 167 | const ArchSpec &exe_arch, |
| 168 | lldb::ModuleSP &exe_module_sp) |
| 169 | { |
| 170 | Error error; |
| 171 | if (exe_file.Exists()) |
| 172 | { |
| 173 | if (exe_arch.IsValid()) |
| 174 | { |
| 175 | error = ModuleList::GetSharedModule (exe_file, |
| 176 | exe_arch, |
| 177 | NULL, |
| 178 | NULL, |
| 179 | 0, |
| 180 | exe_module_sp, |
| 181 | NULL, |
| 182 | NULL); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | // No valid architecture was specified, ask the platform for |
| 187 | // the architectures that we should be using (in the correct order) |
| 188 | // and see if we can find a match that way |
| 189 | ArchSpec platform_arch; |
| 190 | for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, platform_arch); ++idx) |
| 191 | { |
| 192 | error = ModuleList::GetSharedModule (exe_file, |
| 193 | platform_arch, |
| 194 | NULL, |
| 195 | NULL, |
| 196 | 0, |
| 197 | exe_module_sp, |
| 198 | NULL, |
| 199 | NULL); |
| 200 | // Did we find an executable using one of the |
| 201 | if (error.Success() && exe_module_sp) |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | error.SetErrorStringWithFormat ("'%s%s%s' does not exist", |
| 209 | exe_file.GetDirectory().AsCString(""), |
| 210 | exe_file.GetDirectory() ? "/" : "", |
| 211 | exe_file.GetFilename().AsCString("")); |
| 212 | } |
| 213 | return error; |
| 214 | } |
| 215 | |
| 216 | Error |
| 217 | Platform::ConnectRemote (const char *remote_url) |
| 218 | { |
| 219 | Error error; |
| 220 | error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetShortPluginName()); |
| 221 | return error; |
| 222 | } |
| 223 | |
| 224 | Error |
| 225 | Platform::DisconnectRemote (const lldb::PlatformSP &platform_sp) |
| 226 | { |
| 227 | Error error; |
| 228 | error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetShortPluginName()); |
| 229 | return error; |
| 230 | } |