blob: 0637cffddcc19eb7a5b927b8f8d3b0ca8aae4222 [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
10// C Includes
11// C++ Includes
12// Other libraries and framework includes
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000013#include "llvm/ADT/SmallString.h"
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015// Project includes
16#include "lldb/Core/Broadcaster.h"
Greg Claytonded470d2011-03-19 01:12:21 +000017#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Event.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000020#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/State.h"
22#include "lldb/Core/Timer.h"
23#include "lldb/Host/Host.h"
Jim Ingham893c9322014-11-22 01:42:44 +000024#include "lldb/Host/HostInfo.h"
Greg Claytonb3a40ba2012-03-20 18:34:04 +000025#include "lldb/Interpreter/CommandInterpreter.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000026#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000027#include "lldb/Symbol/ObjectFile.h"
Greg Claytone996fd32011-03-08 22:40:15 +000028#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
30#include "lldb/Target/TargetList.h"
31
32using 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 Turnera47464b2016-11-18 20:44:46 +000058Error TargetList::CreateTarget(Debugger &debugger,
59 llvm::StringRef user_exe_path,
60 llvm::StringRef triple_str,
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 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 Turnera47464b2016-11-18 20:44:46 +000069Error TargetList::CreateTarget(Debugger &debugger,
70 llvm::StringRef user_exe_path,
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 const ArchSpec &specified_arch,
72 bool get_dependent_files,
73 PlatformSP &platform_sp, TargetSP &target_sp) {
74 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000079Error 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) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 Error error;
85 PlatformSP platform_sp;
86
87 // This is purposely left empty unless it is specified by triple_cstr.
88 // If not initialized via triple_cstr, then the currently selected platform
89 // will set 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
126 // bundle
127 // like a .app bundle on MacOSX
128 Host::ResolveExecutableInBundle(module_spec.GetFileSpec());
129
130 lldb::offset_t file_offset = 0;
131 lldb::offset_t file_size = 0;
132 const size_t num_specs = ObjectFile::GetModuleSpecifications(
133 module_spec.GetFileSpec(), file_offset, file_size, module_specs);
134 if (num_specs > 0) {
135 ModuleSpec matching_module_spec;
136
137 if (num_specs == 1) {
138 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) {
139 if (platform_arch.IsValid()) {
140 if (platform_arch.IsCompatibleMatch(
141 matching_module_spec.GetArchitecture())) {
142 // If the OS or vendor weren't specified, then adopt the module's
143 // architecture so that the platform matching can be more accurate
144 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
197 // Next check the host platform it if wasn't already checked above
198 if (host_platform_sp &&
199 (!platform_sp ||
200 host_platform_sp->GetName() != platform_sp->GetName())) {
201 if (host_platform_sp->IsCompatibleArchitecture(
202 module_spec.GetArchitecture(), false, nullptr)) {
203 platforms.push_back(host_platform_sp);
204 continue;
205 }
206 }
207
208 // Just find a platform that matches the architecture in the
209 // executable file
210 PlatformSP fallback_platform_sp(
211 Platform::GetPlatformForArchitecture(
212 module_spec.GetArchitecture(), nullptr));
213 if (fallback_platform_sp) {
214 platforms.push_back(fallback_platform_sp);
215 }
216 }
217 }
218
219 Platform *platform_ptr = nullptr;
220 bool more_than_one_platforms = false;
221 for (const auto &the_platform_sp : platforms) {
222 if (platform_ptr) {
223 if (platform_ptr->GetName() != the_platform_sp->GetName()) {
224 more_than_one_platforms = true;
225 platform_ptr = nullptr;
226 break;
227 }
228 } else {
229 platform_ptr = the_platform_sp.get();
230 }
231 }
232
233 if (platform_ptr) {
234 // All platforms for all modules in the exectuable match, so we can
235 // select this platform
236 platform_sp = platforms.front();
237 } else if (more_than_one_platforms == false) {
238 // No platforms claim to support this file
239 error.SetErrorString("No matching platforms found for this file, "
240 "specify one with the --platform option");
241 return error;
242 } else {
243 // More than one platform claims to support this file, so the
244 // --platform option must be specified
245 StreamString error_strm;
246 std::set<Platform *> platform_set;
247 error_strm.Printf(
248 "more than one platform supports this executable (");
249 for (const auto &the_platform_sp : platforms) {
250 if (platform_set.find(the_platform_sp.get()) ==
251 platform_set.end()) {
252 if (!platform_set.empty())
253 error_strm.PutCString(", ");
254 error_strm.PutCString(the_platform_sp->GetName().GetCString());
255 platform_set.insert(the_platform_sp.get());
256 }
257 }
258 error_strm.Printf(
259 "), use the --platform option to specify a platform");
Zachary Turnerc1564272016-11-16 21:15:24 +0000260 error.SetErrorString(error_strm.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 return error;
262 }
263 }
264 }
265 }
266 }
267
268 // If we have a valid architecture, make sure the current platform is
269 // compatible with that architecture
270 if (!prefer_platform_arch && arch.IsValid()) {
271 if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) {
272 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
273 if (!is_dummy_target && platform_sp)
274 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
275 }
276 } else if (platform_arch.IsValid()) {
277 // if "arch" isn't valid, yet "platform_arch" is, it means we have an
278 // executable file with
279 // a single architecture which should be used
280 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();
305 Error err = CreateDummyTarget(
306 debugger, arch.GetTriple().getTriple().c_str(), m_dummy_target_sp);
307 }
308
309 return m_dummy_target_sp;
310}
311
312Error TargetList::CreateDummyTarget(Debugger &debugger,
Zachary Turnera47464b2016-11-18 20:44:46 +0000313 llvm::StringRef specified_arch_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 lldb::TargetSP &target_sp) {
315 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
321Error TargetList::CreateTargetInternal(Debugger &debugger,
Zachary Turnera47464b2016-11-18 20:44:46 +0000322 llvm::StringRef user_exe_path,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 const ArchSpec &specified_arch,
324 bool get_dependent_files,
325 lldb::PlatformSP &platform_sp,
326 lldb::TargetSP &target_sp,
327 bool is_dummy_target) {
328 Timer scoped_timer(LLVM_PRETTY_FUNCTION,
329 "TargetList::CreateTarget (file = '%s', arch = '%s')",
Zachary Turner72f4997c2016-11-19 00:50:29 +0000330 user_exe_path.str().c_str(),
331 specified_arch.GetArchitectureName());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 Error error;
333
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
351 // links
352 // so we can't use the FileSpec constructor's resolve flag
353 llvm::SmallString<64> unglobbed_path(user_exe_path);
354 FileSpec::ResolveUsername(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) {
366 if (file.GetFileType() == FileSpec::eFileTypeDirectory)
367 user_exe_path_is_bundle = true;
368
Zachary Turnera47464b2016-11-18 20:44:46 +0000369 if (file.IsRelative() && !user_exe_path.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 // Ignore paths that start with "./" and "../"
Zachary Turnera47464b2016-11-18 20:44:46 +0000371 if (!user_exe_path.startswith("./") && !user_exe_path.startswith("../")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 char cwd[PATH_MAX];
373 if (getcwd(cwd, sizeof(cwd))) {
374 std::string cwd_user_exe_path(cwd);
375 cwd_user_exe_path += '/';
376 cwd_user_exe_path += user_exe_path;
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000377 FileSpec cwd_file(cwd_user_exe_path, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 if (cwd_file.Exists())
379 file = cwd_file;
380 }
381 }
Greg Claytone996fd32011-03-08 22:40:15 +0000382 }
383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 ModuleSP exe_module_sp;
385 if (platform_sp) {
386 FileSpecList executable_search_paths(
387 Target::GetDefaultExecutableSearchPaths());
388 ModuleSpec module_spec(file, arch);
389 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp,
390 executable_search_paths.GetSize()
391 ? &executable_search_paths
392 : nullptr);
Jim Ingham5aee1622010-08-09 23:31:02 +0000393 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 if (error.Success() && exe_module_sp) {
396 if (exe_module_sp->GetObjectFile() == nullptr) {
397 if (arch.IsValid()) {
398 error.SetErrorStringWithFormat(
399 "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(),
400 arch.GetArchitectureName());
401 } else {
402 error.SetErrorStringWithFormat("unsupported file type \"%s\"",
403 file.GetPath().c_str());
404 }
405 return error;
406 }
407 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
408 target_sp->SetExecutableModule(exe_module_sp, get_dependent_files);
409 if (user_exe_path_is_bundle)
410 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
411 sizeof(resolved_bundle_exe_path));
412 }
413 } else {
414 // No file was specified, just create an empty target with any arch
415 // if a valid arch was specified
416 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
417 }
418
419 if (target_sp) {
420 // Set argv0 with what the user typed, unless the user specified a
421 // directory. If the user specified a directory, then it is probably a
422 // bundle that was resolved and we need to use the resolved bundle path
Zachary Turnera47464b2016-11-18 20:44:46 +0000423 if (!user_exe_path.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 // Use exactly what the user typed as the first argument when we exec or
425 // posix_spawn
426 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
427 target_sp->SetArg0(resolved_bundle_exe_path);
428 } else {
429 // Use resolved path
430 target_sp->SetArg0(file.GetPath().c_str());
431 }
432 }
433 if (file.GetDirectory()) {
434 FileSpec file_dir;
435 file_dir.GetDirectory() = file.GetDirectory();
436 target_sp->GetExecutableSearchPaths().Append(file_dir);
437 }
438
439 // Don't put the dummy target in the target list, it's held separately.
440 if (!is_dummy_target) {
441 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
442 m_selected_target_idx = m_target_list.size();
443 m_target_list.push_back(target_sp);
444 // Now prime this from the dummy target:
445 target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget());
446 } else {
447 m_dummy_target_sp = target_sp;
448 }
449 }
450
451 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452}
453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454bool TargetList::DeleteTarget(TargetSP &target_sp) {
455 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
456 collection::iterator pos, end = m_target_list.end();
457
458 for (pos = m_target_list.begin(); pos != end; ++pos) {
459 if (pos->get() == target_sp.get()) {
460 m_target_list.erase(pos);
461 return true;
462 }
463 }
464 return false;
465}
466
467TargetSP TargetList::FindTargetWithExecutableAndArchitecture(
468 const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const {
469 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
470 TargetSP target_sp;
471 bool full_match = (bool)exe_file_spec.GetDirectory();
472
473 collection::const_iterator pos, end = m_target_list.end();
474 for (pos = m_target_list.begin(); pos != end; ++pos) {
475 Module *exe_module = (*pos)->GetExecutableModulePointer();
476
477 if (exe_module) {
478 if (FileSpec::Equal(exe_file_spec, exe_module->GetFileSpec(),
479 full_match)) {
480 if (exe_arch_ptr) {
481 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
482 continue;
483 }
484 target_sp = *pos;
485 break;
486 }
487 }
488 }
489 return target_sp;
490}
491
492TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const {
493 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
494 TargetSP target_sp;
495 collection::const_iterator pos, end = m_target_list.end();
496 for (pos = m_target_list.begin(); pos != end; ++pos) {
497 Process *process = (*pos)->GetProcessSP().get();
498 if (process && process->GetID() == pid) {
499 target_sp = *pos;
500 break;
501 }
502 }
503 return target_sp;
504}
505
506TargetSP TargetList::FindTargetWithProcess(Process *process) const {
507 TargetSP target_sp;
508 if (process) {
509 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
510 collection::const_iterator pos, end = m_target_list.end();
511 for (pos = m_target_list.begin(); pos != end; ++pos) {
512 if (process == (*pos)->GetProcessSP().get()) {
513 target_sp = *pos;
514 break;
515 }
516 }
517 }
518 return target_sp;
519}
520
521TargetSP TargetList::GetTargetSP(Target *target) const {
522 TargetSP target_sp;
523 if (target) {
524 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
525 collection::const_iterator pos, end = m_target_list.end();
526 for (pos = m_target_list.begin(); pos != end; ++pos) {
527 if (target == (*pos).get()) {
528 target_sp = *pos;
529 break;
530 }
531 }
532 }
533 return target_sp;
534}
535
536uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) {
537 uint32_t num_async_interrupts_sent = 0;
538
539 if (pid != LLDB_INVALID_PROCESS_ID) {
540 TargetSP target_sp(FindTargetWithProcessID(pid));
541 if (target_sp) {
542 Process *process = target_sp->GetProcessSP().get();
543 if (process) {
544 process->SendAsyncInterrupt();
545 ++num_async_interrupts_sent;
546 }
547 }
548 } else {
549 // We don't have a valid pid to broadcast to, so broadcast to the target
550 // list's async broadcaster...
551 BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr);
552 }
553
554 return num_async_interrupts_sent;
555}
556
557uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) {
558 uint32_t num_signals_sent = 0;
559 Process *process = nullptr;
560 if (pid == LLDB_INVALID_PROCESS_ID) {
561 // Signal all processes with signal
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000562 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 collection::iterator pos, end = m_target_list.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 for (pos = m_target_list.begin(); pos != end; ++pos) {
565 process = (*pos)->GetProcessSP().get();
566 if (process) {
567 if (process->IsAlive()) {
568 ++num_signals_sent;
569 process->Signal(signo);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 } else {
574 // Signal a specific process with signal
575 TargetSP target_sp(FindTargetWithProcessID(pid));
576 if (target_sp) {
577 process = target_sp->GetProcessSP().get();
578 if (process) {
579 if (process->IsAlive()) {
580 ++num_signals_sent;
581 process->Signal(signo);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000585 }
586 return num_signals_sent;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589int TargetList::GetNumTargets() const {
590 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
591 return m_target_list.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592}
593
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const {
595 TargetSP target_sp;
596 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
597 if (idx < m_target_list.size())
598 target_sp = m_target_list[idx];
599 return target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const {
603 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
604 size_t num_targets = m_target_list.size();
605 for (size_t idx = 0; idx < num_targets; idx++) {
606 if (target_sp == m_target_list[idx])
607 return idx;
608 }
609 return UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610}
611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612uint32_t TargetList::SetSelectedTarget(Target *target) {
613 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
614 collection::const_iterator pos, begin = m_target_list.begin(),
615 end = m_target_list.end();
616 for (pos = begin; pos != end; ++pos) {
617 if (pos->get() == target) {
618 m_selected_target_idx = std::distance(begin, pos);
619 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621 }
622 m_selected_target_idx = 0;
623 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624}
625
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626lldb::TargetSP TargetList::GetSelectedTarget() {
627 std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
628 if (m_selected_target_idx >= m_target_list.size())
Jim Ingham2976d002010-08-26 21:32:51 +0000629 m_selected_target_idx = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 return GetTargetAtIndex(m_selected_target_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631}