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