]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Host/macosx/HostInfoMacOSX.mm
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / source / Host / macosx / HostInfoMacOSX.mm
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
10 #if !defined(LLDB_DISABLE_PYTHON)
11 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
12 #endif
13
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"
19
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 // C++ Includes
24 #include <string>
25
26 // C inclues
27 #include <stdlib.h>
28 #include <sys/sysctl.h>
29 #include <sys/syslimits.h>
30 #include <sys/types.h>
31
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>
37
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
48 #include <TargetConditionals.h> // for TARGET_OS_TV, TARGET_OS_WATCH
49
50 using namespace lldb_private;
51
52 bool
53 HostInfoMacOSX::GetOSBuildString(std::string &s)
54 {
55     int mib[2] = {CTL_KERN, KERN_OSVERSION};
56     char cstr[PATH_MAX];
57     size_t cstr_len = sizeof(cstr);
58     if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0)
59     {
60         s.assign(cstr, cstr_len);
61         return true;
62     }
63
64     s.clear();
65     return false;
66 }
67
68 bool
69 HostInfoMacOSX::GetOSKernelDescription(std::string &s)
70 {
71     int mib[2] = {CTL_KERN, KERN_VERSION};
72     char cstr[PATH_MAX];
73     size_t cstr_len = sizeof(cstr);
74     if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0)
75     {
76         s.assign(cstr, cstr_len);
77         return true;
78     }
79     s.clear();
80     return false;
81 }
82
83 bool
84 HostInfoMacOSX::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
85 {
86     static uint32_t g_major = 0;
87     static uint32_t g_minor = 0;
88     static uint32_t g_update = 0;
89
90     if (g_major == 0)
91     {
92         @autoreleasepool
93         {
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];
97             if (version_str)
98                 Args::StringToVersion(version_str, g_major, g_minor, g_update);
99         }
100     }
101
102     if (g_major != 0)
103     {
104         major = g_major;
105         minor = g_minor;
106         update = g_update;
107         return true;
108     }
109     return false;
110 }
111
112 FileSpec
113 HostInfoMacOSX::GetProgramFileSpec()
114 {
115     static FileSpec g_program_filespec;
116     if (!g_program_filespec)
117     {
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);
122         if (err == 0)
123             g_program_filespec.SetFile(program_fullpath, false);
124         else if (err == -1)
125         {
126             char *large_program_fullpath = (char *)::malloc(len + 1);
127
128             err = _NSGetExecutablePath(large_program_fullpath, &len);
129             if (err == 0)
130                 g_program_filespec.SetFile(large_program_fullpath, false);
131
132             ::free(large_program_fullpath);
133         }
134     }
135     return g_program_filespec;
136 }
137
138 bool
139 HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec)
140 {
141     FileSpec lldb_file_spec;
142     if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
143         return false;
144
145     std::string raw_path = lldb_file_spec.GetPath();
146
147     size_t framework_pos = raw_path.find("LLDB.framework");
148     if (framework_pos != std::string::npos)
149     {
150         framework_pos += strlen("LLDB.framework");
151 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
152         // Shallow bundle
153         raw_path.resize(framework_pos);
154 #else
155         // Normal bundle
156         raw_path.resize(framework_pos);
157         raw_path.append("/Resources");
158 #endif
159     }
160     else
161     {
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.
165         //
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
168         // the lldb driver.
169         raw_path.append("/../bin");
170         FileSpec support_dir_spec(raw_path, true);
171         if (!support_dir_spec.Exists() || !support_dir_spec.IsDirectory())
172         {
173             Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
174             if (log)
175                 log->Printf("HostInfoMacOSX::%s(): failed to find support directory",
176                             __FUNCTION__);
177             return false;
178         }
179
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);
183         if (dir_realpath)
184         {
185             raw_path = dir_realpath;
186             free(dir_realpath);
187         }
188         else
189         {
190             raw_path = support_dir_spec.GetPath();
191         }
192     }
193
194     file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size()));
195     return (bool)file_spec.GetDirectory();
196 }
197
198 bool
199 HostInfoMacOSX::ComputeHeaderDirectory(FileSpec &file_spec)
200 {
201     FileSpec lldb_file_spec;
202     if (!HostInfo::GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
203         return false;
204
205     std::string raw_path = lldb_file_spec.GetPath();
206
207     size_t framework_pos = raw_path.find("LLDB.framework");
208     if (framework_pos != std::string::npos)
209     {
210         framework_pos += strlen("LLDB.framework");
211         raw_path.resize(framework_pos);
212         raw_path.append("/Headers");
213     }
214     file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size()));
215     return true;
216 }
217
218 bool
219 HostInfoMacOSX::ComputePythonDirectory(FileSpec &file_spec)
220 {
221 #ifndef LLDB_DISABLE_PYTHON
222     FileSpec lldb_file_spec;
223     if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
224         return false;
225
226     std::string raw_path = lldb_file_spec.GetPath();
227
228     size_t framework_pos = raw_path.find("LLDB.framework");
229     if (framework_pos != std::string::npos)
230     {
231         framework_pos += strlen("LLDB.framework");
232         raw_path.resize(framework_pos);
233         raw_path.append("/Resources/Python");
234     }
235     else
236     {
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";
240
241         // We may get our string truncated. Should we protect this with an assert?
242         raw_path.append(python_version_dir.c_str());
243     }
244     file_spec.GetDirectory().SetString(llvm::StringRef(raw_path.c_str(), raw_path.size()));
245     return true;
246 #else
247     return false;
248 #endif
249 }
250
251 bool
252 HostInfoMacOSX::ComputeClangDirectory(FileSpec &file_spec)
253 {
254     FileSpec lldb_file_spec;
255     if (!GetLLDBPath (lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
256         return false;
257     
258     std::string raw_path = lldb_file_spec.GetPath();
259     
260     size_t framework_pos = raw_path.find("LLDB.framework");
261     if (framework_pos != std::string::npos)
262     {
263         framework_pos += strlen("LLDB.framework");
264         raw_path.resize(framework_pos);
265         raw_path.append("/Resources/Clang");
266     }
267     file_spec.SetFile (raw_path.c_str(), true);
268     return true;
269 }
270
271 bool
272 HostInfoMacOSX::ComputeSystemPluginsDirectory(FileSpec &file_spec)
273 {
274     FileSpec lldb_file_spec;
275     if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
276         return false;
277
278     std::string raw_path = lldb_file_spec.GetPath();
279
280     size_t framework_pos = raw_path.find("LLDB.framework");
281     if (framework_pos == std::string::npos)
282         return false;
283
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()));
288     return true;
289 }
290
291 bool
292 HostInfoMacOSX::ComputeUserPluginsDirectory(FileSpec &file_spec)
293 {
294     FileSpec temp_file("~/Library/Application Support/LLDB/PlugIns", true);
295     file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
296     return true;
297 }
298
299 void
300 HostInfoMacOSX::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
301 {
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);
306     ArchSpec host_arch;
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)
310     {
311         len = sizeof(cpusubtype);
312         if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
313             cpusubtype = CPU_TYPE_ANY;
314
315         len = sizeof(is_64_bit_capable);
316         ::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0);
317
318         if (is_64_bit_capable)
319         {
320             if (cputype & CPU_ARCH_ABI64)
321             {
322                 // We have a 64 bit kernel on a 64 bit system
323                 arch_64.SetArchitecture(eArchTypeMachO, cputype, cpusubtype);
324             }
325             else
326             {
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);
330             }
331
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;
340 #endif
341             arch_32.SetArchitecture(eArchTypeMachO, cputype & ~(CPU_ARCH_MASK), cpusubtype32);
342
343             if (cputype == CPU_TYPE_ARM || cputype == CPU_TYPE_ARM64)
344             {
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);
349 #else
350                 arch_32.GetTriple().setOS(llvm::Triple::IOS);
351                 arch_64.GetTriple().setOS(llvm::Triple::IOS);
352 #endif
353             }
354             else
355             {
356                 arch_32.GetTriple().setOS(llvm::Triple::MacOSX);
357                 arch_64.GetTriple().setOS(llvm::Triple::MacOSX);
358             }
359         }
360         else
361         {
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);
366 #else
367             arch_32.GetTriple().setOS(llvm::Triple::IOS);
368 #endif
369             arch_64.Clear();
370         }
371     }
372 }