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