26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/FileSystem.h"
40 TargetList::GetStaticBroadcasterClass().AsCString()),
41 m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) {
46 llvm::StringRef user_exe_path,
47 llvm::StringRef triple_str,
50 TargetSP &target_sp) {
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,
65 PlatformSP &platform_sp, TargetSP &target_sp) {
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) {
114 if (!platform_arch.TripleOSWasSpecified() ||
115 !platform_arch.TripleVendorWasSpecified()) {
116 prefer_platform_arch =
true;
117 platform_arch = module_arch;
121 if (!user_exe_path.empty()) {
127 if (platform_sp->IsHost() &&
130 module_spec.GetFileSpec());
140 module_spec.GetFileSpec(), file_offset, file_size, module_specs);
145 if (num_specs == 1) {
147 if (platform_arch.IsValid()) {
148 if (platform_arch.IsCompatibleMatch(
158 platform_arch.DumpTriple(platform_arch_strm.
AsRawOstream());
161 error.SetErrorStringWithFormat(
162 "the specified architecture '%s' is not compatible with '%s' "
165 module_spec.GetFileSpec().GetPath().c_str());
170 prefer_platform_arch =
true;
174 }
else if (arch.IsValid()) {
176 module_spec.GetArchitecture() = arch;
178 matching_module_spec))
183 std::vector<PlatformSP> candidates;
184 std::vector<ArchSpec> archs;
186 archs.push_back(spec.GetArchitecture());
187 if (PlatformSP platform_for_archs_sp =
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(arch, {},
false,
nullptr)) {
220 platform_sp = platform_list.
GetOrCreate(arch, {}, &platform_arch);
224 }
else if (platform_arch.IsValid()) {
228 if (!platform_sp->IsCompatibleArchitecture(platform_arch, {},
false,
nullptr)) {
230 platform_list.
GetOrCreate(platform_arch, {}, &fixed_platform_arch);
236 if (!platform_arch.IsValid())
237 platform_arch = arch;
240 platform_arch, load_dependent_files,
241 platform_sp, target_sp);
245 llvm::StringRef user_exe_path,
248 lldb::PlatformSP &platform_sp,
249 lldb::TargetSP &target_sp) {
251 user_exe_path.str().c_str(),
254 const bool is_dummy_target =
false;
260 !platform_sp->IsCompatibleArchitecture(arch, {},
false,
nullptr))
269 arch = specified_arch;
275 llvm::SmallString<64> unglobbed_path;
279 if (unglobbed_path.empty())
282 file =
FileSpec(unglobbed_path.c_str());
285 bool user_exe_path_is_bundle =
false;
286 char resolved_bundle_exe_path[
PATH_MAX];
287 resolved_bundle_exe_path[0] =
'\0';
290 user_exe_path_is_bundle =
true;
292 if (file.
IsRelative() && !user_exe_path.empty()) {
293 llvm::SmallString<64> cwd;
294 if (! llvm::sys::fs::current_path(cwd)) {
302 ModuleSP exe_module_sp;
304 FileSpecList executable_search_paths(
307 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp,
308 executable_search_paths.GetSize()
309 ? &executable_search_paths
313 if (
error.Success() && exe_module_sp) {
314 if (exe_module_sp->GetObjectFile() ==
nullptr) {
316 error.SetErrorStringWithFormat(
317 "\"%s\" doesn't contain architecture %s", file.
GetPath().c_str(),
320 error.SetErrorStringWithFormat(
"unsupported file type \"%s\"",
325 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
326 target_sp->SetExecutableModule(exe_module_sp, load_dependent_files);
327 if (user_exe_path_is_bundle)
328 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
329 sizeof(resolved_bundle_exe_path));
330 if (target_sp->GetPreloadSymbols())
331 exe_module_sp->PreloadSymbols();
336 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
345 if (!user_exe_path.empty()) {
348 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
349 target_sp->SetArg0(resolved_bundle_exe_path);
352 target_sp->SetArg0(file.
GetPath().c_str());
358 target_sp->AppendExecutableSearchPaths(file_dir);
381 [&exe_file_spec, exe_arch_ptr](
const TargetSP &item) {
382 Module *exe_module = item->GetExecutableModulePointer();
384 !FileSpec::Match(exe_file_spec, exe_module->GetFileSpec()))
387 return !exe_arch_ptr ||
388 exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture());
400 [pid](
const TargetSP &item) {
401 auto *process_ptr = item->GetProcessSP().get();
402 return process_ptr && (process_ptr->GetID() == pid);
418 [process](
const TargetSP &item) {
419 return item->GetProcessSP().get() == process;
435 [target](
const TargetSP &item) { return item.get() == target; });
443 uint32_t num_async_interrupts_sent = 0;
448 Process *process = target_sp->GetProcessSP().get();
451 ++num_async_interrupts_sent;
460 return num_async_interrupts_sent;
470 process = target_sp->GetProcessSP().get();
471 if (process && process->
IsAlive()) {
480 process = target_sp->GetProcessSP().get();
481 if (process && process->
IsAlive()) {
487 return num_signals_sent;
514 "target already exists it the list");