| 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 | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
| 12 | #include "lldb/Host/HostInfo.h" |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 13 | #include "lldb/Host/macosx/HostInfoMacOSX.h" |
| 14 | #include "lldb/Interpreter/Args.h" |
| Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/SafeMachO.h" |
| 16 | |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 20 | // C++ Includes |
| 21 | #include <string> |
| 22 | |
| 23 | // C inclues |
| 24 | #include <sys/sysctl.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | // Objective C/C++ includes |
| 28 | #include <CoreFoundation/CoreFoundation.h> |
| 29 | #include <Foundation/Foundation.h> |
| Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 30 | #include <mach-o/dyld.h> |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 31 | #include <objc/objc-auto.h> |
| 32 | |
| Keno Fischer | 5777edf | 2014-08-23 21:40:29 +0000 | [diff] [blame] | 33 | // These are needed when compiling on systems |
| 34 | // that do not yet have these definitions |
| 35 | #include <AvailabilityMacros.h> |
| 36 | #ifndef CPU_SUBTYPE_X86_64_H |
| 37 | #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t)8) |
| 38 | #endif |
| 39 | #ifndef CPU_TYPE_ARM64 |
| 40 | #define CPU_TYPE_ARM64 (CPU_TYPE_ARM|CPU_ARCH_ABI64) |
| 41 | #endif |
| 42 | |
| Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 43 | using namespace lldb_private; |
| 44 | |
| 45 | bool |
| 46 | HostInfoMacOSX::GetOSBuildString(std::string &s) |
| 47 | { |
| 48 | int mib[2] = {CTL_KERN, KERN_OSVERSION}; |
| 49 | char cstr[PATH_MAX]; |
| 50 | size_t cstr_len = sizeof(cstr); |
| 51 | if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0) |
| 52 | { |
| 53 | s.assign(cstr, cstr_len); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | s.clear(); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | bool |
| 62 | HostInfoMacOSX::GetOSKernelDescription(std::string &s) |
| 63 | { |
| 64 | int mib[2] = {CTL_KERN, KERN_VERSION}; |
| 65 | char cstr[PATH_MAX]; |
| 66 | size_t cstr_len = sizeof(cstr); |
| 67 | if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0) |
| 68 | { |
| 69 | s.assign(cstr, cstr_len); |
| 70 | return true; |
| 71 | } |
| 72 | s.clear(); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bool |
| 77 | HostInfoMacOSX::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update) |
| 78 | { |
| 79 | static uint32_t g_major = 0; |
| 80 | static uint32_t g_minor = 0; |
| 81 | static uint32_t g_update = 0; |
| 82 | |
| 83 | if (g_major == 0) |
| 84 | { |
| 85 | @autoreleasepool |
| 86 | { |
| 87 | NSDictionary *version_info = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; |
| 88 | NSString *version_value = [version_info objectForKey:@"ProductVersion"]; |
| 89 | const char *version_str = [version_value UTF8String]; |
| 90 | if (version_str) |
| 91 | Args::StringToVersion(version_str, g_major, g_minor, g_update); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (g_major != 0) |
| 96 | { |
| 97 | major = g_major; |
| 98 | minor = g_minor; |
| 99 | update = g_update; |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 104 | |
| Zachary Turner | a21fee0 | 2014-08-21 21:49:24 +0000 | [diff] [blame] | 105 | FileSpec |
| 106 | HostInfoMacOSX::GetProgramFileSpec() |
| 107 | { |
| 108 | static FileSpec g_program_filespec; |
| 109 | if (!g_program_filespec) |
| 110 | { |
| 111 | char program_fullpath[PATH_MAX]; |
| 112 | // If DST is NULL, then return the number of bytes needed. |
| 113 | uint32_t len = sizeof(program_fullpath); |
| 114 | int err = _NSGetExecutablePath(program_fullpath, &len); |
| 115 | if (err == 0) |
| 116 | g_program_filespec.SetFile(program_fullpath, false); |
| 117 | else if (err == -1) |
| 118 | { |
| 119 | char *large_program_fullpath = (char *)::malloc(len + 1); |
| 120 | |
| 121 | err = _NSGetExecutablePath(large_program_fullpath, &len); |
| 122 | if (err == 0) |
| 123 | g_program_filespec.SetFile(large_program_fullpath, false); |
| 124 | |
| 125 | ::free(large_program_fullpath); |
| 126 | } |
| 127 | } |
| 128 | return g_program_filespec; |
| 129 | } |
| 130 | |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 131 | bool |
| 132 | HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec) |
| 133 | { |
| 134 | FileSpec lldb_file_spec; |
| 135 | if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 136 | return false; |
| 137 | char raw_path[PATH_MAX]; |
| 138 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 139 | |
| 140 | char *framework_pos = ::strstr(raw_path, "LLDB.framework"); |
| 141 | if (framework_pos) |
| 142 | { |
| 143 | framework_pos += strlen("LLDB.framework"); |
| 144 | #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
| 145 | // Shallow bundle |
| 146 | *framework_pos = '\0'; |
| 147 | #else |
| 148 | // Normal bundle |
| 149 | ::strncpy(framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path)); |
| 150 | #endif |
| 151 | } |
| Greg Clayton | 0d8400c | 2014-08-25 18:21:06 +0000 | [diff] [blame^] | 152 | file_spec.GetDirectory().SetCString(raw_path); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 153 | return (bool)file_spec.GetDirectory(); |
| 154 | } |
| 155 | |
| 156 | bool |
| 157 | HostInfoMacOSX::ComputeHeaderDirectory(FileSpec &file_spec) |
| 158 | { |
| 159 | FileSpec lldb_file_spec; |
| 160 | if (!HostInfo::GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 161 | return false; |
| 162 | |
| 163 | char raw_path[PATH_MAX]; |
| 164 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 165 | |
| 166 | char *framework_pos = ::strstr(raw_path, "LLDB.framework"); |
| 167 | if (framework_pos) |
| 168 | { |
| 169 | framework_pos += strlen("LLDB.framework"); |
| 170 | ::strncpy(framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path)); |
| 171 | } |
| Greg Clayton | 0d8400c | 2014-08-25 18:21:06 +0000 | [diff] [blame^] | 172 | file_spec.GetDirectory().SetCString(raw_path); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
| 176 | bool |
| 177 | HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec) |
| 178 | { |
| 179 | FileSpec lldb_file_spec; |
| 180 | if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 181 | return false; |
| 182 | |
| 183 | char raw_path[PATH_MAX]; |
| 184 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 185 | |
| 186 | char *framework_pos = ::strstr(raw_path, "LLDB.framework"); |
| 187 | if (framework_pos) |
| 188 | { |
| 189 | framework_pos += strlen("LLDB.framework"); |
| 190 | ::strncpy(framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path)); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | llvm::SmallString<256> python_version_dir; |
| 195 | llvm::raw_svector_ostream os(python_version_dir); |
| 196 | os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages"; |
| 197 | os.flush(); |
| 198 | |
| 199 | // We may get our string truncated. Should we protect this with an assert? |
| 200 | ::strncat(raw_path, python_version_dir.c_str(), sizeof(raw_path) - strlen(raw_path) - 1); |
| 201 | } |
| Greg Clayton | 0d8400c | 2014-08-25 18:21:06 +0000 | [diff] [blame^] | 202 | file_spec.GetDirectory().SetCString(raw_path); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | |
| 206 | bool |
| 207 | HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec) |
| 208 | { |
| 209 | FileSpec lldb_file_spec; |
| 210 | if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 211 | return false; |
| 212 | char raw_path[PATH_MAX]; |
| 213 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 214 | |
| 215 | char *framework_pos = ::strstr(raw_path, "LLDB.framework"); |
| 216 | if (!framework_pos) |
| 217 | return false; |
| 218 | |
| 219 | framework_pos += strlen("LLDB.framework"); |
| 220 | ::strncpy(framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path)); |
| Greg Clayton | 0d8400c | 2014-08-25 18:21:06 +0000 | [diff] [blame^] | 221 | file_spec.GetDirectory().SetCString(raw_path); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
| 225 | bool |
| 226 | HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec) |
| 227 | { |
| Greg Clayton | 0d8400c | 2014-08-25 18:21:06 +0000 | [diff] [blame^] | 228 | FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns", true); |
| 229 | file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str()); |
| Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
| Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 233 | void |
| 234 | HostInfoMacOSX::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64) |
| 235 | { |
| 236 | // All apple systems support 32 bit execution. |
| 237 | uint32_t cputype, cpusubtype; |
| 238 | uint32_t is_64_bit_capable = false; |
| 239 | size_t len = sizeof(cputype); |
| 240 | ArchSpec host_arch; |
| 241 | // These will tell us about the kernel architecture, which even on a 64 |
| 242 | // bit machine can be 32 bit... |
| 243 | if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0) |
| 244 | { |
| 245 | len = sizeof(cpusubtype); |
| 246 | if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0) |
| 247 | cpusubtype = CPU_TYPE_ANY; |
| 248 | |
| 249 | len = sizeof(is_64_bit_capable); |
| 250 | ::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0); |
| 251 | |
| 252 | if (is_64_bit_capable) |
| 253 | { |
| 254 | if (cputype & CPU_ARCH_ABI64) |
| 255 | { |
| 256 | // We have a 64 bit kernel on a 64 bit system |
| 257 | arch_64.SetArchitecture(eArchTypeMachO, cputype, cpusubtype); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | // We have a 64 bit kernel that is returning a 32 bit cputype, the |
| 262 | // cpusubtype will be correct as if it were for a 64 bit architecture |
| 263 | arch_64.SetArchitecture(eArchTypeMachO, cputype | CPU_ARCH_ABI64, cpusubtype); |
| 264 | } |
| 265 | |
| 266 | // Now we need modify the cpusubtype for the 32 bit slices. |
| 267 | uint32_t cpusubtype32 = cpusubtype; |
| 268 | #if defined(__i386__) || defined(__x86_64__) |
| 269 | if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H) |
| 270 | cpusubtype32 = CPU_SUBTYPE_I386_ALL; |
| 271 | #elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__) |
| 272 | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
| 273 | cpusubtype32 = CPU_SUBTYPE_ARM_V7S; |
| 274 | #endif |
| 275 | arch_32.SetArchitecture(eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), cpusubtype32); |
| 276 | |
| 277 | if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64) |
| 278 | { |
| 279 | arch_32.GetTriple().setOS(llvm::Triple::IOS); |
| 280 | arch_64.GetTriple().setOS(llvm::Triple::IOS); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | arch_32.GetTriple().setOS(llvm::Triple::MacOSX); |
| 285 | arch_64.GetTriple().setOS(llvm::Triple::MacOSX); |
| 286 | } |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | // We have a 32 bit kernel on a 32 bit system |
| 291 | arch_32.SetArchitecture(eArchTypeMachO, cputype, cpusubtype); |
| 292 | arch_64.Clear(); |
| 293 | } |
| 294 | } |
| 295 | } |