Chris Lattner | 24943d2 | 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 |
| 13 | // Project includes |
| 14 | #include "lldb/Core/Broadcaster.h" |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Event.h" |
| 17 | #include "lldb/Core/State.h" |
| 18 | #include "lldb/Core/Timer.h" |
| 19 | #include "lldb/Host/Host.h" |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 20 | #include "lldb/Target/Platform.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Process.h" |
| 22 | #include "lldb/Target/TargetList.h" |
| 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | |
| 28 | //---------------------------------------------------------------------- |
| 29 | // TargetList constructor |
| 30 | //---------------------------------------------------------------------- |
| 31 | TargetList::TargetList() : |
| 32 | Broadcaster("TargetList"), |
| 33 | m_target_list(), |
| 34 | m_target_list_mutex (Mutex::eMutexTypeRecursive), |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 35 | m_selected_target_idx (0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | //---------------------------------------------------------------------- |
| 40 | // Destructor |
| 41 | //---------------------------------------------------------------------- |
| 42 | TargetList::~TargetList() |
| 43 | { |
| 44 | Mutex::Locker locker(m_target_list_mutex); |
| 45 | m_target_list.clear(); |
| 46 | } |
| 47 | |
| 48 | Error |
| 49 | TargetList::CreateTarget |
| 50 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 51 | Debugger &debugger, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | const FileSpec& file, |
| 53 | const ArchSpec& arch, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | bool get_dependent_files, |
| 55 | TargetSP &target_sp |
| 56 | ) |
| 57 | { |
| 58 | Timer scoped_timer (__PRETTY_FUNCTION__, |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 59 | "TargetList::CreateTarget (file = '%s/%s', arch = '%s')", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | file.GetDirectory().AsCString(), |
| 61 | file.GetFilename().AsCString(), |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 62 | arch.GetArchitectureName()); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 63 | Error error; |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 64 | |
| 65 | PlatformSP platform_sp (debugger.GetPlatformList().GetSelectedPlatform ()); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 66 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 67 | if (file) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 68 | { |
| 69 | ModuleSP exe_module_sp; |
| 70 | FileSpec resolved_file(file); |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 71 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 72 | if (platform_sp) |
| 73 | error = platform_sp->ResolveExecutable (file, arch, exe_module_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 75 | if (error.Success() && exe_module_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | { |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 77 | if (exe_module_sp->GetObjectFile() == NULL) |
| 78 | { |
Greg Clayton | fa2f48b | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 79 | if (arch.IsValid()) |
| 80 | { |
| 81 | error.SetErrorStringWithFormat("\"%s%s%s\" doesn't contain architecture %s", |
| 82 | file.GetDirectory().AsCString(), |
| 83 | file.GetDirectory() ? "/" : "", |
| 84 | file.GetFilename().AsCString(), |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 85 | arch.GetArchitectureName()); |
Greg Clayton | fa2f48b | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 86 | } |
| 87 | else |
| 88 | { |
| 89 | error.SetErrorStringWithFormat("unsupported file type \"%s%s%s\"", |
| 90 | file.GetDirectory().AsCString(), |
| 91 | file.GetDirectory() ? "/" : "", |
| 92 | file.GetFilename().AsCString()); |
| 93 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 94 | return error; |
| 95 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 96 | target_sp.reset(new Target(debugger, arch, platform_sp)); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 97 | target_sp->SetExecutableModule (exe_module_sp, get_dependent_files); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 99 | } |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 100 | else |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 101 | { |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 102 | // No file was specified, just create an empty target with any arch |
| 103 | // if a valid arch was specified |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 104 | target_sp.reset(new Target(debugger, arch, platform_sp)); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (target_sp) |
| 108 | { |
| 109 | target_sp->UpdateInstanceName(); |
| 110 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 111 | Mutex::Locker locker(m_target_list_mutex); |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 112 | m_selected_target_idx = m_target_list.size(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 113 | m_target_list.push_back(target_sp); |
| 114 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | return error; |
| 117 | } |
| 118 | |
| 119 | bool |
| 120 | TargetList::DeleteTarget (TargetSP &target_sp) |
| 121 | { |
| 122 | Mutex::Locker locker(m_target_list_mutex); |
| 123 | collection::iterator pos, end = m_target_list.end(); |
| 124 | |
| 125 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 126 | { |
| 127 | if (pos->get() == target_sp.get()) |
| 128 | { |
| 129 | m_target_list.erase(pos); |
| 130 | return true; |
| 131 | } |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | TargetSP |
| 138 | TargetList::FindTargetWithExecutableAndArchitecture |
| 139 | ( |
| 140 | const FileSpec &exe_file_spec, |
| 141 | const ArchSpec *exe_arch_ptr |
| 142 | ) const |
| 143 | { |
| 144 | Mutex::Locker locker (m_target_list_mutex); |
| 145 | TargetSP target_sp; |
| 146 | bool full_match = exe_file_spec.GetDirectory(); |
| 147 | |
| 148 | collection::const_iterator pos, end = m_target_list.end(); |
| 149 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 150 | { |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 151 | Module *exe_module = (*pos)->GetExecutableModulePointer(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 153 | if (exe_module) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | { |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 155 | if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | { |
| 157 | if (exe_arch_ptr) |
| 158 | { |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 159 | if (*exe_arch_ptr != exe_module->GetArchitecture()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | target_sp = *pos; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | return target_sp; |
| 168 | } |
| 169 | |
| 170 | TargetSP |
| 171 | TargetList::FindTargetWithProcessID (lldb::pid_t pid) const |
| 172 | { |
| 173 | Mutex::Locker locker(m_target_list_mutex); |
| 174 | TargetSP target_sp; |
| 175 | collection::const_iterator pos, end = m_target_list.end(); |
| 176 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 177 | { |
| 178 | Process* process = (*pos)->GetProcessSP().get(); |
| 179 | if (process && process->GetID() == pid) |
| 180 | { |
| 181 | target_sp = *pos; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | return target_sp; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | TargetSP |
| 190 | TargetList::FindTargetWithProcess (Process *process) const |
| 191 | { |
| 192 | TargetSP target_sp; |
| 193 | if (process) |
| 194 | { |
| 195 | Mutex::Locker locker(m_target_list_mutex); |
| 196 | collection::const_iterator pos, end = m_target_list.end(); |
| 197 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 198 | { |
| 199 | if (process == (*pos)->GetProcessSP().get()) |
| 200 | { |
| 201 | target_sp = *pos; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | return target_sp; |
| 207 | } |
| 208 | |
| 209 | TargetSP |
| 210 | TargetList::GetTargetSP (Target *target) const |
| 211 | { |
| 212 | TargetSP target_sp; |
| 213 | if (target) |
| 214 | { |
| 215 | Mutex::Locker locker(m_target_list_mutex); |
| 216 | collection::const_iterator pos, end = m_target_list.end(); |
| 217 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 218 | { |
| 219 | if (target == (*pos).get()) |
| 220 | { |
| 221 | target_sp = *pos; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | return target_sp; |
| 227 | } |
| 228 | |
| 229 | uint32_t |
| 230 | TargetList::SendAsyncInterrupt (lldb::pid_t pid) |
| 231 | { |
| 232 | uint32_t num_async_interrupts_sent = 0; |
| 233 | |
| 234 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 235 | { |
| 236 | TargetSP target_sp(FindTargetWithProcessID (pid)); |
| 237 | if (target_sp.get()) |
| 238 | { |
| 239 | Process* process = target_sp->GetProcessSP().get(); |
| 240 | if (process) |
| 241 | { |
| 242 | process->BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 243 | ++num_async_interrupts_sent; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | // We don't have a valid pid to broadcast to, so broadcast to the target |
| 250 | // list's async broadcaster... |
| 251 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 252 | } |
| 253 | |
| 254 | return num_async_interrupts_sent; |
| 255 | } |
| 256 | |
| 257 | uint32_t |
| 258 | TargetList::SignalIfRunning (lldb::pid_t pid, int signo) |
| 259 | { |
| 260 | uint32_t num_signals_sent = 0; |
| 261 | Process *process = NULL; |
| 262 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 263 | { |
| 264 | // Signal all processes with signal |
| 265 | Mutex::Locker locker(m_target_list_mutex); |
| 266 | collection::iterator pos, end = m_target_list.end(); |
| 267 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 268 | { |
| 269 | process = (*pos)->GetProcessSP().get(); |
| 270 | if (process) |
| 271 | { |
| 272 | if (process->IsAlive()) |
| 273 | { |
| 274 | ++num_signals_sent; |
| 275 | process->Signal (signo); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | // Signal a specific process with signal |
| 283 | TargetSP target_sp(FindTargetWithProcessID (pid)); |
| 284 | if (target_sp.get()) |
| 285 | { |
| 286 | process = target_sp->GetProcessSP().get(); |
| 287 | if (process) |
| 288 | { |
| 289 | if (process->IsAlive()) |
| 290 | { |
| 291 | ++num_signals_sent; |
| 292 | process->Signal (signo); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | return num_signals_sent; |
| 298 | } |
| 299 | |
| 300 | int |
| 301 | TargetList::GetNumTargets () const |
| 302 | { |
| 303 | Mutex::Locker locker (m_target_list_mutex); |
| 304 | return m_target_list.size(); |
| 305 | } |
| 306 | |
| 307 | lldb::TargetSP |
| 308 | TargetList::GetTargetAtIndex (uint32_t idx) const |
| 309 | { |
| 310 | TargetSP target_sp; |
| 311 | Mutex::Locker locker (m_target_list_mutex); |
| 312 | if (idx < m_target_list.size()) |
| 313 | target_sp = m_target_list[idx]; |
| 314 | return target_sp; |
| 315 | } |
| 316 | |
| 317 | uint32_t |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 318 | TargetList::SetSelectedTarget (Target* target) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | { |
| 320 | Mutex::Locker locker (m_target_list_mutex); |
| 321 | collection::const_iterator pos, |
| 322 | begin = m_target_list.begin(), |
| 323 | end = m_target_list.end(); |
| 324 | for (pos = begin; pos != end; ++pos) |
| 325 | { |
| 326 | if (pos->get() == target) |
| 327 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 328 | m_selected_target_idx = std::distance (begin, pos); |
| 329 | return m_selected_target_idx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 332 | m_selected_target_idx = 0; |
| 333 | return m_selected_target_idx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | lldb::TargetSP |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 337 | TargetList::GetSelectedTarget () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 338 | { |
| 339 | Mutex::Locker locker (m_target_list_mutex); |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 340 | if (m_selected_target_idx >= m_target_list.size()) |
| 341 | m_selected_target_idx = 0; |
| 342 | return GetTargetAtIndex (m_selected_target_idx); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | } |