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()) { 21981ad6265SDimitry Andric if (!platform_sp->IsCompatibleArchitecture(arch, {}, false, nullptr)) { 22081ad6265SDimitry Andric platform_sp = platform_list.GetOrCreate(arch, {}, &platform_arch); 221e8d8bef9SDimitry Andric if (platform_sp) 22281ad6265SDimitry Andric platform_list.SetSelectedPlatform(platform_sp); 223*0b57cec5SDimitry Andric } 224*0b57cec5SDimitry Andric } else if (platform_arch.IsValid()) { 225e8d8bef9SDimitry Andric // If "arch" isn't valid, yet "platform_arch" is, it means we have an 226e8d8bef9SDimitry Andric // executable file with a single architecture which should be used. 227*0b57cec5SDimitry Andric ArchSpec fixed_platform_arch; 22881ad6265SDimitry Andric if (!platform_sp->IsCompatibleArchitecture(platform_arch, {}, false, nullptr)) { 22981ad6265SDimitry Andric platform_sp = 23081ad6265SDimitry Andric platform_list.GetOrCreate(platform_arch, {}, &fixed_platform_arch); 231e8d8bef9SDimitry Andric if (platform_sp) 23281ad6265SDimitry Andric platform_list.SetSelectedPlatform(platform_sp); 233*0b57cec5SDimitry Andric } 234*0b57cec5SDimitry Andric } 235*0b57cec5SDimitry Andric 236*0b57cec5SDimitry Andric if (!platform_arch.IsValid()) 237*0b57cec5SDimitry Andric platform_arch = arch; 238*0b57cec5SDimitry Andric 239e8d8bef9SDimitry Andric return TargetList::CreateTargetInternal(debugger, user_exe_path, 240e8d8bef9SDimitry Andric platform_arch, load_dependent_files, 241e8d8bef9SDimitry Andric platform_sp, target_sp); 242*0b57cec5SDimitry Andric } 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric Status TargetList::CreateTargetInternal(Debugger &debugger, 245*0b57cec5SDimitry Andric llvm::StringRef user_exe_path, 246*0b57cec5SDimitry Andric const ArchSpec &specified_arch, 247*0b57cec5SDimitry Andric LoadDependentFiles load_dependent_files, 248*0b57cec5SDimitry Andric lldb::PlatformSP &platform_sp, 249e8d8bef9SDimitry Andric lldb::TargetSP &target_sp) { 250e8d8bef9SDimitry Andric LLDB_SCOPED_TIMERF("TargetList::CreateTarget (file = '%s', arch = '%s')", 251e8d8bef9SDimitry Andric user_exe_path.str().c_str(), 252e8d8bef9SDimitry Andric specified_arch.GetArchitectureName()); 253*0b57cec5SDimitry Andric Status error; 254e8d8bef9SDimitry Andric const bool is_dummy_target = false; 255*0b57cec5SDimitry Andric 256*0b57cec5SDimitry Andric ArchSpec arch(specified_arch); 257*0b57cec5SDimitry Andric 258*0b57cec5SDimitry Andric if (arch.IsValid()) { 259*0b57cec5SDimitry Andric if (!platform_sp || 26081ad6265SDimitry Andric !platform_sp->IsCompatibleArchitecture(arch, {}, false, nullptr)) 26181ad6265SDimitry Andric platform_sp = 26281ad6265SDimitry Andric debugger.GetPlatformList().GetOrCreate(specified_arch, {}, &arch); 263*0b57cec5SDimitry Andric } 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric if (!platform_sp) 266*0b57cec5SDimitry Andric platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); 267*0b57cec5SDimitry Andric 268*0b57cec5SDimitry Andric if (!arch.IsValid()) 269*0b57cec5SDimitry Andric arch = specified_arch; 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric FileSpec file(user_exe_path); 272*0b57cec5SDimitry Andric if (!FileSystem::Instance().Exists(file) && user_exe_path.startswith("~")) { 273*0b57cec5SDimitry Andric // we want to expand the tilde but we don't want to resolve any symbolic 274*0b57cec5SDimitry Andric // links so we can't use the FileSpec constructor's resolve flag 275*0b57cec5SDimitry Andric llvm::SmallString<64> unglobbed_path; 276*0b57cec5SDimitry Andric StandardTildeExpressionResolver Resolver; 277*0b57cec5SDimitry Andric Resolver.ResolveFullPath(user_exe_path, unglobbed_path); 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric if (unglobbed_path.empty()) 280*0b57cec5SDimitry Andric file = FileSpec(user_exe_path); 281*0b57cec5SDimitry Andric else 282*0b57cec5SDimitry Andric file = FileSpec(unglobbed_path.c_str()); 283*0b57cec5SDimitry Andric } 284*0b57cec5SDimitry Andric 285*0b57cec5SDimitry Andric bool user_exe_path_is_bundle = false; 286*0b57cec5SDimitry Andric char resolved_bundle_exe_path[PATH_MAX]; 287*0b57cec5SDimitry Andric resolved_bundle_exe_path[0] = '\0'; 288*0b57cec5SDimitry Andric if (file) { 289*0b57cec5SDimitry Andric if (FileSystem::Instance().IsDirectory(file)) 290*0b57cec5SDimitry Andric user_exe_path_is_bundle = true; 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric if (file.IsRelative() && !user_exe_path.empty()) { 293*0b57cec5SDimitry Andric llvm::SmallString<64> cwd; 294*0b57cec5SDimitry Andric if (! llvm::sys::fs::current_path(cwd)) { 295*0b57cec5SDimitry Andric FileSpec cwd_file(cwd.c_str()); 296*0b57cec5SDimitry Andric cwd_file.AppendPathComponent(file); 297*0b57cec5SDimitry Andric if (FileSystem::Instance().Exists(cwd_file)) 298*0b57cec5SDimitry Andric file = cwd_file; 299*0b57cec5SDimitry Andric } 300*0b57cec5SDimitry Andric } 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric ModuleSP exe_module_sp; 303*0b57cec5SDimitry Andric if (platform_sp) { 304*0b57cec5SDimitry Andric FileSpecList executable_search_paths( 305*0b57cec5SDimitry Andric Target::GetDefaultExecutableSearchPaths()); 306*0b57cec5SDimitry Andric ModuleSpec module_spec(file, arch); 307*0b57cec5SDimitry Andric error = platform_sp->ResolveExecutable(module_spec, exe_module_sp, 308*0b57cec5SDimitry Andric executable_search_paths.GetSize() 309*0b57cec5SDimitry Andric ? &executable_search_paths 310*0b57cec5SDimitry Andric : nullptr); 311*0b57cec5SDimitry Andric } 312*0b57cec5SDimitry Andric 313*0b57cec5SDimitry Andric if (error.Success() && exe_module_sp) { 314*0b57cec5SDimitry Andric if (exe_module_sp->GetObjectFile() == nullptr) { 315*0b57cec5SDimitry Andric if (arch.IsValid()) { 316*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 317*0b57cec5SDimitry Andric "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(), 318*0b57cec5SDimitry Andric arch.GetArchitectureName()); 319*0b57cec5SDimitry Andric } else { 320*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("unsupported file type \"%s\"", 321*0b57cec5SDimitry Andric file.GetPath().c_str()); 322*0b57cec5SDimitry Andric } 323*0b57cec5SDimitry Andric return error; 324*0b57cec5SDimitry Andric } 325*0b57cec5SDimitry Andric target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 326*0b57cec5SDimitry Andric target_sp->SetExecutableModule(exe_module_sp, load_dependent_files); 327*0b57cec5SDimitry Andric if (user_exe_path_is_bundle) 328*0b57cec5SDimitry Andric exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, 329*0b57cec5SDimitry Andric sizeof(resolved_bundle_exe_path)); 3305ffd83dbSDimitry Andric if (target_sp->GetPreloadSymbols()) 3315ffd83dbSDimitry Andric exe_module_sp->PreloadSymbols(); 332*0b57cec5SDimitry Andric } 333*0b57cec5SDimitry Andric } else { 334*0b57cec5SDimitry Andric // No file was specified, just create an empty target with any arch if a 335*0b57cec5SDimitry Andric // valid arch was specified 336*0b57cec5SDimitry Andric target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 337*0b57cec5SDimitry Andric } 338*0b57cec5SDimitry Andric 339e8d8bef9SDimitry Andric if (!target_sp) 340e8d8bef9SDimitry Andric return error; 341e8d8bef9SDimitry Andric 342*0b57cec5SDimitry Andric // Set argv0 with what the user typed, unless the user specified a 343*0b57cec5SDimitry Andric // directory. If the user specified a directory, then it is probably a 344*0b57cec5SDimitry Andric // bundle that was resolved and we need to use the resolved bundle path 345*0b57cec5SDimitry Andric if (!user_exe_path.empty()) { 346*0b57cec5SDimitry Andric // Use exactly what the user typed as the first argument when we exec or 347*0b57cec5SDimitry Andric // posix_spawn 348*0b57cec5SDimitry Andric if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) { 349*0b57cec5SDimitry Andric target_sp->SetArg0(resolved_bundle_exe_path); 350*0b57cec5SDimitry Andric } else { 351*0b57cec5SDimitry Andric // Use resolved path 352*0b57cec5SDimitry Andric target_sp->SetArg0(file.GetPath().c_str()); 353*0b57cec5SDimitry Andric } 354*0b57cec5SDimitry Andric } 355*0b57cec5SDimitry Andric if (file.GetDirectory()) { 356*0b57cec5SDimitry Andric FileSpec file_dir; 357*0b57cec5SDimitry Andric file_dir.GetDirectory() = file.GetDirectory(); 358*0b57cec5SDimitry Andric target_sp->AppendExecutableSearchPaths(file_dir); 359*0b57cec5SDimitry Andric } 360*0b57cec5SDimitry Andric 361*0b57cec5SDimitry Andric // Now prime this from the dummy target: 362*0b57cec5SDimitry Andric target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget()); 363*0b57cec5SDimitry Andric 364*0b57cec5SDimitry Andric return error; 365*0b57cec5SDimitry Andric } 366*0b57cec5SDimitry Andric 367*0b57cec5SDimitry Andric bool TargetList::DeleteTarget(TargetSP &target_sp) { 368*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 369e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 370e8d8bef9SDimitry Andric if (it == m_target_list.end()) 371*0b57cec5SDimitry Andric return false; 372e8d8bef9SDimitry Andric 373e8d8bef9SDimitry Andric m_target_list.erase(it); 374e8d8bef9SDimitry Andric return true; 375*0b57cec5SDimitry Andric } 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithExecutableAndArchitecture( 378*0b57cec5SDimitry Andric const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const { 379*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 380e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 381e8d8bef9SDimitry Andric [&exe_file_spec, exe_arch_ptr](const TargetSP &item) { 382e8d8bef9SDimitry Andric Module *exe_module = item->GetExecutableModulePointer(); 383e8d8bef9SDimitry Andric if (!exe_module || 384e8d8bef9SDimitry Andric !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) 385e8d8bef9SDimitry Andric return false; 386*0b57cec5SDimitry Andric 387e8d8bef9SDimitry Andric return !exe_arch_ptr || 388e8d8bef9SDimitry Andric exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()); 389e8d8bef9SDimitry Andric }); 390e8d8bef9SDimitry Andric 391e8d8bef9SDimitry Andric if (it != m_target_list.end()) 392e8d8bef9SDimitry Andric return *it; 393e8d8bef9SDimitry Andric 394e8d8bef9SDimitry Andric return TargetSP(); 395*0b57cec5SDimitry Andric } 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const { 398*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 399e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 400e8d8bef9SDimitry Andric [pid](const TargetSP &item) { 401e8d8bef9SDimitry Andric auto *process_ptr = item->GetProcessSP().get(); 402e8d8bef9SDimitry Andric return process_ptr && (process_ptr->GetID() == pid); 403e8d8bef9SDimitry Andric }); 404e8d8bef9SDimitry Andric 405e8d8bef9SDimitry Andric if (it != m_target_list.end()) 406e8d8bef9SDimitry Andric return *it; 407e8d8bef9SDimitry Andric 408e8d8bef9SDimitry Andric return TargetSP(); 409*0b57cec5SDimitry Andric } 410*0b57cec5SDimitry Andric 411*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithProcess(Process *process) const { 412*0b57cec5SDimitry Andric TargetSP target_sp; 413e8d8bef9SDimitry Andric if (!process) 414e8d8bef9SDimitry Andric return target_sp; 415e8d8bef9SDimitry Andric 416*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 417e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 418e8d8bef9SDimitry Andric [process](const TargetSP &item) { 419e8d8bef9SDimitry Andric return item->GetProcessSP().get() == process; 420e8d8bef9SDimitry Andric }); 421e8d8bef9SDimitry Andric 422e8d8bef9SDimitry Andric if (it != m_target_list.end()) 423e8d8bef9SDimitry Andric target_sp = *it; 424e8d8bef9SDimitry Andric 425*0b57cec5SDimitry Andric return target_sp; 426*0b57cec5SDimitry Andric } 427*0b57cec5SDimitry Andric 428*0b57cec5SDimitry Andric TargetSP TargetList::GetTargetSP(Target *target) const { 429*0b57cec5SDimitry Andric TargetSP target_sp; 430e8d8bef9SDimitry Andric if (!target) 431e8d8bef9SDimitry Andric return target_sp; 432e8d8bef9SDimitry Andric 433*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 434e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 435e8d8bef9SDimitry Andric [target](const TargetSP &item) { return item.get() == target; }); 436e8d8bef9SDimitry Andric if (it != m_target_list.end()) 437e8d8bef9SDimitry Andric target_sp = *it; 438e8d8bef9SDimitry Andric 439*0b57cec5SDimitry Andric return target_sp; 440*0b57cec5SDimitry Andric } 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) { 443*0b57cec5SDimitry Andric uint32_t num_async_interrupts_sent = 0; 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric if (pid != LLDB_INVALID_PROCESS_ID) { 446*0b57cec5SDimitry Andric TargetSP target_sp(FindTargetWithProcessID(pid)); 447*0b57cec5SDimitry Andric if (target_sp) { 448*0b57cec5SDimitry Andric Process *process = target_sp->GetProcessSP().get(); 449*0b57cec5SDimitry Andric if (process) { 450*0b57cec5SDimitry Andric process->SendAsyncInterrupt(); 451*0b57cec5SDimitry Andric ++num_async_interrupts_sent; 452*0b57cec5SDimitry Andric } 453*0b57cec5SDimitry Andric } 454*0b57cec5SDimitry Andric } else { 455*0b57cec5SDimitry Andric // We don't have a valid pid to broadcast to, so broadcast to the target 456*0b57cec5SDimitry Andric // list's async broadcaster... 457*0b57cec5SDimitry Andric BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); 458*0b57cec5SDimitry Andric } 459*0b57cec5SDimitry Andric 460*0b57cec5SDimitry Andric return num_async_interrupts_sent; 461*0b57cec5SDimitry Andric } 462*0b57cec5SDimitry Andric 463*0b57cec5SDimitry Andric uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { 464*0b57cec5SDimitry Andric uint32_t num_signals_sent = 0; 465*0b57cec5SDimitry Andric Process *process = nullptr; 466*0b57cec5SDimitry Andric if (pid == LLDB_INVALID_PROCESS_ID) { 467*0b57cec5SDimitry Andric // Signal all processes with signal 468*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 469e8d8bef9SDimitry Andric for (const auto &target_sp : m_target_list) { 470e8d8bef9SDimitry Andric process = target_sp->GetProcessSP().get(); 471e8d8bef9SDimitry Andric if (process && process->IsAlive()) { 472*0b57cec5SDimitry Andric ++num_signals_sent; 473*0b57cec5SDimitry Andric process->Signal(signo); 474*0b57cec5SDimitry Andric } 475*0b57cec5SDimitry Andric } 476*0b57cec5SDimitry Andric } else { 477*0b57cec5SDimitry Andric // Signal a specific process with signal 478*0b57cec5SDimitry Andric TargetSP target_sp(FindTargetWithProcessID(pid)); 479*0b57cec5SDimitry Andric if (target_sp) { 480*0b57cec5SDimitry Andric process = target_sp->GetProcessSP().get(); 481e8d8bef9SDimitry Andric if (process && process->IsAlive()) { 482*0b57cec5SDimitry Andric ++num_signals_sent; 483*0b57cec5SDimitry Andric process->Signal(signo); 484*0b57cec5SDimitry Andric } 485*0b57cec5SDimitry Andric } 486*0b57cec5SDimitry Andric } 487*0b57cec5SDimitry Andric return num_signals_sent; 488*0b57cec5SDimitry Andric } 489*0b57cec5SDimitry Andric 490*0b57cec5SDimitry Andric int TargetList::GetNumTargets() const { 491*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 492*0b57cec5SDimitry Andric return m_target_list.size(); 493*0b57cec5SDimitry Andric } 494*0b57cec5SDimitry Andric 495*0b57cec5SDimitry Andric lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const { 496*0b57cec5SDimitry Andric TargetSP target_sp; 497*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 498*0b57cec5SDimitry Andric if (idx < m_target_list.size()) 499*0b57cec5SDimitry Andric target_sp = m_target_list[idx]; 500*0b57cec5SDimitry Andric return target_sp; 501*0b57cec5SDimitry Andric } 502*0b57cec5SDimitry Andric 503*0b57cec5SDimitry Andric uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const { 504*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 505e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 506e8d8bef9SDimitry Andric if (it != m_target_list.end()) 507e8d8bef9SDimitry Andric return std::distance(m_target_list.begin(), it); 508*0b57cec5SDimitry Andric return UINT32_MAX; 509*0b57cec5SDimitry Andric } 510*0b57cec5SDimitry Andric 511e8d8bef9SDimitry Andric void TargetList::AddTargetInternal(TargetSP target_sp, bool do_select) { 512e8d8bef9SDimitry Andric lldbassert(std::find(m_target_list.begin(), m_target_list.end(), target_sp) == 513e8d8bef9SDimitry Andric m_target_list.end() && 514e8d8bef9SDimitry Andric "target already exists it the list"); 515e8d8bef9SDimitry Andric m_target_list.push_back(std::move(target_sp)); 516e8d8bef9SDimitry Andric if (do_select) 517e8d8bef9SDimitry Andric SetSelectedTargetInternal(m_target_list.size() - 1); 518e8d8bef9SDimitry Andric } 519e8d8bef9SDimitry Andric 520e8d8bef9SDimitry Andric void TargetList::SetSelectedTargetInternal(uint32_t index) { 521e8d8bef9SDimitry Andric lldbassert(!m_target_list.empty()); 522e8d8bef9SDimitry Andric m_selected_target_idx = index < m_target_list.size() ? index : 0; 523e8d8bef9SDimitry Andric } 524e8d8bef9SDimitry Andric 525e8d8bef9SDimitry Andric void TargetList::SetSelectedTarget(uint32_t index) { 526*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 527e8d8bef9SDimitry Andric SetSelectedTargetInternal(index); 528*0b57cec5SDimitry Andric } 529e8d8bef9SDimitry Andric 530e8d8bef9SDimitry Andric void TargetList::SetSelectedTarget(const TargetSP &target_sp) { 531e8d8bef9SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 532e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 533e8d8bef9SDimitry Andric SetSelectedTargetInternal(std::distance(m_target_list.begin(), it)); 534*0b57cec5SDimitry Andric } 535*0b57cec5SDimitry Andric 536*0b57cec5SDimitry Andric lldb::TargetSP TargetList::GetSelectedTarget() { 537*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 538*0b57cec5SDimitry Andric if (m_selected_target_idx >= m_target_list.size()) 539*0b57cec5SDimitry Andric m_selected_target_idx = 0; 540*0b57cec5SDimitry Andric return GetTargetAtIndex(m_selected_target_idx); 541*0b57cec5SDimitry Andric } 542