xref: /llvm-project/lldb/source/Plugins/Platform/MacOSX/objcxx/PlatformiOSSimulatorCoreSimulatorSupport.h (revision b798f4bd50bbf0f5eb46804afad10629797c73aa)
1 //===-- PlatformiOSSimulatorCoreSimulatorSupport.h ----------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_OBJCXX_PLATFORMIOSSIMULATORCORESIMULATORSUPPORT_H
11 #define LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_OBJCXX_PLATFORMIOSSIMULATORCORESIMULATORSUPPORT_H
12 
13 #include <functional>
14 #include <optional>
15 #include <ostream>
16 #include <string>
17 #include <vector>
18 #ifdef __APPLE__
19 #include <objc/objc.h>
20 #else
21 typedef void *id;
22 #endif
23 #include "lldb/Host/ProcessLaunchInfo.h"
24 #include "lldb/Utility/Args.h"
25 #include "lldb/Utility/ConstString.h"
26 #include "lldb/Utility/Status.h"
27 
28 
29 // And now the actual magic
30 namespace CoreSimulatorSupport {
31 class Process {
32 public:
33   lldb::pid_t GetPID() { return m_pid; }
34 
35   explicit operator bool() { return m_pid != LLDB_INVALID_PROCESS_ID; }
36 
37   lldb_private::Status GetError() { return m_error.Clone(); }
38 
39 private:
40   Process(lldb::pid_t p);
41 
42   Process(lldb_private::Status error);
43 
44   Process(lldb::pid_t p, lldb_private::Status error);
45 
46   lldb::pid_t m_pid;
47   lldb_private::Status m_error;
48 
49   friend class Device;
50 };
51 
52 class ModelIdentifier {
53 public:
54   ModelIdentifier(const std::string &mi);
55   ModelIdentifier();
56 
57   explicit operator bool() const { return !m_versions.empty(); }
58 
59   size_t GetNumVersions() const { return m_versions.size(); }
60 
61   unsigned int GetVersionAtIndex(size_t idx) const { return m_versions[idx]; }
62 
63   std::string GetFamily() const { return m_family.c_str(); }
64 
65 private:
66   std::string m_family;
67   std::vector<unsigned int> m_versions;
68 };
69 
70 class DeviceType {
71 public:
72   enum class ProductFamilyID : int32_t {
73     iPhone = 1,
74     iPad = 2,
75     appleTV = 3,
76     appleWatch = 4,
77     appleXR = 7,
78   };
79 
80   DeviceType();
81 
82   DeviceType(id d);
83 
84   explicit operator bool();
85 
86   std::string GetName();
87 
88   lldb_private::ConstString GetIdentifier();
89 
90   ModelIdentifier GetModelIdentifier();
91 
92   lldb_private::ConstString GetProductFamily();
93 
94   ProductFamilyID GetProductFamilyID();
95 
96 private:
97   id m_dev = nullptr;
98   std::optional<ModelIdentifier> m_model_identifier;
99 };
100 
101 class OSVersion {
102 public:
103   OSVersion(const std::string &ver, const std::string &build);
104 
105   OSVersion();
106 
107   explicit operator bool() const { return !m_versions.empty(); }
108 
109   size_t GetNumVersions() const { return m_versions.size(); }
110 
111   unsigned int GetVersionAtIndex(size_t idx) const { return m_versions[idx]; }
112 
113   const char *GetBuild() const { return m_build.c_str(); }
114 
115 private:
116   std::vector<unsigned int> m_versions;
117   std::string m_build;
118 };
119 
120 class DeviceRuntime {
121 public:
122   DeviceRuntime();
123 
124   DeviceRuntime(id d);
125 
126   explicit operator bool();
127 
128   OSVersion GetVersion();
129 
130   bool IsAvailable();
131 
132 private:
133   id m_dev = nullptr;
134   std::optional<OSVersion> m_os_version;
135 };
136 
137 class Device {
138 private:
139   typedef unsigned long int NSUInteger;
140 
141 public:
142   enum class State : NSUInteger {
143     Creating,
144     Shutdown,
145     Booting,
146     Booted,
147     ShuttingDown
148   };
149 
150   Device();
151 
152   Device(id d);
153 
154   explicit operator bool();
155 
156   std::string GetName() const;
157 
158   DeviceType GetDeviceType();
159 
160   DeviceRuntime GetDeviceRuntime();
161 
162   State GetState();
163 
164   bool Boot(lldb_private::Status &err);
165 
166   bool Shutdown(lldb_private::Status &err);
167 
168   std::string GetUDID() const;
169 
170   Process Spawn(lldb_private::ProcessLaunchInfo &launch_info);
171 
172 private:
173   id m_dev = nullptr;
174   std::optional<DeviceType> m_dev_type;
175   std::optional<DeviceRuntime> m_dev_runtime;
176 
177   friend class DeviceSet;
178 };
179 
180 bool operator>(const OSVersion &lhs, const OSVersion &rhs);
181 
182 bool operator>(const ModelIdentifier &lhs, const ModelIdentifier &rhs);
183 
184 bool operator<(const OSVersion &lhs, const OSVersion &rhs);
185 
186 bool operator<(const ModelIdentifier &lhs, const ModelIdentifier &rhs);
187 
188 bool operator==(const OSVersion &lhs, const OSVersion &rhs);
189 
190 bool operator==(const ModelIdentifier &lhs, const ModelIdentifier &rhs);
191 
192 bool operator!=(const OSVersion &lhs, const OSVersion &rhs);
193 
194 bool operator!=(const ModelIdentifier &lhs, const ModelIdentifier &rhs);
195 
196 class DeviceSet {
197 public:
198   static DeviceSet GetAllDevices(const char *developer_dir);
199 
200   static DeviceSet GetAvailableDevices(const char *developer_dir);
201 
202   size_t GetNumDevices();
203 
204   Device GetDeviceAtIndex(size_t idx);
205 
206   void ForEach(std::function<bool(const Device &)> f);
207 
208   DeviceSet GetDevicesIf(std::function<bool(Device)> f);
209 
210   DeviceSet GetDevices(DeviceType::ProductFamilyID dev_id);
211 
212   Device GetFanciest(DeviceType::ProductFamilyID dev_id);
213 
214 private:
215   DeviceSet(id arr) : m_dev(arr) {}
216 
217   id m_dev;
218 };
219 }
220 
221 #endif // LLDB_SOURCE_PLUGINS_PLATFORM_MACOSX_OBJCXX_PLATFORMIOSSIMULATORCORESIMULATORSUPPORT_H
222