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