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