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