1 //===-- TargetList.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 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 #include "llvm/ADT/SmallString.h" 14 15 // Project includes 16 #include "lldb/Core/Broadcaster.h" 17 #include "lldb/Core/Debugger.h" 18 #include "lldb/Core/Event.h" 19 #include "lldb/Core/Module.h" 20 #include "lldb/Core/ModuleSpec.h" 21 #include "lldb/Core/State.h" 22 #include "lldb/Core/Timer.h" 23 #include "lldb/Host/Host.h" 24 #include "lldb/Host/HostInfo.h" 25 #include "lldb/Interpreter/CommandInterpreter.h" 26 #include "lldb/Interpreter/OptionGroupPlatform.h" 27 #include "lldb/Symbol/ObjectFile.h" 28 #include "lldb/Target/Platform.h" 29 #include "lldb/Target/Process.h" 30 #include "lldb/Target/TargetList.h" 31 32 using namespace lldb; 33 using namespace lldb_private; 34 35 ConstString &TargetList::GetStaticBroadcasterClass() { 36 static ConstString class_name("lldb.targetList"); 37 return class_name; 38 } 39 40 //---------------------------------------------------------------------- 41 // TargetList constructor 42 //---------------------------------------------------------------------- 43 TargetList::TargetList(Debugger &debugger) 44 : Broadcaster(debugger.GetBroadcasterManager(), 45 TargetList::GetStaticBroadcasterClass().AsCString()), 46 m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) { 47 CheckInWithManager(); 48 } 49 50 //---------------------------------------------------------------------- 51 // Destructor 52 //---------------------------------------------------------------------- 53 TargetList::~TargetList() { 54 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 55 m_target_list.clear(); 56 } 57 58 Error TargetList::CreateTarget(Debugger &debugger, 59 llvm::StringRef user_exe_path, 60 llvm::StringRef triple_str, 61 bool get_dependent_files, 62 const OptionGroupPlatform *platform_options, 63 TargetSP &target_sp) { 64 return CreateTargetInternal(debugger, user_exe_path, triple_str, 65 get_dependent_files, platform_options, target_sp, 66 false); 67 } 68 69 Error TargetList::CreateTarget(Debugger &debugger, 70 llvm::StringRef user_exe_path, 71 const ArchSpec &specified_arch, 72 bool get_dependent_files, 73 PlatformSP &platform_sp, TargetSP &target_sp) { 74 return CreateTargetInternal(debugger, user_exe_path, specified_arch, 75 get_dependent_files, platform_sp, target_sp, 76 false); 77 } 78 79 Error TargetList::CreateTargetInternal( 80 Debugger &debugger, llvm::StringRef user_exe_path, 81 llvm::StringRef triple_str, bool get_dependent_files, 82 const OptionGroupPlatform *platform_options, TargetSP &target_sp, 83 bool is_dummy_target) { 84 Error error; 85 PlatformSP platform_sp; 86 87 // This is purposely left empty unless it is specified by triple_cstr. 88 // If not initialized via triple_cstr, then the currently selected platform 89 // will set the architecture correctly. 90 const ArchSpec arch(triple_str); 91 if (!triple_str.empty()) { 92 if (!arch.IsValid()) { 93 error.SetErrorStringWithFormat("invalid triple '%s'", 94 triple_str.str().c_str()); 95 return error; 96 } 97 } 98 99 ArchSpec platform_arch(arch); 100 101 bool prefer_platform_arch = false; 102 103 CommandInterpreter &interpreter = debugger.GetCommandInterpreter(); 104 105 // let's see if there is already an existing plaform before we go creating 106 // another... 107 platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); 108 109 if (platform_options && platform_options->PlatformWasSpecified()) { 110 // Create a new platform if it doesn't match the selected platform 111 if (!platform_options->PlatformMatches(platform_sp)) { 112 const bool select_platform = true; 113 platform_sp = platform_options->CreatePlatformWithOptions( 114 interpreter, arch, select_platform, error, platform_arch); 115 if (!platform_sp) 116 return error; 117 } 118 } 119 120 if (!user_exe_path.empty()) { 121 ModuleSpecList module_specs; 122 ModuleSpec module_spec; 123 module_spec.GetFileSpec().SetFile(user_exe_path, true); 124 125 // Resolve the executable in case we are given a path to a application 126 // bundle 127 // like a .app bundle on MacOSX 128 Host::ResolveExecutableInBundle(module_spec.GetFileSpec()); 129 130 lldb::offset_t file_offset = 0; 131 lldb::offset_t file_size = 0; 132 const size_t num_specs = ObjectFile::GetModuleSpecifications( 133 module_spec.GetFileSpec(), file_offset, file_size, module_specs); 134 if (num_specs > 0) { 135 ModuleSpec matching_module_spec; 136 137 if (num_specs == 1) { 138 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) { 139 if (platform_arch.IsValid()) { 140 if (platform_arch.IsCompatibleMatch( 141 matching_module_spec.GetArchitecture())) { 142 // If the OS or vendor weren't specified, then adopt the module's 143 // architecture so that the platform matching can be more accurate 144 if (!platform_arch.TripleOSWasSpecified() || 145 !platform_arch.TripleVendorWasSpecified()) { 146 prefer_platform_arch = true; 147 platform_arch = matching_module_spec.GetArchitecture(); 148 } 149 } else { 150 StreamString platform_arch_strm; 151 StreamString module_arch_strm; 152 153 platform_arch.DumpTriple(platform_arch_strm); 154 matching_module_spec.GetArchitecture().DumpTriple( 155 module_arch_strm); 156 error.SetErrorStringWithFormat( 157 "the specified architecture '%s' is not compatible with '%s' " 158 "in '%s'", 159 platform_arch_strm.GetData(), module_arch_strm.GetData(), 160 module_spec.GetFileSpec().GetPath().c_str()); 161 return error; 162 } 163 } else { 164 // Only one arch and none was specified 165 prefer_platform_arch = true; 166 platform_arch = matching_module_spec.GetArchitecture(); 167 } 168 } 169 } else { 170 if (arch.IsValid()) { 171 module_spec.GetArchitecture() = arch; 172 if (module_specs.FindMatchingModuleSpec(module_spec, 173 matching_module_spec)) { 174 prefer_platform_arch = true; 175 platform_arch = matching_module_spec.GetArchitecture(); 176 } 177 } else { 178 // No architecture specified, check if there is only one platform for 179 // all of the architectures. 180 181 typedef std::vector<PlatformSP> PlatformList; 182 PlatformList platforms; 183 PlatformSP host_platform_sp = Platform::GetHostPlatform(); 184 for (size_t i = 0; i < num_specs; ++i) { 185 ModuleSpec module_spec; 186 if (module_specs.GetModuleSpecAtIndex(i, module_spec)) { 187 // See if there was a selected platform and check that first 188 // since the user may have specified it. 189 if (platform_sp) { 190 if (platform_sp->IsCompatibleArchitecture( 191 module_spec.GetArchitecture(), false, nullptr)) { 192 platforms.push_back(platform_sp); 193 continue; 194 } 195 } 196 197 // Next check the host platform it if wasn't already checked above 198 if (host_platform_sp && 199 (!platform_sp || 200 host_platform_sp->GetName() != platform_sp->GetName())) { 201 if (host_platform_sp->IsCompatibleArchitecture( 202 module_spec.GetArchitecture(), false, nullptr)) { 203 platforms.push_back(host_platform_sp); 204 continue; 205 } 206 } 207 208 // Just find a platform that matches the architecture in the 209 // executable file 210 PlatformSP fallback_platform_sp( 211 Platform::GetPlatformForArchitecture( 212 module_spec.GetArchitecture(), nullptr)); 213 if (fallback_platform_sp) { 214 platforms.push_back(fallback_platform_sp); 215 } 216 } 217 } 218 219 Platform *platform_ptr = nullptr; 220 bool more_than_one_platforms = false; 221 for (const auto &the_platform_sp : platforms) { 222 if (platform_ptr) { 223 if (platform_ptr->GetName() != the_platform_sp->GetName()) { 224 more_than_one_platforms = true; 225 platform_ptr = nullptr; 226 break; 227 } 228 } else { 229 platform_ptr = the_platform_sp.get(); 230 } 231 } 232 233 if (platform_ptr) { 234 // All platforms for all modules in the exectuable match, so we can 235 // select this platform 236 platform_sp = platforms.front(); 237 } else if (more_than_one_platforms == false) { 238 // No platforms claim to support this file 239 error.SetErrorString("No matching platforms found for this file, " 240 "specify one with the --platform option"); 241 return error; 242 } else { 243 // More than one platform claims to support this file, so the 244 // --platform option must be specified 245 StreamString error_strm; 246 std::set<Platform *> platform_set; 247 error_strm.Printf( 248 "more than one platform supports this executable ("); 249 for (const auto &the_platform_sp : platforms) { 250 if (platform_set.find(the_platform_sp.get()) == 251 platform_set.end()) { 252 if (!platform_set.empty()) 253 error_strm.PutCString(", "); 254 error_strm.PutCString(the_platform_sp->GetName().GetCString()); 255 platform_set.insert(the_platform_sp.get()); 256 } 257 } 258 error_strm.Printf( 259 "), use the --platform option to specify a platform"); 260 error.SetErrorString(error_strm.GetString()); 261 return error; 262 } 263 } 264 } 265 } 266 } 267 268 // If we have a valid architecture, make sure the current platform is 269 // compatible with that architecture 270 if (!prefer_platform_arch && arch.IsValid()) { 271 if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) { 272 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch); 273 if (!is_dummy_target && platform_sp) 274 debugger.GetPlatformList().SetSelectedPlatform(platform_sp); 275 } 276 } else if (platform_arch.IsValid()) { 277 // if "arch" isn't valid, yet "platform_arch" is, it means we have an 278 // executable file with 279 // a single architecture which should be used 280 ArchSpec fixed_platform_arch; 281 if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, 282 &fixed_platform_arch)) { 283 platform_sp = Platform::GetPlatformForArchitecture(platform_arch, 284 &fixed_platform_arch); 285 if (!is_dummy_target && platform_sp) 286 debugger.GetPlatformList().SetSelectedPlatform(platform_sp); 287 } 288 } 289 290 if (!platform_arch.IsValid()) 291 platform_arch = arch; 292 293 error = TargetList::CreateTargetInternal( 294 debugger, user_exe_path, platform_arch, get_dependent_files, platform_sp, 295 target_sp, is_dummy_target); 296 return error; 297 } 298 299 lldb::TargetSP TargetList::GetDummyTarget(lldb_private::Debugger &debugger) { 300 // FIXME: Maybe the dummy target should be per-Debugger 301 if (!m_dummy_target_sp || !m_dummy_target_sp->IsValid()) { 302 ArchSpec arch(Target::GetDefaultArchitecture()); 303 if (!arch.IsValid()) 304 arch = HostInfo::GetArchitecture(); 305 Error err = CreateDummyTarget( 306 debugger, arch.GetTriple().getTriple().c_str(), m_dummy_target_sp); 307 } 308 309 return m_dummy_target_sp; 310 } 311 312 Error TargetList::CreateDummyTarget(Debugger &debugger, 313 llvm::StringRef specified_arch_name, 314 lldb::TargetSP &target_sp) { 315 PlatformSP host_platform_sp(Platform::GetHostPlatform()); 316 return CreateTargetInternal( 317 debugger, (const char *)nullptr, specified_arch_name, false, 318 (const OptionGroupPlatform *)nullptr, target_sp, true); 319 } 320 321 Error TargetList::CreateTargetInternal(Debugger &debugger, 322 llvm::StringRef user_exe_path, 323 const ArchSpec &specified_arch, 324 bool get_dependent_files, 325 lldb::PlatformSP &platform_sp, 326 lldb::TargetSP &target_sp, 327 bool is_dummy_target) { 328 Timer scoped_timer(LLVM_PRETTY_FUNCTION, 329 "TargetList::CreateTarget (file = '%s', arch = '%s')", 330 user_exe_path.str().c_str(), 331 specified_arch.GetArchitectureName()); 332 Error error; 333 334 ArchSpec arch(specified_arch); 335 336 if (arch.IsValid()) { 337 if (!platform_sp || 338 !platform_sp->IsCompatibleArchitecture(arch, false, nullptr)) 339 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch); 340 } 341 342 if (!platform_sp) 343 platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); 344 345 if (!arch.IsValid()) 346 arch = specified_arch; 347 348 FileSpec file(user_exe_path, false); 349 if (!file.Exists() && user_exe_path.startswith("~")) { 350 // we want to expand the tilde but we don't want to resolve any symbolic 351 // links 352 // so we can't use the FileSpec constructor's resolve flag 353 llvm::SmallString<64> unglobbed_path(user_exe_path); 354 FileSpec::ResolveUsername(unglobbed_path); 355 356 if (unglobbed_path.empty()) 357 file = FileSpec(user_exe_path, false); 358 else 359 file = FileSpec(unglobbed_path.c_str(), false); 360 } 361 362 bool user_exe_path_is_bundle = false; 363 char resolved_bundle_exe_path[PATH_MAX]; 364 resolved_bundle_exe_path[0] = '\0'; 365 if (file) { 366 if (file.GetFileType() == FileSpec::eFileTypeDirectory) 367 user_exe_path_is_bundle = true; 368 369 if (file.IsRelative() && !user_exe_path.empty()) { 370 // Ignore paths that start with "./" and "../" 371 if (!user_exe_path.startswith("./") && !user_exe_path.startswith("../")) { 372 char cwd[PATH_MAX]; 373 if (getcwd(cwd, sizeof(cwd))) { 374 std::string cwd_user_exe_path(cwd); 375 cwd_user_exe_path += '/'; 376 cwd_user_exe_path += user_exe_path; 377 FileSpec cwd_file(cwd_user_exe_path, false); 378 if (cwd_file.Exists()) 379 file = cwd_file; 380 } 381 } 382 } 383 384 ModuleSP exe_module_sp; 385 if (platform_sp) { 386 FileSpecList executable_search_paths( 387 Target::GetDefaultExecutableSearchPaths()); 388 ModuleSpec module_spec(file, arch); 389 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp, 390 executable_search_paths.GetSize() 391 ? &executable_search_paths 392 : nullptr); 393 } 394 395 if (error.Success() && exe_module_sp) { 396 if (exe_module_sp->GetObjectFile() == nullptr) { 397 if (arch.IsValid()) { 398 error.SetErrorStringWithFormat( 399 "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(), 400 arch.GetArchitectureName()); 401 } else { 402 error.SetErrorStringWithFormat("unsupported file type \"%s\"", 403 file.GetPath().c_str()); 404 } 405 return error; 406 } 407 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 408 target_sp->SetExecutableModule(exe_module_sp, get_dependent_files); 409 if (user_exe_path_is_bundle) 410 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, 411 sizeof(resolved_bundle_exe_path)); 412 } 413 } else { 414 // No file was specified, just create an empty target with any arch 415 // if a valid arch was specified 416 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 417 } 418 419 if (target_sp) { 420 // Set argv0 with what the user typed, unless the user specified a 421 // directory. If the user specified a directory, then it is probably a 422 // bundle that was resolved and we need to use the resolved bundle path 423 if (!user_exe_path.empty()) { 424 // Use exactly what the user typed as the first argument when we exec or 425 // posix_spawn 426 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) { 427 target_sp->SetArg0(resolved_bundle_exe_path); 428 } else { 429 // Use resolved path 430 target_sp->SetArg0(file.GetPath().c_str()); 431 } 432 } 433 if (file.GetDirectory()) { 434 FileSpec file_dir; 435 file_dir.GetDirectory() = file.GetDirectory(); 436 target_sp->GetExecutableSearchPaths().Append(file_dir); 437 } 438 439 // Don't put the dummy target in the target list, it's held separately. 440 if (!is_dummy_target) { 441 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 442 m_selected_target_idx = m_target_list.size(); 443 m_target_list.push_back(target_sp); 444 // Now prime this from the dummy target: 445 target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget()); 446 } else { 447 m_dummy_target_sp = target_sp; 448 } 449 } 450 451 return error; 452 } 453 454 bool TargetList::DeleteTarget(TargetSP &target_sp) { 455 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 456 collection::iterator pos, end = m_target_list.end(); 457 458 for (pos = m_target_list.begin(); pos != end; ++pos) { 459 if (pos->get() == target_sp.get()) { 460 m_target_list.erase(pos); 461 return true; 462 } 463 } 464 return false; 465 } 466 467 TargetSP TargetList::FindTargetWithExecutableAndArchitecture( 468 const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const { 469 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 470 TargetSP target_sp; 471 bool full_match = (bool)exe_file_spec.GetDirectory(); 472 473 collection::const_iterator pos, end = m_target_list.end(); 474 for (pos = m_target_list.begin(); pos != end; ++pos) { 475 Module *exe_module = (*pos)->GetExecutableModulePointer(); 476 477 if (exe_module) { 478 if (FileSpec::Equal(exe_file_spec, exe_module->GetFileSpec(), 479 full_match)) { 480 if (exe_arch_ptr) { 481 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture())) 482 continue; 483 } 484 target_sp = *pos; 485 break; 486 } 487 } 488 } 489 return target_sp; 490 } 491 492 TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const { 493 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 494 TargetSP target_sp; 495 collection::const_iterator pos, end = m_target_list.end(); 496 for (pos = m_target_list.begin(); pos != end; ++pos) { 497 Process *process = (*pos)->GetProcessSP().get(); 498 if (process && process->GetID() == pid) { 499 target_sp = *pos; 500 break; 501 } 502 } 503 return target_sp; 504 } 505 506 TargetSP TargetList::FindTargetWithProcess(Process *process) const { 507 TargetSP target_sp; 508 if (process) { 509 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 510 collection::const_iterator pos, end = m_target_list.end(); 511 for (pos = m_target_list.begin(); pos != end; ++pos) { 512 if (process == (*pos)->GetProcessSP().get()) { 513 target_sp = *pos; 514 break; 515 } 516 } 517 } 518 return target_sp; 519 } 520 521 TargetSP TargetList::GetTargetSP(Target *target) const { 522 TargetSP target_sp; 523 if (target) { 524 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 525 collection::const_iterator pos, end = m_target_list.end(); 526 for (pos = m_target_list.begin(); pos != end; ++pos) { 527 if (target == (*pos).get()) { 528 target_sp = *pos; 529 break; 530 } 531 } 532 } 533 return target_sp; 534 } 535 536 uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) { 537 uint32_t num_async_interrupts_sent = 0; 538 539 if (pid != LLDB_INVALID_PROCESS_ID) { 540 TargetSP target_sp(FindTargetWithProcessID(pid)); 541 if (target_sp) { 542 Process *process = target_sp->GetProcessSP().get(); 543 if (process) { 544 process->SendAsyncInterrupt(); 545 ++num_async_interrupts_sent; 546 } 547 } 548 } else { 549 // We don't have a valid pid to broadcast to, so broadcast to the target 550 // list's async broadcaster... 551 BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); 552 } 553 554 return num_async_interrupts_sent; 555 } 556 557 uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { 558 uint32_t num_signals_sent = 0; 559 Process *process = nullptr; 560 if (pid == LLDB_INVALID_PROCESS_ID) { 561 // Signal all processes with signal 562 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 563 collection::iterator pos, end = m_target_list.end(); 564 for (pos = m_target_list.begin(); pos != end; ++pos) { 565 process = (*pos)->GetProcessSP().get(); 566 if (process) { 567 if (process->IsAlive()) { 568 ++num_signals_sent; 569 process->Signal(signo); 570 } 571 } 572 } 573 } else { 574 // Signal a specific process with signal 575 TargetSP target_sp(FindTargetWithProcessID(pid)); 576 if (target_sp) { 577 process = target_sp->GetProcessSP().get(); 578 if (process) { 579 if (process->IsAlive()) { 580 ++num_signals_sent; 581 process->Signal(signo); 582 } 583 } 584 } 585 } 586 return num_signals_sent; 587 } 588 589 int TargetList::GetNumTargets() const { 590 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 591 return m_target_list.size(); 592 } 593 594 lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const { 595 TargetSP target_sp; 596 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 597 if (idx < m_target_list.size()) 598 target_sp = m_target_list[idx]; 599 return target_sp; 600 } 601 602 uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const { 603 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 604 size_t num_targets = m_target_list.size(); 605 for (size_t idx = 0; idx < num_targets; idx++) { 606 if (target_sp == m_target_list[idx]) 607 return idx; 608 } 609 return UINT32_MAX; 610 } 611 612 uint32_t TargetList::SetSelectedTarget(Target *target) { 613 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 614 collection::const_iterator pos, begin = m_target_list.begin(), 615 end = m_target_list.end(); 616 for (pos = begin; pos != end; ++pos) { 617 if (pos->get() == target) { 618 m_selected_target_idx = std::distance(begin, pos); 619 return m_selected_target_idx; 620 } 621 } 622 m_selected_target_idx = 0; 623 return m_selected_target_idx; 624 } 625 626 lldb::TargetSP TargetList::GetSelectedTarget() { 627 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 628 if (m_selected_target_idx >= m_target_list.size()) 629 m_selected_target_idx = 0; 630 return GetTargetAtIndex(m_selected_target_idx); 631 } 632