1cfca06d7SDimitry Andric //===-- TargetList.cpp ----------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
974a628f7SDimitry Andric #include "lldb/Target/TargetList.h"
10f034231aSEd Maste #include "lldb/Core/Debugger.h"
11f034231aSEd Maste #include "lldb/Core/Module.h"
12f034231aSEd Maste #include "lldb/Core/ModuleSpec.h"
13f034231aSEd Maste #include "lldb/Host/Host.h"
14205afe67SEd Maste #include "lldb/Host/HostInfo.h"
15f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
16f034231aSEd Maste #include "lldb/Interpreter/OptionGroupPlatform.h"
17f034231aSEd Maste #include "lldb/Symbol/ObjectFile.h"
18f034231aSEd Maste #include "lldb/Target/Platform.h"
19f034231aSEd Maste #include "lldb/Target/Process.h"
2094994d37SDimitry Andric #include "lldb/Utility/Broadcaster.h"
2194994d37SDimitry Andric #include "lldb/Utility/Event.h"
2294994d37SDimitry Andric #include "lldb/Utility/State.h"
2374a628f7SDimitry Andric #include "lldb/Utility/TildeExpressionResolver.h"
241b306c26SDimitry Andric #include "lldb/Utility/Timer.h"
2574a628f7SDimitry Andric
2674a628f7SDimitry Andric #include "llvm/ADT/SmallString.h"
2774a628f7SDimitry Andric #include "llvm/Support/FileSystem.h"
28f034231aSEd Maste
29f034231aSEd Maste using namespace lldb;
30f034231aSEd Maste using namespace lldb_private;
31f034231aSEd Maste
GetStaticBroadcasterClass()32ac9a064cSDimitry Andric llvm::StringRef TargetList::GetStaticBroadcasterClass() {
33ac9a064cSDimitry Andric static constexpr llvm::StringLiteral class_name("lldb.targetList");
34f034231aSEd Maste return class_name;
35f034231aSEd Maste }
36f034231aSEd Maste
37f034231aSEd Maste // TargetList constructor
TargetList(Debugger & debugger)38f3fbd1c0SDimitry Andric TargetList::TargetList(Debugger &debugger)
3914f1b3e8SDimitry Andric : Broadcaster(debugger.GetBroadcasterManager(),
40ac9a064cSDimitry Andric TargetList::GetStaticBroadcasterClass().str()),
4114f1b3e8SDimitry Andric m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) {
42f034231aSEd Maste CheckInWithManager();
43f034231aSEd Maste }
44f034231aSEd Maste
CreateTarget(Debugger & debugger,llvm::StringRef user_exe_path,llvm::StringRef triple_str,LoadDependentFiles load_dependent_files,const OptionGroupPlatform * platform_options,TargetSP & target_sp)45b76161e4SDimitry Andric Status TargetList::CreateTarget(Debugger &debugger,
4614f1b3e8SDimitry Andric llvm::StringRef user_exe_path,
4714f1b3e8SDimitry Andric llvm::StringRef triple_str,
4894994d37SDimitry Andric LoadDependentFiles load_dependent_files,
49f034231aSEd Maste const OptionGroupPlatform *platform_options,
5014f1b3e8SDimitry Andric TargetSP &target_sp) {
51344a3780SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
52b60736ecSDimitry Andric auto result = TargetList::CreateTargetInternal(
53b60736ecSDimitry Andric debugger, user_exe_path, triple_str, load_dependent_files,
54b60736ecSDimitry Andric platform_options, target_sp);
55b60736ecSDimitry Andric
56b60736ecSDimitry Andric if (target_sp && result.Success())
57b60736ecSDimitry Andric AddTargetInternal(target_sp, /*do_select*/ true);
58b60736ecSDimitry Andric return result;
59205afe67SEd Maste }
60205afe67SEd Maste
CreateTarget(Debugger & debugger,llvm::StringRef user_exe_path,const ArchSpec & specified_arch,LoadDependentFiles load_dependent_files,PlatformSP & platform_sp,TargetSP & target_sp)61b76161e4SDimitry Andric Status TargetList::CreateTarget(Debugger &debugger,
6214f1b3e8SDimitry Andric llvm::StringRef user_exe_path,
63205afe67SEd Maste const ArchSpec &specified_arch,
6494994d37SDimitry Andric LoadDependentFiles load_dependent_files,
6514f1b3e8SDimitry Andric PlatformSP &platform_sp, TargetSP &target_sp) {
66344a3780SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
67b60736ecSDimitry Andric auto result = TargetList::CreateTargetInternal(
68b60736ecSDimitry Andric debugger, user_exe_path, specified_arch, load_dependent_files,
69b60736ecSDimitry Andric platform_sp, target_sp);
70b60736ecSDimitry Andric
71b60736ecSDimitry Andric if (target_sp && result.Success())
72b60736ecSDimitry Andric AddTargetInternal(target_sp, /*do_select*/ true);
73b60736ecSDimitry Andric return result;
74205afe67SEd Maste }
75205afe67SEd Maste
CreateTargetInternal(Debugger & debugger,llvm::StringRef user_exe_path,llvm::StringRef triple_str,LoadDependentFiles load_dependent_files,const OptionGroupPlatform * platform_options,TargetSP & target_sp)76b76161e4SDimitry Andric Status TargetList::CreateTargetInternal(
7714f1b3e8SDimitry Andric Debugger &debugger, llvm::StringRef user_exe_path,
7894994d37SDimitry Andric llvm::StringRef triple_str, LoadDependentFiles load_dependent_files,
79b60736ecSDimitry Andric const OptionGroupPlatform *platform_options, TargetSP &target_sp) {
80b76161e4SDimitry Andric Status error;
81f034231aSEd Maste
82145449b1SDimitry Andric PlatformList &platform_list = debugger.GetPlatformList();
83b60736ecSDimitry Andric // Let's start by looking at the selected platform.
84145449b1SDimitry Andric PlatformSP platform_sp = platform_list.GetSelectedPlatform();
85b60736ecSDimitry Andric
86b60736ecSDimitry Andric // This variable corresponds to the architecture specified by the triple
87b60736ecSDimitry Andric // string. If that string was empty the currently selected platform will
88b60736ecSDimitry Andric // determine the architecture.
8914f1b3e8SDimitry Andric const ArchSpec arch(triple_str);
90b60736ecSDimitry Andric if (!triple_str.empty() && !arch.IsValid()) {
9114f1b3e8SDimitry Andric error.SetErrorStringWithFormat("invalid triple '%s'",
9214f1b3e8SDimitry Andric triple_str.str().c_str());
93f034231aSEd Maste return error;
94f034231aSEd Maste }
95f034231aSEd Maste
96f034231aSEd Maste ArchSpec platform_arch(arch);
97f034231aSEd Maste
98b60736ecSDimitry Andric // Create a new platform if a platform was specified in the platform options
99b60736ecSDimitry Andric // and doesn't match the selected platform.
100b60736ecSDimitry Andric if (platform_options && platform_options->PlatformWasSpecified() &&
101b60736ecSDimitry Andric !platform_options->PlatformMatches(platform_sp)) {
1020cac4ca3SEd Maste const bool select_platform = true;
10314f1b3e8SDimitry Andric platform_sp = platform_options->CreatePlatformWithOptions(
104b60736ecSDimitry Andric debugger.GetCommandInterpreter(), arch, select_platform, error,
105b60736ecSDimitry Andric platform_arch);
1060cac4ca3SEd Maste if (!platform_sp)
1070cac4ca3SEd Maste return error;
1080cac4ca3SEd Maste }
109b60736ecSDimitry Andric
110b60736ecSDimitry Andric bool prefer_platform_arch = false;
111b60736ecSDimitry Andric auto update_platform_arch = [&](const ArchSpec &module_arch) {
112b60736ecSDimitry Andric // If the OS or vendor weren't specified, then adopt the module's
113b60736ecSDimitry Andric // architecture so that the platform matching can be more accurate.
114b60736ecSDimitry Andric if (!platform_arch.TripleOSWasSpecified() ||
115b60736ecSDimitry Andric !platform_arch.TripleVendorWasSpecified()) {
116b60736ecSDimitry Andric prefer_platform_arch = true;
117b60736ecSDimitry Andric platform_arch = module_arch;
11812bd4897SEd Maste }
119b60736ecSDimitry Andric };
120f034231aSEd Maste
12114f1b3e8SDimitry Andric if (!user_exe_path.empty()) {
122b60736ecSDimitry Andric ModuleSpec module_spec(FileSpec(user_exe_path, FileSpec::Style::native));
12394994d37SDimitry Andric FileSystem::Instance().Resolve(module_spec.GetFileSpec());
124145449b1SDimitry Andric
125145449b1SDimitry Andric // Try to resolve the exe based on PATH and/or platform-specific suffixes,
126145449b1SDimitry Andric // but only if using the host platform.
127145449b1SDimitry Andric if (platform_sp->IsHost() &&
128145449b1SDimitry Andric !FileSystem::Instance().Exists(module_spec.GetFileSpec()))
129145449b1SDimitry Andric FileSystem::Instance().ResolveExecutableLocation(
130145449b1SDimitry Andric module_spec.GetFileSpec());
131145449b1SDimitry Andric
13214f1b3e8SDimitry Andric // Resolve the executable in case we are given a path to a application
133b60736ecSDimitry Andric // bundle like a .app bundle on MacOSX.
1340cac4ca3SEd Maste Host::ResolveExecutableInBundle(module_spec.GetFileSpec());
1350cac4ca3SEd Maste
136f034231aSEd Maste lldb::offset_t file_offset = 0;
137f034231aSEd Maste lldb::offset_t file_size = 0;
138b60736ecSDimitry Andric ModuleSpecList module_specs;
13914f1b3e8SDimitry Andric const size_t num_specs = ObjectFile::GetModuleSpecifications(
14014f1b3e8SDimitry Andric module_spec.GetFileSpec(), file_offset, file_size, module_specs);
141b60736ecSDimitry Andric
14214f1b3e8SDimitry Andric if (num_specs > 0) {
143f034231aSEd Maste ModuleSpec matching_module_spec;
144f034231aSEd Maste
14514f1b3e8SDimitry Andric if (num_specs == 1) {
14614f1b3e8SDimitry Andric if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) {
14714f1b3e8SDimitry Andric if (platform_arch.IsValid()) {
14814f1b3e8SDimitry Andric if (platform_arch.IsCompatibleMatch(
14914f1b3e8SDimitry Andric matching_module_spec.GetArchitecture())) {
1500cac4ca3SEd Maste // If the OS or vendor weren't specified, then adopt the module's
151f73363f1SDimitry Andric // architecture so that the platform matching can be more
152b60736ecSDimitry Andric // accurate.
153b60736ecSDimitry Andric update_platform_arch(matching_module_spec.GetArchitecture());
15414f1b3e8SDimitry Andric } else {
155e81d9d49SDimitry Andric StreamString platform_arch_strm;
156e81d9d49SDimitry Andric StreamString module_arch_strm;
157e81d9d49SDimitry Andric
158706b4fc4SDimitry Andric platform_arch.DumpTriple(platform_arch_strm.AsRawOstream());
15914f1b3e8SDimitry Andric matching_module_spec.GetArchitecture().DumpTriple(
160706b4fc4SDimitry Andric module_arch_strm.AsRawOstream());
16114f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
16214f1b3e8SDimitry Andric "the specified architecture '%s' is not compatible with '%s' "
16314f1b3e8SDimitry Andric "in '%s'",
16414f1b3e8SDimitry Andric platform_arch_strm.GetData(), module_arch_strm.GetData(),
165f034231aSEd Maste module_spec.GetFileSpec().GetPath().c_str());
166f034231aSEd Maste return error;
167f034231aSEd Maste }
16814f1b3e8SDimitry Andric } else {
169b60736ecSDimitry Andric // Only one arch and none was specified.
1700cac4ca3SEd Maste prefer_platform_arch = true;
171f034231aSEd Maste platform_arch = matching_module_spec.GetArchitecture();
172f034231aSEd Maste }
173f034231aSEd Maste }
174b60736ecSDimitry Andric } else if (arch.IsValid()) {
175b60736ecSDimitry Andric // Fat binary. A (valid) architecture was specified.
176f034231aSEd Maste module_spec.GetArchitecture() = arch;
17714f1b3e8SDimitry Andric if (module_specs.FindMatchingModuleSpec(module_spec,
178b60736ecSDimitry Andric matching_module_spec))
179b60736ecSDimitry Andric update_platform_arch(matching_module_spec.GetArchitecture());
18014f1b3e8SDimitry Andric } else {
181b60736ecSDimitry Andric // Fat binary. No architecture specified, check if there is
182b60736ecSDimitry Andric // only one platform for all of the architectures.
183145449b1SDimitry Andric std::vector<PlatformSP> candidates;
184145449b1SDimitry Andric std::vector<ArchSpec> archs;
185145449b1SDimitry Andric for (const ModuleSpec &spec : module_specs.ModuleSpecs())
186145449b1SDimitry Andric archs.push_back(spec.GetArchitecture());
187145449b1SDimitry Andric if (PlatformSP platform_for_archs_sp =
188145449b1SDimitry Andric platform_list.GetOrCreate(archs, {}, candidates)) {
189145449b1SDimitry Andric platform_sp = platform_for_archs_sp;
190145449b1SDimitry Andric } else if (candidates.empty()) {
191b60736ecSDimitry Andric error.SetErrorString("no matching platforms found for this file");
192e81d9d49SDimitry Andric return error;
19314f1b3e8SDimitry Andric } else {
194b60736ecSDimitry Andric // More than one platform claims to support this file.
1950cac4ca3SEd Maste StreamString error_strm;
196145449b1SDimitry Andric std::set<llvm::StringRef> platform_set;
19714f1b3e8SDimitry Andric error_strm.Printf(
19814f1b3e8SDimitry Andric "more than one platform supports this executable (");
199145449b1SDimitry Andric for (const auto &candidate : candidates) {
200145449b1SDimitry Andric llvm::StringRef platform_name = candidate->GetName();
201145449b1SDimitry Andric if (platform_set.count(platform_name))
202145449b1SDimitry Andric continue;
2030cac4ca3SEd Maste if (!platform_set.empty())
2040cac4ca3SEd Maste error_strm.PutCString(", ");
205145449b1SDimitry Andric error_strm.PutCString(platform_name);
206145449b1SDimitry Andric platform_set.insert(platform_name);
2070cac4ca3SEd Maste }
208b60736ecSDimitry Andric error_strm.Printf("), specify an architecture to disambiguate");
20914f1b3e8SDimitry Andric error.SetErrorString(error_strm.GetString());
210f034231aSEd Maste return error;
211f034231aSEd Maste }
212f034231aSEd Maste }
2130cac4ca3SEd Maste }
2140cac4ca3SEd Maste }
215f034231aSEd Maste
2165e95aa85SEd Maste // If we have a valid architecture, make sure the current platform is
217b60736ecSDimitry Andric // compatible with that architecture.
21814f1b3e8SDimitry Andric if (!prefer_platform_arch && arch.IsValid()) {
219e3b55780SDimitry Andric if (!platform_sp->IsCompatibleArchitecture(
220e3b55780SDimitry Andric arch, {}, ArchSpec::CompatibleMatch, nullptr)) {
221145449b1SDimitry Andric platform_sp = platform_list.GetOrCreate(arch, {}, &platform_arch);
222b60736ecSDimitry Andric if (platform_sp)
223145449b1SDimitry Andric platform_list.SetSelectedPlatform(platform_sp);
224205afe67SEd Maste }
22514f1b3e8SDimitry Andric } else if (platform_arch.IsValid()) {
226b60736ecSDimitry Andric // If "arch" isn't valid, yet "platform_arch" is, it means we have an
227b60736ecSDimitry Andric // executable file with a single architecture which should be used.
2280cac4ca3SEd Maste ArchSpec fixed_platform_arch;
229e3b55780SDimitry Andric if (!platform_sp->IsCompatibleArchitecture(
230e3b55780SDimitry Andric platform_arch, {}, ArchSpec::CompatibleMatch, nullptr)) {
231145449b1SDimitry Andric platform_sp =
232145449b1SDimitry Andric platform_list.GetOrCreate(platform_arch, {}, &fixed_platform_arch);
233b60736ecSDimitry Andric if (platform_sp)
234145449b1SDimitry Andric platform_list.SetSelectedPlatform(platform_sp);
235205afe67SEd Maste }
2360cac4ca3SEd Maste }
237f034231aSEd Maste
238f034231aSEd Maste if (!platform_arch.IsValid())
239f034231aSEd Maste platform_arch = arch;
240f034231aSEd Maste
241b60736ecSDimitry Andric return TargetList::CreateTargetInternal(debugger, user_exe_path,
242b60736ecSDimitry Andric platform_arch, load_dependent_files,
243b60736ecSDimitry Andric platform_sp, target_sp);
244205afe67SEd Maste }
245205afe67SEd Maste
CreateTargetInternal(Debugger & debugger,llvm::StringRef user_exe_path,const ArchSpec & specified_arch,LoadDependentFiles load_dependent_files,lldb::PlatformSP & platform_sp,lldb::TargetSP & target_sp)246b76161e4SDimitry Andric Status TargetList::CreateTargetInternal(Debugger &debugger,
24714f1b3e8SDimitry Andric llvm::StringRef user_exe_path,
248f034231aSEd Maste const ArchSpec &specified_arch,
24994994d37SDimitry Andric LoadDependentFiles load_dependent_files,
250205afe67SEd Maste lldb::PlatformSP &platform_sp,
251b60736ecSDimitry Andric lldb::TargetSP &target_sp) {
252b60736ecSDimitry Andric LLDB_SCOPED_TIMERF("TargetList::CreateTarget (file = '%s', arch = '%s')",
253b60736ecSDimitry Andric user_exe_path.str().c_str(),
254b60736ecSDimitry Andric specified_arch.GetArchitectureName());
255b76161e4SDimitry Andric Status error;
256b60736ecSDimitry Andric const bool is_dummy_target = false;
257f034231aSEd Maste
258f034231aSEd Maste ArchSpec arch(specified_arch);
259f034231aSEd Maste
26014f1b3e8SDimitry Andric if (arch.IsValid()) {
261e3b55780SDimitry Andric if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
262e3b55780SDimitry Andric arch, {}, ArchSpec::CompatibleMatch, nullptr))
263145449b1SDimitry Andric platform_sp =
264145449b1SDimitry Andric debugger.GetPlatformList().GetOrCreate(specified_arch, {}, &arch);
265f034231aSEd Maste }
266f034231aSEd Maste
267f034231aSEd Maste if (!platform_sp)
268f034231aSEd Maste platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
269f034231aSEd Maste
270f034231aSEd Maste if (!arch.IsValid())
271f034231aSEd Maste arch = specified_arch;
272f034231aSEd Maste
27394994d37SDimitry Andric FileSpec file(user_exe_path);
274312c0ed1SDimitry Andric if (!FileSystem::Instance().Exists(file) && user_exe_path.starts_with("~")) {
27514f1b3e8SDimitry Andric // we want to expand the tilde but we don't want to resolve any symbolic
27674a628f7SDimitry Andric // links so we can't use the FileSpec constructor's resolve flag
27774a628f7SDimitry Andric llvm::SmallString<64> unglobbed_path;
27874a628f7SDimitry Andric StandardTildeExpressionResolver Resolver;
27974a628f7SDimitry Andric Resolver.ResolveFullPath(user_exe_path, unglobbed_path);
280f034231aSEd Maste
2810cac4ca3SEd Maste if (unglobbed_path.empty())
28294994d37SDimitry Andric file = FileSpec(user_exe_path);
2830cac4ca3SEd Maste else
28494994d37SDimitry Andric file = FileSpec(unglobbed_path.c_str());
285f034231aSEd Maste }
286f034231aSEd Maste
287f034231aSEd Maste bool user_exe_path_is_bundle = false;
288f034231aSEd Maste char resolved_bundle_exe_path[PATH_MAX];
289f034231aSEd Maste resolved_bundle_exe_path[0] = '\0';
29014f1b3e8SDimitry Andric if (file) {
29194994d37SDimitry Andric if (FileSystem::Instance().IsDirectory(file))
292f034231aSEd Maste user_exe_path_is_bundle = true;
293f034231aSEd Maste
29414f1b3e8SDimitry Andric if (file.IsRelative() && !user_exe_path.empty()) {
29574a628f7SDimitry Andric llvm::SmallString<64> cwd;
29674a628f7SDimitry Andric if (! llvm::sys::fs::current_path(cwd)) {
29794994d37SDimitry Andric FileSpec cwd_file(cwd.c_str());
298f73363f1SDimitry Andric cwd_file.AppendPathComponent(file);
29994994d37SDimitry Andric if (FileSystem::Instance().Exists(cwd_file))
300f034231aSEd Maste file = cwd_file;
301f034231aSEd Maste }
302f034231aSEd Maste }
303f034231aSEd Maste
304f034231aSEd Maste ModuleSP exe_module_sp;
30514f1b3e8SDimitry Andric if (platform_sp) {
30614f1b3e8SDimitry Andric FileSpecList executable_search_paths(
30714f1b3e8SDimitry Andric Target::GetDefaultExecutableSearchPaths());
308205afe67SEd Maste ModuleSpec module_spec(file, arch);
30914f1b3e8SDimitry Andric error = platform_sp->ResolveExecutable(module_spec, exe_module_sp,
31014f1b3e8SDimitry Andric executable_search_paths.GetSize()
31114f1b3e8SDimitry Andric ? &executable_search_paths
31214f1b3e8SDimitry Andric : nullptr);
313f034231aSEd Maste }
314f034231aSEd Maste
31514f1b3e8SDimitry Andric if (error.Success() && exe_module_sp) {
31614f1b3e8SDimitry Andric if (exe_module_sp->GetObjectFile() == nullptr) {
31714f1b3e8SDimitry Andric if (arch.IsValid()) {
31814f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
31914f1b3e8SDimitry Andric "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(),
320f034231aSEd Maste arch.GetArchitectureName());
32114f1b3e8SDimitry Andric } else {
322f034231aSEd Maste error.SetErrorStringWithFormat("unsupported file type \"%s\"",
323f034231aSEd Maste file.GetPath().c_str());
324f034231aSEd Maste }
325f034231aSEd Maste return error;
326f034231aSEd Maste }
327205afe67SEd Maste target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
3287fa27ce4SDimitry Andric debugger.GetTargetList().RegisterInProcessTarget(target_sp);
32994994d37SDimitry Andric target_sp->SetExecutableModule(exe_module_sp, load_dependent_files);
330f034231aSEd Maste if (user_exe_path_is_bundle)
33114f1b3e8SDimitry Andric exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
33214f1b3e8SDimitry Andric sizeof(resolved_bundle_exe_path));
333cfca06d7SDimitry Andric if (target_sp->GetPreloadSymbols())
334cfca06d7SDimitry Andric exe_module_sp->PreloadSymbols();
335f034231aSEd Maste }
33614f1b3e8SDimitry Andric } else {
337f73363f1SDimitry Andric // No file was specified, just create an empty target with any arch if a
338f73363f1SDimitry Andric // valid arch was specified
339205afe67SEd Maste target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
3407fa27ce4SDimitry Andric debugger.GetTargetList().RegisterInProcessTarget(target_sp);
341f034231aSEd Maste }
342f034231aSEd Maste
343b60736ecSDimitry Andric if (!target_sp)
344b60736ecSDimitry Andric return error;
345b60736ecSDimitry Andric
346f034231aSEd Maste // Set argv0 with what the user typed, unless the user specified a
347f034231aSEd Maste // directory. If the user specified a directory, then it is probably a
348f034231aSEd Maste // bundle that was resolved and we need to use the resolved bundle path
34914f1b3e8SDimitry Andric if (!user_exe_path.empty()) {
35014f1b3e8SDimitry Andric // Use exactly what the user typed as the first argument when we exec or
35114f1b3e8SDimitry Andric // posix_spawn
35214f1b3e8SDimitry Andric if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
353f034231aSEd Maste target_sp->SetArg0(resolved_bundle_exe_path);
35414f1b3e8SDimitry Andric } else {
355f034231aSEd Maste // Use resolved path
356f034231aSEd Maste target_sp->SetArg0(file.GetPath().c_str());
357f034231aSEd Maste }
358f034231aSEd Maste }
35914f1b3e8SDimitry Andric if (file.GetDirectory()) {
360f034231aSEd Maste FileSpec file_dir;
361e3b55780SDimitry Andric file_dir.SetDirectory(file.GetDirectory());
3625f29bb8aSDimitry Andric target_sp->AppendExecutableSearchPaths(file_dir);
363f034231aSEd Maste }
364205afe67SEd Maste
365205afe67SEd Maste // Now prime this from the dummy target:
366205afe67SEd Maste target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget());
367f034231aSEd Maste
368f034231aSEd Maste return error;
369f034231aSEd Maste }
370f034231aSEd Maste
DeleteTarget(TargetSP & target_sp)37114f1b3e8SDimitry Andric bool TargetList::DeleteTarget(TargetSP &target_sp) {
372f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
373b1c73532SDimitry Andric auto it = llvm::find(m_target_list, target_sp);
374b60736ecSDimitry Andric if (it == m_target_list.end())
375f034231aSEd Maste return false;
376b60736ecSDimitry Andric
377b60736ecSDimitry Andric m_target_list.erase(it);
378b60736ecSDimitry Andric return true;
379f034231aSEd Maste }
380f034231aSEd Maste
FindTargetWithExecutableAndArchitecture(const FileSpec & exe_file_spec,const ArchSpec * exe_arch_ptr) const38114f1b3e8SDimitry Andric TargetSP TargetList::FindTargetWithExecutableAndArchitecture(
38214f1b3e8SDimitry Andric const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const {
383f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
384b60736ecSDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
385b60736ecSDimitry Andric [&exe_file_spec, exe_arch_ptr](const TargetSP &item) {
386b60736ecSDimitry Andric Module *exe_module = item->GetExecutableModulePointer();
387b60736ecSDimitry Andric if (!exe_module ||
388b60736ecSDimitry Andric !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec()))
389b60736ecSDimitry Andric return false;
390f034231aSEd Maste
391b60736ecSDimitry Andric return !exe_arch_ptr ||
392b60736ecSDimitry Andric exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture());
393b60736ecSDimitry Andric });
394b60736ecSDimitry Andric
395b60736ecSDimitry Andric if (it != m_target_list.end())
396b60736ecSDimitry Andric return *it;
397b60736ecSDimitry Andric
398b60736ecSDimitry Andric return TargetSP();
399f034231aSEd Maste }
400f034231aSEd Maste
FindTargetWithProcessID(lldb::pid_t pid) const40114f1b3e8SDimitry Andric TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const {
402f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
403b60736ecSDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
404b60736ecSDimitry Andric [pid](const TargetSP &item) {
405b60736ecSDimitry Andric auto *process_ptr = item->GetProcessSP().get();
406b60736ecSDimitry Andric return process_ptr && (process_ptr->GetID() == pid);
407b60736ecSDimitry Andric });
408b60736ecSDimitry Andric
409b60736ecSDimitry Andric if (it != m_target_list.end())
410b60736ecSDimitry Andric return *it;
411b60736ecSDimitry Andric
412b60736ecSDimitry Andric return TargetSP();
413f034231aSEd Maste }
414f034231aSEd Maste
FindTargetWithProcess(Process * process) const41514f1b3e8SDimitry Andric TargetSP TargetList::FindTargetWithProcess(Process *process) const {
416f034231aSEd Maste TargetSP target_sp;
417b60736ecSDimitry Andric if (!process)
418b60736ecSDimitry Andric return target_sp;
419b60736ecSDimitry Andric
420f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
421b60736ecSDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
422b60736ecSDimitry Andric [process](const TargetSP &item) {
423b60736ecSDimitry Andric return item->GetProcessSP().get() == process;
424b60736ecSDimitry Andric });
425b60736ecSDimitry Andric
426b60736ecSDimitry Andric if (it != m_target_list.end())
427b60736ecSDimitry Andric target_sp = *it;
428b60736ecSDimitry Andric
429f034231aSEd Maste return target_sp;
430f034231aSEd Maste }
431f034231aSEd Maste
GetTargetSP(Target * target) const43214f1b3e8SDimitry Andric TargetSP TargetList::GetTargetSP(Target *target) const {
433f034231aSEd Maste TargetSP target_sp;
434b60736ecSDimitry Andric if (!target)
435b60736ecSDimitry Andric return target_sp;
436b60736ecSDimitry Andric
437f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
438b60736ecSDimitry Andric auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
439b60736ecSDimitry Andric [target](const TargetSP &item) { return item.get() == target; });
440b60736ecSDimitry Andric if (it != m_target_list.end())
441b60736ecSDimitry Andric target_sp = *it;
442b60736ecSDimitry Andric
443f034231aSEd Maste return target_sp;
444f034231aSEd Maste }
445f034231aSEd Maste
SendAsyncInterrupt(lldb::pid_t pid)44614f1b3e8SDimitry Andric uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) {
447f034231aSEd Maste uint32_t num_async_interrupts_sent = 0;
448f034231aSEd Maste
44914f1b3e8SDimitry Andric if (pid != LLDB_INVALID_PROCESS_ID) {
450f034231aSEd Maste TargetSP target_sp(FindTargetWithProcessID(pid));
45114f1b3e8SDimitry Andric if (target_sp) {
452f034231aSEd Maste Process *process = target_sp->GetProcessSP().get();
45314f1b3e8SDimitry Andric if (process) {
454f034231aSEd Maste process->SendAsyncInterrupt();
455f034231aSEd Maste ++num_async_interrupts_sent;
456f034231aSEd Maste }
457f034231aSEd Maste }
45814f1b3e8SDimitry Andric } else {
459f034231aSEd Maste // We don't have a valid pid to broadcast to, so broadcast to the target
460f034231aSEd Maste // list's async broadcaster...
461e81d9d49SDimitry Andric BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr);
462f034231aSEd Maste }
463f034231aSEd Maste
464f034231aSEd Maste return num_async_interrupts_sent;
465f034231aSEd Maste }
466f034231aSEd Maste
SignalIfRunning(lldb::pid_t pid,int signo)46714f1b3e8SDimitry Andric uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) {
468f034231aSEd Maste uint32_t num_signals_sent = 0;
469e81d9d49SDimitry Andric Process *process = nullptr;
47014f1b3e8SDimitry Andric if (pid == LLDB_INVALID_PROCESS_ID) {
471f034231aSEd Maste // Signal all processes with signal
472f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
473b60736ecSDimitry Andric for (const auto &target_sp : m_target_list) {
474b60736ecSDimitry Andric process = target_sp->GetProcessSP().get();
475b60736ecSDimitry Andric if (process && process->IsAlive()) {
476f034231aSEd Maste ++num_signals_sent;
477f034231aSEd Maste process->Signal(signo);
478f034231aSEd Maste }
479f034231aSEd Maste }
48014f1b3e8SDimitry Andric } else {
481f034231aSEd Maste // Signal a specific process with signal
482f034231aSEd Maste TargetSP target_sp(FindTargetWithProcessID(pid));
48314f1b3e8SDimitry Andric if (target_sp) {
484f034231aSEd Maste process = target_sp->GetProcessSP().get();
485b60736ecSDimitry Andric if (process && process->IsAlive()) {
486f034231aSEd Maste ++num_signals_sent;
487f034231aSEd Maste process->Signal(signo);
488f034231aSEd Maste }
489f034231aSEd Maste }
490f034231aSEd Maste }
491f034231aSEd Maste return num_signals_sent;
492f034231aSEd Maste }
493f034231aSEd Maste
GetNumTargets() const4947fa27ce4SDimitry Andric size_t TargetList::GetNumTargets() const {
495f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
496f034231aSEd Maste return m_target_list.size();
497f034231aSEd Maste }
498f034231aSEd Maste
GetTargetAtIndex(uint32_t idx) const49914f1b3e8SDimitry Andric lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const {
500f034231aSEd Maste TargetSP target_sp;
501f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
502f034231aSEd Maste if (idx < m_target_list.size())
503f034231aSEd Maste target_sp = m_target_list[idx];
504f034231aSEd Maste return target_sp;
505f034231aSEd Maste }
506f034231aSEd Maste
GetIndexOfTarget(lldb::TargetSP target_sp) const50714f1b3e8SDimitry Andric uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const {
508f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
509b1c73532SDimitry Andric auto it = llvm::find(m_target_list, target_sp);
510b60736ecSDimitry Andric if (it != m_target_list.end())
511b60736ecSDimitry Andric return std::distance(m_target_list.begin(), it);
512f034231aSEd Maste return UINT32_MAX;
513f034231aSEd Maste }
514f034231aSEd Maste
AddTargetInternal(TargetSP target_sp,bool do_select)515b60736ecSDimitry Andric void TargetList::AddTargetInternal(TargetSP target_sp, bool do_select) {
5164b4fe385SDimitry Andric lldbassert(!llvm::is_contained(m_target_list, target_sp) &&
517b60736ecSDimitry Andric "target already exists it the list");
5187fa27ce4SDimitry Andric UnregisterInProcessTarget(target_sp);
519b60736ecSDimitry Andric m_target_list.push_back(std::move(target_sp));
520b60736ecSDimitry Andric if (do_select)
521b60736ecSDimitry Andric SetSelectedTargetInternal(m_target_list.size() - 1);
522b60736ecSDimitry Andric }
523b60736ecSDimitry Andric
SetSelectedTargetInternal(uint32_t index)524b60736ecSDimitry Andric void TargetList::SetSelectedTargetInternal(uint32_t index) {
525b60736ecSDimitry Andric lldbassert(!m_target_list.empty());
526b60736ecSDimitry Andric m_selected_target_idx = index < m_target_list.size() ? index : 0;
527b60736ecSDimitry Andric }
528b60736ecSDimitry Andric
SetSelectedTarget(uint32_t index)529b60736ecSDimitry Andric void TargetList::SetSelectedTarget(uint32_t index) {
530f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
531b60736ecSDimitry Andric SetSelectedTargetInternal(index);
532f034231aSEd Maste }
533b60736ecSDimitry Andric
SetSelectedTarget(const TargetSP & target_sp)534b60736ecSDimitry Andric void TargetList::SetSelectedTarget(const TargetSP &target_sp) {
535ac9a064cSDimitry Andric // Don't allow an invalid target shared pointer or a target that has been
536ac9a064cSDimitry Andric // destroyed to become the selected target.
537ac9a064cSDimitry Andric if (target_sp && target_sp->IsValid()) {
538b60736ecSDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
539b1c73532SDimitry Andric auto it = llvm::find(m_target_list, target_sp);
540b60736ecSDimitry Andric SetSelectedTargetInternal(std::distance(m_target_list.begin(), it));
541f034231aSEd Maste }
542ac9a064cSDimitry Andric }
543f034231aSEd Maste
GetSelectedTarget()54414f1b3e8SDimitry Andric lldb::TargetSP TargetList::GetSelectedTarget() {
545f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
546f034231aSEd Maste if (m_selected_target_idx >= m_target_list.size())
547f034231aSEd Maste m_selected_target_idx = 0;
548f034231aSEd Maste return GetTargetAtIndex(m_selected_target_idx);
549f034231aSEd Maste }
5507fa27ce4SDimitry Andric
AnyTargetContainsModule(Module & module)5517fa27ce4SDimitry Andric bool TargetList::AnyTargetContainsModule(Module &module) {
5527fa27ce4SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
5537fa27ce4SDimitry Andric for (const auto &target_sp : m_target_list) {
5547fa27ce4SDimitry Andric if (target_sp->GetImages().FindModule(&module))
5557fa27ce4SDimitry Andric return true;
5567fa27ce4SDimitry Andric }
5577fa27ce4SDimitry Andric for (const auto &target_sp: m_in_process_target_list) {
5587fa27ce4SDimitry Andric if (target_sp->GetImages().FindModule(&module))
5597fa27ce4SDimitry Andric return true;
5607fa27ce4SDimitry Andric }
5617fa27ce4SDimitry Andric return false;
5627fa27ce4SDimitry Andric }
5637fa27ce4SDimitry Andric
RegisterInProcessTarget(TargetSP target_sp)5647fa27ce4SDimitry Andric void TargetList::RegisterInProcessTarget(TargetSP target_sp) {
5657fa27ce4SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
5667fa27ce4SDimitry Andric [[maybe_unused]] bool was_added;
5677fa27ce4SDimitry Andric std::tie(std::ignore, was_added) =
5687fa27ce4SDimitry Andric m_in_process_target_list.insert(target_sp);
5697fa27ce4SDimitry Andric assert(was_added && "Target pointer was left in the in-process map");
5707fa27ce4SDimitry Andric }
5717fa27ce4SDimitry Andric
UnregisterInProcessTarget(TargetSP target_sp)5727fa27ce4SDimitry Andric void TargetList::UnregisterInProcessTarget(TargetSP target_sp) {
5737fa27ce4SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
5747fa27ce4SDimitry Andric [[maybe_unused]] bool was_present =
5757fa27ce4SDimitry Andric m_in_process_target_list.erase(target_sp);
5767fa27ce4SDimitry Andric assert(was_present && "Target pointer being removed was not registered");
5777fa27ce4SDimitry Andric }
5787fa27ce4SDimitry Andric
IsTargetInProcess(TargetSP target_sp)5797fa27ce4SDimitry Andric bool TargetList::IsTargetInProcess(TargetSP target_sp) {
5807fa27ce4SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
5817fa27ce4SDimitry Andric return m_in_process_target_list.count(target_sp) == 1;
5827fa27ce4SDimitry Andric }
583