1 //===-- PlatformRemoteiOS.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 "PlatformRemoteiOS.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Core/Error.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/ModuleList.h"
21 #include "lldb/Core/ModuleSpec.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 PlatformRemoteiOS::SDKDirectoryInfo::SDKDirectoryInfo (const lldb_private::FileSpec &sdk_dir) :
33     directory(sdk_dir),
34     build(),
35     version_major(0),
36     version_minor(0),
37     version_update(0),
38     user_cached(false)
39 {
40     const char *dirname_cstr = sdk_dir.GetFilename().GetCString();
41     const char *pos = Args::StringToVersion (dirname_cstr,
42                                              version_major,
43                                              version_minor,
44                                              version_update);
45 
46     if (pos && pos[0] == ' ' && pos[1] == '(')
47     {
48         const char *build_start = pos + 2;
49         const char *end_paren = strchr (build_start, ')');
50         if (end_paren && build_start < end_paren)
51             build.SetCStringWithLength(build_start, end_paren - build_start);
52     }
53 }
54 
55 //------------------------------------------------------------------
56 // Static Variables
57 //------------------------------------------------------------------
58 static uint32_t g_initialize_count = 0;
59 
60 //------------------------------------------------------------------
61 // Static Functions
62 //------------------------------------------------------------------
63 void
64 PlatformRemoteiOS::Initialize ()
65 {
66     if (g_initialize_count++ == 0)
67     {
68         PluginManager::RegisterPlugin (PlatformRemoteiOS::GetPluginNameStatic(),
69                                        PlatformRemoteiOS::GetDescriptionStatic(),
70                                        PlatformRemoteiOS::CreateInstance);
71     }
72 }
73 
74 void
75 PlatformRemoteiOS::Terminate ()
76 {
77     if (g_initialize_count > 0)
78     {
79         if (--g_initialize_count == 0)
80         {
81             PluginManager::UnregisterPlugin (PlatformRemoteiOS::CreateInstance);
82         }
83     }
84 }
85 
86 PlatformSP
87 PlatformRemoteiOS::CreateInstance (bool force, const ArchSpec *arch)
88 {
89     bool create = force;
90     if (create == false && arch && arch->IsValid())
91     {
92         switch (arch->GetMachine())
93         {
94         case llvm::Triple::arm:
95         case llvm::Triple::aarch64:
96         case llvm::Triple::thumb:
97             {
98                 const llvm::Triple &triple = arch->GetTriple();
99                 llvm::Triple::VendorType vendor = triple.getVendor();
100                 switch (vendor)
101                 {
102                     case llvm::Triple::Apple:
103                         create = true;
104                         break;
105 
106 #if defined(__APPLE__)
107                     // Only accept "unknown" for the vendor if the host is Apple and
108                     // it "unknown" wasn't specified (it was just returned because it
109                     // was NOT specified)
110                     case llvm::Triple::UnknownArch:
111                         create = !arch->TripleVendorWasSpecified();
112                         break;
113 
114 #endif
115                     default:
116                         break;
117                 }
118                 if (create)
119                 {
120                     switch (triple.getOS())
121                     {
122                         case llvm::Triple::Darwin:  // Deprecated, but still support Darwin for historical reasons
123                         case llvm::Triple::IOS:     // This is the right triple value for iOS debugging
124                             break;
125 
126 #if defined(__APPLE__)
127                         // Only accept "unknown" for the OS if the host is Apple and
128                         // it "unknown" wasn't specified (it was just returned because it
129                         // was NOT specified)
130                         case llvm::Triple::UnknownOS:
131                             create = !arch->TripleOSWasSpecified();
132                             break;
133 #endif
134                         default:
135                             create = false;
136                             break;
137                     }
138                 }
139             }
140             break;
141         default:
142             break;
143         }
144     }
145 
146     if (create)
147         return lldb::PlatformSP(new PlatformRemoteiOS ());
148     return lldb::PlatformSP();
149 }
150 
151 
152 lldb_private::ConstString
153 PlatformRemoteiOS::GetPluginNameStatic ()
154 {
155     static ConstString g_name("remote-ios");
156     return g_name;
157 }
158 
159 const char *
160 PlatformRemoteiOS::GetDescriptionStatic()
161 {
162     return "Remote iOS platform plug-in.";
163 }
164 
165 
166 //------------------------------------------------------------------
167 /// Default Constructor
168 //------------------------------------------------------------------
169 PlatformRemoteiOS::PlatformRemoteiOS () :
170     PlatformDarwin (false),    // This is a remote platform
171     m_sdk_directory_infos(),
172     m_device_support_directory(),
173     m_device_support_directory_for_os_version (),
174     m_build_update(),
175     m_last_module_sdk_idx (UINT32_MAX),
176     m_connected_module_sdk_idx (UINT32_MAX)
177 {
178 }
179 
180 //------------------------------------------------------------------
181 /// Destructor.
182 ///
183 /// The destructor is virtual since this class is designed to be
184 /// inherited from by the plug-in instance.
185 //------------------------------------------------------------------
186 PlatformRemoteiOS::~PlatformRemoteiOS()
187 {
188 }
189 
190 
191 void
192 PlatformRemoteiOS::GetStatus (Stream &strm)
193 {
194     Platform::GetStatus (strm);
195     const char *sdk_directory = GetDeviceSupportDirectoryForOSVersion();
196     if (sdk_directory)
197         strm.Printf ("  SDK Path: \"%s\"\n", sdk_directory);
198     else
199         strm.PutCString ("  SDK Path: error: unable to locate SDK\n");
200 
201     const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
202     for (uint32_t i=0; i<num_sdk_infos; ++i)
203     {
204         const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
205         strm.Printf (" SDK Roots: [%2u] \"%s\"\n",
206                      i,
207                      sdk_dir_info.directory.GetPath().c_str());
208     }
209 }
210 
211 
212 Error
213 PlatformRemoteiOS::ResolveExecutable (const ModuleSpec &ms,
214                                       lldb::ModuleSP &exe_module_sp,
215                                       const FileSpecList *module_search_paths_ptr)
216 {
217     Error error;
218     // Nothing special to do here, just use the actual file and architecture
219 
220     ModuleSpec resolved_module_spec(ms);
221 
222     // Resolve any executable within a bundle on MacOSX
223     // TODO: verify that this handles shallow bundles, if not then implement one ourselves
224     Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
225 
226     if (resolved_module_spec.GetFileSpec().Exists())
227     {
228         if (resolved_module_spec.GetArchitecture().IsValid() || resolved_module_spec.GetUUID().IsValid())
229         {
230             error = ModuleList::GetSharedModule (resolved_module_spec,
231                                                  exe_module_sp,
232                                                  NULL,
233                                                  NULL,
234                                                  NULL);
235 
236             if (exe_module_sp && exe_module_sp->GetObjectFile())
237                 return error;
238             exe_module_sp.reset();
239         }
240         // No valid architecture was specified or the exact ARM slice wasn't
241         // found so ask the platform for the architectures that we should be
242         // using (in the correct order) and see if we can find a match that way
243         StreamString arch_names;
244         for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
245         {
246             error = ModuleList::GetSharedModule (resolved_module_spec,
247                                                  exe_module_sp,
248                                                  NULL,
249                                                  NULL,
250                                                  NULL);
251             // Did we find an executable using one of the
252             if (error.Success())
253             {
254                 if (exe_module_sp && exe_module_sp->GetObjectFile())
255                     break;
256                 else
257                     error.SetErrorToGenericError();
258             }
259 
260             if (idx > 0)
261                 arch_names.PutCString (", ");
262             arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
263         }
264 
265         if (error.Fail() || !exe_module_sp)
266         {
267             if (resolved_module_spec.GetFileSpec().Readable())
268             {
269                 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
270                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
271                                                 GetPluginName().GetCString(),
272                                                 arch_names.GetString().c_str());
273             }
274             else
275             {
276                 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
277             }
278         }
279     }
280     else
281     {
282         error.SetErrorStringWithFormat ("'%s' does not exist",
283                                         resolved_module_spec.GetFileSpec().GetPath().c_str());
284     }
285 
286     return error;
287 }
288 
289 FileSpec::EnumerateDirectoryResult
290 PlatformRemoteiOS::GetContainedFilesIntoVectorOfStringsCallback (void *baton,
291                                                                  FileSpec::FileType file_type,
292                                                                  const FileSpec &file_spec)
293 {
294     ((PlatformRemoteiOS::SDKDirectoryInfoCollection *)baton)->push_back(PlatformRemoteiOS::SDKDirectoryInfo(file_spec));
295     return FileSpec::eEnumerateDirectoryResultNext;
296 }
297 
298 bool
299 PlatformRemoteiOS::UpdateSDKDirectoryInfosIfNeeded()
300 {
301     if (m_sdk_directory_infos.empty())
302     {
303         const char *device_support_dir = GetDeviceSupportDirectory();
304         if (device_support_dir)
305         {
306             const bool find_directories = true;
307             const bool find_files = false;
308             const bool find_other = false;
309 
310             SDKDirectoryInfoCollection builtin_sdk_directory_infos;
311             FileSpec::EnumerateDirectory (m_device_support_directory.c_str(),
312                                           find_directories,
313                                           find_files,
314                                           find_other,
315                                           GetContainedFilesIntoVectorOfStringsCallback,
316                                           &builtin_sdk_directory_infos);
317 
318             // Only add SDK directories that have symbols in them, some SDKs only contain
319             // developer disk images and no symbols, so they aren't useful to us.
320             FileSpec sdk_symbols_symlink_fspec;
321             for (const auto &sdk_directory_info : builtin_sdk_directory_infos)
322             {
323                 sdk_symbols_symlink_fspec = sdk_directory_info.directory;
324                 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols");
325                 if (sdk_symbols_symlink_fspec.Exists())
326                 {
327                     m_sdk_directory_infos.push_back(sdk_directory_info);
328                 }
329             }
330 
331             const uint32_t num_installed = m_sdk_directory_infos.size();
332             FileSpec local_sdk_cache("~/Library/Developer/Xcode/iOS DeviceSupport", true);
333             if (local_sdk_cache.Exists())
334             {
335                 char path[PATH_MAX];
336                 if (local_sdk_cache.GetPath(path, sizeof(path)))
337                 {
338                     FileSpec::EnumerateDirectory (path,
339                                                   find_directories,
340                                                   find_files,
341                                                   find_other,
342                                                   GetContainedFilesIntoVectorOfStringsCallback,
343                                                   &m_sdk_directory_infos);
344                     const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
345                     // First try for an exact match of major, minor and update
346                     for (uint32_t i=num_installed; i<num_sdk_infos; ++i)
347                     {
348                         m_sdk_directory_infos[i].user_cached = true;
349                     }
350                 }
351             }
352         }
353     }
354     return !m_sdk_directory_infos.empty();
355 }
356 
357 const PlatformRemoteiOS::SDKDirectoryInfo *
358 PlatformRemoteiOS::GetSDKDirectoryForCurrentOSVersion ()
359 {
360     uint32_t i;
361     if (UpdateSDKDirectoryInfosIfNeeded())
362     {
363         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
364 
365         // Check to see if the user specified a build string. If they did, then
366         // be sure to match it.
367         std::vector<bool> check_sdk_info(num_sdk_infos, true);
368         ConstString build(m_sdk_build);
369         if (build)
370         {
371             for (i=0; i<num_sdk_infos; ++i)
372                 check_sdk_info[i] = m_sdk_directory_infos[i].build == build;
373         }
374 
375         // If we are connected we can find the version of the OS the platform
376         // us running on and select the right SDK
377         uint32_t major, minor, update;
378         if (GetOSVersion(major, minor, update))
379         {
380             if (UpdateSDKDirectoryInfosIfNeeded())
381             {
382                 // First try for an exact match of major, minor and update
383                 for (i=0; i<num_sdk_infos; ++i)
384                 {
385                     if (check_sdk_info[i])
386                     {
387                         if (m_sdk_directory_infos[i].version_major == major &&
388                             m_sdk_directory_infos[i].version_minor == minor &&
389                             m_sdk_directory_infos[i].version_update == update)
390                         {
391                             return &m_sdk_directory_infos[i];
392                         }
393                     }
394                 }
395                 // First try for an exact match of major and minor
396                 for (i=0; i<num_sdk_infos; ++i)
397                 {
398                     if (check_sdk_info[i])
399                     {
400                         if (m_sdk_directory_infos[i].version_major == major &&
401                             m_sdk_directory_infos[i].version_minor == minor)
402                         {
403                             return &m_sdk_directory_infos[i];
404                         }
405                     }
406                 }
407                 // Lastly try to match of major version only..
408                 for (i=0; i<num_sdk_infos; ++i)
409                 {
410                     if (check_sdk_info[i])
411                     {
412                         if (m_sdk_directory_infos[i].version_major == major)
413                         {
414                             return &m_sdk_directory_infos[i];
415                         }
416                     }
417                 }
418             }
419         }
420         else if (build)
421         {
422             // No version, just a build number, search for the first one that matches
423             for (i=0; i<num_sdk_infos; ++i)
424                 if (check_sdk_info[i])
425                     return &m_sdk_directory_infos[i];
426         }
427     }
428     return NULL;
429 }
430 
431 const PlatformRemoteiOS::SDKDirectoryInfo *
432 PlatformRemoteiOS::GetSDKDirectoryForLatestOSVersion ()
433 {
434     const PlatformRemoteiOS::SDKDirectoryInfo *result = NULL;
435     if (UpdateSDKDirectoryInfosIfNeeded())
436     {
437         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
438         // First try for an exact match of major, minor and update
439         for (uint32_t i=0; i<num_sdk_infos; ++i)
440         {
441             const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
442             if (sdk_dir_info.version_major != UINT32_MAX)
443             {
444                 if (result == NULL || sdk_dir_info.version_major > result->version_major)
445                 {
446                     result = &sdk_dir_info;
447                 }
448                 else if (sdk_dir_info.version_major == result->version_major)
449                 {
450                     if (sdk_dir_info.version_minor > result->version_minor)
451                     {
452                         result = &sdk_dir_info;
453                     }
454                     else if (sdk_dir_info.version_minor == result->version_minor)
455                     {
456                         if (sdk_dir_info.version_update > result->version_update)
457                         {
458                             result = &sdk_dir_info;
459                         }
460                     }
461                 }
462             }
463         }
464     }
465     return result;
466 }
467 
468 
469 
470 const char *
471 PlatformRemoteiOS::GetDeviceSupportDirectory()
472 {
473     if (m_device_support_directory.empty())
474     {
475         const char *device_support_dir = GetDeveloperDirectory();
476         if (device_support_dir)
477         {
478             m_device_support_directory.assign (device_support_dir);
479             m_device_support_directory.append ("/Platforms/iPhoneOS.platform/DeviceSupport");
480         }
481         else
482         {
483             // Assign a single NULL character so we know we tried to find the device
484             // support directory and we don't keep trying to find it over and over.
485             m_device_support_directory.assign (1, '\0');
486         }
487     }
488     // We should have put a single NULL character into m_device_support_directory
489     // or it should have a valid path if the code gets here
490     assert (m_device_support_directory.empty() == false);
491     if (m_device_support_directory[0])
492         return m_device_support_directory.c_str();
493     return NULL;
494 }
495 
496 
497 const char *
498 PlatformRemoteiOS::GetDeviceSupportDirectoryForOSVersion()
499 {
500     if (m_sdk_sysroot)
501         return m_sdk_sysroot.GetCString();
502 
503     if (m_device_support_directory_for_os_version.empty())
504     {
505         const PlatformRemoteiOS::SDKDirectoryInfo *sdk_dir_info = GetSDKDirectoryForCurrentOSVersion ();
506         if (sdk_dir_info == NULL)
507             sdk_dir_info = GetSDKDirectoryForLatestOSVersion ();
508         if (sdk_dir_info)
509         {
510             char path[PATH_MAX];
511             if (sdk_dir_info->directory.GetPath(path, sizeof(path)))
512             {
513                 m_device_support_directory_for_os_version = path;
514                 return m_device_support_directory_for_os_version.c_str();
515             }
516         }
517         else
518         {
519             // Assign a single NULL character so we know we tried to find the device
520             // support directory and we don't keep trying to find it over and over.
521             m_device_support_directory_for_os_version.assign (1, '\0');
522         }
523     }
524     // We should have put a single NULL character into m_device_support_directory_for_os_version
525     // or it should have a valid path if the code gets here
526     assert (m_device_support_directory_for_os_version.empty() == false);
527     if (m_device_support_directory_for_os_version[0])
528         return m_device_support_directory_for_os_version.c_str();
529     return NULL;
530 }
531 
532 uint32_t
533 PlatformRemoteiOS::FindFileInAllSDKs (const char *platform_file_path,
534                                       FileSpecList &file_list)
535 {
536     if (platform_file_path && platform_file_path[0] && UpdateSDKDirectoryInfosIfNeeded())
537     {
538         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
539         lldb_private::FileSpec local_file;
540         // First try for an exact match of major, minor and update
541         for (uint32_t sdk_idx=0; sdk_idx<num_sdk_infos; ++sdk_idx)
542         {
543             if (GetFileInSDK (platform_file_path,
544                               sdk_idx,
545                               local_file))
546             {
547                 file_list.Append(local_file);
548             }
549         }
550     }
551     return file_list.GetSize();
552 }
553 
554 bool
555 PlatformRemoteiOS::GetFileInSDK (const char *platform_file_path,
556                                  uint32_t sdk_idx,
557                                  lldb_private::FileSpec &local_file)
558 {
559     if (sdk_idx < m_sdk_directory_infos.size())
560     {
561         char sdkroot_path[PATH_MAX];
562         const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[sdk_idx];
563         if (sdk_dir_info.directory.GetPath(sdkroot_path, sizeof(sdkroot_path)))
564         {
565             const bool symbols_dirs_only = true;
566 
567             return GetFileInSDKRoot (platform_file_path,
568                                      sdkroot_path,
569                                      symbols_dirs_only,
570                                      local_file);
571         }
572     }
573     return false;
574 }
575 
576 
577 bool
578 PlatformRemoteiOS::GetFileInSDKRoot (const char *platform_file_path,
579                                      const char *sdkroot_path,
580                                      bool symbols_dirs_only,
581                                      lldb_private::FileSpec &local_file)
582 {
583     if (sdkroot_path && sdkroot_path[0] && platform_file_path && platform_file_path[0])
584     {
585         char resolved_path[PATH_MAX];
586 
587         if (!symbols_dirs_only)
588         {
589             ::snprintf (resolved_path,
590                         sizeof(resolved_path),
591                         "%s%s",
592                         sdkroot_path,
593                         platform_file_path);
594 
595             local_file.SetFile(resolved_path, true);
596             if (local_file.Exists())
597                 return true;
598         }
599 
600         ::snprintf (resolved_path,
601                     sizeof(resolved_path),
602                     "%s/Symbols.Internal%s",
603                     sdkroot_path,
604                     platform_file_path);
605 
606         local_file.SetFile(resolved_path, true);
607         if (local_file.Exists())
608             return true;
609         ::snprintf (resolved_path,
610                     sizeof(resolved_path),
611                     "%s/Symbols%s",
612                     sdkroot_path,
613                     platform_file_path);
614 
615         local_file.SetFile(resolved_path, true);
616         if (local_file.Exists())
617             return true;
618     }
619     return false;
620 }
621 
622 
623 Error
624 PlatformRemoteiOS::GetSymbolFile (const FileSpec &platform_file,
625                                   const UUID *uuid_ptr,
626                                   FileSpec &local_file)
627 {
628     Error error;
629     char platform_file_path[PATH_MAX];
630     if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path)))
631     {
632         char resolved_path[PATH_MAX];
633 
634         const char * os_version_dir = GetDeviceSupportDirectoryForOSVersion();
635         if (os_version_dir)
636         {
637             ::snprintf (resolved_path,
638                         sizeof(resolved_path),
639                         "%s/%s",
640                         os_version_dir,
641                         platform_file_path);
642 
643             local_file.SetFile(resolved_path, true);
644             if (local_file.Exists())
645                 return error;
646 
647             ::snprintf (resolved_path,
648                         sizeof(resolved_path),
649                         "%s/Symbols.Internal/%s",
650                         os_version_dir,
651                         platform_file_path);
652 
653             local_file.SetFile(resolved_path, true);
654             if (local_file.Exists())
655                 return error;
656             ::snprintf (resolved_path,
657                         sizeof(resolved_path),
658                         "%s/Symbols/%s",
659                         os_version_dir,
660                         platform_file_path);
661 
662             local_file.SetFile(resolved_path, true);
663             if (local_file.Exists())
664                 return error;
665 
666         }
667         local_file = platform_file;
668         if (local_file.Exists())
669             return error;
670 
671         error.SetErrorStringWithFormat ("unable to locate a platform file for '%s' in platform '%s'",
672                                         platform_file_path,
673                                         GetPluginName().GetCString());
674     }
675     else
676     {
677         error.SetErrorString ("invalid platform file argument");
678     }
679     return error;
680 }
681 
682 Error
683 PlatformRemoteiOS::GetSharedModule (const ModuleSpec &module_spec,
684                                     ModuleSP &module_sp,
685                                     const FileSpecList *module_search_paths_ptr,
686                                     ModuleSP *old_module_sp_ptr,
687                                     bool *did_create_ptr)
688 {
689     // For iOS, the SDK files are all cached locally on the host
690     // system. So first we ask for the file in the cached SDK,
691     // then we attempt to get a shared module for the right architecture
692     // with the right UUID.
693     const FileSpec &platform_file = module_spec.GetFileSpec();
694 
695     Error error;
696     char platform_file_path[PATH_MAX];
697 
698     if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path)))
699     {
700         ModuleSpec platform_module_spec(module_spec);
701 
702         UpdateSDKDirectoryInfosIfNeeded();
703 
704         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
705 
706         // If we are connected we migth be able to correctly deduce the SDK directory
707         // using the OS build.
708         const uint32_t connected_sdk_idx = GetConnectedSDKIndex ();
709         if (connected_sdk_idx < num_sdk_infos)
710         {
711             if (GetFileInSDK (platform_file_path, connected_sdk_idx, platform_module_spec.GetFileSpec()))
712             {
713                 module_sp.reset();
714                 error = ResolveExecutable (platform_module_spec,
715                                            module_sp,
716                                            NULL);
717                 if (module_sp)
718                 {
719                     m_last_module_sdk_idx = connected_sdk_idx;
720                     error.Clear();
721                     return error;
722                 }
723             }
724         }
725 
726         // Try the last SDK index if it is set as most files from an SDK
727         // will tend to be valid in that same SDK.
728         if (m_last_module_sdk_idx < num_sdk_infos)
729         {
730             if (GetFileInSDK (platform_file_path, m_last_module_sdk_idx, platform_module_spec.GetFileSpec()))
731             {
732                 module_sp.reset();
733                 error = ResolveExecutable (platform_module_spec,
734                                            module_sp,
735                                            NULL);
736                 if (module_sp)
737                 {
738                     error.Clear();
739                     return error;
740                 }
741             }
742         }
743 
744         // First try for an exact match of major, minor and update
745         for (uint32_t sdk_idx=0; sdk_idx<num_sdk_infos; ++sdk_idx)
746         {
747             if (m_last_module_sdk_idx == sdk_idx)
748             {
749                 // Skip the last module SDK index if we already searched
750                 // it above
751                 continue;
752             }
753             if (GetFileInSDK (platform_file_path, sdk_idx, platform_module_spec.GetFileSpec()))
754             {
755                 //printf ("sdk[%u]: '%s'\n", sdk_idx, local_file.GetPath().c_str());
756 
757                 error = ResolveExecutable (platform_module_spec, module_sp, NULL);
758                 if (module_sp)
759                 {
760                     // Remember the index of the last SDK that we found a file
761                     // in in case the wrong SDK was selected.
762                     m_last_module_sdk_idx = sdk_idx;
763                     error.Clear();
764                     return error;
765                 }
766             }
767         }
768     }
769     // Not the module we are looking for... Nothing to see here...
770     module_sp.reset();
771 
772     // This may not be an SDK-related module.  Try whether we can bring in the thing to our local cache.
773     error = GetSharedModuleWithLocalCache(module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
774     if (error.Success())
775         return error;
776 
777     const bool always_create = false;
778     error = ModuleList::GetSharedModule (module_spec,
779                                          module_sp,
780                                          module_search_paths_ptr,
781                                          old_module_sp_ptr,
782                                          did_create_ptr,
783                                          always_create);
784 
785     if (module_sp)
786         module_sp->SetPlatformFileSpec(platform_file);
787 
788     return error;
789 }
790 
791 bool
792 PlatformRemoteiOS::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
793 {
794     return ARMGetSupportedArchitectureAtIndex (idx, arch);
795 }
796 
797 uint32_t
798 PlatformRemoteiOS::GetConnectedSDKIndex ()
799 {
800     if (IsConnected())
801     {
802         if (m_connected_module_sdk_idx == UINT32_MAX)
803         {
804             std::string build;
805             if (GetRemoteOSBuildString(build))
806             {
807                 const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
808                 for (uint32_t i=0; i<num_sdk_infos; ++i)
809                 {
810                     const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
811                     if (strstr(sdk_dir_info.directory.GetFilename().AsCString(""), build.c_str()))
812                     {
813                         m_connected_module_sdk_idx = i;
814                     }
815                 }
816             }
817         }
818     }
819     else
820     {
821         m_connected_module_sdk_idx = UINT32_MAX;
822     }
823     return m_connected_module_sdk_idx;
824 }
825 
826