blob: 12307b6ac0646fcaa8489c93737d6616a90cd563 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- TargetList.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// Project includes
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000011#include "lldb/Target/TargetList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Broadcaster.h"
Greg Claytonded470d2011-03-19 01:12:21 +000013#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Event.h"
Greg Clayton1f746072012-08-29 21:13:06 +000015#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000016#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Host/Host.h"
Jim Ingham893c9322014-11-22 01:42:44 +000019#include "lldb/Host/HostInfo.h"
Greg Claytonb3a40ba2012-03-20 18:34:04 +000020#include "lldb/Interpreter/CommandInterpreter.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000021#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000022#include "lldb/Symbol/ObjectFile.h"
Greg Claytone996fd32011-03-08 22:40:15 +000023#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
Zachary Turner8d48cd62017-03-22 17:33:23 +000025#include "lldb/Utility/TildeExpressionResolver.h"
Pavel Labath38d06322017-06-29 14:32:17 +000026#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Pavel Labath1d5855b2017-01-23 15:56:45 +000028// Other libraries and framework includes
29#include "llvm/ADT/SmallString.h"
30#include "llvm/Support/FileSystem.h"
31
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032using namespace lldb;
33using namespace lldb_private;
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035ConstString &TargetList::GetStaticBroadcasterClass() {
36 static ConstString class_name("lldb.targetList");
37 return class_name;
Jim Ingham4bddaeb2012-02-16 06:50:00 +000038}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
40//----------------------------------------------------------------------
41// TargetList constructor
42//----------------------------------------------------------------------
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000043TargetList::TargetList(Debugger &debugger)
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 : Broadcaster(debugger.GetBroadcasterManager(),
45 TargetList::GetStaticBroadcasterClass().AsCString()),
46 m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) {
47 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048}
49
50//----------------------------------------------------------------------
51// Destructor
52//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000053TargetList::~TargetList() {
54 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
55 m_target_list.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Zachary Turner97206d52017-05-12 04:51:55 +000058Status TargetList::CreateTarget(Debugger &debugger,
59 llvm::StringRef user_exe_path,
60 llvm::StringRef triple_str,
61 bool get_dependent_files,
62 const OptionGroupPlatform *platform_options,
63 TargetSP &target_sp) {
Zachary Turnera47464b2016-11-18 20:44:46 +000064 return CreateTargetInternal(debugger, user_exe_path, triple_str,
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 get_dependent_files, platform_options, target_sp,
66 false);
Jim Ingham893c9322014-11-22 01:42:44 +000067}
68
Zachary Turner97206d52017-05-12 04:51:55 +000069Status TargetList::CreateTarget(Debugger &debugger,
70 llvm::StringRef user_exe_path,
71 const ArchSpec &specified_arch,
72 bool get_dependent_files,
73 PlatformSP &platform_sp, TargetSP &target_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 return CreateTargetInternal(debugger, user_exe_path, specified_arch,
75 get_dependent_files, platform_sp, target_sp,
76 false);
Jim Ingham893c9322014-11-22 01:42:44 +000077}
78
Zachary Turner97206d52017-05-12 04:51:55 +000079Status TargetList::CreateTargetInternal(
Zachary Turnera47464b2016-11-18 20:44:46 +000080 Debugger &debugger, llvm::StringRef user_exe_path,
81 llvm::StringRef triple_str, bool get_dependent_files,
82 const OptionGroupPlatform *platform_options, TargetSP &target_sp,
83 bool is_dummy_target) {
Zachary Turner97206d52017-05-12 04:51:55 +000084 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 PlatformSP platform_sp;
86
Adrian Prantl05097242018-04-30 16:49:04 +000087 // This is purposely left empty unless it is specified by triple_cstr. If not
88 // initialized via triple_cstr, then the currently selected platform will set
89 // the architecture correctly.
Zachary Turnera47464b2016-11-18 20:44:46 +000090 const ArchSpec arch(triple_str);
91 if (!triple_str.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 if (!arch.IsValid()) {
Zachary Turnera47464b2016-11-18 20:44:46 +000093 error.SetErrorStringWithFormat("invalid triple '%s'",
94 triple_str.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 return error;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000096 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 }
Greg Claytonf4d6de62013-04-24 22:29:28 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 ArchSpec platform_arch(arch);
Vince Harron1b5a74e2015-01-21 22:42:49 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 bool prefer_platform_arch = false;
102
103 CommandInterpreter &interpreter = debugger.GetCommandInterpreter();
104
105 // let's see if there is already an existing plaform before we go creating
106 // another...
107 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
108
109 if (platform_options && platform_options->PlatformWasSpecified()) {
110 // Create a new platform if it doesn't match the selected platform
111 if (!platform_options->PlatformMatches(platform_sp)) {
112 const bool select_platform = true;
113 platform_sp = platform_options->CreatePlatformWithOptions(
114 interpreter, arch, select_platform, error, platform_arch);
115 if (!platform_sp)
116 return error;
117 }
118 }
119
Zachary Turnera47464b2016-11-18 20:44:46 +0000120 if (!user_exe_path.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 ModuleSpecList module_specs;
122 ModuleSpec module_spec;
123 module_spec.GetFileSpec().SetFile(user_exe_path, true);
124
125 // Resolve the executable in case we are given a path to a application
Adrian Prantl05097242018-04-30 16:49:04 +0000126 // bundle like a .app bundle on MacOSX
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 Host::ResolveExecutableInBundle(module_spec.GetFileSpec());
128
129 lldb::offset_t file_offset = 0;
130 lldb::offset_t file_size = 0;
131 const size_t num_specs = ObjectFile::GetModuleSpecifications(
132 module_spec.GetFileSpec(), file_offset, file_size, module_specs);
133 if (num_specs > 0) {
134 ModuleSpec matching_module_spec;
135
136 if (num_specs == 1) {
137 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) {
138 if (platform_arch.IsValid()) {
139 if (platform_arch.IsCompatibleMatch(
140 matching_module_spec.GetArchitecture())) {
141 // If the OS or vendor weren't specified, then adopt the module's
Adrian Prantl05097242018-04-30 16:49:04 +0000142 // architecture so that the platform matching can be more
143 // accurate
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 if (!platform_arch.TripleOSWasSpecified() ||
145 !platform_arch.TripleVendorWasSpecified()) {
146 prefer_platform_arch = true;
147 platform_arch = matching_module_spec.GetArchitecture();
148 }
149 } else {
150 StreamString platform_arch_strm;
151 StreamString module_arch_strm;
152
153 platform_arch.DumpTriple(platform_arch_strm);
154 matching_module_spec.GetArchitecture().DumpTriple(
155 module_arch_strm);
156 error.SetErrorStringWithFormat(
157 "the specified architecture '%s' is not compatible with '%s' "
158 "in '%s'",
Zachary Turnerc1564272016-11-16 21:15:24 +0000159 platform_arch_strm.GetData(), module_arch_strm.GetData(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 module_spec.GetFileSpec().GetPath().c_str());
161 return error;
162 }
163 } else {
164 // Only one arch and none was specified
165 prefer_platform_arch = true;
166 platform_arch = matching_module_spec.GetArchitecture();
167 }
168 }
169 } else {
170 if (arch.IsValid()) {
171 module_spec.GetArchitecture() = arch;
172 if (module_specs.FindMatchingModuleSpec(module_spec,
173 matching_module_spec)) {
174 prefer_platform_arch = true;
175 platform_arch = matching_module_spec.GetArchitecture();
176 }
177 } else {
178 // No architecture specified, check if there is only one platform for
179 // all of the architectures.
180
181 typedef std::vector<PlatformSP> PlatformList;
182 PlatformList platforms;
183 PlatformSP host_platform_sp = Platform::GetHostPlatform();
184 for (size_t i = 0; i < num_specs; ++i) {
185 ModuleSpec module_spec;
186 if (module_specs.GetModuleSpecAtIndex(i, module_spec)) {
187 // See if there was a selected platform and check that first
188 // since the user may have specified it.
189 if (platform_sp) {
190 if (platform_sp->IsCompatibleArchitecture(
191 module_spec.GetArchitecture(), false, nullptr)) {
192 platforms.push_back(platform_sp);
193 continue;
194 }
195 }
196
Adrian Prantl05097242018-04-30 16:49:04 +0000197 // Next check the host platform it if wasn't already checked
198 // above
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 if (host_platform_sp &&
200 (!platform_sp ||
201 host_platform_sp->GetName() != platform_sp->GetName())) {
202 if (host_platform_sp->IsCompatibleArchitecture(
203 module_spec.GetArchitecture(), false, nullptr)) {
204 platforms.push_back(host_platform_sp);
205 continue;
206 }
207 }
208
209 // Just find a platform that matches the architecture in the
210 // executable file
211 PlatformSP fallback_platform_sp(
212 Platform::GetPlatformForArchitecture(
213 module_spec.GetArchitecture(), nullptr));
214 if (fallback_platform_sp) {
215 platforms.push_back(fallback_platform_sp);
216 }
217 }
218 }
219
220 Platform *platform_ptr = nullptr;
221 bool more_than_one_platforms = false;
222 for (const auto &the_platform_sp : platforms) {
223 if (platform_ptr) {
224 if (platform_ptr->GetName() != the_platform_sp->GetName()) {
225 more_than_one_platforms = true;
226 platform_ptr = nullptr;
227 break;
228 }
229 } else {
230 platform_ptr = the_platform_sp.get();
231 }
232 }
233
234 if (platform_ptr) {
235 // All platforms for all modules in the exectuable match, so we can
236 // select this platform
237 platform_sp = platforms.front();
238 } else if (more_than_one_platforms == false) {
239 // No platforms claim to support this file
240 error.SetErrorString("No matching platforms found for this file, "
241 "specify one with the --platform option");
242 return error;
243 } else {
244 // More than one platform claims to support this file, so the
245 // --platform option must be specified
246 StreamString error_strm;
247 std::set<Platform *> platform_set;
248 error_strm.Printf(
249 "more than one platform supports this executable (");
250 for (const auto &the_platform_sp : platforms) {
251 if (platform_set.find(the_platform_sp.get()) ==
252 platform_set.end()) {
253 if (!platform_set.empty())
254 error_strm.PutCString(", ");
255 error_strm.PutCString(the_platform_sp->GetName().GetCString());
256 platform_set.insert(the_platform_sp.get());
257 }
258 }
259 error_strm.Printf(
260 "), use the --platform option to specify a platform");
Zachary Turnerc1564272016-11-16 21:15:24 +0000261 error.SetErrorString(error_strm.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 return error;
263 }
264 }
265 }
266 }
267 }
268
269 // If we have a valid architecture, make sure the current platform is
270 // compatible with that architecture
271 if (!prefer_platform_arch && arch.IsValid()) {
272 if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) {
273 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
274 if (!is_dummy_target && platform_sp)
275 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
276 }
277 } else if (platform_arch.IsValid()) {
278 // if "arch" isn't valid, yet "platform_arch" is, it means we have an
Adrian Prantl05097242018-04-30 16:49:04 +0000279 // executable file with a single architecture which should be used
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 ArchSpec fixed_platform_arch;
281 if (!platform_sp->IsCompatibleArchitecture(platform_arch, false,
282 &fixed_platform_arch)) {
283 platform_sp = Platform::GetPlatformForArchitecture(platform_arch,
284 &fixed_platform_arch);
285 if (!is_dummy_target && platform_sp)
286 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
287 }
288 }
289
290 if (!platform_arch.IsValid())
291 platform_arch = arch;
292
293 error = TargetList::CreateTargetInternal(
294 debugger, user_exe_path, platform_arch, get_dependent_files, platform_sp,
295 target_sp, is_dummy_target);
296 return error;
297}
298
299lldb::TargetSP TargetList::GetDummyTarget(lldb_private::Debugger &debugger) {
300 // FIXME: Maybe the dummy target should be per-Debugger
301 if (!m_dummy_target_sp || !m_dummy_target_sp->IsValid()) {
302 ArchSpec arch(Target::GetDefaultArchitecture());
303 if (!arch.IsValid())
304 arch = HostInfo::GetArchitecture();
Zachary Turner97206d52017-05-12 04:51:55 +0000305 Status err = CreateDummyTarget(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 debugger, arch.GetTriple().getTriple().c_str(), m_dummy_target_sp);
307 }
308
309 return m_dummy_target_sp;
310}
311
Zachary Turner97206d52017-05-12 04:51:55 +0000312Status TargetList::CreateDummyTarget(Debugger &debugger,
313 llvm::StringRef specified_arch_name,
314 lldb::TargetSP &target_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 PlatformSP host_platform_sp(Platform::GetHostPlatform());
316 return CreateTargetInternal(
317 debugger, (const char *)nullptr, specified_arch_name, false,
318 (const OptionGroupPlatform *)nullptr, target_sp, true);
319}
320
Zachary Turner97206d52017-05-12 04:51:55 +0000321Status TargetList::CreateTargetInternal(Debugger &debugger,
322 llvm::StringRef user_exe_path,
323 const ArchSpec &specified_arch,
324 bool get_dependent_files,
325 lldb::PlatformSP &platform_sp,
326 lldb::TargetSP &target_sp,
327 bool is_dummy_target) {
Pavel Labathf9d16472017-05-15 13:02:37 +0000328 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
329 Timer scoped_timer(
330 func_cat, "TargetList::CreateTarget (file = '%s', arch = '%s')",
331 user_exe_path.str().c_str(), specified_arch.GetArchitectureName());
Zachary Turner97206d52017-05-12 04:51:55 +0000332 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333
334 ArchSpec arch(specified_arch);
335
336 if (arch.IsValid()) {
337 if (!platform_sp ||
338 !platform_sp->IsCompatibleArchitecture(arch, false, nullptr))
339 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
340 }
341
342 if (!platform_sp)
Vince Harron1b5a74e2015-01-21 22:42:49 +0000343 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
344
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 if (!arch.IsValid())
346 arch = specified_arch;
Greg Claytonc76fa8a2014-07-29 21:27:21 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 FileSpec file(user_exe_path, false);
Zachary Turnera47464b2016-11-18 20:44:46 +0000349 if (!file.Exists() && user_exe_path.startswith("~")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 // we want to expand the tilde but we don't want to resolve any symbolic
Zachary Turner8d48cd62017-03-22 17:33:23 +0000351 // links so we can't use the FileSpec constructor's resolve flag
352 llvm::SmallString<64> unglobbed_path;
353 StandardTildeExpressionResolver Resolver;
354 Resolver.ResolveFullPath(user_exe_path, unglobbed_path);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 if (unglobbed_path.empty())
357 file = FileSpec(user_exe_path, false);
Greg Claytone996fd32011-03-08 22:40:15 +0000358 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 file = FileSpec(unglobbed_path.c_str(), false);
360 }
361
362 bool user_exe_path_is_bundle = false;
363 char resolved_bundle_exe_path[PATH_MAX];
364 resolved_bundle_exe_path[0] = '\0';
365 if (file) {
Zachary Turner7d86ee52017-03-08 17:56:08 +0000366 if (llvm::sys::fs::is_directory(file.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 user_exe_path_is_bundle = true;
368
Zachary Turnera47464b2016-11-18 20:44:46 +0000369 if (file.IsRelative() && !user_exe_path.empty()) {
Greg Claytond68dbd52018-05-07 14:21:04 +0000370 llvm::SmallString<64> cwd;
371 if (! llvm::sys::fs::current_path(cwd)) {
372 FileSpec cwd_file(cwd.c_str(), false);
373 cwd_file.AppendPathComponent(file);
374 if (cwd_file.Exists())
375 file = cwd_file;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 }
Greg Claytone996fd32011-03-08 22:40:15 +0000377 }
378
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 ModuleSP exe_module_sp;
380 if (platform_sp) {
381 FileSpecList executable_search_paths(
382 Target::GetDefaultExecutableSearchPaths());
383 ModuleSpec module_spec(file, arch);
384 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp,
385 executable_search_paths.GetSize()
386 ? &executable_search_paths
387 : nullptr);
Jim Ingham5aee1622010-08-09 23:31:02 +0000388 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 if (error.Success() && exe_module_sp) {
391 if (exe_module_sp->GetObjectFile() == nullptr) {
392 if (arch.IsValid()) {
393 error.SetErrorStringWithFormat(
394 "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(),
395 arch.GetArchitectureName());
396 } else {
397 error.SetErrorStringWithFormat("unsupported file type \"%s\"",
398 file.GetPath().c_str());
399 }
400 return error;
401 }
402 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
403 target_sp->SetExecutableModule(exe_module_sp, get_dependent_files);
404 if (user_exe_path_is_bundle)
405 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
406 sizeof(resolved_bundle_exe_path));
407 }
408 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000409 // No file was specified, just create an empty target with any arch if a
410 // valid arch was specified
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
412 }
413
414 if (target_sp) {
415 // Set argv0 with what the user typed, unless the user specified a
416 // directory. If the user specified a directory, then it is probably a
417 // bundle that was resolved and we need to use the resolved bundle path
Zachary Turnera47464b2016-11-18 20:44:46 +0000418 if (!user_exe_path.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 // Use exactly what the user typed as the first argument when we exec or
420 // posix_spawn
421 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
422 target_sp->SetArg0(resolved_bundle_exe_path);
423 } else {
424 // Use resolved path
425 target_sp->SetArg0(file.GetPath().c_str());
426 }
427 }
428 if (file.GetDirectory()) {
429 FileSpec file_dir;
430 file_dir.GetDirectory() = file.GetDirectory();
431 target_sp->GetExecutableSearchPaths().Append(file_dir);
432 }
433
434 // Don't put the dummy target in the target list, it's held separately.
435 if (!is_dummy_target) {
436 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
437 m_selected_target_idx = m_target_list.size();
438 m_target_list.push_back(target_sp);
439 // Now prime this from the dummy target:
440 target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget());
441 } else {
442 m_dummy_target_sp = target_sp;
443 }
444 }
445
446 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449bool TargetList::DeleteTarget(TargetSP &target_sp) {
450 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
451 collection::iterator pos, end = m_target_list.end();
452
453 for (pos = m_target_list.begin(); pos != end; ++pos) {
454 if (pos->get() == target_sp.get()) {
455 m_target_list.erase(pos);
456 return true;
457 }
458 }
459 return false;
460}
461
462TargetSP TargetList::FindTargetWithExecutableAndArchitecture(
463 const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const {
464 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
465 TargetSP target_sp;
466 bool full_match = (bool)exe_file_spec.GetDirectory();
467
468 collection::const_iterator pos, end = m_target_list.end();
469 for (pos = m_target_list.begin(); pos != end; ++pos) {
470 Module *exe_module = (*pos)->GetExecutableModulePointer();
471
472 if (exe_module) {
473 if (FileSpec::Equal(exe_file_spec, exe_module->GetFileSpec(),
474 full_match)) {
475 if (exe_arch_ptr) {
476 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
477 continue;
478 }
479 target_sp = *pos;
480 break;
481 }
482 }
483 }
484 return target_sp;
485}
486
487TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const {
488 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
489 TargetSP target_sp;
490 collection::const_iterator pos, end = m_target_list.end();
491 for (pos = m_target_list.begin(); pos != end; ++pos) {
492 Process *process = (*pos)->GetProcessSP().get();
493 if (process && process->GetID() == pid) {
494 target_sp = *pos;
495 break;
496 }
497 }
498 return target_sp;
499}
500
501TargetSP TargetList::FindTargetWithProcess(Process *process) const {
502 TargetSP target_sp;
503 if (process) {
504 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
505 collection::const_iterator pos, end = m_target_list.end();
506 for (pos = m_target_list.begin(); pos != end; ++pos) {
507 if (process == (*pos)->GetProcessSP().get()) {
508 target_sp = *pos;
509 break;
510 }
511 }
512 }
513 return target_sp;
514}
515
516TargetSP TargetList::GetTargetSP(Target *target) const {
517 TargetSP target_sp;
518 if (target) {
519 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
520 collection::const_iterator pos, end = m_target_list.end();
521 for (pos = m_target_list.begin(); pos != end; ++pos) {
522 if (target == (*pos).get()) {
523 target_sp = *pos;
524 break;
525 }
526 }
527 }
528 return target_sp;
529}
530
531uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) {
532 uint32_t num_async_interrupts_sent = 0;
533
534 if (pid != LLDB_INVALID_PROCESS_ID) {
535 TargetSP target_sp(FindTargetWithProcessID(pid));
536 if (target_sp) {
537 Process *process = target_sp->GetProcessSP().get();
538 if (process) {
539 process->SendAsyncInterrupt();
540 ++num_async_interrupts_sent;
541 }
542 }
543 } else {
544 // We don't have a valid pid to broadcast to, so broadcast to the target
545 // list's async broadcaster...
546 BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr);
547 }
548
549 return num_async_interrupts_sent;
550}
551
552uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) {
553 uint32_t num_signals_sent = 0;
554 Process *process = nullptr;
555 if (pid == LLDB_INVALID_PROCESS_ID) {
556 // Signal all processes with signal
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000557 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 collection::iterator pos, end = m_target_list.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 for (pos = m_target_list.begin(); pos != end; ++pos) {
560 process = (*pos)->GetProcessSP().get();
561 if (process) {
562 if (process->IsAlive()) {
563 ++num_signals_sent;
564 process->Signal(signo);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 } else {
569 // Signal a specific process with signal
570 TargetSP target_sp(FindTargetWithProcessID(pid));
571 if (target_sp) {
572 process = target_sp->GetProcessSP().get();
573 if (process) {
574 if (process->IsAlive()) {
575 ++num_signals_sent;
576 process->Signal(signo);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000578 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 }
581 return num_signals_sent;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582}
583
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584int TargetList::GetNumTargets() const {
585 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
586 return m_target_list.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const {
590 TargetSP target_sp;
591 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
592 if (idx < m_target_list.size())
593 target_sp = m_target_list[idx];
594 return target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595}
596
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const {
598 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
599 size_t num_targets = m_target_list.size();
600 for (size_t idx = 0; idx < num_targets; idx++) {
601 if (target_sp == m_target_list[idx])
602 return idx;
603 }
604 return UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605}
606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607uint32_t TargetList::SetSelectedTarget(Target *target) {
608 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
609 collection::const_iterator pos, begin = m_target_list.begin(),
610 end = m_target_list.end();
611 for (pos = begin; pos != end; ++pos) {
612 if (pos->get() == target) {
613 m_selected_target_idx = std::distance(begin, pos);
614 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 }
617 m_selected_target_idx = 0;
618 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619}
620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621lldb::TargetSP TargetList::GetSelectedTarget() {
622 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
623 if (m_selected_target_idx >= m_target_list.size())
Jim Ingham2976d002010-08-26 21:32:51 +0000624 m_selected_target_idx = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 return GetTargetAtIndex(m_selected_target_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626}