blob: b5b673a33e44b902242cc3fe6e128c63b8af1d83 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- Platform.cpp ------------------------------------------------------===//
Greg Claytone996fd32011-03-08 22:40:15 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Greg Claytone996fd32011-03-08 22:40:15 +00006//
7//===----------------------------------------------------------------------===//
8
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00009#include <algorithm>
Pavel Labathbc4987b2018-04-05 16:59:36 +000010#include <csignal>
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000011#include <fstream>
Jonas Devlieghere796ac802019-02-11 23:13:08 +000012#include <memory>
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000013#include <vector>
14
Greg Clayton4116e932012-05-15 02:33:01 +000015#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000016#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000017#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000018#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000020#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000021#include "lldb/Core/StreamFile.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000022#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000023#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000024#include "lldb/Host/HostInfo.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000025#include "lldb/Host/OptionParser.h"
Tatyana Krasnukhafe74df02020-03-12 21:48:23 +030026#include "lldb/Interpreter/OptionValueFileSpec.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000027#include "lldb/Interpreter/OptionValueProperties.h"
28#include "lldb/Interpreter/Property.h"
29#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000030#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000031#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000032#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000033#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000034#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000035#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000036#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000037#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000038#include "lldb/Utility/Status.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000039#include "lldb/Utility/StructuredData.h"
Zachary Turner7d86ee52017-03-08 17:56:08 +000040#include "llvm/Support/FileSystem.h"
Jonas Devlieghere706cccb2020-07-14 08:44:40 -070041#include "llvm/Support/Path.h"
Zachary Turner7d86ee52017-03-08 17:56:08 +000042
Adrian Prantl05097242018-04-30 16:49:04 +000043// Define these constants from POSIX mman.h rather than include the file so
44// that they will be correct even when compiled on Linux.
Robert Flack96ad3de2015-05-09 15:53:31 +000045#define MAP_PRIVATE 2
46#define MAP_ANON 0x1000
47
Greg Claytone996fd32011-03-08 22:40:15 +000048using namespace lldb;
49using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000050
51static uint32_t g_initialize_count = 0;
52
Adrian Prantl05097242018-04-30 16:49:04 +000053// Use a singleton function for g_local_platform_sp to avoid init constructors
54// since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000055static PlatformSP &GetHostPlatformSP() {
56 static PlatformSP g_platform_sp;
57 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000058}
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000061
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000062namespace {
63
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000064#define LLDB_PROPERTIES_platform
Jordan Rupprecht6a253d32019-07-29 17:22:10 +000065#include "TargetProperties.inc"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000066
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000067enum {
68#define LLDB_PROPERTIES_platform
Jordan Rupprecht6a253d32019-07-29 17:22:10 +000069#include "TargetPropertiesEnum.inc"
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000070};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074ConstString PlatformProperties::GetSettingName() {
75 static ConstString g_setting_name("platform");
76 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000077}
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079PlatformProperties::PlatformProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +000080 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Jonas Devliegherea8ea5952019-07-29 16:41:30 +000081 m_collection_sp->Initialize(g_platform_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 auto module_cache_dir = GetModuleCacheDirectory();
84 if (module_cache_dir)
85 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 llvm::SmallString<64> user_home_dir;
Jonas Devlieghere921c1b72020-08-20 11:36:32 -070088 if (!FileSystem::Instance().GetHomeDirectory(user_home_dir))
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000090
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000091 module_cache_dir = FileSpec(user_home_dir.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 module_cache_dir.AppendPathComponent(".lldb");
93 module_cache_dir.AppendPathComponent("module_cache");
Tatyana Krasnukhafe74df02020-03-12 21:48:23 +030094 SetDefaultModuleCacheDirectory(module_cache_dir);
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098bool PlatformProperties::GetUseModuleCache() const {
99 const auto idx = ePropertyUseModuleCache;
100 return m_collection_sp->GetPropertyAtIndexAsBoolean(
Jonas Devliegherea8ea5952019-07-29 16:41:30 +0000101 nullptr, idx, g_platform_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
105 return m_collection_sp->SetPropertyAtIndexAsBoolean(
106 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109FileSpec PlatformProperties::GetModuleCacheDirectory() const {
110 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
111 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
115 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
116 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000117}
118
Tatyana Krasnukhafe74df02020-03-12 21:48:23 +0300119void PlatformProperties::SetDefaultModuleCacheDirectory(
120 const FileSpec &dir_spec) {
121 auto f_spec_opt = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(
122 nullptr, false, ePropertyModuleCacheDirectory);
123 assert(f_spec_opt);
124 f_spec_opt->SetDefaultValue(dir_spec);
125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000128///
129/// There should only be one of these for each host that LLDB runs
130/// upon that should be statically compiled in and registered using
131/// preprocessor macros or other similar build mechanisms.
132///
133/// This platform will be used as the default platform when launching
134/// or attaching to processes unless another platform is specified.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
136
137static std::vector<PlatformSP> &GetPlatformList() {
138 static std::vector<PlatformSP> g_platform_list;
139 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142static std::recursive_mutex &GetPlatformListMutex() {
143 static std::recursive_mutex g_mutex;
144 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000145}
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149void Platform::Terminate() {
150 if (g_initialize_count > 0) {
151 if (--g_initialize_count == 0) {
152 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
153 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000156}
157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
159 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
160 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
164 // The native platform should use its static void Platform::Initialize()
165 // function to register itself as the native platform.
166 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 if (platform_sp) {
169 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
170 GetPlatformList().push_back(platform_sp);
171 }
Greg Claytone996fd32011-03-08 22:40:15 +0000172}
173
Zachary Turner97206d52017-05-12 04:51:55 +0000174Status Platform::GetFileWithUUID(const FileSpec &platform_file,
175 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 // Default to the local case
177 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000178 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000179}
180
Greg Clayton91c0e742013-01-11 23:44:27 +0000181FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182Platform::LocateExecutableScriptingResources(Target *target, Module &module,
183 Stream *feedback_stream) {
184 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187// PlatformSP
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000188// Platform::FindPlugin (Process *process, ConstString plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000189//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000190// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000191// if (plugin_name)
192// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193// create_callback =
194// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000195// if (create_callback)
196// {
197// ArchSpec arch;
198// if (process)
199// {
200// arch = process->GetTarget().GetArchitecture();
201// }
202// PlatformSP platform_sp(create_callback(process, &arch));
203// if (platform_sp)
204// return platform_sp;
205// }
206// }
207// else
208// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209// for (uint32_t idx = 0; (create_callback =
210// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
211// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000212// {
213// PlatformSP platform_sp(create_callback(process, nullptr));
214// if (platform_sp)
215// return platform_sp;
216// }
217// }
218// return PlatformSP();
219//}
Jason Molenda1c627542013-04-05 01:03:25 +0000220
Joseph Tremoulet61bfc702020-10-30 15:12:10 -0400221Status Platform::GetSharedModule(
222 const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
223 const FileSpecList *module_search_paths_ptr,
224 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 if (IsHost())
Joseph Tremoulet61bfc702020-10-30 15:12:10 -0400226 return ModuleList::GetSharedModule(module_spec, module_sp,
227 module_search_paths_ptr, old_modules,
228 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000229
Pavel Labath1f8639a2018-08-28 16:32:46 +0000230 // Module resolver lambda.
231 auto resolver = [&](const ModuleSpec &spec) {
232 Status error(eErrorTypeGeneric);
233 ModuleSpec resolved_spec;
234 // Check if we have sysroot set.
235 if (m_sdk_sysroot) {
236 // Prepend sysroot to module spec.
237 resolved_spec = spec;
238 resolved_spec.GetFileSpec().PrependPathComponent(
239 m_sdk_sysroot.GetStringRef());
240 // Try to get shared module with resolved spec.
Joseph Tremoulet61bfc702020-10-30 15:12:10 -0400241 error = ModuleList::GetSharedModule(resolved_spec, module_sp,
242 module_search_paths_ptr, old_modules,
243 did_create_ptr, false);
Pavel Labath1f8639a2018-08-28 16:32:46 +0000244 }
245 // If we don't have sysroot or it didn't work then
246 // try original module spec.
247 if (!error.Success()) {
248 resolved_spec = spec;
Joseph Tremoulet61bfc702020-10-30 15:12:10 -0400249 error = ModuleList::GetSharedModule(resolved_spec, module_sp,
250 module_search_paths_ptr, old_modules,
251 did_create_ptr, false);
Pavel Labath1f8639a2018-08-28 16:32:46 +0000252 }
253 if (error.Success() && module_sp)
254 module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
255 return error;
256 };
257
258 return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000260}
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
263 const ArchSpec &arch, ModuleSpec &module_spec) {
264 ModuleSpecList module_specs;
265 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
266 module_specs) == 0)
267 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 ModuleSpec matched_module_spec;
270 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
271 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000272}
273
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000274PlatformSP Platform::Find(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 if (name) {
276 static ConstString g_host_platform_name("host");
277 if (name == g_host_platform_name)
278 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
281 for (const auto &platform_sp : GetPlatformList()) {
282 if (platform_sp->GetName() == name)
283 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000284 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 }
286 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000287}
288
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000289PlatformSP Platform::Create(ConstString name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 PlatformCreateInstance create_callback = nullptr;
291 lldb::PlatformSP platform_sp;
292 if (name) {
293 static ConstString g_host_platform_name("host");
294 if (name == g_host_platform_name)
295 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000296
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 create_callback =
298 PluginManager::GetPlatformCreateCallbackForPluginName(name);
299 if (create_callback)
300 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000301 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 error.SetErrorStringWithFormat(
303 "unable to find a plug-in for the platform named \"%s\"",
304 name.GetCString());
305 } else
306 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 if (platform_sp) {
309 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
310 GetPlatformList().push_back(platform_sp);
311 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000314}
315
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000317 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 lldb::PlatformSP platform_sp;
319 if (arch.IsValid()) {
320 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000321 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 // First try exact arch matches across all platforms already created
323 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
324 for (const auto &platform_sp : GetPlatformList()) {
325 if (platform_sp->IsCompatibleArchitecture(arch, true,
326 platform_arch_ptr))
327 return platform_sp;
328 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 // Next try compatible arch matches across all platforms already created
331 for (const auto &platform_sp : GetPlatformList()) {
332 if (platform_sp->IsCompatibleArchitecture(arch, false,
333 platform_arch_ptr))
334 return platform_sp;
335 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000336 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337
338 PlatformCreateInstance create_callback;
339 // First try exact arch matches across all platform plug-ins
340 uint32_t idx;
341 for (idx = 0; (create_callback =
342 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
343 ++idx) {
344 if (create_callback) {
345 platform_sp = create_callback(false, &arch);
346 if (platform_sp &&
347 platform_sp->IsCompatibleArchitecture(arch, true,
348 platform_arch_ptr)) {
349 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
350 GetPlatformList().push_back(platform_sp);
351 return platform_sp;
352 }
353 }
354 }
355 // Next try compatible arch matches across all platform plug-ins
356 for (idx = 0; (create_callback =
357 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
358 ++idx) {
359 if (create_callback) {
360 platform_sp = create_callback(false, &arch);
361 if (platform_sp &&
362 platform_sp->IsCompatibleArchitecture(arch, false,
363 platform_arch_ptr)) {
364 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
365 GetPlatformList().push_back(platform_sp);
366 return platform_sp;
367 }
368 }
369 }
370 } else
371 error.SetErrorString("invalid platform name");
372 if (platform_arch_ptr)
373 platform_arch_ptr->Clear();
374 platform_sp.reset();
375 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000376}
377
Pavel Labath7263f1b2017-10-31 10:56:03 +0000378ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
379 if (platform)
380 return platform->GetAugmentedArchSpec(triple);
381 return HostInfo::GetAugmentedArchSpec(triple);
382}
383
Greg Claytone996fd32011-03-08 22:40:15 +0000384/// Default Constructor
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000385Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 : m_is_host(is_host), m_os_version_set_while_connected(false),
387 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000388 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000389 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
390 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
391 m_ignores_remote_hostname(false), m_trap_handlers(),
392 m_calculated_trap_handlers(false),
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +0000393 m_module_cache(std::make_unique<ModuleCache>()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000395 LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000396}
397
Greg Claytone996fd32011-03-08 22:40:15 +0000398/// Destructor.
399///
400/// The destructor is virtual since this class is designed to be
401/// inherited from by the plug-in instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402Platform::~Platform() {
403 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000404 LLDB_LOGF(log, "%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000405}
406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 std::string s;
409 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 ArchSpec arch(GetSystemArchitecture());
412 if (arch.IsValid()) {
413 if (!arch.GetTriple().str().empty()) {
414 strm.Printf(" Triple: ");
Raphael Isemann2f1e7b32019-12-04 08:27:43 +0100415 arch.DumpTriple(strm.AsRawOstream());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000417 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000419
Pavel Labath2272c482018-06-18 15:02:23 +0000420 llvm::VersionTuple os_version = GetOSVersion();
421 if (!os_version.empty()) {
422 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 if (GetOSBuildString(s))
425 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 strm.EOL();
428 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000429
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 if (IsHost()) {
431 strm.Printf(" Hostname: %s\n", GetHostname());
432 } else {
433 const bool is_connected = IsConnected();
434 if (is_connected)
435 strm.Printf(" Hostname: %s\n", GetHostname());
436 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
437 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 if (GetWorkingDirectory()) {
440 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
441 }
442 if (!IsConnected())
443 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000444
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 std::string specific_info(GetPlatformSpecificConnectionInformation());
446
447 if (!specific_info.empty())
448 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Jonas Devlieghered3bdd512020-01-31 14:23:56 -0800449
450 if (GetOSKernelDescription(s))
451 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000452}
453
Pavel Labath2272c482018-06-18 15:02:23 +0000454llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000458 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000460 m_os_version = HostInfo::GetOSVersion();
461 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000462 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 } else {
464 // We have a remote platform. We can only fetch the remote
465 // OS version if we are connected, and we don't want to do it
466 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000471 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000472 // We have valid OS version info, check to make sure it wasn't manually
473 // set prior to connecting. If it was manually set prior to connecting,
474 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 if (is_connected && !m_os_version_set_while_connected)
476 fetch = true;
477 } else {
478 // We don't have valid OS version info, fetch it if we are connected
479 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000480 }
481
Pavel Labath2272c482018-06-18 15:02:23 +0000482 if (fetch)
483 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 }
485
Pavel Labath2272c482018-06-18 15:02:23 +0000486 if (!m_os_version.empty())
487 return m_os_version;
488 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000489 // Check with the process in case it can answer the question if a process
490 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000491 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 }
Pavel Labath2272c482018-06-18 15:02:23 +0000493 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000494}
Greg Clayton1cb64962011-03-24 04:28:38 +0000495
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496bool Platform::GetOSBuildString(std::string &s) {
497 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000500#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000502#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000504#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 else
506 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000507}
508
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509bool Platform::GetOSKernelDescription(std::string &s) {
510 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000511#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000513#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000515#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 else
517 return GetRemoteOSKernelDescription(s);
518}
519
520void Platform::AddClangModuleCompilationOptions(
521 Target *target, std::vector<std::string> &options) {
522 std::vector<std::string> default_compilation_options = {
523 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
524
525 options.insert(options.end(), default_compilation_options.begin(),
526 default_compilation_options.end());
527}
528
529FileSpec Platform::GetWorkingDirectory() {
530 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000531 llvm::SmallString<64> cwd;
532 if (llvm::sys::fs::current_path(cwd))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000533 return {};
534 else {
535 FileSpec file_spec(cwd);
536 FileSystem::Instance().Resolve(file_spec);
537 return file_spec;
538 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539 } else {
540 if (!m_working_dir)
541 m_working_dir = GetRemoteWorkingDirectory();
542 return m_working_dir;
543 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000544}
545
Kate Stoneb9c1b512016-09-06 20:57:50 +0000546struct RecurseCopyBaton {
547 const FileSpec &dst;
548 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000549 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000550};
551
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000552static FileSystem::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000553RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000554 llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000556 FileSpec src(path);
Zachary Turner7d86ee52017-03-08 17:56:08 +0000557 namespace fs = llvm::sys::fs;
558 switch (ft) {
559 case fs::file_type::fifo_file:
560 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 // we have no way to copy pipes and sockets - ignore them and continue
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000562 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000564
Zachary Turner7d86ee52017-03-08 17:56:08 +0000565 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 // make the new directory and get in there
567 FileSpec dst_dir = rc_baton->dst;
568 if (!dst_dir.GetFilename())
569 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000570 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 dst_dir, lldb::eFilePermissionsDirectoryDefault);
572 if (error.Fail()) {
573 rc_baton->error.SetErrorStringWithFormat(
574 "unable to setup directory %s on remote end", dst_dir.GetCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000575 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577
578 // now recurse
579 std::string src_dir_path(src.GetPath());
580
Adrian Prantl05097242018-04-30 16:49:04 +0000581 // Make a filespec that only fills in the directory of a FileSpec so when
582 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 FileSpec recurse_dst;
584 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000585 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
586 Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000587 FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
588 RecurseCopy_Callback, &rc_baton2);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 if (rc_baton2.error.Fail()) {
590 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000591 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000593 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 } break;
595
Zachary Turner7d86ee52017-03-08 17:56:08 +0000596 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 // copy the file and keep going
598 FileSpec dst_file = rc_baton->dst;
599 if (!dst_file.GetFilename())
600 dst_file.GetFilename() = src.GetFilename();
601
602 FileSpec src_resolved;
603
Jonas Devlieghere46376962018-10-31 21:49:27 +0000604 rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605
606 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000607 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608
609 rc_baton->error =
610 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
611
612 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000613 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000615 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 } break;
617
Zachary Turner7d86ee52017-03-08 17:56:08 +0000618 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 // copy the file and keep going
620 FileSpec dst_file = rc_baton->dst;
621 if (!dst_file.GetFilename())
622 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000623 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 if (err.Fail()) {
625 rc_baton->error.SetErrorString(err.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000626 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000628 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 } break;
630
Zachary Turner7d86ee52017-03-08 17:56:08 +0000631 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 rc_baton->error.SetErrorStringWithFormat(
633 "invalid file detected during copy: %s", src.GetPath().c_str());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000634 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 break;
636 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000637 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000638}
639
Zachary Turner97206d52017-05-12 04:51:55 +0000640Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
641 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642
643 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000644 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')",
645 src.GetPath().c_str(), dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 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 }
Adrian Prantl05097242018-04-30 16:49:04 +0000659 // If the fixed destination file doesn't have a directory yet, then we
660 // must have a relative path. We will resolve this relative path against
661 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 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
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000696 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
697 src.GetPath().c_str(), dst.GetPath().c_str(),
698 fixed_dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699
700 if (GetSupportsRSync()) {
701 error = PutFile(src, dst);
702 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000703 namespace fs = llvm::sys::fs;
704 switch (fs::get_file_type(src.GetPath(), false)) {
705 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000706 llvm::sys::fs::remove(fixed_dst.GetPath());
Jonas Devlieghere7c5310b2018-11-01 15:47:33 +0000707 uint32_t permissions = FileSystem::Instance().GetPermissions(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 if (permissions == 0)
709 permissions = eFilePermissionsDirectoryDefault;
710 error = MakeDirectory(fixed_dst, permissions);
711 if (error.Success()) {
712 // Make a filespec that only fills in the directory of a FileSpec so
713 // when we enumerate we can quickly fill in the filename for dst copies
714 FileSpec recurse_dst;
715 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
716 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000717 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000718 FileSystem::Instance().EnumerateDirectory(
719 src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 return baton.error;
721 }
722 } break;
723
Zachary Turner7d86ee52017-03-08 17:56:08 +0000724 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000725 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000726 error = PutFile(src, fixed_dst);
727 break;
728
Zachary Turner7d86ee52017-03-08 17:56:08 +0000729 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000730 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 FileSpec src_resolved;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000732 error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 if (error.Success())
734 error = CreateSymlink(dst, src_resolved);
735 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000736 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 error.SetErrorString("platform install doesn't handle pipes");
738 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000739 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740 error.SetErrorString("platform install doesn't handle sockets");
741 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000742 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 error.SetErrorString(
744 "platform install doesn't handle non file or directory items");
745 break;
746 }
747 }
748 return error;
749}
750
751bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
752 if (IsHost()) {
753 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000754 LLDB_LOG(log, "{0}", file_spec);
755 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
756 LLDB_LOG(log, "error: {0}", ec.message());
757 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000758 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000759 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 } else {
761 m_working_dir.Clear();
762 return SetRemoteWorkingDirectory(file_spec);
763 }
764}
765
Zachary Turner97206d52017-05-12 04:51:55 +0000766Status Platform::MakeDirectory(const FileSpec &file_spec,
767 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000768 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000769 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000771 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
773 GetPluginName().GetCString(),
774 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000775 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000777}
778
Zachary Turner97206d52017-05-12 04:51:55 +0000779Status Platform::GetFilePermissions(const FileSpec &file_spec,
780 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000781 if (IsHost()) {
782 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
783 if (Value)
784 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000785 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000786 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000787 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
789 GetPluginName().GetCString(),
790 LLVM_PRETTY_FUNCTION);
791 return error;
792 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000793}
794
Zachary Turner97206d52017-05-12 04:51:55 +0000795Status Platform::SetFilePermissions(const FileSpec &file_spec,
796 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000797 if (IsHost()) {
798 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
799 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
800 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000801 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
803 GetPluginName().GetCString(),
804 LLVM_PRETTY_FUNCTION);
805 return error;
806 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000807}
808
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809ConstString Platform::GetName() { return GetPluginName(); }
810
811const char *Platform::GetHostname() {
812 if (IsHost())
813 return "127.0.0.1";
814
815 if (m_name.empty())
816 return nullptr;
817 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000818}
819
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820ConstString Platform::GetFullNameForDylib(ConstString basename) {
821 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000822}
823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
825 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000826 LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')",
827 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 m_working_dir = working_dir;
829 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000830}
831
Pavel Labath2272c482018-06-18 15:02:23 +0000832bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000834 // We don't need anyone setting the OS version for the host platform, we
835 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000836 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000838 // We have a remote platform, allow setting the target OS version if we
839 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 // request the remote OS version from the connected platform.
841 if (IsConnected())
842 return false;
843 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000844 // We aren't connected and we might want to set the OS version ahead of
845 // time before we connect so we can peruse files and use a local SDK or
846 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000847 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000849 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 }
851 return false;
852}
853
Zachary Turner97206d52017-05-12 04:51:55 +0000854Status
855Platform::ResolveExecutable(const ModuleSpec &module_spec,
856 lldb::ModuleSP &exe_module_sp,
857 const FileSpecList *module_search_paths_ptr) {
858 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000859 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860 if (module_spec.GetArchitecture().IsValid()) {
861 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
862 module_search_paths_ptr, nullptr,
863 nullptr);
864 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000865 // No valid architecture was specified, ask the platform for the
866 // architectures that we should be using (in the correct order) and see
867 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 ModuleSpec arch_module_spec(module_spec);
869 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
870 idx, arch_module_spec.GetArchitecture());
871 ++idx) {
872 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
873 module_search_paths_ptr, nullptr,
874 nullptr);
875 // Did we find an executable using one of the
876 if (error.Success() && exe_module_sp)
877 break;
878 }
Greg Claytone996fd32011-03-08 22:40:15 +0000879 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 } else {
881 error.SetErrorStringWithFormat("'%s' does not exist",
882 module_spec.GetFileSpec().GetPath().c_str());
883 }
884 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000885}
886
Zachary Turner97206d52017-05-12 04:51:55 +0000887Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
888 FileSpec &sym_file) {
889 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000890 if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 sym_file = sym_spec.GetSymbolFileSpec();
892 else
893 error.SetErrorString("unable to resolve symbol file");
894 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000895}
896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897bool Platform::ResolveRemotePath(const FileSpec &platform_path,
898 FileSpec &resolved_platform_path) {
899 resolved_platform_path = platform_path;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000900 FileSystem::Instance().Resolve(resolved_platform_path);
901 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902}
903
904const ArchSpec &Platform::GetSystemArchitecture() {
905 if (IsHost()) {
906 if (!m_system_arch.IsValid()) {
907 // We have a local host platform
908 m_system_arch = HostInfo::GetArchitecture();
909 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000910 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000911 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000912 // We have a remote platform. We can only fetch the remote system
913 // architecture if we are connected, and we don't want to do it more than
914 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000915
Kate Stoneb9c1b512016-09-06 20:57:50 +0000916 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000917
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 bool fetch = false;
919 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000920 // We have valid OS version info, check to make sure it wasn't manually
921 // set prior to connecting. If it was manually set prior to connecting,
922 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000923 if (is_connected && !m_system_arch_set_while_connected)
924 fetch = true;
925 } else {
926 // We don't have valid OS version info, fetch it if we are connected
927 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929
930 if (fetch) {
931 m_system_arch = GetRemoteSystemArchitecture();
932 m_system_arch_set_while_connected = m_system_arch.IsValid();
933 }
934 }
935 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000936}
937
Pavel Labath7263f1b2017-10-31 10:56:03 +0000938ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
939 if (triple.empty())
940 return ArchSpec();
941 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
942 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
943 return ArchSpec(triple);
944
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000945 if (auto kind = HostInfo::ParseArchitectureKind(triple))
946 return HostInfo::GetArchitecture(*kind);
947
Pavel Labath7263f1b2017-10-31 10:56:03 +0000948 ArchSpec compatible_arch;
949 ArchSpec raw_arch(triple);
950 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
951 return raw_arch;
952
953 if (!compatible_arch.IsValid())
954 return ArchSpec(normalized_triple);
955
956 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
957 if (normalized_triple.getVendorName().empty())
958 normalized_triple.setVendor(compatible_triple.getVendor());
959 if (normalized_triple.getOSName().empty())
960 normalized_triple.setOS(compatible_triple.getOS());
961 if (normalized_triple.getEnvironmentName().empty())
962 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
963 return ArchSpec(normalized_triple);
964}
965
Zachary Turner97206d52017-05-12 04:51:55 +0000966Status Platform::ConnectRemote(Args &args) {
967 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 if (IsHost())
969 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
970 "the host platform and is always connected.",
971 GetPluginName().GetCString());
972 else
973 error.SetErrorStringWithFormat(
974 "Platform::ConnectRemote() is not supported by %s",
975 GetPluginName().GetCString());
976 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000977}
978
Zachary Turner97206d52017-05-12 04:51:55 +0000979Status Platform::DisconnectRemote() {
980 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 if (IsHost())
982 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
983 "the host platform and is always connected.",
984 GetPluginName().GetCString());
985 else
986 error.SetErrorStringWithFormat(
987 "Platform::DisconnectRemote() is not supported by %s",
988 GetPluginName().GetCString());
989 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000990}
Greg Clayton32e0a752011-03-30 18:16:51 +0000991
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992bool Platform::GetProcessInfo(lldb::pid_t pid,
993 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000994 // Take care of the host case so that each subclass can just call this
995 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996 if (IsHost())
997 return Host::GetProcessInfo(pid, process_info);
998 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +0000999}
1000
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1002 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +00001003 // Take care of the host case so that each subclass can just call this
1004 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 uint32_t match_count = 0;
1006 if (IsHost())
1007 match_count = Host::FindProcesses(match_info, process_infos);
1008 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001009}
Greg Clayton8b82f082011-04-12 05:54:46 +00001010
Zachary Turner97206d52017-05-12 04:51:55 +00001011Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1012 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001014 LLDB_LOGF(log, "Platform::%s()", __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015
Adrian Prantl05097242018-04-30 16:49:04 +00001016 // Take care of the host case so that each subclass can just call this
1017 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 if (IsHost()) {
1019 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1020 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1021
1022 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1024 const bool first_arg_is_full_shell_command = false;
1025 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1026 if (log) {
1027 const FileSpec &shell = launch_info.GetShell();
Jason Molenda3b687612019-02-08 23:36:25 +00001028 std::string shell_str = (shell) ? shell.GetPath() : "<null>";
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001029 LLDB_LOGF(log,
1030 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1031 ", shell is '%s'",
1032 __FUNCTION__, num_resumes, shell_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 }
1034
1035 if (!launch_info.ConvertArgumentsForLaunchingInShell(
Raphael Isemann45c3fc92020-10-27 12:39:44 +01001036 error, will_debug, first_arg_is_full_shell_command, num_resumes))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 return error;
1038 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1039 error = ShellExpandArguments(launch_info);
1040 if (error.Fail()) {
1041 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1042 "consider launching with 'process "
1043 "launch'.",
1044 error.AsCString("unknown"));
1045 return error;
1046 }
1047 }
1048
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001049 LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32,
1050 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001051
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 error = Host::LaunchProcess(launch_info);
1053 } else
1054 error.SetErrorString(
1055 "base lldb_private::Platform class can't launch remote processes");
1056 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001057}
1058
Zachary Turner97206d52017-05-12 04:51:55 +00001059Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 if (IsHost())
1061 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001062 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001063}
1064
Zachary Turner97206d52017-05-12 04:51:55 +00001065Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001067 LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001068
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001070 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 size_t num_debuggers = Debugger::GetNumDebuggers();
1072 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1073 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1074 lldb_private::TargetList &targets = debugger->GetTargetList();
1075 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1076 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1077 if (process->GetID() == pid)
1078 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001079 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001081
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001083 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084 "base lldb_private::Platform class can't kill remote processes unless "
1085 "they are controlled by a process plugin");
1086 }
1087 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001088 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001089}
1090
Greg Clayton8b82f082011-04-12 05:54:46 +00001091lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1093 Target *target, // Can be nullptr, if nullptr create a
1094 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001095 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001097 LLDB_LOGF(log, "Platform::%s entered (target %p)", __FUNCTION__,
1098 static_cast<void *>(target));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099
1100 ProcessSP process_sp;
1101 // Make sure we stop at the entry point
1102 launch_info.GetFlags().Set(eLaunchFlagDebug);
1103 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001104 // group, since then we can handle ^C interrupts ourselves w/o having to
1105 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 launch_info.SetLaunchInSeparateProcessGroup(true);
1107
1108 // Allow any StructuredData process-bound plugins to adjust the launch info
1109 // if needed
1110 size_t i = 0;
1111 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001112 // Note iteration can't simply go until a nullptr callback is returned, as it
1113 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1115 for (auto filter_callback = get_filter_func(i, iteration_complete);
1116 !iteration_complete;
1117 filter_callback = get_filter_func(++i, iteration_complete)) {
1118 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001119 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 error = (*filter_callback)(launch_info, target);
1121 if (!error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001122 LLDB_LOGF(log,
1123 "Platform::%s() StructuredDataPlugin launch "
1124 "filter failed.",
1125 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 return process_sp;
1127 }
1128 }
1129 }
1130
1131 error = LaunchProcess(launch_info);
1132 if (error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001133 LLDB_LOGF(log,
1134 "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")",
1135 __FUNCTION__, launch_info.GetProcessID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1137 ProcessAttachInfo attach_info(launch_info);
1138 process_sp = Attach(attach_info, debugger, target, error);
1139 if (process_sp) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001140 LLDB_LOGF(log, "Platform::%s Attach() succeeded, Process plugin: %s",
1141 __FUNCTION__, process_sp->GetPluginName().AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001143
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 // Since we attached to the process, it will think it needs to detach
1145 // if the process object just goes away without an explicit call to
1146 // Process::Kill() or Process::Detach(), so let it know to kill the
1147 // process if this happens.
1148 process_sp->SetShouldDetach(false);
1149
Adrian Prantl05097242018-04-30 16:49:04 +00001150 // If we didn't have any file actions, the pseudo terminal might have
Jonas Devlieghere64ec5052020-06-15 15:36:14 -07001151 // been used where the secondary side was given as the file to open for
Adrian Prantl05097242018-04-30 16:49:04 +00001152 // stdin/out/err after we have already opened the master so we can
1153 // read/write stdin/out/err.
Jonas Devlieghere64ec5052020-06-15 15:36:14 -07001154 int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001155 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001156 process_sp->SetSTDIOFileDescriptor(pty_fd);
1157 }
1158 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001159 LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__,
1160 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161 }
1162 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001163 LLDB_LOGF(log,
1164 "Platform::%s LaunchProcess() returned launch_info with "
1165 "invalid process id",
1166 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167 }
1168 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001169 LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1170 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 }
1172
1173 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001174}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001175
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001176lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1178 ArchSpec *platform_arch_ptr) {
1179 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001180 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181 if (arch.IsValid())
1182 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1183 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001184}
1185
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001186/// Lets a platform answer if it is compatible with a given
1187/// architecture and the target triple contained within.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1189 bool exact_arch_match,
1190 ArchSpec *compatible_arch_ptr) {
1191 // If the architecture is invalid, we must answer true...
1192 if (arch.IsValid()) {
1193 ArchSpec platform_arch;
1194 // Try for an exact architecture match first.
1195 if (exact_arch_match) {
1196 for (uint32_t arch_idx = 0;
1197 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1198 ++arch_idx) {
1199 if (arch.IsExactMatch(platform_arch)) {
1200 if (compatible_arch_ptr)
1201 *compatible_arch_ptr = platform_arch;
1202 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001203 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204 }
1205 } else {
1206 for (uint32_t arch_idx = 0;
1207 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1208 ++arch_idx) {
1209 if (arch.IsCompatibleMatch(platform_arch)) {
1210 if (compatible_arch_ptr)
1211 *compatible_arch_ptr = platform_arch;
1212 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001215 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 }
1217 if (compatible_arch_ptr)
1218 compatible_arch_ptr->Clear();
1219 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001220}
1221
Zachary Turner97206d52017-05-12 04:51:55 +00001222Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1223 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001224 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001225 LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001226
Lawrence D'Anna62c9fe42019-10-14 20:15:34 +00001227 auto source_open_options =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001229 namespace fs = llvm::sys::fs;
1230 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001232
Lawrence D'Anna7ca15ba2019-09-27 14:33:35 +00001233 auto source_file = FileSystem::Instance().Open(source, source_open_options,
1234 lldb::eFilePermissionsUserRW);
Lawrence D'Anna2fce1132019-09-26 17:54:59 +00001235 if (!source_file)
1236 return Status(source_file.takeError());
1237 Status error;
1238 uint32_t permissions = source_file.get()->GetPermissions(error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 if (permissions == 0)
1240 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001241
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 lldb::user_id_t dest_file = OpenFile(
1243 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1244 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1245 permissions, error);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001246 LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001247
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248 if (error.Fail())
1249 return error;
1250 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001251 return Status("unable to open target file");
Jason Molendaeb9902a2018-12-07 00:35:26 +00001252 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001253 uint64_t offset = 0;
1254 for (;;) {
1255 size_t bytes_read = buffer_sp->GetByteSize();
Lawrence D'Anna2fce1132019-09-26 17:54:59 +00001256 error = source_file.get()->Read(buffer_sp->GetBytes(), bytes_read);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 if (error.Fail() || bytes_read == 0)
1258 break;
1259
1260 const uint64_t bytes_written =
1261 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001262 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001264
Kate Stoneb9c1b512016-09-06 20:57:50 +00001265 offset += bytes_written;
1266 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001267 // We didn't write the correct number of bytes, so adjust the file
1268 // position in the source file we are reading from...
Lawrence D'Anna2fce1132019-09-26 17:54:59 +00001269 source_file.get()->SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 }
1272 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001273
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001275 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276
1277 // TODO: ChownFile?
1278
1279 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001280}
1281
Zachary Turner97206d52017-05-12 04:51:55 +00001282Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1283 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001285}
1286
Zachary Turner97206d52017-05-12 04:51:55 +00001287Status
1288Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1289 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001290{
Zachary Turner97206d52017-05-12 04:51:55 +00001291 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001293}
1294
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1296 return false;
1297}
1298
Zachary Turner97206d52017-05-12 04:51:55 +00001299Status Platform::Unlink(const FileSpec &path) {
1300 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 return error;
1302}
1303
Ed Maste37c40af2017-08-16 12:55:02 +00001304MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1305 addr_t length, unsigned prot,
1306 unsigned flags, addr_t fd,
1307 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308 uint64_t flags_platform = 0;
1309 if (flags & eMmapFlagsPrivate)
1310 flags_platform |= MAP_PRIVATE;
1311 if (flags & eMmapFlagsAnon)
1312 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001313
1314 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1315 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316}
1317
Zachary Turner97206d52017-05-12 04:51:55 +00001318lldb_private::Status Platform::RunShellCommand(
Med Ismail Bennaniaddb5142020-08-28 15:38:39 +02001319 llvm::StringRef command,
1320 const FileSpec &
1321 working_dir, // Pass empty FileSpec to use the current working directory
1322 int *status_ptr, // Pass nullptr if you don't want the process exit status
1323 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1324 // process to exit
1325 std::string
1326 *command_output, // Pass nullptr if you don't want the command output
1327 const Timeout<std::micro> &timeout) {
1328 return RunShellCommand(llvm::StringRef(), command, working_dir, status_ptr,
1329 signo_ptr, command_output, timeout);
1330}
1331
1332lldb_private::Status Platform::RunShellCommand(
1333 llvm::StringRef shell, // Pass empty if you want to use the default
1334 // shell interpreter
1335 llvm::StringRef command, // Shouldn't be empty
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 const FileSpec &
1337 working_dir, // Pass empty FileSpec to use the current working directory
1338 int *status_ptr, // Pass nullptr if you don't want the process exit status
1339 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1340 // process to exit
1341 std::string
1342 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001343 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344 if (IsHost())
Med Ismail Bennaniaddb5142020-08-28 15:38:39 +02001345 return Host::RunShellCommand(shell, command, working_dir, status_ptr,
1346 signo_ptr, command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347 else
Zachary Turner97206d52017-05-12 04:51:55 +00001348 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349}
1350
1351bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1352 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001353 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001354 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001355 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1356 if (!Result)
1357 return false;
1358 std::tie(high, low) = Result->words();
1359 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001360}
1361
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362void Platform::SetLocalCacheDirectory(const char *local) {
1363 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001364}
1365
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366const char *Platform::GetLocalCacheDirectory() {
1367 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001368}
Greg Claytonfbb76342013-11-20 21:07:01 +00001369
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001370static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001372 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001374 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 "Platform-specific options required for rsync to work."},
1376 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001377 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 "Platform-specific rsync prefix put before the remote path."},
1379 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001380 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 "Do not automatically fill in the remote hostname when composing the "
1382 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001383};
1384
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001385static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001387 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001389 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001391};
1392
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001393static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001394 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001395 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001397};
1398
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001399llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001400 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001401}
1402
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403void OptionGroupPlatformRSync::OptionParsingStarting(
1404 ExecutionContext *execution_context) {
1405 m_rsync = false;
1406 m_rsync_opts.clear();
1407 m_rsync_prefix.clear();
1408 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001409}
1410
Zachary Turner97206d52017-05-12 04:51:55 +00001411lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001412OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001413 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001415 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416 char short_option = (char)GetDefinitions()[option_idx].short_option;
1417 switch (short_option) {
1418 case 'r':
1419 m_rsync = true;
1420 break;
1421
1422 case 'R':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001423 m_rsync_opts.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 break;
1425
1426 case 'P':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001427 m_rsync_prefix.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428 break;
1429
1430 case 'i':
1431 m_ignores_remote_hostname = true;
1432 break;
1433
1434 default:
1435 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1436 break;
1437 }
1438
1439 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001440}
1441
Greg Clayton4116e932012-05-15 02:33:01 +00001442lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1444 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001445}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001446
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001447llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001448 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001449}
1450
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451void OptionGroupPlatformSSH::OptionParsingStarting(
1452 ExecutionContext *execution_context) {
1453 m_ssh = false;
1454 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001455}
1456
Zachary Turner97206d52017-05-12 04:51:55 +00001457lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001458OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001459 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001461 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 char short_option = (char)GetDefinitions()[option_idx].short_option;
1463 switch (short_option) {
1464 case 's':
1465 m_ssh = true;
1466 break;
1467
1468 case 'S':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001469 m_ssh_opts.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470 break;
1471
1472 default:
1473 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1474 break;
1475 }
1476
1477 return error;
1478}
1479
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001480llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001481 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482}
1483
1484void OptionGroupPlatformCaching::OptionParsingStarting(
1485 ExecutionContext *execution_context) {
1486 m_cache_dir.clear();
1487}
1488
Zachary Turner97206d52017-05-12 04:51:55 +00001489lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001490 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001492 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001493 char short_option = (char)GetDefinitions()[option_idx].short_option;
1494 switch (short_option) {
1495 case 'c':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001496 m_cache_dir.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001497 break;
1498
1499 default:
1500 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1501 break;
1502 }
1503
1504 return error;
1505}
1506
Pavel Labath62930e52018-01-10 11:57:31 +00001507Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508
1509const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1510 if (!m_calculated_trap_handlers) {
1511 std::lock_guard<std::mutex> guard(m_mutex);
1512 if (!m_calculated_trap_handlers) {
1513 CalculateTrapHandlerSymbolNames();
1514 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 }
1517 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001518}
1519
Zachary Turner97206d52017-05-12 04:51:55 +00001520Status Platform::GetCachedExecutable(
1521 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1522 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 const auto platform_spec = module_spec.GetFileSpec();
1524 const auto error = LoadCachedExecutable(
1525 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1526 if (error.Success()) {
1527 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1528 module_spec.GetPlatformFileSpec() = platform_spec;
1529 }
1530
1531 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001532}
1533
Zachary Turner97206d52017-05-12 04:51:55 +00001534Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1536 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1537 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1538 [&](const ModuleSpec &spec) {
1539 return remote_platform.ResolveExecutable(
1540 spec, module_sp, module_search_paths_ptr);
1541 },
1542 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001543}
1544
Zachary Turner97206d52017-05-12 04:51:55 +00001545Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1546 Process *process,
1547 lldb::ModuleSP &module_sp,
1548 const ModuleResolver &module_resolver,
1549 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 // Get module information from a target.
1551 ModuleSpec resolved_module_spec;
1552 bool got_module_spec = false;
1553 if (process) {
1554 // Try to get module information from the process
1555 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1556 module_spec.GetArchitecture(),
1557 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001558 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1560 got_module_spec = true;
1561 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001562 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001564
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001565 if (!module_spec.GetArchitecture().IsValid()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001566 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001567 // No valid architecture was specified, ask the platform for the
1568 // architectures that we should be using (in the correct order) and see if
1569 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001570 ModuleSpec arch_module_spec(module_spec);
1571 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1572 idx, arch_module_spec.GetArchitecture());
1573 ++idx) {
1574 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1575 nullptr, nullptr);
1576 // Did we find an executable using one of the
1577 if (error.Success() && module_sp)
1578 break;
1579 }
Joseph Tremoulet20f84252020-09-23 06:00:50 -07001580 if (module_sp) {
1581 resolved_module_spec = arch_module_spec;
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001582 got_module_spec = true;
Joseph Tremoulet20f84252020-09-23 06:00:50 -07001583 }
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001584 }
1585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001587 // Get module information from a target.
Joseph Tremoulet20f84252020-09-23 06:00:50 -07001588 if (GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1589 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001590 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
Joseph Tremoulet20f84252020-09-23 06:00:50 -07001592 got_module_spec = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001594 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001596
Joseph Tremoulet20f84252020-09-23 06:00:50 -07001597 if (!got_module_spec) {
1598 // Fall back to the given module resolver, which may have its own
1599 // search logic.
1600 return module_resolver(module_spec);
1601 }
1602
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603 // If we are looking for a specific UUID, make sure resolved_module_spec has
1604 // the same one before we search.
1605 if (module_spec.GetUUID().IsValid()) {
1606 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1607 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001608
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 // Trying to find a module by UUID on local file system.
1610 const auto error = module_resolver(resolved_module_spec);
1611 if (error.Fail()) {
1612 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001613 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001615
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001617}
1618
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1620 lldb::ModuleSP &module_sp,
1621 bool *did_create_ptr) {
1622 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1623 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001624 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625
1626 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1627
1628 // Check local cache for a module.
1629 auto error = m_module_cache->GetAndPut(
1630 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1631 [this](const ModuleSpec &module_spec,
1632 const FileSpec &tmp_download_file_spec) {
1633 return DownloadModuleSlice(
1634 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1635 module_spec.GetObjectSize(), tmp_download_file_spec);
1636
1637 },
1638 [this](const ModuleSP &module_sp,
1639 const FileSpec &tmp_download_file_spec) {
1640 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1641 },
1642 module_sp, did_create_ptr);
1643 if (error.Success())
1644 return true;
1645
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001646 LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s",
1647 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1648 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001650}
1651
Zachary Turner97206d52017-05-12 04:51:55 +00001652Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1653 const uint64_t src_offset,
1654 const uint64_t src_size,
1655 const FileSpec &dst_file_spec) {
1656 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001657
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001658 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +00001659 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::OF_None);
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001660 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001661 error.SetErrorStringWithFormat("unable to open destination file: %s",
1662 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001663 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001665
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1667 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001668
Kate Stoneb9c1b512016-09-06 20:57:50 +00001669 if (error.Fail()) {
1670 error.SetErrorStringWithFormat("unable to open source file: %s",
1671 error.AsCString());
1672 return error;
1673 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001674
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675 std::vector<char> buffer(1024);
1676 auto offset = src_offset;
1677 uint64_t total_bytes_read = 0;
1678 while (total_bytes_read < src_size) {
1679 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1680 src_size - total_bytes_read);
1681 const uint64_t n_read =
1682 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001683 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684 break;
1685 if (n_read == 0) {
1686 error.SetErrorString("read 0 bytes");
1687 break;
1688 }
1689 offset += n_read;
1690 total_bytes_read += n_read;
1691 dst.write(&buffer[0], n_read);
1692 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001693
Zachary Turner97206d52017-05-12 04:51:55 +00001694 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695 CloseFile(src_fd, close_error); // Ignoring close error.
1696
1697 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001698}
1699
Zachary Turner97206d52017-05-12 04:51:55 +00001700Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1701 const FileSpec &dst_file_spec) {
1702 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001703 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001704}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001705
Kate Stoneb9c1b512016-09-06 20:57:50 +00001706FileSpec Platform::GetModuleCacheRoot() {
1707 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1708 dir_spec.AppendPathComponent(GetName().AsCString());
1709 return dir_spec;
1710}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001711
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001713
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1715 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1716 return s_default_unix_signals_sp;
1717}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001718
Zachary Turnera89ce432019-03-06 18:20:23 +00001719UnixSignalsSP Platform::GetUnixSignals() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 if (IsHost())
Zachary Turnera89ce432019-03-06 18:20:23 +00001721 return UnixSignals::CreateForHost();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001722 return GetRemoteUnixSignals();
1723}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001724
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725uint32_t Platform::LoadImage(lldb_private::Process *process,
1726 const lldb_private::FileSpec &local_file,
1727 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001728 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729 if (local_file && remote_file) {
1730 // Both local and remote file was specified. Install the local file to the
1731 // given location.
1732 if (IsRemote() || local_file != remote_file) {
1733 error = Install(local_file, remote_file);
1734 if (error.Fail())
1735 return LLDB_INVALID_IMAGE_TOKEN;
1736 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001737 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001739
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740 if (local_file) {
1741 // Only local file was specified. Install it to the current working
1742 // directory.
1743 FileSpec target_file = GetWorkingDirectory();
1744 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1745 if (IsRemote() || local_file != target_file) {
1746 error = Install(local_file, target_file);
1747 if (error.Fail())
1748 return LLDB_INVALID_IMAGE_TOKEN;
1749 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001750 return DoLoadImage(process, target_file, nullptr, error);
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001751 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001752
Kate Stoneb9c1b512016-09-06 20:57:50 +00001753 if (remote_file) {
1754 // Only remote file was specified so we don't have to do any copying
Jim Ingham0d231f72018-06-28 20:02:11 +00001755 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 error.SetErrorString("Neither local nor remote file was specified");
1759 return LLDB_INVALID_IMAGE_TOKEN;
1760}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001761
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1763 const lldb_private::FileSpec &remote_file,
Jim Ingham0d231f72018-06-28 20:02:11 +00001764 const std::vector<std::string> *paths,
1765 lldb_private::Status &error,
1766 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001767 error.SetErrorString("LoadImage is not supported on the current platform");
1768 return LLDB_INVALID_IMAGE_TOKEN;
1769}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001770
Jim Ingham0d231f72018-06-28 20:02:11 +00001771uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1772 const lldb_private::FileSpec &remote_filename,
1773 const std::vector<std::string> &paths,
1774 lldb_private::Status &error,
1775 lldb_private::FileSpec *loaded_path)
1776{
1777 FileSpec file_to_use;
1778 if (remote_filename.IsAbsolute())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001779 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1780
1781 remote_filename.GetPathStyle());
Jim Ingham0d231f72018-06-28 20:02:11 +00001782 else
1783 file_to_use = remote_filename;
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001784
Jim Ingham0d231f72018-06-28 20:02:11 +00001785 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1786}
1787
Zachary Turner97206d52017-05-12 04:51:55 +00001788Status Platform::UnloadImage(lldb_private::Process *process,
1789 uint32_t image_token) {
1790 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001792
Zachary Turner31659452016-11-17 21:15:14 +00001793lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1794 llvm::StringRef plugin_name,
Jonas Devlieghere706cccb2020-07-14 08:44:40 -07001795 Debugger &debugger, Target *target,
1796 Status &error) {
1797 return DoConnectProcess(connect_url, plugin_name, debugger, nullptr, target,
1798 error);
1799}
1800
1801lldb::ProcessSP Platform::ConnectProcessSynchronous(
1802 llvm::StringRef connect_url, llvm::StringRef plugin_name,
1803 Debugger &debugger, Stream &stream, Target *target, Status &error) {
1804 return DoConnectProcess(connect_url, plugin_name, debugger, &stream, target,
1805 error);
1806}
1807
1808lldb::ProcessSP Platform::DoConnectProcess(llvm::StringRef connect_url,
1809 llvm::StringRef plugin_name,
1810 Debugger &debugger, Stream *stream,
1811 Target *target, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001812 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001813
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814 if (!target) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001815 ArchSpec arch;
1816 if (target && target->GetArchitecture().IsValid())
1817 arch = target->GetArchitecture();
1818 else
1819 arch = Target::GetDefaultArchitecture();
1820
1821 const char *triple = "";
1822 if (arch.IsValid())
1823 triple = arch.GetTriple().getTriple().c_str();
1824
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001826 error = debugger.GetTargetList().CreateTarget(
Jason Molendadf9f7962018-10-23 23:45:56 +00001827 debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001828 target = new_target_sp.get();
1829 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001830
Kate Stoneb9c1b512016-09-06 20:57:50 +00001831 if (!target || error.Fail())
1832 return nullptr;
1833
1834 debugger.GetTargetList().SetSelectedTarget(target);
1835
1836 lldb::ProcessSP process_sp =
Michał Górny18e42722020-11-20 17:12:22 +01001837 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr, true);
Jonas Devlieghere706cccb2020-07-14 08:44:40 -07001838
Kate Stoneb9c1b512016-09-06 20:57:50 +00001839 if (!process_sp)
1840 return nullptr;
1841
Jonas Devlieghere706cccb2020-07-14 08:44:40 -07001842 // If this private method is called with a stream we are synchronous.
1843 const bool synchronous = stream != nullptr;
1844
1845 ListenerSP listener_sp(
1846 Listener::MakeListener("lldb.Process.ConnectProcess.hijack"));
1847 if (synchronous)
1848 process_sp->HijackProcessEvents(listener_sp);
1849
Jonas Devlieghere32d35fb2020-07-13 13:44:01 -07001850 error = process_sp->ConnectRemote(connect_url);
Jonas Devlieghere706cccb2020-07-14 08:44:40 -07001851 if (error.Fail()) {
1852 if (synchronous)
1853 process_sp->RestoreProcessEvents();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001854 return nullptr;
Jonas Devlieghere706cccb2020-07-14 08:44:40 -07001855 }
1856
1857 if (synchronous) {
1858 EventSP event_sp;
1859 process_sp->WaitForProcessToStop(llvm::None, &event_sp, true, listener_sp,
1860 nullptr);
1861 process_sp->RestoreProcessEvents();
1862 bool pop_process_io_handler = false;
1863 Process::HandleProcessStateChangedEvent(event_sp, stream,
1864 pop_process_io_handler);
1865 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001866
1867 return process_sp;
1868}
1869
1870size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001871 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872 error.Clear();
1873 return 0;
1874}
1875
1876size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1877 BreakpointSite *bp_site) {
1878 ArchSpec arch = target.GetArchitecture();
Jonas Devlieghere1fcd234a2020-04-24 09:03:51 -07001879 assert(arch.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001880 const uint8_t *trap_opcode = nullptr;
1881 size_t trap_opcode_size = 0;
1882
1883 switch (arch.GetMachine()) {
Jason Molenda7dd7a362019-10-16 19:14:49 +00001884 case llvm::Triple::aarch64_32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001885 case llvm::Triple::aarch64: {
1886 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1887 trap_opcode = g_aarch64_opcode;
1888 trap_opcode_size = sizeof(g_aarch64_opcode);
1889 } break;
1890
Tatyana Krasnukhafaf6b252019-10-17 15:16:21 +00001891 case llvm::Triple::arc: {
1892 static const uint8_t g_hex_opcode[] = { 0xff, 0x7f };
1893 trap_opcode = g_hex_opcode;
1894 trap_opcode_size = sizeof(g_hex_opcode);
1895 } break;
1896
Kate Stoneb9c1b512016-09-06 20:57:50 +00001897 // TODO: support big-endian arm and thumb trap codes.
1898 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001899 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1900 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001901 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1902 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1903
1904 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001905 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906
1907 if (bp_loc_sp) {
1908 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001909 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001911 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001912 }
1913
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001914 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001915 trap_opcode = g_thumb_breakpoint_opcode;
1916 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1917 } else {
1918 trap_opcode = g_arm_breakpoint_opcode;
1919 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1920 }
1921 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001922
Ayke van Laethem0818e6c2020-02-07 22:39:56 +01001923 case llvm::Triple::avr: {
1924 static const uint8_t g_hex_opcode[] = {0x98, 0x95};
1925 trap_opcode = g_hex_opcode;
1926 trap_opcode_size = sizeof(g_hex_opcode);
1927 } break;
1928
Kate Stoneb9c1b512016-09-06 20:57:50 +00001929 case llvm::Triple::mips:
1930 case llvm::Triple::mips64: {
1931 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1932 trap_opcode = g_hex_opcode;
1933 trap_opcode_size = sizeof(g_hex_opcode);
1934 } break;
1935
1936 case llvm::Triple::mipsel:
1937 case llvm::Triple::mips64el: {
1938 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1939 trap_opcode = g_hex_opcode;
1940 trap_opcode_size = sizeof(g_hex_opcode);
1941 } break;
1942
1943 case llvm::Triple::systemz: {
1944 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1945 trap_opcode = g_hex_opcode;
1946 trap_opcode_size = sizeof(g_hex_opcode);
1947 } break;
1948
1949 case llvm::Triple::hexagon: {
1950 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1951 trap_opcode = g_hex_opcode;
1952 trap_opcode_size = sizeof(g_hex_opcode);
1953 } break;
1954
1955 case llvm::Triple::ppc:
1956 case llvm::Triple::ppc64: {
1957 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1958 trap_opcode = g_ppc_opcode;
1959 trap_opcode_size = sizeof(g_ppc_opcode);
1960 } break;
1961
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001962 case llvm::Triple::ppc64le: {
1963 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1964 trap_opcode = g_ppc64le_opcode;
1965 trap_opcode_size = sizeof(g_ppc64le_opcode);
1966 } break;
1967
Kate Stoneb9c1b512016-09-06 20:57:50 +00001968 case llvm::Triple::x86:
1969 case llvm::Triple::x86_64: {
1970 static const uint8_t g_i386_opcode[] = {0xCC};
1971 trap_opcode = g_i386_opcode;
1972 trap_opcode_size = sizeof(g_i386_opcode);
1973 } break;
1974
1975 default:
Jonas Devlieghere1fcd234a2020-04-24 09:03:51 -07001976 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977 }
1978
1979 assert(bp_site);
1980 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1981 return trap_opcode_size;
1982
1983 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001984}