Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- PlatformMacOSX.cpp ------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "PlatformMacOSX.h"
10
#include "PlatformRemoteMacOSX.h"
11
#include "PlatformRemoteiOS.h"
12
#if defined(__APPLE__)
13
#include "PlatformAppleSimulator.h"
14
#include "PlatformDarwinKernel.h"
15
#include "PlatformRemoteAppleBridge.h"
16
#include "PlatformRemoteAppleTV.h"
17
#include "PlatformRemoteAppleWatch.h"
18
#endif
19
#include "lldb/Breakpoint/BreakpointLocation.h"
20
#include "lldb/Core/Module.h"
21
#include "lldb/Core/ModuleList.h"
22
#include "lldb/Core/ModuleSpec.h"
23
#include "lldb/Core/PluginManager.h"
24
#include "lldb/Host/Config.h"
25
#include "lldb/Host/Host.h"
26
#include "lldb/Host/HostInfo.h"
27
#include "lldb/Symbol/ObjectFile.h"
28
#include "lldb/Target/Process.h"
29
#include "lldb/Target/Target.h"
30
#include "lldb/Utility/DataBufferHeap.h"
31
#include "lldb/Utility/FileSpec.h"
32
#include "lldb/Utility/Log.h"
33
#include "lldb/Utility/Status.h"
34
#include "lldb/Utility/StreamString.h"
35
36
#include <sstream>
37
38
using namespace lldb;
39
using namespace lldb_private;
40
41
LLDB_PLUGIN_DEFINE(PlatformMacOSX)
42
43
static uint32_t g_initialize_count = 0;
44
45
3.44k
void PlatformMacOSX::Initialize() {
46
3.44k
  PlatformDarwin::Initialize();
47
3.44k
  PlatformRemoteiOS::Initialize();
48
3.44k
  PlatformRemoteMacOSX::Initialize();
49
3.44k
#if defined(__APPLE__)
50
3.44k
  PlatformAppleSimulator::Initialize();
51
3.44k
  PlatformDarwinKernel::Initialize();
52
3.44k
  PlatformRemoteAppleTV::Initialize();
53
3.44k
  PlatformRemoteAppleWatch::Initialize();
54
3.44k
  PlatformRemoteAppleBridge::Initialize();
55
3.44k
#endif
56
57
3.44k
  if (g_initialize_count++ == 0) {
58
3.44k
#if defined(__APPLE__)
59
3.44k
    PlatformSP default_platform_sp(new PlatformMacOSX());
60
3.44k
    default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
61
3.44k
    Platform::SetHostPlatform(default_platform_sp);
62
3.44k
#endif
63
3.44k
    PluginManager::RegisterPlugin(PlatformMacOSX::GetPluginNameStatic(),
64
3.44k
                                  PlatformMacOSX::GetDescriptionStatic(),
65
3.44k
                                  PlatformMacOSX::CreateInstance);
66
3.44k
  }
67
3.44k
}
68
69
3.43k
void PlatformMacOSX::Terminate() {
70
3.43k
  if (g_initialize_count > 0) {
71
3.43k
    if (--g_initialize_count == 0) {
72
3.43k
      PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance);
73
3.43k
    }
74
3.43k
  }
75
76
3.43k
#if defined(__APPLE__)
77
3.43k
  PlatformRemoteAppleBridge::Terminate();
78
3.43k
  PlatformRemoteAppleWatch::Terminate();
79
3.43k
  PlatformRemoteAppleTV::Terminate();
80
3.43k
  PlatformDarwinKernel::Terminate();
81
3.43k
  PlatformAppleSimulator::Terminate();
82
3.43k
#endif
83
3.43k
  PlatformRemoteMacOSX::Initialize();
84
3.43k
  PlatformRemoteiOS::Terminate();
85
3.43k
  PlatformDarwin::Terminate();
86
3.43k
}
87
88
3.44k
llvm::StringRef PlatformMacOSX::GetDescriptionStatic() {
89
3.44k
  return "Local Mac OS X user platform plug-in.";
90
3.44k
}
91
92
77
PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
93
  // The only time we create an instance is when we are creating a remote
94
  // macosx platform which is handled by PlatformRemoteMacOSX.
95
77
  return PlatformSP();
