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 bool
132 PlatformMacOSX::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
133 {
134 #if defined (__arm__)
135     return ARMGetSupportedArchitectureAtIndex (idx, arch);
136 #endif
137 
138     if (idx == 0)
139     {
140         arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
141         return arch.IsValid();
142     }
143     else if (idx == 1)
144     {
145         ArchSpec platform_arch (Host::GetArchitecture (Host::eSystemDefaultArchitecture));
146         ArchSpec platform_arch64 (Host::GetArchitecture (Host::eSystemDefaultArchitecture64));
147         if (platform_arch == platform_arch64)
148         {
149             // This macosx platform supports both 32 and 64 bit. Since we already
150             // returned the 64 bit arch for idx == 0, return the 32 bit arch
151             // for idx == 1
152             arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
153             return arch.IsValid();
154         }
155     }
156     return false;
157 }
158 
159