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