aboutsummaryrefslogtreecommitdiff
path: root/source/Plugins/Platform/MacOSX/PlatformiOSSimulatorCoreSimulatorSupport.mm
blob: 233c548eb37b18ed436ec018a5e19af838b917de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//===-- PlatformiOSSimulatorCoreSimulatorSupport.cpp ---------------*- C++
//-*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "PlatformiOSSimulatorCoreSimulatorSupport.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>
// Project includes
#include "lldb/Target/FileAction.h"
#include "lldb/Utility/PseudoTerminal.h"

#include "llvm/ADT/StringRef.h"

using namespace lldb_private;
using namespace lldb_utility;
// CoreSimulator lives as part of Xcode, which means we can't really link
// against it, so we dlopen()
// it at runtime, and error out nicely if that fails
@interface SimServiceContext {
}
+ (id)sharedServiceContextForDeveloperDir:(NSString *)dir
                                    error:(NSError **)error;
@end
// However, the drawback is that the compiler will not know about the selectors
// we're trying to use
// until runtime; to appease clang in this regard, define a fake protocol on
// NSObject that exposes
// the needed interface names for us
@protocol LLDBCoreSimulatorSupport <NSObject>
- (id)defaultDeviceSetWithError:(NSError **)error;
- (NSArray *)devices;
- (id)deviceType;
- (NSString *)name;
- (NSString *)identifier;
- (NSString *)modelIdentifier;
- (NSString *)productFamily;
- (int32_t)productFamilyID;
- (id)runtime;
- (BOOL)available;
- (NSString *)versionString;
- (NSString *)buildVersionString;
- (BOOL)bootWithOptions:(NSDictionary *)options error:(NSError **)error;
- (NSUInteger)state;
- (BOOL)shutdownWithError:(NSError **)error;
- (NSUUID *)UDID;
- (pid_t)spawnWithPath:(NSString *)path
               options:(NSDictionary *)options
    terminationHandler:(void (^)(int status))terminationHandler
                 error:(NSError **)error;
@end

CoreSimulatorSupport::Process::Process(lldb::pid_t p) : m_pid(p), m_error() {}

CoreSimulatorSupport::Process::Process(Error error)
    : m_pid(LLDB_INVALID_PROCESS_ID), m_error(error) {}

CoreSimulatorSupport::Process::Process(lldb::pid_t p, Error error)
    : m_pid(p), m_error(error) {}

CoreSimulatorSupport::DeviceType::DeviceType()
    : m_dev(nil), m_model_identifier() {}

CoreSimulatorSupport::DeviceType::DeviceType(id d)
    : m_dev(d), m_model_identifier() {}

CoreSimulatorSupport::DeviceType::operator bool() { return m_dev != nil; }

ConstString CoreSimulatorSupport::DeviceType::GetIdentifier() {
  return ConstString([[m_dev identifier] UTF8String]);
}

ConstString CoreSimulatorSupport::DeviceType::GetProductFamily() {
  return ConstString([[m_dev productFamily] UTF8String]);
}

CoreSimulatorSupport::DeviceType::ProductFamilyID
CoreSimulatorSupport::DeviceType::GetProductFamilyID() {
  return ProductFamilyID([m_dev productFamilyID]);
}

CoreSimulatorSupport::DeviceRuntime::DeviceRuntime()
    : m_dev(nil), m_os_version() {}

CoreSimulatorSupport::DeviceRuntime::DeviceRuntime(id d)
    : m_dev(d), m_os_version() {}

CoreSimulatorSupport::DeviceRuntime::operator bool() { return m_dev != nil; }

bool CoreSimulatorSupport::DeviceRuntime::IsAvailable() {
  return [m_dev available];
}

CoreSimulatorSupport::Device::Device()
    : m_dev(nil), m_dev_type(), m_dev_runtime() {}

CoreSimulatorSupport::Device::Device(id d)
    : m_dev(d), m_dev_type(), m_dev_runtime() {}

CoreSimulatorSupport::Device::operator bool() { return m_dev != nil; }

CoreSimulatorSupport::Device::State CoreSimulatorSupport::Device::GetState() {
  return (State)([m_dev state]);
}

