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