96
77
}
97
98
/// Default Constructor
99
3.44k
PlatformMacOSX::PlatformMacOSX() : PlatformDarwin(true) {}
100
101
0
ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) {
102
0
  ModuleSP exe_module_sp(target.GetExecutableModule());
103
0
  if (!exe_module_sp)
104
0
    return {};
105
106
0
  ObjectFile *objfile = exe_module_sp->GetObjectFile();
107
0
  if (!objfile)
108
0
    return {};
109
110
0
  llvm::VersionTuple version = objfile->GetSDKVersion();
111
0
  if (version.empty())
112
0
    return {};
113
114
  // First try to find an SDK that matches the given SDK version.
115
0
  if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
116
0
    StreamString sdk_path;
117
0
    sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
118
0
                    "SDKs/MacOSX%u.%u.sdk",
119
0
                    fspec.GetPath().c_str(), version.getMajor(),
120
0
                    version.getMinor().getValue());
121
0
    if (FileSystem::Instance().Exists(fspec))
122
0
      return ConstString(sdk_path.GetString());
123
0
  }
124
125
  // Use the default SDK as a fallback.
126
0
  FileSpec fspec(
127
0
      HostInfo::GetXcodeSDKPath(lldb_private::XcodeSDK::GetAnyMacOS()));
128
0
  if (fspec) {
129
0
    if (FileSystem::Instance().Exists(fspec))
130
0
      return ConstString(fspec.GetPath());
131
0
  }
132
133
0
  return {};
134
0
}
135
136
8.68k
std::vector<ArchSpec> PlatformMacOSX::GetSupportedArchitectures() {
137
8.68k
  std::vector<ArchSpec> result;
138
#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
139
  // macOS for ARM64 support both native and translated x86_64 processes
140
  ARMGetSupportedArchitectures(result, llvm::Triple::MacOSX);
141
142
  // We can't use x86GetSupportedArchitectures() because it uses
143
  // the system architecture for some of its return values and also
144
  // has a 32bits variant.
145
  result.push_back(ArchSpec("x86_64-apple-macosx"));
146
  result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
147
  result.push_back(ArchSpec("arm64-apple-ios-macabi"));
148
  result.push_back(ArchSpec("arm64e-apple-ios-macabi"));
149
#else
150
8.68k
  x86GetSupportedArchitectures(result);
151
8.68k
  result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
152
8.68k
#endif
153
8.68k
  return result;
154
8.68k
}
155
156
lldb_private::Status PlatformMacOSX::GetSharedModule(
157
    const lldb_private::ModuleSpec &module_spec, Process *process,
158
    lldb::ModuleSP &module_sp,
159
    const lldb_private::FileSpecList *module_search_paths_ptr,
160
136k
    llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
161
136k
  Status error = GetSharedModuleWithLocalCache(module_spec, module_sp,
162
136k
                                               module_search_paths_ptr,
163
136k
                                               old_modules, did_create_ptr);
164
165
136k
  if (module_sp) {
166
136k
    if (module_spec.GetArchitecture().GetCore() ==
167
136k
        ArchSpec::eCore_x86_64_x86_64h) {
168
0
      ObjectFile *objfile = module_sp->GetObjectFile();
169
0
      if (objfile == nullptr) {
170
        // We didn't find an x86_64h slice, fall back to a x86_64 slice
171
0
        ModuleSpec module_spec_x86_64(module_spec);
172
0
        module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
173
0
        lldb::ModuleSP x86_64_module_sp;
174
0
        llvm::SmallVector<lldb::ModuleSP, 1> old_x86_64_modules;
175
0
        bool did_create = false;
176
0
        Status x86_64_error = GetSharedModuleWithLocalCache(
177
0
            module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr,
178
0
            &old_x86_64_modules, &did_create);
179
0
        if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
180
0
          module_sp = x86_64_module_sp;
181
0
          if (old_modules)
182
0
            old_modules->append(old_x86_64_modules.begin(),
183
0
                                old_x86_64_modules.end());
184
0
          if (did_create_ptr)
185
0
            *did_create_ptr = did_create;
186
0
          return x86_64_error;
187
0
        }
188
0
      }
189
0
    }
190
136k
  }
191
192
136k
  if (!module_sp) {
193
220
    error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp,
194
220
                                              module_search_paths_ptr,
195
220
                                              old_modules, did_create_ptr);
196
220
  }
197
136k
  return error;
198
136k
}