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" |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Log.h" |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 18 | #include "lldb/Core/PluginManager.h" |
| 19 | #include "lldb/Host/FileSpec.h" |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 20 | #include "lldb/Host/Host.h" |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Process.h" |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
| 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | // Use a singleton function for g_local_platform_sp to avoid init |
| 28 | // constructors since LLDB is often part of a shared library |
| 29 | static PlatformSP& |
| 30 | GetDefaultPlatformSP () |
| 31 | { |
| 32 | static PlatformSP g_default_platform_sp; |
| 33 | return g_default_platform_sp; |
| 34 | } |
| 35 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 36 | static Mutex & |
| 37 | GetConnectedPlatformListMutex () |
| 38 | { |
| 39 | static Mutex g_remote_connected_platforms_mutex (Mutex::eMutexTypeRecursive); |
| 40 | return g_remote_connected_platforms_mutex; |
| 41 | } |
| 42 | static std::vector<PlatformSP> & |
| 43 | GetConnectedPlatformList () |
| 44 | { |
| 45 | static std::vector<PlatformSP> g_remote_connected_platforms; |
| 46 | return g_remote_connected_platforms; |
| 47 | } |
| 48 | |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 49 | |
| 50 | const char * |
| 51 | Platform::GetHostPlatformName () |
| 52 | { |
| 53 | return "host"; |
| 54 | } |
| 55 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 56 | //------------------------------------------------------------------ |
| 57 | /// Get the native host platform plug-in. |
| 58 | /// |
| 59 | /// There should only be one of these for each host that LLDB runs |
| 60 | /// upon that should be statically compiled in and registered using |
| 61 | /// preprocessor macros or other similar build mechanisms. |
| 62 | /// |
| 63 | /// This platform will be used as the default platform when launching |
| 64 | /// or attaching to processes unless another platform is specified. |
| 65 | //------------------------------------------------------------------ |
| 66 | PlatformSP |
| 67 | Platform::GetDefaultPlatform () |
| 68 | { |
| 69 | return GetDefaultPlatformSP (); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | Platform::SetDefaultPlatform (const lldb::PlatformSP &platform_sp) |
| 74 | { |
| 75 | // The native platform should use its static void Platform::Initialize() |
| 76 | // function to register itself as the native platform. |
| 77 | GetDefaultPlatformSP () = platform_sp; |
| 78 | } |
| 79 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 80 | Error |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 81 | Platform::GetFile (const FileSpec &platform_file, |
| 82 | const UUID *uuid_ptr, |
| 83 | FileSpec &local_file) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 84 | { |
| 85 | // Default to the local case |
| 86 | local_file = platform_file; |
| 87 | return Error(); |
| 88 | } |
| 89 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 90 | Error |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 91 | Platform::GetSharedModule (const ModuleSpec &module_spec, |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 92 | ModuleSP &module_sp, |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 93 | const FileSpecList *module_search_paths_ptr, |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 94 | ModuleSP *old_module_sp_ptr, |
| 95 | bool *did_create_ptr) |
| 96 | { |
| 97 | // Don't do any path remapping for the default implementation |
| 98 | // of the platform GetSharedModule function, just call through |
| 99 | // to our static ModuleList function. Platform subclasses that |
| 100 | // implement remote debugging, might have a developer kits |
| 101 | // installed that have cached versions of the files for the |
| 102 | // remote target, or might implement a download and cache |
| 103 | // locally implementation. |
| 104 | const bool always_create = false; |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 105 | return ModuleList::GetSharedModule (module_spec, |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 106 | module_sp, |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 107 | module_search_paths_ptr, |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 108 | old_module_sp_ptr, |
| 109 | did_create_ptr, |
| 110 | always_create); |
| 111 | } |
| 112 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 113 | PlatformSP |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 114 | Platform::Create (const char *platform_name, Error &error) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 115 | { |
| 116 | PlatformCreateInstance create_callback = NULL; |
| 117 | lldb::PlatformSP platform_sp; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 118 | if (platform_name && platform_name[0]) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 119 | { |
| 120 | create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (platform_name); |
| 121 | if (create_callback) |
Greg Clayton | b1db658 | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 122 | platform_sp.reset(create_callback(true, NULL)); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 123 | else |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 124 | error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", platform_name); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 125 | } |
| 126 | else |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 127 | error.SetErrorString ("invalid platform name"); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 128 | return platform_sp; |
| 129 | } |
| 130 | |
Greg Clayton | b1db658 | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 131 | |
| 132 | PlatformSP |
| 133 | Platform::Create (const ArchSpec &arch, Error &error) |
| 134 | { |
| 135 | lldb::PlatformSP platform_sp; |
| 136 | if (arch.IsValid()) |
| 137 | { |
| 138 | PlatformCreateInstance create_callback; |
| 139 | for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) |
| 140 | { |
| 141 | if (create_callback) |
| 142 | platform_sp.reset(create_callback(false, &arch)); |
| 143 | if (platform_sp && platform_sp->IsCompatibleWithArchitecture(arch)) |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | error.SetErrorString ("invalid platform name"); |
| 149 | return platform_sp; |
| 150 | } |
| 151 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 152 | uint32_t |
| 153 | Platform::GetNumConnectedRemotePlatforms () |
| 154 | { |
| 155 | Mutex::Locker locker (GetConnectedPlatformListMutex ()); |
| 156 | return GetConnectedPlatformList().size(); |
| 157 | } |
| 158 | |
| 159 | PlatformSP |
| 160 | Platform::GetConnectedRemotePlatformAtIndex (uint32_t idx) |
| 161 | { |
| 162 | PlatformSP platform_sp; |
| 163 | { |
| 164 | Mutex::Locker locker (GetConnectedPlatformListMutex ()); |
| 165 | if (idx < GetConnectedPlatformList().size()) |
| 166 | platform_sp = GetConnectedPlatformList ()[idx]; |
| 167 | } |
| 168 | return platform_sp; |
| 169 | } |
| 170 | |
| 171 | //------------------------------------------------------------------ |
| 172 | /// Default Constructor |
| 173 | //------------------------------------------------------------------ |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 174 | Platform::Platform (bool is_host) : |
| 175 | m_is_host (is_host), |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 176 | m_os_version_set_while_connected (false), |
| 177 | m_system_arch_set_while_connected (false), |
Greg Clayton | 604f0d3 | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 178 | m_sdk_sysroot (), |
| 179 | m_sdk_build (), |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 180 | m_remote_url (), |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 181 | m_name (), |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 182 | m_major_os_version (UINT32_MAX), |
| 183 | m_minor_os_version (UINT32_MAX), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 184 | m_update_os_version (UINT32_MAX), |
| 185 | m_system_arch(), |
| 186 | m_uid_map_mutex (Mutex::eMutexTypeNormal), |
| 187 | m_gid_map_mutex (Mutex::eMutexTypeNormal), |
| 188 | m_uid_map(), |
| 189 | m_gid_map(), |
| 190 | m_max_uid_name_len (0), |
| 191 | m_max_gid_name_len (0) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 192 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 193 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
| 194 | if (log) |
| 195 | log->Printf ("%p Platform::Platform()", this); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | //------------------------------------------------------------------ |
| 199 | /// Destructor. |
| 200 | /// |
| 201 | /// The destructor is virtual since this class is designed to be |
| 202 | /// inherited from by the plug-in instance. |
| 203 | //------------------------------------------------------------------ |
| 204 | Platform::~Platform() |
| 205 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 206 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
| 207 | if (log) |
| 208 | log->Printf ("%p Platform::~Platform()", this); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 211 | void |
| 212 | Platform::GetStatus (Stream &strm) |
| 213 | { |
| 214 | uint32_t major = UINT32_MAX; |
| 215 | uint32_t minor = UINT32_MAX; |
| 216 | uint32_t update = UINT32_MAX; |
| 217 | std::string s; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 218 | strm.Printf (" Platform: %s\n", GetShortPluginName()); |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 219 | |
| 220 | ArchSpec arch (GetSystemArchitecture()); |
| 221 | if (arch.IsValid()) |
| 222 | { |
| 223 | if (!arch.GetTriple().str().empty()) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 224 | strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str()); |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if (GetOSVersion(major, minor, update)) |
| 228 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 229 | strm.Printf("OS Version: %u", major); |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 230 | if (minor != UINT32_MAX) |
| 231 | strm.Printf(".%u", minor); |
| 232 | if (update != UINT32_MAX) |
| 233 | strm.Printf(".%u", update); |
| 234 | |
| 235 | if (GetOSBuildString (s)) |
| 236 | strm.Printf(" (%s)", s.c_str()); |
| 237 | |
| 238 | strm.EOL(); |
| 239 | } |
| 240 | |
| 241 | if (GetOSKernelDescription (s)) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 242 | strm.Printf(" Kernel: %s\n", s.c_str()); |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 243 | |
| 244 | if (IsHost()) |
| 245 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 246 | strm.Printf(" Hostname: %s\n", GetHostname()); |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 250 | const bool is_connected = IsConnected(); |
| 251 | if (is_connected) |
| 252 | strm.Printf(" Hostname: %s\n", GetHostname()); |
| 253 | strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no"); |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 257 | |
| 258 | bool |
| 259 | Platform::GetOSVersion (uint32_t &major, |
| 260 | uint32_t &minor, |
| 261 | uint32_t &update) |
| 262 | { |
| 263 | bool success = m_major_os_version != UINT32_MAX; |
| 264 | if (IsHost()) |
| 265 | { |
| 266 | if (!success) |
| 267 | { |
| 268 | // We have a local host platform |
| 269 | success = Host::GetOSVersion (m_major_os_version, |
| 270 | m_minor_os_version, |
| 271 | m_update_os_version); |
| 272 | m_os_version_set_while_connected = success; |
| 273 | } |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | // We have a remote platform. We can only fetch the remote |
| 278 | // OS version if we are connected, and we don't want to do it |
| 279 | // more than once. |
| 280 | |
| 281 | const bool is_connected = IsConnected(); |
| 282 | |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 283 | bool fetch = false; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 284 | if (success) |
| 285 | { |
| 286 | // We have valid OS version info, check to make sure it wasn't |
| 287 | // manually set prior to connecting. If it was manually set prior |
| 288 | // to connecting, then lets fetch the actual OS version info |
| 289 | // if we are now connected. |
| 290 | if (is_connected && !m_os_version_set_while_connected) |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 291 | fetch = true; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 292 | } |
| 293 | else |
| 294 | { |
| 295 | // We don't have valid OS version info, fetch it if we are connected |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 296 | fetch = is_connected; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 299 | if (fetch) |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 300 | { |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 301 | success = GetRemoteOSVersion (); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 302 | m_os_version_set_while_connected = success; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (success) |
| 307 | { |
| 308 | major = m_major_os_version; |
| 309 | minor = m_minor_os_version; |
| 310 | update = m_update_os_version; |
| 311 | } |
| 312 | return success; |
| 313 | } |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 314 | |
| 315 | bool |
| 316 | Platform::GetOSBuildString (std::string &s) |
| 317 | { |
| 318 | if (IsHost()) |
| 319 | return Host::GetOSBuildString (s); |
| 320 | else |
| 321 | return GetRemoteOSBuildString (s); |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | Platform::GetOSKernelDescription (std::string &s) |
| 326 | { |
| 327 | if (IsHost()) |
| 328 | return Host::GetOSKernelDescription (s); |
| 329 | else |
| 330 | return GetRemoteOSKernelDescription (s); |
| 331 | } |
| 332 | |
| 333 | const char * |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 334 | Platform::GetName () |
| 335 | { |
| 336 | const char *name = GetHostname(); |
| 337 | if (name == NULL || name[0] == '\0') |
| 338 | name = GetShortPluginName(); |
| 339 | return name; |
| 340 | } |
| 341 | |
| 342 | const char * |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 343 | Platform::GetHostname () |
| 344 | { |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 345 | if (IsHost()) |
| 346 | return "localhost"; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 347 | |
| 348 | if (m_name.empty()) |
| 349 | return NULL; |
| 350 | return m_name.c_str(); |
| 351 | } |
| 352 | |
| 353 | const char * |
| 354 | Platform::GetUserName (uint32_t uid) |
| 355 | { |
| 356 | const char *user_name = GetCachedUserName(uid); |
| 357 | if (user_name) |
| 358 | return user_name; |
| 359 | if (IsHost()) |
| 360 | { |
| 361 | std::string name; |
| 362 | if (Host::GetUserName(uid, name)) |
| 363 | return SetCachedUserName (uid, name.c_str(), name.size()); |
| 364 | } |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 365 | return NULL; |
| 366 | } |
| 367 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 368 | const char * |
| 369 | Platform::GetGroupName (uint32_t gid) |
| 370 | { |
| 371 | const char *group_name = GetCachedGroupName(gid); |
| 372 | if (group_name) |
| 373 | return group_name; |
| 374 | if (IsHost()) |
| 375 | { |
| 376 | std::string name; |
| 377 | if (Host::GetGroupName(gid, name)) |
| 378 | return SetCachedGroupName (gid, name.c_str(), name.size()); |
| 379 | } |
| 380 | return NULL; |
| 381 | } |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 382 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 383 | bool |
| 384 | Platform::SetOSVersion (uint32_t major, |
| 385 | uint32_t minor, |
| 386 | uint32_t update) |
| 387 | { |
| 388 | if (IsHost()) |
| 389 | { |
| 390 | // We don't need anyone setting the OS version for the host platform, |
| 391 | // we should be able to figure it out by calling Host::GetOSVersion(...). |
| 392 | return false; |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | // We have a remote platform, allow setting the target OS version if |
| 397 | // we aren't connected, since if we are connected, we should be able to |
| 398 | // request the remote OS version from the connected platform. |
| 399 | if (IsConnected()) |
| 400 | return false; |
| 401 | else |
| 402 | { |
| 403 | // We aren't connected and we might want to set the OS version |
| 404 | // ahead of time before we connect so we can peruse files and |
| 405 | // use a local SDK or PDK cache of support files to disassemble |
| 406 | // or do other things. |
| 407 | m_major_os_version = major; |
| 408 | m_minor_os_version = minor; |
| 409 | m_update_os_version = update; |
| 410 | return true; |
| 411 | } |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 417 | Error |
| 418 | Platform::ResolveExecutable (const FileSpec &exe_file, |
| 419 | const ArchSpec &exe_arch, |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 420 | lldb::ModuleSP &exe_module_sp, |
| 421 | const FileSpecList *module_search_paths_ptr) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 422 | { |
| 423 | Error error; |
| 424 | if (exe_file.Exists()) |
| 425 | { |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 426 | ModuleSpec module_spec (exe_file, exe_arch); |
| 427 | if (module_spec.GetArchitecture().IsValid()) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 428 | { |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 429 | error = ModuleList::GetSharedModule (module_spec, |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 430 | exe_module_sp, |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 431 | module_search_paths_ptr, |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 432 | NULL, |
| 433 | NULL); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | // No valid architecture was specified, ask the platform for |
| 438 | // the architectures that we should be using (in the correct order) |
| 439 | // and see if we can find a match that way |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 440 | for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 441 | { |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 442 | error = ModuleList::GetSharedModule (module_spec, |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 443 | exe_module_sp, |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 444 | module_search_paths_ptr, |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 445 | NULL, |
| 446 | NULL); |
| 447 | // Did we find an executable using one of the |
| 448 | if (error.Success() && exe_module_sp) |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | error.SetErrorStringWithFormat ("'%s%s%s' does not exist", |
| 456 | exe_file.GetDirectory().AsCString(""), |
| 457 | exe_file.GetDirectory() ? "/" : "", |
| 458 | exe_file.GetFilename().AsCString("")); |
| 459 | } |
| 460 | return error; |
| 461 | } |
| 462 | |
Greg Clayton | f2bf870 | 2011-08-11 16:25:18 +0000 | [diff] [blame] | 463 | bool |
| 464 | Platform::ResolveRemotePath (const FileSpec &platform_path, |
| 465 | FileSpec &resolved_platform_path) |
| 466 | { |
| 467 | resolved_platform_path = platform_path; |
| 468 | return resolved_platform_path.ResolvePath(); |
| 469 | } |
| 470 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 471 | |
| 472 | const ArchSpec & |
| 473 | Platform::GetSystemArchitecture() |
| 474 | { |
| 475 | if (IsHost()) |
| 476 | { |
| 477 | if (!m_system_arch.IsValid()) |
| 478 | { |
| 479 | // We have a local host platform |
| 480 | m_system_arch = Host::GetArchitecture(); |
| 481 | m_system_arch_set_while_connected = m_system_arch.IsValid(); |
| 482 | } |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | // We have a remote platform. We can only fetch the remote |
| 487 | // system architecture if we are connected, and we don't want to do it |
| 488 | // more than once. |
| 489 | |
| 490 | const bool is_connected = IsConnected(); |
| 491 | |
| 492 | bool fetch = false; |
| 493 | if (m_system_arch.IsValid()) |
| 494 | { |
| 495 | // We have valid OS version info, check to make sure it wasn't |
| 496 | // manually set prior to connecting. If it was manually set prior |
| 497 | // to connecting, then lets fetch the actual OS version info |
| 498 | // if we are now connected. |
| 499 | if (is_connected && !m_system_arch_set_while_connected) |
| 500 | fetch = true; |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | // We don't have valid OS version info, fetch it if we are connected |
| 505 | fetch = is_connected; |
| 506 | } |
| 507 | |
| 508 | if (fetch) |
| 509 | { |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 510 | m_system_arch = GetRemoteSystemArchitecture (); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 511 | m_system_arch_set_while_connected = m_system_arch.IsValid(); |
| 512 | } |
| 513 | } |
| 514 | return m_system_arch; |
| 515 | } |
| 516 | |
| 517 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 518 | Error |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 519 | Platform::ConnectRemote (Args& args) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 520 | { |
| 521 | Error error; |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 522 | if (IsHost()) |
| 523 | error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetShortPluginName()); |
| 524 | else |
| 525 | error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetShortPluginName()); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 526 | return error; |
| 527 | } |
| 528 | |
| 529 | Error |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 530 | Platform::DisconnectRemote () |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 531 | { |
| 532 | Error error; |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 533 | if (IsHost()) |
| 534 | error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetShortPluginName()); |
| 535 | else |
| 536 | error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetShortPluginName()); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 537 | return error; |
| 538 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 539 | |
| 540 | bool |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 541 | Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 542 | { |
| 543 | // Take care of the host case so that each subclass can just |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 544 | // call this function to get the host functionality. |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 545 | if (IsHost()) |
| 546 | return Host::GetProcessInfo (pid, process_info); |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | uint32_t |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 551 | Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info, |
| 552 | ProcessInstanceInfoList &process_infos) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 553 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 554 | // Take care of the host case so that each subclass can just |
| 555 | // call this function to get the host functionality. |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 556 | uint32_t match_count = 0; |
| 557 | if (IsHost()) |
| 558 | match_count = Host::FindProcesses (match_info, process_infos); |
| 559 | return match_count; |
| 560 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 561 | |
| 562 | |
| 563 | Error |
| 564 | Platform::LaunchProcess (ProcessLaunchInfo &launch_info) |
| 565 | { |
| 566 | Error error; |
| 567 | // Take care of the host case so that each subclass can just |
| 568 | // call this function to get the host functionality. |
| 569 | if (IsHost()) |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 570 | { |
| 571 | if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY")) |
| 572 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 573 | |
| 574 | if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) |
| 575 | { |
| 576 | const bool is_localhost = true; |
Greg Clayton | 9747118 | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 577 | const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); |
| 578 | const bool first_arg_is_full_shell_command = false; |
| 579 | if (!launch_info.ConvertArgumentsForLaunchingInShell (error, |
| 580 | is_localhost, |
| 581 | will_debug, |
| 582 | first_arg_is_full_shell_command)) |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 583 | return error; |
| 584 | } |
| 585 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 586 | error = Host::LaunchProcess (launch_info); |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 587 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 588 | else |
| 589 | error.SetErrorString ("base lldb_private::Platform class can't launch remote processes"); |
| 590 | return error; |
| 591 | } |
| 592 | |
| 593 | lldb::ProcessSP |
| 594 | Platform::DebugProcess (ProcessLaunchInfo &launch_info, |
| 595 | Debugger &debugger, |
| 596 | Target *target, // Can be NULL, if NULL create a new target, else use existing one |
| 597 | Listener &listener, |
| 598 | Error &error) |
| 599 | { |
| 600 | ProcessSP process_sp; |
| 601 | // Make sure we stop at the entry point |
| 602 | launch_info.GetFlags ().Set (eLaunchFlagDebug); |
| 603 | error = LaunchProcess (launch_info); |
| 604 | if (error.Success()) |
| 605 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 606 | if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 607 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 608 | ProcessAttachInfo attach_info (launch_info); |
| 609 | process_sp = Attach (attach_info, debugger, target, listener, error); |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 610 | if (process_sp) |
| 611 | { |
| 612 | // Since we attached to the process, it will think it needs to detach |
| 613 | // if the process object just goes away without an explicit call to |
| 614 | // Process::Kill() or Process::Detach(), so let it know to kill the |
| 615 | // process if this happens. |
| 616 | process_sp->SetShouldDetach (false); |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 617 | |
| 618 | // If we didn't have any file actions, the pseudo terminal might |
| 619 | // have been used where the slave side was given as the file to |
| 620 | // open for stdin/out/err after we have already opened the master |
| 621 | // so we can read/write stdin/out/err. |
| 622 | int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); |
| 623 | if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) |
| 624 | { |
| 625 | process_sp->SetSTDIOFileDescriptor(pty_fd); |
| 626 | } |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 627 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | return process_sp; |
| 631 | } |
Greg Clayton | b1db658 | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 632 | |
| 633 | |
| 634 | lldb::PlatformSP |
| 635 | Platform::GetPlatformForArchitecture (const ArchSpec &arch) |
| 636 | { |
| 637 | lldb::PlatformSP platform_sp; |
| 638 | Error error; |
| 639 | if (arch.IsValid()) |
| 640 | platform_sp = Platform::Create (arch, error); |
| 641 | return platform_sp; |
| 642 | } |
| 643 | |
| 644 | |
| 645 | //------------------------------------------------------------------ |
| 646 | /// Lets a platform answer if it is compatible with a given |
| 647 | /// architecture and the target triple contained within. |
| 648 | //------------------------------------------------------------------ |
| 649 | bool |
| 650 | Platform::IsCompatibleWithArchitecture (const ArchSpec &arch) |
| 651 | { |
| 652 | // If the architecture is invalid, we must answer true... |
| 653 | if (!arch.IsValid()) |
| 654 | return true; |
| 655 | |
| 656 | ArchSpec platform_arch; |
| 657 | for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx) |
| 658 | { |
| 659 | if (arch == platform_arch) |
| 660 | return true; |
| 661 | } |
| 662 | return false; |
| 663 | |
| 664 | } |
| 665 | |
| 666 | |