15ffd83dbSDimitry Andric //===-- TargetList.cpp ----------------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "lldb/Target/TargetList.h" 10*0b57cec5SDimitry Andric #include "lldb/Core/Debugger.h" 11*0b57cec5SDimitry Andric #include "lldb/Core/Module.h" 12*0b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h" 13*0b57cec5SDimitry Andric #include "lldb/Host/Host.h" 14*0b57cec5SDimitry Andric #include "lldb/Host/HostInfo.h" 15*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 16*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupPlatform.h" 17*0b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h" 18*0b57cec5SDimitry Andric #include "lldb/Target/Platform.h" 19*0b57cec5SDimitry Andric #include "lldb/Target/Process.h" 20*0b57cec5SDimitry Andric #include "lldb/Utility/Broadcaster.h" 21*0b57cec5SDimitry Andric #include "lldb/Utility/Event.h" 22*0b57cec5SDimitry Andric #include "lldb/Utility/State.h" 23*0b57cec5SDimitry Andric #include "lldb/Utility/TildeExpressionResolver.h" 24*0b57cec5SDimitry Andric #include "lldb/Utility/Timer.h" 25*0b57cec5SDimitry Andric 26*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 27*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric using namespace lldb; 30*0b57cec5SDimitry Andric using namespace lldb_private; 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric ConstString &TargetList::GetStaticBroadcasterClass() { 33*0b57cec5SDimitry Andric static ConstString class_name("lldb.targetList"); 34*0b57cec5SDimitry Andric return class_name; 35*0b57cec5SDimitry Andric } 36*0b57cec5SDimitry Andric 37*0b57cec5SDimitry Andric // TargetList constructor 38*0b57cec5SDimitry Andric TargetList::TargetList(Debugger &debugger) 39*0b57cec5SDimitry Andric : Broadcaster(debugger.GetBroadcasterManager(), 40*0b57cec5SDimitry Andric TargetList::GetStaticBroadcasterClass().AsCString()), 41*0b57cec5SDimitry Andric m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) { 42*0b57cec5SDimitry Andric CheckInWithManager(); 43*0b57cec5SDimitry Andric } 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric Status TargetList::CreateTarget(Debugger &debugger, 46*0b57cec5SDimitry Andric llvm::StringRef user_exe_path, 47*0b57cec5SDimitry Andric llvm::StringRef triple_str, 48*0b57cec5SDimitry Andric LoadDependentFiles load_dependent_files, 49*0b57cec5SDimitry Andric const OptionGroupPlatform *platform_options, 50*0b57cec5SDimitry Andric TargetSP &target_sp) { 51fe6060f1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 52e8d8bef9SDimitry Andric auto result = TargetList::CreateTargetInternal( 53e8d8bef9SDimitry Andric debugger, user_exe_path, triple_str, load_dependent_files, 54e8d8bef9SDimitry Andric platform_options, target_sp); 55e8d8bef9SDimitry Andric 56e8d8bef9SDimitry Andric if (target_sp && result.Success()) 57e8d8bef9SDimitry Andric AddTargetInternal(target_sp, /*do_select*/ true); 58e8d8bef9SDimitry Andric return result; 59*0b57cec5SDimitry Andric } 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric Status TargetList::CreateTarget(Debugger &debugger, 62*0b57cec5SDimitry Andric llvm::StringRef user_exe_path, 63*0b57cec5SDimitry Andric const ArchSpec &specified_arch, 64*0b57cec5SDimitry Andric LoadDependentFiles load_dependent_files, 65*0b57cec5SDimitry Andric PlatformSP &platform_sp, TargetSP &target_sp) { 66fe6060f1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 67e8d8bef9SDimitry Andric auto result = TargetList::CreateTargetInternal( 68e8d8bef9SDimitry Andric debugger, user_exe_path, specified_arch, load_dependent_files, 69e8d8bef9SDimitry Andric platform_sp, target_sp); 70e8d8bef9SDimitry Andric 71e8d8bef9SDimitry Andric if (target_sp && result.Success()) 72e8d8bef9SDimitry Andric AddTargetInternal(target_sp, /*do_select*/ true); 73e8d8bef9SDimitry Andric return result; 74*0b57cec5SDimitry Andric } 75*0b57cec5SDimitry Andric 76*0b57cec5SDimitry Andric Status TargetList::CreateTargetInternal( 77*0b57cec5SDimitry Andric Debugger &debugger, llvm::StringRef user_exe_path, 78*0b57cec5SDimitry Andric llvm::StringRef triple_str, LoadDependentFiles load_dependent_files, 79e8d8bef9SDimitry Andric const OptionGroupPlatform *platform_options, TargetSP &target_sp) { 80*0b57cec5SDimitry Andric Status error; 81*0b57cec5SDimitry Andric 8281ad6265SDimitry Andric PlatformList &platform_list = debugger.GetPlatformList(); 83e8d8bef9SDimitry Andric // Let's start by looking at the selected platform. 8481ad6265SDimitry Andric PlatformSP platform_sp = platform_list.GetSelectedPlatform(); 85e8d8bef9SDimitry Andric 86e8d8bef9SDimitry Andric // This variable corresponds to the architecture specified by the triple 87e8d8bef9SDimitry Andric // string. If that string was empty the currently selected platform will 88e8d8bef9SDimitry Andric // determine the architecture. 89*0b57cec5SDimitry Andric const ArchSpec arch(triple_str); 90e8d8bef9SDimitry Andric if (!triple_str.empty() && !arch.IsValid()) { 91*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid triple '%s'", 92*0b57cec5SDimitry Andric triple_str.str().c_str()); 93*0b57cec5SDimitry Andric return error; 94*0b57cec5SDimitry Andric } 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric ArchSpec platform_arch(arch); 97*0b57cec5SDimitry Andric 98e8d8bef9SDimitry Andric // Create a new platform if a platform was specified in the platform options 99e8d8bef9SDimitry Andric // and doesn't match the selected platform. 100e8d8bef9SDimitry Andric if (platform_options && platform_options->PlatformWasSpecified() && 101e8d8bef9SDimitry Andric !platform_options->PlatformMatches(platform_sp)) { 102*0b57cec5SDimitry Andric const bool select_platform = true; 103*0b57cec5SDimitry Andric platform_sp = platform_options->CreatePlatformWithOptions( 104e8d8bef9SDimitry Andric debugger.GetCommandInterpreter(), arch, select_platform, error, 105e8d8bef9SDimitry Andric platform_arch); 106*0b57cec5SDimitry Andric if (!platform_sp) 107*0b57cec5SDimitry Andric return error; 108*0b57cec5SDimitry Andric } 109e8d8bef9SDimitry Andric 110e8d8bef9SDimitry Andric bool prefer_platform_arch = false; 111e8d8bef9SDimitry Andric auto update_platform_arch = [&](const ArchSpec &module_arch) { 112e8d8bef9SDimitry Andric // If the OS or vendor weren't specified, then adopt the module's 113e8d8bef9SDimitry Andric // architecture so that the platform matching can be more accurate. 114e8d8bef9SDimitry Andric if (!platform_arch.TripleOSWasSpecified() || 115e8d8bef9SDimitry Andric !platform_arch.TripleVendorWasSpecified()) { 116e8d8bef9SDimitry Andric prefer_platform_arch = true; 117e8d8bef9SDimitry Andric platform_arch = module_arch; 118*0b57cec5SDimitry Andric } 119e8d8bef9SDimitry Andric }; 120*0b57cec5SDimitry Andric 121*0b57cec5SDimitry Andric if (!user_exe_path.empty()) { 122e8d8bef9SDimitry Andric ModuleSpec module_spec(FileSpec(user_exe_path, FileSpec::Style::native)); 123*0b57cec5SDimitry Andric FileSystem::Instance().Resolve(module_spec.GetFileSpec()); 12481ad6265SDimitry Andric 12581ad6265SDimitry Andric // Try to resolve the exe based on PATH and/or platform-specific suffixes, 12681ad6265SDimitry Andric // but only if using the host platform. 12781ad6265SDimitry Andric if (platform_sp->IsHost() && 12881ad6265SDimitry Andric !FileSystem::Instance().Exists(module_spec.GetFileSpec())) 12981ad6265SDimitry Andric FileSystem::Instance().ResolveExecutableLocation( 13081ad6265SDimitry Andric module_spec.GetFileSpec()); 13181ad6265SDimitry Andric 132*0b57cec5SDimitry Andric // Resolve the executable in case we are given a path to a application 133e8d8bef9SDimitry Andric // bundle like a .app bundle on MacOSX. 134*0b57cec5SDimitry Andric Host::ResolveExecutableInBundle(module_spec.GetFileSpec()); 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric lldb::offset_t file_offset = 0; 137*0b57cec5SDimitry Andric lldb::offset_t file_size = 0; 138e8d8bef9SDimitry Andric ModuleSpecList module_specs; 139*0b57cec5SDimitry Andric const size_t num_specs = ObjectFile::GetModuleSpecifications( 140*0b57cec5SDimitry Andric module_spec.GetFileSpec(), file_offset, file_size, module_specs); 141e8d8bef9SDimitry Andric 142*0b57cec5SDimitry Andric if (num_specs > 0) { 143*0b57cec5SDimitry Andric ModuleSpec matching_module_spec; 144*0b57cec5SDimitry Andric 145*0b57cec5SDimitry Andric if (num_specs == 1) { 146*0b57cec5SDimitry Andric if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) { 147*0b57cec5SDimitry Andric if (platform_arch.IsValid()) { 148*0b57cec5SDimitry Andric if (platform_arch.IsCompatibleMatch( 149*0b57cec5SDimitry Andric matching_module_spec.GetArchitecture())) { 150*0b57cec5SDimitry Andric // If the OS or vendor weren't specified, then adopt the module's 151*0b57cec5SDimitry Andric // architecture so that the platform matching can be more 152e8d8bef9SDimitry Andric // accurate. 153e8d8bef9SDimitry Andric update_platform_arch(matching_module_spec.GetArchitecture()); 154*0b57cec5SDimitry Andric } else { 155*0b57cec5SDimitry Andric StreamString platform_arch_strm; 156*0b57cec5SDimitry Andric StreamString module_arch_strm; 157*0b57cec5SDimitry Andric 158480093f4SDimitry Andric platform_arch.DumpTriple(platform_arch_strm.AsRawOstream()); 159*0b57cec5SDimitry Andric matching_module_spec.GetArchitecture().DumpTriple( 160480093f4SDimitry Andric module_arch_strm.AsRawOstream()); 161*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 162*0b57cec5SDimitry Andric "the specified architecture '%s' is not compatible with '%s' " 163*0b57cec5SDimitry Andric "in '%s'", 164*0b57cec5SDimitry Andric platform_arch_strm.GetData(), module_arch_strm.GetData(), 165*0b57cec5SDimitry Andric module_spec.GetFileSpec().GetPath().c_str()); 166*0b57cec5SDimitry Andric return error; 167*0b57cec5SDimitry Andric } 168*0b57cec5SDimitry Andric } else { 169e8d8bef9SDimitry Andric // Only one arch and none was specified. 170*0b57cec5SDimitry Andric prefer_platform_arch = true; 171*0b57cec5SDimitry Andric platform_arch = matching_module_spec.GetArchitecture(); 172*0b57cec5SDimitry Andric } 173*0b57cec5SDimitry Andric } 174e8d8bef9SDimitry Andric } else if (arch.IsValid()) { 175e8d8bef9SDimitry Andric // Fat binary. A (valid) architecture was specified. 176*0b57cec5SDimitry Andric module_spec.GetArchitecture() = arch; 177*0b57cec5SDimitry Andric if (module_specs.FindMatchingModuleSpec(module_spec, 178e8d8bef9SDimitry Andric matching_module_spec)) 179e8d8bef9SDimitry Andric update_platform_arch(matching_module_spec.GetArchitecture()); 180*0b57cec5SDimitry Andric } else { 181e8d8bef9SDimitry Andric // Fat binary. No architecture specified, check if there is 182e8d8bef9SDimitry Andric // only one platform for all of the architectures. 18381ad6265SDimitry Andric std::vector<PlatformSP> candidates; 18481ad6265SDimitry Andric std::vector<ArchSpec> archs; 18581ad6265SDimitry Andric for (const ModuleSpec &spec : module_specs.ModuleSpecs()) 18681ad6265SDimitry Andric archs.push_back(spec.GetArchitecture()); 18781ad6265SDimitry Andric if (PlatformSP platform_for_archs_sp = 18881ad6265SDimitry Andric platform_list.GetOrCreate(archs, {}, candidates)) { 18981ad6265SDimitry Andric platform_sp = platform_for_archs_sp; 19081ad6265SDimitry Andric } else if (candidates.empty()) { 191e8d8bef9SDimitry Andric error.SetErrorString("no matching platforms found for this file"); 192*0b57cec5SDimitry Andric return error; 193*0b57cec5SDimitry Andric } else { 194e8d8bef9SDimitry Andric // More than one platform claims to support this file. 195*0b57cec5SDimitry Andric StreamString error_strm; 19681ad6265SDimitry Andric std::set<llvm::StringRef> platform_set; 197*0b57cec5SDimitry Andric error_strm.Printf( 198*0b57cec5SDimitry Andric "more than one platform supports this executable ("); 19981ad6265SDimitry Andric for (const auto &candidate : candidates) { 20081ad6265SDimitry Andric llvm::StringRef platform_name = candidate->GetName(); 20181ad6265SDimitry Andric if (platform_set.count(platform_name)) 20281ad6265SDimitry Andric continue; 203*0b57cec5SDimitry Andric if (!platform_set.empty()) 204*0b57cec5SDimitry Andric error_strm.PutCString(", "); 20581ad6265SDimitry Andric error_strm.PutCString(platform_name); 20681ad6265SDimitry Andric platform_set.insert(platform_name); 207*0b57cec5SDimitry Andric } 208e8d8bef9SDimitry Andric error_strm.Printf("), specify an architecture to disambiguate"); 209*0b57cec5SDimitry Andric error.SetErrorString(error_strm.GetString()); 210*0b57cec5SDimitry Andric return error; 211*0b57cec5SDimitry Andric } 212*0b57cec5SDimitry Andric } 213*0b57cec5SDimitry Andric } 214*0b57cec5SDimitry Andric } 215*0b57cec5SDimitry Andric 216*0b57cec5SDimitry Andric // If we have a valid architecture, make sure the current platform is 217e8d8bef9SDimitry Andric // compatible with that architecture. 218*0b57cec5SDimitry Andric if (!prefer_platform_arch && arch.IsValid()) { 219bdd1243dSDimitry Andric if (!platform_sp->IsCompatibleArchitecture( 220bdd1243dSDimitry Andric arch, {}, ArchSpec::CompatibleMatch, nullptr)) { 22181ad6265SDimitry Andric platform_sp = platform_list.GetOrCreate(arch, {}, &platform_arch); 222e8d8bef9SDimitry Andric if (platform_sp) 22381ad6265SDimitry Andric platform_list.SetSelectedPlatform(platform_sp); 224*0b57cec5SDimitry Andric } 225*0b57cec5SDimitry Andric } else if (platform_arch.IsValid()) { 226e8d8bef9SDimitry Andric // If "arch" isn't valid, yet "platform_arch" is, it means we have an 227e8d8bef9SDimitry Andric // executable file with a single architecture which should be used. 228*0b57cec5SDimitry Andric ArchSpec fixed_platform_arch; 229bdd1243dSDimitry Andric if (!platform_sp->IsCompatibleArchitecture( 230bdd1243dSDimitry Andric platform_arch, {}, ArchSpec::CompatibleMatch, nullptr)) { 23181ad6265SDimitry Andric platform_sp = 23281ad6265SDimitry Andric platform_list.GetOrCreate(platform_arch, {}, &fixed_platform_arch); 233e8d8bef9SDimitry Andric if (platform_sp) 23481ad6265SDimitry Andric platform_list.SetSelectedPlatform(platform_sp); 235*0b57cec5SDimitry Andric } 236*0b57cec5SDimitry Andric } 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric if (!platform_arch.IsValid()) 239*0b57cec5SDimitry Andric platform_arch = arch; 240*0b57cec5SDimitry Andric 241e8d8bef9SDimitry Andric return TargetList::CreateTargetInternal(debugger, user_exe_path, 242e8d8bef9SDimitry Andric platform_arch, load_dependent_files, 243e8d8bef9SDimitry Andric platform_sp, target_sp); 244*0b57cec5SDimitry Andric } 245*0b57cec5SDimitry Andric 246*0b57cec5SDimitry Andric Status TargetList::CreateTargetInternal(Debugger &debugger, 247*0b57cec5SDimitry Andric llvm::StringRef user_exe_path, 248*0b57cec5SDimitry Andric const ArchSpec &specified_arch, 249*0b57cec5SDimitry Andric LoadDependentFiles load_dependent_files, 250*0b57cec5SDimitry Andric lldb::PlatformSP &platform_sp, 251e8d8bef9SDimitry Andric lldb::TargetSP &target_sp) { 252e8d8bef9SDimitry Andric LLDB_SCOPED_TIMERF("TargetList::CreateTarget (file = '%s', arch = '%s')", 253e8d8bef9SDimitry Andric user_exe_path.str().c_str(), 254e8d8bef9SDimitry Andric specified_arch.GetArchitectureName()); 255*0b57cec5SDimitry Andric Status error; 256e8d8bef9SDimitry Andric const bool is_dummy_target = false; 257*0b57cec5SDimitry Andric 258*0b57cec5SDimitry Andric ArchSpec arch(specified_arch); 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andric if (arch.IsValid()) { 261bdd1243dSDimitry Andric if (!platform_sp || !platform_sp->IsCompatibleArchitecture( 262bdd1243dSDimitry Andric arch, {}, ArchSpec::CompatibleMatch, nullptr)) 26381ad6265SDimitry Andric platform_sp = 26481ad6265SDimitry Andric debugger.GetPlatformList().GetOrCreate(specified_arch, {}, &arch); 265*0b57cec5SDimitry Andric } 266*0b57cec5SDimitry Andric 267*0b57cec5SDimitry Andric if (!platform_sp) 268*0b57cec5SDimitry Andric platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric if (!arch.IsValid()) 271*0b57cec5SDimitry Andric arch = specified_arch; 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric FileSpec file(user_exe_path); 274*0b57cec5SDimitry Andric if (!FileSystem::Instance().Exists(file) && user_exe_path.startswith("~")) { 275*0b57cec5SDimitry Andric // we want to expand the tilde but we don't want to resolve any symbolic 276*0b57cec5SDimitry Andric // links so we can't use the FileSpec constructor's resolve flag 277*0b57cec5SDimitry Andric llvm::SmallString<64> unglobbed_path; 278*0b57cec5SDimitry Andric StandardTildeExpressionResolver Resolver; 279*0b57cec5SDimitry Andric Resolver.ResolveFullPath(user_exe_path, unglobbed_path); 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric if (unglobbed_path.empty()) 282*0b57cec5SDimitry Andric file = FileSpec(user_exe_path); 283*0b57cec5SDimitry Andric else 284*0b57cec5SDimitry Andric file = FileSpec(unglobbed_path.c_str()); 285*0b57cec5SDimitry Andric } 286*0b57cec5SDimitry Andric 287*0b57cec5SDimitry Andric bool user_exe_path_is_bundle = false; 288*0b57cec5SDimitry Andric char resolved_bundle_exe_path[PATH_MAX]; 289*0b57cec5SDimitry Andric resolved_bundle_exe_path[0] = '\0'; 290*0b57cec5SDimitry Andric if (file) { 291*0b57cec5SDimitry Andric if (FileSystem::Instance().IsDirectory(file)) 292*0b57cec5SDimitry Andric user_exe_path_is_bundle = true; 293*0b57cec5SDimitry Andric 294*0b57cec5SDimitry Andric if (file.IsRelative() && !user_exe_path.empty()) { 295*0b57cec5SDimitry Andric llvm::SmallString<64> cwd; 296*0b57cec5SDimitry Andric if (! llvm::sys::fs::current_path(cwd)) { 297*0b57cec5SDimitry Andric FileSpec cwd_file(cwd.c_str()); 298*0b57cec5SDimitry Andric cwd_file.AppendPathComponent(file); 299*0b57cec5SDimitry Andric if (FileSystem::Instance().Exists(cwd_file)) 300*0b57cec5SDimitry Andric file = cwd_file; 301*0b57cec5SDimitry Andric } 302*0b57cec5SDimitry Andric } 303*0b57cec5SDimitry Andric 304*0b57cec5SDimitry Andric ModuleSP exe_module_sp; 305*0b57cec5SDimitry Andric if (platform_sp) { 306*0b57cec5SDimitry Andric FileSpecList executable_search_paths( 307*0b57cec5SDimitry Andric Target::GetDefaultExecutableSearchPaths()); 308*0b57cec5SDimitry Andric ModuleSpec module_spec(file, arch); 309*0b57cec5SDimitry Andric error = platform_sp->ResolveExecutable(module_spec, exe_module_sp, 310*0b57cec5SDimitry Andric executable_search_paths.GetSize() 311*0b57cec5SDimitry Andric ? &executable_search_paths 312*0b57cec5SDimitry Andric : nullptr); 313*0b57cec5SDimitry Andric } 314*0b57cec5SDimitry Andric 315*0b57cec5SDimitry Andric if (error.Success() && exe_module_sp) { 316*0b57cec5SDimitry Andric if (exe_module_sp->GetObjectFile() == nullptr) { 317*0b57cec5SDimitry Andric if (arch.IsValid()) { 318*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 319*0b57cec5SDimitry Andric "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(), 320*0b57cec5SDimitry Andric arch.GetArchitectureName()); 321*0b57cec5SDimitry Andric } else { 322*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("unsupported file type \"%s\"", 323*0b57cec5SDimitry Andric file.GetPath().c_str()); 324*0b57cec5SDimitry Andric } 325*0b57cec5SDimitry Andric return error; 326*0b57cec5SDimitry Andric } 327*0b57cec5SDimitry Andric target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 328*0b57cec5SDimitry Andric target_sp->SetExecutableModule(exe_module_sp, load_dependent_files); 329*0b57cec5SDimitry Andric if (user_exe_path_is_bundle) 330*0b57cec5SDimitry Andric exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, 331*0b57cec5SDimitry Andric sizeof(resolved_bundle_exe_path)); 3325ffd83dbSDimitry Andric if (target_sp->GetPreloadSymbols()) 3335ffd83dbSDimitry Andric exe_module_sp->PreloadSymbols(); 334*0b57cec5SDimitry Andric } 335*0b57cec5SDimitry Andric } else { 336*0b57cec5SDimitry Andric // No file was specified, just create an empty target with any arch if a 337*0b57cec5SDimitry Andric // valid arch was specified 338*0b57cec5SDimitry Andric target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 339*0b57cec5SDimitry Andric } 340*0b57cec5SDimitry Andric 341e8d8bef9SDimitry Andric if (!target_sp) 342e8d8bef9SDimitry Andric return error; 343e8d8bef9SDimitry Andric 344*0b57cec5SDimitry Andric // Set argv0 with what the user typed, unless the user specified a 345*0b57cec5SDimitry Andric // directory. If the user specified a directory, then it is probably a 346*0b57cec5SDimitry Andric // bundle that was resolved and we need to use the resolved bundle path 347*0b57cec5SDimitry Andric if (!user_exe_path.empty()) { 348*0b57cec5SDimitry Andric // Use exactly what the user typed as the first argument when we exec or 349*0b57cec5SDimitry Andric // posix_spawn 350*0b57cec5SDimitry Andric if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) { 351*0b57cec5SDimitry Andric target_sp->SetArg0(resolved_bundle_exe_path); 352*0b57cec5SDimitry Andric } else { 353*0b57cec5SDimitry Andric // Use resolved path 354*0b57cec5SDimitry Andric target_sp->SetArg0(file.GetPath().c_str()); 355*0b57cec5SDimitry Andric } 356*0b57cec5SDimitry Andric } 357*0b57cec5SDimitry Andric if (file.GetDirectory()) { 358*0b57cec5SDimitry Andric FileSpec file_dir; 359bdd1243dSDimitry Andric file_dir.SetDirectory(file.GetDirectory()); 360*0b57cec5SDimitry Andric target_sp->AppendExecutableSearchPaths(file_dir); 361*0b57cec5SDimitry Andric } 362*0b57cec5SDimitry Andric 363*0b57cec5SDimitry Andric // Now prime this from the dummy target: 364*0b57cec5SDimitry Andric target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget()); 365*0b57cec5SDimitry Andric 366*0b57cec5SDimitry Andric return error; 367*0b57cec5SDimitry Andric } 368*0b57cec5SDimitry Andric 369*0b57cec5SDimitry Andric bool TargetList::DeleteTarget(TargetSP &target_sp) { 370*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 371e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 372e8d8bef9SDimitry Andric if (it == m_target_list.end()) 373*0b57cec5SDimitry Andric return false; 374e8d8bef9SDimitry Andric 375e8d8bef9SDimitry Andric m_target_list.erase(it); 376e8d8bef9SDimitry Andric return true; 377*0b57cec5SDimitry Andric } 378*0b57cec5SDimitry Andric 379*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithExecutableAndArchitecture( 380*0b57cec5SDimitry Andric const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const { 381*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 382e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 383e8d8bef9SDimitry Andric [&exe_file_spec, exe_arch_ptr](const TargetSP &item) { 384e8d8bef9SDimitry Andric Module *exe_module = item->GetExecutableModulePointer(); 385e8d8bef9SDimitry Andric if (!exe_module || 386e8d8bef9SDimitry Andric !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) 387e8d8bef9SDimitry Andric return false; 388*0b57cec5SDimitry Andric 389e8d8bef9SDimitry Andric return !exe_arch_ptr || 390e8d8bef9SDimitry Andric exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()); 391e8d8bef9SDimitry Andric }); 392e8d8bef9SDimitry Andric 393e8d8bef9SDimitry Andric if (it != m_target_list.end()) 394e8d8bef9SDimitry Andric return *it; 395e8d8bef9SDimitry Andric 396e8d8bef9SDimitry Andric return TargetSP(); 397*0b57cec5SDimitry Andric } 398*0b57cec5SDimitry Andric 399*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const { 400*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 401e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 402e8d8bef9SDimitry Andric [pid](const TargetSP &item) { 403e8d8bef9SDimitry Andric auto *process_ptr = item->GetProcessSP().get(); 404e8d8bef9SDimitry Andric return process_ptr && (process_ptr->GetID() == pid); 405e8d8bef9SDimitry Andric }); 406e8d8bef9SDimitry Andric 407e8d8bef9SDimitry Andric if (it != m_target_list.end()) 408e8d8bef9SDimitry Andric return *it; 409e8d8bef9SDimitry Andric 410e8d8bef9SDimitry Andric return TargetSP(); 411*0b57cec5SDimitry Andric } 412*0b57cec5SDimitry Andric 413*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithProcess(Process *process) const { 414*0b57cec5SDimitry Andric TargetSP target_sp; 415e8d8bef9SDimitry Andric if (!process) 416e8d8bef9SDimitry Andric return target_sp; 417e8d8bef9SDimitry Andric 418*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 419e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 420e8d8bef9SDimitry Andric [process](const TargetSP &item) { 421e8d8bef9SDimitry Andric return item->GetProcessSP().get() == process; 422e8d8bef9SDimitry Andric }); 423e8d8bef9SDimitry Andric 424e8d8bef9SDimitry Andric if (it != m_target_list.end()) 425e8d8bef9SDimitry Andric target_sp = *it; 426e8d8bef9SDimitry Andric 427*0b57cec5SDimitry Andric return target_sp; 428*0b57cec5SDimitry Andric } 429*0b57cec5SDimitry Andric 430*0b57cec5SDimitry Andric TargetSP TargetList::GetTargetSP(Target *target) const { 431*0b57cec5SDimitry Andric TargetSP target_sp; 432e8d8bef9SDimitry Andric if (!target) 433e8d8bef9SDimitry Andric return target_sp; 434e8d8bef9SDimitry Andric 435*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 436e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 437e8d8bef9SDimitry Andric [target](const TargetSP &item) { return item.get() == target; }); 438e8d8bef9SDimitry Andric if (it != m_target_list.end()) 439e8d8bef9SDimitry Andric target_sp = *it; 440e8d8bef9SDimitry Andric 441*0b57cec5SDimitry Andric return target_sp; 442*0b57cec5SDimitry Andric } 443*0b57cec5SDimitry Andric 444*0b57cec5SDimitry Andric uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) { 445*0b57cec5SDimitry Andric uint32_t num_async_interrupts_sent = 0; 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric if (pid != LLDB_INVALID_PROCESS_ID) { 448*0b57cec5SDimitry Andric TargetSP target_sp(FindTargetWithProcessID(pid)); 449*0b57cec5SDimitry Andric if (target_sp) { 450*0b57cec5SDimitry Andric Process *process = target_sp->GetProcessSP().get(); 451*0b57cec5SDimitry Andric if (process) { 452*0b57cec5SDimitry Andric process->SendAsyncInterrupt(); 453*0b57cec5SDimitry Andric ++num_async_interrupts_sent; 454*0b57cec5SDimitry Andric } 455*0b57cec5SDimitry Andric } 456*0b57cec5SDimitry Andric } else { 457*0b57cec5SDimitry Andric // We don't have a valid pid to broadcast to, so broadcast to the target 458*0b57cec5SDimitry Andric // list's async broadcaster... 459*0b57cec5SDimitry Andric BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); 460*0b57cec5SDimitry Andric } 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric return num_async_interrupts_sent; 463*0b57cec5SDimitry Andric } 464*0b57cec5SDimitry Andric 465*0b57cec5SDimitry Andric uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { 466*0b57cec5SDimitry Andric uint32_t num_signals_sent = 0; 467*0b57cec5SDimitry Andric Process *process = nullptr; 468*0b57cec5SDimitry Andric if (pid == LLDB_INVALID_PROCESS_ID) { 469*0b57cec5SDimitry Andric // Signal all processes with signal 470*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 471e8d8bef9SDimitry Andric for (const auto &target_sp : m_target_list) { 472e8d8bef9SDimitry Andric process = target_sp->GetProcessSP().get(); 473e8d8bef9SDimitry Andric if (process && process->IsAlive()) { 474*0b57cec5SDimitry Andric ++num_signals_sent; 475*0b57cec5SDimitry Andric process->Signal(signo); 476*0b57cec5SDimitry Andric } 477*0b57cec5SDimitry Andric } 478*0b57cec5SDimitry Andric } else { 479*0b57cec5SDimitry Andric // Signal a specific process with signal 480*0b57cec5SDimitry Andric TargetSP target_sp(FindTargetWithProcessID(pid)); 481*0b57cec5SDimitry Andric if (target_sp) { 482*0b57cec5SDimitry Andric process = target_sp->GetProcessSP().get(); 483e8d8bef9SDimitry Andric if (process && process->IsAlive()) { 484*0b57cec5SDimitry Andric ++num_signals_sent; 485*0b57cec5SDimitry Andric process->Signal(signo); 486*0b57cec5SDimitry Andric } 487*0b57cec5SDimitry Andric } 488*0b57cec5SDimitry Andric } 489*0b57cec5SDimitry Andric return num_signals_sent; 490*0b57cec5SDimitry Andric } 491*0b57cec5SDimitry Andric 492*0b57cec5SDimitry Andric int TargetList::GetNumTargets() const { 493*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 494*0b57cec5SDimitry Andric return m_target_list.size(); 495*0b57cec5SDimitry Andric } 496*0b57cec5SDimitry Andric 497*0b57cec5SDimitry Andric lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const { 498*0b57cec5SDimitry Andric TargetSP target_sp; 499*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 500*0b57cec5SDimitry Andric if (idx < m_target_list.size()) 501*0b57cec5SDimitry Andric target_sp = m_target_list[idx]; 502*0b57cec5SDimitry Andric return target_sp; 503*0b57cec5SDimitry Andric } 504*0b57cec5SDimitry Andric 505*0b57cec5SDimitry Andric uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const { 506*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 507e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 508e8d8bef9SDimitry Andric if (it != m_target_list.end()) 509e8d8bef9SDimitry Andric return std::distance(m_target_list.begin(), it); 510*0b57cec5SDimitry Andric return UINT32_MAX; 511*0b57cec5SDimitry Andric } 512*0b57cec5SDimitry Andric 513e8d8bef9SDimitry Andric void TargetList::AddTargetInternal(TargetSP target_sp, bool do_select) { 514fcaf7f86SDimitry Andric lldbassert(!llvm::is_contained(m_target_list, target_sp) && 515e8d8bef9SDimitry Andric "target already exists it the list"); 516e8d8bef9SDimitry Andric m_target_list.push_back(std::move(target_sp)); 517e8d8bef9SDimitry Andric if (do_select) 518e8d8bef9SDimitry Andric SetSelectedTargetInternal(m_target_list.size() - 1); 519e8d8bef9SDimitry Andric } 520e8d8bef9SDimitry Andric 521e8d8bef9SDimitry Andric void TargetList::SetSelectedTargetInternal(uint32_t index) { 522e8d8bef9SDimitry Andric lldbassert(!m_target_list.empty()); 523e8d8bef9SDimitry Andric m_selected_target_idx = index < m_target_list.size() ? index : 0; 524e8d8bef9SDimitry Andric } 525e8d8bef9SDimitry Andric 526e8d8bef9SDimitry Andric void TargetList::SetSelectedTarget(uint32_t index) { 527*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 528e8d8bef9SDimitry Andric SetSelectedTargetInternal(index); 529*0b57cec5SDimitry Andric } 530e8d8bef9SDimitry Andric 531e8d8bef9SDimitry Andric void TargetList::SetSelectedTarget(const TargetSP &target_sp) { 532e8d8bef9SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 533e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 534e8d8bef9SDimitry Andric SetSelectedTargetInternal(std::distance(m_target_list.begin(), it)); 535*0b57cec5SDimitry Andric } 536*0b57cec5SDimitry Andric 537*0b57cec5SDimitry Andric lldb::TargetSP TargetList::GetSelectedTarget() { 538*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 539*0b57cec5SDimitry Andric if (m_selected_target_idx >= m_target_list.size()) 540*0b57cec5SDimitry Andric m_selected_target_idx = 0; 541*0b57cec5SDimitry Andric return GetTargetAtIndex(m_selected_target_idx); 542*0b57cec5SDimitry Andric } 543