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" |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 39 | #include "lldb/Target/UnixSignals.h" |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 40 | #include "lldb/Utility/Utils.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 41 | #include "llvm/Support/FileSystem.h" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 42 | |
| 43 | #include "Utility/ModuleCache.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 44 | |
Robert Flack | 96ad3de | 2015-05-09 15:53:31 +0000 | [diff] [blame] | 45 | // Define these constants from POSIX mman.h rather than include the file |
| 46 | // so that they will be correct even when compiled on Linux. |
| 47 | #define MAP_PRIVATE 2 |
| 48 | #define MAP_ANON 0x1000 |
| 49 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 50 | using namespace lldb; |
| 51 | using namespace lldb_private; |
Tamas Berghammer | 3c4f89d | 2015-02-12 18:18:27 +0000 | [diff] [blame] | 52 | |
| 53 | static uint32_t g_initialize_count = 0; |
| 54 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 55 | // Use a singleton function for g_local_platform_sp to avoid init |
| 56 | // constructors since LLDB is often part of a shared library |
| 57 | static PlatformSP& |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 58 | GetHostPlatformSP () |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 59 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 60 | static PlatformSP g_platform_sp; |
| 61 | return g_platform_sp; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 64 | const char * |
| 65 | Platform::GetHostPlatformName () |
| 66 | { |
| 67 | return "host"; |
| 68 | } |
| 69 | |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 70 | namespace { |
| 71 | |
| 72 | PropertyDefinition |
| 73 | g_properties[] = |
| 74 | { |
| 75 | { "use-module-cache" , OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "Use module cache." }, |
| 76 | { "module-cache-directory", OptionValue::eTypeFileSpec, true, 0 , nullptr, nullptr, "Root directory for cached modules." }, |
| 77 | { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr } |
| 78 | }; |
| 79 | |
| 80 | enum |
| 81 | { |
| 82 | ePropertyUseModuleCache, |
| 83 | ePropertyModuleCacheDirectory |
| 84 | }; |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | |
| 89 | ConstString |
| 90 | PlatformProperties::GetSettingName () |
| 91 | { |
| 92 | static ConstString g_setting_name("platform"); |
| 93 | return g_setting_name; |
| 94 | } |
| 95 | |
| 96 | PlatformProperties::PlatformProperties () |
| 97 | { |
| 98 | m_collection_sp.reset (new OptionValueProperties (GetSettingName ())); |
| 99 | m_collection_sp->Initialize (g_properties); |
| 100 | |
| 101 | auto module_cache_dir = GetModuleCacheDirectory (); |
| 102 | if (!module_cache_dir) |
| 103 | { |
| 104 | if (!HostInfo::GetLLDBPath (ePathTypeGlobalLLDBTempSystemDir, module_cache_dir)) |
| 105 | module_cache_dir = FileSpec ("/tmp/lldb", false); |
| 106 | module_cache_dir.AppendPathComponent ("module_cache"); |
| 107 | SetModuleCacheDirectory (module_cache_dir); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | bool |
| 112 | PlatformProperties::GetUseModuleCache () const |
| 113 | { |
| 114 | const auto idx = ePropertyUseModuleCache; |
| 115 | return m_collection_sp->GetPropertyAtIndexAsBoolean ( |
| 116 | nullptr, idx, g_properties[idx].default_uint_value != 0); |
| 117 | } |
| 118 | |
| 119 | bool |
| 120 | PlatformProperties::SetUseModuleCache (bool use_module_cache) |
| 121 | { |
| 122 | return m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, ePropertyUseModuleCache, use_module_cache); |
| 123 | } |
| 124 | |
| 125 | FileSpec |
| 126 | PlatformProperties::GetModuleCacheDirectory () const |
| 127 | { |
| 128 | return m_collection_sp->GetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory); |
| 129 | } |
| 130 | |
| 131 | bool |
| 132 | PlatformProperties::SetModuleCacheDirectory (const FileSpec& dir_spec) |
| 133 | { |
| 134 | return m_collection_sp->SetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory, dir_spec); |
| 135 | } |
| 136 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 137 | //------------------------------------------------------------------ |
| 138 | /// Get the native host platform plug-in. |
| 139 | /// |
| 140 | /// There should only be one of these for each host that LLDB runs |
| 141 | /// upon that should be statically compiled in and registered using |
| 142 | /// preprocessor macros or other similar build mechanisms. |
| 143 | /// |
| 144 | /// This platform will be used as the default platform when launching |
| 145 | /// or attaching to processes unless another platform is specified. |
| 146 | //------------------------------------------------------------------ |
| 147 | PlatformSP |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 148 | Platform::GetHostPlatform () |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 149 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 150 | return GetHostPlatformSP (); |
| 151 | } |
| 152 | |
| 153 | static std::vector<PlatformSP> & |
| 154 | GetPlatformList() |
| 155 | { |
| 156 | static std::vector<PlatformSP> g_platform_list; |
| 157 | return g_platform_list; |
| 158 | } |
| 159 | |
| 160 | static Mutex & |
| 161 | GetPlatformListMutex () |
| 162 | { |
| 163 | static Mutex g_mutex(Mutex::eMutexTypeRecursive); |
| 164 | return g_mutex; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
Tamas Berghammer | 3c4f89d | 2015-02-12 18:18:27 +0000 | [diff] [blame] | 168 | Platform::Initialize () |
| 169 | { |
| 170 | g_initialize_count++; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | Platform::Terminate () |
| 175 | { |
| 176 | if (g_initialize_count > 0) |
| 177 | { |
| 178 | if (--g_initialize_count == 0) |
| 179 | { |
| 180 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 181 | GetPlatformList().clear(); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 186 | const PlatformPropertiesSP & |
| 187 | Platform::GetGlobalPlatformProperties () |
| 188 | { |
| 189 | static const auto g_settings_sp (std::make_shared<PlatformProperties> ()); |
| 190 | return g_settings_sp; |
| 191 | } |
| 192 | |
Tamas Berghammer | 3c4f89d | 2015-02-12 18:18:27 +0000 | [diff] [blame] | 193 | void |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 194 | Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 195 | { |
| 196 | // The native platform should use its static void Platform::Initialize() |
| 197 | // function to register itself as the native platform. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 198 | GetHostPlatformSP () = platform_sp; |
| 199 | |
| 200 | if (platform_sp) |
| 201 | { |
| 202 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 203 | GetPlatformList().push_back(platform_sp); |
| 204 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 207 | Error |
Steve Pucci | fc99572 | 2014-01-17 18:18:31 +0000 | [diff] [blame] | 208 | Platform::GetFileWithUUID (const FileSpec &platform_file, |
| 209 | const UUID *uuid_ptr, |
| 210 | FileSpec &local_file) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 211 | { |
| 212 | // Default to the local case |
| 213 | local_file = platform_file; |
| 214 | return Error(); |
| 215 | } |
| 216 | |
Greg Clayton | 91c0e74 | 2013-01-11 23:44:27 +0000 | [diff] [blame] | 217 | FileSpecList |
Enrico Granata | fe7295d | 2014-08-16 00:32:58 +0000 | [diff] [blame] | 218 | Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream) |
Enrico Granata | 1759848 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 219 | { |
Greg Clayton | 91c0e74 | 2013-01-11 23:44:27 +0000 | [diff] [blame] | 220 | return FileSpecList(); |
Enrico Granata | 1759848 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 223 | //PlatformSP |
| 224 | //Platform::FindPlugin (Process *process, const ConstString &plugin_name) |
| 225 | //{ |
| 226 | // PlatformCreateInstance create_callback = NULL; |
| 227 | // if (plugin_name) |
| 228 | // { |
| 229 | // create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name); |
| 230 | // if (create_callback) |
| 231 | // { |
| 232 | // ArchSpec arch; |
| 233 | // if (process) |
| 234 | // { |
| 235 | // arch = process->GetTarget().GetArchitecture(); |
| 236 | // } |
| 237 | // PlatformSP platform_sp(create_callback(process, &arch)); |
| 238 | // if (platform_sp) |
| 239 | // return platform_sp; |
| 240 | // } |
| 241 | // } |
| 242 | // else |
| 243 | // { |
| 244 | // for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx) |
| 245 | // { |
| 246 | // PlatformSP platform_sp(create_callback(process, nullptr)); |
| 247 | // if (platform_sp) |
| 248 | // return platform_sp; |
| 249 | // } |
| 250 | // } |
| 251 | // return PlatformSP(); |
| 252 | //} |
Jason Molenda | 1c62754 | 2013-04-05 01:03:25 +0000 | [diff] [blame] | 253 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 254 | Error |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 255 | Platform::GetSharedModule (const ModuleSpec &module_spec, |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 256 | Process* process, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 257 | ModuleSP &module_sp, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 258 | const FileSpecList *module_search_paths_ptr, |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 259 | ModuleSP *old_module_sp_ptr, |
| 260 | bool *did_create_ptr) |
| 261 | { |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 262 | if (IsHost ()) |
| 263 | return ModuleList::GetSharedModule (module_spec, |
| 264 | module_sp, |
| 265 | module_search_paths_ptr, |
| 266 | old_module_sp_ptr, |
| 267 | did_create_ptr, |
| 268 | false); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 269 | |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 270 | return GetRemoteSharedModule (module_spec, |
| 271 | process, |
| 272 | module_sp, |
| 273 | [&](const ModuleSpec &spec) |
| 274 | { |
| 275 | return ModuleList::GetSharedModule ( |
| 276 | spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr, false); |
| 277 | }, |
| 278 | did_create_ptr); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool |
| 282 | Platform::GetModuleSpec (const FileSpec& module_file_spec, |
| 283 | const ArchSpec& arch, |
| 284 | ModuleSpec &module_spec) |
| 285 | { |
| 286 | ModuleSpecList module_specs; |
| 287 | if (ObjectFile::GetModuleSpecifications (module_file_spec, 0, 0, module_specs) == 0) |
| 288 | return false; |
| 289 | |
| 290 | ModuleSpec matched_module_spec; |
| 291 | return module_specs.FindMatchingModuleSpec (ModuleSpec (module_file_spec, arch), |
| 292 | module_spec); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 295 | PlatformSP |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 296 | Platform::Find (const ConstString &name) |
| 297 | { |
| 298 | if (name) |
| 299 | { |
| 300 | static ConstString g_host_platform_name ("host"); |
| 301 | if (name == g_host_platform_name) |
| 302 | return GetHostPlatform(); |
| 303 | |
| 304 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 305 | for (const auto &platform_sp : GetPlatformList()) |
| 306 | { |
| 307 | if (platform_sp->GetName() == name) |
| 308 | return platform_sp; |
| 309 | } |
| 310 | } |
| 311 | return PlatformSP(); |
| 312 | } |
| 313 | |
| 314 | PlatformSP |
| 315 | Platform::Create (const ConstString &name, Error &error) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 316 | { |
| 317 | PlatformCreateInstance create_callback = NULL; |
| 318 | lldb::PlatformSP platform_sp; |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 319 | if (name) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 320 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 321 | static ConstString g_host_platform_name ("host"); |
| 322 | if (name == g_host_platform_name) |
| 323 | return GetHostPlatform(); |
| 324 | |
| 325 | create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 326 | if (create_callback) |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 327 | platform_sp = create_callback(true, NULL); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 328 | else |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 329 | 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] | 330 | } |
| 331 | else |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 332 | error.SetErrorString ("invalid platform name"); |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 333 | |
| 334 | if (platform_sp) |
| 335 | { |
| 336 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 337 | GetPlatformList().push_back(platform_sp); |
| 338 | } |
| 339 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 340 | return platform_sp; |
| 341 | } |
| 342 | |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 343 | |
| 344 | PlatformSP |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 345 | Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 346 | { |
| 347 | lldb::PlatformSP platform_sp; |
| 348 | if (arch.IsValid()) |
| 349 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 350 | // Scope for locker |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 351 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 352 | // First try exact arch matches across all platforms already created |
| 353 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 354 | for (const auto &platform_sp : GetPlatformList()) |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 355 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 356 | if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr)) |
| 357 | return platform_sp; |
| 358 | } |
| 359 | |
| 360 | // Next try compatible arch matches across all platforms already created |
| 361 | for (const auto &platform_sp : GetPlatformList()) |
| 362 | { |
| 363 | if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr)) |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 364 | return platform_sp; |
| 365 | } |
| 366 | } |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 367 | |
| 368 | PlatformCreateInstance create_callback; |
| 369 | // First try exact arch matches across all platform plug-ins |
| 370 | uint32_t idx; |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 371 | for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) |
| 372 | { |
| 373 | if (create_callback) |
| 374 | { |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 375 | platform_sp = create_callback(false, &arch); |
| 376 | if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr)) |
| 377 | { |
| 378 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 379 | GetPlatformList().push_back(platform_sp); |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 380 | return platform_sp; |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | // Next try compatible arch matches across all platform plug-ins |
| 385 | for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) |
| 386 | { |
| 387 | if (create_callback) |
| 388 | { |
| 389 | platform_sp = create_callback(false, &arch); |
| 390 | if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr)) |
| 391 | { |
| 392 | Mutex::Locker locker(GetPlatformListMutex ()); |
| 393 | GetPlatformList().push_back(platform_sp); |
| 394 | return platform_sp; |
| 395 | } |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 396 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | else |
| 400 | error.SetErrorString ("invalid platform name"); |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 401 | if (platform_arch_ptr) |
| 402 | platform_arch_ptr->Clear(); |
| 403 | platform_sp.reset(); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 404 | return platform_sp; |
| 405 | } |
| 406 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 407 | //------------------------------------------------------------------ |
| 408 | /// Default Constructor |
| 409 | //------------------------------------------------------------------ |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 410 | Platform::Platform (bool is_host) : |
| 411 | m_is_host (is_host), |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 412 | m_os_version_set_while_connected (false), |
| 413 | m_system_arch_set_while_connected (false), |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 414 | m_sdk_sysroot (), |
| 415 | m_sdk_build (), |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 416 | m_working_dir (), |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 417 | m_remote_url (), |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 418 | m_name (), |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 419 | m_major_os_version (UINT32_MAX), |
| 420 | m_minor_os_version (UINT32_MAX), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 421 | m_update_os_version (UINT32_MAX), |
| 422 | m_system_arch(), |
Greg Clayton | 7597b35 | 2015-02-02 20:45:17 +0000 | [diff] [blame] | 423 | m_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 424 | m_uid_map(), |
| 425 | m_gid_map(), |
| 426 | m_max_uid_name_len (0), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 427 | m_max_gid_name_len (0), |
| 428 | m_supports_rsync (false), |
| 429 | m_rsync_opts (), |
| 430 | m_rsync_prefix (), |
| 431 | m_supports_ssh (false), |
| 432 | m_ssh_opts (), |
Jason Molenda | 6223db27 | 2014-02-13 07:11:08 +0000 | [diff] [blame] | 433 | m_ignores_remote_hostname (false), |
Jason Molenda | 2094dbf | 2014-02-13 23:11:45 +0000 | [diff] [blame] | 434 | m_trap_handlers(), |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 435 | m_calculated_trap_handlers (false), |
| 436 | m_module_cache (llvm::make_unique<ModuleCache> ()) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 437 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 438 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 439 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 440 | log->Printf ("%p Platform::Platform()", static_cast<void*>(this)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | //------------------------------------------------------------------ |
| 444 | /// Destructor. |
| 445 | /// |
| 446 | /// The destructor is virtual since this class is designed to be |
| 447 | /// inherited from by the plug-in instance. |
| 448 | //------------------------------------------------------------------ |
| 449 | Platform::~Platform() |
| 450 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 451 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 452 | if (log) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 453 | log->Printf ("%p Platform::~Platform()", static_cast<void*>(this)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 456 | void |
| 457 | Platform::GetStatus (Stream &strm) |
| 458 | { |
| 459 | uint32_t major = UINT32_MAX; |
| 460 | uint32_t minor = UINT32_MAX; |
| 461 | uint32_t update = UINT32_MAX; |
| 462 | std::string s; |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 463 | strm.Printf (" Platform: %s\n", GetPluginName().GetCString()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 464 | |
| 465 | ArchSpec arch (GetSystemArchitecture()); |
| 466 | if (arch.IsValid()) |
| 467 | { |
| 468 | if (!arch.GetTriple().str().empty()) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 469 | strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | if (GetOSVersion(major, minor, update)) |
| 473 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 474 | strm.Printf("OS Version: %u", major); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 475 | if (minor != UINT32_MAX) |
| 476 | strm.Printf(".%u", minor); |
| 477 | if (update != UINT32_MAX) |
| 478 | strm.Printf(".%u", update); |
| 479 | |
| 480 | if (GetOSBuildString (s)) |
| 481 | strm.Printf(" (%s)", s.c_str()); |
| 482 | |
| 483 | strm.EOL(); |
| 484 | } |
| 485 | |
| 486 | if (GetOSKernelDescription (s)) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 487 | strm.Printf(" Kernel: %s\n", s.c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 488 | |
| 489 | if (IsHost()) |
| 490 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 491 | strm.Printf(" Hostname: %s\n", GetHostname()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 492 | } |
| 493 | else |
| 494 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 495 | const bool is_connected = IsConnected(); |
| 496 | if (is_connected) |
| 497 | strm.Printf(" Hostname: %s\n", GetHostname()); |
| 498 | strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no"); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 499 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 500 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 501 | if (GetWorkingDirectory()) |
| 502 | { |
| 503 | strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString()); |
| 504 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 505 | if (!IsConnected()) |
| 506 | return; |
| 507 | |
| 508 | std::string specific_info(GetPlatformSpecificConnectionInformation()); |
| 509 | |
| 510 | if (specific_info.empty() == false) |
| 511 | strm.Printf("Platform-specific connection: %s\n", specific_info.c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 514 | |
| 515 | bool |
| 516 | Platform::GetOSVersion (uint32_t &major, |
| 517 | uint32_t &minor, |
| 518 | uint32_t &update) |
| 519 | { |
Greg Clayton | 7597b35 | 2015-02-02 20:45:17 +0000 | [diff] [blame] | 520 | Mutex::Locker locker (m_mutex); |
| 521 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 522 | bool success = m_major_os_version != UINT32_MAX; |
| 523 | if (IsHost()) |
| 524 | { |
| 525 | if (!success) |
| 526 | { |
| 527 | // We have a local host platform |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 528 | 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] | 529 | m_os_version_set_while_connected = success; |
| 530 | } |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | // We have a remote platform. We can only fetch the remote |
| 535 | // OS version if we are connected, and we don't want to do it |
| 536 | // more than once. |
| 537 | |
| 538 | const bool is_connected = IsConnected(); |
| 539 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 540 | bool fetch = false; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 541 | if (success) |
| 542 | { |
| 543 | // We have valid OS version info, check to make sure it wasn't |
| 544 | // manually set prior to connecting. If it was manually set prior |
| 545 | // to connecting, then lets fetch the actual OS version info |
| 546 | // if we are now connected. |
| 547 | if (is_connected && !m_os_version_set_while_connected) |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 548 | fetch = true; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 549 | } |
| 550 | else |
| 551 | { |
| 552 | // 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] | 553 | fetch = is_connected; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 556 | if (fetch) |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 557 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 558 | success = GetRemoteOSVersion (); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 559 | m_os_version_set_while_connected = success; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | if (success) |
| 564 | { |
| 565 | major = m_major_os_version; |
| 566 | minor = m_minor_os_version; |
| 567 | update = m_update_os_version; |
| 568 | } |
| 569 | return success; |
| 570 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 571 | |
| 572 | bool |
| 573 | Platform::GetOSBuildString (std::string &s) |
| 574 | { |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 575 | s.clear(); |
| 576 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 577 | if (IsHost()) |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 578 | #if !defined(__linux__) |
| 579 | return HostInfo::GetOSBuildString(s); |
| 580 | #else |
| 581 | return false; |
| 582 | #endif |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 583 | else |
| 584 | return GetRemoteOSBuildString (s); |
| 585 | } |
| 586 | |
| 587 | bool |
| 588 | Platform::GetOSKernelDescription (std::string &s) |
| 589 | { |
| 590 | if (IsHost()) |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 591 | #if !defined(__linux__) |
| 592 | return HostInfo::GetOSKernelDescription(s); |
| 593 | #else |
| 594 | return false; |
| 595 | #endif |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 596 | else |
| 597 | return GetRemoteOSKernelDescription (s); |
| 598 | } |
| 599 | |
Sean Callanan | 5dc2981 | 2014-12-05 01:16:31 +0000 | [diff] [blame] | 600 | void |
Greg Clayton | cd6bbba | 2015-01-22 18:25:49 +0000 | [diff] [blame] | 601 | Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options) |
Sean Callanan | 5dc2981 | 2014-12-05 01:16:31 +0000 | [diff] [blame] | 602 | { |
| 603 | std::vector<std::string> default_compilation_options = |
| 604 | { |
| 605 | "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc" |
| 606 | }; |
| 607 | |
| 608 | options.insert(options.end(), |
| 609 | default_compilation_options.begin(), |
| 610 | default_compilation_options.end()); |
| 611 | } |
| 612 | |
| 613 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 614 | FileSpec |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 615 | Platform::GetWorkingDirectory () |
| 616 | { |
| 617 | if (IsHost()) |
| 618 | { |
| 619 | char cwd[PATH_MAX]; |
| 620 | if (getcwd(cwd, sizeof(cwd))) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 621 | return FileSpec{cwd, true}; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 622 | else |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 623 | return FileSpec{}; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 624 | } |
| 625 | else |
| 626 | { |
| 627 | if (!m_working_dir) |
| 628 | m_working_dir = GetRemoteWorkingDirectory(); |
| 629 | return m_working_dir; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | |
| 634 | struct RecurseCopyBaton |
| 635 | { |
| 636 | const FileSpec& dst; |
| 637 | Platform *platform_ptr; |
| 638 | Error error; |
| 639 | }; |
| 640 | |
| 641 | |
| 642 | static FileSpec::EnumerateDirectoryResult |
| 643 | RecurseCopy_Callback (void *baton, |
| 644 | FileSpec::FileType file_type, |
| 645 | const FileSpec &src) |
| 646 | { |
| 647 | RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton; |
| 648 | switch (file_type) |
| 649 | { |
| 650 | case FileSpec::eFileTypePipe: |
| 651 | case FileSpec::eFileTypeSocket: |
| 652 | // we have no way to copy pipes and sockets - ignore them and continue |
| 653 | return FileSpec::eEnumerateDirectoryResultNext; |
| 654 | break; |
| 655 | |
| 656 | case FileSpec::eFileTypeDirectory: |
| 657 | { |
| 658 | // make the new directory and get in there |
| 659 | FileSpec dst_dir = rc_baton->dst; |
| 660 | if (!dst_dir.GetFilename()) |
| 661 | dst_dir.GetFilename() = src.GetLastPathComponent(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 662 | Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 663 | if (error.Fail()) |
| 664 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 665 | rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", |
| 666 | dst_dir.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 667 | return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out |
| 668 | } |
| 669 | |
| 670 | // now recurse |
| 671 | std::string src_dir_path (src.GetPath()); |
| 672 | |
| 673 | // Make a filespec that only fills in the directory of a FileSpec so |
| 674 | // when we enumerate we can quickly fill in the filename for dst copies |
| 675 | FileSpec recurse_dst; |
| 676 | recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str()); |
| 677 | RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() }; |
| 678 | FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2); |
| 679 | if (rc_baton2.error.Fail()) |
| 680 | { |
| 681 | rc_baton->error.SetErrorString(rc_baton2.error.AsCString()); |
| 682 | return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out |
| 683 | } |
| 684 | return FileSpec::eEnumerateDirectoryResultNext; |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case FileSpec::eFileTypeSymbolicLink: |
| 689 | { |
| 690 | // copy the file and keep going |
| 691 | FileSpec dst_file = rc_baton->dst; |
| 692 | if (!dst_file.GetFilename()) |
| 693 | dst_file.GetFilename() = src.GetFilename(); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 694 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 695 | FileSpec src_resolved; |
| 696 | |
| 697 | rc_baton->error = FileSystem::Readlink(src, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 698 | |
| 699 | if (rc_baton->error.Fail()) |
| 700 | return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 701 | |
| 702 | rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 703 | |
| 704 | if (rc_baton->error.Fail()) |
| 705 | return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out |
| 706 | |
| 707 | return FileSpec::eEnumerateDirectoryResultNext; |
| 708 | } |
| 709 | break; |
| 710 | case FileSpec::eFileTypeRegular: |
| 711 | { |
| 712 | // copy the file and keep going |
| 713 | FileSpec dst_file = rc_baton->dst; |
| 714 | if (!dst_file.GetFilename()) |
| 715 | dst_file.GetFilename() = src.GetFilename(); |
| 716 | Error err = rc_baton->platform_ptr->PutFile(src, dst_file); |
| 717 | if (err.Fail()) |
| 718 | { |
| 719 | rc_baton->error.SetErrorString(err.AsCString()); |
| 720 | return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out |
| 721 | } |
| 722 | return FileSpec::eEnumerateDirectoryResultNext; |
| 723 | } |
| 724 | break; |
| 725 | |
| 726 | case FileSpec::eFileTypeInvalid: |
| 727 | case FileSpec::eFileTypeOther: |
| 728 | case FileSpec::eFileTypeUnknown: |
| 729 | rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str()); |
| 730 | return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out |
| 731 | break; |
| 732 | } |
David Majnemer | 8faf937 | 2014-09-16 06:34:29 +0000 | [diff] [blame] | 733 | llvm_unreachable("Unhandled FileSpec::FileType!"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | Error |
| 737 | Platform::Install (const FileSpec& src, const FileSpec& dst) |
| 738 | { |
| 739 | Error error; |
| 740 | |
| 741 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
| 742 | if (log) |
| 743 | log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str()); |
| 744 | FileSpec fixed_dst(dst); |
| 745 | |
| 746 | if (!fixed_dst.GetFilename()) |
| 747 | fixed_dst.GetFilename() = src.GetFilename(); |
| 748 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 749 | FileSpec working_dir = GetWorkingDirectory(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 750 | |
| 751 | if (dst) |
| 752 | { |
| 753 | if (dst.GetDirectory()) |
| 754 | { |
| 755 | const char first_dst_dir_char = dst.GetDirectory().GetCString()[0]; |
| 756 | if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') |
| 757 | { |
| 758 | fixed_dst.GetDirectory() = dst.GetDirectory(); |
| 759 | } |
| 760 | // If the fixed destination file doesn't have a directory yet, |
| 761 | // then we must have a relative path. We will resolve this relative |
| 762 | // path against the platform's working directory |
| 763 | if (!fixed_dst.GetDirectory()) |
| 764 | { |
| 765 | FileSpec relative_spec; |
| 766 | std::string path; |
| 767 | if (working_dir) |
| 768 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 769 | relative_spec = working_dir; |
| 770 | relative_spec.AppendPathComponent(dst.GetPath()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 771 | fixed_dst.GetDirectory() = relative_spec.GetDirectory(); |
| 772 | } |
| 773 | else |
| 774 | { |
| 775 | error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str()); |
| 776 | return error; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | if (working_dir) |
| 783 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 784 | fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 785 | } |
| 786 | else |
| 787 | { |
| 788 | error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str()); |
| 789 | return error; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | else |
| 794 | { |
| 795 | if (working_dir) |
| 796 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 797 | fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 798 | } |
| 799 | else |
| 800 | { |
| 801 | error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty"); |
| 802 | return error; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (log) |
| 807 | log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str()); |
| 808 | |
| 809 | if (GetSupportsRSync()) |
| 810 | { |
| 811 | error = PutFile(src, dst); |
| 812 | } |
| 813 | else |
| 814 | { |
| 815 | switch (src.GetFileType()) |
| 816 | { |
| 817 | case FileSpec::eFileTypeDirectory: |
| 818 | { |
| 819 | if (GetFileExists (fixed_dst)) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 820 | Unlink(fixed_dst); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 821 | uint32_t permissions = src.GetPermissions(); |
| 822 | if (permissions == 0) |
| 823 | permissions = eFilePermissionsDirectoryDefault; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 824 | error = MakeDirectory(fixed_dst, permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 825 | if (error.Success()) |
| 826 | { |
| 827 | // Make a filespec that only fills in the directory of a FileSpec so |
| 828 | // when we enumerate we can quickly fill in the filename for dst copies |
| 829 | FileSpec recurse_dst; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 830 | recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 831 | std::string src_dir_path (src.GetPath()); |
| 832 | RecurseCopyBaton baton = { recurse_dst, this, Error() }; |
| 833 | FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton); |
| 834 | return baton.error; |
| 835 | } |
| 836 | } |
| 837 | break; |
| 838 | |
| 839 | case FileSpec::eFileTypeRegular: |
| 840 | if (GetFileExists (fixed_dst)) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 841 | Unlink(fixed_dst); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 842 | error = PutFile(src, fixed_dst); |
| 843 | break; |
| 844 | |
| 845 | case FileSpec::eFileTypeSymbolicLink: |
| 846 | { |
| 847 | if (GetFileExists (fixed_dst)) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 848 | Unlink(fixed_dst); |
| 849 | FileSpec src_resolved; |
| 850 | error = FileSystem::Readlink(src, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 851 | if (error.Success()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 852 | error = CreateSymlink(dst, src_resolved); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 853 | } |
| 854 | break; |
| 855 | case FileSpec::eFileTypePipe: |
| 856 | error.SetErrorString("platform install doesn't handle pipes"); |
| 857 | break; |
| 858 | case FileSpec::eFileTypeSocket: |
| 859 | error.SetErrorString("platform install doesn't handle sockets"); |
| 860 | break; |
| 861 | case FileSpec::eFileTypeInvalid: |
| 862 | case FileSpec::eFileTypeUnknown: |
| 863 | case FileSpec::eFileTypeOther: |
| 864 | error.SetErrorString("platform install doesn't handle non file or directory items"); |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | return error; |
| 869 | } |
| 870 | |
| 871 | bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 872 | Platform::SetWorkingDirectory(const FileSpec &file_spec) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 873 | { |
| 874 | if (IsHost()) |
| 875 | { |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 876 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
| 877 | if (log) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 878 | log->Printf("Platform::SetWorkingDirectory('%s')", |
| 879 | file_spec.GetCString()); |
| 880 | if (file_spec) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 881 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 882 | if (::chdir(file_spec.GetCString()) == 0) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 883 | return true; |
| 884 | } |
| 885 | return false; |
| 886 | } |
| 887 | else |
| 888 | { |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 889 | m_working_dir.Clear(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 890 | return SetRemoteWorkingDirectory(file_spec); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
| 894 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 895 | Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 896 | { |
| 897 | if (IsHost()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 898 | return FileSystem::MakeDirectory(file_spec, permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 899 | else |
| 900 | { |
| 901 | Error error; |
| 902 | error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); |
| 903 | return error; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 908 | Platform::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 909 | { |
| 910 | if (IsHost()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 911 | return FileSystem::GetFilePermissions(file_spec, file_permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 912 | else |
| 913 | { |
| 914 | Error error; |
| 915 | error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); |
| 916 | return error; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 921 | Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 922 | { |
| 923 | if (IsHost()) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 924 | return FileSystem::SetFilePermissions(file_spec, file_permissions); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 925 | else |
| 926 | { |
| 927 | Error error; |
| 928 | error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); |
| 929 | return error; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | ConstString |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 934 | Platform::GetName () |
| 935 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 936 | return GetPluginName(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | const char * |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 940 | Platform::GetHostname () |
| 941 | { |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 942 | if (IsHost()) |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 943 | return "127.0.0.1"; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 944 | |
| 945 | if (m_name.empty()) |
| 946 | return NULL; |
| 947 | return m_name.c_str(); |
| 948 | } |
| 949 | |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 950 | bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 951 | Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 952 | { |
| 953 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
| 954 | if (log) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 955 | log->Printf("Platform::SetRemoteWorkingDirectory('%s')", |
| 956 | working_dir.GetCString()); |
| 957 | m_working_dir = working_dir; |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 958 | return true; |
| 959 | } |
| 960 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 961 | const char * |
| 962 | Platform::GetUserName (uint32_t uid) |
| 963 | { |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 964 | #if !defined(LLDB_DISABLE_POSIX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 965 | const char *user_name = GetCachedUserName(uid); |
| 966 | if (user_name) |
| 967 | return user_name; |
| 968 | if (IsHost()) |
| 969 | { |
| 970 | std::string name; |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 971 | if (HostInfo::LookupUserName(uid, name)) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 972 | return SetCachedUserName (uid, name.c_str(), name.size()); |
| 973 | } |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 974 | #endif |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 975 | return NULL; |
| 976 | } |
| 977 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 978 | const char * |
| 979 | Platform::GetGroupName (uint32_t gid) |
| 980 | { |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 981 | #if !defined(LLDB_DISABLE_POSIX) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 982 | const char *group_name = GetCachedGroupName(gid); |
| 983 | if (group_name) |
| 984 | return group_name; |
| 985 | if (IsHost()) |
| 986 | { |
| 987 | std::string name; |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 988 | if (HostInfo::LookupGroupName(gid, name)) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 989 | return SetCachedGroupName (gid, name.c_str(), name.size()); |
| 990 | } |
Zachary Turner | b245eca | 2014-08-21 20:02:17 +0000 | [diff] [blame] | 991 | #endif |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 992 | return NULL; |
| 993 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 994 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 995 | bool |
| 996 | Platform::SetOSVersion (uint32_t major, |
| 997 | uint32_t minor, |
| 998 | uint32_t update) |
| 999 | { |
| 1000 | if (IsHost()) |
| 1001 | { |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 1002 | // We don't need anyone setting the OS version for the host platform, |
| 1003 | // we should be able to figure it out by calling HostInfo::GetOSVersion(...). |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1004 | return false; |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | // We have a remote platform, allow setting the target OS version if |
| 1009 | // we aren't connected, since if we are connected, we should be able to |
| 1010 | // request the remote OS version from the connected platform. |
| 1011 | if (IsConnected()) |
| 1012 | return false; |
| 1013 | else |
| 1014 | { |
| 1015 | // We aren't connected and we might want to set the OS version |
| 1016 | // ahead of time before we connect so we can peruse files and |
| 1017 | // use a local SDK or PDK cache of support files to disassemble |
| 1018 | // or do other things. |
| 1019 | m_major_os_version = major; |
| 1020 | m_minor_os_version = minor; |
| 1021 | m_update_os_version = update; |
| 1022 | return true; |
| 1023 | } |
| 1024 | } |
| 1025 | return false; |
| 1026 | } |
| 1027 | |
| 1028 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1029 | Error |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1030 | Platform::ResolveExecutable (const ModuleSpec &module_spec, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 1031 | lldb::ModuleSP &exe_module_sp, |
| 1032 | const FileSpecList *module_search_paths_ptr) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1033 | { |
| 1034 | Error error; |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1035 | if (module_spec.GetFileSpec().Exists()) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1036 | { |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 1037 | if (module_spec.GetArchitecture().IsValid()) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1038 | { |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 1039 | error = ModuleList::GetSharedModule (module_spec, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1040 | exe_module_sp, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 1041 | module_search_paths_ptr, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1042 | NULL, |
| 1043 | NULL); |
| 1044 | } |
| 1045 | else |
| 1046 | { |
| 1047 | // No valid architecture was specified, ask the platform for |
| 1048 | // the architectures that we should be using (in the correct order) |
| 1049 | // and see if we can find a match that way |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1050 | ModuleSpec arch_module_spec(module_spec); |
| 1051 | for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1052 | { |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1053 | error = ModuleList::GetSharedModule (arch_module_spec, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1054 | exe_module_sp, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 1055 | module_search_paths_ptr, |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1056 | NULL, |
| 1057 | NULL); |
| 1058 | // Did we find an executable using one of the |
| 1059 | if (error.Success() && exe_module_sp) |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | else |
| 1065 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 1066 | error.SetErrorStringWithFormat ("'%s' does not exist", |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1067 | module_spec.GetFileSpec().GetPath().c_str()); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1068 | } |
| 1069 | return error; |
| 1070 | } |
| 1071 | |
Greg Clayton | 103f028 | 2012-09-12 02:03:59 +0000 | [diff] [blame] | 1072 | Error |
| 1073 | Platform::ResolveSymbolFile (Target &target, |
| 1074 | const ModuleSpec &sym_spec, |
| 1075 | FileSpec &sym_file) |
| 1076 | { |
| 1077 | Error error; |
| 1078 | if (sym_spec.GetSymbolFileSpec().Exists()) |
| 1079 | sym_file = sym_spec.GetSymbolFileSpec(); |
| 1080 | else |
| 1081 | error.SetErrorString("unable to resolve symbol file"); |
| 1082 | return error; |
| 1083 | |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | |
Greg Clayton | aa51684 | 2011-08-11 16:25:18 +0000 | [diff] [blame] | 1088 | bool |
| 1089 | Platform::ResolveRemotePath (const FileSpec &platform_path, |
| 1090 | FileSpec &resolved_platform_path) |
| 1091 | { |
| 1092 | resolved_platform_path = platform_path; |
| 1093 | return resolved_platform_path.ResolvePath(); |
| 1094 | } |
| 1095 | |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1096 | |
| 1097 | const ArchSpec & |
| 1098 | Platform::GetSystemArchitecture() |
| 1099 | { |
| 1100 | if (IsHost()) |
| 1101 | { |
| 1102 | if (!m_system_arch.IsValid()) |
| 1103 | { |
| 1104 | // We have a local host platform |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 1105 | m_system_arch = HostInfo::GetArchitecture(); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1106 | m_system_arch_set_while_connected = m_system_arch.IsValid(); |
| 1107 | } |
| 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | // We have a remote platform. We can only fetch the remote |
| 1112 | // system architecture if we are connected, and we don't want to do it |
| 1113 | // more than once. |
| 1114 | |
| 1115 | const bool is_connected = IsConnected(); |
| 1116 | |
| 1117 | bool fetch = false; |
| 1118 | if (m_system_arch.IsValid()) |
| 1119 | { |
| 1120 | // We have valid OS version info, check to make sure it wasn't |
| 1121 | // manually set prior to connecting. If it was manually set prior |
| 1122 | // to connecting, then lets fetch the actual OS version info |
| 1123 | // if we are now connected. |
| 1124 | if (is_connected && !m_system_arch_set_while_connected) |
| 1125 | fetch = true; |
| 1126 | } |
| 1127 | else |
| 1128 | { |
| 1129 | // We don't have valid OS version info, fetch it if we are connected |
| 1130 | fetch = is_connected; |
| 1131 | } |
| 1132 | |
| 1133 | if (fetch) |
| 1134 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1135 | m_system_arch = GetRemoteSystemArchitecture (); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1136 | m_system_arch_set_while_connected = m_system_arch.IsValid(); |
| 1137 | } |
| 1138 | } |
| 1139 | return m_system_arch; |
| 1140 | } |
| 1141 | |
| 1142 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1143 | Error |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1144 | Platform::ConnectRemote (Args& args) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1145 | { |
| 1146 | Error error; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1147 | if (IsHost()) |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1148 | 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] | 1149 | else |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1150 | error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString()); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1151 | return error; |
| 1152 | } |
| 1153 | |
| 1154 | Error |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1155 | Platform::DisconnectRemote () |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1156 | { |
| 1157 | Error error; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1158 | if (IsHost()) |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1159 | 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] | 1160 | else |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1161 | error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString()); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1162 | return error; |
| 1163 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1164 | |
| 1165 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1166 | Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1167 | { |
| 1168 | // Take care of the host case so that each subclass can just |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1169 | // call this function to get the host functionality. |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1170 | if (IsHost()) |
| 1171 | return Host::GetProcessInfo (pid, process_info); |
| 1172 | return false; |
| 1173 | } |
| 1174 | |
| 1175 | uint32_t |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1176 | Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info, |
| 1177 | ProcessInstanceInfoList &process_infos) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1178 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1179 | // Take care of the host case so that each subclass can just |
| 1180 | // call this function to get the host functionality. |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1181 | uint32_t match_count = 0; |
| 1182 | if (IsHost()) |
| 1183 | match_count = Host::FindProcesses (match_info, process_infos); |
| 1184 | return match_count; |
| 1185 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1186 | |
| 1187 | |
| 1188 | Error |
| 1189 | Platform::LaunchProcess (ProcessLaunchInfo &launch_info) |
| 1190 | { |
| 1191 | Error error; |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1192 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); |
| 1193 | if (log) |
| 1194 | log->Printf ("Platform::%s()", __FUNCTION__); |
| 1195 | |
| 1196 | // Take care of the host case so that each subclass can just |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1197 | // call this function to get the host functionality. |
| 1198 | if (IsHost()) |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1199 | { |
| 1200 | if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY")) |
| 1201 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 1202 | |
| 1203 | if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) |
| 1204 | { |
| 1205 | const bool is_localhost = true; |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1206 | const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); |
| 1207 | const bool first_arg_is_full_shell_command = false; |
Jim Ingham | d399079 | 2013-09-11 18:23:22 +0000 | [diff] [blame] | 1208 | uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info); |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1209 | if (log) |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 1210 | { |
| 1211 | const FileSpec &shell = launch_info.GetShell(); |
| 1212 | const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>"; |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1213 | log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'", |
| 1214 | __FUNCTION__, |
| 1215 | num_resumes, |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 1216 | shell_str); |
| 1217 | } |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1218 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1219 | if (!launch_info.ConvertArgumentsForLaunchingInShell (error, |
| 1220 | is_localhost, |
| 1221 | will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 1222 | first_arg_is_full_shell_command, |
| 1223 | num_resumes)) |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1224 | return error; |
| 1225 | } |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1226 | else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 1227 | { |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1228 | error = ShellExpandArguments(launch_info); |
Enrico Granata | 83a1437 | 2015-02-20 21:48:38 +0000 | [diff] [blame] | 1229 | if (error.Fail()) |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 1230 | return error; |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 1231 | } |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1232 | |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1233 | if (log) |
| 1234 | log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ()); |
| 1235 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1236 | error = Host::LaunchProcess (launch_info); |
Greg Clayton | 84db910 | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 1237 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1238 | else |
| 1239 | error.SetErrorString ("base lldb_private::Platform class can't launch remote processes"); |
| 1240 | return error; |
| 1241 | } |
| 1242 | |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1243 | Error |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1244 | Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info) |
Enrico Granata | 83a1437 | 2015-02-20 21:48:38 +0000 | [diff] [blame] | 1245 | { |
| 1246 | if (IsHost()) |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 1247 | return Host::ShellExpandArguments(launch_info); |
| 1248 | return Error("base lldb_private::Platform class can't expand arguments"); |
Enrico Granata | 83a1437 | 2015-02-20 21:48:38 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | Error |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1252 | Platform::KillProcess (const lldb::pid_t pid) |
| 1253 | { |
| 1254 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); |
| 1255 | if (log) |
| 1256 | log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid); |
| 1257 | |
Zachary Turner | 7271bab | 2015-05-13 19:44:24 +0000 | [diff] [blame] | 1258 | // Try to find a process plugin to handle this Kill request. If we can't, fall back to |
| 1259 | // the default OS implementation. |
| 1260 | size_t num_debuggers = Debugger::GetNumDebuggers(); |
| 1261 | for (size_t didx = 0; didx < num_debuggers; ++didx) |
| 1262 | { |
| 1263 | DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx); |
| 1264 | lldb_private::TargetList &targets = debugger->GetTargetList(); |
| 1265 | for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) |
| 1266 | { |
| 1267 | ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP(); |
| 1268 | if (process->GetID() == pid) |
| 1269 | return process->Destroy(true); |
| 1270 | } |
| 1271 | } |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1272 | |
Zachary Turner | 7271bab | 2015-05-13 19:44:24 +0000 | [diff] [blame] | 1273 | if (!IsHost()) |
| 1274 | { |
| 1275 | return Error("base lldb_private::Platform class can't kill remote processes unless " |
| 1276 | "they are controlled by a process plugin"); |
| 1277 | } |
| 1278 | Host::Kill(pid, SIGTERM); |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1279 | return Error(); |
| 1280 | } |
| 1281 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1282 | lldb::ProcessSP |
| 1283 | Platform::DebugProcess (ProcessLaunchInfo &launch_info, |
| 1284 | Debugger &debugger, |
| 1285 | 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] | 1286 | Error &error) |
| 1287 | { |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1288 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); |
| 1289 | if (log) |
| 1290 | log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target)); |
| 1291 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1292 | ProcessSP process_sp; |
| 1293 | // Make sure we stop at the entry point |
| 1294 | launch_info.GetFlags ().Set (eLaunchFlagDebug); |
Jim Ingham | b4451b1 | 2012-06-01 01:22:13 +0000 | [diff] [blame] | 1295 | // We always launch the process we are going to debug in a separate process |
| 1296 | // group, since then we can handle ^C interrupts ourselves w/o having to worry |
| 1297 | // about the target getting them as well. |
| 1298 | launch_info.SetLaunchInSeparateProcessGroup(true); |
| 1299 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1300 | error = LaunchProcess (launch_info); |
| 1301 | if (error.Success()) |
| 1302 | { |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1303 | if (log) |
| 1304 | 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] | 1305 | if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1306 | { |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 1307 | ProcessAttachInfo attach_info (launch_info); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 1308 | process_sp = Attach (attach_info, debugger, target, error); |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1309 | if (process_sp) |
| 1310 | { |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1311 | if (log) |
| 1312 | 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] | 1313 | launch_info.SetHijackListener(attach_info.GetHijackListener()); |
| 1314 | |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1315 | // Since we attached to the process, it will think it needs to detach |
| 1316 | // if the process object just goes away without an explicit call to |
| 1317 | // Process::Kill() or Process::Detach(), so let it know to kill the |
| 1318 | // process if this happens. |
| 1319 | process_sp->SetShouldDetach (false); |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 1320 | |
| 1321 | // If we didn't have any file actions, the pseudo terminal might |
| 1322 | // have been used where the slave side was given as the file to |
| 1323 | // open for stdin/out/err after we have already opened the master |
| 1324 | // so we can read/write stdin/out/err. |
| 1325 | int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); |
| 1326 | if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) |
| 1327 | { |
| 1328 | process_sp->SetSTDIOFileDescriptor(pty_fd); |
| 1329 | } |
Greg Clayton | e24c4ac | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1330 | } |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1331 | else |
| 1332 | { |
| 1333 | if (log) |
| 1334 | log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ()); |
| 1335 | } |
| 1336 | } |
| 1337 | else |
| 1338 | { |
| 1339 | if (log) |
| 1340 | 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] | 1341 | } |
| 1342 | } |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1343 | else |
| 1344 | { |
| 1345 | if (log) |
| 1346 | log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ()); |
| 1347 | } |
| 1348 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1349 | return process_sp; |
| 1350 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1351 | |
| 1352 | |
| 1353 | lldb::PlatformSP |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1354 | Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1355 | { |
| 1356 | lldb::PlatformSP platform_sp; |
| 1357 | Error error; |
| 1358 | if (arch.IsValid()) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1359 | platform_sp = Platform::Create (arch, platform_arch_ptr, error); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1360 | return platform_sp; |
| 1361 | } |
| 1362 | |
| 1363 | |
| 1364 | //------------------------------------------------------------------ |
| 1365 | /// Lets a platform answer if it is compatible with a given |
| 1366 | /// architecture and the target triple contained within. |
| 1367 | //------------------------------------------------------------------ |
| 1368 | bool |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1369 | 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] | 1370 | { |
| 1371 | // If the architecture is invalid, we must answer true... |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1372 | if (arch.IsValid()) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1373 | { |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1374 | ArchSpec platform_arch; |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1375 | // Try for an exact architecture match first. |
| 1376 | if (exact_arch_match) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1377 | { |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1378 | 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] | 1379 | { |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1380 | if (arch.IsExactMatch(platform_arch)) |
| 1381 | { |
| 1382 | if (compatible_arch_ptr) |
| 1383 | *compatible_arch_ptr = platform_arch; |
| 1384 | return true; |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | else |
| 1389 | { |
| 1390 | for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx) |
| 1391 | { |
| 1392 | if (arch.IsCompatibleMatch(platform_arch)) |
| 1393 | { |
| 1394 | if (compatible_arch_ptr) |
| 1395 | *compatible_arch_ptr = platform_arch; |
| 1396 | return true; |
| 1397 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1398 | } |
| 1399 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1400 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1401 | if (compatible_arch_ptr) |
| 1402 | compatible_arch_ptr->Clear(); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1403 | return false; |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1406 | Error |
| 1407 | Platform::PutFile (const FileSpec& source, |
| 1408 | const FileSpec& destination, |
| 1409 | uint32_t uid, |
| 1410 | uint32_t gid) |
| 1411 | { |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1412 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); |
| 1413 | if (log) |
| 1414 | log->Printf("[PutFile] Using block by block transfer....\n"); |
| 1415 | |
Pavel Labath | 646b064 | 2015-02-17 16:07:52 +0000 | [diff] [blame] | 1416 | uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec; |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1417 | if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink) |
| 1418 | source_open_options |= File::eOpenoptionDontFollowSymlinks; |
| 1419 | |
| 1420 | File source_file(source, source_open_options, lldb::eFilePermissionsUserRW); |
| 1421 | Error error; |
| 1422 | uint32_t permissions = source_file.GetPermissions(error); |
| 1423 | if (permissions == 0) |
| 1424 | permissions = lldb::eFilePermissionsFileDefault; |
| 1425 | |
| 1426 | if (!source_file.IsValid()) |
| 1427 | return Error("PutFile: unable to open source file"); |
| 1428 | lldb::user_id_t dest_file = OpenFile (destination, |
| 1429 | File::eOpenOptionCanCreate | |
| 1430 | File::eOpenOptionWrite | |
Pavel Labath | 646b064 | 2015-02-17 16:07:52 +0000 | [diff] [blame] | 1431 | File::eOpenOptionTruncate | |
| 1432 | File::eOpenOptionCloseOnExec, |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1433 | permissions, |
| 1434 | error); |
| 1435 | if (log) |
| 1436 | log->Printf ("dest_file = %" PRIu64 "\n", dest_file); |
| 1437 | |
| 1438 | if (error.Fail()) |
| 1439 | return error; |
| 1440 | if (dest_file == UINT64_MAX) |
| 1441 | return Error("unable to open target file"); |
| 1442 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0)); |
| 1443 | uint64_t offset = 0; |
| 1444 | for (;;) |
| 1445 | { |
| 1446 | size_t bytes_read = buffer_sp->GetByteSize(); |
| 1447 | error = source_file.Read(buffer_sp->GetBytes(), bytes_read); |
| 1448 | if (error.Fail() || bytes_read == 0) |
| 1449 | break; |
| 1450 | |
| 1451 | const uint64_t bytes_written = WriteFile(dest_file, offset, |
| 1452 | buffer_sp->GetBytes(), bytes_read, error); |
| 1453 | if (error.Fail()) |
| 1454 | break; |
| 1455 | |
| 1456 | offset += bytes_written; |
| 1457 | if (bytes_written != bytes_read) |
| 1458 | { |
| 1459 | // We didn't write the correct number of bytes, so adjust |
| 1460 | // the file position in the source file we are reading from... |
| 1461 | source_file.SeekFromStart(offset); |
| 1462 | } |
| 1463 | } |
| 1464 | CloseFile(dest_file, error); |
| 1465 | |
| 1466 | if (uid == UINT32_MAX && gid == UINT32_MAX) |
| 1467 | return error; |
| 1468 | |
| 1469 | // TODO: ChownFile? |
| 1470 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1471 | return error; |
| 1472 | } |
| 1473 | |
| 1474 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 1475 | Platform::GetFile(const FileSpec &source, |
| 1476 | const FileSpec &destination) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1477 | { |
| 1478 | Error error("unimplemented"); |
| 1479 | return error; |
| 1480 | } |
| 1481 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1482 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 1483 | Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src |
| 1484 | const FileSpec &dst) // The symlink points to dst |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1485 | { |
| 1486 | Error error("unimplemented"); |
| 1487 | return error; |
| 1488 | } |
| 1489 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1490 | bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 1491 | Platform::GetFileExists(const lldb_private::FileSpec &file_spec) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1492 | { |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1496 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 1497 | Platform::Unlink(const FileSpec &path) |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1498 | { |
| 1499 | Error error("unimplemented"); |
| 1500 | return error; |
| 1501 | } |
| 1502 | |
Robert Flack | 96ad3de | 2015-05-09 15:53:31 +0000 | [diff] [blame] | 1503 | uint64_t |
Mohit K. Bhakkad | e0d8c42 | 2015-06-30 06:29:12 +0000 | [diff] [blame] | 1504 | Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags) |
Robert Flack | 96ad3de | 2015-05-09 15:53:31 +0000 | [diff] [blame] | 1505 | { |
| 1506 | uint64_t flags_platform = 0; |
| 1507 | if (flags & eMmapFlagsPrivate) |
| 1508 | flags_platform |= MAP_PRIVATE; |
| 1509 | if (flags & eMmapFlagsAnon) |
| 1510 | flags_platform |= MAP_ANON; |
| 1511 | return flags_platform; |
| 1512 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1513 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1514 | lldb_private::Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 1515 | Platform::RunShellCommand(const char *command, // Shouldn't be NULL |
| 1516 | const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory |
| 1517 | int *status_ptr, // Pass NULL if you don't want the process exit status |
| 1518 | int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit |
| 1519 | std::string *command_output, // Pass NULL if you don't want the command output |
| 1520 | 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] | 1521 | { |
| 1522 | if (IsHost()) |
| 1523 | return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec); |
| 1524 | else |
| 1525 | return Error("unimplemented"); |
| 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | bool |
| 1530 | Platform::CalculateMD5 (const FileSpec& file_spec, |
| 1531 | uint64_t &low, |
| 1532 | uint64_t &high) |
| 1533 | { |
| 1534 | if (IsHost()) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1535 | return FileSystem::CalculateMD5(file_spec, low, high); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1536 | else |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | void |
| 1541 | Platform::SetLocalCacheDirectory (const char* local) |
| 1542 | { |
| 1543 | m_local_cache_directory.assign(local); |
| 1544 | } |
| 1545 | |
| 1546 | const char* |
| 1547 | Platform::GetLocalCacheDirectory () |
| 1548 | { |
| 1549 | return m_local_cache_directory.c_str(); |
| 1550 | } |
| 1551 | |
| 1552 | static OptionDefinition |
| 1553 | g_rsync_option_table[] = |
| 1554 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1555 | { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." }, |
| 1556 | { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." }, |
| 1557 | { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." }, |
| 1558 | { 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] | 1559 | }; |
| 1560 | |
| 1561 | static OptionDefinition |
| 1562 | g_ssh_option_table[] = |
| 1563 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1564 | { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." }, |
| 1565 | { 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] | 1566 | }; |
| 1567 | |
| 1568 | static OptionDefinition |
| 1569 | g_caching_option_table[] = |
| 1570 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 1571 | { 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] | 1572 | }; |
| 1573 | |
| 1574 | OptionGroupPlatformRSync::OptionGroupPlatformRSync () |
| 1575 | { |
| 1576 | } |
| 1577 | |
| 1578 | OptionGroupPlatformRSync::~OptionGroupPlatformRSync () |
| 1579 | { |
| 1580 | } |
| 1581 | |
| 1582 | const lldb_private::OptionDefinition* |
| 1583 | OptionGroupPlatformRSync::GetDefinitions () |
| 1584 | { |
| 1585 | return g_rsync_option_table; |
| 1586 | } |
| 1587 | |
| 1588 | void |
| 1589 | OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter) |
| 1590 | { |
| 1591 | m_rsync = false; |
| 1592 | m_rsync_opts.clear(); |
| 1593 | m_rsync_prefix.clear(); |
| 1594 | m_ignores_remote_hostname = false; |
| 1595 | } |
| 1596 | |
| 1597 | lldb_private::Error |
| 1598 | OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter, |
| 1599 | uint32_t option_idx, |
| 1600 | const char *option_arg) |
| 1601 | { |
| 1602 | Error error; |
| 1603 | char short_option = (char) GetDefinitions()[option_idx].short_option; |
| 1604 | switch (short_option) |
| 1605 | { |
| 1606 | case 'r': |
| 1607 | m_rsync = true; |
| 1608 | break; |
| 1609 | |
| 1610 | case 'R': |
| 1611 | m_rsync_opts.assign(option_arg); |
| 1612 | break; |
| 1613 | |
| 1614 | case 'P': |
| 1615 | m_rsync_prefix.assign(option_arg); |
| 1616 | break; |
| 1617 | |
| 1618 | case 'i': |
| 1619 | m_ignores_remote_hostname = true; |
| 1620 | break; |
| 1621 | |
| 1622 | default: |
| 1623 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
| 1624 | break; |
| 1625 | } |
| 1626 | |
| 1627 | return error; |
| 1628 | } |
| 1629 | |
| 1630 | uint32_t |
| 1631 | OptionGroupPlatformRSync::GetNumDefinitions () |
| 1632 | { |
| 1633 | return llvm::array_lengthof(g_rsync_option_table); |
| 1634 | } |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1635 | |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 1636 | lldb::BreakpointSP |
| 1637 | Platform::SetThreadCreationBreakpoint (lldb_private::Target &target) |
| 1638 | { |
| 1639 | return lldb::BreakpointSP(); |
| 1640 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1641 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1642 | OptionGroupPlatformSSH::OptionGroupPlatformSSH () |
| 1643 | { |
| 1644 | } |
| 1645 | |
| 1646 | OptionGroupPlatformSSH::~OptionGroupPlatformSSH () |
| 1647 | { |
| 1648 | } |
| 1649 | |
| 1650 | const lldb_private::OptionDefinition* |
| 1651 | OptionGroupPlatformSSH::GetDefinitions () |
| 1652 | { |
| 1653 | return g_ssh_option_table; |
| 1654 | } |
| 1655 | |
| 1656 | void |
| 1657 | OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter) |
| 1658 | { |
| 1659 | m_ssh = false; |
| 1660 | m_ssh_opts.clear(); |
| 1661 | } |
| 1662 | |
| 1663 | lldb_private::Error |
| 1664 | OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter, |
| 1665 | uint32_t option_idx, |
| 1666 | const char *option_arg) |
| 1667 | { |
| 1668 | Error error; |
| 1669 | char short_option = (char) GetDefinitions()[option_idx].short_option; |
| 1670 | switch (short_option) |
| 1671 | { |
| 1672 | case 's': |
| 1673 | m_ssh = true; |
| 1674 | break; |
| 1675 | |
| 1676 | case 'S': |
| 1677 | m_ssh_opts.assign(option_arg); |
| 1678 | break; |
| 1679 | |
| 1680 | default: |
| 1681 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
| 1682 | break; |
| 1683 | } |
| 1684 | |
| 1685 | return error; |
| 1686 | } |
| 1687 | |
| 1688 | uint32_t |
| 1689 | OptionGroupPlatformSSH::GetNumDefinitions () |
| 1690 | { |
| 1691 | return llvm::array_lengthof(g_ssh_option_table); |
| 1692 | } |
| 1693 | |
| 1694 | OptionGroupPlatformCaching::OptionGroupPlatformCaching () |
| 1695 | { |
| 1696 | } |
| 1697 | |
| 1698 | OptionGroupPlatformCaching::~OptionGroupPlatformCaching () |
| 1699 | { |
| 1700 | } |
| 1701 | |
| 1702 | const lldb_private::OptionDefinition* |
| 1703 | OptionGroupPlatformCaching::GetDefinitions () |
| 1704 | { |
| 1705 | return g_caching_option_table; |
| 1706 | } |
| 1707 | |
| 1708 | void |
| 1709 | OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter) |
| 1710 | { |
| 1711 | m_cache_dir.clear(); |
| 1712 | } |
| 1713 | |
| 1714 | lldb_private::Error |
| 1715 | OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter, |
| 1716 | uint32_t option_idx, |
| 1717 | const char *option_arg) |
| 1718 | { |
| 1719 | Error error; |
| 1720 | char short_option = (char) GetDefinitions()[option_idx].short_option; |
| 1721 | switch (short_option) |
| 1722 | { |
| 1723 | case 'c': |
| 1724 | m_cache_dir.assign(option_arg); |
| 1725 | break; |
| 1726 | |
| 1727 | default: |
| 1728 | error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); |
| 1729 | break; |
| 1730 | } |
| 1731 | |
| 1732 | return error; |
| 1733 | } |
| 1734 | |
| 1735 | uint32_t |
| 1736 | OptionGroupPlatformCaching::GetNumDefinitions () |
| 1737 | { |
| 1738 | return llvm::array_lengthof(g_caching_option_table); |
| 1739 | } |
| 1740 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1741 | size_t |
| 1742 | Platform::GetEnvironment (StringList &environment) |
| 1743 | { |
| 1744 | environment.Clear(); |
| 1745 | return false; |
| 1746 | } |
Jason Molenda | 2094dbf | 2014-02-13 23:11:45 +0000 | [diff] [blame] | 1747 | |
| 1748 | const std::vector<ConstString> & |
| 1749 | Platform::GetTrapHandlerSymbolNames () |
| 1750 | { |
| 1751 | if (!m_calculated_trap_handlers) |
| 1752 | { |
Greg Clayton | 7597b35 | 2015-02-02 20:45:17 +0000 | [diff] [blame] | 1753 | Mutex::Locker locker (m_mutex); |
Jason Molenda | 4da8706 | 2014-05-23 23:11:27 +0000 | [diff] [blame] | 1754 | if (!m_calculated_trap_handlers) |
| 1755 | { |
| 1756 | CalculateTrapHandlerSymbolNames(); |
| 1757 | m_calculated_trap_handlers = true; |
| 1758 | } |
Jason Molenda | 2094dbf | 2014-02-13 23:11:45 +0000 | [diff] [blame] | 1759 | } |
| 1760 | return m_trap_handlers; |
| 1761 | } |
| 1762 | |
Oleksiy Vyalov | 6474721 | 2015-03-13 18:44:56 +0000 | [diff] [blame] | 1763 | Error |
| 1764 | Platform::GetCachedExecutable (ModuleSpec &module_spec, |
| 1765 | lldb::ModuleSP &module_sp, |
| 1766 | const FileSpecList *module_search_paths_ptr, |
| 1767 | Platform &remote_platform) |
| 1768 | { |
| 1769 | const auto platform_spec = module_spec.GetFileSpec (); |
| 1770 | const auto error = LoadCachedExecutable (module_spec, |
| 1771 | module_sp, |
| 1772 | module_search_paths_ptr, |
| 1773 | remote_platform); |
| 1774 | if (error.Success ()) |
| 1775 | { |
| 1776 | module_spec.GetFileSpec () = module_sp->GetFileSpec (); |
| 1777 | module_spec.GetPlatformFileSpec () = platform_spec; |
| 1778 | } |
| 1779 | |
| 1780 | return error; |
| 1781 | } |
| 1782 | |
| 1783 | Error |
| 1784 | Platform::LoadCachedExecutable (const ModuleSpec &module_spec, |
| 1785 | lldb::ModuleSP &module_sp, |
| 1786 | const FileSpecList *module_search_paths_ptr, |
| 1787 | Platform &remote_platform) |
| 1788 | { |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1789 | return GetRemoteSharedModule (module_spec, |
| 1790 | nullptr, |
| 1791 | module_sp, |
| 1792 | [&](const ModuleSpec &spec) |
| 1793 | { |
| 1794 | return remote_platform.ResolveExecutable ( |
| 1795 | spec, module_sp, module_search_paths_ptr); |
| 1796 | }, |
| 1797 | nullptr); |
Oleksiy Vyalov | 6474721 | 2015-03-13 18:44:56 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1800 | Error |
| 1801 | Platform::GetRemoteSharedModule (const ModuleSpec &module_spec, |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1802 | Process* process, |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1803 | lldb::ModuleSP &module_sp, |
| 1804 | const ModuleResolver &module_resolver, |
| 1805 | bool *did_create_ptr) |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1806 | { |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1807 | // Get module information from a target. |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1808 | ModuleSpec resolved_module_spec; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1809 | bool got_module_spec = false; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1810 | if (process) |
| 1811 | { |
| 1812 | // Try to get module information from the process |
| 1813 | if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec)) |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame^] | 1814 | got_module_spec = true; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | if (!got_module_spec) |
| 1818 | { |
| 1819 | // Get module information from a target. |
| 1820 | if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec)) |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1821 | return module_resolver (module_spec); |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1822 | } |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1823 | |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1824 | // Trying to find a module by UUID on local file system. |
| 1825 | const auto error = module_resolver (resolved_module_spec); |
| 1826 | if (error.Fail ()) |
| 1827 | { |
| 1828 | if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr)) |
| 1829 | return Error (); |
| 1830 | } |
| 1831 | |
| 1832 | return error; |
| 1833 | } |
| 1834 | |
| 1835 | bool |
| 1836 | Platform::GetCachedSharedModule (const ModuleSpec &module_spec, |
| 1837 | lldb::ModuleSP &module_sp, |
| 1838 | bool *did_create_ptr) |
| 1839 | { |
| 1840 | if (IsHost() || |
| 1841 | !GetGlobalPlatformProperties ()->GetUseModuleCache ()) |
| 1842 | return false; |
| 1843 | |
| 1844 | Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM); |
| 1845 | |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1846 | // Check local cache for a module. |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1847 | auto error = m_module_cache->GetAndPut ( |
| 1848 | GetModuleCacheRoot (), |
| 1849 | GetCacheHostname (), |
| 1850 | module_spec, |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame^] | 1851 | [this](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec) |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1852 | { |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1853 | return DownloadModuleSlice (module_spec.GetFileSpec (), |
| 1854 | module_spec.GetObjectOffset (), |
| 1855 | module_spec.GetObjectSize (), |
| 1856 | tmp_download_file_spec); |
| 1857 | |
| 1858 | }, |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame^] | 1859 | [this](const ModuleSP& module_sp, const FileSpec& tmp_download_file_spec) |
| 1860 | { |
| 1861 | return DownloadSymbolFile (module_sp, tmp_download_file_spec); |
| 1862 | }, |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1863 | module_sp, |
| 1864 | did_create_ptr); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1865 | if (error.Success ()) |
| 1866 | return true; |
| 1867 | |
| 1868 | if (log) |
| 1869 | log->Printf("Platform::%s - module %s not found in local cache: %s", |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1870 | __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ()); |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1871 | return false; |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | Error |
| 1875 | Platform::DownloadModuleSlice (const FileSpec& src_file_spec, |
| 1876 | const uint64_t src_offset, |
| 1877 | const uint64_t src_size, |
| 1878 | const FileSpec& dst_file_spec) |
| 1879 | { |
| 1880 | Error error; |
| 1881 | |
| 1882 | std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary); |
| 1883 | if (!dst.is_open()) |
| 1884 | { |
| 1885 | error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ()); |
| 1886 | return error; |
| 1887 | } |
| 1888 | |
| 1889 | auto src_fd = OpenFile (src_file_spec, |
| 1890 | File::eOpenOptionRead, |
| 1891 | lldb::eFilePermissionsFileDefault, |
| 1892 | error); |
| 1893 | |
| 1894 | if (error.Fail ()) |
| 1895 | { |
| 1896 | error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ()); |
| 1897 | return error; |
| 1898 | } |
| 1899 | |
| 1900 | std::vector<char> buffer (1024); |
| 1901 | auto offset = src_offset; |
| 1902 | uint64_t total_bytes_read = 0; |
| 1903 | while (total_bytes_read < src_size) |
| 1904 | { |
| 1905 | const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read); |
| 1906 | const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error); |
| 1907 | if (error.Fail ()) |
| 1908 | break; |
| 1909 | if (n_read == 0) |
| 1910 | { |
| 1911 | error.SetErrorString ("read 0 bytes"); |
| 1912 | break; |
| 1913 | } |
| 1914 | offset += n_read; |
| 1915 | total_bytes_read += n_read; |
| 1916 | dst.write (&buffer[0], n_read); |
| 1917 | } |
| 1918 | |
| 1919 | Error close_error; |
| 1920 | CloseFile (src_fd, close_error); // Ignoring close error. |
| 1921 | |
| 1922 | return error; |
| 1923 | } |
| 1924 | |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame^] | 1925 | Error |
| 1926 | Platform::DownloadSymbolFile (const lldb::ModuleSP& module_sp, const FileSpec& dst_file_spec) |
| 1927 | { |
| 1928 | return Error ("Symbol file downloading not supported by the default platform."); |
| 1929 | } |
| 1930 | |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1931 | FileSpec |
| 1932 | Platform::GetModuleCacheRoot () |
| 1933 | { |
| 1934 | auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory (); |
| 1935 | dir_spec.AppendPathComponent (GetName ().AsCString ()); |
| 1936 | return dir_spec; |
| 1937 | } |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 1938 | |
| 1939 | const char * |
| 1940 | Platform::GetCacheHostname () |
| 1941 | { |
| 1942 | return GetHostname (); |
| 1943 | } |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 1944 | |
| 1945 | const UnixSignalsSP & |
| 1946 | Platform::GetRemoteUnixSignals() |
| 1947 | { |
| 1948 | static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>(); |
| 1949 | return s_default_unix_signals_sp; |
| 1950 | } |
| 1951 | |
| 1952 | const UnixSignalsSP & |
| 1953 | Platform::GetUnixSignals() |
| 1954 | { |
| 1955 | if (IsHost()) |
| 1956 | return Host::GetUnixSignals(); |
| 1957 | return GetRemoteUnixSignals(); |
| 1958 | } |