1 //===-- PlatformRemoteDarwinDevice.cpp ------------------------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 #include "PlatformRemoteDarwinDevice.h"
11 #include "lldb/Breakpoint/BreakpointLocation.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Host/Host.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/FileSpec.h"
22 #include "lldb/Utility/LLDBLog.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/Status.h"
25 #include "lldb/Utility/StreamString.h"
29 using namespace lldb_private
;
31 PlatformRemoteDarwinDevice::SDKDirectoryInfo::SDKDirectoryInfo(
32 const lldb_private::FileSpec
&sdk_dir
)
33 : directory(sdk_dir
), build(), user_cached(false) {
34 llvm::StringRef dirname_str
= sdk_dir
.GetFilename().GetStringRef();
35 llvm::StringRef build_str
;
36 std::tie(version
, build_str
) = ParseVersionBuildDir(dirname_str
);
37 build
.SetString(build_str
);
40 /// Default Constructor
41 PlatformRemoteDarwinDevice::PlatformRemoteDarwinDevice()
42 : PlatformDarwinDevice(false) {} // This is a remote platform
46 /// The destructor is virtual since this class is designed to be
47 /// inherited from by the plug-in instance.
48 PlatformRemoteDarwinDevice::~PlatformRemoteDarwinDevice() = default;
50 void PlatformRemoteDarwinDevice::GetStatus(Stream
&strm
) {
51 Platform::GetStatus(strm
);
52 const char *sdk_directory
= GetDeviceSupportDirectoryForOSVersion();
54 strm
.Printf(" SDK Path: \"%s\"\n", sdk_directory
);
56 strm
.PutCString(" SDK Path: error: unable to locate SDK\n");
58 const uint32_t num_sdk_infos
= m_sdk_directory_infos
.size();
59 for (uint32_t i
= 0; i
< num_sdk_infos
; ++i
) {
60 const SDKDirectoryInfo
&sdk_dir_info
= m_sdk_directory_infos
[i
];
61 strm
.Printf(" SDK Roots: [%2u] \"%s\"\n", i
,
62 sdk_dir_info
.directory
.GetPath().c_str());
66 bool PlatformRemoteDarwinDevice::GetFileInSDK(const char *platform_file_path
,
68 lldb_private::FileSpec
&local_file
) {
69 Log
*log
= GetLog(LLDBLog::Host
);
70 if (sdk_idx
< m_sdk_directory_infos
.size()) {
71 std::string sdkroot_path
=
72 m_sdk_directory_infos
[sdk_idx
].directory
.GetPath();
75 if (!sdkroot_path
.empty() && platform_file_path
&& platform_file_path
[0]) {
76 // We may need to interpose "/Symbols/" or "/Symbols.Internal/" between
78 // SDK root directory and the file path.
80 const char *paths_to_try
[] = {"Symbols", "", "Symbols.Internal", nullptr};
81 for (size_t i
= 0; paths_to_try
[i
] != nullptr; i
++) {
82 local_file
.SetFile(sdkroot_path
, FileSpec::Style::native
);
83 if (paths_to_try
[i
][0] != '\0')
84 local_file
.AppendPathComponent(paths_to_try
[i
]);
85 local_file
.AppendPathComponent(platform_file_path
);
86 FileSystem::Instance().Resolve(local_file
);
87 if (FileSystem::Instance().Exists(local_file
)) {
88 LLDB_LOGF(log
, "Found a copy of %s in the SDK dir %s/%s",
89 platform_file_path
, sdkroot_path
.c_str(), paths_to_try
[i
]);
99 Status
PlatformRemoteDarwinDevice::GetSymbolFile(const FileSpec
&platform_file
,
100 const UUID
*uuid_ptr
,
101 FileSpec
&local_file
) {
102 Log
*log
= GetLog(LLDBLog::Host
);
104 char platform_file_path
[PATH_MAX
];
105 if (platform_file
.GetPath(platform_file_path
, sizeof(platform_file_path
))) {
106 const char *os_version_dir
= GetDeviceSupportDirectoryForOSVersion();
107 if (os_version_dir
) {
108 std::string resolved_path
=
109 (llvm::Twine(os_version_dir
) + "/" + platform_file_path
).str();
111 local_file
.SetFile(resolved_path
, FileSpec::Style::native
);
112 FileSystem::Instance().Resolve(local_file
);
113 if (FileSystem::Instance().Exists(local_file
)) {
115 LLDB_LOGF(log
, "Found a copy of %s in the DeviceSupport dir %s",
116 platform_file_path
, os_version_dir
);
121 resolved_path
= (llvm::Twine(os_version_dir
) + "/Symbols.Internal/" +
125 local_file
.SetFile(resolved_path
, FileSpec::Style::native
);
126 FileSystem::Instance().Resolve(local_file
);
127 if (FileSystem::Instance().Exists(local_file
)) {
130 "Found a copy of %s in the DeviceSupport dir %s/Symbols.Internal",
131 platform_file_path
, os_version_dir
);
135 (llvm::Twine(os_version_dir
) + "/Symbols/" + platform_file_path
)
138 local_file
.SetFile(resolved_path
, FileSpec::Style::native
);
139 FileSystem::Instance().Resolve(local_file
);
140 if (FileSystem::Instance().Exists(local_file
)) {
141 LLDB_LOGF(log
, "Found a copy of %s in the DeviceSupport dir %s/Symbols",
142 platform_file_path
, os_version_dir
);
146 local_file
= platform_file
;
147 if (FileSystem::Instance().Exists(local_file
))
150 error
= Status::FromErrorStringWithFormatv(
151 "unable to locate a platform file for '{0}' in platform '{1}'",
152 platform_file_path
, GetPluginName());
154 error
= Status::FromErrorString("invalid platform file argument");
159 Status
PlatformRemoteDarwinDevice::GetSharedModule(
160 const ModuleSpec
&module_spec
, Process
*process
, ModuleSP
&module_sp
,
161 const FileSpecList
*module_search_paths_ptr
,
162 llvm::SmallVectorImpl
<ModuleSP
> *old_modules
, bool *did_create_ptr
) {
163 // For iOS, the SDK files are all cached locally on the host system. So first
164 // we ask for the file in the cached SDK, then we attempt to get a shared
165 // module for the right architecture with the right UUID.
166 const FileSpec
&platform_file
= module_spec
.GetFileSpec();
167 Log
*log
= GetLog(LLDBLog::Host
);
170 char platform_file_path
[PATH_MAX
];
172 if (platform_file
.GetPath(platform_file_path
, sizeof(platform_file_path
))) {
173 ModuleSpec
platform_module_spec(module_spec
);
175 UpdateSDKDirectoryInfosIfNeeded();
177 const uint32_t num_sdk_infos
= m_sdk_directory_infos
.size();
179 // If we are connected we migth be able to correctly deduce the SDK
180 // directory using the OS build.
181 const uint32_t connected_sdk_idx
= GetConnectedSDKIndex();
182 if (connected_sdk_idx
< num_sdk_infos
) {
183 LLDB_LOGV(log
, "Searching for {0} in sdk path {1}", platform_file
,
184 m_sdk_directory_infos
[connected_sdk_idx
].directory
);
185 if (GetFileInSDK(platform_file_path
, connected_sdk_idx
,
186 platform_module_spec
.GetFileSpec())) {
188 error
= ResolveExecutable(platform_module_spec
, module_sp
, nullptr);
190 m_last_module_sdk_idx
= connected_sdk_idx
;
197 // Try the last SDK index if it is set as most files from an SDK will tend
198 // to be valid in that same SDK.
199 if (m_last_module_sdk_idx
< num_sdk_infos
) {
200 LLDB_LOGV(log
, "Searching for {0} in sdk path {1}", platform_file
,
201 m_sdk_directory_infos
[m_last_module_sdk_idx
].directory
);
202 if (GetFileInSDK(platform_file_path
, m_last_module_sdk_idx
,
203 platform_module_spec
.GetFileSpec())) {
205 error
= ResolveExecutable(platform_module_spec
, module_sp
, nullptr);
213 // First try for an exact match of major, minor and update: If a particalar
214 // SDK version was specified via --version or --build, look for a match on
216 const SDKDirectoryInfo
*current_sdk_info
=
217 GetSDKDirectoryForCurrentOSVersion();
218 const uint32_t current_sdk_idx
=
219 GetSDKIndexBySDKDirectoryInfo(current_sdk_info
);
220 if (current_sdk_idx
< num_sdk_infos
&&
221 current_sdk_idx
!= m_last_module_sdk_idx
) {
222 LLDB_LOGV(log
, "Searching for {0} in sdk path {1}", platform_file
,
223 m_sdk_directory_infos
[current_sdk_idx
].directory
);
224 if (GetFileInSDK(platform_file_path
, current_sdk_idx
,
225 platform_module_spec
.GetFileSpec())) {
227 error
= ResolveExecutable(platform_module_spec
, module_sp
, nullptr);
229 m_last_module_sdk_idx
= current_sdk_idx
;
236 // Second try all SDKs that were found.
237 for (uint32_t sdk_idx
= 0; sdk_idx
< num_sdk_infos
; ++sdk_idx
) {
238 if (m_last_module_sdk_idx
== sdk_idx
) {
239 // Skip the last module SDK index if we already searched it above
242 LLDB_LOGV(log
, "Searching for {0} in sdk path {1}", platform_file
,
243 m_sdk_directory_infos
[sdk_idx
].directory
);
244 if (GetFileInSDK(platform_file_path
, sdk_idx
,
245 platform_module_spec
.GetFileSpec())) {
246 // printf ("sdk[%u]: '%s'\n", sdk_idx, local_file.GetPath().c_str());
248 error
= ResolveExecutable(platform_module_spec
, module_sp
, nullptr);
250 // Remember the index of the last SDK that we found a file in in case
251 // the wrong SDK was selected.
252 m_last_module_sdk_idx
= sdk_idx
;
259 // Not the module we are looking for... Nothing to see here...
262 // This may not be an SDK-related module. Try whether we can bring in the
263 // thing to our local cache.
264 error
= GetSharedModuleWithLocalCache(module_spec
, module_sp
,
265 module_search_paths_ptr
, old_modules
,
270 // See if the file is present in any of the module_search_paths_ptr
273 error
= PlatformDarwin::FindBundleBinaryInExecSearchPaths(
274 module_spec
, process
, module_sp
, module_search_paths_ptr
, old_modules
,
280 const bool always_create
= false;
281 error
= ModuleList::GetSharedModule(module_spec
, module_sp
,
282 module_search_paths_ptr
, old_modules
,
283 did_create_ptr
, always_create
);
286 module_sp
->SetPlatformFileSpec(platform_file
);
291 uint32_t PlatformRemoteDarwinDevice::GetConnectedSDKIndex() {
293 if (m_connected_module_sdk_idx
== UINT32_MAX
) {
294 if (std::optional
<std::string
> build
= GetRemoteOSBuildString()) {
295 const uint32_t num_sdk_infos
= m_sdk_directory_infos
.size();
296 for (uint32_t i
= 0; i
< num_sdk_infos
; ++i
) {
297 const SDKDirectoryInfo
&sdk_dir_info
= m_sdk_directory_infos
[i
];
298 if (strstr(sdk_dir_info
.directory
.GetFilename().AsCString(""),
300 m_connected_module_sdk_idx
= i
;
306 m_connected_module_sdk_idx
= UINT32_MAX
;
308 return m_connected_module_sdk_idx
;
311 uint32_t PlatformRemoteDarwinDevice::GetSDKIndexBySDKDirectoryInfo(
312 const SDKDirectoryInfo
*sdk_info
) {
313 if (sdk_info
== nullptr) {
317 return sdk_info
- &m_sdk_directory_infos
[0];