LLDB mainline
PlatformDarwinDevice.cpp
Go to the documentation of this file.
1//===-- PlatformDarwinDevice.cpp ------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "lldb/Core/Module.h"
13#include "lldb/Host/HostInfo.h"
17#include "lldb/Utility/Log.h"
18#include <optional>
19
20using namespace lldb;
21using namespace lldb_private;
22
24
32
34 Log *log = GetLog(LLDBLog::Host);
35 std::lock_guard<std::mutex> guard(m_sdk_dir_mutex);
36 if (m_sdk_directory_infos.empty()) {
37 // A --sysroot option was supplied - add it to our list of SDKs to check
38 if (!m_sdk_sysroot.empty()) {
39 FileSpec sdk_sysroot_fspec(m_sdk_sysroot.c_str());
40 FileSystem::Instance().Resolve(sdk_sysroot_fspec);
41 const SDKDirectoryInfo sdk_sysroot_directory_info(sdk_sysroot_fspec);
42 m_sdk_directory_infos.push_back(sdk_sysroot_directory_info);
43 if (log) {
44 LLDB_LOGF(log,
45 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded added "
46 "--sysroot SDK directory %s",
47 m_sdk_sysroot.c_str());
48 }
49 return true;
50 }
51 const char *device_support_dir = GetDeviceSupportDirectory();
52 if (log) {
53 LLDB_LOGF(log,
54 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded Got "
55 "DeviceSupport directory %s",
56 device_support_dir);
57 }
58 if (device_support_dir) {
59 const bool find_directories = true;
60 const bool find_files = false;
61 const bool find_other = false;
62
63 SDKDirectoryInfoCollection builtin_sdk_directory_infos;
65 m_device_support_directory, find_directories, find_files, find_other,
67 &builtin_sdk_directory_infos);
68
69 // Only add SDK directories that have symbols in them, some SDKs only
70 // contain developer disk images and no symbols, so they aren't useful to
71 // us.
72 FileSpec sdk_symbols_symlink_fspec;
73 for (const auto &sdk_directory_info : builtin_sdk_directory_infos) {
74 sdk_symbols_symlink_fspec = sdk_directory_info.directory;
75 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols");
76 if (FileSystem::Instance().Exists(sdk_symbols_symlink_fspec)) {
77 m_sdk_directory_infos.push_back(sdk_directory_info);
78 if (log) {
79 LLDB_LOGF(log,
80 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded "
81 "added builtin SDK directory %s",
82 sdk_symbols_symlink_fspec.GetPath().c_str());
83 }
84 }
85 }
86
87 const uint32_t num_installed = m_sdk_directory_infos.size();
88 llvm::StringRef dirname = GetDeviceSupportDirectoryName();
89 std::string local_sdk_cache_str = "~/Library/Developer/Xcode/";
90 local_sdk_cache_str += std::string(dirname);
91 FileSpec local_sdk_cache(local_sdk_cache_str.c_str());
92 FileSystem::Instance().Resolve(local_sdk_cache);
93 if (FileSystem::Instance().Exists(local_sdk_cache)) {
94 if (log) {
95 LLDB_LOGF(log,
96 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded "
97 "searching %s for additional SDKs",
98 local_sdk_cache.GetPath().c_str());
99 }
100 char path[PATH_MAX];
101 if (local_sdk_cache.GetPath(path, sizeof(path))) {
103 path, find_directories, find_files, find_other,
106 const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
107 // First try for an exact match of major, minor and update
108 for (uint32_t i = num_installed; i < num_sdk_infos; ++i) {
109 m_sdk_directory_infos[i].user_cached = true;
110 if (log) {
111 LLDB_LOGF(log,
112 "PlatformDarwinDevice::"
113 "UpdateSDKDirectoryInfosIfNeeded "
114 "user SDK directory %s",
115 m_sdk_directory_infos[i].directory.GetPath().c_str());
116 }
117 }
118 }
119 }
120
121 const char *addtional_platform_dirs = getenv("PLATFORM_SDK_DIRECTORY");
122 if (addtional_platform_dirs) {
123 SDKDirectoryInfoCollection env_var_sdk_directory_infos;
125 addtional_platform_dirs, find_directories, find_files, find_other,
127 &env_var_sdk_directory_infos);
128 FileSpec sdk_symbols_symlink_fspec;
129 for (const auto &sdk_directory_info : env_var_sdk_directory_infos) {
130 sdk_symbols_symlink_fspec = sdk_directory_info.directory;
131 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols");
132 if (FileSystem::Instance().Exists(sdk_symbols_symlink_fspec)) {
133 m_sdk_directory_infos.push_back(sdk_directory_info);
134 if (log) {
135 LLDB_LOGF(log,
136 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded "
137 "added env var SDK directory %s",
138 sdk_symbols_symlink_fspec.GetPath().c_str());
139 }
140 }
141 }
142 }
143 }
144 }
145 return !m_sdk_directory_infos.empty();
146}
147
150 uint32_t i;
152 const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
153 std::vector<bool> check_sdk_info(num_sdk_infos, true);
154
155 // Prefer the user SDK build string.
156 std::string build = GetSDKBuild();
157
158 // Fall back to the platform's build string.
159 if (build.empty()) {
160 if (std::optional<std::string> os_build_str = GetOSBuildString())
161 build.assign(*os_build_str);
162 }
163
164 // If we have a build string, only check platforms for which the build
165 // string matches.
166 if (!build.empty()) {
167 for (i = 0; i < num_sdk_infos; ++i)
168 check_sdk_info[i] = m_sdk_directory_infos[i].build.GetStringRef() ==
169 llvm::StringRef(build);
170 }
171
172 // If we are connected we can find the version of the OS the platform us
173 // running on and select the right SDK
174 llvm::VersionTuple version = GetOSVersion();
175 if (!version.empty()) {
177 // First try for an exact match of major, minor and update.
178 for (i = 0; i < num_sdk_infos; ++i) {
179 if (check_sdk_info[i]) {
180 if (m_sdk_directory_infos[i].version == version)
181 return &m_sdk_directory_infos[i];
182 }
183 }
184 // Try for an exact match of major and minor.
185 for (i = 0; i < num_sdk_infos; ++i) {
186 if (check_sdk_info[i]) {
187 if (m_sdk_directory_infos[i].version.getMajor() ==
188 version.getMajor() &&
189 m_sdk_directory_infos[i].version.getMinor() ==
190 version.getMinor()) {
191 return &m_sdk_directory_infos[i];
192 }
193 }
194 }
195 // Lastly try to match of major version only.
196 for (i = 0; i < num_sdk_infos; ++i) {
197 if (check_sdk_info[i]) {
198 if (m_sdk_directory_infos[i].version.getMajor() ==
199 version.getMajor()) {
200 return &m_sdk_directory_infos[i];
201 }
202 }
203 }
204 }
205 } else if (!build.empty()) {
206 // No version, just a build number, return the first one that matches.
207 for (i = 0; i < num_sdk_infos; ++i)
208 if (check_sdk_info[i])
209 return &m_sdk_directory_infos[i];
210 }
211 }
212 return nullptr;
213}
214
217 const PlatformDarwinDevice::SDKDirectoryInfo *result = nullptr;
219 auto max = std::max_element(
221 [](const SDKDirectoryInfo &a, const SDKDirectoryInfo &b) {
222 return a.version < b.version;
223 });
224 if (max != m_sdk_directory_infos.end())
225 result = &*max;
226 }
227 return result;
228}
229
231 std::string platform_dir =
232 ("/Platforms/" + GetPlatformName() + "/DeviceSupport").str();
233 if (m_device_support_directory.empty()) {
234 if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) {
235 m_device_support_directory = fspec.GetPath();
236 m_device_support_directory.append(platform_dir.c_str());
237 } else {
238 // Assign a single NULL character so we know we tried to find the device
239 // support directory and we don't keep trying to find it over and over.
240 m_device_support_directory.assign(1, '\0');
241 }
242 }
243 // We should have put a single NULL character into m_device_support_directory
244 // or it should have a valid path if the code gets here
245 assert(m_device_support_directory.empty() == false);
247 return m_device_support_directory.c_str();
248 return nullptr;
249}
250
252 if (!m_sdk_sysroot.empty())
253 return m_sdk_sysroot.c_str();
254
256 const PlatformDarwinDevice::SDKDirectoryInfo *sdk_dir_info =
258 if (sdk_dir_info == nullptr)
259 sdk_dir_info = GetSDKDirectoryForLatestOSVersion();
260 if (sdk_dir_info) {
261 char path[PATH_MAX];
262 if (sdk_dir_info->directory.GetPath(path, sizeof(path))) {
265 }
266 } else {
267 // Assign a single NULL character so we know we tried to find the device
268 // support directory and we don't keep trying to find it over and over.
270 }
271 }
272 // We should have put a single NULL character into
273 // m_device_support_directory_for_os_version or it should have a valid path
274 // if the code gets here
275 assert(m_device_support_directory_for_os_version.empty() == false);
278 return nullptr;
279}
280
282MakeCacheFolderForFile(const FileSpec &module_cache_spec) {
283 FileSpec module_cache_folder =
284 module_cache_spec.CopyByRemovingLastPathComponent();
285 return llvm::sys::fs::create_directory(module_cache_folder.GetPath());
286}
287
290 const lldb_private::ModuleSpec &module_spec,
291 const FileSpec &module_cache_spec) {
292 MakeCacheFolderForFile(module_cache_spec);
293 Status err = platform->GetFile(module_spec.GetFileSpec(), module_cache_spec);
294 return err;
295}
296
298 const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
299 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr,
300 Process *process) {
301
303 LLDB_LOGF(log,
304 "[%s] Trying to find module %s/%s - platform path %s/%s symbol "
305 "path %s/%s",
306 (IsHost() ? "host" : "remote"),
307 module_spec.GetFileSpec().GetDirectory().AsCString(),
308 module_spec.GetFileSpec().GetFilename().AsCString(),
310 module_spec.GetPlatformFileSpec().GetFilename().AsCString(),
311 module_spec.GetSymbolFileSpec().GetDirectory().AsCString(),
312 module_spec.GetSymbolFileSpec().GetFilename().AsCString());
313
314 Status err;
315
316 if (CheckLocalSharedCache()) {
317 err = GetModuleFromSharedCaches(module_spec, process, module_sp,
318 old_modules, did_create_ptr);
319 if (module_sp)
320 return err;
321 }
322
323 // We failed to find the module in our shared cache. Let's see if we have a
324 // copy in our device support directory.
325 FileSpec device_support_spec(GetDeviceSupportDirectoryForOSVersion());
326 device_support_spec.AppendPathComponent("Symbols");
327 device_support_spec.AppendPathComponent(
328 module_spec.GetFileSpec().GetPath());
329 FileSystem::Instance().Resolve(device_support_spec);
330 if (FileSystem::Instance().Exists(device_support_spec)) {
331 ModuleSpec local_spec(device_support_spec, module_spec.GetUUID());
332 err = ModuleList::GetSharedModule(local_spec, module_sp, old_modules,
333 did_create_ptr);
334 if (module_sp) {
335 LLDB_LOGF(log,
336 "[%s] module %s was found in Device Support "
337 "directory: %s",
338 (IsHost() ? "host" : "remote"),
339 module_spec.GetFileSpec().GetPath().c_str(),
340 local_spec.GetFileSpec().GetPath().c_str());
341 return err;
342 }
343 }
344
345 err = ModuleList::GetSharedModule(module_spec, module_sp, old_modules,
346 did_create_ptr);
347 if (module_sp)
348 return err;
349
350 if (!IsHost()) {
351 std::string cache_path(GetLocalCacheDirectory());
352 // Only search for a locally cached file if we have a valid cache path
353 if (!cache_path.empty()) {
354 std::string module_path(module_spec.GetFileSpec().GetPath());
355 cache_path.append(module_path);
356 FileSpec module_cache_spec(cache_path);
357
358 // if rsync is supported, always bring in the file - rsync will be very
359 // efficient when files are the same on the local and remote end of the
360 // connection
361 if (this->GetSupportsRSync()) {
362 err = BringInRemoteFile(this, module_spec, module_cache_spec);
363 if (err.Fail())
364 return err;
365 if (FileSystem::Instance().Exists(module_cache_spec)) {
367 LLDB_LOGF(log, "[%s] module %s/%s was rsynced and is now there",
368 (IsHost() ? "host" : "remote"),
369 module_spec.GetFileSpec().GetDirectory().AsCString(),
370 module_spec.GetFileSpec().GetFilename().AsCString());
371 ModuleSpec local_spec(module_cache_spec,
372 module_spec.GetArchitecture());
373 module_sp = std::make_shared<Module>(local_spec);
374 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
375 return Status();
376 }
377 }
378
379 // try to find the module in the cache
380 if (FileSystem::Instance().Exists(module_cache_spec)) {
381 // get the local and remote MD5 and compare
383 // when going over the *slow* GDB remote transfer mechanism we first
384 // check the hashes of the files - and only do the actual transfer if
385 // they differ
386 auto MD5 = llvm::sys::fs::md5_contents(module_cache_spec.GetPath());
387 if (!MD5)
388 return Status(MD5.getError());
389
391 bool requires_transfer = true;
392 llvm::ErrorOr<llvm::MD5::MD5Result> remote_md5 =
393 m_remote_platform_sp->CalculateMD5(module_spec.GetFileSpec());
394 if (std::error_code ec = remote_md5.getError())
395 LLDB_LOG(log, "couldn't get md5 sum from remote: {0}",
396 ec.message());
397 else
398 requires_transfer = *MD5 != *remote_md5;
399 if (requires_transfer) {
400 // bring in the remote file
401 LLDB_LOGF(log,
402 "[%s] module %s/%s needs to be replaced from remote copy",
403 (IsHost() ? "host" : "remote"),
404 module_spec.GetFileSpec().GetDirectory().AsCString(),
405 module_spec.GetFileSpec().GetFilename().AsCString());
406 Status err =
407 BringInRemoteFile(this, module_spec, module_cache_spec);
408 if (err.Fail())
409 return err;
410 }
411 }
412
413 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture());
414 module_sp = std::make_shared<Module>(local_spec);
415 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
417 LLDB_LOGF(log, "[%s] module %s/%s was found in the cache",
418 (IsHost() ? "host" : "remote"),
419 module_spec.GetFileSpec().GetDirectory().AsCString(),
420 module_spec.GetFileSpec().GetFilename().AsCString());
421 return Status();
422 }
423
424 // bring in the remote module file
425 LLDB_LOGF(log, "[%s] module %s/%s needs to come in remotely",
426 (IsHost() ? "host" : "remote"),
427 module_spec.GetFileSpec().GetDirectory().AsCString(),
428 module_spec.GetFileSpec().GetFilename().AsCString());
429 Status err = BringInRemoteFile(this, module_spec, module_cache_spec);
430 if (err.Fail())
431 return err;
432 if (FileSystem::Instance().Exists(module_cache_spec)) {
434 LLDB_LOGF(log, "[%s] module %s/%s is now cached and fine",
435 (IsHost() ? "host" : "remote"),
436 module_spec.GetFileSpec().GetDirectory().AsCString(),
437 module_spec.GetFileSpec().GetFilename().AsCString());
438 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture());
439 module_sp = std::make_shared<Module>(local_spec);
440 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
441 return Status();
442 } else
443 return Status::FromErrorString("unable to obtain valid module file");
444 } else
445 return Status::FromErrorString("no cache path");
446 } else
447 return Status::FromErrorString("unable to resolve module");
448}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOGF(log,...)
Definition Log.h:376
static lldb_private::Status BringInRemoteFile(Platform *platform, const lldb_private::ModuleSpec &module_spec, const FileSpec &module_cache_spec)
static lldb_private::Status MakeCacheFolderForFile(const FileSpec &module_cache_spec)
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
A file utility class.
Definition FileSpec.h:57
void AppendPathComponent(llvm::StringRef component)
Definition FileSpec.cpp:454
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:250
const ConstString & GetDirectory() const
Directory string const get accessor.
Definition FileSpec.h:234
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
FileSpec CopyByRemovingLastPathComponent() const
Definition FileSpec.cpp:431
void EnumerateDirectory(llvm::Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, void *callback_baton)
@ eEnumerateDirectoryResultNext
Enumerate next entry in the current directory.
Definition FileSystem.h:182
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
static Status GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, bool invoke_locate_callback=true)
FileSpec & GetPlatformFileSpec()
Definition ModuleSpec.h:69
FileSpec & GetFileSpec()
Definition ModuleSpec.h:57
ArchSpec & GetArchitecture()
Definition ModuleSpec.h:93
FileSpec & GetSymbolFileSpec()
Definition ModuleSpec.h:81
virtual llvm::StringRef GetDeviceSupportDirectoryName()=0
virtual llvm::StringRef GetPlatformName()=0
static FileSystem::EnumerateDirectoryResult GetContainedFilesIntoVectorOfStringsCallback(void *baton, llvm::sys::fs::file_type ft, llvm::StringRef path)
std::vector< SDKDirectoryInfo > SDKDirectoryInfoCollection
const SDKDirectoryInfo * GetSDKDirectoryForCurrentOSVersion()
SDKDirectoryInfoCollection m_sdk_directory_infos
const SDKDirectoryInfo * GetSDKDirectoryForLatestOSVersion()
virtual Status GetSharedModuleWithLocalCache(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, lldb_private::Process *process)
Status GetModuleFromSharedCaches(const ModuleSpec &module_spec, Process *process, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr)
llvm::VersionTuple GetOSVersion(Process *process=nullptr) override
Get the OS version from a connected platform.
virtual bool CheckLocalSharedCache() const
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition Platform.h:78
const std::string & GetSDKBuild() const
Definition Platform.h:539
virtual const char * GetLocalCacheDirectory()
std::optional< std::string > GetOSBuildString()
Definition Platform.cpp:338
virtual Status GetFile(const FileSpec &source, const FileSpec &destination)
virtual bool GetSupportsRSync()
Definition Platform.h:640
bool IsHost() const
Definition Platform.h:505
std::string m_sdk_sysroot
Definition Platform.h:1011
A plug-in interface definition class for debugging a process.
Definition Process.h:354
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Fail() const
Test for error condition.
Definition Status.cpp:293
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::Module > ModuleSP
#define PATH_MAX