CoreSimulatorSupport::ModelIdentifier::ModelIdentifier(const std::string &mi)
    : m_family(), m_versions() {
  bool any = false;
  bool first_digit = false;
  unsigned int val = 0;

  for (char c : mi) {
    any = true;
    if (::isdigit(c)) {
      if (!first_digit)
        first_digit = true;
      val = 10 * val + (c - '0');
    } else if (c == ',') {
      if (first_digit) {
        m_versions.push_back(val);
        val = 0;
      } else
        m_family.push_back(c);
    } else {
      if (first_digit) {
        m_family.clear();
        m_versions.clear();
        return;
      } else {
        m_family.push_back(c);
      }
    }
  }

  if (first_digit)
    m_versions.push_back(val);
}

CoreSimulatorSupport::ModelIdentifier::ModelIdentifier()
    : ModelIdentifier("") {}

CoreSimulatorSupport::OSVersion::OSVersion(const std::string &ver,
                                           const std::string &build)
    : m_versions(), m_build(build) {
  bool any = false;
  unsigned int val = 0;
  for (char c : ver) {
    if (c == '.') {
      m_versions.push_back(val);
      val = 0;
    } else if (::isdigit(c)) {
      val = 10 * val + (c - '0');
      any = true;
    } else {
      m_versions.clear();
      return;
    }
  }
  if (any)
    m_versions.push_back(val);
}

CoreSimulatorSupport::OSVersion::OSVersion() : OSVersion("", "") {}

CoreSimulatorSupport::ModelIdentifier
CoreSimulatorSupport::DeviceType::GetModelIdentifier() {
  if (!m_model_identifier.hasValue()) {
    auto utf8_model_id = [[m_dev modelIdentifier] UTF8String];
    if (utf8_model_id && *utf8_model_id)
      m_model_identifier = ModelIdentifier(utf8_model_id);
  }

  if (m_model_identifier.hasValue())
    return m_model_identifier.getValue();
  else
    return ModelIdentifier();
}

CoreSimulatorSupport::OSVersion
CoreSimulatorSupport::DeviceRuntime::GetVersion() {
  if (!m_os_version.hasValue()) {
    auto utf8_ver_string = [[m_dev versionString] UTF8String];
    auto utf8_build_ver = [[m_dev buildVersionString] UTF8String];
    if (utf8_ver_string && *utf8_ver_string && utf8_build_ver &&
        *utf8_build_ver) {
      m_os_version = OSVersion(utf8_ver_string, utf8_build_ver);
    }
  }

  if (m_os_version.hasValue())
    return m_os_version.getValue();
  return OSVersion();
}

std::string CoreSimulatorSupport::DeviceType::GetName() {
  auto utf8_name = [[m_dev name] UTF8String];
  if (utf8_name)
    return std::string(utf8_name);
  return "";
}

std::string CoreSimulatorSupport::Device::GetName() const {
  auto utf8_name = [[m_dev name] UTF8String];
  if (utf8_name)
    return std::string(utf8_name);
  return "";
}

std::string CoreSimulatorSupport::Device::GetUDID() const {
  auto utf8_udid = [[[m_dev UDID] UUIDString] UTF8String];
  if (utf8_udid)
    return std::string(utf8_udid);
  else
    return std::string();
}

CoreSimulatorSupport::DeviceType CoreSimulatorSupport::Device::GetDeviceType() {
  if (!m_dev_type.hasValue())
    m_dev_type = DeviceType([m_dev deviceType]);

  return m_dev_type.getValue();
}

CoreSimulatorSupport::DeviceRuntime
CoreSimulatorSupport::Device::GetDeviceRuntime() {
  if (!m_dev_runtime.hasValue())
    m_dev_runtime = DeviceRuntime([m_dev runtime]);

  return m_dev_runtime.getValue();
}

bool CoreSimulatorSupport::
operator>(const CoreSimulatorSupport::OSVersion &lhs,
          const CoreSimulatorSupport::OSVersion &rhs) {
  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l > r)
      return true;
  }
  return false;
}

bool CoreSimulatorSupport::
operator>(const CoreSimulatorSupport::ModelIdentifier &lhs,
          const CoreSimulatorSupport::ModelIdentifier &rhs) {
  if (lhs.GetFamily() != rhs.GetFamily())
    return false;
  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l > r)
      return true;
  }
  return false;
}

