blob: 741ed9b6d884b013b5a15ca59670ab32326bdfd6 [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"
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
Zachary Turner3f559742014-08-07 17:33:36 +000032#include "llvm/ADT/SmallString.h"
33
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034using namespace lldb;
35using namespace lldb_private;
36
Jim Ingham4bddaeb2012-02-16 06:50:00 +000037ConstString &
38TargetList::GetStaticBroadcasterClass ()
39{
40 static ConstString class_name ("lldb.targetList");
41 return class_name;
42}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
44//----------------------------------------------------------------------
45// TargetList constructor
46//----------------------------------------------------------------------
Jim Ingham4bddaeb2012-02-16 06:50:00 +000047TargetList::TargetList(Debugger &debugger) :
Jim Ingham4f465cf2012-10-10 18:32:14 +000048 Broadcaster(&debugger, TargetList::GetStaticBroadcasterClass().AsCString()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 m_target_list(),
50 m_target_list_mutex (Mutex::eMutexTypeRecursive),
Jim Ingham2976d002010-08-26 21:32:51 +000051 m_selected_target_idx (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052{
Jim Ingham4bddaeb2012-02-16 06:50:00 +000053 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
56//----------------------------------------------------------------------
57// Destructor
58//----------------------------------------------------------------------
59TargetList::~TargetList()
60{
61 Mutex::Locker locker(m_target_list_mutex);
62 m_target_list.clear();
63}
64
65Error
Greg Claytoncac9c5f2011-09-24 00:52:29 +000066TargetList::CreateTarget (Debugger &debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +000067 const char *user_exe_path,
Greg Claytoncac9c5f2011-09-24 00:52:29 +000068 const char *triple_cstr,
69 bool get_dependent_files,
70 const OptionGroupPlatform *platform_options,
71 TargetSP &target_sp)
72{
Jim Ingham893c9322014-11-22 01:42:44 +000073 return CreateTargetInternal (debugger,
74 user_exe_path,
75 triple_cstr,
76 get_dependent_files,
77 platform_options,
78 target_sp,
79 false);
80}
81
82Error
83TargetList::CreateTarget (Debugger &debugger,
84 const char *user_exe_path,
85 const ArchSpec& specified_arch,
86 bool get_dependent_files,
87 PlatformSP &platform_sp,
88 TargetSP &target_sp)
89{
90 return CreateTargetInternal (debugger,
91 user_exe_path,
92 specified_arch,
93 get_dependent_files,
94 platform_sp,
95 target_sp,
96 false);
97}
98
99Error
100TargetList::CreateTargetInternal (Debugger &debugger,
101 const char *user_exe_path,
102 const char *triple_cstr,
103 bool get_dependent_files,
104 const OptionGroupPlatform *platform_options,
105 TargetSP &target_sp,
106 bool is_dummy_target)
107{
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000108 Error error;
109 PlatformSP platform_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000110
Johnny Chencdc21d42012-01-05 01:26:01 +0000111 // This is purposely left empty unless it is specified by triple_cstr.
112 // If not initialized via triple_cstr, then the currently selected platform
113 // will set the architecture correctly.
Greg Clayton70512312012-05-08 01:45:38 +0000114 const ArchSpec arch(triple_cstr);
115 if (triple_cstr && triple_cstr[0])
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000116 {
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000117 if (!arch.IsValid())
118 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000119 error.SetErrorStringWithFormat("invalid triple '%s'", triple_cstr);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000120 return error;
121 }
122 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000123
Greg Clayton70512312012-05-08 01:45:38 +0000124 ArchSpec platform_arch(arch);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000125
Greg Clayton3f19ada2014-07-10 23:33:37 +0000126 bool prefer_platform_arch = false;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000127
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000128 CommandInterpreter &interpreter = debugger.GetCommandInterpreter();
129 if (platform_options && platform_options->PlatformWasSpecified ())
130 {
131 const bool select_platform = true;
132 platform_sp = platform_options->CreatePlatformWithOptions (interpreter,
133 arch,
134 select_platform,
135 error,
136 platform_arch);
137 if (!platform_sp)
138 return error;
139 }
140
Greg Claytonf4d6de62013-04-24 22:29:28 +0000141 if (user_exe_path && user_exe_path[0])
142 {
143 ModuleSpecList module_specs;
144 ModuleSpec module_spec;
145 module_spec.GetFileSpec().SetFile(user_exe_path, true);
Greg Claytonc76fa8a2014-07-29 21:27:21 +0000146
147 // Resolve the executable in case we are given a path to a application bundle
148 // like a .app bundle on MacOSX
149 Host::ResolveExecutableInBundle (module_spec.GetFileSpec());
150
Greg Claytonf4d6de62013-04-24 22:29:28 +0000151 lldb::offset_t file_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +0000152 lldb::offset_t file_size = 0;
153 const size_t num_specs = ObjectFile::GetModuleSpecifications (module_spec.GetFileSpec(), file_offset, file_size, module_specs);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000154 if (num_specs > 0)
155 {
156 ModuleSpec matching_module_spec;
157
158 if (num_specs == 1)
159 {
160 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec))
161 {
162 if (platform_arch.IsValid())
163 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000164 if (platform_arch.IsCompatibleMatch(matching_module_spec.GetArchitecture()))
165 {
166 // If the OS or vendor weren't specified, then adopt the module's
167 // architecture so that the platform matching can be more accurate
168 if (!platform_arch.TripleOSWasSpecified() || !platform_arch.TripleVendorWasSpecified())
169 {
170 prefer_platform_arch = true;
171 platform_arch = matching_module_spec.GetArchitecture();
172 }
173 }
174 else
Greg Claytonf4d6de62013-04-24 22:29:28 +0000175 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000176 error.SetErrorStringWithFormat("the specified architecture '%s' is not compatible with '%s' in '%s'",
Greg Claytonf4d6de62013-04-24 22:29:28 +0000177 platform_arch.GetTriple().str().c_str(),
178 matching_module_spec.GetArchitecture().GetTriple().str().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000179 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytonf4d6de62013-04-24 22:29:28 +0000180 return error;
181 }
182 }
183 else
184 {
185 // Only one arch and none was specified
Greg Clayton3f19ada2014-07-10 23:33:37 +0000186 prefer_platform_arch = true;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000187 platform_arch = matching_module_spec.GetArchitecture();
188 }
189 }
190 }
191 else
192 {
193 if (arch.IsValid())
194 {
195 module_spec.GetArchitecture() = arch;
196 if (module_specs.FindMatchingModuleSpec(module_spec, matching_module_spec))
197 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000198 prefer_platform_arch = true;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000199 platform_arch = matching_module_spec.GetArchitecture();
200 }
201 }
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000202 else
203 {
204 // No architecture specified, check if there is only one platform for
205 // all of the architectures.
206
207 typedef std::vector<PlatformSP> PlatformList;
208 PlatformList platforms;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000209 PlatformSP host_platform_sp = Platform::GetHostPlatform();
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000210 for (size_t i=0; i<num_specs; ++i)
211 {
212 ModuleSpec module_spec;
213 if (module_specs.GetModuleSpecAtIndex(i, module_spec))
214 {
215 // See if there was a selected platform and check that first
216 // since the user may have specified it.
217 if (platform_sp)
218 {
219 if (platform_sp->IsCompatibleArchitecture(module_spec.GetArchitecture(), false, NULL))
220 {
221 platforms.push_back(platform_sp);
222 continue;
223 }
224 }
225
226 // Next check the host platform it if wasn't already checked above
227 if (host_platform_sp && (!platform_sp || host_platform_sp->GetName() != platform_sp->GetName()))
228 {
229 if (host_platform_sp->IsCompatibleArchitecture(module_spec.GetArchitecture(), false, NULL))
230 {
231 platforms.push_back(host_platform_sp);
232 continue;
233 }
234 }
235
236 // Just find a platform that matches the architecture in the executable file
237 platforms.push_back(Platform::GetPlatformForArchitecture(module_spec.GetArchitecture(), nullptr));
238 }
239 }
240
241 Platform *platform_ptr = NULL;
242 for (const auto &the_platform_sp : platforms)
243 {
244 if (platform_ptr)
245 {
246 if (platform_ptr->GetName() != the_platform_sp->GetName())
247 {
248 platform_ptr = NULL;
249 break;
250 }
251 }
252 else
253 {
254 platform_ptr = the_platform_sp.get();
255 }
256 }
257
258 if (platform_ptr)
259 {
260 // All platforms for all modules in the exectuable match, so we can select this platform
261 platform_sp = platforms.front();
262 }
263 else
264 {
265 // More than one platform claims to support this file, so the --platform option must be specified
266 StreamString error_strm;
267 std::set<Platform *> platform_set;
268 error_strm.Printf ("more than one platform supports this executable (");
269 for (const auto &the_platform_sp : platforms)
270 {
271 if (platform_set.find(the_platform_sp.get()) == platform_set.end())
272 {
273 if (!platform_set.empty())
274 error_strm.PutCString(", ");
275 error_strm.PutCString(the_platform_sp->GetName().GetCString());
276 platform_set.insert(the_platform_sp.get());
277 }
278 }
279 error_strm.Printf("), use the --platform option to specify a platform");
280 error.SetErrorString(error_strm.GetString().c_str());
281 return error;
282 }
283 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000284 }
285 }
286 }
287
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000288 if (!platform_sp)
289 {
290 // Get the current platform and make sure it is compatible with the
291 // current architecture if we have a valid architecture.
292 platform_sp = debugger.GetPlatformList().GetSelectedPlatform ();
293
Greg Clayton3f19ada2014-07-10 23:33:37 +0000294 if (!prefer_platform_arch && arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000295 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000296 if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch))
Greg Clayton95bbdf62014-09-17 16:42:50 +0000297 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000298 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
Greg Clayton95bbdf62014-09-17 16:42:50 +0000299 if (platform_sp)
300 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
301 }
Greg Clayton3f19ada2014-07-10 23:33:37 +0000302 }
303 else if (platform_arch.IsValid())
304 {
305 // if "arch" isn't valid, yet "platform_arch" is, it means we have an executable file with
306 // a single architecture which should be used
307 ArchSpec fixed_platform_arch;
308 if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, &fixed_platform_arch))
Greg Clayton95bbdf62014-09-17 16:42:50 +0000309 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000310 platform_sp = Platform::GetPlatformForArchitecture(platform_arch, &fixed_platform_arch);
Greg Clayton95bbdf62014-09-17 16:42:50 +0000311 if (platform_sp)
312 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
313 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000314 }
315 }
Greg Clayton70512312012-05-08 01:45:38 +0000316
317 if (!platform_arch.IsValid())
318 platform_arch = arch;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000319
Jim Ingham893c9322014-11-22 01:42:44 +0000320 error = TargetList::CreateTargetInternal (debugger,
321 user_exe_path,
322 platform_arch,
323 get_dependent_files,
324 platform_sp,
325 target_sp,
326 is_dummy_target);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000327 return error;
328}
329
Jim Ingham893c9322014-11-22 01:42:44 +0000330lldb::TargetSP
331TargetList::GetDummyTarget (lldb_private::Debugger &debugger)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332{
Jim Ingham893c9322014-11-22 01:42:44 +0000333 // FIXME: Maybe the dummy target should be per-Debugger
334 if (!m_dummy_target_sp || !m_dummy_target_sp->IsValid())
335 {
336 ArchSpec arch(Target::GetDefaultArchitecture());
337 if (!arch.IsValid())
338 arch = HostInfo::GetArchitecture();
339 Error err = CreateDummyTarget(debugger,
340 arch.GetTriple().getTriple().c_str(),
341 m_dummy_target_sp);
342 }
343
344 return m_dummy_target_sp;
345}
346
347Error
348TargetList::CreateDummyTarget (Debugger &debugger,
349 const char *specified_arch_name,
350 lldb::TargetSP &target_sp)
351{
352 PlatformSP host_platform_sp(Platform::GetHostPlatform());
353 return CreateTargetInternal (debugger,
354 (const char *) nullptr,
355 specified_arch_name,
356 false,
357 (const OptionGroupPlatform *) nullptr,
358 target_sp,
359 true);
360}
361
362Error
363TargetList::CreateTargetInternal (Debugger &debugger,
364 const char *user_exe_path,
365 const ArchSpec& specified_arch,
366 bool get_dependent_files,
367 lldb::PlatformSP &platform_sp,
368 lldb::TargetSP &target_sp,
369 bool is_dummy_target)
370{
371
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytona0ca6602012-10-18 16:33:33 +0000373 "TargetList::CreateTarget (file = '%s', arch = '%s')",
374 user_exe_path,
Greg Clayton70512312012-05-08 01:45:38 +0000375 specified_arch.GetArchitectureName());
Jim Ingham5aee1622010-08-09 23:31:02 +0000376 Error error;
Greg Claytonded470d2011-03-19 01:12:21 +0000377
Greg Clayton70512312012-05-08 01:45:38 +0000378 ArchSpec arch(specified_arch);
379
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000380 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +0000381 {
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000382 if (!platform_sp || !platform_sp->IsCompatibleArchitecture(arch, false, NULL))
383 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
Greg Clayton70512312012-05-08 01:45:38 +0000384 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000385
Greg Clayton70512312012-05-08 01:45:38 +0000386 if (!platform_sp)
387 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
388
Greg Clayton8ae50eb2012-06-05 21:17:09 +0000389 if (!arch.IsValid())
390 arch = specified_arch;
Greg Clayton8ae50eb2012-06-05 21:17:09 +0000391
Jason Molendad26206b52013-04-19 22:38:50 +0000392 FileSpec file (user_exe_path, false);
393 if (!file.Exists() && user_exe_path && user_exe_path[0] == '~')
394 {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000395 // we want to expand the tilde but we don't want to resolve any symbolic links
396 // so we can't use the FileSpec constructor's resolve flag
Zachary Turner3f559742014-08-07 17:33:36 +0000397 llvm::SmallString<64> unglobbed_path(user_exe_path);
398 FileSpec::ResolveUsername(unglobbed_path);
Michael Sartain9f822cd2013-07-31 23:27:46 +0000399
Zachary Turner3f559742014-08-07 17:33:36 +0000400 if (unglobbed_path.empty())
401 file = FileSpec(user_exe_path, false);
402 else
403 file = FileSpec(unglobbed_path.c_str(), false);
Jason Molendad26206b52013-04-19 22:38:50 +0000404 }
Michael Sartain9f822cd2013-07-31 23:27:46 +0000405
Greg Clayton82d79292012-10-25 22:45:35 +0000406 bool user_exe_path_is_bundle = false;
407 char resolved_bundle_exe_path[PATH_MAX];
408 resolved_bundle_exe_path[0] = '\0';
Greg Claytone996fd32011-03-08 22:40:15 +0000409 if (file)
Jim Ingham5aee1622010-08-09 23:31:02 +0000410 {
Greg Clayton82d79292012-10-25 22:45:35 +0000411 if (file.GetFileType() == FileSpec::eFileTypeDirectory)
412 user_exe_path_is_bundle = true;
413
Jason Molenda14468812014-10-16 01:42:11 +0000414 if (file.IsRelativeToCurrentWorkingDirectory() && user_exe_path)
Greg Claytona0ca6602012-10-18 16:33:33 +0000415 {
416 // Ignore paths that start with "./" and "../"
417 if (!((user_exe_path[0] == '.' && user_exe_path[1] == '/') ||
418 (user_exe_path[0] == '.' && user_exe_path[1] == '.' && user_exe_path[2] == '/')))
419 {
420 char cwd[PATH_MAX];
421 if (getcwd (cwd, sizeof(cwd)))
422 {
423 std::string cwd_user_exe_path (cwd);
424 cwd_user_exe_path += '/';
425 cwd_user_exe_path += user_exe_path;
Greg Clayton9ff5aae2013-04-04 00:15:09 +0000426 FileSpec cwd_file (cwd_user_exe_path.c_str(), false);
427 if (cwd_file.Exists())
428 file = cwd_file;
Greg Claytona0ca6602012-10-18 16:33:33 +0000429 }
430 }
431 }
432
Jim Ingham5aee1622010-08-09 23:31:02 +0000433 ModuleSP exe_module_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000434 if (platform_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000435 {
436 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
Greg Clayton8012cad2014-11-17 19:39:20 +0000437 ModuleSpec module_spec(file, arch);
438 error = platform_sp->ResolveExecutable (module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000439 exe_module_sp,
440 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
441 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442
Greg Claytone996fd32011-03-08 22:40:15 +0000443 if (error.Success() && exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 {
Jim Ingham5aee1622010-08-09 23:31:02 +0000445 if (exe_module_sp->GetObjectFile() == NULL)
446 {
Greg Claytonbc5cad62010-12-08 04:55:11 +0000447 if (arch.IsValid())
448 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000449 error.SetErrorStringWithFormat("\"%s\" doesn't contain architecture %s",
450 file.GetPath().c_str(),
Greg Clayton64195a22011-02-23 00:35:02 +0000451 arch.GetArchitectureName());
Greg Claytonbc5cad62010-12-08 04:55:11 +0000452 }
453 else
454 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000455 error.SetErrorStringWithFormat("unsupported file type \"%s\"",
456 file.GetPath().c_str());
Greg Claytonbc5cad62010-12-08 04:55:11 +0000457 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000458 return error;
459 }
Jim Ingham893c9322014-11-22 01:42:44 +0000460 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
Jim Ingham5aee1622010-08-09 23:31:02 +0000461 target_sp->SetExecutableModule (exe_module_sp, get_dependent_files);
Greg Clayton82d79292012-10-25 22:45:35 +0000462 if (user_exe_path_is_bundle)
463 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, sizeof(resolved_bundle_exe_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000465 }
Greg Claytone996fd32011-03-08 22:40:15 +0000466 else
Jim Ingham5aee1622010-08-09 23:31:02 +0000467 {
Greg Claytone996fd32011-03-08 22:40:15 +0000468 // No file was specified, just create an empty target with any arch
469 // if a valid arch was specified
Jim Ingham893c9322014-11-22 01:42:44 +0000470 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
Greg Claytone996fd32011-03-08 22:40:15 +0000471 }
472
473 if (target_sp)
474 {
Greg Clayton82d79292012-10-25 22:45:35 +0000475 // Set argv0 with what the user typed, unless the user specified a
476 // directory. If the user specified a directory, then it is probably a
477 // bundle that was resolved and we need to use the resolved bundle path
Greg Claytona0ca6602012-10-18 16:33:33 +0000478 if (user_exe_path)
479 {
480 // Use exactly what the user typed as the first argument when we exec or posix_spawn
Greg Clayton82d79292012-10-25 22:45:35 +0000481 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0])
482 {
483 target_sp->SetArg0 (resolved_bundle_exe_path);
484 }
485 else
486 {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000487 // Use resolved path
488 target_sp->SetArg0 (file.GetPath().c_str());
Greg Clayton82d79292012-10-25 22:45:35 +0000489 }
Greg Claytona0ca6602012-10-18 16:33:33 +0000490 }
491 if (file.GetDirectory())
492 {
493 FileSpec file_dir;
494 file_dir.GetDirectory() = file.GetDirectory();
495 target_sp->GetExecutableSearchPaths ().Append (file_dir);
496 }
Jim Ingham893c9322014-11-22 01:42:44 +0000497
498 // Don't put the dummy target in the target list, it's held separately.
499 if (!is_dummy_target)
500 {
501 Mutex::Locker locker(m_target_list_mutex);
502 m_selected_target_idx = m_target_list.size();
503 m_target_list.push_back(target_sp);
504 }
505 else
506 {
507 m_dummy_target_sp = target_sp;
508 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000509 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 return error;
512}
513
514bool
515TargetList::DeleteTarget (TargetSP &target_sp)
516{
517 Mutex::Locker locker(m_target_list_mutex);
518 collection::iterator pos, end = m_target_list.end();
519
520 for (pos = m_target_list.begin(); pos != end; ++pos)
521 {
522 if (pos->get() == target_sp.get())
523 {
524 m_target_list.erase(pos);
525 return true;
526 }
527 }
528 return false;
529}
530
531
532TargetSP
533TargetList::FindTargetWithExecutableAndArchitecture
534(
535 const FileSpec &exe_file_spec,
536 const ArchSpec *exe_arch_ptr
537) const
538{
539 Mutex::Locker locker (m_target_list_mutex);
540 TargetSP target_sp;
Sean Callananddd7a2a2013-10-03 22:27:29 +0000541 bool full_match = (bool)exe_file_spec.GetDirectory();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542
543 collection::const_iterator pos, end = m_target_list.end();
544 for (pos = m_target_list.begin(); pos != end; ++pos)
545 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000546 Module *exe_module = (*pos)->GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547
Greg Claytonaa149cb2011-08-11 02:48:45 +0000548 if (exe_module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000550 if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551 {
552 if (exe_arch_ptr)
553 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000554 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555 continue;
556 }
557 target_sp = *pos;
558 break;
559 }
560 }
561 }
562 return target_sp;
563}
564
565TargetSP
566TargetList::FindTargetWithProcessID (lldb::pid_t pid) const
567{
568 Mutex::Locker locker(m_target_list_mutex);
569 TargetSP target_sp;
570 collection::const_iterator pos, end = m_target_list.end();
571 for (pos = m_target_list.begin(); pos != end; ++pos)
572 {
573 Process* process = (*pos)->GetProcessSP().get();
574 if (process && process->GetID() == pid)
575 {
576 target_sp = *pos;
577 break;
578 }
579 }
580 return target_sp;
581}
582
583
584TargetSP
585TargetList::FindTargetWithProcess (Process *process) const
586{
587 TargetSP target_sp;
588 if (process)
589 {
590 Mutex::Locker locker(m_target_list_mutex);
591 collection::const_iterator pos, end = m_target_list.end();
592 for (pos = m_target_list.begin(); pos != end; ++pos)
593 {
594 if (process == (*pos)->GetProcessSP().get())
595 {
596 target_sp = *pos;
597 break;
598 }
599 }
600 }
601 return target_sp;
602}
603
604TargetSP
605TargetList::GetTargetSP (Target *target) const
606{
607 TargetSP target_sp;
608 if (target)
609 {
610 Mutex::Locker locker(m_target_list_mutex);
611 collection::const_iterator pos, end = m_target_list.end();
612 for (pos = m_target_list.begin(); pos != end; ++pos)
613 {
614 if (target == (*pos).get())
615 {
616 target_sp = *pos;
617 break;
618 }
619 }
620 }
621 return target_sp;
622}
623
624uint32_t
625TargetList::SendAsyncInterrupt (lldb::pid_t pid)
626{
627 uint32_t num_async_interrupts_sent = 0;
628
629 if (pid != LLDB_INVALID_PROCESS_ID)
630 {
631 TargetSP target_sp(FindTargetWithProcessID (pid));
632 if (target_sp.get())
633 {
634 Process* process = target_sp->GetProcessSP().get();
635 if (process)
636 {
Jim Inghamcfc09352012-07-27 23:57:19 +0000637 process->SendAsyncInterrupt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 ++num_async_interrupts_sent;
639 }
640 }
641 }
642 else
643 {
644 // We don't have a valid pid to broadcast to, so broadcast to the target
645 // list's async broadcaster...
646 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
647 }
648
649 return num_async_interrupts_sent;
650}
651
652uint32_t
653TargetList::SignalIfRunning (lldb::pid_t pid, int signo)
654{
655 uint32_t num_signals_sent = 0;
656 Process *process = NULL;
657 if (pid == LLDB_INVALID_PROCESS_ID)
658 {
659 // Signal all processes with signal
660 Mutex::Locker locker(m_target_list_mutex);
661 collection::iterator pos, end = m_target_list.end();
662 for (pos = m_target_list.begin(); pos != end; ++pos)
663 {
664 process = (*pos)->GetProcessSP().get();
665 if (process)
666 {
667 if (process->IsAlive())
668 {
669 ++num_signals_sent;
670 process->Signal (signo);
671 }
672 }
673 }
674 }
675 else
676 {
677 // Signal a specific process with signal
678 TargetSP target_sp(FindTargetWithProcessID (pid));
679 if (target_sp.get())
680 {
681 process = target_sp->GetProcessSP().get();
682 if (process)
683 {
684 if (process->IsAlive())
685 {
686 ++num_signals_sent;
687 process->Signal (signo);
688 }
689 }
690 }
691 }
692 return num_signals_sent;
693}
694
695int
696TargetList::GetNumTargets () const
697{
698 Mutex::Locker locker (m_target_list_mutex);
699 return m_target_list.size();
700}
701
702lldb::TargetSP
703TargetList::GetTargetAtIndex (uint32_t idx) const
704{
705 TargetSP target_sp;
706 Mutex::Locker locker (m_target_list_mutex);
707 if (idx < m_target_list.size())
708 target_sp = m_target_list[idx];
709 return target_sp;
710}
711
712uint32_t
Jim Ingham8499e1a2012-05-08 23:06:07 +0000713TargetList::GetIndexOfTarget (lldb::TargetSP target_sp) const
714{
715 Mutex::Locker locker (m_target_list_mutex);
716 size_t num_targets = m_target_list.size();
717 for (size_t idx = 0; idx < num_targets; idx++)
718 {
719 if (target_sp == m_target_list[idx])
720 return idx;
721 }
722 return UINT32_MAX;
723}
724
725uint32_t
Jim Ingham2976d002010-08-26 21:32:51 +0000726TargetList::SetSelectedTarget (Target* target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727{
728 Mutex::Locker locker (m_target_list_mutex);
729 collection::const_iterator pos,
730 begin = m_target_list.begin(),
731 end = m_target_list.end();
732 for (pos = begin; pos != end; ++pos)
733 {
734 if (pos->get() == target)
735 {
Jim Ingham2976d002010-08-26 21:32:51 +0000736 m_selected_target_idx = std::distance (begin, pos);
737 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738 }
739 }
Jim Ingham2976d002010-08-26 21:32:51 +0000740 m_selected_target_idx = 0;
741 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742}
743
744lldb::TargetSP
Jim Ingham2976d002010-08-26 21:32:51 +0000745TargetList::GetSelectedTarget ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746{
747 Mutex::Locker locker (m_target_list_mutex);
Jim Ingham2976d002010-08-26 21:32:51 +0000748 if (m_selected_target_idx >= m_target_list.size())
749 m_selected_target_idx = 0;
750 return GetTargetAtIndex (m_selected_target_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751}