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