26#include "llvm/ADT/SmallString.h"
27#include "llvm/Support/FileSystem.h"
33 static constexpr llvm::StringLiteral class_name(
"lldb.targetList");
40 TargetList::GetStaticBroadcasterClass().str()),
41 m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) {
46 llvm::StringRef user_exe_path,
47 llvm::StringRef triple_str,
53 debugger, user_exe_path, triple_str, load_dependent_files,
54 platform_options, target_sp);
56 if (target_sp && result.Success())
62 llvm::StringRef user_exe_path,
68 debugger, user_exe_path, specified_arch, load_dependent_files,
69 platform_sp, target_sp);
71 if (target_sp && result.Success())
77 Debugger &debugger, llvm::StringRef user_exe_path,
90 if (!triple_str.empty() && !arch.
IsValid()) {
91 error.SetErrorStringWithFormat(
"invalid triple '%s'",
92 triple_str.str().c_str());
102 const bool select_platform =
true;
110 bool prefer_platform_arch =
false;
111 auto update_platform_arch = [&](
const ArchSpec &module_arch) {
116 prefer_platform_arch =
true;
117 platform_arch = module_arch;
121 if (!user_exe_path.empty()) {
127 if (platform_sp->IsHost() &&
140 module_spec.
GetFileSpec(), file_offset, file_size, module_specs);
145 if (num_specs == 1) {
161 error.SetErrorStringWithFormat(
162 "the specified architecture '%s' is not compatible with '%s' "
170 prefer_platform_arch =
true;
178 matching_module_spec))
183 std::vector<PlatformSP> candidates;
184 std::vector<ArchSpec> archs;
186 archs.push_back(spec.GetArchitecture());
188 platform_list.
GetOrCreate(archs, {}, candidates)) {
189 platform_sp = platform_for_archs_sp;
190 }
else if (candidates.empty()) {
191 error.SetErrorString(
"no matching platforms found for this file");
196 std::set<llvm::StringRef> platform_set;
198 "more than one platform supports this executable (");
199 for (
const auto &candidate : candidates) {
200 llvm::StringRef platform_name = candidate->GetName();
201 if (platform_set.count(platform_name))
203 if (!platform_set.empty())
206 platform_set.insert(platform_name);
208 error_strm.
Printf(
"), specify an architecture to disambiguate");
218 if (!prefer_platform_arch && arch.
IsValid()) {
219 if (!platform_sp->IsCompatibleArchitecture(
221 platform_sp = platform_list.
GetOrCreate(arch, {}, &platform_arch);
225 }
else if (platform_arch.
IsValid()) {
229 if (!platform_sp->IsCompatibleArchitecture(
232 platform_list.
GetOrCreate(platform_arch, {}, &fixed_platform_arch);
239 platform_arch = arch;
242 platform_arch, load_dependent_files,
243 platform_sp, target_sp);
247 llvm::StringRef user_exe_path,
253 user_exe_path.str().c_str(),
256 const bool is_dummy_target =
false;
261 if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
271 arch = specified_arch;
277 llvm::SmallString<64> unglobbed_path;
281 if (unglobbed_path.empty())
284 file =
FileSpec(unglobbed_path.c_str());
287 bool user_exe_path_is_bundle =
false;
288 char resolved_bundle_exe_path[
PATH_MAX];
289 resolved_bundle_exe_path[0] =
'\0';
292 user_exe_path_is_bundle =
true;
294 if (file.
IsRelative() && !user_exe_path.empty()) {
295 llvm::SmallString<64> cwd;
296 if (! llvm::sys::fs::current_path(cwd)) {
309 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp,
310 executable_search_paths.
GetSize()
311 ? &executable_search_paths
315 if (
error.Success() && exe_module_sp) {
316 if (exe_module_sp->GetObjectFile() ==
nullptr) {
318 error.SetErrorStringWithFormat(
319 "\"%s\" doesn't contain architecture %s", file.
GetPath().c_str(),
322 error.SetErrorStringWithFormat(
"unsupported file type \"%s\"",
327 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
329 target_sp->SetExecutableModule(exe_module_sp, load_dependent_files);
330 if (user_exe_path_is_bundle)
331 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
332 sizeof(resolved_bundle_exe_path));
333 if (target_sp->GetPreloadSymbols())
334 exe_module_sp->PreloadSymbols();
339 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
349 if (!user_exe_path.empty()) {
352 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
353 target_sp->SetArg0(resolved_bundle_exe_path);
356 target_sp->SetArg0(file.
GetPath().c_str());
362 target_sp->AppendExecutableSearchPaths(file_dir);
385 [&exe_file_spec, exe_arch_ptr](
const TargetSP &item) {
386 Module *exe_module = item->GetExecutableModulePointer();
388 !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec()))
391 return !exe_arch_ptr ||
392 exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture());
405 auto *process_ptr = item->GetProcessSP().get();
406 return process_ptr && (process_ptr->GetID() == pid);
423 return item->GetProcessSP().get() == process;
439 [target](
const TargetSP &item) { return item.get() == target; });
447 uint32_t num_async_interrupts_sent = 0;
452 Process *process = target_sp->GetProcessSP().get();
455 ++num_async_interrupts_sent;
464 return num_async_interrupts_sent;
468 uint32_t num_signals_sent = 0;
474 process = target_sp->GetProcessSP().get();
475 if (process && process->
IsAlive()) {
484 process = target_sp->GetProcessSP().get();
485 if (process && process->
IsAlive()) {
491 return num_signals_sent;
517 "target already exists it the list");
537 if (target_sp && target_sp->IsValid()) {
554 if (target_sp->GetImages().FindModule(&module))
558 if (target_sp->GetImages().FindModule(&module))
566 [[maybe_unused]]
bool was_added;
567 std::tie(std::ignore, was_added) =
569 assert(was_added &&
"Target pointer was left in the in-process map");
574 [[maybe_unused]]
bool was_present =
576 assert(was_present &&
"Target pointer being removed was not registered");
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_SCOPED_TIMERF(...)
An architecture specification class.
bool IsValid() const
Tests if this ArchSpec is valid.
void DumpTriple(llvm::raw_ostream &s) const
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
bool TripleVendorWasSpecified() const
bool TripleOSWasSpecified() const
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
An event broadcasting class.
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
void CheckInWithManager()
A class to manage flag bits.
CommandInterpreter & GetCommandInterpreter()
TargetList & GetTargetList()
Get accessor for the target list.
PlatformList & GetPlatformList()
Target & GetDummyTarget()
size_t GetSize() const
Get the number of files in the file list.
void AppendPathComponent(llvm::StringRef component)
void SetDirectory(ConstString directory)
Directory string set accessor.
bool IsRelative() const
Returns true if the filespec represents a relative path.
const ConstString & GetDirectory() const
Directory string const get accessor.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
static bool ResolveExecutableInBundle(FileSpec &file)
When executable files may live within a directory, where the directory represents an executable bundl...
ModuleSpecIterable ModuleSpecs()
bool GetModuleSpecAtIndex(size_t i, ModuleSpec &module_spec) const
bool FindMatchingModuleSpec(const ModuleSpec &module_spec, ModuleSpec &match_module_spec) const
ArchSpec & GetArchitecture()
A class that describes an executable image and its associated object and symbol files.
static size_t GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs, lldb::DataBufferSP data_sp=lldb::DataBufferSP())
A plug-in interface definition class for debugging a process.
void SendAsyncInterrupt(Thread *thread=nullptr)
Send an async interrupt request.
Status Signal(int signal)
Sends a process a UNIX signal signal.
virtual bool IsAlive()
Check if a process is still alive.
const char * GetData() const
llvm::StringRef GetString() const
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
void SetSelectedTargetInternal(uint32_t index)
static llvm::StringRef GetStaticBroadcasterClass()
lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid) const
Find the target that contains a process with process ID pid.
bool IsTargetInProcess(lldb::TargetSP target_sp)
lldb::TargetSP GetTargetAtIndex(uint32_t index) const
TargetList(Debugger &debugger)
Constructor.
std::recursive_mutex m_target_list_mutex
uint32_t GetIndexOfTarget(lldb::TargetSP target_sp) const
lldb::TargetSP GetTargetSP(Target *target) const
void UnregisterInProcessTarget(lldb::TargetSP target_sp)
void SetSelectedTarget(uint32_t index)
uint32_t SendAsyncInterrupt(lldb::pid_t pid=LLDB_INVALID_PROCESS_ID)
Send an async interrupt to one or all processes.
bool DeleteTarget(lldb::TargetSP &target_sp)
Delete a Target object from the list.
lldb::TargetSP FindTargetWithProcess(lldb_private::Process *process) const
void RegisterInProcessTarget(lldb::TargetSP target_sp)
Status CreateTarget(Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles get_dependent_modules, const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp)
Create a new Target.
lldb::TargetSP GetSelectedTarget()
void AddTargetInternal(lldb::TargetSP target_sp, bool do_select)
static Status CreateTargetInternal(Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles load_dependent_files, const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp)
bool AnyTargetContainsModule(Module &module)
Returns whether any module, including ones in the process of being added, contains this module.
size_t GetNumTargets() const
uint32_t SignalIfRunning(lldb::pid_t pid, int signo)
lldb::TargetSP FindTargetWithExecutableAndArchitecture(const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr=nullptr) const
Find the target that contains has an executable whose path matches exe_file_spec, and whose architect...
std::unordered_set< lldb::TargetSP > m_in_process_target_list
uint32_t m_selected_target_idx
static FileSpecList GetDefaultExecutableSearchPaths()
bool ResolveFullPath(llvm::StringRef Expr, llvm::SmallVectorImpl< char > &Output)
Resolve an entire path that begins with a tilde expression, replacing the username portion with the m...
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Platform > PlatformSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP