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()) {
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) {
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()) {
192 "no matching platforms found for this file");
197 std::set<llvm::StringRef> platform_set;
199 "more than one platform supports this executable (");
200 for (
const auto &candidate : candidates) {
201 llvm::StringRef platform_name = candidate->GetName();
202 if (platform_set.count(platform_name))
204 if (!platform_set.empty())
207 platform_set.insert(platform_name);
209 error_strm.
Printf(
"), specify an architecture to disambiguate");
219 if (!prefer_platform_arch && arch.
IsValid()) {
220 if (!platform_sp->IsCompatibleArchitecture(
222 platform_sp = platform_list.
GetOrCreate(arch, {}, &platform_arch);
226 }
else if (platform_arch.
IsValid()) {
230 if (!platform_sp->IsCompatibleArchitecture(
233 platform_list.
GetOrCreate(platform_arch, {}, &fixed_platform_arch);
240 platform_arch = arch;
243 platform_arch, load_dependent_files,
244 platform_sp, target_sp);
248 llvm::StringRef user_exe_path,
254 user_exe_path.str().c_str(),
257 const bool is_dummy_target =
false;
262 if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
272 arch = specified_arch;
278 llvm::SmallString<64> unglobbed_path;
282 if (unglobbed_path.empty())
285 file =
FileSpec(unglobbed_path.c_str());
288 bool user_exe_path_is_bundle =
false;
289 char resolved_bundle_exe_path[
PATH_MAX];
290 resolved_bundle_exe_path[0] =
'\0';
293 user_exe_path_is_bundle =
true;
295 if (file.
IsRelative() && !user_exe_path.empty()) {
296 llvm::SmallString<64> cwd;
297 if (! llvm::sys::fs::current_path(cwd)) {
310 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp,
311 executable_search_paths.
GetSize()
312 ? &executable_search_paths
316 if (
error.Success() && exe_module_sp) {
317 if (exe_module_sp->GetObjectFile() ==
nullptr) {
320 "\"%s\" doesn't contain architecture %s", file.
GetPath().c_str(),
324 "unsupported file type \"%s\"", file.
GetPath().c_str());
328 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
330 target_sp->SetExecutableModule(exe_module_sp, load_dependent_files);
331 if (user_exe_path_is_bundle)
332 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
333 sizeof(resolved_bundle_exe_path));
334 if (target_sp->GetPreloadSymbols())
335 exe_module_sp->PreloadSymbols();
340 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
350 if (!user_exe_path.empty()) {
353 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
354 target_sp->SetArg0(resolved_bundle_exe_path);
357 target_sp->SetArg0(file.
GetPath().c_str());
363 target_sp->AppendExecutableSearchPaths(file_dir);
386 [&exe_file_spec, exe_arch_ptr](
const TargetSP &item) {
387 Module *exe_module = item->GetExecutableModulePointer();
389 !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec()))
392 return !exe_arch_ptr ||
393 exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture());
406 auto *process_ptr = item->GetProcessSP().get();
407 return process_ptr && (process_ptr->GetID() == pid);
424 return item->GetProcessSP().get() == process;
440 [target](
const TargetSP &item) { return item.get() == target; });
448 uint32_t num_async_interrupts_sent = 0;
453 Process *process = target_sp->GetProcessSP().get();
456 ++num_async_interrupts_sent;
465 return num_async_interrupts_sent;
469 uint32_t num_signals_sent = 0;
475 process = target_sp->GetProcessSP().get();
476 if (process && process->
IsAlive()) {
485 process = target_sp->GetProcessSP().get();
486 if (process && process->
IsAlive()) {
492 return num_signals_sent;
518 "target already exists it the list");
538 if (target_sp && target_sp->IsValid()) {
555 if (target_sp->GetImages().FindModule(&module))
559 if (target_sp->GetImages().FindModule(&module))
567 [[maybe_unused]]
bool was_added;
568 std::tie(std::ignore, was_added) =
570 assert(was_added &&
"Target pointer was left in the in-process map");
575 [[maybe_unused]]
bool was_present =
577 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.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
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