Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 | |
| 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallString.h" |
| 14 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | // Project includes |
| 16 | #include "lldb/Core/Broadcaster.h" |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Event.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/State.h" |
| 22 | #include "lldb/Core/Timer.h" |
| 23 | #include "lldb/Host/Host.h" |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame] | 24 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/OptionGroupPlatform.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Process.h" |
| 30 | #include "lldb/Target/TargetList.h" |
| 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | ConstString &TargetList::GetStaticBroadcasterClass() { |
| 36 | static ConstString class_name("lldb.targetList"); |
| 37 | return class_name; |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 38 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | |
| 40 | //---------------------------------------------------------------------- |
| 41 | // TargetList constructor |
| 42 | //---------------------------------------------------------------------- |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 43 | TargetList::TargetList(Debugger &debugger) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | : Broadcaster(debugger.GetBroadcasterManager(), |
| 45 | TargetList::GetStaticBroadcasterClass().AsCString()), |
| 46 | m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) { |
| 47 | CheckInWithManager(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | //---------------------------------------------------------------------- |
| 51 | // Destructor |
| 52 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | TargetList::~TargetList() { |
| 54 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 55 | m_target_list.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 58 | Error TargetList::CreateTarget(Debugger &debugger, |
| 59 | llvm::StringRef user_exe_path, |
| 60 | llvm::StringRef triple_str, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | bool get_dependent_files, |
| 62 | const OptionGroupPlatform *platform_options, |
| 63 | TargetSP &target_sp) { |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 64 | return CreateTargetInternal(debugger, user_exe_path, triple_str, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | get_dependent_files, platform_options, target_sp, |
| 66 | false); |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 69 | Error TargetList::CreateTarget(Debugger &debugger, |
| 70 | llvm::StringRef user_exe_path, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | const ArchSpec &specified_arch, |
| 72 | bool get_dependent_files, |
| 73 | PlatformSP &platform_sp, TargetSP &target_sp) { |
| 74 | return CreateTargetInternal(debugger, user_exe_path, specified_arch, |
| 75 | get_dependent_files, platform_sp, target_sp, |
| 76 | false); |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | Error TargetList::CreateTargetInternal( |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 80 | Debugger &debugger, llvm::StringRef user_exe_path, |
| 81 | llvm::StringRef triple_str, bool get_dependent_files, |
| 82 | const OptionGroupPlatform *platform_options, TargetSP &target_sp, |
| 83 | bool is_dummy_target) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | Error error; |
| 85 | PlatformSP platform_sp; |
| 86 | |
| 87 | // This is purposely left empty unless it is specified by triple_cstr. |
| 88 | // If not initialized via triple_cstr, then the currently selected platform |
| 89 | // will set the architecture correctly. |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 90 | const ArchSpec arch(triple_str); |
| 91 | if (!triple_str.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | if (!arch.IsValid()) { |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 93 | error.SetErrorStringWithFormat("invalid triple '%s'", |
| 94 | triple_str.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | return error; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 96 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | ArchSpec platform_arch(arch); |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | bool prefer_platform_arch = false; |
| 102 | |
| 103 | CommandInterpreter &interpreter = debugger.GetCommandInterpreter(); |
| 104 | |
| 105 | // let's see if there is already an existing plaform before we go creating |
| 106 | // another... |
| 107 | platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); |
| 108 | |
| 109 | if (platform_options && platform_options->PlatformWasSpecified()) { |
| 110 | // Create a new platform if it doesn't match the selected platform |
| 111 | if (!platform_options->PlatformMatches(platform_sp)) { |
| 112 | const bool select_platform = true; |
| 113 | platform_sp = platform_options->CreatePlatformWithOptions( |
| 114 | interpreter, arch, select_platform, error, platform_arch); |
| 115 | if (!platform_sp) |
| 116 | return error; |
| 117 | } |
| 118 | } |
| 119 | |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 120 | if (!user_exe_path.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | ModuleSpecList module_specs; |
| 122 | ModuleSpec module_spec; |
| 123 | module_spec.GetFileSpec().SetFile(user_exe_path, true); |
| 124 | |
| 125 | // Resolve the executable in case we are given a path to a application |
| 126 | // bundle |
| 127 | // like a .app bundle on MacOSX |
| 128 | Host::ResolveExecutableInBundle(module_spec.GetFileSpec()); |
| 129 | |
| 130 | lldb::offset_t file_offset = 0; |
| 131 | lldb::offset_t file_size = 0; |
| 132 | const size_t num_specs = ObjectFile::GetModuleSpecifications( |
| 133 | module_spec.GetFileSpec(), file_offset, file_size, module_specs); |
| 134 | if (num_specs > 0) { |
| 135 | ModuleSpec matching_module_spec; |
| 136 | |
| 137 | if (num_specs == 1) { |
| 138 | if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) { |
| 139 | if (platform_arch.IsValid()) { |
| 140 | if (platform_arch.IsCompatibleMatch( |
| 141 | matching_module_spec.GetArchitecture())) { |
| 142 | // If the OS or vendor weren't specified, then adopt the module's |
| 143 | // architecture so that the platform matching can be more accurate |
| 144 | if (!platform_arch.TripleOSWasSpecified() || |
| 145 | !platform_arch.TripleVendorWasSpecified()) { |
| 146 | prefer_platform_arch = true; |
| 147 | platform_arch = matching_module_spec.GetArchitecture(); |
| 148 | } |
| 149 | } else { |
| 150 | StreamString platform_arch_strm; |
| 151 | StreamString module_arch_strm; |
| 152 | |
| 153 | platform_arch.DumpTriple(platform_arch_strm); |
| 154 | matching_module_spec.GetArchitecture().DumpTriple( |
| 155 | module_arch_strm); |
| 156 | error.SetErrorStringWithFormat( |
| 157 | "the specified architecture '%s' is not compatible with '%s' " |
| 158 | "in '%s'", |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 159 | platform_arch_strm.GetData(), module_arch_strm.GetData(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | module_spec.GetFileSpec().GetPath().c_str()); |
| 161 | return error; |
| 162 | } |
| 163 | } else { |
| 164 | // Only one arch and none was specified |
| 165 | prefer_platform_arch = true; |
| 166 | platform_arch = matching_module_spec.GetArchitecture(); |
| 167 | } |
| 168 | } |
| 169 | } else { |
| 170 | if (arch.IsValid()) { |
| 171 | module_spec.GetArchitecture() = arch; |
| 172 | if (module_specs.FindMatchingModuleSpec(module_spec, |
| 173 | matching_module_spec)) { |
| 174 | prefer_platform_arch = true; |
| 175 | platform_arch = matching_module_spec.GetArchitecture(); |
| 176 | } |
| 177 | } else { |
| 178 | // No architecture specified, check if there is only one platform for |
| 179 | // all of the architectures. |
| 180 | |
| 181 | typedef std::vector<PlatformSP> PlatformList; |
| 182 | PlatformList platforms; |
| 183 | PlatformSP host_platform_sp = Platform::GetHostPlatform(); |
| 184 | for (size_t i = 0; i < num_specs; ++i) { |
| 185 | ModuleSpec module_spec; |
| 186 | if (module_specs.GetModuleSpecAtIndex(i, module_spec)) { |
| 187 | // See if there was a selected platform and check that first |
| 188 | // since the user may have specified it. |
| 189 | if (platform_sp) { |
| 190 | if (platform_sp->IsCompatibleArchitecture( |
| 191 | module_spec.GetArchitecture(), false, nullptr)) { |
| 192 | platforms.push_back(platform_sp); |
| 193 | continue; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Next check the host platform it if wasn't already checked above |
| 198 | if (host_platform_sp && |
| 199 | (!platform_sp || |
| 200 | host_platform_sp->GetName() != platform_sp->GetName())) { |
| 201 | if (host_platform_sp->IsCompatibleArchitecture( |
| 202 | module_spec.GetArchitecture(), false, nullptr)) { |
| 203 | platforms.push_back(host_platform_sp); |
| 204 | continue; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // Just find a platform that matches the architecture in the |
| 209 | // executable file |
| 210 | PlatformSP fallback_platform_sp( |
| 211 | Platform::GetPlatformForArchitecture( |
| 212 | module_spec.GetArchitecture(), nullptr)); |
| 213 | if (fallback_platform_sp) { |
| 214 | platforms.push_back(fallback_platform_sp); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | Platform *platform_ptr = nullptr; |
| 220 | bool more_than_one_platforms = false; |
| 221 | for (const auto &the_platform_sp : platforms) { |
| 222 | if (platform_ptr) { |
| 223 | if (platform_ptr->GetName() != the_platform_sp->GetName()) { |
| 224 | more_than_one_platforms = true; |
| 225 | platform_ptr = nullptr; |
| 226 | break; |
| 227 | } |
| 228 | } else { |
| 229 | platform_ptr = the_platform_sp.get(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (platform_ptr) { |
| 234 | // All platforms for all modules in the exectuable match, so we can |
| 235 | // select this platform |
| 236 | platform_sp = platforms.front(); |
| 237 | } else if (more_than_one_platforms == false) { |
| 238 | // No platforms claim to support this file |
| 239 | error.SetErrorString("No matching platforms found for this file, " |
| 240 | "specify one with the --platform option"); |
| 241 | return error; |
| 242 | } else { |
| 243 | // More than one platform claims to support this file, so the |
| 244 | // --platform option must be specified |
| 245 | StreamString error_strm; |
| 246 | std::set<Platform *> platform_set; |
| 247 | error_strm.Printf( |
| 248 | "more than one platform supports this executable ("); |
| 249 | for (const auto &the_platform_sp : platforms) { |
| 250 | if (platform_set.find(the_platform_sp.get()) == |
| 251 | platform_set.end()) { |
| 252 | if (!platform_set.empty()) |
| 253 | error_strm.PutCString(", "); |
| 254 | error_strm.PutCString(the_platform_sp->GetName().GetCString()); |
| 255 | platform_set.insert(the_platform_sp.get()); |
| 256 | } |
| 257 | } |
| 258 | error_strm.Printf( |
| 259 | "), use the --platform option to specify a platform"); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 260 | error.SetErrorString(error_strm.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | return error; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // If we have a valid architecture, make sure the current platform is |
| 269 | // compatible with that architecture |
| 270 | if (!prefer_platform_arch && arch.IsValid()) { |
| 271 | if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) { |
| 272 | platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch); |
| 273 | if (!is_dummy_target && platform_sp) |
| 274 | debugger.GetPlatformList().SetSelectedPlatform(platform_sp); |
| 275 | } |
| 276 | } else if (platform_arch.IsValid()) { |
| 277 | // if "arch" isn't valid, yet "platform_arch" is, it means we have an |
| 278 | // executable file with |
| 279 | // a single architecture which should be used |
| 280 | ArchSpec fixed_platform_arch; |
| 281 | if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, |
| 282 | &fixed_platform_arch)) { |
| 283 | platform_sp = Platform::GetPlatformForArchitecture(platform_arch, |
| 284 | &fixed_platform_arch); |
| 285 | if (!is_dummy_target && platform_sp) |
| 286 | debugger.GetPlatformList().SetSelectedPlatform(platform_sp); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (!platform_arch.IsValid()) |
| 291 | platform_arch = arch; |
| 292 | |
| 293 | error = TargetList::CreateTargetInternal( |
| 294 | debugger, user_exe_path, platform_arch, get_dependent_files, platform_sp, |
| 295 | target_sp, is_dummy_target); |
| 296 | return error; |
| 297 | } |
| 298 | |
| 299 | lldb::TargetSP TargetList::GetDummyTarget(lldb_private::Debugger &debugger) { |
| 300 | // FIXME: Maybe the dummy target should be per-Debugger |
| 301 | if (!m_dummy_target_sp || !m_dummy_target_sp->IsValid()) { |
| 302 | ArchSpec arch(Target::GetDefaultArchitecture()); |
| 303 | if (!arch.IsValid()) |
| 304 | arch = HostInfo::GetArchitecture(); |
| 305 | Error err = CreateDummyTarget( |
| 306 | debugger, arch.GetTriple().getTriple().c_str(), m_dummy_target_sp); |
| 307 | } |
| 308 | |
| 309 | return m_dummy_target_sp; |
| 310 | } |
| 311 | |
| 312 | Error TargetList::CreateDummyTarget(Debugger &debugger, |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 313 | llvm::StringRef specified_arch_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | lldb::TargetSP &target_sp) { |
| 315 | PlatformSP host_platform_sp(Platform::GetHostPlatform()); |
| 316 | return CreateTargetInternal( |
| 317 | debugger, (const char *)nullptr, specified_arch_name, false, |
| 318 | (const OptionGroupPlatform *)nullptr, target_sp, true); |
| 319 | } |
| 320 | |
| 321 | Error TargetList::CreateTargetInternal(Debugger &debugger, |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 322 | llvm::StringRef user_exe_path, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 323 | const ArchSpec &specified_arch, |
| 324 | bool get_dependent_files, |
| 325 | lldb::PlatformSP &platform_sp, |
| 326 | lldb::TargetSP &target_sp, |
| 327 | bool is_dummy_target) { |
| 328 | Timer scoped_timer(LLVM_PRETTY_FUNCTION, |
| 329 | "TargetList::CreateTarget (file = '%s', arch = '%s')", |
Zachary Turner | 72f4997c | 2016-11-19 00:50:29 +0000 | [diff] [blame] | 330 | user_exe_path.str().c_str(), |
| 331 | specified_arch.GetArchitectureName()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 332 | Error error; |
| 333 | |
| 334 | ArchSpec arch(specified_arch); |
| 335 | |
| 336 | if (arch.IsValid()) { |
| 337 | if (!platform_sp || |
| 338 | !platform_sp->IsCompatibleArchitecture(arch, false, nullptr)) |
| 339 | platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch); |
| 340 | } |
| 341 | |
| 342 | if (!platform_sp) |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 343 | platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); |
| 344 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | if (!arch.IsValid()) |
| 346 | arch = specified_arch; |
Greg Clayton | c76fa8a | 2014-07-29 21:27:21 +0000 | [diff] [blame] | 347 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | FileSpec file(user_exe_path, false); |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 349 | if (!file.Exists() && user_exe_path.startswith("~")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | // we want to expand the tilde but we don't want to resolve any symbolic |
| 351 | // links |
| 352 | // so we can't use the FileSpec constructor's resolve flag |
| 353 | llvm::SmallString<64> unglobbed_path(user_exe_path); |
| 354 | FileSpec::ResolveUsername(unglobbed_path); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 355 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | if (unglobbed_path.empty()) |
| 357 | file = FileSpec(user_exe_path, false); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 358 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 359 | file = FileSpec(unglobbed_path.c_str(), false); |
| 360 | } |
| 361 | |
| 362 | bool user_exe_path_is_bundle = false; |
| 363 | char resolved_bundle_exe_path[PATH_MAX]; |
| 364 | resolved_bundle_exe_path[0] = '\0'; |
| 365 | if (file) { |
| 366 | if (file.GetFileType() == FileSpec::eFileTypeDirectory) |
| 367 | user_exe_path_is_bundle = true; |
| 368 | |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 369 | if (file.IsRelative() && !user_exe_path.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 370 | // Ignore paths that start with "./" and "../" |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 371 | if (!user_exe_path.startswith("./") && !user_exe_path.startswith("../")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 372 | char cwd[PATH_MAX]; |
| 373 | if (getcwd(cwd, sizeof(cwd))) { |
| 374 | std::string cwd_user_exe_path(cwd); |
| 375 | cwd_user_exe_path += '/'; |
| 376 | cwd_user_exe_path += user_exe_path; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 377 | FileSpec cwd_file(cwd_user_exe_path, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 378 | if (cwd_file.Exists()) |
| 379 | file = cwd_file; |
| 380 | } |
| 381 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 384 | ModuleSP exe_module_sp; |
| 385 | if (platform_sp) { |
| 386 | FileSpecList executable_search_paths( |
| 387 | Target::GetDefaultExecutableSearchPaths()); |
| 388 | ModuleSpec module_spec(file, arch); |
| 389 | error = platform_sp->ResolveExecutable(module_spec, exe_module_sp, |
| 390 | executable_search_paths.GetSize() |
| 391 | ? &executable_search_paths |
| 392 | : nullptr); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 393 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | if (error.Success() && exe_module_sp) { |
| 396 | if (exe_module_sp->GetObjectFile() == nullptr) { |
| 397 | if (arch.IsValid()) { |
| 398 | error.SetErrorStringWithFormat( |
| 399 | "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(), |
| 400 | arch.GetArchitectureName()); |
| 401 | } else { |
| 402 | error.SetErrorStringWithFormat("unsupported file type \"%s\"", |
| 403 | file.GetPath().c_str()); |
| 404 | } |
| 405 | return error; |
| 406 | } |
| 407 | target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); |
| 408 | target_sp->SetExecutableModule(exe_module_sp, get_dependent_files); |
| 409 | if (user_exe_path_is_bundle) |
| 410 | exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, |
| 411 | sizeof(resolved_bundle_exe_path)); |
| 412 | } |
| 413 | } else { |
| 414 | // No file was specified, just create an empty target with any arch |
| 415 | // if a valid arch was specified |
| 416 | target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); |
| 417 | } |
| 418 | |
| 419 | if (target_sp) { |
| 420 | // Set argv0 with what the user typed, unless the user specified a |
| 421 | // directory. If the user specified a directory, then it is probably a |
| 422 | // bundle that was resolved and we need to use the resolved bundle path |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 423 | if (!user_exe_path.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 424 | // Use exactly what the user typed as the first argument when we exec or |
| 425 | // posix_spawn |
| 426 | if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) { |
| 427 | target_sp->SetArg0(resolved_bundle_exe_path); |
| 428 | } else { |
| 429 | // Use resolved path |
| 430 | target_sp->SetArg0(file.GetPath().c_str()); |
| 431 | } |
| 432 | } |
| 433 | if (file.GetDirectory()) { |
| 434 | FileSpec file_dir; |
| 435 | file_dir.GetDirectory() = file.GetDirectory(); |
| 436 | target_sp->GetExecutableSearchPaths().Append(file_dir); |
| 437 | } |
| 438 | |
| 439 | // Don't put the dummy target in the target list, it's held separately. |
| 440 | if (!is_dummy_target) { |
| 441 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 442 | m_selected_target_idx = m_target_list.size(); |
| 443 | m_target_list.push_back(target_sp); |
| 444 | // Now prime this from the dummy target: |
| 445 | target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget()); |
| 446 | } else { |
| 447 | m_dummy_target_sp = target_sp; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 454 | bool TargetList::DeleteTarget(TargetSP &target_sp) { |
| 455 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 456 | collection::iterator pos, end = m_target_list.end(); |
| 457 | |
| 458 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 459 | if (pos->get() == target_sp.get()) { |
| 460 | m_target_list.erase(pos); |
| 461 | return true; |
| 462 | } |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | TargetSP TargetList::FindTargetWithExecutableAndArchitecture( |
| 468 | const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const { |
| 469 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 470 | TargetSP target_sp; |
| 471 | bool full_match = (bool)exe_file_spec.GetDirectory(); |
| 472 | |
| 473 | collection::const_iterator pos, end = m_target_list.end(); |
| 474 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 475 | Module *exe_module = (*pos)->GetExecutableModulePointer(); |
| 476 | |
| 477 | if (exe_module) { |
| 478 | if (FileSpec::Equal(exe_file_spec, exe_module->GetFileSpec(), |
| 479 | full_match)) { |
| 480 | if (exe_arch_ptr) { |
| 481 | if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture())) |
| 482 | continue; |
| 483 | } |
| 484 | target_sp = *pos; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | return target_sp; |
| 490 | } |
| 491 | |
| 492 | TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const { |
| 493 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 494 | TargetSP target_sp; |
| 495 | collection::const_iterator pos, end = m_target_list.end(); |
| 496 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 497 | Process *process = (*pos)->GetProcessSP().get(); |
| 498 | if (process && process->GetID() == pid) { |
| 499 | target_sp = *pos; |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | return target_sp; |
| 504 | } |
| 505 | |
| 506 | TargetSP TargetList::FindTargetWithProcess(Process *process) const { |
| 507 | TargetSP target_sp; |
| 508 | if (process) { |
| 509 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 510 | collection::const_iterator pos, end = m_target_list.end(); |
| 511 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 512 | if (process == (*pos)->GetProcessSP().get()) { |
| 513 | target_sp = *pos; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return target_sp; |
| 519 | } |
| 520 | |
| 521 | TargetSP TargetList::GetTargetSP(Target *target) const { |
| 522 | TargetSP target_sp; |
| 523 | if (target) { |
| 524 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 525 | collection::const_iterator pos, end = m_target_list.end(); |
| 526 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 527 | if (target == (*pos).get()) { |
| 528 | target_sp = *pos; |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | return target_sp; |
| 534 | } |
| 535 | |
| 536 | uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) { |
| 537 | uint32_t num_async_interrupts_sent = 0; |
| 538 | |
| 539 | if (pid != LLDB_INVALID_PROCESS_ID) { |
| 540 | TargetSP target_sp(FindTargetWithProcessID(pid)); |
| 541 | if (target_sp) { |
| 542 | Process *process = target_sp->GetProcessSP().get(); |
| 543 | if (process) { |
| 544 | process->SendAsyncInterrupt(); |
| 545 | ++num_async_interrupts_sent; |
| 546 | } |
| 547 | } |
| 548 | } else { |
| 549 | // We don't have a valid pid to broadcast to, so broadcast to the target |
| 550 | // list's async broadcaster... |
| 551 | BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); |
| 552 | } |
| 553 | |
| 554 | return num_async_interrupts_sent; |
| 555 | } |
| 556 | |
| 557 | uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { |
| 558 | uint32_t num_signals_sent = 0; |
| 559 | Process *process = nullptr; |
| 560 | if (pid == LLDB_INVALID_PROCESS_ID) { |
| 561 | // Signal all processes with signal |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 562 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 563 | collection::iterator pos, end = m_target_list.end(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 564 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 565 | process = (*pos)->GetProcessSP().get(); |
| 566 | if (process) { |
| 567 | if (process->IsAlive()) { |
| 568 | ++num_signals_sent; |
| 569 | process->Signal(signo); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 570 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 571 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 572 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 573 | } else { |
| 574 | // Signal a specific process with signal |
| 575 | TargetSP target_sp(FindTargetWithProcessID(pid)); |
| 576 | if (target_sp) { |
| 577 | process = target_sp->GetProcessSP().get(); |
| 578 | if (process) { |
| 579 | if (process->IsAlive()) { |
| 580 | ++num_signals_sent; |
| 581 | process->Signal(signo); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 583 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 585 | } |
| 586 | return num_signals_sent; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | int TargetList::GetNumTargets() const { |
| 590 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 591 | return m_target_list.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 594 | lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const { |
| 595 | TargetSP target_sp; |
| 596 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 597 | if (idx < m_target_list.size()) |
| 598 | target_sp = m_target_list[idx]; |
| 599 | return target_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const { |
| 603 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 604 | size_t num_targets = m_target_list.size(); |
| 605 | for (size_t idx = 0; idx < num_targets; idx++) { |
| 606 | if (target_sp == m_target_list[idx]) |
| 607 | return idx; |
| 608 | } |
| 609 | return UINT32_MAX; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 612 | uint32_t TargetList::SetSelectedTarget(Target *target) { |
| 613 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 614 | collection::const_iterator pos, begin = m_target_list.begin(), |
| 615 | end = m_target_list.end(); |
| 616 | for (pos = begin; pos != end; ++pos) { |
| 617 | if (pos->get() == target) { |
| 618 | m_selected_target_idx = std::distance(begin, pos); |
| 619 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 620 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 621 | } |
| 622 | m_selected_target_idx = 0; |
| 623 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 626 | lldb::TargetSP TargetList::GetSelectedTarget() { |
| 627 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 628 | if (m_selected_target_idx >= m_target_list.size()) |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 629 | m_selected_target_idx = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 630 | return GetTargetAtIndex(m_selected_target_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 631 | } |