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 
12 // C Includes
13 #include <sys/sysctl.h>
14 
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Core/Error.h"
19 #include "lldb/Breakpoint/BreakpointLocation.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/ModuleList.h"
22 #include "lldb/Core/PluginManager.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Host/FileSpec.h"
25 #include "lldb/Host/Host.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Target.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 static uint32_t g_initialize_count = 0;
33 
34 void
35 PlatformMacOSX::Initialize ()
36 {
37     if (g_initialize_count++ == 0)
38     {
39 #if defined (__APPLE__)
40         PlatformSP default_platform_sp (new PlatformMacOSX(true));
41         default_platform_sp->SetSystemArchitecture (Host::GetArchitecture());
42         Platform::SetDefaultPlatform (default_platform_sp);
43 #endif
44         PluginManager::RegisterPlugin (PlatformMacOSX::GetShortPluginNameStatic(false),
45                                        PlatformMacOSX::GetDescriptionStatic(false),
46                                        PlatformMacOSX::CreateInstance);
47     }
48 
49 }
50 
51 void
52 PlatformMacOSX::Terminate ()
53 {
54     if (g_initialize_count > 0)
55     {
56         if (--g_initialize_count == 0)
57         {
58             PluginManager::UnregisterPlugin (PlatformMacOSX::CreateInstance);
59         }
60     }
61 }
62 
63 Platform*
64 PlatformMacOSX::CreateInstance ()
65 {
66     // The only time we create an instance is when we are creating a remote
67     // macosx platform
68     const bool is_host = false;
69     return new PlatformMacOSX (is_host);
70 }
71 
72 
73 const char *
74 PlatformMacOSX::GetPluginNameStatic ()
75 {
76     return "PlatformMacOSX";
77 }
78 
79 const char *
80 PlatformMacOSX::GetShortPluginNameStatic (bool is_host)
81 {
82     if (is_host)
83         return Platform::GetHostPlatformName ();
84     else
85         return "remote-macosx";
86 }
87 
88 const char *
89 PlatformMacOSX::GetDescriptionStatic (bool is_host)
90 {
91     if (is_host)
92         return "Local Mac OS X user platform plug-in.";
93     else
94         return "Remote Mac OS X user platform plug-in.";
95 }
96 
97 //------------------------------------------------------------------
98 /// Default Constructor
99 //------------------------------------------------------------------
100 PlatformMacOSX::PlatformMacOSX (bool is_host) :
101     PlatformDarwin (is_host)
102 {
103 }
104 
105 //------------------------------------------------------------------
106 /// Destructor.
107 ///
108 /// The destructor is virtual since this class is designed to be
109 /// inherited from by the plug-in instance.
110 //------------------------------------------------------------------
111 PlatformMacOSX::~PlatformMacOSX()
112 {
113 }
114 
115 Error
116 PlatformMacOSX::GetFile (const FileSpec &platform_file,
117                          const UUID *uuid_ptr,
118                          FileSpec &local_file)
119 {
120     if (IsRemote())
121     {
122         if (m_remote_platform_sp)
123             return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file);
124     }
125 
126     // Default to the local case
127     local_file = platform_file;
128     return Error();
129 }
130 
131 Error
132 PlatformMacOSX::GetSharedModule (const FileSpec &platform_file,
133                                  const ArchSpec &arch,
134                                  const UUID *uuid_ptr,
135                                  const ConstString *object_name_ptr,
136                                  off_t object_offset,
137                                  ModuleSP &module_sp,
138                                  ModuleSP *old_module_sp_ptr,
139                                  bool *did_create_ptr)
140 {
141     Error error;
142     module_sp.reset();
143 
144     if (IsRemote())
145     {
146         // If we have a remote platform always, let it try and locate
147         // the shared module first.
148         if (m_remote_platform_sp)
149         {
150             error = m_remote_platform_sp->GetSharedModule (platform_file,
151                                                            arch,
152                                                            uuid_ptr,
153                                                            object_name_ptr,
154                                                            object_offset,
155                                                            module_sp,
156                                                            old_module_sp_ptr,
157                                                            did_create_ptr);
158         }
159     }
160 
161     if (!module_sp)
162     {
163         // Fall back to the local platform and find the file locally
164         error = Platform::GetSharedModule(platform_file,
165                                           arch,
166                                           uuid_ptr,
167                                           object_name_ptr,
168                                           object_offset,
169                                           module_sp,
170                                           old_module_sp_ptr,
171                                           did_create_ptr);
172     }
173     if (module_sp)
174         module_sp->SetPlatformFileSpec(platform_file);
175     return error;
176 }
177 
178 bool
179 PlatformMacOSX::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
180 {
181     if (idx == 0)
182     {
183         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
184         return arch.IsValid();
185     }
186     else if (idx == 1)
187     {
188         ArchSpec platform_arch (Host::GetArchitecture (Host::eSystemDefaultArchitecture));
189         ArchSpec platform_arch64 (Host::GetArchitecture (Host::eSystemDefaultArchitecture64));
190         if (platform_arch == platform_arch64)
191         {
192             // This macosx platform supports both 32 and 64 bit. Since we already
193             // returned the 64 bit arch for idx == 0, return the 32 bit arch
194             // for idx == 1
195             arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
196             return arch.IsValid();
197         }
198     }
199     return false;
200 }
201 
202 
203