1 //===-- PlatformMacOSX.cpp --------------------------------------*- 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 #include "PlatformMacOSX.h"
11 #include "lldb/Host/Config.h"
12 
13 // C++ Includes
14 
15 #include <sstream>
16 
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/Error.h"
21 #include "lldb/Core/DataBufferHeap.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleList.h"
24 #include "lldb/Core/ModuleSpec.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Host/FileSpec.h"
28 #include "lldb/Host/FileSystem.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Host/HostInfo.h"
31 #include "lldb/Symbol/ObjectFile.h"
32 #include "lldb/Target/Process.h"
33 #include "lldb/Target/Target.h"
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
38 static uint32_t g_initialize_count = 0;
39 
40 void
41 PlatformMacOSX::Initialize ()
42 {
43     PlatformDarwin::Initialize ();
44 
45     if (g_initialize_count++ == 0)
46     {
47 #if defined (__APPLE__)
48         PlatformSP default_platform_sp (new PlatformMacOSX(true));
49         default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
50         Platform::SetHostPlatform (default_platform_sp);
51 #endif
52         PluginManager::RegisterPlugin (PlatformMacOSX::GetPluginNameStatic(false),
53                                        PlatformMacOSX::GetDescriptionStatic(false),
54                                        PlatformMacOSX::CreateInstance);
55     }
56 
57 }
58 
59 void
60 PlatformMacOSX::Terminate ()
61 {
62     if (g_initialize_count > 0)
63     {
64         if (--g_initialize_count == 0)
65         {
66             PluginManager::UnregisterPlugin (PlatformMacOSX::CreateInstance);
67         }
68     }
69 
70     PlatformDarwin::Terminate ();
71 }
72 
73 PlatformSP
74 PlatformMacOSX::CreateInstance (bool force, const ArchSpec *arch)
75 {
76     // The only time we create an instance is when we are creating a remote
77     // macosx platform
78     const bool is_host = false;
79 
80     bool create = force;
81     if (create == false && arch && arch->IsValid())
82     {
83         const llvm::Triple &triple = arch->GetTriple();
84         switch (triple.getVendor())
85         {
86             case llvm::Triple::Apple:
87                 create = true;
88                 break;
89 
90 #if defined(__APPLE__)
91             // Only accept "unknown" for vendor if the host is Apple and
92             // it "unknown" wasn't specified (it was just returned because it
93             // was NOT specified)
94             case llvm::Triple::UnknownArch:
95                 create = !arch->TripleVendorWasSpecified();
96                 break;
97 #endif
98             default:
99                 break;
100         }
101 
102         if (create)
103         {
104             switch (triple.getOS())
105             {
106                 case llvm::Triple::Darwin:  // Deprecated, but still support Darwin for historical reasons
107                 case llvm::Triple::MacOSX:
108                     break;
109 #if defined(__APPLE__)
110                 // Only accept "vendor" for vendor if the host is Apple and
111                 // it "unknown" wasn't specified (it was just returned because it
112                 // was NOT specified)
113                 case llvm::Triple::UnknownOS:
114                     create = !arch->TripleOSWasSpecified();
115                     break;
116 #endif
117                 default:
118                     create = false;
119                     break;
120             }
121         }
122     }
123     if (create)
124         return PlatformSP(new PlatformMacOSX (is_host));
125     return PlatformSP();
126 }
127 
128 lldb_private::ConstString
129 PlatformMacOSX::GetPluginNameStatic (bool is_host)
130 {
131     if (is_host)
132     {
133         static ConstString g_host_name(Platform::GetHostPlatformName ());
134         return g_host_name;
135     }
136     else
137     {
138         static ConstString g_remote_name("remote-macosx");
139         return g_remote_name;
140     }
141 }
142 
143 const char *
144 PlatformMacOSX::GetDescriptionStatic (bool is_host)
145 {
146     if (is_host)
147         return "Local Mac OS X user platform plug-in.";
148     else
149         return "Remote Mac OS X user platform plug-in.";
150 }
151 
152 //------------------------------------------------------------------
153 /// Default Constructor
154 //------------------------------------------------------------------
155 PlatformMacOSX::PlatformMacOSX (bool is_host) :
156     PlatformDarwin (is_host)
157 {
158 }
159 
160 //------------------------------------------------------------------
161 /// Destructor.
162 ///
163 /// The destructor is virtual since this class is designed to be
164 /// inherited from by the plug-in instance.
165 //------------------------------------------------------------------
166 PlatformMacOSX::~PlatformMacOSX()
167 {
168 }
169 
170 ConstString
171 PlatformMacOSX::GetSDKDirectory (lldb_private::Target &target)
172 {
173     ModuleSP exe_module_sp (target.GetExecutableModule());
174     if (exe_module_sp)
175     {
176         ObjectFile *objfile = exe_module_sp->GetObjectFile();
177         if (objfile)
178         {
179             std::string xcode_contents_path;
180             std::string default_xcode_sdk;
181             FileSpec fspec;
182             uint32_t versions[2];
183             if (objfile->GetSDKVersion(versions, sizeof(versions)))
184             {
185                 if (HostInfo::GetLLDBPath(ePathTypeLLDBShlibDir, fspec))
186                 {
187                     std::string path;
188                     xcode_contents_path = fspec.GetPath();
189                     size_t pos = xcode_contents_path.find("/Xcode.app/Contents/");
190                     if (pos != std::string::npos)
191                     {
192                         // LLDB.framework is inside an Xcode app bundle, we can locate the SDK from here
193                         xcode_contents_path.erase(pos + strlen("/Xcode.app/Contents/"));
194                     }
195                     else
196                     {
197                         xcode_contents_path.clear();
198                         // Use the selected Xcode
199                         int status = 0;
200                         int signo = 0;
201                         std::string output;
202                         const char *command = "xcrun -sdk macosx --show-sdk-path";
203                         lldb_private::Error error = RunShellCommand (command,   // shell command to run
204                                                                      NULL,      // current working directory
205                                                                      &status,   // Put the exit status of the process in here
206                                                                      &signo,    // Put the signal that caused the process to exit in here
207                                                                      &output,   // Get the output from the command and place it in this string
208                                                                      3);        // Timeout in seconds to wait for shell program to finish
209                         if (status == 0 && !output.empty())
210                         {
211                             size_t first_non_newline = output.find_last_not_of("\r\n");
212                             if (first_non_newline != std::string::npos)
213                                 output.erase(first_non_newline+1);
214                             default_xcode_sdk = output;
215 
216                             pos = default_xcode_sdk.find("/Xcode.app/Contents/");
217                             if (pos != std::string::npos)
218                                 xcode_contents_path = default_xcode_sdk.substr(0, pos + strlen("/Xcode.app/Contents/"));
219                         }
220                     }
221                 }
222 
223                 if (!xcode_contents_path.empty())
224                 {
225                     StreamString sdk_path;
226                     sdk_path.Printf("%sDeveloper/Platforms/MacOSX.platform/Developer/SDKs/MacOSX%u.%u.sdk", xcode_contents_path.c_str(), versions[0], versions[1]);
227                     fspec.SetFile(sdk_path.GetString().c_str(), false);
228                     if (fspec.Exists())
229                         return ConstString(sdk_path.GetString().c_str());
230                 }
231 
232                 if (!default_xcode_sdk.empty())
233                 {
234                     fspec.SetFile(default_xcode_sdk.c_str(), false);
235                     if (fspec.Exists())
236                         return ConstString(default_xcode_sdk.c_str());
237                 }
238             }
239         }
240     }
241     return ConstString();
242 }
243 
244 
245 Error
246 PlatformMacOSX::GetSymbolFile (const FileSpec &platform_file,
247                                const UUID *uuid_ptr,
248                                FileSpec &local_file)
249 {
250     if (IsRemote())
251     {
252         if (m_remote_platform_sp)
253             return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file);
254     }
255 
256     // Default to the local case
257     local_file = platform_file;
258     return Error();
259 }
260 
261 lldb_private::Error
262 PlatformMacOSX::GetFileWithUUID (const lldb_private::FileSpec &platform_file,
263                                  const lldb_private::UUID *uuid_ptr,
264                                  lldb_private::FileSpec &local_file)
265 {
266     if (IsRemote() && m_remote_platform_sp)
267     {
268         std::string local_os_build;
269 #if !defined(__linux__)
270         HostInfo::GetOSBuildString(local_os_build);
271 #endif
272         std::string remote_os_build;
273         m_remote_platform_sp->GetOSBuildString(remote_os_build);
274         if (local_os_build.compare(remote_os_build) == 0)
275         {
276             // same OS version: the local file is good enough
277             local_file = platform_file;
278             return Error();
279         }
280         else
281         {
282             // try to find the file in the cache
283             std::string cache_path(GetLocalCacheDirectory());
284             std::string module_path (platform_file.GetPath());
285             cache_path.append(module_path);
286             FileSpec module_cache_spec(cache_path.c_str(),false);
287             if (module_cache_spec.Exists())
288             {
289                 local_file = module_cache_spec;
290                 return Error();
291             }
292             // bring in the remote module file
293             FileSpec module_cache_folder = module_cache_spec.CopyByRemovingLastPathComponent();
294             // try to make the local directory first
295             Error err =
296                 FileSystem::MakeDirectory(module_cache_folder, eFilePermissionsDirectoryDefault);
297             if (err.Fail())
298                 return err;
299             err = GetFile(platform_file, module_cache_spec);
300             if (err.Fail())
301                 return err;
302             if (module_cache_spec.Exists())
303             {
304                 local_file = module_cache_spec;
305                 return Error();
306             }
307             else
308                 return Error("unable to obtain valid module file");
309         }
310     }
311     local_file = platform_file;
312     return Error();
313 }
314 
315 bool
316 PlatformMacOSX::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
317 {
318 #if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
319     return ARMGetSupportedArchitectureAtIndex (idx, arch);
320 #else
321     return x86GetSupportedArchitectureAtIndex (idx, arch);
322 #endif
323 }
324 
325 lldb_private::Error
326 PlatformMacOSX::GetSharedModule (const lldb_private::ModuleSpec &module_spec,
327                                  Process* process,
328                                  lldb::ModuleSP &module_sp,
329                                  const lldb_private::FileSpecList *module_search_paths_ptr,
330                                  lldb::ModuleSP *old_module_sp_ptr,
331                                  bool *did_create_ptr)
332 {
333     Error error = GetSharedModuleWithLocalCache(module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
334 
335     if (module_sp)
336     {
337         if (module_spec.GetArchitecture().GetCore() == ArchSpec::eCore_x86_64_x86_64h)
338         {
339             ObjectFile *objfile = module_sp->GetObjectFile();
340             if (objfile == NULL)
341             {
342                 // We didn't find an x86_64h slice, fall back to a x86_64 slice
343                 ModuleSpec module_spec_x86_64 (module_spec);
344                 module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
345                 lldb::ModuleSP x86_64_module_sp;
346                 lldb::ModuleSP old_x86_64_module_sp;
347                 bool did_create = false;
348                 Error x86_64_error = GetSharedModuleWithLocalCache(module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr, &old_x86_64_module_sp, &did_create);
349                 if (x86_64_module_sp && x86_64_module_sp->GetObjectFile())
350                 {
351                     module_sp = x86_64_module_sp;
352                     if (old_module_sp_ptr)
353                         *old_module_sp_ptr = old_x86_64_module_sp;
354                     if (did_create_ptr)
355                         *did_create_ptr = did_create;
356                     return x86_64_error;
357                 }
358             }
359         }
360     }
361     return error;
362 }
363 
364