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