/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- HostInfoMacOSX.mm ---------------------------------------*- C++ -*-===// |
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 | | |
9 | | #include "lldb/Host/macosx/HostInfoMacOSX.h" |
10 | | #include "lldb/Host/FileSystem.h" |
11 | | #include "lldb/Host/Host.h" |
12 | | #include "lldb/Host/HostInfo.h" |
13 | | #include "lldb/Utility/Args.h" |
14 | | #include "lldb/Utility/LLDBLog.h" |
15 | | #include "lldb/Utility/Log.h" |
16 | | #include "lldb/Utility/Timer.h" |
17 | | |
18 | | #include "llvm/ADT/ScopeExit.h" |
19 | | #include "llvm/ADT/SmallString.h" |
20 | | #include "llvm/ADT/StringMap.h" |
21 | | #include "llvm/Support/FileSystem.h" |
22 | | #include "llvm/Support/Path.h" |
23 | | #include "llvm/Support/raw_ostream.h" |
24 | | |
25 | | // C++ Includes |
26 | | #include <optional> |
27 | | #include <string> |
28 | | |
29 | | // C inclues |
30 | | #include <cstdlib> |
31 | | #include <sys/sysctl.h> |
32 | | #include <sys/syslimits.h> |
33 | | #include <sys/types.h> |
34 | | #include <uuid/uuid.h> |
35 | | |
36 | | // Objective-C/C++ includes |
37 | | #include <AvailabilityMacros.h> |
38 | | #include <CoreFoundation/CoreFoundation.h> |
39 | | #include <Foundation/Foundation.h> |
40 | | #include <mach-o/dyld.h> |
41 | | #if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
42 | | MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_12_0 |
43 | | #if __has_include(<mach-o/dyld_introspection.h>) |
44 | | #include <mach-o/dyld_introspection.h> |
45 | | #define SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS |
46 | | #endif |
47 | | #endif |
48 | | #include <objc/objc-auto.h> |
49 | | |
50 | | // These are needed when compiling on systems |
51 | | // that do not yet have these definitions |
52 | | #ifndef CPU_SUBTYPE_X86_64_H |
53 | | #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t)8) |
54 | | #endif |
55 | | #ifndef CPU_TYPE_ARM64 |
56 | | #define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64) |
57 | | #endif |
58 | | |
59 | | #ifndef CPU_TYPE_ARM64_32 |
60 | | #define CPU_ARCH_ABI64_32 0x02000000 |
61 | | #define CPU_TYPE_ARM64_32 (CPU_TYPE_ARM | CPU_ARCH_ABI64_32) |
62 | | #endif |
63 | | |
64 | | #include <TargetConditionals.h> // for TARGET_OS_TV, TARGET_OS_WATCH |
65 | | |
66 | | using namespace lldb_private; |
67 | | |
68 | 13 | std::optional<std::string> HostInfoMacOSX::GetOSBuildString() { |
69 | 13 | int mib[2] = {CTL_KERN, KERN_OSVERSION}; |
70 | 13 | char cstr[PATH_MAX]; |
71 | 13 | size_t cstr_len = sizeof(cstr); |
72 | 13 | if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0) |
73 | 13 | return std::string(cstr, cstr_len - 1); |
74 | | |
75 | 0 | return std::nullopt; |
76 | 13 | } |
77 | | |
78 | 966 | static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) { |
79 | 966 | @autoreleasepool { |
80 | 966 | NSDictionary *version_info = |
81 | 966 | [NSDictionary dictionaryWithContentsOfFile: |
82 | 966 | @"/System/Library/CoreServices/SystemVersion.plist"]; |
83 | 966 | NSString *version_value = [version_info objectForKey: Key]; |
84 | 966 | const char *version_str = [version_value UTF8String]; |
85 | 966 | version.tryParse(version_str); |
86 | 966 | } |
87 | 966 | } |
88 | | |
89 | 1.61k | llvm::VersionTuple HostInfoMacOSX::GetOSVersion() { |
90 | 1.61k | static llvm::VersionTuple g_version; |
91 | 1.61k | if (g_version.empty()) |
92 | 966 | ParseOSVersion(g_version, @"ProductVersion"); |
93 | 1.61k | return g_version; |
94 | 1.61k | } |
95 | | |
96 | 0 | llvm::VersionTuple HostInfoMacOSX::GetMacCatalystVersion() { |
97 | 0 | static llvm::VersionTuple g_version; |
98 | 0 | if (g_version.empty()) |
99 | 0 | ParseOSVersion(g_version, @"iOSSupportVersion"); |
100 | 0 | return g_version; |
101 | 0 | } |
102 | | |
103 | | |
104 | 3 | FileSpec HostInfoMacOSX::GetProgramFileSpec() { |
105 | 3 | static FileSpec g_program_filespec; |
106 | 3 | if (!g_program_filespec) { |
107 | 3 | char program_fullpath[PATH_MAX]; |
108 | | // If DST is NULL, then return the number of bytes needed. |
109 | 3 | uint32_t len = sizeof(program_fullpath); |
110 | 3 | int err = _NSGetExecutablePath(program_fullpath, &len); |
111 | 3 | if (err == 0) |
112 | 3 | g_program_filespec.SetFile(program_fullpath, FileSpec::Style::native); |
113 | 0 | else if (err == -1) { |
114 | 0 | char *large_program_fullpath = (char *)::malloc(len + 1); |
115 | |
|
116 | 0 | err = _NSGetExecutablePath(large_program_fullpath, &len); |
117 | 0 | if (err == 0) |
118 | 0 | g_program_filespec.SetFile(large_program_fullpath, |
119 | 0 | FileSpec::Style::native); |
120 | |
|
121 | 0 | ::free(large_program_fullpath); |
122 | 0 | } |
123 | 3 | } |
124 | 3 | return g_program_filespec; |
125 | 3 | } |
126 | | |
127 | 809 | bool HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec) { |
128 | 809 | FileSpec lldb_file_spec = GetShlibDir(); |
129 | 809 | if (!lldb_file_spec) |
130 | 0 | return false; |
131 | | |
132 | 809 | std::string raw_path = lldb_file_spec.GetPath(); |
133 | | |
134 | 809 | size_t framework_pos = raw_path.find("LLDB.framework"); |
135 | 809 | if (framework_pos != std::string::npos) { |
136 | 0 | framework_pos += strlen("LLDB.framework"); |
137 | | #if TARGET_OS_IPHONE |
138 | | // Shallow bundle |
139 | | raw_path.resize(framework_pos); |
140 | | #else |
141 | | // Normal bundle |
142 | 0 | raw_path.resize(framework_pos); |
143 | 0 | raw_path.append("/Resources"); |
144 | 0 | #endif |
145 | 809 | } else { |
146 | | // Find the bin path relative to the lib path where the cmake-based |
147 | | // OS X .dylib lives. This is not going to work if the bin and lib |
148 | | // dir are not both in the same dir. |
149 | | // |
150 | | // It is not going to work to do it by the executable path either, |
151 | | // as in the case of a python script, the executable is python, not |
152 | | // the lldb driver. |
153 | 809 | raw_path.append("/../bin"); |
154 | 809 | FileSpec support_dir_spec(raw_path); |
155 | 809 | FileSystem::Instance().Resolve(support_dir_spec); |
156 | 809 | if (!FileSystem::Instance().IsDirectory(support_dir_spec)) { |
157 | 0 | Log *log = GetLog(LLDBLog::Host); |
158 | 0 | LLDB_LOG(log, "failed to find support directory"); |
159 | 0 | return false; |
160 | 0 | } |
161 | | |
162 | | // Get normalization from support_dir_spec. Note the FileSpec resolve |
163 | | // does not remove '..' in the path. |
164 | 809 | char *const dir_realpath = |
165 | 809 | realpath(support_dir_spec.GetPath().c_str(), NULL); |
166 | 809 | if (dir_realpath) { |
167 | 809 | raw_path = dir_realpath; |
168 | 809 | free(dir_realpath); |
169 | 809 | } else { |
170 | 0 | raw_path = support_dir_spec.GetPath(); |
171 | 0 | } |
172 | 809 | } |
173 | | |
174 | 809 | file_spec.SetDirectory(raw_path); |
175 | 809 | return (bool)file_spec.GetDirectory(); |
176 | 809 | } |
177 | | |
178 | 1 | bool HostInfoMacOSX::ComputeHeaderDirectory(FileSpec &file_spec) { |
179 | 1 | FileSpec lldb_file_spec = GetShlibDir(); |
180 | 1 | if (!lldb_file_spec) |
181 | 0 | return false; |
182 | | |
183 | 1 | std::string raw_path = lldb_file_spec.GetPath(); |
184 | | |
185 | 1 | size_t framework_pos = raw_path.find("LLDB.framework"); |
186 | 1 | if (framework_pos != std::string::npos) { |
187 | 0 | framework_pos += strlen("LLDB.framework"); |
188 | 0 | raw_path.resize(framework_pos); |
189 | 0 | raw_path.append("/Headers"); |
190 | 0 | } |
191 | 1 | file_spec.SetDirectory(raw_path); |
192 | 1 | return true; |
193 | 1 | } |
194 | | |
195 | 3.97k | bool HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec) { |
196 | 3.97k | FileSpec lldb_file_spec = GetShlibDir(); |
197 | 3.97k | if (!lldb_file_spec) |
198 | 0 | return false; |
199 | | |
200 | 3.97k | std::string raw_path = lldb_file_spec.GetPath(); |
201 | | |
202 | 3.97k | size_t framework_pos = raw_path.find("LLDB.framework"); |
203 | 3.97k | if (framework_pos == std::string::npos) |
204 | 3.97k | return false; |
205 | | |
206 | 0 | framework_pos += strlen("LLDB.framework"); |
207 | 0 | raw_path.resize(framework_pos); |
208 | 0 | raw_path.append("/Resources/PlugIns"); |
209 | 0 | file_spec.SetDirectory(raw_path); |
210 | 0 | return true; |
211 | 3.97k | } |
212 | | |
213 | 3.97k | bool HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec) { |
214 | 3.97k | FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns"); |
215 | 3.97k | FileSystem::Instance().Resolve(temp_file); |
216 | 3.97k | file_spec.SetDirectory(temp_file.GetPathAsConstString()); |
217 | 3.97k | return true; |
218 | 3.97k | } |
219 | | |
220 | | void HostInfoMacOSX::ComputeHostArchitectureSupport(ArchSpec &arch_32, |
221 | 4.04k | ArchSpec &arch_64) { |
222 | | // All apple systems support 32 bit execution. |
223 | 4.04k | uint32_t cputype, cpusubtype; |
224 | 4.04k | uint32_t is_64_bit_capable = false; |
225 | 4.04k | size_t len = sizeof(cputype); |
226 | 4.04k | ArchSpec host_arch; |
227 | | // These will tell us about the kernel architecture, which even on a 64 |
228 | | // bit machine can be 32 bit... |
229 | 4.04k | if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0) { |
230 | 4.04k | len = sizeof(cpusubtype); |
231 | 4.04k | if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0) |
232 | 0 | cpusubtype = CPU_TYPE_ANY; |
233 | | |
234 | 4.04k | len = sizeof(is_64_bit_capable); |
235 | 4.04k | ::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0); |
236 | | |
237 | 4.04k | if (cputype == CPU_TYPE_ARM64 && cpusubtype == CPU_SUBTYPE_ARM64E0 ) { |
238 | | // The arm64e architecture is a preview. Pretend the host architecture |
239 | | // is arm64. |
240 | 0 | cpusubtype = CPU_SUBTYPE_ARM64_ALL; |
241 | 0 | } |
242 | | |
243 | 4.04k | if (is_64_bit_capable) { |
244 | 4.04k | if (cputype & CPU_ARCH_ABI64) { |
245 | | // We have a 64 bit kernel on a 64 bit system |
246 | 0 | arch_64.SetArchitecture(eArchTypeMachO, cputype, cpusubtype); |
247 | 4.04k | } else { |
248 | | // We have a 64 bit kernel that is returning a 32 bit cputype, the |
249 | | // cpusubtype will be correct as if it were for a 64 bit architecture |
250 | 4.04k | arch_64.SetArchitecture(eArchTypeMachO, cputype | CPU_ARCH_ABI64, |
251 | 4.04k | cpusubtype); |
252 | 4.04k | } |
253 | | |
254 | | // Now we need modify the cpusubtype for the 32 bit slices. |
255 | 4.04k | uint32_t cpusubtype32 = cpusubtype; |
256 | 4.04k | #if defined(__i386__) || defined(__x86_64__) |
257 | 4.04k | if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H0 ) |
258 | 4.04k | cpusubtype32 = CPU_SUBTYPE_I386_ALL; |
259 | | #elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
260 | | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
261 | | cpusubtype32 = CPU_SUBTYPE_ARM_V7S; |
262 | | #endif |
263 | 4.04k | arch_32.SetArchitecture(eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), |
264 | 4.04k | cpusubtype32); |
265 | | |
266 | 4.04k | if (cputype == CPU_TYPE_ARM || |
267 | 4.04k | cputype == CPU_TYPE_ARM64 || |
268 | 4.04k | cputype == CPU_TYPE_ARM64_32) { |
269 | | // When running on a watch or tv, report the host os correctly |
270 | | #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1 |
271 | | arch_32.GetTriple().setOS(llvm::Triple::TvOS); |
272 | | arch_64.GetTriple().setOS(llvm::Triple::TvOS); |
273 | | #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1 |
274 | | arch_32.GetTriple().setOS(llvm::Triple::BridgeOS); |
275 | | arch_64.GetTriple().setOS(llvm::Triple::BridgeOS); |
276 | | #elif defined(TARGET_OS_WATCHOS) && TARGET_OS_WATCHOS == 1 |
277 | | arch_32.GetTriple().setOS(llvm::Triple::WatchOS); |
278 | | arch_64.GetTriple().setOS(llvm::Triple::WatchOS); |
279 | | #elif defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1 |
280 | | arch_32.GetTriple().setOS(llvm::Triple::MacOSX); |
281 | 0 | arch_64.GetTriple().setOS(llvm::Triple::MacOSX); |
282 | | #else |
283 | | arch_32.GetTriple().setOS(llvm::Triple::IOS); |
284 | | arch_64.GetTriple().setOS(llvm::Triple::IOS); |
285 | | #endif |
286 | 4.04k | } else { |
287 | 4.04k | arch_32.GetTriple().setOS(llvm::Triple::MacOSX); |
288 | 4.04k | arch_64.GetTriple().setOS(llvm::Triple::MacOSX); |
289 | 4.04k | } |
290 | 4.04k | } else { |
291 | | // We have a 32 bit kernel on a 32 bit system |
292 | 0 | arch_32.SetArchitecture(eArchTypeMachO, cputype, cpusubtype); |
293 | | #if defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1 |
294 | | arch_32.GetTriple().setOS(llvm::Triple::WatchOS); |
295 | | #else |
296 | 0 | arch_32.GetTriple().setOS(llvm::Triple::IOS); |
297 | 0 | #endif |
298 | 0 | arch_64.Clear(); |
299 | 0 | } |
300 | 4.04k | } |
301 | 4.04k | } |
302 | | |
303 | | /// Return and cache $DEVELOPER_DIR if it is set and exists. |
304 | 2.32k | static std::string GetEnvDeveloperDir() { |
305 | 2.32k | static std::string g_env_developer_dir; |
306 | 2.32k | static std::once_flag g_once_flag; |
307 | 2.32k | std::call_once(g_once_flag, [&]() { |
308 | 944 | if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) { |
309 | 0 | FileSpec fspec(developer_dir_env_var); |
310 | 0 | if (FileSystem::Instance().Exists(fspec)) |
311 | 0 | g_env_developer_dir = fspec.GetPath(); |
312 | 0 | }}); |
313 | 2.32k | return g_env_developer_dir; |
314 | 2.32k | } |
315 | | |
316 | 1.04k | FileSpec HostInfoMacOSX::GetXcodeContentsDirectory() { |
317 | 1.04k | static FileSpec g_xcode_contents_path; |
318 | 1.04k | static std::once_flag g_once_flag; |
319 | 1.04k | std::call_once(g_once_flag, [&]() { |
320 | | // Try the shlib dir first. |
321 | 929 | if (FileSpec fspec = HostInfo::GetShlibDir()) { |
322 | 929 | if (FileSystem::Instance().Exists(fspec)) { |
323 | 929 | std::string xcode_contents_dir = |
324 | 929 | XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath()); |
325 | 929 | if (!xcode_contents_dir.empty()) { |
326 | 0 | g_xcode_contents_path = FileSpec(xcode_contents_dir); |
327 | 0 | return; |
328 | 0 | } |
329 | 929 | } |
330 | 929 | } |
331 | | |
332 | 929 | llvm::SmallString<128> env_developer_dir(GetEnvDeveloperDir()); |
333 | 929 | if (!env_developer_dir.empty()) { |
334 | 0 | llvm::sys::path::append(env_developer_dir, "Contents"); |
335 | 0 | std::string xcode_contents_dir = |
336 | 0 | XcodeSDK::FindXcodeContentsDirectoryInPath(env_developer_dir); |
337 | 0 | if (!xcode_contents_dir.empty()) { |
338 | 0 | g_xcode_contents_path = FileSpec(xcode_contents_dir); |
339 | 0 | return; |
340 | 0 | } |
341 | 0 | } |
342 | | |
343 | 929 | auto sdk_path_or_err = |
344 | 929 | HostInfo::GetSDKRoot(SDKOptions{XcodeSDK::GetAnyMacOS()}); |
345 | 929 | if (!sdk_path_or_err) { |
346 | 0 | Log *log = GetLog(LLDBLog::Host); |
347 | 0 | LLDB_LOG_ERROR(log, sdk_path_or_err.takeError(), |
348 | 0 | "Error while searching for Xcode SDK: {0}"); |
349 | 0 | return; |
350 | 0 | } |
351 | 929 | FileSpec fspec(*sdk_path_or_err); |
352 | 929 | if (fspec) { |
353 | 929 | if (FileSystem::Instance().Exists(fspec)) { |
354 | 929 | std::string xcode_contents_dir = |
355 | 929 | XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath()); |
356 | 929 | if (!xcode_contents_dir.empty()) { |
357 | 929 | g_xcode_contents_path = FileSpec(xcode_contents_dir); |
358 | 929 | return; |
359 | 929 | } |
360 | 929 | } |
361 | 929 | } |
362 | 929 | }); |
363 | 1.04k | return g_xcode_contents_path; |
364 | 1.04k | } |
365 | | |
366 | 896 | lldb_private::FileSpec HostInfoMacOSX::GetXcodeDeveloperDirectory() { |
367 | 896 | static lldb_private::FileSpec g_developer_directory; |
368 | 896 | static llvm::once_flag g_once_flag; |
369 | 896 | llvm::call_once(g_once_flag, []() { |
370 | 895 | if (FileSpec fspec = GetXcodeContentsDirectory()) { |
371 | 895 | fspec.AppendPathComponent("Developer"); |
372 | 895 | if (FileSystem::Instance().Exists(fspec)) |
373 | 895 | g_developer_directory = fspec; |
374 | 895 | } |
375 | 895 | }); |
376 | 896 | return g_developer_directory; |
377 | 896 | } |
378 | | |
379 | | static llvm::Expected<std::string> |
380 | | xcrun(const std::string &sdk, llvm::ArrayRef<llvm::StringRef> arguments, |
381 | 1.39k | llvm::StringRef developer_dir = "") { |
382 | 1.39k | Args args; |
383 | 1.39k | if (!developer_dir.empty()) { |
384 | 0 | args.AppendArgument("/usr/bin/env"); |
385 | 0 | args.AppendArgument("DEVELOPER_DIR=" + developer_dir.str()); |
386 | 0 | } |
387 | 1.39k | args.AppendArgument("/usr/bin/xcrun"); |
388 | 1.39k | args.AppendArgument("--sdk"); |
389 | 1.39k | args.AppendArgument(sdk); |
390 | 1.39k | for (auto arg: arguments) |
391 | 1.40k | args.AppendArgument(arg); |
392 | | |
393 | 1.39k | Log *log = GetLog(LLDBLog::Host); |
394 | 1.39k | if (log) { |
395 | 1 | std::string cmdstr; |
396 | 1 | args.GetCommandString(cmdstr); |
397 | 1 | log->Printf("GetXcodeSDK() running shell cmd '%s'", cmdstr.c_str()); |
398 | 1 | } |
399 | | |
400 | 1.39k | int status = 0; |
401 | 1.39k | int signo = 0; |
402 | 1.39k | std::string output_str; |
403 | | // The first time after Xcode was updated or freshly installed, |
404 | | // xcrun can take surprisingly long to build up its database. |
405 | 1.39k | auto timeout = std::chrono::seconds(60); |
406 | 1.39k | bool run_in_shell = false; |
407 | 1.39k | lldb_private::Status error = Host::RunShellCommand( |
408 | 1.39k | args, FileSpec(), &status, &signo, &output_str, timeout, run_in_shell); |
409 | | |
410 | | // Check that xcrun returned something useful. |
411 | 1.39k | if (error.Fail()) { |
412 | | // Catastrophic error. |
413 | 0 | LLDB_LOG(log, "xcrun failed to execute: %s", error.AsCString()); |
414 | 0 | return error.ToError(); |
415 | 0 | } |
416 | 1.39k | if (status != 0) { |
417 | | // xcrun didn't find a matching SDK. Not an error, we'll try |
418 | | // different spellings. |
419 | 14 | LLDB_LOG(log, "xcrun returned exit code %d", status); |
420 | 14 | return ""; |
421 | 14 | } |
422 | 1.38k | if (output_str.empty()) { |
423 | 0 | LLDB_LOG(log, "xcrun returned no results"); |
424 | 0 | return ""; |
425 | 0 | } |
426 | | |
427 | | // Convert to a StringRef so we can manipulate the string without modifying |
428 | | // the underlying data. |
429 | 1.38k | llvm::StringRef output(output_str); |
430 | | |
431 | | // Remove any trailing newline characters. |
432 | 1.38k | output = output.rtrim(); |
433 | | |
434 | | // Strip any leading newline characters and everything before them. |
435 | 1.38k | const size_t last_newline = output.rfind('\n'); |
436 | 1.38k | if (last_newline != llvm::StringRef::npos) |
437 | 0 | output = output.substr(last_newline + 1); |
438 | | |
439 | 1.38k | return output.str(); |
440 | 1.38k | } |
441 | | |
442 | 1.40k | static llvm::Expected<std::string> GetXcodeSDK(XcodeSDK sdk) { |
443 | 1.40k | XcodeSDK::Info info = sdk.Parse(); |
444 | 1.40k | std::string sdk_name = XcodeSDK::GetCanonicalName(info); |
445 | 1.40k | if (sdk_name.empty()) |
446 | 19 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
447 | 19 | "Unrecognized SDK type: " + sdk.GetString()); |
448 | | |
449 | 1.38k | Log *log = GetLog(LLDBLog::Host); |
450 | | |
451 | 1.38k | auto find_sdk = |
452 | 1.39k | [](const std::string &sdk_name) -> llvm::Expected<std::string> { |
453 | 1.39k | llvm::SmallVector<llvm::StringRef, 1> show_sdk_path = {"--show-sdk-path"}; |
454 | | // Invoke xcrun with the developer dir specified in the environment. |
455 | 1.39k | std::string developer_dir = GetEnvDeveloperDir(); |
456 | 1.39k | if (!developer_dir.empty()) { |
457 | | // Don't fallback if DEVELOPER_DIR was set. |
458 | 0 | return xcrun(sdk_name, show_sdk_path, developer_dir); |
459 | 0 | } |
460 | | |
461 | | // Invoke xcrun with the shlib dir. |
462 | 1.39k | if (FileSpec fspec = HostInfo::GetShlibDir()) { |
463 | 1.39k | if (FileSystem::Instance().Exists(fspec)) { |
464 | 1.39k | std::string contents_dir = |
465 | 1.39k | XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath()); |
466 | 1.39k | llvm::StringRef shlib_developer_dir = |
467 | 1.39k | llvm::sys::path::parent_path(contents_dir); |
468 | 1.39k | if (!shlib_developer_dir.empty()) { |
469 | 0 | auto sdk = |
470 | 0 | xcrun(sdk_name, show_sdk_path, std::move(shlib_developer_dir)); |
471 | 0 | if (!sdk) |
472 | 0 | return sdk.takeError(); |
473 | 0 | if (!sdk->empty()) |
474 | 0 | return sdk; |
475 | 0 | } |
476 | 1.39k | } |
477 | 1.39k | } |
478 | | |
479 | | // Invoke xcrun without a developer dir as a last resort. |
480 | 1.39k | return xcrun(sdk_name, show_sdk_path); |
481 | 1.39k | }; |
482 | | |
483 | 1.38k | auto path_or_err = find_sdk(sdk_name); |
484 | 1.38k | if (!path_or_err) |
485 | 0 | return path_or_err.takeError(); |
486 | 1.38k | std::string path = *path_or_err; |
487 | 1.38k | while (path.empty()) { |
488 | | // Try an alternate spelling of the name ("macosx10.9internal"). |
489 | 9 | if (info.type == XcodeSDK::Type::MacOSX && !info.version.empty()4 && |
490 | 9 | info.internal4 ) { |
491 | 0 | llvm::StringRef fixed(sdk_name); |
492 | 0 | if (fixed.consume_back(".internal")) |
493 | 0 | sdk_name = fixed.str() + "internal"; |
494 | 0 | path_or_err = find_sdk(sdk_name); |
495 | 0 | if (!path_or_err) |
496 | 0 | return path_or_err.takeError(); |
497 | 0 | path = *path_or_err; |
498 | 0 | if (!path.empty()) |
499 | 0 | break; |
500 | 0 | } |
501 | 9 | LLDB_LOG(log, "Couldn't find SDK {0} on host", sdk_name); |
502 | | |
503 | | // Try without the version. |
504 | 9 | if (!info.version.empty()) { |
505 | 9 | info.version = {}; |
506 | 9 | sdk_name = XcodeSDK::GetCanonicalName(info); |
507 | 9 | path_or_err = find_sdk(sdk_name); |
508 | 9 | if (!path_or_err) |
509 | 0 | return path_or_err.takeError(); |
510 | 9 | path = *path_or_err; |
511 | 9 | if (!path.empty()) |
512 | 5 | break; |
513 | 9 | } |
514 | | |
515 | 4 | LLDB_LOG(log, "Couldn't find any matching SDK on host"); |
516 | 4 | return ""; |
517 | 9 | } |
518 | | |
519 | | // Whatever is left in output should be a valid path. |
520 | 1.38k | if (!FileSystem::Instance().Exists(path)) { |
521 | 0 | LLDB_LOG(log, "SDK returned by xcrun doesn't exist"); |
522 | 0 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
523 | 0 | "SDK returned by xcrun doesn't exist"); |
524 | 0 | } |
525 | 1.38k | return path; |
526 | 1.38k | } |
527 | | |
528 | | namespace { |
529 | | struct ErrorOrPath { |
530 | | std::string str; |
531 | | bool is_error; |
532 | | }; |
533 | | } // namespace |
534 | | |
535 | | static llvm::Expected<llvm::StringRef> |
536 | | find_cached_path(llvm::StringMap<ErrorOrPath> &cache, std::mutex &mutex, |
537 | | llvm::StringRef key, |
538 | 3.81k | std::function<llvm::Expected<std::string>(void)> compute) { |
539 | 3.81k | std::lock_guard<std::mutex> guard(mutex); |
540 | 3.81k | LLDB_SCOPED_TIMER(); |
541 | | |
542 | 3.81k | auto it = cache.find(key); |
543 | 3.81k | if (it != cache.end()) { |
544 | 2.40k | if (it->second.is_error) |
545 | 39 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
546 | 39 | it->second.str); |
547 | 2.36k | return it->second.str; |
548 | 2.40k | } |
549 | 1.40k | auto path_or_err = compute(); |
550 | 1.40k | if (!path_or_err) { |
551 | 19 | std::string error = toString(path_or_err.takeError()); |
552 | 19 | cache.insert({key, {error, true}}); |
553 | 19 | return llvm::createStringError(llvm::inconvertibleErrorCode(), error); |
554 | 19 | } |
555 | 1.38k | auto it_new = cache.insert({key, {*path_or_err, false}}); |
556 | 1.38k | return it_new.first->second.str; |
557 | 1.40k | } |
558 | | |
559 | 3.80k | llvm::Expected<llvm::StringRef> HostInfoMacOSX::GetSDKRoot(SDKOptions options) { |
560 | 3.80k | static llvm::StringMap<ErrorOrPath> g_sdk_path; |
561 | 3.80k | static std::mutex g_sdk_path_mutex; |
562 | 3.80k | if (!options.XcodeSDKSelection) |
563 | 0 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
564 | 0 | "XcodeSDK not specified"); |
565 | 3.80k | XcodeSDK sdk = *options.XcodeSDKSelection; |
566 | 3.80k | auto key = sdk.GetString(); |
567 | 3.80k | return find_cached_path(g_sdk_path, g_sdk_path_mutex, key, [&](){ |
568 | 1.40k | return GetXcodeSDK(sdk); |
569 | 1.40k | }); |
570 | 3.80k | } |
571 | | |
572 | | llvm::Expected<llvm::StringRef> |
573 | 2 | HostInfoMacOSX::FindSDKTool(XcodeSDK sdk, llvm::StringRef tool) { |
574 | 2 | static llvm::StringMap<ErrorOrPath> g_tool_path; |
575 | 2 | static std::mutex g_tool_path_mutex; |
576 | 2 | std::string key; |
577 | 2 | llvm::raw_string_ostream(key) << sdk.GetString() << ":" << tool; |
578 | 2 | return find_cached_path( |
579 | 2 | g_tool_path, g_tool_path_mutex, key, |
580 | 2 | [&]() -> llvm::Expected<std::string> { |
581 | 2 | std::string sdk_name = XcodeSDK::GetCanonicalName(sdk.Parse()); |
582 | 2 | if (sdk_name.empty()) |
583 | 0 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
584 | 0 | "Unrecognized SDK type: " + |
585 | 0 | sdk.GetString()); |
586 | 2 | llvm::SmallVector<llvm::StringRef, 2> find = {"-find", tool}; |
587 | 2 | return xcrun(sdk_name, find); |
588 | 2 | }); |
589 | 2 | } |
590 | | |
591 | | namespace { |
592 | | struct dyld_shared_cache_dylib_text_info { |
593 | | uint64_t version; // current version 1 |
594 | | // following fields all exist in version 1 |
595 | | uint64_t loadAddressUnslid; |
596 | | uint64_t textSegmentSize; |
597 | | uuid_t dylibUuid; |
598 | | const char *path; // pointer invalid at end of iterations |
599 | | // following fields all exist in version 2 |
600 | | uint64_t textSegmentOffset; // offset from start of cache |
601 | | }; |
602 | | typedef struct dyld_shared_cache_dylib_text_info |
603 | | dyld_shared_cache_dylib_text_info; |
604 | | } |
605 | | |
606 | | extern "C" int dyld_shared_cache_iterate_text( |
607 | | const uuid_t cacheUuid, |
608 | | void (^callback)(const dyld_shared_cache_dylib_text_info *info)); |
609 | | extern "C" uint8_t *_dyld_get_shared_cache_range(size_t *length); |
610 | | extern "C" bool _dyld_get_shared_cache_uuid(uuid_t uuid); |
611 | | |
612 | | namespace { |
613 | | class SharedCacheInfo { |
614 | | public: |
615 | 0 | const UUID &GetUUID() const { return m_uuid; } |
616 | 225k | const llvm::StringMap<SharedCacheImageInfo> &GetImages() const { |
617 | 225k | return m_images; |
618 | 225k | } |
619 | | |
620 | | SharedCacheInfo(); |
621 | | |
622 | | private: |
623 | | bool CreateSharedCacheInfoWithInstrospectionSPIs(); |
624 | | |
625 | | llvm::StringMap<SharedCacheImageInfo> m_images; |
626 | | UUID m_uuid; |
627 | | }; |
628 | | } |
629 | | |
630 | 908 | bool SharedCacheInfo::CreateSharedCacheInfoWithInstrospectionSPIs() { |
631 | | #if defined(SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS) |
632 | | dyld_process_t dyld_process = dyld_process_create_for_current_task(); |
633 | | if (!dyld_process) |
634 | | return false; |
635 | | |
636 | | dyld_process_snapshot_t snapshot = |
637 | | dyld_process_snapshot_create_for_process(dyld_process, nullptr); |
638 | | if (!snapshot) |
639 | | return false; |
640 | | |
641 | | auto on_exit = |
642 | | llvm::make_scope_exit([&]() { dyld_process_snapshot_dispose(snapshot); }); |
643 | | |
644 | | dyld_shared_cache_t shared_cache = |
645 | | dyld_process_snapshot_get_shared_cache(snapshot); |
646 | | if (!shared_cache) |
647 | | return false; |
648 | | |
649 | | dyld_shared_cache_for_each_image(shared_cache, ^(dyld_image_t image) { |
650 | | __block uint64_t minVmAddr = UINT64_MAX; |
651 | | __block uint64_t maxVmAddr = 0; |
652 | | uuid_t uuidStore; |
653 | | __block uuid_t *uuid = &uuidStore; |
654 | | |
655 | | dyld_image_for_each_segment_info( |
656 | | image, |
657 | | ^(const char *segmentName, uint64_t vmAddr, uint64_t vmSize, int perm) { |
658 | | minVmAddr = std::min(minVmAddr, vmAddr); |
659 | | maxVmAddr = std::max(maxVmAddr, vmAddr + vmSize); |
660 | | dyld_image_copy_uuid(image, uuid); |
661 | | }); |
662 | | assert(minVmAddr != UINT_MAX); |
663 | | assert(maxVmAddr != 0); |
664 | | m_images[dyld_image_get_installname(image)] = SharedCacheImageInfo{ |
665 | | UUID(uuid, 16), std::make_shared<DataBufferUnowned>( |
666 | | (uint8_t *)minVmAddr, maxVmAddr - minVmAddr)}; |
667 | | }); |
668 | | return true; |
669 | | #endif |
670 | 908 | return false; |
671 | 908 | } |
672 | | |
673 | 908 | SharedCacheInfo::SharedCacheInfo() { |
674 | 908 | if (CreateSharedCacheInfoWithInstrospectionSPIs()) |
675 | 0 | return; |
676 | | |
677 | 908 | size_t shared_cache_size; |
678 | 908 | uint8_t *shared_cache_start = |
679 | 908 | _dyld_get_shared_cache_range(&shared_cache_size); |
680 | 908 | uuid_t dsc_uuid; |
681 | 908 | _dyld_get_shared_cache_uuid(dsc_uuid); |
682 | 908 | m_uuid = UUID(dsc_uuid); |
683 | | |
684 | 908 | dyld_shared_cache_iterate_text( |
685 | 1.64M | dsc_uuid, ^(const dyld_shared_cache_dylib_text_info *info) { |
686 | 1.64M | m_images[info->path] = SharedCacheImageInfo{ |
687 | 1.64M | UUID(info->dylibUuid, 16), |
688 | 1.64M | std::make_shared<DataBufferUnowned>( |
689 | 1.64M | shared_cache_start + info->textSegmentOffset, |
690 | 1.64M | shared_cache_size - info->textSegmentOffset)}; |
691 | 1.64M | }); |
692 | 908 | } |
693 | | |
694 | | SharedCacheImageInfo |
695 | 225k | HostInfoMacOSX::GetSharedCacheImageInfo(llvm::StringRef image_name) { |
696 | 225k | static SharedCacheInfo g_shared_cache_info; |
697 | 225k | return g_shared_cache_info.GetImages().lookup(image_name); |
698 | 225k | } |