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