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 82e8d8bef9SDimitry Andric // Let's start by looking at the selected platform. 83e8d8bef9SDimitry Andric PlatformSP platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); 84e8d8bef9SDimitry Andric 85e8d8bef9SDimitry Andric // This variable corresponds to the architecture specified by the triple 86e8d8bef9SDimitry Andric // string. If that string was empty the currently selected platform will 87e8d8bef9SDimitry Andric // determine the architecture. 88*0b57cec5SDimitry Andric const ArchSpec arch(triple_str); 89e8d8bef9SDimitry Andric if (!triple_str.empty() && !arch.IsValid()) { 90*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid triple '%s'", 91*0b57cec5SDimitry Andric triple_str.str().c_str()); 92*0b57cec5SDimitry Andric return error; 93*0b57cec5SDimitry Andric } 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric ArchSpec platform_arch(arch); 96*0b57cec5SDimitry Andric 97e8d8bef9SDimitry Andric // Create a new platform if a platform was specified in the platform options 98e8d8bef9SDimitry Andric // and doesn't match the selected platform. 99e8d8bef9SDimitry Andric if (platform_options && platform_options->PlatformWasSpecified() && 100e8d8bef9SDimitry Andric !platform_options->PlatformMatches(platform_sp)) { 101*0b57cec5SDimitry Andric const bool select_platform = true; 102*0b57cec5SDimitry Andric platform_sp = platform_options->CreatePlatformWithOptions( 103e8d8bef9SDimitry Andric debugger.GetCommandInterpreter(), arch, select_platform, error, 104e8d8bef9SDimitry Andric platform_arch); 105*0b57cec5SDimitry Andric if (!platform_sp) 106*0b57cec5SDimitry Andric return error; 107*0b57cec5SDimitry Andric } 108e8d8bef9SDimitry Andric 109e8d8bef9SDimitry Andric bool prefer_platform_arch = false; 110e8d8bef9SDimitry Andric auto update_platform_arch = [&](const ArchSpec &module_arch) { 111e8d8bef9SDimitry Andric // If the OS or vendor weren't specified, then adopt the module's 112e8d8bef9SDimitry Andric // architecture so that the platform matching can be more accurate. 113e8d8bef9SDimitry Andric if (!platform_arch.TripleOSWasSpecified() || 114e8d8bef9SDimitry Andric !platform_arch.TripleVendorWasSpecified()) { 115e8d8bef9SDimitry Andric prefer_platform_arch = true; 116e8d8bef9SDimitry Andric platform_arch = module_arch; 117*0b57cec5SDimitry Andric } 118e8d8bef9SDimitry Andric }; 119*0b57cec5SDimitry Andric 120*0b57cec5SDimitry Andric if (!user_exe_path.empty()) { 121e8d8bef9SDimitry Andric ModuleSpec module_spec(FileSpec(user_exe_path, FileSpec::Style::native)); 122*0b57cec5SDimitry Andric FileSystem::Instance().Resolve(module_spec.GetFileSpec()); 123*0b57cec5SDimitry Andric // Resolve the executable in case we are given a path to a application 124e8d8bef9SDimitry Andric // bundle like a .app bundle on MacOSX. 125*0b57cec5SDimitry Andric Host::ResolveExecutableInBundle(module_spec.GetFileSpec()); 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric lldb::offset_t file_offset = 0; 128*0b57cec5SDimitry Andric lldb::offset_t file_size = 0; 129e8d8bef9SDimitry Andric ModuleSpecList module_specs; 130*0b57cec5SDimitry Andric const size_t num_specs = ObjectFile::GetModuleSpecifications( 131*0b57cec5SDimitry Andric module_spec.GetFileSpec(), file_offset, file_size, module_specs); 132e8d8bef9SDimitry Andric 133*0b57cec5SDimitry Andric if (num_specs > 0) { 134*0b57cec5SDimitry Andric ModuleSpec matching_module_spec; 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric if (num_specs == 1) { 137*0b57cec5SDimitry Andric if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) { 138*0b57cec5SDimitry Andric if (platform_arch.IsValid()) { 139*0b57cec5SDimitry Andric if (platform_arch.IsCompatibleMatch( 140*0b57cec5SDimitry Andric matching_module_spec.GetArchitecture())) { 141*0b57cec5SDimitry Andric // If the OS or vendor weren't specified, then adopt the module's 142*0b57cec5SDimitry Andric // architecture so that the platform matching can be more 143e8d8bef9SDimitry Andric // accurate. 144e8d8bef9SDimitry Andric update_platform_arch(matching_module_spec.GetArchitecture()); 145*0b57cec5SDimitry Andric } else { 146*0b57cec5SDimitry Andric StreamString platform_arch_strm; 147*0b57cec5SDimitry Andric StreamString module_arch_strm; 148*0b57cec5SDimitry Andric 149480093f4SDimitry Andric platform_arch.DumpTriple(platform_arch_strm.AsRawOstream()); 150*0b57cec5SDimitry Andric matching_module_spec.GetArchitecture().DumpTriple( 151480093f4SDimitry Andric module_arch_strm.AsRawOstream()); 152*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 153*0b57cec5SDimitry Andric "the specified architecture '%s' is not compatible with '%s' " 154*0b57cec5SDimitry Andric "in '%s'", 155*0b57cec5SDimitry Andric platform_arch_strm.GetData(), module_arch_strm.GetData(), 156*0b57cec5SDimitry Andric module_spec.GetFileSpec().GetPath().c_str()); 157*0b57cec5SDimitry Andric return error; 158*0b57cec5SDimitry Andric } 159*0b57cec5SDimitry Andric } else { 160e8d8bef9SDimitry Andric // Only one arch and none was specified. 161*0b57cec5SDimitry Andric prefer_platform_arch = true; 162*0b57cec5SDimitry Andric platform_arch = matching_module_spec.GetArchitecture(); 163*0b57cec5SDimitry Andric } 164*0b57cec5SDimitry Andric } 165e8d8bef9SDimitry Andric } else if (arch.IsValid()) { 166e8d8bef9SDimitry Andric // Fat binary. A (valid) architecture was specified. 167*0b57cec5SDimitry Andric module_spec.GetArchitecture() = arch; 168*0b57cec5SDimitry Andric if (module_specs.FindMatchingModuleSpec(module_spec, 169e8d8bef9SDimitry Andric matching_module_spec)) 170e8d8bef9SDimitry Andric update_platform_arch(matching_module_spec.GetArchitecture()); 171*0b57cec5SDimitry Andric } else { 172e8d8bef9SDimitry Andric // Fat binary. No architecture specified, check if there is 173e8d8bef9SDimitry Andric // only one platform for all of the architectures. 174*0b57cec5SDimitry Andric PlatformSP host_platform_sp = Platform::GetHostPlatform(); 175e8d8bef9SDimitry Andric std::vector<PlatformSP> platforms; 176*0b57cec5SDimitry Andric for (size_t i = 0; i < num_specs; ++i) { 177*0b57cec5SDimitry Andric ModuleSpec module_spec; 178*0b57cec5SDimitry Andric if (module_specs.GetModuleSpecAtIndex(i, module_spec)) { 179e8d8bef9SDimitry Andric // First consider the platform specified by the user, if any, and 180e8d8bef9SDimitry Andric // the selected platform otherwise. 181*0b57cec5SDimitry Andric if (platform_sp) { 182*0b57cec5SDimitry Andric if (platform_sp->IsCompatibleArchitecture( 183*0b57cec5SDimitry Andric module_spec.GetArchitecture(), false, nullptr)) { 184*0b57cec5SDimitry Andric platforms.push_back(platform_sp); 185*0b57cec5SDimitry Andric continue; 186*0b57cec5SDimitry Andric } 187*0b57cec5SDimitry Andric } 188*0b57cec5SDimitry Andric 189e8d8bef9SDimitry Andric // Now consider the host platform if it is different from the 190e8d8bef9SDimitry Andric // specified/selected platform. 191*0b57cec5SDimitry Andric if (host_platform_sp && 192*0b57cec5SDimitry Andric (!platform_sp || 193*0b57cec5SDimitry Andric host_platform_sp->GetName() != platform_sp->GetName())) { 194*0b57cec5SDimitry Andric if (host_platform_sp->IsCompatibleArchitecture( 195*0b57cec5SDimitry Andric module_spec.GetArchitecture(), false, nullptr)) { 196*0b57cec5SDimitry Andric platforms.push_back(host_platform_sp); 197*0b57cec5SDimitry Andric continue; 198*0b57cec5SDimitry Andric } 199*0b57cec5SDimitry Andric } 200*0b57cec5SDimitry Andric 201e8d8bef9SDimitry Andric // Finally find a platform that matches the architecture in the 202e8d8bef9SDimitry Andric // executable file. 203*0b57cec5SDimitry Andric PlatformSP fallback_platform_sp( 204*0b57cec5SDimitry Andric Platform::GetPlatformForArchitecture( 205*0b57cec5SDimitry Andric module_spec.GetArchitecture(), nullptr)); 206*0b57cec5SDimitry Andric if (fallback_platform_sp) { 207*0b57cec5SDimitry Andric platforms.push_back(fallback_platform_sp); 208*0b57cec5SDimitry Andric } 209*0b57cec5SDimitry Andric } 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric 212*0b57cec5SDimitry Andric Platform *platform_ptr = nullptr; 213*0b57cec5SDimitry Andric bool more_than_one_platforms = false; 214*0b57cec5SDimitry Andric for (const auto &the_platform_sp : platforms) { 215*0b57cec5SDimitry Andric if (platform_ptr) { 216*0b57cec5SDimitry Andric if (platform_ptr->GetName() != the_platform_sp->GetName()) { 217*0b57cec5SDimitry Andric more_than_one_platforms = true; 218*0b57cec5SDimitry Andric platform_ptr = nullptr; 219*0b57cec5SDimitry Andric break; 220*0b57cec5SDimitry Andric } 221*0b57cec5SDimitry Andric } else { 222*0b57cec5SDimitry Andric platform_ptr = the_platform_sp.get(); 223*0b57cec5SDimitry Andric } 224*0b57cec5SDimitry Andric } 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric if (platform_ptr) { 227*0b57cec5SDimitry Andric // All platforms for all modules in the executable match, so we can 228e8d8bef9SDimitry Andric // select this platform. 229*0b57cec5SDimitry Andric platform_sp = platforms.front(); 230*0b57cec5SDimitry Andric } else if (!more_than_one_platforms) { 231e8d8bef9SDimitry Andric // No platforms claim to support this file. 232e8d8bef9SDimitry Andric error.SetErrorString("no matching platforms found for this file"); 233*0b57cec5SDimitry Andric return error; 234*0b57cec5SDimitry Andric } else { 235e8d8bef9SDimitry Andric // More than one platform claims to support this file. 236*0b57cec5SDimitry Andric StreamString error_strm; 237*0b57cec5SDimitry Andric std::set<Platform *> platform_set; 238*0b57cec5SDimitry Andric error_strm.Printf( 239*0b57cec5SDimitry Andric "more than one platform supports this executable ("); 240*0b57cec5SDimitry Andric for (const auto &the_platform_sp : platforms) { 241*0b57cec5SDimitry Andric if (platform_set.find(the_platform_sp.get()) == 242*0b57cec5SDimitry Andric platform_set.end()) { 243*0b57cec5SDimitry Andric if (!platform_set.empty()) 244*0b57cec5SDimitry Andric error_strm.PutCString(", "); 245*0b57cec5SDimitry Andric error_strm.PutCString(the_platform_sp->GetName().GetCString()); 246*0b57cec5SDimitry Andric platform_set.insert(the_platform_sp.get()); 247*0b57cec5SDimitry Andric } 248*0b57cec5SDimitry Andric } 249e8d8bef9SDimitry Andric error_strm.Printf("), specify an architecture to disambiguate"); 250*0b57cec5SDimitry Andric error.SetErrorString(error_strm.GetString()); 251*0b57cec5SDimitry Andric return error; 252*0b57cec5SDimitry Andric } 253*0b57cec5SDimitry Andric } 254*0b57cec5SDimitry Andric } 255*0b57cec5SDimitry Andric } 256*0b57cec5SDimitry Andric 257*0b57cec5SDimitry Andric // If we have a valid architecture, make sure the current platform is 258e8d8bef9SDimitry Andric // compatible with that architecture. 259*0b57cec5SDimitry Andric if (!prefer_platform_arch && arch.IsValid()) { 260e8d8bef9SDimitry Andric if (!platform_sp->IsCompatibleArchitecture(arch, false, nullptr)) { 261*0b57cec5SDimitry Andric platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch); 262e8d8bef9SDimitry Andric if (platform_sp) 263*0b57cec5SDimitry Andric debugger.GetPlatformList().SetSelectedPlatform(platform_sp); 264*0b57cec5SDimitry Andric } 265*0b57cec5SDimitry Andric } else if (platform_arch.IsValid()) { 266e8d8bef9SDimitry Andric // If "arch" isn't valid, yet "platform_arch" is, it means we have an 267e8d8bef9SDimitry Andric // executable file with a single architecture which should be used. 268*0b57cec5SDimitry Andric ArchSpec fixed_platform_arch; 269e8d8bef9SDimitry Andric if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, nullptr)) { 270*0b57cec5SDimitry Andric platform_sp = Platform::GetPlatformForArchitecture(platform_arch, 271*0b57cec5SDimitry Andric &fixed_platform_arch); 272e8d8bef9SDimitry Andric if (platform_sp) 273*0b57cec5SDimitry Andric debugger.GetPlatformList().SetSelectedPlatform(platform_sp); 274*0b57cec5SDimitry Andric } 275*0b57cec5SDimitry Andric } 276*0b57cec5SDimitry Andric 277*0b57cec5SDimitry Andric if (!platform_arch.IsValid()) 278*0b57cec5SDimitry Andric platform_arch = arch; 279*0b57cec5SDimitry Andric 280e8d8bef9SDimitry Andric return TargetList::CreateTargetInternal(debugger, user_exe_path, 281e8d8bef9SDimitry Andric platform_arch, load_dependent_files, 282e8d8bef9SDimitry Andric platform_sp, target_sp); 283*0b57cec5SDimitry Andric } 284*0b57cec5SDimitry Andric 285*0b57cec5SDimitry Andric Status TargetList::CreateTargetInternal(Debugger &debugger, 286*0b57cec5SDimitry Andric llvm::StringRef user_exe_path, 287*0b57cec5SDimitry Andric const ArchSpec &specified_arch, 288*0b57cec5SDimitry Andric LoadDependentFiles load_dependent_files, 289*0b57cec5SDimitry Andric lldb::PlatformSP &platform_sp, 290e8d8bef9SDimitry Andric lldb::TargetSP &target_sp) { 291e8d8bef9SDimitry Andric LLDB_SCOPED_TIMERF("TargetList::CreateTarget (file = '%s', arch = '%s')", 292e8d8bef9SDimitry Andric user_exe_path.str().c_str(), 293e8d8bef9SDimitry Andric specified_arch.GetArchitectureName()); 294*0b57cec5SDimitry Andric Status error; 295e8d8bef9SDimitry Andric const bool is_dummy_target = false; 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric ArchSpec arch(specified_arch); 298*0b57cec5SDimitry Andric 299*0b57cec5SDimitry Andric if (arch.IsValid()) { 300*0b57cec5SDimitry Andric if (!platform_sp || 301*0b57cec5SDimitry Andric !platform_sp->IsCompatibleArchitecture(arch, false, nullptr)) 302*0b57cec5SDimitry Andric platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch); 303*0b57cec5SDimitry Andric } 304*0b57cec5SDimitry Andric 305*0b57cec5SDimitry Andric if (!platform_sp) 306*0b57cec5SDimitry Andric platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); 307*0b57cec5SDimitry Andric 308*0b57cec5SDimitry Andric if (!arch.IsValid()) 309*0b57cec5SDimitry Andric arch = specified_arch; 310*0b57cec5SDimitry Andric 311*0b57cec5SDimitry Andric FileSpec file(user_exe_path); 312*0b57cec5SDimitry Andric if (!FileSystem::Instance().Exists(file) && user_exe_path.startswith("~")) { 313*0b57cec5SDimitry Andric // we want to expand the tilde but we don't want to resolve any symbolic 314*0b57cec5SDimitry Andric // links so we can't use the FileSpec constructor's resolve flag 315*0b57cec5SDimitry Andric llvm::SmallString<64> unglobbed_path; 316*0b57cec5SDimitry Andric StandardTildeExpressionResolver Resolver; 317*0b57cec5SDimitry Andric Resolver.ResolveFullPath(user_exe_path, unglobbed_path); 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric if (unglobbed_path.empty()) 320*0b57cec5SDimitry Andric file = FileSpec(user_exe_path); 321*0b57cec5SDimitry Andric else 322*0b57cec5SDimitry Andric file = FileSpec(unglobbed_path.c_str()); 323*0b57cec5SDimitry Andric } 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric bool user_exe_path_is_bundle = false; 326*0b57cec5SDimitry Andric char resolved_bundle_exe_path[PATH_MAX]; 327*0b57cec5SDimitry Andric resolved_bundle_exe_path[0] = '\0'; 328*0b57cec5SDimitry Andric if (file) { 329*0b57cec5SDimitry Andric if (FileSystem::Instance().IsDirectory(file)) 330*0b57cec5SDimitry Andric user_exe_path_is_bundle = true; 331*0b57cec5SDimitry Andric 332*0b57cec5SDimitry Andric if (file.IsRelative() && !user_exe_path.empty()) { 333*0b57cec5SDimitry Andric llvm::SmallString<64> cwd; 334*0b57cec5SDimitry Andric if (! llvm::sys::fs::current_path(cwd)) { 335*0b57cec5SDimitry Andric FileSpec cwd_file(cwd.c_str()); 336*0b57cec5SDimitry Andric cwd_file.AppendPathComponent(file); 337*0b57cec5SDimitry Andric if (FileSystem::Instance().Exists(cwd_file)) 338*0b57cec5SDimitry Andric file = cwd_file; 339*0b57cec5SDimitry Andric } 340*0b57cec5SDimitry Andric } 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric ModuleSP exe_module_sp; 343*0b57cec5SDimitry Andric if (platform_sp) { 344*0b57cec5SDimitry Andric FileSpecList executable_search_paths( 345*0b57cec5SDimitry Andric Target::GetDefaultExecutableSearchPaths()); 346*0b57cec5SDimitry Andric ModuleSpec module_spec(file, arch); 347*0b57cec5SDimitry Andric error = platform_sp->ResolveExecutable(module_spec, exe_module_sp, 348*0b57cec5SDimitry Andric executable_search_paths.GetSize() 349*0b57cec5SDimitry Andric ? &executable_search_paths 350*0b57cec5SDimitry Andric : nullptr); 351*0b57cec5SDimitry Andric } 352*0b57cec5SDimitry Andric 353*0b57cec5SDimitry Andric if (error.Success() && exe_module_sp) { 354*0b57cec5SDimitry Andric if (exe_module_sp->GetObjectFile() == nullptr) { 355*0b57cec5SDimitry Andric if (arch.IsValid()) { 356*0b57cec5SDimitry Andric error.SetErrorStringWithFormat( 357*0b57cec5SDimitry Andric "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(), 358*0b57cec5SDimitry Andric arch.GetArchitectureName()); 359*0b57cec5SDimitry Andric } else { 360*0b57cec5SDimitry Andric error.SetErrorStringWithFormat("unsupported file type \"%s\"", 361*0b57cec5SDimitry Andric file.GetPath().c_str()); 362*0b57cec5SDimitry Andric } 363*0b57cec5SDimitry Andric return error; 364*0b57cec5SDimitry Andric } 365*0b57cec5SDimitry Andric target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 366*0b57cec5SDimitry Andric target_sp->SetExecutableModule(exe_module_sp, load_dependent_files); 367*0b57cec5SDimitry Andric if (user_exe_path_is_bundle) 368*0b57cec5SDimitry Andric exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, 369*0b57cec5SDimitry Andric sizeof(resolved_bundle_exe_path)); 3705ffd83dbSDimitry Andric if (target_sp->GetPreloadSymbols()) 3715ffd83dbSDimitry Andric exe_module_sp->PreloadSymbols(); 372*0b57cec5SDimitry Andric } 373*0b57cec5SDimitry Andric } else { 374*0b57cec5SDimitry Andric // No file was specified, just create an empty target with any arch if a 375*0b57cec5SDimitry Andric // valid arch was specified 376*0b57cec5SDimitry Andric target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); 377*0b57cec5SDimitry Andric } 378*0b57cec5SDimitry Andric 379e8d8bef9SDimitry Andric if (!target_sp) 380e8d8bef9SDimitry Andric return error; 381e8d8bef9SDimitry Andric 382*0b57cec5SDimitry Andric // Set argv0 with what the user typed, unless the user specified a 383*0b57cec5SDimitry Andric // directory. If the user specified a directory, then it is probably a 384*0b57cec5SDimitry Andric // bundle that was resolved and we need to use the resolved bundle path 385*0b57cec5SDimitry Andric if (!user_exe_path.empty()) { 386*0b57cec5SDimitry Andric // Use exactly what the user typed as the first argument when we exec or 387*0b57cec5SDimitry Andric // posix_spawn 388*0b57cec5SDimitry Andric if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) { 389*0b57cec5SDimitry Andric target_sp->SetArg0(resolved_bundle_exe_path); 390*0b57cec5SDimitry Andric } else { 391*0b57cec5SDimitry Andric // Use resolved path 392*0b57cec5SDimitry Andric target_sp->SetArg0(file.GetPath().c_str()); 393*0b57cec5SDimitry Andric } 394*0b57cec5SDimitry Andric } 395*0b57cec5SDimitry Andric if (file.GetDirectory()) { 396*0b57cec5SDimitry Andric FileSpec file_dir; 397*0b57cec5SDimitry Andric file_dir.GetDirectory() = file.GetDirectory(); 398*0b57cec5SDimitry Andric target_sp->AppendExecutableSearchPaths(file_dir); 399*0b57cec5SDimitry Andric } 400*0b57cec5SDimitry Andric 401*0b57cec5SDimitry Andric // Now prime this from the dummy target: 402*0b57cec5SDimitry Andric target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget()); 403*0b57cec5SDimitry Andric 404*0b57cec5SDimitry Andric return error; 405*0b57cec5SDimitry Andric } 406*0b57cec5SDimitry Andric 407*0b57cec5SDimitry Andric bool TargetList::DeleteTarget(TargetSP &target_sp) { 408*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 409e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 410e8d8bef9SDimitry Andric if (it == m_target_list.end()) 411*0b57cec5SDimitry Andric return false; 412e8d8bef9SDimitry Andric 413e8d8bef9SDimitry Andric m_target_list.erase(it); 414e8d8bef9SDimitry Andric return true; 415*0b57cec5SDimitry Andric } 416*0b57cec5SDimitry Andric 417*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithExecutableAndArchitecture( 418*0b57cec5SDimitry Andric const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const { 419*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 420e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 421e8d8bef9SDimitry Andric [&exe_file_spec, exe_arch_ptr](const TargetSP &item) { 422e8d8bef9SDimitry Andric Module *exe_module = item->GetExecutableModulePointer(); 423e8d8bef9SDimitry Andric if (!exe_module || 424e8d8bef9SDimitry Andric !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) 425e8d8bef9SDimitry Andric return false; 426*0b57cec5SDimitry Andric 427e8d8bef9SDimitry Andric return !exe_arch_ptr || 428e8d8bef9SDimitry Andric exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()); 429e8d8bef9SDimitry Andric }); 430e8d8bef9SDimitry Andric 431e8d8bef9SDimitry Andric if (it != m_target_list.end()) 432e8d8bef9SDimitry Andric return *it; 433e8d8bef9SDimitry Andric 434e8d8bef9SDimitry Andric return TargetSP(); 435*0b57cec5SDimitry Andric } 436*0b57cec5SDimitry Andric 437*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const { 438*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 439e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 440e8d8bef9SDimitry Andric [pid](const TargetSP &item) { 441e8d8bef9SDimitry Andric auto *process_ptr = item->GetProcessSP().get(); 442e8d8bef9SDimitry Andric return process_ptr && (process_ptr->GetID() == pid); 443e8d8bef9SDimitry Andric }); 444e8d8bef9SDimitry Andric 445e8d8bef9SDimitry Andric if (it != m_target_list.end()) 446e8d8bef9SDimitry Andric return *it; 447e8d8bef9SDimitry Andric 448e8d8bef9SDimitry Andric return TargetSP(); 449*0b57cec5SDimitry Andric } 450*0b57cec5SDimitry Andric 451*0b57cec5SDimitry Andric TargetSP TargetList::FindTargetWithProcess(Process *process) const { 452*0b57cec5SDimitry Andric TargetSP target_sp; 453e8d8bef9SDimitry Andric if (!process) 454e8d8bef9SDimitry Andric return target_sp; 455e8d8bef9SDimitry Andric 456*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 457e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 458e8d8bef9SDimitry Andric [process](const TargetSP &item) { 459e8d8bef9SDimitry Andric return item->GetProcessSP().get() == process; 460e8d8bef9SDimitry Andric }); 461e8d8bef9SDimitry Andric 462e8d8bef9SDimitry Andric if (it != m_target_list.end()) 463e8d8bef9SDimitry Andric target_sp = *it; 464e8d8bef9SDimitry Andric 465*0b57cec5SDimitry Andric return target_sp; 466*0b57cec5SDimitry Andric } 467*0b57cec5SDimitry Andric 468*0b57cec5SDimitry Andric TargetSP TargetList::GetTargetSP(Target *target) const { 469*0b57cec5SDimitry Andric TargetSP target_sp; 470e8d8bef9SDimitry Andric if (!target) 471e8d8bef9SDimitry Andric return target_sp; 472e8d8bef9SDimitry Andric 473*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 474e8d8bef9SDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(), 475e8d8bef9SDimitry Andric [target](const TargetSP &item) { return item.get() == target; }); 476e8d8bef9SDimitry Andric if (it != m_target_list.end()) 477e8d8bef9SDimitry Andric target_sp = *it; 478e8d8bef9SDimitry Andric 479*0b57cec5SDimitry Andric return target_sp; 480*0b57cec5SDimitry Andric } 481*0b57cec5SDimitry Andric 482*0b57cec5SDimitry Andric uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) { 483*0b57cec5SDimitry Andric uint32_t num_async_interrupts_sent = 0; 484*0b57cec5SDimitry Andric 485*0b57cec5SDimitry Andric if (pid != LLDB_INVALID_PROCESS_ID) { 486*0b57cec5SDimitry Andric TargetSP target_sp(FindTargetWithProcessID(pid)); 487*0b57cec5SDimitry Andric if (target_sp) { 488*0b57cec5SDimitry Andric Process *process = target_sp->GetProcessSP().get(); 489*0b57cec5SDimitry Andric if (process) { 490*0b57cec5SDimitry Andric process->SendAsyncInterrupt(); 491*0b57cec5SDimitry Andric ++num_async_interrupts_sent; 492*0b57cec5SDimitry Andric } 493*0b57cec5SDimitry Andric } 494*0b57cec5SDimitry Andric } else { 495*0b57cec5SDimitry Andric // We don't have a valid pid to broadcast to, so broadcast to the target 496*0b57cec5SDimitry Andric // list's async broadcaster... 497*0b57cec5SDimitry Andric BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); 498*0b57cec5SDimitry Andric } 499*0b57cec5SDimitry Andric 500*0b57cec5SDimitry Andric return num_async_interrupts_sent; 501*0b57cec5SDimitry Andric } 502*0b57cec5SDimitry Andric 503*0b57cec5SDimitry Andric uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { 504*0b57cec5SDimitry Andric uint32_t num_signals_sent = 0; 505*0b57cec5SDimitry Andric Process *process = nullptr; 506*0b57cec5SDimitry Andric if (pid == LLDB_INVALID_PROCESS_ID) { 507*0b57cec5SDimitry Andric // Signal all processes with signal 508*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 509e8d8bef9SDimitry Andric for (const auto &target_sp : m_target_list) { 510e8d8bef9SDimitry Andric process = target_sp->GetProcessSP().get(); 511e8d8bef9SDimitry Andric if (process && process->IsAlive()) { 512*0b57cec5SDimitry Andric ++num_signals_sent; 513*0b57cec5SDimitry Andric process->Signal(signo); 514*0b57cec5SDimitry Andric } 515*0b57cec5SDimitry Andric } 516*0b57cec5SDimitry Andric } else { 517*0b57cec5SDimitry Andric // Signal a specific process with signal 518*0b57cec5SDimitry Andric TargetSP target_sp(FindTargetWithProcessID(pid)); 519*0b57cec5SDimitry Andric if (target_sp) { 520*0b57cec5SDimitry Andric process = target_sp->GetProcessSP().get(); 521e8d8bef9SDimitry Andric if (process && process->IsAlive()) { 522*0b57cec5SDimitry Andric ++num_signals_sent; 523*0b57cec5SDimitry Andric process->Signal(signo); 524*0b57cec5SDimitry Andric } 525*0b57cec5SDimitry Andric } 526*0b57cec5SDimitry Andric } 527*0b57cec5SDimitry Andric return num_signals_sent; 528*0b57cec5SDimitry Andric } 529*0b57cec5SDimitry Andric 530*0b57cec5SDimitry Andric int TargetList::GetNumTargets() const { 531*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 532*0b57cec5SDimitry Andric return m_target_list.size(); 533*0b57cec5SDimitry Andric } 534*0b57cec5SDimitry Andric 535*0b57cec5SDimitry Andric lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const { 536*0b57cec5SDimitry Andric TargetSP target_sp; 537*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 538*0b57cec5SDimitry Andric if (idx < m_target_list.size()) 539*0b57cec5SDimitry Andric target_sp = m_target_list[idx]; 540*0b57cec5SDimitry Andric return target_sp; 541*0b57cec5SDimitry Andric } 542*0b57cec5SDimitry Andric 543*0b57cec5SDimitry Andric uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const { 544*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 545e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 546e8d8bef9SDimitry Andric if (it != m_target_list.end()) 547e8d8bef9SDimitry Andric return std::distance(m_target_list.begin(), it); 548*0b57cec5SDimitry Andric return UINT32_MAX; 549*0b57cec5SDimitry Andric } 550*0b57cec5SDimitry Andric 551e8d8bef9SDimitry Andric void TargetList::AddTargetInternal(TargetSP target_sp, bool do_select) { 552e8d8bef9SDimitry Andric lldbassert(std::find(m_target_list.begin(), m_target_list.end(), target_sp) == 553e8d8bef9SDimitry Andric m_target_list.end() && 554e8d8bef9SDimitry Andric "target already exists it the list"); 555e8d8bef9SDimitry Andric m_target_list.push_back(std::move(target_sp)); 556e8d8bef9SDimitry Andric if (do_select) 557e8d8bef9SDimitry Andric SetSelectedTargetInternal(m_target_list.size() - 1); 558e8d8bef9SDimitry Andric } 559e8d8bef9SDimitry Andric 560e8d8bef9SDimitry Andric void TargetList::SetSelectedTargetInternal(uint32_t index) { 561e8d8bef9SDimitry Andric lldbassert(!m_target_list.empty()); 562e8d8bef9SDimitry Andric m_selected_target_idx = index < m_target_list.size() ? index : 0; 563e8d8bef9SDimitry Andric } 564e8d8bef9SDimitry Andric 565e8d8bef9SDimitry Andric void TargetList::SetSelectedTarget(uint32_t index) { 566*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 567e8d8bef9SDimitry Andric SetSelectedTargetInternal(index); 568*0b57cec5SDimitry Andric } 569e8d8bef9SDimitry Andric 570e8d8bef9SDimitry Andric void TargetList::SetSelectedTarget(const TargetSP &target_sp) { 571e8d8bef9SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 572e8d8bef9SDimitry Andric auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp); 573e8d8bef9SDimitry Andric SetSelectedTargetInternal(std::distance(m_target_list.begin(), it)); 574*0b57cec5SDimitry Andric } 575*0b57cec5SDimitry Andric 576*0b57cec5SDimitry Andric lldb::TargetSP TargetList::GetSelectedTarget() { 577*0b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); 578*0b57cec5SDimitry Andric if (m_selected_target_idx >= m_target_list.size()) 579*0b57cec5SDimitry Andric m_selected_target_idx = 0; 580*0b57cec5SDimitry Andric return GetTargetAtIndex(m_selected_target_idx); 581*0b57cec5SDimitry Andric } 582