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