Greg Clayton | e996fd3 | 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 |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 13 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 14 | // C++ Includes |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 15 | #include <algorithm> |
| 16 | #include <fstream> |
| 17 | #include <vector> |
| 18 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 19 | // Other libraries and framework includes |
| 20 | // Project includes |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 21 | #include "lldb/Breakpoint/BreakpointIDList.h" |
Zachary Turner | af0f45f | 2015-03-03 21:05:17 +0000 | [diff] [blame] | 22 | #include "lldb/Core/DataBufferHeap.h" |
Zachary Turner | 7271bab | 2015-05-13 19:44:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Debugger.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Error.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Log.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 26 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 27 | #include "lldb/Core/ModuleSpec.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 28 | #include "lldb/Core/PluginManager.h" |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 29 | #include "lldb/Core/StructuredData.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 30 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 31 | #include "lldb/Host/FileSystem.h" |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 32 | #include "lldb/Host/Host.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 33 | #include "lldb/Host/HostInfo.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 34 | #include "lldb/Interpreter/OptionValueProperties.h" |
| 35 | #include "lldb/Interpreter/Property.h" |
| 36 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 37 | #include "lldb/Target/Process.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 38 | #include "lldb/Target/Target.h" |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 39 | #include "lldb/Utility/Utils.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 40 | #include "llvm/Support/FileSystem.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 41 | |
| 42 | #include "Utility/ModuleCache.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 43 | |
Robert Flack | 96ad3de | 2015-05-09 15:53:31 +0000 | [diff] [blame] | 44 | // 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 Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 49 | using namespace lldb; |
| 50 | using namespace lldb_private; |
Tamas Berghammer | 3c4f89d | 2015-02-12 18:18:27 +0000 | [diff] [blame] | 51 | |
| 52 | static uint32_t g_initialize_count = 0; |
| 53 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 54 | // Use a singleton function for g_local_platform_sp to avoid init |
| 55 | // constructors since LLDB is often part of a shared library |
| 56 | static PlatformSP& |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 57 | GetHostPlatformSP () |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 58 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 59 | static PlatformSP g_platform_sp; |
| 60 | return g_platform_sp; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 63 | const char * |
| 64 | Platform::GetHostPlatformName () |
| 65 | { |
| 66 | return "host"; |
| 67 | } |
| 68 | |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 69 | namespace { |
| 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 | |
| 88 | ConstString |
| 89 | PlatformProperties::GetSettingName () |
| 90 | { |
| 91 | static ConstString g_setting_name("platform"); |
| 92 | return g_setting_name; |
| 93 | } |
| 94 | |
| 95 | PlatformProperties::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 | |
| 110 | bool |
| 111 | PlatformProperties::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 | |
| 118 | bool |
| 119 | PlatformProperties::SetUseModuleCache (bool use_module_cache) |
| 120 | { |
| 121 | return m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, ePropertyUseModuleCache, use_module_cache); |
| 122 | } |
| 123 | |
| 124 | FileSpec |
| 125 | PlatformProperties::GetModuleCacheDirectory () const |
| 126 | { |
| 127 | return m_collection_sp->GetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory); |
| 128 | } |
| 129 | |
| 130 | bool |
| 131 | PlatformProperties::SetModuleCacheDirectory (const FileSpec& dir_spec) |
| 132 | { |
| 133 | return m_collection_sp->SetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory, dir_spec); |
| 134 | } |
| 135 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 136 | //------------------------------------------------------------------ |
| 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 | //------------------------------------------------------------------ |
| 146 | PlatformSP |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 147 | Platform::GetHostPlatform () |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 148 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 149 | return GetHostPlatformSP (); |
| 150 | } |
| 151 | |
| 152 | static std::vector<PlatformSP> & |
| 153 | GetPlatformList() |
| 154 | { |
| 155 | static std::vector<PlatformSP> g_platform_list; |
| 156 | return g_platform_list; |
| 157 | } |
| 158 | |
| 159 | static Mutex & |
| 160 | GetPlatformListMutex () |
| 161 | { |
| 162 | static Mutex g_mutex(Mutex::eMutexTypeRecursive); |
| 163 | return g_mutex; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void |
Tamas Berghammer | 3c4f89d | 2015-02-12 18:18:27 +0000 | [diff] [blame] | 167 | Platform::Initialize () |
| 168 | { |
| 169 | g_initialize_count++; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | Platform::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 Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 185 | const PlatformPropertiesSP & |
| 186 | Platform::GetGlobalPlatformProperties () |
| 187 | { |
| 188 | static const auto g_settings_sp (std::make_shared<PlatformProperties> ()); |
| 189 | return g_settings_sp; |
| 190 | } |
| 191 | |
Tamas Berghammer | 3c4f89d | 2015-02-12 18:18:27 +0000 | [diff] [blame] | 192 | void |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 193 | Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 194 | { |
| 195 | // The native platform should use its static void Platform::Initialize() |
| 196 | // function to register itself as the native platform. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 197 | GetHostPlatformSP () = platform_sp; |
| 198 | |
| 199 | if (platform_sp) |
| 200 | { |
| 201 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 202 | GetPlatformList().push_back(platform_sp); |
| 203 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 206 | Error |
Steve Pucci | fc99572 | 2014-01-17 18:18:31 +0000 | [diff] [blame] | 207 | Platform::GetFileWithUUID (const FileSpec &platform_file, |
| 208 | const UUID *uuid_ptr, |
| 209 | FileSpec &local_file) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 210 | { |
| 211 | // Default to the local case |
| 212 | local_file = platform_file; |
| 213 | return Error(); |
| 214 | } |
| 215 | |
Greg Clayton | 91c0e74 | 2013-01-11 23:44:27 +0000 | [diff] [blame] | 216 | FileSpecList |
Enrico Granata | fe7295d | 2014-08-16 00:32:58 +0000 | [diff] [blame] | 217 | Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream) |
Enrico Granata | 1759848 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 218 | { |
Greg Clayton | 91c0e74 | 2013-01-11 23:44:27 +0000 | [diff] [blame] | 219 | return FileSpecList(); |
Enrico Granata | 1759848 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 222 | //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 Molenda | 1c62754 | 2013-04-05 01:03:25 +0000 | [diff] [blame] | 252 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 253 | Error |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 254 | Platform::GetSharedModule (const ModuleSpec &module_spec, |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 255 | Process* process, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 256 | ModuleSP &module_sp, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 257 | const FileSpecList *module_search_paths_ptr, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 258 | ModuleSP *old_module_sp_ptr, |
| 259 | bool *did_create_ptr) |
| 260 | { |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 261 | 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 Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 268 | |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 269 | 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 Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | bool |
| 281 | Platform::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 Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 294 | PlatformSP |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 295 | Platform::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 | |
| 313 | PlatformSP |
| 314 | Platform::Create (const ConstString &name, Error &error) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 315 | { |
| 316 | PlatformCreateInstance create_callback = NULL; |
| 317 | lldb::PlatformSP platform_sp; |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 318 | if (name) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 319 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 320 | 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 Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 325 | if (create_callback) |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 326 | platform_sp = create_callback(true, NULL); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 327 | else |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 328 | error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString()); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 329 | } |
| 330 | else |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 331 | error.SetErrorString ("invalid platform name"); |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 332 | |
| 333 | if (platform_sp) |
| 334 | { |
| 335 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 336 | GetPlatformList().push_back(platform_sp); |
| 337 | } |
| 338 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 339 | return platform_sp; |
| 340 | } |
| 341 | |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 342 | |
| 343 | PlatformSP |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 344 | Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 345 | { |
| 346 | lldb::PlatformSP platform_sp; |
| 347 | if (arch.IsValid()) |
| 348 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 349 | // Scope for locker |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 350 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 351 | // First try exact arch matches across all platforms already created |
| 352 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 353 | for (const auto &platform_sp : GetPlatformList()) |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 354 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 355 | 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 Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 363 | return platform_sp; |
| 364 | } |
| 365 | } |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 366 | |
| 367 | PlatformCreateInstance create_callback; |
| 368 | // First try exact arch matches across all platform plug-ins |
| 369 | uint32_t idx; |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 370 | for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) |
| 371 | { |
| 372 | if (create_callback) |
| 373 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 374 | 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 Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 379 | return platform_sp; |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 380 | } |
| 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 Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 395 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | else |
| 399 | error.SetErrorString ("invalid platform name"); |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 400 | if (platform_arch_ptr) |
| 401 | platform_arch_ptr->Clear(); |
| 402 | platform_sp.reset(); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 403 | return platform_sp; |
| 404 | } |
| 405 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 406 | //------------------------------------------------------------------ |
| 407 | /// Default Constructor |
| 408 | //------------------------------------------------------------------ |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 409 | Platform::Platform (bool is_host) : |
| 410 | m_is_host (is_host), |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 411 | m_os_version_set_while_connected (false), |
| 412 | m_system_arch_set_while_connected (false), |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 413 | m_sdk_sysroot (), |
| 414 | m_sdk_build (), |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 415 | m_working_dir (), |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 416 | m_remote_url (), |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 417 | m_name (), |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 418 | m_major_os_version (UINT32_MAX), |
| 419 | m_minor_os_version (UINT32_MAX), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 420 | m_update_os_version (UINT32_MAX), |
| 421 | m_system_arch(), |
Greg Clayton | 7597b35 | 2015-02-02 20:45:17 +0000 | [diff] [blame] | 422 | m_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 423 | m_uid_map(), |
| 424 | m_gid_map(), |
| 425 | m_max_uid_name_len (0), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 426 | 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 Molenda | 6223db27 | 2014-02-13 07:11:08 +0000 | [diff] [blame] | 432 | m_ignores_remote_hostname (false), |
Jason Molenda | 2094dbf | 2014-02-13 23:11:45 +0000 | [diff] [blame] | 433 | m_trap_handlers(), |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 434 | m_calculated_trap_handlers (false), |
| 435 | m_module_cache (llvm::make_unique<ModuleCache> ()) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 436 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 437 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 438 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 439 | log->Printf ("%p Platform::Platform()", static_cast<void*>(this)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 440 | } |
| 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 | //------------------------------------------------------------------ |
| 448 | Platform::~Platform() |
| 449 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 450 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 451 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 452 | log->Printf ("%p Platform::~Platform()", static_cast<void*>(this)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 455 | void |
| 456 | Platform::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 Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 462 | strm.Printf (" Platform: %s\n", GetPluginName().GetCString()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 463 | |
| 464 | ArchSpec arch (GetSystemArchitecture()); |
| 465 | if (arch.IsValid()) |
| 466 | { |
| 467 | if (!arch.GetTriple().str().empty()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 468 | strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (GetOSVersion(major, minor, update)) |
| 472 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 473 | strm.Printf("OS Version: %u", major); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 474 | 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 Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 486 | strm.Printf(" Kernel: %s\n", s.c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 487 | |
| 488 | if (IsHost()) |
| 489 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 490 | strm.Printf(" Hostname: %s\n", GetHostname()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 491 | } |
| 492 | else |
| 493 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 494 | 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 Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 498 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 499 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 500 | if (GetWorkingDirectory()) |
| 501 | { |
| 502 | strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString()); |
| 503 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 504 | 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 Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 513 | |
| 514 | bool |
| 515 | Platform::GetOSVersion (uint32_t &major, |
| 516 | uint32_t &minor, |
| 517 | uint32_t &update) |
| 518 | { |
Greg Clayton | 7597b35 | 2015-02-02 20:45:17 +0000 | [diff] [blame] | 519 | Mutex::Locker locker (m_mutex); |
| 520 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 521 | bool success = m_major_os_version != UINT32_MAX; |
| 522 | if (IsHost()) |
| 523 | { |
| 524 | if (!success) |
| 525 | { |
| 526 | // We have a local host platform |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 527 | success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 528 | 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 Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 539 | bool fetch = false; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 540 | 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 Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 547 | fetch = true; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 548 | } |
| 549 | else |
| 550 | { |
| 551 | // We don't have valid OS version info, fetch it if we are connected |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 552 | fetch = is_connected; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 555 | if (fetch) |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 556 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 557 | success = GetRemoteOSVersion (); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 558 | 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 Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 570 | |
| 571 | bool |
| 572 | Platform::GetOSBuildString (std::string &s) |
| 573 | { |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 574 | s.clear(); |
| 575 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 576 | if (IsHost()) |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 577 | #if !defined(__linux__) |
| 578 | return HostInfo::GetOSBuildString(s); |
| 579 | #else |
| 580 | return false; |
| 581 | #endif |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 582 | else |
| 583 | return GetRemoteOSBuildString (s); |
| 584 | } |
| 585 | |
| 586 | bool |
| 587 | Platform::GetOSKernelDescription (std::string &s) |
| 588 | { |
| 589 | if (IsHost()) |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 590 | #if !defined(__linux__) |
| 591 | return HostInfo::GetOSKernelDescription(s); |
| 592 | #else |
| 593 | return false; |
| 594 | #endif |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 595 | else |
| 596 | return GetRemoteOSKernelDescription (s); |
| 597 | } |
| 598 | |
Sean Callanan | 5dc2981 | 2014-12-05 01:16:31 +0000 | [diff] [blame] | 599 | void |
Greg Clayton | cd6bbba | 2015-01-22 18:25:49 +0000 | [diff] [blame] | 600 | Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options) |
Sean Callanan | 5dc2981 | 2014-12-05 01:16:31 +0000 | [diff] [blame] | 601 | { |
| 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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 613 | FileSpec |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 614 | Platform::GetWorkingDirectory () |
| 615 | { |
| 616 | if (IsHost()) |
| 617 | { |
| 618 | char cwd[PATH_MAX]; |
| 619 | if (getcwd(cwd, sizeof(cwd))) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 620 | return FileSpec{cwd, true}; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 621 | else |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 622 | return FileSpec{}; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 623 | } |
| 624 | else |
| 625 | { |
| 626 | if (!m_working_dir) |
| 627 | m_working_dir = GetRemoteWorkingDirectory(); |
| 628 | return m_working_dir; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | |
| 633 | struct RecurseCopyBaton |
| 634 | { |
| 635 | const FileSpec& dst; |
| 636 | Platform *platform_ptr; |
| 637 | Error error; |
| 638 | }; |
| 639 | |
| 640 | |
| 641 | static FileSpec::EnumerateDirectoryResult |
| 642 | RecurseCopy_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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 661 | Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 662 | if (error.Fail()) |
| 663 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 664 | rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", |
| 665 | dst_dir.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 666 | 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 Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 693 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 694 | FileSpec src_resolved; |
| 695 | |
| 696 | rc_baton->error = FileSystem::Readlink(src, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 697 | |
| 698 | if (rc_baton->error.Fail()) |
| 699 | return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 700 | |
| 701 | rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 702 | |
| 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 Majnemer | 8faf937 | 2014-09-16 06:34:29 +0000 | [diff] [blame] | 732 | llvm_unreachable("Unhandled FileSpec::FileType!"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | Error |
| 736 | Platform::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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 748 | FileSpec working_dir = GetWorkingDirectory(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 749 | |
| 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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 768 | relative_spec = working_dir; |
| 769 | relative_spec.AppendPathComponent(dst.GetPath()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 770 | 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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 783 | fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 784 | } |
| 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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 796 | fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 797 | } |
| 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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 819 | Unlink(fixed_dst); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 820 | uint32_t permissions = src.GetPermissions(); |
| 821 | if (permissions == 0) |
| 822 | permissions = eFilePermissionsDirectoryDefault; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 823 | error = MakeDirectory(fixed_dst, permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 824 | 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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 829 | recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 830 | 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 Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 840 | Unlink(fixed_dst); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 841 | error = PutFile(src, fixed_dst); |
| 842 | break; |
| 843 | |
| 844 | case FileSpec::eFileTypeSymbolicLink: |
| 845 | { |
| 846 | if (GetFileExists (fixed_dst)) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 847 | Unlink(fixed_dst); |
| 848 | FileSpec src_resolved; |
| 849 | error = FileSystem::Readlink(src, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 850 | if (error.Success()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 851 | error = CreateSymlink(dst, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 852 | } |
| 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 | |
| 870 | bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 871 | Platform::SetWorkingDirectory(const FileSpec &file_spec) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 872 | { |
| 873 | if (IsHost()) |
| 874 | { |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 875 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
| 876 | if (log) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 877 | log->Printf("Platform::SetWorkingDirectory('%s')", |
| 878 | file_spec.GetCString()); |
| 879 | if (file_spec) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 880 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 881 | if (::chdir(file_spec.GetCString()) == 0) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 882 | return true; |
| 883 | } |
| 884 | return false; |
| 885 | } |
| 886 | else |
| 887 | { |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 888 | m_working_dir.Clear(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 889 | return SetRemoteWorkingDirectory(file_spec); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | |
| 893 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 894 | Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 895 | { |
| 896 | if (IsHost()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 897 | return FileSystem::MakeDirectory(file_spec, permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 898 | 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 | |
| 906 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 907 | Platform::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 908 | { |
| 909 | if (IsHost()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 910 | return FileSystem::GetFilePermissions(file_spec, file_permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 911 | 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 | |
| 919 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 920 | Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 921 | { |
| 922 | if (IsHost()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 923 | return FileSystem::SetFilePermissions(file_spec, file_permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 924 | 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 | |
| 932 | ConstString |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 933 | Platform::GetName () |
| 934 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 935 | return GetPluginName(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | const char * |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 939 | Platform::GetHostname () |
| 940 | { |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 941 | if (IsHost()) |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 942 | return "127.0.0.1"; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 943 | |
| 944 | if (m_name.empty()) |
| 945 | return NULL; |
| 946 | return m_name.c_str(); |
| 947 | } |
| 948 | |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 949 | bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 950 | Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 951 | { |
| 952 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
| 953 | if (log) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 954 | log->Printf("Platform::SetRemoteWorkingDirectory('%s')", |
| 955 | working_dir.GetCString()); |
| 956 | m_working_dir = working_dir; |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 957 | return true; |
| 958 | } |
| 959 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 960 | const char * |
| 961 | Platform::GetUserName (uint32_t uid) |
| 962 | { |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 963 | #if !defined(LLDB_DISABLE_POSIX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 964 | const char *user_name = GetCachedUserName(uid); |
| 965 | if (user_name) |
| 966 | return user_name; |
| 967 | if (IsHost()) |
| 968 | { |
| 969 | std::string name; |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 970 | if (HostInfo::LookupUserName(uid, name)) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 971 | return SetCachedUserName (uid, name.c_str(), name.size()); |
| 972 | } |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 973 | #endif |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 974 | return NULL; |
| 975 | } |
| 976 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 977 | const char * |
| 978 | Platform::GetGroupName (uint32_t gid) |
| 979 | { |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 980 | #if !defined(LLDB_DISABLE_POSIX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 981 | const char *group_name = GetCachedGroupName(gid); |
| 982 | if (group_name) |
| 983 | return group_name; |
| 984 | if (IsHost()) |
| 985 | { |
| 986 | std::string name; |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 987 | if (HostInfo::LookupGroupName(gid, name)) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 988 | return SetCachedGroupName (gid, name.c_str(), name.size()); |
| 989 | } |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 990 | #endif |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 991 | return NULL; |
| 992 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 993 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 994 | bool |
| 995 | Platform::SetOSVersion (uint32_t major, |
| 996 | uint32_t minor, |
| 997 | uint32_t update) |
| 998 | { |
| 999 | if (IsHost()) |
| 1000 | { |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 1001 | // 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 Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1003 | 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 Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1028 | Error |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1029 | Platform::ResolveExecutable (const ModuleSpec &module_spec, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 1030 | lldb::ModuleSP &exe_module_sp, |
| 1031 | const FileSpecList *module_search_paths_ptr) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1032 | { |
| 1033 | Error error; |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1034 | if (module_spec.GetFileSpec().Exists()) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1035 | { |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 1036 | if (module_spec.GetArchitecture().IsValid()) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1037 | { |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 1038 | error = ModuleList::GetSharedModule (module_spec, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1039 | exe_module_sp, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 1040 | module_search_paths_ptr, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1041 | 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 Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1049 | ModuleSpec arch_module_spec(module_spec); |
| 1050 | for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1051 | { |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1052 | error = ModuleList::GetSharedModule (arch_module_spec, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1053 | exe_module_sp, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 1054 | module_search_paths_ptr, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1055 | 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 Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 1065 | error.SetErrorStringWithFormat ("'%s' does not exist", |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1066 | module_spec.GetFileSpec().GetPath().c_str()); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1067 | } |
| 1068 | return error; |
| 1069 | } |
| 1070 | |
Greg Clayton | 103f028 | 2012-09-12 02:03:59 +0000 | [diff] [blame] | 1071 | Error |
| 1072 | Platform::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 Clayton | aa51684 | 2011-08-11 16:25:18 +0000 | [diff] [blame] | 1087 | bool |
| 1088 | Platform::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 Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1095 | |
| 1096 | const ArchSpec & |
| 1097 | Platform::GetSystemArchitecture() |
| 1098 | { |
| 1099 | if (IsHost()) |
| 1100 | { |
| 1101 | if (!m_system_arch.IsValid()) |
| 1102 | { |
| 1103 | // We have a local host platform |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 1104 | m_system_arch = HostInfo::GetArchitecture(); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1105 | 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 Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1134 | m_system_arch = GetRemoteSystemArchitecture (); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1135 | m_system_arch_set_while_connected = m_system_arch.IsValid(); |
| 1136 | } |
| 1137 | } |
| 1138 | return m_system_arch; |
| 1139 | } |
| 1140 | |
| 1141 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1142 | Error |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1143 | Platform::ConnectRemote (Args& args) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1144 | { |
| 1145 | Error error; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1146 | if (IsHost()) |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1147 | error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1148 | else |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1149 | error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString()); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1150 | return error; |
| 1151 | } |
| 1152 | |
| 1153 | Error |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1154 | Platform::DisconnectRemote () |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1155 | { |
| 1156 | Error error; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1157 | if (IsHost()) |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1158 | error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1159 | else |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1160 | error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString()); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1161 | return error; |
| 1162 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1163 | |
| 1164 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1165 | Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1166 | { |
| 1167 | // Take care of the host case so that each subclass can just |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1168 | // call this function to get the host functionality. |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1169 | if (IsHost()) |
| 1170 | return Host::GetProcessInfo (pid, process_info); |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | uint32_t |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1175 | Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info, |
| 1176 | ProcessInstanceInfoList &process_infos) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1177 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1178 | // Take care of the host case so that each subclass can just |
| 1179 | // call this function to get the host functionality. |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1180 | uint32_t match_count = 0; |
| 1181 | if (IsHost()) |
| 1182 | match_count = Host::FindProcesses (match_info, process_infos); |
| 1183 | return match_count; |
| 1184 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1185 | |
| 1186 | |
| 1187 | Error |
| 1188 | Platform::LaunchProcess (ProcessLaunchInfo &launch_info) |
| 1189 | { |
| 1190 | Error error; |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1191 | 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 Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1196 | // call this function to get the host functionality. |
| 1197 | if (IsHost()) |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1198 | { |
| 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 Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1205 | const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); |
| 1206 | const bool first_arg_is_full_shell_command = false; |
Jim Ingham | d399079 | 2013-09-11 18:23:22 +0000 | [diff] [blame] | 1207 | uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info); |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1208 | if (log) |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 1209 | { |
| 1210 | const FileSpec &shell = launch_info.GetShell(); |
| 1211 | const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>"; |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1212 | log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'", |
| 1213 | __FUNCTION__, |
| 1214 | num_resumes, |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 1215 | shell_str); |
| 1216 | } |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1217 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1218 | if (!launch_info.ConvertArgumentsForLaunchingInShell (error, |
| 1219 | is_localhost, |
| 1220 | will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 1221 | first_arg_is_full_shell_command, |
| 1222 | num_resumes)) |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1223 | return error; |
| 1224 | } |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1225 | else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 1226 | { |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1227 | error = ShellExpandArguments(launch_info); |
Enrico Granata | 83a1437 | 2015-02-20 21:48:38 +0000 | [diff] [blame] | 1228 | if (error.Fail()) |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 1229 | return error; |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 1230 | } |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1231 | |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1232 | if (log) |
| 1233 | log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ()); |
| 1234 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1235 | error = Host::LaunchProcess (launch_info); |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1236 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1237 | else |
| 1238 | error.SetErrorString ("base lldb_private::Platform class can't launch remote processes"); |
| 1239 | return error; |
| 1240 | } |
| 1241 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1242 | Error |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1243 | Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info) |
Enrico Granata | 83a1437 | 2015-02-20 21:48:38 +0000 | [diff] [blame] | 1244 | { |
| 1245 | if (IsHost()) |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1246 | return Host::ShellExpandArguments(launch_info); |
| 1247 | return Error("base lldb_private::Platform class can't expand arguments"); |
Enrico Granata | 83a1437 | 2015-02-20 21:48:38 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | Error |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1251 | Platform::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 Turner | 7271bab | 2015-05-13 19:44:24 +0000 | [diff] [blame] | 1257 | // 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 Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1271 | |
Zachary Turner | 7271bab | 2015-05-13 19:44:24 +0000 | [diff] [blame] | 1272 | 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 Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1278 | return Error(); |
| 1279 | } |
| 1280 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1281 | lldb::ProcessSP |
| 1282 | Platform::DebugProcess (ProcessLaunchInfo &launch_info, |
| 1283 | Debugger &debugger, |
| 1284 | Target *target, // Can be NULL, if NULL create a new target, else use existing one |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1285 | Error &error) |
| 1286 | { |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1287 | 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 Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1291 | ProcessSP process_sp; |
| 1292 | // Make sure we stop at the entry point |
| 1293 | launch_info.GetFlags ().Set (eLaunchFlagDebug); |
Jim Ingham | b4451b1 | 2012-06-01 01:22:13 +0000 | [diff] [blame] | 1294 | // 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 Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1299 | error = LaunchProcess (launch_info); |
| 1300 | if (error.Success()) |
| 1301 | { |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1302 | if (log) |
| 1303 | log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ()); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 1304 | if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1305 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 1306 | ProcessAttachInfo attach_info (launch_info); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1307 | process_sp = Attach (attach_info, debugger, target, error); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1308 | if (process_sp) |
| 1309 | { |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1310 | if (log) |
| 1311 | log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ()); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1312 | launch_info.SetHijackListener(attach_info.GetHijackListener()); |
| 1313 | |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1314 | // 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 Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 1319 | |
| 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 Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1329 | } |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1330 | 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 Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1342 | else |
| 1343 | { |
| 1344 | if (log) |
| 1345 | log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ()); |
| 1346 | } |
| 1347 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1348 | return process_sp; |
| 1349 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1350 | |
| 1351 | |
| 1352 | lldb::PlatformSP |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1353 | Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1354 | { |
| 1355 | lldb::PlatformSP platform_sp; |
| 1356 | Error error; |
| 1357 | if (arch.IsValid()) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1358 | platform_sp = Platform::Create (arch, platform_arch_ptr, error); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1359 | 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 | //------------------------------------------------------------------ |
| 1367 | bool |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1368 | Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1369 | { |
| 1370 | // If the architecture is invalid, we must answer true... |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1371 | if (arch.IsValid()) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1372 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1373 | ArchSpec platform_arch; |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1374 | // Try for an exact architecture match first. |
| 1375 | if (exact_arch_match) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1376 | { |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1377 | for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1378 | { |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1379 | 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 Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1397 | } |
| 1398 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1399 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1400 | if (compatible_arch_ptr) |
| 1401 | compatible_arch_ptr->Clear(); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1402 | return false; |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1405 | Error |
| 1406 | Platform::PutFile (const FileSpec& source, |
| 1407 | const FileSpec& destination, |
| 1408 | uint32_t uid, |
| 1409 | uint32_t gid) |
| 1410 | { |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1411 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); |
| 1412 | if (log) |
| 1413 | log->Printf("[PutFile] Using block by block transfer....\n"); |
| 1414 | |
Pavel Labath | 646b064 | 2015-02-17 16:07:52 +0000 | [diff] [blame] | 1415 | uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec; |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1416 | 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 Labath | 646b064 | 2015-02-17 16:07:52 +0000 | [diff] [blame] | 1430 | File::eOpenOptionTruncate | |
| 1431 | File::eOpenOptionCloseOnExec, |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1432 | 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 Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1470 | return error; |
| 1471 | } |
| 1472 | |
| 1473 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 1474 | Platform::GetFile(const FileSpec &source, |
| 1475 | const FileSpec &destination) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1476 | { |
| 1477 | Error error("unimplemented"); |
| 1478 | return error; |
| 1479 | } |
| 1480 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1481 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 1482 | Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src |
| 1483 | const FileSpec &dst) // The symlink points to dst |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1484 | { |
| 1485 | Error error("unimplemented"); |
| 1486 | return error; |
| 1487 | } |
| 1488 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1489 | bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 1490 | Platform::GetFileExists(const lldb_private::FileSpec &file_spec) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1491 | { |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1495 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 1496 | Platform::Unlink(const FileSpec &path) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1497 | { |
| 1498 | Error error("unimplemented"); |
| 1499 | return error; |
| 1500 | } |
| 1501 | |
Robert Flack | 96ad3de | 2015-05-09 15:53:31 +0000 | [diff] [blame] | 1502 | uint64_t |
| 1503 | Platform::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 Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1512 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1513 | lldb_private::Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame^] | 1514 | Platform::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 Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1520 | { |
| 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 | |
| 1528 | bool |
| 1529 | Platform::CalculateMD5 (const FileSpec& file_spec, |
| 1530 | uint64_t &low, |
| 1531 | uint64_t &high) |
| 1532 | { |
| 1533 | if (IsHost()) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1534 | return FileSystem::CalculateMD5(file_spec, low, high); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1535 | else |
| 1536 | return false; |
| 1537 | } |
| 1538 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1539 | Error |
| 1540 | Platform::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 | |
| 1550 | Error |
| 1551 | Platform::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 Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1560 | void |
| 1561 | Platform::SetLocalCacheDirectory (const char* local) |
| 1562 | { |
| 1563 | m_local_cache_directory.assign(local); |
| 1564 | } |
| 1565 | |
| 1566 | const char* |
| 1567 | Platform::GetLocalCacheDirectory () |
| 1568 | { |
| 1569 | return m_local_cache_directory.c_str(); |
| 1570 | } |
| 1571 | |
| 1572 | static OptionDefinition |
| 1573 | g_rsync_option_table[] = |
| 1574 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1575 | { 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 Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1579 | }; |
| 1580 | |
| 1581 | static OptionDefinition |
| 1582 | g_ssh_option_table[] = |
| 1583 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1584 | { 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 Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1586 | }; |
| 1587 | |
| 1588 | static OptionDefinition |
| 1589 | g_caching_option_table[] = |
| 1590 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1591 | { 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 Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1592 | }; |
| 1593 | |
| 1594 | OptionGroupPlatformRSync::OptionGroupPlatformRSync () |
| 1595 | { |
| 1596 | } |
| 1597 | |
| 1598 | OptionGroupPlatformRSync::~OptionGroupPlatformRSync () |
| 1599 | { |
| 1600 | } |
| 1601 | |
| 1602 | const lldb_private::OptionDefinition* |
| 1603 | OptionGroupPlatformRSync::GetDefinitions () |
| 1604 | { |
| 1605 | return g_rsync_option_table; |
| 1606 | } |
| 1607 | |
| 1608 | void |
| 1609 | OptionGroupPlatformRSync::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 | |
| 1617 | lldb_private::Error |
| 1618 | OptionGroupPlatformRSync::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 | |
| 1650 | uint32_t |
| 1651 | OptionGroupPlatformRSync::GetNumDefinitions () |
| 1652 | { |
| 1653 | return llvm::array_lengthof(g_rsync_option_table); |
| 1654 | } |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1655 | |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 1656 | lldb::BreakpointSP |
| 1657 | Platform::SetThreadCreationBreakpoint (lldb_private::Target &target) |
| 1658 | { |
| 1659 | return lldb::BreakpointSP(); |
| 1660 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1661 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1662 | OptionGroupPlatformSSH::OptionGroupPlatformSSH () |
| 1663 | { |
| 1664 | } |
| 1665 | |
| 1666 | OptionGroupPlatformSSH::~OptionGroupPlatformSSH () |
| 1667 | { |
| 1668 | } |
| 1669 | |
| 1670 | const lldb_private::OptionDefinition* |
| 1671 | OptionGroupPlatformSSH::GetDefinitions () |
| 1672 | { |
| 1673 | return g_ssh_option_table; |
| 1674 | } |
| 1675 | |
| 1676 | void |
| 1677 | OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter) |
| 1678 | { |
| 1679 | m_ssh = false; |
| 1680 | m_ssh_opts.clear(); |
| 1681 | } |
| 1682 | |
| 1683 | lldb_private::Error |
| 1684 | OptionGroupPlatformSSH::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 | |
| 1708 | uint32_t |
| 1709 | OptionGroupPlatformSSH::GetNumDefinitions () |
| 1710 | { |
| 1711 | return llvm::array_lengthof(g_ssh_option_table); |
| 1712 | } |
| 1713 | |
| 1714 | OptionGroupPlatformCaching::OptionGroupPlatformCaching () |
| 1715 | { |
| 1716 | } |
| 1717 | |
| 1718 | OptionGroupPlatformCaching::~OptionGroupPlatformCaching () |
| 1719 | { |
| 1720 | } |
| 1721 | |
| 1722 | const lldb_private::OptionDefinition* |
| 1723 | OptionGroupPlatformCaching::GetDefinitions () |
| 1724 | { |
| 1725 | return g_caching_option_table; |
| 1726 | } |
| 1727 | |
| 1728 | void |
| 1729 | OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter) |
| 1730 | { |
| 1731 | m_cache_dir.clear(); |
| 1732 | } |
| 1733 | |
| 1734 | lldb_private::Error |
| 1735 | OptionGroupPlatformCaching::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 | |
| 1755 | uint32_t |
| 1756 | OptionGroupPlatformCaching::GetNumDefinitions () |
| 1757 | { |
| 1758 | return llvm::array_lengthof(g_caching_option_table); |
| 1759 | } |
| 1760 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1761 | size_t |
| 1762 | Platform::GetEnvironment (StringList &environment) |
| 1763 | { |
| 1764 | environment.Clear(); |
| 1765 | return false; |
| 1766 | } |
Jason Molenda | 2094dbf | 2014-02-13 23:11:45 +0000 | [diff] [blame] | 1767 | |
| 1768 | const std::vector<ConstString> & |
| 1769 | Platform::GetTrapHandlerSymbolNames () |
| 1770 | { |
| 1771 | if (!m_calculated_trap_handlers) |
| 1772 | { |
Greg Clayton | 7597b35 | 2015-02-02 20:45:17 +0000 | [diff] [blame] | 1773 | Mutex::Locker locker (m_mutex); |
Jason Molenda | 4da8706 | 2014-05-23 23:11:27 +0000 | [diff] [blame] | 1774 | if (!m_calculated_trap_handlers) |
| 1775 | { |
| 1776 | CalculateTrapHandlerSymbolNames(); |
| 1777 | m_calculated_trap_handlers = true; |
| 1778 | } |
Jason Molenda | 2094dbf | 2014-02-13 23:11:45 +0000 | [diff] [blame] | 1779 | } |
| 1780 | return m_trap_handlers; |
| 1781 | } |
| 1782 | |
Oleksiy Vyalov | 6474721 | 2015-03-13 18:44:56 +0000 | [diff] [blame] | 1783 | Error |
| 1784 | Platform::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 | |
| 1803 | Error |
| 1804 | Platform::LoadCachedExecutable (const ModuleSpec &module_spec, |
| 1805 | lldb::ModuleSP &module_sp, |
| 1806 | const FileSpecList *module_search_paths_ptr, |
| 1807 | Platform &remote_platform) |
| 1808 | { |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1809 | 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 Vyalov | 6474721 | 2015-03-13 18:44:56 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1820 | Error |
| 1821 | Platform::GetRemoteSharedModule (const ModuleSpec &module_spec, |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1822 | Process* process, |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1823 | lldb::ModuleSP &module_sp, |
| 1824 | const ModuleResolver &module_resolver, |
| 1825 | bool *did_create_ptr) |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1826 | { |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1827 | // Get module information from a target. |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1828 | ModuleSpec resolved_module_spec; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1829 | bool got_module_spec = false; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1830 | 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 Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1834 | got_module_spec = true; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1835 | } |
| 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 Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1841 | return module_resolver (module_spec); |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1842 | } |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1843 | |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1844 | // 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 | |
| 1855 | bool |
| 1856 | Platform::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 Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1866 | // Check local cache for a module. |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1867 | auto error = m_module_cache->GetAndPut ( |
| 1868 | GetModuleCacheRoot (), |
| 1869 | GetCacheHostname (), |
| 1870 | module_spec, |
Oleksiy Vyalov | 919ef9d | 2015-05-07 15:28:49 +0000 | [diff] [blame] | 1871 | [=](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec) |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1872 | { |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1873 | 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 Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1881 | if (error.Success ()) |
| 1882 | return true; |
| 1883 | |
| 1884 | if (log) |
| 1885 | log->Printf("Platform::%s - module %s not found in local cache: %s", |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1886 | __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ()); |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1887 | return false; |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | Error |
| 1891 | Platform::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 | |
| 1941 | FileSpec |
| 1942 | Platform::GetModuleCacheRoot () |
| 1943 | { |
| 1944 | auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory (); |
| 1945 | dir_spec.AppendPathComponent (GetName ().AsCString ()); |
| 1946 | return dir_spec; |
| 1947 | } |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 1948 | |
| 1949 | const char * |
| 1950 | Platform::GetCacheHostname () |
| 1951 | { |
| 1952 | return GetHostname (); |
| 1953 | } |