blob: 1e0634102ebaa0d0a4fdddd4af783a0935ac3a70 [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();
Vince Harron1b5a74e2015-01-21 22:42:49 +0000129
130 // let's see if there is already an existing plaform before we go creating another...
131 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
132
Greg Claytonccd2a6d2015-01-28 01:33:37 +0000133 if (platform_options && platform_options->PlatformWasSpecified ())
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000134 {
Greg Claytonccd2a6d2015-01-28 01:33:37 +0000135 // Create a new platform if it doesn't match the selected platform
136 if (!platform_options->PlatformMatches(platform_sp))
137 {
138 const bool select_platform = true;
139 platform_sp = platform_options->CreatePlatformWithOptions (interpreter,
140 arch,
141 select_platform,
142 error,
143 platform_arch);
144 if (!platform_sp)
145 return error;
146 }
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000147 }
148
Greg Claytonf4d6de62013-04-24 22:29:28 +0000149 if (user_exe_path && user_exe_path[0])
150 {
151 ModuleSpecList module_specs;
152 ModuleSpec module_spec;
153 module_spec.GetFileSpec().SetFile(user_exe_path, true);
Greg Claytonc76fa8a2014-07-29 21:27:21 +0000154
155 // Resolve the executable in case we are given a path to a application bundle
156 // like a .app bundle on MacOSX
157 Host::ResolveExecutableInBundle (module_spec.GetFileSpec());
158
Greg Claytonf4d6de62013-04-24 22:29:28 +0000159 lldb::offset_t file_offset = 0;
Greg Clayton2540a8a2013-07-12 22:07:46 +0000160 lldb::offset_t file_size = 0;
161 const size_t num_specs = ObjectFile::GetModuleSpecifications (module_spec.GetFileSpec(), file_offset, file_size, module_specs);
Greg Claytonf4d6de62013-04-24 22:29:28 +0000162 if (num_specs > 0)
163 {
164 ModuleSpec matching_module_spec;
165
166 if (num_specs == 1)
167 {
168 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec))
169 {
170 if (platform_arch.IsValid())
171 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000172 if (platform_arch.IsCompatibleMatch(matching_module_spec.GetArchitecture()))
173 {
174 // If the OS or vendor weren't specified, then adopt the module's
175 // architecture so that the platform matching can be more accurate
176 if (!platform_arch.TripleOSWasSpecified() || !platform_arch.TripleVendorWasSpecified())
177 {
178 prefer_platform_arch = true;
179 platform_arch = matching_module_spec.GetArchitecture();
180 }
181 }
182 else
Greg Claytonf4d6de62013-04-24 22:29:28 +0000183 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000184 error.SetErrorStringWithFormat("the specified architecture '%s' is not compatible with '%s' in '%s'",
Greg Claytonf4d6de62013-04-24 22:29:28 +0000185 platform_arch.GetTriple().str().c_str(),
186 matching_module_spec.GetArchitecture().GetTriple().str().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000187 module_spec.GetFileSpec().GetPath().c_str());
Greg Claytonf4d6de62013-04-24 22:29:28 +0000188 return error;
189 }
190 }
191 else
192 {
193 // Only one arch and none was specified
Greg Clayton3f19ada2014-07-10 23:33:37 +0000194 prefer_platform_arch = true;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000195 platform_arch = matching_module_spec.GetArchitecture();
196 }
197 }
198 }
199 else
200 {
201 if (arch.IsValid())
202 {
203 module_spec.GetArchitecture() = arch;
204 if (module_specs.FindMatchingModuleSpec(module_spec, matching_module_spec))
205 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000206 prefer_platform_arch = true;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000207 platform_arch = matching_module_spec.GetArchitecture();
208 }
209 }
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000210 else
211 {
212 // No architecture specified, check if there is only one platform for
213 // all of the architectures.
214
215 typedef std::vector<PlatformSP> PlatformList;
216 PlatformList platforms;
Greg Clayton615eb7e2014-09-19 20:11:50 +0000217 PlatformSP host_platform_sp = Platform::GetHostPlatform();
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000218 for (size_t i=0; i<num_specs; ++i)
219 {
220 ModuleSpec module_spec;
221 if (module_specs.GetModuleSpecAtIndex(i, module_spec))
222 {
223 // See if there was a selected platform and check that first
224 // since the user may have specified it.
225 if (platform_sp)
226 {
227 if (platform_sp->IsCompatibleArchitecture(module_spec.GetArchitecture(), false, NULL))
228 {
229 platforms.push_back(platform_sp);
230 continue;
231 }
232 }
233
234 // Next check the host platform it if wasn't already checked above
235 if (host_platform_sp && (!platform_sp || host_platform_sp->GetName() != platform_sp->GetName()))
236 {
237 if (host_platform_sp->IsCompatibleArchitecture(module_spec.GetArchitecture(), false, NULL))
238 {
239 platforms.push_back(host_platform_sp);
240 continue;
241 }
242 }
243
244 // Just find a platform that matches the architecture in the executable file
245 platforms.push_back(Platform::GetPlatformForArchitecture(module_spec.GetArchitecture(), nullptr));
246 }
247 }
248
249 Platform *platform_ptr = NULL;
250 for (const auto &the_platform_sp : platforms)
251 {
252 if (platform_ptr)
253 {
254 if (platform_ptr->GetName() != the_platform_sp->GetName())
255 {
256 platform_ptr = NULL;
257 break;
258 }
259 }
260 else
261 {
262 platform_ptr = the_platform_sp.get();
263 }
264 }
265
266 if (platform_ptr)
267 {
268 // All platforms for all modules in the exectuable match, so we can select this platform
269 platform_sp = platforms.front();
270 }
271 else
272 {
273 // More than one platform claims to support this file, so the --platform option must be specified
274 StreamString error_strm;
275 std::set<Platform *> platform_set;
276 error_strm.Printf ("more than one platform supports this executable (");
277 for (const auto &the_platform_sp : platforms)
278 {
279 if (platform_set.find(the_platform_sp.get()) == platform_set.end())
280 {
281 if (!platform_set.empty())
282 error_strm.PutCString(", ");
283 error_strm.PutCString(the_platform_sp->GetName().GetCString());
284 platform_set.insert(the_platform_sp.get());
285 }
286 }
287 error_strm.Printf("), use the --platform option to specify a platform");
288 error.SetErrorString(error_strm.GetString().c_str());
289 return error;
290 }
291 }
Greg Claytonf4d6de62013-04-24 22:29:28 +0000292 }
293 }
294 }
295
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000296 if (!platform_sp)
297 {
298 // Get the current platform and make sure it is compatible with the
299 // current architecture if we have a valid architecture.
300 platform_sp = debugger.GetPlatformList().GetSelectedPlatform ();
301
Greg Clayton3f19ada2014-07-10 23:33:37 +0000302 if (!prefer_platform_arch && arch.IsValid())
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000303 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000304 if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch))
Greg Clayton95bbdf62014-09-17 16:42:50 +0000305 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000306 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
Greg Clayton95bbdf62014-09-17 16:42:50 +0000307 if (platform_sp)
308 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
309 }
Greg Clayton3f19ada2014-07-10 23:33:37 +0000310 }
311 else if (platform_arch.IsValid())
312 {
313 // if "arch" isn't valid, yet "platform_arch" is, it means we have an executable file with
314 // a single architecture which should be used
315 ArchSpec fixed_platform_arch;
316 if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, &fixed_platform_arch))
Greg Clayton95bbdf62014-09-17 16:42:50 +0000317 {
Greg Clayton3f19ada2014-07-10 23:33:37 +0000318 platform_sp = Platform::GetPlatformForArchitecture(platform_arch, &fixed_platform_arch);
Greg Clayton95bbdf62014-09-17 16:42:50 +0000319 if (platform_sp)
320 debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
321 }
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000322 }
323 }
Greg Clayton70512312012-05-08 01:45:38 +0000324
325 if (!platform_arch.IsValid())
326 platform_arch = arch;
Greg Claytonb3a40ba2012-03-20 18:34:04 +0000327
Jim Ingham893c9322014-11-22 01:42:44 +0000328 error = TargetList::CreateTargetInternal (debugger,
329 user_exe_path,
330 platform_arch,
331 get_dependent_files,
332 platform_sp,
333 target_sp,
334 is_dummy_target);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000335 return error;
336}
337
Jim Ingham893c9322014-11-22 01:42:44 +0000338lldb::TargetSP
339TargetList::GetDummyTarget (lldb_private::Debugger &debugger)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340{
Jim Ingham893c9322014-11-22 01:42:44 +0000341 // FIXME: Maybe the dummy target should be per-Debugger
342 if (!m_dummy_target_sp || !m_dummy_target_sp->IsValid())
343 {
344 ArchSpec arch(Target::GetDefaultArchitecture());
345 if (!arch.IsValid())
346 arch = HostInfo::GetArchitecture();
347 Error err = CreateDummyTarget(debugger,
348 arch.GetTriple().getTriple().c_str(),
349 m_dummy_target_sp);
350 }
351
352 return m_dummy_target_sp;
353}
354
355Error
356TargetList::CreateDummyTarget (Debugger &debugger,
357 const char *specified_arch_name,
358 lldb::TargetSP &target_sp)
359{
360 PlatformSP host_platform_sp(Platform::GetHostPlatform());
361 return CreateTargetInternal (debugger,
362 (const char *) nullptr,
363 specified_arch_name,
364 false,
365 (const OptionGroupPlatform *) nullptr,
366 target_sp,
367 true);
368}
369
370Error
371TargetList::CreateTargetInternal (Debugger &debugger,
372 const char *user_exe_path,
373 const ArchSpec& specified_arch,
374 bool get_dependent_files,
375 lldb::PlatformSP &platform_sp,
376 lldb::TargetSP &target_sp,
377 bool is_dummy_target)
378{
379
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytona0ca6602012-10-18 16:33:33 +0000381 "TargetList::CreateTarget (file = '%s', arch = '%s')",
382 user_exe_path,
Greg Clayton70512312012-05-08 01:45:38 +0000383 specified_arch.GetArchitectureName());
Jim Ingham5aee1622010-08-09 23:31:02 +0000384 Error error;
Greg Claytonded470d2011-03-19 01:12:21 +0000385
Greg Clayton70512312012-05-08 01:45:38 +0000386 ArchSpec arch(specified_arch);
387
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000388 if (arch.IsValid())
Greg Clayton70512312012-05-08 01:45:38 +0000389 {
Greg Claytonb0cc53c2014-08-20 18:13:03 +0000390 if (!platform_sp || !platform_sp->IsCompatibleArchitecture(arch, false, NULL))
391 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
Greg Clayton70512312012-05-08 01:45:38 +0000392 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000393
Greg Clayton70512312012-05-08 01:45:38 +0000394 if (!platform_sp)
395 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
396
Greg Clayton8ae50eb2012-06-05 21:17:09 +0000397 if (!arch.IsValid())
398 arch = specified_arch;
Greg Clayton8ae50eb2012-06-05 21:17:09 +0000399
Jason Molendad26206b52013-04-19 22:38:50 +0000400 FileSpec file (user_exe_path, false);
401 if (!file.Exists() && user_exe_path && user_exe_path[0] == '~')
402 {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000403 // we want to expand the tilde but we don't want to resolve any symbolic links
404 // so we can't use the FileSpec constructor's resolve flag
Zachary Turner3f559742014-08-07 17:33:36 +0000405 llvm::SmallString<64> unglobbed_path(user_exe_path);
406 FileSpec::ResolveUsername(unglobbed_path);
Michael Sartain9f822cd2013-07-31 23:27:46 +0000407
Zachary Turner3f559742014-08-07 17:33:36 +0000408 if (unglobbed_path.empty())
409 file = FileSpec(user_exe_path, false);
410 else
411 file = FileSpec(unglobbed_path.c_str(), false);
Jason Molendad26206b52013-04-19 22:38:50 +0000412 }
Michael Sartain9f822cd2013-07-31 23:27:46 +0000413
Greg Clayton82d79292012-10-25 22:45:35 +0000414 bool user_exe_path_is_bundle = false;
415 char resolved_bundle_exe_path[PATH_MAX];
416 resolved_bundle_exe_path[0] = '\0';
Greg Claytone996fd32011-03-08 22:40:15 +0000417 if (file)
Jim Ingham5aee1622010-08-09 23:31:02 +0000418 {
Greg Clayton82d79292012-10-25 22:45:35 +0000419 if (file.GetFileType() == FileSpec::eFileTypeDirectory)
420 user_exe_path_is_bundle = true;
421
Jason Molenda14468812014-10-16 01:42:11 +0000422 if (file.IsRelativeToCurrentWorkingDirectory() && user_exe_path)
Greg Claytona0ca6602012-10-18 16:33:33 +0000423 {
424 // Ignore paths that start with "./" and "../"
425 if (!((user_exe_path[0] == '.' && user_exe_path[1] == '/') ||
426 (user_exe_path[0] == '.' && user_exe_path[1] == '.' && user_exe_path[2] == '/')))
427 {
428 char cwd[PATH_MAX];
429 if (getcwd (cwd, sizeof(cwd)))
430 {
431 std::string cwd_user_exe_path (cwd);
432 cwd_user_exe_path += '/';
433 cwd_user_exe_path += user_exe_path;
Greg Clayton9ff5aae2013-04-04 00:15:09 +0000434 FileSpec cwd_file (cwd_user_exe_path.c_str(), false);
435 if (cwd_file.Exists())
436 file = cwd_file;
Greg Claytona0ca6602012-10-18 16:33:33 +0000437 }
438 }
439 }
440
Jim Ingham5aee1622010-08-09 23:31:02 +0000441 ModuleSP exe_module_sp;
Greg Claytone996fd32011-03-08 22:40:15 +0000442 if (platform_sp)
Greg Claytonc859e2d2012-02-13 23:10:39 +0000443 {
444 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
Greg Clayton8012cad2014-11-17 19:39:20 +0000445 ModuleSpec module_spec(file, arch);
446 error = platform_sp->ResolveExecutable (module_spec,
Greg Claytonc859e2d2012-02-13 23:10:39 +0000447 exe_module_sp,
448 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
449 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450
Greg Claytone996fd32011-03-08 22:40:15 +0000451 if (error.Success() && exe_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 {
Jim Ingham5aee1622010-08-09 23:31:02 +0000453 if (exe_module_sp->GetObjectFile() == NULL)
454 {
Greg Claytonbc5cad62010-12-08 04:55:11 +0000455 if (arch.IsValid())
456 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000457 error.SetErrorStringWithFormat("\"%s\" doesn't contain architecture %s",
458 file.GetPath().c_str(),
Greg Clayton64195a22011-02-23 00:35:02 +0000459 arch.GetArchitectureName());
Greg Claytonbc5cad62010-12-08 04:55:11 +0000460 }
461 else
462 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000463 error.SetErrorStringWithFormat("unsupported file type \"%s\"",
464 file.GetPath().c_str());
Greg Claytonbc5cad62010-12-08 04:55:11 +0000465 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000466 return error;
467 }
Jim Ingham893c9322014-11-22 01:42:44 +0000468 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
Jim Ingham5aee1622010-08-09 23:31:02 +0000469 target_sp->SetExecutableModule (exe_module_sp, get_dependent_files);
Greg Clayton82d79292012-10-25 22:45:35 +0000470 if (user_exe_path_is_bundle)
471 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, sizeof(resolved_bundle_exe_path));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000473 }
Greg Claytone996fd32011-03-08 22:40:15 +0000474 else
Jim Ingham5aee1622010-08-09 23:31:02 +0000475 {
Greg Claytone996fd32011-03-08 22:40:15 +0000476 // No file was specified, just create an empty target with any arch
477 // if a valid arch was specified
Jim Ingham893c9322014-11-22 01:42:44 +0000478 target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target));
Greg Claytone996fd32011-03-08 22:40:15 +0000479 }
480
481 if (target_sp)
482 {
Greg Clayton82d79292012-10-25 22:45:35 +0000483 // Set argv0 with what the user typed, unless the user specified a
484 // directory. If the user specified a directory, then it is probably a
485 // bundle that was resolved and we need to use the resolved bundle path
Greg Claytona0ca6602012-10-18 16:33:33 +0000486 if (user_exe_path)
487 {
488 // Use exactly what the user typed as the first argument when we exec or posix_spawn
Greg Clayton82d79292012-10-25 22:45:35 +0000489 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0])
490 {
491 target_sp->SetArg0 (resolved_bundle_exe_path);
492 }
493 else
494 {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000495 // Use resolved path
496 target_sp->SetArg0 (file.GetPath().c_str());
Greg Clayton82d79292012-10-25 22:45:35 +0000497 }
Greg Claytona0ca6602012-10-18 16:33:33 +0000498 }
499 if (file.GetDirectory())
500 {
501 FileSpec file_dir;
502 file_dir.GetDirectory() = file.GetDirectory();
503 target_sp->GetExecutableSearchPaths ().Append (file_dir);
504 }
Jim Ingham893c9322014-11-22 01:42:44 +0000505
506 // Don't put the dummy target in the target list, it's held separately.
507 if (!is_dummy_target)
508 {
509 Mutex::Locker locker(m_target_list_mutex);
510 m_selected_target_idx = m_target_list.size();
511 m_target_list.push_back(target_sp);
Jim Ingham33df7cd2014-12-06 01:28:03 +0000512 // Now prime this from the dummy target:
513 target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget());
Jim Ingham893c9322014-11-22 01:42:44 +0000514 }
515 else
516 {
517 m_dummy_target_sp = target_sp;
518 }
Jim Ingham5aee1622010-08-09 23:31:02 +0000519 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 return error;
522}
523
524bool
525TargetList::DeleteTarget (TargetSP &target_sp)
526{
527 Mutex::Locker locker(m_target_list_mutex);
528 collection::iterator pos, end = m_target_list.end();
529
530 for (pos = m_target_list.begin(); pos != end; ++pos)
531 {
532 if (pos->get() == target_sp.get())
533 {
534 m_target_list.erase(pos);
535 return true;
536 }
537 }
538 return false;
539}
540
541
542TargetSP
543TargetList::FindTargetWithExecutableAndArchitecture
544(
545 const FileSpec &exe_file_spec,
546 const ArchSpec *exe_arch_ptr
547) const
548{
549 Mutex::Locker locker (m_target_list_mutex);
550 TargetSP target_sp;
Sean Callananddd7a2a2013-10-03 22:27:29 +0000551 bool full_match = (bool)exe_file_spec.GetDirectory();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552
553 collection::const_iterator pos, end = m_target_list.end();
554 for (pos = m_target_list.begin(); pos != end; ++pos)
555 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000556 Module *exe_module = (*pos)->GetExecutableModulePointer();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557
Greg Claytonaa149cb2011-08-11 02:48:45 +0000558 if (exe_module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000560 if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 {
562 if (exe_arch_ptr)
563 {
Sean Callananbf4b7be2012-12-13 22:07:14 +0000564 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 continue;
566 }
567 target_sp = *pos;
568 break;
569 }
570 }
571 }
572 return target_sp;
573}
574
575TargetSP
576TargetList::FindTargetWithProcessID (lldb::pid_t pid) const
577{
578 Mutex::Locker locker(m_target_list_mutex);
579 TargetSP target_sp;
580 collection::const_iterator pos, end = m_target_list.end();
581 for (pos = m_target_list.begin(); pos != end; ++pos)
582 {
583 Process* process = (*pos)->GetProcessSP().get();
584 if (process && process->GetID() == pid)
585 {
586 target_sp = *pos;
587 break;
588 }
589 }
590 return target_sp;
591}
592
593
594TargetSP
595TargetList::FindTargetWithProcess (Process *process) const
596{
597 TargetSP target_sp;
598 if (process)
599 {
600 Mutex::Locker locker(m_target_list_mutex);
601 collection::const_iterator pos, end = m_target_list.end();
602 for (pos = m_target_list.begin(); pos != end; ++pos)
603 {
604 if (process == (*pos)->GetProcessSP().get())
605 {
606 target_sp = *pos;
607 break;
608 }
609 }
610 }
611 return target_sp;
612}
613
614TargetSP
615TargetList::GetTargetSP (Target *target) const
616{
617 TargetSP target_sp;
618 if (target)
619 {
620 Mutex::Locker locker(m_target_list_mutex);
621 collection::const_iterator pos, end = m_target_list.end();
622 for (pos = m_target_list.begin(); pos != end; ++pos)
623 {
624 if (target == (*pos).get())
625 {
626 target_sp = *pos;
627 break;
628 }
629 }
630 }
631 return target_sp;
632}
633
634uint32_t
635TargetList::SendAsyncInterrupt (lldb::pid_t pid)
636{
637 uint32_t num_async_interrupts_sent = 0;
638
639 if (pid != LLDB_INVALID_PROCESS_ID)
640 {
641 TargetSP target_sp(FindTargetWithProcessID (pid));
642 if (target_sp.get())
643 {
644 Process* process = target_sp->GetProcessSP().get();
645 if (process)
646 {
Jim Inghamcfc09352012-07-27 23:57:19 +0000647 process->SendAsyncInterrupt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 ++num_async_interrupts_sent;
649 }
650 }
651 }
652 else
653 {
654 // We don't have a valid pid to broadcast to, so broadcast to the target
655 // list's async broadcaster...
656 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
657 }
658
659 return num_async_interrupts_sent;
660}
661
662uint32_t
663TargetList::SignalIfRunning (lldb::pid_t pid, int signo)
664{
665 uint32_t num_signals_sent = 0;
666 Process *process = NULL;
667 if (pid == LLDB_INVALID_PROCESS_ID)
668 {
669 // Signal all processes with signal
670 Mutex::Locker locker(m_target_list_mutex);
671 collection::iterator pos, end = m_target_list.end();
672 for (pos = m_target_list.begin(); pos != end; ++pos)
673 {
674 process = (*pos)->GetProcessSP().get();
675 if (process)
676 {
677 if (process->IsAlive())
678 {
679 ++num_signals_sent;
680 process->Signal (signo);
681 }
682 }
683 }
684 }
685 else
686 {
687 // Signal a specific process with signal
688 TargetSP target_sp(FindTargetWithProcessID (pid));
689 if (target_sp.get())
690 {
691 process = target_sp->GetProcessSP().get();
692 if (process)
693 {
694 if (process->IsAlive())
695 {
696 ++num_signals_sent;
697 process->Signal (signo);
698 }
699 }
700 }
701 }
702 return num_signals_sent;
703}
704
705int
706TargetList::GetNumTargets () const
707{
708 Mutex::Locker locker (m_target_list_mutex);
709 return m_target_list.size();
710}
711
712lldb::TargetSP
713TargetList::GetTargetAtIndex (uint32_t idx) const
714{
715 TargetSP target_sp;
716 Mutex::Locker locker (m_target_list_mutex);
717 if (idx < m_target_list.size())
718 target_sp = m_target_list[idx];
719 return target_sp;
720}
721
722uint32_t
Jim Ingham8499e1a2012-05-08 23:06:07 +0000723TargetList::GetIndexOfTarget (lldb::TargetSP target_sp) const
724{
725 Mutex::Locker locker (m_target_list_mutex);
726 size_t num_targets = m_target_list.size();
727 for (size_t idx = 0; idx < num_targets; idx++)
728 {
729 if (target_sp == m_target_list[idx])
730 return idx;
731 }
732 return UINT32_MAX;
733}
734
735uint32_t
Jim Ingham2976d002010-08-26 21:32:51 +0000736TargetList::SetSelectedTarget (Target* target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737{
738 Mutex::Locker locker (m_target_list_mutex);
739 collection::const_iterator pos,
740 begin = m_target_list.begin(),
741 end = m_target_list.end();
742 for (pos = begin; pos != end; ++pos)
743 {
744 if (pos->get() == target)
745 {
Jim Ingham2976d002010-08-26 21:32:51 +0000746 m_selected_target_idx = std::distance (begin, pos);
747 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 }
749 }
Jim Ingham2976d002010-08-26 21:32:51 +0000750 m_selected_target_idx = 0;
751 return m_selected_target_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752}
753
754lldb::TargetSP
Jim Ingham2976d002010-08-26 21:32:51 +0000755TargetList::GetSelectedTarget ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756{
757 Mutex::Locker locker (m_target_list_mutex);
Jim Ingham2976d002010-08-26 21:32:51 +0000758 if (m_selected_target_idx >= m_target_list.size())
759 m_selected_target_idx = 0;
760 return GetTargetAtIndex (m_selected_target_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761}