| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 1 | //===-- HostInfoMacOSX.mm ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| Zachary Turner | 2c1f46d | 2015-07-30 20:28:07 +0000 | [diff] [blame] | 10 | #if !defined(LLDB_DISABLE_PYTHON) |
| 11 | #include "Plugins/ScriptInterpreter/Python/lldb-python.h" |
| 12 | #endif |
| Zachary Turner | 365dc29 | 2015-05-29 18:18:26 +0000 | [diff] [blame] | 13 | |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 14 | #include "lldb/Host/HostInfo.h" |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 15 | #include "lldb/Host/macosx/HostInfoMacOSX.h" |
| Todd Fiala | 5e36548 | 2015-09-24 21:59:48 +0000 | [diff] [blame^] | 16 | #include "lldb/Core/Log.h" |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/Args.h" |
| Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/SafeMachO.h" |
| 19 | |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 23 | // C++ Includes |
| 24 | #include <string> |
| 25 | |
| 26 | // C inclues |
| Todd Fiala | 5e36548 | 2015-09-24 21:59:48 +0000 | [diff] [blame^] | 27 | #include <stdlib.h> |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 28 | #include <sys/sysctl.h> |
| Todd Fiala | 5e36548 | 2015-09-24 21:59:48 +0000 | [diff] [blame^] | 29 | #include <sys/syslimits.h> |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | |
| 32 | // Objective C/C++ includes |
| 33 | #include <CoreFoundation/CoreFoundation.h> |
| 34 | #include <Foundation/Foundation.h> |
| Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 35 | #include <mach-o/dyld.h> |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 36 | #include <objc/objc-auto.h> |
| 37 | |
| Keno Fischer | 5777edf | 2014-08-23 21:40:29 +0000 | [diff] [blame] | 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) |
| 43 | #endif |
| 44 | #ifndef CPU_TYPE_ARM64 |
| 45 | #define CPU_TYPE_ARM64 (CPU_TYPE_ARM|CPU_ARCH_ABI64) |
| 46 | #endif |
| 47 | |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 48 | using namespace lldb_private; |
| 49 | |
| 50 | bool |
| 51 | HostInfoMacOSX::GetOSBuildString(std::string &s) |
| 52 | { |
| 53 | int mib[2] = {CTL_KERN, KERN_OSVERSION}; |
| 54 | char cstr[PATH_MAX]; |
| 55 | size_t cstr_len = sizeof(cstr); |
| 56 | if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0) |
| 57 | { |
| 58 | s.assign(cstr, cstr_len); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | s.clear(); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | bool |
| 67 | HostInfoMacOSX::GetOSKernelDescription(std::string &s) |
| 68 | { |
| 69 | int mib[2] = {CTL_KERN, KERN_VERSION}; |
| 70 | char cstr[PATH_MAX]; |
| 71 | size_t cstr_len = sizeof(cstr); |
| 72 | if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0) |
| 73 | { |
| 74 | s.assign(cstr, cstr_len); |
| 75 | return true; |
| 76 | } |
| 77 | s.clear(); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | bool |
| 82 | HostInfoMacOSX::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update) |
| 83 | { |
| 84 | static uint32_t g_major = 0; |
| 85 | static uint32_t g_minor = 0; |
| 86 | static uint32_t g_update = 0; |
| 87 | |
| 88 | if (g_major == 0) |
| 89 | { |
| 90 | @autoreleasepool |
| 91 | { |
| 92 | NSDictionary *version_info = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; |
| 93 | NSString *version_value = [version_info objectForKey:@"ProductVersion"]; |
| 94 | const char *version_str = [version_value UTF8String]; |
| 95 | if (version_str) |
| 96 | Args::StringToVersion(version_str, g_major, g_minor, g_update); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (g_major != 0) |
| 101 | { |
| 102 | major = g_major; |
| 103 | minor = g_minor; |
| 104 | update = g_update; |
| 105 | return true; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 109 | |
| Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 110 | FileSpec |
| 111 | HostInfoMacOSX::GetProgramFileSpec() |
| 112 | { |
| 113 | static FileSpec g_program_filespec; |
| 114 | if (!g_program_filespec) |
| 115 | { |
| 116 | char program_fullpath[PATH_MAX]; |
| 117 | // If DST is NULL, then return the number of bytes needed. |
| 118 | uint32_t len = sizeof(program_fullpath); |
| 119 | int err = _NSGetExecutablePath(program_fullpath, &len); |
| 120 | if (err == 0) |
| 121 | g_program_filespec.SetFile(program_fullpath, false); |
| 122 | else if (err == -1) |
| 123 | { |
| 124 | char *large_program_fullpath = (char *)::malloc(len + 1); |
| 125 | |
| 126 | err = _NSGetExecutablePath(large_program_fullpath, &len); |
| 127 | if (err == 0) |
| 128 | g_program_filespec.SetFile(large_program_fullpath, false); |
| 129 | |
| 130 | ::free(large_program_fullpath); |
| 131 | } |
| 132 | } |
| 133 | return g_program_filespec; |
| 134 | } |
| 135 | |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 136 | bool |
| 137 | HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec) |
| 138 | { |
| 139 | FileSpec lldb_file_spec; |
| 140 | if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 141 | return false; |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 142 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 143 | std::string raw_path = lldb_file_spec.GetPath(); |
| 144 | |
| 145 | size_t framework_pos = raw_path.find("LLDB.framework"); |
| 146 | if (framework_pos != std::string::npos) |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 147 | { |
| 148 | framework_pos += strlen("LLDB.framework"); |
| 149 | #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
| 150 | // Shallow bundle |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 151 | raw_path.resize(framework_pos); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 152 | #else |
| 153 | // Normal bundle |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 154 | raw_path.resize(framework_pos); |
| 155 | raw_path.append("/Resources"); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 156 | #endif |
| 157 | } |
| Todd Fiala | 5e36548 | 2015-09-24 21:59:48 +0000 | [diff] [blame^] | 158 | else |
| 159 | { |
| 160 | // Find the bin path relative to the lib path where the cmake-based |
| 161 | // OS X .dylib lives. This is not going to work if the bin and lib |
| 162 | // dir are not both in the same dir. |
| 163 | // |
| 164 | // It is not going to work to do it by the executable path either, |
| 165 | // as in the case of a python script, the executable is python, not |
| 166 | // the lldb driver. |
| 167 | raw_path.append("/../bin"); |
| 168 | FileSpec support_dir_spec(raw_path, true); |
| 169 | if (!support_dir_spec.Exists() || !support_dir_spec.IsDirectory()) |
| 170 | { |
| 171 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); |
| 172 | if (log) |
| 173 | log->Printf("HostInfoMacOSX::%s(): failed to find support directory", |
| 174 | __FUNCTION__); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // Get normalization from support_dir_spec. Note the FileSpec resolve |
| 179 | // does not remove '..' in the path. |
| 180 | char *const dir_realpath = realpath(support_dir_spec.GetPath().c_str(), NULL); |
| 181 | if (dir_realpath) |
| 182 | { |
| 183 | raw_path = dir_realpath; |
| 184 | free(dir_realpath); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | raw_path = support_dir_spec.GetPath(); |
| 189 | } |
| 190 | } |
| 191 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 192 | file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 193 | return (bool)file_spec.GetDirectory(); |
| 194 | } |
| 195 | |
| 196 | bool |
| 197 | HostInfoMacOSX::ComputeHeaderDirectory(FileSpec &file_spec) |
| 198 | { |
| 199 | FileSpec lldb_file_spec; |
| 200 | if (!HostInfo::GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 201 | return false; |
| 202 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 203 | std::string raw_path = lldb_file_spec.GetPath(); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 204 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 205 | size_t framework_pos = raw_path.find("LLDB.framework"); |
| 206 | if (framework_pos != std::string::npos) |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 207 | { |
| 208 | framework_pos += strlen("LLDB.framework"); |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 209 | raw_path.resize(framework_pos); |
| 210 | raw_path.append("/Headers"); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 211 | } |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 212 | file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool |
| 217 | HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec) |
| 218 | { |
| Keno Fischer | 15d5e2b | 2014-09-09 04:52:37 +0000 | [diff] [blame] | 219 | #ifndef LLDB_DISABLE_PYTHON |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 220 | FileSpec lldb_file_spec; |
| 221 | if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 222 | return false; |
| 223 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 224 | std::string raw_path = lldb_file_spec.GetPath(); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 225 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 226 | size_t framework_pos = raw_path.find("LLDB.framework"); |
| 227 | if (framework_pos != std::string::npos) |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 228 | { |
| 229 | framework_pos += strlen("LLDB.framework"); |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 230 | raw_path.resize(framework_pos); |
| 231 | raw_path.append("/Resources/Python"); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 232 | } |
| 233 | else |
| 234 | { |
| 235 | llvm::SmallString<256> python_version_dir; |
| 236 | llvm::raw_svector_ostream os(python_version_dir); |
| 237 | os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages"; |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 238 | |
| 239 | // We may get our string truncated. Should we protect this with an assert? |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 240 | raw_path.append(python_version_dir.c_str()); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 241 | } |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 242 | file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); |
| Keno Fischer | a89da49 | 2014-09-09 04:56:16 +0000 | [diff] [blame] | 243 | return true; |
| Keno Fischer | 15d5e2b | 2014-09-09 04:52:37 +0000 | [diff] [blame] | 244 | #else |
| 245 | return false; |
| 246 | #endif |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | bool |
| Sean Callanan | b1e1c62 | 2014-12-05 01:15:04 +0000 | [diff] [blame] | 250 | HostInfoMacOSX::ComputeClangDirectory(FileSpec &file_spec) |
| 251 | { |
| 252 | FileSpec lldb_file_spec; |
| 253 | if (!GetLLDBPath (lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 254 | return false; |
| 255 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 256 | std::string raw_path = lldb_file_spec.GetPath(); |
| Sean Callanan | b1e1c62 | 2014-12-05 01:15:04 +0000 | [diff] [blame] | 257 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 258 | size_t framework_pos = raw_path.find("LLDB.framework"); |
| 259 | if (framework_pos != std::string::npos) |
| Sean Callanan | b1e1c62 | 2014-12-05 01:15:04 +0000 | [diff] [blame] | 260 | { |
| 261 | framework_pos += strlen("LLDB.framework"); |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 262 | raw_path.resize(framework_pos); |
| 263 | raw_path.append("/Resources/Clang"); |
| Sean Callanan | b1e1c62 | 2014-12-05 01:15:04 +0000 | [diff] [blame] | 264 | } |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 265 | file_spec.SetFile (raw_path.c_str(), true); |
| Sean Callanan | b1e1c62 | 2014-12-05 01:15:04 +0000 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | |
| 269 | bool |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 270 | HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec) |
| 271 | { |
| 272 | FileSpec lldb_file_spec; |
| 273 | if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 274 | return false; |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 275 | |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 276 | std::string raw_path = lldb_file_spec.GetPath(); |
| 277 | |
| 278 | size_t framework_pos = raw_path.find("LLDB.framework"); |
| 279 | if (framework_pos == std::string::npos) |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 280 | return false; |
| 281 | |
| 282 | framework_pos += strlen("LLDB.framework"); |
| Ilia K | 686b1fe | 2015-02-27 19:43:08 +0000 | [diff] [blame] | 283 | raw_path.resize(framework_pos); |
| 284 | raw_path.append("/Resources/PlugIns"); |
| 285 | file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size())); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | |
| 289 | bool |
| 290 | HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec) |
| 291 | { |
| Greg Clayton | 0d8400c | 2014-08-25 18:21:06 +0000 | [diff] [blame] | 292 | FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns", true); |
| 293 | file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str()); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
| Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 297 | void |
| 298 | HostInfoMacOSX::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64) |
| 299 | { |
| 300 | // All apple systems support 32 bit execution. |
| 301 | uint32_t cputype, cpusubtype; |
| 302 | uint32_t is_64_bit_capable = false; |
| 303 | size_t len = sizeof(cputype); |
| 304 | ArchSpec host_arch; |
| 305 | // These will tell us about the kernel architecture, which even on a 64 |
| 306 | // bit machine can be 32 bit... |
| 307 | if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0) |
| 308 | { |
| 309 | len = sizeof(cpusubtype); |
| 310 | if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0) |
| 311 | cpusubtype = CPU_TYPE_ANY; |
| 312 | |
| 313 | len = sizeof(is_64_bit_capable); |
| 314 | ::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0); |
| 315 | |
| 316 | if (is_64_bit_capable) |
| 317 | { |
| 318 | if (cputype & CPU_ARCH_ABI64) |
| 319 | { |
| 320 | // We have a 64 bit kernel on a 64 bit system |
| 321 | arch_64.SetArchitecture(eArchTypeMachO, cputype, cpusubtype); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | // We have a 64 bit kernel that is returning a 32 bit cputype, the |
| 326 | // cpusubtype will be correct as if it were for a 64 bit architecture |
| 327 | arch_64.SetArchitecture(eArchTypeMachO, cputype | CPU_ARCH_ABI64, cpusubtype); |
| 328 | } |
| 329 | |
| 330 | // Now we need modify the cpusubtype for the 32 bit slices. |
| 331 | uint32_t cpusubtype32 = cpusubtype; |
| 332 | #if defined(__i386__) || defined(__x86_64__) |
| 333 | if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H) |
| 334 | cpusubtype32 = CPU_SUBTYPE_I386_ALL; |
| 335 | #elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
| 336 | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
| 337 | cpusubtype32 = CPU_SUBTYPE_ARM_V7S; |
| 338 | #endif |
| 339 | arch_32.SetArchitecture(eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), cpusubtype32); |
| 340 | |
| 341 | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
| 342 | { |
| 343 | arch_32.GetTriple().setOS(llvm::Triple::IOS); |
| 344 | arch_64.GetTriple().setOS(llvm::Triple::IOS); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | arch_32.GetTriple().setOS(llvm::Triple::MacOSX); |
| 349 | arch_64.GetTriple().setOS(llvm::Triple::MacOSX); |
| 350 | } |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | // We have a 32 bit kernel on a 32 bit system |
| 355 | arch_32.SetArchitecture(eArchTypeMachO, cputype, cpusubtype); |
| 356 | arch_64.Clear(); |
| 357 | } |
| 358 | } |
| 359 | } |