blob: 2be32c53da9fa6e94d843880e19701ed76bd9b03 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- TargetList.cpp ----------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turnerfb1a0a02017-03-06 18:34:25 +00009#include "lldb/Target/TargetList.h"
Greg Claytonded470d2011-03-19 01:12:21 +000010#include "lldb/Core/Debugger.h"
Greg Clayton1f746072012-08-29 21:13:06 +000011#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000012#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Host/Host.h"
Jim Ingham893c9322014-11-22 01:42:44 +000014#include "lldb/Host/HostInfo.h"
Greg Claytonb3a40ba2012-03-20 18:34:04 +000015#include "lldb/Interpreter/CommandInterpreter.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000016#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000017#include "lldb/Symbol/ObjectFile.h"
Greg Claytone996fd32011-03-08 22:40:15 +000018#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Target/Process.h"
Pavel Labath181b8232018-12-14 15:59:49 +000020#include "lldb/Utility/Broadcaster.h"
21#include "lldb/Utility/Event.h"
Pavel Labathd821c992018-08-07 11:07:21 +000022#include "lldb/Utility/State.h"
Zachary Turner8d48cd62017-03-22 17:33:23 +000023#include "lldb/Utility/TildeExpressionResolver.h"
Pavel Labath38d06322017-06-29 14:32:17 +000024#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Pavel Labath1d5855b2017-01-23 15:56:45 +000026#include "llvm/ADT/SmallString.h"
27#include "llvm/Support/FileSystem.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029using namespace lldb;
30using namespace lldb_private;
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032ConstString &TargetList::GetStaticBroadcasterClass() {
33 static ConstString class_name("lldb.targetList");
34 return class_name;
Jim Ingham4bddaeb2012-02-16 06:50:00 +000035}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037// TargetList constructor
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000038TargetList::TargetList(Debugger &debugger)
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 : Broadcaster(debugger.GetBroadcasterManager(),
40 TargetList::GetStaticBroadcasterClass().AsCString()),
41 m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) {
42 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
Zachary Turner97206d52017-05-12 04:51:55 +000045Status TargetList::CreateTarget(Debugger &debugger,
46 llvm::StringRef user_exe_path,
47 llvm::StringRef triple_str,
Jonas Devliegheref9a07e92018-09-20 09:09:05 +000048 LoadDependentFiles load_dependent_files,
Zachary Turner97206d52017-05-12 04:51:55 +000049 const OptionGroupPlatform *platform_options,
50 TargetSP &target_sp) {
Zachary Turnera47464b2016-11-18 20:44:46 +000051 return CreateTargetInternal(debugger, user_exe_path, triple_str,
Vedant Kumar16e5a342020-11-05 11:38:50 -080052 load_dependent_files, platform_options,
53 target_sp);
Jim Ingham893c9322014-11-22 01:42:44 +000054}
55
Zachary Turner97206d52017-05-12 04:51:55 +000056Status TargetList::CreateTarget(Debugger &debugger,
57 llvm::StringRef user_exe_path,
58 const ArchSpec &specified_arch,
Jonas Devliegheref9a07e92018-09-20 09:09:05 +000059 LoadDependentFiles load_dependent_files,
Zachary Turner97206d52017-05-12 04:51:55 +000060 PlatformSP &platform_sp, TargetSP &target_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 return CreateTargetInternal(debugger, user_exe_path, specified_arch,
Vedant Kumar16e5a342020-11-05 11:38:50 -080062 load_dependent_files, platform_sp, target_sp);
Jim Ingham893c9322014-11-22 01:42:44 +000063}
64
Zachary Turner97206d52017-05-12 04:51:55 +000065Status TargetList::CreateTargetInternal(
Zachary Turnera47464b2016-11-18 20:44:46 +000066 Debugger &debugger, llvm::StringRef user_exe_path,
Jonas Devliegheref9a07e92018-09-20 09:09:05 +000067 llvm::StringRef triple_str, LoadDependentFiles load_dependent_files,
Vedant Kumar16e5a342020-11-05 11:38:50 -080068 const OptionGroupPlatform *platform_options, TargetSP &target_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +000069 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000070
Jonas Devlieghere4add8532020-07-29 08:59:57 -070071 // Let's start by looking at the selected platform.
72 PlatformSP platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
73
74 // This variable corresponds to the architecture specified by the triple
75 // string. If that string was empty the currently selected platform will
76 // determine the architecture.
Zachary Turnera47464b2016-11-18 20:44:46 +000077 const ArchSpec arch(triple_str);
Jonas Devlieghere4add8532020-07-29 08:59:57 -070078 if (!triple_str.empty() && !arch.IsValid()) {
79 error.SetErrorStringWithFormat("invalid triple '%s'",
80 triple_str.str().c_str());
81 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
Greg Claytonf4d6de62013-04-24 22:29:28 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 ArchSpec platform_arch(arch);
Vince Harron1b5a74e2015-01-21 22:42:49 +000085
Jonas Devlieghere4add8532020-07-29 08:59:57 -070086 // Create a new platform if a platform was specified in the platform options
87 // and doesn't match the selected platform.
88 if (platform_options && platform_options->PlatformWasSpecified() &&
89 !platform_options->PlatformMatches(platform_sp)) {
90 const bool select_platform = true;
91 platform_sp = platform_options->CreatePlatformWithOptions(
92 debugger.GetCommandInterpreter(), arch, select_platform, error,
93 platform_arch);
94 if (!platform_sp)
95 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 }
97
Jonas Devlieghere4add8532020-07-29 08:59:57 -070098 bool prefer_platform_arch = false;
Adrian Prantl0fa520a2020-08-06 10:52:16 -070099 auto update_platform_arch = [&](const ArchSpec &module_arch) {
100 // If the OS or vendor weren't specified, then adopt the module's
101 // architecture so that the platform matching can be more accurate.
102 if (!platform_arch.TripleOSWasSpecified() ||
103 !platform_arch.TripleVendorWasSpecified()) {
104 prefer_platform_arch = true;
105 platform_arch = module_arch;
106 }
107 };
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700109 if (!user_exe_path.empty()) {
110 ModuleSpec module_spec(FileSpec(user_exe_path, FileSpec::Style::native));
111 FileSystem::Instance().Resolve(module_spec.GetFileSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 // Resolve the executable in case we are given a path to a application
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700113 // bundle like a .app bundle on MacOSX.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 Host::ResolveExecutableInBundle(module_spec.GetFileSpec());
115
116 lldb::offset_t file_offset = 0;
117 lldb::offset_t file_size = 0;
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700118 ModuleSpecList module_specs;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 const size_t num_specs = ObjectFile::GetModuleSpecifications(
120 module_spec.GetFileSpec(), file_offset, file_size, module_specs);
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 if (num_specs > 0) {
123 ModuleSpec matching_module_spec;
124
125 if (num_specs == 1) {
126 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) {
127 if (platform_arch.IsValid()) {
128 if (platform_arch.IsCompatibleMatch(
129 matching_module_spec.GetArchitecture())) {
130 // If the OS or vendor weren't specified, then adopt the module's
Adrian Prantl05097242018-04-30 16:49:04 +0000131 // architecture so that the platform matching can be more
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700132 // accurate.
Adrian Prantl0fa520a2020-08-06 10:52:16 -0700133 update_platform_arch(matching_module_spec.GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 } else {
135 StreamString platform_arch_strm;
136 StreamString module_arch_strm;
137
Raphael Isemann2f1e7b32019-12-04 08:27:43 +0100138 platform_arch.DumpTriple(platform_arch_strm.AsRawOstream());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 matching_module_spec.GetArchitecture().DumpTriple(
Raphael Isemann2f1e7b32019-12-04 08:27:43 +0100140 module_arch_strm.AsRawOstream());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 error.SetErrorStringWithFormat(
142 "the specified architecture '%s' is not compatible with '%s' "
143 "in '%s'",
Zachary Turnerc1564272016-11-16 21:15:24 +0000144 platform_arch_strm.GetData(), module_arch_strm.GetData(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 module_spec.GetFileSpec().GetPath().c_str());
146 return error;
147 }
148 } else {
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700149 // Only one arch and none was specified.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 prefer_platform_arch = true;
151 platform_arch = matching_module_spec.GetArchitecture();
152 }
153 }
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700154 } else if (arch.IsValid()) {
Adrian Prantl0fa520a2020-08-06 10:52:16 -0700155 // Fat binary. A (valid) architecture was specified.
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700156 module_spec.GetArchitecture() = arch;
157 if (module_specs.FindMatchingModuleSpec(module_spec,
Adrian Prantl0fa520a2020-08-06 10:52:16 -0700158 matching_module_spec))
159 update_platform_arch(matching_module_spec.GetArchitecture());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 } else {
Adrian Prantl0fa520a2020-08-06 10:52:16 -0700161 // Fat binary. No architecture specified, check if there is
162 // only one platform for all of the architectures.
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700163 PlatformSP host_platform_sp = Platform::GetHostPlatform();
164 std::vector<PlatformSP> platforms;
165 for (size_t i = 0; i < num_specs; ++i) {
166 ModuleSpec module_spec;
167 if (module_specs.GetModuleSpecAtIndex(i, module_spec)) {
168 // First consider the platform specified by the user, if any, and
169 // the selected platform otherwise.
170 if (platform_sp) {
171 if (platform_sp->IsCompatibleArchitecture(
172 module_spec.GetArchitecture(), false, nullptr)) {
173 platforms.push_back(platform_sp);
174 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 }
176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700178 // Now consider the host platform if it is different from the
179 // specified/selected platform.
180 if (host_platform_sp &&
181 (!platform_sp ||
182 host_platform_sp->GetName() != platform_sp->GetName())) {
183 if (host_platform_sp->IsCompatibleArchitecture(
184 module_spec.GetArchitecture(), false, nullptr)) {
185 platforms.push_back(host_platform_sp);
186 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 }
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700188 }
189
190 // Finally find a platform that matches the architecture in the
191 // executable file.
192 PlatformSP fallback_platform_sp(
193 Platform::GetPlatformForArchitecture(
194 module_spec.GetArchitecture(), nullptr));
195 if (fallback_platform_sp) {
196 platforms.push_back(fallback_platform_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 }
198 }
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700199 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700201 Platform *platform_ptr = nullptr;
202 bool more_than_one_platforms = false;
203 for (const auto &the_platform_sp : platforms) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 if (platform_ptr) {
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700205 if (platform_ptr->GetName() != the_platform_sp->GetName()) {
206 more_than_one_platforms = true;
207 platform_ptr = nullptr;
208 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 }
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700210 } else {
211 platform_ptr = the_platform_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 }
213 }
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700214
215 if (platform_ptr) {
216 // All platforms for all modules in the executable match, so we can
217 // select this platform.
218 platform_sp = platforms.front();
219 } else if (!more_than_one_platforms) {
220 // No platforms claim to support this file.
221 error.SetErrorString("no matching platforms found for this file");
222 return error;
223 } else {
224 // More than one platform claims to support this file.
225 StreamString error_strm;
226 std::set<Platform *> platform_set;
227 error_strm.Printf(
228 "more than one platform supports this executable (");
229 for (const auto &the_platform_sp : platforms) {
230 if (platform_set.find(the_platform_sp.get()) ==
231 platform_set.end()) {
232 if (!platform_set.empty())
233 error_strm.PutCString(", ");
234 error_strm.PutCString(the_platform_sp->GetName().GetCString());
235 platform_set.insert(the_platform_sp.get());
236 }
237 }
238 error_strm.Printf("), specify an architecture to disambiguate");
239 error.SetErrorString(error_strm.GetString());
240 return error;
241 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 }
243 }
244 }
245
246 // If we have a valid architecture, make sure the current platform is
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700247 // compatible with that architecture.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 if (!prefer_platform_arch && arch.IsValid()) {
Adrian Prantl0fa520a2020-08-06 10:52:16 -0700249 if (!platform_sp->IsCompatibleArchitecture(arch, false, nullptr)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
Vedant Kumar16e5a342020-11-05 11:38:50 -0800251 if (platform_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
253 }
254 } else if (platform_arch.IsValid()) {
Jonas Devlieghere4add8532020-07-29 08:59:57 -0700255 // If "arch" isn't valid, yet "platform_arch" is, it means we have an
256 // executable file with a single architecture which should be used.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 ArchSpec fixed_platform_arch;
Adrian Prantl0fa520a2020-08-06 10:52:16 -0700258 if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, nullptr)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 platform_sp = Platform::GetPlatformForArchitecture(platform_arch,
260 &fixed_platform_arch);
Vedant Kumar16e5a342020-11-05 11:38:50 -0800261 if (platform_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
263 }
264 }
265
266 if (!platform_arch.IsValid())
267 platform_arch = arch;
268
Vedant Kumar16e5a342020-11-05 11:38:50 -0800269 return TargetList::CreateTargetInternal(debugger, user_exe_path,
270 platform_arch, load_dependent_files,
271 platform_sp, target_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272}
273
Zachary Turner97206d52017-05-12 04:51:55 +0000274Status TargetList::CreateTargetInternal(Debugger &debugger,
275 llvm::StringRef user_exe_path,
276 const ArchSpec &specified_arch,
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000277 LoadDependentFiles load_dependent_files,
Zachary Turner97206d52017-05-12 04:51:55 +0000278 lldb::PlatformSP &platform_sp,
Vedant Kumar16e5a342020-11-05 11:38:50 -0800279 lldb::TargetSP &target_sp) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000280 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
281 Timer scoped_timer(
282 func_cat, "TargetList::CreateTarget (file = '%s', arch = '%s')",
283 user_exe_path.str().c_str(), specified_arch.GetArchitectureName());
Zachary Turner97206d52017-05-12 04:51:55 +0000284 Status error;
Vedant Kumar16e5a342020-11-05 11:38:50 -0800285 const bool is_dummy_target = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286
287 ArchSpec arch(specified_arch);
288
289 if (arch.IsValid()) {
290 if (!platform_sp ||
291 !platform_sp->IsCompatibleArchitecture(arch, false, nullptr))
292 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
293 }
294
295 if (!platform_sp)
Vince Harron1b5a74e2015-01-21 22:42:49 +0000296 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
297
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 if (!arch.IsValid())
299 arch = specified_arch;
Greg Claytonc76fa8a2014-07-29 21:27:21 +0000300
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000301 FileSpec file(user_exe_path);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000302 if (!FileSystem::Instance().Exists(file) && user_exe_path.startswith("~")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 // we want to expand the tilde but we don't want to resolve any symbolic
Zachary Turner8d48cd62017-03-22 17:33:23 +0000304 // links so we can't use the FileSpec constructor's resolve flag
305 llvm::SmallString<64> unglobbed_path;
Jonas Devlieghere72787ac2018-11-09 01:59:28 +0000306 StandardTildeExpressionResolver Resolver;
307 Resolver.ResolveFullPath(user_exe_path, unglobbed_path);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 if (unglobbed_path.empty())
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000310 file = FileSpec(user_exe_path);
Greg Claytone996fd32011-03-08 22:40:15 +0000311 else
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000312 file = FileSpec(unglobbed_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 }
314
315 bool user_exe_path_is_bundle = false;
316 char resolved_bundle_exe_path[PATH_MAX];
317 resolved_bundle_exe_path[0] = '\0';
318 if (file) {
Jonas Devlieghere3a58d892018-11-08 00:14:50 +0000319 if (FileSystem::Instance().IsDirectory(file))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 user_exe_path_is_bundle = true;
321
Zachary Turnera47464b2016-11-18 20:44:46 +0000322 if (file.IsRelative() && !user_exe_path.empty()) {
Greg Claytond68dbd52018-05-07 14:21:04 +0000323 llvm::SmallString<64> cwd;
324 if (! llvm::sys::fs::current_path(cwd)) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000325 FileSpec cwd_file(cwd.c_str());
Greg Claytond68dbd52018-05-07 14:21:04 +0000326 cwd_file.AppendPathComponent(file);
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000327 if (FileSystem::Instance().Exists(cwd_file))
Greg Claytond68dbd52018-05-07 14:21:04 +0000328 file = cwd_file;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 }
Greg Claytone996fd32011-03-08 22:40:15 +0000330 }
331
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 ModuleSP exe_module_sp;
333 if (platform_sp) {
334 FileSpecList executable_search_paths(
335 Target::GetDefaultExecutableSearchPaths());
336 ModuleSpec module_spec(file, arch);
337 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp,
338 executable_search_paths.GetSize()
339 ? &executable_search_paths
340 : nullptr);
Jim Ingham5aee1622010-08-09 23:31:02 +0000341 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 if (error.Success() && exe_module_sp) {
344 if (exe_module_sp->GetObjectFile() == nullptr) {
345 if (arch.IsValid()) {
346 error.SetErrorStringWithFormat(
347 "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(),
348 arch.GetArchitectureName());
349 } else {
350 error.SetErrorStringWithFormat("unsupported file type \"%s\"",
351 file.GetPath().c_str());
352 }
353 return error;
354 }
355 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
Jonas Devliegheref9a07e92018-09-20 09:09:05 +0000356 target_sp->SetExecutableModule(exe_module_sp, load_dependent_files);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 if (user_exe_path_is_bundle)
358 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
359 sizeof(resolved_bundle_exe_path));
Levon Ter-Grigoryan631048e2020-05-14 16:05:40 +0200360 if (target_sp->GetPreloadSymbols())
361 exe_module_sp->PreloadSymbols();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 }
363 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000364 // No file was specified, just create an empty target with any arch if a
365 // valid arch was specified
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
367 }
368
Adrian Prantl5110fd02020-07-31 13:30:59 -0700369 if (!target_sp)
370 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371
Adrian Prantl5110fd02020-07-31 13:30:59 -0700372 // Set argv0 with what the user typed, unless the user specified a
373 // directory. If the user specified a directory, then it is probably a
374 // bundle that was resolved and we need to use the resolved bundle path
375 if (!user_exe_path.empty()) {
376 // Use exactly what the user typed as the first argument when we exec or
377 // posix_spawn
378 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
379 target_sp->SetArg0(resolved_bundle_exe_path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 } else {
Adrian Prantl5110fd02020-07-31 13:30:59 -0700381 // Use resolved path
382 target_sp->SetArg0(file.GetPath().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383 }
384 }
Adrian Prantl5110fd02020-07-31 13:30:59 -0700385 if (file.GetDirectory()) {
386 FileSpec file_dir;
387 file_dir.GetDirectory() = file.GetDirectory();
388 target_sp->AppendExecutableSearchPaths(file_dir);
389 }
390
Vedant Kumar16e5a342020-11-05 11:38:50 -0800391 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
392 m_selected_target_idx = m_target_list.size();
393 m_target_list.push_back(target_sp);
394 // Now prime this from the dummy target:
395 target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396
397 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398}
399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400bool TargetList::DeleteTarget(TargetSP &target_sp) {
401 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
402 collection::iterator pos, end = m_target_list.end();
403
404 for (pos = m_target_list.begin(); pos != end; ++pos) {
405 if (pos->get() == target_sp.get()) {
406 m_target_list.erase(pos);
407 return true;
408 }
409 }
410 return false;
411}
412
413TargetSP TargetList::FindTargetWithExecutableAndArchitecture(
414 const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const {
415 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
416 TargetSP target_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417 collection::const_iterator pos, end = m_target_list.end();
418 for (pos = m_target_list.begin(); pos != end; ++pos) {
419 Module *exe_module = (*pos)->GetExecutableModulePointer();
420
421 if (exe_module) {
Pavel Labath532290e2019-11-29 11:31:00 +0100422 if (FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 if (exe_arch_ptr) {
424 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
425 continue;
426 }
427 target_sp = *pos;
428 break;
429 }
430 }
431 }
432 return target_sp;
433}
434
435TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const {
436 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
437 TargetSP target_sp;
438 collection::const_iterator pos, end = m_target_list.end();
439 for (pos = m_target_list.begin(); pos != end; ++pos) {
440 Process *process = (*pos)->GetProcessSP().get();
441 if (process && process->GetID() == pid) {
442 target_sp = *pos;
443 break;
444 }
445 }
446 return target_sp;
447}
448
449TargetSP TargetList::FindTargetWithProcess(Process *process) const {
450 TargetSP target_sp;
451 if (process) {
452 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
453 collection::const_iterator pos, end = m_target_list.end();
454 for (pos = m_target_list.begin(); pos != end; ++pos) {
455 if (process == (*pos)->GetProcessSP().get()) {
456 target_sp = *pos;
457 break;
458 }
459 }
460 }
461 return target_sp;
462}
463
464TargetSP TargetList::GetTargetSP(Target *target) const {
465 TargetSP target_sp;
466 if (target) {
467 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
468 collection::const_iterator pos, end = m_target_list.end();
469 for (pos = m_target_list.begin(); pos != end; ++pos) {
470 if (target == (*pos).get()) {
471 target_sp = *pos;
472 break;
473 }
474 }
475 }
476 return target_sp;
477}
478
479uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) {
480 uint32_t num_async_interrupts_sent = 0;
481
482 if (pid != LLDB_INVALID_PROCESS_ID) {
483 TargetSP target_sp(FindTargetWithProcessID(pid));
484 if (target_sp) {
485 Process *process = target_sp->GetProcessSP().get();
486 if (process) {
487 process->SendAsyncInterrupt();
488 ++num_async_interrupts_sent;
489 }
490 }
491 } else {
492 // We don't have a valid pid to broadcast to, so broadcast to the target
493 // list's async broadcaster...
494 BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr);
495 }
496
497 return num_async_interrupts_sent;
498}
499
500uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) {
501 uint32_t num_signals_sent = 0;
502 Process *process = nullptr;
503 if (pid == LLDB_INVALID_PROCESS_ID) {
504 // Signal all processes with signal
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000505 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506 collection::iterator pos, end = m_target_list.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 for (pos = m_target_list.begin(); pos != end; ++pos) {
508 process = (*pos)->GetProcessSP().get();
509 if (process) {
510 if (process->IsAlive()) {
511 ++num_signals_sent;
512 process->Signal(signo);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 } else {
517 // Signal a specific process with signal
518 TargetSP target_sp(FindTargetWithProcessID(pid));
519 if (target_sp) {
520 process = target_sp->GetProcessSP().get();
521 if (process) {
522 if (process->IsAlive()) {
523 ++num_signals_sent;
524 process->Signal(signo);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000526 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 }
529 return num_signals_sent;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530}
531
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532int TargetList::GetNumTargets() const {
533 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
534 return m_target_list.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535}
536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const {
538 TargetSP target_sp;
539 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
540 if (idx < m_target_list.size())
541 target_sp = m_target_list[idx];
542 return target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000543}
544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const {
546 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
547 size_t num_targets = m_target_list.size();
548 for (size_t idx = 0; idx < num_targets; idx++) {
549 if (target_sp == m_target_list[idx])
550 return idx;
551 }
552 return UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553}
554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555uint32_t TargetList::SetSelectedTarget(Target *target) {
556 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
557 collection::const_iterator pos, begin = m_target_list.begin(),
558 end = m_target_list.end();
559 for (pos = begin; pos != end; ++pos) {
560 if (pos->get() == target) {
561 m_selected_target_idx = std::distance(begin, pos);
562 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 }
565 m_selected_target_idx = 0;
566 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567}
568
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569lldb::TargetSP TargetList::GetSelectedTarget() {
570 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
571 if (m_selected_target_idx >= m_target_list.size())
Jim Ingham2976d002010-08-26 21:32:51 +0000572 m_selected_target_idx = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 return GetTargetAtIndex(m_selected_target_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574}