bool CoreSimulatorSupport::
operator<(const CoreSimulatorSupport::OSVersion &lhs,
          const CoreSimulatorSupport::OSVersion &rhs) {
  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l < r)
      return true;
  }
  return false;
}

bool CoreSimulatorSupport::
operator<(const CoreSimulatorSupport::ModelIdentifier &lhs,
          const CoreSimulatorSupport::ModelIdentifier &rhs) {
  if (lhs.GetFamily() != rhs.GetFamily())
    return false;

  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l < r)
      return true;
  }
  return false;
}

bool CoreSimulatorSupport::
operator==(const CoreSimulatorSupport::OSVersion &lhs,
           const CoreSimulatorSupport::OSVersion &rhs) {
  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l != r)
      return false;
  }
  return true;
}

bool CoreSimulatorSupport::
operator==(const CoreSimulatorSupport::ModelIdentifier &lhs,
           const CoreSimulatorSupport::ModelIdentifier &rhs) {
  if (lhs.GetFamily() != rhs.GetFamily())
    return false;

  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l != r)
      return false;
  }
  return true;
}

bool CoreSimulatorSupport::
operator!=(const CoreSimulatorSupport::OSVersion &lhs,
           const CoreSimulatorSupport::OSVersion &rhs) {
  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l != r)
      return true;
  }
  return false;
}

bool CoreSimulatorSupport::
operator!=(const CoreSimulatorSupport::ModelIdentifier &lhs,
           const CoreSimulatorSupport::ModelIdentifier &rhs) {
  if (lhs.GetFamily() != rhs.GetFamily())
    return false;

  for (size_t i = 0; i < rhs.GetNumVersions(); i++) {
    unsigned int l = lhs.GetVersionAtIndex(i);
    unsigned int r = rhs.GetVersionAtIndex(i);
    if (l != r)
      return true;
  }
  return false;
}

bool CoreSimulatorSupport::Device::Boot(Error &err) {
  if (m_dev == nil) {
    err.SetErrorString("no valid simulator instance");
    return false;
  }

#define kSimDeviceBootPersist                                                  \
  @"persist" /* An NSNumber (boolean) indicating whether or not the session    \
                should outlive the calling process (default false) */

  NSDictionary *options = @{
    kSimDeviceBootPersist : @NO,
  };

#undef kSimDeviceBootPersist

  NSError *nserror;
  if ([m_dev bootWithOptions:options error:&nserror]) {
    err.Clear();
    return true;
  } else {
    err.SetErrorString([[nserror description] UTF8String]);
    return false;
  }
}

bool CoreSimulatorSupport::Device::Shutdown(Error &err) {
  NSError *nserror;
  if ([m_dev shutdownWithError:&nserror]) {
    err.Clear();
    return true;
  } else {
    err.SetErrorString([[nserror description] UTF8String]);
    return false;
  }
}

static Error HandleFileAction(ProcessLaunchInfo &launch_info,
                              NSMutableDictionary *options, NSString *key,
                              const int fd, File &file) {
  Error error;
  const FileAction *file_action = launch_info.GetFileActionForFD(fd);
  if (file_action) {
    switch (file_action->GetAction()) {
    case FileAction::eFileActionNone:
      break;

    case FileAction::eFileActionClose:
      error.SetErrorStringWithFormat("close file action for %i not supported",
                                     fd);
      break;

    case FileAction::eFileActionDuplicate:
      error.SetErrorStringWithFormat(
          "duplication file action for %i not supported", fd);
      break;

    case FileAction::eFileActionOpen: {
      FileSpec file_spec = file_action->GetFileSpec();
      if (file_spec) {
        const int master_fd = launch_info.GetPTY().GetMasterFileDescriptor();
        if (master_fd != PseudoTerminal::invalid_fd) {
          // Check in case our file action open wants to open the slave
          const char *slave_path = launch_info.GetPTY().GetSlaveName(NULL, 0);
          if (slave_path) {
            FileSpec slave_spec(slave_path, false);
            if (file_spec == slave_spec) {
              int slave_fd = launch_info.GetPTY().GetSlaveFileDescriptor();
              if (slave_fd == PseudoTerminal::invalid_fd)
                slave_fd = launch_info.GetPTY().OpenSlave(O_RDWR, nullptr, 0);
              if (slave_fd == PseudoTerminal::invalid_fd) {
                error.SetErrorStringWithFormat("unable to open slave pty '%s'",
                                               slave_path);
                return error; // Failure
              }
              [options setValue:[NSNumber numberWithInteger:slave_fd]
                         forKey:key];
              return error; // Success
            }
          }
        }
        Error posix_error;
        int created_fd =
            open(file_spec.GetPath().c_str(), file_action->GetActionArgument(),
                 S_IRUSR | S_IWUSR);
        if (created_fd >= 0) {
          file.SetDescriptor(created_fd, true);
          [options setValue:[NSNumber numberWithInteger:created_fd] forKey:key];
          return error; // Success
        } else {
          posix_error.SetErrorToErrno();
          error.SetErrorStringWithFormat("unable to open file '%s': %s",
                                         file_spec.GetPath().c_str(),
                                         posix_error.AsCString());
        }
      }
    } break;
    }
  }
  return error; // Success, no file action, nothing to do
}

