blob: 5bd6ea9db643fd01be1b013b63737c6499da273d [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// 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"
Greg Claytonb3a40ba2012-03-20 18:34:04 +000024#include "lldb/Interpreter/CommandInterpreter.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000025#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000026#include "lldb/Symbol/ObjectFile.h"
Greg Claytone996fd32011-03-08 22:40:15 +000027#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/Process.h"
29#include "lldb/Target/TargetList.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
Jim Ingham4bddaeb2012-02-16 06:50:00 +000034ConstString &
35TargetList::GetStaticBroadcasterClass ()
36{
37 static ConstString class_name ("lldb.targetList");
38 return class_name;
39}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
41//----------------------------------------------------------------------
42// TargetList constructor
43//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +000044TargetList::TargetList(Debugger &debugger) :
Jim Ingham4f465cf2012-10-10 18:32:14 +000045 Broadcaster(&debugger, TargetList::GetStaticBroadcasterClass().AsCString()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 m_target_list(),
47 m_target_list_mutex (Mutex::eMutexTypeRecursive),
Jim Ingham2976d002010-08-26 21:32:51 +000048 m_selected_target_idx (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049{
Jim Ingham4bddaeb2012-02-16 06:50:00 +000050 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
53//----------------------------------------------------------------------
54// Destructor
55//----------------------------------------------------------------------
56TargetList::~TargetList()
57{
58 Mutex::Locker locker(m_target_list_mutex);
59 m_target_list.clear();
60}
61
62Error
Greg Claytoncac9c5f2011-09-24 00:52:29 +000063TargetList::CreateTarget (Debugger &debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +000064 const char *user_exe_path,
Greg Claytoncac9c5f2011-09-24 00:52:29 +000065 const char *triple_cstr,
66 bool get_dependent_files,
67 const OptionGroupPlatform *platform_options,
68 TargetSP &target_sp)
69{
70 Error error;
71 PlatformSP platform_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000072
Johnny Chencdc21d42012-01-05 01:26:01 +000073 // This is purposely left empty unless it is specified by triple_cstr.
74 // If not initialized via triple_cstr, then the currently selected platform
75 // will set the architecture correctly.
Greg Clayton70512312012-05-08 01:45:38 +000076 const ArchSpec arch(triple_cstr);
77 if (triple_cstr && triple_cstr[0])
Greg Claytoncac9c5f2011-09-24 00:52:29 +000078 {
Greg Claytoncac9c5f2011-09-24 00:52:29 +000079 if (!arch.IsValid())
80 {
Greg Clayton86edbf42011-10-26 00:56:27 +000081 error.SetErrorStringWithFormat("invalid triple '%s'", triple_cstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +000082 return error;
83 }
84 }
Greg Claytonf4d6de62013-04-24 22:29:28 +000085
Greg Clayton70512312012-05-08 01:45:38 +000086 ArchSpec platform_arch(arch);
Greg Claytonf4d6de62013-04-24 22:29:28 +000087
88
89 if (user_exe_path && user_exe_path[0])
90 {
91 ModuleSpecList module_specs;
92 ModuleSpec module_spec;
93 module_spec.GetFileSpec().SetFile(user_exe_path, true);
94 lldb::offset_t file_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +000095 lldb::offset_t file_size = 0;
96 const size_t num_specs = ObjectFile::GetModuleSpecifications (module_spec.GetFileSpec(), file_offset, file_size, module_specs);
Greg Claytonf4d6de62013-04-24 22:29:28 +000097 if (num_specs > 0)
98 {
99 ModuleSpec matching_module_spec;
100
101 if (num_specs == 1)
102 {
103 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec))
104 {
105 if (platform_arch.IsValid())
106 {
107 if (!platform_arch.IsCompatibleMatch(matching_module_spec.GetArchitecture()))
108 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000109 error.SetErrorStringWithFormat("the specified architecture '%s' is not compatible with '%s' in '%s'",
Greg Claytonf4d6de62013-04-24 22:29:28 +0000110 platform_arch.GetTriple().str().c_str(),
111 matching_module_spec.GetArchitecture().GetTriple().str().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000112 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytonf4d6de62013-04-24 22:29:28 +0000113 return error;
114 }
115 }
116 else
117 {
118 // Only one arch and none was specified
119 platform_arch = matching_module_spec.GetArchitecture();
120 }
121 }
122 }
123 else
124 {
125 if (arch.IsValid())
126 {
127 module_spec.GetArchitecture() = arch;
128 if (module_specs.FindMatchingModuleSpec(module_spec, matching_module_spec))
129 {
130 platform_arch = matching_module_spec.GetArchitecture();
131 }
132 }
Greg Claytonb23b1132013-05-03 23:22:20 +0000133 // Don't just select the first architecture, we want to let the platform select
134 // the best architecture first when there are multiple archs.
135// else
136// {
137// // No arch specified, select the first arch
138// if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec))
139// {
140// platform_arch = matching_module_spec.GetArchitecture();
141// }
142// }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000143 }
144 }
145 }
146
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000147 CommandInterpreter &interpreter = debugger.GetCommandInterpreter();
148 if (platform_options)
149 {
150 if (platform_options->PlatformWasSpecified ())
151 {
152 const bool select_platform = true;
153 platform_sp = platform_options->CreatePlatformWithOptions (interpreter,
154 arch,
155 select_platform,
Greg Clayton70512312012-05-08 01:45:38 +0000156 error,
157 platform_arch);
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000158 if (!platform_sp)
159 return error;
160 }
161 }
162
163 if (!platform_sp)
164 {
165 // Get the current platform and make sure it is compatible with the
166 // current architecture if we have a valid architecture.
167 platform_sp = debugger.GetPlatformList().GetSelectedPlatform ();
168
Greg Clayton1e0c8842013-01-11 20:49:54 +0000169 if (arch.IsValid() && !platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch))
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000170 {
Greg Clayton70512312012-05-08 01:45:38 +0000171 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000172 }
173 }
Greg Clayton70512312012-05-08 01:45:38 +0000174
175 if (!platform_arch.IsValid())
176 platform_arch = arch;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000177
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000178 error = TargetList::CreateTarget (debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +0000179 user_exe_path,
Greg Clayton70512312012-05-08 01:45:38 +0000180 platform_arch,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000181 get_dependent_files,
182 platform_sp,
183 target_sp);
184 return error;
185}
186
187Error
Greg Claytona0ca6602012-10-18 16:33:33 +0000188TargetList::CreateTarget (Debugger &debugger,
189 const char *user_exe_path,
190 const ArchSpec& specified_arch,
191 bool get_dependent_files,
192 PlatformSP &platform_sp,
193 TargetSP &target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194{
195 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytona0ca6602012-10-18 16:33:33 +0000196 "TargetList::CreateTarget (file = '%s', arch = '%s')",
197 user_exe_path,
Greg Clayton70512312012-05-08 01:45:38 +0000198 specified_arch.GetArchitectureName());
Jim Ingham5aee1622010-08-09 23:31:02 +0000199 Error error;
Greg Claytonded470d2011-03-19 01:12:21 +0000200
Greg Clayton70512312012-05-08 01:45:38 +0000201 ArchSpec arch(specified_arch);
202
203 if (platform_sp)
204 {
205 if (arch.IsValid())
206 {
Greg Clayton1e0c8842013-01-11 20:49:54 +0000207 if (!platform_sp->IsCompatibleArchitecture(arch, false, NULL))
Greg Clayton70512312012-05-08 01:45:38 +0000208 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
209 }
210 }
211 else if (arch.IsValid())
212 {
213 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
214 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000215
Greg Clayton70512312012-05-08 01:45:38 +0000216 if (!platform_sp)
217 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
218
Greg Clayton8ae50eb2012-06-05 21:17:09 +0000219 if (!arch.IsValid())
220 arch = specified_arch;
Greg Clayton8ae50eb2012-06-05 21:17:09 +0000221
Jason Molendad26206b52013-04-19 22:38:50 +0000222 FileSpec file (user_exe_path, false);
223 if (!file.Exists() && user_exe_path && user_exe_path[0] == '~')
224 {
225 file = FileSpec(user_exe_path, true);
226 }
Greg Clayton82d79292012-10-25 22:45:35 +0000227 bool user_exe_path_is_bundle = false;
228 char resolved_bundle_exe_path[PATH_MAX];
229 resolved_bundle_exe_path[0] = '\0';
Greg Claytone996fd32011-03-08 22:40:15 +0000230 if (file)
Jim Ingham5aee1622010-08-09 23:31:02 +0000231 {
Greg Clayton82d79292012-10-25 22:45:35 +0000232 if (file.GetFileType() == FileSpec::eFileTypeDirectory)
233 user_exe_path_is_bundle = true;
234
Greg Claytona0ca6602012-10-18 16:33:33 +0000235 if (file.IsRelativeToCurrentWorkingDirectory())
236 {
237 // Ignore paths that start with "./" and "../"
238 if (!((user_exe_path[0] == '.' && user_exe_path[1] == '/') ||
239 (user_exe_path[0] == '.' && user_exe_path[1] == '.' && user_exe_path[2] == '/')))
240 {
241 char cwd[PATH_MAX];
242 if (getcwd (cwd, sizeof(cwd)))
243 {
244 std::string cwd_user_exe_path (cwd);
245 cwd_user_exe_path += '/';
246 cwd_user_exe_path += user_exe_path;
Greg Clayton9ff5aae2013-04-04 00:15:09 +0000247 FileSpec cwd_file (cwd_user_exe_path.c_str(), false);
248 if (cwd_file.Exists())
249 file = cwd_file;
Greg Claytona0ca6602012-10-18 16:33:33 +0000250 }
251 }
252 }
253
Jim Ingham5aee1622010-08-09 23:31:02 +0000254 ModuleSP exe_module_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000255 if (platform_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000256 {
257 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
Greg Clayton70512312012-05-08 01:45:38 +0000258 error = platform_sp->ResolveExecutable (file,
259 arch,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000260 exe_module_sp,
261 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
262 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263
Greg Claytone996fd32011-03-08 22:40:15 +0000264 if (error.Success() && exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 {
Jim Ingham5aee1622010-08-09 23:31:02 +0000266 if (exe_module_sp->GetObjectFile() == NULL)
267 {
Greg Claytonbc5cad62010-12-08 04:55:11 +0000268 if (arch.IsValid())
269 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000270 error.SetErrorStringWithFormat("\"%s\" doesn't contain architecture %s",
271 file.GetPath().c_str(),
Greg Clayton64195a22011-02-23 00:35:02 +0000272 arch.GetArchitectureName());
Greg Claytonbc5cad62010-12-08 04:55:11 +0000273 }
274 else
275 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000276 error.SetErrorStringWithFormat("unsupported file type \"%s\"",
277 file.GetPath().c_str());
Greg Claytonbc5cad62010-12-08 04:55:11 +0000278 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000279 return error;
280 }
Greg Clayton32e0a752011-03-30 18:16:51 +0000281 target_sp.reset(new Target(debugger, arch, platform_sp));
Jim Ingham5aee1622010-08-09 23:31:02 +0000282 target_sp->SetExecutableModule (exe_module_sp, get_dependent_files);
Greg Clayton82d79292012-10-25 22:45:35 +0000283 if (user_exe_path_is_bundle)
284 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, sizeof(resolved_bundle_exe_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000286 }
Greg Claytone996fd32011-03-08 22:40:15 +0000287 else
Jim Ingham5aee1622010-08-09 23:31:02 +0000288 {
Greg Claytone996fd32011-03-08 22:40:15 +0000289 // No file was specified, just create an empty target with any arch
290 // if a valid arch was specified
Greg Clayton32e0a752011-03-30 18:16:51 +0000291 target_sp.reset(new Target(debugger, arch, platform_sp));
Greg Claytone996fd32011-03-08 22:40:15 +0000292 }
293
294 if (target_sp)
295 {
Greg Clayton82d79292012-10-25 22:45:35 +0000296 // Set argv0 with what the user typed, unless the user specified a
297 // directory. If the user specified a directory, then it is probably a
298 // bundle that was resolved and we need to use the resolved bundle path
Greg Claytona0ca6602012-10-18 16:33:33 +0000299 if (user_exe_path)
300 {
301 // Use exactly what the user typed as the first argument when we exec or posix_spawn
Greg Clayton82d79292012-10-25 22:45:35 +0000302 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0])
303 {
304 target_sp->SetArg0 (resolved_bundle_exe_path);
305 }
306 else
307 {
308 // Just use what the user typed
309 target_sp->SetArg0 (user_exe_path);
310 }
Greg Claytona0ca6602012-10-18 16:33:33 +0000311 }
312 if (file.GetDirectory())
313 {
314 FileSpec file_dir;
315 file_dir.GetDirectory() = file.GetDirectory();
316 target_sp->GetExecutableSearchPaths ().Append (file_dir);
317 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000318 Mutex::Locker locker(m_target_list_mutex);
Jim Ingham2976d002010-08-26 21:32:51 +0000319 m_selected_target_idx = m_target_list.size();
Jim Ingham5aee1622010-08-09 23:31:02 +0000320 m_target_list.push_back(target_sp);
Greg Claytona0ca6602012-10-18 16:33:33 +0000321
322
Jim Ingham5aee1622010-08-09 23:31:02 +0000323 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 return error;
326}
327
328bool
329TargetList::DeleteTarget (TargetSP &target_sp)
330{
331 Mutex::Locker locker(m_target_list_mutex);
332 collection::iterator pos, end = m_target_list.end();
333
334 for (pos = m_target_list.begin(); pos != end; ++pos)
335 {
336 if (pos->get() == target_sp.get())
337 {
338 m_target_list.erase(pos);
339 return true;
340 }
341 }
342 return false;
343}
344
345
346TargetSP
347TargetList::FindTargetWithExecutableAndArchitecture
348(
349 const FileSpec &exe_file_spec,
350 const ArchSpec *exe_arch_ptr
351) const
352{
353 Mutex::Locker locker (m_target_list_mutex);
354 TargetSP target_sp;
355 bool full_match = exe_file_spec.GetDirectory();
356
357 collection::const_iterator pos, end = m_target_list.end();
358 for (pos = m_target_list.begin(); pos != end; ++pos)
359 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000360 Module *exe_module = (*pos)->GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361
Greg Claytonaa149cb2011-08-11 02:48:45 +0000362 if (exe_module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000364 if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 {
366 if (exe_arch_ptr)
367 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000368 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 continue;
370 }
371 target_sp = *pos;
372 break;
373 }
374 }
375 }
376 return target_sp;
377}
378
379TargetSP
380TargetList::FindTargetWithProcessID (lldb::pid_t pid) const
381{
382 Mutex::Locker locker(m_target_list_mutex);
383 TargetSP target_sp;
384 collection::const_iterator pos, end = m_target_list.end();
385 for (pos = m_target_list.begin(); pos != end; ++pos)
386 {
387 Process* process = (*pos)->GetProcessSP().get();
388 if (process && process->GetID() == pid)
389 {
390 target_sp = *pos;
391 break;
392 }
393 }
394 return target_sp;
395}
396
397
398TargetSP
399TargetList::FindTargetWithProcess (Process *process) const
400{
401 TargetSP target_sp;
402 if (process)
403 {
404 Mutex::Locker locker(m_target_list_mutex);
405 collection::const_iterator pos, end = m_target_list.end();
406 for (pos = m_target_list.begin(); pos != end; ++pos)
407 {
408 if (process == (*pos)->GetProcessSP().get())
409 {
410 target_sp = *pos;
411 break;
412 }
413 }
414 }
415 return target_sp;
416}
417
418TargetSP
419TargetList::GetTargetSP (Target *target) const
420{
421 TargetSP target_sp;
422 if (target)
423 {
424 Mutex::Locker locker(m_target_list_mutex);
425 collection::const_iterator pos, end = m_target_list.end();
426 for (pos = m_target_list.begin(); pos != end; ++pos)
427 {
428 if (target == (*pos).get())
429 {
430 target_sp = *pos;
431 break;
432 }
433 }
434 }
435 return target_sp;
436}
437
438uint32_t
439TargetList::SendAsyncInterrupt (lldb::pid_t pid)
440{
441 uint32_t num_async_interrupts_sent = 0;
442
443 if (pid != LLDB_INVALID_PROCESS_ID)
444 {
445 TargetSP target_sp(FindTargetWithProcessID (pid));
446 if (target_sp.get())
447 {
448 Process* process = target_sp->GetProcessSP().get();
449 if (process)
450 {
Jim Inghamcfc09352012-07-27 23:57:19 +0000451 process->SendAsyncInterrupt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 ++num_async_interrupts_sent;
453 }
454 }
455 }
456 else
457 {
458 // We don't have a valid pid to broadcast to, so broadcast to the target
459 // list's async broadcaster...
460 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
461 }
462
463 return num_async_interrupts_sent;
464}
465
466uint32_t
467TargetList::SignalIfRunning (lldb::pid_t pid, int signo)
468{
469 uint32_t num_signals_sent = 0;
470 Process *process = NULL;
471 if (pid == LLDB_INVALID_PROCESS_ID)
472 {
473 // Signal all processes with signal
474 Mutex::Locker locker(m_target_list_mutex);
475 collection::iterator pos, end = m_target_list.end();
476 for (pos = m_target_list.begin(); pos != end; ++pos)
477 {
478 process = (*pos)->GetProcessSP().get();
479 if (process)
480 {
481 if (process->IsAlive())
482 {
483 ++num_signals_sent;
484 process->Signal (signo);
485 }
486 }
487 }
488 }
489 else
490 {
491 // Signal a specific process with signal
492 TargetSP target_sp(FindTargetWithProcessID (pid));
493 if (target_sp.get())
494 {
495 process = target_sp->GetProcessSP().get();
496 if (process)
497 {
498 if (process->IsAlive())
499 {
500 ++num_signals_sent;
501 process->Signal (signo);
502 }
503 }
504 }
505 }
506 return num_signals_sent;
507}
508
509int
510TargetList::GetNumTargets () const
511{
512 Mutex::Locker locker (m_target_list_mutex);
513 return m_target_list.size();
514}
515
516lldb::TargetSP
517TargetList::GetTargetAtIndex (uint32_t idx) const
518{
519 TargetSP target_sp;
520 Mutex::Locker locker (m_target_list_mutex);
521 if (idx < m_target_list.size())
522 target_sp = m_target_list[idx];
523 return target_sp;
524}
525
526uint32_t
Jim Ingham8499e1a2012-05-08 23:06:07 +0000527TargetList::GetIndexOfTarget (lldb::TargetSP target_sp) const
528{
529 Mutex::Locker locker (m_target_list_mutex);
530 size_t num_targets = m_target_list.size();
531 for (size_t idx = 0; idx < num_targets; idx++)
532 {
533 if (target_sp == m_target_list[idx])
534 return idx;
535 }
536 return UINT32_MAX;
537}
538
539uint32_t
Jim Ingham2976d002010-08-26 21:32:51 +0000540TargetList::SetSelectedTarget (Target* target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541{
542 Mutex::Locker locker (m_target_list_mutex);
543 collection::const_iterator pos,
544 begin = m_target_list.begin(),
545 end = m_target_list.end();
546 for (pos = begin; pos != end; ++pos)
547 {
548 if (pos->get() == target)
549 {
Jim Ingham2976d002010-08-26 21:32:51 +0000550 m_selected_target_idx = std::distance (begin, pos);
551 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 }
553 }
Jim Ingham2976d002010-08-26 21:32:51 +0000554 m_selected_target_idx = 0;
555 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556}
557
558lldb::TargetSP
Jim Ingham2976d002010-08-26 21:32:51 +0000559TargetList::GetSelectedTarget ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560{
561 Mutex::Locker locker (m_target_list_mutex);
Jim Ingham2976d002010-08-26 21:32:51 +0000562 if (m_selected_target_idx >= m_target_list.size())
563 m_selected_target_idx = 0;
564 return GetTargetAtIndex (m_selected_target_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565}