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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 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" |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 24 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/OptionGroupPlatform.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 27 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Process.h" |
| 29 | #include "lldb/Target/TargetList.h" |
| 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 34 | ConstString & |
| 35 | TargetList::GetStaticBroadcasterClass () |
| 36 | { |
| 37 | static ConstString class_name ("lldb.targetList"); |
| 38 | return class_name; |
| 39 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
| 41 | //---------------------------------------------------------------------- |
| 42 | // TargetList constructor |
| 43 | //---------------------------------------------------------------------- |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 44 | TargetList::TargetList(Debugger &debugger) : |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 45 | Broadcaster(&debugger, TargetList::GetStaticBroadcasterClass().AsCString()), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | m_target_list(), |
| 47 | m_target_list_mutex (Mutex::eMutexTypeRecursive), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 48 | m_selected_target_idx (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | { |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 50 | CheckInWithManager(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | //---------------------------------------------------------------------- |
| 54 | // Destructor |
| 55 | //---------------------------------------------------------------------- |
| 56 | TargetList::~TargetList() |
| 57 | { |
| 58 | Mutex::Locker locker(m_target_list_mutex); |
| 59 | m_target_list.clear(); |
| 60 | } |
| 61 | |
| 62 | Error |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 63 | TargetList::CreateTarget (Debugger &debugger, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 64 | const char *user_exe_path, |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 65 | const char *triple_cstr, |
| 66 | bool get_dependent_files, |
| 67 | const OptionGroupPlatform *platform_options, |
| 68 | TargetSP &target_sp) |
| 69 | { |
| 70 | Error error; |
| 71 | PlatformSP platform_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 72 | |
Johnny Chen | cdc21d4 | 2012-01-05 01:26:01 +0000 | [diff] [blame] | 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. |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 76 | const ArchSpec arch(triple_cstr); |
| 77 | if (triple_cstr && triple_cstr[0]) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 78 | { |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 79 | if (!arch.IsValid()) |
| 80 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 81 | error.SetErrorStringWithFormat("invalid triple '%s'", triple_cstr); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 82 | return error; |
| 83 | } |
| 84 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 85 | |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 86 | ArchSpec platform_arch(arch); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 87 | |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 88 | bool prefer_platform_arch = false; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 89 | |
| 90 | if (user_exe_path && user_exe_path[0]) |
| 91 | { |
| 92 | ModuleSpecList module_specs; |
| 93 | ModuleSpec module_spec; |
| 94 | module_spec.GetFileSpec().SetFile(user_exe_path, true); |
Greg Clayton | c76fa8a | 2014-07-29 21:27:21 +0000 | [diff] [blame] | 95 | |
| 96 | // Resolve the executable in case we are given a path to a application bundle |
| 97 | // like a .app bundle on MacOSX |
| 98 | Host::ResolveExecutableInBundle (module_spec.GetFileSpec()); |
| 99 | |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 100 | lldb::offset_t file_offset = 0; |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 101 | lldb::offset_t file_size = 0; |
| 102 | const size_t num_specs = ObjectFile::GetModuleSpecifications (module_spec.GetFileSpec(), file_offset, file_size, module_specs); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 103 | if (num_specs > 0) |
| 104 | { |
| 105 | ModuleSpec matching_module_spec; |
| 106 | |
| 107 | if (num_specs == 1) |
| 108 | { |
| 109 | if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) |
| 110 | { |
| 111 | if (platform_arch.IsValid()) |
| 112 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 113 | if (platform_arch.IsCompatibleMatch(matching_module_spec.GetArchitecture())) |
| 114 | { |
| 115 | // If the OS or vendor weren't specified, then adopt the module's |
| 116 | // architecture so that the platform matching can be more accurate |
| 117 | if (!platform_arch.TripleOSWasSpecified() || !platform_arch.TripleVendorWasSpecified()) |
| 118 | { |
| 119 | prefer_platform_arch = true; |
| 120 | platform_arch = matching_module_spec.GetArchitecture(); |
| 121 | } |
| 122 | } |
| 123 | else |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 124 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 125 | error.SetErrorStringWithFormat("the specified architecture '%s' is not compatible with '%s' in '%s'", |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 126 | platform_arch.GetTriple().str().c_str(), |
| 127 | matching_module_spec.GetArchitecture().GetTriple().str().c_str(), |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 128 | module_spec.GetFileSpec().GetPath().c_str()); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 129 | return error; |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | // Only one arch and none was specified |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 135 | prefer_platform_arch = true; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 136 | platform_arch = matching_module_spec.GetArchitecture(); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | if (arch.IsValid()) |
| 143 | { |
| 144 | module_spec.GetArchitecture() = arch; |
| 145 | if (module_specs.FindMatchingModuleSpec(module_spec, matching_module_spec)) |
| 146 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 147 | prefer_platform_arch = true; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 148 | platform_arch = matching_module_spec.GetArchitecture(); |
| 149 | } |
| 150 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 155 | CommandInterpreter &interpreter = debugger.GetCommandInterpreter(); |
| 156 | if (platform_options) |
| 157 | { |
| 158 | if (platform_options->PlatformWasSpecified ()) |
| 159 | { |
| 160 | const bool select_platform = true; |
| 161 | platform_sp = platform_options->CreatePlatformWithOptions (interpreter, |
| 162 | arch, |
| 163 | select_platform, |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 164 | error, |
| 165 | platform_arch); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 166 | if (!platform_sp) |
| 167 | return error; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (!platform_sp) |
| 172 | { |
| 173 | // Get the current platform and make sure it is compatible with the |
| 174 | // current architecture if we have a valid architecture. |
| 175 | platform_sp = debugger.GetPlatformList().GetSelectedPlatform (); |
| 176 | |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 177 | if (!prefer_platform_arch && arch.IsValid()) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 178 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 179 | if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) |
| 180 | platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch); |
| 181 | } |
| 182 | else if (platform_arch.IsValid()) |
| 183 | { |
| 184 | // if "arch" isn't valid, yet "platform_arch" is, it means we have an executable file with |
| 185 | // a single architecture which should be used |
| 186 | ArchSpec fixed_platform_arch; |
| 187 | if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, &fixed_platform_arch)) |
| 188 | platform_sp = Platform::GetPlatformForArchitecture(platform_arch, &fixed_platform_arch); |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 191 | |
| 192 | if (!platform_arch.IsValid()) |
| 193 | platform_arch = arch; |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 194 | |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 195 | error = TargetList::CreateTarget (debugger, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 196 | user_exe_path, |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 197 | platform_arch, |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 198 | get_dependent_files, |
| 199 | platform_sp, |
| 200 | target_sp); |
| 201 | return error; |
| 202 | } |
| 203 | |
| 204 | Error |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 205 | TargetList::CreateTarget (Debugger &debugger, |
| 206 | const char *user_exe_path, |
| 207 | const ArchSpec& specified_arch, |
| 208 | bool get_dependent_files, |
| 209 | PlatformSP &platform_sp, |
| 210 | TargetSP &target_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | { |
| 212 | Timer scoped_timer (__PRETTY_FUNCTION__, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 213 | "TargetList::CreateTarget (file = '%s', arch = '%s')", |
| 214 | user_exe_path, |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 215 | specified_arch.GetArchitectureName()); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 216 | Error error; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 217 | |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 218 | ArchSpec arch(specified_arch); |
| 219 | |
| 220 | if (platform_sp) |
| 221 | { |
| 222 | if (arch.IsValid()) |
| 223 | { |
Greg Clayton | 1e0c884 | 2013-01-11 20:49:54 +0000 | [diff] [blame] | 224 | if (!platform_sp->IsCompatibleArchitecture(arch, false, NULL)) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 225 | platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch); |
| 226 | } |
| 227 | } |
| 228 | else if (arch.IsValid()) |
| 229 | { |
| 230 | platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch); |
| 231 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 232 | |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 233 | if (!platform_sp) |
| 234 | platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); |
| 235 | |
Greg Clayton | 8ae50eb | 2012-06-05 21:17:09 +0000 | [diff] [blame] | 236 | if (!arch.IsValid()) |
| 237 | arch = specified_arch; |
Greg Clayton | 8ae50eb | 2012-06-05 21:17:09 +0000 | [diff] [blame] | 238 | |
Jason Molenda | d26206b5 | 2013-04-19 22:38:50 +0000 | [diff] [blame] | 239 | FileSpec file (user_exe_path, false); |
| 240 | if (!file.Exists() && user_exe_path && user_exe_path[0] == '~') |
| 241 | { |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 242 | // we want to expand the tilde but we don't want to resolve any symbolic links |
| 243 | // so we can't use the FileSpec constructor's resolve flag |
| 244 | char unglobbed_path[PATH_MAX]; |
| 245 | unglobbed_path[0] = '\0'; |
| 246 | |
| 247 | size_t return_count = FileSpec::ResolveUsername(user_exe_path, unglobbed_path, sizeof(unglobbed_path)); |
| 248 | |
| 249 | if (return_count == 0 || return_count >= sizeof(unglobbed_path)) |
| 250 | ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", user_exe_path); |
| 251 | |
| 252 | file = FileSpec(unglobbed_path, false); |
Jason Molenda | d26206b5 | 2013-04-19 22:38:50 +0000 | [diff] [blame] | 253 | } |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 254 | |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 255 | bool user_exe_path_is_bundle = false; |
| 256 | char resolved_bundle_exe_path[PATH_MAX]; |
| 257 | resolved_bundle_exe_path[0] = '\0'; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 258 | if (file) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 259 | { |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 260 | if (file.GetFileType() == FileSpec::eFileTypeDirectory) |
| 261 | user_exe_path_is_bundle = true; |
| 262 | |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 263 | if (file.IsRelativeToCurrentWorkingDirectory()) |
| 264 | { |
| 265 | // Ignore paths that start with "./" and "../" |
| 266 | if (!((user_exe_path[0] == '.' && user_exe_path[1] == '/') || |
| 267 | (user_exe_path[0] == '.' && user_exe_path[1] == '.' && user_exe_path[2] == '/'))) |
| 268 | { |
| 269 | char cwd[PATH_MAX]; |
| 270 | if (getcwd (cwd, sizeof(cwd))) |
| 271 | { |
| 272 | std::string cwd_user_exe_path (cwd); |
| 273 | cwd_user_exe_path += '/'; |
| 274 | cwd_user_exe_path += user_exe_path; |
Greg Clayton | 9ff5aae | 2013-04-04 00:15:09 +0000 | [diff] [blame] | 275 | FileSpec cwd_file (cwd_user_exe_path.c_str(), false); |
| 276 | if (cwd_file.Exists()) |
| 277 | file = cwd_file; |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 282 | ModuleSP exe_module_sp; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 283 | if (platform_sp) |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 284 | { |
| 285 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 286 | error = platform_sp->ResolveExecutable (file, |
| 287 | arch, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 288 | exe_module_sp, |
| 289 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 290 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 291 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 292 | if (error.Success() && exe_module_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | { |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 294 | if (exe_module_sp->GetObjectFile() == NULL) |
| 295 | { |
Greg Clayton | bc5cad6 | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 296 | if (arch.IsValid()) |
| 297 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 298 | error.SetErrorStringWithFormat("\"%s\" doesn't contain architecture %s", |
| 299 | file.GetPath().c_str(), |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 300 | arch.GetArchitectureName()); |
Greg Clayton | bc5cad6 | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 301 | } |
| 302 | else |
| 303 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 304 | error.SetErrorStringWithFormat("unsupported file type \"%s\"", |
| 305 | file.GetPath().c_str()); |
Greg Clayton | bc5cad6 | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 306 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 307 | return error; |
| 308 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 309 | target_sp.reset(new Target(debugger, arch, platform_sp)); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 310 | target_sp->SetExecutableModule (exe_module_sp, get_dependent_files); |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 311 | if (user_exe_path_is_bundle) |
| 312 | exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, sizeof(resolved_bundle_exe_path)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 314 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 315 | else |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 316 | { |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 317 | // No file was specified, just create an empty target with any arch |
| 318 | // if a valid arch was specified |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 319 | target_sp.reset(new Target(debugger, arch, platform_sp)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | if (target_sp) |
| 323 | { |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 324 | // Set argv0 with what the user typed, unless the user specified a |
| 325 | // directory. If the user specified a directory, then it is probably a |
| 326 | // bundle that was resolved and we need to use the resolved bundle path |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 327 | if (user_exe_path) |
| 328 | { |
| 329 | // Use exactly what the user typed as the first argument when we exec or posix_spawn |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 330 | if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) |
| 331 | { |
| 332 | target_sp->SetArg0 (resolved_bundle_exe_path); |
| 333 | } |
| 334 | else |
| 335 | { |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 336 | // Use resolved path |
| 337 | target_sp->SetArg0 (file.GetPath().c_str()); |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 338 | } |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 339 | } |
| 340 | if (file.GetDirectory()) |
| 341 | { |
| 342 | FileSpec file_dir; |
| 343 | file_dir.GetDirectory() = file.GetDirectory(); |
| 344 | target_sp->GetExecutableSearchPaths ().Append (file_dir); |
| 345 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 346 | Mutex::Locker locker(m_target_list_mutex); |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 347 | m_selected_target_idx = m_target_list.size(); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 348 | m_target_list.push_back(target_sp); |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 349 | |
| 350 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | return error; |
| 354 | } |
| 355 | |
| 356 | bool |
| 357 | TargetList::DeleteTarget (TargetSP &target_sp) |
| 358 | { |
| 359 | Mutex::Locker locker(m_target_list_mutex); |
| 360 | collection::iterator pos, end = m_target_list.end(); |
| 361 | |
| 362 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 363 | { |
| 364 | if (pos->get() == target_sp.get()) |
| 365 | { |
| 366 | m_target_list.erase(pos); |
| 367 | return true; |
| 368 | } |
| 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | TargetSP |
| 375 | TargetList::FindTargetWithExecutableAndArchitecture |
| 376 | ( |
| 377 | const FileSpec &exe_file_spec, |
| 378 | const ArchSpec *exe_arch_ptr |
| 379 | ) const |
| 380 | { |
| 381 | Mutex::Locker locker (m_target_list_mutex); |
| 382 | TargetSP target_sp; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 383 | bool full_match = (bool)exe_file_spec.GetDirectory(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 384 | |
| 385 | collection::const_iterator pos, end = m_target_list.end(); |
| 386 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 387 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 388 | Module *exe_module = (*pos)->GetExecutableModulePointer(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 390 | if (exe_module) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 391 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 392 | if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 393 | { |
| 394 | if (exe_arch_ptr) |
| 395 | { |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 396 | if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 397 | continue; |
| 398 | } |
| 399 | target_sp = *pos; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | return target_sp; |
| 405 | } |
| 406 | |
| 407 | TargetSP |
| 408 | TargetList::FindTargetWithProcessID (lldb::pid_t pid) const |
| 409 | { |
| 410 | Mutex::Locker locker(m_target_list_mutex); |
| 411 | TargetSP target_sp; |
| 412 | collection::const_iterator pos, end = m_target_list.end(); |
| 413 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 414 | { |
| 415 | Process* process = (*pos)->GetProcessSP().get(); |
| 416 | if (process && process->GetID() == pid) |
| 417 | { |
| 418 | target_sp = *pos; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | return target_sp; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | TargetSP |
| 427 | TargetList::FindTargetWithProcess (Process *process) const |
| 428 | { |
| 429 | TargetSP target_sp; |
| 430 | if (process) |
| 431 | { |
| 432 | Mutex::Locker locker(m_target_list_mutex); |
| 433 | collection::const_iterator pos, end = m_target_list.end(); |
| 434 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 435 | { |
| 436 | if (process == (*pos)->GetProcessSP().get()) |
| 437 | { |
| 438 | target_sp = *pos; |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | return target_sp; |
| 444 | } |
| 445 | |
| 446 | TargetSP |
| 447 | TargetList::GetTargetSP (Target *target) const |
| 448 | { |
| 449 | TargetSP target_sp; |
| 450 | if (target) |
| 451 | { |
| 452 | Mutex::Locker locker(m_target_list_mutex); |
| 453 | collection::const_iterator pos, end = m_target_list.end(); |
| 454 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 455 | { |
| 456 | if (target == (*pos).get()) |
| 457 | { |
| 458 | target_sp = *pos; |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | return target_sp; |
| 464 | } |
| 465 | |
| 466 | uint32_t |
| 467 | TargetList::SendAsyncInterrupt (lldb::pid_t pid) |
| 468 | { |
| 469 | uint32_t num_async_interrupts_sent = 0; |
| 470 | |
| 471 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 472 | { |
| 473 | TargetSP target_sp(FindTargetWithProcessID (pid)); |
| 474 | if (target_sp.get()) |
| 475 | { |
| 476 | Process* process = target_sp->GetProcessSP().get(); |
| 477 | if (process) |
| 478 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 479 | process->SendAsyncInterrupt(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 480 | ++num_async_interrupts_sent; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | // We don't have a valid pid to broadcast to, so broadcast to the target |
| 487 | // list's async broadcaster... |
| 488 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 489 | } |
| 490 | |
| 491 | return num_async_interrupts_sent; |
| 492 | } |
| 493 | |
| 494 | uint32_t |
| 495 | TargetList::SignalIfRunning (lldb::pid_t pid, int signo) |
| 496 | { |
| 497 | uint32_t num_signals_sent = 0; |
| 498 | Process *process = NULL; |
| 499 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 500 | { |
| 501 | // Signal all processes with signal |
| 502 | Mutex::Locker locker(m_target_list_mutex); |
| 503 | collection::iterator pos, end = m_target_list.end(); |
| 504 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 505 | { |
| 506 | process = (*pos)->GetProcessSP().get(); |
| 507 | if (process) |
| 508 | { |
| 509 | if (process->IsAlive()) |
| 510 | { |
| 511 | ++num_signals_sent; |
| 512 | process->Signal (signo); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | // Signal a specific process with signal |
| 520 | TargetSP target_sp(FindTargetWithProcessID (pid)); |
| 521 | if (target_sp.get()) |
| 522 | { |
| 523 | process = target_sp->GetProcessSP().get(); |
| 524 | if (process) |
| 525 | { |
| 526 | if (process->IsAlive()) |
| 527 | { |
| 528 | ++num_signals_sent; |
| 529 | process->Signal (signo); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | return num_signals_sent; |
| 535 | } |
| 536 | |
| 537 | int |
| 538 | TargetList::GetNumTargets () const |
| 539 | { |
| 540 | Mutex::Locker locker (m_target_list_mutex); |
| 541 | return m_target_list.size(); |
| 542 | } |
| 543 | |
| 544 | lldb::TargetSP |
| 545 | TargetList::GetTargetAtIndex (uint32_t idx) const |
| 546 | { |
| 547 | TargetSP target_sp; |
| 548 | Mutex::Locker locker (m_target_list_mutex); |
| 549 | if (idx < m_target_list.size()) |
| 550 | target_sp = m_target_list[idx]; |
| 551 | return target_sp; |
| 552 | } |
| 553 | |
| 554 | uint32_t |
Jim Ingham | 8499e1a | 2012-05-08 23:06:07 +0000 | [diff] [blame] | 555 | TargetList::GetIndexOfTarget (lldb::TargetSP target_sp) const |
| 556 | { |
| 557 | Mutex::Locker locker (m_target_list_mutex); |
| 558 | size_t num_targets = m_target_list.size(); |
| 559 | for (size_t idx = 0; idx < num_targets; idx++) |
| 560 | { |
| 561 | if (target_sp == m_target_list[idx]) |
| 562 | return idx; |
| 563 | } |
| 564 | return UINT32_MAX; |
| 565 | } |
| 566 | |
| 567 | uint32_t |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 568 | TargetList::SetSelectedTarget (Target* target) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 569 | { |
| 570 | Mutex::Locker locker (m_target_list_mutex); |
| 571 | collection::const_iterator pos, |
| 572 | begin = m_target_list.begin(), |
| 573 | end = m_target_list.end(); |
| 574 | for (pos = begin; pos != end; ++pos) |
| 575 | { |
| 576 | if (pos->get() == target) |
| 577 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 578 | m_selected_target_idx = std::distance (begin, pos); |
| 579 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 582 | m_selected_target_idx = 0; |
| 583 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | lldb::TargetSP |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 587 | TargetList::GetSelectedTarget () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 588 | { |
| 589 | Mutex::Locker locker (m_target_list_mutex); |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 590 | if (m_selected_target_idx >= m_target_list.size()) |
| 591 | m_selected_target_idx = 0; |
| 592 | return GetTargetAtIndex (m_selected_target_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 593 | } |