180814287SRaphael Isemann //===-- PlatformRemoteiOS.cpp ---------------------------------------------===//
2ded470d3SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ded470d3SGreg Clayton //
7ded470d3SGreg Clayton //===----------------------------------------------------------------------===//
8ded470d3SGreg Clayton 
9ded470d3SGreg Clayton #include "PlatformRemoteiOS.h"
10ded470d3SGreg Clayton 
11ded470d3SGreg Clayton #include "lldb/Breakpoint/BreakpointLocation.h"
12ded470d3SGreg Clayton #include "lldb/Core/Module.h"
13ded470d3SGreg Clayton #include "lldb/Core/ModuleList.h"
141f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
15ded470d3SGreg Clayton #include "lldb/Core/PluginManager.h"
16ded470d3SGreg Clayton #include "lldb/Host/Host.h"
17ded470d3SGreg Clayton #include "lldb/Target/Process.h"
18ded470d3SGreg Clayton #include "lldb/Target/Target.h"
195f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
205713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
21c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
226f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2397206d57SZachary Turner #include "lldb/Utility/Status.h"
24bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
25ded470d3SGreg Clayton 
26ded470d3SGreg Clayton using namespace lldb;
27ded470d3SGreg Clayton using namespace lldb_private;
28ded470d3SGreg Clayton 
29bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(PlatformRemoteiOS)
30fbb4d1e4SJonas Devlieghere 
311cb6496eSGreg Clayton // Static Variables
321cb6496eSGreg Clayton static uint32_t g_initialize_count = 0;
33d314e810SGreg Clayton 
341cb6496eSGreg Clayton // Static Functions
Initialize()35b9c1b51eSKate Stone void PlatformRemoteiOS::Initialize() {
363c4f89d7STamas Berghammer   PlatformDarwin::Initialize();
373c4f89d7STamas Berghammer 
38b9c1b51eSKate Stone   if (g_initialize_count++ == 0) {
3957abc5d6SGreg Clayton     PluginManager::RegisterPlugin(PlatformRemoteiOS::GetPluginNameStatic(),
40d314e810SGreg Clayton                                   PlatformRemoteiOS::GetDescriptionStatic(),
41d314e810SGreg Clayton                                   PlatformRemoteiOS::CreateInstance);
42d314e810SGreg Clayton   }
43d314e810SGreg Clayton }
44d314e810SGreg Clayton 
Terminate()45b9c1b51eSKate Stone void PlatformRemoteiOS::Terminate() {
46b9c1b51eSKate Stone   if (g_initialize_count > 0) {
47b9c1b51eSKate Stone     if (--g_initialize_count == 0) {
48d314e810SGreg Clayton       PluginManager::UnregisterPlugin(PlatformRemoteiOS::CreateInstance);
49ded470d3SGreg Clayton     }
50ded470d3SGreg Clayton   }
513c4f89d7STamas Berghammer 
523c4f89d7STamas Berghammer   PlatformDarwin::Terminate();
531cb6496eSGreg Clayton }
54ded470d3SGreg Clayton 
CreateInstance(bool force,const ArchSpec * arch)55b9c1b51eSKate Stone PlatformSP PlatformRemoteiOS::CreateInstance(bool force, const ArchSpec *arch) {
56a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Platform);
57b9c1b51eSKate Stone   if (log) {
58f638ffddSJason Molenda     const char *arch_name;
59f638ffddSJason Molenda     if (arch && arch->GetArchitectureName())
60f638ffddSJason Molenda       arch_name = arch->GetArchitectureName();
61f638ffddSJason Molenda     else
62f638ffddSJason Molenda       arch_name = "<null>";
63f638ffddSJason Molenda 
64b9c1b51eSKate Stone     const char *triple_cstr =
65b9c1b51eSKate Stone         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
66f638ffddSJason Molenda 
6763e5fb76SJonas Devlieghere     LLDB_LOGF(log, "PlatformRemoteiOS::%s(force=%s, arch={%s,%s})",
6863e5fb76SJonas Devlieghere               __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
69f638ffddSJason Molenda   }
70f638ffddSJason Molenda 
71b3a40ba8SGreg Clayton   bool create = force;
72a6682a41SJonas Devlieghere   if (!create && arch && arch->IsValid()) {
73b9c1b51eSKate Stone     switch (arch->GetMachine()) {
74b3a40ba8SGreg Clayton     case llvm::Triple::arm:
75d8eaa175STodd Fiala     case llvm::Triple::aarch64:
76b9c1b51eSKate Stone     case llvm::Triple::thumb: {
77b3a40ba8SGreg Clayton       const llvm::Triple &triple = arch->GetTriple();
7870512317SGreg Clayton       llvm::Triple::VendorType vendor = triple.getVendor();
79b9c1b51eSKate Stone       switch (vendor) {
8070512317SGreg Clayton       case llvm::Triple::Apple:
81b3a40ba8SGreg Clayton         create = true;
8223f59509SGreg Clayton         break;
8370512317SGreg Clayton 
84dbc6c0bbSGreg Clayton #if defined(__APPLE__)
85c791417aSBob Wilson       // Only accept "unknown" for the vendor if the host is Apple and
8605097246SAdrian Prantl       // "unknown" wasn't specified (it was just returned because it was NOT
8705097246SAdrian Prantl       // specified)
88d8024371SAdrian Prantl       case llvm::Triple::UnknownVendor:
8970512317SGreg Clayton         create = !arch->TripleVendorWasSpecified();
9070512317SGreg Clayton         break;
9170512317SGreg Clayton 
925c5ae47aSEnrico Granata #endif
9370512317SGreg Clayton       default:
9470512317SGreg Clayton         break;
9570512317SGreg Clayton       }
96b9c1b51eSKate Stone       if (create) {
97b9c1b51eSKate Stone         switch (triple.getOS()) {
98b9c1b51eSKate Stone         case llvm::Triple::Darwin: // Deprecated, but still support Darwin for
99b9c1b51eSKate Stone                                    // historical reasons
100b9c1b51eSKate Stone         case llvm::Triple::IOS:    // This is the right triple value for iOS
101b9c1b51eSKate Stone                                    // debugging
10270512317SGreg Clayton           break;
10370512317SGreg Clayton 
10470512317SGreg Clayton         default:
10570512317SGreg Clayton           create = false;
10670512317SGreg Clayton           break;
10770512317SGreg Clayton         }
10870512317SGreg Clayton       }
109b9c1b51eSKate Stone     } break;
110b3a40ba8SGreg Clayton     default:
111b3a40ba8SGreg Clayton       break;
112b3a40ba8SGreg Clayton     }
113b3a40ba8SGreg Clayton   }
114b3a40ba8SGreg Clayton 
115b9c1b51eSKate Stone   if (create) {
116f638ffddSJason Molenda     if (log)
11763e5fb76SJonas Devlieghere       LLDB_LOGF(log, "PlatformRemoteiOS::%s() creating platform", __FUNCTION__);
118f638ffddSJason Molenda 
119615eb7e6SGreg Clayton     return lldb::PlatformSP(new PlatformRemoteiOS());
120f638ffddSJason Molenda   }
121f638ffddSJason Molenda 
122f638ffddSJason Molenda   if (log)
12363e5fb76SJonas Devlieghere     LLDB_LOGF(log, "PlatformRemoteiOS::%s() aborting creation of platform",
124b9c1b51eSKate Stone               __FUNCTION__);
125f638ffddSJason Molenda 
126615eb7e6SGreg Clayton   return lldb::PlatformSP();
127ded470d3SGreg Clayton }
128ded470d3SGreg Clayton 
GetDescriptionStatic()129a458ef4fSPavel Labath llvm::StringRef PlatformRemoteiOS::GetDescriptionStatic() {
130ded470d3SGreg Clayton   return "Remote iOS platform plug-in.";
131ded470d3SGreg Clayton }
132ded470d3SGreg Clayton 
1331cb6496eSGreg Clayton /// Default Constructor
PlatformRemoteiOS()134b9c1b51eSKate Stone PlatformRemoteiOS::PlatformRemoteiOS()
1351781d6f7SJason Molenda     : PlatformRemoteDarwinDevice() {}
13632e0a750SGreg Clayton 
GetSupportedArchitectures(const ArchSpec & process_host_arch)137*dde487e5SJonas Devlieghere std::vector<ArchSpec> PlatformRemoteiOS::GetSupportedArchitectures(
138*dde487e5SJonas Devlieghere     const ArchSpec &process_host_arch) {
139e7c48f3cSPavel Labath   std::vector<ArchSpec> result;
1408faca2edSJonas Devlieghere   ARMGetSupportedArchitectures(result, llvm::Triple::IOS);
141e7c48f3cSPavel Labath   return result;
142ded470d3SGreg Clayton }
1438012cadbSGreg Clayton 
CheckLocalSharedCache() const1448faca2edSJonas Devlieghere bool PlatformRemoteiOS::CheckLocalSharedCache() const {
1458faca2edSJonas Devlieghere   // You can run iPhone and iPad apps on Mac with Apple Silicon. At the
1468faca2edSJonas Devlieghere   // platform level there's no way to distinguish them from remote iOS
1478faca2edSJonas Devlieghere   // applications. Make sure we still read from our own shared cache.
1488faca2edSJonas Devlieghere   return true;
1498faca2edSJonas Devlieghere }
1508faca2edSJonas Devlieghere 
GetDeviceSupportDirectoryName()151173bb3c2SJonas Devlieghere llvm::StringRef PlatformRemoteiOS::GetDeviceSupportDirectoryName() {
152173bb3c2SJonas Devlieghere   return "iOS DeviceSupport";
1538012cadbSGreg Clayton }
1548012cadbSGreg Clayton 
GetPlatformName()155173bb3c2SJonas Devlieghere llvm::StringRef PlatformRemoteiOS::GetPlatformName() {
1561781d6f7SJason Molenda   return "iPhoneOS.platform";
157e4af28b7SIlia K }
158