blob: 95c35ea826a07afe81ec804841ab3c156f97cbcf [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
Eugene Zelenko9394d7722016-02-18 00:10:17 +000015#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Path.h"
17
Greg Clayton4116e932012-05-15 02:33:01 +000018#include "lldb/Breakpoint/BreakpointIDList.h"
Aidan Dodds933d8db2016-02-22 17:29:56 +000019#include "lldb/Breakpoint/BreakpointLocation.h"
Zachary Turner7271bab2015-05-13 19:44:24 +000020#include "lldb/Core/Debugger.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000021#include "lldb/Core/Module.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/ModuleSpec.h"
Greg Claytone996fd32011-03-08 22:40:15 +000023#include "lldb/Core/PluginManager.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000024#include "lldb/Core/StreamFile.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000025#include "lldb/Host/FileSystem.h"
Greg Claytonded470d2011-03-19 01:12:21 +000026#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000027#include "lldb/Host/HostInfo.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000028#include "lldb/Host/OptionParser.h"
Tatyana Krasnukhafe74df02020-03-12 21:48:23 +030029#include "lldb/Interpreter/OptionValueFileSpec.h"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000030#include "lldb/Interpreter/OptionValueProperties.h"
31#include "lldb/Interpreter/Property.h"
32#include "lldb/Symbol/ObjectFile.h"
Zachary Turner01c32432017-02-14 19:06:07 +000033#include "lldb/Target/ModuleCache.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000034#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000035#include "lldb/Target/Process.h"
Greg Claytone996fd32011-03-08 22:40:15 +000036#include "lldb/Target/Target.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000037#include "lldb/Target/UnixSignals.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000038#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner5713a052017-03-22 18:40:07 +000039#include "lldb/Utility/FileSpec.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000040#include "lldb/Utility/Log.h"
Zachary Turner97206d52017-05-12 04:51:55 +000041#include "lldb/Utility/Status.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000042#include "lldb/Utility/StructuredData.h"
Greg Claytone996fd32011-03-08 22:40:15 +000043
Zachary Turner7d86ee52017-03-08 17:56:08 +000044#include "llvm/Support/FileSystem.h"
45
Adrian Prantl05097242018-04-30 16:49:04 +000046// Define these constants from POSIX mman.h rather than include the file so
47// that they will be correct even when compiled on Linux.
Robert Flack96ad3de2015-05-09 15:53:31 +000048#define MAP_PRIVATE 2
49#define MAP_ANON 0x1000
50
Greg Claytone996fd32011-03-08 22:40:15 +000051using namespace lldb;
52using namespace lldb_private;
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +000053
54static uint32_t g_initialize_count = 0;
55
Adrian Prantl05097242018-04-30 16:49:04 +000056// Use a singleton function for g_local_platform_sp to avoid init constructors
57// since LLDB is often part of a shared library
Kate Stoneb9c1b512016-09-06 20:57:50 +000058static PlatformSP &GetHostPlatformSP() {
59 static PlatformSP g_platform_sp;
60 return g_platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063const char *Platform::GetHostPlatformName() { return "host"; }
Greg Claytonab65b342011-04-13 22:47:15 +000064
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000065namespace {
66
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000067#define LLDB_PROPERTIES_platform
Jordan Rupprecht6a253d32019-07-29 17:22:10 +000068#include "TargetProperties.inc"
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000069
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000070enum {
71#define LLDB_PROPERTIES_platform
Jordan Rupprecht6a253d32019-07-29 17:22:10 +000072#include "TargetPropertiesEnum.inc"
Jonas Devlieghere971f9ca2019-07-25 21:36:37 +000073};
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075} // namespace
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077ConstString PlatformProperties::GetSettingName() {
78 static ConstString g_setting_name("platform");
79 return g_setting_name;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082PlatformProperties::PlatformProperties() {
Jonas Devlieghere796ac802019-02-11 23:13:08 +000083 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
Jonas Devliegherea8ea5952019-07-29 16:41:30 +000084 m_collection_sp->Initialize(g_platform_properties);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 auto module_cache_dir = GetModuleCacheDirectory();
87 if (module_cache_dir)
88 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 llvm::SmallString<64> user_home_dir;
91 if (!llvm::sys::path::home_directory(user_home_dir))
92 return;
Oleksiy Vyalovd21ca282015-10-01 17:48:57 +000093
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +000094 module_cache_dir = FileSpec(user_home_dir.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 module_cache_dir.AppendPathComponent(".lldb");
96 module_cache_dir.AppendPathComponent("module_cache");
Tatyana Krasnukhafe74df02020-03-12 21:48:23 +030097 SetDefaultModuleCacheDirectory(module_cache_dir);
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 SetModuleCacheDirectory(module_cache_dir);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101bool PlatformProperties::GetUseModuleCache() const {
102 const auto idx = ePropertyUseModuleCache;
103 return m_collection_sp->GetPropertyAtIndexAsBoolean(
Jonas Devliegherea8ea5952019-07-29 16:41:30 +0000104 nullptr, idx, g_platform_properties[idx].default_uint_value != 0);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107bool PlatformProperties::SetUseModuleCache(bool use_module_cache) {
108 return m_collection_sp->SetPropertyAtIndexAsBoolean(
109 nullptr, ePropertyUseModuleCache, use_module_cache);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112FileSpec PlatformProperties::GetModuleCacheDirectory() const {
113 return m_collection_sp->GetPropertyAtIndexAsFileSpec(
114 nullptr, ePropertyModuleCacheDirectory);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117bool PlatformProperties::SetModuleCacheDirectory(const FileSpec &dir_spec) {
118 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
119 nullptr, ePropertyModuleCacheDirectory, dir_spec);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000120}
121
Tatyana Krasnukhafe74df02020-03-12 21:48:23 +0300122void PlatformProperties::SetDefaultModuleCacheDirectory(
123 const FileSpec &dir_spec) {
124 auto f_spec_opt = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(
125 nullptr, false, ePropertyModuleCacheDirectory);
126 assert(f_spec_opt);
127 f_spec_opt->SetDefaultValue(dir_spec);
128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130/// Get the native host platform plug-in.
Greg Claytone996fd32011-03-08 22:40:15 +0000131///
132/// There should only be one of these for each host that LLDB runs
133/// upon that should be statically compiled in and registered using
134/// preprocessor macros or other similar build mechanisms.
135///
136/// This platform will be used as the default platform when launching
137/// or attaching to processes unless another platform is specified.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138PlatformSP Platform::GetHostPlatform() { return GetHostPlatformSP(); }
139
140static std::vector<PlatformSP> &GetPlatformList() {
141 static std::vector<PlatformSP> g_platform_list;
142 return g_platform_list;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145static std::recursive_mutex &GetPlatformListMutex() {
146 static std::recursive_mutex g_mutex;
147 return g_mutex;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000148}
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150void Platform::Initialize() { g_initialize_count++; }
Greg Claytone996fd32011-03-08 22:40:15 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152void Platform::Terminate() {
153 if (g_initialize_count > 0) {
154 if (--g_initialize_count == 0) {
155 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
156 GetPlatformList().clear();
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000157 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 }
Tamas Berghammer3c4f89d2015-02-12 18:18:27 +0000159}
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161const PlatformPropertiesSP &Platform::GetGlobalPlatformProperties() {
162 static const auto g_settings_sp(std::make_shared<PlatformProperties>());
163 return g_settings_sp;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000164}
165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166void Platform::SetHostPlatform(const lldb::PlatformSP &platform_sp) {
167 // The native platform should use its static void Platform::Initialize()
168 // function to register itself as the native platform.
169 GetHostPlatformSP() = platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 if (platform_sp) {
172 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
173 GetPlatformList().push_back(platform_sp);
174 }
Greg Claytone996fd32011-03-08 22:40:15 +0000175}
176
Zachary Turner97206d52017-05-12 04:51:55 +0000177Status Platform::GetFileWithUUID(const FileSpec &platform_file,
178 const UUID *uuid_ptr, FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 // Default to the local case
180 local_file = platform_file;
Zachary Turner97206d52017-05-12 04:51:55 +0000181 return Status();
Greg Claytone996fd32011-03-08 22:40:15 +0000182}
183
Greg Clayton91c0e742013-01-11 23:44:27 +0000184FileSpecList
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185Platform::LocateExecutableScriptingResources(Target *target, Module &module,
186 Stream *feedback_stream) {
187 return FileSpecList();
Enrico Granata17598482012-11-08 02:22:02 +0000188}
189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190// PlatformSP
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000191// Platform::FindPlugin (Process *process, ConstString plugin_name)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000192//{
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000193// PlatformCreateInstance create_callback = nullptr;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000194// if (plugin_name)
195// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196// create_callback =
197// PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name);
Greg Clayton615eb7e2014-09-19 20:11:50 +0000198// if (create_callback)
199// {
200// ArchSpec arch;
201// if (process)
202// {
203// arch = process->GetTarget().GetArchitecture();
204// }
205// PlatformSP platform_sp(create_callback(process, &arch));
206// if (platform_sp)
207// return platform_sp;
208// }
209// }
210// else
211// {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212// for (uint32_t idx = 0; (create_callback =
213// PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != nullptr;
214// ++idx)
Greg Clayton615eb7e2014-09-19 20:11:50 +0000215// {
216// PlatformSP platform_sp(create_callback(process, nullptr));
217// if (platform_sp)
218// return platform_sp;
219// }
220// }
221// return PlatformSP();
222//}
Jason Molenda1c627542013-04-05 01:03:25 +0000223
Zachary Turner97206d52017-05-12 04:51:55 +0000224Status Platform::GetSharedModule(const ModuleSpec &module_spec,
225 Process *process, ModuleSP &module_sp,
226 const FileSpecList *module_search_paths_ptr,
227 ModuleSP *old_module_sp_ptr,
228 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 if (IsHost())
230 return ModuleList::GetSharedModule(
231 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
232 did_create_ptr, false);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000233
Pavel Labath1f8639a2018-08-28 16:32:46 +0000234 // Module resolver lambda.
235 auto resolver = [&](const ModuleSpec &spec) {
236 Status error(eErrorTypeGeneric);
237 ModuleSpec resolved_spec;
238 // Check if we have sysroot set.
239 if (m_sdk_sysroot) {
240 // Prepend sysroot to module spec.
241 resolved_spec = spec;
242 resolved_spec.GetFileSpec().PrependPathComponent(
243 m_sdk_sysroot.GetStringRef());
244 // Try to get shared module with resolved spec.
245 error = ModuleList::GetSharedModule(
246 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
247 did_create_ptr, false);
248 }
249 // If we don't have sysroot or it didn't work then
250 // try original module spec.
251 if (!error.Success()) {
252 resolved_spec = spec;
253 error = ModuleList::GetSharedModule(
254 resolved_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
255 did_create_ptr, false);
256 }
257 if (error.Success() && module_sp)
258 module_sp->SetPlatformFileSpec(resolved_spec.GetFileSpec());
259 return error;
260 };
261
262 return GetRemoteSharedModule(module_spec, process, module_sp, resolver,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 did_create_ptr);
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000264}
265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266bool Platform::GetModuleSpec(const FileSpec &module_file_spec,
267 const ArchSpec &arch, ModuleSpec &module_spec) {
268 ModuleSpecList module_specs;
269 if (ObjectFile::GetModuleSpecifications(module_file_spec, 0, 0,
270 module_specs) == 0)
271 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 ModuleSpec matched_module_spec;
274 return module_specs.FindMatchingModuleSpec(ModuleSpec(module_file_spec, arch),
275 module_spec);
Greg Clayton32e0a752011-03-30 18:16:51 +0000276}
277
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000278PlatformSP Platform::Find(ConstString name) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 if (name) {
280 static ConstString g_host_platform_name("host");
281 if (name == g_host_platform_name)
282 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
285 for (const auto &platform_sp : GetPlatformList()) {
286 if (platform_sp->GetName() == name)
287 return platform_sp;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000288 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 }
290 return PlatformSP();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000291}
292
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000293PlatformSP Platform::Create(ConstString name, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 PlatformCreateInstance create_callback = nullptr;
295 lldb::PlatformSP platform_sp;
296 if (name) {
297 static ConstString g_host_platform_name("host");
298 if (name == g_host_platform_name)
299 return GetHostPlatform();
Greg Clayton615eb7e2014-09-19 20:11:50 +0000300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 create_callback =
302 PluginManager::GetPlatformCreateCallbackForPluginName(name);
303 if (create_callback)
304 platform_sp = create_callback(true, nullptr);
Greg Claytone996fd32011-03-08 22:40:15 +0000305 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 error.SetErrorStringWithFormat(
307 "unable to find a plug-in for the platform named \"%s\"",
308 name.GetCString());
309 } else
310 error.SetErrorString("invalid platform name");
Greg Clayton615eb7e2014-09-19 20:11:50 +0000311
Kate Stoneb9c1b512016-09-06 20:57:50 +0000312 if (platform_sp) {
313 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
314 GetPlatformList().push_back(platform_sp);
315 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 return platform_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000318}
319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320PlatformSP Platform::Create(const ArchSpec &arch, ArchSpec *platform_arch_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +0000321 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 lldb::PlatformSP platform_sp;
323 if (arch.IsValid()) {
324 // Scope for locker
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000325 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 // First try exact arch matches across all platforms already created
327 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
328 for (const auto &platform_sp : GetPlatformList()) {
329 if (platform_sp->IsCompatibleArchitecture(arch, true,
330 platform_arch_ptr))
331 return platform_sp;
332 }
Greg Clayton615eb7e2014-09-19 20:11:50 +0000333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 // Next try compatible arch matches across all platforms already created
335 for (const auto &platform_sp : GetPlatformList()) {
336 if (platform_sp->IsCompatibleArchitecture(arch, false,
337 platform_arch_ptr))
338 return platform_sp;
339 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000340 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341
342 PlatformCreateInstance create_callback;
343 // First try exact arch matches across all platform plug-ins
344 uint32_t idx;
345 for (idx = 0; (create_callback =
346 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
347 ++idx) {
348 if (create_callback) {
349 platform_sp = create_callback(false, &arch);
350 if (platform_sp &&
351 platform_sp->IsCompatibleArchitecture(arch, true,
352 platform_arch_ptr)) {
353 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
354 GetPlatformList().push_back(platform_sp);
355 return platform_sp;
356 }
357 }
358 }
359 // Next try compatible arch matches across all platform plug-ins
360 for (idx = 0; (create_callback =
361 PluginManager::GetPlatformCreateCallbackAtIndex(idx));
362 ++idx) {
363 if (create_callback) {
364 platform_sp = create_callback(false, &arch);
365 if (platform_sp &&
366 platform_sp->IsCompatibleArchitecture(arch, false,
367 platform_arch_ptr)) {
368 std::lock_guard<std::recursive_mutex> guard(GetPlatformListMutex());
369 GetPlatformList().push_back(platform_sp);
370 return platform_sp;
371 }
372 }
373 }
374 } else
375 error.SetErrorString("invalid platform name");
376 if (platform_arch_ptr)
377 platform_arch_ptr->Clear();
378 platform_sp.reset();
379 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000380}
381
Pavel Labath7263f1b2017-10-31 10:56:03 +0000382ArchSpec Platform::GetAugmentedArchSpec(Platform *platform, llvm::StringRef triple) {
383 if (platform)
384 return platform->GetAugmentedArchSpec(triple);
385 return HostInfo::GetAugmentedArchSpec(triple);
386}
387
Greg Claytone996fd32011-03-08 22:40:15 +0000388/// Default Constructor
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000389Platform::Platform(bool is_host)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 : m_is_host(is_host), m_os_version_set_while_connected(false),
391 m_system_arch_set_while_connected(false), m_sdk_sysroot(), m_sdk_build(),
Pavel Labath2272c482018-06-18 15:02:23 +0000392 m_working_dir(), m_remote_url(), m_name(), m_system_arch(), m_mutex(),
Pavel Labathaa51e6a2019-03-04 18:48:00 +0000393 m_max_uid_name_len(0), m_max_gid_name_len(0), m_supports_rsync(false),
394 m_rsync_opts(), m_rsync_prefix(), m_supports_ssh(false), m_ssh_opts(),
395 m_ignores_remote_hostname(false), m_trap_handlers(),
396 m_calculated_trap_handlers(false),
Jonas Devliegherea8f3ae72019-08-14 22:19:23 +0000397 m_module_cache(std::make_unique<ModuleCache>()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000399 LLDB_LOGF(log, "%p Platform::Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000400}
401
Greg Claytone996fd32011-03-08 22:40:15 +0000402/// Destructor.
403///
404/// The destructor is virtual since this class is designed to be
405/// inherited from by the plug-in instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406Platform::~Platform() {
407 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000408 LLDB_LOGF(log, "%p Platform::~Platform()", static_cast<void *>(this));
Greg Claytone996fd32011-03-08 22:40:15 +0000409}
410
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411void Platform::GetStatus(Stream &strm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 std::string s;
413 strm.Printf(" Platform: %s\n", GetPluginName().GetCString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 ArchSpec arch(GetSystemArchitecture());
416 if (arch.IsValid()) {
417 if (!arch.GetTriple().str().empty()) {
418 strm.Printf(" Triple: ");
Raphael Isemann2f1e7b32019-12-04 08:27:43 +0100419 arch.DumpTriple(strm.AsRawOstream());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 strm.EOL();
Greg Clayton1cb64962011-03-24 04:28:38 +0000421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000423
Pavel Labath2272c482018-06-18 15:02:23 +0000424 llvm::VersionTuple os_version = GetOSVersion();
425 if (!os_version.empty()) {
426 strm.Format("OS Version: {0}", os_version.getAsString());
Greg Clayton1cb64962011-03-24 04:28:38 +0000427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 if (GetOSBuildString(s))
429 strm.Printf(" (%s)", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 strm.EOL();
432 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000433
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 if (IsHost()) {
435 strm.Printf(" Hostname: %s\n", GetHostname());
436 } else {
437 const bool is_connected = IsConnected();
438 if (is_connected)
439 strm.Printf(" Hostname: %s\n", GetHostname());
440 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no");
441 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000442
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 if (GetWorkingDirectory()) {
444 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString());
445 }
446 if (!IsConnected())
447 return;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 std::string specific_info(GetPlatformSpecificConnectionInformation());
450
451 if (!specific_info.empty())
452 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str());
Jonas Devlieghered3bdd512020-01-31 14:23:56 -0800453
454 if (GetOSKernelDescription(s))
455 strm.Printf(" Kernel: %s\n", s.c_str());
Greg Clayton1cb64962011-03-24 04:28:38 +0000456}
457
Pavel Labath2272c482018-06-18 15:02:23 +0000458llvm::VersionTuple Platform::GetOSVersion(Process *process) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 std::lock_guard<std::mutex> guard(m_mutex);
Greg Clayton7597b352015-02-02 20:45:17 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 if (IsHost()) {
Pavel Labath2272c482018-06-18 15:02:23 +0000462 if (m_os_version.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 // We have a local host platform
Pavel Labath2272c482018-06-18 15:02:23 +0000464 m_os_version = HostInfo::GetOSVersion();
465 m_os_version_set_while_connected = !m_os_version.empty();
Greg Claytonded470d2011-03-19 01:12:21 +0000466 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 } else {
468 // We have a remote platform. We can only fetch the remote
469 // OS version if we are connected, and we don't want to do it
470 // more than once.
Greg Claytonded470d2011-03-19 01:12:21 +0000471
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000473
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 bool fetch = false;
Pavel Labath2272c482018-06-18 15:02:23 +0000475 if (!m_os_version.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000476 // We have valid OS version info, check to make sure it wasn't manually
477 // set prior to connecting. If it was manually set prior to connecting,
478 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 if (is_connected && !m_os_version_set_while_connected)
480 fetch = true;
481 } else {
482 // We don't have valid OS version info, fetch it if we are connected
483 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000484 }
485
Pavel Labath2272c482018-06-18 15:02:23 +0000486 if (fetch)
487 m_os_version_set_while_connected = GetRemoteOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 }
489
Pavel Labath2272c482018-06-18 15:02:23 +0000490 if (!m_os_version.empty())
491 return m_os_version;
492 if (process) {
Adrian Prantl05097242018-04-30 16:49:04 +0000493 // Check with the process in case it can answer the question if a process
494 // was provided
Pavel Labath2272c482018-06-18 15:02:23 +0000495 return process->GetHostOSVersion();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 }
Pavel Labath2272c482018-06-18 15:02:23 +0000497 return llvm::VersionTuple();
Greg Claytonded470d2011-03-19 01:12:21 +0000498}
Greg Clayton1cb64962011-03-24 04:28:38 +0000499
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500bool Platform::GetOSBuildString(std::string &s) {
501 s.clear();
Zachary Turner97a14e62014-08-19 17:18:29 +0000502
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000504#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505 return HostInfo::GetOSBuildString(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000506#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000508#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 else
510 return GetRemoteOSBuildString(s);
Greg Clayton1cb64962011-03-24 04:28:38 +0000511}
512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513bool Platform::GetOSKernelDescription(std::string &s) {
514 if (IsHost())
Zachary Turner97a14e62014-08-19 17:18:29 +0000515#if !defined(__linux__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 return HostInfo::GetOSKernelDescription(s);
Zachary Turner97a14e62014-08-19 17:18:29 +0000517#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 return false;
Zachary Turner97a14e62014-08-19 17:18:29 +0000519#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 else
521 return GetRemoteOSKernelDescription(s);
522}
523
524void Platform::AddClangModuleCompilationOptions(
525 Target *target, std::vector<std::string> &options) {
526 std::vector<std::string> default_compilation_options = {
527 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc"};
528
529 options.insert(options.end(), default_compilation_options.begin(),
530 default_compilation_options.end());
531}
532
533FileSpec Platform::GetWorkingDirectory() {
534 if (IsHost()) {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000535 llvm::SmallString<64> cwd;
536 if (llvm::sys::fs::current_path(cwd))
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000537 return {};
538 else {
539 FileSpec file_spec(cwd);
540 FileSystem::Instance().Resolve(file_spec);
541 return file_spec;
542 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 } else {
544 if (!m_working_dir)
545 m_working_dir = GetRemoteWorkingDirectory();
546 return m_working_dir;
547 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000548}
549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550struct RecurseCopyBaton {
551 const FileSpec &dst;
552 Platform *platform_ptr;
Zachary Turner97206d52017-05-12 04:51:55 +0000553 Status error;
Greg Claytonfbb76342013-11-20 21:07:01 +0000554};
555
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000556static FileSystem::EnumerateDirectoryResult
Zachary Turner7d86ee52017-03-08 17:56:08 +0000557RecurseCopy_Callback(void *baton, llvm::sys::fs::file_type ft,
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000558 llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 RecurseCopyBaton *rc_baton = (RecurseCopyBaton *)baton;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000560 FileSpec src(path);
Zachary Turner7d86ee52017-03-08 17:56:08 +0000561 namespace fs = llvm::sys::fs;
562 switch (ft) {
563 case fs::file_type::fifo_file:
564 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 // we have no way to copy pipes and sockets - ignore them and continue
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000566 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567 break;
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000568
Zachary Turner7d86ee52017-03-08 17:56:08 +0000569 case fs::file_type::directory_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 // make the new directory and get in there
571 FileSpec dst_dir = rc_baton->dst;
572 if (!dst_dir.GetFilename())
573 dst_dir.GetFilename() = src.GetLastPathComponent();
Zachary Turner97206d52017-05-12 04:51:55 +0000574 Status error = rc_baton->platform_ptr->MakeDirectory(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 dst_dir, lldb::eFilePermissionsDirectoryDefault);
576 if (error.Fail()) {
577 rc_baton->error.SetErrorStringWithFormat(
578 "unable to setup directory %s on remote end", dst_dir.GetCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000579 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Greg Claytonfbb76342013-11-20 21:07:01 +0000580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581
582 // now recurse
583 std::string src_dir_path(src.GetPath());
584
Adrian Prantl05097242018-04-30 16:49:04 +0000585 // Make a filespec that only fills in the directory of a FileSpec so when
586 // we enumerate we can quickly fill in the filename for dst copies
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 FileSpec recurse_dst;
588 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str());
Zachary Turner97206d52017-05-12 04:51:55 +0000589 RecurseCopyBaton rc_baton2 = {recurse_dst, rc_baton->platform_ptr,
590 Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000591 FileSystem::Instance().EnumerateDirectory(src_dir_path, true, true, true,
592 RecurseCopy_Callback, &rc_baton2);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 if (rc_baton2.error.Fail()) {
594 rc_baton->error.SetErrorString(rc_baton2.error.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000595 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000597 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 } break;
599
Zachary Turner7d86ee52017-03-08 17:56:08 +0000600 case fs::file_type::symlink_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601 // copy the file and keep going
602 FileSpec dst_file = rc_baton->dst;
603 if (!dst_file.GetFilename())
604 dst_file.GetFilename() = src.GetFilename();
605
606 FileSpec src_resolved;
607
Jonas Devlieghere46376962018-10-31 21:49:27 +0000608 rc_baton->error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609
610 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000611 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612
613 rc_baton->error =
614 rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved);
615
616 if (rc_baton->error.Fail())
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000617 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000619 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 } break;
621
Zachary Turner7d86ee52017-03-08 17:56:08 +0000622 case fs::file_type::regular_file: {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 // copy the file and keep going
624 FileSpec dst_file = rc_baton->dst;
625 if (!dst_file.GetFilename())
626 dst_file.GetFilename() = src.GetFilename();
Zachary Turner97206d52017-05-12 04:51:55 +0000627 Status err = rc_baton->platform_ptr->PutFile(src, dst_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 if (err.Fail()) {
629 rc_baton->error.SetErrorString(err.AsCString());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000630 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 }
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000632 return FileSystem::eEnumerateDirectoryResultNext;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 } break;
634
Zachary Turner7d86ee52017-03-08 17:56:08 +0000635 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 rc_baton->error.SetErrorStringWithFormat(
637 "invalid file detected during copy: %s", src.GetPath().c_str());
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000638 return FileSystem::eEnumerateDirectoryResultQuit; // got an error, bail out
Kate Stoneb9c1b512016-09-06 20:57:50 +0000639 break;
640 }
Zachary Turner7d86ee52017-03-08 17:56:08 +0000641 llvm_unreachable("Unhandled file_type!");
Greg Claytonfbb76342013-11-20 21:07:01 +0000642}
643
Zachary Turner97206d52017-05-12 04:51:55 +0000644Status Platform::Install(const FileSpec &src, const FileSpec &dst) {
645 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646
647 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000648 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s')",
649 src.GetPath().c_str(), dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 FileSpec fixed_dst(dst);
651
652 if (!fixed_dst.GetFilename())
653 fixed_dst.GetFilename() = src.GetFilename();
654
655 FileSpec working_dir = GetWorkingDirectory();
656
657 if (dst) {
658 if (dst.GetDirectory()) {
659 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0];
660 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') {
661 fixed_dst.GetDirectory() = dst.GetDirectory();
662 }
Adrian Prantl05097242018-04-30 16:49:04 +0000663 // If the fixed destination file doesn't have a directory yet, then we
664 // must have a relative path. We will resolve this relative path against
665 // the platform's working directory
Kate Stoneb9c1b512016-09-06 20:57:50 +0000666 if (!fixed_dst.GetDirectory()) {
667 FileSpec relative_spec;
668 std::string path;
669 if (working_dir) {
670 relative_spec = working_dir;
671 relative_spec.AppendPathComponent(dst.GetPath());
672 fixed_dst.GetDirectory() = relative_spec.GetDirectory();
673 } else {
674 error.SetErrorStringWithFormat(
675 "platform working directory must be valid for relative path '%s'",
676 dst.GetPath().c_str());
677 return error;
678 }
679 }
680 } else {
681 if (working_dir) {
682 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
683 } else {
684 error.SetErrorStringWithFormat(
685 "platform working directory must be valid for relative path '%s'",
686 dst.GetPath().c_str());
687 return error;
688 }
689 }
690 } else {
691 if (working_dir) {
692 fixed_dst.GetDirectory().SetCString(working_dir.GetCString());
693 } else {
694 error.SetErrorStringWithFormat("platform working directory must be valid "
695 "when destination directory is empty");
696 return error;
697 }
698 }
699
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000700 LLDB_LOGF(log, "Platform::Install (src='%s', dst='%s') fixed_dst='%s'",
701 src.GetPath().c_str(), dst.GetPath().c_str(),
702 fixed_dst.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703
704 if (GetSupportsRSync()) {
705 error = PutFile(src, dst);
706 } else {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000707 namespace fs = llvm::sys::fs;
708 switch (fs::get_file_type(src.GetPath(), false)) {
709 case fs::file_type::directory_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000710 llvm::sys::fs::remove(fixed_dst.GetPath());
Jonas Devlieghere7c5310b2018-11-01 15:47:33 +0000711 uint32_t permissions = FileSystem::Instance().GetPermissions(src);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 if (permissions == 0)
713 permissions = eFilePermissionsDirectoryDefault;
714 error = MakeDirectory(fixed_dst, permissions);
715 if (error.Success()) {
716 // Make a filespec that only fills in the directory of a FileSpec so
717 // when we enumerate we can quickly fill in the filename for dst copies
718 FileSpec recurse_dst;
719 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString());
720 std::string src_dir_path(src.GetPath());
Zachary Turner97206d52017-05-12 04:51:55 +0000721 RecurseCopyBaton baton = {recurse_dst, this, Status()};
Jonas Devlieghere35e4c842018-11-01 00:33:27 +0000722 FileSystem::Instance().EnumerateDirectory(
723 src_dir_path, true, true, true, RecurseCopy_Callback, &baton);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 return baton.error;
725 }
726 } break;
727
Zachary Turner7d86ee52017-03-08 17:56:08 +0000728 case fs::file_type::regular_file:
Zachary Turner07db3f72017-03-21 05:47:57 +0000729 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 error = PutFile(src, fixed_dst);
731 break;
732
Zachary Turner7d86ee52017-03-08 17:56:08 +0000733 case fs::file_type::symlink_file: {
Zachary Turner07db3f72017-03-21 05:47:57 +0000734 llvm::sys::fs::remove(fixed_dst.GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 FileSpec src_resolved;
Jonas Devlieghere46376962018-10-31 21:49:27 +0000736 error = FileSystem::Instance().Readlink(src, src_resolved);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 if (error.Success())
738 error = CreateSymlink(dst, src_resolved);
739 } break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000740 case fs::file_type::fifo_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 error.SetErrorString("platform install doesn't handle pipes");
742 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000743 case fs::file_type::socket_file:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 error.SetErrorString("platform install doesn't handle sockets");
745 break;
Zachary Turner7d86ee52017-03-08 17:56:08 +0000746 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 error.SetErrorString(
748 "platform install doesn't handle non file or directory items");
749 break;
750 }
751 }
752 return error;
753}
754
755bool Platform::SetWorkingDirectory(const FileSpec &file_spec) {
756 if (IsHost()) {
757 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000758 LLDB_LOG(log, "{0}", file_spec);
759 if (std::error_code ec = llvm::sys::fs::set_current_path(file_spec.GetPath())) {
760 LLDB_LOG(log, "error: {0}", ec.message());
761 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 }
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000763 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 } else {
765 m_working_dir.Clear();
766 return SetRemoteWorkingDirectory(file_spec);
767 }
768}
769
Zachary Turner97206d52017-05-12 04:51:55 +0000770Status Platform::MakeDirectory(const FileSpec &file_spec,
771 uint32_t permissions) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000772 if (IsHost())
Zachary Turnerd3d95fd2017-03-19 05:48:47 +0000773 return llvm::sys::fs::create_directory(file_spec.GetPath(), permissions);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 else {
Zachary Turner97206d52017-05-12 04:51:55 +0000775 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
777 GetPluginName().GetCString(),
778 LLVM_PRETTY_FUNCTION);
Greg Claytonfbb76342013-11-20 21:07:01 +0000779 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000780 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000781}
782
Zachary Turner97206d52017-05-12 04:51:55 +0000783Status Platform::GetFilePermissions(const FileSpec &file_spec,
784 uint32_t &file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000785 if (IsHost()) {
786 auto Value = llvm::sys::fs::getPermissions(file_spec.GetPath());
787 if (Value)
788 file_permissions = Value.get();
Zachary Turner97206d52017-05-12 04:51:55 +0000789 return Status(Value.getError());
Zachary Turner6934e0a2017-03-19 05:49:43 +0000790 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000791 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
793 GetPluginName().GetCString(),
794 LLVM_PRETTY_FUNCTION);
795 return error;
796 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000797}
798
Zachary Turner97206d52017-05-12 04:51:55 +0000799Status Platform::SetFilePermissions(const FileSpec &file_spec,
800 uint32_t file_permissions) {
Zachary Turner6934e0a2017-03-19 05:49:43 +0000801 if (IsHost()) {
802 auto Perms = static_cast<llvm::sys::fs::perms>(file_permissions);
803 return llvm::sys::fs::setPermissions(file_spec.GetPath(), Perms);
804 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000805 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000806 error.SetErrorStringWithFormat("remote platform %s doesn't support %s",
807 GetPluginName().GetCString(),
808 LLVM_PRETTY_FUNCTION);
809 return error;
810 }
Greg Claytonfbb76342013-11-20 21:07:01 +0000811}
812
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813ConstString Platform::GetName() { return GetPluginName(); }
814
815const char *Platform::GetHostname() {
816 if (IsHost())
817 return "127.0.0.1";
818
819 if (m_name.empty())
820 return nullptr;
821 return m_name.c_str();
Greg Claytonfbb76342013-11-20 21:07:01 +0000822}
823
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824ConstString Platform::GetFullNameForDylib(ConstString basename) {
825 return basename;
Greg Claytonfbb76342013-11-20 21:07:01 +0000826}
827
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828bool Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) {
829 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000830 LLDB_LOGF(log, "Platform::SetRemoteWorkingDirectory('%s')",
831 working_dir.GetCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 m_working_dir = working_dir;
833 return true;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000834}
835
Pavel Labath2272c482018-06-18 15:02:23 +0000836bool Platform::SetOSVersion(llvm::VersionTuple version) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000837 if (IsHost()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000838 // We don't need anyone setting the OS version for the host platform, we
839 // should be able to figure it out by calling HostInfo::GetOSVersion(...).
Greg Claytonded470d2011-03-19 01:12:21 +0000840 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000842 // We have a remote platform, allow setting the target OS version if we
843 // aren't connected, since if we are connected, we should be able to
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 // request the remote OS version from the connected platform.
845 if (IsConnected())
846 return false;
847 else {
Adrian Prantl05097242018-04-30 16:49:04 +0000848 // We aren't connected and we might want to set the OS version ahead of
849 // time before we connect so we can peruse files and use a local SDK or
850 // PDK cache of support files to disassemble or do other things.
Pavel Labath2272c482018-06-18 15:02:23 +0000851 m_os_version = version;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000852 return true;
Greg Claytone996fd32011-03-08 22:40:15 +0000853 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000854 }
855 return false;
856}
857
Zachary Turner97206d52017-05-12 04:51:55 +0000858Status
859Platform::ResolveExecutable(const ModuleSpec &module_spec,
860 lldb::ModuleSP &exe_module_sp,
861 const FileSpecList *module_search_paths_ptr) {
862 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000863 if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864 if (module_spec.GetArchitecture().IsValid()) {
865 error = ModuleList::GetSharedModule(module_spec, exe_module_sp,
866 module_search_paths_ptr, nullptr,
867 nullptr);
868 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000869 // No valid architecture was specified, ask the platform for the
870 // architectures that we should be using (in the correct order) and see
871 // if we can find a match that way
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 ModuleSpec arch_module_spec(module_spec);
873 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
874 idx, arch_module_spec.GetArchitecture());
875 ++idx) {
876 error = ModuleList::GetSharedModule(arch_module_spec, exe_module_sp,
877 module_search_paths_ptr, nullptr,
878 nullptr);
879 // Did we find an executable using one of the
880 if (error.Success() && exe_module_sp)
881 break;
882 }
Greg Claytone996fd32011-03-08 22:40:15 +0000883 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884 } else {
885 error.SetErrorStringWithFormat("'%s' does not exist",
886 module_spec.GetFileSpec().GetPath().c_str());
887 }
888 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000889}
890
Zachary Turner97206d52017-05-12 04:51:55 +0000891Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,
892 FileSpec &sym_file) {
893 Status error;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000894 if (FileSystem::Instance().Exists(sym_spec.GetSymbolFileSpec()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 sym_file = sym_spec.GetSymbolFileSpec();
896 else
897 error.SetErrorString("unable to resolve symbol file");
898 return error;
Greg Claytonaa516842011-08-11 16:25:18 +0000899}
900
Kate Stoneb9c1b512016-09-06 20:57:50 +0000901bool Platform::ResolveRemotePath(const FileSpec &platform_path,
902 FileSpec &resolved_platform_path) {
903 resolved_platform_path = platform_path;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000904 FileSystem::Instance().Resolve(resolved_platform_path);
905 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906}
907
908const ArchSpec &Platform::GetSystemArchitecture() {
909 if (IsHost()) {
910 if (!m_system_arch.IsValid()) {
911 // We have a local host platform
912 m_system_arch = HostInfo::GetArchitecture();
913 m_system_arch_set_while_connected = m_system_arch.IsValid();
Greg Claytonded470d2011-03-19 01:12:21 +0000914 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000916 // We have a remote platform. We can only fetch the remote system
917 // architecture if we are connected, and we don't want to do it more than
918 // once.
Greg Claytonded470d2011-03-19 01:12:21 +0000919
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920 const bool is_connected = IsConnected();
Greg Claytonded470d2011-03-19 01:12:21 +0000921
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 bool fetch = false;
923 if (m_system_arch.IsValid()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000924 // We have valid OS version info, check to make sure it wasn't manually
925 // set prior to connecting. If it was manually set prior to connecting,
926 // then lets fetch the actual OS version info if we are now connected.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000927 if (is_connected && !m_system_arch_set_while_connected)
928 fetch = true;
929 } else {
930 // We don't have valid OS version info, fetch it if we are connected
931 fetch = is_connected;
Greg Claytonded470d2011-03-19 01:12:21 +0000932 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933
934 if (fetch) {
935 m_system_arch = GetRemoteSystemArchitecture();
936 m_system_arch_set_while_connected = m_system_arch.IsValid();
937 }
938 }
939 return m_system_arch;
Greg Claytonded470d2011-03-19 01:12:21 +0000940}
941
Pavel Labath7263f1b2017-10-31 10:56:03 +0000942ArchSpec Platform::GetAugmentedArchSpec(llvm::StringRef triple) {
943 if (triple.empty())
944 return ArchSpec();
945 llvm::Triple normalized_triple(llvm::Triple::normalize(triple));
946 if (!ArchSpec::ContainsOnlyArch(normalized_triple))
947 return ArchSpec(triple);
948
Pavel Labath4ebb64b2017-11-13 15:57:20 +0000949 if (auto kind = HostInfo::ParseArchitectureKind(triple))
950 return HostInfo::GetArchitecture(*kind);
951
Pavel Labath7263f1b2017-10-31 10:56:03 +0000952 ArchSpec compatible_arch;
953 ArchSpec raw_arch(triple);
954 if (!IsCompatibleArchitecture(raw_arch, false, &compatible_arch))
955 return raw_arch;
956
957 if (!compatible_arch.IsValid())
958 return ArchSpec(normalized_triple);
959
960 const llvm::Triple &compatible_triple = compatible_arch.GetTriple();
961 if (normalized_triple.getVendorName().empty())
962 normalized_triple.setVendor(compatible_triple.getVendor());
963 if (normalized_triple.getOSName().empty())
964 normalized_triple.setOS(compatible_triple.getOS());
965 if (normalized_triple.getEnvironmentName().empty())
966 normalized_triple.setEnvironment(compatible_triple.getEnvironment());
967 return ArchSpec(normalized_triple);
968}
969
Zachary Turner97206d52017-05-12 04:51:55 +0000970Status Platform::ConnectRemote(Args &args) {
971 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 if (IsHost())
973 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
974 "the host platform and is always connected.",
975 GetPluginName().GetCString());
976 else
977 error.SetErrorStringWithFormat(
978 "Platform::ConnectRemote() is not supported by %s",
979 GetPluginName().GetCString());
980 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000981}
982
Zachary Turner97206d52017-05-12 04:51:55 +0000983Status Platform::DisconnectRemote() {
984 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 if (IsHost())
986 error.SetErrorStringWithFormat("The currently selected platform (%s) is "
987 "the host platform and is always connected.",
988 GetPluginName().GetCString());
989 else
990 error.SetErrorStringWithFormat(
991 "Platform::DisconnectRemote() is not supported by %s",
992 GetPluginName().GetCString());
993 return error;
Greg Claytone996fd32011-03-08 22:40:15 +0000994}
Greg Clayton32e0a752011-03-30 18:16:51 +0000995
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996bool Platform::GetProcessInfo(lldb::pid_t pid,
997 ProcessInstanceInfo &process_info) {
Adrian Prantl05097242018-04-30 16:49:04 +0000998 // Take care of the host case so that each subclass can just call this
999 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 if (IsHost())
1001 return Host::GetProcessInfo(pid, process_info);
1002 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00001003}
1004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005uint32_t Platform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
1006 ProcessInstanceInfoList &process_infos) {
Adrian Prantl05097242018-04-30 16:49:04 +00001007 // Take care of the host case so that each subclass can just call this
1008 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 uint32_t match_count = 0;
1010 if (IsHost())
1011 match_count = Host::FindProcesses(match_info, process_infos);
1012 return match_count;
Greg Clayton32e0a752011-03-30 18:16:51 +00001013}
Greg Clayton8b82f082011-04-12 05:54:46 +00001014
Zachary Turner97206d52017-05-12 04:51:55 +00001015Status Platform::LaunchProcess(ProcessLaunchInfo &launch_info) {
1016 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001017 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001018 LLDB_LOGF(log, "Platform::%s()", __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019
Adrian Prantl05097242018-04-30 16:49:04 +00001020 // Take care of the host case so that each subclass can just call this
1021 // function to get the host functionality.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001022 if (IsHost()) {
1023 if (::getenv("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY"))
1024 launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
1025
1026 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInShell)) {
1027 const bool is_localhost = true;
1028 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug);
1029 const bool first_arg_is_full_shell_command = false;
1030 uint32_t num_resumes = GetResumeCountForLaunchInfo(launch_info);
1031 if (log) {
1032 const FileSpec &shell = launch_info.GetShell();
Jason Molenda3b687612019-02-08 23:36:25 +00001033 std::string shell_str = (shell) ? shell.GetPath() : "<null>";
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001034 LLDB_LOGF(log,
1035 "Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32
1036 ", shell is '%s'",
1037 __FUNCTION__, num_resumes, shell_str.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 }
1039
1040 if (!launch_info.ConvertArgumentsForLaunchingInShell(
1041 error, is_localhost, will_debug, first_arg_is_full_shell_command,
1042 num_resumes))
1043 return error;
1044 } else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) {
1045 error = ShellExpandArguments(launch_info);
1046 if (error.Fail()) {
1047 error.SetErrorStringWithFormat("shell expansion failed (reason: %s). "
1048 "consider launching with 'process "
1049 "launch'.",
1050 error.AsCString("unknown"));
1051 return error;
1052 }
1053 }
1054
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001055 LLDB_LOGF(log, "Platform::%s final launch_info resume count: %" PRIu32,
1056 __FUNCTION__, launch_info.GetResumeCount());
Todd Fialaac33cc92014-10-09 01:02:08 +00001057
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 error = Host::LaunchProcess(launch_info);
1059 } else
1060 error.SetErrorString(
1061 "base lldb_private::Platform class can't launch remote processes");
1062 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001063}
1064
Zachary Turner97206d52017-05-12 04:51:55 +00001065Status Platform::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 if (IsHost())
1067 return Host::ShellExpandArguments(launch_info);
Zachary Turner97206d52017-05-12 04:51:55 +00001068 return Status("base lldb_private::Platform class can't expand arguments");
Enrico Granata83a14372015-02-20 21:48:38 +00001069}
1070
Zachary Turner97206d52017-05-12 04:51:55 +00001071Status Platform::KillProcess(const lldb::pid_t pid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001073 LLDB_LOGF(log, "Platform::%s, pid %" PRIu64, __FUNCTION__, pid);
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001074
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 // Try to find a process plugin to handle this Kill request. If we can't,
Adrian Prantl05097242018-04-30 16:49:04 +00001076 // fall back to the default OS implementation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 size_t num_debuggers = Debugger::GetNumDebuggers();
1078 for (size_t didx = 0; didx < num_debuggers; ++didx) {
1079 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx);
1080 lldb_private::TargetList &targets = debugger->GetTargetList();
1081 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) {
1082 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP();
1083 if (process->GetID() == pid)
1084 return process->Destroy(true);
Zachary Turner7271bab2015-05-13 19:44:24 +00001085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 }
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001087
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 if (!IsHost()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001089 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090 "base lldb_private::Platform class can't kill remote processes unless "
1091 "they are controlled by a process plugin");
1092 }
1093 Host::Kill(pid, SIGTERM);
Zachary Turner97206d52017-05-12 04:51:55 +00001094 return Status();
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001095}
1096
Greg Clayton8b82f082011-04-12 05:54:46 +00001097lldb::ProcessSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098Platform::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
1099 Target *target, // Can be nullptr, if nullptr create a
1100 // new target, else use existing one
Zachary Turner97206d52017-05-12 04:51:55 +00001101 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001103 LLDB_LOGF(log, "Platform::%s entered (target %p)", __FUNCTION__,
1104 static_cast<void *>(target));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001105
1106 ProcessSP process_sp;
1107 // Make sure we stop at the entry point
1108 launch_info.GetFlags().Set(eLaunchFlagDebug);
1109 // We always launch the process we are going to debug in a separate process
Adrian Prantl05097242018-04-30 16:49:04 +00001110 // group, since then we can handle ^C interrupts ourselves w/o having to
1111 // worry about the target getting them as well.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 launch_info.SetLaunchInSeparateProcessGroup(true);
1113
1114 // Allow any StructuredData process-bound plugins to adjust the launch info
1115 // if needed
1116 size_t i = 0;
1117 bool iteration_complete = false;
Adrian Prantl05097242018-04-30 16:49:04 +00001118 // Note iteration can't simply go until a nullptr callback is returned, as it
1119 // is valid for a plugin to not supply a filter.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120 auto get_filter_func = PluginManager::GetStructuredDataFilterCallbackAtIndex;
1121 for (auto filter_callback = get_filter_func(i, iteration_complete);
1122 !iteration_complete;
1123 filter_callback = get_filter_func(++i, iteration_complete)) {
1124 if (filter_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001125 // Give this ProcessLaunchInfo filter a chance to adjust the launch info.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 error = (*filter_callback)(launch_info, target);
1127 if (!error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001128 LLDB_LOGF(log,
1129 "Platform::%s() StructuredDataPlugin launch "
1130 "filter failed.",
1131 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 return process_sp;
1133 }
1134 }
1135 }
1136
1137 error = LaunchProcess(launch_info);
1138 if (error.Success()) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001139 LLDB_LOGF(log,
1140 "Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")",
1141 __FUNCTION__, launch_info.GetProcessID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
1143 ProcessAttachInfo attach_info(launch_info);
1144 process_sp = Attach(attach_info, debugger, target, error);
1145 if (process_sp) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001146 LLDB_LOGF(log, "Platform::%s Attach() succeeded, Process plugin: %s",
1147 __FUNCTION__, process_sp->GetPluginName().AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 launch_info.SetHijackListener(attach_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00001149
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 // Since we attached to the process, it will think it needs to detach
1151 // if the process object just goes away without an explicit call to
1152 // Process::Kill() or Process::Detach(), so let it know to kill the
1153 // process if this happens.
1154 process_sp->SetShouldDetach(false);
1155
Adrian Prantl05097242018-04-30 16:49:04 +00001156 // If we didn't have any file actions, the pseudo terminal might have
Jonas Devlieghere64ec5052020-06-15 15:36:14 -07001157 // been used where the secondary side was given as the file to open for
Adrian Prantl05097242018-04-30 16:49:04 +00001158 // stdin/out/err after we have already opened the master so we can
1159 // read/write stdin/out/err.
Jonas Devlieghere64ec5052020-06-15 15:36:14 -07001160 int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
Pavel Labath07d6f882017-12-11 10:09:14 +00001161 if (pty_fd != PseudoTerminal::invalid_fd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162 process_sp->SetSTDIOFileDescriptor(pty_fd);
1163 }
1164 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001165 LLDB_LOGF(log, "Platform::%s Attach() failed: %s", __FUNCTION__,
1166 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167 }
1168 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001169 LLDB_LOGF(log,
1170 "Platform::%s LaunchProcess() returned launch_info with "
1171 "invalid process id",
1172 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173 }
1174 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001175 LLDB_LOGF(log, "Platform::%s LaunchProcess() failed: %s", __FUNCTION__,
1176 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 }
1178
1179 return process_sp;
Greg Clayton8b82f082011-04-12 05:54:46 +00001180}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001181
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001182lldb::PlatformSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183Platform::GetPlatformForArchitecture(const ArchSpec &arch,
1184 ArchSpec *platform_arch_ptr) {
1185 lldb::PlatformSP platform_sp;
Zachary Turner97206d52017-05-12 04:51:55 +00001186 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001187 if (arch.IsValid())
1188 platform_sp = Platform::Create(arch, platform_arch_ptr, error);
1189 return platform_sp;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001190}
1191
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001192/// Lets a platform answer if it is compatible with a given
1193/// architecture and the target triple contained within.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001194bool Platform::IsCompatibleArchitecture(const ArchSpec &arch,
1195 bool exact_arch_match,
1196 ArchSpec *compatible_arch_ptr) {
1197 // If the architecture is invalid, we must answer true...
1198 if (arch.IsValid()) {
1199 ArchSpec platform_arch;
1200 // Try for an exact architecture match first.
1201 if (exact_arch_match) {
1202 for (uint32_t arch_idx = 0;
1203 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1204 ++arch_idx) {
1205 if (arch.IsExactMatch(platform_arch)) {
1206 if (compatible_arch_ptr)
1207 *compatible_arch_ptr = platform_arch;
1208 return true;
Greg Clayton1e0c8842013-01-11 20:49:54 +00001209 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 }
1211 } else {
1212 for (uint32_t arch_idx = 0;
1213 GetSupportedArchitectureAtIndex(arch_idx, platform_arch);
1214 ++arch_idx) {
1215 if (arch.IsCompatibleMatch(platform_arch)) {
1216 if (compatible_arch_ptr)
1217 *compatible_arch_ptr = platform_arch;
1218 return true;
Greg Clayton70512312012-05-08 01:45:38 +00001219 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 }
1223 if (compatible_arch_ptr)
1224 compatible_arch_ptr->Clear();
1225 return false;
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001226}
1227
Zachary Turner97206d52017-05-12 04:51:55 +00001228Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
1229 uint32_t uid, uint32_t gid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001231 LLDB_LOGF(log, "[PutFile] Using block by block transfer....\n");
Vince Harron1b5a74e2015-01-21 22:42:49 +00001232
Lawrence D'Anna62c9fe42019-10-14 20:15:34 +00001233 auto source_open_options =
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234 File::eOpenOptionRead | File::eOpenOptionCloseOnExec;
Zachary Turner7d86ee52017-03-08 17:56:08 +00001235 namespace fs = llvm::sys::fs;
1236 if (fs::is_symlink_file(source.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237 source_open_options |= File::eOpenOptionDontFollowSymlinks;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001238
Lawrence D'Anna7ca15ba2019-09-27 14:33:35 +00001239 auto source_file = FileSystem::Instance().Open(source, source_open_options,
1240 lldb::eFilePermissionsUserRW);
Lawrence D'Anna2fce1132019-09-26 17:54:59 +00001241 if (!source_file)
1242 return Status(source_file.takeError());
1243 Status error;
1244 uint32_t permissions = source_file.get()->GetPermissions(error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001245 if (permissions == 0)
1246 permissions = lldb::eFilePermissionsFileDefault;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001247
Kate Stoneb9c1b512016-09-06 20:57:50 +00001248 lldb::user_id_t dest_file = OpenFile(
1249 destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
1250 File::eOpenOptionTruncate | File::eOpenOptionCloseOnExec,
1251 permissions, error);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001252 LLDB_LOGF(log, "dest_file = %" PRIu64 "\n", dest_file);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001253
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 if (error.Fail())
1255 return error;
1256 if (dest_file == UINT64_MAX)
Zachary Turner97206d52017-05-12 04:51:55 +00001257 return Status("unable to open target file");
Jason Molendaeb9902a2018-12-07 00:35:26 +00001258 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024 * 16, 0));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 uint64_t offset = 0;
1260 for (;;) {
1261 size_t bytes_read = buffer_sp->GetByteSize();
Lawrence D'Anna2fce1132019-09-26 17:54:59 +00001262 error = source_file.get()->Read(buffer_sp->GetBytes(), bytes_read);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001263 if (error.Fail() || bytes_read == 0)
1264 break;
1265
1266 const uint64_t bytes_written =
1267 WriteFile(dest_file, offset, buffer_sp->GetBytes(), bytes_read, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001268 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 break;
Vince Harron1b5a74e2015-01-21 22:42:49 +00001270
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 offset += bytes_written;
1272 if (bytes_written != bytes_read) {
Adrian Prantl05097242018-04-30 16:49:04 +00001273 // We didn't write the correct number of bytes, so adjust the file
1274 // position in the source file we are reading from...
Lawrence D'Anna2fce1132019-09-26 17:54:59 +00001275 source_file.get()->SeekFromStart(offset);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001276 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277 }
1278 CloseFile(dest_file, error);
Vince Harron1b5a74e2015-01-21 22:42:49 +00001279
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280 if (uid == UINT32_MAX && gid == UINT32_MAX)
Daniel Maleae0f8f572013-08-26 23:57:52 +00001281 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282
1283 // TODO: ChownFile?
1284
1285 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001286}
1287
Zachary Turner97206d52017-05-12 04:51:55 +00001288Status Platform::GetFile(const FileSpec &source, const FileSpec &destination) {
1289 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001291}
1292
Zachary Turner97206d52017-05-12 04:51:55 +00001293Status
1294Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src
1295 const FileSpec &dst) // The symlink points to dst
Greg Claytonfbb76342013-11-20 21:07:01 +00001296{
Zachary Turner97206d52017-05-12 04:51:55 +00001297 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 return error;
Greg Claytonfbb76342013-11-20 21:07:01 +00001299}
1300
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301bool Platform::GetFileExists(const lldb_private::FileSpec &file_spec) {
1302 return false;
1303}
1304
Zachary Turner97206d52017-05-12 04:51:55 +00001305Status Platform::Unlink(const FileSpec &path) {
1306 Status error("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 return error;
1308}
1309
Ed Maste37c40af2017-08-16 12:55:02 +00001310MmapArgList Platform::GetMmapArgumentList(const ArchSpec &arch, addr_t addr,
1311 addr_t length, unsigned prot,
1312 unsigned flags, addr_t fd,
1313 addr_t offset) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314 uint64_t flags_platform = 0;
1315 if (flags & eMmapFlagsPrivate)
1316 flags_platform |= MAP_PRIVATE;
1317 if (flags & eMmapFlagsAnon)
1318 flags_platform |= MAP_ANON;
Ed Maste37c40af2017-08-16 12:55:02 +00001319
1320 MmapArgList args({addr, length, prot, flags_platform, fd, offset});
1321 return args;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001322}
1323
Zachary Turner97206d52017-05-12 04:51:55 +00001324lldb_private::Status Platform::RunShellCommand(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325 const char *command, // Shouldn't be nullptr
1326 const FileSpec &
1327 working_dir, // Pass empty FileSpec to use the current working directory
1328 int *status_ptr, // Pass nullptr if you don't want the process exit status
1329 int *signo_ptr, // Pass nullptr if you don't want the signal that caused the
1330 // process to exit
1331 std::string
1332 *command_output, // Pass nullptr if you don't want the command output
Pavel Labath19dd1a02018-05-10 10:46:03 +00001333 const Timeout<std::micro> &timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 if (IsHost())
1335 return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
Pavel Labath19dd1a02018-05-10 10:46:03 +00001336 command_output, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 else
Zachary Turner97206d52017-05-12 04:51:55 +00001338 return Status("unimplemented");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339}
1340
1341bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1342 uint64_t &high) {
Zachary Turner076a2592017-03-20 23:54:54 +00001343 if (!IsHost())
Daniel Maleae0f8f572013-08-26 23:57:52 +00001344 return false;
Zachary Turner076a2592017-03-20 23:54:54 +00001345 auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1346 if (!Result)
1347 return false;
1348 std::tie(high, low) = Result->words();
1349 return true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001350}
1351
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352void Platform::SetLocalCacheDirectory(const char *local) {
1353 m_local_cache_directory.assign(local);
Greg Claytonfbb76342013-11-20 21:07:01 +00001354}
1355
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356const char *Platform::GetLocalCacheDirectory() {
1357 return m_local_cache_directory.c_str();
Robert Flack96ad3de2015-05-09 15:53:31 +00001358}
Greg Claytonfbb76342013-11-20 21:07:01 +00001359
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001360static constexpr OptionDefinition g_rsync_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 {LLDB_OPT_SET_ALL, false, "rsync", 'r', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001362 {}, 0, eArgTypeNone, "Enable rsync."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363 {LLDB_OPT_SET_ALL, false, "rsync-opts", 'R',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001364 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 "Platform-specific options required for rsync to work."},
1366 {LLDB_OPT_SET_ALL, false, "rsync-prefix", 'P',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001367 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 "Platform-specific rsync prefix put before the remote path."},
1369 {LLDB_OPT_SET_ALL, false, "ignore-remote-hostname", 'i',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001370 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 "Do not automatically fill in the remote hostname when composing the "
1372 "rsync command."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001373};
1374
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001375static constexpr OptionDefinition g_ssh_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 {LLDB_OPT_SET_ALL, false, "ssh", 's', OptionParser::eNoArgument, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001377 {}, 0, eArgTypeNone, "Enable SSH."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 {LLDB_OPT_SET_ALL, false, "ssh-opts", 'S', OptionParser::eRequiredArgument,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001379 nullptr, {}, 0, eArgTypeCommandName,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 "Platform-specific options required for SSH to work."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001381};
1382
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001383static constexpr OptionDefinition g_caching_option_table[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 {LLDB_OPT_SET_ALL, false, "local-cache-dir", 'c',
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00001385 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePath,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386 "Path in which to store local copies of files."},
Daniel Maleae0f8f572013-08-26 23:57:52 +00001387};
1388
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001389llvm::ArrayRef<OptionDefinition> OptionGroupPlatformRSync::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001390 return llvm::makeArrayRef(g_rsync_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001391}
1392
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393void OptionGroupPlatformRSync::OptionParsingStarting(
1394 ExecutionContext *execution_context) {
1395 m_rsync = false;
1396 m_rsync_opts.clear();
1397 m_rsync_prefix.clear();
1398 m_ignores_remote_hostname = false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001399}
1400
Zachary Turner97206d52017-05-12 04:51:55 +00001401lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001402OptionGroupPlatformRSync::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001403 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001405 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406 char short_option = (char)GetDefinitions()[option_idx].short_option;
1407 switch (short_option) {
1408 case 'r':
1409 m_rsync = true;
1410 break;
1411
1412 case 'R':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001413 m_rsync_opts.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414 break;
1415
1416 case 'P':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001417 m_rsync_prefix.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 break;
1419
1420 case 'i':
1421 m_ignores_remote_hostname = true;
1422 break;
1423
1424 default:
1425 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1426 break;
1427 }
1428
1429 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001430}
1431
Greg Clayton4116e932012-05-15 02:33:01 +00001432lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433Platform::SetThreadCreationBreakpoint(lldb_private::Target &target) {
1434 return lldb::BreakpointSP();
Greg Clayton4116e932012-05-15 02:33:01 +00001435}
Greg Claytonb3a40ba2012-03-20 18:34:04 +00001436
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001437llvm::ArrayRef<OptionDefinition> OptionGroupPlatformSSH::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001438 return llvm::makeArrayRef(g_ssh_option_table);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001439}
1440
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441void OptionGroupPlatformSSH::OptionParsingStarting(
1442 ExecutionContext *execution_context) {
1443 m_ssh = false;
1444 m_ssh_opts.clear();
Daniel Maleae0f8f572013-08-26 23:57:52 +00001445}
1446
Zachary Turner97206d52017-05-12 04:51:55 +00001447lldb_private::Status
Todd Fialae1cfbc72016-08-11 23:51:28 +00001448OptionGroupPlatformSSH::SetOptionValue(uint32_t option_idx,
Zachary Turner8cef4b02016-09-23 17:48:13 +00001449 llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001451 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 char short_option = (char)GetDefinitions()[option_idx].short_option;
1453 switch (short_option) {
1454 case 's':
1455 m_ssh = true;
1456 break;
1457
1458 case 'S':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001459 m_ssh_opts.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001460 break;
1461
1462 default:
1463 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1464 break;
1465 }
1466
1467 return error;
1468}
1469
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001470llvm::ArrayRef<OptionDefinition> OptionGroupPlatformCaching::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +00001471 return llvm::makeArrayRef(g_caching_option_table);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001472}
1473
1474void OptionGroupPlatformCaching::OptionParsingStarting(
1475 ExecutionContext *execution_context) {
1476 m_cache_dir.clear();
1477}
1478
Zachary Turner97206d52017-05-12 04:51:55 +00001479lldb_private::Status OptionGroupPlatformCaching::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +00001480 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +00001482 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 char short_option = (char)GetDefinitions()[option_idx].short_option;
1484 switch (short_option) {
1485 case 'c':
Benjamin Krameradcd0262020-01-28 20:23:46 +01001486 m_cache_dir.assign(std::string(option_arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487 break;
1488
1489 default:
1490 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
1491 break;
1492 }
1493
1494 return error;
1495}
1496
Pavel Labath62930e52018-01-10 11:57:31 +00001497Environment Platform::GetEnvironment() { return Environment(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498
1499const std::vector<ConstString> &Platform::GetTrapHandlerSymbolNames() {
1500 if (!m_calculated_trap_handlers) {
1501 std::lock_guard<std::mutex> guard(m_mutex);
1502 if (!m_calculated_trap_handlers) {
1503 CalculateTrapHandlerSymbolNames();
1504 m_calculated_trap_handlers = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001505 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506 }
1507 return m_trap_handlers;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001508}
1509
Zachary Turner97206d52017-05-12 04:51:55 +00001510Status Platform::GetCachedExecutable(
1511 ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1512 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001513 const auto platform_spec = module_spec.GetFileSpec();
1514 const auto error = LoadCachedExecutable(
1515 module_spec, module_sp, module_search_paths_ptr, remote_platform);
1516 if (error.Success()) {
1517 module_spec.GetFileSpec() = module_sp->GetFileSpec();
1518 module_spec.GetPlatformFileSpec() = platform_spec;
1519 }
1520
1521 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00001522}
1523
Zachary Turner97206d52017-05-12 04:51:55 +00001524Status Platform::LoadCachedExecutable(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525 const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
1526 const FileSpecList *module_search_paths_ptr, Platform &remote_platform) {
1527 return GetRemoteSharedModule(module_spec, nullptr, module_sp,
1528 [&](const ModuleSpec &spec) {
1529 return remote_platform.ResolveExecutable(
1530 spec, module_sp, module_search_paths_ptr);
1531 },
1532 nullptr);
Daniel Maleae0f8f572013-08-26 23:57:52 +00001533}
1534
Zachary Turner97206d52017-05-12 04:51:55 +00001535Status Platform::GetRemoteSharedModule(const ModuleSpec &module_spec,
1536 Process *process,
1537 lldb::ModuleSP &module_sp,
1538 const ModuleResolver &module_resolver,
1539 bool *did_create_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 // Get module information from a target.
1541 ModuleSpec resolved_module_spec;
1542 bool got_module_spec = false;
1543 if (process) {
1544 // Try to get module information from the process
1545 if (process->GetModuleSpec(module_spec.GetFileSpec(),
1546 module_spec.GetArchitecture(),
1547 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001548 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001549 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1550 got_module_spec = true;
1551 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001552 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00001554
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001555 if (!module_spec.GetArchitecture().IsValid()) {
Zachary Turner97206d52017-05-12 04:51:55 +00001556 Status error;
Adrian Prantl05097242018-04-30 16:49:04 +00001557 // No valid architecture was specified, ask the platform for the
1558 // architectures that we should be using (in the correct order) and see if
1559 // we can find a match that way
Jason Molenda3fce2fd2016-10-05 02:29:13 +00001560 ModuleSpec arch_module_spec(module_spec);
1561 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
1562 idx, arch_module_spec.GetArchitecture());
1563 ++idx) {
1564 error = ModuleList::GetSharedModule(arch_module_spec, module_sp, nullptr,
1565 nullptr, nullptr);
1566 // Did we find an executable using one of the
1567 if (error.Success() && module_sp)
1568 break;
1569 }
1570 if (module_sp)
1571 got_module_spec = true;
1572 }
1573
Kate Stoneb9c1b512016-09-06 20:57:50 +00001574 if (!got_module_spec) {
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001575 // Get module information from a target.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 if (!GetModuleSpec(module_spec.GetFileSpec(), module_spec.GetArchitecture(),
1577 resolved_module_spec)) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001578 if (!module_spec.GetUUID().IsValid() ||
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 module_spec.GetUUID() == resolved_module_spec.GetUUID()) {
1580 return module_resolver(module_spec);
1581 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 }
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001584
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 // If we are looking for a specific UUID, make sure resolved_module_spec has
1586 // the same one before we search.
1587 if (module_spec.GetUUID().IsValid()) {
1588 resolved_module_spec.GetUUID() = module_spec.GetUUID();
1589 }
Jason Molenda85967fa2016-07-12 03:25:22 +00001590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 // Trying to find a module by UUID on local file system.
1592 const auto error = module_resolver(resolved_module_spec);
1593 if (error.Fail()) {
1594 if (GetCachedSharedModule(resolved_module_spec, module_sp, did_create_ptr))
Zachary Turner97206d52017-05-12 04:51:55 +00001595 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001597
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 return error;
Oleksiy Vyalov037f6b92015-03-24 23:45:49 +00001599}
1600
Kate Stoneb9c1b512016-09-06 20:57:50 +00001601bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
1602 lldb::ModuleSP &module_sp,
1603 bool *did_create_ptr) {
1604 if (IsHost() || !GetGlobalPlatformProperties()->GetUseModuleCache() ||
1605 !GetGlobalPlatformProperties()->GetModuleCacheDirectory())
Oleksiy Vyalov280d8dc2015-04-15 14:35:10 +00001606 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001607
1608 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
1609
1610 // Check local cache for a module.
1611 auto error = m_module_cache->GetAndPut(
1612 GetModuleCacheRoot(), GetCacheHostname(), module_spec,
1613 [this](const ModuleSpec &module_spec,
1614 const FileSpec &tmp_download_file_spec) {
1615 return DownloadModuleSlice(
1616 module_spec.GetFileSpec(), module_spec.GetObjectOffset(),
1617 module_spec.GetObjectSize(), tmp_download_file_spec);
1618
1619 },
1620 [this](const ModuleSP &module_sp,
1621 const FileSpec &tmp_download_file_spec) {
1622 return DownloadSymbolFile(module_sp, tmp_download_file_spec);
1623 },
1624 module_sp, did_create_ptr);
1625 if (error.Success())
1626 return true;
1627
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +00001628 LLDB_LOGF(log, "Platform::%s - module %s not found in local cache: %s",
1629 __FUNCTION__, module_spec.GetUUID().GetAsString().c_str(),
1630 error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 return false;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001632}
1633
Zachary Turner97206d52017-05-12 04:51:55 +00001634Status Platform::DownloadModuleSlice(const FileSpec &src_file_spec,
1635 const uint64_t src_offset,
1636 const uint64_t src_size,
1637 const FileSpec &dst_file_spec) {
1638 Status error;
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001639
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001640 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +00001641 llvm::raw_fd_ostream dst(dst_file_spec.GetPath(), EC, llvm::sys::fs::OF_None);
Pavel Labathe3ad2e22017-03-21 13:49:50 +00001642 if (EC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001643 error.SetErrorStringWithFormat("unable to open destination file: %s",
1644 dst_file_spec.GetPath().c_str());
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001645 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646 }
Oleksiy Vyalov63acdfd2015-03-10 01:15:28 +00001647
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 auto src_fd = OpenFile(src_file_spec, File::eOpenOptionRead,
1649 lldb::eFilePermissionsFileDefault, error);
Tamas Berghammerec3f92a2015-08-12 11:10:25 +00001650
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 if (error.Fail()) {
1652 error.SetErrorStringWithFormat("unable to open source file: %s",
1653 error.AsCString());
1654 return error;
1655 }
Oleksiy Vyalov6f001062015-03-25 17:58:13 +00001656
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657 std::vector<char> buffer(1024);
1658 auto offset = src_offset;
1659 uint64_t total_bytes_read = 0;
1660 while (total_bytes_read < src_size) {
1661 const auto to_read = std::min(static_cast<uint64_t>(buffer.size()),
1662 src_size - total_bytes_read);
1663 const uint64_t n_read =
1664 ReadFile(src_fd, offset, &buffer[0], to_read, error);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001665 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666 break;
1667 if (n_read == 0) {
1668 error.SetErrorString("read 0 bytes");
1669 break;
1670 }
1671 offset += n_read;
1672 total_bytes_read += n_read;
1673 dst.write(&buffer[0], n_read);
1674 }
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001675
Zachary Turner97206d52017-05-12 04:51:55 +00001676 Status close_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677 CloseFile(src_fd, close_error); // Ignoring close error.
1678
1679 return error;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001680}
1681
Zachary Turner97206d52017-05-12 04:51:55 +00001682Status Platform::DownloadSymbolFile(const lldb::ModuleSP &module_sp,
1683 const FileSpec &dst_file_spec) {
1684 return Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685 "Symbol file downloading not supported by the default platform.");
Tamas Berghammerccd6cff2015-12-08 14:08:19 +00001686}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001687
Kate Stoneb9c1b512016-09-06 20:57:50 +00001688FileSpec Platform::GetModuleCacheRoot() {
1689 auto dir_spec = GetGlobalPlatformProperties()->GetModuleCacheDirectory();
1690 dir_spec.AppendPathComponent(GetName().AsCString());
1691 return dir_spec;
1692}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001693
Kate Stoneb9c1b512016-09-06 20:57:50 +00001694const char *Platform::GetCacheHostname() { return GetHostname(); }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001695
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696const UnixSignalsSP &Platform::GetRemoteUnixSignals() {
1697 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>();
1698 return s_default_unix_signals_sp;
1699}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001700
Zachary Turnera89ce432019-03-06 18:20:23 +00001701UnixSignalsSP Platform::GetUnixSignals() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702 if (IsHost())
Zachary Turnera89ce432019-03-06 18:20:23 +00001703 return UnixSignals::CreateForHost();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704 return GetRemoteUnixSignals();
1705}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001706
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707uint32_t Platform::LoadImage(lldb_private::Process *process,
1708 const lldb_private::FileSpec &local_file,
1709 const lldb_private::FileSpec &remote_file,
Zachary Turner97206d52017-05-12 04:51:55 +00001710 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711 if (local_file && remote_file) {
1712 // Both local and remote file was specified. Install the local file to the
1713 // given location.
1714 if (IsRemote() || local_file != remote_file) {
1715 error = Install(local_file, remote_file);
1716 if (error.Fail())
1717 return LLDB_INVALID_IMAGE_TOKEN;
1718 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001719 return DoLoadImage(process, remote_file, nullptr, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001721
Kate Stoneb9c1b512016-09-06 20:57:50 +00001722 if (local_file) {
1723 // Only local file was specified. Install it to the current working
1724 // directory.
1725 FileSpec target_file = GetWorkingDirectory();
1726 target_file.AppendPathComponent(local_file.GetFilename().AsCString());
1727 if (IsRemote() || local_file != target_file) {
1728 error = Install(local_file, target_file);
1729 if (error.Fail())
1730 return LLDB_INVALID_IMAGE_TOKEN;
1731 }
Jim Ingham0d231f72018-06-28 20:02:11 +00001732 return DoLoadImage(process, target_file, nullptr, error);
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001733 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001734
Kate Stoneb9c1b512016-09-06 20:57:50 +00001735 if (remote_file) {
1736 // Only remote file was specified so we don't have to do any copying
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 error.SetErrorString("Neither local nor remote file was specified");
1741 return LLDB_INVALID_IMAGE_TOKEN;
1742}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001743
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744uint32_t Platform::DoLoadImage(lldb_private::Process *process,
1745 const lldb_private::FileSpec &remote_file,
Jim Ingham0d231f72018-06-28 20:02:11 +00001746 const std::vector<std::string> *paths,
1747 lldb_private::Status &error,
1748 lldb_private::FileSpec *loaded_image) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749 error.SetErrorString("LoadImage is not supported on the current platform");
1750 return LLDB_INVALID_IMAGE_TOKEN;
1751}
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00001752
Jim Ingham0d231f72018-06-28 20:02:11 +00001753uint32_t Platform::LoadImageUsingPaths(lldb_private::Process *process,
1754 const lldb_private::FileSpec &remote_filename,
1755 const std::vector<std::string> &paths,
1756 lldb_private::Status &error,
1757 lldb_private::FileSpec *loaded_path)
1758{
1759 FileSpec file_to_use;
1760 if (remote_filename.IsAbsolute())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001761 file_to_use = FileSpec(remote_filename.GetFilename().GetStringRef(),
1762
1763 remote_filename.GetPathStyle());
Jim Ingham0d231f72018-06-28 20:02:11 +00001764 else
1765 file_to_use = remote_filename;
Jonas Devlieghere463a48e2019-07-26 18:14:08 +00001766
Jim Ingham0d231f72018-06-28 20:02:11 +00001767 return DoLoadImage(process, file_to_use, &paths, error, loaded_path);
1768}
1769
Zachary Turner97206d52017-05-12 04:51:55 +00001770Status Platform::UnloadImage(lldb_private::Process *process,
1771 uint32_t image_token) {
1772 return Status("UnloadImage is not supported on the current platform");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773}
Aidan Dodds933d8db2016-02-22 17:29:56 +00001774
Zachary Turner31659452016-11-17 21:15:14 +00001775lldb::ProcessSP Platform::ConnectProcess(llvm::StringRef connect_url,
1776 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777 lldb_private::Debugger &debugger,
1778 lldb_private::Target *target,
Zachary Turner97206d52017-05-12 04:51:55 +00001779 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001780 error.Clear();
Aidan Dodds933d8db2016-02-22 17:29:56 +00001781
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782 if (!target) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001783 ArchSpec arch;
1784 if (target && target->GetArchitecture().IsValid())
1785 arch = target->GetArchitecture();
1786 else
1787 arch = Target::GetDefaultArchitecture();
1788
1789 const char *triple = "";
1790 if (arch.IsValid())
1791 triple = arch.GetTriple().getTriple().c_str();
1792
Kate Stoneb9c1b512016-09-06 20:57:50 +00001793 TargetSP new_target_sp;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001794 error = debugger.GetTargetList().CreateTarget(
Jason Molendadf9f7962018-10-23 23:45:56 +00001795 debugger, "", triple, eLoadDependentsNo, nullptr, new_target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 target = new_target_sp.get();
1797 }
Aidan Dodds933d8db2016-02-22 17:29:56 +00001798
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 if (!target || error.Fail())
1800 return nullptr;
1801
1802 debugger.GetTargetList().SetSelectedTarget(target);
1803
1804 lldb::ProcessSP process_sp =
1805 target->CreateProcess(debugger.GetListener(), plugin_name, nullptr);
1806 if (!process_sp)
1807 return nullptr;
1808
Jonas Devlieghere32d35fb2020-07-13 13:44:01 -07001809 error = process_sp->ConnectRemote(connect_url);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001810 if (error.Fail())
1811 return nullptr;
1812
1813 return process_sp;
1814}
1815
1816size_t Platform::ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
Zachary Turner97206d52017-05-12 04:51:55 +00001817 lldb_private::Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 error.Clear();
1819 return 0;
1820}
1821
1822size_t Platform::GetSoftwareBreakpointTrapOpcode(Target &target,
1823 BreakpointSite *bp_site) {
1824 ArchSpec arch = target.GetArchitecture();
Jonas Devlieghere1fcd234a2020-04-24 09:03:51 -07001825 assert(arch.IsValid());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001826 const uint8_t *trap_opcode = nullptr;
1827 size_t trap_opcode_size = 0;
1828
1829 switch (arch.GetMachine()) {
Jason Molenda7dd7a362019-10-16 19:14:49 +00001830 case llvm::Triple::aarch64_32:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001831 case llvm::Triple::aarch64: {
1832 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
1833 trap_opcode = g_aarch64_opcode;
1834 trap_opcode_size = sizeof(g_aarch64_opcode);
1835 } break;
1836
Tatyana Krasnukhafaf6b252019-10-17 15:16:21 +00001837 case llvm::Triple::arc: {
1838 static const uint8_t g_hex_opcode[] = { 0xff, 0x7f };
1839 trap_opcode = g_hex_opcode;
1840 trap_opcode_size = sizeof(g_hex_opcode);
1841 } break;
1842
Kate Stoneb9c1b512016-09-06 20:57:50 +00001843 // TODO: support big-endian arm and thumb trap codes.
1844 case llvm::Triple::arm: {
Adrian Prantl05097242018-04-30 16:49:04 +00001845 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
1846 // linux kernel does otherwise.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001847 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
1848 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
1849
1850 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001851 AddressClass addr_class = AddressClass::eUnknown;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001852
1853 if (bp_loc_sp) {
1854 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001855 if (addr_class == AddressClass::eUnknown &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 (bp_loc_sp->GetAddress().GetFileAddress() & 1))
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001857 addr_class = AddressClass::eCodeAlternateISA;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001858 }
1859
Tatyana Krasnukha04803b32018-06-26 13:06:54 +00001860 if (addr_class == AddressClass::eCodeAlternateISA) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001861 trap_opcode = g_thumb_breakpoint_opcode;
1862 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
1863 } else {
1864 trap_opcode = g_arm_breakpoint_opcode;
1865 trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
1866 }
1867 } break;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001868
Ayke van Laethem0818e6c2020-02-07 22:39:56 +01001869 case llvm::Triple::avr: {
1870 static const uint8_t g_hex_opcode[] = {0x98, 0x95};
1871 trap_opcode = g_hex_opcode;
1872 trap_opcode_size = sizeof(g_hex_opcode);
1873 } break;
1874
Kate Stoneb9c1b512016-09-06 20:57:50 +00001875 case llvm::Triple::mips:
1876 case llvm::Triple::mips64: {
1877 static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
1878 trap_opcode = g_hex_opcode;
1879 trap_opcode_size = sizeof(g_hex_opcode);
1880 } break;
1881
1882 case llvm::Triple::mipsel:
1883 case llvm::Triple::mips64el: {
1884 static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
1885 trap_opcode = g_hex_opcode;
1886 trap_opcode_size = sizeof(g_hex_opcode);
1887 } break;
1888
1889 case llvm::Triple::systemz: {
1890 static const uint8_t g_hex_opcode[] = {0x00, 0x01};
1891 trap_opcode = g_hex_opcode;
1892 trap_opcode_size = sizeof(g_hex_opcode);
1893 } break;
1894
1895 case llvm::Triple::hexagon: {
1896 static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
1897 trap_opcode = g_hex_opcode;
1898 trap_opcode_size = sizeof(g_hex_opcode);
1899 } break;
1900
1901 case llvm::Triple::ppc:
1902 case llvm::Triple::ppc64: {
1903 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
1904 trap_opcode = g_ppc_opcode;
1905 trap_opcode_size = sizeof(g_ppc_opcode);
1906 } break;
1907
Eugene Zemtsovaae0a752017-10-05 19:44:05 +00001908 case llvm::Triple::ppc64le: {
1909 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
1910 trap_opcode = g_ppc64le_opcode;
1911 trap_opcode_size = sizeof(g_ppc64le_opcode);
1912 } break;
1913
Kate Stoneb9c1b512016-09-06 20:57:50 +00001914 case llvm::Triple::x86:
1915 case llvm::Triple::x86_64: {
1916 static const uint8_t g_i386_opcode[] = {0xCC};
1917 trap_opcode = g_i386_opcode;
1918 trap_opcode_size = sizeof(g_i386_opcode);
1919 } break;
1920
1921 default:
Jonas Devlieghere1fcd234a2020-04-24 09:03:51 -07001922 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001923 }
1924
1925 assert(bp_site);
1926 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
1927 return trap_opcode_size;
1928
1929 return 0;
Aidan Dodds933d8db2016-02-22 17:29:56 +00001930}