CoreSimulatorSupport::Process
CoreSimulatorSupport::Device::Spawn(ProcessLaunchInfo &launch_info) {
#define kSimDeviceSpawnEnvironment                                             \
  @"environment" /* An NSDictionary (NSStrings -> NSStrings) of environment    \
                    key/values */
#define kSimDeviceSpawnStdin @"stdin"   /* An NSNumber corresponding to a fd */
#define kSimDeviceSpawnStdout @"stdout" /* An NSNumber corresponding to a fd   \
                                           */
#define kSimDeviceSpawnStderr @"stderr" /* An NSNumber corresponding to a fd   \
                                           */
#define kSimDeviceSpawnArguments                                               \
  @"arguments" /* An NSArray of strings to use as the argv array.  If not      \
                  provided, path will be argv[0] */
#define kSimDeviceSpawnWaitForDebugger                                         \
  @"wait_for_debugger" /* An NSNumber (bool) */

  NSMutableDictionary *options = [[NSMutableDictionary alloc] init];

  if (launch_info.GetFlags().Test(lldb::eLaunchFlagDebug))
    [options setObject:@YES forKey:kSimDeviceSpawnWaitForDebugger];

  if (launch_info.GetArguments().GetArgumentCount()) {
    const Args &args(launch_info.GetArguments());
    NSMutableArray *args_array = [[NSMutableArray alloc] init];
    for (size_t idx = 0; idx < args.GetArgumentCount(); idx++)
      [args_array
          addObject:[NSString
                        stringWithUTF8String:args.GetArgumentAtIndex(idx)]];

    [options setObject:args_array forKey:kSimDeviceSpawnArguments];
  }

  if (launch_info.GetEnvironmentEntries().GetArgumentCount()) {
    const Args &envs(launch_info.GetEnvironmentEntries());
    NSMutableDictionary *env_dict = [[NSMutableDictionary alloc] init];
    for (size_t idx = 0; idx < envs.GetArgumentCount(); idx++) {
      llvm::StringRef arg_sr(envs.GetArgumentAtIndex(idx));
      auto first_eq = arg_sr.find('=');
      if (first_eq == llvm::StringRef::npos)
        continue;
      llvm::StringRef key = arg_sr.substr(0, first_eq);
      llvm::StringRef value = arg_sr.substr(first_eq + 1);

      NSString *key_ns = [NSString stringWithUTF8String:key.str().c_str()];
      NSString *value_ns = [NSString stringWithUTF8String:value.str().c_str()];

      [env_dict setValue:value_ns forKey:key_ns];
    }

    [options setObject:env_dict forKey:kSimDeviceSpawnEnvironment];
  }

  Error error;
  File stdin_file;
  File stdout_file;
  File stderr_file;
  error = HandleFileAction(launch_info, options, kSimDeviceSpawnStdin,
                           STDIN_FILENO, stdin_file);

  if (error.Fail())
    return CoreSimulatorSupport::Process(error);

  error = HandleFileAction(launch_info, options, kSimDeviceSpawnStdout,
                           STDOUT_FILENO, stdout_file);

  if (error.Fail())
    return CoreSimulatorSupport::Process(error);

  error = HandleFileAction(launch_info, options, kSimDeviceSpawnStderr,
                           STDERR_FILENO, stderr_file);

  if (error.Fail())
    return CoreSimulatorSupport::Process(error);

#undef kSimDeviceSpawnEnvironment
#undef kSimDeviceSpawnStdin
#undef kSimDeviceSpawnStdout
#undef kSimDeviceSpawnStderr
#undef kSimDeviceSpawnWaitForDebugger
#undef kSimDeviceSpawnArguments

  NSError *nserror;

  pid_t pid = [m_dev
           spawnWithPath:[NSString stringWithUTF8String:launch_info
                                                            .GetExecutableFile()
                                                            .GetPath()
                                                            .c_str()]
                 options:options
      terminationHandler:nil
                   error:&nserror];

  if (pid < 0) {
    const char *nserror_string = [[nserror description] UTF8String];
    error.SetErrorString(nserror_string ? nserror_string : "unable to launch");
  }

  return CoreSimulatorSupport::Process(pid, error);
}

