1 //===-- PlatformDarwin.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 "PlatformDarwin.h" 10 11 #include <string.h> 12 13 #include <algorithm> 14 #include <memory> 15 #include <mutex> 16 17 #include "lldb/Breakpoint/BreakpointLocation.h" 18 #include "lldb/Breakpoint/BreakpointSite.h" 19 #include "lldb/Core/Debugger.h" 20 #include "lldb/Core/Module.h" 21 #include "lldb/Core/ModuleSpec.h" 22 #include "lldb/Core/Section.h" 23 #include "lldb/Host/Host.h" 24 #include "lldb/Host/HostInfo.h" 25 #include "lldb/Host/XML.h" 26 #include "lldb/Interpreter/CommandInterpreter.h" 27 #include "lldb/Symbol/LocateSymbolFile.h" 28 #include "lldb/Symbol/ObjectFile.h" 29 #include "lldb/Symbol/SymbolFile.h" 30 #include "lldb/Symbol/SymbolVendor.h" 31 #include "lldb/Target/Platform.h" 32 #include "lldb/Target/Process.h" 33 #include "lldb/Target/Target.h" 34 #include "lldb/Utility/Log.h" 35 #include "lldb/Utility/ProcessInfo.h" 36 #include "lldb/Utility/Status.h" 37 #include "lldb/Utility/Timer.h" 38 #include "llvm/ADT/STLExtras.h" 39 #include "llvm/Support/FileSystem.h" 40 #include "llvm/Support/Threading.h" 41 #include "llvm/Support/VersionTuple.h" 42 43 #if defined(__APPLE__) 44 #include <TargetConditionals.h> 45 #endif 46 47 using namespace lldb; 48 using namespace lldb_private; 49 50 /// Default Constructor 51 PlatformDarwin::PlatformDarwin(bool is_host) : PlatformPOSIX(is_host) {} 52 53 /// Destructor. 54 /// 55 /// The destructor is virtual since this class is designed to be 56 /// inherited from by the plug-in instance. 57 PlatformDarwin::~PlatformDarwin() {} 58 59 lldb_private::Status 60 PlatformDarwin::PutFile(const lldb_private::FileSpec &source, 61 const lldb_private::FileSpec &destination, uint32_t uid, 62 uint32_t gid) { 63 // Unconditionally unlink the destination. If it is an executable, 64 // simply opening it and truncating its contents would invalidate 65 // its cached code signature. 66 Unlink(destination); 67 return PlatformPOSIX::PutFile(source, destination, uid, gid); 68 } 69 70 FileSpecList PlatformDarwin::LocateExecutableScriptingResources( 71 Target *target, Module &module, Stream *feedback_stream) { 72 FileSpecList file_list; 73 if (target && 74 target->GetDebugger().GetScriptLanguage() == eScriptLanguagePython) { 75 // NB some extensions might be meaningful and should not be stripped - 76 // "this.binary.file" 77 // should not lose ".file" but GetFileNameStrippingExtension() will do 78 // precisely that. Ideally, we should have a per-platform list of 79 // extensions (".exe", ".app", ".dSYM", ".framework") which should be 80 // stripped while leaving "this.binary.file" as-is. 81 82 FileSpec module_spec = module.GetFileSpec(); 83 84 if (module_spec) { 85 if (SymbolFile *symfile = module.GetSymbolFile()) { 86 ObjectFile *objfile = symfile->GetObjectFile(); 87 if (objfile) { 88 FileSpec symfile_spec(objfile->GetFileSpec()); 89 if (symfile_spec && 90 strcasestr(symfile_spec.GetPath().c_str(), 91 ".dSYM/Contents/Resources/DWARF") != nullptr && 92 FileSystem::Instance().Exists(symfile_spec)) { 93 while (module_spec.GetFilename()) { 94 std::string module_basename( 95 module_spec.GetFilename().GetCString()); 96 std::string original_module_basename(module_basename); 97 98 bool was_keyword = false; 99 100 // FIXME: for Python, we cannot allow certain characters in 101 // module 102 // filenames we import. Theoretically, different scripting 103 // languages may have different sets of forbidden tokens in 104 // filenames, and that should be dealt with by each 105 // ScriptInterpreter. For now, we just replace dots with 106 // underscores, but if we ever support anything other than 107 // Python we will need to rework this 108 std::replace(module_basename.begin(), module_basename.end(), '.', 109 '_'); 110 std::replace(module_basename.begin(), module_basename.end(), ' ', 111 '_'); 112 std::replace(module_basename.begin(), module_basename.end(), '-', 113 '_'); 114 ScriptInterpreter *script_interpreter = 115 target->GetDebugger().GetScriptInterpreter(); 116 if (script_interpreter && 117 script_interpreter->IsReservedWord(module_basename.c_str())) { 118 module_basename.insert(module_basename.begin(), '_'); 119 was_keyword = true; 120 } 121 122 StreamString path_string; 123 StreamString original_path_string; 124 // for OSX we are going to be in 125 // .dSYM/Contents/Resources/DWARF/<basename> let us go to 126 // .dSYM/Contents/Resources/Python/<basename>.py and see if the 127 // file exists 128 path_string.Printf("%s/../Python/%s.py", 129 symfile_spec.GetDirectory().GetCString(), 130 module_basename.c_str()); 131 original_path_string.Printf( 132 "%s/../Python/%s.py", 133 symfile_spec.GetDirectory().GetCString(), 134 original_module_basename.c_str()); 135 FileSpec script_fspec(path_string.GetString()); 136 FileSystem::Instance().Resolve(script_fspec); 137 FileSpec orig_script_fspec(original_path_string.GetString()); 138 FileSystem::Instance().Resolve(orig_script_fspec); 139 140 // if we did some replacements of reserved characters, and a 141 // file with the untampered name exists, then warn the user 142 // that the file as-is shall not be loaded 143 if (feedback_stream) { 144 if (module_basename != original_module_basename && 145 FileSystem::Instance().Exists(orig_script_fspec)) { 146 const char *reason_for_complaint = 147 was_keyword ? "conflicts with a keyword" 148 : "contains reserved characters"; 149 if (FileSystem::Instance().Exists(script_fspec)) 150 feedback_stream->Printf( 151 "warning: the symbol file '%s' contains a debug " 152 "script. However, its name" 153 " '%s' %s and as such cannot be loaded. LLDB will" 154 " load '%s' instead. Consider removing the file with " 155 "the malformed name to" 156 " eliminate this warning.\n", 157 symfile_spec.GetPath().c_str(), 158 original_path_string.GetData(), reason_for_complaint, 159 path_string.GetData()); 160 else 161 feedback_stream->Printf( 162 "warning: the symbol file '%s' contains a debug " 163 "script. However, its name" 164 " %s and as such cannot be loaded. If you intend" 165 " to have this script loaded, please rename '%s' to " 166 "'%s' and retry.\n", 167 symfile_spec.GetPath().c_str(), reason_for_complaint, 168 original_path_string.GetData(), path_string.GetData()); 169 } 170 } 171 172 if (FileSystem::Instance().Exists(script_fspec)) { 173 file_list.Append(script_fspec); 174 break; 175 } 176 177 // If we didn't find the python file, then keep stripping the 178 // extensions and try again 179 ConstString filename_no_extension( 180 module_spec.GetFileNameStrippingExtension()); 181 if (module_spec.GetFilename() == filename_no_extension) 182 break; 183 184 module_spec.GetFilename() = filename_no_extension; 185 } 186 } 187 } 188 } 189 } 190 } 191 return file_list; 192 } 193 194 Status PlatformDarwin::ResolveSymbolFile(Target &target, 195 const ModuleSpec &sym_spec, 196 FileSpec &sym_file) { 197 sym_file = sym_spec.GetSymbolFileSpec(); 198 if (FileSystem::Instance().IsDirectory(sym_file)) { 199 sym_file = Symbols::FindSymbolFileInBundle(sym_file, sym_spec.GetUUIDPtr(), 200 sym_spec.GetArchitecturePtr()); 201 } 202 return {}; 203 } 204 205 static lldb_private::Status 206 MakeCacheFolderForFile(const FileSpec &module_cache_spec) { 207 FileSpec module_cache_folder = 208 module_cache_spec.CopyByRemovingLastPathComponent(); 209 return llvm::sys::fs::create_directory(module_cache_folder.GetPath()); 210 } 211 212 static lldb_private::Status 213 BringInRemoteFile(Platform *platform, 214 const lldb_private::ModuleSpec &module_spec, 215 const FileSpec &module_cache_spec) { 216 MakeCacheFolderForFile(module_cache_spec); 217 Status err = platform->GetFile(module_spec.GetFileSpec(), module_cache_spec); 218 return err; 219 } 220 221 lldb_private::Status PlatformDarwin::GetSharedModuleWithLocalCache( 222 const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp, 223 const lldb_private::FileSpecList *module_search_paths_ptr, 224 lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr) { 225 226 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); 227 LLDB_LOGF(log, 228 "[%s] Trying to find module %s/%s - platform path %s/%s symbol " 229 "path %s/%s", 230 (IsHost() ? "host" : "remote"), 231 module_spec.GetFileSpec().GetDirectory().AsCString(), 232 module_spec.GetFileSpec().GetFilename().AsCString(), 233 module_spec.GetPlatformFileSpec().GetDirectory().AsCString(), 234 module_spec.GetPlatformFileSpec().GetFilename().AsCString(), 235 module_spec.GetSymbolFileSpec().GetDirectory().AsCString(), 236 module_spec.GetSymbolFileSpec().GetFilename().AsCString()); 237 238 Status err; 239 240 if (IsHost()) { 241 // When debugging on the host, we are most likely using the same shared 242 // cache as our inferior. The dylibs from the shared cache might not 243 // exist on the filesystem, so let's use the images in our own memory 244 // to create the modules. 245 246 // Check if the requested image is in our shared cache. 247 SharedCacheImageInfo image_info = 248 HostInfo::GetSharedCacheImageInfo(module_spec.GetFileSpec().GetPath()); 249 250 // If we found it and it has the correct UUID, let's proceed with 251 // creating a module from the memory contents. 252 if (image_info.uuid && 253 (!module_spec.GetUUID() || module_spec.GetUUID() == image_info.uuid)) { 254 ModuleSpec shared_cache_spec(module_spec.GetFileSpec(), image_info.uuid, 255 image_info.data_sp); 256 err = ModuleList::GetSharedModule(shared_cache_spec, module_sp, 257 module_search_paths_ptr, 258 old_module_sp_ptr, did_create_ptr); 259 if (module_sp) 260 return err; 261 } 262 } 263 264 err = ModuleList::GetSharedModule(module_spec, module_sp, 265 module_search_paths_ptr, old_module_sp_ptr, 266 did_create_ptr); 267 if (module_sp) 268 return err; 269 270 if (!IsHost()) { 271 std::string cache_path(GetLocalCacheDirectory()); 272 // Only search for a locally cached file if we have a valid cache path 273 if (!cache_path.empty()) { 274 std::string module_path(module_spec.GetFileSpec().GetPath()); 275 cache_path.append(module_path); 276 FileSpec module_cache_spec(cache_path); 277 278 // if rsync is supported, always bring in the file - rsync will be very 279 // efficient when files are the same on the local and remote end of the 280 // connection 281 if (this->GetSupportsRSync()) { 282 err = BringInRemoteFile(this, module_spec, module_cache_spec); 283 if (err.Fail()) 284 return err; 285 if (FileSystem::Instance().Exists(module_cache_spec)) { 286 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); 287 LLDB_LOGF(log, "[%s] module %s/%s was rsynced and is now there", 288 (IsHost() ? "host" : "remote"), 289 module_spec.GetFileSpec().GetDirectory().AsCString(), 290 module_spec.GetFileSpec().GetFilename().AsCString()); 291 ModuleSpec local_spec(module_cache_spec, 292 module_spec.GetArchitecture()); 293 module_sp = std::make_shared<Module>(local_spec); 294 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 295 return Status(); 296 } 297 } 298 299 // try to find the module in the cache 300 if (FileSystem::Instance().Exists(module_cache_spec)) { 301 // get the local and remote MD5 and compare 302 if (m_remote_platform_sp) { 303 // when going over the *slow* GDB remote transfer mechanism we first 304 // check the hashes of the files - and only do the actual transfer if 305 // they differ 306 uint64_t high_local, high_remote, low_local, low_remote; 307 auto MD5 = llvm::sys::fs::md5_contents(module_cache_spec.GetPath()); 308 if (!MD5) 309 return Status(MD5.getError()); 310 std::tie(high_local, low_local) = MD5->words(); 311 312 m_remote_platform_sp->CalculateMD5(module_spec.GetFileSpec(), 313 low_remote, high_remote); 314 if (low_local != low_remote || high_local != high_remote) { 315 // bring in the remote file 316 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); 317 LLDB_LOGF(log, 318 "[%s] module %s/%s needs to be replaced from remote copy", 319 (IsHost() ? "host" : "remote"), 320 module_spec.GetFileSpec().GetDirectory().AsCString(), 321 module_spec.GetFileSpec().GetFilename().AsCString()); 322 Status err = 323 BringInRemoteFile(this, module_spec, module_cache_spec); 324 if (err.Fail()) 325 return err; 326 } 327 } 328 329 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture()); 330 module_sp = std::make_shared<Module>(local_spec); 331 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 332 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); 333 LLDB_LOGF(log, "[%s] module %s/%s was found in the cache", 334 (IsHost() ? "host" : "remote"), 335 module_spec.GetFileSpec().GetDirectory().AsCString(), 336 module_spec.GetFileSpec().GetFilename().AsCString()); 337 return Status(); 338 } 339 340 // bring in the remote module file 341 LLDB_LOGF(log, "[%s] module %s/%s needs to come in remotely", 342 (IsHost() ? "host" : "remote"), 343 module_spec.GetFileSpec().GetDirectory().AsCString(), 344 module_spec.GetFileSpec().GetFilename().AsCString()); 345 Status err = BringInRemoteFile(this, module_spec, module_cache_spec); 346 if (err.Fail()) 347 return err; 348 if (FileSystem::Instance().Exists(module_cache_spec)) { 349 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); 350 LLDB_LOGF(log, "[%s] module %s/%s is now cached and fine", 351 (IsHost() ? "host" : "remote"), 352 module_spec.GetFileSpec().GetDirectory().AsCString(), 353 module_spec.GetFileSpec().GetFilename().AsCString()); 354 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture()); 355 module_sp = std::make_shared<Module>(local_spec); 356 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 357 return Status(); 358 } else 359 return Status("unable to obtain valid module file"); 360 } else 361 return Status("no cache path"); 362 } else 363 return Status("unable to resolve module"); 364 } 365 366 Status PlatformDarwin::GetSharedModule( 367 const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp, 368 const FileSpecList *module_search_paths_ptr, ModuleSP *old_module_sp_ptr, 369 bool *did_create_ptr) { 370 Status error; 371 module_sp.reset(); 372 373 if (IsRemote()) { 374 // If we have a remote platform always, let it try and locate the shared 375 // module first. 376 if (m_remote_platform_sp) { 377 error = m_remote_platform_sp->GetSharedModule( 378 module_spec, process, module_sp, module_search_paths_ptr, 379 old_module_sp_ptr, did_create_ptr); 380 } 381 } 382 383 if (!module_sp) { 384 // Fall back to the local platform and find the file locally 385 error = Platform::GetSharedModule(module_spec, process, module_sp, 386 module_search_paths_ptr, 387 old_module_sp_ptr, did_create_ptr); 388 389 const FileSpec &platform_file = module_spec.GetFileSpec(); 390 if (!module_sp && module_search_paths_ptr && platform_file) { 391 // We can try to pull off part of the file path up to the bundle 392 // directory level and try any module search paths... 393 FileSpec bundle_directory; 394 if (Host::GetBundleDirectory(platform_file, bundle_directory)) { 395 if (platform_file == bundle_directory) { 396 ModuleSpec new_module_spec(module_spec); 397 new_module_spec.GetFileSpec() = bundle_directory; 398 if (Host::ResolveExecutableInBundle(new_module_spec.GetFileSpec())) { 399 Status new_error(Platform::GetSharedModule( 400 new_module_spec, process, module_sp, nullptr, old_module_sp_ptr, 401 did_create_ptr)); 402 403 if (module_sp) 404 return new_error; 405 } 406 } else { 407 char platform_path[PATH_MAX]; 408 char bundle_dir[PATH_MAX]; 409 platform_file.GetPath(platform_path, sizeof(platform_path)); 410 const size_t bundle_directory_len = 411 bundle_directory.GetPath(bundle_dir, sizeof(bundle_dir)); 412 char new_path[PATH_MAX]; 413 size_t num_module_search_paths = module_search_paths_ptr->GetSize(); 414 for (size_t i = 0; i < num_module_search_paths; ++i) { 415 const size_t search_path_len = 416 module_search_paths_ptr->GetFileSpecAtIndex(i).GetPath( 417 new_path, sizeof(new_path)); 418 if (search_path_len < sizeof(new_path)) { 419 snprintf(new_path + search_path_len, 420 sizeof(new_path) - search_path_len, "/%s", 421 platform_path + bundle_directory_len); 422 FileSpec new_file_spec(new_path); 423 if (FileSystem::Instance().Exists(new_file_spec)) { 424 ModuleSpec new_module_spec(module_spec); 425 new_module_spec.GetFileSpec() = new_file_spec; 426 Status new_error(Platform::GetSharedModule( 427 new_module_spec, process, module_sp, nullptr, 428 old_module_sp_ptr, did_create_ptr)); 429 430 if (module_sp) { 431 module_sp->SetPlatformFileSpec(new_file_spec); 432 return new_error; 433 } 434 } 435 } 436 } 437 } 438 } 439 } 440 } 441 if (module_sp) 442 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 443 return error; 444 } 445 446 size_t 447 PlatformDarwin::GetSoftwareBreakpointTrapOpcode(Target &target, 448 BreakpointSite *bp_site) { 449 const uint8_t *trap_opcode = nullptr; 450 uint32_t trap_opcode_size = 0; 451 bool bp_is_thumb = false; 452 453 llvm::Triple::ArchType machine = target.GetArchitecture().GetMachine(); 454 switch (machine) { 455 case llvm::Triple::aarch64_32: 456 case llvm::Triple::aarch64: { 457 // 'brk #0' or 0xd4200000 in BE byte order 458 static const uint8_t g_arm64_breakpoint_opcode[] = {0x00, 0x00, 0x20, 0xD4}; 459 trap_opcode = g_arm64_breakpoint_opcode; 460 trap_opcode_size = sizeof(g_arm64_breakpoint_opcode); 461 } break; 462 463 case llvm::Triple::thumb: 464 bp_is_thumb = true; 465 LLVM_FALLTHROUGH; 466 case llvm::Triple::arm: { 467 static const uint8_t g_arm_breakpoint_opcode[] = {0xFE, 0xDE, 0xFF, 0xE7}; 468 static const uint8_t g_thumb_breakpooint_opcode[] = {0xFE, 0xDE}; 469 470 // Auto detect arm/thumb if it wasn't explicitly specified 471 if (!bp_is_thumb) { 472 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0)); 473 if (bp_loc_sp) 474 bp_is_thumb = bp_loc_sp->GetAddress().GetAddressClass() == 475 AddressClass::eCodeAlternateISA; 476 } 477 if (bp_is_thumb) { 478 trap_opcode = g_thumb_breakpooint_opcode; 479 trap_opcode_size = sizeof(g_thumb_breakpooint_opcode); 480 break; 481 } 482 trap_opcode = g_arm_breakpoint_opcode; 483 trap_opcode_size = sizeof(g_arm_breakpoint_opcode); 484 } break; 485 486 case llvm::Triple::ppc: 487 case llvm::Triple::ppc64: { 488 static const uint8_t g_ppc_breakpoint_opcode[] = {0x7F, 0xC0, 0x00, 0x08}; 489 trap_opcode = g_ppc_breakpoint_opcode; 490 trap_opcode_size = sizeof(g_ppc_breakpoint_opcode); 491 } break; 492 493 default: 494 return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site); 495 } 496 497 if (trap_opcode && trap_opcode_size) { 498 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 499 return trap_opcode_size; 500 } 501 return 0; 502 } 503 504 bool PlatformDarwin::ModuleIsExcludedForUnconstrainedSearches( 505 lldb_private::Target &target, const lldb::ModuleSP &module_sp) { 506 if (!module_sp) 507 return false; 508 509 ObjectFile *obj_file = module_sp->GetObjectFile(); 510 if (!obj_file) 511 return false; 512 513 ObjectFile::Type obj_type = obj_file->GetType(); 514 return obj_type == ObjectFile::eTypeDynamicLinker; 515 } 516 517 bool PlatformDarwin::x86GetSupportedArchitectureAtIndex(uint32_t idx, 518 ArchSpec &arch) { 519 ArchSpec host_arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 520 if (host_arch.GetCore() == ArchSpec::eCore_x86_64_x86_64h) { 521 switch (idx) { 522 case 0: 523 arch = host_arch; 524 return true; 525 526 case 1: 527 arch.SetTriple("x86_64-apple-macosx"); 528 return true; 529 530 case 2: 531 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 532 return true; 533 534 default: 535 return false; 536 } 537 } else { 538 if (idx == 0) { 539 arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 540 return arch.IsValid(); 541 } else if (idx == 1) { 542 ArchSpec platform_arch( 543 HostInfo::GetArchitecture(HostInfo::eArchKindDefault)); 544 ArchSpec platform_arch64( 545 HostInfo::GetArchitecture(HostInfo::eArchKind64)); 546 if (platform_arch.IsExactMatch(platform_arch64)) { 547 // This macosx platform supports both 32 and 64 bit. Since we already 548 // returned the 64 bit arch for idx == 0, return the 32 bit arch for 549 // idx == 1 550 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 551 return arch.IsValid(); 552 } 553 } 554 } 555 return false; 556 } 557 558 // The architecture selection rules for arm processors These cpu subtypes have 559 // distinct names (e.g. armv7f) but armv7 binaries run fine on an armv7f 560 // processor. 561 562 bool PlatformDarwin::ARMGetSupportedArchitectureAtIndex(uint32_t idx, 563 ArchSpec &arch) { 564 ArchSpec system_arch(GetSystemArchitecture()); 565 566 // When lldb is running on a watch or tv, set the arch OS name appropriately. 567 #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1 568 #define OSNAME "tvos" 569 #elif defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1 570 #define OSNAME "watchos" 571 #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1 572 #define OSNAME "bridgeos" 573 #else 574 #define OSNAME "ios" 575 #endif 576 577 #if TARGET_OS_OSX 578 if (IsHost()) { 579 if (idx == 0) { 580 arch.SetTriple("arm64e-apple-macosx"); 581 return true; 582 } else if (idx == 1) { 583 arch.SetTriple("arm64-apple-macosx"); 584 return true; 585 } 586 return false; 587 } 588 #endif 589 590 const ArchSpec::Core system_core = system_arch.GetCore(); 591 switch (system_core) { 592 default: 593 switch (idx) { 594 case 0: 595 arch.SetTriple("arm64-apple-" OSNAME); 596 return true; 597 case 1: 598 arch.SetTriple("armv7-apple-" OSNAME); 599 return true; 600 case 2: 601 arch.SetTriple("armv7f-apple-" OSNAME); 602 return true; 603 case 3: 604 arch.SetTriple("armv7k-apple-" OSNAME); 605 return true; 606 case 4: 607 arch.SetTriple("armv7s-apple-" OSNAME); 608 return true; 609 case 5: 610 arch.SetTriple("armv7m-apple-" OSNAME); 611 return true; 612 case 6: 613 arch.SetTriple("armv7em-apple-" OSNAME); 614 return true; 615 case 7: 616 arch.SetTriple("armv6m-apple-" OSNAME); 617 return true; 618 case 8: 619 arch.SetTriple("armv6-apple-" OSNAME); 620 return true; 621 case 9: 622 arch.SetTriple("armv5-apple-" OSNAME); 623 return true; 624 case 10: 625 arch.SetTriple("armv4-apple-" OSNAME); 626 return true; 627 case 11: 628 arch.SetTriple("arm-apple-" OSNAME); 629 return true; 630 case 12: 631 arch.SetTriple("thumbv7-apple-" OSNAME); 632 return true; 633 case 13: 634 arch.SetTriple("thumbv7f-apple-" OSNAME); 635 return true; 636 case 14: 637 arch.SetTriple("thumbv7k-apple-" OSNAME); 638 return true; 639 case 15: 640 arch.SetTriple("thumbv7s-apple-" OSNAME); 641 return true; 642 case 16: 643 arch.SetTriple("thumbv7m-apple-" OSNAME); 644 return true; 645 case 17: 646 arch.SetTriple("thumbv7em-apple-" OSNAME); 647 return true; 648 case 18: 649 arch.SetTriple("thumbv6m-apple-" OSNAME); 650 return true; 651 case 19: 652 arch.SetTriple("thumbv6-apple-" OSNAME); 653 return true; 654 case 20: 655 arch.SetTriple("thumbv5-apple-" OSNAME); 656 return true; 657 case 21: 658 arch.SetTriple("thumbv4t-apple-" OSNAME); 659 return true; 660 case 22: 661 arch.SetTriple("thumb-apple-" OSNAME); 662 return true; 663 default: 664 break; 665 } 666 break; 667 668 case ArchSpec::eCore_arm_arm64: 669 switch (idx) { 670 case 0: 671 arch.SetTriple("arm64-apple-" OSNAME); 672 return true; 673 case 1: 674 arch.SetTriple("armv7s-apple-" OSNAME); 675 return true; 676 case 2: 677 arch.SetTriple("armv7f-apple-" OSNAME); 678 return true; 679 case 3: 680 arch.SetTriple("armv7m-apple-" OSNAME); 681 return true; 682 case 4: 683 arch.SetTriple("armv7em-apple-" OSNAME); 684 return true; 685 case 5: 686 arch.SetTriple("armv7-apple-" OSNAME); 687 return true; 688 case 6: 689 arch.SetTriple("armv6m-apple-" OSNAME); 690 return true; 691 case 7: 692 arch.SetTriple("armv6-apple-" OSNAME); 693 return true; 694 case 8: 695 arch.SetTriple("armv5-apple-" OSNAME); 696 return true; 697 case 9: 698 arch.SetTriple("armv4-apple-" OSNAME); 699 return true; 700 case 10: 701 arch.SetTriple("arm-apple-" OSNAME); 702 return true; 703 case 11: 704 arch.SetTriple("thumbv7-apple-" OSNAME); 705 return true; 706 case 12: 707 arch.SetTriple("thumbv7f-apple-" OSNAME); 708 return true; 709 case 13: 710 arch.SetTriple("thumbv7k-apple-" OSNAME); 711 return true; 712 case 14: 713 arch.SetTriple("thumbv7s-apple-" OSNAME); 714 return true; 715 case 15: 716 arch.SetTriple("thumbv7m-apple-" OSNAME); 717 return true; 718 case 16: 719 arch.SetTriple("thumbv7em-apple-" OSNAME); 720 return true; 721 case 17: 722 arch.SetTriple("thumbv6m-apple-" OSNAME); 723 return true; 724 case 18: 725 arch.SetTriple("thumbv6-apple-" OSNAME); 726 return true; 727 case 19: 728 arch.SetTriple("thumbv5-apple-" OSNAME); 729 return true; 730 case 20: 731 arch.SetTriple("thumbv4t-apple-" OSNAME); 732 return true; 733 case 21: 734 arch.SetTriple("thumb-apple-" OSNAME); 735 return true; 736 default: 737 break; 738 } 739 break; 740 741 case ArchSpec::eCore_arm_armv7f: 742 switch (idx) { 743 case 0: 744 arch.SetTriple("armv7f-apple-" OSNAME); 745 return true; 746 case 1: 747 arch.SetTriple("armv7-apple-" OSNAME); 748 return true; 749 case 2: 750 arch.SetTriple("armv6m-apple-" OSNAME); 751 return true; 752 case 3: 753 arch.SetTriple("armv6-apple-" OSNAME); 754 return true; 755 case 4: 756 arch.SetTriple("armv5-apple-" OSNAME); 757 return true; 758 case 5: 759 arch.SetTriple("armv4-apple-" OSNAME); 760 return true; 761 case 6: 762 arch.SetTriple("arm-apple-" OSNAME); 763 return true; 764 case 7: 765 arch.SetTriple("thumbv7f-apple-" OSNAME); 766 return true; 767 case 8: 768 arch.SetTriple("thumbv7-apple-" OSNAME); 769 return true; 770 case 9: 771 arch.SetTriple("thumbv6m-apple-" OSNAME); 772 return true; 773 case 10: 774 arch.SetTriple("thumbv6-apple-" OSNAME); 775 return true; 776 case 11: 777 arch.SetTriple("thumbv5-apple-" OSNAME); 778 return true; 779 case 12: 780 arch.SetTriple("thumbv4t-apple-" OSNAME); 781 return true; 782 case 13: 783 arch.SetTriple("thumb-apple-" OSNAME); 784 return true; 785 default: 786 break; 787 } 788 break; 789 790 case ArchSpec::eCore_arm_armv7k: 791 switch (idx) { 792 case 0: 793 arch.SetTriple("armv7k-apple-" OSNAME); 794 return true; 795 case 1: 796 arch.SetTriple("armv7-apple-" OSNAME); 797 return true; 798 case 2: 799 arch.SetTriple("armv6m-apple-" OSNAME); 800 return true; 801 case 3: 802 arch.SetTriple("armv6-apple-" OSNAME); 803 return true; 804 case 4: 805 arch.SetTriple("armv5-apple-" OSNAME); 806 return true; 807 case 5: 808 arch.SetTriple("armv4-apple-" OSNAME); 809 return true; 810 case 6: 811 arch.SetTriple("arm-apple-" OSNAME); 812 return true; 813 case 7: 814 arch.SetTriple("thumbv7k-apple-" OSNAME); 815 return true; 816 case 8: 817 arch.SetTriple("thumbv7-apple-" OSNAME); 818 return true; 819 case 9: 820 arch.SetTriple("thumbv6m-apple-" OSNAME); 821 return true; 822 case 10: 823 arch.SetTriple("thumbv6-apple-" OSNAME); 824 return true; 825 case 11: 826 arch.SetTriple("thumbv5-apple-" OSNAME); 827 return true; 828 case 12: 829 arch.SetTriple("thumbv4t-apple-" OSNAME); 830 return true; 831 case 13: 832 arch.SetTriple("thumb-apple-" OSNAME); 833 return true; 834 default: 835 break; 836 } 837 break; 838 839 case ArchSpec::eCore_arm_armv7s: 840 switch (idx) { 841 case 0: 842 arch.SetTriple("armv7s-apple-" OSNAME); 843 return true; 844 case 1: 845 arch.SetTriple("armv7-apple-" OSNAME); 846 return true; 847 case 2: 848 arch.SetTriple("armv6m-apple-" OSNAME); 849 return true; 850 case 3: 851 arch.SetTriple("armv6-apple-" OSNAME); 852 return true; 853 case 4: 854 arch.SetTriple("armv5-apple-" OSNAME); 855 return true; 856 case 5: 857 arch.SetTriple("armv4-apple-" OSNAME); 858 return true; 859 case 6: 860 arch.SetTriple("arm-apple-" OSNAME); 861 return true; 862 case 7: 863 arch.SetTriple("thumbv7s-apple-" OSNAME); 864 return true; 865 case 8: 866 arch.SetTriple("thumbv7-apple-" OSNAME); 867 return true; 868 case 9: 869 arch.SetTriple("thumbv6m-apple-" OSNAME); 870 return true; 871 case 10: 872 arch.SetTriple("thumbv6-apple-" OSNAME); 873 return true; 874 case 11: 875 arch.SetTriple("thumbv5-apple-" OSNAME); 876 return true; 877 case 12: 878 arch.SetTriple("thumbv4t-apple-" OSNAME); 879 return true; 880 case 13: 881 arch.SetTriple("thumb-apple-" OSNAME); 882 return true; 883 default: 884 break; 885 } 886 break; 887 888 case ArchSpec::eCore_arm_armv7m: 889 switch (idx) { 890 case 0: 891 arch.SetTriple("armv7m-apple-" OSNAME); 892 return true; 893 case 1: 894 arch.SetTriple("armv7-apple-" OSNAME); 895 return true; 896 case 2: 897 arch.SetTriple("armv6m-apple-" OSNAME); 898 return true; 899 case 3: 900 arch.SetTriple("armv6-apple-" OSNAME); 901 return true; 902 case 4: 903 arch.SetTriple("armv5-apple-" OSNAME); 904 return true; 905 case 5: 906 arch.SetTriple("armv4-apple-" OSNAME); 907 return true; 908 case 6: 909 arch.SetTriple("arm-apple-" OSNAME); 910 return true; 911 case 7: 912 arch.SetTriple("thumbv7m-apple-" OSNAME); 913 return true; 914 case 8: 915 arch.SetTriple("thumbv7-apple-" OSNAME); 916 return true; 917 case 9: 918 arch.SetTriple("thumbv6m-apple-" OSNAME); 919 return true; 920 case 10: 921 arch.SetTriple("thumbv6-apple-" OSNAME); 922 return true; 923 case 11: 924 arch.SetTriple("thumbv5-apple-" OSNAME); 925 return true; 926 case 12: 927 arch.SetTriple("thumbv4t-apple-" OSNAME); 928 return true; 929 case 13: 930 arch.SetTriple("thumb-apple-" OSNAME); 931 return true; 932 default: 933 break; 934 } 935 break; 936 937 case ArchSpec::eCore_arm_armv7em: 938 switch (idx) { 939 case 0: 940 arch.SetTriple("armv7em-apple-" OSNAME); 941 return true; 942 case 1: 943 arch.SetTriple("armv7-apple-" OSNAME); 944 return true; 945 case 2: 946 arch.SetTriple("armv6m-apple-" OSNAME); 947 return true; 948 case 3: 949 arch.SetTriple("armv6-apple-" OSNAME); 950 return true; 951 case 4: 952 arch.SetTriple("armv5-apple-" OSNAME); 953 return true; 954 case 5: 955 arch.SetTriple("armv4-apple-" OSNAME); 956 return true; 957 case 6: 958 arch.SetTriple("arm-apple-" OSNAME); 959 return true; 960 case 7: 961 arch.SetTriple("thumbv7em-apple-" OSNAME); 962 return true; 963 case 8: 964 arch.SetTriple("thumbv7-apple-" OSNAME); 965 return true; 966 case 9: 967 arch.SetTriple("thumbv6m-apple-" OSNAME); 968 return true; 969 case 10: 970 arch.SetTriple("thumbv6-apple-" OSNAME); 971 return true; 972 case 11: 973 arch.SetTriple("thumbv5-apple-" OSNAME); 974 return true; 975 case 12: 976 arch.SetTriple("thumbv4t-apple-" OSNAME); 977 return true; 978 case 13: 979 arch.SetTriple("thumb-apple-" OSNAME); 980 return true; 981 default: 982 break; 983 } 984 break; 985 986 case ArchSpec::eCore_arm_armv7: 987 switch (idx) { 988 case 0: 989 arch.SetTriple("armv7-apple-" OSNAME); 990 return true; 991 case 1: 992 arch.SetTriple("armv6m-apple-" OSNAME); 993 return true; 994 case 2: 995 arch.SetTriple("armv6-apple-" OSNAME); 996 return true; 997 case 3: 998 arch.SetTriple("armv5-apple-" OSNAME); 999 return true; 1000 case 4: 1001 arch.SetTriple("armv4-apple-" OSNAME); 1002 return true; 1003 case 5: 1004 arch.SetTriple("arm-apple-" OSNAME); 1005 return true; 1006 case 6: 1007 arch.SetTriple("thumbv7-apple-" OSNAME); 1008 return true; 1009 case 7: 1010 arch.SetTriple("thumbv6m-apple-" OSNAME); 1011 return true; 1012 case 8: 1013 arch.SetTriple("thumbv6-apple-" OSNAME); 1014 return true; 1015 case 9: 1016 arch.SetTriple("thumbv5-apple-" OSNAME); 1017 return true; 1018 case 10: 1019 arch.SetTriple("thumbv4t-apple-" OSNAME); 1020 return true; 1021 case 11: 1022 arch.SetTriple("thumb-apple-" OSNAME); 1023 return true; 1024 default: 1025 break; 1026 } 1027 break; 1028 1029 case ArchSpec::eCore_arm_armv6m: 1030 switch (idx) { 1031 case 0: 1032 arch.SetTriple("armv6m-apple-" OSNAME); 1033 return true; 1034 case 1: 1035 arch.SetTriple("armv6-apple-" OSNAME); 1036 return true; 1037 case 2: 1038 arch.SetTriple("armv5-apple-" OSNAME); 1039 return true; 1040 case 3: 1041 arch.SetTriple("armv4-apple-" OSNAME); 1042 return true; 1043 case 4: 1044 arch.SetTriple("arm-apple-" OSNAME); 1045 return true; 1046 case 5: 1047 arch.SetTriple("thumbv6m-apple-" OSNAME); 1048 return true; 1049 case 6: 1050 arch.SetTriple("thumbv6-apple-" OSNAME); 1051 return true; 1052 case 7: 1053 arch.SetTriple("thumbv5-apple-" OSNAME); 1054 return true; 1055 case 8: 1056 arch.SetTriple("thumbv4t-apple-" OSNAME); 1057 return true; 1058 case 9: 1059 arch.SetTriple("thumb-apple-" OSNAME); 1060 return true; 1061 default: 1062 break; 1063 } 1064 break; 1065 1066 case ArchSpec::eCore_arm_armv6: 1067 switch (idx) { 1068 case 0: 1069 arch.SetTriple("armv6-apple-" OSNAME); 1070 return true; 1071 case 1: 1072 arch.SetTriple("armv5-apple-" OSNAME); 1073 return true; 1074 case 2: 1075 arch.SetTriple("armv4-apple-" OSNAME); 1076 return true; 1077 case 3: 1078 arch.SetTriple("arm-apple-" OSNAME); 1079 return true; 1080 case 4: 1081 arch.SetTriple("thumbv6-apple-" OSNAME); 1082 return true; 1083 case 5: 1084 arch.SetTriple("thumbv5-apple-" OSNAME); 1085 return true; 1086 case 6: 1087 arch.SetTriple("thumbv4t-apple-" OSNAME); 1088 return true; 1089 case 7: 1090 arch.SetTriple("thumb-apple-" OSNAME); 1091 return true; 1092 default: 1093 break; 1094 } 1095 break; 1096 1097 case ArchSpec::eCore_arm_armv5: 1098 switch (idx) { 1099 case 0: 1100 arch.SetTriple("armv5-apple-" OSNAME); 1101 return true; 1102 case 1: 1103 arch.SetTriple("armv4-apple-" OSNAME); 1104 return true; 1105 case 2: 1106 arch.SetTriple("arm-apple-" OSNAME); 1107 return true; 1108 case 3: 1109 arch.SetTriple("thumbv5-apple-" OSNAME); 1110 return true; 1111 case 4: 1112 arch.SetTriple("thumbv4t-apple-" OSNAME); 1113 return true; 1114 case 5: 1115 arch.SetTriple("thumb-apple-" OSNAME); 1116 return true; 1117 default: 1118 break; 1119 } 1120 break; 1121 1122 case ArchSpec::eCore_arm_armv4: 1123 switch (idx) { 1124 case 0: 1125 arch.SetTriple("armv4-apple-" OSNAME); 1126 return true; 1127 case 1: 1128 arch.SetTriple("arm-apple-" OSNAME); 1129 return true; 1130 case 2: 1131 arch.SetTriple("thumbv4t-apple-" OSNAME); 1132 return true; 1133 case 3: 1134 arch.SetTriple("thumb-apple-" OSNAME); 1135 return true; 1136 default: 1137 break; 1138 } 1139 break; 1140 } 1141 arch.Clear(); 1142 return false; 1143 } 1144 1145 static FileSpec GetXcodeSelectPath() { 1146 static FileSpec g_xcode_select_filespec; 1147 1148 if (!g_xcode_select_filespec) { 1149 FileSpec xcode_select_cmd("/usr/bin/xcode-select"); 1150 if (FileSystem::Instance().Exists(xcode_select_cmd)) { 1151 int exit_status = -1; 1152 int signo = -1; 1153 std::string command_output; 1154 Status status = 1155 Host::RunShellCommand("/usr/bin/xcode-select --print-path", 1156 FileSpec(), // current working directory 1157 &exit_status, &signo, &command_output, 1158 std::chrono::seconds(2), // short timeout 1159 false); // don't run in a shell 1160 if (status.Success() && exit_status == 0 && !command_output.empty()) { 1161 size_t first_non_newline = command_output.find_last_not_of("\r\n"); 1162 if (first_non_newline != std::string::npos) { 1163 command_output.erase(first_non_newline + 1); 1164 } 1165 g_xcode_select_filespec = FileSpec(command_output); 1166 } 1167 } 1168 } 1169 1170 return g_xcode_select_filespec; 1171 } 1172 1173 BreakpointSP PlatformDarwin::SetThreadCreationBreakpoint(Target &target) { 1174 BreakpointSP bp_sp; 1175 static const char *g_bp_names[] = { 1176 "start_wqthread", "_pthread_wqthread", "_pthread_start", 1177 }; 1178 1179 static const char *g_bp_modules[] = {"libsystem_c.dylib", 1180 "libSystem.B.dylib"}; 1181 1182 FileSpecList bp_modules; 1183 for (size_t i = 0; i < llvm::array_lengthof(g_bp_modules); i++) { 1184 const char *bp_module = g_bp_modules[i]; 1185 bp_modules.EmplaceBack(bp_module); 1186 } 1187 1188 bool internal = true; 1189 bool hardware = false; 1190 LazyBool skip_prologue = eLazyBoolNo; 1191 bp_sp = target.CreateBreakpoint(&bp_modules, nullptr, g_bp_names, 1192 llvm::array_lengthof(g_bp_names), 1193 eFunctionNameTypeFull, eLanguageTypeUnknown, 1194 0, skip_prologue, internal, hardware); 1195 bp_sp->SetBreakpointKind("thread-creation"); 1196 1197 return bp_sp; 1198 } 1199 1200 int32_t 1201 PlatformDarwin::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { 1202 const FileSpec &shell = launch_info.GetShell(); 1203 if (!shell) 1204 return 1; 1205 1206 std::string shell_string = shell.GetPath(); 1207 const char *shell_name = strrchr(shell_string.c_str(), '/'); 1208 if (shell_name == nullptr) 1209 shell_name = shell_string.c_str(); 1210 else 1211 shell_name++; 1212 1213 if (strcmp(shell_name, "sh") == 0) { 1214 // /bin/sh re-exec's itself as /bin/bash requiring another resume. But it 1215 // only does this if the COMMAND_MODE environment variable is set to 1216 // "legacy". 1217 if (launch_info.GetEnvironment().lookup("COMMAND_MODE") == "legacy") 1218 return 2; 1219 return 1; 1220 } else if (strcmp(shell_name, "csh") == 0 || 1221 strcmp(shell_name, "tcsh") == 0 || 1222 strcmp(shell_name, "zsh") == 0) { 1223 // csh and tcsh always seem to re-exec themselves. 1224 return 2; 1225 } else 1226 return 1; 1227 } 1228 1229 void PlatformDarwin::CalculateTrapHandlerSymbolNames() { 1230 m_trap_handlers.push_back(ConstString("_sigtramp")); 1231 } 1232 1233 static FileSpec GetCommandLineToolsLibraryPath() { 1234 static FileSpec g_command_line_tools_filespec; 1235 1236 if (!g_command_line_tools_filespec) { 1237 FileSpec command_line_tools_path(GetXcodeSelectPath()); 1238 command_line_tools_path.AppendPathComponent("Library"); 1239 if (FileSystem::Instance().Exists(command_line_tools_path)) { 1240 g_command_line_tools_filespec = command_line_tools_path; 1241 } 1242 } 1243 1244 return g_command_line_tools_filespec; 1245 } 1246 1247 FileSystem::EnumerateDirectoryResult PlatformDarwin::DirectoryEnumerator( 1248 void *baton, llvm::sys::fs::file_type file_type, llvm::StringRef path) { 1249 SDKEnumeratorInfo *enumerator_info = static_cast<SDKEnumeratorInfo *>(baton); 1250 1251 FileSpec spec(path); 1252 if (XcodeSDK::SDKSupportsModules(enumerator_info->sdk_type, spec)) { 1253 enumerator_info->found_path = spec; 1254 return FileSystem::EnumerateDirectoryResult::eEnumerateDirectoryResultNext; 1255 } 1256 1257 return FileSystem::EnumerateDirectoryResult::eEnumerateDirectoryResultNext; 1258 } 1259 1260 FileSpec PlatformDarwin::FindSDKInXcodeForModules(XcodeSDK::Type sdk_type, 1261 const FileSpec &sdks_spec) { 1262 // Look inside Xcode for the required installed iOS SDK version 1263 1264 if (!FileSystem::Instance().IsDirectory(sdks_spec)) { 1265 return FileSpec(); 1266 } 1267 1268 const bool find_directories = true; 1269 const bool find_files = false; 1270 const bool find_other = true; // include symlinks 1271 1272 SDKEnumeratorInfo enumerator_info; 1273 1274 enumerator_info.sdk_type = sdk_type; 1275 1276 FileSystem::Instance().EnumerateDirectory( 1277 sdks_spec.GetPath(), find_directories, find_files, find_other, 1278 DirectoryEnumerator, &enumerator_info); 1279 1280 if (FileSystem::Instance().IsDirectory(enumerator_info.found_path)) 1281 return enumerator_info.found_path; 1282 else 1283 return FileSpec(); 1284 } 1285 1286 FileSpec PlatformDarwin::GetSDKDirectoryForModules(XcodeSDK::Type sdk_type) { 1287 FileSpec sdks_spec = HostInfo::GetXcodeContentsDirectory(); 1288 sdks_spec.AppendPathComponent("Developer"); 1289 sdks_spec.AppendPathComponent("Platforms"); 1290 1291 switch (sdk_type) { 1292 case XcodeSDK::Type::MacOSX: 1293 sdks_spec.AppendPathComponent("MacOSX.platform"); 1294 break; 1295 case XcodeSDK::Type::iPhoneSimulator: 1296 sdks_spec.AppendPathComponent("iPhoneSimulator.platform"); 1297 break; 1298 case XcodeSDK::Type::iPhoneOS: 1299 sdks_spec.AppendPathComponent("iPhoneOS.platform"); 1300 break; 1301 default: 1302 llvm_unreachable("unsupported sdk"); 1303 } 1304 1305 sdks_spec.AppendPathComponent("Developer"); 1306 sdks_spec.AppendPathComponent("SDKs"); 1307 1308 if (sdk_type == XcodeSDK::Type::MacOSX) { 1309 llvm::VersionTuple version = HostInfo::GetOSVersion(); 1310 1311 if (!version.empty()) { 1312 if (XcodeSDK::SDKSupportsModules(XcodeSDK::Type::MacOSX, version)) { 1313 // If the Xcode SDKs are not available then try to use the 1314 // Command Line Tools one which is only for MacOSX. 1315 if (!FileSystem::Instance().Exists(sdks_spec)) { 1316 sdks_spec = GetCommandLineToolsLibraryPath(); 1317 sdks_spec.AppendPathComponent("SDKs"); 1318 } 1319 1320 // We slightly prefer the exact SDK for this machine. See if it is 1321 // there. 1322 1323 FileSpec native_sdk_spec = sdks_spec; 1324 StreamString native_sdk_name; 1325 native_sdk_name.Printf("MacOSX%u.%u.sdk", version.getMajor(), 1326 version.getMinor().getValueOr(0)); 1327 native_sdk_spec.AppendPathComponent(native_sdk_name.GetString()); 1328 1329 if (FileSystem::Instance().Exists(native_sdk_spec)) { 1330 return native_sdk_spec; 1331 } 1332 } 1333 } 1334 } 1335 1336 return FindSDKInXcodeForModules(sdk_type, sdks_spec); 1337 } 1338 1339 std::tuple<llvm::VersionTuple, llvm::StringRef> 1340 PlatformDarwin::ParseVersionBuildDir(llvm::StringRef dir) { 1341 llvm::StringRef build; 1342 llvm::StringRef version_str; 1343 llvm::StringRef build_str; 1344 std::tie(version_str, build_str) = dir.split(' '); 1345 llvm::VersionTuple version; 1346 if (!version.tryParse(version_str) || 1347 build_str.empty()) { 1348 if (build_str.consume_front("(")) { 1349 size_t pos = build_str.find(')'); 1350 build = build_str.slice(0, pos); 1351 } 1352 } 1353 1354 return std::make_tuple(version, build); 1355 } 1356 1357 llvm::Expected<StructuredData::DictionarySP> 1358 PlatformDarwin::FetchExtendedCrashInformation(Process &process) { 1359 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1360 1361 StructuredData::ArraySP annotations = ExtractCrashInfoAnnotations(process); 1362 1363 if (!annotations || !annotations->GetSize()) { 1364 LLDB_LOG(log, "Couldn't extract crash information annotations"); 1365 return nullptr; 1366 } 1367 1368 StructuredData::DictionarySP extended_crash_info = 1369 std::make_shared<StructuredData::Dictionary>(); 1370 1371 extended_crash_info->AddItem("crash-info annotations", annotations); 1372 1373 return extended_crash_info; 1374 } 1375 1376 StructuredData::ArraySP 1377 PlatformDarwin::ExtractCrashInfoAnnotations(Process &process) { 1378 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1379 1380 ConstString section_name("__crash_info"); 1381 Target &target = process.GetTarget(); 1382 StructuredData::ArraySP array_sp = std::make_shared<StructuredData::Array>(); 1383 1384 for (ModuleSP module : target.GetImages().Modules()) { 1385 SectionList *sections = module->GetSectionList(); 1386 1387 std::string module_name = module->GetSpecificationDescription(); 1388 1389 // The DYDL module is skipped since it's always loaded when running the 1390 // binary. 1391 if (module_name == "/usr/lib/dyld") 1392 continue; 1393 1394 if (!sections) { 1395 LLDB_LOG(log, "Module {0} doesn't have any section!", module_name); 1396 continue; 1397 } 1398 1399 SectionSP crash_info = sections->FindSectionByName(section_name); 1400 if (!crash_info) { 1401 LLDB_LOG(log, "Module {0} doesn't have section {1}!", module_name, 1402 section_name); 1403 continue; 1404 } 1405 1406 addr_t load_addr = crash_info->GetLoadBaseAddress(&target); 1407 1408 if (load_addr == LLDB_INVALID_ADDRESS) { 1409 LLDB_LOG(log, "Module {0} has an invalid '{1}' section load address: {2}", 1410 module_name, section_name, load_addr); 1411 continue; 1412 } 1413 1414 Status error; 1415 CrashInfoAnnotations annotations; 1416 size_t expected_size = sizeof(CrashInfoAnnotations); 1417 size_t bytes_read = process.ReadMemoryFromInferior(load_addr, &annotations, 1418 expected_size, error); 1419 1420 if (expected_size != bytes_read || error.Fail()) { 1421 LLDB_LOG(log, "Failed to read {0} section from memory in module {1}: {2}", 1422 section_name, module_name, error); 1423 continue; 1424 } 1425 1426 // initial support added for version 5 1427 if (annotations.version < 5) { 1428 LLDB_LOG(log, 1429 "Annotation version lower than 5 unsupported! Module {0} has " 1430 "version {1} instead.", 1431 module_name, annotations.version); 1432 continue; 1433 } 1434 1435 if (!annotations.message) { 1436 LLDB_LOG(log, "No message available for module {0}.", module_name); 1437 continue; 1438 } 1439 1440 std::string message; 1441 bytes_read = 1442 process.ReadCStringFromMemory(annotations.message, message, error); 1443 1444 if (message.empty() || bytes_read != message.size() || error.Fail()) { 1445 LLDB_LOG(log, "Failed to read the message from memory in module {0}: {1}", 1446 module_name, error); 1447 continue; 1448 } 1449 1450 // Remove trailing newline from message 1451 if (message.back() == '\n') 1452 message.pop_back(); 1453 1454 if (!annotations.message2) 1455 LLDB_LOG(log, "No message2 available for module {0}.", module_name); 1456 1457 std::string message2; 1458 bytes_read = 1459 process.ReadCStringFromMemory(annotations.message2, message2, error); 1460 1461 if (!message2.empty() && bytes_read == message2.size() && error.Success()) 1462 if (message2.back() == '\n') 1463 message2.pop_back(); 1464 1465 StructuredData::DictionarySP entry_sp = 1466 std::make_shared<StructuredData::Dictionary>(); 1467 1468 entry_sp->AddStringItem("image", module->GetFileSpec().GetPath(false)); 1469 entry_sp->AddStringItem("uuid", module->GetUUID().GetAsString()); 1470 entry_sp->AddStringItem("message", message); 1471 entry_sp->AddStringItem("message2", message2); 1472 entry_sp->AddIntegerItem("abort-cause", annotations.abort_cause); 1473 1474 array_sp->AddItem(entry_sp); 1475 } 1476 1477 return array_sp; 1478 } 1479 1480 void PlatformDarwin::AddClangModuleCompilationOptionsForSDKType( 1481 Target *target, std::vector<std::string> &options, XcodeSDK::Type sdk_type) { 1482 const std::vector<std::string> apple_arguments = { 1483 "-x", "objective-c++", "-fobjc-arc", 1484 "-fblocks", "-D_ISO646_H", "-D__ISO646_H", 1485 "-fgnuc-version=4.2.1"}; 1486 1487 options.insert(options.end(), apple_arguments.begin(), apple_arguments.end()); 1488 1489 StreamString minimum_version_option; 1490 bool use_current_os_version = false; 1491 switch (sdk_type) { 1492 case XcodeSDK::Type::iPhoneOS: 1493 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 1494 use_current_os_version = true; 1495 #else 1496 use_current_os_version = false; 1497 #endif 1498 break; 1499 1500 case XcodeSDK::Type::iPhoneSimulator: 1501 use_current_os_version = false; 1502 break; 1503 1504 case XcodeSDK::Type::MacOSX: 1505 #if defined(__i386__) || defined(__x86_64__) 1506 use_current_os_version = true; 1507 #else 1508 use_current_os_version = false; 1509 #endif 1510 break; 1511 default: 1512 break; 1513 } 1514 1515 llvm::VersionTuple version; 1516 if (use_current_os_version) 1517 version = GetOSVersion(); 1518 else if (target) { 1519 // Our OS doesn't match our executable so we need to get the min OS version 1520 // from the object file 1521 ModuleSP exe_module_sp = target->GetExecutableModule(); 1522 if (exe_module_sp) { 1523 ObjectFile *object_file = exe_module_sp->GetObjectFile(); 1524 if (object_file) 1525 version = object_file->GetMinimumOSVersion(); 1526 } 1527 } 1528 // Only add the version-min options if we got a version from somewhere 1529 if (!version.empty()) { 1530 switch (sdk_type) { 1531 case XcodeSDK::Type::iPhoneOS: 1532 minimum_version_option.PutCString("-mios-version-min="); 1533 minimum_version_option.PutCString(version.getAsString()); 1534 break; 1535 case XcodeSDK::Type::iPhoneSimulator: 1536 minimum_version_option.PutCString("-mios-simulator-version-min="); 1537 minimum_version_option.PutCString(version.getAsString()); 1538 break; 1539 case XcodeSDK::Type::MacOSX: 1540 minimum_version_option.PutCString("-mmacosx-version-min="); 1541 minimum_version_option.PutCString(version.getAsString()); 1542 break; 1543 default: 1544 llvm_unreachable("unsupported sdk"); 1545 } 1546 options.push_back(std::string(minimum_version_option.GetString())); 1547 } 1548 1549 FileSpec sysroot_spec; 1550 // Scope for mutex locker below 1551 { 1552 std::lock_guard<std::mutex> guard(m_mutex); 1553 sysroot_spec = GetSDKDirectoryForModules(sdk_type); 1554 } 1555 1556 if (FileSystem::Instance().IsDirectory(sysroot_spec.GetPath())) { 1557 options.push_back("-isysroot"); 1558 options.push_back(sysroot_spec.GetPath()); 1559 } 1560 } 1561 1562 ConstString PlatformDarwin::GetFullNameForDylib(ConstString basename) { 1563 if (basename.IsEmpty()) 1564 return basename; 1565 1566 StreamString stream; 1567 stream.Printf("lib%s.dylib", basename.GetCString()); 1568 return ConstString(stream.GetString()); 1569 } 1570 1571 llvm::VersionTuple PlatformDarwin::GetOSVersion(Process *process) { 1572 if (process && strstr(GetPluginName().GetCString(), "-simulator")) { 1573 lldb_private::ProcessInstanceInfo proc_info; 1574 if (Host::GetProcessInfo(process->GetID(), proc_info)) { 1575 const Environment &env = proc_info.GetEnvironment(); 1576 1577 llvm::VersionTuple result; 1578 if (!result.tryParse(env.lookup("SIMULATOR_RUNTIME_VERSION"))) 1579 return result; 1580 1581 std::string dyld_root_path = env.lookup("DYLD_ROOT_PATH"); 1582 if (!dyld_root_path.empty()) { 1583 dyld_root_path += "/System/Library/CoreServices/SystemVersion.plist"; 1584 ApplePropertyList system_version_plist(dyld_root_path.c_str()); 1585 std::string product_version; 1586 if (system_version_plist.GetValueAsString("ProductVersion", 1587 product_version)) { 1588 if (!result.tryParse(product_version)) 1589 return result; 1590 } 1591 } 1592 } 1593 // For simulator platforms, do NOT call back through 1594 // Platform::GetOSVersion() as it might call Process::GetHostOSVersion() 1595 // which we don't want as it will be incorrect 1596 return llvm::VersionTuple(); 1597 } 1598 1599 return Platform::GetOSVersion(process); 1600 } 1601 1602 lldb_private::FileSpec PlatformDarwin::LocateExecutable(const char *basename) { 1603 // A collection of SBFileSpec whose SBFileSpec.m_directory members are filled 1604 // in with any executable directories that should be searched. 1605 static std::vector<FileSpec> g_executable_dirs; 1606 1607 // Find the global list of directories that we will search for executables 1608 // once so we don't keep doing the work over and over. 1609 static llvm::once_flag g_once_flag; 1610 llvm::call_once(g_once_flag, []() { 1611 1612 // When locating executables, trust the DEVELOPER_DIR first if it is set 1613 FileSpec xcode_contents_dir = HostInfo::GetXcodeContentsDirectory(); 1614 if (xcode_contents_dir) { 1615 FileSpec xcode_lldb_resources = xcode_contents_dir; 1616 xcode_lldb_resources.AppendPathComponent("SharedFrameworks"); 1617 xcode_lldb_resources.AppendPathComponent("LLDB.framework"); 1618 xcode_lldb_resources.AppendPathComponent("Resources"); 1619 if (FileSystem::Instance().Exists(xcode_lldb_resources)) { 1620 FileSpec dir; 1621 dir.GetDirectory().SetCString(xcode_lldb_resources.GetPath().c_str()); 1622 g_executable_dirs.push_back(dir); 1623 } 1624 } 1625 // Xcode might not be installed so we also check for the Command Line Tools. 1626 FileSpec command_line_tools_dir = GetCommandLineToolsLibraryPath(); 1627 if (command_line_tools_dir) { 1628 FileSpec cmd_line_lldb_resources = command_line_tools_dir; 1629 cmd_line_lldb_resources.AppendPathComponent("PrivateFrameworks"); 1630 cmd_line_lldb_resources.AppendPathComponent("LLDB.framework"); 1631 cmd_line_lldb_resources.AppendPathComponent("Resources"); 1632 if (FileSystem::Instance().Exists(cmd_line_lldb_resources)) { 1633 FileSpec dir; 1634 dir.GetDirectory().SetCString( 1635 cmd_line_lldb_resources.GetPath().c_str()); 1636 g_executable_dirs.push_back(dir); 1637 } 1638 } 1639 }); 1640 1641 // Now search the global list of executable directories for the executable we 1642 // are looking for 1643 for (const auto &executable_dir : g_executable_dirs) { 1644 FileSpec executable_file; 1645 executable_file.GetDirectory() = executable_dir.GetDirectory(); 1646 executable_file.GetFilename().SetCString(basename); 1647 if (FileSystem::Instance().Exists(executable_file)) 1648 return executable_file; 1649 } 1650 1651 return FileSpec(); 1652 } 1653 1654 lldb_private::Status 1655 PlatformDarwin::LaunchProcess(lldb_private::ProcessLaunchInfo &launch_info) { 1656 // Starting in Fall 2016 OSes, NSLog messages only get mirrored to stderr if 1657 // the OS_ACTIVITY_DT_MODE environment variable is set. (It doesn't require 1658 // any specific value; rather, it just needs to exist). We will set it here 1659 // as long as the IDE_DISABLED_OS_ACTIVITY_DT_MODE flag is not set. Xcode 1660 // makes use of IDE_DISABLED_OS_ACTIVITY_DT_MODE to tell 1661 // LLDB *not* to muck with the OS_ACTIVITY_DT_MODE flag when they 1662 // specifically want it unset. 1663 const char *disable_env_var = "IDE_DISABLED_OS_ACTIVITY_DT_MODE"; 1664 auto &env_vars = launch_info.GetEnvironment(); 1665 if (!env_vars.count(disable_env_var)) { 1666 // We want to make sure that OS_ACTIVITY_DT_MODE is set so that we get 1667 // os_log and NSLog messages mirrored to the target process stderr. 1668 env_vars.try_emplace("OS_ACTIVITY_DT_MODE", "enable"); 1669 } 1670 1671 // Let our parent class do the real launching. 1672 return PlatformPOSIX::LaunchProcess(launch_info); 1673 } 1674 1675 lldb_private::Status PlatformDarwin::FindBundleBinaryInExecSearchPaths( 1676 const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp, 1677 const FileSpecList *module_search_paths_ptr, ModuleSP *old_module_sp_ptr, 1678 bool *did_create_ptr) { 1679 const FileSpec &platform_file = module_spec.GetFileSpec(); 1680 // See if the file is present in any of the module_search_paths_ptr 1681 // directories. 1682 if (!module_sp && module_search_paths_ptr && platform_file) { 1683 // create a vector of all the file / directory names in platform_file e.g. 1684 // this might be 1685 // /System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation 1686 // 1687 // We'll need to look in the module_search_paths_ptr directories for both 1688 // "UIFoundation" and "UIFoundation.framework" -- most likely the latter 1689 // will be the one we find there. 1690 1691 FileSpec platform_pull_upart(platform_file); 1692 std::vector<std::string> path_parts; 1693 path_parts.push_back( 1694 platform_pull_upart.GetLastPathComponent().AsCString()); 1695 while (platform_pull_upart.RemoveLastPathComponent()) { 1696 ConstString part = platform_pull_upart.GetLastPathComponent(); 1697 path_parts.push_back(part.AsCString()); 1698 } 1699 const size_t path_parts_size = path_parts.size(); 1700 1701 size_t num_module_search_paths = module_search_paths_ptr->GetSize(); 1702 for (size_t i = 0; i < num_module_search_paths; ++i) { 1703 Log *log_verbose = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); 1704 LLDB_LOGF( 1705 log_verbose, 1706 "PlatformRemoteDarwinDevice::GetSharedModule searching for binary in " 1707 "search-path %s", 1708 module_search_paths_ptr->GetFileSpecAtIndex(i).GetPath().c_str()); 1709 // Create a new FileSpec with this module_search_paths_ptr plus just the 1710 // filename ("UIFoundation"), then the parent dir plus filename 1711 // ("UIFoundation.framework/UIFoundation") etc - up to four names (to 1712 // handle "Foo.framework/Contents/MacOS/Foo") 1713 1714 for (size_t j = 0; j < 4 && j < path_parts_size - 1; ++j) { 1715 FileSpec path_to_try(module_search_paths_ptr->GetFileSpecAtIndex(i)); 1716 1717 // Add the components backwards. For 1718 // .../PrivateFrameworks/UIFoundation.framework/UIFoundation path_parts 1719 // is 1720 // [0] UIFoundation 1721 // [1] UIFoundation.framework 1722 // [2] PrivateFrameworks 1723 // 1724 // and if 'j' is 2, we want to append path_parts[1] and then 1725 // path_parts[0], aka 'UIFoundation.framework/UIFoundation', to the 1726 // module_search_paths_ptr path. 1727 1728 for (int k = j; k >= 0; --k) { 1729 path_to_try.AppendPathComponent(path_parts[k]); 1730 } 1731 1732 if (FileSystem::Instance().Exists(path_to_try)) { 1733 ModuleSpec new_module_spec(module_spec); 1734 new_module_spec.GetFileSpec() = path_to_try; 1735 Status new_error(Platform::GetSharedModule( 1736 new_module_spec, process, module_sp, nullptr, old_module_sp_ptr, 1737 did_create_ptr)); 1738 1739 if (module_sp) { 1740 module_sp->SetPlatformFileSpec(path_to_try); 1741 return new_error; 1742 } 1743 } 1744 } 1745 } 1746 } 1747 return Status(); 1748 } 1749 1750 std::string PlatformDarwin::FindComponentInPath(llvm::StringRef path, 1751 llvm::StringRef component) { 1752 auto begin = llvm::sys::path::begin(path); 1753 auto end = llvm::sys::path::end(path); 1754 for (auto it = begin; it != end; ++it) { 1755 if (it->contains(component)) { 1756 llvm::SmallString<128> buffer; 1757 llvm::sys::path::append(buffer, begin, ++it, 1758 llvm::sys::path::Style::posix); 1759 return buffer.str().str(); 1760 } 1761 } 1762 return {}; 1763 } 1764 1765 FileSpec PlatformDarwin::GetCurrentToolchainDirectory() { 1766 if (FileSpec fspec = HostInfo::GetShlibDir()) 1767 return FileSpec(FindComponentInPath(fspec.GetPath(), ".xctoolchain")); 1768 return {}; 1769 } 1770 1771 FileSpec PlatformDarwin::GetCurrentCommandLineToolsDirectory() { 1772 if (FileSpec fspec = HostInfo::GetShlibDir()) 1773 return FileSpec(FindComponentInPath(fspec.GetPath(), "CommandLineTools")); 1774 return {}; 1775 } 1776