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