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