CoreSimulatorSupport::DeviceSet
CoreSimulatorSupport::DeviceSet::GetAllDevices(const char *developer_dir) {
  if (!developer_dir || !developer_dir[0])
    return DeviceSet([NSArray new]);

  Class SimServiceContextClass = NSClassFromString(@"SimServiceContext");
  NSString *dev_dir = @(developer_dir);
  NSError *error = nil;

  id serviceContext =
      [SimServiceContextClass sharedServiceContextForDeveloperDir:dev_dir
                                                            error:&error];
  if (!serviceContext)
    return DeviceSet([NSArray new]);

  return DeviceSet([[serviceContext defaultDeviceSetWithError:&error] devices]);
}

CoreSimulatorSupport::DeviceSet
CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
    const char *developer_dir) {
  return GetAllDevices(developer_dir).GetDevicesIf([](Device d) -> bool {
    return (d && d.GetDeviceType() && d.GetDeviceRuntime() &&
            d.GetDeviceRuntime().IsAvailable());
  });
}

size_t CoreSimulatorSupport::DeviceSet::GetNumDevices() {
  return [m_dev count];
}

CoreSimulatorSupport::Device
CoreSimulatorSupport::DeviceSet::GetDeviceAtIndex(size_t idx) {
  if (idx < GetNumDevices())
    return Device([m_dev objectAtIndex:idx]);
  return Device();
}

CoreSimulatorSupport::DeviceSet CoreSimulatorSupport::DeviceSet::GetDevicesIf(
    std::function<bool(CoreSimulatorSupport::Device)> f) {
  NSMutableArray *array = [[NSMutableArray alloc] init];
  for (NSUInteger i = 0; i < GetNumDevices(); i++) {
    Device d(GetDeviceAtIndex(i));
    if (f(d))
      [array addObject:(id)d.m_dev];
  }

  return DeviceSet(array);
}

void CoreSimulatorSupport::DeviceSet::ForEach(
    std::function<bool(const Device &)> f) {
  const size_t n = GetNumDevices();
  for (NSUInteger i = 0; i < n; ++i) {
    if (f(GetDeviceAtIndex(i)) == false)
      break;
  }
}

CoreSimulatorSupport::DeviceSet CoreSimulatorSupport::DeviceSet::GetDevices(
    CoreSimulatorSupport::DeviceType::ProductFamilyID dev_id) {
  NSMutableArray *array = [[NSMutableArray alloc] init];
  const size_t n = GetNumDevices();
  for (NSUInteger i = 0; i < n; ++i) {
    Device d(GetDeviceAtIndex(i));
    if (d && d.GetDeviceType() &&
        d.GetDeviceType().GetProductFamilyID() == dev_id)
      [array addObject:(id)d.m_dev];
  }

  return DeviceSet(array);
}

CoreSimulatorSupport::Device CoreSimulatorSupport::DeviceSet::GetFanciest(
    CoreSimulatorSupport::DeviceType::ProductFamilyID dev_id) {
  Device dev;

  for (NSUInteger i = 0; i < GetNumDevices(); i++) {
    Device d(GetDeviceAtIndex(i));
    if (d && d.GetDeviceType() &&
        d.GetDeviceType().GetProductFamilyID() == dev_id) {
      if (!dev)
        dev = d;
      else {
        if ((d.GetDeviceType().GetModelIdentifier() >
             dev.GetDeviceType().GetModelIdentifier()) ||
            d.GetDeviceRuntime().GetVersion() >
                dev.GetDeviceRuntime().GetVersion())
          dev = d;
      }
    }
  }

  return dev;
}