1 //===-- TargetList.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Broadcaster.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Event.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/ModuleSpec.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Core/Timer.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Interpreter/CommandInterpreter.h"
25 #include "lldb/Interpreter/OptionGroupPlatform.h"
26 #include "lldb/Symbol/ObjectFile.h"
27 #include "lldb/Target/Platform.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/TargetList.h"
32 using namespace lldb_private;
35 TargetList::GetStaticBroadcasterClass ()
37 static ConstString class_name ("lldb.targetList");
41 //----------------------------------------------------------------------
42 // TargetList constructor
43 //----------------------------------------------------------------------
44 TargetList::TargetList(Debugger &debugger) :
45 Broadcaster(&debugger, TargetList::GetStaticBroadcasterClass().AsCString()),
47 m_target_list_mutex (Mutex::eMutexTypeRecursive),
48 m_selected_target_idx (0)
53 //----------------------------------------------------------------------
55 //----------------------------------------------------------------------
56 TargetList::~TargetList()
58 Mutex::Locker locker(m_target_list_mutex);
59 m_target_list.clear();
63 TargetList::CreateTarget (Debugger &debugger,
64 const char *user_exe_path,
65 const char *triple_cstr,
66 bool get_dependent_files,
67 const OptionGroupPlatform *platform_options,
71 PlatformSP platform_sp;
73 // 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.
76 const ArchSpec arch(triple_cstr);
77 if (triple_cstr && triple_cstr[0])
81 error.SetErrorStringWithFormat("invalid triple '%s'", triple_cstr);
86 ArchSpec platform_arch(arch);
89 if (user_exe_path && user_exe_path[0])
91 ModuleSpecList module_specs;
92 ModuleSpec module_spec;
93 module_spec.GetFileSpec().SetFile(user_exe_path, true);
94 lldb::offset_t file_offset = 0;
95 lldb::offset_t file_size = 0;
96 const size_t num_specs = ObjectFile::GetModuleSpecifications (module_spec.GetFileSpec(), file_offset, file_size, module_specs);
99 ModuleSpec matching_module_spec;
103 if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec))
105 if (platform_arch.IsValid())
107 if (!platform_arch.IsCompatibleMatch(matching_module_spec.GetArchitecture()))
109 error.SetErrorStringWithFormat("the specified architecture '%s' is not compatible with '%s' in '%s'",
110 platform_arch.GetTriple().str().c_str(),
111 matching_module_spec.GetArchitecture().GetTriple().str().c_str(),
112 module_spec.GetFileSpec().GetPath().c_str());
118 // Only one arch and none was specified
119 platform_arch = matching_module_spec.GetArchitecture();
127 module_spec.GetArchitecture() = arch;
128 if (module_specs.FindMatchingModuleSpec(module_spec, matching_module_spec))
130 platform_arch = matching_module_spec.GetArchitecture();
133 // Don't just select the first architecture, we want to let the platform select
134 // the best architecture first when there are multiple archs.
137 // // No arch specified, select the first arch
138 // if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec))
140 // platform_arch = matching_module_spec.GetArchitecture();
147 CommandInterpreter &interpreter = debugger.GetCommandInterpreter();
148 if (platform_options)
150 if (platform_options->PlatformWasSpecified ())
152 const bool select_platform = true;
153 platform_sp = platform_options->CreatePlatformWithOptions (interpreter,
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 ();
169 if (arch.IsValid() && !platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch))
171 platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
175 if (!platform_arch.IsValid())
176 platform_arch = arch;
178 error = TargetList::CreateTarget (debugger,
188 TargetList::CreateTarget (Debugger &debugger,
189 const char *user_exe_path,
190 const ArchSpec& specified_arch,
191 bool get_dependent_files,
192 PlatformSP &platform_sp,
195 Timer scoped_timer (__PRETTY_FUNCTION__,
196 "TargetList::CreateTarget (file = '%s', arch = '%s')",
198 specified_arch.GetArchitectureName());
201 ArchSpec arch(specified_arch);
207 if (!platform_sp->IsCompatibleArchitecture(arch, false, NULL))
208 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
211 else if (arch.IsValid())
213 platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch);
217 platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
220 arch = specified_arch;
222 FileSpec file (user_exe_path, false);
223 if (!file.Exists() && user_exe_path && user_exe_path[0] == '~')
225 // we want to expand the tilde but we don't want to resolve any symbolic links
226 // so we can't use the FileSpec constructor's resolve flag
227 char unglobbed_path[PATH_MAX];
228 unglobbed_path[0] = '\0';
230 size_t return_count = FileSpec::ResolveUsername(user_exe_path, unglobbed_path, sizeof(unglobbed_path));
232 if (return_count == 0 || return_count >= sizeof(unglobbed_path))
233 ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", user_exe_path);
235 file = FileSpec(unglobbed_path, false);
238 bool user_exe_path_is_bundle = false;
239 char resolved_bundle_exe_path[PATH_MAX];
240 resolved_bundle_exe_path[0] = '\0';
243 if (file.GetFileType() == FileSpec::eFileTypeDirectory)
244 user_exe_path_is_bundle = true;
246 if (file.IsRelativeToCurrentWorkingDirectory())
248 // Ignore paths that start with "./" and "../"
249 if (!((user_exe_path[0] == '.' && user_exe_path[1] == '/') ||
250 (user_exe_path[0] == '.' && user_exe_path[1] == '.' && user_exe_path[2] == '/')))
253 if (getcwd (cwd, sizeof(cwd)))
255 std::string cwd_user_exe_path (cwd);
256 cwd_user_exe_path += '/';
257 cwd_user_exe_path += user_exe_path;
258 FileSpec cwd_file (cwd_user_exe_path.c_str(), false);
259 if (cwd_file.Exists())
265 ModuleSP exe_module_sp;
268 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
269 error = platform_sp->ResolveExecutable (file,
272 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
275 if (error.Success() && exe_module_sp)
277 if (exe_module_sp->GetObjectFile() == NULL)
281 error.SetErrorStringWithFormat("\"%s\" doesn't contain architecture %s",
282 file.GetPath().c_str(),
283 arch.GetArchitectureName());
287 error.SetErrorStringWithFormat("unsupported file type \"%s\"",
288 file.GetPath().c_str());
292 target_sp.reset(new Target(debugger, arch, platform_sp));
293 target_sp->SetExecutableModule (exe_module_sp, get_dependent_files);
294 if (user_exe_path_is_bundle)
295 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, sizeof(resolved_bundle_exe_path));
300 // No file was specified, just create an empty target with any arch
301 // if a valid arch was specified
302 target_sp.reset(new Target(debugger, arch, platform_sp));
307 // Set argv0 with what the user typed, unless the user specified a
308 // directory. If the user specified a directory, then it is probably a
309 // bundle that was resolved and we need to use the resolved bundle path
312 // Use exactly what the user typed as the first argument when we exec or posix_spawn
313 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0])
315 target_sp->SetArg0 (resolved_bundle_exe_path);
320 target_sp->SetArg0 (file.GetPath().c_str());
323 if (file.GetDirectory())
326 file_dir.GetDirectory() = file.GetDirectory();
327 target_sp->GetExecutableSearchPaths ().Append (file_dir);
329 Mutex::Locker locker(m_target_list_mutex);
330 m_selected_target_idx = m_target_list.size();
331 m_target_list.push_back(target_sp);
340 TargetList::DeleteTarget (TargetSP &target_sp)
342 Mutex::Locker locker(m_target_list_mutex);
343 collection::iterator pos, end = m_target_list.end();
345 for (pos = m_target_list.begin(); pos != end; ++pos)
347 if (pos->get() == target_sp.get())
349 m_target_list.erase(pos);
358 TargetList::FindTargetWithExecutableAndArchitecture
360 const FileSpec &exe_file_spec,
361 const ArchSpec *exe_arch_ptr
364 Mutex::Locker locker (m_target_list_mutex);
366 bool full_match = (bool)exe_file_spec.GetDirectory();
368 collection::const_iterator pos, end = m_target_list.end();
369 for (pos = m_target_list.begin(); pos != end; ++pos)
371 Module *exe_module = (*pos)->GetExecutableModulePointer();
375 if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match))
379 if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
391 TargetList::FindTargetWithProcessID (lldb::pid_t pid) const
393 Mutex::Locker locker(m_target_list_mutex);
395 collection::const_iterator pos, end = m_target_list.end();
396 for (pos = m_target_list.begin(); pos != end; ++pos)
398 Process* process = (*pos)->GetProcessSP().get();
399 if (process && process->GetID() == pid)
410 TargetList::FindTargetWithProcess (Process *process) const
415 Mutex::Locker locker(m_target_list_mutex);
416 collection::const_iterator pos, end = m_target_list.end();
417 for (pos = m_target_list.begin(); pos != end; ++pos)
419 if (process == (*pos)->GetProcessSP().get())
430 TargetList::GetTargetSP (Target *target) const
435 Mutex::Locker locker(m_target_list_mutex);
436 collection::const_iterator pos, end = m_target_list.end();
437 for (pos = m_target_list.begin(); pos != end; ++pos)
439 if (target == (*pos).get())
450 TargetList::SendAsyncInterrupt (lldb::pid_t pid)
452 uint32_t num_async_interrupts_sent = 0;
454 if (pid != LLDB_INVALID_PROCESS_ID)
456 TargetSP target_sp(FindTargetWithProcessID (pid));
459 Process* process = target_sp->GetProcessSP().get();
462 process->SendAsyncInterrupt();
463 ++num_async_interrupts_sent;
469 // We don't have a valid pid to broadcast to, so broadcast to the target
470 // list's async broadcaster...
471 BroadcastEvent (Process::eBroadcastBitInterrupt, NULL);
474 return num_async_interrupts_sent;
478 TargetList::SignalIfRunning (lldb::pid_t pid, int signo)
480 uint32_t num_signals_sent = 0;
481 Process *process = NULL;
482 if (pid == LLDB_INVALID_PROCESS_ID)
484 // Signal all processes with signal
485 Mutex::Locker locker(m_target_list_mutex);
486 collection::iterator pos, end = m_target_list.end();
487 for (pos = m_target_list.begin(); pos != end; ++pos)
489 process = (*pos)->GetProcessSP().get();
492 if (process->IsAlive())
495 process->Signal (signo);
502 // Signal a specific process with signal
503 TargetSP target_sp(FindTargetWithProcessID (pid));
506 process = target_sp->GetProcessSP().get();
509 if (process->IsAlive())
512 process->Signal (signo);
517 return num_signals_sent;
521 TargetList::GetNumTargets () const
523 Mutex::Locker locker (m_target_list_mutex);
524 return m_target_list.size();
528 TargetList::GetTargetAtIndex (uint32_t idx) const
531 Mutex::Locker locker (m_target_list_mutex);
532 if (idx < m_target_list.size())
533 target_sp = m_target_list[idx];
538 TargetList::GetIndexOfTarget (lldb::TargetSP target_sp) const
540 Mutex::Locker locker (m_target_list_mutex);
541 size_t num_targets = m_target_list.size();
542 for (size_t idx = 0; idx < num_targets; idx++)
544 if (target_sp == m_target_list[idx])
551 TargetList::SetSelectedTarget (Target* target)
553 Mutex::Locker locker (m_target_list_mutex);
554 collection::const_iterator pos,
555 begin = m_target_list.begin(),
556 end = m_target_list.end();
557 for (pos = begin; pos != end; ++pos)
559 if (pos->get() == target)
561 m_selected_target_idx = std::distance (begin, pos);
562 return m_selected_target_idx;
565 m_selected_target_idx = 0;
566 return m_selected_target_idx;
570 TargetList::GetSelectedTarget ()
572 Mutex::Locker locker (m_target_list_mutex);
573 if (m_selected_target_idx >= m_target_list.size())
574 m_selected_target_idx = 0;
575 return GetTargetAtIndex (m_selected_target_idx);