1 //===-- HostInfoMacOSX.mm ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #if !defined(LLDB_DISABLE_PYTHON)
11 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
14 #include "lldb/Host/HostInfo.h"
15 #include "lldb/Host/macosx/HostInfoMacOSX.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Interpreter/Args.h"
18 #include "lldb/Utility/SafeMachO.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/raw_ostream.h"
28 #include <sys/sysctl.h>
29 #include <sys/syslimits.h>
30 #include <sys/types.h>
32 // Objective C/C++ includes
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <Foundation/Foundation.h>
35 #include <mach-o/dyld.h>
36 #include <objc/objc-auto.h>
38 // These are needed when compiling on systems
39 // that do not yet have these definitions
40 #include <AvailabilityMacros.h>
41 #ifndef CPU_SUBTYPE_X86_64_H
42 #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t)8)
44 #ifndef CPU_TYPE_ARM64
45 #define CPU_TYPE_ARM64 (CPU_TYPE_ARM|CPU_ARCH_ABI64)
48 #include <TargetConditionals.h> // for TARGET_OS_TV, TARGET_OS_WATCH
50 using namespace lldb_private;
53 HostInfoMacOSX::GetOSBuildString(std::string &s)
55 int mib[2] = {CTL_KERN, KERN_OSVERSION};
57 size_t cstr_len = sizeof(cstr);
58 if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0)
60 s.assign(cstr, cstr_len);
69 HostInfoMacOSX::GetOSKernelDescription(std::string &s)
71 int mib[2] = {CTL_KERN, KERN_VERSION};
73 size_t cstr_len = sizeof(cstr);
74 if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0)
76 s.assign(cstr, cstr_len);
84 HostInfoMacOSX::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
86 static uint32_t g_major = 0;
87 static uint32_t g_minor = 0;
88 static uint32_t g_update = 0;
94 NSDictionary *version_info = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
95 NSString *version_value = [version_info objectForKey:@"ProductVersion"];
96 const char *version_str = [version_value UTF8String];
98 Args::StringToVersion(version_str, g_major, g_minor, g_update);
113 HostInfoMacOSX::GetProgramFileSpec()
115 static FileSpec g_program_filespec;
116 if (!g_program_filespec)
118 char program_fullpath[PATH_MAX];
119 // If DST is NULL, then return the number of bytes needed.
120 uint32_t len = sizeof(program_fullpath);
121 int err = _NSGetExecutablePath(program_fullpath, &len);
123 g_program_filespec.SetFile(program_fullpath, false);
126 char *large_program_fullpath = (char *)::malloc(len + 1);
128 err = _NSGetExecutablePath(large_program_fullpath, &len);
130 g_program_filespec.SetFile(large_program_fullpath, false);
132 ::free(large_program_fullpath);
135 return g_program_filespec;
139 HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec)
141 FileSpec lldb_file_spec;
142 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
145 std::string raw_path = lldb_file_spec.GetPath();
147 size_t framework_pos = raw_path.find("LLDB.framework");
148 if (framework_pos != std::string::npos)
150 framework_pos += strlen("LLDB.framework");
151 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
153 raw_path.resize(framework_pos);
156 raw_path.resize(framework_pos);
157 raw_path.append("/Resources");
162 // Find the bin path relative to the lib path where the cmake-based
163 // OS X .dylib lives. This is not going to work if the bin and lib
164 // dir are not both in the same dir.
166 // It is not going to work to do it by the executable path either,
167 // as in the case of a python script, the executable is python, not
169 raw_path.append("/../bin");
170 FileSpec support_dir_spec(raw_path, true);
171 if (!support_dir_spec.Exists() || !support_dir_spec.IsDirectory())
173 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
175 log->Printf("HostInfoMacOSX::%s(): failed to find support directory",
180 // Get normalization from support_dir_spec. Note the FileSpec resolve
181 // does not remove '..' in the path.
182 char *const dir_realpath = realpath(support_dir_spec.GetPath().c_str(), NULL);
185 raw_path = dir_realpath;
190 raw_path = support_dir_spec.GetPath();
194 file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size()));
195 return (bool)file_spec.GetDirectory();
199 HostInfoMacOSX::ComputeHeaderDirectory(FileSpec &file_spec)
201 FileSpec lldb_file_spec;
202 if (!HostInfo::GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
205 std::string raw_path = lldb_file_spec.GetPath();
207 size_t framework_pos = raw_path.find("LLDB.framework");
208 if (framework_pos != std::string::npos)
210 framework_pos += strlen("LLDB.framework");
211 raw_path.resize(framework_pos);
212 raw_path.append("/Headers");
214 file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size()));
219 HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec)
221 #ifndef LLDB_DISABLE_PYTHON
222 FileSpec lldb_file_spec;
223 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
226 std::string raw_path = lldb_file_spec.GetPath();
228 size_t framework_pos = raw_path.find("LLDB.framework");
229 if (framework_pos != std::string::npos)
231 framework_pos += strlen("LLDB.framework");
232 raw_path.resize(framework_pos);
233 raw_path.append("/Resources/Python");
237 llvm::SmallString<256> python_version_dir;
238 llvm::raw_svector_ostream os(python_version_dir);
239 os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages";
241 // We may get our string truncated. Should we protect this with an assert?
242 raw_path.append(python_version_dir.c_str());
244 file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size()));
252 HostInfoMacOSX::ComputeClangDirectory(FileSpec &file_spec)
254 FileSpec lldb_file_spec;
255 if (!GetLLDBPath (lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
258 std::string raw_path = lldb_file_spec.GetPath();
260 size_t framework_pos = raw_path.find("LLDB.framework");
261 if (framework_pos != std::string::npos)
263 framework_pos += strlen("LLDB.framework");
264 raw_path.resize(framework_pos);
265 raw_path.append("/Resources/Clang");
267 file_spec.SetFile (raw_path.c_str(), true);
272 HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec)
274 FileSpec lldb_file_spec;
275 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
278 std::string raw_path = lldb_file_spec.GetPath();
280 size_t framework_pos = raw_path.find("LLDB.framework");
281 if (framework_pos == std::string::npos)
284 framework_pos += strlen("LLDB.framework");
285 raw_path.resize(framework_pos);
286 raw_path.append("/Resources/PlugIns");
287 file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size()));
292 HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec)
294 FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns", true);
295 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
300 HostInfoMacOSX::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
302 // All apple systems support 32 bit execution.
303 uint32_t cputype, cpusubtype;
304 uint32_t is_64_bit_capable = false;
305 size_t len = sizeof(cputype);
307 // These will tell us about the kernel architecture, which even on a 64
308 // bit machine can be 32 bit...
309 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
311 len = sizeof(cpusubtype);
312 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
313 cpusubtype = CPU_TYPE_ANY;
315 len = sizeof(is_64_bit_capable);
316 ::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0);
318 if (is_64_bit_capable)
320 if (cputype & CPU_ARCH_ABI64)
322 // We have a 64 bit kernel on a 64 bit system
323 arch_64.SetArchitecture(eArchTypeMachO, cputype, cpusubtype);
327 // We have a 64 bit kernel that is returning a 32 bit cputype, the
328 // cpusubtype will be correct as if it were for a 64 bit architecture
329 arch_64.SetArchitecture(eArchTypeMachO, cputype | CPU_ARCH_ABI64, cpusubtype);
332 // Now we need modify the cpusubtype for the 32 bit slices.
333 uint32_t cpusubtype32 = cpusubtype;
334 #if defined(__i386__) || defined(__x86_64__)
335 if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H)
336 cpusubtype32 = CPU_SUBTYPE_I386_ALL;
337 #elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
338 if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64)
339 cpusubtype32 = CPU_SUBTYPE_ARM_V7S;
341 arch_32.SetArchitecture(eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), cpusubtype32);
343 if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64)
345 // When running on a watch or tv, report the host os correctly
346 #if defined (TARGET_OS_TV) && TARGET_OS_TV == 1
347 arch_32.GetTriple().setOS(llvm::Triple::TvOS);
348 arch_64.GetTriple().setOS(llvm::Triple::TvOS);
350 arch_32.GetTriple().setOS(llvm::Triple::IOS);
351 arch_64.GetTriple().setOS(llvm::Triple::IOS);
356 arch_32.GetTriple().setOS(llvm::Triple::MacOSX);
357 arch_64.GetTriple().setOS(llvm::Triple::MacOSX);
362 // We have a 32 bit kernel on a 32 bit system
363 arch_32.SetArchitecture(eArchTypeMachO, cputype, cpusubtype);
364 #if defined (TARGET_OS_WATCH) && TARGET_OS_WATCH == 1
365 arch_32.GetTriple().setOS(llvm::Triple::WatchOS);
367 arch_32.GetTriple().setOS(llvm::Triple::IOS);