blob: a6a65531454b4c0d5aa2b6e344993ddd6fec4298 [file] [log] [blame]
Zachary Turner97a14e62014-08-19 17:18:29 +00001//===-- 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 Turner42ff0ad2014-08-21 17:29:12 +000010#include "lldb/lldb-python.h"
11
12#include "lldb/Host/HostInfo.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000013#include "lldb/Host/macosx/HostInfoMacOSX.h"
14#include "lldb/Interpreter/Args.h"
Zachary Turner13b18262014-08-20 16:42:51 +000015#include "lldb/Utility/SafeMachO.h"
16
Zachary Turner42ff0ad2014-08-21 17:29:12 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/Support/raw_ostream.h"
19
Zachary Turner97a14e62014-08-19 17:18:29 +000020// 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 Turnera21fee02014-08-21 21:49:24 +000030#include <mach-o/dyld.h>
Zachary Turner97a14e62014-08-19 17:18:29 +000031#include <objc/objc-auto.h>
32
Keno Fischer5777edf2014-08-23 21:40:29 +000033// 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 Turner97a14e62014-08-19 17:18:29 +000043using namespace lldb_private;
44
45bool
46HostInfoMacOSX::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
61bool
62HostInfoMacOSX::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
76bool
77HostInfoMacOSX::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 Turner13b18262014-08-20 16:42:51 +0000104
Zachary Turnera21fee02014-08-21 21:49:24 +0000105FileSpec
106HostInfoMacOSX::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 Turner42ff0ad2014-08-21 17:29:12 +0000131bool
132HostInfoMacOSX::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 Clayton0d8400c2014-08-25 18:21:06 +0000152 file_spec.GetDirectory().SetCString(raw_path);
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000153 return (bool)file_spec.GetDirectory();
154}
155
156bool
157HostInfoMacOSX::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 Clayton0d8400c2014-08-25 18:21:06 +0000172 file_spec.GetDirectory().SetCString(raw_path);
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000173 return true;
174}
175
176bool
177HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec)
178{
Keno Fischer15d5e2b2014-09-09 04:52:37 +0000179#ifndef LLDB_DISABLE_PYTHON
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000180 FileSpec lldb_file_spec;
181 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
182 return false;
183
184 char raw_path[PATH_MAX];
185 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
186
187 char *framework_pos = ::strstr(raw_path, "LLDB.framework");
188 if (framework_pos)
189 {
190 framework_pos += strlen("LLDB.framework");
191 ::strncpy(framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
192 }
193 else
194 {
195 llvm::SmallString<256> python_version_dir;
196 llvm::raw_svector_ostream os(python_version_dir);
197 os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages";
198 os.flush();
199
200 // We may get our string truncated. Should we protect this with an assert?
201 ::strncat(raw_path, python_version_dir.c_str(), sizeof(raw_path) - strlen(raw_path) - 1);
202 }
Greg Clayton0d8400c2014-08-25 18:21:06 +0000203 file_spec.GetDirectory().SetCString(raw_path);
Keno Fischera89da492014-09-09 04:56:16 +0000204 return true;
Keno Fischer15d5e2b2014-09-09 04:52:37 +0000205#else
206 return false;
207#endif
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000208}
209
210bool
211HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec)
212{
213 FileSpec lldb_file_spec;
214 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
215 return false;
216 char raw_path[PATH_MAX];
217 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
218
219 char *framework_pos = ::strstr(raw_path, "LLDB.framework");
220 if (!framework_pos)
221 return false;
222
223 framework_pos += strlen("LLDB.framework");
224 ::strncpy(framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
Greg Clayton0d8400c2014-08-25 18:21:06 +0000225 file_spec.GetDirectory().SetCString(raw_path);
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000226 return true;
227}
228
229bool
230HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec)
231{
Greg Clayton0d8400c2014-08-25 18:21:06 +0000232 FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns", true);
233 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000234 return true;
235}
236
Zachary Turner13b18262014-08-20 16:42:51 +0000237void
238HostInfoMacOSX::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
239{
240 // All apple systems support 32 bit execution.
241 uint32_t cputype, cpusubtype;
242 uint32_t is_64_bit_capable = false;
243 size_t len = sizeof(cputype);
244 ArchSpec host_arch;
245 // These will tell us about the kernel architecture, which even on a 64
246 // bit machine can be 32 bit...
247 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
248 {
249 len = sizeof(cpusubtype);
250 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
251 cpusubtype = CPU_TYPE_ANY;
252
253 len = sizeof(is_64_bit_capable);
254 ::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0);
255
256 if (is_64_bit_capable)
257 {
258 if (cputype & CPU_ARCH_ABI64)
259 {
260 // We have a 64 bit kernel on a 64 bit system
261 arch_64.SetArchitecture(eArchTypeMachO, cputype, cpusubtype);
262 }
263 else
264 {
265 // We have a 64 bit kernel that is returning a 32 bit cputype, the
266 // cpusubtype will be correct as if it were for a 64 bit architecture
267 arch_64.SetArchitecture(eArchTypeMachO, cputype | CPU_ARCH_ABI64, cpusubtype);
268 }
269
270 // Now we need modify the cpusubtype for the 32 bit slices.
271 uint32_t cpusubtype32 = cpusubtype;
272#if defined(__i386__) || defined(__x86_64__)
273 if (cpusubtype == CPU_SUBTYPE_486 || cpusubtype == CPU_SUBTYPE_X86_64_H)
274 cpusubtype32 = CPU_SUBTYPE_I386_ALL;
275#elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
276 if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64)
277 cpusubtype32 = CPU_SUBTYPE_ARM_V7S;
278#endif
279 arch_32.SetArchitecture(eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), cpusubtype32);
280
281 if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64)
282 {
283 arch_32.GetTriple().setOS(llvm::Triple::IOS);
284 arch_64.GetTriple().setOS(llvm::Triple::IOS);
285 }
286 else
287 {
288 arch_32.GetTriple().setOS(llvm::Triple::MacOSX);
289 arch_64.GetTriple().setOS(llvm::Triple::MacOSX);
290 }
291 }
292 else
293 {
294 // We have a 32 bit kernel on a 32 bit system
295 arch_32.SetArchitecture(eArchTypeMachO, cputype, cpusubtype);
296 arch_64.Clear();
297 }
298 }
299}