Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Process.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 | #include "lldb/Target/Process.h" |
| 11 | |
| 12 | #include "lldb/lldb-private-log.h" |
| 13 | |
| 14 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 15 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| 16 | #include "lldb/Core/Event.h" |
| 17 | #include "lldb/Core/Debugger.h" |
| 18 | #include "lldb/Core/Log.h" |
| 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/State.h" |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Host/Host.h" |
| 23 | #include "lldb/Target/ABI.h" |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 24 | #include "lldb/Target/LanguageRuntime.h" |
| 25 | #include "lldb/Target/CPPLanguageRuntime.h" |
| 26 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 28 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Target.h" |
| 30 | #include "lldb/Target/TargetList.h" |
| 31 | #include "lldb/Target/Thread.h" |
| 32 | #include "lldb/Target/ThreadPlan.h" |
| 33 | |
| 34 | using namespace lldb; |
| 35 | using namespace lldb_private; |
| 36 | |
| 37 | Process* |
| 38 | Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener) |
| 39 | { |
| 40 | ProcessCreateInstance create_callback = NULL; |
| 41 | if (plugin_name) |
| 42 | { |
| 43 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name); |
| 44 | if (create_callback) |
| 45 | { |
| 46 | std::auto_ptr<Process> debugger_ap(create_callback(target, listener)); |
| 47 | if (debugger_ap->CanDebug(target)) |
| 48 | return debugger_ap.release(); |
| 49 | } |
| 50 | } |
| 51 | else |
| 52 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 53 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 55 | std::auto_ptr<Process> debugger_ap(create_callback(target, listener)); |
| 56 | if (debugger_ap->CanDebug(target)) |
| 57 | return debugger_ap.release(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | //---------------------------------------------------------------------- |
| 65 | // Process constructor |
| 66 | //---------------------------------------------------------------------- |
| 67 | Process::Process(Target &target, Listener &listener) : |
| 68 | UserID (LLDB_INVALID_PROCESS_ID), |
| 69 | Broadcaster ("Process"), |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 70 | ProcessInstanceSettings (*(Process::GetSettingsController().get())), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | m_target (target), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | m_public_state (eStateUnloaded), |
| 73 | m_private_state (eStateUnloaded), |
| 74 | m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"), |
| 75 | m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"), |
| 76 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 77 | m_private_state_control_wait(), |
| 78 | m_private_state_thread (LLDB_INVALID_HOST_THREAD), |
| 79 | m_stop_id (0), |
| 80 | m_thread_index_id (0), |
| 81 | m_exit_status (-1), |
| 82 | m_exit_string (), |
| 83 | m_thread_list (this), |
| 84 | m_notifications (), |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 85 | m_persistent_vars(), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | m_listener(listener), |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 87 | m_unix_signals () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | { |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 89 | UpdateInstanceName(); |
| 90 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); |
| 92 | if (log) |
| 93 | log->Printf ("%p Process::Process()", this); |
| 94 | |
| 95 | listener.StartListeningForEvents (this, |
| 96 | eBroadcastBitStateChanged | |
| 97 | eBroadcastBitInterrupt | |
| 98 | eBroadcastBitSTDOUT | |
| 99 | eBroadcastBitSTDERR); |
| 100 | |
| 101 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
| 102 | eBroadcastBitStateChanged); |
| 103 | |
| 104 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 105 | eBroadcastInternalStateControlStop | |
| 106 | eBroadcastInternalStateControlPause | |
| 107 | eBroadcastInternalStateControlResume); |
| 108 | } |
| 109 | |
| 110 | //---------------------------------------------------------------------- |
| 111 | // Destructor |
| 112 | //---------------------------------------------------------------------- |
| 113 | Process::~Process() |
| 114 | { |
| 115 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT); |
| 116 | if (log) |
| 117 | log->Printf ("%p Process::~Process()", this); |
| 118 | StopPrivateStateThread(); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | Process::Finalize() |
| 123 | { |
| 124 | // Do any cleanup needed prior to being destructed... Subclasses |
| 125 | // that override this method should call this superclass method as well. |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 130 | { |
| 131 | m_notifications.push_back(callbacks); |
| 132 | if (callbacks.initialize != NULL) |
| 133 | callbacks.initialize (callbacks.baton, this); |
| 134 | } |
| 135 | |
| 136 | bool |
| 137 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 138 | { |
| 139 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 140 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 141 | { |
| 142 | if (pos->baton == callbacks.baton && |
| 143 | pos->initialize == callbacks.initialize && |
| 144 | pos->process_state_changed == callbacks.process_state_changed) |
| 145 | { |
| 146 | m_notifications.erase(pos); |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 155 | { |
| 156 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 157 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 158 | { |
| 159 | if (notification_pos->process_state_changed) |
| 160 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 165 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 166 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 167 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 168 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 169 | // stall. So instead we do it when we fetch the event off of the queue. |
| 170 | // |
| 171 | |
| 172 | StateType |
| 173 | Process::GetNextEvent (EventSP &event_sp) |
| 174 | { |
| 175 | StateType state = eStateInvalid; |
| 176 | |
| 177 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 178 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 179 | |
| 180 | return state; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | StateType |
| 185 | Process::WaitForProcessToStop (const TimeValue *timeout) |
| 186 | { |
| 187 | StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded }; |
| 188 | return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType)); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | StateType |
| 193 | Process::WaitForState |
| 194 | ( |
| 195 | const TimeValue *timeout, |
| 196 | const StateType *match_states, const uint32_t num_match_states |
| 197 | ) |
| 198 | { |
| 199 | EventSP event_sp; |
| 200 | uint32_t i; |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 201 | StateType state = GetState(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | while (state != eStateInvalid) |
| 203 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 204 | // If we are exited or detached, we won't ever get back to any |
| 205 | // other valid state... |
| 206 | if (state == eStateDetached || state == eStateExited) |
| 207 | return state; |
| 208 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | state = WaitForStateChangedEvents (timeout, event_sp); |
| 210 | |
| 211 | for (i=0; i<num_match_states; ++i) |
| 212 | { |
| 213 | if (match_states[i] == state) |
| 214 | return state; |
| 215 | } |
| 216 | } |
| 217 | return state; |
| 218 | } |
| 219 | |
Jim Ingham | 63e24d7 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 220 | bool |
| 221 | Process::HijackProcessEvents (Listener *listener) |
| 222 | { |
| 223 | if (listener != NULL) |
| 224 | { |
| 225 | return HijackBroadcaster(listener, eBroadcastBitStateChanged); |
| 226 | } |
| 227 | else |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | Process::RestoreProcessEvents () |
| 233 | { |
| 234 | RestoreBroadcaster(); |
| 235 | } |
| 236 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | StateType |
| 238 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp) |
| 239 | { |
| 240 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 241 | |
| 242 | if (log) |
| 243 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 244 | |
| 245 | StateType state = eStateInvalid; |
| 246 | if (m_listener.WaitForEventForBroadcasterWithType(timeout, |
| 247 | this, |
| 248 | eBroadcastBitStateChanged, |
| 249 | event_sp)) |
| 250 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 251 | |
| 252 | if (log) |
| 253 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 254 | __FUNCTION__, |
| 255 | timeout, |
| 256 | StateAsCString(state)); |
| 257 | return state; |
| 258 | } |
| 259 | |
| 260 | Event * |
| 261 | Process::PeekAtStateChangedEvents () |
| 262 | { |
| 263 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 264 | |
| 265 | if (log) |
| 266 | log->Printf ("Process::%s...", __FUNCTION__); |
| 267 | |
| 268 | Event *event_ptr; |
| 269 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType(this, |
| 270 | eBroadcastBitStateChanged); |
| 271 | if (log) |
| 272 | { |
| 273 | if (event_ptr) |
| 274 | { |
| 275 | log->Printf ("Process::%s (event_ptr) => %s", |
| 276 | __FUNCTION__, |
| 277 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | log->Printf ("Process::%s no events found", |
| 282 | __FUNCTION__); |
| 283 | } |
| 284 | } |
| 285 | return event_ptr; |
| 286 | } |
| 287 | |
| 288 | StateType |
| 289 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 290 | { |
| 291 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 292 | |
| 293 | if (log) |
| 294 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 295 | |
| 296 | StateType state = eStateInvalid; |
| 297 | if (m_private_state_listener.WaitForEventForBroadcasterWithType(timeout, |
| 298 | &m_private_state_broadcaster, |
| 299 | eBroadcastBitStateChanged, |
| 300 | event_sp)) |
| 301 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 302 | |
| 303 | // This is a bit of a hack, but when we wait here we could very well return |
| 304 | // to the command-line, and that could disable the log, which would render the |
| 305 | // log we got above invalid. |
| 306 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 307 | if (log) |
| 308 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state)); |
| 309 | return state; |
| 310 | } |
| 311 | |
| 312 | bool |
| 313 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 314 | { |
| 315 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 316 | |
| 317 | if (log) |
| 318 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 319 | |
| 320 | if (control_only) |
| 321 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 322 | else |
| 323 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 324 | } |
| 325 | |
| 326 | bool |
| 327 | Process::IsRunning () const |
| 328 | { |
| 329 | return StateIsRunningState (m_public_state.GetValue()); |
| 330 | } |
| 331 | |
| 332 | int |
| 333 | Process::GetExitStatus () |
| 334 | { |
| 335 | if (m_public_state.GetValue() == eStateExited) |
| 336 | return m_exit_status; |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | const char * |
| 341 | Process::GetExitDescription () |
| 342 | { |
| 343 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 344 | return m_exit_string.c_str(); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | Process::SetExitStatus (int status, const char *cstr) |
| 350 | { |
| 351 | m_exit_status = status; |
| 352 | if (cstr) |
| 353 | m_exit_string = cstr; |
| 354 | else |
| 355 | m_exit_string.clear(); |
| 356 | |
| 357 | SetPrivateState (eStateExited); |
| 358 | } |
| 359 | |
| 360 | // This static callback can be used to watch for local child processes on |
| 361 | // the current host. The the child process exits, the process will be |
| 362 | // found in the global target list (we want to be completely sure that the |
| 363 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 364 | bool |
| 365 | Process::SetProcessExitStatus |
| 366 | ( |
| 367 | void *callback_baton, |
| 368 | lldb::pid_t pid, |
| 369 | int signo, // Zero for no signal |
| 370 | int exit_status // Exit value of process if signal is zero |
| 371 | ) |
| 372 | { |
| 373 | if (signo == 0 || exit_status) |
| 374 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 375 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 376 | if (target_sp) |
| 377 | { |
| 378 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 379 | if (process_sp) |
| 380 | { |
| 381 | const char *signal_cstr = NULL; |
| 382 | if (signo) |
| 383 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 384 | |
| 385 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 386 | } |
| 387 | } |
| 388 | return true; |
| 389 | } |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | uint32_t |
| 395 | Process::GetNextThreadIndexID () |
| 396 | { |
| 397 | return ++m_thread_index_id; |
| 398 | } |
| 399 | |
| 400 | StateType |
| 401 | Process::GetState() |
| 402 | { |
| 403 | // If any other threads access this we will need a mutex for it |
| 404 | return m_public_state.GetValue (); |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | Process::SetPublicState (StateType new_state) |
| 409 | { |
| 410 | Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE); |
| 411 | if (log) |
| 412 | log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state)); |
| 413 | m_public_state.SetValue (new_state); |
| 414 | } |
| 415 | |
| 416 | StateType |
| 417 | Process::GetPrivateState () |
| 418 | { |
| 419 | return m_private_state.GetValue(); |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | Process::SetPrivateState (StateType new_state) |
| 424 | { |
| 425 | Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE); |
| 426 | bool state_changed = false; |
| 427 | |
| 428 | if (log) |
| 429 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 430 | |
| 431 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 432 | |
| 433 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 434 | state_changed = old_state != new_state; |
| 435 | if (state_changed) |
| 436 | { |
| 437 | m_private_state.SetValueNoLock (new_state); |
| 438 | if (StateIsStoppedState(new_state)) |
| 439 | { |
| 440 | m_stop_id++; |
| 441 | if (log) |
| 442 | log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id); |
| 443 | } |
| 444 | // Use our target to get a shared pointer to ourselves... |
| 445 | m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state)); |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | if (log) |
| 450 | log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state)); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | |
| 455 | uint32_t |
| 456 | Process::GetStopID() const |
| 457 | { |
| 458 | return m_stop_id; |
| 459 | } |
| 460 | |
| 461 | addr_t |
| 462 | Process::GetImageInfoAddress() |
| 463 | { |
| 464 | return LLDB_INVALID_ADDRESS; |
| 465 | } |
| 466 | |
| 467 | DynamicLoader * |
| 468 | Process::GetDynamicLoader() |
| 469 | { |
| 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | const ABI * |
| 474 | Process::GetABI() |
| 475 | { |
| 476 | ConstString& triple = m_target_triple; |
| 477 | |
| 478 | if (triple.IsEmpty()) |
| 479 | return NULL; |
| 480 | |
| 481 | if (m_abi_sp.get() == NULL) |
| 482 | { |
| 483 | m_abi_sp.reset(ABI::FindPlugin(triple)); |
| 484 | } |
| 485 | |
| 486 | return m_abi_sp.get(); |
| 487 | } |
| 488 | |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 489 | LanguageRuntime * |
| 490 | Process::GetLanguageRuntime(lldb::LanguageType language) |
| 491 | { |
| 492 | LanguageRuntimeCollection::iterator pos; |
| 493 | pos = m_language_runtimes.find (language); |
| 494 | if (pos == m_language_runtimes.end()) |
| 495 | { |
| 496 | lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language)); |
| 497 | |
| 498 | m_language_runtimes[language] |
| 499 | = runtime; |
| 500 | return runtime.get(); |
| 501 | } |
| 502 | else |
| 503 | return (*pos).second.get(); |
| 504 | } |
| 505 | |
| 506 | CPPLanguageRuntime * |
| 507 | Process::GetCPPLanguageRuntime () |
| 508 | { |
| 509 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus); |
| 510 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 511 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 512 | return NULL; |
| 513 | } |
| 514 | |
| 515 | ObjCLanguageRuntime * |
| 516 | Process::GetObjCLanguageRuntime () |
| 517 | { |
| 518 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC); |
| 519 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 520 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 521 | return NULL; |
| 522 | } |
| 523 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 524 | BreakpointSiteList & |
| 525 | Process::GetBreakpointSiteList() |
| 526 | { |
| 527 | return m_breakpoint_site_list; |
| 528 | } |
| 529 | |
| 530 | const BreakpointSiteList & |
| 531 | Process::GetBreakpointSiteList() const |
| 532 | { |
| 533 | return m_breakpoint_site_list; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | void |
| 538 | Process::DisableAllBreakpointSites () |
| 539 | { |
| 540 | m_breakpoint_site_list.SetEnabledForAll (false); |
| 541 | } |
| 542 | |
| 543 | Error |
| 544 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 545 | { |
| 546 | Error error (DisableBreakpointSiteByID (break_id)); |
| 547 | |
| 548 | if (error.Success()) |
| 549 | m_breakpoint_site_list.Remove(break_id); |
| 550 | |
| 551 | return error; |
| 552 | } |
| 553 | |
| 554 | Error |
| 555 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 556 | { |
| 557 | Error error; |
| 558 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 559 | if (bp_site_sp) |
| 560 | { |
| 561 | if (bp_site_sp->IsEnabled()) |
| 562 | error = DisableBreakpoint (bp_site_sp.get()); |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id); |
| 567 | } |
| 568 | |
| 569 | return error; |
| 570 | } |
| 571 | |
| 572 | Error |
| 573 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 574 | { |
| 575 | Error error; |
| 576 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 577 | if (bp_site_sp) |
| 578 | { |
| 579 | if (!bp_site_sp->IsEnabled()) |
| 580 | error = EnableBreakpoint (bp_site_sp.get()); |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id); |
| 585 | } |
| 586 | return error; |
| 587 | } |
| 588 | |
Stephen Wilson | 3fd1f36 | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 589 | lldb::break_id_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 590 | Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware) |
| 591 | { |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 592 | const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 593 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 594 | { |
| 595 | BreakpointSiteSP bp_site_sp; |
| 596 | |
| 597 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 598 | // create a new breakpoint site and add it. |
| 599 | |
| 600 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 601 | |
| 602 | if (bp_site_sp) |
| 603 | { |
| 604 | bp_site_sp->AddOwner (owner); |
| 605 | owner->SetBreakpointSite (bp_site_sp); |
| 606 | return bp_site_sp->GetID(); |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware)); |
| 611 | if (bp_site_sp) |
| 612 | { |
| 613 | if (EnableBreakpoint (bp_site_sp.get()).Success()) |
| 614 | { |
| 615 | owner->SetBreakpointSite (bp_site_sp); |
| 616 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | // We failed to enable the breakpoint |
| 622 | return LLDB_INVALID_BREAK_ID; |
| 623 | |
| 624 | } |
| 625 | |
| 626 | void |
| 627 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 628 | { |
| 629 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 630 | if (num_owners == 0) |
| 631 | { |
| 632 | DisableBreakpoint(bp_site_sp.get()); |
| 633 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | |
| 638 | size_t |
| 639 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 640 | { |
| 641 | size_t bytes_removed = 0; |
| 642 | addr_t intersect_addr; |
| 643 | size_t intersect_size; |
| 644 | size_t opcode_offset; |
| 645 | size_t idx; |
| 646 | BreakpointSiteSP bp; |
| 647 | |
| 648 | for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx) |
| 649 | { |
| 650 | if (bp->GetType() == BreakpointSite::eSoftware) |
| 651 | { |
| 652 | if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset)) |
| 653 | { |
| 654 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 655 | assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size); |
| 656 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 657 | size_t buf_offset = intersect_addr - bp_addr; |
| 658 | ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | return bytes_removed; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | Error |
| 667 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 668 | { |
| 669 | Error error; |
| 670 | assert (bp_site != NULL); |
| 671 | Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 672 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 673 | if (log) |
| 674 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr); |
| 675 | if (bp_site->IsEnabled()) |
| 676 | { |
| 677 | if (log) |
| 678 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 679 | return error; |
| 680 | } |
| 681 | |
| 682 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 683 | { |
| 684 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 685 | return error; |
| 686 | } |
| 687 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 688 | // trap for the breakpoint site |
| 689 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 690 | |
| 691 | if (bp_opcode_size == 0) |
| 692 | { |
| 693 | error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr); |
| 694 | } |
| 695 | else |
| 696 | { |
| 697 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 698 | |
| 699 | if (bp_opcode_bytes == NULL) |
| 700 | { |
| 701 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 702 | return error; |
| 703 | } |
| 704 | |
| 705 | // Save the original opcode by reading it |
| 706 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 707 | { |
| 708 | // Write a software breakpoint in place of the original opcode |
| 709 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 710 | { |
| 711 | uint8_t verify_bp_opcode_bytes[64]; |
| 712 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 713 | { |
| 714 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 715 | { |
| 716 | bp_site->SetEnabled(true); |
| 717 | bp_site->SetType (BreakpointSite::eSoftware); |
| 718 | if (log) |
| 719 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", |
| 720 | bp_site->GetID(), |
| 721 | (uint64_t)bp_addr); |
| 722 | } |
| 723 | else |
| 724 | error.SetErrorString("Failed to verify the breakpoint trap in memory."); |
| 725 | } |
| 726 | else |
| 727 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 728 | } |
| 729 | else |
| 730 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 731 | } |
| 732 | else |
| 733 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 734 | } |
| 735 | if (log) |
| 736 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 737 | bp_site->GetID(), |
| 738 | (uint64_t)bp_addr, |
| 739 | error.AsCString()); |
| 740 | return error; |
| 741 | } |
| 742 | |
| 743 | Error |
| 744 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 745 | { |
| 746 | Error error; |
| 747 | assert (bp_site != NULL); |
| 748 | Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS); |
| 749 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 750 | lldb::user_id_t breakID = bp_site->GetID(); |
| 751 | if (log) |
| 752 | log->Printf ("ProcessMacOSX::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr); |
| 753 | |
| 754 | if (bp_site->IsHardware()) |
| 755 | { |
| 756 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 757 | } |
| 758 | else if (bp_site->IsEnabled()) |
| 759 | { |
| 760 | const size_t break_op_size = bp_site->GetByteSize(); |
| 761 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 762 | if (break_op_size > 0) |
| 763 | { |
| 764 | // Clear a software breakoint instruction |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 765 | uint8_t curr_break_op[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 766 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 767 | bool break_op_found = false; |
| 768 | |
| 769 | // Read the breakpoint opcode |
| 770 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 771 | { |
| 772 | bool verify = false; |
| 773 | // Make sure we have the a breakpoint opcode exists at this address |
| 774 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 775 | { |
| 776 | break_op_found = true; |
| 777 | // We found a valid breakpoint opcode at this address, now restore |
| 778 | // the saved opcode. |
| 779 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 780 | { |
| 781 | verify = true; |
| 782 | } |
| 783 | else |
| 784 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 789 | // Set verify to true and so we can check if the original opcode has already been restored |
| 790 | verify = true; |
| 791 | } |
| 792 | |
| 793 | if (verify) |
| 794 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 795 | uint8_t verify_opcode[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 796 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 797 | // Verify that our original opcode made it back to the inferior |
| 798 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 799 | { |
| 800 | // compare the memory we just read with the original opcode |
| 801 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 802 | { |
| 803 | // SUCCESS |
| 804 | bp_site->SetEnabled(false); |
| 805 | if (log) |
| 806 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr); |
| 807 | return error; |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | if (break_op_found) |
| 812 | error.SetErrorString("Failed to restore original opcode."); |
| 813 | } |
| 814 | } |
| 815 | else |
| 816 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 817 | } |
| 818 | } |
| 819 | else |
| 820 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 821 | } |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | if (log) |
| 826 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 827 | return error; |
| 828 | } |
| 829 | |
| 830 | if (log) |
| 831 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 832 | bp_site->GetID(), |
| 833 | (uint64_t)bp_addr, |
| 834 | error.AsCString()); |
| 835 | return error; |
| 836 | |
| 837 | } |
| 838 | |
| 839 | |
| 840 | size_t |
| 841 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 842 | { |
| 843 | if (buf == NULL || size == 0) |
| 844 | return 0; |
| 845 | |
| 846 | size_t bytes_read = 0; |
| 847 | uint8_t *bytes = (uint8_t *)buf; |
| 848 | |
| 849 | while (bytes_read < size) |
| 850 | { |
| 851 | const size_t curr_size = size - bytes_read; |
| 852 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 853 | bytes + bytes_read, |
| 854 | curr_size, |
| 855 | error); |
| 856 | bytes_read += curr_bytes_read; |
| 857 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | // Replace any software breakpoint opcodes that fall into this range back |
| 862 | // into "buf" before we return |
| 863 | if (bytes_read > 0) |
| 864 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 865 | return bytes_read; |
| 866 | } |
| 867 | |
| 868 | size_t |
| 869 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 870 | { |
| 871 | size_t bytes_written = 0; |
| 872 | const uint8_t *bytes = (const uint8_t *)buf; |
| 873 | |
| 874 | while (bytes_written < size) |
| 875 | { |
| 876 | const size_t curr_size = size - bytes_written; |
| 877 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 878 | bytes + bytes_written, |
| 879 | curr_size, |
| 880 | error); |
| 881 | bytes_written += curr_bytes_written; |
| 882 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 883 | break; |
| 884 | } |
| 885 | return bytes_written; |
| 886 | } |
| 887 | |
| 888 | size_t |
| 889 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 890 | { |
| 891 | if (buf == NULL || size == 0) |
| 892 | return 0; |
| 893 | // We need to write any data that would go where any current software traps |
| 894 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 895 | // may have placed in our tasks memory. |
| 896 | |
| 897 | BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr); |
| 898 | BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end(); |
| 899 | |
| 900 | if (iter == end || iter->second->GetLoadAddress() > addr + size) |
| 901 | return DoWriteMemory(addr, buf, size, error); |
| 902 | |
| 903 | BreakpointSiteList::collection::const_iterator pos; |
| 904 | size_t bytes_written = 0; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 905 | addr_t intersect_addr = 0; |
| 906 | size_t intersect_size = 0; |
| 907 | size_t opcode_offset = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 908 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 909 | |
| 910 | for (pos = iter; pos != end; ++pos) |
| 911 | { |
| 912 | BreakpointSiteSP bp; |
| 913 | bp = pos->second; |
| 914 | |
| 915 | assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset)); |
| 916 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 917 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 918 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 919 | |
| 920 | // Check for bytes before this breakpoint |
| 921 | const addr_t curr_addr = addr + bytes_written; |
| 922 | if (intersect_addr > curr_addr) |
| 923 | { |
| 924 | // There are some bytes before this breakpoint that we need to |
| 925 | // just write to memory |
| 926 | size_t curr_size = intersect_addr - curr_addr; |
| 927 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 928 | ubuf + bytes_written, |
| 929 | curr_size, |
| 930 | error); |
| 931 | bytes_written += curr_bytes_written; |
| 932 | if (curr_bytes_written != curr_size) |
| 933 | { |
| 934 | // We weren't able to write all of the requested bytes, we |
| 935 | // are done looping and will return the number of bytes that |
| 936 | // we have written so far. |
| 937 | break; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | // Now write any bytes that would cover up any software breakpoints |
| 942 | // directly into the breakpoint opcode buffer |
| 943 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 944 | bytes_written += intersect_size; |
| 945 | } |
| 946 | |
| 947 | // Write any remaining bytes after the last breakpoint if we have any left |
| 948 | if (bytes_written < size) |
| 949 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 950 | ubuf + bytes_written, |
| 951 | size - bytes_written, |
| 952 | error); |
| 953 | |
| 954 | return bytes_written; |
| 955 | } |
| 956 | |
| 957 | addr_t |
| 958 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 959 | { |
| 960 | // Fixme: we should track the blocks we've allocated, and clean them up... |
| 961 | // We could even do our own allocator here if that ends up being more efficient. |
| 962 | return DoAllocateMemory (size, permissions, error); |
| 963 | } |
| 964 | |
| 965 | Error |
| 966 | Process::DeallocateMemory (addr_t ptr) |
| 967 | { |
| 968 | return DoDeallocateMemory (ptr); |
| 969 | } |
| 970 | |
| 971 | |
| 972 | Error |
| 973 | Process::EnableWatchpoint (WatchpointLocation *watchpoint) |
| 974 | { |
| 975 | Error error; |
| 976 | error.SetErrorString("watchpoints are not supported"); |
| 977 | return error; |
| 978 | } |
| 979 | |
| 980 | Error |
| 981 | Process::DisableWatchpoint (WatchpointLocation *watchpoint) |
| 982 | { |
| 983 | Error error; |
| 984 | error.SetErrorString("watchpoints are not supported"); |
| 985 | return error; |
| 986 | } |
| 987 | |
| 988 | StateType |
| 989 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 990 | { |
| 991 | StateType state; |
| 992 | // Now wait for the process to launch and return control to us, and then |
| 993 | // call DidLaunch: |
| 994 | while (1) |
| 995 | { |
| 996 | // FIXME: Might want to put a timeout in here: |
| 997 | state = WaitForStateChangedEventsPrivate (NULL, event_sp); |
| 998 | if (state == eStateStopped || state == eStateCrashed || state == eStateExited) |
| 999 | break; |
| 1000 | else |
| 1001 | HandlePrivateEvent (event_sp); |
| 1002 | } |
| 1003 | return state; |
| 1004 | } |
| 1005 | |
| 1006 | Error |
| 1007 | Process::Launch |
| 1008 | ( |
| 1009 | char const *argv[], |
| 1010 | char const *envp[], |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1011 | uint32_t launch_flags, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1012 | const char *stdin_path, |
| 1013 | const char *stdout_path, |
| 1014 | const char *stderr_path |
| 1015 | ) |
| 1016 | { |
| 1017 | Error error; |
| 1018 | m_target_triple.Clear(); |
| 1019 | m_abi_sp.reset(); |
| 1020 | |
| 1021 | Module *exe_module = m_target.GetExecutableModule().get(); |
| 1022 | if (exe_module) |
| 1023 | { |
| 1024 | char exec_file_path[PATH_MAX]; |
| 1025 | exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path)); |
| 1026 | if (exe_module->GetFileSpec().Exists()) |
| 1027 | { |
| 1028 | error = WillLaunch (exe_module); |
| 1029 | if (error.Success()) |
| 1030 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1031 | SetPublicState (eStateLaunching); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1032 | // The args coming in should not contain the application name, the |
| 1033 | // lldb_private::Process class will add this in case the executable |
| 1034 | // gets resolved to a different file than was given on the command |
| 1035 | // line (like when an applicaiton bundle is specified and will |
| 1036 | // resolve to the contained exectuable file, or the file given was |
| 1037 | // a symlink or other file system link that resolves to a different |
| 1038 | // file). |
| 1039 | |
| 1040 | // Get the resolved exectuable path |
| 1041 | |
| 1042 | // Make a new argument vector |
| 1043 | std::vector<const char *> exec_path_plus_argv; |
| 1044 | // Append the resolved executable path |
| 1045 | exec_path_plus_argv.push_back (exec_file_path); |
| 1046 | |
| 1047 | // Push all args if there are any |
| 1048 | if (argv) |
| 1049 | { |
| 1050 | for (int i = 0; argv[i]; ++i) |
| 1051 | exec_path_plus_argv.push_back(argv[i]); |
| 1052 | } |
| 1053 | |
| 1054 | // Push a NULL to terminate the args. |
| 1055 | exec_path_plus_argv.push_back(NULL); |
| 1056 | |
| 1057 | // Now launch using these arguments. |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 1058 | error = DoLaunch (exe_module, |
| 1059 | exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(), |
| 1060 | envp, |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1061 | launch_flags, |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 1062 | stdin_path, |
| 1063 | stdout_path, |
| 1064 | stderr_path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1065 | |
| 1066 | if (error.Fail()) |
| 1067 | { |
| 1068 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1069 | { |
| 1070 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1071 | const char *error_string = error.AsCString(); |
| 1072 | if (error_string == NULL) |
| 1073 | error_string = "launch failed"; |
| 1074 | SetExitStatus (-1, error_string); |
| 1075 | } |
| 1076 | } |
| 1077 | else |
| 1078 | { |
| 1079 | EventSP event_sp; |
| 1080 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 1081 | |
| 1082 | if (state == eStateStopped || state == eStateCrashed) |
| 1083 | { |
| 1084 | DidLaunch (); |
| 1085 | |
| 1086 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 1087 | // a chance to complete... |
| 1088 | HandlePrivateEvent (event_sp); |
| 1089 | StartPrivateStateThread (); |
| 1090 | } |
| 1091 | else if (state == eStateExited) |
| 1092 | { |
| 1093 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 1094 | // not likely to work, and return an invalid pid. |
| 1095 | HandlePrivateEvent (event_sp); |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | else |
| 1101 | { |
| 1102 | error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path); |
| 1103 | } |
| 1104 | } |
| 1105 | return error; |
| 1106 | } |
| 1107 | |
| 1108 | Error |
| 1109 | Process::CompleteAttach () |
| 1110 | { |
| 1111 | Error error; |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame^] | 1112 | |
| 1113 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1114 | { |
| 1115 | error.SetErrorString("no process"); |
| 1116 | } |
| 1117 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1118 | EventSP event_sp; |
| 1119 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 1120 | if (state == eStateStopped || state == eStateCrashed) |
| 1121 | { |
| 1122 | DidAttach (); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1123 | // Figure out which one is the executable, and set that in our target: |
| 1124 | ModuleList &modules = GetTarget().GetImages(); |
| 1125 | |
| 1126 | size_t num_modules = modules.GetSize(); |
| 1127 | for (int i = 0; i < num_modules; i++) |
| 1128 | { |
| 1129 | ModuleSP module_sp = modules.GetModuleAtIndex(i); |
| 1130 | if (module_sp->IsExecutable()) |
| 1131 | { |
| 1132 | ModuleSP exec_module = GetTarget().GetExecutableModule(); |
| 1133 | if (!exec_module || exec_module != module_sp) |
| 1134 | { |
| 1135 | |
| 1136 | GetTarget().SetExecutableModule (module_sp, false); |
| 1137 | } |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1141 | |
| 1142 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 1143 | // a chance to complete... |
| 1144 | HandlePrivateEvent(event_sp); |
| 1145 | StartPrivateStateThread(); |
| 1146 | } |
| 1147 | else |
| 1148 | { |
| 1149 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 1150 | // not likely to work, and return an invalid pid. |
| 1151 | if (state == eStateExited) |
| 1152 | HandlePrivateEvent (event_sp); |
| 1153 | error.SetErrorStringWithFormat("invalid state after attach: %s", |
| 1154 | lldb_private::StateAsCString(state)); |
| 1155 | } |
| 1156 | return error; |
| 1157 | } |
| 1158 | |
| 1159 | Error |
| 1160 | Process::Attach (lldb::pid_t attach_pid) |
| 1161 | { |
| 1162 | |
| 1163 | m_target_triple.Clear(); |
| 1164 | m_abi_sp.reset(); |
| 1165 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1166 | // Find the process and its architecture. Make sure it matches the architecture |
| 1167 | // of the current Target, and if not adjust it. |
| 1168 | |
| 1169 | ArchSpec attach_spec = GetArchSpecForExistingProcess (attach_pid); |
| 1170 | if (attach_spec != GetTarget().GetArchitecture()) |
| 1171 | { |
| 1172 | // Set the architecture on the target. |
| 1173 | GetTarget().SetArchitecture(attach_spec); |
| 1174 | } |
| 1175 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1176 | Error error (WillAttachToProcessWithID(attach_pid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1177 | if (error.Success()) |
| 1178 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1179 | SetPublicState (eStateAttaching); |
| 1180 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1181 | error = DoAttachToProcessWithID (attach_pid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1182 | if (error.Success()) |
| 1183 | { |
| 1184 | error = CompleteAttach(); |
| 1185 | } |
| 1186 | else |
| 1187 | { |
| 1188 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1189 | { |
| 1190 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1191 | const char *error_string = error.AsCString(); |
| 1192 | if (error_string == NULL) |
| 1193 | error_string = "attach failed"; |
| 1194 | |
| 1195 | SetExitStatus(-1, error_string); |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | return error; |
| 1200 | } |
| 1201 | |
| 1202 | Error |
| 1203 | Process::Attach (const char *process_name, bool wait_for_launch) |
| 1204 | { |
| 1205 | m_target_triple.Clear(); |
| 1206 | m_abi_sp.reset(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1207 | |
| 1208 | // Find the process and its architecture. Make sure it matches the architecture |
| 1209 | // of the current Target, and if not adjust it. |
| 1210 | |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1211 | if (!wait_for_launch) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1212 | { |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1213 | ArchSpec attach_spec = GetArchSpecForExistingProcess (process_name); |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame^] | 1214 | if (attach_spec.IsValid() && attach_spec != GetTarget().GetArchitecture()) |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1215 | { |
| 1216 | // Set the architecture on the target. |
| 1217 | GetTarget().SetArchitecture(attach_spec); |
| 1218 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1219 | } |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1220 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1221 | Error error (WillAttachToProcessWithName(process_name, wait_for_launch)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1222 | if (error.Success()) |
| 1223 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1224 | SetPublicState (eStateAttaching); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1225 | StartPrivateStateThread(); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1226 | error = DoAttachToProcessWithName (process_name, wait_for_launch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1227 | if (error.Fail()) |
| 1228 | { |
| 1229 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1230 | { |
| 1231 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1232 | const char *error_string = error.AsCString(); |
| 1233 | if (error_string == NULL) |
| 1234 | error_string = "attach failed"; |
| 1235 | |
| 1236 | SetExitStatus(-1, error_string); |
| 1237 | } |
| 1238 | } |
| 1239 | else |
| 1240 | { |
| 1241 | error = CompleteAttach(); |
| 1242 | } |
| 1243 | } |
| 1244 | return error; |
| 1245 | } |
| 1246 | |
| 1247 | Error |
| 1248 | Process::Resume () |
| 1249 | { |
| 1250 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 1251 | if (log) |
| 1252 | log->Printf("Process::Resume() m_stop_id = %u", m_stop_id); |
| 1253 | |
| 1254 | Error error (WillResume()); |
| 1255 | // Tell the process it is about to resume before the thread list |
| 1256 | if (error.Success()) |
| 1257 | { |
| 1258 | // Now let the thread list know we are about to resume to it |
| 1259 | // can let all of our threads know that they are about to be |
| 1260 | // resumed. Threads will each be called with |
| 1261 | // Thread::WillResume(StateType) where StateType contains the state |
| 1262 | // that they are supposed to have when the process is resumed |
| 1263 | // (suspended/running/stepping). Threads should also check |
| 1264 | // their resume signal in lldb::Thread::GetResumeSignal() |
| 1265 | // to see if they are suppoed to start back up with a signal. |
| 1266 | if (m_thread_list.WillResume()) |
| 1267 | { |
| 1268 | error = DoResume(); |
| 1269 | if (error.Success()) |
| 1270 | { |
| 1271 | DidResume(); |
| 1272 | m_thread_list.DidResume(); |
| 1273 | } |
| 1274 | } |
| 1275 | else |
| 1276 | { |
| 1277 | error.SetErrorStringWithFormat("thread list returned flase after WillResume"); |
| 1278 | } |
| 1279 | } |
| 1280 | return error; |
| 1281 | } |
| 1282 | |
| 1283 | Error |
| 1284 | Process::Halt () |
| 1285 | { |
| 1286 | Error error (WillHalt()); |
| 1287 | |
| 1288 | if (error.Success()) |
| 1289 | { |
| 1290 | error = DoHalt(); |
| 1291 | if (error.Success()) |
| 1292 | DidHalt(); |
| 1293 | } |
| 1294 | return error; |
| 1295 | } |
| 1296 | |
| 1297 | Error |
| 1298 | Process::Detach () |
| 1299 | { |
| 1300 | Error error (WillDetach()); |
| 1301 | |
| 1302 | if (error.Success()) |
| 1303 | { |
| 1304 | DisableAllBreakpointSites(); |
| 1305 | error = DoDetach(); |
| 1306 | if (error.Success()) |
| 1307 | { |
| 1308 | DidDetach(); |
| 1309 | StopPrivateStateThread(); |
| 1310 | } |
| 1311 | } |
| 1312 | return error; |
| 1313 | } |
| 1314 | |
| 1315 | Error |
| 1316 | Process::Destroy () |
| 1317 | { |
| 1318 | Error error (WillDestroy()); |
| 1319 | if (error.Success()) |
| 1320 | { |
| 1321 | DisableAllBreakpointSites(); |
| 1322 | error = DoDestroy(); |
| 1323 | if (error.Success()) |
| 1324 | { |
| 1325 | DidDestroy(); |
| 1326 | StopPrivateStateThread(); |
| 1327 | } |
| 1328 | } |
| 1329 | return error; |
| 1330 | } |
| 1331 | |
| 1332 | Error |
| 1333 | Process::Signal (int signal) |
| 1334 | { |
| 1335 | Error error (WillSignal()); |
| 1336 | if (error.Success()) |
| 1337 | { |
| 1338 | error = DoSignal(signal); |
| 1339 | if (error.Success()) |
| 1340 | DidSignal(); |
| 1341 | } |
| 1342 | return error; |
| 1343 | } |
| 1344 | |
| 1345 | UnixSignals & |
| 1346 | Process::GetUnixSignals () |
| 1347 | { |
| 1348 | return m_unix_signals; |
| 1349 | } |
| 1350 | |
| 1351 | Target & |
| 1352 | Process::GetTarget () |
| 1353 | { |
| 1354 | return m_target; |
| 1355 | } |
| 1356 | |
| 1357 | const Target & |
| 1358 | Process::GetTarget () const |
| 1359 | { |
| 1360 | return m_target; |
| 1361 | } |
| 1362 | |
| 1363 | uint32_t |
| 1364 | Process::GetAddressByteSize() |
| 1365 | { |
| 1366 | return m_target.GetArchitecture().GetAddressByteSize(); |
| 1367 | } |
| 1368 | |
| 1369 | bool |
| 1370 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 1371 | { |
| 1372 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 1373 | bool return_value = true; |
| 1374 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS); |
| 1375 | |
| 1376 | switch (state) |
| 1377 | { |
| 1378 | case eStateAttaching: |
| 1379 | case eStateLaunching: |
| 1380 | case eStateDetached: |
| 1381 | case eStateExited: |
| 1382 | case eStateUnloaded: |
| 1383 | // These events indicate changes in the state of the debugging session, always report them. |
| 1384 | return_value = true; |
| 1385 | break; |
| 1386 | case eStateInvalid: |
| 1387 | // We stopped for no apparent reason, don't report it. |
| 1388 | return_value = false; |
| 1389 | break; |
| 1390 | case eStateRunning: |
| 1391 | case eStateStepping: |
| 1392 | // If we've started the target running, we handle the cases where we |
| 1393 | // are already running and where there is a transition from stopped to |
| 1394 | // running differently. |
| 1395 | // running -> running: Automatically suppress extra running events |
| 1396 | // stopped -> running: Report except when there is one or more no votes |
| 1397 | // and no yes votes. |
| 1398 | SynchronouslyNotifyStateChanged (state); |
| 1399 | switch (m_public_state.GetValue()) |
| 1400 | { |
| 1401 | case eStateRunning: |
| 1402 | case eStateStepping: |
| 1403 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 1404 | return_value = false; |
| 1405 | break; |
| 1406 | default: |
| 1407 | // TODO: make this work correctly. For now always report |
| 1408 | // run if we aren't running so we don't miss any runnning |
| 1409 | // events. If I run the lldb/test/thread/a.out file and |
| 1410 | // break at main.cpp:58, run and hit the breakpoints on |
| 1411 | // multiple threads, then somehow during the stepping over |
| 1412 | // of all breakpoints no run gets reported. |
| 1413 | return_value = true; |
| 1414 | |
| 1415 | // This is a transition from stop to run. |
| 1416 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 1417 | { |
| 1418 | case eVoteYes: |
| 1419 | case eVoteNoOpinion: |
| 1420 | return_value = true; |
| 1421 | break; |
| 1422 | case eVoteNo: |
| 1423 | return_value = false; |
| 1424 | break; |
| 1425 | } |
| 1426 | break; |
| 1427 | } |
| 1428 | break; |
| 1429 | case eStateStopped: |
| 1430 | case eStateCrashed: |
| 1431 | case eStateSuspended: |
| 1432 | { |
| 1433 | // We've stopped. First see if we're going to restart the target. |
| 1434 | // If we are going to stop, then we always broadcast the event. |
| 1435 | // If we aren't going to stop, let the thread plans decide if we're going to report this event. |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 1436 | // If no thread has an opinion, we don't report it. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1437 | if (state != eStateInvalid) |
| 1438 | { |
| 1439 | |
| 1440 | RefreshStateAfterStop (); |
| 1441 | |
| 1442 | if (m_thread_list.ShouldStop (event_ptr) == false) |
| 1443 | { |
| 1444 | switch (m_thread_list.ShouldReportStop (event_ptr)) |
| 1445 | { |
| 1446 | case eVoteYes: |
| 1447 | Process::ProcessEventData::SetRestartedInEvent (event_ptr, true); |
Johnny Chen | 028784b | 2010-10-14 00:54:32 +0000 | [diff] [blame] | 1448 | // Intentional fall-through here. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1449 | case eVoteNoOpinion: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1450 | case eVoteNo: |
| 1451 | return_value = false; |
| 1452 | break; |
| 1453 | } |
| 1454 | |
| 1455 | if (log) |
| 1456 | log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process", event_ptr, StateAsCString(state)); |
| 1457 | Resume (); |
| 1458 | } |
| 1459 | else |
| 1460 | { |
| 1461 | return_value = true; |
| 1462 | SynchronouslyNotifyStateChanged (state); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | if (log) |
| 1469 | log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO"); |
| 1470 | return return_value; |
| 1471 | } |
| 1472 | |
| 1473 | //------------------------------------------------------------------ |
| 1474 | // Thread Queries |
| 1475 | //------------------------------------------------------------------ |
| 1476 | |
| 1477 | ThreadList & |
| 1478 | Process::GetThreadList () |
| 1479 | { |
| 1480 | return m_thread_list; |
| 1481 | } |
| 1482 | |
| 1483 | const ThreadList & |
| 1484 | Process::GetThreadList () const |
| 1485 | { |
| 1486 | return m_thread_list; |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | bool |
| 1491 | Process::StartPrivateStateThread () |
| 1492 | { |
| 1493 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS); |
| 1494 | |
| 1495 | if (log) |
| 1496 | log->Printf ("Process::%s ( )", __FUNCTION__); |
| 1497 | |
| 1498 | // Create a thread that watches our internal state and controls which |
| 1499 | // events make it to clients (into the DCProcess event queue). |
| 1500 | m_private_state_thread = Host::ThreadCreate ("<lldb.process.internal-state>", Process::PrivateStateThread, this, NULL); |
| 1501 | return m_private_state_thread != LLDB_INVALID_HOST_THREAD; |
| 1502 | } |
| 1503 | |
| 1504 | void |
| 1505 | Process::PausePrivateStateThread () |
| 1506 | { |
| 1507 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 1508 | } |
| 1509 | |
| 1510 | void |
| 1511 | Process::ResumePrivateStateThread () |
| 1512 | { |
| 1513 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 1514 | } |
| 1515 | |
| 1516 | void |
| 1517 | Process::StopPrivateStateThread () |
| 1518 | { |
| 1519 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
| 1520 | } |
| 1521 | |
| 1522 | void |
| 1523 | Process::ControlPrivateStateThread (uint32_t signal) |
| 1524 | { |
| 1525 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS); |
| 1526 | |
| 1527 | assert (signal == eBroadcastInternalStateControlStop || |
| 1528 | signal == eBroadcastInternalStateControlPause || |
| 1529 | signal == eBroadcastInternalStateControlResume); |
| 1530 | |
| 1531 | if (log) |
| 1532 | log->Printf ("Process::%s ( ) - signal: %d", __FUNCTION__, signal); |
| 1533 | |
| 1534 | // Signal the private state thread |
| 1535 | if (m_private_state_thread != LLDB_INVALID_HOST_THREAD) |
| 1536 | { |
| 1537 | TimeValue timeout_time; |
| 1538 | bool timed_out; |
| 1539 | |
| 1540 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 1541 | |
| 1542 | timeout_time = TimeValue::Now(); |
| 1543 | timeout_time.OffsetWithSeconds(2); |
| 1544 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 1545 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 1546 | |
| 1547 | if (signal == eBroadcastInternalStateControlStop) |
| 1548 | { |
| 1549 | if (timed_out) |
| 1550 | Host::ThreadCancel (m_private_state_thread, NULL); |
| 1551 | |
| 1552 | thread_result_t result = NULL; |
| 1553 | Host::ThreadJoin (m_private_state_thread, &result, NULL); |
Greg Clayton | c607d86 | 2010-07-22 18:34:21 +0000 | [diff] [blame] | 1554 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | void |
| 1560 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 1561 | { |
| 1562 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 1563 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1564 | // See if we should broadcast this state to external clients? |
| 1565 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
| 1566 | if (log) |
| 1567 | log->Printf ("Process::%s (arg = %p, pid = %i) got event '%s' broadcast = %s", __FUNCTION__, this, GetID(), StateAsCString(internal_state), should_broadcast ? "yes" : "no"); |
| 1568 | |
| 1569 | if (should_broadcast) |
| 1570 | { |
| 1571 | if (log) |
| 1572 | { |
| 1573 | log->Printf ("\tChanging public state from: %s to %s", StateAsCString(GetState ()), StateAsCString (internal_state)); |
| 1574 | } |
| 1575 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
| 1576 | BroadcastEvent (event_sp); |
| 1577 | } |
| 1578 | else |
| 1579 | { |
| 1580 | if (log) |
| 1581 | { |
| 1582 | log->Printf ("\tNot changing public state with event: %s", StateAsCString (internal_state)); |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | void * |
| 1588 | Process::PrivateStateThread (void *arg) |
| 1589 | { |
| 1590 | Process *proc = static_cast<Process*> (arg); |
| 1591 | void *result = proc->RunPrivateStateThread (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1592 | return result; |
| 1593 | } |
| 1594 | |
| 1595 | void * |
| 1596 | Process::RunPrivateStateThread () |
| 1597 | { |
| 1598 | bool control_only = false; |
| 1599 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 1600 | |
| 1601 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 1602 | if (log) |
| 1603 | log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID()); |
| 1604 | |
| 1605 | bool exit_now = false; |
| 1606 | while (!exit_now) |
| 1607 | { |
| 1608 | EventSP event_sp; |
| 1609 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 1610 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 1611 | { |
| 1612 | switch (event_sp->GetType()) |
| 1613 | { |
| 1614 | case eBroadcastInternalStateControlStop: |
| 1615 | exit_now = true; |
| 1616 | continue; // Go to next loop iteration so we exit without |
| 1617 | break; // doing any internal state managment below |
| 1618 | |
| 1619 | case eBroadcastInternalStateControlPause: |
| 1620 | control_only = true; |
| 1621 | break; |
| 1622 | |
| 1623 | case eBroadcastInternalStateControlResume: |
| 1624 | control_only = false; |
| 1625 | break; |
| 1626 | } |
| 1627 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1632 | |
| 1633 | if (internal_state != eStateInvalid) |
| 1634 | { |
| 1635 | HandlePrivateEvent (event_sp); |
| 1636 | } |
| 1637 | |
| 1638 | if (internal_state == eStateInvalid || internal_state == eStateExited) |
| 1639 | break; |
| 1640 | } |
| 1641 | |
| 1642 | if (log) |
| 1643 | log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID()); |
| 1644 | |
Greg Clayton | 8b4c16e | 2010-08-19 21:50:06 +0000 | [diff] [blame] | 1645 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1646 | return NULL; |
| 1647 | } |
| 1648 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1649 | //------------------------------------------------------------------ |
| 1650 | // Process Event Data |
| 1651 | //------------------------------------------------------------------ |
| 1652 | |
| 1653 | Process::ProcessEventData::ProcessEventData () : |
| 1654 | EventData (), |
| 1655 | m_process_sp (), |
| 1656 | m_state (eStateInvalid), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1657 | m_restarted (false), |
| 1658 | m_update_state (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1659 | { |
| 1660 | } |
| 1661 | |
| 1662 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 1663 | EventData (), |
| 1664 | m_process_sp (process_sp), |
| 1665 | m_state (state), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1666 | m_restarted (false), |
| 1667 | m_update_state (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1668 | { |
| 1669 | } |
| 1670 | |
| 1671 | Process::ProcessEventData::~ProcessEventData() |
| 1672 | { |
| 1673 | } |
| 1674 | |
| 1675 | const ConstString & |
| 1676 | Process::ProcessEventData::GetFlavorString () |
| 1677 | { |
| 1678 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 1679 | return g_flavor; |
| 1680 | } |
| 1681 | |
| 1682 | const ConstString & |
| 1683 | Process::ProcessEventData::GetFlavor () const |
| 1684 | { |
| 1685 | return ProcessEventData::GetFlavorString (); |
| 1686 | } |
| 1687 | |
| 1688 | const ProcessSP & |
| 1689 | Process::ProcessEventData::GetProcessSP () const |
| 1690 | { |
| 1691 | return m_process_sp; |
| 1692 | } |
| 1693 | |
| 1694 | StateType |
| 1695 | Process::ProcessEventData::GetState () const |
| 1696 | { |
| 1697 | return m_state; |
| 1698 | } |
| 1699 | |
| 1700 | bool |
| 1701 | Process::ProcessEventData::GetRestarted () const |
| 1702 | { |
| 1703 | return m_restarted; |
| 1704 | } |
| 1705 | |
| 1706 | void |
| 1707 | Process::ProcessEventData::SetRestarted (bool new_value) |
| 1708 | { |
| 1709 | m_restarted = new_value; |
| 1710 | } |
| 1711 | |
| 1712 | void |
| 1713 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 1714 | { |
| 1715 | // This function gets called twice for each event, once when the event gets pulled |
| 1716 | // off of the private process event queue, and once when it gets pulled off of |
| 1717 | // the public event queue. m_update_state is used to distinguish these |
| 1718 | // two cases; it is false when we're just pulling it off for private handling, |
| 1719 | // and we don't want to do the breakpoint command handling then. |
| 1720 | |
| 1721 | if (!m_update_state) |
| 1722 | return; |
| 1723 | |
| 1724 | m_process_sp->SetPublicState (m_state); |
| 1725 | |
| 1726 | // If we're stopped and haven't restarted, then do the breakpoint commands here: |
| 1727 | if (m_state == eStateStopped && ! m_restarted) |
| 1728 | { |
| 1729 | int num_threads = m_process_sp->GetThreadList().GetSize(); |
| 1730 | int idx; |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1731 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1732 | for (idx = 0; idx < num_threads; ++idx) |
| 1733 | { |
| 1734 | lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx); |
| 1735 | |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1736 | StopInfo *stop_info = thread_sp->GetStopInfo (); |
| 1737 | if (stop_info) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1738 | { |
Jim Ingham | 6fb8baa | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 1739 | stop_info->PerformAction(event_ptr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1742 | |
Jim Ingham | 6fb8baa | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 1743 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 1744 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 1745 | // that state appropriately. |
| 1746 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1747 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 1748 | SetRestarted(true); |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | void |
| 1753 | Process::ProcessEventData::Dump (Stream *s) const |
| 1754 | { |
| 1755 | if (m_process_sp) |
| 1756 | s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID()); |
| 1757 | |
| 1758 | s->Printf("state = %s", StateAsCString(GetState()));; |
| 1759 | } |
| 1760 | |
| 1761 | const Process::ProcessEventData * |
| 1762 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 1763 | { |
| 1764 | if (event_ptr) |
| 1765 | { |
| 1766 | const EventData *event_data = event_ptr->GetData(); |
| 1767 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 1768 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 1769 | } |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | |
| 1773 | ProcessSP |
| 1774 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 1775 | { |
| 1776 | ProcessSP process_sp; |
| 1777 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 1778 | if (data) |
| 1779 | process_sp = data->GetProcessSP(); |
| 1780 | return process_sp; |
| 1781 | } |
| 1782 | |
| 1783 | StateType |
| 1784 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 1785 | { |
| 1786 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 1787 | if (data == NULL) |
| 1788 | return eStateInvalid; |
| 1789 | else |
| 1790 | return data->GetState(); |
| 1791 | } |
| 1792 | |
| 1793 | bool |
| 1794 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 1795 | { |
| 1796 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 1797 | if (data == NULL) |
| 1798 | return false; |
| 1799 | else |
| 1800 | return data->GetRestarted(); |
| 1801 | } |
| 1802 | |
| 1803 | void |
| 1804 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 1805 | { |
| 1806 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 1807 | if (data != NULL) |
| 1808 | data->SetRestarted(new_value); |
| 1809 | } |
| 1810 | |
| 1811 | bool |
| 1812 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 1813 | { |
| 1814 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 1815 | if (data) |
| 1816 | { |
| 1817 | data->SetUpdateStateOnRemoval(); |
| 1818 | return true; |
| 1819 | } |
| 1820 | return false; |
| 1821 | } |
| 1822 | |
| 1823 | void |
| 1824 | Process::ProcessEventData::SetUpdateStateOnRemoval() |
| 1825 | { |
| 1826 | m_update_state = true; |
| 1827 | } |
| 1828 | |
| 1829 | Target * |
| 1830 | Process::CalculateTarget () |
| 1831 | { |
| 1832 | return &m_target; |
| 1833 | } |
| 1834 | |
| 1835 | Process * |
| 1836 | Process::CalculateProcess () |
| 1837 | { |
| 1838 | return this; |
| 1839 | } |
| 1840 | |
| 1841 | Thread * |
| 1842 | Process::CalculateThread () |
| 1843 | { |
| 1844 | return NULL; |
| 1845 | } |
| 1846 | |
| 1847 | StackFrame * |
| 1848 | Process::CalculateStackFrame () |
| 1849 | { |
| 1850 | return NULL; |
| 1851 | } |
| 1852 | |
| 1853 | void |
Greg Clayton | a830adb | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 1854 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1855 | { |
| 1856 | exe_ctx.target = &m_target; |
| 1857 | exe_ctx.process = this; |
| 1858 | exe_ctx.thread = NULL; |
| 1859 | exe_ctx.frame = NULL; |
| 1860 | } |
| 1861 | |
| 1862 | lldb::ProcessSP |
| 1863 | Process::GetSP () |
| 1864 | { |
| 1865 | return GetTarget().GetProcessSP(); |
| 1866 | } |
| 1867 | |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 1868 | ClangPersistentVariables & |
| 1869 | Process::GetPersistentVariables() |
| 1870 | { |
| 1871 | return m_persistent_vars; |
| 1872 | } |
| 1873 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1874 | uint32_t |
| 1875 | Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 1876 | { |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
| 1880 | ArchSpec |
| 1881 | Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 1882 | { |
| 1883 | return Host::GetArchSpecForExistingProcess (pid); |
| 1884 | } |
| 1885 | |
| 1886 | ArchSpec |
| 1887 | Process::GetArchSpecForExistingProcess (const char *process_name) |
| 1888 | { |
| 1889 | return Host::GetArchSpecForExistingProcess (process_name); |
| 1890 | } |
| 1891 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1892 | lldb::UserSettingsControllerSP |
| 1893 | Process::GetSettingsController (bool finish) |
| 1894 | { |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 1895 | static UserSettingsControllerSP g_settings_controller (new SettingsController); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1896 | static bool initialized = false; |
| 1897 | |
| 1898 | if (!initialized) |
| 1899 | { |
Jim Ingham | 7ac83bd | 2010-09-07 20:27:09 +0000 | [diff] [blame] | 1900 | initialized = UserSettingsController::InitializeSettingsController (g_settings_controller, |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 1901 | Process::SettingsController::global_settings_table, |
| 1902 | Process::SettingsController::instance_settings_table); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | if (finish) |
| 1906 | { |
Jim Ingham | 7ac83bd | 2010-09-07 20:27:09 +0000 | [diff] [blame] | 1907 | UserSettingsController::FinalizeSettingsController (g_settings_controller); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1908 | g_settings_controller.reset(); |
Jim Ingham | 7ac83bd | 2010-09-07 20:27:09 +0000 | [diff] [blame] | 1909 | initialized = false; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | return g_settings_controller; |
| 1913 | } |
| 1914 | |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 1915 | void |
| 1916 | Process::UpdateInstanceName () |
| 1917 | { |
| 1918 | ModuleSP module_sp = GetTarget().GetExecutableModule(); |
| 1919 | if (module_sp) |
| 1920 | { |
| 1921 | StreamString sstr; |
| 1922 | sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString()); |
| 1923 | |
| 1924 | Process::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), |
| 1925 | sstr.GetData()); |
| 1926 | } |
| 1927 | } |
| 1928 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1929 | //-------------------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 1930 | // class Process::SettingsController |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1931 | //-------------------------------------------------------------- |
| 1932 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 1933 | Process::SettingsController::SettingsController () : |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1934 | UserSettingsController ("process", Target::GetSettingsController()) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1935 | { |
Caroline Tice | 004afcb | 2010-09-08 17:48:55 +0000 | [diff] [blame] | 1936 | m_default_settings.reset (new ProcessInstanceSettings (*this, false, |
| 1937 | InstanceSettings::GetDefaultName().AsCString())); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 1940 | Process::SettingsController::~SettingsController () |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1941 | { |
| 1942 | } |
| 1943 | |
| 1944 | lldb::InstanceSettingsSP |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 1945 | Process::SettingsController::CreateInstanceSettings (const char *instance_name) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1946 | { |
Caroline Tice | 004afcb | 2010-09-08 17:48:55 +0000 | [diff] [blame] | 1947 | ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*(Process::GetSettingsController().get()), |
| 1948 | false, instance_name); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1949 | lldb::InstanceSettingsSP new_settings_sp (new_settings); |
| 1950 | return new_settings_sp; |
| 1951 | } |
| 1952 | |
| 1953 | //-------------------------------------------------------------- |
| 1954 | // class ProcessInstanceSettings |
| 1955 | //-------------------------------------------------------------- |
| 1956 | |
Caroline Tice | 004afcb | 2010-09-08 17:48:55 +0000 | [diff] [blame] | 1957 | ProcessInstanceSettings::ProcessInstanceSettings (UserSettingsController &owner, bool live_instance, |
| 1958 | const char *name) : |
Caroline Tice | 75b11a3 | 2010-09-16 19:05:55 +0000 | [diff] [blame] | 1959 | InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance), |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1960 | m_run_args (), |
| 1961 | m_env_vars (), |
| 1962 | m_input_path (), |
| 1963 | m_output_path (), |
| 1964 | m_error_path (), |
| 1965 | m_plugin (), |
| 1966 | m_disable_aslr (true) |
| 1967 | { |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 1968 | // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called |
| 1969 | // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers. |
| 1970 | // For this reason it has to be called here, rather than in the initializer or in the parent constructor. |
Caroline Tice | 75b11a3 | 2010-09-16 19:05:55 +0000 | [diff] [blame] | 1971 | // This is true for CreateInstanceName() too. |
| 1972 | |
| 1973 | if (GetInstanceName () == InstanceSettings::InvalidName()) |
| 1974 | { |
| 1975 | ChangeInstanceName (std::string (CreateInstanceName().AsCString())); |
| 1976 | m_owner.RegisterInstanceSettings (this); |
| 1977 | } |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 1978 | |
| 1979 | if (live_instance) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1980 | { |
| 1981 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 1982 | CopyInstanceSettings (pending_settings,false); |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 1983 | //m_owner.RemovePendingSettings (m_instance_name); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) : |
| 1988 | InstanceSettings (*(Process::GetSettingsController().get()), CreateInstanceName().AsCString()), |
| 1989 | m_run_args (rhs.m_run_args), |
| 1990 | m_env_vars (rhs.m_env_vars), |
| 1991 | m_input_path (rhs.m_input_path), |
| 1992 | m_output_path (rhs.m_output_path), |
| 1993 | m_error_path (rhs.m_error_path), |
| 1994 | m_plugin (rhs.m_plugin), |
| 1995 | m_disable_aslr (rhs.m_disable_aslr) |
| 1996 | { |
| 1997 | if (m_instance_name != InstanceSettings::GetDefaultName()) |
| 1998 | { |
| 1999 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 2000 | CopyInstanceSettings (pending_settings,false); |
| 2001 | m_owner.RemovePendingSettings (m_instance_name); |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | ProcessInstanceSettings::~ProcessInstanceSettings () |
| 2006 | { |
| 2007 | } |
| 2008 | |
| 2009 | ProcessInstanceSettings& |
| 2010 | ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs) |
| 2011 | { |
| 2012 | if (this != &rhs) |
| 2013 | { |
| 2014 | m_run_args = rhs.m_run_args; |
| 2015 | m_env_vars = rhs.m_env_vars; |
| 2016 | m_input_path = rhs.m_input_path; |
| 2017 | m_output_path = rhs.m_output_path; |
| 2018 | m_error_path = rhs.m_error_path; |
| 2019 | m_plugin = rhs.m_plugin; |
| 2020 | m_disable_aslr = rhs.m_disable_aslr; |
| 2021 | } |
| 2022 | |
| 2023 | return *this; |
| 2024 | } |
| 2025 | |
| 2026 | |
| 2027 | void |
| 2028 | ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, |
| 2029 | const char *index_value, |
| 2030 | const char *value, |
| 2031 | const ConstString &instance_name, |
| 2032 | const SettingEntry &entry, |
| 2033 | lldb::VarSetOperationType op, |
| 2034 | Error &err, |
| 2035 | bool pending) |
| 2036 | { |
| 2037 | if (var_name == RunArgsVarName()) |
| 2038 | UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err); |
| 2039 | else if (var_name == EnvVarsVarName()) |
| 2040 | UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err); |
| 2041 | else if (var_name == InputPathVarName()) |
| 2042 | UserSettingsController::UpdateStringVariable (op, m_input_path, value, err); |
| 2043 | else if (var_name == OutputPathVarName()) |
| 2044 | UserSettingsController::UpdateStringVariable (op, m_output_path, value, err); |
| 2045 | else if (var_name == ErrorPathVarName()) |
| 2046 | UserSettingsController::UpdateStringVariable (op, m_error_path, value, err); |
| 2047 | else if (var_name == PluginVarName()) |
| 2048 | UserSettingsController::UpdateEnumVariable (entry.enum_values, (int *) &m_plugin, value, err); |
| 2049 | else if (var_name == DisableASLRVarName()) |
| 2050 | UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err); |
| 2051 | } |
| 2052 | |
| 2053 | void |
| 2054 | ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, |
| 2055 | bool pending) |
| 2056 | { |
| 2057 | if (new_settings.get() == NULL) |
| 2058 | return; |
| 2059 | |
| 2060 | ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get(); |
| 2061 | |
| 2062 | m_run_args = new_process_settings->m_run_args; |
| 2063 | m_env_vars = new_process_settings->m_env_vars; |
| 2064 | m_input_path = new_process_settings->m_input_path; |
| 2065 | m_output_path = new_process_settings->m_output_path; |
| 2066 | m_error_path = new_process_settings->m_error_path; |
| 2067 | m_plugin = new_process_settings->m_plugin; |
| 2068 | m_disable_aslr = new_process_settings->m_disable_aslr; |
| 2069 | } |
| 2070 | |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 2071 | bool |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2072 | ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, |
| 2073 | const ConstString &var_name, |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 2074 | StringList &value, |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 2075 | Error *err) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2076 | { |
| 2077 | if (var_name == RunArgsVarName()) |
| 2078 | { |
| 2079 | if (m_run_args.GetArgumentCount() > 0) |
Greg Clayton | c14069e | 2010-09-14 03:47:41 +0000 | [diff] [blame] | 2080 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2081 | for (int i = 0; i < m_run_args.GetArgumentCount(); ++i) |
| 2082 | value.AppendString (m_run_args.GetArgumentAtIndex (i)); |
Greg Clayton | c14069e | 2010-09-14 03:47:41 +0000 | [diff] [blame] | 2083 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2084 | } |
| 2085 | else if (var_name == EnvVarsVarName()) |
| 2086 | { |
| 2087 | if (m_env_vars.size() > 0) |
| 2088 | { |
| 2089 | std::map<std::string, std::string>::iterator pos; |
| 2090 | for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos) |
| 2091 | { |
| 2092 | StreamString value_str; |
| 2093 | value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str()); |
| 2094 | value.AppendString (value_str.GetData()); |
| 2095 | } |
| 2096 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2097 | } |
| 2098 | else if (var_name == InputPathVarName()) |
| 2099 | { |
| 2100 | value.AppendString (m_input_path.c_str()); |
| 2101 | } |
| 2102 | else if (var_name == OutputPathVarName()) |
| 2103 | { |
| 2104 | value.AppendString (m_output_path.c_str()); |
| 2105 | } |
| 2106 | else if (var_name == ErrorPathVarName()) |
| 2107 | { |
| 2108 | value.AppendString (m_error_path.c_str()); |
| 2109 | } |
| 2110 | else if (var_name == PluginVarName()) |
| 2111 | { |
| 2112 | value.AppendString (UserSettingsController::EnumToString (entry.enum_values, (int) m_plugin)); |
| 2113 | } |
| 2114 | else if (var_name == DisableASLRVarName()) |
| 2115 | { |
| 2116 | if (m_disable_aslr) |
| 2117 | value.AppendString ("true"); |
| 2118 | else |
| 2119 | value.AppendString ("false"); |
| 2120 | } |
| 2121 | else |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 2122 | { |
| 2123 | if (err) |
| 2124 | err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 2125 | return false; |
| 2126 | } |
| 2127 | return true; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2130 | const ConstString |
| 2131 | ProcessInstanceSettings::CreateInstanceName () |
| 2132 | { |
| 2133 | static int instance_count = 1; |
| 2134 | StreamString sstr; |
| 2135 | |
| 2136 | sstr.Printf ("process_%d", instance_count); |
| 2137 | ++instance_count; |
| 2138 | |
| 2139 | const ConstString ret_val (sstr.GetData()); |
| 2140 | return ret_val; |
| 2141 | } |
| 2142 | |
| 2143 | const ConstString & |
| 2144 | ProcessInstanceSettings::RunArgsVarName () |
| 2145 | { |
| 2146 | static ConstString run_args_var_name ("run-args"); |
| 2147 | |
| 2148 | return run_args_var_name; |
| 2149 | } |
| 2150 | |
| 2151 | const ConstString & |
| 2152 | ProcessInstanceSettings::EnvVarsVarName () |
| 2153 | { |
| 2154 | static ConstString env_vars_var_name ("env-vars"); |
| 2155 | |
| 2156 | return env_vars_var_name; |
| 2157 | } |
| 2158 | |
| 2159 | const ConstString & |
| 2160 | ProcessInstanceSettings::InputPathVarName () |
| 2161 | { |
| 2162 | static ConstString input_path_var_name ("input-path"); |
| 2163 | |
| 2164 | return input_path_var_name; |
| 2165 | } |
| 2166 | |
| 2167 | const ConstString & |
| 2168 | ProcessInstanceSettings::OutputPathVarName () |
| 2169 | { |
Caroline Tice | 8709723 | 2010-09-07 18:35:40 +0000 | [diff] [blame] | 2170 | static ConstString output_path_var_name ("output-path"); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2171 | |
| 2172 | return output_path_var_name; |
| 2173 | } |
| 2174 | |
| 2175 | const ConstString & |
| 2176 | ProcessInstanceSettings::ErrorPathVarName () |
| 2177 | { |
Caroline Tice | 8709723 | 2010-09-07 18:35:40 +0000 | [diff] [blame] | 2178 | static ConstString error_path_var_name ("error-path"); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2179 | |
| 2180 | return error_path_var_name; |
| 2181 | } |
| 2182 | |
| 2183 | const ConstString & |
| 2184 | ProcessInstanceSettings::PluginVarName () |
| 2185 | { |
| 2186 | static ConstString plugin_var_name ("plugin"); |
| 2187 | |
| 2188 | return plugin_var_name; |
| 2189 | } |
| 2190 | |
| 2191 | |
| 2192 | const ConstString & |
| 2193 | ProcessInstanceSettings::DisableASLRVarName () |
| 2194 | { |
| 2195 | static ConstString disable_aslr_var_name ("disable-aslr"); |
| 2196 | |
| 2197 | return disable_aslr_var_name; |
| 2198 | } |
| 2199 | |
| 2200 | |
| 2201 | //-------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2202 | // SettingsController Variable Tables |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2203 | //-------------------------------------------------- |
| 2204 | |
| 2205 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2206 | Process::SettingsController::global_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2207 | { |
| 2208 | //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"}, |
| 2209 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 2210 | }; |
| 2211 | |
| 2212 | |
| 2213 | lldb::OptionEnumValueElement |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2214 | Process::SettingsController::g_plugins[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2215 | { |
Caroline Tice | f2c330d | 2010-09-09 18:01:59 +0000 | [diff] [blame] | 2216 | { eMacosx, "process.macosx", "Use the native MacOSX debugger plugin" }, |
| 2217 | { eRemoteDebugger, "process.gdb-remote" , "Use the GDB Remote protocol based debugger plugin" }, |
| 2218 | { 0, NULL, NULL } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2219 | }; |
| 2220 | |
| 2221 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2222 | Process::SettingsController::instance_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2223 | { |
| 2224 | //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, |
| 2225 | { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." }, |
| 2226 | { "env-vars", eSetVarTypeDictionary, NULL, NULL, false, false, "A list of all the environment variables to be passed to the executable's environment, and their values." }, |
| 2227 | { "input-path", eSetVarTypeString, "/dev/stdin", NULL, false, false, "The file/path to be used by the executable program for reading its input." }, |
| 2228 | { "output-path", eSetVarTypeString, "/dev/stdout", NULL, false, false, "The file/path to be used by the executable program for writing its output." }, |
| 2229 | { "error-path", eSetVarTypeString, "/dev/stderr", NULL, false, false, "The file/path to be used by the executable program for writings its error messages." }, |
| 2230 | { "plugin", eSetVarTypeEnum, NULL , g_plugins, false, false, "The plugin to be used to run the process." }, |
| 2231 | { "disable-aslr", eSetVarTypeBool, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" }, |
| 2232 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 2233 | }; |
| 2234 | |
| 2235 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 2236 | |