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 | |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 66 | #define LLDB_PROPERTIES_platform |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame^] | 67 | #include "TargetProperties.inc" |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 68 | |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 69 | enum { |
| 70 | #define LLDB_PROPERTIES_platform |
Jordan Rupprecht | 6a253d3 | 2019-07-29 17:22:10 +0000 | [diff] [blame^] | 71 | #include "TargetPropertiesEnum.inc" |
Jonas Devlieghere | 971f9ca | 2019-07-25 21:36:37 +0000 | [diff] [blame] | 72 | }; |
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()); |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 83 | m_collection_sp->Initialize(g_platform_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( |
Jonas Devlieghere | a8ea595 | 2019-07-29 16:41:30 +0000 | [diff] [blame] | 102 | nullptr, idx, g_platform_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)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 389 | LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 392 | /// Destructor. |
| 393 | /// |
| 394 | /// The destructor is virtual since this class is designed to be |
| 395 | /// inherited from by the plug-in instance. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | Platform::~Platform() { |
| 397 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 398 | LLDB_LOGF(log, "%p Platform::~Platform()", static_cast<void *>(this)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 401 | void Platform::GetStatus(Stream &strm) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | std::string s; |
| 403 | strm.Printf(" Platform: %s\n", GetPluginName().GetCString()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 404 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | ArchSpec arch(GetSystemArchitecture()); |
| 406 | if (arch.IsValid()) { |
| 407 | if (!arch.GetTriple().str().empty()) { |
| 408 | strm.Printf(" Triple: "); |
| 409 | arch.DumpTriple(strm); |
| 410 | strm.EOL(); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 411 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 412 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 413 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 414 | llvm::VersionTuple os_version = GetOSVersion(); |
| 415 | if (!os_version.empty()) { |
| 416 | strm.Format("OS Version: {0}", os_version.getAsString()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 417 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | if (GetOSBuildString(s)) |
| 419 | strm.Printf(" (%s)", s.c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | strm.EOL(); |
| 422 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 423 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 424 | if (GetOSKernelDescription(s)) |
| 425 | strm.Printf(" Kernel: %s\n", s.c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 426 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 427 | if (IsHost()) { |
| 428 | strm.Printf(" Hostname: %s\n", GetHostname()); |
| 429 | } else { |
| 430 | const bool is_connected = IsConnected(); |
| 431 | if (is_connected) |
| 432 | strm.Printf(" Hostname: %s\n", GetHostname()); |
| 433 | strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no"); |
| 434 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 435 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 436 | if (GetWorkingDirectory()) { |
| 437 | strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString()); |
| 438 | } |
| 439 | if (!IsConnected()) |
| 440 | return; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 441 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | std::string specific_info(GetPlatformSpecificConnectionInformation()); |
| 443 | |
| 444 | if (!specific_info.empty()) |
| 445 | strm.Printf("Platform-specific connection: %s\n", specific_info.c_str()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 448 | llvm::VersionTuple Platform::GetOSVersion(Process *process) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | std::lock_guard<std::mutex> guard(m_mutex); |
Greg Clayton | 7597b35 | 2015-02-02 20:45:17 +0000 | [diff] [blame] | 450 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 451 | if (IsHost()) { |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 452 | if (m_os_version.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 453 | // We have a local host platform |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 454 | m_os_version = HostInfo::GetOSVersion(); |
| 455 | m_os_version_set_while_connected = !m_os_version.empty(); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 456 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 457 | } else { |
| 458 | // We have a remote platform. We can only fetch the remote |
| 459 | // OS version if we are connected, and we don't want to do it |
| 460 | // more than once. |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 461 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | const bool is_connected = IsConnected(); |
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 | bool fetch = false; |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 465 | if (!m_os_version.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 466 | // We have valid OS version info, check to make sure it wasn't manually |
| 467 | // set prior to connecting. If it was manually set prior to connecting, |
| 468 | // 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] | 469 | if (is_connected && !m_os_version_set_while_connected) |
| 470 | fetch = true; |
| 471 | } else { |
| 472 | // We don't have valid OS version info, fetch it if we are connected |
| 473 | fetch = is_connected; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 476 | if (fetch) |
| 477 | m_os_version_set_while_connected = GetRemoteOSVersion(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 480 | if (!m_os_version.empty()) |
| 481 | return m_os_version; |
| 482 | if (process) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 483 | // Check with the process in case it can answer the question if a process |
| 484 | // was provided |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 485 | return process->GetHostOSVersion(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 486 | } |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 487 | return llvm::VersionTuple(); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 488 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 489 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 490 | bool Platform::GetOSBuildString(std::string &s) { |
| 491 | s.clear(); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 492 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | if (IsHost()) |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 494 | #if !defined(__linux__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | return HostInfo::GetOSBuildString(s); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 496 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 497 | return false; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 498 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | else |
| 500 | return GetRemoteOSBuildString(s); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 503 | bool Platform::GetOSKernelDescription(std::string &s) { |
| 504 | if (IsHost()) |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 505 | #if !defined(__linux__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | return HostInfo::GetOSKernelDescription(s); |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 507 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 508 | return false; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 509 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 510 | else |
| 511 | return GetRemoteOSKernelDescription(s); |
| 512 | } |
| 513 | |
| 514 | void Platform::AddClangModuleCompilationOptions( |
| 515 | Target *target, std::vector<std::string> &options) { |
| 516 | std::vector<std::string> default_compilation_options = { |
| 517 | "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"}; |
| 518 | |
| 519 | options.insert(options.end(), default_compilation_options.begin(), |
| 520 | default_compilation_options.end()); |
| 521 | } |
| 522 | |
| 523 | FileSpec Platform::GetWorkingDirectory() { |
| 524 | if (IsHost()) { |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 525 | llvm::SmallString<64> cwd; |
| 526 | if (llvm::sys::fs::current_path(cwd)) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 527 | return {}; |
| 528 | else { |
| 529 | FileSpec file_spec(cwd); |
| 530 | FileSystem::Instance().Resolve(file_spec); |
| 531 | return file_spec; |
| 532 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 533 | } else { |
| 534 | if (!m_working_dir) |
| 535 | m_working_dir = GetRemoteWorkingDirectory(); |
| 536 | return m_working_dir; |
| 537 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | struct RecurseCopyBaton { |
| 541 | const FileSpec &dst; |
| 542 | Platform *platform_ptr; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 543 | Status error; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 544 | }; |
| 545 | |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 546 | static FileSystem::EnumerateDirectoryResult |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 547 | RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft, |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 548 | llvm::StringRef path) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 550 | FileSpec src(path); |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 551 | namespace fs = llvm::sys::fs; |
| 552 | switch (ft) { |
| 553 | case fs::file_type::fifo_file: |
| 554 | case fs::file_type::socket_file: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 555 | // 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] | 556 | return FileSystem::eEnumerateDirectoryResultNext; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 557 | break; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 558 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 559 | case fs::file_type::directory_file: { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | // make the new directory and get in there |
| 561 | FileSpec dst_dir = rc_baton->dst; |
| 562 | if (!dst_dir.GetFilename()) |
| 563 | dst_dir.GetFilename() = src.GetLastPathComponent(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 564 | Status error = rc_baton->platform_ptr->MakeDirectory( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 565 | dst_dir, lldb::eFilePermissionsDirectoryDefault); |
| 566 | if (error.Fail()) { |
| 567 | rc_baton->error.SetErrorStringWithFormat( |
| 568 | "unable to setup directory %s on remote end", dst_dir.GetCString()); |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 569 | return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 570 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 571 | |
| 572 | // now recurse |
| 573 | std::string src_dir_path(src.GetPath()); |
| 574 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 575 | // Make a filespec that only fills in the directory of a FileSpec so when |
| 576 | // we enumerate we can quickly fill in the filename for dst copies |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | FileSpec recurse_dst; |
| 578 | recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 579 | RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr, |
| 580 | Status()}; |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 581 | FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true, |
| 582 | RecurseCopy_Callback, &rc_baton2); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 583 | if (rc_baton2.error.Fail()) { |
| 584 | rc_baton->error.SetErrorString(rc_baton2.error.AsCString()); |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 585 | return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | } |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 587 | return FileSystem::eEnumerateDirectoryResultNext; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | } break; |
| 589 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 590 | case fs::file_type::symlink_file: { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 591 | // copy the file and keep going |
| 592 | FileSpec dst_file = rc_baton->dst; |
| 593 | if (!dst_file.GetFilename()) |
| 594 | dst_file.GetFilename() = src.GetFilename(); |
| 595 | |
| 596 | FileSpec src_resolved; |
| 597 | |
Jonas Devlieghere | 4637696 | 2018-10-31 21:49:27 +0000 | [diff] [blame] | 598 | rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 599 | |
| 600 | if (rc_baton->error.Fail()) |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 601 | return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | |
| 603 | rc_baton->error = |
| 604 | rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved); |
| 605 | |
| 606 | if (rc_baton->error.Fail()) |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 607 | return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 608 | |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 609 | return FileSystem::eEnumerateDirectoryResultNext; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 610 | } break; |
| 611 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 612 | case fs::file_type::regular_file: { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 613 | // copy the file and keep going |
| 614 | FileSpec dst_file = rc_baton->dst; |
| 615 | if (!dst_file.GetFilename()) |
| 616 | dst_file.GetFilename() = src.GetFilename(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 617 | Status err = rc_baton->platform_ptr->PutFile(src, dst_file); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 618 | if (err.Fail()) { |
| 619 | rc_baton->error.SetErrorString(err.AsCString()); |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 620 | return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 621 | } |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 622 | return FileSystem::eEnumerateDirectoryResultNext; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 623 | } break; |
| 624 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 625 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 626 | rc_baton->error.SetErrorStringWithFormat( |
| 627 | "invalid file detected during copy: %s", src.GetPath().c_str()); |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 628 | return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 631 | llvm_unreachable("Unhandled file_type!"); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 634 | Status Platform::Install(const FileSpec &src, const FileSpec &dst) { |
| 635 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 636 | |
| 637 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 638 | LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')", |
| 639 | src.GetPath().c_str(), dst.GetPath().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 640 | FileSpec fixed_dst(dst); |
| 641 | |
| 642 | if (!fixed_dst.GetFilename()) |
| 643 | fixed_dst.GetFilename() = src.GetFilename(); |
| 644 | |
| 645 | FileSpec working_dir = GetWorkingDirectory(); |
| 646 | |
| 647 | if (dst) { |
| 648 | if (dst.GetDirectory()) { |
| 649 | const char first_dst_dir_char = dst.GetDirectory().GetCString()[0]; |
| 650 | if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') { |
| 651 | fixed_dst.GetDirectory() = dst.GetDirectory(); |
| 652 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 653 | // If the fixed destination file doesn't have a directory yet, then we |
| 654 | // must have a relative path. We will resolve this relative path against |
| 655 | // the platform's working directory |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | if (!fixed_dst.GetDirectory()) { |
| 657 | FileSpec relative_spec; |
| 658 | std::string path; |
| 659 | if (working_dir) { |
| 660 | relative_spec = working_dir; |
| 661 | relative_spec.AppendPathComponent(dst.GetPath()); |
| 662 | fixed_dst.GetDirectory() = relative_spec.GetDirectory(); |
| 663 | } else { |
| 664 | error.SetErrorStringWithFormat( |
| 665 | "platform working directory must be valid for relative path '%s'", |
| 666 | dst.GetPath().c_str()); |
| 667 | return error; |
| 668 | } |
| 669 | } |
| 670 | } else { |
| 671 | if (working_dir) { |
| 672 | fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); |
| 673 | } else { |
| 674 | error.SetErrorStringWithFormat( |
| 675 | "platform working directory must be valid for relative path '%s'", |
| 676 | dst.GetPath().c_str()); |
| 677 | return error; |
| 678 | } |
| 679 | } |
| 680 | } else { |
| 681 | if (working_dir) { |
| 682 | fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); |
| 683 | } else { |
| 684 | error.SetErrorStringWithFormat("platform working directory must be valid " |
| 685 | "when destination directory is empty"); |
| 686 | return error; |
| 687 | } |
| 688 | } |
| 689 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 690 | LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'", |
| 691 | src.GetPath().c_str(), dst.GetPath().c_str(), |
| 692 | fixed_dst.GetPath().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 693 | |
| 694 | if (GetSupportsRSync()) { |
| 695 | error = PutFile(src, dst); |
| 696 | } else { |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 697 | namespace fs = llvm::sys::fs; |
| 698 | switch (fs::get_file_type(src.GetPath(), false)) { |
| 699 | case fs::file_type::directory_file: { |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 700 | llvm::sys::fs::remove(fixed_dst.GetPath()); |
Jonas Devlieghere | 7c5310b | 2018-11-01 15:47:33 +0000 | [diff] [blame] | 701 | uint32_t permissions = FileSystem::Instance().GetPermissions(src); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 702 | if (permissions == 0) |
| 703 | permissions = eFilePermissionsDirectoryDefault; |
| 704 | error = MakeDirectory(fixed_dst, permissions); |
| 705 | if (error.Success()) { |
| 706 | // Make a filespec that only fills in the directory of a FileSpec so |
| 707 | // when we enumerate we can quickly fill in the filename for dst copies |
| 708 | FileSpec recurse_dst; |
| 709 | recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString()); |
| 710 | std::string src_dir_path(src.GetPath()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 711 | RecurseCopyBaton baton = {recurse_dst, this, Status()}; |
Jonas Devlieghere | 35e4c84 | 2018-11-01 00:33:27 +0000 | [diff] [blame] | 712 | FileSystem::Instance().EnumerateDirectory( |
| 713 | src_dir_path, true, true, true, RecurseCopy_Callback, &baton); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 714 | return baton.error; |
| 715 | } |
| 716 | } break; |
| 717 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 718 | case fs::file_type::regular_file: |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 719 | llvm::sys::fs::remove(fixed_dst.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 720 | error = PutFile(src, fixed_dst); |
| 721 | break; |
| 722 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 723 | case fs::file_type::symlink_file: { |
Zachary Turner | 07db3f7 | 2017-03-21 05:47:57 +0000 | [diff] [blame] | 724 | llvm::sys::fs::remove(fixed_dst.GetPath()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 725 | FileSpec src_resolved; |
Jonas Devlieghere | 4637696 | 2018-10-31 21:49:27 +0000 | [diff] [blame] | 726 | error = FileSystem::Instance().Readlink(src, src_resolved); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 727 | if (error.Success()) |
| 728 | error = CreateSymlink(dst, src_resolved); |
| 729 | } break; |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 730 | case fs::file_type::fifo_file: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 731 | error.SetErrorString("platform install doesn't handle pipes"); |
| 732 | break; |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 733 | case fs::file_type::socket_file: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 734 | error.SetErrorString("platform install doesn't handle sockets"); |
| 735 | break; |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 736 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 737 | error.SetErrorString( |
| 738 | "platform install doesn't handle non file or directory items"); |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | return error; |
| 743 | } |
| 744 | |
| 745 | bool Platform::SetWorkingDirectory(const FileSpec &file_spec) { |
| 746 | if (IsHost()) { |
| 747 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
Pavel Labath | 2d0c5b0 | 2017-01-25 11:10:52 +0000 | [diff] [blame] | 748 | LLDB_LOG(log, "{0}", file_spec); |
| 749 | if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) { |
| 750 | LLDB_LOG(log, "error: {0}", ec.message()); |
| 751 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 752 | } |
Pavel Labath | 2d0c5b0 | 2017-01-25 11:10:52 +0000 | [diff] [blame] | 753 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 754 | } else { |
| 755 | m_working_dir.Clear(); |
| 756 | return SetRemoteWorkingDirectory(file_spec); |
| 757 | } |
| 758 | } |
| 759 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 760 | Status Platform::MakeDirectory(const FileSpec &file_spec, |
| 761 | uint32_t permissions) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 762 | if (IsHost()) |
Zachary Turner | d3d95fd | 2017-03-19 05:48:47 +0000 | [diff] [blame] | 763 | return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 764 | else { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 765 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 766 | error.SetErrorStringWithFormat("remote platform %s doesn't support %s", |
| 767 | GetPluginName().GetCString(), |
| 768 | LLVM_PRETTY_FUNCTION); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 769 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 770 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 773 | Status Platform::GetFilePermissions(const FileSpec &file_spec, |
| 774 | uint32_t &file_permissions) { |
Zachary Turner | 6934e0a | 2017-03-19 05:49:43 +0000 | [diff] [blame] | 775 | if (IsHost()) { |
| 776 | auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath()); |
| 777 | if (Value) |
| 778 | file_permissions = Value.get(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 779 | return Status(Value.getError()); |
Zachary Turner | 6934e0a | 2017-03-19 05:49:43 +0000 | [diff] [blame] | 780 | } else { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 781 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | error.SetErrorStringWithFormat("remote platform %s doesn't support %s", |
| 783 | GetPluginName().GetCString(), |
| 784 | LLVM_PRETTY_FUNCTION); |
| 785 | return error; |
| 786 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 789 | Status Platform::SetFilePermissions(const FileSpec &file_spec, |
| 790 | uint32_t file_permissions) { |
Zachary Turner | 6934e0a | 2017-03-19 05:49:43 +0000 | [diff] [blame] | 791 | if (IsHost()) { |
| 792 | auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions); |
| 793 | return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms); |
| 794 | } else { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 795 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 796 | error.SetErrorStringWithFormat("remote platform %s doesn't support %s", |
| 797 | GetPluginName().GetCString(), |
| 798 | LLVM_PRETTY_FUNCTION); |
| 799 | return error; |
| 800 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 803 | ConstString Platform::GetName() { return GetPluginName(); } |
| 804 | |
| 805 | const char *Platform::GetHostname() { |
| 806 | if (IsHost()) |
| 807 | return "127.0.0.1"; |
| 808 | |
| 809 | if (m_name.empty()) |
| 810 | return nullptr; |
| 811 | return m_name.c_str(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | ConstString Platform::GetFullNameForDylib(ConstString basename) { |
| 815 | return basename; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 818 | bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) { |
| 819 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 820 | LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')", |
| 821 | working_dir.GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 822 | m_working_dir = working_dir; |
| 823 | return true; |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 826 | bool Platform::SetOSVersion(llvm::VersionTuple version) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 827 | if (IsHost()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 828 | // We don't need anyone setting the OS version for the host platform, we |
| 829 | // should be able to figure it out by calling HostInfo::GetOSVersion(...). |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 830 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 831 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 832 | // We have a remote platform, allow setting the target OS version if we |
| 833 | // 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] | 834 | // request the remote OS version from the connected platform. |
| 835 | if (IsConnected()) |
| 836 | return false; |
| 837 | else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 838 | // We aren't connected and we might want to set the OS version ahead of |
| 839 | // time before we connect so we can peruse files and use a local SDK or |
| 840 | // PDK cache of support files to disassemble or do other things. |
Pavel Labath | 2272c48 | 2018-06-18 15:02:23 +0000 | [diff] [blame] | 841 | m_os_version = version; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | return true; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 843 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 844 | } |
| 845 | return false; |
| 846 | } |
| 847 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 848 | Status |
| 849 | Platform::ResolveExecutable(const ModuleSpec &module_spec, |
| 850 | lldb::ModuleSP &exe_module_sp, |
| 851 | const FileSpecList *module_search_paths_ptr) { |
| 852 | Status error; |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 853 | if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 854 | if (module_spec.GetArchitecture().IsValid()) { |
| 855 | error = ModuleList::GetSharedModule(module_spec, exe_module_sp, |
| 856 | module_search_paths_ptr, nullptr, |
| 857 | nullptr); |
| 858 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 859 | // No valid architecture was specified, ask the platform for the |
| 860 | // architectures that we should be using (in the correct order) and see |
| 861 | // if we can find a match that way |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 862 | ModuleSpec arch_module_spec(module_spec); |
| 863 | for (uint32_t idx = 0; GetSupportedArchitectureAtIndex( |
| 864 | idx, arch_module_spec.GetArchitecture()); |
| 865 | ++idx) { |
| 866 | error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp, |
| 867 | module_search_paths_ptr, nullptr, |
| 868 | nullptr); |
| 869 | // Did we find an executable using one of the |
| 870 | if (error.Success() && exe_module_sp) |
| 871 | break; |
| 872 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 873 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 874 | } else { |
| 875 | error.SetErrorStringWithFormat("'%s' does not exist", |
| 876 | module_spec.GetFileSpec().GetPath().c_str()); |
| 877 | } |
| 878 | return error; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 881 | Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec, |
| 882 | FileSpec &sym_file) { |
| 883 | Status error; |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 884 | if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 885 | sym_file = sym_spec.GetSymbolFileSpec(); |
| 886 | else |
| 887 | error.SetErrorString("unable to resolve symbol file"); |
| 888 | return error; |
Greg Clayton | aa51684 | 2011-08-11 16:25:18 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 891 | bool Platform::ResolveRemotePath(const FileSpec &platform_path, |
| 892 | FileSpec &resolved_platform_path) { |
| 893 | resolved_platform_path = platform_path; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 894 | FileSystem::Instance().Resolve(resolved_platform_path); |
| 895 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | const ArchSpec &Platform::GetSystemArchitecture() { |
| 899 | if (IsHost()) { |
| 900 | if (!m_system_arch.IsValid()) { |
| 901 | // We have a local host platform |
| 902 | m_system_arch = HostInfo::GetArchitecture(); |
| 903 | m_system_arch_set_while_connected = m_system_arch.IsValid(); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 904 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 905 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 906 | // We have a remote platform. We can only fetch the remote system |
| 907 | // architecture if we are connected, and we don't want to do it more than |
| 908 | // once. |
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 | const bool is_connected = IsConnected(); |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 911 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 912 | bool fetch = false; |
| 913 | if (m_system_arch.IsValid()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 914 | // We have valid OS version info, check to make sure it wasn't manually |
| 915 | // set prior to connecting. If it was manually set prior to connecting, |
| 916 | // 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] | 917 | if (is_connected && !m_system_arch_set_while_connected) |
| 918 | fetch = true; |
| 919 | } else { |
| 920 | // We don't have valid OS version info, fetch it if we are connected |
| 921 | fetch = is_connected; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 922 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 923 | |
| 924 | if (fetch) { |
| 925 | m_system_arch = GetRemoteSystemArchitecture(); |
| 926 | m_system_arch_set_while_connected = m_system_arch.IsValid(); |
| 927 | } |
| 928 | } |
| 929 | return m_system_arch; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame] | 932 | ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) { |
| 933 | if (triple.empty()) |
| 934 | return ArchSpec(); |
| 935 | llvm::Triple normalized_triple(llvm::Triple::normalize(triple)); |
| 936 | if (!ArchSpec::ContainsOnlyArch(normalized_triple)) |
| 937 | return ArchSpec(triple); |
| 938 | |
Pavel Labath | 4ebb64b | 2017-11-13 15:57:20 +0000 | [diff] [blame] | 939 | if (auto kind = HostInfo::ParseArchitectureKind(triple)) |
| 940 | return HostInfo::GetArchitecture(*kind); |
| 941 | |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame] | 942 | ArchSpec compatible_arch; |
| 943 | ArchSpec raw_arch(triple); |
| 944 | if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch)) |
| 945 | return raw_arch; |
| 946 | |
| 947 | if (!compatible_arch.IsValid()) |
| 948 | return ArchSpec(normalized_triple); |
| 949 | |
| 950 | const llvm::Triple &compatible_triple = compatible_arch.GetTriple(); |
| 951 | if (normalized_triple.getVendorName().empty()) |
| 952 | normalized_triple.setVendor(compatible_triple.getVendor()); |
| 953 | if (normalized_triple.getOSName().empty()) |
| 954 | normalized_triple.setOS(compatible_triple.getOS()); |
| 955 | if (normalized_triple.getEnvironmentName().empty()) |
| 956 | normalized_triple.setEnvironment(compatible_triple.getEnvironment()); |
| 957 | return ArchSpec(normalized_triple); |
| 958 | } |
| 959 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 960 | Status Platform::ConnectRemote(Args &args) { |
| 961 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 962 | if (IsHost()) |
| 963 | error.SetErrorStringWithFormat("The currently selected platform (%s) is " |
| 964 | "the host platform and is always connected.", |
| 965 | GetPluginName().GetCString()); |
| 966 | else |
| 967 | error.SetErrorStringWithFormat( |
| 968 | "Platform::ConnectRemote() is not supported by %s", |
| 969 | GetPluginName().GetCString()); |
| 970 | return error; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 973 | Status Platform::DisconnectRemote() { |
| 974 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 975 | if (IsHost()) |
| 976 | error.SetErrorStringWithFormat("The currently selected platform (%s) is " |
| 977 | "the host platform and is always connected.", |
| 978 | GetPluginName().GetCString()); |
| 979 | else |
| 980 | error.SetErrorStringWithFormat( |
| 981 | "Platform::DisconnectRemote() is not supported by %s", |
| 982 | GetPluginName().GetCString()); |
| 983 | return error; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 984 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 985 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 986 | bool Platform::GetProcessInfo(lldb::pid_t pid, |
| 987 | ProcessInstanceInfo &process_info) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 988 | // Take care of the host case so that each subclass can just call this |
| 989 | // function to get the host functionality. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 990 | if (IsHost()) |
| 991 | return Host::GetProcessInfo(pid, process_info); |
| 992 | return false; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 995 | uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info, |
| 996 | ProcessInstanceInfoList &process_infos) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 997 | // Take care of the host case so that each subclass can just call this |
| 998 | // function to get the host functionality. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 999 | uint32_t match_count = 0; |
| 1000 | if (IsHost()) |
| 1001 | match_count = Host::FindProcesses(match_info, process_infos); |
| 1002 | return match_count; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1003 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1004 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1005 | Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) { |
| 1006 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1007 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1008 | LLDB_LOGF(log, "Platform::%s()", __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1009 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1010 | // Take care of the host case so that each subclass can just call this |
| 1011 | // function to get the host functionality. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1012 | if (IsHost()) { |
| 1013 | if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY")) |
| 1014 | launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY); |
| 1015 | |
| 1016 | if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) { |
| 1017 | const bool is_localhost = true; |
| 1018 | const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); |
| 1019 | const bool first_arg_is_full_shell_command = false; |
| 1020 | uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info); |
| 1021 | if (log) { |
| 1022 | const FileSpec &shell = launch_info.GetShell(); |
Jason Molenda | 3b68761 | 2019-02-08 23:36:25 +0000 | [diff] [blame] | 1023 | std::string shell_str = (shell) ? shell.GetPath() : "<null>"; |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1024 | LLDB_LOGF(log, |
| 1025 | "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 |
| 1026 | ", shell is '%s'", |
| 1027 | __FUNCTION__, num_resumes, shell_str.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (!launch_info.ConvertArgumentsForLaunchingInShell( |
| 1031 | error, is_localhost, will_debug, first_arg_is_full_shell_command, |
| 1032 | num_resumes)) |
| 1033 | return error; |
| 1034 | } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) { |
| 1035 | error = ShellExpandArguments(launch_info); |
| 1036 | if (error.Fail()) { |
| 1037 | error.SetErrorStringWithFormat("shell expansion failed (reason: %s). " |
| 1038 | "consider launching with 'process " |
| 1039 | "launch'.", |
| 1040 | error.AsCString("unknown")); |
| 1041 | return error; |
| 1042 | } |
| 1043 | } |
| 1044 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1045 | LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32, |
| 1046 | __FUNCTION__, launch_info.GetResumeCount()); |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1047 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1048 | error = Host::LaunchProcess(launch_info); |
| 1049 | } else |
| 1050 | error.SetErrorString( |
| 1051 | "base lldb_private::Platform class can't launch remote processes"); |
| 1052 | return error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1055 | Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1056 | if (IsHost()) |
| 1057 | return Host::ShellExpandArguments(launch_info); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1058 | return Status("base lldb_private::Platform class can't expand arguments"); |
Enrico Granata | 83a1437 | 2015-02-20 21:48:38 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1061 | Status Platform::KillProcess(const lldb::pid_t pid) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1062 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1063 | LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid); |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1064 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1065 | // 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] | 1066 | // fall back to the default OS implementation. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1067 | size_t num_debuggers = Debugger::GetNumDebuggers(); |
| 1068 | for (size_t didx = 0; didx < num_debuggers; ++didx) { |
| 1069 | DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx); |
| 1070 | lldb_private::TargetList &targets = debugger->GetTargetList(); |
| 1071 | for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) { |
| 1072 | ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP(); |
| 1073 | if (process->GetID() == pid) |
| 1074 | return process->Destroy(true); |
Zachary Turner | 7271bab | 2015-05-13 19:44:24 +0000 | [diff] [blame] | 1075 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1076 | } |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1077 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1078 | if (!IsHost()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1079 | return Status( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1080 | "base lldb_private::Platform class can't kill remote processes unless " |
| 1081 | "they are controlled by a process plugin"); |
| 1082 | } |
| 1083 | Host::Kill(pid, SIGTERM); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1084 | return Status(); |
Oleksiy Vyalov | 1ef7b2c | 2015-02-04 23:19:15 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1087 | lldb::ProcessSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1088 | Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger, |
| 1089 | Target *target, // Can be nullptr, if nullptr create a |
| 1090 | // new target, else use existing one |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1091 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1092 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1093 | LLDB_LOGF(log, "Platform::%s entered (target %p)", __FUNCTION__, |
| 1094 | static_cast<void *>(target)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1095 | |
| 1096 | ProcessSP process_sp; |
| 1097 | // Make sure we stop at the entry point |
| 1098 | launch_info.GetFlags().Set(eLaunchFlagDebug); |
| 1099 | // 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] | 1100 | // group, since then we can handle ^C interrupts ourselves w/o having to |
| 1101 | // worry about the target getting them as well. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1102 | launch_info.SetLaunchInSeparateProcessGroup(true); |
| 1103 | |
| 1104 | // Allow any StructuredData process-bound plugins to adjust the launch info |
| 1105 | // if needed |
| 1106 | size_t i = 0; |
| 1107 | bool iteration_complete = false; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1108 | // Note iteration can't simply go until a nullptr callback is returned, as it |
| 1109 | // is valid for a plugin to not supply a filter. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1110 | auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex; |
| 1111 | for (auto filter_callback = get_filter_func(i, iteration_complete); |
| 1112 | !iteration_complete; |
| 1113 | filter_callback = get_filter_func(++i, iteration_complete)) { |
| 1114 | if (filter_callback) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1115 | // Give this ProcessLaunchInfo filter a chance to adjust the launch info. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1116 | error = (*filter_callback)(launch_info, target); |
| 1117 | if (!error.Success()) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1118 | LLDB_LOGF(log, |
| 1119 | "Platform::%s() StructuredDataPlugin launch " |
| 1120 | "filter failed.", |
| 1121 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1122 | return process_sp; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | error = LaunchProcess(launch_info); |
| 1128 | if (error.Success()) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1129 | LLDB_LOGF(log, |
| 1130 | "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", |
| 1131 | __FUNCTION__, launch_info.GetProcessID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1132 | if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) { |
| 1133 | ProcessAttachInfo attach_info(launch_info); |
| 1134 | process_sp = Attach(attach_info, debugger, target, error); |
| 1135 | if (process_sp) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1136 | LLDB_LOGF(log, "Platform::%s Attach() succeeded, Process plugin: %s", |
| 1137 | __FUNCTION__, process_sp->GetPluginName().AsCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1138 | launch_info.SetHijackListener(attach_info.GetHijackListener()); |
Todd Fiala | ac33cc9 | 2014-10-09 01:02:08 +0000 | [diff] [blame] | 1139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1140 | // Since we attached to the process, it will think it needs to detach |
| 1141 | // if the process object just goes away without an explicit call to |
| 1142 | // Process::Kill() or Process::Detach(), so let it know to kill the |
| 1143 | // process if this happens. |
| 1144 | process_sp->SetShouldDetach(false); |
| 1145 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1146 | // If we didn't have any file actions, the pseudo terminal might have |
| 1147 | // been used where the slave side was given as the file to open for |
| 1148 | // stdin/out/err after we have already opened the master so we can |
| 1149 | // read/write stdin/out/err. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); |
Pavel Labath | 07d6f88 | 2017-12-11 10:09:14 +0000 | [diff] [blame] | 1151 | if (pty_fd != PseudoTerminal::invalid_fd) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1152 | process_sp->SetSTDIOFileDescriptor(pty_fd); |
| 1153 | } |
| 1154 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1155 | LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__, |
| 1156 | error.AsCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1159 | LLDB_LOGF(log, |
| 1160 | "Platform::%s LaunchProcess() returned launch_info with " |
| 1161 | "invalid process id", |
| 1162 | __FUNCTION__); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1165 | LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__, |
| 1166 | error.AsCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | return process_sp; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1170 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1171 | |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1172 | lldb::PlatformSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1173 | Platform::GetPlatformForArchitecture(const ArchSpec &arch, |
| 1174 | ArchSpec *platform_arch_ptr) { |
| 1175 | lldb::PlatformSP platform_sp; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1176 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1177 | if (arch.IsValid()) |
| 1178 | platform_sp = Platform::Create(arch, platform_arch_ptr, error); |
| 1179 | return platform_sp; |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1182 | /// Lets a platform answer if it is compatible with a given |
| 1183 | /// architecture and the target triple contained within. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1184 | bool Platform::IsCompatibleArchitecture(const ArchSpec &arch, |
| 1185 | bool exact_arch_match, |
| 1186 | ArchSpec *compatible_arch_ptr) { |
| 1187 | // If the architecture is invalid, we must answer true... |
| 1188 | if (arch.IsValid()) { |
| 1189 | ArchSpec platform_arch; |
| 1190 | // Try for an exact architecture match first. |
| 1191 | if (exact_arch_match) { |
| 1192 | for (uint32_t arch_idx = 0; |
| 1193 | GetSupportedArchitectureAtIndex(arch_idx, platform_arch); |
| 1194 | ++arch_idx) { |
| 1195 | if (arch.IsExactMatch(platform_arch)) { |
| 1196 | if (compatible_arch_ptr) |
| 1197 | *compatible_arch_ptr = platform_arch; |
| 1198 | return true; |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 1199 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1200 | } |
| 1201 | } else { |
| 1202 | for (uint32_t arch_idx = 0; |
| 1203 | GetSupportedArchitectureAtIndex(arch_idx, platform_arch); |
| 1204 | ++arch_idx) { |
| 1205 | if (arch.IsCompatibleMatch(platform_arch)) { |
| 1206 | if (compatible_arch_ptr) |
| 1207 | *compatible_arch_ptr = platform_arch; |
| 1208 | return true; |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 1209 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1210 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1211 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1212 | } |
| 1213 | if (compatible_arch_ptr) |
| 1214 | compatible_arch_ptr->Clear(); |
| 1215 | return false; |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1218 | Status Platform::PutFile(const FileSpec &source, const FileSpec &destination, |
| 1219 | uint32_t uid, uint32_t gid) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1220 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1221 | LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n"); |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1223 | uint32_t source_open_options = |
| 1224 | File::eOpenOptionRead | File::eOpenOptionCloseOnExec; |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 1225 | namespace fs = llvm::sys::fs; |
| 1226 | if (fs::is_symlink_file(source.GetPath())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1227 | source_open_options |= File::eOpenOptionDontFollowSymlinks; |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1228 | |
Jonas Devlieghere | 50bc1ed | 2018-11-02 22:34:51 +0000 | [diff] [blame] | 1229 | File source_file; |
| 1230 | Status error = FileSystem::Instance().Open( |
| 1231 | source_file, source, source_open_options, lldb::eFilePermissionsUserRW); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1232 | uint32_t permissions = source_file.GetPermissions(error); |
| 1233 | if (permissions == 0) |
| 1234 | permissions = lldb::eFilePermissionsFileDefault; |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1236 | if (!source_file.IsValid()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1237 | return Status("PutFile: unable to open source file"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1238 | lldb::user_id_t dest_file = OpenFile( |
| 1239 | destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite | |
| 1240 | File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec, |
| 1241 | permissions, error); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1242 | LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file); |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1244 | if (error.Fail()) |
| 1245 | return error; |
| 1246 | if (dest_file == UINT64_MAX) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1247 | return Status("unable to open target file"); |
Jason Molenda | eb9902a | 2018-12-07 00:35:26 +0000 | [diff] [blame] | 1248 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1249 | uint64_t offset = 0; |
| 1250 | for (;;) { |
| 1251 | size_t bytes_read = buffer_sp->GetByteSize(); |
| 1252 | error = source_file.Read(buffer_sp->GetBytes(), bytes_read); |
| 1253 | if (error.Fail() || bytes_read == 0) |
| 1254 | break; |
| 1255 | |
| 1256 | const uint64_t bytes_written = |
| 1257 | WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error); |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1258 | if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1259 | break; |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1260 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1261 | offset += bytes_written; |
| 1262 | if (bytes_written != bytes_read) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1263 | // We didn't write the correct number of bytes, so adjust the file |
| 1264 | // position in the source file we are reading from... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1265 | source_file.SeekFromStart(offset); |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1266 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1267 | } |
| 1268 | CloseFile(dest_file, error); |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1270 | if (uid == UINT32_MAX && gid == UINT32_MAX) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1271 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1272 | |
| 1273 | // TODO: ChownFile? |
| 1274 | |
| 1275 | return error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1278 | Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) { |
| 1279 | Status error("unimplemented"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1280 | return error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1283 | Status |
| 1284 | Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src |
| 1285 | const FileSpec &dst) // The symlink points to dst |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1286 | { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1287 | Status error("unimplemented"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1288 | return error; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1291 | bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) { |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1295 | Status Platform::Unlink(const FileSpec &path) { |
| 1296 | Status error("unimplemented"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1297 | return error; |
| 1298 | } |
| 1299 | |
Ed Maste | 37c40af | 2017-08-16 12:55:02 +0000 | [diff] [blame] | 1300 | MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr, |
| 1301 | addr_t length, unsigned prot, |
| 1302 | unsigned flags, addr_t fd, |
| 1303 | addr_t offset) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1304 | uint64_t flags_platform = 0; |
| 1305 | if (flags & eMmapFlagsPrivate) |
| 1306 | flags_platform |= MAP_PRIVATE; |
| 1307 | if (flags & eMmapFlagsAnon) |
| 1308 | flags_platform |= MAP_ANON; |
Ed Maste | 37c40af | 2017-08-16 12:55:02 +0000 | [diff] [blame] | 1309 | |
| 1310 | MmapArgList args({addr, length, prot, flags_platform, fd, offset}); |
| 1311 | return args; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1314 | lldb_private::Status Platform::RunShellCommand( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1315 | const char *command, // Shouldn't be nullptr |
| 1316 | const FileSpec & |
| 1317 | working_dir, // Pass empty FileSpec to use the current working directory |
| 1318 | int *status_ptr, // Pass nullptr if you don't want the process exit status |
| 1319 | int *signo_ptr, // Pass nullptr if you don't want the signal that caused the |
| 1320 | // process to exit |
| 1321 | std::string |
| 1322 | *command_output, // Pass nullptr if you don't want the command output |
Pavel Labath | 19dd1a0 | 2018-05-10 10:46:03 +0000 | [diff] [blame] | 1323 | const Timeout<std::micro> &timeout) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1324 | if (IsHost()) |
| 1325 | return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr, |
Pavel Labath | 19dd1a0 | 2018-05-10 10:46:03 +0000 | [diff] [blame] | 1326 | command_output, timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1327 | else |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1328 | return Status("unimplemented"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low, |
| 1332 | uint64_t &high) { |
Zachary Turner | 076a259 | 2017-03-20 23:54:54 +0000 | [diff] [blame] | 1333 | if (!IsHost()) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1334 | return false; |
Zachary Turner | 076a259 | 2017-03-20 23:54:54 +0000 | [diff] [blame] | 1335 | auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath()); |
| 1336 | if (!Result) |
| 1337 | return false; |
| 1338 | std::tie(high, low) = Result->words(); |
| 1339 | return true; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1342 | void Platform::SetLocalCacheDirectory(const char *local) { |
| 1343 | m_local_cache_directory.assign(local); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1346 | const char *Platform::GetLocalCacheDirectory() { |
| 1347 | return m_local_cache_directory.c_str(); |
Robert Flack | 96ad3de | 2015-05-09 15:53:31 +0000 | [diff] [blame] | 1348 | } |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1349 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1350 | static constexpr OptionDefinition g_rsync_option_table[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1351 | {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1352 | {}, 0, eArgTypeNone, "Enable rsync."}, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1353 | {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1354 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1355 | "Platform-specific options required for rsync to work."}, |
| 1356 | {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1357 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1358 | "Platform-specific rsync prefix put before the remote path."}, |
| 1359 | {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1360 | OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1361 | "Do not automatically fill in the remote hostname when composing the " |
| 1362 | "rsync command."}, |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1363 | }; |
| 1364 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1365 | static constexpr OptionDefinition g_ssh_option_table[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1366 | {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1367 | {}, 0, eArgTypeNone, "Enable SSH."}, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1368 | {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument, |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1369 | nullptr, {}, 0, eArgTypeCommandName, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1370 | "Platform-specific options required for SSH to work."}, |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1371 | }; |
| 1372 | |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1373 | static constexpr OptionDefinition g_caching_option_table[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1374 | {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c', |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 1375 | OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1376 | "Path in which to store local copies of files."}, |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1377 | }; |
| 1378 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1379 | llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1380 | return llvm::makeArrayRef(g_rsync_option_table); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1383 | void OptionGroupPlatformRSync::OptionParsingStarting( |
| 1384 | ExecutionContext *execution_context) { |
| 1385 | m_rsync = false; |
| 1386 | m_rsync_opts.clear(); |
| 1387 | m_rsync_prefix.clear(); |
| 1388 | m_ignores_remote_hostname = false; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1391 | lldb_private::Status |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 1392 | OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx, |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 1393 | llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1394 | ExecutionContext *execution_context) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1395 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1396 | char short_option = (char)GetDefinitions()[option_idx].short_option; |
| 1397 | switch (short_option) { |
| 1398 | case 'r': |
| 1399 | m_rsync = true; |
| 1400 | break; |
| 1401 | |
| 1402 | case 'R': |
| 1403 | m_rsync_opts.assign(option_arg); |
| 1404 | break; |
| 1405 | |
| 1406 | case 'P': |
| 1407 | m_rsync_prefix.assign(option_arg); |
| 1408 | break; |
| 1409 | |
| 1410 | case 'i': |
| 1411 | m_ignores_remote_hostname = true; |
| 1412 | break; |
| 1413 | |
| 1414 | default: |
| 1415 | error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); |
| 1416 | break; |
| 1417 | } |
| 1418 | |
| 1419 | return error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 1422 | lldb::BreakpointSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1423 | Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) { |
| 1424 | return lldb::BreakpointSP(); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 1425 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 1426 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1427 | llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1428 | return llvm::makeArrayRef(g_ssh_option_table); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1431 | void OptionGroupPlatformSSH::OptionParsingStarting( |
| 1432 | ExecutionContext *execution_context) { |
| 1433 | m_ssh = false; |
| 1434 | m_ssh_opts.clear(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1437 | lldb_private::Status |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 1438 | OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx, |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 1439 | llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1440 | ExecutionContext *execution_context) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1441 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1442 | char short_option = (char)GetDefinitions()[option_idx].short_option; |
| 1443 | switch (short_option) { |
| 1444 | case 's': |
| 1445 | m_ssh = true; |
| 1446 | break; |
| 1447 | |
| 1448 | case 'S': |
| 1449 | m_ssh_opts.assign(option_arg); |
| 1450 | break; |
| 1451 | |
| 1452 | default: |
| 1453 | error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); |
| 1454 | break; |
| 1455 | } |
| 1456 | |
| 1457 | return error; |
| 1458 | } |
| 1459 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1460 | llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1461 | return llvm::makeArrayRef(g_caching_option_table); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | void OptionGroupPlatformCaching::OptionParsingStarting( |
| 1465 | ExecutionContext *execution_context) { |
| 1466 | m_cache_dir.clear(); |
| 1467 | } |
| 1468 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1469 | lldb_private::Status OptionGroupPlatformCaching::SetOptionValue( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 1470 | uint32_t option_idx, llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1471 | ExecutionContext *execution_context) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1472 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | char short_option = (char)GetDefinitions()[option_idx].short_option; |
| 1474 | switch (short_option) { |
| 1475 | case 'c': |
| 1476 | m_cache_dir.assign(option_arg); |
| 1477 | break; |
| 1478 | |
| 1479 | default: |
| 1480 | error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); |
| 1481 | break; |
| 1482 | } |
| 1483 | |
| 1484 | return error; |
| 1485 | } |
| 1486 | |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 1487 | Environment Platform::GetEnvironment() { return Environment(); } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1488 | |
| 1489 | const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() { |
| 1490 | if (!m_calculated_trap_handlers) { |
| 1491 | std::lock_guard<std::mutex> guard(m_mutex); |
| 1492 | if (!m_calculated_trap_handlers) { |
| 1493 | CalculateTrapHandlerSymbolNames(); |
| 1494 | m_calculated_trap_handlers = true; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1495 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1496 | } |
| 1497 | return m_trap_handlers; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1500 | Status Platform::GetCachedExecutable( |
| 1501 | ModuleSpec &module_spec, lldb::ModuleSP &module_sp, |
| 1502 | const FileSpecList *module_search_paths_ptr, Platform &remote_platform) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1503 | const auto platform_spec = module_spec.GetFileSpec(); |
| 1504 | const auto error = LoadCachedExecutable( |
| 1505 | module_spec, module_sp, module_search_paths_ptr, remote_platform); |
| 1506 | if (error.Success()) { |
| 1507 | module_spec.GetFileSpec() = module_sp->GetFileSpec(); |
| 1508 | module_spec.GetPlatformFileSpec() = platform_spec; |
| 1509 | } |
| 1510 | |
| 1511 | return error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1514 | Status Platform::LoadCachedExecutable( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1515 | const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, |
| 1516 | const FileSpecList *module_search_paths_ptr, Platform &remote_platform) { |
| 1517 | return GetRemoteSharedModule(module_spec, nullptr, module_sp, |
| 1518 | [&](const ModuleSpec &spec) { |
| 1519 | return remote_platform.ResolveExecutable( |
| 1520 | spec, module_sp, module_search_paths_ptr); |
| 1521 | }, |
| 1522 | nullptr); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1525 | Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec, |
| 1526 | Process *process, |
| 1527 | lldb::ModuleSP &module_sp, |
| 1528 | const ModuleResolver &module_resolver, |
| 1529 | bool *did_create_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1530 | // Get module information from a target. |
| 1531 | ModuleSpec resolved_module_spec; |
| 1532 | bool got_module_spec = false; |
| 1533 | if (process) { |
| 1534 | // Try to get module information from the process |
| 1535 | if (process->GetModuleSpec(module_spec.GetFileSpec(), |
| 1536 | module_spec.GetArchitecture(), |
| 1537 | resolved_module_spec)) { |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 1538 | if (!module_spec.GetUUID().IsValid() || |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1539 | module_spec.GetUUID() == resolved_module_spec.GetUUID()) { |
| 1540 | got_module_spec = true; |
| 1541 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1542 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1543 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1544 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 1545 | if (!module_spec.GetArchitecture().IsValid()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1546 | Status error; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1547 | // No valid architecture was specified, ask the platform for the |
| 1548 | // architectures that we should be using (in the correct order) and see if |
| 1549 | // we can find a match that way |
Jason Molenda | 3fce2fd | 2016-10-05 02:29:13 +0000 | [diff] [blame] | 1550 | ModuleSpec arch_module_spec(module_spec); |
| 1551 | for (uint32_t idx = 0; GetSupportedArchitectureAtIndex( |
| 1552 | idx, arch_module_spec.GetArchitecture()); |
| 1553 | ++idx) { |
| 1554 | error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr, |
| 1555 | nullptr, nullptr); |
| 1556 | // Did we find an executable using one of the |
| 1557 | if (error.Success() && module_sp) |
| 1558 | break; |
| 1559 | } |
| 1560 | if (module_sp) |
| 1561 | got_module_spec = true; |
| 1562 | } |
| 1563 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1564 | if (!got_module_spec) { |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1565 | // Get module information from a target. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1566 | if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(), |
| 1567 | resolved_module_spec)) { |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 1568 | if (!module_spec.GetUUID().IsValid() || |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1569 | module_spec.GetUUID() == resolved_module_spec.GetUUID()) { |
| 1570 | return module_resolver(module_spec); |
| 1571 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1572 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1573 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 1574 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1575 | // If we are looking for a specific UUID, make sure resolved_module_spec has |
| 1576 | // the same one before we search. |
| 1577 | if (module_spec.GetUUID().IsValid()) { |
| 1578 | resolved_module_spec.GetUUID() = module_spec.GetUUID(); |
| 1579 | } |
Jason Molenda | 85967fa | 2016-07-12 03:25:22 +0000 | [diff] [blame] | 1580 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1581 | // Trying to find a module by UUID on local file system. |
| 1582 | const auto error = module_resolver(resolved_module_spec); |
| 1583 | if (error.Fail()) { |
| 1584 | if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr)) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1585 | return Status(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1586 | } |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1587 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1588 | return error; |
Oleksiy Vyalov | 037f6b9 | 2015-03-24 23:45:49 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1591 | bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec, |
| 1592 | lldb::ModuleSP &module_sp, |
| 1593 | bool *did_create_ptr) { |
| 1594 | if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() || |
| 1595 | !GetGlobalPlatformProperties()->GetModuleCacheDirectory()) |
Oleksiy Vyalov | 280d8dc | 2015-04-15 14:35:10 +0000 | [diff] [blame] | 1596 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1597 | |
| 1598 | Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); |
| 1599 | |
| 1600 | // Check local cache for a module. |
| 1601 | auto error = m_module_cache->GetAndPut( |
| 1602 | GetModuleCacheRoot(), GetCacheHostname(), module_spec, |
| 1603 | [this](const ModuleSpec &module_spec, |
| 1604 | const FileSpec &tmp_download_file_spec) { |
| 1605 | return DownloadModuleSlice( |
| 1606 | module_spec.GetFileSpec(), module_spec.GetObjectOffset(), |
| 1607 | module_spec.GetObjectSize(), tmp_download_file_spec); |
| 1608 | |
| 1609 | }, |
| 1610 | [this](const ModuleSP &module_sp, |
| 1611 | const FileSpec &tmp_download_file_spec) { |
| 1612 | return DownloadSymbolFile(module_sp, tmp_download_file_spec); |
| 1613 | }, |
| 1614 | module_sp, did_create_ptr); |
| 1615 | if (error.Success()) |
| 1616 | return true; |
| 1617 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1618 | LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s", |
| 1619 | __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(), |
| 1620 | error.AsCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1621 | return false; |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1624 | Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec, |
| 1625 | const uint64_t src_offset, |
| 1626 | const uint64_t src_size, |
| 1627 | const FileSpec &dst_file_spec) { |
| 1628 | Status error; |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1629 | |
Pavel Labath | e3ad2e2 | 2017-03-21 13:49:50 +0000 | [diff] [blame] | 1630 | std::error_code EC; |
| 1631 | llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::F_None); |
| 1632 | if (EC) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1633 | error.SetErrorStringWithFormat("unable to open destination file: %s", |
| 1634 | dst_file_spec.GetPath().c_str()); |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1635 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1636 | } |
Oleksiy Vyalov | 63acdfd | 2015-03-10 01:15:28 +0000 | [diff] [blame] | 1637 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1638 | auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead, |
| 1639 | lldb::eFilePermissionsFileDefault, error); |
Tamas Berghammer | ec3f92a | 2015-08-12 11:10:25 +0000 | [diff] [blame] | 1640 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1641 | if (error.Fail()) { |
| 1642 | error.SetErrorStringWithFormat("unable to open source file: %s", |
| 1643 | error.AsCString()); |
| 1644 | return error; |
| 1645 | } |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 1646 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1647 | std::vector<char> buffer(1024); |
| 1648 | auto offset = src_offset; |
| 1649 | uint64_t total_bytes_read = 0; |
| 1650 | while (total_bytes_read < src_size) { |
| 1651 | const auto to_read = std::min(static_cast<uint64_t>(buffer.size()), |
| 1652 | src_size - total_bytes_read); |
| 1653 | const uint64_t n_read = |
| 1654 | ReadFile(src_fd, offset, &buffer[0], to_read, error); |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1655 | if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1656 | break; |
| 1657 | if (n_read == 0) { |
| 1658 | error.SetErrorString("read 0 bytes"); |
| 1659 | break; |
| 1660 | } |
| 1661 | offset += n_read; |
| 1662 | total_bytes_read += n_read; |
| 1663 | dst.write(&buffer[0], n_read); |
| 1664 | } |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1665 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1666 | Status close_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1667 | CloseFile(src_fd, close_error); // Ignoring close error. |
| 1668 | |
| 1669 | return error; |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1672 | Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp, |
| 1673 | const FileSpec &dst_file_spec) { |
| 1674 | return Status( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1675 | "Symbol file downloading not supported by the default platform."); |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1676 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1677 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1678 | FileSpec Platform::GetModuleCacheRoot() { |
| 1679 | auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory(); |
| 1680 | dir_spec.AppendPathComponent(GetName().AsCString()); |
| 1681 | return dir_spec; |
| 1682 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1683 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1684 | const char *Platform::GetCacheHostname() { return GetHostname(); } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1685 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1686 | const UnixSignalsSP &Platform::GetRemoteUnixSignals() { |
| 1687 | static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>(); |
| 1688 | return s_default_unix_signals_sp; |
| 1689 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1690 | |
Zachary Turner | a89ce43 | 2019-03-06 18:20:23 +0000 | [diff] [blame] | 1691 | UnixSignalsSP Platform::GetUnixSignals() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1692 | if (IsHost()) |
Zachary Turner | a89ce43 | 2019-03-06 18:20:23 +0000 | [diff] [blame] | 1693 | return UnixSignals::CreateForHost(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1694 | return GetRemoteUnixSignals(); |
| 1695 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1696 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1697 | uint32_t Platform::LoadImage(lldb_private::Process *process, |
| 1698 | const lldb_private::FileSpec &local_file, |
| 1699 | const lldb_private::FileSpec &remote_file, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1700 | lldb_private::Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1701 | if (local_file && remote_file) { |
| 1702 | // Both local and remote file was specified. Install the local file to the |
| 1703 | // given location. |
| 1704 | if (IsRemote() || local_file != remote_file) { |
| 1705 | error = Install(local_file, remote_file); |
| 1706 | if (error.Fail()) |
| 1707 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1708 | } |
Jim Ingham | 0d231f7 | 2018-06-28 20:02:11 +0000 | [diff] [blame] | 1709 | return DoLoadImage(process, remote_file, nullptr, error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1710 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1712 | if (local_file) { |
| 1713 | // Only local file was specified. Install it to the current working |
| 1714 | // directory. |
| 1715 | FileSpec target_file = GetWorkingDirectory(); |
| 1716 | target_file.AppendPathComponent(local_file.GetFilename().AsCString()); |
| 1717 | if (IsRemote() || local_file != target_file) { |
| 1718 | error = Install(local_file, target_file); |
| 1719 | if (error.Fail()) |
| 1720 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1721 | } |
Jim Ingham | 0d231f7 | 2018-06-28 20:02:11 +0000 | [diff] [blame] | 1722 | return DoLoadImage(process, target_file, nullptr, error); |
Jonas Devlieghere | 463a48e | 2019-07-26 18:14:08 +0000 | [diff] [blame] | 1723 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1724 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1725 | if (remote_file) { |
| 1726 | // 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] | 1727 | return DoLoadImage(process, remote_file, nullptr, error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1728 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1730 | error.SetErrorString("Neither local nor remote file was specified"); |
| 1731 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1732 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1733 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1734 | uint32_t Platform::DoLoadImage(lldb_private::Process *process, |
| 1735 | const lldb_private::FileSpec &remote_file, |
Jim Ingham | 0d231f7 | 2018-06-28 20:02:11 +0000 | [diff] [blame] | 1736 | const std::vector<std::string> *paths, |
| 1737 | lldb_private::Status &error, |
| 1738 | lldb_private::FileSpec *loaded_image) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1739 | error.SetErrorString("LoadImage is not supported on the current platform"); |
| 1740 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1741 | } |
Ulrich Weigand | bb00d0b | 2016-04-14 14:28:34 +0000 | [diff] [blame] | 1742 | |
Jim Ingham | 0d231f7 | 2018-06-28 20:02:11 +0000 | [diff] [blame] | 1743 | uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process, |
| 1744 | const lldb_private::FileSpec &remote_filename, |
| 1745 | const std::vector<std::string> &paths, |
| 1746 | lldb_private::Status &error, |
| 1747 | lldb_private::FileSpec *loaded_path) |
| 1748 | { |
| 1749 | FileSpec file_to_use; |
| 1750 | if (remote_filename.IsAbsolute()) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1751 | file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(), |
| 1752 | |
| 1753 | remote_filename.GetPathStyle()); |
Jim Ingham | 0d231f7 | 2018-06-28 20:02:11 +0000 | [diff] [blame] | 1754 | else |
| 1755 | file_to_use = remote_filename; |
Jonas Devlieghere | 463a48e | 2019-07-26 18:14:08 +0000 | [diff] [blame] | 1756 | |
Jim Ingham | 0d231f7 | 2018-06-28 20:02:11 +0000 | [diff] [blame] | 1757 | return DoLoadImage(process, file_to_use, &paths, error, loaded_path); |
| 1758 | } |
| 1759 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1760 | Status Platform::UnloadImage(lldb_private::Process *process, |
| 1761 | uint32_t image_token) { |
| 1762 | return Status("UnloadImage is not supported on the current platform"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1763 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1764 | |
Zachary Turner | 3165945 | 2016-11-17 21:15:14 +0000 | [diff] [blame] | 1765 | lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url, |
| 1766 | llvm::StringRef plugin_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1767 | lldb_private::Debugger &debugger, |
| 1768 | lldb_private::Target *target, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1769 | lldb_private::Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1770 | error.Clear(); |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1771 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1772 | if (!target) { |
Jason Molenda | df9f796 | 2018-10-23 23:45:56 +0000 | [diff] [blame] | 1773 | ArchSpec arch; |
| 1774 | if (target && target->GetArchitecture().IsValid()) |
| 1775 | arch = target->GetArchitecture(); |
| 1776 | else |
| 1777 | arch = Target::GetDefaultArchitecture(); |
| 1778 | |
| 1779 | const char *triple = ""; |
| 1780 | if (arch.IsValid()) |
| 1781 | triple = arch.GetTriple().getTriple().c_str(); |
| 1782 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1783 | TargetSP new_target_sp; |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 1784 | error = debugger.GetTargetList().CreateTarget( |
Jason Molenda | df9f796 | 2018-10-23 23:45:56 +0000 | [diff] [blame] | 1785 | debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1786 | target = new_target_sp.get(); |
| 1787 | } |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1788 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1789 | if (!target || error.Fail()) |
| 1790 | return nullptr; |
| 1791 | |
| 1792 | debugger.GetTargetList().SetSelectedTarget(target); |
| 1793 | |
| 1794 | lldb::ProcessSP process_sp = |
| 1795 | target->CreateProcess(debugger.GetListener(), plugin_name, nullptr); |
| 1796 | if (!process_sp) |
| 1797 | return nullptr; |
| 1798 | |
| 1799 | error = |
| 1800 | process_sp->ConnectRemote(debugger.GetOutputFile().get(), connect_url); |
| 1801 | if (error.Fail()) |
| 1802 | return nullptr; |
| 1803 | |
| 1804 | return process_sp; |
| 1805 | } |
| 1806 | |
| 1807 | size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1808 | lldb_private::Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1809 | error.Clear(); |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target, |
| 1814 | BreakpointSite *bp_site) { |
| 1815 | ArchSpec arch = target.GetArchitecture(); |
| 1816 | const uint8_t *trap_opcode = nullptr; |
| 1817 | size_t trap_opcode_size = 0; |
| 1818 | |
| 1819 | switch (arch.GetMachine()) { |
| 1820 | case llvm::Triple::aarch64: { |
| 1821 | static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; |
| 1822 | trap_opcode = g_aarch64_opcode; |
| 1823 | trap_opcode_size = sizeof(g_aarch64_opcode); |
| 1824 | } break; |
| 1825 | |
| 1826 | // TODO: support big-endian arm and thumb trap codes. |
| 1827 | case llvm::Triple::arm: { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1828 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the |
| 1829 | // linux kernel does otherwise. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1830 | static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; |
| 1831 | static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; |
| 1832 | |
| 1833 | lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0)); |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1834 | AddressClass addr_class = AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1835 | |
| 1836 | if (bp_loc_sp) { |
| 1837 | addr_class = bp_loc_sp->GetAddress().GetAddressClass(); |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1838 | if (addr_class == AddressClass::eUnknown && |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1839 | (bp_loc_sp->GetAddress().GetFileAddress() & 1)) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1840 | addr_class = AddressClass::eCodeAlternateISA; |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1843 | if (addr_class == AddressClass::eCodeAlternateISA) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1844 | trap_opcode = g_thumb_breakpoint_opcode; |
| 1845 | trap_opcode_size = sizeof(g_thumb_breakpoint_opcode); |
| 1846 | } else { |
| 1847 | trap_opcode = g_arm_breakpoint_opcode; |
| 1848 | trap_opcode_size = sizeof(g_arm_breakpoint_opcode); |
| 1849 | } |
| 1850 | } break; |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1851 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1852 | case llvm::Triple::mips: |
| 1853 | case llvm::Triple::mips64: { |
| 1854 | static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d}; |
| 1855 | trap_opcode = g_hex_opcode; |
| 1856 | trap_opcode_size = sizeof(g_hex_opcode); |
| 1857 | } break; |
| 1858 | |
| 1859 | case llvm::Triple::mipsel: |
| 1860 | case llvm::Triple::mips64el: { |
| 1861 | static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00}; |
| 1862 | trap_opcode = g_hex_opcode; |
| 1863 | trap_opcode_size = sizeof(g_hex_opcode); |
| 1864 | } break; |
| 1865 | |
| 1866 | case llvm::Triple::systemz: { |
| 1867 | static const uint8_t g_hex_opcode[] = {0x00, 0x01}; |
| 1868 | trap_opcode = g_hex_opcode; |
| 1869 | trap_opcode_size = sizeof(g_hex_opcode); |
| 1870 | } break; |
| 1871 | |
| 1872 | case llvm::Triple::hexagon: { |
| 1873 | static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54}; |
| 1874 | trap_opcode = g_hex_opcode; |
| 1875 | trap_opcode_size = sizeof(g_hex_opcode); |
| 1876 | } break; |
| 1877 | |
| 1878 | case llvm::Triple::ppc: |
| 1879 | case llvm::Triple::ppc64: { |
| 1880 | static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08}; |
| 1881 | trap_opcode = g_ppc_opcode; |
| 1882 | trap_opcode_size = sizeof(g_ppc_opcode); |
| 1883 | } break; |
| 1884 | |
Eugene Zemtsov | aae0a75 | 2017-10-05 19:44:05 +0000 | [diff] [blame] | 1885 | case llvm::Triple::ppc64le: { |
| 1886 | static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap |
| 1887 | trap_opcode = g_ppc64le_opcode; |
| 1888 | trap_opcode_size = sizeof(g_ppc64le_opcode); |
| 1889 | } break; |
| 1890 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1891 | case llvm::Triple::x86: |
| 1892 | case llvm::Triple::x86_64: { |
| 1893 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 1894 | trap_opcode = g_i386_opcode; |
| 1895 | trap_opcode_size = sizeof(g_i386_opcode); |
| 1896 | } break; |
| 1897 | |
| 1898 | default: |
David Blaikie | a322f36 | 2017-01-06 00:38:06 +0000 | [diff] [blame] | 1899 | llvm_unreachable( |
| 1900 | "Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | assert(bp_site); |
| 1904 | if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) |
| 1905 | return trap_opcode_size; |
| 1906 | |
| 1907 | return 0; |
Aidan Dodds | 933d8db | 2016-02-22 17:29:56 +0000 | [diff] [blame] | 1908 | } |